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.
 
 
 
 
 
 

1605 lines
40 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_state == cur_state)
  470. ; /* nothing to do */
  471. else if (goal_state == TAP_RESET)
  472. {
  473. jtag_add_tlr();
  474. }
  475. else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
  476. {
  477. unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
  478. unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
  479. tap_state_t moves[8];
  480. assert(tms_count < DIM(moves));
  481. for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
  482. {
  483. bool bit = tms_bits & 1;
  484. cur_state = tap_state_transition(cur_state, bit);
  485. moves[i] = cur_state;
  486. }
  487. jtag_add_pathmove(tms_count, moves);
  488. }
  489. else if (tap_state_transition(cur_state, true) == goal_state
  490. || tap_state_transition(cur_state, false) == goal_state)
  491. {
  492. jtag_add_pathmove(1, &goal_state);
  493. }
  494. else
  495. return ERROR_FAIL;
  496. return ERROR_OK;
  497. }
  498. void jtag_add_runtest(int num_cycles, tap_state_t state)
  499. {
  500. jtag_prelude(state);
  501. jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
  502. }
  503. void jtag_add_clocks(int num_cycles)
  504. {
  505. if (!tap_is_state_stable(cmd_queue_cur_state))
  506. {
  507. LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
  508. tap_state_name(cmd_queue_cur_state));
  509. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  510. return;
  511. }
  512. if (num_cycles > 0)
  513. {
  514. jtag_checks();
  515. jtag_set_error(interface_jtag_add_clocks(num_cycles));
  516. }
  517. }
  518. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  519. {
  520. int trst_with_tlr = 0;
  521. int new_srst = 0;
  522. int new_trst = 0;
  523. /* Without SRST, we must use target-specific JTAG operations
  524. * on each target; callers should not be requesting SRST when
  525. * that signal doesn't exist.
  526. *
  527. * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
  528. * can kick in even if the JTAG adapter can't drive TRST.
  529. */
  530. if (req_srst) {
  531. if (!(jtag_reset_config & RESET_HAS_SRST)) {
  532. LOG_ERROR("BUG: can't assert SRST");
  533. jtag_set_error(ERROR_FAIL);
  534. return;
  535. }
  536. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
  537. && !req_tlr_or_trst) {
  538. LOG_ERROR("BUG: can't assert only SRST");
  539. jtag_set_error(ERROR_FAIL);
  540. return;
  541. }
  542. new_srst = 1;
  543. }
  544. /* JTAG reset (entry to TAP_RESET state) can always be achieved
  545. * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
  546. * state first. TRST accelerates it, and bypasses those states.
  547. *
  548. * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
  549. * can kick in even if the JTAG adapter can't drive SRST.
  550. */
  551. if (req_tlr_or_trst) {
  552. if (!(jtag_reset_config & RESET_HAS_TRST))
  553. trst_with_tlr = 1;
  554. else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
  555. && !req_srst)
  556. trst_with_tlr = 1;
  557. else
  558. new_trst = 1;
  559. }
  560. /* Maybe change TRST and/or SRST signal state */
  561. if (jtag_srst != new_srst || jtag_trst != new_trst) {
  562. int retval;
  563. retval = interface_jtag_add_reset(new_trst, new_srst);
  564. if (retval != ERROR_OK)
  565. jtag_set_error(retval);
  566. else
  567. retval = jtag_execute_queue();
  568. if (retval != ERROR_OK) {
  569. LOG_ERROR("TRST/SRST error %d", retval);
  570. return;
  571. }
  572. }
  573. /* SRST resets everything hooked up to that signal */
  574. if (jtag_srst != new_srst) {
  575. jtag_srst = new_srst;
  576. if (jtag_srst)
  577. {
  578. LOG_DEBUG("SRST line asserted");
  579. if (jtag_nsrst_assert_width)
  580. jtag_add_sleep(jtag_nsrst_assert_width * 1000);
  581. }
  582. else {
  583. LOG_DEBUG("SRST line released");
  584. if (jtag_nsrst_delay)
  585. jtag_add_sleep(jtag_nsrst_delay * 1000);
  586. }
  587. }
  588. /* Maybe enter the JTAG TAP_RESET state ...
  589. * - using only TMS, TCK, and the JTAG state machine
  590. * - or else more directly, using TRST
  591. *
  592. * TAP_RESET should be invisible to non-debug parts of the system.
  593. */
  594. if (trst_with_tlr) {
  595. LOG_DEBUG("JTAG reset with TLR instead of TRST");
  596. jtag_set_end_state(TAP_RESET);
  597. jtag_add_tlr();
  598. } else if (jtag_trst != new_trst) {
  599. jtag_trst = new_trst;
  600. if (jtag_trst) {
  601. LOG_DEBUG("TRST line asserted");
  602. tap_set_state(TAP_RESET);
  603. if (jtag_ntrst_assert_width)
  604. jtag_add_sleep(jtag_ntrst_assert_width * 1000);
  605. } else {
  606. LOG_DEBUG("TRST line released");
  607. if (jtag_ntrst_delay)
  608. jtag_add_sleep(jtag_ntrst_delay * 1000);
  609. /* We just asserted nTRST, so we're now in TAP_RESET.
  610. * Inform possible listeners about this, now that
  611. * JTAG instructions and data can be shifted. This
  612. * sequence must match jtag_add_tlr().
  613. */
  614. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  615. jtag_notify_event(JTAG_TRST_ASSERTED);
  616. }
  617. }
  618. }
  619. tap_state_t jtag_set_end_state(tap_state_t state)
  620. {
  621. if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
  622. {
  623. LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
  624. }
  625. if (state != TAP_INVALID)
  626. cmd_queue_end_state = state;
  627. return cmd_queue_end_state;
  628. }
  629. tap_state_t jtag_get_end_state(void)
  630. {
  631. return cmd_queue_end_state;
  632. }
  633. void jtag_add_sleep(uint32_t us)
  634. {
  635. /// @todo Here, keep_alive() appears to be a layering violation!!!
  636. keep_alive();
  637. jtag_set_error(interface_jtag_add_sleep(us));
  638. }
  639. static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
  640. uint8_t *in_check_mask, int num_bits)
  641. {
  642. int retval = ERROR_OK;
  643. int compare_failed = 0;
  644. if (in_check_mask)
  645. compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
  646. else
  647. compare_failed = buf_cmp(captured, in_check_value, num_bits);
  648. if (compare_failed) {
  649. char *captured_str, *in_check_value_str;
  650. int bits = (num_bits > DEBUG_JTAG_IOZ)
  651. ? DEBUG_JTAG_IOZ
  652. : num_bits;
  653. /* NOTE: we've lost diagnostic context here -- 'which tap' */
  654. captured_str = buf_to_str(captured, bits, 16);
  655. in_check_value_str = buf_to_str(in_check_value, bits, 16);
  656. LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
  657. captured_str);
  658. LOG_WARNING(" check_value: 0x%s", in_check_value_str);
  659. free(captured_str);
  660. free(in_check_value_str);
  661. if (in_check_mask) {
  662. char *in_check_mask_str;
  663. in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
  664. LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
  665. free(in_check_mask_str);
  666. }
  667. retval = ERROR_JTAG_QUEUE_FAILED;
  668. }
  669. return retval;
  670. }
  671. void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
  672. {
  673. assert(field->in_value != NULL);
  674. if (value == NULL)
  675. {
  676. /* no checking to do */
  677. return;
  678. }
  679. jtag_execute_queue_noclear();
  680. int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
  681. jtag_set_error(retval);
  682. }
  683. int default_interface_jtag_execute_queue(void)
  684. {
  685. if (NULL == jtag)
  686. {
  687. LOG_ERROR("No JTAG interface configured yet. "
  688. "Issue 'init' command in startup scripts "
  689. "before communicating with targets.");
  690. return ERROR_FAIL;
  691. }
  692. return jtag->execute_queue();
  693. }
  694. void jtag_execute_queue_noclear(void)
  695. {
  696. jtag_flush_queue_count++;
  697. jtag_set_error(interface_jtag_execute_queue());
  698. }
  699. int jtag_get_flush_queue_count(void)
  700. {
  701. return jtag_flush_queue_count;
  702. }
  703. int jtag_execute_queue(void)
  704. {
  705. jtag_execute_queue_noclear();
  706. return jtag_error_clear();
  707. }
  708. static int jtag_reset_callback(enum jtag_event event, void *priv)
  709. {
  710. jtag_tap_t *tap = priv;
  711. if (event == JTAG_TRST_ASSERTED)
  712. {
  713. tap->enabled = !tap->disabled_after_reset;
  714. /* current instruction is either BYPASS or IDCODE */
  715. buf_set_ones(tap->cur_instr, tap->ir_length);
  716. tap->bypass = 1;
  717. }
  718. return ERROR_OK;
  719. }
  720. void jtag_sleep(uint32_t us)
  721. {
  722. alive_sleep(us/1000);
  723. }
  724. /* Maximum number of enabled JTAG devices we expect in the scan chain,
  725. * plus one (to detect garbage at the end). Devices that don't support
  726. * IDCODE take up fewer bits, possibly allowing a few more devices.
  727. */
  728. #define JTAG_MAX_CHAIN_SIZE 20
  729. #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
  730. #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
  731. #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
  732. /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
  733. * know that no valid TAP will have it as an IDCODE value.
  734. */
  735. #define END_OF_CHAIN_FLAG 0x000000ff
  736. static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
  737. {
  738. scan_field_t field = {
  739. .tap = NULL,
  740. .num_bits = num_idcode * 32,
  741. .out_value = idcode_buffer,
  742. .in_value = idcode_buffer,
  743. };
  744. // initialize to the end of chain ID value
  745. for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
  746. buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
  747. jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
  748. jtag_add_tlr();
  749. return jtag_execute_queue();
  750. }
  751. static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
  752. {
  753. uint8_t zero_check = 0x0;
  754. uint8_t one_check = 0xff;
  755. for (unsigned i = 0; i < count * 4; i++)
  756. {
  757. zero_check |= idcodes[i];
  758. one_check &= idcodes[i];
  759. }
  760. /* if there wasn't a single non-zero bit or if all bits were one,
  761. * the scan is not valid. We wrote a mix of both values; either
  762. *
  763. * - There's a hardware issue (almost certainly):
  764. * + all-zeroes can mean a target stuck in JTAG reset
  765. * + all-ones tends to mean no target
  766. * - The scan chain is WAY longer than we can handle, *AND* either
  767. * + there are several hundreds of TAPs in bypass, or
  768. * + at least a few dozen TAPs all have an all-ones IDCODE
  769. */
  770. if (zero_check == 0x00 || one_check == 0xff)
  771. {
  772. LOG_ERROR("JTAG scan chain interrogation failed: all %s",
  773. (zero_check == 0x00) ? "zeroes" : "ones");
  774. LOG_ERROR("Check JTAG interface, timings, target power, etc.");
  775. return false;
  776. }
  777. return true;
  778. }
  779. static void jtag_examine_chain_display(enum log_levels level, const char *msg,
  780. const char *name, uint32_t idcode)
  781. {
  782. log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
  783. "JTAG tap: %s %16.16s: 0x%08x "
  784. "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  785. name, msg,
  786. (unsigned int)idcode,
  787. (unsigned int)EXTRACT_MFG(idcode),
  788. (unsigned int)EXTRACT_PART(idcode),
  789. (unsigned int)EXTRACT_VER(idcode));
  790. }
  791. static bool jtag_idcode_is_final(uint32_t idcode)
  792. {
  793. /*
  794. * Some devices, such as AVR8, will output all 1's instead
  795. * of TDI input value at end of chain. Allow those values
  796. * instead of failing.
  797. */
  798. return idcode == END_OF_CHAIN_FLAG || idcode == 0xFFFFFFFF;
  799. }
  800. /**
  801. * This helper checks that remaining bits in the examined chain data are
  802. * all as expected, but a single JTAG device requires only 64 bits to be
  803. * read back correctly. This can help identify and diagnose problems
  804. * with the JTAG chain earlier, gives more helpful/explicit error messages.
  805. * Returns TRUE iff garbage was found.
  806. */
  807. static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
  808. {
  809. bool triggered = false;
  810. for (; count < max - 31; count += 32)
  811. {
  812. uint32_t idcode = buf_get_u32(idcodes, count, 32);
  813. /* do not trigger the warning if the data looks good */
  814. if (jtag_idcode_is_final(idcode))
  815. continue;
  816. LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
  817. count, (unsigned int)idcode);
  818. triggered = true;
  819. }
  820. return triggered;
  821. }
  822. static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
  823. {
  824. /* ignore expected BYPASS codes; warn otherwise */
  825. if (0 == tap->expected_ids_cnt && !tap->idcode)
  826. return true;
  827. /* Loop over the expected identification codes and test for a match */
  828. uint8_t ii;
  829. for (ii = 0; ii < tap->expected_ids_cnt; ii++)
  830. {
  831. if (tap->idcode == tap->expected_ids[ii])
  832. return true;
  833. /* treat "-expected-id 0" as a "don't-warn" wildcard */
  834. if (0 == tap->expected_ids[ii])
  835. return true;
  836. }
  837. /* If none of the expected ids matched, warn */
  838. jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
  839. tap->dotted_name, tap->idcode);
  840. for (ii = 0; ii < tap->expected_ids_cnt; ii++)
  841. {
  842. char msg[32];
  843. snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
  844. ii + 1, tap->expected_ids_cnt);
  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. assert(0 != tap->ir_length);
  1009. /// @todo fix, this allocates one byte per bit for all three fields!
  1010. tap->expected = malloc(tap->ir_length);
  1011. tap->expected_mask = malloc(tap->ir_length);
  1012. tap->cur_instr = malloc(tap->ir_length);
  1013. /// @todo cope sanely with ir_length bigger than 32 bits
  1014. buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
  1015. buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
  1016. buf_set_ones(tap->cur_instr, tap->ir_length);
  1017. // place TAP in bypass mode
  1018. tap->bypass = 1;
  1019. // register the reset callback for the TAP
  1020. jtag_register_event_callback(&jtag_reset_callback, tap);
  1021. LOG_DEBUG("Created Tap: %s @ abs position %d, "
  1022. "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
  1023. tap->abs_chain_position, tap->ir_length,
  1024. (unsigned) tap->ir_capture_value,
  1025. (unsigned) tap->ir_capture_mask);
  1026. jtag_tap_add(tap);
  1027. }
  1028. void jtag_tap_free(jtag_tap_t *tap)
  1029. {
  1030. jtag_unregister_event_callback(&jtag_reset_callback, tap);
  1031. /// @todo is anything missing? no memory leaks please
  1032. free((void *)tap->expected);
  1033. free((void *)tap->expected_ids);
  1034. free((void *)tap->chip);
  1035. free((void *)tap->tapname);
  1036. free((void *)tap->dotted_name);
  1037. free(tap);
  1038. }
  1039. int jtag_interface_init(struct command_context_s *cmd_ctx)
  1040. {
  1041. if (jtag)
  1042. return ERROR_OK;
  1043. if (!jtag_interface)
  1044. {
  1045. /* nothing was previously specified by "interface" command */
  1046. LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
  1047. return ERROR_JTAG_INVALID_INTERFACE;
  1048. }
  1049. jtag = jtag_interface;
  1050. if (jtag_interface->init() != ERROR_OK)
  1051. {
  1052. jtag = NULL;
  1053. return ERROR_JTAG_INIT_FAILED;
  1054. }
  1055. int requested_khz = jtag_get_speed_khz();
  1056. int actual_khz = requested_khz;
  1057. int retval = jtag_get_speed_readable(&actual_khz);
  1058. if (ERROR_OK != retval)
  1059. LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
  1060. else if (actual_khz)
  1061. {
  1062. if ((CLOCK_MODE_RCLK == clock_mode)
  1063. || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
  1064. {
  1065. LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
  1066. , actual_khz);
  1067. }
  1068. else
  1069. LOG_INFO("clock speed %d kHz", actual_khz);
  1070. }
  1071. else
  1072. LOG_INFO("RCLK (adaptive clock speed)");
  1073. return ERROR_OK;
  1074. }
  1075. int jtag_init_inner(struct command_context_s *cmd_ctx)
  1076. {
  1077. jtag_tap_t *tap;
  1078. int retval;
  1079. bool issue_setup = true;
  1080. LOG_DEBUG("Init JTAG chain");
  1081. tap = jtag_tap_next_enabled(NULL);
  1082. if (tap == NULL) {
  1083. LOG_ERROR("There are no enabled taps?");
  1084. return ERROR_JTAG_INIT_FAILED;
  1085. }
  1086. jtag_add_tlr();
  1087. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1088. return retval;
  1089. /* Examine DR values first. This discovers problems which will
  1090. * prevent communication ... hardware issues like TDO stuck, or
  1091. * configuring the wrong number of (enabled) TAPs.
  1092. */
  1093. retval = jtag_examine_chain();
  1094. switch (retval) {
  1095. case ERROR_OK:
  1096. /* complete success */
  1097. break;
  1098. case ERROR_JTAG_INIT_SOFT_FAIL:
  1099. /* For backward compatibility reasons, try coping with
  1100. * configuration errors involving only ID mismatches.
  1101. * We might be able to talk to the devices.
  1102. */
  1103. LOG_ERROR("Trying to use configured scan chain anyway...");
  1104. issue_setup = false;
  1105. break;
  1106. default:
  1107. /* some hard error; already issued diagnostics */
  1108. return retval;
  1109. }
  1110. /* Now look at IR values. Problems here will prevent real
  1111. * communication. They mostly mean that the IR length is
  1112. * wrong ... or that the IR capture value is wrong. (The
  1113. * latter is uncommon, but easily worked around: provide
  1114. * ircapture/irmask values during TAP setup.)
  1115. */
  1116. retval = jtag_validate_ircapture();
  1117. if (retval != ERROR_OK)
  1118. return retval;
  1119. if (issue_setup)
  1120. jtag_notify_event(JTAG_TAP_EVENT_SETUP);
  1121. else
  1122. LOG_WARNING("Bypassing JTAG setup events due to errors");
  1123. return ERROR_OK;
  1124. }
  1125. int jtag_interface_quit(void)
  1126. {
  1127. if (!jtag || !jtag->quit)
  1128. return ERROR_OK;
  1129. // close the JTAG interface
  1130. int result = jtag->quit();
  1131. if (ERROR_OK != result)
  1132. LOG_ERROR("failed: %d", result);
  1133. return ERROR_OK;
  1134. }
  1135. int jtag_init_reset(struct command_context_s *cmd_ctx)
  1136. {
  1137. int retval;
  1138. if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1139. return retval;
  1140. LOG_DEBUG("Initializing with hard TRST+SRST reset");
  1141. /*
  1142. * This procedure is used by default when OpenOCD triggers a reset.
  1143. * It's now done through an overridable Tcl "init_reset" wrapper.
  1144. *
  1145. * This started out as a more powerful "get JTAG working" reset than
  1146. * jtag_init_inner(), applying TRST because some chips won't activate
  1147. * JTAG without a TRST cycle (presumed to be async, though some of
  1148. * those chips synchronize JTAG activation using TCK).
  1149. *
  1150. * But some chips only activate JTAG as part of an SRST cycle; SRST
  1151. * got mixed in. So it became a hard reset routine, which got used
  1152. * in more places, and which coped with JTAG reset being forced as
  1153. * part of SRST (srst_pulls_trst).
  1154. *
  1155. * And even more corner cases started to surface: TRST and/or SRST
  1156. * assertion timings matter; some chips need other JTAG operations;
  1157. * TRST/SRST sequences can need to be different from these, etc.
  1158. *
  1159. * Systems should override that wrapper to support system-specific
  1160. * requirements that this not-fully-generic code doesn't handle.
  1161. *
  1162. * REVISIT once Tcl code can read the reset_config modes, this won't
  1163. * need to be a C routine at all...
  1164. */
  1165. jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
  1166. if (jtag_reset_config & RESET_HAS_SRST)
  1167. {
  1168. jtag_add_reset(1, 1);
  1169. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
  1170. jtag_add_reset(0, 1);
  1171. }
  1172. jtag_add_reset(0, 0);
  1173. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1174. return retval;
  1175. /* Check that we can communication on the JTAG chain + eventually we want to
  1176. * be able to perform enumeration only after OpenOCD has started
  1177. * telnet and GDB server
  1178. *
  1179. * That would allow users to more easily perform any magic they need to before
  1180. * reset happens.
  1181. */
  1182. return jtag_init_inner(cmd_ctx);
  1183. }
  1184. int jtag_init(struct command_context_s *cmd_ctx)
  1185. {
  1186. int retval;
  1187. if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1188. return retval;
  1189. /* guard against oddball hardware: force resets to be inactive */
  1190. jtag_add_reset(0, 0);
  1191. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1192. return retval;
  1193. if (Jim_Eval_Named(interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
  1194. return ERROR_FAIL;
  1195. return ERROR_OK;
  1196. }
  1197. unsigned jtag_get_speed_khz(void)
  1198. {
  1199. return speed_khz;
  1200. }
  1201. static int jtag_khz_to_speed(unsigned khz, int* speed)
  1202. {
  1203. LOG_DEBUG("convert khz to interface specific speed value");
  1204. speed_khz = khz;
  1205. if (jtag != NULL)
  1206. {
  1207. LOG_DEBUG("have interface set up");
  1208. int speed_div1;
  1209. int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
  1210. if (ERROR_OK != retval)
  1211. {
  1212. return retval;
  1213. }
  1214. *speed = speed_div1;
  1215. }
  1216. return ERROR_OK;
  1217. }
  1218. static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
  1219. {
  1220. int retval = jtag_khz_to_speed(0, speed);
  1221. if ((ERROR_OK != retval) && fallback_speed_khz)
  1222. {
  1223. LOG_DEBUG("trying fallback speed...");
  1224. retval = jtag_khz_to_speed(fallback_speed_khz, speed);
  1225. }
  1226. return retval;
  1227. }
  1228. static int jtag_set_speed(int speed)
  1229. {
  1230. jtag_speed = speed;
  1231. /* this command can be called during CONFIG,
  1232. * in which case jtag isn't initialized */
  1233. return jtag ? jtag->speed(speed) : ERROR_OK;
  1234. }
  1235. int jtag_config_speed(int speed)
  1236. {
  1237. LOG_DEBUG("handle jtag speed");
  1238. clock_mode = CLOCK_MODE_SPEED;
  1239. return jtag_set_speed(speed);
  1240. }
  1241. int jtag_config_khz(unsigned khz)
  1242. {
  1243. LOG_DEBUG("handle jtag khz");
  1244. clock_mode = CLOCK_MODE_KHZ;
  1245. int speed = 0;
  1246. int retval = jtag_khz_to_speed(khz, &speed);
  1247. return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
  1248. }
  1249. int jtag_config_rclk(unsigned fallback_speed_khz)
  1250. {
  1251. LOG_DEBUG("handle jtag rclk");
  1252. clock_mode = CLOCK_MODE_RCLK;
  1253. rclk_fallback_speed_khz = fallback_speed_khz;
  1254. int speed = 0;
  1255. int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
  1256. return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
  1257. }
  1258. int jtag_get_speed(void)
  1259. {
  1260. int speed;
  1261. switch(clock_mode)
  1262. {
  1263. case CLOCK_MODE_SPEED:
  1264. speed = jtag_speed;
  1265. break;
  1266. case CLOCK_MODE_KHZ:
  1267. jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
  1268. break;
  1269. case CLOCK_MODE_RCLK:
  1270. jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
  1271. break;
  1272. default:
  1273. LOG_ERROR("BUG: unknown jtag clock mode");
  1274. speed = 0;
  1275. break;
  1276. }
  1277. return speed;
  1278. }
  1279. int jtag_get_speed_readable(int *khz)
  1280. {
  1281. return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
  1282. }
  1283. void jtag_set_verify(bool enable)
  1284. {
  1285. jtag_verify = enable;
  1286. }
  1287. bool jtag_will_verify()
  1288. {
  1289. return jtag_verify;
  1290. }
  1291. void jtag_set_verify_capture_ir(bool enable)
  1292. {
  1293. jtag_verify_capture_ir = enable;
  1294. }
  1295. bool jtag_will_verify_capture_ir()
  1296. {
  1297. return jtag_verify_capture_ir;
  1298. }
  1299. int jtag_power_dropout(int *dropout)
  1300. {
  1301. return jtag->power_dropout(dropout);
  1302. }
  1303. int jtag_srst_asserted(int *srst_asserted)
  1304. {
  1305. return jtag->srst_asserted(srst_asserted);
  1306. }
  1307. enum reset_types jtag_get_reset_config(void)
  1308. {
  1309. return jtag_reset_config;
  1310. }
  1311. void jtag_set_reset_config(enum reset_types type)
  1312. {
  1313. jtag_reset_config = type;
  1314. }
  1315. int jtag_get_trst(void)
  1316. {
  1317. return jtag_trst;
  1318. }
  1319. int jtag_get_srst(void)
  1320. {
  1321. return jtag_srst;
  1322. }
  1323. void jtag_set_nsrst_delay(unsigned delay)
  1324. {
  1325. jtag_nsrst_delay = delay;
  1326. }
  1327. unsigned jtag_get_nsrst_delay(void)
  1328. {
  1329. return jtag_nsrst_delay;
  1330. }
  1331. void jtag_set_ntrst_delay(unsigned delay)
  1332. {
  1333. jtag_ntrst_delay = delay;
  1334. }
  1335. unsigned jtag_get_ntrst_delay(void)
  1336. {
  1337. return jtag_ntrst_delay;
  1338. }
  1339. void jtag_set_nsrst_assert_width(unsigned delay)
  1340. {
  1341. jtag_nsrst_assert_width = delay;
  1342. }
  1343. unsigned jtag_get_nsrst_assert_width(void)
  1344. {
  1345. return jtag_nsrst_assert_width;
  1346. }
  1347. void jtag_set_ntrst_assert_width(unsigned delay)
  1348. {
  1349. jtag_ntrst_assert_width = delay;
  1350. }
  1351. unsigned jtag_get_ntrst_assert_width(void)
  1352. {
  1353. return jtag_ntrst_assert_width;
  1354. }