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.
 
 
 
 
 
 

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