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.
 
 
 
 
 
 

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