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.
 
 
 
 
 
 

3684 lines
92 KiB

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