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.
 
 
 
 
 
 

1361 lines
33 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Ø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) 2009 Zachary T Welch *
  13. * zw@superlucidity.net *
  14. * *
  15. * This program is free software; you can redistribute it and/or modify *
  16. * it under the terms of the GNU General Public License as published by *
  17. * the Free Software Foundation; either version 2 of the License, or *
  18. * (at your option) any later version. *
  19. * *
  20. * This program is distributed in the hope that it will be useful, *
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  23. * GNU General Public License for more details. *
  24. * *
  25. * You should have received a copy of the GNU General Public License *
  26. * along with this program; if not, write to the *
  27. * Free Software Foundation, Inc., *
  28. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "jtag.h"
  34. #include "minidriver.h"
  35. #include "interface.h"
  36. #ifdef HAVE_STRINGS_H
  37. #include <strings.h>
  38. #endif
  39. /// The number of JTAG queue flushes (for profiling and debugging purposes).
  40. static int jtag_flush_queue_count;
  41. static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
  42. int in_num_fields, scan_field_t *in_fields, tap_state_t state);
  43. /**
  44. * The jtag_error variable is set when an error occurs while executing
  45. * the queue. Application code may set this using jtag_set_error(),
  46. * when an error occurs during processing that should be reported during
  47. * jtag_execute_queue().
  48. *
  49. * Tts value may be checked with jtag_get_error() and cleared with
  50. * jtag_error_clear(). This value is returned (and cleared) by
  51. * jtag_execute_queue().
  52. */
  53. static int jtag_error = ERROR_OK;
  54. static const char *jtag_event_strings[] =
  55. {
  56. [JTAG_TRST_ASSERTED] = "JTAG controller reset (RESET or TRST)",
  57. [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
  58. [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
  59. };
  60. static int jtag_trst = 0;
  61. static int jtag_srst = 0;
  62. /**
  63. * List all TAPs that have been created.
  64. */
  65. static jtag_tap_t *__jtag_all_taps = NULL;
  66. /**
  67. * The number of TAPs in the __jtag_all_taps list, used to track the
  68. * assigned chain position to new TAPs
  69. */
  70. static unsigned jtag_num_taps = 0;
  71. static enum reset_types jtag_reset_config = RESET_NONE;
  72. static tap_state_t cmd_queue_end_state = TAP_RESET;
  73. tap_state_t cmd_queue_cur_state = TAP_RESET;
  74. static bool jtag_verify_capture_ir = true;
  75. static int jtag_verify = 1;
  76. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
  77. static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
  78. static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
  79. typedef struct jtag_event_callback_s
  80. {
  81. jtag_event_handler_t callback;
  82. void* priv;
  83. struct jtag_event_callback_s* next;
  84. } jtag_event_callback_t;
  85. /* callbacks to inform high-level handlers about JTAG state changes */
  86. static jtag_event_callback_t *jtag_event_callbacks;
  87. /* speed in kHz*/
  88. static int speed_khz = 0;
  89. /* flag if the kHz speed was defined */
  90. static bool hasKHz = false;
  91. static int jtag_speed = 0;
  92. static struct jtag_interface_s *jtag = NULL;
  93. /* configuration */
  94. jtag_interface_t *jtag_interface = NULL;
  95. void jtag_set_error(int error)
  96. {
  97. if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
  98. return;
  99. jtag_error = error;
  100. }
  101. int jtag_get_error(void)
  102. {
  103. return jtag_error;
  104. }
  105. int jtag_error_clear(void)
  106. {
  107. int temp = jtag_error;
  108. jtag_error = ERROR_OK;
  109. return temp;
  110. }
  111. jtag_tap_t *jtag_all_taps(void)
  112. {
  113. return __jtag_all_taps;
  114. };
  115. unsigned jtag_tap_count(void)
  116. {
  117. return jtag_num_taps;
  118. }
  119. unsigned jtag_tap_count_enabled(void)
  120. {
  121. jtag_tap_t *t = jtag_all_taps();
  122. unsigned n = 0;
  123. while (t)
  124. {
  125. if (t->enabled)
  126. n++;
  127. t = t->next_tap;
  128. }
  129. return n;
  130. }
  131. /// Append a new TAP to the chain of all taps.
  132. void jtag_tap_add(struct jtag_tap_s *t)
  133. {
  134. t->abs_chain_position = jtag_num_taps++;
  135. jtag_tap_t **tap = &__jtag_all_taps;
  136. while (*tap != NULL)
  137. tap = &(*tap)->next_tap;
  138. *tap = t;
  139. }
  140. jtag_tap_t *jtag_tap_by_string(const char *s)
  141. {
  142. /* try by name first */
  143. jtag_tap_t *t = jtag_all_taps();
  144. while (t)
  145. {
  146. if (0 == strcmp(t->dotted_name, s))
  147. return t;
  148. t = t->next_tap;
  149. }
  150. /* no tap found by name, so try to parse the name as a number */
  151. unsigned n;
  152. if (parse_uint(s, &n) != ERROR_OK)
  153. return NULL;
  154. return jtag_tap_by_position(n);
  155. }
  156. jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
  157. {
  158. const char *cp = Jim_GetString(o, NULL);
  159. jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
  160. if (NULL == cp)
  161. cp = "(unknown)";
  162. if (NULL == t)
  163. Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
  164. return t;
  165. }
  166. /* returns a pointer to the n-th device in the scan chain */
  167. jtag_tap_t *jtag_tap_by_position(unsigned n)
  168. {
  169. jtag_tap_t *t = jtag_all_taps();
  170. while (t && n-- > 0)
  171. t = t->next_tap;
  172. return t;
  173. }
  174. jtag_tap_t* jtag_tap_next_enabled(jtag_tap_t* p)
  175. {
  176. p = p ? p->next_tap : jtag_all_taps();
  177. while (p)
  178. {
  179. if (p->enabled)
  180. return p;
  181. p = p->next_tap;
  182. }
  183. return NULL;
  184. }
  185. const char *jtag_tap_name(const jtag_tap_t *tap)
  186. {
  187. return (tap == NULL) ? "(unknown)" : tap->dotted_name;
  188. }
  189. int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
  190. {
  191. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  192. if (callback == NULL)
  193. {
  194. return ERROR_INVALID_ARGUMENTS;
  195. }
  196. if (*callbacks_p)
  197. {
  198. while ((*callbacks_p)->next)
  199. callbacks_p = &((*callbacks_p)->next);
  200. callbacks_p = &((*callbacks_p)->next);
  201. }
  202. (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
  203. (*callbacks_p)->callback = callback;
  204. (*callbacks_p)->priv = priv;
  205. (*callbacks_p)->next = NULL;
  206. return ERROR_OK;
  207. }
  208. int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
  209. {
  210. jtag_event_callback_t **callbacks_p;
  211. jtag_event_callback_t **next;
  212. if (callback == NULL)
  213. {
  214. return ERROR_INVALID_ARGUMENTS;
  215. }
  216. for (callbacks_p = &jtag_event_callbacks;
  217. *callbacks_p != NULL;
  218. callbacks_p = next)
  219. {
  220. next = &((*callbacks_p)->next);
  221. if ((*callbacks_p)->priv != priv)
  222. continue;
  223. if ((*callbacks_p)->callback == callback)
  224. {
  225. free(*callbacks_p);
  226. *callbacks_p = *next;
  227. }
  228. }
  229. return ERROR_OK;
  230. }
  231. int jtag_call_event_callbacks(enum jtag_event event)
  232. {
  233. jtag_event_callback_t *callback = jtag_event_callbacks;
  234. LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
  235. while (callback)
  236. {
  237. jtag_event_callback_t *next;
  238. /* callback may remove itself */
  239. next = callback->next;
  240. callback->callback(event, callback->priv);
  241. callback = next;
  242. }
  243. return ERROR_OK;
  244. }
  245. static void jtag_checks(void)
  246. {
  247. assert(jtag_trst == 0);
  248. }
  249. static void jtag_prelude(tap_state_t state)
  250. {
  251. jtag_checks();
  252. assert(state!=TAP_INVALID);
  253. cmd_queue_cur_state = state;
  254. }
  255. void jtag_alloc_in_value32(scan_field_t *field)
  256. {
  257. interface_jtag_alloc_in_value32(field);
  258. }
  259. void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
  260. tap_state_t state)
  261. {
  262. jtag_prelude(state);
  263. int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
  264. jtag_set_error(retval);
  265. }
  266. void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  267. {
  268. if (jtag_verify&&jtag_verify_capture_ir)
  269. {
  270. /* 8 x 32 bit id's is enough for all invocations */
  271. for (int j = 0; j < in_num_fields; j++)
  272. {
  273. /* if we are to run a verification of the ir scan, we need to get the input back.
  274. * We may have to allocate space if the caller didn't ask for the input back.
  275. */
  276. in_fields[j].check_value=in_fields[j].tap->expected;
  277. in_fields[j].check_mask=in_fields[j].tap->expected_mask;
  278. }
  279. jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
  280. } else
  281. {
  282. jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
  283. }
  284. }
  285. void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
  286. tap_state_t state)
  287. {
  288. jtag_prelude(state);
  289. int retval = interface_jtag_add_plain_ir_scan(
  290. in_num_fields, in_fields, state);
  291. jtag_set_error(retval);
  292. }
  293. void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
  294. {
  295. interface_jtag_add_callback(f, data0);
  296. }
  297. void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
  298. jtag_callback_data_t data1, jtag_callback_data_t data2,
  299. jtag_callback_data_t data3)
  300. {
  301. interface_jtag_add_callback4(f, data0, data1, data2, data3);
  302. }
  303. int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value, uint8_t *in_check_mask, int num_bits);
  304. static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  305. {
  306. return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
  307. }
  308. static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
  309. int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  310. {
  311. for (int i = 0; i < in_num_fields; i++)
  312. {
  313. struct scan_field_s *field = &in_fields[i];
  314. field->allocated = 0;
  315. field->modified = 0;
  316. if (field->check_value || field->in_value)
  317. continue;
  318. interface_jtag_add_scan_check_alloc(field);
  319. field->modified = 1;
  320. }
  321. jtag_add_scan(in_num_fields, in_fields, state);
  322. for (int i = 0; i < in_num_fields; i++)
  323. {
  324. if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
  325. {
  326. /* this is synchronous for a minidriver */
  327. jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
  328. (jtag_callback_data_t)in_fields[i].check_value,
  329. (jtag_callback_data_t)in_fields[i].check_mask,
  330. (jtag_callback_data_t)in_fields[i].num_bits);
  331. }
  332. if (in_fields[i].allocated)
  333. {
  334. free(in_fields[i].in_value);
  335. }
  336. if (in_fields[i].modified)
  337. {
  338. in_fields[i].in_value = NULL;
  339. }
  340. }
  341. }
  342. void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  343. {
  344. if (jtag_verify)
  345. {
  346. jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
  347. } else
  348. {
  349. jtag_add_dr_scan(in_num_fields, in_fields, state);
  350. }
  351. }
  352. void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
  353. tap_state_t state)
  354. {
  355. jtag_prelude(state);
  356. int retval;
  357. retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
  358. jtag_set_error(retval);
  359. }
  360. void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
  361. tap_state_t state)
  362. {
  363. jtag_prelude(state);
  364. int retval;
  365. retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
  366. jtag_set_error(retval);
  367. }
  368. void jtag_add_dr_out(jtag_tap_t* tap,
  369. int num_fields, const int* num_bits, const uint32_t* value,
  370. tap_state_t end_state)
  371. {
  372. assert(end_state != TAP_INVALID);
  373. cmd_queue_cur_state = end_state;
  374. interface_jtag_add_dr_out(tap,
  375. num_fields, num_bits, value,
  376. end_state);
  377. }
  378. void jtag_add_tlr(void)
  379. {
  380. jtag_prelude(TAP_RESET);
  381. jtag_set_error(interface_jtag_add_tlr());
  382. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  383. }
  384. void jtag_add_pathmove(int num_states, const tap_state_t *path)
  385. {
  386. tap_state_t cur_state = cmd_queue_cur_state;
  387. /* the last state has to be a stable state */
  388. if (!tap_is_state_stable(path[num_states - 1]))
  389. {
  390. LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
  391. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  392. return;
  393. }
  394. for (int i = 0; i < num_states; i++)
  395. {
  396. if (path[i] == TAP_RESET)
  397. {
  398. LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
  399. jtag_set_error(ERROR_JTAG_STATE_INVALID);
  400. return;
  401. }
  402. if ( tap_state_transition(cur_state, true) != path[i]
  403. && tap_state_transition(cur_state, false) != path[i])
  404. {
  405. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  406. tap_state_name(cur_state), tap_state_name(path[i]));
  407. jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
  408. return;
  409. }
  410. cur_state = path[i];
  411. }
  412. jtag_checks();
  413. jtag_set_error(interface_jtag_add_pathmove(num_states, path));
  414. cmd_queue_cur_state = path[num_states - 1];
  415. }
  416. int jtag_add_statemove(tap_state_t goal_state)
  417. {
  418. tap_state_t cur_state = cmd_queue_cur_state;
  419. LOG_DEBUG( "cur_state=%s goal_state=%s",
  420. tap_state_name(cur_state),
  421. tap_state_name(goal_state) );
  422. if (goal_state==cur_state )
  423. ; /* nothing to do */
  424. else if ( goal_state==TAP_RESET )
  425. {
  426. jtag_add_tlr();
  427. }
  428. else if ( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
  429. {
  430. unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
  431. unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
  432. tap_state_t moves[8];
  433. assert(tms_count < DIM(moves));
  434. for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
  435. {
  436. bool bit = tms_bits & 1;
  437. cur_state = tap_state_transition(cur_state, bit);
  438. moves[i] = cur_state;
  439. }
  440. jtag_add_pathmove(tms_count, moves);
  441. }
  442. else if ( tap_state_transition(cur_state, true) == goal_state
  443. || tap_state_transition(cur_state, false) == goal_state )
  444. {
  445. jtag_add_pathmove(1, &goal_state);
  446. }
  447. else
  448. return ERROR_FAIL;
  449. return ERROR_OK;
  450. }
  451. void jtag_add_runtest(int num_cycles, tap_state_t state)
  452. {
  453. jtag_prelude(state);
  454. jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
  455. }
  456. void jtag_add_clocks(int num_cycles)
  457. {
  458. if (!tap_is_state_stable(cmd_queue_cur_state))
  459. {
  460. LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
  461. tap_state_name(cmd_queue_cur_state));
  462. jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
  463. return;
  464. }
  465. if (num_cycles > 0)
  466. {
  467. jtag_checks();
  468. jtag_set_error(interface_jtag_add_clocks(num_cycles));
  469. }
  470. }
  471. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  472. {
  473. int trst_with_tlr = 0;
  474. /* FIX!!! there are *many* different cases here. A better
  475. * approach is needed for legal combinations of transitions...
  476. */
  477. if ((jtag_reset_config & RESET_HAS_SRST)&&
  478. (jtag_reset_config & RESET_HAS_TRST)&&
  479. ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
  480. {
  481. if (((req_tlr_or_trst&&!jtag_trst)||
  482. (!req_tlr_or_trst&&jtag_trst))&&
  483. ((req_srst&&!jtag_srst)||
  484. (!req_srst&&jtag_srst)))
  485. {
  486. /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
  487. //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
  488. }
  489. }
  490. /* Make sure that jtag_reset_config allows the requested reset */
  491. /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
  492. if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
  493. {
  494. LOG_ERROR("BUG: requested reset would assert trst");
  495. jtag_set_error(ERROR_FAIL);
  496. return;
  497. }
  498. /* if TRST pulls SRST, we reset with TAP T-L-R */
  499. if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
  500. {
  501. trst_with_tlr = 1;
  502. }
  503. if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
  504. {
  505. LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
  506. jtag_set_error(ERROR_FAIL);
  507. return;
  508. }
  509. if (req_tlr_or_trst)
  510. {
  511. if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
  512. {
  513. jtag_trst = 1;
  514. } else
  515. {
  516. trst_with_tlr = 1;
  517. }
  518. } else
  519. {
  520. jtag_trst = 0;
  521. }
  522. jtag_srst = req_srst;
  523. int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
  524. if (retval != ERROR_OK)
  525. {
  526. jtag_set_error(retval);
  527. return;
  528. }
  529. jtag_execute_queue();
  530. if (jtag_srst)
  531. {
  532. LOG_DEBUG("SRST line asserted");
  533. }
  534. else
  535. {
  536. LOG_DEBUG("SRST line released");
  537. if (jtag_nsrst_delay)
  538. jtag_add_sleep(jtag_nsrst_delay * 1000);
  539. }
  540. if (trst_with_tlr)
  541. {
  542. LOG_DEBUG("JTAG reset with RESET instead of TRST");
  543. jtag_set_end_state(TAP_RESET);
  544. jtag_add_tlr();
  545. return;
  546. }
  547. if (jtag_trst)
  548. {
  549. /* we just asserted nTRST, so we're now in Test-Logic-Reset,
  550. * and inform possible listeners about this
  551. */
  552. LOG_DEBUG("TRST line asserted");
  553. tap_set_state(TAP_RESET);
  554. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  555. }
  556. else
  557. {
  558. if (jtag_ntrst_delay)
  559. jtag_add_sleep(jtag_ntrst_delay * 1000);
  560. }
  561. }
  562. tap_state_t jtag_set_end_state(tap_state_t state)
  563. {
  564. if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
  565. {
  566. LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
  567. }
  568. if (state!=TAP_INVALID)
  569. cmd_queue_end_state = state;
  570. return cmd_queue_end_state;
  571. }
  572. tap_state_t jtag_get_end_state(void)
  573. {
  574. return cmd_queue_end_state;
  575. }
  576. void jtag_add_sleep(uint32_t us)
  577. {
  578. /// @todo Here, keep_alive() appears to be a layering violation!!!
  579. keep_alive();
  580. jtag_set_error(interface_jtag_add_sleep(us));
  581. }
  582. int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value, uint8_t *in_check_mask, int num_bits)
  583. {
  584. int retval = ERROR_OK;
  585. int compare_failed = 0;
  586. if (in_check_mask)
  587. compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
  588. else
  589. compare_failed = buf_cmp(captured, in_check_value, num_bits);
  590. if (compare_failed){
  591. /* An error handler could have caught the failing check
  592. * only report a problem when there wasn't a handler, or if the handler
  593. * acknowledged the error
  594. */
  595. /*
  596. LOG_WARNING("TAP %s:",
  597. jtag_tap_name(field->tap));
  598. */
  599. if (compare_failed)
  600. {
  601. char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  602. char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  603. if (in_check_mask)
  604. {
  605. char *in_check_mask_char;
  606. in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  607. LOG_WARNING("value captured during scan didn't pass the requested check:");
  608. LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
  609. captured_char, in_check_value_char, in_check_mask_char);
  610. free(in_check_mask_char);
  611. }
  612. else
  613. {
  614. LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
  615. }
  616. free(captured_char);
  617. free(in_check_value_char);
  618. retval = ERROR_JTAG_QUEUE_FAILED;
  619. }
  620. }
  621. return retval;
  622. }
  623. void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
  624. {
  625. assert(field->in_value != NULL);
  626. if (value==NULL)
  627. {
  628. /* no checking to do */
  629. return;
  630. }
  631. jtag_execute_queue_noclear();
  632. int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
  633. jtag_set_error(retval);
  634. }
  635. int default_interface_jtag_execute_queue(void)
  636. {
  637. if (NULL == jtag)
  638. {
  639. LOG_ERROR("No JTAG interface configured yet. "
  640. "Issue 'init' command in startup scripts "
  641. "before communicating with targets.");
  642. return ERROR_FAIL;
  643. }
  644. return jtag->execute_queue();
  645. }
  646. void jtag_execute_queue_noclear(void)
  647. {
  648. jtag_flush_queue_count++;
  649. jtag_set_error(interface_jtag_execute_queue());
  650. }
  651. int jtag_get_flush_queue_count(void)
  652. {
  653. return jtag_flush_queue_count;
  654. }
  655. int jtag_execute_queue(void)
  656. {
  657. jtag_execute_queue_noclear();
  658. return jtag_error_clear();
  659. }
  660. static int jtag_reset_callback(enum jtag_event event, void *priv)
  661. {
  662. jtag_tap_t *tap = priv;
  663. LOG_DEBUG("-");
  664. if (event == JTAG_TRST_ASSERTED)
  665. {
  666. tap->enabled = !tap->disabled_after_reset;
  667. buf_set_ones(tap->cur_instr, tap->ir_length);
  668. tap->bypass = 1;
  669. }
  670. return ERROR_OK;
  671. }
  672. void jtag_sleep(uint32_t us)
  673. {
  674. alive_sleep(us/1000);
  675. }
  676. /// maximum number of JTAG devices expected in the chain
  677. #define JTAG_MAX_CHAIN_SIZE 20
  678. #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
  679. #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
  680. #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
  681. static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
  682. {
  683. scan_field_t field = {
  684. .tap = NULL,
  685. .num_bits = num_idcode * 32,
  686. .out_value = idcode_buffer,
  687. .in_value = idcode_buffer,
  688. };
  689. // initialize to the end of chain ID value
  690. for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
  691. buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
  692. jtag_add_plain_dr_scan(1, &field, TAP_RESET);
  693. return jtag_execute_queue();
  694. }
  695. static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
  696. {
  697. uint8_t zero_check = 0x0;
  698. uint8_t one_check = 0xff;
  699. for (unsigned i = 0; i < count * 4; i++)
  700. {
  701. zero_check |= idcodes[i];
  702. one_check &= idcodes[i];
  703. }
  704. /* if there wasn't a single non-zero bit or if all bits were one,
  705. * the scan is not valid */
  706. if (zero_check == 0x00 || one_check == 0xff)
  707. {
  708. LOG_ERROR("JTAG communication failure: check connection, "
  709. "JTAG interface, target power etc.");
  710. return false;
  711. }
  712. return true;
  713. }
  714. static void jtag_examine_chain_display(enum log_levels level, const char *msg,
  715. const char *name, uint32_t idcode)
  716. {
  717. log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
  718. "JTAG tap: %s %16.16s: 0x%08x "
  719. "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  720. name, msg,
  721. (unsigned int)idcode,
  722. (unsigned int)EXTRACT_MFG(idcode),
  723. (unsigned int)EXTRACT_PART(idcode),
  724. (unsigned int)EXTRACT_VER(idcode) );
  725. }
  726. static bool jtag_idcode_is_final(uint32_t idcode)
  727. {
  728. return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
  729. }
  730. /**
  731. * This helper checks that remaining bits in the examined chain data are
  732. * all as expected, but a single JTAG device requires only 64 bits to be
  733. * read back correctly. This can help identify and diagnose problems
  734. * with the JTAG chain earlier, gives more helpful/explicit error messages.
  735. */
  736. static void jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
  737. {
  738. bool triggered = false;
  739. for ( ; count < max - 31; count += 32)
  740. {
  741. uint32_t idcode = buf_get_u32(idcodes, count, 32);
  742. // do not trigger the warning if the data looks good
  743. if (!triggered && jtag_idcode_is_final(idcode))
  744. continue;
  745. LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
  746. count, (unsigned int)idcode);
  747. triggered = true;
  748. }
  749. }
  750. static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
  751. {
  752. if (0 == tap->expected_ids_cnt)
  753. {
  754. /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
  755. #if 0
  756. LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
  757. LOG_INFO("Please report the chip name and reported ID code to the openocd project");
  758. #endif
  759. return true;
  760. }
  761. /* Loop over the expected identification codes and test for a match */
  762. uint8_t ii;
  763. for (ii = 0; ii < tap->expected_ids_cnt; ii++)
  764. {
  765. if (tap->idcode == tap->expected_ids[ii])
  766. break;
  767. }
  768. /* If none of the expected ids matched, log an error */
  769. if (ii != tap->expected_ids_cnt)
  770. {
  771. LOG_INFO("JTAG Tap/device matched");
  772. return true;
  773. }
  774. jtag_examine_chain_display(LOG_LVL_ERROR, "got",
  775. tap->dotted_name, tap->idcode);
  776. for (ii = 0; ii < tap->expected_ids_cnt; ii++)
  777. {
  778. char msg[32];
  779. snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
  780. ii + 1, tap->expected_ids_cnt);
  781. jtag_examine_chain_display(LOG_LVL_ERROR, msg,
  782. tap->dotted_name, tap->expected_ids[ii]);
  783. }
  784. return false;
  785. }
  786. /* Try to examine chain layout according to IEEE 1149.1 §12
  787. */
  788. int jtag_examine_chain(void)
  789. {
  790. uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
  791. unsigned device_count = 0;
  792. jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
  793. if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
  794. return ERROR_JTAG_INIT_FAILED;
  795. /* point at the 1st tap */
  796. jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
  797. if (tap == NULL)
  798. {
  799. LOG_ERROR("JTAG: No taps enabled?");
  800. return ERROR_JTAG_INIT_FAILED;
  801. }
  802. for (unsigned bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
  803. {
  804. uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  805. if ((idcode & 1) == 0)
  806. {
  807. /* LSB must not be 0, this indicates a device in bypass */
  808. LOG_WARNING("Tap/Device does not have IDCODE");
  809. idcode = 0;
  810. bit_count += 1;
  811. }
  812. else
  813. {
  814. /*
  815. * End of chain (invalid manufacturer ID) some devices, such
  816. * as AVR will output all 1's instead of TDI input value at
  817. * end of chain.
  818. */
  819. if (jtag_idcode_is_final(idcode))
  820. {
  821. jtag_examine_chain_end(idcode_buffer,
  822. bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
  823. break;
  824. }
  825. jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
  826. tap ? tap->dotted_name : "(not-named)",
  827. idcode);
  828. bit_count += 32;
  829. }
  830. device_count++;
  831. if (!tap)
  832. continue;
  833. tap->idcode = idcode;
  834. // ensure the TAP ID does matches what was expected
  835. if (!jtag_examine_chain_match_tap(tap))
  836. return ERROR_JTAG_INIT_FAILED;
  837. tap = jtag_tap_next_enabled(tap);
  838. }
  839. /* see if number of discovered devices matches configuration */
  840. if (device_count != jtag_tap_count_enabled())
  841. {
  842. LOG_ERROR("number of discovered devices in JTAG chain (%i) "
  843. "does not match (enabled) configuration (%i), total taps: %d",
  844. device_count, jtag_tap_count_enabled(), jtag_tap_count());
  845. LOG_ERROR("check the config file and ensure proper JTAG communication"
  846. " (connections, speed, ...)");
  847. return ERROR_JTAG_INIT_FAILED;
  848. }
  849. return ERROR_OK;
  850. }
  851. int jtag_validate_chain(void)
  852. {
  853. jtag_tap_t *tap;
  854. int total_ir_length = 0;
  855. uint8_t *ir_test = NULL;
  856. scan_field_t field;
  857. int chain_pos = 0;
  858. tap = NULL;
  859. total_ir_length = 0;
  860. for (;;){
  861. tap = jtag_tap_next_enabled(tap);
  862. if ( tap == NULL ){
  863. break;
  864. }
  865. total_ir_length += tap->ir_length;
  866. }
  867. total_ir_length += 2;
  868. ir_test = malloc(CEIL(total_ir_length, 8));
  869. buf_set_ones(ir_test, total_ir_length);
  870. field.tap = NULL;
  871. field.num_bits = total_ir_length;
  872. field.out_value = ir_test;
  873. field.in_value = ir_test;
  874. jtag_add_plain_ir_scan(1, &field, TAP_RESET);
  875. jtag_execute_queue();
  876. tap = NULL;
  877. chain_pos = 0;
  878. int val;
  879. for (;;){
  880. tap = jtag_tap_next_enabled(tap);
  881. if ( tap == NULL ){
  882. break;
  883. }
  884. val = buf_get_u32(ir_test, chain_pos, 2);
  885. if (val != 0x1)
  886. {
  887. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  888. LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf, jtag_tap_name(tap), chain_pos, val);
  889. free(cbuf);
  890. free(ir_test);
  891. return ERROR_JTAG_INIT_FAILED;
  892. }
  893. chain_pos += tap->ir_length;
  894. }
  895. val = buf_get_u32(ir_test, chain_pos, 2);
  896. if (val != 0x3)
  897. {
  898. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  899. LOG_ERROR("Could not validate end of JTAG scan chain, IR mismatch, scan returned 0x%s. pos=%d expected 0x3 got %0x", cbuf, chain_pos, val);
  900. free(cbuf);
  901. free(ir_test);
  902. return ERROR_JTAG_INIT_FAILED;
  903. }
  904. free(ir_test);
  905. return ERROR_OK;
  906. }
  907. void jtag_tap_init(jtag_tap_t *tap)
  908. {
  909. assert(0 != tap->ir_length);
  910. tap->expected = malloc(tap->ir_length);
  911. tap->expected_mask = malloc(tap->ir_length);
  912. tap->cur_instr = malloc(tap->ir_length);
  913. /// @todo cope sanely with ir_length bigger than 32 bits
  914. buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
  915. buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
  916. buf_set_ones(tap->cur_instr, tap->ir_length);
  917. // place TAP in bypass mode
  918. tap->bypass = 1;
  919. // register the reset callback for the TAP
  920. jtag_register_event_callback(&jtag_reset_callback, tap);
  921. LOG_DEBUG("Created Tap: %s @ abs position %d, "
  922. "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
  923. tap->abs_chain_position, tap->ir_length,
  924. (unsigned int)(tap->ir_capture_value), (unsigned int)(tap->ir_capture_mask));
  925. jtag_tap_add(tap);
  926. }
  927. void jtag_tap_free(jtag_tap_t *tap)
  928. {
  929. jtag_unregister_event_callback(&jtag_reset_callback, tap);
  930. /// @todo is anything missing? no memory leaks please
  931. free((void *)tap->expected_ids);
  932. free((void *)tap->chip);
  933. free((void *)tap->tapname);
  934. free((void *)tap->dotted_name);
  935. free(tap);
  936. }
  937. int jtag_interface_init(struct command_context_s *cmd_ctx)
  938. {
  939. if (jtag)
  940. return ERROR_OK;
  941. if (!jtag_interface)
  942. {
  943. /* nothing was previously specified by "interface" command */
  944. LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
  945. return ERROR_JTAG_INVALID_INTERFACE;
  946. }
  947. if (hasKHz)
  948. {
  949. jtag_interface->khz(jtag_get_speed_khz(), &jtag_speed);
  950. hasKHz = false;
  951. }
  952. if (jtag_interface->init() != ERROR_OK)
  953. return ERROR_JTAG_INIT_FAILED;
  954. jtag = jtag_interface;
  955. return ERROR_OK;
  956. }
  957. static int jtag_init_inner(struct command_context_s *cmd_ctx)
  958. {
  959. jtag_tap_t *tap;
  960. int retval;
  961. LOG_DEBUG("Init JTAG chain");
  962. tap = jtag_tap_next_enabled(NULL);
  963. if ( tap == NULL ){
  964. LOG_ERROR("There are no enabled taps?");
  965. return ERROR_JTAG_INIT_FAILED;
  966. }
  967. jtag_add_tlr();
  968. if ((retval=jtag_execute_queue())!=ERROR_OK)
  969. return retval;
  970. /* examine chain first, as this could discover the real chain layout */
  971. if (jtag_examine_chain() != ERROR_OK)
  972. {
  973. LOG_ERROR("trying to validate configured JTAG chain anyway...");
  974. }
  975. if (jtag_validate_chain() != ERROR_OK)
  976. {
  977. LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
  978. }
  979. return ERROR_OK;
  980. }
  981. int jtag_interface_quit(void)
  982. {
  983. if (!jtag || !jtag->quit)
  984. return ERROR_OK;
  985. // close the JTAG interface
  986. int result = jtag->quit();
  987. if (ERROR_OK != result)
  988. LOG_ERROR("failed: %d", result);
  989. return ERROR_OK;
  990. }
  991. int jtag_init_reset(struct command_context_s *cmd_ctx)
  992. {
  993. int retval;
  994. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  995. return retval;
  996. LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
  997. /* Reset can happen after a power cycle.
  998. *
  999. * Ideally we would only assert TRST or run RESET before the target reset.
  1000. *
  1001. * However w/srst_pulls_trst, trst is asserted together with the target
  1002. * reset whether we want it or not.
  1003. *
  1004. * NB! Some targets have JTAG circuitry disabled until a
  1005. * trst & srst has been asserted.
  1006. *
  1007. * NB! here we assume nsrst/ntrst delay are sufficient!
  1008. *
  1009. * NB! order matters!!!! srst *can* disconnect JTAG circuitry
  1010. *
  1011. */
  1012. jtag_add_reset(1, 0); /* RESET or TRST */
  1013. if (jtag_reset_config & RESET_HAS_SRST)
  1014. {
  1015. jtag_add_reset(1, 1);
  1016. if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
  1017. jtag_add_reset(0, 1);
  1018. }
  1019. jtag_add_reset(0, 0);
  1020. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1021. return retval;
  1022. /* Check that we can communication on the JTAG chain + eventually we want to
  1023. * be able to perform enumeration only after OpenOCD has started
  1024. * telnet and GDB server
  1025. *
  1026. * That would allow users to more easily perform any magic they need to before
  1027. * reset happens.
  1028. */
  1029. return jtag_init_inner(cmd_ctx);
  1030. }
  1031. int jtag_init(struct command_context_s *cmd_ctx)
  1032. {
  1033. int retval;
  1034. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1035. return retval;
  1036. if (jtag_init_inner(cmd_ctx)==ERROR_OK)
  1037. {
  1038. return ERROR_OK;
  1039. }
  1040. return jtag_init_reset(cmd_ctx);
  1041. }
  1042. void jtag_set_speed_khz(unsigned khz)
  1043. {
  1044. speed_khz = khz;
  1045. }
  1046. unsigned jtag_get_speed_khz(void)
  1047. {
  1048. return speed_khz;
  1049. }
  1050. int jtag_config_khz(unsigned khz)
  1051. {
  1052. LOG_DEBUG("handle jtag khz");
  1053. jtag_set_speed_khz(khz);
  1054. int cur_speed = 0;
  1055. if (jtag != NULL)
  1056. {
  1057. LOG_DEBUG("have interface set up");
  1058. int speed_div1;
  1059. int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
  1060. if (ERROR_OK != retval)
  1061. {
  1062. jtag_set_speed_khz(0);
  1063. return retval;
  1064. }
  1065. cur_speed = speed_div1;
  1066. }
  1067. return jtag_set_speed(cur_speed);
  1068. }
  1069. int jtag_get_speed(void)
  1070. {
  1071. return jtag_speed;
  1072. }
  1073. int jtag_set_speed(int speed)
  1074. {
  1075. jtag_speed = speed;
  1076. /* this command can be called during CONFIG,
  1077. * in which case jtag isn't initialized */
  1078. hasKHz = !jtag;
  1079. return jtag ? jtag->speed(speed) : ERROR_OK;
  1080. }
  1081. int jtag_get_speed_readable(int *speed)
  1082. {
  1083. return jtag ? jtag->speed_div(jtag_get_speed(), speed) : ERROR_OK;
  1084. }
  1085. void jtag_set_verify(bool enable)
  1086. {
  1087. jtag_verify = enable;
  1088. }
  1089. bool jtag_will_verify()
  1090. {
  1091. return jtag_verify;
  1092. }
  1093. void jtag_set_verify_capture_ir(bool enable)
  1094. {
  1095. jtag_verify_capture_ir = enable;
  1096. }
  1097. bool jtag_will_verify_capture_ir()
  1098. {
  1099. return jtag_verify_capture_ir;
  1100. }
  1101. int jtag_power_dropout(int *dropout)
  1102. {
  1103. return jtag->power_dropout(dropout);
  1104. }
  1105. int jtag_srst_asserted(int *srst_asserted)
  1106. {
  1107. return jtag->srst_asserted(srst_asserted);
  1108. }
  1109. enum reset_types jtag_get_reset_config(void)
  1110. {
  1111. return jtag_reset_config;
  1112. }
  1113. void jtag_set_reset_config(enum reset_types type)
  1114. {
  1115. jtag_reset_config = type;
  1116. }
  1117. int jtag_get_trst(void)
  1118. {
  1119. return jtag_trst;
  1120. }
  1121. int jtag_get_srst(void)
  1122. {
  1123. return jtag_srst;
  1124. }
  1125. void jtag_set_nsrst_delay(unsigned delay)
  1126. {
  1127. jtag_nsrst_delay = delay;
  1128. }
  1129. unsigned jtag_get_nsrst_delay(void)
  1130. {
  1131. return jtag_nsrst_delay;
  1132. }
  1133. void jtag_set_ntrst_delay(unsigned delay)
  1134. {
  1135. jtag_ntrst_delay = delay;
  1136. }
  1137. unsigned jtag_get_ntrst_delay(void)
  1138. {
  1139. return jtag_ntrst_delay;
  1140. }