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.
 
 
 
 
 
 

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