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.
 
 
 
 
 
 

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