You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1812 lines
47 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 Zachary T Welch *
  3. * zw@superlucidity.net *
  4. * *
  5. * Copyright (C) 2007,2008,2009 ร˜yvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2009 SoftPLC Corporation *
  9. * http://softplc.com *
  10. * dick@softplc.com *
  11. * *
  12. * Copyright (C) 2005 by Dominic Rath *
  13. * Dominic.Rath@gmx.de *
  14. * *
  15. * This program is free software; you can redistribute it and/or modify *
  16. * it under the terms of the GNU General Public License as published by *
  17. * the Free Software Foundation; either version 2 of the License, or *
  18. * (at your option) any later version. *
  19. * *
  20. * This program is distributed in the hope that it will be useful, *
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  23. * GNU General Public License for more details. *
  24. * *
  25. * You should have received a copy of the GNU General Public License *
  26. * along with this program; if not, write to the *
  27. * Free Software Foundation, Inc., *
  28. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "jtag.h"
  34. #include "interface.h"
  35. #include "transport.h"
  36. #ifdef HAVE_STRINGS_H
  37. #include <strings.h>
  38. #endif
  39. /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
  40. #include "svf/svf.h"
  41. #include "xsvf/xsvf.h"
  42. /// The number of JTAG queue flushes (for profiling and debugging purposes).
  43. static int jtag_flush_queue_count;
  44. // Sleep this # of ms after flushing the queue
  45. static int jtag_flush_queue_sleep = 0;
  46. static void jtag_add_scan_check(struct jtag_tap *active,
  47. void (*jtag_add_scan)(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state),
  48. int in_num_fields, struct scan_field *in_fields, tap_state_t state);
  49. /**
  50. * The jtag_error variable is set when an error occurs while executing
  51. * the queue. Application code may set this using jtag_set_error(),
  52. * when an error occurs during processing that should be reported during
  53. * jtag_execute_queue().
  54. *
  55. * The value is set and cleared, but never read by normal application code.
  56. *
  57. * This value is returned (and cleared) by jtag_execute_queue().
  58. */
  59. static int jtag_error = ERROR_OK;
  60. static const char *jtag_event_strings[] =
  61. {
  62. [JTAG_TRST_ASSERTED] = "TAP reset",
  63. [JTAG_TAP_EVENT_SETUP] = "TAP setup",
  64. [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
  65. [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
  66. };
  67. /*
  68. * JTAG adapters must initialize with TRST and SRST de-asserted
  69. * (they're negative logic, so that means *high*). But some
  70. * hardware doesn't necessarily work that way ... so set things
  71. * up so that jtag_init() always forces that state.
  72. */
  73. static int jtag_trst = -1;
  74. static int jtag_srst = -1;
  75. /**
  76. * List all TAPs that have been created.
  77. */
  78. static struct jtag_tap *__jtag_all_taps = NULL;
  79. /**
  80. * The number of TAPs in the __jtag_all_taps list, used to track the
  81. * assigned chain position to new TAPs
  82. */
  83. static unsigned jtag_num_taps = 0;
  84. static enum reset_types jtag_reset_config = RESET_NONE;
  85. tap_state_t cmd_queue_cur_state = TAP_RESET;
  86. static bool jtag_verify_capture_ir = true;
  87. static int jtag_verify = 1;
  88. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
  89. static int adapter_nsrst_delay = 0; /* default to no nSRST delay */
  90. static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
  91. static int adapter_nsrst_assert_width = 0; /* width of assertion */
  92. static int jtag_ntrst_assert_width = 0; /* width of assertion */
  93. /**
  94. * Contains a single callback along with a pointer that will be passed
  95. * when an event occurs.
  96. */
  97. struct jtag_event_callback {
  98. /// a event callback
  99. jtag_event_handler_t callback;
  100. /// the private data to pass to the callback
  101. void* priv;
  102. /// the next callback
  103. struct jtag_event_callback* next;
  104. };
  105. /* callbacks to inform high-level handlers about JTAG state changes */
  106. static struct jtag_event_callback *jtag_event_callbacks;
  107. /* speed in kHz*/
  108. static int speed_khz = 0;
  109. /* speed to fallback to when RCLK is requested but not supported */
  110. static int rclk_fallback_speed_khz = 0;
  111. static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
  112. static int jtag_speed = 0;
  113. static struct jtag_interface *jtag = NULL;
  114. const struct swd_driver *swd = NULL;
  115. /* configuration */
  116. struct jtag_interface *jtag_interface = NULL;
  117. void jtag_set_flush_queue_sleep(int ms)
  118. {
  119. jtag_flush_queue_sleep = ms;
  120. }
  121. void jtag_set_error(int error)
  122. {
  123. if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
  124. return;
  125. jtag_error = error;
  126. }
  127. int jtag_error_clear(void)
  128. {
  129. int temp = jtag_error;
  130. jtag_error = ERROR_OK;
  131. return temp;
  132. }
  133. /************/
  134. static bool jtag_poll = 1;
  135. bool is_jtag_poll_safe(void)
  136. {
  137. /* Polling can be disabled explicitly with set_enabled(false).
  138. * It is also implicitly disabled while TRST is active and
  139. * while SRST is gating the JTAG clock.
  140. */
  141. if (!jtag_poll || jtag_trst != 0)
  142. return false;
  143. return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
  144. }
  145. bool jtag_poll_get_enabled(void)
  146. {
  147. return jtag_poll;
  148. }
  149. void jtag_poll_set_enabled(bool value)
  150. {
  151. jtag_poll = value;
  152. }
  153. /************/
  154. struct jtag_tap *jtag_all_taps(void)
  155. {
  156. return __jtag_all_taps;
  157. };
  158. unsigned jtag_tap_count(void)
  159. {
  160. return jtag_num_taps;
  161. }
  162. unsigned jtag_tap_count_enabled(void)
  163. {
  164. struct jtag_tap *t = jtag_all_taps();
  165. unsigned n = 0;
  166. while (t)
  167. {
  168. if (t->enabled)
  169. n++;
  170. t = t->next_tap;
  171. }
  172. return n;
  173. }
  174. /// Append a new TAP to the chain of all taps.
  175. void jtag_tap_add(struct jtag_tap *t)
  176. {
  177. t->abs_chain_position = jtag_num_taps++;
  178. struct jtag_tap **tap = &__jtag_all_taps;
  179. while (*tap != NULL)
  180. tap = &(*tap)->next_tap;
  181. *tap = t;
  182. }
  183. /* returns a pointer to the n-th device in the scan chain */
  184. struct jtag_tap *jtag_tap_by_position(unsigned n)
  185. {
  186. struct jtag_tap *t = jtag_all_taps();
  187. while (t && n-- > 0)
  188. t = t->next_tap;
  189. return t;
  190. }
  191. struct jtag_tap *jtag_tap_by_string(const char *s)
  192. {
  193. /* try by name first */
  194. struct jtag_tap *t = jtag_all_taps();
  195. while (t)
  196. {
  197. if (0 == strcmp(t->dotted_name, s))
  198. return t;
  199. t = t->next_tap;
  200. }
  201. /* no tap found by name, so try to parse the name as a number */
  202. unsigned n;
  203. if (parse_uint(s, &n) != ERROR_OK)
  204. return NULL;
  205. /* FIXME remove this numeric fallback code late June 2010, along
  206. * with all info in the User's Guide that TAPs have numeric IDs.
  207. * Also update "scan_chain" output to not display the numbers.
  208. */
  209. t = jtag_tap_by_position(n);
  210. if (t)
  211. LOG_WARNING("Specify TAP '%s' by name, not number %u",
  212. t->dotted_name, n);
  213. return t;
  214. }
  215. struct jtag_tap* jtag_tap_next_enabled(struct jtag_tap* p)
  216. {
  217. p = p ? p->next_tap : jtag_all_taps();
  218. while (p)
  219. {
  220. if (p->enabled)
  221. return p;
  222. p = p->next_tap;
  223. }
  224. return NULL;
  225. }
  226. const char *jtag_tap_name(const struct jtag_tap *tap)
  227. {
  228. return (tap == NULL) ? "(unknown)" : tap->dotted_name;
  229. }
  230. int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
  231. {
  232. struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
  233. if (callback == NULL)
  234. {
  235. return ERROR_INVALID_ARGUMENTS;
  236. }
  237. if (*callbacks_p)
  238. {
  239. while ((*callbacks_p)->next)
  240. callbacks_p = &((*callbacks_p)->next);
  241. callbacks_p = &((*callbacks_p)->next);
  242. }
  243. (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
  244. (*callbacks_p)->callback = callback;
  245. (*callbacks_p)->priv = priv;
  246. (*callbacks_p)->next = NULL;
  247. return ERROR_OK;
  248. }
  249. int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
  250. {
  251. struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
  252. if (callback == NULL)
  253. {
  254. return ERROR_INVALID_ARGUMENTS;
  255. }
  256. while (*p)
  257. {
  258. if (((*p)->priv != priv) || ((*p)->callback != callback))
  259. {
  260. p = &(*p)->next;
  261. continue;
  262. }
  263. temp = *p;
  264. *p = (*p)->next;
  265. free(temp);
  266. }
  267. return ERROR_OK;
  268. }
  269. int jtag_call_event_callbacks(enum jtag_event event)
  270. {
  271. struct jtag_event_callback *callback = jtag_event_callbacks;
  272. LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
  273. while (callback)
  274. {
  275. struct jtag_event_callback *next;
  276. /* callback may remove itself */
  277. next = callback->next;
  278. callback->callback(event, callback->priv);
  279. callback = next;
  280. }
  281. return ERROR_OK;
  282. }
  283. static void jtag_checks(void)
  284. {
  285. assert(jtag_trst == 0);
  286. }
  287. static void jtag_prelude(tap_state_t state)
  288. {
  289. jtag_checks();
  290. assert(state != TAP_INVALID);
  291. cmd_queue_cur_state = state;
  292. }
  293. void jtag_alloc_in_value32(struct scan_field *field)
  294. {
  295. interface_jtag_alloc_in_value32(field);
  296. }
  297. void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
  298. tap_state_t state)
  299. {
  300. jtag_prelude(state);
  301. int retval = interface_jtag_add_ir_scan(active, in_fields, state);
  302. jtag_set_error(retval);
  303. }
  304. static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active, int dummy, const struct scan_field *in_fields,
  305. tap_state_t state)
  306. {
  307. jtag_add_ir_scan_noverify(active, in_fields, state);
  308. }
  309. void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
  310. {
  311. assert(state != TAP_RESET);
  312. if (jtag_verify && jtag_verify_capture_ir)
  313. {
  314. /* 8 x 32 bit id's is enough for all invocations */
  315. /* if we are to run a verification of the ir scan, we need to get the input back.
  316. * We may have to allocate space if the caller didn't ask for the input back.
  317. */
  318. in_fields->check_value = active->expected;
  319. in_fields->check_mask = active->expected_mask;
  320. jtag_add_scan_check(active, jtag_add_ir_scan_noverify_callback, 1, in_fields, state);
  321. } else
  322. {
  323. jtag_add_ir_scan_noverify(active, in_fields, state);
  324. }
  325. }
  326. void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
  327. tap_state_t state)
  328. {
  329. assert(out_bits != NULL);
  330. assert(state != TAP_RESET);
  331. jtag_prelude(state);
  332. int retval = interface_jtag_add_plain_ir_scan(
  333. num_bits, out_bits, in_bits, state);
  334. jtag_set_error(retval);
  335. }
  336. static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
  337. uint8_t *in_check_mask, int num_bits);
  338. static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  339. {
  340. return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
  341. }
  342. static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state),
  343. int in_num_fields, struct scan_field *in_fields, tap_state_t state)
  344. {
  345. for (int i = 0; i < in_num_fields; i++)
  346. {
  347. struct scan_field *field = &in_fields[i];
  348. field->allocated = 0;
  349. field->modified = 0;
  350. if (field->check_value || field->in_value)
  351. continue;
  352. interface_jtag_add_scan_check_alloc(field);
  353. field->modified = 1;
  354. }
  355. jtag_add_scan(active, in_num_fields, in_fields, state);
  356. for (int i = 0; i < in_num_fields; i++)
  357. {
  358. if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
  359. {
  360. /* this is synchronous for a minidriver */
  361. jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
  362. (jtag_callback_data_t)in_fields[i].check_value,
  363. (jtag_callback_data_t)in_fields[i].check_mask,
  364. (jtag_callback_data_t)in_fields[i].num_bits);
  365. }
  366. if (in_fields[i].allocated)
  367. {
  368. free(in_fields[i].in_value);
  369. }
  370. if (in_fields[i].modified)
  371. {
  372. in_fields[i].in_value = NULL;
  373. }
  374. }
  375. }
  376. void jtag_add_dr_scan_check(struct jtag_tap *active, int in_num_fields, struct scan_field *in_fields, tap_state_t state)
  377. {
  378. if (jtag_verify)
  379. {
  380. jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
  381. } else
  382. {
  383. jtag_add_dr_scan(active, in_num_fields, in_fields, state);
  384. }
  385. }
  386. void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields,
  387. tap_state_t state)
  388. {
  389. assert(state != TAP_RESET);
  390. jtag_prelude(state);
  391. int retval;
  392. retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
  393. jtag_set_error(retval);
  394. }
  395. void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
  396. tap_state_t state)
  397. {
  398. assert(out_bits != NULL);
  399. assert(state != TAP_RESET);
  400. jtag_prelude(state);
  401. int retval;
  402. retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
  403. jtag_set_error(retval);
  404. }
  405. void jtag_add_tlr(void)
  406. {
  407. jtag_prelude(TAP_RESET);
  408. jtag_set_error(interface_jtag_add_tlr());
  409. /* NOTE: order here matches TRST path in jtag_add_reset() */
  410. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  411. jtag_notify_event(JTAG_TRST_ASSERTED);
  412. }
  413. /**
  414. * If supported by the underlying adapter, this clocks a raw bit sequence
  415. * onto TMS for switching betwen JTAG and SWD modes.
  416. *
  417. * DO NOT use this to bypass the integrity checks and logging provided
  418. * by the jtag_add_pathmove() and jtag_add_statemove() calls.
  419. *
  420. * @param nbits How many bits to clock out.
  421. * @param seq The bit sequence. The LSB is bit 0 of seq[0].
  422. * @param state The JTAG tap state to record on completion. Use
  423. * TAP_INVALID to represent being in in SWD mode.
  424. *
  425. * @todo Update naming conventions to stop assuming everything is JTAG.
  426. */
  427. int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
  428. {
  429. int retval;
  430. if (!(jtag->supported & DEBUG_CAP_TMS_SEQ))
  431. return ERROR_JTAG_NOT_IMPLEMENTED;
  432. jtag_checks();
  433. cmd_queue_cur_state = state;
  434. retval = interface_add_tms_seq(nbits, seq, state);
  435. jtag_set_error(retval);
  436. return retval;
  437. }
  438. void jtag_add_pathmove(int num_states, const tap_state_t *path)
  439. {
  440. tap_state_t cur_state = cmd_queue_cur_state;
  441. /* the last state has to be a stable state */
  442. if (!tap_is_state_stable(path[num_states - 1]))
  443. {
  444. LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
  445. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  446. return;
  447. }
  448. for (int i = 0; i < num_states; i++)
  449. {
  450. if (path[i] == TAP_RESET)
  451. {
  452. LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
  453. jtag_set_error(ERROR_JTAG_STATE_INVALID);
  454. return;
  455. }
  456. if (tap_state_transition(cur_state, true) != path[i]
  457. && tap_state_transition(cur_state, false) != path[i])
  458. {
  459. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  460. tap_state_name(cur_state), tap_state_name(path[i]));
  461. jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
  462. return;
  463. }
  464. cur_state = path[i];
  465. }
  466. jtag_checks();
  467. jtag_set_error(interface_jtag_add_pathmove(num_states, path));
  468. cmd_queue_cur_state = path[num_states - 1];
  469. }
  470. int jtag_add_statemove(tap_state_t goal_state)
  471. {
  472. tap_state_t cur_state = cmd_queue_cur_state;
  473. if (goal_state != cur_state)
  474. {
  475. LOG_DEBUG("cur_state=%s goal_state=%s",
  476. tap_state_name(cur_state),
  477. tap_state_name(goal_state));
  478. }
  479. /* If goal is RESET, be paranoid and force that that transition
  480. * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
  481. */
  482. if (goal_state == TAP_RESET)
  483. jtag_add_tlr();
  484. else if (goal_state == cur_state)
  485. /* nothing to do */ ;
  486. else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
  487. {
  488. unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
  489. unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
  490. tap_state_t moves[8];
  491. assert(tms_count < ARRAY_SIZE(moves));
  492. for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
  493. {
  494. bool bit = tms_bits & 1;
  495. cur_state = tap_state_transition(cur_state, bit);
  496. moves[i] = cur_state;
  497. }
  498. jtag_add_pathmove(tms_count, moves);
  499. }
  500. else if (tap_state_transition(cur_state, true) == goal_state
  501. || tap_state_transition(cur_state, false) == goal_state)
  502. {
  503. jtag_add_pathmove(1, &goal_state);
  504. }
  505. else
  506. return ERROR_FAIL;
  507. return ERROR_OK;
  508. }
  509. void jtag_add_runtest(int num_cycles, tap_state_t state)
  510. {
  511. jtag_prelude(state);
  512. jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
  513. }
  514. void jtag_add_clocks(int num_cycles)
  515. {
  516. if (!tap_is_state_stable(cmd_queue_cur_state))
  517. {
  518. LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
  519. tap_state_name(cmd_queue_cur_state));
  520. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  521. return;
  522. }
  523. if (num_cycles > 0)
  524. {
  525. jtag_checks();
  526. jtag_set_error(interface_jtag_add_clocks(num_cycles));
  527. }
  528. }
  529. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  530. {
  531. int trst_with_tlr = 0;
  532. int new_srst = 0;
  533. int new_trst = 0;
  534. /* Without SRST, we must use target-specific JTAG operations
  535. * on each target; callers should not be requesting SRST when
  536. * that signal doesn't exist.
  537. *
  538. * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
  539. * can kick in even if the JTAG adapter can't drive TRST.
  540. */
  541. if (req_srst) {
  542. if (!(jtag_reset_config & RESET_HAS_SRST)) {
  543. LOG_ERROR("BUG: can't assert SRST");
  544. jtag_set_error(ERROR_FAIL);
  545. return;
  546. }
  547. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
  548. && !req_tlr_or_trst) {
  549. LOG_ERROR("BUG: can't assert only SRST");
  550. jtag_set_error(ERROR_FAIL);
  551. return;
  552. }
  553. new_srst = 1;
  554. }
  555. /* JTAG reset (entry to TAP_RESET state) can always be achieved
  556. * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
  557. * state first. TRST accelerates it, and bypasses those states.
  558. *
  559. * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
  560. * can kick in even if the JTAG adapter can't drive SRST.
  561. */
  562. if (req_tlr_or_trst) {
  563. if (!(jtag_reset_config & RESET_HAS_TRST))
  564. trst_with_tlr = 1;
  565. else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
  566. && !req_srst)
  567. trst_with_tlr = 1;
  568. else
  569. new_trst = 1;
  570. }
  571. /* Maybe change TRST and/or SRST signal state */
  572. if (jtag_srst != new_srst || jtag_trst != new_trst) {
  573. int retval;
  574. retval = interface_jtag_add_reset(new_trst, new_srst);
  575. if (retval != ERROR_OK)
  576. jtag_set_error(retval);
  577. else
  578. retval = jtag_execute_queue();
  579. if (retval != ERROR_OK) {
  580. LOG_ERROR("TRST/SRST error");
  581. return;
  582. }
  583. }
  584. /* SRST resets everything hooked up to that signal */
  585. if (jtag_srst != new_srst) {
  586. jtag_srst = new_srst;
  587. if (jtag_srst)
  588. {
  589. LOG_DEBUG("SRST line asserted");
  590. if (adapter_nsrst_assert_width)
  591. jtag_add_sleep(adapter_nsrst_assert_width * 1000);
  592. }
  593. else {
  594. LOG_DEBUG("SRST line released");
  595. if (adapter_nsrst_delay)
  596. jtag_add_sleep(adapter_nsrst_delay * 1000);
  597. }
  598. }
  599. /* Maybe enter the JTAG TAP_RESET state ...
  600. * - using only TMS, TCK, and the JTAG state machine
  601. * - or else more directly, using TRST
  602. *
  603. * TAP_RESET should be invisible to non-debug parts of the system.
  604. */
  605. if (trst_with_tlr) {
  606. LOG_DEBUG("JTAG reset with TLR instead of TRST");
  607. jtag_add_tlr();
  608. } else if (jtag_trst != new_trst) {
  609. jtag_trst = new_trst;
  610. if (jtag_trst) {
  611. LOG_DEBUG("TRST line asserted");
  612. tap_set_state(TAP_RESET);
  613. if (jtag_ntrst_assert_width)
  614. jtag_add_sleep(jtag_ntrst_assert_width * 1000);
  615. } else {
  616. LOG_DEBUG("TRST line released");
  617. if (jtag_ntrst_delay)
  618. jtag_add_sleep(jtag_ntrst_delay * 1000);
  619. /* We just asserted nTRST, so we're now in TAP_RESET.
  620. * Inform possible listeners about this, now that
  621. * JTAG instructions and data can be shifted. This
  622. * sequence must match jtag_add_tlr().
  623. */
  624. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  625. jtag_notify_event(JTAG_TRST_ASSERTED);
  626. }
  627. }
  628. }
  629. void jtag_add_sleep(uint32_t us)
  630. {
  631. /// @todo Here, keep_alive() appears to be a layering violation!!!
  632. keep_alive();
  633. jtag_set_error(interface_jtag_add_sleep(us));
  634. }
  635. static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
  636. uint8_t *in_check_mask, int num_bits)
  637. {
  638. int retval = ERROR_OK;
  639. int compare_failed;
  640. if (in_check_mask)
  641. compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
  642. else
  643. compare_failed = buf_cmp(captured, in_check_value, num_bits);
  644. if (compare_failed) {
  645. char *captured_str, *in_check_value_str;
  646. int bits = (num_bits > DEBUG_JTAG_IOZ)
  647. ? DEBUG_JTAG_IOZ
  648. : num_bits;
  649. /* NOTE: we've lost diagnostic context here -- 'which tap' */
  650. captured_str = buf_to_str(captured, bits, 16);
  651. in_check_value_str = buf_to_str(in_check_value, bits, 16);
  652. LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
  653. captured_str);
  654. LOG_WARNING(" check_value: 0x%s", in_check_value_str);
  655. free(captured_str);
  656. free(in_check_value_str);
  657. if (in_check_mask) {
  658. char *in_check_mask_str;
  659. in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
  660. LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
  661. free(in_check_mask_str);
  662. }
  663. retval = ERROR_JTAG_QUEUE_FAILED;
  664. }
  665. return retval;
  666. }
  667. void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
  668. {
  669. assert(field->in_value != NULL);
  670. if (value == NULL)
  671. {
  672. /* no checking to do */
  673. return;
  674. }
  675. jtag_execute_queue_noclear();
  676. int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
  677. jtag_set_error(retval);
  678. }
  679. int default_interface_jtag_execute_queue(void)
  680. {
  681. if (NULL == jtag)
  682. {
  683. LOG_ERROR("No JTAG interface configured yet. "
  684. "Issue 'init' command in startup scripts "
  685. "before communicating with targets.");
  686. return ERROR_FAIL;
  687. }
  688. return jtag->execute_queue();
  689. }
  690. void jtag_execute_queue_noclear(void)
  691. {
  692. jtag_flush_queue_count++;
  693. jtag_set_error(interface_jtag_execute_queue());
  694. if (jtag_flush_queue_sleep > 0)
  695. {
  696. /* For debug purposes it can be useful to test performance
  697. * or behavior when delaying after flushing the queue,
  698. * e.g. to simulate long roundtrip times.
  699. */
  700. usleep(jtag_flush_queue_sleep * 1000);
  701. }
  702. }
  703. int jtag_get_flush_queue_count(void)
  704. {
  705. return jtag_flush_queue_count;
  706. }
  707. int jtag_execute_queue(void)
  708. {
  709. jtag_execute_queue_noclear();
  710. return jtag_error_clear();
  711. }
  712. static int jtag_reset_callback(enum jtag_event event, void *priv)
  713. {
  714. struct jtag_tap *tap = priv;
  715. if (event == JTAG_TRST_ASSERTED)
  716. {
  717. tap->enabled = !tap->disabled_after_reset;
  718. /* current instruction is either BYPASS or IDCODE */
  719. buf_set_ones(tap->cur_instr, tap->ir_length);
  720. tap->bypass = 1;
  721. }
  722. return ERROR_OK;
  723. }
  724. /* sleep at least us microseconds. When we sleep more than 1000ms we
  725. * do an alive sleep, i.e. keep GDB alive. Note that we could starve
  726. * GDB if we slept for <1000ms many times.
  727. */
  728. void jtag_sleep(uint32_t us)
  729. {
  730. if (us < 1000)
  731. usleep(us);
  732. else
  733. alive_sleep((us+999)/1000);
  734. }
  735. /* Maximum number of enabled JTAG devices we expect in the scan chain,
  736. * plus one (to detect garbage at the end). Devices that don't support
  737. * IDCODE take up fewer bits, possibly allowing a few more devices.
  738. */
  739. #define JTAG_MAX_CHAIN_SIZE 20
  740. #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
  741. #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
  742. #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
  743. /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
  744. * know that no valid TAP will have it as an IDCODE value.
  745. */
  746. #define END_OF_CHAIN_FLAG 0x000000ff
  747. /* a larger IR length than we ever expect to autoprobe */
  748. #define JTAG_IRLEN_MAX 60
  749. static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
  750. {
  751. struct scan_field field = {
  752. .num_bits = num_idcode * 32,
  753. .out_value = idcode_buffer,
  754. .in_value = idcode_buffer,
  755. };
  756. // initialize to the end of chain ID value
  757. for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
  758. buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
  759. jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
  760. jtag_add_tlr();
  761. return jtag_execute_queue();
  762. }
  763. static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
  764. {
  765. uint8_t zero_check = 0x0;
  766. uint8_t one_check = 0xff;
  767. for (unsigned i = 0; i < count * 4; i++)
  768. {
  769. zero_check |= idcodes[i];
  770. one_check &= idcodes[i];
  771. }
  772. /* if there wasn't a single non-zero bit or if all bits were one,
  773. * the scan is not valid. We wrote a mix of both values; either
  774. *
  775. * - There's a hardware issue (almost certainly):
  776. * + all-zeroes can mean a target stuck in JTAG reset
  777. * + all-ones tends to mean no target
  778. * - The scan chain is WAY longer than we can handle, *AND* either
  779. * + there are several hundreds of TAPs in bypass, or
  780. * + at least a few dozen TAPs all have an all-ones IDCODE
  781. */
  782. if (zero_check == 0x00 || one_check == 0xff)
  783. {
  784. LOG_ERROR("JTAG scan chain interrogation failed: all %s",
  785. (zero_check == 0x00) ? "zeroes" : "ones");
  786. LOG_ERROR("Check JTAG interface, timings, target power, etc.");
  787. return false;
  788. }
  789. return true;
  790. }
  791. static void jtag_examine_chain_display(enum log_levels level, const char *msg,
  792. const char *name, uint32_t idcode)
  793. {
  794. log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
  795. "JTAG tap: %s %16.16s: 0x%08x "
  796. "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  797. name, msg,
  798. (unsigned int)idcode,
  799. (unsigned int)EXTRACT_MFG(idcode),
  800. (unsigned int)EXTRACT_PART(idcode),
  801. (unsigned int)EXTRACT_VER(idcode));
  802. }
  803. static bool jtag_idcode_is_final(uint32_t idcode)
  804. {
  805. /*
  806. * Some devices, such as AVR8, will output all 1's instead
  807. * of TDI input value at end of chain. Allow those values
  808. * instead of failing.
  809. */
  810. return idcode == END_OF_CHAIN_FLAG || idcode == 0xFFFFFFFF;
  811. }
  812. /**
  813. * This helper checks that remaining bits in the examined chain data are
  814. * all as expected, but a single JTAG device requires only 64 bits to be
  815. * read back correctly. This can help identify and diagnose problems
  816. * with the JTAG chain earlier, gives more helpful/explicit error messages.
  817. * Returns TRUE iff garbage was found.
  818. */
  819. static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
  820. {
  821. bool triggered = false;
  822. for (; count < max - 31; count += 32)
  823. {
  824. uint32_t idcode = buf_get_u32(idcodes, count, 32);
  825. /* do not trigger the warning if the data looks good */
  826. if (jtag_idcode_is_final(idcode))
  827. continue;
  828. LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
  829. count, (unsigned int)idcode);
  830. triggered = true;
  831. }
  832. return triggered;
  833. }
  834. static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
  835. {
  836. uint32_t idcode = tap->idcode;
  837. /* ignore expected BYPASS codes; warn otherwise */
  838. if (0 == tap->expected_ids_cnt && !idcode)
  839. return true;
  840. /* optionally ignore the JTAG version field */
  841. uint32_t mask = tap->ignore_version ? ~(0xff << 24) : ~0;
  842. idcode &= mask;
  843. /* Loop over the expected identification codes and test for a match */
  844. unsigned ii, limit = tap->expected_ids_cnt;
  845. for (ii = 0; ii < limit; ii++)
  846. {
  847. uint32_t expected = tap->expected_ids[ii] & mask;
  848. if (idcode == expected)
  849. return true;
  850. /* treat "-expected-id 0" as a "don't-warn" wildcard */
  851. if (0 == tap->expected_ids[ii])
  852. return true;
  853. }
  854. /* If none of the expected ids matched, warn */
  855. jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
  856. tap->dotted_name, tap->idcode);
  857. for (ii = 0; ii < limit; ii++)
  858. {
  859. char msg[32];
  860. snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, limit);
  861. jtag_examine_chain_display(LOG_LVL_ERROR, msg,
  862. tap->dotted_name, tap->expected_ids[ii]);
  863. }
  864. return false;
  865. }
  866. /* Try to examine chain layout according to IEEE 1149.1 ยง12
  867. * This is called a "blind interrogation" of the scan chain.
  868. */
  869. static int jtag_examine_chain(void)
  870. {
  871. uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
  872. unsigned bit_count;
  873. int retval;
  874. int tapcount = 0;
  875. bool autoprobe = false;
  876. /* DR scan to collect BYPASS or IDCODE register contents.
  877. * Then make sure the scan data has both ones and zeroes.
  878. */
  879. LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
  880. retval = jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
  881. if (retval != ERROR_OK)
  882. return retval;
  883. if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
  884. return ERROR_JTAG_INIT_FAILED;
  885. /* point at the 1st tap */
  886. struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
  887. if (!tap)
  888. autoprobe = true;
  889. for (bit_count = 0;
  890. tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
  891. tap = jtag_tap_next_enabled(tap))
  892. {
  893. uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  894. if ((idcode & 1) == 0)
  895. {
  896. /* Zero for LSB indicates a device in bypass */
  897. LOG_INFO("TAP %s does not have IDCODE",
  898. tap->dotted_name);
  899. idcode = 0;
  900. tap->hasidcode = false;
  901. bit_count += 1;
  902. }
  903. else
  904. {
  905. /* Friendly devices support IDCODE */
  906. tap->hasidcode = true;
  907. jtag_examine_chain_display(LOG_LVL_INFO,
  908. "tap/device found",
  909. tap->dotted_name, idcode);
  910. bit_count += 32;
  911. }
  912. tap->idcode = idcode;
  913. /* ensure the TAP ID matches what was expected */
  914. if (!jtag_examine_chain_match_tap(tap))
  915. retval = ERROR_JTAG_INIT_SOFT_FAIL;
  916. }
  917. /* Fail if too many TAPs were enabled for us to verify them all. */
  918. if (tap) {
  919. LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
  920. tap->dotted_name);
  921. return ERROR_JTAG_INIT_FAILED;
  922. }
  923. /* if autoprobing, the tap list is still empty ... populate it! */
  924. while (autoprobe && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31) {
  925. uint32_t idcode;
  926. char buf[12];
  927. /* Is there another TAP? */
  928. idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  929. if (jtag_idcode_is_final(idcode))
  930. break;
  931. /* Default everything in this TAP except IR length.
  932. *
  933. * REVISIT create a jtag_alloc(chip, tap) routine, and
  934. * share it with jim_newtap_cmd().
  935. */
  936. tap = calloc(1, sizeof *tap);
  937. if (!tap)
  938. return ERROR_FAIL;
  939. sprintf(buf, "auto%d", tapcount++);
  940. tap->chip = strdup(buf);
  941. tap->tapname = strdup("tap");
  942. sprintf(buf, "%s.%s", tap->chip, tap->tapname);
  943. tap->dotted_name = strdup(buf);
  944. /* tap->ir_length == 0 ... signifying irlen autoprobe */
  945. tap->ir_capture_mask = 0x03;
  946. tap->ir_capture_value = 0x01;
  947. tap->enabled = true;
  948. if ((idcode & 1) == 0) {
  949. bit_count += 1;
  950. tap->hasidcode = false;
  951. } else {
  952. bit_count += 32;
  953. tap->hasidcode = true;
  954. tap->idcode = idcode;
  955. tap->expected_ids_cnt = 1;
  956. tap->expected_ids = malloc(sizeof(uint32_t));
  957. tap->expected_ids[0] = idcode;
  958. }
  959. LOG_WARNING("AUTO %s - use \"jtag newtap "
  960. "%s %s -expected-id 0x%8.8" PRIx32 " ...\"",
  961. tap->dotted_name, tap->chip, tap->tapname,
  962. tap->idcode);
  963. jtag_tap_init(tap);
  964. }
  965. /* After those IDCODE or BYPASS register values should be
  966. * only the data we fed into the scan chain.
  967. */
  968. if (jtag_examine_chain_end(idcode_buffer, bit_count,
  969. 8 * sizeof(idcode_buffer))) {
  970. LOG_ERROR("double-check your JTAG setup (interface, "
  971. "speed, missing TAPs, ...)");
  972. return ERROR_JTAG_INIT_FAILED;
  973. }
  974. /* Return success or, for backwards compatibility if only
  975. * some IDCODE values mismatched, a soft/continuable fault.
  976. */
  977. return retval;
  978. }
  979. /*
  980. * Validate the date loaded by entry to the Capture-IR state, to help
  981. * find errors related to scan chain configuration (wrong IR lengths)
  982. * or communication.
  983. *
  984. * Entry state can be anything. On non-error exit, all TAPs are in
  985. * bypass mode. On error exits, the scan chain is reset.
  986. */
  987. static int jtag_validate_ircapture(void)
  988. {
  989. struct jtag_tap *tap;
  990. int total_ir_length = 0;
  991. uint8_t *ir_test = NULL;
  992. struct scan_field field;
  993. int val;
  994. int chain_pos = 0;
  995. int retval;
  996. /* when autoprobing, accomodate huge IR lengths */
  997. for (tap = NULL, total_ir_length = 0;
  998. (tap = jtag_tap_next_enabled(tap)) != NULL;
  999. total_ir_length += tap->ir_length) {
  1000. if (tap->ir_length == 0)
  1001. total_ir_length += JTAG_IRLEN_MAX;
  1002. }
  1003. /* increase length to add 2 bit sentinel after scan */
  1004. total_ir_length += 2;
  1005. ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
  1006. if (ir_test == NULL)
  1007. return ERROR_FAIL;
  1008. /* after this scan, all TAPs will capture BYPASS instructions */
  1009. buf_set_ones(ir_test, total_ir_length);
  1010. field.num_bits = total_ir_length;
  1011. field.out_value = ir_test;
  1012. field.in_value = ir_test;
  1013. jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
  1014. LOG_DEBUG("IR capture validation scan");
  1015. retval = jtag_execute_queue();
  1016. if (retval != ERROR_OK)
  1017. goto done;
  1018. tap = NULL;
  1019. chain_pos = 0;
  1020. for (;;) {
  1021. tap = jtag_tap_next_enabled(tap);
  1022. if (tap == NULL) {
  1023. break;
  1024. }
  1025. /* If we're autoprobing, guess IR lengths. They must be at
  1026. * least two bits. Guessing will fail if (a) any TAP does
  1027. * not conform to the JTAG spec; or (b) when the upper bits
  1028. * captured from some conforming TAP are nonzero. Or if
  1029. * (c) an IR length is longer than 32 bits -- which is only
  1030. * an implementation limit, which could someday be raised.
  1031. *
  1032. * REVISIT optimization: if there's a *single* TAP we can
  1033. * lift restrictions (a) and (b) by scanning a recognizable
  1034. * pattern before the all-ones BYPASS. Check for where the
  1035. * pattern starts in the result, instead of an 0...01 value.
  1036. *
  1037. * REVISIT alternative approach: escape to some tcl code
  1038. * which could provide more knowledge, based on IDCODE; and
  1039. * only guess when that has no success.
  1040. */
  1041. if (tap->ir_length == 0) {
  1042. tap->ir_length = 2;
  1043. while ((val = buf_get_u32(ir_test, chain_pos,
  1044. tap->ir_length + 1)) == 1
  1045. && tap->ir_length <= 32) {
  1046. tap->ir_length++;
  1047. }
  1048. LOG_WARNING("AUTO %s - use \"... -irlen %d\"",
  1049. jtag_tap_name(tap), tap->ir_length);
  1050. }
  1051. /* Validate the two LSBs, which must be 01 per JTAG spec.
  1052. *
  1053. * Or ... more bits could be provided by TAP declaration.
  1054. * Plus, some taps (notably in i.MX series chips) violate
  1055. * this part of the JTAG spec, so their capture mask/value
  1056. * attributes might disable this test.
  1057. */
  1058. val = buf_get_u32(ir_test, chain_pos, tap->ir_length);
  1059. if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
  1060. LOG_ERROR("%s: IR capture error; saw 0x%0*x not 0x%0*x",
  1061. jtag_tap_name(tap),
  1062. (tap->ir_length + 7) / tap->ir_length,
  1063. val,
  1064. (tap->ir_length + 7) / tap->ir_length,
  1065. (unsigned) tap->ir_capture_value);
  1066. retval = ERROR_JTAG_INIT_FAILED;
  1067. goto done;
  1068. }
  1069. LOG_DEBUG("%s: IR capture 0x%0*x", jtag_tap_name(tap),
  1070. (tap->ir_length + 7) / tap->ir_length, val);
  1071. chain_pos += tap->ir_length;
  1072. }
  1073. /* verify the '11' sentinel we wrote is returned at the end */
  1074. val = buf_get_u32(ir_test, chain_pos, 2);
  1075. if (val != 0x3)
  1076. {
  1077. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  1078. LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
  1079. chain_pos, cbuf);
  1080. free(cbuf);
  1081. retval = ERROR_JTAG_INIT_FAILED;
  1082. }
  1083. done:
  1084. free(ir_test);
  1085. if (retval != ERROR_OK) {
  1086. jtag_add_tlr();
  1087. jtag_execute_queue();
  1088. }
  1089. return retval;
  1090. }
  1091. void jtag_tap_init(struct jtag_tap *tap)
  1092. {
  1093. unsigned ir_len_bits;
  1094. unsigned ir_len_bytes;
  1095. /* if we're autoprobing, cope with potentially huge ir_length */
  1096. ir_len_bits = tap->ir_length ? : JTAG_IRLEN_MAX;
  1097. ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
  1098. tap->expected = calloc(1, ir_len_bytes);
  1099. tap->expected_mask = calloc(1, ir_len_bytes);
  1100. tap->cur_instr = malloc(ir_len_bytes);
  1101. /// @todo cope better with ir_length bigger than 32 bits
  1102. if (ir_len_bits > 32)
  1103. ir_len_bits = 32;
  1104. buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
  1105. buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
  1106. // TAP will be in bypass mode after jtag_validate_ircapture()
  1107. tap->bypass = 1;
  1108. buf_set_ones(tap->cur_instr, tap->ir_length);
  1109. // register the reset callback for the TAP
  1110. jtag_register_event_callback(&jtag_reset_callback, tap);
  1111. LOG_DEBUG("Created Tap: %s @ abs position %d, "
  1112. "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
  1113. tap->abs_chain_position, tap->ir_length,
  1114. (unsigned) tap->ir_capture_value,
  1115. (unsigned) tap->ir_capture_mask);
  1116. jtag_tap_add(tap);
  1117. }
  1118. void jtag_tap_free(struct jtag_tap *tap)
  1119. {
  1120. jtag_unregister_event_callback(&jtag_reset_callback, tap);
  1121. /// @todo is anything missing? no memory leaks please
  1122. free((void *)tap->expected);
  1123. free((void *)tap->expected_ids);
  1124. free((void *)tap->chip);
  1125. free((void *)tap->tapname);
  1126. free((void *)tap->dotted_name);
  1127. free(tap);
  1128. }
  1129. /**
  1130. * Do low-level setup like initializing registers, output signals,
  1131. * and clocking.
  1132. */
  1133. int adapter_init(struct command_context *cmd_ctx)
  1134. {
  1135. if (jtag)
  1136. return ERROR_OK;
  1137. if (!jtag_interface)
  1138. {
  1139. /* nothing was previously specified by "interface" command */
  1140. LOG_ERROR("Debug Adapter has to be specified, "
  1141. "see \"interface\" command");
  1142. return ERROR_JTAG_INVALID_INTERFACE;
  1143. }
  1144. int retval;
  1145. retval = jtag_interface->init();
  1146. if (retval != ERROR_OK)
  1147. {
  1148. return retval;
  1149. }
  1150. jtag = jtag_interface;
  1151. /* LEGACY SUPPORT ... adapter drivers must declare what
  1152. * transports they allow. Until they all do so, assume
  1153. * the legacy drivers are JTAG-only
  1154. */
  1155. if (!transports_are_declared()) {
  1156. LOG_ERROR("Adapter driver '%s' did not declare "
  1157. "which transports it allows; assuming "
  1158. "JTAG-only", jtag->name);
  1159. retval = allow_transports(cmd_ctx, jtag_only);
  1160. if (retval != ERROR_OK)
  1161. return retval;
  1162. }
  1163. if (CLOCK_MODE_UNSELECTED == clock_mode)
  1164. {
  1165. LOG_ERROR("An adapter speed is not selected in the init script."
  1166. " Insert a call to adapter_khz or jtag_rclk to proceed.");
  1167. return ERROR_JTAG_INIT_FAILED;
  1168. }
  1169. int requested_khz = jtag_get_speed_khz();
  1170. int actual_khz = requested_khz;
  1171. int jtag_speed_var;
  1172. retval = jtag_get_speed(&jtag_speed_var);
  1173. if (retval != ERROR_OK)
  1174. return retval;
  1175. retval = jtag->speed(jtag_speed_var);
  1176. if (retval != ERROR_OK)
  1177. return retval;
  1178. retval = jtag_get_speed_readable(&actual_khz);
  1179. if (ERROR_OK != retval)
  1180. LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
  1181. else if (actual_khz)
  1182. {
  1183. /* Adaptive clocking -- JTAG-specific */
  1184. if ((CLOCK_MODE_RCLK == clock_mode)
  1185. || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
  1186. {
  1187. LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
  1188. , actual_khz);
  1189. }
  1190. else
  1191. LOG_INFO("clock speed %d kHz", actual_khz);
  1192. }
  1193. else
  1194. LOG_INFO("RCLK (adaptive clock speed)");
  1195. return ERROR_OK;
  1196. }
  1197. int jtag_init_inner(struct command_context *cmd_ctx)
  1198. {
  1199. struct jtag_tap *tap;
  1200. int retval;
  1201. bool issue_setup = true;
  1202. LOG_DEBUG("Init JTAG chain");
  1203. tap = jtag_tap_next_enabled(NULL);
  1204. if (tap == NULL) {
  1205. /* Once JTAG itself is properly set up, and the scan chain
  1206. * isn't absurdly large, IDCODE autoprobe should work fine.
  1207. *
  1208. * But ... IRLEN autoprobe can fail even on systems which
  1209. * are fully conformant to JTAG. Also, JTAG setup can be
  1210. * quite finicky on some systems.
  1211. *
  1212. * REVISIT: if TAP autoprobe works OK, then in many cases
  1213. * we could escape to tcl code and set up targets based on
  1214. * the TAP's IDCODE values.
  1215. */
  1216. LOG_WARNING("There are no enabled taps. "
  1217. "AUTO PROBING MIGHT NOT WORK!!");
  1218. /* REVISIT default clock will often be too fast ... */
  1219. }
  1220. jtag_add_tlr();
  1221. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1222. return retval;
  1223. /* Examine DR values first. This discovers problems which will
  1224. * prevent communication ... hardware issues like TDO stuck, or
  1225. * configuring the wrong number of (enabled) TAPs.
  1226. */
  1227. retval = jtag_examine_chain();
  1228. switch (retval) {
  1229. case ERROR_OK:
  1230. /* complete success */
  1231. break;
  1232. default:
  1233. /* For backward compatibility reasons, try coping with
  1234. * configuration errors involving only ID mismatches.
  1235. * We might be able to talk to the devices.
  1236. *
  1237. * Also the device might be powered down during startup.
  1238. *
  1239. * After OpenOCD starts, we can try to power on the device
  1240. * and run a reset.
  1241. */
  1242. LOG_ERROR("Trying to use configured scan chain anyway...");
  1243. issue_setup = false;
  1244. break;
  1245. }
  1246. /* Now look at IR values. Problems here will prevent real
  1247. * communication. They mostly mean that the IR length is
  1248. * wrong ... or that the IR capture value is wrong. (The
  1249. * latter is uncommon, but easily worked around: provide
  1250. * ircapture/irmask values during TAP setup.)
  1251. */
  1252. retval = jtag_validate_ircapture();
  1253. if (retval != ERROR_OK)
  1254. {
  1255. /* The target might be powered down. The user
  1256. * can power it up and reset it after firing
  1257. * up OpenOCD.
  1258. */
  1259. issue_setup = false;
  1260. }
  1261. if (issue_setup)
  1262. jtag_notify_event(JTAG_TAP_EVENT_SETUP);
  1263. else
  1264. LOG_WARNING("Bypassing JTAG setup events due to errors");
  1265. return ERROR_OK;
  1266. }
  1267. int adapter_quit(void)
  1268. {
  1269. if (!jtag || !jtag->quit)
  1270. return ERROR_OK;
  1271. // close the JTAG interface
  1272. int result = jtag->quit();
  1273. if (ERROR_OK != result)
  1274. LOG_ERROR("failed: %d", result);
  1275. return ERROR_OK;
  1276. }
  1277. int jtag_init_reset(struct command_context *cmd_ctx)
  1278. {
  1279. int retval;
  1280. if ((retval = adapter_init(cmd_ctx)) != ERROR_OK)
  1281. return retval;
  1282. LOG_DEBUG("Initializing with hard TRST+SRST reset");
  1283. /*
  1284. * This procedure is used by default when OpenOCD triggers a reset.
  1285. * It's now done through an overridable Tcl "init_reset" wrapper.
  1286. *
  1287. * This started out as a more powerful "get JTAG working" reset than
  1288. * jtag_init_inner(), applying TRST because some chips won't activate
  1289. * JTAG without a TRST cycle (presumed to be async, though some of
  1290. * those chips synchronize JTAG activation using TCK).
  1291. *
  1292. * But some chips only activate JTAG as part of an SRST cycle; SRST
  1293. * got mixed in. So it became a hard reset routine, which got used
  1294. * in more places, and which coped with JTAG reset being forced as
  1295. * part of SRST (srst_pulls_trst).
  1296. *
  1297. * And even more corner cases started to surface: TRST and/or SRST
  1298. * assertion timings matter; some chips need other JTAG operations;
  1299. * TRST/SRST sequences can need to be different from these, etc.
  1300. *
  1301. * Systems should override that wrapper to support system-specific
  1302. * requirements that this not-fully-generic code doesn't handle.
  1303. *
  1304. * REVISIT once Tcl code can read the reset_config modes, this won't
  1305. * need to be a C routine at all...
  1306. */
  1307. jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
  1308. if (jtag_reset_config & RESET_HAS_SRST)
  1309. {
  1310. jtag_add_reset(1, 1);
  1311. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
  1312. jtag_add_reset(0, 1);
  1313. }
  1314. jtag_add_reset(0, 0);
  1315. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1316. return retval;
  1317. /* Check that we can communication on the JTAG chain + eventually we want to
  1318. * be able to perform enumeration only after OpenOCD has started
  1319. * telnet and GDB server
  1320. *
  1321. * That would allow users to more easily perform any magic they need to before
  1322. * reset happens.
  1323. */
  1324. return jtag_init_inner(cmd_ctx);
  1325. }
  1326. int jtag_init(struct command_context *cmd_ctx)
  1327. {
  1328. int retval;
  1329. if ((retval = adapter_init(cmd_ctx)) != ERROR_OK)
  1330. return retval;
  1331. /* guard against oddball hardware: force resets to be inactive */
  1332. jtag_add_reset(0, 0);
  1333. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1334. return retval;
  1335. if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
  1336. return ERROR_FAIL;
  1337. return ERROR_OK;
  1338. }
  1339. unsigned jtag_get_speed_khz(void)
  1340. {
  1341. return speed_khz;
  1342. }
  1343. static int adapter_khz_to_speed(unsigned khz, int* speed)
  1344. {
  1345. LOG_DEBUG("convert khz to interface specific speed value");
  1346. speed_khz = khz;
  1347. if (jtag != NULL)
  1348. {
  1349. LOG_DEBUG("have interface set up");
  1350. int speed_div1;
  1351. int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
  1352. if (ERROR_OK != retval)
  1353. {
  1354. return retval;
  1355. }
  1356. *speed = speed_div1;
  1357. }
  1358. return ERROR_OK;
  1359. }
  1360. static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
  1361. {
  1362. int retval = adapter_khz_to_speed(0, speed);
  1363. if ((ERROR_OK != retval) && fallback_speed_khz)
  1364. {
  1365. LOG_DEBUG("trying fallback speed...");
  1366. retval = adapter_khz_to_speed(fallback_speed_khz, speed);
  1367. }
  1368. return retval;
  1369. }
  1370. static int jtag_set_speed(int speed)
  1371. {
  1372. jtag_speed = speed;
  1373. /* this command can be called during CONFIG,
  1374. * in which case jtag isn't initialized */
  1375. return jtag ? jtag->speed(speed) : ERROR_OK;
  1376. }
  1377. int jtag_config_khz(unsigned khz)
  1378. {
  1379. LOG_DEBUG("handle jtag khz");
  1380. clock_mode = CLOCK_MODE_KHZ;
  1381. int speed = 0;
  1382. int retval = adapter_khz_to_speed(khz, &speed);
  1383. return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
  1384. }
  1385. int jtag_config_rclk(unsigned fallback_speed_khz)
  1386. {
  1387. LOG_DEBUG("handle jtag rclk");
  1388. clock_mode = CLOCK_MODE_RCLK;
  1389. rclk_fallback_speed_khz = fallback_speed_khz;
  1390. int speed = 0;
  1391. int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
  1392. return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
  1393. }
  1394. int jtag_get_speed(int *speed)
  1395. {
  1396. switch(clock_mode)
  1397. {
  1398. case CLOCK_MODE_KHZ:
  1399. adapter_khz_to_speed(jtag_get_speed_khz(), speed);
  1400. break;
  1401. case CLOCK_MODE_RCLK:
  1402. jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
  1403. break;
  1404. default:
  1405. LOG_ERROR("BUG: unknown jtag clock mode");
  1406. return ERROR_FAIL;
  1407. }
  1408. return ERROR_OK;
  1409. }
  1410. int jtag_get_speed_readable(int *khz)
  1411. {
  1412. int jtag_speed_var = 0;
  1413. int retval = jtag_get_speed(&jtag_speed_var);
  1414. if (retval != ERROR_OK)
  1415. return retval;
  1416. return jtag ? jtag->speed_div(jtag_speed_var, khz) : ERROR_OK;
  1417. }
  1418. void jtag_set_verify(bool enable)
  1419. {
  1420. jtag_verify = enable;
  1421. }
  1422. bool jtag_will_verify()
  1423. {
  1424. return jtag_verify;
  1425. }
  1426. void jtag_set_verify_capture_ir(bool enable)
  1427. {
  1428. jtag_verify_capture_ir = enable;
  1429. }
  1430. bool jtag_will_verify_capture_ir()
  1431. {
  1432. return jtag_verify_capture_ir;
  1433. }
  1434. int jtag_power_dropout(int *dropout)
  1435. {
  1436. if (jtag == NULL)
  1437. {
  1438. /* TODO: as the jtag interface is not valid all
  1439. * we can do at the moment is exit OpenOCD */
  1440. LOG_ERROR("No Valid JTAG Interface Configured.");
  1441. exit(-1);
  1442. }
  1443. return jtag->power_dropout(dropout);
  1444. }
  1445. int jtag_srst_asserted(int *srst_asserted)
  1446. {
  1447. return jtag->srst_asserted(srst_asserted);
  1448. }
  1449. enum reset_types jtag_get_reset_config(void)
  1450. {
  1451. return jtag_reset_config;
  1452. }
  1453. void jtag_set_reset_config(enum reset_types type)
  1454. {
  1455. jtag_reset_config = type;
  1456. }
  1457. int jtag_get_trst(void)
  1458. {
  1459. return jtag_trst;
  1460. }
  1461. int jtag_get_srst(void)
  1462. {
  1463. return jtag_srst;
  1464. }
  1465. void jtag_set_nsrst_delay(unsigned delay)
  1466. {
  1467. adapter_nsrst_delay = delay;
  1468. }
  1469. unsigned jtag_get_nsrst_delay(void)
  1470. {
  1471. return adapter_nsrst_delay;
  1472. }
  1473. void jtag_set_ntrst_delay(unsigned delay)
  1474. {
  1475. jtag_ntrst_delay = delay;
  1476. }
  1477. unsigned jtag_get_ntrst_delay(void)
  1478. {
  1479. return jtag_ntrst_delay;
  1480. }
  1481. void jtag_set_nsrst_assert_width(unsigned delay)
  1482. {
  1483. adapter_nsrst_assert_width = delay;
  1484. }
  1485. unsigned jtag_get_nsrst_assert_width(void)
  1486. {
  1487. return adapter_nsrst_assert_width;
  1488. }
  1489. void jtag_set_ntrst_assert_width(unsigned delay)
  1490. {
  1491. jtag_ntrst_assert_width = delay;
  1492. }
  1493. unsigned jtag_get_ntrst_assert_width(void)
  1494. {
  1495. return jtag_ntrst_assert_width;
  1496. }
  1497. static int jtag_select(struct command_context *ctx)
  1498. {
  1499. int retval;
  1500. /* NOTE: interface init must already have been done.
  1501. * That works with only C code ... no Tcl glue required.
  1502. */
  1503. retval = jtag_register_commands(ctx);
  1504. if (retval != ERROR_OK)
  1505. return retval;
  1506. retval = svf_register_commands(ctx);
  1507. if (retval != ERROR_OK)
  1508. return retval;
  1509. return xsvf_register_commands(ctx);
  1510. }
  1511. static struct transport jtag_transport = {
  1512. .name = "jtag",
  1513. .select = jtag_select,
  1514. .init = jtag_init,
  1515. };
  1516. static void jtag_constructor(void) __attribute__((constructor));
  1517. static void jtag_constructor(void)
  1518. {
  1519. transport_register(&jtag_transport);
  1520. }
  1521. /** Returns true if the current debug session
  1522. * is using JTAG as its transport.
  1523. */
  1524. bool transport_is_jtag(void)
  1525. {
  1526. return get_current_transport() == &jtag_transport;
  1527. }