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.
 
 
 
 
 
 

1731 lines
45 KiB

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