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.
 
 
 
 
 
 

2106 lines
56 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, see <http://www.gnu.org/licenses/>. *
  27. ***************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "jtag.h"
  32. #include "swd.h"
  33. #include "interface.h"
  34. #include <transport/transport.h>
  35. #include <helper/jep106.h>
  36. #ifdef HAVE_STRINGS_H
  37. #include <strings.h>
  38. #endif
  39. /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
  40. #include "svf/svf.h"
  41. #include "xsvf/xsvf.h"
  42. /** The number of JTAG queue flushes (for profiling and debugging purposes). */
  43. static int jtag_flush_queue_count;
  44. /* Sleep this # of ms after flushing the queue */
  45. static int jtag_flush_queue_sleep;
  46. static void jtag_add_scan_check(struct jtag_tap *active,
  47. void (*jtag_add_scan)(struct jtag_tap *active,
  48. int in_num_fields,
  49. const struct scan_field *in_fields,
  50. tap_state_t state),
  51. int in_num_fields, struct scan_field *in_fields, tap_state_t state);
  52. /**
  53. * The jtag_error variable is set when an error occurs while executing
  54. * the queue. Application code may set this using jtag_set_error(),
  55. * when an error occurs during processing that should be reported during
  56. * jtag_execute_queue().
  57. *
  58. * The value is set and cleared, but never read by normal application code.
  59. *
  60. * This value is returned (and cleared) by jtag_execute_queue().
  61. */
  62. static int jtag_error = ERROR_OK;
  63. static const char *jtag_event_strings[] = {
  64. [JTAG_TRST_ASSERTED] = "TAP reset",
  65. [JTAG_TAP_EVENT_SETUP] = "TAP setup",
  66. [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
  67. [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
  68. };
  69. /*
  70. * JTAG adapters must initialize with TRST and SRST de-asserted
  71. * (they're negative logic, so that means *high*). But some
  72. * hardware doesn't necessarily work that way ... so set things
  73. * up so that jtag_init() always forces that state.
  74. */
  75. static int jtag_trst = -1;
  76. static int jtag_srst = -1;
  77. /**
  78. * List all TAPs that have been created.
  79. */
  80. static struct jtag_tap *__jtag_all_taps;
  81. static enum reset_types jtag_reset_config = RESET_NONE;
  82. tap_state_t cmd_queue_cur_state = TAP_RESET;
  83. static bool jtag_verify_capture_ir = true;
  84. static int jtag_verify = 1;
  85. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
  86. *deasserted (in ms) */
  87. static int adapter_nsrst_delay; /* default to no nSRST delay */
  88. static int jtag_ntrst_delay;/* default to no nTRST delay */
  89. static int adapter_nsrst_assert_width; /* width of assertion */
  90. static int jtag_ntrst_assert_width; /* width of assertion */
  91. /**
  92. * Contains a single callback along with a pointer that will be passed
  93. * when an event occurs.
  94. */
  95. struct jtag_event_callback {
  96. /** a event callback */
  97. jtag_event_handler_t callback;
  98. /** the private data to pass to the callback */
  99. void *priv;
  100. /** the next callback */
  101. struct jtag_event_callback *next;
  102. };
  103. /* callbacks to inform high-level handlers about JTAG state changes */
  104. static struct jtag_event_callback *jtag_event_callbacks;
  105. /* speed in kHz*/
  106. static int speed_khz;
  107. /* speed to fallback to when RCLK is requested but not supported */
  108. static int rclk_fallback_speed_khz;
  109. static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
  110. static int jtag_speed;
  111. /* FIXME: change name to this variable, it is not anymore JTAG only */
  112. static struct adapter_driver *jtag;
  113. extern struct adapter_driver *adapter_driver;
  114. void jtag_set_flush_queue_sleep(int ms)
  115. {
  116. jtag_flush_queue_sleep = ms;
  117. }
  118. void jtag_set_error(int error)
  119. {
  120. if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
  121. return;
  122. jtag_error = error;
  123. }
  124. int jtag_error_clear(void)
  125. {
  126. int temp = jtag_error;
  127. jtag_error = ERROR_OK;
  128. return temp;
  129. }
  130. /************/
  131. static bool jtag_poll = 1;
  132. bool is_jtag_poll_safe(void)
  133. {
  134. /* Polling can be disabled explicitly with set_enabled(false).
  135. * It is also implicitly disabled while TRST is active and
  136. * while SRST is gating the JTAG clock.
  137. */
  138. if (!transport_is_jtag())
  139. return jtag_poll;
  140. if (!jtag_poll || jtag_trst != 0)
  141. return false;
  142. return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
  143. }
  144. bool jtag_poll_get_enabled(void)
  145. {
  146. return jtag_poll;
  147. }
  148. void jtag_poll_set_enabled(bool value)
  149. {
  150. jtag_poll = value;
  151. }
  152. /************/
  153. struct jtag_tap *jtag_all_taps(void)
  154. {
  155. return __jtag_all_taps;
  156. };
  157. unsigned jtag_tap_count(void)
  158. {
  159. struct jtag_tap *t = jtag_all_taps();
  160. unsigned n = 0;
  161. while (t) {
  162. n++;
  163. t = t->next_tap;
  164. }
  165. return n;
  166. }
  167. unsigned jtag_tap_count_enabled(void)
  168. {
  169. struct jtag_tap *t = jtag_all_taps();
  170. unsigned n = 0;
  171. while (t) {
  172. if (t->enabled)
  173. n++;
  174. t = t->next_tap;
  175. }
  176. return n;
  177. }
  178. /** Append a new TAP to the chain of all taps. */
  179. static void jtag_tap_add(struct jtag_tap *t)
  180. {
  181. unsigned jtag_num_taps = 0;
  182. struct jtag_tap **tap = &__jtag_all_taps;
  183. while (*tap != NULL) {
  184. jtag_num_taps++;
  185. tap = &(*tap)->next_tap;
  186. }
  187. *tap = t;
  188. t->abs_chain_position = jtag_num_taps;
  189. }
  190. /* returns a pointer to the n-th device in the scan chain */
  191. struct jtag_tap *jtag_tap_by_position(unsigned n)
  192. {
  193. struct jtag_tap *t = jtag_all_taps();
  194. while (t && n-- > 0)
  195. t = t->next_tap;
  196. return t;
  197. }
  198. struct jtag_tap *jtag_tap_by_string(const char *s)
  199. {
  200. /* try by name first */
  201. struct jtag_tap *t = jtag_all_taps();
  202. while (t) {
  203. if (0 == strcmp(t->dotted_name, s))
  204. return t;
  205. t = t->next_tap;
  206. }
  207. /* no tap found by name, so try to parse the name as a number */
  208. unsigned n;
  209. if (parse_uint(s, &n) != ERROR_OK)
  210. return NULL;
  211. /* FIXME remove this numeric fallback code late June 2010, along
  212. * with all info in the User's Guide that TAPs have numeric IDs.
  213. * Also update "scan_chain" output to not display the numbers.
  214. */
  215. t = jtag_tap_by_position(n);
  216. if (t)
  217. LOG_WARNING("Specify TAP '%s' by name, not number %u",
  218. t->dotted_name, n);
  219. return t;
  220. }
  221. struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p)
  222. {
  223. p = p ? p->next_tap : jtag_all_taps();
  224. while (p) {
  225. if (p->enabled)
  226. return p;
  227. p = p->next_tap;
  228. }
  229. return NULL;
  230. }
  231. const char *jtag_tap_name(const struct jtag_tap *tap)
  232. {
  233. return (tap == NULL) ? "(unknown)" : tap->dotted_name;
  234. }
  235. int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
  236. {
  237. struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
  238. if (callback == NULL)
  239. return ERROR_COMMAND_SYNTAX_ERROR;
  240. if (*callbacks_p) {
  241. while ((*callbacks_p)->next)
  242. callbacks_p = &((*callbacks_p)->next);
  243. callbacks_p = &((*callbacks_p)->next);
  244. }
  245. (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
  246. (*callbacks_p)->callback = callback;
  247. (*callbacks_p)->priv = priv;
  248. (*callbacks_p)->next = NULL;
  249. return ERROR_OK;
  250. }
  251. int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
  252. {
  253. struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
  254. if (callback == NULL)
  255. return ERROR_COMMAND_SYNTAX_ERROR;
  256. while (*p) {
  257. if (((*p)->priv != priv) || ((*p)->callback != callback)) {
  258. p = &(*p)->next;
  259. continue;
  260. }
  261. temp = *p;
  262. *p = (*p)->next;
  263. free(temp);
  264. }
  265. return ERROR_OK;
  266. }
  267. int jtag_call_event_callbacks(enum jtag_event event)
  268. {
  269. struct jtag_event_callback *callback = jtag_event_callbacks;
  270. LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
  271. while (callback) {
  272. struct jtag_event_callback *next;
  273. /* callback may remove itself */
  274. next = callback->next;
  275. callback->callback(event, callback->priv);
  276. callback = next;
  277. }
  278. return ERROR_OK;
  279. }
  280. static void jtag_checks(void)
  281. {
  282. assert(jtag_trst == 0);
  283. }
  284. static void jtag_prelude(tap_state_t state)
  285. {
  286. jtag_checks();
  287. assert(state != TAP_INVALID);
  288. cmd_queue_cur_state = state;
  289. }
  290. void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
  291. tap_state_t state)
  292. {
  293. jtag_prelude(state);
  294. int retval = interface_jtag_add_ir_scan(active, in_fields, state);
  295. jtag_set_error(retval);
  296. }
  297. static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
  298. int dummy,
  299. const struct scan_field *in_fields,
  300. tap_state_t state)
  301. {
  302. jtag_add_ir_scan_noverify(active, in_fields, state);
  303. }
  304. /* If fields->in_value is filled out, then the captured IR value will be checked */
  305. void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
  306. {
  307. assert(state != TAP_RESET);
  308. if (jtag_verify && jtag_verify_capture_ir) {
  309. /* 8 x 32 bit id's is enough for all invocations */
  310. /* if we are to run a verification of the ir scan, we need to get the input back.
  311. * We may have to allocate space if the caller didn't ask for the input back.
  312. */
  313. in_fields->check_value = active->expected;
  314. in_fields->check_mask = active->expected_mask;
  315. jtag_add_scan_check(active, jtag_add_ir_scan_noverify_callback, 1, in_fields,
  316. state);
  317. } else
  318. jtag_add_ir_scan_noverify(active, in_fields, state);
  319. }
  320. void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
  321. tap_state_t state)
  322. {
  323. assert(out_bits != NULL);
  324. assert(state != TAP_RESET);
  325. jtag_prelude(state);
  326. int retval = interface_jtag_add_plain_ir_scan(
  327. num_bits, out_bits, in_bits, state);
  328. jtag_set_error(retval);
  329. }
  330. static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
  331. uint8_t *in_check_mask, int num_bits);
  332. static int jtag_check_value_mask_callback(jtag_callback_data_t data0,
  333. jtag_callback_data_t data1,
  334. jtag_callback_data_t data2,
  335. jtag_callback_data_t data3)
  336. {
  337. return jtag_check_value_inner((uint8_t *)data0,
  338. (uint8_t *)data1,
  339. (uint8_t *)data2,
  340. (int)data3);
  341. }
  342. static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
  343. struct jtag_tap *active,
  344. int in_num_fields,
  345. const struct scan_field *in_fields,
  346. tap_state_t state),
  347. int in_num_fields, struct scan_field *in_fields, tap_state_t state)
  348. {
  349. jtag_add_scan(active, in_num_fields, in_fields, state);
  350. for (int i = 0; i < in_num_fields; i++) {
  351. if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL)) {
  352. jtag_add_callback4(jtag_check_value_mask_callback,
  353. (jtag_callback_data_t)in_fields[i].in_value,
  354. (jtag_callback_data_t)in_fields[i].check_value,
  355. (jtag_callback_data_t)in_fields[i].check_mask,
  356. (jtag_callback_data_t)in_fields[i].num_bits);
  357. }
  358. }
  359. }
  360. void jtag_add_dr_scan_check(struct jtag_tap *active,
  361. int in_num_fields,
  362. struct scan_field *in_fields,
  363. tap_state_t state)
  364. {
  365. if (jtag_verify)
  366. jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
  367. else
  368. jtag_add_dr_scan(active, in_num_fields, in_fields, state);
  369. }
  370. void jtag_add_dr_scan(struct jtag_tap *active,
  371. int in_num_fields,
  372. const struct scan_field *in_fields,
  373. tap_state_t state)
  374. {
  375. assert(state != TAP_RESET);
  376. jtag_prelude(state);
  377. int retval;
  378. retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
  379. jtag_set_error(retval);
  380. }
  381. void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
  382. tap_state_t state)
  383. {
  384. assert(out_bits != NULL);
  385. assert(state != TAP_RESET);
  386. jtag_prelude(state);
  387. int retval;
  388. retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
  389. jtag_set_error(retval);
  390. }
  391. void jtag_add_tlr(void)
  392. {
  393. jtag_prelude(TAP_RESET);
  394. jtag_set_error(interface_jtag_add_tlr());
  395. /* NOTE: order here matches TRST path in jtag_add_reset() */
  396. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  397. jtag_notify_event(JTAG_TRST_ASSERTED);
  398. }
  399. /**
  400. * If supported by the underlying adapter, this clocks a raw bit sequence
  401. * onto TMS for switching betwen JTAG and SWD modes.
  402. *
  403. * DO NOT use this to bypass the integrity checks and logging provided
  404. * by the jtag_add_pathmove() and jtag_add_statemove() calls.
  405. *
  406. * @param nbits How many bits to clock out.
  407. * @param seq The bit sequence. The LSB is bit 0 of seq[0].
  408. * @param state The JTAG tap state to record on completion. Use
  409. * TAP_INVALID to represent being in in SWD mode.
  410. *
  411. * @todo Update naming conventions to stop assuming everything is JTAG.
  412. */
  413. int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
  414. {
  415. int retval;
  416. if (!(jtag->jtag_ops->supported & DEBUG_CAP_TMS_SEQ))
  417. return ERROR_JTAG_NOT_IMPLEMENTED;
  418. jtag_checks();
  419. cmd_queue_cur_state = state;
  420. retval = interface_add_tms_seq(nbits, seq, state);
  421. jtag_set_error(retval);
  422. return retval;
  423. }
  424. void jtag_add_pathmove(int num_states, const tap_state_t *path)
  425. {
  426. tap_state_t cur_state = cmd_queue_cur_state;
  427. /* the last state has to be a stable state */
  428. if (!tap_is_state_stable(path[num_states - 1])) {
  429. LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
  430. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  431. return;
  432. }
  433. for (int i = 0; i < num_states; i++) {
  434. if (path[i] == TAP_RESET) {
  435. LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
  436. jtag_set_error(ERROR_JTAG_STATE_INVALID);
  437. return;
  438. }
  439. if (tap_state_transition(cur_state, true) != path[i] &&
  440. tap_state_transition(cur_state, false) != path[i]) {
  441. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  442. tap_state_name(cur_state), tap_state_name(path[i]));
  443. jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
  444. return;
  445. }
  446. cur_state = path[i];
  447. }
  448. jtag_checks();
  449. jtag_set_error(interface_jtag_add_pathmove(num_states, path));
  450. cmd_queue_cur_state = path[num_states - 1];
  451. }
  452. int jtag_add_statemove(tap_state_t goal_state)
  453. {
  454. tap_state_t cur_state = cmd_queue_cur_state;
  455. if (goal_state != cur_state) {
  456. LOG_DEBUG("cur_state=%s goal_state=%s",
  457. tap_state_name(cur_state),
  458. tap_state_name(goal_state));
  459. }
  460. /* If goal is RESET, be paranoid and force that that transition
  461. * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
  462. */
  463. if (goal_state == TAP_RESET)
  464. jtag_add_tlr();
  465. else if (goal_state == cur_state)
  466. /* nothing to do */;
  467. else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state)) {
  468. unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
  469. unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
  470. tap_state_t moves[8];
  471. assert(tms_count < ARRAY_SIZE(moves));
  472. for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
  473. bool bit = tms_bits & 1;
  474. cur_state = tap_state_transition(cur_state, bit);
  475. moves[i] = cur_state;
  476. }
  477. jtag_add_pathmove(tms_count, moves);
  478. } else if (tap_state_transition(cur_state, true) == goal_state
  479. || tap_state_transition(cur_state, false) == goal_state)
  480. jtag_add_pathmove(1, &goal_state);
  481. else
  482. return ERROR_FAIL;
  483. return ERROR_OK;
  484. }
  485. void jtag_add_runtest(int num_cycles, tap_state_t state)
  486. {
  487. jtag_prelude(state);
  488. jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
  489. }
  490. void jtag_add_clocks(int num_cycles)
  491. {
  492. if (!tap_is_state_stable(cmd_queue_cur_state)) {
  493. LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
  494. tap_state_name(cmd_queue_cur_state));
  495. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  496. return;
  497. }
  498. if (num_cycles > 0) {
  499. jtag_checks();
  500. jtag_set_error(interface_jtag_add_clocks(num_cycles));
  501. }
  502. }
  503. static int adapter_system_reset(int req_srst)
  504. {
  505. int retval;
  506. if (req_srst) {
  507. if (!(jtag_reset_config & RESET_HAS_SRST)) {
  508. LOG_ERROR("BUG: can't assert SRST");
  509. return ERROR_FAIL;
  510. }
  511. req_srst = 1;
  512. }
  513. /* Maybe change SRST signal state */
  514. if (jtag_srst != req_srst) {
  515. retval = jtag->reset(0, req_srst);
  516. if (retval != ERROR_OK) {
  517. LOG_ERROR("SRST error");
  518. return ERROR_FAIL;
  519. }
  520. jtag_srst = req_srst;
  521. if (req_srst) {
  522. LOG_DEBUG("SRST line asserted");
  523. if (adapter_nsrst_assert_width)
  524. jtag_sleep(adapter_nsrst_assert_width * 1000);
  525. } else {
  526. LOG_DEBUG("SRST line released");
  527. if (adapter_nsrst_delay)
  528. jtag_sleep(adapter_nsrst_delay * 1000);
  529. }
  530. }
  531. return ERROR_OK;
  532. }
  533. static void legacy_jtag_add_reset(int req_tlr_or_trst, int req_srst)
  534. {
  535. int trst_with_tlr = 0;
  536. int new_srst = 0;
  537. int new_trst = 0;
  538. /* Without SRST, we must use target-specific JTAG operations
  539. * on each target; callers should not be requesting SRST when
  540. * that signal doesn't exist.
  541. *
  542. * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
  543. * can kick in even if the JTAG adapter can't drive TRST.
  544. */
  545. if (req_srst) {
  546. if (!(jtag_reset_config & RESET_HAS_SRST)) {
  547. LOG_ERROR("BUG: can't assert SRST");
  548. jtag_set_error(ERROR_FAIL);
  549. return;
  550. }
  551. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
  552. && !req_tlr_or_trst) {
  553. LOG_ERROR("BUG: can't assert only SRST");
  554. jtag_set_error(ERROR_FAIL);
  555. return;
  556. }
  557. new_srst = 1;
  558. }
  559. /* JTAG reset (entry to TAP_RESET state) can always be achieved
  560. * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
  561. * state first. TRST accelerates it, and bypasses those states.
  562. *
  563. * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
  564. * can kick in even if the JTAG adapter can't drive SRST.
  565. */
  566. if (req_tlr_or_trst) {
  567. if (!(jtag_reset_config & RESET_HAS_TRST))
  568. trst_with_tlr = 1;
  569. else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
  570. && !req_srst)
  571. trst_with_tlr = 1;
  572. else
  573. new_trst = 1;
  574. }
  575. /* Maybe change TRST and/or SRST signal state */
  576. if (jtag_srst != new_srst || jtag_trst != new_trst) {
  577. int retval;
  578. retval = interface_jtag_add_reset(new_trst, new_srst);
  579. if (retval != ERROR_OK)
  580. jtag_set_error(retval);
  581. else
  582. retval = jtag_execute_queue();
  583. if (retval != ERROR_OK) {
  584. LOG_ERROR("TRST/SRST error");
  585. return;
  586. }
  587. }
  588. /* SRST resets everything hooked up to that signal */
  589. if (jtag_srst != new_srst) {
  590. jtag_srst = new_srst;
  591. if (jtag_srst) {
  592. LOG_DEBUG("SRST line asserted");
  593. if (adapter_nsrst_assert_width)
  594. jtag_add_sleep(adapter_nsrst_assert_width * 1000);
  595. } else {
  596. LOG_DEBUG("SRST line released");
  597. if (adapter_nsrst_delay)
  598. jtag_add_sleep(adapter_nsrst_delay * 1000);
  599. }
  600. }
  601. /* Maybe enter the JTAG TAP_RESET state ...
  602. * - using only TMS, TCK, and the JTAG state machine
  603. * - or else more directly, using TRST
  604. *
  605. * TAP_RESET should be invisible to non-debug parts of the system.
  606. */
  607. if (trst_with_tlr) {
  608. LOG_DEBUG("JTAG reset with TLR instead of TRST");
  609. jtag_add_tlr();
  610. } else if (jtag_trst != new_trst) {
  611. jtag_trst = new_trst;
  612. if (jtag_trst) {
  613. LOG_DEBUG("TRST line asserted");
  614. tap_set_state(TAP_RESET);
  615. if (jtag_ntrst_assert_width)
  616. jtag_add_sleep(jtag_ntrst_assert_width * 1000);
  617. } else {
  618. LOG_DEBUG("TRST line released");
  619. if (jtag_ntrst_delay)
  620. jtag_add_sleep(jtag_ntrst_delay * 1000);
  621. /* We just asserted nTRST, so we're now in TAP_RESET.
  622. * Inform possible listeners about this, now that
  623. * JTAG instructions and data can be shifted. This
  624. * sequence must match jtag_add_tlr().
  625. */
  626. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  627. jtag_notify_event(JTAG_TRST_ASSERTED);
  628. }
  629. }
  630. }
  631. /* FIXME: name is misleading; we do not plan to "add" reset into jtag queue */
  632. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  633. {
  634. int retval;
  635. int trst_with_tlr = 0;
  636. int new_srst = 0;
  637. int new_trst = 0;
  638. if (!jtag->reset) {
  639. legacy_jtag_add_reset(req_tlr_or_trst, req_srst);
  640. return;
  641. }
  642. /* Without SRST, we must use target-specific JTAG operations
  643. * on each target; callers should not be requesting SRST when
  644. * that signal doesn't exist.
  645. *
  646. * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
  647. * can kick in even if the JTAG adapter can't drive TRST.
  648. */
  649. if (req_srst) {
  650. if (!(jtag_reset_config & RESET_HAS_SRST)) {
  651. LOG_ERROR("BUG: can't assert SRST");
  652. jtag_set_error(ERROR_FAIL);
  653. return;
  654. }
  655. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
  656. && !req_tlr_or_trst) {
  657. LOG_ERROR("BUG: can't assert only SRST");
  658. jtag_set_error(ERROR_FAIL);
  659. return;
  660. }
  661. new_srst = 1;
  662. }
  663. /* JTAG reset (entry to TAP_RESET state) can always be achieved
  664. * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
  665. * state first. TRST accelerates it, and bypasses those states.
  666. *
  667. * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
  668. * can kick in even if the JTAG adapter can't drive SRST.
  669. */
  670. if (req_tlr_or_trst) {
  671. if (!(jtag_reset_config & RESET_HAS_TRST))
  672. trst_with_tlr = 1;
  673. else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
  674. && !req_srst)
  675. trst_with_tlr = 1;
  676. else
  677. new_trst = 1;
  678. }
  679. /* Maybe change TRST and/or SRST signal state */
  680. if (jtag_srst != new_srst || jtag_trst != new_trst) {
  681. /* guarantee jtag queue empty before changing reset status */
  682. jtag_execute_queue();
  683. retval = jtag->reset(new_trst, new_srst);
  684. if (retval != ERROR_OK) {
  685. jtag_set_error(retval);
  686. LOG_ERROR("TRST/SRST error");
  687. return;
  688. }
  689. }
  690. /* SRST resets everything hooked up to that signal */
  691. if (jtag_srst != new_srst) {
  692. jtag_srst = new_srst;
  693. if (jtag_srst) {
  694. LOG_DEBUG("SRST line asserted");
  695. if (adapter_nsrst_assert_width)
  696. jtag_add_sleep(adapter_nsrst_assert_width * 1000);
  697. } else {
  698. LOG_DEBUG("SRST line released");
  699. if (adapter_nsrst_delay)
  700. jtag_add_sleep(adapter_nsrst_delay * 1000);
  701. }
  702. }
  703. /* Maybe enter the JTAG TAP_RESET state ...
  704. * - using only TMS, TCK, and the JTAG state machine
  705. * - or else more directly, using TRST
  706. *
  707. * TAP_RESET should be invisible to non-debug parts of the system.
  708. */
  709. if (trst_with_tlr) {
  710. LOG_DEBUG("JTAG reset with TLR instead of TRST");
  711. jtag_add_tlr();
  712. jtag_execute_queue();
  713. } else if (jtag_trst != new_trst) {
  714. jtag_trst = new_trst;
  715. if (jtag_trst) {
  716. LOG_DEBUG("TRST line asserted");
  717. tap_set_state(TAP_RESET);
  718. if (jtag_ntrst_assert_width)
  719. jtag_add_sleep(jtag_ntrst_assert_width * 1000);
  720. } else {
  721. LOG_DEBUG("TRST line released");
  722. if (jtag_ntrst_delay)
  723. jtag_add_sleep(jtag_ntrst_delay * 1000);
  724. /* We just asserted nTRST, so we're now in TAP_RESET.
  725. * Inform possible listeners about this, now that
  726. * JTAG instructions and data can be shifted. This
  727. * sequence must match jtag_add_tlr().
  728. */
  729. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  730. jtag_notify_event(JTAG_TRST_ASSERTED);
  731. }
  732. }
  733. }
  734. void jtag_add_sleep(uint32_t us)
  735. {
  736. /** @todo Here, keep_alive() appears to be a layering violation!!! */
  737. keep_alive();
  738. jtag_set_error(interface_jtag_add_sleep(us));
  739. }
  740. static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
  741. uint8_t *in_check_mask, int num_bits)
  742. {
  743. int retval = ERROR_OK;
  744. int compare_failed;
  745. if (in_check_mask)
  746. compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
  747. else
  748. compare_failed = buf_cmp(captured, in_check_value, num_bits);
  749. if (compare_failed) {
  750. char *captured_str, *in_check_value_str;
  751. int bits = (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits;
  752. /* NOTE: we've lost diagnostic context here -- 'which tap' */
  753. captured_str = buf_to_hex_str(captured, bits);
  754. in_check_value_str = buf_to_hex_str(in_check_value, bits);
  755. LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
  756. captured_str);
  757. LOG_WARNING(" check_value: 0x%s", in_check_value_str);
  758. free(captured_str);
  759. free(in_check_value_str);
  760. if (in_check_mask) {
  761. char *in_check_mask_str;
  762. in_check_mask_str = buf_to_hex_str(in_check_mask, bits);
  763. LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
  764. free(in_check_mask_str);
  765. }
  766. retval = ERROR_JTAG_QUEUE_FAILED;
  767. }
  768. return retval;
  769. }
  770. void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
  771. {
  772. assert(field->in_value != NULL);
  773. if (value == NULL) {
  774. /* no checking to do */
  775. return;
  776. }
  777. jtag_execute_queue_noclear();
  778. int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
  779. jtag_set_error(retval);
  780. }
  781. int default_interface_jtag_execute_queue(void)
  782. {
  783. if (NULL == jtag) {
  784. LOG_ERROR("No JTAG interface configured yet. "
  785. "Issue 'init' command in startup scripts "
  786. "before communicating with targets.");
  787. return ERROR_FAIL;
  788. }
  789. if (!transport_is_jtag()) {
  790. /*
  791. * FIXME: This should not happen!
  792. * There could be old code that queues jtag commands with non jtag interfaces so, for
  793. * the moment simply highlight it by log an error and return on empty execute_queue.
  794. * We should fix it quitting with assert(0) because it is an internal error.
  795. * The fix can be applied immediately after next release (v0.11.0 ?)
  796. */
  797. LOG_ERROR("JTAG API jtag_execute_queue() called on non JTAG interface");
  798. if (!jtag->jtag_ops || !jtag->jtag_ops->execute_queue)
  799. return ERROR_OK;
  800. }
  801. int result = jtag->jtag_ops->execute_queue();
  802. struct jtag_command *cmd = jtag_command_queue;
  803. while (debug_level >= LOG_LVL_DEBUG_IO && cmd) {
  804. switch (cmd->type) {
  805. case JTAG_SCAN:
  806. LOG_DEBUG_IO("JTAG %s SCAN to %s",
  807. cmd->cmd.scan->ir_scan ? "IR" : "DR",
  808. tap_state_name(cmd->cmd.scan->end_state));
  809. for (int i = 0; i < cmd->cmd.scan->num_fields; i++) {
  810. struct scan_field *field = cmd->cmd.scan->fields + i;
  811. if (field->out_value) {
  812. char *str = buf_to_hex_str(field->out_value, field->num_bits);
  813. LOG_DEBUG_IO(" %db out: %s", field->num_bits, str);
  814. free(str);
  815. }
  816. if (field->in_value) {
  817. char *str = buf_to_hex_str(field->in_value, field->num_bits);
  818. LOG_DEBUG_IO(" %db in: %s", field->num_bits, str);
  819. free(str);
  820. }
  821. }
  822. break;
  823. case JTAG_TLR_RESET:
  824. LOG_DEBUG_IO("JTAG TLR RESET to %s",
  825. tap_state_name(cmd->cmd.statemove->end_state));
  826. break;
  827. case JTAG_RUNTEST:
  828. LOG_DEBUG_IO("JTAG RUNTEST %d cycles to %s",
  829. cmd->cmd.runtest->num_cycles,
  830. tap_state_name(cmd->cmd.runtest->end_state));
  831. break;
  832. case JTAG_RESET:
  833. {
  834. const char *reset_str[3] = {
  835. "leave", "deassert", "assert"
  836. };
  837. LOG_DEBUG_IO("JTAG RESET %s TRST, %s SRST",
  838. reset_str[cmd->cmd.reset->trst + 1],
  839. reset_str[cmd->cmd.reset->srst + 1]);
  840. }
  841. break;
  842. case JTAG_PATHMOVE:
  843. LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
  844. break;
  845. case JTAG_SLEEP:
  846. LOG_DEBUG_IO("JTAG SLEEP (TODO)");
  847. break;
  848. case JTAG_STABLECLOCKS:
  849. LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
  850. break;
  851. case JTAG_TMS:
  852. LOG_DEBUG_IO("JTAG TMS (TODO)");
  853. break;
  854. default:
  855. LOG_ERROR("Unknown JTAG command: %d", cmd->type);
  856. break;
  857. }
  858. cmd = cmd->next;
  859. }
  860. return result;
  861. }
  862. void jtag_execute_queue_noclear(void)
  863. {
  864. jtag_flush_queue_count++;
  865. jtag_set_error(interface_jtag_execute_queue());
  866. if (jtag_flush_queue_sleep > 0) {
  867. /* For debug purposes it can be useful to test performance
  868. * or behavior when delaying after flushing the queue,
  869. * e.g. to simulate long roundtrip times.
  870. */
  871. usleep(jtag_flush_queue_sleep * 1000);
  872. }
  873. }
  874. int jtag_get_flush_queue_count(void)
  875. {
  876. return jtag_flush_queue_count;
  877. }
  878. int jtag_execute_queue(void)
  879. {
  880. jtag_execute_queue_noclear();
  881. return jtag_error_clear();
  882. }
  883. static int jtag_reset_callback(enum jtag_event event, void *priv)
  884. {
  885. struct jtag_tap *tap = priv;
  886. if (event == JTAG_TRST_ASSERTED) {
  887. tap->enabled = !tap->disabled_after_reset;
  888. /* current instruction is either BYPASS or IDCODE */
  889. buf_set_ones(tap->cur_instr, tap->ir_length);
  890. tap->bypass = 1;
  891. }
  892. return ERROR_OK;
  893. }
  894. /* sleep at least us microseconds. When we sleep more than 1000ms we
  895. * do an alive sleep, i.e. keep GDB alive. Note that we could starve
  896. * GDB if we slept for <1000ms many times.
  897. */
  898. void jtag_sleep(uint32_t us)
  899. {
  900. if (us < 1000)
  901. usleep(us);
  902. else
  903. alive_sleep((us+999)/1000);
  904. }
  905. #define JTAG_MAX_AUTO_TAPS 20
  906. #define EXTRACT_JEP106_BANK(X) (((X) & 0xf00) >> 8)
  907. #define EXTRACT_JEP106_ID(X) (((X) & 0xfe) >> 1)
  908. #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
  909. #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
  910. #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
  911. /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
  912. * know that no valid TAP will have it as an IDCODE value.
  913. */
  914. #define END_OF_CHAIN_FLAG 0xffffffff
  915. /* a larger IR length than we ever expect to autoprobe */
  916. #define JTAG_IRLEN_MAX 60
  917. static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
  918. {
  919. struct scan_field field = {
  920. .num_bits = num_idcode * 32,
  921. .out_value = idcode_buffer,
  922. .in_value = idcode_buffer,
  923. };
  924. /* initialize to the end of chain ID value */
  925. for (unsigned i = 0; i < num_idcode; i++)
  926. buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
  927. jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
  928. jtag_add_tlr();
  929. return jtag_execute_queue();
  930. }
  931. static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
  932. {
  933. uint8_t zero_check = 0x0;
  934. uint8_t one_check = 0xff;
  935. for (unsigned i = 0; i < count * 4; i++) {
  936. zero_check |= idcodes[i];
  937. one_check &= idcodes[i];
  938. }
  939. /* if there wasn't a single non-zero bit or if all bits were one,
  940. * the scan is not valid. We wrote a mix of both values; either
  941. *
  942. * - There's a hardware issue (almost certainly):
  943. * + all-zeroes can mean a target stuck in JTAG reset
  944. * + all-ones tends to mean no target
  945. * - The scan chain is WAY longer than we can handle, *AND* either
  946. * + there are several hundreds of TAPs in bypass, or
  947. * + at least a few dozen TAPs all have an all-ones IDCODE
  948. */
  949. if (zero_check == 0x00 || one_check == 0xff) {
  950. LOG_ERROR("JTAG scan chain interrogation failed: all %s",
  951. (zero_check == 0x00) ? "zeroes" : "ones");
  952. LOG_ERROR("Check JTAG interface, timings, target power, etc.");
  953. return false;
  954. }
  955. return true;
  956. }
  957. static void jtag_examine_chain_display(enum log_levels level, const char *msg,
  958. const char *name, uint32_t idcode)
  959. {
  960. log_printf_lf(level, __FILE__, __LINE__, __func__,
  961. "JTAG tap: %s %16.16s: 0x%08x "
  962. "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
  963. name, msg,
  964. (unsigned int)idcode,
  965. (unsigned int)EXTRACT_MFG(idcode),
  966. jep106_manufacturer(EXTRACT_JEP106_BANK(idcode), EXTRACT_JEP106_ID(idcode)),
  967. (unsigned int)EXTRACT_PART(idcode),
  968. (unsigned int)EXTRACT_VER(idcode));
  969. }
  970. static bool jtag_idcode_is_final(uint32_t idcode)
  971. {
  972. /*
  973. * Some devices, such as AVR8, will output all 1's instead
  974. * of TDI input value at end of chain. Allow those values
  975. * instead of failing.
  976. */
  977. return idcode == END_OF_CHAIN_FLAG;
  978. }
  979. /**
  980. * This helper checks that remaining bits in the examined chain data are
  981. * all as expected, but a single JTAG device requires only 64 bits to be
  982. * read back correctly. This can help identify and diagnose problems
  983. * with the JTAG chain earlier, gives more helpful/explicit error messages.
  984. * Returns TRUE iff garbage was found.
  985. */
  986. static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
  987. {
  988. bool triggered = false;
  989. for (; count < max - 31; count += 32) {
  990. uint32_t idcode = buf_get_u32(idcodes, count, 32);
  991. /* do not trigger the warning if the data looks good */
  992. if (jtag_idcode_is_final(idcode))
  993. continue;
  994. LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
  995. count, (unsigned int)idcode);
  996. triggered = true;
  997. }
  998. return triggered;
  999. }
  1000. static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
  1001. {
  1002. if (tap->expected_ids_cnt == 0 || !tap->hasidcode)
  1003. return true;
  1004. /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
  1005. uint32_t mask = tap->ignore_version ? ~(0xfU << 28) : ~0U;
  1006. uint32_t idcode = tap->idcode & mask;
  1007. /* Loop over the expected identification codes and test for a match */
  1008. for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
  1009. uint32_t expected = tap->expected_ids[ii] & mask;
  1010. if (idcode == expected)
  1011. return true;
  1012. /* treat "-expected-id 0" as a "don't-warn" wildcard */
  1013. if (0 == tap->expected_ids[ii])
  1014. return true;
  1015. }
  1016. /* If none of the expected ids matched, warn */
  1017. jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
  1018. tap->dotted_name, tap->idcode);
  1019. for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
  1020. char msg[32];
  1021. snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, tap->expected_ids_cnt);
  1022. jtag_examine_chain_display(LOG_LVL_ERROR, msg,
  1023. tap->dotted_name, tap->expected_ids[ii]);
  1024. }
  1025. return false;
  1026. }
  1027. /* Try to examine chain layout according to IEEE 1149.1 ยง12
  1028. * This is called a "blind interrogation" of the scan chain.
  1029. */
  1030. static int jtag_examine_chain(void)
  1031. {
  1032. int retval;
  1033. unsigned max_taps = jtag_tap_count();
  1034. /* Autoprobe up to this many. */
  1035. if (max_taps < JTAG_MAX_AUTO_TAPS)
  1036. max_taps = JTAG_MAX_AUTO_TAPS;
  1037. /* Add room for end-of-chain marker. */
  1038. max_taps++;
  1039. uint8_t *idcode_buffer = calloc(4, max_taps);
  1040. if (idcode_buffer == NULL)
  1041. return ERROR_JTAG_INIT_FAILED;
  1042. /* DR scan to collect BYPASS or IDCODE register contents.
  1043. * Then make sure the scan data has both ones and zeroes.
  1044. */
  1045. LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
  1046. retval = jtag_examine_chain_execute(idcode_buffer, max_taps);
  1047. if (retval != ERROR_OK)
  1048. goto out;
  1049. if (!jtag_examine_chain_check(idcode_buffer, max_taps)) {
  1050. retval = ERROR_JTAG_INIT_FAILED;
  1051. goto out;
  1052. }
  1053. /* Point at the 1st predefined tap, if any */
  1054. struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
  1055. unsigned bit_count = 0;
  1056. unsigned autocount = 0;
  1057. for (unsigned i = 0; i < max_taps; i++) {
  1058. assert(bit_count < max_taps * 32);
  1059. uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  1060. /* No predefined TAP? Auto-probe. */
  1061. if (tap == NULL) {
  1062. /* Is there another TAP? */
  1063. if (jtag_idcode_is_final(idcode))
  1064. break;
  1065. /* Default everything in this TAP except IR length.
  1066. *
  1067. * REVISIT create a jtag_alloc(chip, tap) routine, and
  1068. * share it with jim_newtap_cmd().
  1069. */
  1070. tap = calloc(1, sizeof(*tap));
  1071. if (!tap) {
  1072. retval = ERROR_FAIL;
  1073. goto out;
  1074. }
  1075. tap->chip = alloc_printf("auto%u", autocount++);
  1076. tap->tapname = strdup("tap");
  1077. tap->dotted_name = alloc_printf("%s.%s", tap->chip, tap->tapname);
  1078. tap->ir_length = 0; /* ... signifying irlen autoprobe */
  1079. tap->ir_capture_mask = 0x03;
  1080. tap->ir_capture_value = 0x01;
  1081. tap->enabled = true;
  1082. jtag_tap_init(tap);
  1083. }
  1084. if ((idcode & 1) == 0) {
  1085. /* Zero for LSB indicates a device in bypass */
  1086. LOG_INFO("TAP %s does not have valid IDCODE (idcode=0x%" PRIx32 ")",
  1087. tap->dotted_name, idcode);
  1088. tap->hasidcode = false;
  1089. tap->idcode = 0;
  1090. bit_count += 1;
  1091. } else {
  1092. /* Friendly devices support IDCODE */
  1093. tap->hasidcode = true;
  1094. tap->idcode = idcode;
  1095. jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found", tap->dotted_name, idcode);
  1096. bit_count += 32;
  1097. }
  1098. /* ensure the TAP ID matches what was expected */
  1099. if (!jtag_examine_chain_match_tap(tap))
  1100. retval = ERROR_JTAG_INIT_SOFT_FAIL;
  1101. tap = jtag_tap_next_enabled(tap);
  1102. }
  1103. /* After those IDCODE or BYPASS register values should be
  1104. * only the data we fed into the scan chain.
  1105. */
  1106. if (jtag_examine_chain_end(idcode_buffer, bit_count, max_taps * 32)) {
  1107. LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
  1108. retval = ERROR_JTAG_INIT_FAILED;
  1109. goto out;
  1110. }
  1111. /* Return success or, for backwards compatibility if only
  1112. * some IDCODE values mismatched, a soft/continuable fault.
  1113. */
  1114. out:
  1115. free(idcode_buffer);
  1116. return retval;
  1117. }
  1118. /*
  1119. * Validate the date loaded by entry to the Capture-IR state, to help
  1120. * find errors related to scan chain configuration (wrong IR lengths)
  1121. * or communication.
  1122. *
  1123. * Entry state can be anything. On non-error exit, all TAPs are in
  1124. * bypass mode. On error exits, the scan chain is reset.
  1125. */
  1126. static int jtag_validate_ircapture(void)
  1127. {
  1128. struct jtag_tap *tap;
  1129. int total_ir_length = 0;
  1130. uint8_t *ir_test = NULL;
  1131. struct scan_field field;
  1132. uint64_t val;
  1133. int chain_pos = 0;
  1134. int retval;
  1135. /* when autoprobing, accomodate huge IR lengths */
  1136. for (tap = NULL, total_ir_length = 0;
  1137. (tap = jtag_tap_next_enabled(tap)) != NULL;
  1138. total_ir_length += tap->ir_length) {
  1139. if (tap->ir_length == 0)
  1140. total_ir_length += JTAG_IRLEN_MAX;
  1141. }
  1142. /* increase length to add 2 bit sentinel after scan */
  1143. total_ir_length += 2;
  1144. ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
  1145. if (ir_test == NULL)
  1146. return ERROR_FAIL;
  1147. /* after this scan, all TAPs will capture BYPASS instructions */
  1148. buf_set_ones(ir_test, total_ir_length);
  1149. field.num_bits = total_ir_length;
  1150. field.out_value = ir_test;
  1151. field.in_value = ir_test;
  1152. jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
  1153. LOG_DEBUG("IR capture validation scan");
  1154. retval = jtag_execute_queue();
  1155. if (retval != ERROR_OK)
  1156. goto done;
  1157. tap = NULL;
  1158. chain_pos = 0;
  1159. for (;; ) {
  1160. tap = jtag_tap_next_enabled(tap);
  1161. if (tap == NULL)
  1162. break;
  1163. /* If we're autoprobing, guess IR lengths. They must be at
  1164. * least two bits. Guessing will fail if (a) any TAP does
  1165. * not conform to the JTAG spec; or (b) when the upper bits
  1166. * captured from some conforming TAP are nonzero. Or if
  1167. * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
  1168. * an implementation limit, which could someday be raised.
  1169. *
  1170. * REVISIT optimization: if there's a *single* TAP we can
  1171. * lift restrictions (a) and (b) by scanning a recognizable
  1172. * pattern before the all-ones BYPASS. Check for where the
  1173. * pattern starts in the result, instead of an 0...01 value.
  1174. *
  1175. * REVISIT alternative approach: escape to some tcl code
  1176. * which could provide more knowledge, based on IDCODE; and
  1177. * only guess when that has no success.
  1178. */
  1179. if (tap->ir_length == 0) {
  1180. tap->ir_length = 2;
  1181. while ((val = buf_get_u64(ir_test, chain_pos, tap->ir_length + 1)) == 1
  1182. && tap->ir_length < JTAG_IRLEN_MAX) {
  1183. tap->ir_length++;
  1184. }
  1185. LOG_WARNING("AUTO %s - use \"jtag newtap %s %s -irlen %d "
  1186. "-expected-id 0x%08" PRIx32 "\"",
  1187. tap->dotted_name, tap->chip, tap->tapname, tap->ir_length, tap->idcode);
  1188. }
  1189. /* Validate the two LSBs, which must be 01 per JTAG spec.
  1190. *
  1191. * Or ... more bits could be provided by TAP declaration.
  1192. * Plus, some taps (notably in i.MX series chips) violate
  1193. * this part of the JTAG spec, so their capture mask/value
  1194. * attributes might disable this test.
  1195. */
  1196. val = buf_get_u64(ir_test, chain_pos, tap->ir_length);
  1197. if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
  1198. LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64 " not 0x%0*" PRIx32,
  1199. jtag_tap_name(tap),
  1200. (tap->ir_length + 7) / tap->ir_length, val,
  1201. (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
  1202. retval = ERROR_JTAG_INIT_FAILED;
  1203. goto done;
  1204. }
  1205. LOG_DEBUG("%s: IR capture 0x%0*" PRIx64, jtag_tap_name(tap),
  1206. (tap->ir_length + 7) / tap->ir_length, val);
  1207. chain_pos += tap->ir_length;
  1208. }
  1209. /* verify the '11' sentinel we wrote is returned at the end */
  1210. val = buf_get_u64(ir_test, chain_pos, 2);
  1211. if (val != 0x3) {
  1212. char *cbuf = buf_to_hex_str(ir_test, total_ir_length);
  1213. LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
  1214. chain_pos, cbuf);
  1215. free(cbuf);
  1216. retval = ERROR_JTAG_INIT_FAILED;
  1217. }
  1218. done:
  1219. free(ir_test);
  1220. if (retval != ERROR_OK) {
  1221. jtag_add_tlr();
  1222. jtag_execute_queue();
  1223. }
  1224. return retval;
  1225. }
  1226. void jtag_tap_init(struct jtag_tap *tap)
  1227. {
  1228. unsigned ir_len_bits;
  1229. unsigned ir_len_bytes;
  1230. /* if we're autoprobing, cope with potentially huge ir_length */
  1231. ir_len_bits = tap->ir_length ? : JTAG_IRLEN_MAX;
  1232. ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
  1233. tap->expected = calloc(1, ir_len_bytes);
  1234. tap->expected_mask = calloc(1, ir_len_bytes);
  1235. tap->cur_instr = malloc(ir_len_bytes);
  1236. /** @todo cope better with ir_length bigger than 32 bits */
  1237. if (ir_len_bits > 32)
  1238. ir_len_bits = 32;
  1239. buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
  1240. buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
  1241. /* TAP will be in bypass mode after jtag_validate_ircapture() */
  1242. tap->bypass = 1;
  1243. buf_set_ones(tap->cur_instr, tap->ir_length);
  1244. /* register the reset callback for the TAP */
  1245. jtag_register_event_callback(&jtag_reset_callback, tap);
  1246. jtag_tap_add(tap);
  1247. LOG_DEBUG("Created Tap: %s @ abs position %d, "
  1248. "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
  1249. tap->abs_chain_position, tap->ir_length,
  1250. (unsigned) tap->ir_capture_value,
  1251. (unsigned) tap->ir_capture_mask);
  1252. }
  1253. void jtag_tap_free(struct jtag_tap *tap)
  1254. {
  1255. jtag_unregister_event_callback(&jtag_reset_callback, tap);
  1256. struct jtag_tap_event_action *jteap = tap->event_action;
  1257. while (jteap) {
  1258. struct jtag_tap_event_action *next = jteap->next;
  1259. Jim_DecrRefCount(jteap->interp, jteap->body);
  1260. free(jteap);
  1261. jteap = next;
  1262. }
  1263. free(tap->expected);
  1264. free(tap->expected_mask);
  1265. free(tap->expected_ids);
  1266. free(tap->cur_instr);
  1267. free(tap->chip);
  1268. free(tap->tapname);
  1269. free(tap->dotted_name);
  1270. free(tap);
  1271. }
  1272. /**
  1273. * Do low-level setup like initializing registers, output signals,
  1274. * and clocking.
  1275. */
  1276. int adapter_init(struct command_context *cmd_ctx)
  1277. {
  1278. if (jtag)
  1279. return ERROR_OK;
  1280. if (!adapter_driver) {
  1281. /* nothing was previously specified by "adapter driver" command */
  1282. LOG_ERROR("Debug Adapter has to be specified, "
  1283. "see \"adapter driver\" command");
  1284. return ERROR_JTAG_INVALID_INTERFACE;
  1285. }
  1286. int retval;
  1287. retval = adapter_driver->init();
  1288. if (retval != ERROR_OK)
  1289. return retval;
  1290. jtag = adapter_driver;
  1291. if (jtag->speed == NULL) {
  1292. LOG_INFO("This adapter doesn't support configurable speed");
  1293. return ERROR_OK;
  1294. }
  1295. if (CLOCK_MODE_UNSELECTED == clock_mode) {
  1296. LOG_ERROR("An adapter speed is not selected in the init script."
  1297. " Insert a call to \"adapter speed\" or \"jtag_rclk\" to proceed.");
  1298. return ERROR_JTAG_INIT_FAILED;
  1299. }
  1300. int requested_khz = jtag_get_speed_khz();
  1301. int actual_khz = requested_khz;
  1302. int jtag_speed_var = 0;
  1303. retval = jtag_get_speed(&jtag_speed_var);
  1304. if (retval != ERROR_OK)
  1305. return retval;
  1306. retval = jtag->speed(jtag_speed_var);
  1307. if (retval != ERROR_OK)
  1308. return retval;
  1309. retval = jtag_get_speed_readable(&actual_khz);
  1310. if (ERROR_OK != retval)
  1311. LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
  1312. else if (actual_khz) {
  1313. /* Adaptive clocking -- JTAG-specific */
  1314. if ((CLOCK_MODE_RCLK == clock_mode)
  1315. || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz)) {
  1316. LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
  1317. , actual_khz);
  1318. } else
  1319. LOG_INFO("clock speed %d kHz", actual_khz);
  1320. } else
  1321. LOG_INFO("RCLK (adaptive clock speed)");
  1322. return ERROR_OK;
  1323. }
  1324. int jtag_init_inner(struct command_context *cmd_ctx)
  1325. {
  1326. struct jtag_tap *tap;
  1327. int retval;
  1328. bool issue_setup = true;
  1329. LOG_DEBUG("Init JTAG chain");
  1330. tap = jtag_tap_next_enabled(NULL);
  1331. if (tap == NULL) {
  1332. /* Once JTAG itself is properly set up, and the scan chain
  1333. * isn't absurdly large, IDCODE autoprobe should work fine.
  1334. *
  1335. * But ... IRLEN autoprobe can fail even on systems which
  1336. * are fully conformant to JTAG. Also, JTAG setup can be
  1337. * quite finicky on some systems.
  1338. *
  1339. * REVISIT: if TAP autoprobe works OK, then in many cases
  1340. * we could escape to tcl code and set up targets based on
  1341. * the TAP's IDCODE values.
  1342. */
  1343. LOG_WARNING("There are no enabled taps. "
  1344. "AUTO PROBING MIGHT NOT WORK!!");
  1345. /* REVISIT default clock will often be too fast ... */
  1346. }
  1347. jtag_add_tlr();
  1348. retval = jtag_execute_queue();
  1349. if (retval != ERROR_OK)
  1350. return retval;
  1351. /* Examine DR values first. This discovers problems which will
  1352. * prevent communication ... hardware issues like TDO stuck, or
  1353. * configuring the wrong number of (enabled) TAPs.
  1354. */
  1355. retval = jtag_examine_chain();
  1356. switch (retval) {
  1357. case ERROR_OK:
  1358. /* complete success */
  1359. break;
  1360. default:
  1361. /* For backward compatibility reasons, try coping with
  1362. * configuration errors involving only ID mismatches.
  1363. * We might be able to talk to the devices.
  1364. *
  1365. * Also the device might be powered down during startup.
  1366. *
  1367. * After OpenOCD starts, we can try to power on the device
  1368. * and run a reset.
  1369. */
  1370. LOG_ERROR("Trying to use configured scan chain anyway...");
  1371. issue_setup = false;
  1372. break;
  1373. }
  1374. /* Now look at IR values. Problems here will prevent real
  1375. * communication. They mostly mean that the IR length is
  1376. * wrong ... or that the IR capture value is wrong. (The
  1377. * latter is uncommon, but easily worked around: provide
  1378. * ircapture/irmask values during TAP setup.)
  1379. */
  1380. retval = jtag_validate_ircapture();
  1381. if (retval != ERROR_OK) {
  1382. /* The target might be powered down. The user
  1383. * can power it up and reset it after firing
  1384. * up OpenOCD.
  1385. */
  1386. issue_setup = false;
  1387. }
  1388. if (issue_setup)
  1389. jtag_notify_event(JTAG_TAP_EVENT_SETUP);
  1390. else
  1391. LOG_WARNING("Bypassing JTAG setup events due to errors");
  1392. return ERROR_OK;
  1393. }
  1394. int adapter_quit(void)
  1395. {
  1396. if (jtag && jtag->quit) {
  1397. /* close the JTAG interface */
  1398. int result = jtag->quit();
  1399. if (ERROR_OK != result)
  1400. LOG_ERROR("failed: %d", result);
  1401. }
  1402. struct jtag_tap *t = jtag_all_taps();
  1403. while (t) {
  1404. struct jtag_tap *n = t->next_tap;
  1405. jtag_tap_free(t);
  1406. t = n;
  1407. }
  1408. return ERROR_OK;
  1409. }
  1410. int swd_init_reset(struct command_context *cmd_ctx)
  1411. {
  1412. int retval, retval1;
  1413. retval = adapter_init(cmd_ctx);
  1414. if (retval != ERROR_OK)
  1415. return retval;
  1416. LOG_DEBUG("Initializing with hard SRST reset");
  1417. if (jtag_reset_config & RESET_HAS_SRST)
  1418. retval = adapter_system_reset(1);
  1419. retval1 = adapter_system_reset(0);
  1420. return (retval == ERROR_OK) ? retval1 : retval;
  1421. }
  1422. int jtag_init_reset(struct command_context *cmd_ctx)
  1423. {
  1424. int retval = adapter_init(cmd_ctx);
  1425. if (retval != ERROR_OK)
  1426. return retval;
  1427. LOG_DEBUG("Initializing with hard TRST+SRST reset");
  1428. /*
  1429. * This procedure is used by default when OpenOCD triggers a reset.
  1430. * It's now done through an overridable Tcl "init_reset" wrapper.
  1431. *
  1432. * This started out as a more powerful "get JTAG working" reset than
  1433. * jtag_init_inner(), applying TRST because some chips won't activate
  1434. * JTAG without a TRST cycle (presumed to be async, though some of
  1435. * those chips synchronize JTAG activation using TCK).
  1436. *
  1437. * But some chips only activate JTAG as part of an SRST cycle; SRST
  1438. * got mixed in. So it became a hard reset routine, which got used
  1439. * in more places, and which coped with JTAG reset being forced as
  1440. * part of SRST (srst_pulls_trst).
  1441. *
  1442. * And even more corner cases started to surface: TRST and/or SRST
  1443. * assertion timings matter; some chips need other JTAG operations;
  1444. * TRST/SRST sequences can need to be different from these, etc.
  1445. *
  1446. * Systems should override that wrapper to support system-specific
  1447. * requirements that this not-fully-generic code doesn't handle.
  1448. *
  1449. * REVISIT once Tcl code can read the reset_config modes, this won't
  1450. * need to be a C routine at all...
  1451. */
  1452. if (jtag_reset_config & RESET_HAS_SRST) {
  1453. jtag_add_reset(1, 1);
  1454. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
  1455. jtag_add_reset(0, 1);
  1456. } else {
  1457. jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
  1458. }
  1459. /* some targets enable us to connect with srst asserted */
  1460. if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
  1461. if (jtag_reset_config & RESET_SRST_NO_GATING)
  1462. jtag_add_reset(0, 1);
  1463. else {
  1464. LOG_WARNING("\'srst_nogate\' reset_config option is required");
  1465. jtag_add_reset(0, 0);
  1466. }
  1467. } else
  1468. jtag_add_reset(0, 0);
  1469. retval = jtag_execute_queue();
  1470. if (retval != ERROR_OK)
  1471. return retval;
  1472. /* Check that we can communication on the JTAG chain + eventually we want to
  1473. * be able to perform enumeration only after OpenOCD has started
  1474. * telnet and GDB server
  1475. *
  1476. * That would allow users to more easily perform any magic they need to before
  1477. * reset happens.
  1478. */
  1479. return jtag_init_inner(cmd_ctx);
  1480. }
  1481. int jtag_init(struct command_context *cmd_ctx)
  1482. {
  1483. int retval = adapter_init(cmd_ctx);
  1484. if (retval != ERROR_OK)
  1485. return retval;
  1486. /* guard against oddball hardware: force resets to be inactive */
  1487. jtag_add_reset(0, 0);
  1488. /* some targets enable us to connect with srst asserted */
  1489. if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
  1490. if (jtag_reset_config & RESET_SRST_NO_GATING)
  1491. jtag_add_reset(0, 1);
  1492. else
  1493. LOG_WARNING("\'srst_nogate\' reset_config option is required");
  1494. }
  1495. retval = jtag_execute_queue();
  1496. if (retval != ERROR_OK)
  1497. return retval;
  1498. if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
  1499. return ERROR_FAIL;
  1500. return ERROR_OK;
  1501. }
  1502. unsigned jtag_get_speed_khz(void)
  1503. {
  1504. return speed_khz;
  1505. }
  1506. static int adapter_khz_to_speed(unsigned khz, int *speed)
  1507. {
  1508. LOG_DEBUG("convert khz to interface specific speed value");
  1509. speed_khz = khz;
  1510. if (!jtag)
  1511. return ERROR_OK;
  1512. LOG_DEBUG("have interface set up");
  1513. if (!jtag->khz) {
  1514. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  1515. return ERROR_FAIL;
  1516. }
  1517. int speed_div1;
  1518. int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
  1519. if (ERROR_OK != retval)
  1520. return retval;
  1521. *speed = speed_div1;
  1522. return ERROR_OK;
  1523. }
  1524. static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int *speed)
  1525. {
  1526. int retval = adapter_khz_to_speed(0, speed);
  1527. if ((ERROR_OK != retval) && fallback_speed_khz) {
  1528. LOG_DEBUG("trying fallback speed...");
  1529. retval = adapter_khz_to_speed(fallback_speed_khz, speed);
  1530. }
  1531. return retval;
  1532. }
  1533. static int jtag_set_speed(int speed)
  1534. {
  1535. jtag_speed = speed;
  1536. /* this command can be called during CONFIG,
  1537. * in which case jtag isn't initialized */
  1538. return jtag ? jtag->speed(speed) : ERROR_OK;
  1539. }
  1540. int jtag_config_khz(unsigned khz)
  1541. {
  1542. LOG_DEBUG("handle jtag khz");
  1543. clock_mode = CLOCK_MODE_KHZ;
  1544. int speed = 0;
  1545. int retval = adapter_khz_to_speed(khz, &speed);
  1546. return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
  1547. }
  1548. int jtag_config_rclk(unsigned fallback_speed_khz)
  1549. {
  1550. LOG_DEBUG("handle jtag rclk");
  1551. clock_mode = CLOCK_MODE_RCLK;
  1552. rclk_fallback_speed_khz = fallback_speed_khz;
  1553. int speed = 0;
  1554. int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
  1555. return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
  1556. }
  1557. int jtag_get_speed(int *speed)
  1558. {
  1559. switch (clock_mode) {
  1560. case CLOCK_MODE_KHZ:
  1561. adapter_khz_to_speed(jtag_get_speed_khz(), speed);
  1562. break;
  1563. case CLOCK_MODE_RCLK:
  1564. jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
  1565. break;
  1566. default:
  1567. LOG_ERROR("BUG: unknown jtag clock mode");
  1568. return ERROR_FAIL;
  1569. }
  1570. return ERROR_OK;
  1571. }
  1572. int jtag_get_speed_readable(int *khz)
  1573. {
  1574. int jtag_speed_var = 0;
  1575. int retval = jtag_get_speed(&jtag_speed_var);
  1576. if (retval != ERROR_OK)
  1577. return retval;
  1578. if (!jtag)
  1579. return ERROR_OK;
  1580. if (!jtag->speed_div) {
  1581. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  1582. return ERROR_FAIL;
  1583. }
  1584. return jtag->speed_div(jtag_speed_var, khz);
  1585. }
  1586. void jtag_set_verify(bool enable)
  1587. {
  1588. jtag_verify = enable;
  1589. }
  1590. bool jtag_will_verify(void)
  1591. {
  1592. return jtag_verify;
  1593. }
  1594. void jtag_set_verify_capture_ir(bool enable)
  1595. {
  1596. jtag_verify_capture_ir = enable;
  1597. }
  1598. bool jtag_will_verify_capture_ir(void)
  1599. {
  1600. return jtag_verify_capture_ir;
  1601. }
  1602. int jtag_power_dropout(int *dropout)
  1603. {
  1604. if (jtag == NULL) {
  1605. /* TODO: as the jtag interface is not valid all
  1606. * we can do at the moment is exit OpenOCD */
  1607. LOG_ERROR("No Valid JTAG Interface Configured.");
  1608. exit(-1);
  1609. }
  1610. if (jtag->power_dropout)
  1611. return jtag->power_dropout(dropout);
  1612. *dropout = 0; /* by default we can't detect power dropout */
  1613. return ERROR_OK;
  1614. }
  1615. int jtag_srst_asserted(int *srst_asserted)
  1616. {
  1617. if (jtag->srst_asserted)
  1618. return jtag->srst_asserted(srst_asserted);
  1619. *srst_asserted = 0; /* by default we can't detect srst asserted */
  1620. return ERROR_OK;
  1621. }
  1622. enum reset_types jtag_get_reset_config(void)
  1623. {
  1624. return jtag_reset_config;
  1625. }
  1626. void jtag_set_reset_config(enum reset_types type)
  1627. {
  1628. jtag_reset_config = type;
  1629. }
  1630. int jtag_get_trst(void)
  1631. {
  1632. return jtag_trst == 1;
  1633. }
  1634. int jtag_get_srst(void)
  1635. {
  1636. return jtag_srst == 1;
  1637. }
  1638. void jtag_set_nsrst_delay(unsigned delay)
  1639. {
  1640. adapter_nsrst_delay = delay;
  1641. }
  1642. unsigned jtag_get_nsrst_delay(void)
  1643. {
  1644. return adapter_nsrst_delay;
  1645. }
  1646. void jtag_set_ntrst_delay(unsigned delay)
  1647. {
  1648. jtag_ntrst_delay = delay;
  1649. }
  1650. unsigned jtag_get_ntrst_delay(void)
  1651. {
  1652. return jtag_ntrst_delay;
  1653. }
  1654. void jtag_set_nsrst_assert_width(unsigned delay)
  1655. {
  1656. adapter_nsrst_assert_width = delay;
  1657. }
  1658. unsigned jtag_get_nsrst_assert_width(void)
  1659. {
  1660. return adapter_nsrst_assert_width;
  1661. }
  1662. void jtag_set_ntrst_assert_width(unsigned delay)
  1663. {
  1664. jtag_ntrst_assert_width = delay;
  1665. }
  1666. unsigned jtag_get_ntrst_assert_width(void)
  1667. {
  1668. return jtag_ntrst_assert_width;
  1669. }
  1670. static int jtag_select(struct command_context *ctx)
  1671. {
  1672. int retval;
  1673. /* NOTE: interface init must already have been done.
  1674. * That works with only C code ... no Tcl glue required.
  1675. */
  1676. retval = jtag_register_commands(ctx);
  1677. if (retval != ERROR_OK)
  1678. return retval;
  1679. retval = svf_register_commands(ctx);
  1680. if (retval != ERROR_OK)
  1681. return retval;
  1682. return xsvf_register_commands(ctx);
  1683. }
  1684. static struct transport jtag_transport = {
  1685. .name = "jtag",
  1686. .select = jtag_select,
  1687. .init = jtag_init,
  1688. };
  1689. static void jtag_constructor(void) __attribute__((constructor));
  1690. static void jtag_constructor(void)
  1691. {
  1692. transport_register(&jtag_transport);
  1693. }
  1694. /** Returns true if the current debug session
  1695. * is using JTAG as its transport.
  1696. */
  1697. bool transport_is_jtag(void)
  1698. {
  1699. return get_current_transport() == &jtag_transport;
  1700. }
  1701. int adapter_resets(int trst, int srst)
  1702. {
  1703. if (get_current_transport() == NULL) {
  1704. LOG_ERROR("transport is not selected");
  1705. return ERROR_FAIL;
  1706. }
  1707. if (transport_is_jtag()) {
  1708. if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
  1709. LOG_ERROR("adapter has no srst signal");
  1710. return ERROR_FAIL;
  1711. }
  1712. /* adapters without trst signal will eventually use tlr sequence */
  1713. jtag_add_reset(trst, srst);
  1714. /*
  1715. * The jtag queue is still used for reset by some adapter. Flush it!
  1716. * FIXME: To be removed when all adapter drivers will be updated!
  1717. */
  1718. jtag_execute_queue();
  1719. return ERROR_OK;
  1720. } else if (transport_is_swd() || transport_is_hla() ||
  1721. transport_is_dapdirect_swd() || transport_is_dapdirect_jtag() ||
  1722. transport_is_swim()) {
  1723. if (trst == TRST_ASSERT) {
  1724. LOG_ERROR("transport %s has no trst signal",
  1725. get_current_transport()->name);
  1726. return ERROR_FAIL;
  1727. }
  1728. if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
  1729. LOG_ERROR("adapter has no srst signal");
  1730. return ERROR_FAIL;
  1731. }
  1732. adapter_system_reset(srst);
  1733. return ERROR_OK;
  1734. }
  1735. if (trst == TRST_DEASSERT && srst == SRST_DEASSERT)
  1736. return ERROR_OK;
  1737. LOG_ERROR("reset is not supported on transport %s",
  1738. get_current_transport()->name);
  1739. return ERROR_FAIL;
  1740. }
  1741. int adapter_assert_reset(void)
  1742. {
  1743. if (transport_is_jtag()) {
  1744. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  1745. jtag_add_reset(1, 1);
  1746. else
  1747. jtag_add_reset(0, 1);
  1748. return ERROR_OK;
  1749. } else if (transport_is_swd() || transport_is_hla() ||
  1750. transport_is_dapdirect_jtag() || transport_is_dapdirect_swd() ||
  1751. transport_is_swim())
  1752. return adapter_system_reset(1);
  1753. else if (get_current_transport() != NULL)
  1754. LOG_ERROR("reset is not supported on %s",
  1755. get_current_transport()->name);
  1756. else
  1757. LOG_ERROR("transport is not selected");
  1758. return ERROR_FAIL;
  1759. }
  1760. int adapter_deassert_reset(void)
  1761. {
  1762. if (transport_is_jtag()) {
  1763. jtag_add_reset(0, 0);
  1764. return ERROR_OK;
  1765. } else if (transport_is_swd() || transport_is_hla() ||
  1766. transport_is_dapdirect_jtag() || transport_is_dapdirect_swd() ||
  1767. transport_is_swim())
  1768. return adapter_system_reset(0);
  1769. else if (get_current_transport() != NULL)
  1770. LOG_ERROR("reset is not supported on %s",
  1771. get_current_transport()->name);
  1772. else
  1773. LOG_ERROR("transport is not selected");
  1774. return ERROR_FAIL;
  1775. }
  1776. int adapter_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
  1777. uint32_t port_size, unsigned int *trace_freq,
  1778. unsigned int traceclkin_freq, uint16_t *prescaler)
  1779. {
  1780. if (jtag->config_trace) {
  1781. return jtag->config_trace(enabled, pin_protocol, port_size, trace_freq,
  1782. traceclkin_freq, prescaler);
  1783. } else if (enabled) {
  1784. LOG_ERROR("The selected interface does not support tracing");
  1785. return ERROR_FAIL;
  1786. }
  1787. return ERROR_OK;
  1788. }
  1789. int adapter_poll_trace(uint8_t *buf, size_t *size)
  1790. {
  1791. if (jtag->poll_trace)
  1792. return jtag->poll_trace(buf, size);
  1793. return ERROR_FAIL;
  1794. }