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.
 
 
 
 
 
 

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