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.
 
 
 
 
 
 

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