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.
 
 
 
 
 
 

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