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.
 
 
 
 
 
 

3811 lines
94 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. #define INCLUDE_JTAG_MINIDRIVER_H
  31. #define INCLUDE_JTAG_INTERFACE_H
  32. #include "jtag.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. typedef struct cmd_queue_page_s
  44. {
  45. void *address;
  46. size_t used;
  47. struct cmd_queue_page_s *next;
  48. } cmd_queue_page_t;
  49. #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
  50. static cmd_queue_page_t *cmd_queue_pages = NULL;
  51. char* jtag_event_strings[] =
  52. {
  53. "JTAG controller reset (RESET or TRST)"
  54. };
  55. const Jim_Nvp nvp_jtag_tap_event[] = {
  56. { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
  57. { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
  58. { .name = NULL, .value = -1 }
  59. };
  60. int jtag_trst = 0;
  61. int jtag_srst = 0;
  62. #ifndef HAVE_JTAG_MINIDRIVER_H
  63. struct jtag_callback_entry
  64. {
  65. struct jtag_callback_entry *next;
  66. jtag_callback_t callback;
  67. u8 *in;
  68. jtag_callback_data_t data1;
  69. jtag_callback_data_t data2;
  70. jtag_callback_data_t data3;
  71. };
  72. static struct jtag_callback_entry *jtag_callback_queue_head = NULL;
  73. static struct jtag_callback_entry *jtag_callback_queue_tail = NULL;
  74. #endif
  75. jtag_command_t *jtag_command_queue = NULL;
  76. jtag_command_t **last_command_pointer = &jtag_command_queue;
  77. static jtag_tap_t *jtag_all_taps = NULL;
  78. enum reset_types jtag_reset_config = RESET_NONE;
  79. tap_state_t cmd_queue_end_state = TAP_RESET;
  80. tap_state_t cmd_queue_cur_state = TAP_RESET;
  81. int jtag_verify_capture_ir = 1;
  82. int jtag_verify = 1;
  83. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
  84. static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
  85. static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
  86. /* maximum number of JTAG devices expected in the chain
  87. */
  88. #define JTAG_MAX_CHAIN_SIZE 20
  89. /* callbacks to inform high-level handlers about JTAG state changes */
  90. jtag_event_callback_t *jtag_event_callbacks;
  91. /* speed in kHz*/
  92. static int speed_khz = 0;
  93. /* flag if the kHz speed was defined */
  94. static int hasKHz = 0;
  95. /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
  96. */
  97. #if BUILD_ECOSBOARD == 1
  98. extern jtag_interface_t zy1000_interface;
  99. #endif
  100. #if BUILD_PARPORT == 1
  101. extern jtag_interface_t parport_interface;
  102. #endif
  103. #if BUILD_DUMMY == 1
  104. extern jtag_interface_t dummy_interface;
  105. #endif
  106. #if BUILD_FT2232_FTD2XX == 1
  107. extern jtag_interface_t ft2232_interface;
  108. #endif
  109. #if BUILD_FT2232_LIBFTDI == 1
  110. extern jtag_interface_t ft2232_interface;
  111. #endif
  112. #if BUILD_AMTJTAGACCEL == 1
  113. extern jtag_interface_t amt_jtagaccel_interface;
  114. #endif
  115. #if BUILD_EP93XX == 1
  116. extern jtag_interface_t ep93xx_interface;
  117. #endif
  118. #if BUILD_AT91RM9200 == 1
  119. extern jtag_interface_t at91rm9200_interface;
  120. #endif
  121. #if BUILD_GW16012 == 1
  122. extern jtag_interface_t gw16012_interface;
  123. #endif
  124. #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
  125. extern jtag_interface_t presto_interface;
  126. #endif
  127. #if BUILD_USBPROG == 1
  128. extern jtag_interface_t usbprog_interface;
  129. #endif
  130. #if BUILD_JLINK == 1
  131. extern jtag_interface_t jlink_interface;
  132. #endif
  133. #if BUILD_VSLLINK == 1
  134. extern jtag_interface_t vsllink_interface;
  135. #endif
  136. #if BUILD_RLINK == 1
  137. extern jtag_interface_t rlink_interface;
  138. #endif
  139. #if BUILD_ARMJTAGEW == 1
  140. extern jtag_interface_t armjtagew_interface;
  141. #endif
  142. jtag_interface_t *jtag_interfaces[] = {
  143. #if BUILD_ECOSBOARD == 1
  144. &zy1000_interface,
  145. #endif
  146. #if BUILD_PARPORT == 1
  147. &parport_interface,
  148. #endif
  149. #if BUILD_DUMMY == 1
  150. &dummy_interface,
  151. #endif
  152. #if BUILD_FT2232_FTD2XX == 1
  153. &ft2232_interface,
  154. #endif
  155. #if BUILD_FT2232_LIBFTDI == 1
  156. &ft2232_interface,
  157. #endif
  158. #if BUILD_AMTJTAGACCEL == 1
  159. &amt_jtagaccel_interface,
  160. #endif
  161. #if BUILD_EP93XX == 1
  162. &ep93xx_interface,
  163. #endif
  164. #if BUILD_AT91RM9200 == 1
  165. &at91rm9200_interface,
  166. #endif
  167. #if BUILD_GW16012 == 1
  168. &gw16012_interface,
  169. #endif
  170. #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
  171. &presto_interface,
  172. #endif
  173. #if BUILD_USBPROG == 1
  174. &usbprog_interface,
  175. #endif
  176. #if BUILD_JLINK == 1
  177. &jlink_interface,
  178. #endif
  179. #if BUILD_VSLLINK == 1
  180. &vsllink_interface,
  181. #endif
  182. #if BUILD_RLINK == 1
  183. &rlink_interface,
  184. #endif
  185. #if BUILD_ARMJTAGEW == 1
  186. &armjtagew_interface,
  187. #endif
  188. NULL,
  189. };
  190. static jtag_interface_t *jtag = NULL;
  191. /* configuration */
  192. static jtag_interface_t *jtag_interface = NULL;
  193. int jtag_speed = 0;
  194. /* forward declarations */
  195. //void jtag_add_pathmove(int num_states, tap_state_t *path);
  196. //void jtag_add_runtest(int num_cycles, tap_state_t endstate);
  197. //void jtag_add_end_state(tap_state_t endstate);
  198. //void jtag_add_sleep(u32 us);
  199. //int jtag_execute_queue(void);
  200. static tap_state_t tap_state_by_name(const char *name);
  201. /* jtag commands */
  202. static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  203. static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  204. static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  205. static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  206. static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  207. static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  208. static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  209. static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  210. static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  211. static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  212. static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  213. static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  214. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  215. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
  216. static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  217. static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  218. static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  219. jtag_tap_t *jtag_AllTaps(void)
  220. {
  221. return jtag_all_taps;
  222. };
  223. int jtag_NumTotalTaps(void)
  224. {
  225. jtag_tap_t *t;
  226. int n;
  227. n = 0;
  228. t = jtag_AllTaps();
  229. while(t){
  230. n++;
  231. t = t->next_tap;
  232. }
  233. return n;
  234. }
  235. int jtag_NumEnabledTaps(void)
  236. {
  237. jtag_tap_t *t;
  238. int n;
  239. n = 0;
  240. t = jtag_AllTaps();
  241. while(t){
  242. if( t->enabled ){
  243. n++;
  244. }
  245. t = t->next_tap;
  246. }
  247. return n;
  248. }
  249. jtag_tap_t *jtag_TapByString( const char *s )
  250. {
  251. jtag_tap_t *t;
  252. char *cp;
  253. t = jtag_AllTaps();
  254. /* try name first */
  255. while(t){
  256. if( 0 == strcmp( t->dotted_name, s ) ){
  257. break;
  258. } else {
  259. t = t->next_tap;
  260. }
  261. }
  262. /* backup plan is by number */
  263. if( t == NULL ){
  264. /* ok - is "s" a number? */
  265. int n;
  266. n = strtol( s, &cp, 0 );
  267. if( (s != cp) && (*cp == 0) ){
  268. /* Then it is... */
  269. t = jtag_TapByAbsPosition(n);
  270. }
  271. }
  272. return t;
  273. }
  274. jtag_tap_t * jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
  275. {
  276. jtag_tap_t *t;
  277. const char *cp;
  278. cp = Jim_GetString( o, NULL );
  279. if(cp == NULL){
  280. cp = "(unknown)";
  281. t = NULL;
  282. } else {
  283. t = jtag_TapByString( cp );
  284. }
  285. if( t == NULL ){
  286. Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
  287. }
  288. return t;
  289. }
  290. /* returns a pointer to the n-th device in the scan chain */
  291. jtag_tap_t * jtag_TapByAbsPosition( int n )
  292. {
  293. int orig_n;
  294. jtag_tap_t *t;
  295. orig_n = n;
  296. t = jtag_AllTaps();
  297. while( t && (n > 0)) {
  298. n--;
  299. t = t->next_tap;
  300. }
  301. return t;
  302. }
  303. int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
  304. {
  305. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  306. if (callback == NULL)
  307. {
  308. return ERROR_INVALID_ARGUMENTS;
  309. }
  310. if (*callbacks_p)
  311. {
  312. while ((*callbacks_p)->next)
  313. callbacks_p = &((*callbacks_p)->next);
  314. callbacks_p = &((*callbacks_p)->next);
  315. }
  316. (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
  317. (*callbacks_p)->callback = callback;
  318. (*callbacks_p)->priv = priv;
  319. (*callbacks_p)->next = NULL;
  320. return ERROR_OK;
  321. }
  322. int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
  323. {
  324. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  325. if (callback == NULL)
  326. {
  327. return ERROR_INVALID_ARGUMENTS;
  328. }
  329. while (*callbacks_p)
  330. {
  331. jtag_event_callback_t **next = &((*callbacks_p)->next);
  332. if ((*callbacks_p)->callback == callback)
  333. {
  334. free(*callbacks_p);
  335. *callbacks_p = *next;
  336. }
  337. callbacks_p = next;
  338. }
  339. return ERROR_OK;
  340. }
  341. int jtag_call_event_callbacks(enum jtag_event event)
  342. {
  343. jtag_event_callback_t *callback = jtag_event_callbacks;
  344. LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
  345. while (callback)
  346. {
  347. callback->callback(event, callback->priv);
  348. callback = callback->next;
  349. }
  350. return ERROR_OK;
  351. }
  352. /* returns a pointer to the pointer of the last command in queue
  353. * this may be a pointer to the root pointer (jtag_command_queue)
  354. * or to the next member of the last but one command
  355. */
  356. jtag_command_t** jtag_get_last_command_p(void)
  357. {
  358. /* jtag_command_t *cmd = jtag_command_queue;
  359. if (cmd)
  360. while (cmd->next)
  361. cmd = cmd->next;
  362. else
  363. return &jtag_command_queue;
  364. return &cmd->next;*/
  365. return last_command_pointer;
  366. }
  367. void jtag_queue_command(jtag_command_t * cmd)
  368. {
  369. jtag_command_t **last_cmd;
  370. last_cmd = jtag_get_last_command_p();
  371. *last_cmd = cmd;
  372. (*last_cmd)->next = NULL;
  373. last_command_pointer = &((*last_cmd)->next);
  374. }
  375. void* cmd_queue_alloc(size_t size)
  376. {
  377. cmd_queue_page_t **p_page = &cmd_queue_pages;
  378. int offset;
  379. u8 *t;
  380. /*
  381. * WARNING:
  382. * We align/round the *SIZE* per below
  383. * so that all pointers returned by
  384. * this function are reasonably well
  385. * aligned.
  386. *
  387. * If we did not, then an "odd-length" request would cause the
  388. * *next* allocation to be at an *odd* address, and because
  389. * this function has the same type of api as malloc() - we
  390. * must also return pointers that have the same type of
  391. * alignment.
  392. *
  393. * What I do not/have is a reasonable portable means
  394. * to align by...
  395. *
  396. * The solution here, is based on these suggestions.
  397. * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
  398. *
  399. */
  400. union worse_case_align {
  401. int i;
  402. long l;
  403. float f;
  404. void *v;
  405. };
  406. #define ALIGN_SIZE (sizeof(union worse_case_align))
  407. /* The alignment process. */
  408. size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
  409. /* Done... */
  410. if (*p_page)
  411. {
  412. while ((*p_page)->next)
  413. p_page = &((*p_page)->next);
  414. if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
  415. p_page = &((*p_page)->next);
  416. }
  417. if (!*p_page)
  418. {
  419. *p_page = malloc(sizeof(cmd_queue_page_t));
  420. (*p_page)->used = 0;
  421. (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
  422. (*p_page)->next = NULL;
  423. }
  424. offset = (*p_page)->used;
  425. (*p_page)->used += size;
  426. t=(u8 *)((*p_page)->address);
  427. return t + offset;
  428. }
  429. void cmd_queue_free(void)
  430. {
  431. cmd_queue_page_t *page = cmd_queue_pages;
  432. while (page)
  433. {
  434. cmd_queue_page_t *last = page;
  435. free(page->address);
  436. page = page->next;
  437. free(last);
  438. }
  439. cmd_queue_pages = NULL;
  440. }
  441. /**
  442. * Copy a scan_field_t for insertion into the queue.
  443. *
  444. * This allocates a new copy of out_value using cmd_queue_alloc.
  445. */
  446. static void cmd_queue_scan_field_clone(scan_field_t * dst, const scan_field_t * src)
  447. {
  448. dst->tap = src->tap;
  449. dst->num_bits = src->num_bits;
  450. dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(CEIL(src->num_bits, 8)), src->num_bits);
  451. dst->in_value = src->in_value;
  452. }
  453. static void jtag_prelude1(void)
  454. {
  455. if (jtag_trst == 1)
  456. {
  457. LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  458. jtag_error=ERROR_JTAG_TRST_ASSERTED;
  459. return;
  460. }
  461. if (cmd_queue_end_state == TAP_RESET)
  462. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  463. }
  464. static void jtag_prelude(tap_state_t state)
  465. {
  466. jtag_prelude1();
  467. if (state != TAP_INVALID)
  468. jtag_add_end_state(state);
  469. cmd_queue_cur_state = cmd_queue_end_state;
  470. }
  471. void jtag_add_ir_scan_noverify(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  472. {
  473. int retval;
  474. jtag_prelude(state);
  475. retval=interface_jtag_add_ir_scan(in_num_fields, in_fields, cmd_queue_end_state);
  476. if (retval!=ERROR_OK)
  477. jtag_error=retval;
  478. }
  479. /**
  480. * Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
  481. *
  482. * If the input field list contains an instruction value for a TAP then that is used
  483. * otherwise the TAP is set to bypass.
  484. *
  485. * TAPs for which no fields are passed are marked as bypassed for subsequent DR SCANs.
  486. *
  487. */
  488. void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  489. {
  490. if (jtag_verify&&jtag_verify_capture_ir)
  491. {
  492. /* 8 x 32 bit id's is enough for all invoations */
  493. for (int j = 0; j < in_num_fields; j++)
  494. {
  495. in_fields[j].check_value=NULL;
  496. in_fields[j].check_mask=NULL;
  497. /* if we are to run a verification of the ir scan, we need to get the input back.
  498. * We may have to allocate space if the caller didn't ask for the input back.
  499. */
  500. in_fields[j].check_value=in_fields[j].tap->expected;
  501. in_fields[j].check_mask=in_fields[j].tap->expected_mask;
  502. }
  503. jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
  504. } else
  505. {
  506. jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
  507. }
  508. }
  509. /**
  510. * see jtag_add_ir_scan()
  511. *
  512. */
  513. int MINIDRIVER(interface_jtag_add_ir_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  514. {
  515. size_t num_taps = jtag_NumEnabledTaps();
  516. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  517. scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
  518. scan_field_t * out_fields = cmd_queue_alloc(num_taps * sizeof(scan_field_t));
  519. jtag_queue_command(cmd);
  520. cmd->type = JTAG_SCAN;
  521. cmd->cmd.scan = scan;
  522. scan->ir_scan = true;
  523. scan->num_fields = num_taps; /* one field per device */
  524. scan->fields = out_fields;
  525. scan->end_state = state;
  526. scan_field_t * field = out_fields; /* keep track where we insert data */
  527. /* loop over all enabled TAPs */
  528. for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
  529. {
  530. /* search the input field list for fields for the current TAP */
  531. bool found = false;
  532. for (int j = 0; j < in_num_fields; j++)
  533. {
  534. if (tap != in_fields[j].tap)
  535. continue;
  536. /* if TAP is listed in input fields, copy the value */
  537. found = true;
  538. tap->bypass = 0;
  539. assert(in_fields[j].num_bits == tap->ir_length); /* input fields must have the same length as the TAP's IR */
  540. cmd_queue_scan_field_clone(field, in_fields + j);
  541. break;
  542. }
  543. if (!found)
  544. {
  545. /* if a TAP isn't listed in input fields, set it to BYPASS */
  546. tap->bypass = 1;
  547. field->tap = tap;
  548. field->num_bits = tap->ir_length;
  549. field->out_value = buf_set_ones(cmd_queue_alloc(CEIL(tap->ir_length, 8)), tap->ir_length);
  550. field->in_value = NULL; /* do not collect input for tap's in bypass */
  551. }
  552. /* update device information */
  553. buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
  554. field++;
  555. }
  556. assert(field == out_fields + num_taps); /* paranoia: jtag_NumEnabledTaps() and jtag_NextEnabledTap() not in sync */
  557. return ERROR_OK;
  558. }
  559. /**
  560. * Duplicate the scan fields passed into the function into an IR SCAN command
  561. *
  562. * This function assumes that the caller handles extra fields for bypassed TAPs
  563. *
  564. */
  565. void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  566. {
  567. int retval;
  568. jtag_prelude(state);
  569. retval=interface_jtag_add_plain_ir_scan(in_num_fields, in_fields, cmd_queue_end_state);
  570. if (retval!=ERROR_OK)
  571. jtag_error=retval;
  572. }
  573. /**
  574. * see jtag_add_plain_ir_scan()
  575. *
  576. */
  577. int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  578. {
  579. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  580. scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
  581. scan_field_t * out_fields = cmd_queue_alloc(in_num_fields * sizeof(scan_field_t));
  582. jtag_queue_command(cmd);
  583. cmd->type = JTAG_SCAN;
  584. cmd->cmd.scan = scan;
  585. scan->ir_scan = true;
  586. scan->num_fields = in_num_fields;
  587. scan->fields = out_fields;
  588. scan->end_state = state;
  589. for (int i = 0; i < in_num_fields; i++)
  590. cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
  591. return ERROR_OK;
  592. }
  593. int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
  594. static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  595. {
  596. return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
  597. }
  598. static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
  599. int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  600. {
  601. for (int i = 0; i < in_num_fields; i++)
  602. {
  603. in_fields[i].allocated = 0;
  604. in_fields[i].modified = 0;
  605. if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value == NULL))
  606. {
  607. in_fields[i].modified = 1;
  608. /* we need storage space... */
  609. #ifdef HAVE_JTAG_MINIDRIVER_H
  610. if (in_fields[i].num_bits <= 32)
  611. {
  612. /* This is enough space and we're executing this synchronously */
  613. in_fields[i].in_value = in_fields[i].intmp;
  614. } else
  615. {
  616. in_fields[i].in_value = (u8 *)malloc(CEIL(in_fields[i].num_bits, 8));
  617. in_fields[i].allocated = 1;
  618. }
  619. #else
  620. in_fields[i].in_value = (u8 *)cmd_queue_alloc(CEIL(in_fields[i].num_bits, 8));
  621. #endif
  622. }
  623. }
  624. jtag_add_scan(in_num_fields, in_fields, state);
  625. for (int i = 0; i < in_num_fields; i++)
  626. {
  627. if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
  628. {
  629. /* this is synchronous for a minidriver */
  630. jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
  631. (jtag_callback_data_t)in_fields[i].check_value,
  632. (jtag_callback_data_t)in_fields[i].check_mask,
  633. (jtag_callback_data_t)in_fields[i].num_bits);
  634. }
  635. if (in_fields[i].allocated)
  636. {
  637. free(in_fields[i].in_value);
  638. }
  639. if (in_fields[i].modified)
  640. {
  641. in_fields[i].in_value = NULL;
  642. }
  643. }
  644. }
  645. void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
  646. {
  647. if (jtag_verify)
  648. {
  649. jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
  650. } else
  651. {
  652. jtag_add_dr_scan(in_num_fields, in_fields, state);
  653. }
  654. }
  655. /**
  656. * Generate a DR SCAN using the fields passed to the function
  657. *
  658. * For not bypassed TAPs the function checks in_fields and uses fields specified there.
  659. * For bypassed TAPs the function generates a dummy 1bit field.
  660. *
  661. * The bypass status of TAPs is set by jtag_add_ir_scan().
  662. *
  663. */
  664. void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  665. {
  666. int retval;
  667. jtag_prelude(state);
  668. retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, cmd_queue_end_state);
  669. if (retval!=ERROR_OK)
  670. jtag_error=retval;
  671. }
  672. /**
  673. * see jtag_add_dr_scan()
  674. *
  675. */
  676. int MINIDRIVER(interface_jtag_add_dr_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  677. {
  678. /* count devices in bypass */
  679. size_t bypass_devices = 0;
  680. for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
  681. {
  682. if (tap->bypass)
  683. bypass_devices++;
  684. }
  685. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  686. scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
  687. scan_field_t * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(scan_field_t));
  688. jtag_queue_command(cmd);
  689. cmd->type = JTAG_SCAN;
  690. cmd->cmd.scan = scan;
  691. scan->ir_scan = false;
  692. scan->num_fields = in_num_fields + bypass_devices;
  693. scan->fields = out_fields;
  694. scan->end_state = state;
  695. scan_field_t * field = out_fields; /* keep track where we insert data */
  696. /* loop over all enabled TAPs */
  697. for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
  698. {
  699. /* if TAP is not bypassed insert matching input fields */
  700. if (!tap->bypass)
  701. {
  702. scan_field_t * start_field = field; /* keep initial position for assert() */
  703. for (int j = 0; j < in_num_fields; j++)
  704. {
  705. if (tap != in_fields[j].tap)
  706. continue;
  707. cmd_queue_scan_field_clone(field, in_fields + j);
  708. field++;
  709. }
  710. assert(field > start_field); /* must have at least one input field per not bypassed TAP */
  711. }
  712. /* if a TAP is bypassed, generated a dummy bit*/
  713. else
  714. {
  715. field->tap = tap;
  716. field->num_bits = 1;
  717. field->out_value = NULL;
  718. field->in_value = NULL;
  719. field++;
  720. }
  721. }
  722. assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
  723. return ERROR_OK;
  724. }
  725. /**
  726. * Generate a DR SCAN using the array of output values passed to the function
  727. *
  728. * This function assumes that the parameter target_tap specifies the one TAP
  729. * that is not bypassed. All other TAPs must be bypassed and the function will
  730. * generate a dummy 1bit field for them.
  731. *
  732. * For the target_tap a sequence of output-only fields will be generated where
  733. * each field has the size num_bits and the field's values are taken from
  734. * the array value.
  735. *
  736. * The bypass status of TAPs is set by jtag_add_ir_scan().
  737. *
  738. */
  739. void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
  740. int in_num_fields,
  741. const int *num_bits,
  742. const u32 *value,
  743. tap_state_t end_state)
  744. {
  745. /* count devices in bypass */
  746. size_t bypass_devices = 0;
  747. for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
  748. {
  749. if (tap->bypass)
  750. bypass_devices++;
  751. }
  752. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  753. scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
  754. scan_field_t * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(scan_field_t));
  755. jtag_queue_command(cmd);
  756. cmd->type = JTAG_SCAN;
  757. cmd->cmd.scan = scan;
  758. scan->ir_scan = false;
  759. scan->num_fields = in_num_fields + bypass_devices;
  760. scan->fields = out_fields;
  761. scan->end_state = end_state;
  762. bool target_tap_match = false;
  763. scan_field_t * field = out_fields; /* keep track where we insert data */
  764. /* loop over all enabled TAPs */
  765. for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
  766. {
  767. /* if TAP is not bypassed insert matching input fields */
  768. if (!tap->bypass)
  769. {
  770. assert(tap == target_tap); /* target_tap must match the one not bypassed TAP */
  771. target_tap_match = true;
  772. for (int j = 0; j < in_num_fields; j++)
  773. {
  774. u8 out_value[4];
  775. size_t scan_size = num_bits[j];
  776. buf_set_u32(out_value, 0, scan_size, value[j]);
  777. field->tap = tap;
  778. field->num_bits = scan_size;
  779. field->out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  780. field->in_value = NULL;
  781. field++;
  782. }
  783. }
  784. /* if a TAP is bypassed, generated a dummy bit*/
  785. else
  786. {
  787. field->tap = tap;
  788. field->num_bits = 1;
  789. field->out_value = NULL;
  790. field->in_value = NULL;
  791. field++;
  792. }
  793. }
  794. assert(target_tap_match); /* target_tap should be enabled and not bypassed */
  795. }
  796. /**
  797. * Duplicate the scan fields passed into the function into a DR SCAN command
  798. *
  799. * This function assumes that the caller handles extra fields for bypassed TAPs
  800. *
  801. */
  802. void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  803. {
  804. int retval;
  805. jtag_prelude(state);
  806. retval=interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, cmd_queue_end_state);
  807. if (retval!=ERROR_OK)
  808. jtag_error=retval;
  809. }
  810. /**
  811. * see jtag_add_plain_dr_scan()
  812. *
  813. */
  814. int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
  815. {
  816. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  817. scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
  818. scan_field_t * out_fields = cmd_queue_alloc(in_num_fields * sizeof(scan_field_t));
  819. jtag_queue_command(cmd);
  820. cmd->type = JTAG_SCAN;
  821. cmd->cmd.scan = scan;
  822. scan->ir_scan = false;
  823. scan->num_fields = in_num_fields;
  824. scan->fields = out_fields;
  825. scan->end_state = state;
  826. for (int i = 0; i < in_num_fields; i++)
  827. cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
  828. return ERROR_OK;
  829. }
  830. void jtag_add_tlr(void)
  831. {
  832. jtag_prelude(TAP_RESET);
  833. int retval;
  834. retval=interface_jtag_add_tlr();
  835. if (retval!=ERROR_OK)
  836. jtag_error=retval;
  837. }
  838. int MINIDRIVER(interface_jtag_add_tlr)(void)
  839. {
  840. tap_state_t state = TAP_RESET;
  841. /* allocate memory for a new list member */
  842. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  843. jtag_queue_command(cmd);
  844. cmd->type = JTAG_STATEMOVE;
  845. cmd->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  846. cmd->cmd.statemove->end_state = state;
  847. return ERROR_OK;
  848. }
  849. void jtag_add_pathmove(int num_states, const tap_state_t *path)
  850. {
  851. tap_state_t cur_state = cmd_queue_cur_state;
  852. int i;
  853. int retval;
  854. /* the last state has to be a stable state */
  855. if (!tap_is_state_stable(path[num_states - 1]))
  856. {
  857. LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
  858. exit(-1);
  859. }
  860. for (i=0; i<num_states; i++)
  861. {
  862. if (path[i] == TAP_RESET)
  863. {
  864. LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
  865. exit(-1);
  866. }
  867. if ( tap_state_transition(cur_state, true) != path[i]
  868. && tap_state_transition(cur_state, false) != path[i])
  869. {
  870. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[i]));
  871. exit(-1);
  872. }
  873. cur_state = path[i];
  874. }
  875. jtag_prelude1();
  876. retval = interface_jtag_add_pathmove(num_states, path);
  877. cmd_queue_cur_state = path[num_states - 1];
  878. if (retval!=ERROR_OK)
  879. jtag_error=retval;
  880. }
  881. int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, const tap_state_t *path)
  882. {
  883. /* allocate memory for a new list member */
  884. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  885. jtag_queue_command(cmd);
  886. cmd->type = JTAG_PATHMOVE;
  887. cmd->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
  888. cmd->cmd.pathmove->num_states = num_states;
  889. cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
  890. for (int i = 0; i < num_states; i++)
  891. cmd->cmd.pathmove->path[i] = path[i];
  892. return ERROR_OK;
  893. }
  894. int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, tap_state_t state)
  895. {
  896. /* allocate memory for a new list member */
  897. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  898. jtag_queue_command(cmd);
  899. cmd->type = JTAG_RUNTEST;
  900. cmd->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
  901. cmd->cmd.runtest->num_cycles = num_cycles;
  902. cmd->cmd.runtest->end_state = state;
  903. return ERROR_OK;
  904. }
  905. void jtag_add_runtest(int num_cycles, tap_state_t state)
  906. {
  907. int retval;
  908. jtag_prelude(state);
  909. /* executed by sw or hw fifo */
  910. retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
  911. if (retval!=ERROR_OK)
  912. jtag_error=retval;
  913. }
  914. int MINIDRIVER(interface_jtag_add_clocks)( int num_cycles )
  915. {
  916. /* allocate memory for a new list member */
  917. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  918. jtag_queue_command(cmd);
  919. cmd->type = JTAG_STABLECLOCKS;
  920. cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(stableclocks_command_t));
  921. cmd->cmd.stableclocks->num_cycles = num_cycles;
  922. return ERROR_OK;
  923. }
  924. void jtag_add_clocks( int num_cycles )
  925. {
  926. int retval;
  927. if( !tap_is_state_stable(cmd_queue_cur_state) )
  928. {
  929. LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
  930. tap_state_name(cmd_queue_cur_state) );
  931. jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
  932. return;
  933. }
  934. if( num_cycles > 0 )
  935. {
  936. jtag_prelude1();
  937. retval = interface_jtag_add_clocks(num_cycles);
  938. if (retval != ERROR_OK)
  939. jtag_error=retval;
  940. }
  941. }
  942. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  943. {
  944. int trst_with_tlr = 0;
  945. int retval;
  946. /* FIX!!! there are *many* different cases here. A better
  947. * approach is needed for legal combinations of transitions...
  948. */
  949. if ((jtag_reset_config & RESET_HAS_SRST)&&
  950. (jtag_reset_config & RESET_HAS_TRST)&&
  951. ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
  952. {
  953. if (((req_tlr_or_trst&&!jtag_trst)||
  954. (!req_tlr_or_trst&&jtag_trst))&&
  955. ((req_srst&&!jtag_srst)||
  956. (!req_srst&&jtag_srst)))
  957. {
  958. /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
  959. //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
  960. }
  961. }
  962. /* Make sure that jtag_reset_config allows the requested reset */
  963. /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
  964. if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
  965. {
  966. LOG_ERROR("BUG: requested reset would assert trst");
  967. jtag_error=ERROR_FAIL;
  968. return;
  969. }
  970. /* if TRST pulls SRST, we reset with TAP T-L-R */
  971. if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
  972. {
  973. trst_with_tlr = 1;
  974. }
  975. if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
  976. {
  977. LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
  978. jtag_error=ERROR_FAIL;
  979. return;
  980. }
  981. if (req_tlr_or_trst)
  982. {
  983. if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
  984. {
  985. jtag_trst = 1;
  986. } else
  987. {
  988. trst_with_tlr = 1;
  989. }
  990. } else
  991. {
  992. jtag_trst = 0;
  993. }
  994. jtag_srst = req_srst;
  995. retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
  996. if (retval!=ERROR_OK)
  997. {
  998. jtag_error=retval;
  999. return;
  1000. }
  1001. jtag_execute_queue();
  1002. if (jtag_srst)
  1003. {
  1004. LOG_DEBUG("SRST line asserted");
  1005. }
  1006. else
  1007. {
  1008. LOG_DEBUG("SRST line released");
  1009. if (jtag_nsrst_delay)
  1010. jtag_add_sleep(jtag_nsrst_delay * 1000);
  1011. }
  1012. if (trst_with_tlr)
  1013. {
  1014. LOG_DEBUG("JTAG reset with RESET instead of TRST");
  1015. jtag_add_end_state(TAP_RESET);
  1016. jtag_add_tlr();
  1017. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  1018. return;
  1019. }
  1020. if (jtag_trst)
  1021. {
  1022. /* we just asserted nTRST, so we're now in Test-Logic-Reset,
  1023. * and inform possible listeners about this
  1024. */
  1025. LOG_DEBUG("TRST line asserted");
  1026. tap_set_state(TAP_RESET);
  1027. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  1028. }
  1029. else
  1030. {
  1031. if (jtag_ntrst_delay)
  1032. jtag_add_sleep(jtag_ntrst_delay * 1000);
  1033. }
  1034. }
  1035. int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
  1036. {
  1037. /* allocate memory for a new list member */
  1038. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  1039. jtag_queue_command(cmd);
  1040. cmd->type = JTAG_RESET;
  1041. cmd->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
  1042. cmd->cmd.reset->trst = req_trst;
  1043. cmd->cmd.reset->srst = req_srst;
  1044. return ERROR_OK;
  1045. }
  1046. void jtag_add_end_state(tap_state_t state)
  1047. {
  1048. cmd_queue_end_state = state;
  1049. if ((cmd_queue_end_state == TAP_DRSHIFT)||(cmd_queue_end_state == TAP_IRSHIFT))
  1050. {
  1051. LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
  1052. }
  1053. }
  1054. int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
  1055. {
  1056. /* allocate memory for a new list member */
  1057. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  1058. jtag_queue_command(cmd);
  1059. cmd->type = JTAG_SLEEP;
  1060. cmd->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
  1061. cmd->cmd.sleep->us = us;
  1062. return ERROR_OK;
  1063. }
  1064. void jtag_add_sleep(u32 us)
  1065. {
  1066. keep_alive(); /* we might be running on a very slow JTAG clk */
  1067. int retval=interface_jtag_add_sleep(us);
  1068. if (retval!=ERROR_OK)
  1069. jtag_error=retval;
  1070. return;
  1071. }
  1072. int jtag_scan_size(const scan_command_t *cmd)
  1073. {
  1074. int bit_count = 0;
  1075. int i;
  1076. /* count bits in scan command */
  1077. for (i = 0; i < cmd->num_fields; i++)
  1078. {
  1079. bit_count += cmd->fields[i].num_bits;
  1080. }
  1081. return bit_count;
  1082. }
  1083. int jtag_build_buffer(const scan_command_t *cmd, u8 **buffer)
  1084. {
  1085. int bit_count = 0;
  1086. int i;
  1087. bit_count = jtag_scan_size(cmd);
  1088. *buffer = calloc(1,CEIL(bit_count, 8));
  1089. bit_count = 0;
  1090. #ifdef _DEBUG_JTAG_IO_
  1091. LOG_DEBUG("%s num_fields: %i", cmd->ir_scan ? "IRSCAN" : "DRSCAN", cmd->num_fields);
  1092. #endif
  1093. for (i = 0; i < cmd->num_fields; i++)
  1094. {
  1095. if (cmd->fields[i].out_value)
  1096. {
  1097. #ifdef _DEBUG_JTAG_IO_
  1098. char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : cmd->fields[i].num_bits, 16);
  1099. #endif
  1100. buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
  1101. #ifdef _DEBUG_JTAG_IO_
  1102. LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i, cmd->fields[i].num_bits, char_buf);
  1103. free(char_buf);
  1104. #endif
  1105. }
  1106. else
  1107. {
  1108. #ifdef _DEBUG_JTAG_IO_
  1109. LOG_DEBUG("fields[%i].out_value[%i]: NULL", i, cmd->fields[i].num_bits);
  1110. #endif
  1111. }
  1112. bit_count += cmd->fields[i].num_bits;
  1113. }
  1114. #ifdef _DEBUG_JTAG_IO_
  1115. //LOG_DEBUG("bit_count totalling: %i", bit_count );
  1116. #endif
  1117. return bit_count;
  1118. }
  1119. int jtag_read_buffer(u8 *buffer, const scan_command_t *cmd)
  1120. {
  1121. int i;
  1122. int bit_count = 0;
  1123. int retval;
  1124. /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
  1125. retval = ERROR_OK;
  1126. for (i = 0; i < cmd->num_fields; i++)
  1127. {
  1128. /* if neither in_value nor in_handler
  1129. * are specified we don't have to examine this field
  1130. */
  1131. if (cmd->fields[i].in_value)
  1132. {
  1133. int num_bits = cmd->fields[i].num_bits;
  1134. u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
  1135. #ifdef _DEBUG_JTAG_IO_
  1136. char *char_buf = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  1137. LOG_DEBUG("fields[%i].in_value[%i]: 0x%s", i, num_bits, char_buf);
  1138. free(char_buf);
  1139. #endif
  1140. if (cmd->fields[i].in_value)
  1141. {
  1142. buf_cpy(captured, cmd->fields[i].in_value, num_bits);
  1143. }
  1144. free(captured);
  1145. }
  1146. bit_count += cmd->fields[i].num_bits;
  1147. }
  1148. return retval;
  1149. }
  1150. static const char *jtag_tap_name(const jtag_tap_t *tap)
  1151. {
  1152. return (tap == NULL) ? "(unknown)" : tap->dotted_name;
  1153. }
  1154. int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
  1155. {
  1156. int retval = ERROR_OK;
  1157. int compare_failed = 0;
  1158. if (in_check_mask)
  1159. compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
  1160. else
  1161. compare_failed = buf_cmp(captured, in_check_value, num_bits);
  1162. if (compare_failed){
  1163. /* An error handler could have caught the failing check
  1164. * only report a problem when there wasn't a handler, or if the handler
  1165. * acknowledged the error
  1166. */
  1167. /*
  1168. LOG_WARNING("TAP %s:",
  1169. jtag_tap_name(field->tap));
  1170. */
  1171. if (compare_failed)
  1172. {
  1173. char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  1174. char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  1175. if (in_check_mask)
  1176. {
  1177. char *in_check_mask_char;
  1178. in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
  1179. LOG_WARNING("value captured during scan didn't pass the requested check:");
  1180. LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
  1181. captured_char, in_check_value_char, in_check_mask_char);
  1182. free(in_check_mask_char);
  1183. }
  1184. else
  1185. {
  1186. 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);
  1187. }
  1188. free(captured_char);
  1189. free(in_check_value_char);
  1190. retval = ERROR_JTAG_QUEUE_FAILED;
  1191. }
  1192. }
  1193. return retval;
  1194. }
  1195. void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
  1196. {
  1197. assert(field->in_value != NULL);
  1198. if (value==NULL)
  1199. {
  1200. /* no checking to do */
  1201. return;
  1202. }
  1203. jtag_execute_queue_noclear();
  1204. int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
  1205. jtag_set_error(retval);
  1206. }
  1207. enum scan_type jtag_scan_type(const scan_command_t *cmd)
  1208. {
  1209. int i;
  1210. int type = 0;
  1211. for (i = 0; i < cmd->num_fields; i++)
  1212. {
  1213. if (cmd->fields[i].in_value)
  1214. type |= SCAN_IN;
  1215. if (cmd->fields[i].out_value)
  1216. type |= SCAN_OUT;
  1217. }
  1218. return type;
  1219. }
  1220. #ifndef HAVE_JTAG_MINIDRIVER_H
  1221. /* add callback to end of queue */
  1222. void jtag_add_callback4(jtag_callback_t callback, u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  1223. {
  1224. struct jtag_callback_entry *entry=cmd_queue_alloc(sizeof(struct jtag_callback_entry));
  1225. entry->next=NULL;
  1226. entry->callback=callback;
  1227. entry->in=in;
  1228. entry->data1=data1;
  1229. entry->data2=data2;
  1230. entry->data3=data3;
  1231. if (jtag_callback_queue_head==NULL)
  1232. {
  1233. jtag_callback_queue_head=entry;
  1234. jtag_callback_queue_tail=entry;
  1235. } else
  1236. {
  1237. jtag_callback_queue_tail->next=entry;
  1238. jtag_callback_queue_tail=entry;
  1239. }
  1240. }
  1241. static int jtag_convert_to_callback4(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  1242. {
  1243. ((jtag_callback1_t)data1)(in);
  1244. return ERROR_OK;
  1245. }
  1246. void jtag_add_callback(jtag_callback1_t callback, u8 *in)
  1247. {
  1248. jtag_add_callback4(jtag_convert_to_callback4, in, (jtag_callback_data_t)callback, 0, 0);
  1249. }
  1250. #endif
  1251. #ifndef HAVE_JTAG_MINIDRIVER_H
  1252. int interface_jtag_execute_queue(void)
  1253. {
  1254. int retval;
  1255. if (jtag==NULL)
  1256. {
  1257. LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
  1258. return ERROR_FAIL;
  1259. }
  1260. retval = jtag->execute_queue();
  1261. if (retval == ERROR_OK)
  1262. {
  1263. struct jtag_callback_entry *entry;
  1264. for (entry=jtag_callback_queue_head; entry!=NULL; entry=entry->next)
  1265. {
  1266. retval=entry->callback(entry->in, entry->data1, entry->data2, entry->data3);
  1267. if (retval!=ERROR_OK)
  1268. break;
  1269. }
  1270. }
  1271. cmd_queue_free();
  1272. jtag_callback_queue_head = NULL;
  1273. jtag_callback_queue_tail = NULL;
  1274. jtag_command_queue = NULL;
  1275. last_command_pointer = &jtag_command_queue;
  1276. return retval;
  1277. }
  1278. #endif
  1279. void jtag_execute_queue_noclear(void)
  1280. {
  1281. /* each flush can take as much as 1-2ms on high bandwidth low latency interfaces.
  1282. * E.g. a JTAG over TCP/IP or USB....
  1283. */
  1284. jtag_flush_queue_count++;
  1285. int retval=interface_jtag_execute_queue();
  1286. /* we keep the first error */
  1287. if ((jtag_error==ERROR_OK)&&(retval!=ERROR_OK))
  1288. {
  1289. jtag_error=retval;
  1290. }
  1291. }
  1292. int jtag_execute_queue(void)
  1293. {
  1294. int retval;
  1295. jtag_execute_queue_noclear();
  1296. retval=jtag_error;
  1297. jtag_error=ERROR_OK;
  1298. return retval;
  1299. }
  1300. int jtag_reset_callback(enum jtag_event event, void *priv)
  1301. {
  1302. jtag_tap_t *tap = priv;
  1303. LOG_DEBUG("-");
  1304. if (event == JTAG_TRST_ASSERTED)
  1305. {
  1306. buf_set_ones(tap->cur_instr, tap->ir_length);
  1307. tap->bypass = 1;
  1308. }
  1309. return ERROR_OK;
  1310. }
  1311. void jtag_sleep(u32 us)
  1312. {
  1313. alive_sleep(us/1000);
  1314. }
  1315. /* Try to examine chain layout according to IEEE 1149.1 §12
  1316. */
  1317. int jtag_examine_chain(void)
  1318. {
  1319. jtag_tap_t *tap;
  1320. scan_field_t field;
  1321. u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
  1322. int i;
  1323. int bit_count;
  1324. int device_count = 0;
  1325. u8 zero_check = 0x0;
  1326. u8 one_check = 0xff;
  1327. field.tap = NULL;
  1328. field.num_bits = sizeof(idcode_buffer) * 8;
  1329. field.out_value = idcode_buffer;
  1330. field.in_value = idcode_buffer;
  1331. for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
  1332. {
  1333. buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
  1334. }
  1335. jtag_add_plain_dr_scan(1, &field, TAP_RESET);
  1336. jtag_execute_queue();
  1337. for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
  1338. {
  1339. zero_check |= idcode_buffer[i];
  1340. one_check &= idcode_buffer[i];
  1341. }
  1342. /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
  1343. if ((zero_check == 0x00) || (one_check == 0xff))
  1344. {
  1345. LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
  1346. return ERROR_JTAG_INIT_FAILED;
  1347. }
  1348. /* point at the 1st tap */
  1349. tap = jtag_NextEnabledTap(NULL);
  1350. if( tap == NULL ){
  1351. LOG_ERROR("JTAG: No taps enabled?");
  1352. return ERROR_JTAG_INIT_FAILED;
  1353. }
  1354. for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
  1355. {
  1356. u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  1357. if ((idcode & 1) == 0)
  1358. {
  1359. /* LSB must not be 0, this indicates a device in bypass */
  1360. LOG_WARNING("Tap/Device does not have IDCODE");
  1361. idcode=0;
  1362. bit_count += 1;
  1363. }
  1364. else
  1365. {
  1366. u32 manufacturer;
  1367. u32 part;
  1368. u32 version;
  1369. /* some devices, such as AVR will output all 1's instead of TDI
  1370. input value at end of chain. */
  1371. if ((idcode == 0x000000FF)||(idcode == 0xFFFFFFFF))
  1372. {
  1373. int unexpected=0;
  1374. /* End of chain (invalid manufacturer ID)
  1375. *
  1376. * The JTAG examine is the very first thing that happens
  1377. *
  1378. * A single JTAG device requires only 64 bits to be read back correctly.
  1379. *
  1380. * The code below adds a check that the rest of the data scanned (640 bits)
  1381. * are all as expected. This helps diagnose/catch problems with the JTAG chain
  1382. *
  1383. * earlier and gives more helpful/explicit error messages.
  1384. */
  1385. for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
  1386. {
  1387. idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  1388. if (unexpected||((idcode != 0x000000FF)&&(idcode != 0xFFFFFFFF)))
  1389. {
  1390. LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
  1391. unexpected = 1;
  1392. }
  1393. }
  1394. break;
  1395. }
  1396. #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
  1397. manufacturer = EXTRACT_MFG(idcode);
  1398. #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
  1399. part = EXTRACT_PART(idcode);
  1400. #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
  1401. version = EXTRACT_VER(idcode);
  1402. LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
  1403. ((tap != NULL) ? (tap->dotted_name) : "(not-named)"),
  1404. idcode, manufacturer, part, version);
  1405. bit_count += 32;
  1406. }
  1407. if (tap)
  1408. {
  1409. tap->idcode = idcode;
  1410. if (tap->expected_ids_cnt > 0) {
  1411. /* Loop over the expected identification codes and test for a match */
  1412. u8 ii;
  1413. for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
  1414. if( tap->idcode == tap->expected_ids[ii] ){
  1415. break;
  1416. }
  1417. }
  1418. /* If none of the expected ids matched, log an error */
  1419. if (ii == tap->expected_ids_cnt) {
  1420. LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  1421. tap->dotted_name,
  1422. idcode,
  1423. EXTRACT_MFG( tap->idcode ),
  1424. EXTRACT_PART( tap->idcode ),
  1425. EXTRACT_VER( tap->idcode ) );
  1426. for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
  1427. LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
  1428. tap->dotted_name,
  1429. ii + 1,
  1430. tap->expected_ids_cnt,
  1431. tap->expected_ids[ii],
  1432. EXTRACT_MFG( tap->expected_ids[ii] ),
  1433. EXTRACT_PART( tap->expected_ids[ii] ),
  1434. EXTRACT_VER( tap->expected_ids[ii] ) );
  1435. }
  1436. return ERROR_JTAG_INIT_FAILED;
  1437. } else {
  1438. LOG_INFO("JTAG Tap/device matched");
  1439. }
  1440. } else {
  1441. #if 0
  1442. LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
  1443. tap->idcode);
  1444. #endif
  1445. }
  1446. tap = jtag_NextEnabledTap(tap);
  1447. }
  1448. device_count++;
  1449. }
  1450. /* see if number of discovered devices matches configuration */
  1451. if (device_count != jtag_NumEnabledTaps())
  1452. {
  1453. LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
  1454. device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
  1455. LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
  1456. return ERROR_JTAG_INIT_FAILED;
  1457. }
  1458. return ERROR_OK;
  1459. }
  1460. int jtag_validate_chain(void)
  1461. {
  1462. jtag_tap_t *tap;
  1463. int total_ir_length = 0;
  1464. u8 *ir_test = NULL;
  1465. scan_field_t field;
  1466. int chain_pos = 0;
  1467. tap = NULL;
  1468. total_ir_length = 0;
  1469. for(;;){
  1470. tap = jtag_NextEnabledTap(tap);
  1471. if( tap == NULL ){
  1472. break;
  1473. }
  1474. total_ir_length += tap->ir_length;
  1475. }
  1476. total_ir_length += 2;
  1477. ir_test = malloc(CEIL(total_ir_length, 8));
  1478. buf_set_ones(ir_test, total_ir_length);
  1479. field.tap = NULL;
  1480. field.num_bits = total_ir_length;
  1481. field.out_value = ir_test;
  1482. field.in_value = ir_test;
  1483. jtag_add_plain_ir_scan(1, &field, TAP_RESET);
  1484. jtag_execute_queue();
  1485. tap = NULL;
  1486. chain_pos = 0;
  1487. int val;
  1488. for(;;){
  1489. tap = jtag_NextEnabledTap(tap);
  1490. if( tap == NULL ){
  1491. break;
  1492. }
  1493. val = buf_get_u32(ir_test, chain_pos, 2);
  1494. if (val != 0x1)
  1495. {
  1496. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  1497. 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);
  1498. free(cbuf);
  1499. free(ir_test);
  1500. return ERROR_JTAG_INIT_FAILED;
  1501. }
  1502. chain_pos += tap->ir_length;
  1503. }
  1504. val = buf_get_u32(ir_test, chain_pos, 2);
  1505. if (val != 0x3)
  1506. {
  1507. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  1508. 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);
  1509. free(cbuf);
  1510. free(ir_test);
  1511. return ERROR_JTAG_INIT_FAILED;
  1512. }
  1513. free(ir_test);
  1514. return ERROR_OK;
  1515. }
  1516. enum jtag_tap_cfg_param {
  1517. JCFG_EVENT
  1518. };
  1519. static Jim_Nvp nvp_config_opts[] = {
  1520. { .name = "-event", .value = JCFG_EVENT },
  1521. { .name = NULL, .value = -1 }
  1522. };
  1523. static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
  1524. {
  1525. Jim_Nvp *n;
  1526. Jim_Obj *o;
  1527. int e;
  1528. /* parse config or cget options */
  1529. while (goi->argc > 0) {
  1530. Jim_SetEmptyResult (goi->interp);
  1531. e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
  1532. if (e != JIM_OK) {
  1533. Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
  1534. return e;
  1535. }
  1536. switch (n->value) {
  1537. case JCFG_EVENT:
  1538. if (goi->argc == 0) {
  1539. Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
  1540. return JIM_ERR;
  1541. }
  1542. e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
  1543. if (e != JIM_OK) {
  1544. Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
  1545. return e;
  1546. }
  1547. if (goi->isconfigure) {
  1548. if (goi->argc != 1) {
  1549. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
  1550. return JIM_ERR;
  1551. }
  1552. } else {
  1553. if (goi->argc != 0) {
  1554. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
  1555. return JIM_ERR;
  1556. }
  1557. }
  1558. {
  1559. jtag_tap_event_action_t *jteap;
  1560. jteap = tap->event_action;
  1561. /* replace existing? */
  1562. while (jteap) {
  1563. if (jteap->event == (enum jtag_tap_event)n->value) {
  1564. break;
  1565. }
  1566. jteap = jteap->next;
  1567. }
  1568. if (goi->isconfigure) {
  1569. if (jteap == NULL) {
  1570. /* create new */
  1571. jteap = calloc(1, sizeof (*jteap));
  1572. }
  1573. jteap->event = n->value;
  1574. Jim_GetOpt_Obj( goi, &o);
  1575. if (jteap->body) {
  1576. Jim_DecrRefCount(interp, jteap->body);
  1577. }
  1578. jteap->body = Jim_DuplicateObj(goi->interp, o);
  1579. Jim_IncrRefCount(jteap->body);
  1580. /* add to head of event list */
  1581. jteap->next = tap->event_action;
  1582. tap->event_action = jteap;
  1583. Jim_SetEmptyResult(goi->interp);
  1584. } else {
  1585. /* get */
  1586. if (jteap == NULL) {
  1587. Jim_SetEmptyResult(goi->interp);
  1588. } else {
  1589. Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
  1590. }
  1591. }
  1592. }
  1593. /* loop for more */
  1594. break;
  1595. }
  1596. } /* while (goi->argc) */
  1597. return JIM_OK;
  1598. }
  1599. static int jim_newtap_cmd( Jim_GetOptInfo *goi )
  1600. {
  1601. jtag_tap_t *pTap;
  1602. jtag_tap_t **ppTap;
  1603. jim_wide w;
  1604. int x;
  1605. int e;
  1606. int reqbits;
  1607. Jim_Nvp *n;
  1608. char *cp;
  1609. const Jim_Nvp opts[] = {
  1610. #define NTAP_OPT_IRLEN 0
  1611. { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
  1612. #define NTAP_OPT_IRMASK 1
  1613. { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
  1614. #define NTAP_OPT_IRCAPTURE 2
  1615. { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
  1616. #define NTAP_OPT_ENABLED 3
  1617. { .name = "-enable" , .value = NTAP_OPT_ENABLED },
  1618. #define NTAP_OPT_DISABLED 4
  1619. { .name = "-disable" , .value = NTAP_OPT_DISABLED },
  1620. #define NTAP_OPT_EXPECTED_ID 5
  1621. { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
  1622. { .name = NULL , .value = -1 },
  1623. };
  1624. pTap = malloc( sizeof(jtag_tap_t) );
  1625. memset( pTap, 0, sizeof(*pTap) );
  1626. if( !pTap ){
  1627. Jim_SetResult_sprintf( goi->interp, "no memory");
  1628. return JIM_ERR;
  1629. }
  1630. /*
  1631. * we expect CHIP + TAP + OPTIONS
  1632. * */
  1633. if( goi->argc < 3 ){
  1634. Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
  1635. return JIM_ERR;
  1636. }
  1637. Jim_GetOpt_String( goi, &cp, NULL );
  1638. pTap->chip = strdup(cp);
  1639. Jim_GetOpt_String( goi, &cp, NULL );
  1640. pTap->tapname = strdup(cp);
  1641. /* name + dot + name + null */
  1642. x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
  1643. cp = malloc( x );
  1644. sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
  1645. pTap->dotted_name = cp;
  1646. LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
  1647. pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
  1648. /* default is enabled */
  1649. pTap->enabled = 1;
  1650. /* deal with options */
  1651. #define NTREQ_IRLEN 1
  1652. #define NTREQ_IRCAPTURE 2
  1653. #define NTREQ_IRMASK 4
  1654. /* clear them as we find them */
  1655. reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
  1656. while( goi->argc ){
  1657. e = Jim_GetOpt_Nvp( goi, opts, &n );
  1658. if( e != JIM_OK ){
  1659. Jim_GetOpt_NvpUnknown( goi, opts, 0 );
  1660. return e;
  1661. }
  1662. LOG_DEBUG("Processing option: %s", n->name );
  1663. switch( n->value ){
  1664. case NTAP_OPT_ENABLED:
  1665. pTap->enabled = 1;
  1666. break;
  1667. case NTAP_OPT_DISABLED:
  1668. pTap->enabled = 0;
  1669. break;
  1670. case NTAP_OPT_EXPECTED_ID:
  1671. {
  1672. u32 *new_expected_ids;
  1673. e = Jim_GetOpt_Wide( goi, &w );
  1674. if( e != JIM_OK) {
  1675. Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
  1676. return e;
  1677. }
  1678. new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
  1679. if (new_expected_ids == NULL) {
  1680. Jim_SetResult_sprintf( goi->interp, "no memory");
  1681. return JIM_ERR;
  1682. }
  1683. memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
  1684. new_expected_ids[pTap->expected_ids_cnt] = w;
  1685. free(pTap->expected_ids);
  1686. pTap->expected_ids = new_expected_ids;
  1687. pTap->expected_ids_cnt++;
  1688. break;
  1689. }
  1690. case NTAP_OPT_IRLEN:
  1691. case NTAP_OPT_IRMASK:
  1692. case NTAP_OPT_IRCAPTURE:
  1693. e = Jim_GetOpt_Wide( goi, &w );
  1694. if( e != JIM_OK ){
  1695. Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
  1696. return e;
  1697. }
  1698. if( (w < 0) || (w > 0xffff) ){
  1699. /* wacky value */
  1700. Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
  1701. n->name, (int)(w), (int)(w));
  1702. return JIM_ERR;
  1703. }
  1704. switch(n->value){
  1705. case NTAP_OPT_IRLEN:
  1706. pTap->ir_length = w;
  1707. reqbits &= (~(NTREQ_IRLEN));
  1708. break;
  1709. case NTAP_OPT_IRMASK:
  1710. pTap->ir_capture_mask = w;
  1711. reqbits &= (~(NTREQ_IRMASK));
  1712. break;
  1713. case NTAP_OPT_IRCAPTURE:
  1714. pTap->ir_capture_value = w;
  1715. reqbits &= (~(NTREQ_IRCAPTURE));
  1716. break;
  1717. }
  1718. } /* switch(n->value) */
  1719. } /* while( goi->argc ) */
  1720. /* Did we get all the options? */
  1721. if( reqbits ){
  1722. // no
  1723. Jim_SetResult_sprintf( goi->interp,
  1724. "newtap: %s missing required parameters",
  1725. pTap->dotted_name);
  1726. /* TODO: Tell user what is missing :-( */
  1727. /* no memory leaks pelase */
  1728. free(((void *)(pTap->expected_ids)));
  1729. free(((void *)(pTap->chip)));
  1730. free(((void *)(pTap->tapname)));
  1731. free(((void *)(pTap->dotted_name)));
  1732. free(((void *)(pTap)));
  1733. return JIM_ERR;
  1734. }
  1735. pTap->expected = malloc( pTap->ir_length );
  1736. pTap->expected_mask = malloc( pTap->ir_length );
  1737. pTap->cur_instr = malloc( pTap->ir_length );
  1738. buf_set_u32( pTap->expected,
  1739. 0,
  1740. pTap->ir_length,
  1741. pTap->ir_capture_value );
  1742. buf_set_u32( pTap->expected_mask,
  1743. 0,
  1744. pTap->ir_length,
  1745. pTap->ir_capture_mask );
  1746. buf_set_ones( pTap->cur_instr,
  1747. pTap->ir_length );
  1748. pTap->bypass = 1;
  1749. jtag_register_event_callback(jtag_reset_callback, pTap );
  1750. ppTap = &(jtag_all_taps);
  1751. while( (*ppTap) != NULL ){
  1752. ppTap = &((*ppTap)->next_tap);
  1753. }
  1754. *ppTap = pTap;
  1755. {
  1756. static int n_taps = 0;
  1757. pTap->abs_chain_position = n_taps++;
  1758. }
  1759. LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
  1760. (*ppTap)->dotted_name,
  1761. (*ppTap)->abs_chain_position,
  1762. (*ppTap)->ir_length,
  1763. (*ppTap)->ir_capture_value,
  1764. (*ppTap)->ir_capture_mask );
  1765. return ERROR_OK;
  1766. }
  1767. static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
  1768. {
  1769. Jim_GetOptInfo goi;
  1770. int e;
  1771. Jim_Nvp *n;
  1772. Jim_Obj *o;
  1773. struct command_context_s *context;
  1774. enum {
  1775. JTAG_CMD_INTERFACE,
  1776. JTAG_CMD_INIT_RESET,
  1777. JTAG_CMD_NEWTAP,
  1778. JTAG_CMD_TAPENABLE,
  1779. JTAG_CMD_TAPDISABLE,
  1780. JTAG_CMD_TAPISENABLED,
  1781. JTAG_CMD_CONFIGURE,
  1782. JTAG_CMD_CGET
  1783. };
  1784. const Jim_Nvp jtag_cmds[] = {
  1785. { .name = "interface" , .value = JTAG_CMD_INTERFACE },
  1786. { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
  1787. { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
  1788. { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
  1789. { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
  1790. { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
  1791. { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
  1792. { .name = "cget" , .value = JTAG_CMD_CGET },
  1793. { .name = NULL, .value = -1 },
  1794. };
  1795. context = Jim_GetAssocData(interp, "context");
  1796. /* go past the command */
  1797. Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
  1798. e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
  1799. if( e != JIM_OK ){
  1800. Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
  1801. return e;
  1802. }
  1803. Jim_SetEmptyResult( goi.interp );
  1804. switch( n->value ){
  1805. case JTAG_CMD_INTERFACE:
  1806. /* return the name of the interface */
  1807. /* TCL code might need to know the exact type... */
  1808. /* FUTURE: we allow this as a means to "set" the interface. */
  1809. if( goi.argc != 0 ){
  1810. Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
  1811. return JIM_ERR;
  1812. }
  1813. Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
  1814. return JIM_OK;
  1815. case JTAG_CMD_INIT_RESET:
  1816. if( goi.argc != 0 ){
  1817. Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
  1818. return JIM_ERR;
  1819. }
  1820. e = jtag_init_reset(context);
  1821. if( e != ERROR_OK ){
  1822. Jim_SetResult_sprintf( goi.interp, "error: %d", e);
  1823. return JIM_ERR;
  1824. }
  1825. return JIM_OK;
  1826. case JTAG_CMD_NEWTAP:
  1827. return jim_newtap_cmd( &goi );
  1828. break;
  1829. case JTAG_CMD_TAPISENABLED:
  1830. case JTAG_CMD_TAPENABLE:
  1831. case JTAG_CMD_TAPDISABLE:
  1832. if( goi.argc != 1 ){
  1833. Jim_SetResultString( goi.interp, "Too many parameters",-1 );
  1834. return JIM_ERR;
  1835. }
  1836. {
  1837. jtag_tap_t *t;
  1838. t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
  1839. if( t == NULL ){
  1840. return JIM_ERR;
  1841. }
  1842. switch( n->value ){
  1843. case JTAG_CMD_TAPISENABLED:
  1844. e = t->enabled;
  1845. break;
  1846. case JTAG_CMD_TAPENABLE:
  1847. jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
  1848. e = 1;
  1849. t->enabled = e;
  1850. break;
  1851. case JTAG_CMD_TAPDISABLE:
  1852. jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
  1853. e = 0;
  1854. t->enabled = e;
  1855. break;
  1856. }
  1857. Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
  1858. return JIM_OK;
  1859. }
  1860. break;
  1861. case JTAG_CMD_CGET:
  1862. if( goi.argc < 2 ){
  1863. Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
  1864. return JIM_ERR;
  1865. }
  1866. {
  1867. jtag_tap_t *t;
  1868. Jim_GetOpt_Obj(&goi, &o);
  1869. t = jtag_TapByJimObj( goi.interp, o );
  1870. if( t == NULL ){
  1871. return JIM_ERR;
  1872. }
  1873. goi.isconfigure = 0;
  1874. return jtag_tap_configure_cmd( &goi, t);
  1875. }
  1876. break;
  1877. case JTAG_CMD_CONFIGURE:
  1878. if( goi.argc < 3 ){
  1879. Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
  1880. return JIM_ERR;
  1881. }
  1882. {
  1883. jtag_tap_t *t;
  1884. Jim_GetOpt_Obj(&goi, &o);
  1885. t = jtag_TapByJimObj( goi.interp, o );
  1886. if( t == NULL ){
  1887. return JIM_ERR;
  1888. }
  1889. goi.isconfigure = 1;
  1890. return jtag_tap_configure_cmd( &goi, t);
  1891. }
  1892. }
  1893. return JIM_ERR;
  1894. }
  1895. int jtag_register_commands(struct command_context_s *cmd_ctx)
  1896. {
  1897. register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
  1898. register_command(cmd_ctx, NULL, "interface", handle_interface_command,
  1899. COMMAND_CONFIG, "try to configure interface");
  1900. register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
  1901. COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
  1902. register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
  1903. COMMAND_ANY, "set maximum jtag speed (if supported); "
  1904. "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
  1905. register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
  1906. COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
  1907. register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
  1908. COMMAND_ANY,
  1909. "[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]");
  1910. register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
  1911. COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
  1912. register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
  1913. COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
  1914. register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
  1915. COMMAND_EXEC, "print current scan chain configuration");
  1916. register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
  1917. COMMAND_EXEC, "finish JTAG operations in <tap_state>");
  1918. register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
  1919. COMMAND_EXEC, "toggle reset lines <trst> <srst>");
  1920. register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
  1921. COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
  1922. register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
  1923. COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
  1924. register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
  1925. register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
  1926. register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
  1927. COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
  1928. register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
  1929. COMMAND_ANY, "verify value capture <enable|disable>");
  1930. register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
  1931. COMMAND_ANY, "choose short(default) or long tms_sequence <short|long>");
  1932. return ERROR_OK;
  1933. }
  1934. int jtag_interface_init(struct command_context_s *cmd_ctx)
  1935. {
  1936. if (jtag)
  1937. return ERROR_OK;
  1938. if (!jtag_interface)
  1939. {
  1940. /* nothing was previously specified by "interface" command */
  1941. LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
  1942. return ERROR_JTAG_INVALID_INTERFACE;
  1943. }
  1944. if(hasKHz)
  1945. {
  1946. jtag_interface->khz(speed_khz, &jtag_speed);
  1947. hasKHz = 0;
  1948. }
  1949. if (jtag_interface->init() != ERROR_OK)
  1950. return ERROR_JTAG_INIT_FAILED;
  1951. jtag = jtag_interface;
  1952. return ERROR_OK;
  1953. }
  1954. static int jtag_init_inner(struct command_context_s *cmd_ctx)
  1955. {
  1956. jtag_tap_t *tap;
  1957. int retval;
  1958. LOG_DEBUG("Init JTAG chain");
  1959. tap = jtag_NextEnabledTap(NULL);
  1960. if( tap == NULL ){
  1961. LOG_ERROR("There are no enabled taps?");
  1962. return ERROR_JTAG_INIT_FAILED;
  1963. }
  1964. jtag_add_tlr();
  1965. if ((retval=jtag_execute_queue())!=ERROR_OK)
  1966. return retval;
  1967. /* examine chain first, as this could discover the real chain layout */
  1968. if (jtag_examine_chain() != ERROR_OK)
  1969. {
  1970. LOG_ERROR("trying to validate configured JTAG chain anyway...");
  1971. }
  1972. if (jtag_validate_chain() != ERROR_OK)
  1973. {
  1974. LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
  1975. }
  1976. return ERROR_OK;
  1977. }
  1978. int jtag_interface_quit(void)
  1979. {
  1980. if (!jtag || !jtag->quit)
  1981. return ERROR_OK;
  1982. // close the JTAG interface
  1983. int result = jtag->quit();
  1984. if (ERROR_OK != result)
  1985. LOG_ERROR("failed: %d", result);
  1986. return ERROR_OK;
  1987. }
  1988. int jtag_init_reset(struct command_context_s *cmd_ctx)
  1989. {
  1990. int retval;
  1991. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1992. return retval;
  1993. LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
  1994. /* Reset can happen after a power cycle.
  1995. *
  1996. * Ideally we would only assert TRST or run RESET before the target reset.
  1997. *
  1998. * However w/srst_pulls_trst, trst is asserted together with the target
  1999. * reset whether we want it or not.
  2000. *
  2001. * NB! Some targets have JTAG circuitry disabled until a
  2002. * trst & srst has been asserted.
  2003. *
  2004. * NB! here we assume nsrst/ntrst delay are sufficient!
  2005. *
  2006. * NB! order matters!!!! srst *can* disconnect JTAG circuitry
  2007. *
  2008. */
  2009. jtag_add_reset(1, 0); /* RESET or TRST */
  2010. if (jtag_reset_config & RESET_HAS_SRST)
  2011. {
  2012. jtag_add_reset(1, 1);
  2013. if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
  2014. jtag_add_reset(0, 1);
  2015. }
  2016. jtag_add_reset(0, 0);
  2017. if ((retval = jtag_execute_queue()) != ERROR_OK)
  2018. return retval;
  2019. /* Check that we can communication on the JTAG chain + eventually we want to
  2020. * be able to perform enumeration only after OpenOCD has started
  2021. * telnet and GDB server
  2022. *
  2023. * That would allow users to more easily perform any magic they need to before
  2024. * reset happens.
  2025. */
  2026. return jtag_init_inner(cmd_ctx);
  2027. }
  2028. int jtag_init(struct command_context_s *cmd_ctx)
  2029. {
  2030. int retval;
  2031. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  2032. return retval;
  2033. if (jtag_init_inner(cmd_ctx)==ERROR_OK)
  2034. {
  2035. return ERROR_OK;
  2036. }
  2037. return jtag_init_reset(cmd_ctx);
  2038. }
  2039. static int default_khz(int khz, int *jtag_speed)
  2040. {
  2041. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  2042. return ERROR_FAIL;
  2043. }
  2044. static int default_speed_div(int speed, int *khz)
  2045. {
  2046. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  2047. return ERROR_FAIL;
  2048. }
  2049. static int default_power_dropout(int *dropout)
  2050. {
  2051. *dropout=0; /* by default we can't detect power dropout */
  2052. return ERROR_OK;
  2053. }
  2054. static int default_srst_asserted(int *srst_asserted)
  2055. {
  2056. *srst_asserted=0; /* by default we can't detect srst asserted */
  2057. return ERROR_OK;
  2058. }
  2059. static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2060. {
  2061. int i;
  2062. int retval;
  2063. /* check whether the interface is already configured */
  2064. if (jtag_interface)
  2065. {
  2066. LOG_WARNING("Interface already configured, ignoring");
  2067. return ERROR_OK;
  2068. }
  2069. /* interface name is a mandatory argument */
  2070. if (argc < 1 || args[0][0] == '\0')
  2071. {
  2072. return ERROR_COMMAND_SYNTAX_ERROR;
  2073. }
  2074. for (i=0; jtag_interfaces[i]; i++)
  2075. {
  2076. if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
  2077. {
  2078. if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
  2079. {
  2080. return retval;
  2081. }
  2082. jtag_interface = jtag_interfaces[i];
  2083. if (jtag_interface->khz == NULL)
  2084. {
  2085. jtag_interface->khz = default_khz;
  2086. }
  2087. if (jtag_interface->speed_div == NULL)
  2088. {
  2089. jtag_interface->speed_div = default_speed_div;
  2090. }
  2091. if (jtag_interface->power_dropout == NULL)
  2092. {
  2093. jtag_interface->power_dropout = default_power_dropout;
  2094. }
  2095. if (jtag_interface->srst_asserted == NULL)
  2096. {
  2097. jtag_interface->srst_asserted = default_srst_asserted;
  2098. }
  2099. return ERROR_OK;
  2100. }
  2101. }
  2102. /* no valid interface was found (i.e. the configuration option,
  2103. * didn't match one of the compiled-in interfaces
  2104. */
  2105. LOG_ERROR("No valid jtag interface found (%s)", args[0]);
  2106. LOG_ERROR("compiled-in jtag interfaces:");
  2107. for (i = 0; jtag_interfaces[i]; i++)
  2108. {
  2109. LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
  2110. }
  2111. return ERROR_JTAG_INVALID_INTERFACE;
  2112. }
  2113. static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2114. {
  2115. int e;
  2116. char buf[1024];
  2117. Jim_Obj *newargs[ 10 ];
  2118. /*
  2119. * CONVERT SYNTAX
  2120. * argv[-1] = command
  2121. * argv[ 0] = ir length
  2122. * argv[ 1] = ir capture
  2123. * argv[ 2] = ir mask
  2124. * argv[ 3] = not actually used by anything but in the docs
  2125. */
  2126. if( argc < 4 ){
  2127. command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
  2128. return ERROR_OK;
  2129. }
  2130. command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
  2131. command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
  2132. args[0],
  2133. args[1],
  2134. args[2] );
  2135. command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
  2136. command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
  2137. command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
  2138. command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
  2139. newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
  2140. newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
  2141. sprintf( buf, "chip%d", jtag_NumTotalTaps() );
  2142. newargs[2] = Jim_NewStringObj( interp, buf, -1 );
  2143. sprintf( buf, "tap%d", jtag_NumTotalTaps() );
  2144. newargs[3] = Jim_NewStringObj( interp, buf, -1 );
  2145. newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
  2146. newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
  2147. newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
  2148. newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
  2149. newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
  2150. newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
  2151. command_print( cmd_ctx, "NEW COMMAND:");
  2152. sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
  2153. Jim_GetString( newargs[0], NULL ),
  2154. Jim_GetString( newargs[1], NULL ),
  2155. Jim_GetString( newargs[2], NULL ),
  2156. Jim_GetString( newargs[3], NULL ),
  2157. Jim_GetString( newargs[4], NULL ),
  2158. Jim_GetString( newargs[5], NULL ),
  2159. Jim_GetString( newargs[6], NULL ),
  2160. Jim_GetString( newargs[7], NULL ),
  2161. Jim_GetString( newargs[8], NULL ),
  2162. Jim_GetString( newargs[9], NULL ) );
  2163. e = jim_jtag_command( interp, 10, newargs );
  2164. if( e != JIM_OK ){
  2165. command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
  2166. }
  2167. return e;
  2168. }
  2169. static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2170. {
  2171. jtag_tap_t *tap;
  2172. tap = jtag_all_taps;
  2173. command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
  2174. command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
  2175. while( tap ){
  2176. u32 expected, expected_mask, cur_instr, ii;
  2177. expected = buf_get_u32(tap->expected, 0, tap->ir_length);
  2178. expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
  2179. cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
  2180. command_print(cmd_ctx,
  2181. "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
  2182. tap->abs_chain_position,
  2183. tap->dotted_name,
  2184. tap->enabled ? 'Y' : 'n',
  2185. tap->idcode,
  2186. (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
  2187. tap->ir_length,
  2188. expected,
  2189. expected_mask,
  2190. cur_instr);
  2191. for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
  2192. command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
  2193. tap->expected_ids[ii]);
  2194. }
  2195. tap = tap->next_tap;
  2196. }
  2197. return ERROR_OK;
  2198. }
  2199. static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2200. {
  2201. int new_cfg = 0;
  2202. int mask = 0;
  2203. if (argc < 1)
  2204. return ERROR_COMMAND_SYNTAX_ERROR;
  2205. /* Original versions cared about the order of these tokens:
  2206. * reset_config signals [combination [trst_type [srst_type]]]
  2207. * They also clobbered the previous configuration even on error.
  2208. *
  2209. * Here we don't care about the order, and only change values
  2210. * which have been explicitly specified.
  2211. */
  2212. for (; argc; argc--, args++) {
  2213. int tmp = 0;
  2214. int m;
  2215. /* signals */
  2216. m = RESET_HAS_TRST | RESET_HAS_SRST;
  2217. if (strcmp(*args, "none") == 0)
  2218. tmp = RESET_NONE;
  2219. else if (strcmp(*args, "trst_only") == 0)
  2220. tmp = RESET_HAS_TRST;
  2221. else if (strcmp(*args, "srst_only") == 0)
  2222. tmp = RESET_HAS_SRST;
  2223. else if (strcmp(*args, "trst_and_srst") == 0)
  2224. tmp = RESET_HAS_TRST | RESET_HAS_SRST;
  2225. else
  2226. m = 0;
  2227. if (mask & m) {
  2228. LOG_ERROR("extra reset_config %s spec (%s)",
  2229. "signal", *args);
  2230. return ERROR_INVALID_ARGUMENTS;
  2231. }
  2232. if (m)
  2233. goto next;
  2234. /* combination (options for broken wiring) */
  2235. m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  2236. if (strcmp(*args, "separate") == 0)
  2237. /* separate reset lines - default */;
  2238. else if (strcmp(*args, "srst_pulls_trst") == 0)
  2239. tmp |= RESET_SRST_PULLS_TRST;
  2240. else if (strcmp(*args, "trst_pulls_srst") == 0)
  2241. tmp |= RESET_TRST_PULLS_SRST;
  2242. else if (strcmp(*args, "combined") == 0)
  2243. tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  2244. else
  2245. m = 0;
  2246. if (mask & m) {
  2247. LOG_ERROR("extra reset_config %s spec (%s)",
  2248. "combination", *args);
  2249. return ERROR_INVALID_ARGUMENTS;
  2250. }
  2251. if (m)
  2252. goto next;
  2253. /* trst_type (NOP without HAS_TRST) */
  2254. m = RESET_TRST_OPEN_DRAIN;
  2255. if (strcmp(*args, "trst_open_drain") == 0)
  2256. tmp |= RESET_TRST_OPEN_DRAIN;
  2257. else if (strcmp(*args, "trst_push_pull") == 0)
  2258. /* push/pull from adapter - default */;
  2259. else
  2260. m = 0;
  2261. if (mask & m) {
  2262. LOG_ERROR("extra reset_config %s spec (%s)",
  2263. "trst_type", *args);
  2264. return ERROR_INVALID_ARGUMENTS;
  2265. }
  2266. if (m)
  2267. goto next;
  2268. /* srst_type (NOP without HAS_SRST) */
  2269. m |= RESET_SRST_PUSH_PULL;
  2270. if (strcmp(*args, "srst_push_pull") == 0)
  2271. tmp |= RESET_SRST_PUSH_PULL;
  2272. else if (strcmp(*args, "srst_open_drain") == 0)
  2273. /* open drain from adapter - default */;
  2274. else
  2275. m = 0;
  2276. if (mask & m) {
  2277. LOG_ERROR("extra reset_config %s spec (%s)",
  2278. "srst_type", *args);
  2279. return ERROR_INVALID_ARGUMENTS;
  2280. }
  2281. if (m)
  2282. goto next;
  2283. /* caller provided nonsense; fail */
  2284. LOG_ERROR("unknown reset_config flag (%s)", *args);
  2285. return ERROR_INVALID_ARGUMENTS;
  2286. next:
  2287. /* Remember the bits which were specified (mask)
  2288. * and their new values (new_cfg).
  2289. */
  2290. mask |= m;
  2291. new_cfg |= tmp;
  2292. }
  2293. /* clear previous values of those bits, save new values */
  2294. jtag_reset_config &= ~mask;
  2295. jtag_reset_config |= new_cfg;
  2296. return ERROR_OK;
  2297. }
  2298. static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2299. {
  2300. if (argc < 1)
  2301. {
  2302. LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
  2303. exit(-1);
  2304. }
  2305. else
  2306. {
  2307. jtag_nsrst_delay = strtoul(args[0], NULL, 0);
  2308. }
  2309. return ERROR_OK;
  2310. }
  2311. static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2312. {
  2313. if (argc < 1)
  2314. {
  2315. LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
  2316. exit(-1);
  2317. }
  2318. else
  2319. {
  2320. jtag_ntrst_delay = strtoul(args[0], NULL, 0);
  2321. }
  2322. return ERROR_OK;
  2323. }
  2324. static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2325. {
  2326. int retval=ERROR_OK;
  2327. if (argc == 1)
  2328. {
  2329. LOG_DEBUG("handle jtag speed");
  2330. int cur_speed = 0;
  2331. cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
  2332. /* this command can be called during CONFIG,
  2333. * in which case jtag isn't initialized */
  2334. if (jtag)
  2335. {
  2336. retval=jtag->speed(cur_speed);
  2337. }
  2338. } else if (argc == 0)
  2339. {
  2340. } else
  2341. {
  2342. return ERROR_COMMAND_SYNTAX_ERROR;
  2343. }
  2344. command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
  2345. return retval;
  2346. }
  2347. static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2348. {
  2349. int retval=ERROR_OK;
  2350. LOG_DEBUG("handle jtag khz");
  2351. if(argc == 1)
  2352. {
  2353. speed_khz = strtoul(args[0], NULL, 0);
  2354. if (jtag != NULL)
  2355. {
  2356. int cur_speed = 0;
  2357. LOG_DEBUG("have interface set up");
  2358. int speed_div1;
  2359. if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
  2360. {
  2361. speed_khz = 0;
  2362. return retval;
  2363. }
  2364. cur_speed = jtag_speed = speed_div1;
  2365. retval=jtag->speed(cur_speed);
  2366. } else
  2367. {
  2368. hasKHz = 1;
  2369. }
  2370. } else if (argc==0)
  2371. {
  2372. } else
  2373. {
  2374. return ERROR_COMMAND_SYNTAX_ERROR;
  2375. }
  2376. if (jtag!=NULL)
  2377. {
  2378. if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
  2379. return retval;
  2380. }
  2381. if (speed_khz==0)
  2382. {
  2383. command_print(cmd_ctx, "RCLK - adaptive");
  2384. } else
  2385. {
  2386. command_print(cmd_ctx, "%d kHz", speed_khz);
  2387. }
  2388. return retval;
  2389. }
  2390. static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2391. {
  2392. tap_state_t state;
  2393. if (argc < 1)
  2394. {
  2395. return ERROR_COMMAND_SYNTAX_ERROR;
  2396. }
  2397. else
  2398. {
  2399. state = tap_state_by_name( args[0] );
  2400. if( state < 0 ){
  2401. command_print( cmd_ctx, "Invalid state name: %s\n", args[0] );
  2402. return ERROR_COMMAND_SYNTAX_ERROR;
  2403. }
  2404. jtag_add_end_state(state);
  2405. jtag_execute_queue();
  2406. }
  2407. command_print(cmd_ctx, "current endstate: %s", tap_state_name(cmd_queue_end_state));
  2408. return ERROR_OK;
  2409. }
  2410. static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2411. {
  2412. int trst = -1;
  2413. int srst = -1;
  2414. if (argc < 2)
  2415. {
  2416. return ERROR_COMMAND_SYNTAX_ERROR;
  2417. }
  2418. if (args[0][0] == '1')
  2419. trst = 1;
  2420. else if (args[0][0] == '0')
  2421. trst = 0;
  2422. else
  2423. {
  2424. return ERROR_COMMAND_SYNTAX_ERROR;
  2425. }
  2426. if (args[1][0] == '1')
  2427. srst = 1;
  2428. else if (args[1][0] == '0')
  2429. srst = 0;
  2430. else
  2431. {
  2432. return ERROR_COMMAND_SYNTAX_ERROR;
  2433. }
  2434. if (jtag_interface_init(cmd_ctx) != ERROR_OK)
  2435. return ERROR_JTAG_INIT_FAILED;
  2436. jtag_add_reset(trst, srst);
  2437. jtag_execute_queue();
  2438. return ERROR_OK;
  2439. }
  2440. static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2441. {
  2442. if (argc < 1)
  2443. {
  2444. return ERROR_COMMAND_SYNTAX_ERROR;
  2445. }
  2446. jtag_add_runtest(strtol(args[0], NULL, 0), TAP_INVALID);
  2447. jtag_execute_queue();
  2448. return ERROR_OK;
  2449. }
  2450. static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2451. {
  2452. int i;
  2453. scan_field_t *fields;
  2454. jtag_tap_t *tap;
  2455. tap_state_t endstate;
  2456. if ((argc < 2) || (argc % 2))
  2457. {
  2458. return ERROR_COMMAND_SYNTAX_ERROR;
  2459. }
  2460. /* optional "-endstate" */
  2461. /* "statename" */
  2462. /* at the end of the arguments. */
  2463. /* assume none. */
  2464. endstate = cmd_queue_end_state;
  2465. if( argc >= 4 ){
  2466. /* have at least one pair of numbers. */
  2467. /* is last pair the magic text? */
  2468. if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
  2469. const char *cpA;
  2470. const char *cpS;
  2471. cpA = args[ argc-1 ];
  2472. for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
  2473. cpS = tap_state_name( endstate );
  2474. if( 0 == strcmp( cpA, cpS ) ){
  2475. break;
  2476. }
  2477. }
  2478. if( endstate >= TAP_NUM_STATES ){
  2479. return ERROR_COMMAND_SYNTAX_ERROR;
  2480. } else {
  2481. /* found - remove the last 2 args */
  2482. argc -= 2;
  2483. }
  2484. }
  2485. }
  2486. int num_fields = argc / 2;
  2487. fields = malloc(sizeof(scan_field_t) * num_fields);
  2488. for (i = 0; i < num_fields; i++)
  2489. {
  2490. tap = jtag_TapByString( args[i*2] );
  2491. if (tap==NULL)
  2492. {
  2493. command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
  2494. return ERROR_FAIL;
  2495. }
  2496. int field_size = tap->ir_length;
  2497. fields[i].tap = tap;
  2498. fields[i].num_bits = field_size;
  2499. fields[i].out_value = malloc(CEIL(field_size, 8));
  2500. buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
  2501. fields[i].in_value = NULL;
  2502. }
  2503. /* did we have an endstate? */
  2504. jtag_add_ir_scan(num_fields, fields, endstate);
  2505. int retval=jtag_execute_queue();
  2506. for (i = 0; i < num_fields; i++)
  2507. free(fields[i].out_value);
  2508. free (fields);
  2509. return retval;
  2510. }
  2511. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  2512. {
  2513. int retval;
  2514. scan_field_t *fields;
  2515. int num_fields;
  2516. int field_count = 0;
  2517. int i, e;
  2518. jtag_tap_t *tap;
  2519. tap_state_t endstate;
  2520. /* args[1] = device
  2521. * args[2] = num_bits
  2522. * args[3] = hex string
  2523. * ... repeat num bits and hex string ...
  2524. *
  2525. * .. optionally:
  2526. * args[N-2] = "-endstate"
  2527. * args[N-1] = statename
  2528. */
  2529. if ((argc < 4) || ((argc % 2)!=0))
  2530. {
  2531. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  2532. return JIM_ERR;
  2533. }
  2534. /* assume no endstate */
  2535. endstate = cmd_queue_end_state;
  2536. /* validate arguments as numbers */
  2537. e = JIM_OK;
  2538. for (i = 2; i < argc; i+=2)
  2539. {
  2540. long bits;
  2541. const char *cp;
  2542. e = Jim_GetLong(interp, args[i], &bits);
  2543. /* If valid - try next arg */
  2544. if( e == JIM_OK ){
  2545. continue;
  2546. }
  2547. /* Not valid.. are we at the end? */
  2548. if ( ((i+2) != argc) ){
  2549. /* nope, then error */
  2550. return e;
  2551. }
  2552. /* it could be: "-endstate FOO" */
  2553. /* get arg as a string. */
  2554. cp = Jim_GetString( args[i], NULL );
  2555. /* is it the magic? */
  2556. if( 0 == strcmp( "-endstate", cp ) ){
  2557. /* is the statename valid? */
  2558. cp = Jim_GetString( args[i+1], NULL );
  2559. /* see if it is a valid state name */
  2560. endstate = tap_state_by_name(cp);
  2561. if( endstate < 0 ){
  2562. /* update the error message */
  2563. Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
  2564. } else {
  2565. /* valid - so clear the error */
  2566. e = JIM_OK;
  2567. /* and remove the last 2 args */
  2568. argc -= 2;
  2569. }
  2570. }
  2571. /* Still an error? */
  2572. if( e != JIM_OK ){
  2573. return e; /* too bad */
  2574. }
  2575. } /* validate args */
  2576. tap = jtag_TapByJimObj( interp, args[1] );
  2577. if( tap == NULL ){
  2578. return JIM_ERR;
  2579. }
  2580. num_fields=(argc-2)/2;
  2581. fields = malloc(sizeof(scan_field_t) * num_fields);
  2582. for (i = 2; i < argc; i+=2)
  2583. {
  2584. long bits;
  2585. int len;
  2586. const char *str;
  2587. Jim_GetLong(interp, args[i], &bits);
  2588. str = Jim_GetString(args[i+1], &len);
  2589. fields[field_count].tap = tap;
  2590. fields[field_count].num_bits = bits;
  2591. fields[field_count].out_value = malloc(CEIL(bits, 8));
  2592. str_to_buf(str, len, fields[field_count].out_value, bits, 0);
  2593. fields[field_count].in_value = fields[field_count].out_value;
  2594. field_count++;
  2595. }
  2596. jtag_add_dr_scan(num_fields, fields, endstate);
  2597. retval = jtag_execute_queue();
  2598. if (retval != ERROR_OK)
  2599. {
  2600. Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
  2601. return JIM_ERR;
  2602. }
  2603. field_count=0;
  2604. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  2605. for (i = 2; i < argc; i+=2)
  2606. {
  2607. long bits;
  2608. char *str;
  2609. Jim_GetLong(interp, args[i], &bits);
  2610. str = buf_to_str(fields[field_count].in_value, bits, 16);
  2611. free(fields[field_count].out_value);
  2612. Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
  2613. free(str);
  2614. field_count++;
  2615. }
  2616. Jim_SetResult(interp, list);
  2617. free(fields);
  2618. return JIM_OK;
  2619. }
  2620. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  2621. {
  2622. Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_flush_queue_count));
  2623. return JIM_OK;
  2624. }
  2625. static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2626. {
  2627. if (argc == 1)
  2628. {
  2629. if (strcmp(args[0], "enable") == 0)
  2630. {
  2631. jtag_verify_capture_ir = 1;
  2632. }
  2633. else if (strcmp(args[0], "disable") == 0)
  2634. {
  2635. jtag_verify_capture_ir = 0;
  2636. } else
  2637. {
  2638. return ERROR_COMMAND_SYNTAX_ERROR;
  2639. }
  2640. } else if (argc != 0)
  2641. {
  2642. return ERROR_COMMAND_SYNTAX_ERROR;
  2643. }
  2644. command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
  2645. return ERROR_OK;
  2646. }
  2647. static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2648. {
  2649. if (argc == 1)
  2650. {
  2651. if (strcmp(args[0], "enable") == 0)
  2652. {
  2653. jtag_verify = 1;
  2654. }
  2655. else if (strcmp(args[0], "disable") == 0)
  2656. {
  2657. jtag_verify = 0;
  2658. } else
  2659. {
  2660. return ERROR_COMMAND_SYNTAX_ERROR;
  2661. }
  2662. } else if (argc != 0)
  2663. {
  2664. return ERROR_COMMAND_SYNTAX_ERROR;
  2665. }
  2666. command_print(cmd_ctx, "verify jtag capture is %s", (jtag_verify) ? "enabled": "disabled");
  2667. return ERROR_OK;
  2668. }
  2669. int jtag_power_dropout(int *dropout)
  2670. {
  2671. return jtag->power_dropout(dropout);
  2672. }
  2673. int jtag_srst_asserted(int *srst_asserted)
  2674. {
  2675. return jtag->srst_asserted(srst_asserted);
  2676. }
  2677. void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
  2678. {
  2679. jtag_tap_event_action_t * jteap;
  2680. int done;
  2681. jteap = tap->event_action;
  2682. done = 0;
  2683. while (jteap) {
  2684. if (jteap->event == e) {
  2685. done = 1;
  2686. LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
  2687. tap->dotted_name,
  2688. e,
  2689. Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
  2690. Jim_GetString(jteap->body, NULL) );
  2691. if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
  2692. Jim_PrintErrorMessage(interp);
  2693. }
  2694. }
  2695. jteap = jteap->next;
  2696. }
  2697. if (!done) {
  2698. LOG_DEBUG( "event %d %s - no action",
  2699. e,
  2700. Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
  2701. }
  2702. }
  2703. /*-----<Cable Helper API>---------------------------------------*/
  2704. /* these Cable Helper API functions are all documented in the jtag.h header file,
  2705. using a Doxygen format. And since Doxygen's configuration file "Doxyfile",
  2706. is setup to prefer its docs in the header file, no documentation is here, for
  2707. if it were, it would have to be doubly maintained.
  2708. */
  2709. /**
  2710. * @see tap_set_state() and tap_get_state() accessors.
  2711. * Actual name is not important since accessors hide it.
  2712. */
  2713. static tap_state_t state_follower = TAP_RESET;
  2714. void tap_set_state_impl( tap_state_t new_state )
  2715. {
  2716. /* this is the state we think the TAPs are in now, was cur_state */
  2717. state_follower = new_state;
  2718. }
  2719. tap_state_t tap_get_state()
  2720. {
  2721. return state_follower;
  2722. }
  2723. /**
  2724. * @see tap_set_end_state() and tap_get_end_state() accessors.
  2725. * Actual name is not important because accessors hide it.
  2726. */
  2727. static tap_state_t end_state_follower = TAP_RESET;
  2728. void tap_set_end_state( tap_state_t new_end_state )
  2729. {
  2730. /* this is the state we think the TAPs will be in at completion of the
  2731. current TAP operation, was end_state
  2732. */
  2733. end_state_follower = new_end_state;
  2734. }
  2735. tap_state_t tap_get_end_state()
  2736. {
  2737. return end_state_follower;
  2738. }
  2739. int tap_move_ndx( tap_state_t astate )
  2740. {
  2741. /* given a stable state, return the index into the tms_seqs[] array within tap_get_tms_path() */
  2742. int ndx;
  2743. switch( astate )
  2744. {
  2745. case TAP_RESET: ndx = 0; break;
  2746. case TAP_DRSHIFT: ndx = 2; break;
  2747. case TAP_DRPAUSE: ndx = 3; break;
  2748. case TAP_IDLE: ndx = 1; break;
  2749. case TAP_IRSHIFT: ndx = 4; break;
  2750. case TAP_IRPAUSE: ndx = 5; break;
  2751. default:
  2752. LOG_ERROR( "fatal: unstable state \"%s\" used in tap_move_ndx()", tap_state_name(astate) );
  2753. exit(1);
  2754. }
  2755. return ndx;
  2756. }
  2757. /* tap_move[i][j]: tap movement command to go from state i to state j
  2758. * 0: Test-Logic-Reset
  2759. * 1: Run-Test/Idle
  2760. * 2: Shift-DR
  2761. * 3: Pause-DR
  2762. * 4: Shift-IR
  2763. * 5: Pause-IR
  2764. *
  2765. * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
  2766. */
  2767. struct tms_sequences
  2768. {
  2769. u8 bits;
  2770. u8 bit_count;
  2771. };
  2772. /*
  2773. * These macros allow us to specify TMS state transitions by bits rather than hex bytes.
  2774. * Read the bits from LSBit first to MSBit last (right-to-left).
  2775. */
  2776. #define HEX__(n) 0x##n##LU
  2777. #define B8__(x) \
  2778. (((x) & 0x0000000FLU)?(1<<0):0) \
  2779. +(((x) & 0x000000F0LU)?(1<<1):0) \
  2780. +(((x) & 0x00000F00LU)?(1<<2):0) \
  2781. +(((x) & 0x0000F000LU)?(1<<3):0) \
  2782. +(((x) & 0x000F0000LU)?(1<<4):0) \
  2783. +(((x) & 0x00F00000LU)?(1<<5):0) \
  2784. +(((x) & 0x0F000000LU)?(1<<6):0) \
  2785. +(((x) & 0xF0000000LU)?(1<<7):0)
  2786. #define B8(bits,count) { ((u8)B8__(HEX__(bits))), (count) }
  2787. static const struct tms_sequences old_tms_seqs[6][6] = /* [from_state_ndx][to_state_ndx] */
  2788. {
  2789. /* value clocked to TMS to move from one of six stable states to another.
  2790. * N.B. OOCD clocks TMS from LSB first, so read these right-to-left.
  2791. * N.B. These values are tightly bound to the table in tap_get_tms_path_len().
  2792. * N.B. Reset only needs to be 0b11111, but in JLink an even byte of 1's is more stable.
  2793. * These extra ones cause no TAP state problem, because we go into reset and stay in reset.
  2794. */
  2795. /* to state: */
  2796. /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
  2797. { B8(1111111,7), B8(0000000,7), B8(0010111,7), B8(0001010,7), B8(0011011,7), B8(0010110,7) }, /* RESET */
  2798. { B8(1111111,7), B8(0000000,7), B8(0100101,7), B8(0000101,7), B8(0101011,7), B8(0001011,7) }, /* IDLE */
  2799. { B8(1111111,7), B8(0110001,7), B8(0000000,7), B8(0000001,7), B8(0001111,7), B8(0101111,7) }, /* DRSHIFT */
  2800. { B8(1111111,7), B8(0110000,7), B8(0100000,7), B8(0010111,7), B8(0011110,7), B8(0101111,7) }, /* DRPAUSE */
  2801. { B8(1111111,7), B8(0110001,7), B8(0000111,7), B8(0010111,7), B8(0000000,7), B8(0000001,7) }, /* IRSHIFT */
  2802. { B8(1111111,7), B8(0110000,7), B8(0011100,7), B8(0010111,7), B8(0011110,7), B8(0101111,7) }, /* IRPAUSE */
  2803. };
  2804. static const struct tms_sequences short_tms_seqs[6][6] = /* [from_state_ndx][to_state_ndx] */
  2805. {
  2806. /* this is the table submitted by Jeff Williams on 3/30/2009 with this comment:
  2807. OK, I added Peter's version of the state table, and it works OK for
  2808. me on MC1322x. I've recreated the jlink portion of patch with this
  2809. new state table. His changes to my state table are pretty minor in
  2810. terms of total transitions, but Peter feels that his version fixes
  2811. some long-standing problems.
  2812. Jeff
  2813. I added the bit count into the table, reduced RESET column to 7 bits from 8.
  2814. Dick
  2815. state specific comments:
  2816. ------------------------
  2817. *->RESET tried the 5 bit reset and it gave me problems, 7 bits seems to
  2818. work better on ARM9 with ft2232 driver. (Dick)
  2819. RESET->DRSHIFT add 1 extra clock cycles in the RESET state before advancing.
  2820. needed on ARM9 with ft2232 driver. (Dick)
  2821. RESET->IRSHIFT add 1 extra clock cycles in the RESET state before advancing.
  2822. needed on ARM9 with ft2232 driver. (Dick)
  2823. */
  2824. /* to state: */
  2825. /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
  2826. { B8(1111111,7), B8(0000000,7), B8(0010111,7), B8(0001010,7), B8(0011011,7), B8(0010110,7) }, /* RESET */
  2827. { B8(1111111,7), B8(0000000,7), B8(001,3), B8(0101,4), B8(0011,4), B8(01011,5) }, /* IDLE */
  2828. { B8(1111111,7), B8(011,3), B8(00111,5), B8(01,2), B8(001111,6), B8(0101111,7) }, /* DRSHIFT */
  2829. { B8(1111111,7), B8(011,3), B8(01,2), B8(0,1), B8(001111,6), B8(0101111,7) }, /* DRPAUSE */
  2830. { B8(1111111,7), B8(011,3), B8(00111,5), B8(010111,6), B8(001111,6), B8(01,2) }, /* IRSHIFT */
  2831. { B8(1111111,7), B8(011,3), B8(00111,5), B8(010111,6), B8(01,2), B8(0,1) } /* IRPAUSE */
  2832. };
  2833. typedef const struct tms_sequences tms_table[6][6];
  2834. static tms_table *tms_seqs=&short_tms_seqs;
  2835. int tap_get_tms_path( tap_state_t from, tap_state_t to )
  2836. {
  2837. return (*tms_seqs)[tap_move_ndx(from)][tap_move_ndx(to)].bits;
  2838. }
  2839. int tap_get_tms_path_len( tap_state_t from, tap_state_t to )
  2840. {
  2841. return (*tms_seqs)[tap_move_ndx(from)][tap_move_ndx(to)].bit_count;
  2842. }
  2843. bool tap_is_state_stable(tap_state_t astate)
  2844. {
  2845. bool is_stable;
  2846. /* A switch() is used because it is symbol dependent
  2847. (not value dependent like an array), and can also check bounds.
  2848. */
  2849. switch( astate )
  2850. {
  2851. case TAP_RESET:
  2852. case TAP_IDLE:
  2853. case TAP_DRSHIFT:
  2854. case TAP_DRPAUSE:
  2855. case TAP_IRSHIFT:
  2856. case TAP_IRPAUSE:
  2857. is_stable = true;
  2858. break;
  2859. default:
  2860. is_stable = false;
  2861. }
  2862. return is_stable;
  2863. }
  2864. tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
  2865. {
  2866. tap_state_t new_state;
  2867. /* A switch is used because it is symbol dependent and not value dependent
  2868. like an array. Also it can check for out of range conditions.
  2869. */
  2870. if (tms)
  2871. {
  2872. switch (cur_state)
  2873. {
  2874. case TAP_RESET:
  2875. new_state = cur_state;
  2876. break;
  2877. case TAP_IDLE:
  2878. case TAP_DRUPDATE:
  2879. case TAP_IRUPDATE:
  2880. new_state = TAP_DRSELECT;
  2881. break;
  2882. case TAP_DRSELECT:
  2883. new_state = TAP_IRSELECT;
  2884. break;
  2885. case TAP_DRCAPTURE:
  2886. case TAP_DRSHIFT:
  2887. new_state = TAP_DREXIT1;
  2888. break;
  2889. case TAP_DREXIT1:
  2890. case TAP_DREXIT2:
  2891. new_state = TAP_DRUPDATE;
  2892. break;
  2893. case TAP_DRPAUSE:
  2894. new_state = TAP_DREXIT2;
  2895. break;
  2896. case TAP_IRSELECT:
  2897. new_state = TAP_RESET;
  2898. break;
  2899. case TAP_IRCAPTURE:
  2900. case TAP_IRSHIFT:
  2901. new_state = TAP_IREXIT1;
  2902. break;
  2903. case TAP_IREXIT1:
  2904. case TAP_IREXIT2:
  2905. new_state = TAP_IRUPDATE;
  2906. break;
  2907. case TAP_IRPAUSE:
  2908. new_state = TAP_IREXIT2;
  2909. break;
  2910. default:
  2911. LOG_ERROR( "fatal: invalid argument cur_state=%d", cur_state );
  2912. exit(1);
  2913. break;
  2914. }
  2915. }
  2916. else
  2917. {
  2918. switch (cur_state)
  2919. {
  2920. case TAP_RESET:
  2921. case TAP_IDLE:
  2922. case TAP_DRUPDATE:
  2923. case TAP_IRUPDATE:
  2924. new_state = TAP_IDLE;
  2925. break;
  2926. case TAP_DRSELECT:
  2927. new_state = TAP_DRCAPTURE;
  2928. break;
  2929. case TAP_DRCAPTURE:
  2930. case TAP_DRSHIFT:
  2931. case TAP_DREXIT2:
  2932. new_state = TAP_DRSHIFT;
  2933. break;
  2934. case TAP_DREXIT1:
  2935. case TAP_DRPAUSE:
  2936. new_state = TAP_DRPAUSE;
  2937. break;
  2938. case TAP_IRSELECT:
  2939. new_state = TAP_IRCAPTURE;
  2940. break;
  2941. case TAP_IRCAPTURE:
  2942. case TAP_IRSHIFT:
  2943. case TAP_IREXIT2:
  2944. new_state = TAP_IRSHIFT;
  2945. break;
  2946. case TAP_IREXIT1:
  2947. case TAP_IRPAUSE:
  2948. new_state = TAP_IRPAUSE;
  2949. break;
  2950. default:
  2951. LOG_ERROR( "fatal: invalid argument cur_state=%d", cur_state );
  2952. exit(1);
  2953. break;
  2954. }
  2955. }
  2956. return new_state;
  2957. }
  2958. const char* tap_state_name(tap_state_t state)
  2959. {
  2960. const char* ret;
  2961. switch( state )
  2962. {
  2963. case TAP_RESET: ret = "RESET"; break;
  2964. case TAP_IDLE: ret = "RUN/IDLE"; break;
  2965. case TAP_DRSELECT: ret = "DRSELECT"; break;
  2966. case TAP_DRCAPTURE: ret = "DRCAPTURE"; break;
  2967. case TAP_DRSHIFT: ret = "DRSHIFT"; break;
  2968. case TAP_DREXIT1: ret = "DREXIT1"; break;
  2969. case TAP_DRPAUSE: ret = "DRPAUSE"; break;
  2970. case TAP_DREXIT2: ret = "DREXIT2"; break;
  2971. case TAP_DRUPDATE: ret = "DRUPDATE"; break;
  2972. case TAP_IRSELECT: ret = "IRSELECT"; break;
  2973. case TAP_IRCAPTURE: ret = "IRCAPTURE"; break;
  2974. case TAP_IRSHIFT: ret = "IRSHIFT"; break;
  2975. case TAP_IREXIT1: ret = "IREXIT1"; break;
  2976. case TAP_IRPAUSE: ret = "IRPAUSE"; break;
  2977. case TAP_IREXIT2: ret = "IREXIT2"; break;
  2978. case TAP_IRUPDATE: ret = "IRUPDATE"; break;
  2979. default: ret = "???";
  2980. }
  2981. return ret;
  2982. }
  2983. static tap_state_t tap_state_by_name( const char *name )
  2984. {
  2985. tap_state_t x;
  2986. for( x = 0 ; x < TAP_NUM_STATES ; x++ ){
  2987. /* be nice to the human */
  2988. if( 0 == strcasecmp( name, tap_state_name(x) ) ){
  2989. return x;
  2990. }
  2991. }
  2992. /* not found */
  2993. return TAP_INVALID;
  2994. }
  2995. #ifdef _DEBUG_JTAG_IO_
  2996. #define JTAG_DEBUG_STATE_APPEND(buf, len, bit) \
  2997. do { buf[len] = bit ? '1' : '0'; } while(0)
  2998. #define JTAG_DEBUG_STATE_PRINT(a, b, astr, bstr) \
  2999. DEBUG_JTAG_IO("TAP/SM: %9s -> %5s\tTMS: %s\tTDI: %s", \
  3000. tap_state_name(a), tap_state_name(b), astr, bstr)
  3001. tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf,
  3002. unsigned tap_bits, tap_state_t next_state)
  3003. {
  3004. const u8 *tms_buffer;
  3005. const u8 *tdi_buffer;
  3006. unsigned tap_bytes;
  3007. unsigned cur_byte;
  3008. unsigned cur_bit;
  3009. unsigned tap_out_bits;
  3010. char tms_str[33];
  3011. char tdi_str[33];
  3012. tap_state_t last_state;
  3013. // set startstate (and possibly last, if tap_bits == 0)
  3014. last_state = next_state;
  3015. DEBUG_JTAG_IO("TAP/SM: START state: %s", tap_state_name(next_state));
  3016. tms_buffer = (const u8 *)tms_buf;
  3017. tdi_buffer = (const u8 *)tdi_buf;
  3018. tap_bytes = TAP_SCAN_BYTES(tap_bits);
  3019. DEBUG_JTAG_IO("TAP/SM: TMS bits: %u (bytes: %u)", tap_bits, tap_bytes);
  3020. tap_out_bits = 0;
  3021. for(cur_byte = 0; cur_byte < tap_bytes; cur_byte++)
  3022. {
  3023. for(cur_bit = 0; cur_bit < 8; cur_bit++)
  3024. {
  3025. // make sure we do not run off the end of the buffers
  3026. unsigned tap_bit = cur_byte * 8 + cur_bit;
  3027. if (tap_bit == tap_bits)
  3028. break;
  3029. // check and save TMS bit
  3030. tap_bit = !!(tms_buffer[cur_byte] & (1 << cur_bit));
  3031. JTAG_DEBUG_STATE_APPEND(tms_str, tap_out_bits, tap_bit);
  3032. // use TMS bit to find the next TAP state
  3033. next_state = tap_state_transition(last_state, tap_bit);
  3034. // check and store TDI bit
  3035. tap_bit = !!(tdi_buffer[cur_byte] & (1 << cur_bit));
  3036. JTAG_DEBUG_STATE_APPEND(tdi_str, tap_out_bits, tap_bit);
  3037. // increment TAP bits
  3038. tap_out_bits++;
  3039. // Only show TDO bits on state transitions, or
  3040. // after some number of bits in the same state.
  3041. if ((next_state == last_state) && (tap_out_bits < 32))
  3042. continue;
  3043. // terminate strings and display state transition
  3044. tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
  3045. JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
  3046. // reset state
  3047. last_state = next_state;
  3048. tap_out_bits = 0;
  3049. }
  3050. }
  3051. if (tap_out_bits)
  3052. {
  3053. // terminate strings and display state transition
  3054. tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
  3055. JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
  3056. }
  3057. DEBUG_JTAG_IO("TAP/SM: FINAL state: %s", tap_state_name(next_state));
  3058. return next_state;
  3059. }
  3060. #endif // _DEBUG_JTAG_IO_
  3061. #ifndef HAVE_JTAG_MINIDRIVER_H
  3062. void jtag_alloc_in_value32(scan_field_t *field)
  3063. {
  3064. field->in_value=(u8 *)cmd_queue_alloc(4);
  3065. }
  3066. #endif
  3067. static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  3068. {
  3069. if (argc == 1)
  3070. {
  3071. if (strcmp(args[0], "short") == 0)
  3072. {
  3073. tms_seqs=&short_tms_seqs;
  3074. }
  3075. else if (strcmp(args[0], "long") == 0)
  3076. {
  3077. tms_seqs=&old_tms_seqs;
  3078. } else
  3079. {
  3080. return ERROR_COMMAND_SYNTAX_ERROR;
  3081. }
  3082. } else if (argc != 0)
  3083. {
  3084. return ERROR_COMMAND_SYNTAX_ERROR;
  3085. }
  3086. command_print(cmd_ctx, "tms sequence is %s", (tms_seqs==&short_tms_seqs) ? "short": "long");
  3087. return ERROR_OK;
  3088. }
  3089. /*-----</Cable Helper API>--------------------------------------*/
  3090. /**
  3091. * Function jtag_add_statemove
  3092. * moves from the current state to the goal \a state. This needs
  3093. * to be handled according to the xsvf spec, see the XSTATE command
  3094. * description.
  3095. */
  3096. int jtag_add_statemove(tap_state_t goal_state)
  3097. {
  3098. int retval = ERROR_OK;
  3099. tap_state_t moves[8];
  3100. tap_state_t cur_state = cmd_queue_cur_state;
  3101. int i;
  3102. int tms_bits;
  3103. int tms_count;
  3104. LOG_DEBUG( "cur_state=%s goal_state=%s",
  3105. tap_state_name(cur_state),
  3106. tap_state_name(goal_state) );
  3107. /* From the XSVF spec, pertaining to XSTATE:
  3108. For special states known as stable states (Test-Logic-Reset,
  3109. Run-Test/Idle, Pause-DR, Pause- IR), an XSVF interpreter follows
  3110. predefined TAP state paths when the starting state is a stable state and
  3111. when the XSTATE specifies a new stable state (see the STATE command in
  3112. the [Ref 5] for the TAP state paths between stable states). For
  3113. non-stable states, XSTATE should specify a state that is only one TAP
  3114. state transition distance from the current TAP state to avoid undefined
  3115. TAP state paths. A sequence of multiple XSTATE commands can be issued to
  3116. transition the TAP through a specific state path.
  3117. */
  3118. if (goal_state==cur_state )
  3119. ; /* nothing to do */
  3120. else if( goal_state==TAP_RESET )
  3121. {
  3122. jtag_add_tlr();
  3123. }
  3124. else if( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
  3125. {
  3126. /* note: unless tms_bits holds a path that agrees with [Ref 5] in above
  3127. spec, then this code is not fully conformant to the xsvf spec. This
  3128. puts a burden on tap_get_tms_path() function from the xsvf spec.
  3129. If in doubt, you should confirm that that burden is being met.
  3130. */
  3131. tms_bits = tap_get_tms_path(cur_state, goal_state);
  3132. tms_count = tap_get_tms_path_len(cur_state, goal_state);
  3133. assert( (unsigned) tms_count < DIM(moves) );
  3134. for (i=0; i<tms_count; i++, tms_bits>>=1)
  3135. {
  3136. bool bit = tms_bits & 1;
  3137. cur_state = tap_state_transition(cur_state, bit);
  3138. moves[i] = cur_state;
  3139. }
  3140. jtag_add_pathmove(tms_count, moves);
  3141. }
  3142. /* else state must be immediately reachable in one clock cycle, and does not
  3143. need to be a stable state.
  3144. */
  3145. else if( tap_state_transition(cur_state, true) == goal_state
  3146. || tap_state_transition(cur_state, false) == goal_state )
  3147. {
  3148. /* move a single state */
  3149. moves[0] = goal_state;
  3150. jtag_add_pathmove( 1, moves );
  3151. }
  3152. else
  3153. {
  3154. retval = ERROR_FAIL;
  3155. }
  3156. return retval;
  3157. }