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.
 
 
 
 
 
 

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