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.
 
 
 
 
 
 

2684 lines
66 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. * This program is free software; you can redistribute it and/or modify *
  13. * it under the terms of the GNU General Public License as published by *
  14. * the Free Software Foundation; either version 2 of the License, or *
  15. * (at your option) any later version. *
  16. * *
  17. * This program is distributed in the hope that it will be useful, *
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  20. * GNU General Public License for more details. *
  21. * *
  22. * You should have received a copy of the GNU General Public License *
  23. * along with this program; if not, write to the *
  24. * Free Software Foundation, Inc., *
  25. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  26. ***************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "jtag.h"
  31. #include "minidriver.h"
  32. #include "interface.h"
  33. #ifdef HAVE_STRINGS_H
  34. #include <strings.h>
  35. #endif
  36. int jtag_flush_queue_count; /* count # of flushes for profiling / debugging purposes */
  37. static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
  38. int in_num_fields, scan_field_t *in_fields, tap_state_t state);
  39. /* note that this is not marked as static as it must be available from outside jtag.c for those
  40. that implement the jtag_xxx() minidriver layer
  41. */
  42. int jtag_error=ERROR_OK;
  43. char* jtag_event_strings[] =
  44. {
  45. "JTAG controller reset (RESET or TRST)"
  46. };
  47. const Jim_Nvp nvp_jtag_tap_event[] = {
  48. { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
  49. { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
  50. { .name = NULL, .value = -1 }
  51. };
  52. int jtag_trst = 0;
  53. int jtag_srst = 0;
  54. static jtag_tap_t *jtag_all_taps = NULL;
  55. enum reset_types jtag_reset_config = RESET_NONE;
  56. tap_state_t cmd_queue_end_state = TAP_RESET;
  57. tap_state_t cmd_queue_cur_state = TAP_RESET;
  58. int jtag_verify_capture_ir = 1;
  59. int jtag_verify = 1;
  60. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
  61. static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
  62. static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
  63. /* maximum number of JTAG devices expected in the chain
  64. */
  65. #define JTAG_MAX_CHAIN_SIZE 20
  66. /* callbacks to inform high-level handlers about JTAG state changes */
  67. jtag_event_callback_t *jtag_event_callbacks;
  68. /* speed in kHz*/
  69. static int speed_khz = 0;
  70. /* flag if the kHz speed was defined */
  71. static int hasKHz = 0;
  72. /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
  73. */
  74. #if BUILD_ECOSBOARD == 1
  75. extern jtag_interface_t zy1000_interface;
  76. #endif
  77. #if BUILD_PARPORT == 1
  78. extern jtag_interface_t parport_interface;
  79. #endif
  80. #if BUILD_DUMMY == 1
  81. extern jtag_interface_t dummy_interface;
  82. #endif
  83. #if BUILD_FT2232_FTD2XX == 1
  84. extern jtag_interface_t ft2232_interface;
  85. #endif
  86. #if BUILD_FT2232_LIBFTDI == 1
  87. extern jtag_interface_t ft2232_interface;
  88. #endif
  89. #if BUILD_AMTJTAGACCEL == 1
  90. extern jtag_interface_t amt_jtagaccel_interface;
  91. #endif
  92. #if BUILD_EP93XX == 1
  93. extern jtag_interface_t ep93xx_interface;
  94. #endif
  95. #if BUILD_AT91RM9200 == 1
  96. extern jtag_interface_t at91rm9200_interface;
  97. #endif
  98. #if BUILD_GW16012 == 1
  99. extern jtag_interface_t gw16012_interface;
  100. #endif
  101. #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
  102. extern jtag_interface_t presto_interface;
  103. #endif
  104. #if BUILD_USBPROG == 1
  105. extern jtag_interface_t usbprog_interface;
  106. #endif
  107. #if BUILD_JLINK == 1
  108. extern jtag_interface_t jlink_interface;
  109. #endif
  110. #if BUILD_VSLLINK == 1
  111. extern jtag_interface_t vsllink_interface;
  112. #endif
  113. #if BUILD_RLINK == 1
  114. extern jtag_interface_t rlink_interface;
  115. #endif
  116. #if BUILD_ARMJTAGEW == 1
  117. extern jtag_interface_t armjtagew_interface;
  118. #endif
  119. jtag_interface_t *jtag_interfaces[] = {
  120. #if BUILD_ECOSBOARD == 1
  121. &zy1000_interface,
  122. #endif
  123. #if BUILD_PARPORT == 1
  124. &parport_interface,
  125. #endif
  126. #if BUILD_DUMMY == 1
  127. &dummy_interface,
  128. #endif
  129. #if BUILD_FT2232_FTD2XX == 1
  130. &ft2232_interface,
  131. #endif
  132. #if BUILD_FT2232_LIBFTDI == 1
  133. &ft2232_interface,
  134. #endif
  135. #if BUILD_AMTJTAGACCEL == 1
  136. &amt_jtagaccel_interface,
  137. #endif
  138. #if BUILD_EP93XX == 1
  139. &ep93xx_interface,
  140. #endif
  141. #if BUILD_AT91RM9200 == 1
  142. &at91rm9200_interface,
  143. #endif
  144. #if BUILD_GW16012 == 1
  145. &gw16012_interface,
  146. #endif
  147. #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
  148. &presto_interface,
  149. #endif
  150. #if BUILD_USBPROG == 1
  151. &usbprog_interface,
  152. #endif
  153. #if BUILD_JLINK == 1
  154. &jlink_interface,
  155. #endif
  156. #if BUILD_VSLLINK == 1
  157. &vsllink_interface,
  158. #endif
  159. #if BUILD_RLINK == 1
  160. &rlink_interface,
  161. #endif
  162. #if BUILD_ARMJTAGEW == 1
  163. &armjtagew_interface,
  164. #endif
  165. NULL,
  166. };
  167. struct jtag_interface_s *jtag = NULL;
  168. /* configuration */
  169. static jtag_interface_t *jtag_interface = NULL;
  170. int jtag_speed = 0;
  171. /* jtag commands */
  172. static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  173. static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  174. static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  175. static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  176. static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  177. static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  178. static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  179. static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  180. static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  181. static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  182. static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  183. static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  184. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  185. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
  186. static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  187. static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  188. static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  189. jtag_tap_t *jtag_AllTaps(void)
  190. {
  191. return jtag_all_taps;
  192. };
  193. int jtag_NumTotalTaps(void)
  194. {
  195. jtag_tap_t *t;
  196. int n;
  197. n = 0;
  198. t = jtag_AllTaps();
  199. while(t){
  200. n++;
  201. t = t->next_tap;
  202. }
  203. return n;
  204. }
  205. int jtag_NumEnabledTaps(void)
  206. {
  207. jtag_tap_t *t;
  208. int n;
  209. n = 0;
  210. t = jtag_AllTaps();
  211. while(t){
  212. if( t->enabled ){
  213. n++;
  214. }
  215. t = t->next_tap;
  216. }
  217. return n;
  218. }
  219. jtag_tap_t *jtag_TapByString( const char *s )
  220. {
  221. jtag_tap_t *t;
  222. char *cp;
  223. t = jtag_AllTaps();
  224. /* try name first */
  225. while(t){
  226. if( 0 == strcmp( t->dotted_name, s ) ){
  227. break;
  228. } else {
  229. t = t->next_tap;
  230. }
  231. }
  232. /* backup plan is by number */
  233. if( t == NULL ){
  234. /* ok - is "s" a number? */
  235. int n;
  236. n = strtol( s, &cp, 0 );
  237. if( (s != cp) && (*cp == 0) ){
  238. /* Then it is... */
  239. t = jtag_TapByAbsPosition(n);
  240. }
  241. }
  242. return t;
  243. }
  244. jtag_tap_t * jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
  245. {
  246. jtag_tap_t *t;
  247. const char *cp;
  248. cp = Jim_GetString( o, NULL );
  249. if(cp == NULL){
  250. cp = "(unknown)";
  251. t = NULL;
  252. } else {
  253. t = jtag_TapByString( cp );
  254. }
  255. if( t == NULL ){
  256. Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
  257. }
  258. return t;
  259. }
  260. /* returns a pointer to the n-th device in the scan chain */
  261. jtag_tap_t * jtag_TapByAbsPosition( int n )
  262. {
  263. int orig_n;
  264. jtag_tap_t *t;
  265. orig_n = n;
  266. t = jtag_AllTaps();
  267. while( t && (n > 0)) {
  268. n--;
  269. t = t->next_tap;
  270. }
  271. return t;
  272. }
  273. int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
  274. {
  275. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  276. if (callback == NULL)
  277. {
  278. return ERROR_INVALID_ARGUMENTS;
  279. }
  280. if (*callbacks_p)
  281. {
  282. while ((*callbacks_p)->next)
  283. callbacks_p = &((*callbacks_p)->next);
  284. callbacks_p = &((*callbacks_p)->next);
  285. }
  286. (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
  287. (*callbacks_p)->callback = callback;
  288. (*callbacks_p)->priv = priv;
  289. (*callbacks_p)->next = NULL;
  290. return ERROR_OK;
  291. }
  292. int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
  293. {
  294. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  295. if (callback == NULL)
  296. {
  297. return ERROR_INVALID_ARGUMENTS;
  298. }
  299. while (*callbacks_p)
  300. {
  301. jtag_event_callback_t **next = &((*callbacks_p)->next);
  302. if ((*callbacks_p)->callback == callback)
  303. {
  304. free(*callbacks_p);
  305. *callbacks_p = *next;
  306. }
  307. callbacks_p = next;
  308. }
  309. return ERROR_OK;
  310. }
  311. int jtag_call_event_callbacks(enum jtag_event event)
  312. {
  313. jtag_event_callback_t *callback = jtag_event_callbacks;
  314. LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
  315. while (callback)
  316. {
  317. callback->callback(event, callback->priv);
  318. callback = callback->next;
  319. }
  320. return ERROR_OK;
  321. }
  322. static void jtag_checks(void)
  323. {
  324. assert(jtag_trst == 0);
  325. }
  326. static void jtag_prelude(tap_state_t state)
  327. {
  328. jtag_checks();
  329. assert(state!=TAP_INVALID);
  330. cmd_queue_cur_state = state;
  331. }
  332. void jtag_alloc_in_value32(scan_field_t *field)
  333. {
  334. interface_jtag_alloc_in_value32(field);
  335. }
  336. void jtag_add_ir_scan_noverify(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  337. {
  338. int retval;
  339. jtag_prelude(state);
  340. retval=interface_jtag_add_ir_scan(in_num_fields, in_fields, state);
  341. if (retval!=ERROR_OK)
  342. jtag_error=retval;
  343. }
  344. /**
  345. * Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
  346. *
  347. * If the input field list contains an instruction value for a TAP then that is used
  348. * otherwise the TAP is set to bypass.
  349. *
  350. * TAPs for which no fields are passed are marked as bypassed for subsequent DR SCANs.
  351. *
  352. */
  353. void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  354. {
  355. if (jtag_verify&&jtag_verify_capture_ir)
  356. {
  357. /* 8 x 32 bit id's is enough for all invocations */
  358. for (int j = 0; j < in_num_fields; j++)
  359. {
  360. /* if we are to run a verification of the ir scan, we need to get the input back.
  361. * We may have to allocate space if the caller didn't ask for the input back.
  362. */
  363. in_fields[j].check_value=in_fields[j].tap->expected;
  364. in_fields[j].check_mask=in_fields[j].tap->expected_mask;
  365. }
  366. jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
  367. } else
  368. {
  369. jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
  370. }
  371. }
  372. /**
  373. * Duplicate the scan fields passed into the function into an IR SCAN command
  374. *
  375. * This function assumes that the caller handles extra fields for bypassed TAPs
  376. *
  377. */
  378. void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  379. {
  380. int retval;
  381. jtag_prelude(state);
  382. retval=interface_jtag_add_plain_ir_scan(in_num_fields, in_fields, state);
  383. if (retval!=ERROR_OK)
  384. jtag_error=retval;
  385. }
  386. void jtag_add_callback(jtag_callback1_t f, u8 *in)
  387. {
  388. interface_jtag_add_callback(f, in);
  389. }
  390. void jtag_add_callback4(jtag_callback_t f, u8 *in,
  391. jtag_callback_data_t data1, jtag_callback_data_t data2,
  392. jtag_callback_data_t data3)
  393. {
  394. interface_jtag_add_callback4(f, in, data1, data2, data3);
  395. }
  396. int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
  397. static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  398. {
  399. return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
  400. }
  401. static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
  402. int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  403. {
  404. for (int i = 0; i < in_num_fields; i++)
  405. {
  406. struct scan_field_s *field = &in_fields[i];
  407. field->allocated = 0;
  408. field->modified = 0;
  409. if (field->check_value || field->in_value)
  410. continue;
  411. interface_jtag_add_scan_check_alloc(field);
  412. field->modified = 1;
  413. }
  414. jtag_add_scan(in_num_fields, in_fields, state);
  415. for (int i = 0; i < in_num_fields; i++)
  416. {
  417. if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
  418. {
  419. /* this is synchronous for a minidriver */
  420. jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
  421. (jtag_callback_data_t)in_fields[i].check_value,
  422. (jtag_callback_data_t)in_fields[i].check_mask,
  423. (jtag_callback_data_t)in_fields[i].num_bits);
  424. }
  425. if (in_fields[i].allocated)
  426. {
  427. free(in_fields[i].in_value);
  428. }
  429. if (in_fields[i].modified)
  430. {
  431. in_fields[i].in_value = NULL;
  432. }
  433. }
  434. }
  435. void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  436. {
  437. if (jtag_verify)
  438. {
  439. jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
  440. } else
  441. {
  442. jtag_add_dr_scan(in_num_fields, in_fields, state);
  443. }
  444. }
  445. /**
  446. * Generate a DR SCAN using the fields passed to the function
  447. *
  448. * For not bypassed TAPs the function checks in_fields and uses fields specified there.
  449. * For bypassed TAPs the function generates a dummy 1bit field.
  450. *
  451. * The bypass status of TAPs is set by jtag_add_ir_scan().
  452. *
  453. */
  454. void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  455. {
  456. int retval;
  457. jtag_prelude(state);
  458. retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
  459. if (retval!=ERROR_OK)
  460. jtag_error=retval;
  461. }
  462. /**
  463. * Duplicate the scan fields passed into the function into a DR SCAN command
  464. *
  465. * This function assumes that the caller handles extra fields for bypassed TAPs
  466. *
  467. */
  468. void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  469. {
  470. int retval;
  471. jtag_prelude(state);
  472. retval=interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
  473. if (retval!=ERROR_OK)
  474. jtag_error=retval;
  475. }
  476. void jtag_add_dr_out(jtag_tap_t* tap,
  477. int num_fields, const int* num_bits, const u32* value,
  478. tap_state_t end_state)
  479. {
  480. assert(end_state != TAP_INVALID);
  481. cmd_queue_cur_state = end_state;
  482. interface_jtag_add_dr_out(tap,
  483. num_fields, num_bits, value,
  484. end_state);
  485. }
  486. void jtag_add_tlr(void)
  487. {
  488. jtag_prelude(TAP_RESET);
  489. int retval;
  490. retval=interface_jtag_add_tlr();
  491. if (retval!=ERROR_OK)
  492. jtag_error=retval;
  493. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  494. }
  495. void jtag_add_pathmove(int num_states, const tap_state_t *path)
  496. {
  497. tap_state_t cur_state = cmd_queue_cur_state;
  498. int i;
  499. int retval;
  500. /* the last state has to be a stable state */
  501. if (!tap_is_state_stable(path[num_states - 1]))
  502. {
  503. LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
  504. exit(-1);
  505. }
  506. for (i=0; i<num_states; i++)
  507. {
  508. if (path[i] == TAP_RESET)
  509. {
  510. LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
  511. exit(-1);
  512. }
  513. if ( tap_state_transition(cur_state, true) != path[i]
  514. && tap_state_transition(cur_state, false) != path[i])
  515. {
  516. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[i]));
  517. exit(-1);
  518. }
  519. cur_state = path[i];
  520. }
  521. jtag_checks();
  522. retval = interface_jtag_add_pathmove(num_states, path);
  523. cmd_queue_cur_state = path[num_states - 1];
  524. if (retval!=ERROR_OK)
  525. jtag_error=retval;
  526. }
  527. void jtag_add_runtest(int num_cycles, tap_state_t state)
  528. {
  529. int retval;
  530. jtag_prelude(state);
  531. /* executed by sw or hw fifo */
  532. retval=interface_jtag_add_runtest(num_cycles, state);
  533. if (retval!=ERROR_OK)
  534. jtag_error=retval;
  535. }
  536. void jtag_add_clocks( int num_cycles )
  537. {
  538. int retval;
  539. if( !tap_is_state_stable(cmd_queue_cur_state) )
  540. {
  541. LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
  542. tap_state_name(cmd_queue_cur_state) );
  543. jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
  544. return;
  545. }
  546. if( num_cycles > 0 )
  547. {
  548. jtag_checks();
  549. retval = interface_jtag_add_clocks(num_cycles);
  550. if (retval != ERROR_OK)
  551. jtag_error=retval;
  552. }
  553. }
  554. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  555. {
  556. int trst_with_tlr = 0;
  557. int retval;
  558. /* FIX!!! there are *many* different cases here. A better
  559. * approach is needed for legal combinations of transitions...
  560. */
  561. if ((jtag_reset_config & RESET_HAS_SRST)&&
  562. (jtag_reset_config & RESET_HAS_TRST)&&
  563. ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
  564. {
  565. if (((req_tlr_or_trst&&!jtag_trst)||
  566. (!req_tlr_or_trst&&jtag_trst))&&
  567. ((req_srst&&!jtag_srst)||
  568. (!req_srst&&jtag_srst)))
  569. {
  570. /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
  571. //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
  572. }
  573. }
  574. /* Make sure that jtag_reset_config allows the requested reset */
  575. /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
  576. if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
  577. {
  578. LOG_ERROR("BUG: requested reset would assert trst");
  579. jtag_error=ERROR_FAIL;
  580. return;
  581. }
  582. /* if TRST pulls SRST, we reset with TAP T-L-R */
  583. if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
  584. {
  585. trst_with_tlr = 1;
  586. }
  587. if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
  588. {
  589. LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
  590. jtag_error=ERROR_FAIL;
  591. return;
  592. }
  593. if (req_tlr_or_trst)
  594. {
  595. if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
  596. {
  597. jtag_trst = 1;
  598. } else
  599. {
  600. trst_with_tlr = 1;
  601. }
  602. } else
  603. {
  604. jtag_trst = 0;
  605. }
  606. jtag_srst = req_srst;
  607. retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
  608. if (retval!=ERROR_OK)
  609. {
  610. jtag_error=retval;
  611. return;
  612. }
  613. jtag_execute_queue();
  614. if (jtag_srst)
  615. {
  616. LOG_DEBUG("SRST line asserted");
  617. }
  618. else
  619. {
  620. LOG_DEBUG("SRST line released");
  621. if (jtag_nsrst_delay)
  622. jtag_add_sleep(jtag_nsrst_delay * 1000);
  623. }
  624. if (trst_with_tlr)
  625. {
  626. LOG_DEBUG("JTAG reset with RESET instead of TRST");
  627. jtag_set_end_state(TAP_RESET);
  628. jtag_add_tlr();
  629. return;
  630. }
  631. if (jtag_trst)
  632. {
  633. /* we just asserted nTRST, so we're now in Test-Logic-Reset,
  634. * and inform possible listeners about this
  635. */
  636. LOG_DEBUG("TRST line asserted");
  637. tap_set_state(TAP_RESET);
  638. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  639. }
  640. else
  641. {
  642. if (jtag_ntrst_delay)
  643. jtag_add_sleep(jtag_ntrst_delay * 1000);
  644. }
  645. }
  646. tap_state_t jtag_set_end_state(tap_state_t state)
  647. {
  648. if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
  649. {
  650. LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
  651. }
  652. if (state!=TAP_INVALID)
  653. cmd_queue_end_state = state;
  654. return cmd_queue_end_state;
  655. }
  656. tap_state_t jtag_get_end_state(void)
  657. {
  658. return cmd_queue_end_state;
  659. }
  660. void jtag_add_sleep(u32 us)
  661. {
  662. keep_alive(); /* we might be running on a very slow JTAG clk */
  663. int retval=interface_jtag_add_sleep(us);
  664. if (retval!=ERROR_OK)
  665. jtag_error=retval;
  666. return;
  667. }
  668. static const char *jtag_tap_name(const jtag_tap_t *tap)
  669. {
  670. return (tap == NULL) ? "(unknown)" : tap->dotted_name;
  671. }
  672. int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
  673. {
  674. int retval = ERROR_OK;
  675. int compare_failed = 0;
  676. if (in_check_mask)
  677. compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
  678. else
  679. compare_failed = buf_cmp(captured, in_check_value, num_bits);
  680. if (compare_failed){
  681. /* An error handler could have caught the failing check
  682. * only report a problem when there wasn't a handler, or if the handler
  683. * acknowledged the error
  684. */
  685. /*
  686. LOG_WARNING("TAP %s:",
  687. jtag_tap_name(field->tap));
  688. */
  689. if (compare_failed)
  690. {
  691. char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  692. char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  693. if (in_check_mask)
  694. {
  695. char *in_check_mask_char;
  696. in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  697. LOG_WARNING("value captured during scan didn't pass the requested check:");
  698. LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
  699. captured_char, in_check_value_char, in_check_mask_char);
  700. free(in_check_mask_char);
  701. }
  702. else
  703. {
  704. 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);
  705. }
  706. free(captured_char);
  707. free(in_check_value_char);
  708. retval = ERROR_JTAG_QUEUE_FAILED;
  709. }
  710. }
  711. return retval;
  712. }
  713. void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
  714. {
  715. assert(field->in_value != NULL);
  716. if (value==NULL)
  717. {
  718. /* no checking to do */
  719. return;
  720. }
  721. jtag_execute_queue_noclear();
  722. int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
  723. jtag_set_error(retval);
  724. }
  725. int default_interface_jtag_execute_queue(void)
  726. {
  727. if (NULL == jtag)
  728. {
  729. LOG_ERROR("No JTAG interface configured yet. "
  730. "Issue 'init' command in startup scripts "
  731. "before communicating with targets.");
  732. return ERROR_FAIL;
  733. }
  734. return jtag->execute_queue();
  735. }
  736. void jtag_execute_queue_noclear(void)
  737. {
  738. /* each flush can take as much as 1-2ms on high bandwidth low latency interfaces.
  739. * E.g. a JTAG over TCP/IP or USB....
  740. */
  741. jtag_flush_queue_count++;
  742. int retval=interface_jtag_execute_queue();
  743. /* we keep the first error */
  744. if ((jtag_error==ERROR_OK)&&(retval!=ERROR_OK))
  745. {
  746. jtag_error=retval;
  747. }
  748. }
  749. int jtag_execute_queue(void)
  750. {
  751. int retval;
  752. jtag_execute_queue_noclear();
  753. retval=jtag_error;
  754. jtag_error=ERROR_OK;
  755. return retval;
  756. }
  757. int jtag_reset_callback(enum jtag_event event, void *priv)
  758. {
  759. jtag_tap_t *tap = priv;
  760. LOG_DEBUG("-");
  761. if (event == JTAG_TRST_ASSERTED)
  762. {
  763. buf_set_ones(tap->cur_instr, tap->ir_length);
  764. tap->bypass = 1;
  765. }
  766. return ERROR_OK;
  767. }
  768. void jtag_sleep(u32 us)
  769. {
  770. alive_sleep(us/1000);
  771. }
  772. /* Try to examine chain layout according to IEEE 1149.1 §12
  773. */
  774. int jtag_examine_chain(void)
  775. {
  776. jtag_tap_t *tap;
  777. scan_field_t field;
  778. u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
  779. int i;
  780. int bit_count;
  781. int device_count = 0;
  782. u8 zero_check = 0x0;
  783. u8 one_check = 0xff;
  784. field.tap = NULL;
  785. field.num_bits = sizeof(idcode_buffer) * 8;
  786. field.out_value = idcode_buffer;
  787. field.in_value = idcode_buffer;
  788. for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
  789. {
  790. buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
  791. }
  792. jtag_add_plain_dr_scan(1, &field, TAP_RESET);
  793. jtag_execute_queue();
  794. for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
  795. {
  796. zero_check |= idcode_buffer[i];
  797. one_check &= idcode_buffer[i];
  798. }
  799. /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
  800. if ((zero_check == 0x00) || (one_check == 0xff))
  801. {
  802. LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
  803. return ERROR_JTAG_INIT_FAILED;
  804. }
  805. /* point at the 1st tap */
  806. tap = jtag_NextEnabledTap(NULL);
  807. if( tap == NULL ){
  808. LOG_ERROR("JTAG: No taps enabled?");
  809. return ERROR_JTAG_INIT_FAILED;
  810. }
  811. for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
  812. {
  813. u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  814. if ((idcode & 1) == 0)
  815. {
  816. /* LSB must not be 0, this indicates a device in bypass */
  817. LOG_WARNING("Tap/Device does not have IDCODE");
  818. idcode=0;
  819. bit_count += 1;
  820. }
  821. else
  822. {
  823. u32 manufacturer;
  824. u32 part;
  825. u32 version;
  826. /* some devices, such as AVR will output all 1's instead of TDI
  827. input value at end of chain. */
  828. if ((idcode == 0x000000FF)||(idcode == 0xFFFFFFFF))
  829. {
  830. int unexpected=0;
  831. /* End of chain (invalid manufacturer ID)
  832. *
  833. * The JTAG examine is the very first thing that happens
  834. *
  835. * A single JTAG device requires only 64 bits to be read back correctly.
  836. *
  837. * The code below adds a check that the rest of the data scanned (640 bits)
  838. * are all as expected. This helps diagnose/catch problems with the JTAG chain
  839. *
  840. * earlier and gives more helpful/explicit error messages.
  841. */
  842. for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
  843. {
  844. idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  845. if (unexpected||((idcode != 0x000000FF)&&(idcode != 0xFFFFFFFF)))
  846. {
  847. LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
  848. unexpected = 1;
  849. }
  850. }
  851. break;
  852. }
  853. #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
  854. manufacturer = EXTRACT_MFG(idcode);
  855. #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
  856. part = EXTRACT_PART(idcode);
  857. #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
  858. version = EXTRACT_VER(idcode);
  859. LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
  860. ((tap != NULL) ? (tap->dotted_name) : "(not-named)"),
  861. idcode, manufacturer, part, version);
  862. bit_count += 32;
  863. }
  864. if (tap)
  865. {
  866. tap->idcode = idcode;
  867. if (tap->expected_ids_cnt > 0) {
  868. /* Loop over the expected identification codes and test for a match */
  869. u8 ii;
  870. for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
  871. if( tap->idcode == tap->expected_ids[ii] ){
  872. break;
  873. }
  874. }
  875. /* If none of the expected ids matched, log an error */
  876. if (ii == tap->expected_ids_cnt) {
  877. LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  878. tap->dotted_name,
  879. idcode,
  880. EXTRACT_MFG( tap->idcode ),
  881. EXTRACT_PART( tap->idcode ),
  882. EXTRACT_VER( tap->idcode ) );
  883. for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
  884. LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  885. tap->dotted_name,
  886. ii + 1,
  887. tap->expected_ids_cnt,
  888. tap->expected_ids[ii],
  889. EXTRACT_MFG( tap->expected_ids[ii] ),
  890. EXTRACT_PART( tap->expected_ids[ii] ),
  891. EXTRACT_VER( tap->expected_ids[ii] ) );
  892. }
  893. return ERROR_JTAG_INIT_FAILED;
  894. } else {
  895. LOG_INFO("JTAG Tap/device matched");
  896. }
  897. } else {
  898. #if 0
  899. LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
  900. tap->idcode);
  901. #endif
  902. }
  903. tap = jtag_NextEnabledTap(tap);
  904. }
  905. device_count++;
  906. }
  907. /* see if number of discovered devices matches configuration */
  908. if (device_count != jtag_NumEnabledTaps())
  909. {
  910. LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
  911. device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
  912. LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
  913. return ERROR_JTAG_INIT_FAILED;
  914. }
  915. return ERROR_OK;
  916. }
  917. int jtag_validate_chain(void)
  918. {
  919. jtag_tap_t *tap;
  920. int total_ir_length = 0;
  921. u8 *ir_test = NULL;
  922. scan_field_t field;
  923. int chain_pos = 0;
  924. tap = NULL;
  925. total_ir_length = 0;
  926. for(;;){
  927. tap = jtag_NextEnabledTap(tap);
  928. if( tap == NULL ){
  929. break;
  930. }
  931. total_ir_length += tap->ir_length;
  932. }
  933. total_ir_length += 2;
  934. ir_test = malloc(CEIL(total_ir_length, 8));
  935. buf_set_ones(ir_test, total_ir_length);
  936. field.tap = NULL;
  937. field.num_bits = total_ir_length;
  938. field.out_value = ir_test;
  939. field.in_value = ir_test;
  940. jtag_add_plain_ir_scan(1, &field, TAP_RESET);
  941. jtag_execute_queue();
  942. tap = NULL;
  943. chain_pos = 0;
  944. int val;
  945. for(;;){
  946. tap = jtag_NextEnabledTap(tap);
  947. if( tap == NULL ){
  948. break;
  949. }
  950. val = buf_get_u32(ir_test, chain_pos, 2);
  951. if (val != 0x1)
  952. {
  953. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  954. 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);
  955. free(cbuf);
  956. free(ir_test);
  957. return ERROR_JTAG_INIT_FAILED;
  958. }
  959. chain_pos += tap->ir_length;
  960. }
  961. val = buf_get_u32(ir_test, chain_pos, 2);
  962. if (val != 0x3)
  963. {
  964. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  965. 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);
  966. free(cbuf);
  967. free(ir_test);
  968. return ERROR_JTAG_INIT_FAILED;
  969. }
  970. free(ir_test);
  971. return ERROR_OK;
  972. }
  973. enum jtag_tap_cfg_param {
  974. JCFG_EVENT
  975. };
  976. static Jim_Nvp nvp_config_opts[] = {
  977. { .name = "-event", .value = JCFG_EVENT },
  978. { .name = NULL, .value = -1 }
  979. };
  980. static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
  981. {
  982. Jim_Nvp *n;
  983. Jim_Obj *o;
  984. int e;
  985. /* parse config or cget options */
  986. while (goi->argc > 0) {
  987. Jim_SetEmptyResult (goi->interp);
  988. e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
  989. if (e != JIM_OK) {
  990. Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
  991. return e;
  992. }
  993. switch (n->value) {
  994. case JCFG_EVENT:
  995. if (goi->argc == 0) {
  996. Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
  997. return JIM_ERR;
  998. }
  999. e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
  1000. if (e != JIM_OK) {
  1001. Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
  1002. return e;
  1003. }
  1004. if (goi->isconfigure) {
  1005. if (goi->argc != 1) {
  1006. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
  1007. return JIM_ERR;
  1008. }
  1009. } else {
  1010. if (goi->argc != 0) {
  1011. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
  1012. return JIM_ERR;
  1013. }
  1014. }
  1015. {
  1016. jtag_tap_event_action_t *jteap;
  1017. jteap = tap->event_action;
  1018. /* replace existing? */
  1019. while (jteap) {
  1020. if (jteap->event == (enum jtag_tap_event)n->value) {
  1021. break;
  1022. }
  1023. jteap = jteap->next;
  1024. }
  1025. if (goi->isconfigure) {
  1026. if (jteap == NULL) {
  1027. /* create new */
  1028. jteap = calloc(1, sizeof (*jteap));
  1029. }
  1030. jteap->event = n->value;
  1031. Jim_GetOpt_Obj( goi, &o);
  1032. if (jteap->body) {
  1033. Jim_DecrRefCount(interp, jteap->body);
  1034. }
  1035. jteap->body = Jim_DuplicateObj(goi->interp, o);
  1036. Jim_IncrRefCount(jteap->body);
  1037. /* add to head of event list */
  1038. jteap->next = tap->event_action;
  1039. tap->event_action = jteap;
  1040. Jim_SetEmptyResult(goi->interp);
  1041. } else {
  1042. /* get */
  1043. if (jteap == NULL) {
  1044. Jim_SetEmptyResult(goi->interp);
  1045. } else {
  1046. Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
  1047. }
  1048. }
  1049. }
  1050. /* loop for more */
  1051. break;
  1052. }
  1053. } /* while (goi->argc) */
  1054. return JIM_OK;
  1055. }
  1056. static int jim_newtap_cmd( Jim_GetOptInfo *goi )
  1057. {
  1058. jtag_tap_t *pTap;
  1059. jtag_tap_t **ppTap;
  1060. jim_wide w;
  1061. int x;
  1062. int e;
  1063. int reqbits;
  1064. Jim_Nvp *n;
  1065. char *cp;
  1066. const Jim_Nvp opts[] = {
  1067. #define NTAP_OPT_IRLEN 0
  1068. { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
  1069. #define NTAP_OPT_IRMASK 1
  1070. { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
  1071. #define NTAP_OPT_IRCAPTURE 2
  1072. { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
  1073. #define NTAP_OPT_ENABLED 3
  1074. { .name = "-enable" , .value = NTAP_OPT_ENABLED },
  1075. #define NTAP_OPT_DISABLED 4
  1076. { .name = "-disable" , .value = NTAP_OPT_DISABLED },
  1077. #define NTAP_OPT_EXPECTED_ID 5
  1078. { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
  1079. { .name = NULL , .value = -1 },
  1080. };
  1081. pTap = malloc( sizeof(jtag_tap_t) );
  1082. memset( pTap, 0, sizeof(*pTap) );
  1083. if( !pTap ){
  1084. Jim_SetResult_sprintf( goi->interp, "no memory");
  1085. return JIM_ERR;
  1086. }
  1087. /*
  1088. * we expect CHIP + TAP + OPTIONS
  1089. * */
  1090. if( goi->argc < 3 ){
  1091. Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
  1092. return JIM_ERR;
  1093. }
  1094. Jim_GetOpt_String( goi, &cp, NULL );
  1095. pTap->chip = strdup(cp);
  1096. Jim_GetOpt_String( goi, &cp, NULL );
  1097. pTap->tapname = strdup(cp);
  1098. /* name + dot + name + null */
  1099. x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
  1100. cp = malloc( x );
  1101. sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
  1102. pTap->dotted_name = cp;
  1103. LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
  1104. pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
  1105. /* default is enabled */
  1106. pTap->enabled = 1;
  1107. /* deal with options */
  1108. #define NTREQ_IRLEN 1
  1109. #define NTREQ_IRCAPTURE 2
  1110. #define NTREQ_IRMASK 4
  1111. /* clear them as we find them */
  1112. reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
  1113. while( goi->argc ){
  1114. e = Jim_GetOpt_Nvp( goi, opts, &n );
  1115. if( e != JIM_OK ){
  1116. Jim_GetOpt_NvpUnknown( goi, opts, 0 );
  1117. return e;
  1118. }
  1119. LOG_DEBUG("Processing option: %s", n->name );
  1120. switch( n->value ){
  1121. case NTAP_OPT_ENABLED:
  1122. pTap->enabled = 1;
  1123. break;
  1124. case NTAP_OPT_DISABLED:
  1125. pTap->enabled = 0;
  1126. break;
  1127. case NTAP_OPT_EXPECTED_ID:
  1128. {
  1129. u32 *new_expected_ids;
  1130. e = Jim_GetOpt_Wide( goi, &w );
  1131. if( e != JIM_OK) {
  1132. Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
  1133. return e;
  1134. }
  1135. new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
  1136. if (new_expected_ids == NULL) {
  1137. Jim_SetResult_sprintf( goi->interp, "no memory");
  1138. return JIM_ERR;
  1139. }
  1140. memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
  1141. new_expected_ids[pTap->expected_ids_cnt] = w;
  1142. free(pTap->expected_ids);
  1143. pTap->expected_ids = new_expected_ids;
  1144. pTap->expected_ids_cnt++;
  1145. break;
  1146. }
  1147. case NTAP_OPT_IRLEN:
  1148. case NTAP_OPT_IRMASK:
  1149. case NTAP_OPT_IRCAPTURE:
  1150. e = Jim_GetOpt_Wide( goi, &w );
  1151. if( e != JIM_OK ){
  1152. Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
  1153. return e;
  1154. }
  1155. if( (w < 0) || (w > 0xffff) ){
  1156. /* wacky value */
  1157. Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
  1158. n->name, (int)(w), (int)(w));
  1159. return JIM_ERR;
  1160. }
  1161. switch(n->value){
  1162. case NTAP_OPT_IRLEN:
  1163. pTap->ir_length = w;
  1164. reqbits &= (~(NTREQ_IRLEN));
  1165. break;
  1166. case NTAP_OPT_IRMASK:
  1167. pTap->ir_capture_mask = w;
  1168. reqbits &= (~(NTREQ_IRMASK));
  1169. break;
  1170. case NTAP_OPT_IRCAPTURE:
  1171. pTap->ir_capture_value = w;
  1172. reqbits &= (~(NTREQ_IRCAPTURE));
  1173. break;
  1174. }
  1175. } /* switch(n->value) */
  1176. } /* while( goi->argc ) */
  1177. /* Did we get all the options? */
  1178. if( reqbits ){
  1179. // no
  1180. Jim_SetResult_sprintf( goi->interp,
  1181. "newtap: %s missing required parameters",
  1182. pTap->dotted_name);
  1183. /* TODO: Tell user what is missing :-( */
  1184. /* no memory leaks pelase */
  1185. free(((void *)(pTap->expected_ids)));
  1186. free(((void *)(pTap->chip)));
  1187. free(((void *)(pTap->tapname)));
  1188. free(((void *)(pTap->dotted_name)));
  1189. free(((void *)(pTap)));
  1190. return JIM_ERR;
  1191. }
  1192. pTap->expected = malloc( pTap->ir_length );
  1193. pTap->expected_mask = malloc( pTap->ir_length );
  1194. pTap->cur_instr = malloc( pTap->ir_length );
  1195. buf_set_u32( pTap->expected,
  1196. 0,
  1197. pTap->ir_length,
  1198. pTap->ir_capture_value );
  1199. buf_set_u32( pTap->expected_mask,
  1200. 0,
  1201. pTap->ir_length,
  1202. pTap->ir_capture_mask );
  1203. buf_set_ones( pTap->cur_instr,
  1204. pTap->ir_length );
  1205. pTap->bypass = 1;
  1206. jtag_register_event_callback(jtag_reset_callback, pTap );
  1207. ppTap = &(jtag_all_taps);
  1208. while( (*ppTap) != NULL ){
  1209. ppTap = &((*ppTap)->next_tap);
  1210. }
  1211. *ppTap = pTap;
  1212. {
  1213. static int n_taps = 0;
  1214. pTap->abs_chain_position = n_taps++;
  1215. }
  1216. LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
  1217. (*ppTap)->dotted_name,
  1218. (*ppTap)->abs_chain_position,
  1219. (*ppTap)->ir_length,
  1220. (*ppTap)->ir_capture_value,
  1221. (*ppTap)->ir_capture_mask );
  1222. return ERROR_OK;
  1223. }
  1224. static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
  1225. {
  1226. Jim_GetOptInfo goi;
  1227. int e;
  1228. Jim_Nvp *n;
  1229. Jim_Obj *o;
  1230. struct command_context_s *context;
  1231. enum {
  1232. JTAG_CMD_INTERFACE,
  1233. JTAG_CMD_INIT_RESET,
  1234. JTAG_CMD_NEWTAP,
  1235. JTAG_CMD_TAPENABLE,
  1236. JTAG_CMD_TAPDISABLE,
  1237. JTAG_CMD_TAPISENABLED,
  1238. JTAG_CMD_CONFIGURE,
  1239. JTAG_CMD_CGET
  1240. };
  1241. const Jim_Nvp jtag_cmds[] = {
  1242. { .name = "interface" , .value = JTAG_CMD_INTERFACE },
  1243. { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
  1244. { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
  1245. { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
  1246. { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
  1247. { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
  1248. { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
  1249. { .name = "cget" , .value = JTAG_CMD_CGET },
  1250. { .name = NULL, .value = -1 },
  1251. };
  1252. context = Jim_GetAssocData(interp, "context");
  1253. /* go past the command */
  1254. Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
  1255. e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
  1256. if( e != JIM_OK ){
  1257. Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
  1258. return e;
  1259. }
  1260. Jim_SetEmptyResult( goi.interp );
  1261. switch( n->value ){
  1262. case JTAG_CMD_INTERFACE:
  1263. /* return the name of the interface */
  1264. /* TCL code might need to know the exact type... */
  1265. /* FUTURE: we allow this as a means to "set" the interface. */
  1266. if( goi.argc != 0 ){
  1267. Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
  1268. return JIM_ERR;
  1269. }
  1270. Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
  1271. return JIM_OK;
  1272. case JTAG_CMD_INIT_RESET:
  1273. if( goi.argc != 0 ){
  1274. Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
  1275. return JIM_ERR;
  1276. }
  1277. e = jtag_init_reset(context);
  1278. if( e != ERROR_OK ){
  1279. Jim_SetResult_sprintf( goi.interp, "error: %d", e);
  1280. return JIM_ERR;
  1281. }
  1282. return JIM_OK;
  1283. case JTAG_CMD_NEWTAP:
  1284. return jim_newtap_cmd( &goi );
  1285. break;
  1286. case JTAG_CMD_TAPISENABLED:
  1287. case JTAG_CMD_TAPENABLE:
  1288. case JTAG_CMD_TAPDISABLE:
  1289. if( goi.argc != 1 ){
  1290. Jim_SetResultString( goi.interp, "Too many parameters",-1 );
  1291. return JIM_ERR;
  1292. }
  1293. {
  1294. jtag_tap_t *t;
  1295. t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
  1296. if( t == NULL ){
  1297. return JIM_ERR;
  1298. }
  1299. switch( n->value ){
  1300. case JTAG_CMD_TAPISENABLED:
  1301. e = t->enabled;
  1302. break;
  1303. case JTAG_CMD_TAPENABLE:
  1304. jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
  1305. e = 1;
  1306. t->enabled = e;
  1307. break;
  1308. case JTAG_CMD_TAPDISABLE:
  1309. jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
  1310. e = 0;
  1311. t->enabled = e;
  1312. break;
  1313. }
  1314. Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
  1315. return JIM_OK;
  1316. }
  1317. break;
  1318. case JTAG_CMD_CGET:
  1319. if( goi.argc < 2 ){
  1320. Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
  1321. return JIM_ERR;
  1322. }
  1323. {
  1324. jtag_tap_t *t;
  1325. Jim_GetOpt_Obj(&goi, &o);
  1326. t = jtag_TapByJimObj( goi.interp, o );
  1327. if( t == NULL ){
  1328. return JIM_ERR;
  1329. }
  1330. goi.isconfigure = 0;
  1331. return jtag_tap_configure_cmd( &goi, t);
  1332. }
  1333. break;
  1334. case JTAG_CMD_CONFIGURE:
  1335. if( goi.argc < 3 ){
  1336. Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
  1337. return JIM_ERR;
  1338. }
  1339. {
  1340. jtag_tap_t *t;
  1341. Jim_GetOpt_Obj(&goi, &o);
  1342. t = jtag_TapByJimObj( goi.interp, o );
  1343. if( t == NULL ){
  1344. return JIM_ERR;
  1345. }
  1346. goi.isconfigure = 1;
  1347. return jtag_tap_configure_cmd( &goi, t);
  1348. }
  1349. }
  1350. return JIM_ERR;
  1351. }
  1352. int jtag_register_commands(struct command_context_s *cmd_ctx)
  1353. {
  1354. register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
  1355. register_command(cmd_ctx, NULL, "interface", handle_interface_command,
  1356. COMMAND_CONFIG, "try to configure interface");
  1357. register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
  1358. COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
  1359. register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
  1360. COMMAND_ANY, "set maximum jtag speed (if supported); "
  1361. "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
  1362. register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
  1363. COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
  1364. register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
  1365. COMMAND_ANY,
  1366. "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
  1367. register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
  1368. COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
  1369. register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
  1370. COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
  1371. register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
  1372. COMMAND_EXEC, "print current scan chain configuration");
  1373. register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
  1374. COMMAND_EXEC, "finish JTAG operations in <tap_state>");
  1375. register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
  1376. COMMAND_EXEC, "toggle reset lines <trst> <srst>");
  1377. register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
  1378. COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
  1379. register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
  1380. COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
  1381. register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
  1382. register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
  1383. register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
  1384. COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
  1385. register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
  1386. COMMAND_ANY, "verify value capture <enable|disable>");
  1387. register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
  1388. COMMAND_ANY, "choose short(default) or long tms_sequence <short|long>");
  1389. return ERROR_OK;
  1390. }
  1391. int jtag_interface_init(struct command_context_s *cmd_ctx)
  1392. {
  1393. if (jtag)
  1394. return ERROR_OK;
  1395. if (!jtag_interface)
  1396. {
  1397. /* nothing was previously specified by "interface" command */
  1398. LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
  1399. return ERROR_JTAG_INVALID_INTERFACE;
  1400. }
  1401. if(hasKHz)
  1402. {
  1403. jtag_interface->khz(speed_khz, &jtag_speed);
  1404. hasKHz = 0;
  1405. }
  1406. if (jtag_interface->init() != ERROR_OK)
  1407. return ERROR_JTAG_INIT_FAILED;
  1408. jtag = jtag_interface;
  1409. return ERROR_OK;
  1410. }
  1411. static int jtag_init_inner(struct command_context_s *cmd_ctx)
  1412. {
  1413. jtag_tap_t *tap;
  1414. int retval;
  1415. LOG_DEBUG("Init JTAG chain");
  1416. tap = jtag_NextEnabledTap(NULL);
  1417. if( tap == NULL ){
  1418. LOG_ERROR("There are no enabled taps?");
  1419. return ERROR_JTAG_INIT_FAILED;
  1420. }
  1421. jtag_add_tlr();
  1422. if ((retval=jtag_execute_queue())!=ERROR_OK)
  1423. return retval;
  1424. /* examine chain first, as this could discover the real chain layout */
  1425. if (jtag_examine_chain() != ERROR_OK)
  1426. {
  1427. LOG_ERROR("trying to validate configured JTAG chain anyway...");
  1428. }
  1429. if (jtag_validate_chain() != ERROR_OK)
  1430. {
  1431. LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
  1432. }
  1433. return ERROR_OK;
  1434. }
  1435. int jtag_interface_quit(void)
  1436. {
  1437. if (!jtag || !jtag->quit)
  1438. return ERROR_OK;
  1439. // close the JTAG interface
  1440. int result = jtag->quit();
  1441. if (ERROR_OK != result)
  1442. LOG_ERROR("failed: %d", result);
  1443. return ERROR_OK;
  1444. }
  1445. int jtag_init_reset(struct command_context_s *cmd_ctx)
  1446. {
  1447. int retval;
  1448. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1449. return retval;
  1450. LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
  1451. /* Reset can happen after a power cycle.
  1452. *
  1453. * Ideally we would only assert TRST or run RESET before the target reset.
  1454. *
  1455. * However w/srst_pulls_trst, trst is asserted together with the target
  1456. * reset whether we want it or not.
  1457. *
  1458. * NB! Some targets have JTAG circuitry disabled until a
  1459. * trst & srst has been asserted.
  1460. *
  1461. * NB! here we assume nsrst/ntrst delay are sufficient!
  1462. *
  1463. * NB! order matters!!!! srst *can* disconnect JTAG circuitry
  1464. *
  1465. */
  1466. jtag_add_reset(1, 0); /* RESET or TRST */
  1467. if (jtag_reset_config & RESET_HAS_SRST)
  1468. {
  1469. jtag_add_reset(1, 1);
  1470. if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
  1471. jtag_add_reset(0, 1);
  1472. }
  1473. jtag_add_reset(0, 0);
  1474. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1475. return retval;
  1476. /* Check that we can communication on the JTAG chain + eventually we want to
  1477. * be able to perform enumeration only after OpenOCD has started
  1478. * telnet and GDB server
  1479. *
  1480. * That would allow users to more easily perform any magic they need to before
  1481. * reset happens.
  1482. */
  1483. return jtag_init_inner(cmd_ctx);
  1484. }
  1485. int jtag_init(struct command_context_s *cmd_ctx)
  1486. {
  1487. int retval;
  1488. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1489. return retval;
  1490. if (jtag_init_inner(cmd_ctx)==ERROR_OK)
  1491. {
  1492. return ERROR_OK;
  1493. }
  1494. return jtag_init_reset(cmd_ctx);
  1495. }
  1496. static int default_khz(int khz, int *jtag_speed)
  1497. {
  1498. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  1499. return ERROR_FAIL;
  1500. }
  1501. static int default_speed_div(int speed, int *khz)
  1502. {
  1503. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  1504. return ERROR_FAIL;
  1505. }
  1506. static int default_power_dropout(int *dropout)
  1507. {
  1508. *dropout=0; /* by default we can't detect power dropout */
  1509. return ERROR_OK;
  1510. }
  1511. static int default_srst_asserted(int *srst_asserted)
  1512. {
  1513. *srst_asserted=0; /* by default we can't detect srst asserted */
  1514. return ERROR_OK;
  1515. }
  1516. static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1517. {
  1518. int i;
  1519. int retval;
  1520. /* check whether the interface is already configured */
  1521. if (jtag_interface)
  1522. {
  1523. LOG_WARNING("Interface already configured, ignoring");
  1524. return ERROR_OK;
  1525. }
  1526. /* interface name is a mandatory argument */
  1527. if (argc < 1 || args[0][0] == '\0')
  1528. {
  1529. return ERROR_COMMAND_SYNTAX_ERROR;
  1530. }
  1531. for (i=0; jtag_interfaces[i]; i++)
  1532. {
  1533. if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
  1534. {
  1535. if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
  1536. {
  1537. return retval;
  1538. }
  1539. jtag_interface = jtag_interfaces[i];
  1540. if (jtag_interface->khz == NULL)
  1541. {
  1542. jtag_interface->khz = default_khz;
  1543. }
  1544. if (jtag_interface->speed_div == NULL)
  1545. {
  1546. jtag_interface->speed_div = default_speed_div;
  1547. }
  1548. if (jtag_interface->power_dropout == NULL)
  1549. {
  1550. jtag_interface->power_dropout = default_power_dropout;
  1551. }
  1552. if (jtag_interface->srst_asserted == NULL)
  1553. {
  1554. jtag_interface->srst_asserted = default_srst_asserted;
  1555. }
  1556. return ERROR_OK;
  1557. }
  1558. }
  1559. /* no valid interface was found (i.e. the configuration option,
  1560. * didn't match one of the compiled-in interfaces
  1561. */
  1562. LOG_ERROR("No valid jtag interface found (%s)", args[0]);
  1563. LOG_ERROR("compiled-in jtag interfaces:");
  1564. for (i = 0; jtag_interfaces[i]; i++)
  1565. {
  1566. LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
  1567. }
  1568. return ERROR_JTAG_INVALID_INTERFACE;
  1569. }
  1570. static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1571. {
  1572. int e;
  1573. char buf[1024];
  1574. Jim_Obj *newargs[ 10 ];
  1575. /*
  1576. * CONVERT SYNTAX
  1577. * argv[-1] = command
  1578. * argv[ 0] = ir length
  1579. * argv[ 1] = ir capture
  1580. * argv[ 2] = ir mask
  1581. * argv[ 3] = not actually used by anything but in the docs
  1582. */
  1583. if( argc < 4 ){
  1584. command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
  1585. return ERROR_OK;
  1586. }
  1587. command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
  1588. command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
  1589. args[0],
  1590. args[1],
  1591. args[2] );
  1592. command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
  1593. command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
  1594. command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
  1595. command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
  1596. newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
  1597. newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
  1598. sprintf( buf, "chip%d", jtag_NumTotalTaps() );
  1599. newargs[2] = Jim_NewStringObj( interp, buf, -1 );
  1600. sprintf( buf, "tap%d", jtag_NumTotalTaps() );
  1601. newargs[3] = Jim_NewStringObj( interp, buf, -1 );
  1602. newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
  1603. newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
  1604. newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
  1605. newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
  1606. newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
  1607. newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
  1608. command_print( cmd_ctx, "NEW COMMAND:");
  1609. sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
  1610. Jim_GetString( newargs[0], NULL ),
  1611. Jim_GetString( newargs[1], NULL ),
  1612. Jim_GetString( newargs[2], NULL ),
  1613. Jim_GetString( newargs[3], NULL ),
  1614. Jim_GetString( newargs[4], NULL ),
  1615. Jim_GetString( newargs[5], NULL ),
  1616. Jim_GetString( newargs[6], NULL ),
  1617. Jim_GetString( newargs[7], NULL ),
  1618. Jim_GetString( newargs[8], NULL ),
  1619. Jim_GetString( newargs[9], NULL ) );
  1620. e = jim_jtag_command( interp, 10, newargs );
  1621. if( e != JIM_OK ){
  1622. command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
  1623. }
  1624. return e;
  1625. }
  1626. static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1627. {
  1628. jtag_tap_t *tap;
  1629. tap = jtag_all_taps;
  1630. command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
  1631. command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
  1632. while( tap ){
  1633. u32 expected, expected_mask, cur_instr, ii;
  1634. expected = buf_get_u32(tap->expected, 0, tap->ir_length);
  1635. expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
  1636. cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
  1637. command_print(cmd_ctx,
  1638. "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
  1639. tap->abs_chain_position,
  1640. tap->dotted_name,
  1641. tap->enabled ? 'Y' : 'n',
  1642. tap->idcode,
  1643. (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
  1644. tap->ir_length,
  1645. expected,
  1646. expected_mask,
  1647. cur_instr);
  1648. for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
  1649. command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
  1650. tap->expected_ids[ii]);
  1651. }
  1652. tap = tap->next_tap;
  1653. }
  1654. return ERROR_OK;
  1655. }
  1656. static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1657. {
  1658. int new_cfg = 0;
  1659. int mask = 0;
  1660. if (argc < 1)
  1661. return ERROR_COMMAND_SYNTAX_ERROR;
  1662. /* Original versions cared about the order of these tokens:
  1663. * reset_config signals [combination [trst_type [srst_type]]]
  1664. * They also clobbered the previous configuration even on error.
  1665. *
  1666. * Here we don't care about the order, and only change values
  1667. * which have been explicitly specified.
  1668. */
  1669. for (; argc; argc--, args++) {
  1670. int tmp = 0;
  1671. int m;
  1672. /* signals */
  1673. m = RESET_HAS_TRST | RESET_HAS_SRST;
  1674. if (strcmp(*args, "none") == 0)
  1675. tmp = RESET_NONE;
  1676. else if (strcmp(*args, "trst_only") == 0)
  1677. tmp = RESET_HAS_TRST;
  1678. else if (strcmp(*args, "srst_only") == 0)
  1679. tmp = RESET_HAS_SRST;
  1680. else if (strcmp(*args, "trst_and_srst") == 0)
  1681. tmp = RESET_HAS_TRST | RESET_HAS_SRST;
  1682. else
  1683. m = 0;
  1684. if (mask & m) {
  1685. LOG_ERROR("extra reset_config %s spec (%s)",
  1686. "signal", *args);
  1687. return ERROR_INVALID_ARGUMENTS;
  1688. }
  1689. if (m)
  1690. goto next;
  1691. /* combination (options for broken wiring) */
  1692. m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  1693. if (strcmp(*args, "separate") == 0)
  1694. /* separate reset lines - default */;
  1695. else if (strcmp(*args, "srst_pulls_trst") == 0)
  1696. tmp |= RESET_SRST_PULLS_TRST;
  1697. else if (strcmp(*args, "trst_pulls_srst") == 0)
  1698. tmp |= RESET_TRST_PULLS_SRST;
  1699. else if (strcmp(*args, "combined") == 0)
  1700. tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  1701. else
  1702. m = 0;
  1703. if (mask & m) {
  1704. LOG_ERROR("extra reset_config %s spec (%s)",
  1705. "combination", *args);
  1706. return ERROR_INVALID_ARGUMENTS;
  1707. }
  1708. if (m)
  1709. goto next;
  1710. /* trst_type (NOP without HAS_TRST) */
  1711. m = RESET_TRST_OPEN_DRAIN;
  1712. if (strcmp(*args, "trst_open_drain") == 0)
  1713. tmp |= RESET_TRST_OPEN_DRAIN;
  1714. else if (strcmp(*args, "trst_push_pull") == 0)
  1715. /* push/pull from adapter - default */;
  1716. else
  1717. m = 0;
  1718. if (mask & m) {
  1719. LOG_ERROR("extra reset_config %s spec (%s)",
  1720. "trst_type", *args);
  1721. return ERROR_INVALID_ARGUMENTS;
  1722. }
  1723. if (m)
  1724. goto next;
  1725. /* srst_type (NOP without HAS_SRST) */
  1726. m |= RESET_SRST_PUSH_PULL;
  1727. if (strcmp(*args, "srst_push_pull") == 0)
  1728. tmp |= RESET_SRST_PUSH_PULL;
  1729. else if (strcmp(*args, "srst_open_drain") == 0)
  1730. /* open drain from adapter - default */;
  1731. else
  1732. m = 0;
  1733. if (mask & m) {
  1734. LOG_ERROR("extra reset_config %s spec (%s)",
  1735. "srst_type", *args);
  1736. return ERROR_INVALID_ARGUMENTS;
  1737. }
  1738. if (m)
  1739. goto next;
  1740. /* caller provided nonsense; fail */
  1741. LOG_ERROR("unknown reset_config flag (%s)", *args);
  1742. return ERROR_INVALID_ARGUMENTS;
  1743. next:
  1744. /* Remember the bits which were specified (mask)
  1745. * and their new values (new_cfg).
  1746. */
  1747. mask |= m;
  1748. new_cfg |= tmp;
  1749. }
  1750. /* clear previous values of those bits, save new values */
  1751. jtag_reset_config &= ~mask;
  1752. jtag_reset_config |= new_cfg;
  1753. return ERROR_OK;
  1754. }
  1755. static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1756. {
  1757. if (argc < 1)
  1758. {
  1759. LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
  1760. exit(-1);
  1761. }
  1762. else
  1763. {
  1764. jtag_nsrst_delay = strtoul(args[0], NULL, 0);
  1765. }
  1766. return ERROR_OK;
  1767. }
  1768. static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1769. {
  1770. if (argc < 1)
  1771. {
  1772. LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
  1773. exit(-1);
  1774. }
  1775. else
  1776. {
  1777. jtag_ntrst_delay = strtoul(args[0], NULL, 0);
  1778. }
  1779. return ERROR_OK;
  1780. }
  1781. static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1782. {
  1783. int retval=ERROR_OK;
  1784. if (argc == 1)
  1785. {
  1786. LOG_DEBUG("handle jtag speed");
  1787. int cur_speed = 0;
  1788. cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
  1789. /* this command can be called during CONFIG,
  1790. * in which case jtag isn't initialized */
  1791. if (jtag)
  1792. {
  1793. retval=jtag->speed(cur_speed);
  1794. }
  1795. } else if (argc == 0)
  1796. {
  1797. } else
  1798. {
  1799. return ERROR_COMMAND_SYNTAX_ERROR;
  1800. }
  1801. command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
  1802. return retval;
  1803. }
  1804. static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1805. {
  1806. int retval=ERROR_OK;
  1807. LOG_DEBUG("handle jtag khz");
  1808. if(argc == 1)
  1809. {
  1810. speed_khz = strtoul(args[0], NULL, 0);
  1811. if (jtag != NULL)
  1812. {
  1813. int cur_speed = 0;
  1814. LOG_DEBUG("have interface set up");
  1815. int speed_div1;
  1816. if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
  1817. {
  1818. speed_khz = 0;
  1819. return retval;
  1820. }
  1821. cur_speed = jtag_speed = speed_div1;
  1822. retval=jtag->speed(cur_speed);
  1823. } else
  1824. {
  1825. hasKHz = 1;
  1826. }
  1827. } else if (argc==0)
  1828. {
  1829. } else
  1830. {
  1831. return ERROR_COMMAND_SYNTAX_ERROR;
  1832. }
  1833. if (jtag!=NULL)
  1834. {
  1835. if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
  1836. return retval;
  1837. }
  1838. if (speed_khz==0)
  1839. {
  1840. command_print(cmd_ctx, "RCLK - adaptive");
  1841. } else
  1842. {
  1843. command_print(cmd_ctx, "%d kHz", speed_khz);
  1844. }
  1845. return retval;
  1846. }
  1847. static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1848. {
  1849. tap_state_t state;
  1850. if (argc < 1)
  1851. {
  1852. return ERROR_COMMAND_SYNTAX_ERROR;
  1853. }
  1854. else
  1855. {
  1856. state = tap_state_by_name( args[0] );
  1857. if( state < 0 ){
  1858. command_print( cmd_ctx, "Invalid state name: %s\n", args[0] );
  1859. return ERROR_COMMAND_SYNTAX_ERROR;
  1860. }
  1861. jtag_set_end_state(state);
  1862. jtag_execute_queue();
  1863. }
  1864. command_print(cmd_ctx, "current endstate: %s", tap_state_name(cmd_queue_end_state));
  1865. return ERROR_OK;
  1866. }
  1867. static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1868. {
  1869. int trst = -1;
  1870. int srst = -1;
  1871. if (argc < 2)
  1872. {
  1873. return ERROR_COMMAND_SYNTAX_ERROR;
  1874. }
  1875. if (args[0][0] == '1')
  1876. trst = 1;
  1877. else if (args[0][0] == '0')
  1878. trst = 0;
  1879. else
  1880. {
  1881. return ERROR_COMMAND_SYNTAX_ERROR;
  1882. }
  1883. if (args[1][0] == '1')
  1884. srst = 1;
  1885. else if (args[1][0] == '0')
  1886. srst = 0;
  1887. else
  1888. {
  1889. return ERROR_COMMAND_SYNTAX_ERROR;
  1890. }
  1891. if (jtag_interface_init(cmd_ctx) != ERROR_OK)
  1892. return ERROR_JTAG_INIT_FAILED;
  1893. jtag_add_reset(trst, srst);
  1894. jtag_execute_queue();
  1895. return ERROR_OK;
  1896. }
  1897. static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1898. {
  1899. if (argc < 1)
  1900. {
  1901. return ERROR_COMMAND_SYNTAX_ERROR;
  1902. }
  1903. jtag_add_runtest(strtol(args[0], NULL, 0), jtag_get_end_state());
  1904. jtag_execute_queue();
  1905. return ERROR_OK;
  1906. }
  1907. /*
  1908. * For "irscan" or "drscan" commands, the "end" (really, "next") state
  1909. * should be stable ... and *NOT* a shift state, otherwise free-running
  1910. * jtag clocks could change the values latched by the update state.
  1911. */
  1912. static bool scan_is_safe(tap_state_t state)
  1913. {
  1914. switch (state)
  1915. {
  1916. case TAP_RESET:
  1917. case TAP_IDLE:
  1918. case TAP_DRPAUSE:
  1919. case TAP_IRPAUSE:
  1920. return true;
  1921. default:
  1922. return false;
  1923. }
  1924. }
  1925. static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1926. {
  1927. int i;
  1928. scan_field_t *fields;
  1929. jtag_tap_t *tap;
  1930. tap_state_t endstate;
  1931. if ((argc < 2) || (argc % 2))
  1932. {
  1933. return ERROR_COMMAND_SYNTAX_ERROR;
  1934. }
  1935. /* optional "-endstate" "statename" at the end of the arguments,
  1936. * so that e.g. IRPAUSE can let us load the data register before
  1937. * entering RUN/IDLE to execute the instruction we load here.
  1938. */
  1939. endstate = TAP_IDLE;
  1940. if( argc >= 4 ){
  1941. /* have at least one pair of numbers. */
  1942. /* is last pair the magic text? */
  1943. if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
  1944. const char *cpA;
  1945. const char *cpS;
  1946. cpA = args[ argc-1 ];
  1947. for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
  1948. cpS = tap_state_name( endstate );
  1949. if( 0 == strcmp( cpA, cpS ) ){
  1950. break;
  1951. }
  1952. }
  1953. if( endstate >= TAP_NUM_STATES ){
  1954. return ERROR_COMMAND_SYNTAX_ERROR;
  1955. } else {
  1956. if (!scan_is_safe(endstate))
  1957. LOG_WARNING("irscan with unsafe "
  1958. "endstate \"%s\"", cpA);
  1959. /* found - remove the last 2 args */
  1960. argc -= 2;
  1961. }
  1962. }
  1963. }
  1964. int num_fields = argc / 2;
  1965. fields = malloc(sizeof(scan_field_t) * num_fields);
  1966. for (i = 0; i < num_fields; i++)
  1967. {
  1968. tap = jtag_TapByString( args[i*2] );
  1969. if (tap==NULL)
  1970. {
  1971. command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
  1972. return ERROR_FAIL;
  1973. }
  1974. int field_size = tap->ir_length;
  1975. fields[i].tap = tap;
  1976. fields[i].num_bits = field_size;
  1977. fields[i].out_value = malloc(CEIL(field_size, 8));
  1978. buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
  1979. fields[i].in_value = NULL;
  1980. }
  1981. /* did we have an endstate? */
  1982. jtag_add_ir_scan(num_fields, fields, endstate);
  1983. int retval=jtag_execute_queue();
  1984. for (i = 0; i < num_fields; i++)
  1985. free(fields[i].out_value);
  1986. free (fields);
  1987. return retval;
  1988. }
  1989. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  1990. {
  1991. int retval;
  1992. scan_field_t *fields;
  1993. int num_fields;
  1994. int field_count = 0;
  1995. int i, e;
  1996. jtag_tap_t *tap;
  1997. tap_state_t endstate;
  1998. /* args[1] = device
  1999. * args[2] = num_bits
  2000. * args[3] = hex string
  2001. * ... repeat num bits and hex string ...
  2002. *
  2003. * .. optionally:
  2004. * args[N-2] = "-endstate"
  2005. * args[N-1] = statename
  2006. */
  2007. if ((argc < 4) || ((argc % 2)!=0))
  2008. {
  2009. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  2010. return JIM_ERR;
  2011. }
  2012. endstate = TAP_IDLE;
  2013. /* validate arguments as numbers */
  2014. e = JIM_OK;
  2015. for (i = 2; i < argc; i+=2)
  2016. {
  2017. long bits;
  2018. const char *cp;
  2019. e = Jim_GetLong(interp, args[i], &bits);
  2020. /* If valid - try next arg */
  2021. if( e == JIM_OK ){
  2022. continue;
  2023. }
  2024. /* Not valid.. are we at the end? */
  2025. if ( ((i+2) != argc) ){
  2026. /* nope, then error */
  2027. return e;
  2028. }
  2029. /* it could be: "-endstate FOO"
  2030. * e.g. DRPAUSE so we can issue more instructions
  2031. * before entering RUN/IDLE and executing them.
  2032. */
  2033. /* get arg as a string. */
  2034. cp = Jim_GetString( args[i], NULL );
  2035. /* is it the magic? */
  2036. if( 0 == strcmp( "-endstate", cp ) ){
  2037. /* is the statename valid? */
  2038. cp = Jim_GetString( args[i+1], NULL );
  2039. /* see if it is a valid state name */
  2040. endstate = tap_state_by_name(cp);
  2041. if( endstate < 0 ){
  2042. /* update the error message */
  2043. Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
  2044. } else {
  2045. if (!scan_is_safe(endstate))
  2046. LOG_WARNING("drscan with unsafe "
  2047. "endstate \"%s\"", cp);
  2048. /* valid - so clear the error */
  2049. e = JIM_OK;
  2050. /* and remove the last 2 args */
  2051. argc -= 2;
  2052. }
  2053. }
  2054. /* Still an error? */
  2055. if( e != JIM_OK ){
  2056. return e; /* too bad */
  2057. }
  2058. } /* validate args */
  2059. tap = jtag_TapByJimObj( interp, args[1] );
  2060. if( tap == NULL ){
  2061. return JIM_ERR;
  2062. }
  2063. num_fields=(argc-2)/2;
  2064. fields = malloc(sizeof(scan_field_t) * num_fields);
  2065. for (i = 2; i < argc; i+=2)
  2066. {
  2067. long bits;
  2068. int len;
  2069. const char *str;
  2070. Jim_GetLong(interp, args[i], &bits);
  2071. str = Jim_GetString(args[i+1], &len);
  2072. fields[field_count].tap = tap;
  2073. fields[field_count].num_bits = bits;
  2074. fields[field_count].out_value = malloc(CEIL(bits, 8));
  2075. str_to_buf(str, len, fields[field_count].out_value, bits, 0);
  2076. fields[field_count].in_value = fields[field_count].out_value;
  2077. field_count++;
  2078. }
  2079. jtag_add_dr_scan(num_fields, fields, endstate);
  2080. retval = jtag_execute_queue();
  2081. if (retval != ERROR_OK)
  2082. {
  2083. Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
  2084. return JIM_ERR;
  2085. }
  2086. field_count=0;
  2087. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  2088. for (i = 2; i < argc; i+=2)
  2089. {
  2090. long bits;
  2091. char *str;
  2092. Jim_GetLong(interp, args[i], &bits);
  2093. str = buf_to_str(fields[field_count].in_value, bits, 16);
  2094. free(fields[field_count].out_value);
  2095. Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
  2096. free(str);
  2097. field_count++;
  2098. }
  2099. Jim_SetResult(interp, list);
  2100. free(fields);
  2101. return JIM_OK;
  2102. }
  2103. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  2104. {
  2105. Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_flush_queue_count));
  2106. return JIM_OK;
  2107. }
  2108. static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2109. {
  2110. if (argc == 1)
  2111. {
  2112. if (strcmp(args[0], "enable") == 0)
  2113. {
  2114. jtag_verify_capture_ir = 1;
  2115. }
  2116. else if (strcmp(args[0], "disable") == 0)
  2117. {
  2118. jtag_verify_capture_ir = 0;
  2119. } else
  2120. {
  2121. return ERROR_COMMAND_SYNTAX_ERROR;
  2122. }
  2123. } else if (argc != 0)
  2124. {
  2125. return ERROR_COMMAND_SYNTAX_ERROR;
  2126. }
  2127. command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
  2128. return ERROR_OK;
  2129. }
  2130. static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2131. {
  2132. if (argc == 1)
  2133. {
  2134. if (strcmp(args[0], "enable") == 0)
  2135. {
  2136. jtag_verify = 1;
  2137. }
  2138. else if (strcmp(args[0], "disable") == 0)
  2139. {
  2140. jtag_verify = 0;
  2141. } else
  2142. {
  2143. return ERROR_COMMAND_SYNTAX_ERROR;
  2144. }
  2145. } else if (argc != 0)
  2146. {
  2147. return ERROR_COMMAND_SYNTAX_ERROR;
  2148. }
  2149. command_print(cmd_ctx, "verify jtag capture is %s", (jtag_verify) ? "enabled": "disabled");
  2150. return ERROR_OK;
  2151. }
  2152. int jtag_power_dropout(int *dropout)
  2153. {
  2154. return jtag->power_dropout(dropout);
  2155. }
  2156. int jtag_srst_asserted(int *srst_asserted)
  2157. {
  2158. return jtag->srst_asserted(srst_asserted);
  2159. }
  2160. void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
  2161. {
  2162. jtag_tap_event_action_t * jteap;
  2163. int done;
  2164. jteap = tap->event_action;
  2165. done = 0;
  2166. while (jteap) {
  2167. if (jteap->event == e) {
  2168. done = 1;
  2169. LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
  2170. tap->dotted_name,
  2171. e,
  2172. Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
  2173. Jim_GetString(jteap->body, NULL) );
  2174. if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
  2175. Jim_PrintErrorMessage(interp);
  2176. }
  2177. }
  2178. jteap = jteap->next;
  2179. }
  2180. if (!done) {
  2181. LOG_DEBUG( "event %d %s - no action",
  2182. e,
  2183. Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
  2184. }
  2185. }
  2186. static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2187. {
  2188. if (argc > 1)
  2189. return ERROR_COMMAND_SYNTAX_ERROR;
  2190. if (argc == 1)
  2191. {
  2192. bool use_new_table;
  2193. if (strcmp(args[0], "short") == 0)
  2194. use_new_table = true;
  2195. else if (strcmp(args[0], "long") == 0)
  2196. use_new_table = false;
  2197. else
  2198. return ERROR_COMMAND_SYNTAX_ERROR;
  2199. tap_use_new_tms_table(use_new_table);
  2200. }
  2201. command_print(cmd_ctx, "tms sequence is %s",
  2202. tap_uses_new_tms_table() ? "short": "long");
  2203. return ERROR_OK;
  2204. }
  2205. /**
  2206. * Function jtag_add_statemove
  2207. * moves from the current state to the goal \a state. This needs
  2208. * to be handled according to the xsvf spec, see the XSTATE command
  2209. * description.
  2210. */
  2211. int jtag_add_statemove(tap_state_t goal_state)
  2212. {
  2213. int retval = ERROR_OK;
  2214. tap_state_t moves[8];
  2215. tap_state_t cur_state = cmd_queue_cur_state;
  2216. int i;
  2217. int tms_bits;
  2218. int tms_count;
  2219. LOG_DEBUG( "cur_state=%s goal_state=%s",
  2220. tap_state_name(cur_state),
  2221. tap_state_name(goal_state) );
  2222. /* From the XSVF spec, pertaining to XSTATE:
  2223. For special states known as stable states (Test-Logic-Reset,
  2224. Run-Test/Idle, Pause-DR, Pause- IR), an XSVF interpreter follows
  2225. predefined TAP state paths when the starting state is a stable state and
  2226. when the XSTATE specifies a new stable state (see the STATE command in
  2227. the [Ref 5] for the TAP state paths between stable states). For
  2228. non-stable states, XSTATE should specify a state that is only one TAP
  2229. state transition distance from the current TAP state to avoid undefined
  2230. TAP state paths. A sequence of multiple XSTATE commands can be issued to
  2231. transition the TAP through a specific state path.
  2232. */
  2233. if (goal_state==cur_state )
  2234. ; /* nothing to do */
  2235. else if( goal_state==TAP_RESET )
  2236. {
  2237. jtag_add_tlr();
  2238. }
  2239. else if( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
  2240. {
  2241. /* note: unless tms_bits holds a path that agrees with [Ref 5] in above
  2242. spec, then this code is not fully conformant to the xsvf spec. This
  2243. puts a burden on tap_get_tms_path() function from the xsvf spec.
  2244. If in doubt, you should confirm that that burden is being met.
  2245. */
  2246. tms_bits = tap_get_tms_path(cur_state, goal_state);
  2247. tms_count = tap_get_tms_path_len(cur_state, goal_state);
  2248. assert( (unsigned) tms_count < DIM(moves) );
  2249. for (i=0; i<tms_count; i++, tms_bits>>=1)
  2250. {
  2251. bool bit = tms_bits & 1;
  2252. cur_state = tap_state_transition(cur_state, bit);
  2253. moves[i] = cur_state;
  2254. }
  2255. jtag_add_pathmove(tms_count, moves);
  2256. }
  2257. /* else state must be immediately reachable in one clock cycle, and does not
  2258. need to be a stable state.
  2259. */
  2260. else if( tap_state_transition(cur_state, true) == goal_state
  2261. || tap_state_transition(cur_state, false) == goal_state )
  2262. {
  2263. /* move a single state */
  2264. moves[0] = goal_state;
  2265. jtag_add_pathmove( 1, moves );
  2266. }
  2267. else
  2268. {
  2269. retval = ERROR_FAIL;
  2270. }
  2271. return retval;
  2272. }