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.
 
 
 
 
 
 

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