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.
 
 
 
 
 
 

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