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.
 
 
 
 
 
 

2230 lines
59 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. * SD->SD and SI->SI have to be caught in interface specific code
  61. */
  62. u8 tap_move[6][6] =
  63. {
  64. /* TLR RTI SD PD SI PI */
  65. {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
  66. {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
  67. {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
  68. {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
  69. {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
  70. {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
  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_TLR, TAP_RTI}, /* TLR */
  79. {TAP_SIS, TAP_CD}, /* SDS */
  80. {TAP_E1D, TAP_SD}, /* CD */
  81. {TAP_E1D, TAP_SD}, /* SD */
  82. {TAP_UD, TAP_PD}, /* E1D */
  83. {TAP_E2D, TAP_PD}, /* PD */
  84. {TAP_UD, TAP_SD}, /* E2D */
  85. {TAP_SDS, TAP_RTI}, /* UD */
  86. {TAP_SDS, TAP_RTI}, /* RTI */
  87. {TAP_TLR, TAP_CI}, /* SIS */
  88. {TAP_E1I, TAP_SI}, /* CI */
  89. {TAP_E1I, TAP_SI}, /* SI */
  90. {TAP_UI, TAP_PI}, /* E1I */
  91. {TAP_E2I, TAP_PI}, /* PI */
  92. {TAP_UI, TAP_SI}, /* E2I */
  93. {TAP_SDS, TAP_RTI} /* UI */
  94. };
  95. char* jtag_event_strings[] =
  96. {
  97. "JTAG controller reset (TLR 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_TLR;
  105. enum tap_state cur_state = TAP_TLR;
  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. jtag_device_t *jtag_devices = NULL;
  111. int jtag_num_devices = 0;
  112. int jtag_ir_scan_size = 0;
  113. enum reset_types jtag_reset_config = RESET_NONE;
  114. enum tap_state cmd_queue_end_state = TAP_TLR;
  115. enum tap_state cmd_queue_cur_state = TAP_TLR;
  116. int jtag_verify_capture_ir = 1;
  117. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
  118. int jtag_nsrst_delay = 0; /* default to no nSRST delay */
  119. int jtag_ntrst_delay = 0; /* default to no nTRST delay */
  120. /* maximum number of JTAG devices expected in the chain
  121. */
  122. #define JTAG_MAX_CHAIN_SIZE 20
  123. /* callbacks to inform high-level handlers about JTAG state changes */
  124. jtag_event_callback_t *jtag_event_callbacks;
  125. /* speed in kHz*/
  126. static int speed_khz = 0;
  127. /* flag if the kHz speed was defined */
  128. static int hasKHz = 0;
  129. /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
  130. */
  131. #if BUILD_ECOSBOARD == 1
  132. extern jtag_interface_t eCosBoard_interface;
  133. #endif
  134. #if BUILD_PARPORT == 1
  135. extern jtag_interface_t parport_interface;
  136. #endif
  137. #if BUILD_DUMMY == 1
  138. extern jtag_interface_t dummy_interface;
  139. #endif
  140. #if BUILD_FT2232_FTD2XX == 1
  141. extern jtag_interface_t ft2232_interface;
  142. #endif
  143. #if BUILD_FT2232_LIBFTDI == 1
  144. extern jtag_interface_t ft2232_interface;
  145. #endif
  146. #if BUILD_AMTJTAGACCEL == 1
  147. extern jtag_interface_t amt_jtagaccel_interface;
  148. #endif
  149. #if BUILD_EP93XX == 1
  150. extern jtag_interface_t ep93xx_interface;
  151. #endif
  152. #if BUILD_AT91RM9200 == 1
  153. extern jtag_interface_t at91rm9200_interface;
  154. #endif
  155. #if BUILD_GW16012 == 1
  156. extern jtag_interface_t gw16012_interface;
  157. #endif
  158. #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
  159. extern jtag_interface_t presto_interface;
  160. #endif
  161. #if BUILD_USBPROG == 1
  162. extern jtag_interface_t usbprog_interface;
  163. #endif
  164. #if BUILD_JLINK == 1
  165. extern jtag_interface_t jlink_interface;
  166. #endif
  167. jtag_interface_t *jtag_interfaces[] = {
  168. #if BUILD_ECOSBOARD == 1
  169. &eCosBoard_interface,
  170. #endif
  171. #if BUILD_PARPORT == 1
  172. &parport_interface,
  173. #endif
  174. #if BUILD_DUMMY == 1
  175. &dummy_interface,
  176. #endif
  177. #if BUILD_FT2232_FTD2XX == 1
  178. &ft2232_interface,
  179. #endif
  180. #if BUILD_FT2232_LIBFTDI == 1
  181. &ft2232_interface,
  182. #endif
  183. #if BUILD_AMTJTAGACCEL == 1
  184. &amt_jtagaccel_interface,
  185. #endif
  186. #if BUILD_EP93XX == 1
  187. &ep93xx_interface,
  188. #endif
  189. #if BUILD_AT91RM9200 == 1
  190. &at91rm9200_interface,
  191. #endif
  192. #if BUILD_GW16012 == 1
  193. &gw16012_interface,
  194. #endif
  195. #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
  196. &presto_interface,
  197. #endif
  198. #if BUILD_USBPROG == 1
  199. &usbprog_interface,
  200. #endif
  201. #if BUILD_JLINK == 1
  202. &jlink_interface,
  203. #endif
  204. NULL,
  205. };
  206. jtag_interface_t *jtag = NULL;
  207. /* configuration */
  208. jtag_interface_t *jtag_interface = NULL;
  209. int jtag_speed = 0;
  210. /* forward declarations */
  211. void jtag_add_pathmove(int num_states, enum tap_state *path);
  212. void jtag_add_runtest(int num_cycles, enum tap_state endstate);
  213. void jtag_add_end_state(enum tap_state endstate);
  214. void jtag_add_sleep(u32 us);
  215. int jtag_execute_queue(void);
  216. /* jtag commands */
  217. int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  218. int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  219. int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  220. int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  221. int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  222. int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  223. int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  224. int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  225. int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  226. int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  227. int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  228. int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  229. int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  230. int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  231. int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
  232. {
  233. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  234. if (callback == NULL)
  235. {
  236. return ERROR_INVALID_ARGUMENTS;
  237. }
  238. if (*callbacks_p)
  239. {
  240. while ((*callbacks_p)->next)
  241. callbacks_p = &((*callbacks_p)->next);
  242. callbacks_p = &((*callbacks_p)->next);
  243. }
  244. (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
  245. (*callbacks_p)->callback = callback;
  246. (*callbacks_p)->priv = priv;
  247. (*callbacks_p)->next = NULL;
  248. return ERROR_OK;
  249. }
  250. int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
  251. {
  252. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  253. if (callback == NULL)
  254. {
  255. return ERROR_INVALID_ARGUMENTS;
  256. }
  257. while (*callbacks_p)
  258. {
  259. jtag_event_callback_t **next = &((*callbacks_p)->next);
  260. if ((*callbacks_p)->callback == callback)
  261. {
  262. free(*callbacks_p);
  263. *callbacks_p = *next;
  264. }
  265. callbacks_p = next;
  266. }
  267. return ERROR_OK;
  268. }
  269. int jtag_call_event_callbacks(enum jtag_event event)
  270. {
  271. jtag_event_callback_t *callback = jtag_event_callbacks;
  272. LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
  273. while (callback)
  274. {
  275. callback->callback(event, callback->priv);
  276. callback = callback->next;
  277. }
  278. return ERROR_OK;
  279. }
  280. /* returns a pointer to the pointer of the last command in queue
  281. * this may be a pointer to the root pointer (jtag_command_queue)
  282. * or to the next member of the last but one command
  283. */
  284. jtag_command_t** jtag_get_last_command_p(void)
  285. {
  286. /* jtag_command_t *cmd = jtag_command_queue;
  287. if (cmd)
  288. while (cmd->next)
  289. cmd = cmd->next;
  290. else
  291. return &jtag_command_queue;
  292. return &cmd->next;*/
  293. return last_comand_pointer;
  294. }
  295. /* returns a pointer to the n-th device in the scan chain */
  296. jtag_device_t* jtag_get_device(int num)
  297. {
  298. jtag_device_t *device = jtag_devices;
  299. int i = 0;
  300. while (device)
  301. {
  302. if (num == i)
  303. return device;
  304. device = device->next;
  305. i++;
  306. }
  307. LOG_ERROR("jtag device number %d not defined", num);
  308. exit(-1);
  309. }
  310. void* cmd_queue_alloc(size_t size)
  311. {
  312. cmd_queue_page_t **p_page = &cmd_queue_pages;
  313. int offset;
  314. u8 *t;
  315. if (*p_page)
  316. {
  317. while ((*p_page)->next)
  318. p_page = &((*p_page)->next);
  319. if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
  320. p_page = &((*p_page)->next);
  321. }
  322. if (!*p_page)
  323. {
  324. *p_page = malloc(sizeof(cmd_queue_page_t));
  325. (*p_page)->used = 0;
  326. (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
  327. (*p_page)->next = NULL;
  328. }
  329. offset = (*p_page)->used;
  330. (*p_page)->used += size;
  331. t=(u8 *)((*p_page)->address);
  332. return t + offset;
  333. }
  334. void cmd_queue_free(void)
  335. {
  336. cmd_queue_page_t *page = cmd_queue_pages;
  337. while (page)
  338. {
  339. cmd_queue_page_t *last = page;
  340. free(page->address);
  341. page = page->next;
  342. free(last);
  343. }
  344. cmd_queue_pages = NULL;
  345. }
  346. static void jtag_prelude1(void)
  347. {
  348. if (jtag_trst == 1)
  349. {
  350. LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  351. jtag_error=ERROR_JTAG_TRST_ASSERTED;
  352. return;
  353. }
  354. if (cmd_queue_end_state == TAP_TLR)
  355. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  356. }
  357. static void jtag_prelude(enum tap_state state)
  358. {
  359. jtag_prelude1();
  360. if (state != -1)
  361. jtag_add_end_state(state);
  362. cmd_queue_cur_state = cmd_queue_end_state;
  363. }
  364. void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  365. {
  366. int retval;
  367. jtag_prelude(state);
  368. retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
  369. if (retval!=ERROR_OK)
  370. jtag_error=retval;
  371. }
  372. int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
  373. {
  374. jtag_command_t **last_cmd;
  375. jtag_device_t *device;
  376. int i, j;
  377. int scan_size = 0;
  378. last_cmd = jtag_get_last_command_p();
  379. /* allocate memory for a new list member */
  380. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  381. (*last_cmd)->next = NULL;
  382. last_comand_pointer = &((*last_cmd)->next);
  383. (*last_cmd)->type = JTAG_SCAN;
  384. /* allocate memory for ir scan command */
  385. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  386. (*last_cmd)->cmd.scan->ir_scan = 1;
  387. (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
  388. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
  389. (*last_cmd)->cmd.scan->end_state = state;
  390. for (i = 0; i < jtag_num_devices; i++)
  391. {
  392. int found = 0;
  393. device = jtag_get_device(i);
  394. scan_size = device->ir_length;
  395. (*last_cmd)->cmd.scan->fields[i].device = i;
  396. (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
  397. (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
  398. (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
  399. /* search the list */
  400. for (j = 0; j < num_fields; j++)
  401. {
  402. if (i == fields[j].device)
  403. {
  404. found = 1;
  405. (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  406. (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  407. if (jtag_verify_capture_ir)
  408. {
  409. if (fields[j].in_handler==NULL)
  410. {
  411. jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
  412. } else
  413. {
  414. (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
  415. (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
  416. (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
  417. (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
  418. }
  419. }
  420. device->bypass = 0;
  421. break;
  422. }
  423. }
  424. if (!found)
  425. {
  426. /* if a device isn't listed, set it to BYPASS */
  427. (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  428. (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
  429. device->bypass = 1;
  430. }
  431. /* update device information */
  432. buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
  433. }
  434. return ERROR_OK;
  435. }
  436. void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  437. {
  438. int retval;
  439. jtag_prelude(state);
  440. retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
  441. if (retval!=ERROR_OK)
  442. jtag_error=retval;
  443. }
  444. int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
  445. {
  446. int i;
  447. jtag_command_t **last_cmd;
  448. last_cmd = jtag_get_last_command_p();
  449. /* allocate memory for a new list member */
  450. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  451. (*last_cmd)->next = NULL;
  452. last_comand_pointer = &((*last_cmd)->next);
  453. (*last_cmd)->type = JTAG_SCAN;
  454. /* allocate memory for ir scan command */
  455. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  456. (*last_cmd)->cmd.scan->ir_scan = 1;
  457. (*last_cmd)->cmd.scan->num_fields = num_fields;
  458. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
  459. (*last_cmd)->cmd.scan->end_state = state;
  460. for (i = 0; i < num_fields; i++)
  461. {
  462. int num_bits = fields[i].num_bits;
  463. int num_bytes = CEIL(fields[i].num_bits, 8);
  464. (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
  465. (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
  466. (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
  467. (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
  468. (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
  469. (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
  470. (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
  471. (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
  472. (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
  473. }
  474. return ERROR_OK;
  475. }
  476. void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  477. {
  478. int retval;
  479. jtag_prelude(state);
  480. retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
  481. if (retval!=ERROR_OK)
  482. jtag_error=retval;
  483. }
  484. int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
  485. {
  486. int i, j;
  487. int bypass_devices = 0;
  488. int field_count = 0;
  489. int scan_size;
  490. jtag_command_t **last_cmd = jtag_get_last_command_p();
  491. jtag_device_t *device = jtag_devices;
  492. /* count devices in bypass */
  493. while (device)
  494. {
  495. if (device->bypass)
  496. bypass_devices++;
  497. device = device->next;
  498. }
  499. if (bypass_devices >= jtag_num_devices)
  500. {
  501. LOG_ERROR("all devices in bypass");
  502. return ERROR_JTAG_DEVICE_ERROR;
  503. }
  504. /* allocate memory for a new list member */
  505. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  506. last_comand_pointer = &((*last_cmd)->next);
  507. (*last_cmd)->next = NULL;
  508. (*last_cmd)->type = JTAG_SCAN;
  509. /* allocate memory for dr scan command */
  510. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  511. (*last_cmd)->cmd.scan->ir_scan = 0;
  512. (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
  513. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
  514. (*last_cmd)->cmd.scan->end_state = state;
  515. for (i = 0; i < jtag_num_devices; i++)
  516. {
  517. int found = 0;
  518. (*last_cmd)->cmd.scan->fields[field_count].device = i;
  519. for (j = 0; j < num_fields; j++)
  520. {
  521. if (i == fields[j].device)
  522. {
  523. found = 1;
  524. scan_size = fields[j].num_bits;
  525. (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
  526. (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  527. (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  528. (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
  529. (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
  530. (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
  531. (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
  532. (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
  533. }
  534. }
  535. if (!found)
  536. {
  537. #ifdef _DEBUG_JTAG_IO_
  538. /* if a device isn't listed, the BYPASS register should be selected */
  539. if (!jtag_get_device(i)->bypass)
  540. {
  541. LOG_ERROR("BUG: no scan data for a device not in BYPASS");
  542. exit(-1);
  543. }
  544. #endif
  545. /* program the scan field to 1 bit length, and ignore it's value */
  546. (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
  547. (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
  548. (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
  549. (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
  550. (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
  551. (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
  552. (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
  553. (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
  554. }
  555. else
  556. {
  557. #ifdef _DEBUG_JTAG_IO_
  558. /* if a device is listed, the BYPASS register must not be selected */
  559. if (jtag_get_device(i)->bypass)
  560. {
  561. LOG_ERROR("BUG: scan data for a device in BYPASS");
  562. exit(-1);
  563. }
  564. #endif
  565. }
  566. }
  567. return ERROR_OK;
  568. }
  569. void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
  570. int num_fields,
  571. const int *num_bits,
  572. const u32 *value,
  573. enum tap_state end_state)
  574. {
  575. int i;
  576. int field_count = 0;
  577. int scan_size;
  578. int bypass_devices = 0;
  579. jtag_command_t **last_cmd = jtag_get_last_command_p();
  580. jtag_device_t *device = jtag_devices;
  581. /* count devices in bypass */
  582. while (device)
  583. {
  584. if (device->bypass)
  585. bypass_devices++;
  586. device = device->next;
  587. }
  588. /* allocate memory for a new list member */
  589. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  590. last_comand_pointer = &((*last_cmd)->next);
  591. (*last_cmd)->next = NULL;
  592. (*last_cmd)->type = JTAG_SCAN;
  593. /* allocate memory for dr scan command */
  594. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  595. (*last_cmd)->cmd.scan->ir_scan = 0;
  596. (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
  597. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
  598. (*last_cmd)->cmd.scan->end_state = end_state;
  599. for (i = 0; i < jtag_num_devices; i++)
  600. {
  601. (*last_cmd)->cmd.scan->fields[field_count].device = i;
  602. if (i == device_num)
  603. {
  604. int j;
  605. #ifdef _DEBUG_JTAG_IO_
  606. /* if a device is listed, the BYPASS register must not be selected */
  607. if (jtag_get_device(i)->bypass)
  608. {
  609. LOG_ERROR("BUG: scan data for a device in BYPASS");
  610. exit(-1);
  611. }
  612. #endif
  613. for (j = 0; j < num_fields; j++)
  614. {
  615. u8 out_value[4];
  616. scan_size = num_bits[j];
  617. buf_set_u32(out_value, 0, scan_size, value[j]);
  618. (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
  619. (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  620. (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
  621. (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
  622. (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
  623. (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
  624. (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
  625. (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
  626. }
  627. } else
  628. {
  629. #ifdef _DEBUG_JTAG_IO_
  630. /* if a device isn't listed, the BYPASS register should be selected */
  631. if (!jtag_get_device(i)->bypass)
  632. {
  633. LOG_ERROR("BUG: no scan data for a device not in BYPASS");
  634. exit(-1);
  635. }
  636. #endif
  637. /* program the scan field to 1 bit length, and ignore it's value */
  638. (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
  639. (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
  640. (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
  641. (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
  642. (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
  643. (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
  644. (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
  645. (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
  646. }
  647. }
  648. }
  649. void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  650. {
  651. int retval;
  652. jtag_prelude(state);
  653. retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
  654. if (retval!=ERROR_OK)
  655. jtag_error=retval;
  656. }
  657. int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
  658. {
  659. int i;
  660. jtag_command_t **last_cmd = jtag_get_last_command_p();
  661. /* allocate memory for a new list member */
  662. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  663. last_comand_pointer = &((*last_cmd)->next);
  664. (*last_cmd)->next = NULL;
  665. (*last_cmd)->type = JTAG_SCAN;
  666. /* allocate memory for scan command */
  667. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  668. (*last_cmd)->cmd.scan->ir_scan = 0;
  669. (*last_cmd)->cmd.scan->num_fields = num_fields;
  670. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
  671. (*last_cmd)->cmd.scan->end_state = state;
  672. for (i = 0; i < num_fields; i++)
  673. {
  674. int num_bits = fields[i].num_bits;
  675. int num_bytes = CEIL(fields[i].num_bits, 8);
  676. (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
  677. (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
  678. (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
  679. (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
  680. (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
  681. (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
  682. (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
  683. (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
  684. (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
  685. }
  686. return ERROR_OK;
  687. }
  688. void jtag_add_tlr(void)
  689. {
  690. jtag_prelude(TAP_TLR);
  691. int retval;
  692. retval=interface_jtag_add_tlr();
  693. if (retval!=ERROR_OK)
  694. jtag_error=retval;
  695. }
  696. int MINIDRIVER(interface_jtag_add_tlr)()
  697. {
  698. enum tap_state state = TAP_TLR;
  699. jtag_command_t **last_cmd = jtag_get_last_command_p();
  700. /* allocate memory for a new list member */
  701. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  702. last_comand_pointer = &((*last_cmd)->next);
  703. (*last_cmd)->next = NULL;
  704. (*last_cmd)->type = JTAG_STATEMOVE;
  705. (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  706. (*last_cmd)->cmd.statemove->end_state = state;
  707. return ERROR_OK;
  708. }
  709. void jtag_add_pathmove(int num_states, enum tap_state *path)
  710. {
  711. enum tap_state cur_state=cmd_queue_cur_state;
  712. int i;
  713. int retval;
  714. /* the last state has to be a stable state */
  715. if (tap_move_map[path[num_states - 1]] == -1)
  716. {
  717. LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
  718. exit(-1);
  719. }
  720. for (i=0; i<num_states; i++)
  721. {
  722. if (path[i] == TAP_TLR)
  723. {
  724. LOG_ERROR("BUG: TAP_TLR is not a valid state for pathmove sequences");
  725. exit(-1);
  726. }
  727. if ((tap_transitions[cur_state].low != path[i])&&
  728. (tap_transitions[cur_state].high != path[i]))
  729. {
  730. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
  731. exit(-1);
  732. }
  733. cur_state = path[i];
  734. }
  735. jtag_prelude1();
  736. retval=interface_jtag_add_pathmove(num_states, path);
  737. cmd_queue_cur_state = path[num_states - 1];
  738. if (retval!=ERROR_OK)
  739. jtag_error=retval;
  740. }
  741. int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
  742. {
  743. jtag_command_t **last_cmd = jtag_get_last_command_p();
  744. int i;
  745. /* allocate memory for a new list member */
  746. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  747. last_comand_pointer = &((*last_cmd)->next);
  748. (*last_cmd)->next = NULL;
  749. (*last_cmd)->type = JTAG_PATHMOVE;
  750. (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
  751. (*last_cmd)->cmd.pathmove->num_states = num_states;
  752. (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
  753. for (i = 0; i < num_states; i++)
  754. (*last_cmd)->cmd.pathmove->path[i] = path[i];
  755. return ERROR_OK;
  756. }
  757. int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
  758. {
  759. jtag_command_t **last_cmd = jtag_get_last_command_p();
  760. /* allocate memory for a new list member */
  761. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  762. (*last_cmd)->next = NULL;
  763. last_comand_pointer = &((*last_cmd)->next);
  764. (*last_cmd)->type = JTAG_RUNTEST;
  765. (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
  766. (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
  767. (*last_cmd)->cmd.runtest->end_state = state;
  768. return ERROR_OK;
  769. }
  770. void jtag_add_runtest(int num_cycles, enum tap_state state)
  771. {
  772. int retval;
  773. jtag_prelude(state);
  774. /* executed by sw or hw fifo */
  775. retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
  776. if (retval!=ERROR_OK)
  777. jtag_error=retval;
  778. }
  779. void jtag_add_reset(int req_tlr_or_trst, int req_srst)
  780. {
  781. int trst_with_tlr = 0;
  782. int retval;
  783. /* FIX!!! there are *many* different cases here. A better
  784. * approach is needed for legal combinations of transitions...
  785. */
  786. if ((jtag_reset_config & RESET_HAS_SRST)&&
  787. (jtag_reset_config & RESET_HAS_TRST)&&
  788. ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
  789. {
  790. if (((req_tlr_or_trst&&!jtag_trst)||
  791. (!req_tlr_or_trst&&jtag_trst))&&
  792. ((req_srst&&!jtag_srst)||
  793. (!req_srst&&jtag_srst)))
  794. {
  795. /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
  796. //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
  797. }
  798. }
  799. /* Make sure that jtag_reset_config allows the requested reset */
  800. /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
  801. if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
  802. {
  803. LOG_ERROR("BUG: requested reset would assert trst");
  804. jtag_error=ERROR_FAIL;
  805. return;
  806. }
  807. /* if TRST pulls SRST, we reset with TAP T-L-R */
  808. if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
  809. {
  810. trst_with_tlr = 1;
  811. }
  812. if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
  813. {
  814. LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
  815. jtag_error=ERROR_FAIL;
  816. return;
  817. }
  818. if (req_tlr_or_trst)
  819. {
  820. if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
  821. {
  822. jtag_trst = 1;
  823. } else
  824. {
  825. trst_with_tlr = 1;
  826. }
  827. } else
  828. {
  829. jtag_trst = 0;
  830. }
  831. jtag_srst = req_srst;
  832. retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
  833. if (retval!=ERROR_OK)
  834. {
  835. jtag_error=retval;
  836. return;
  837. }
  838. if (jtag_srst)
  839. {
  840. LOG_DEBUG("SRST line asserted");
  841. }
  842. else
  843. {
  844. LOG_DEBUG("SRST line released");
  845. if (jtag_nsrst_delay)
  846. jtag_add_sleep(jtag_nsrst_delay * 1000);
  847. }
  848. if (trst_with_tlr)
  849. {
  850. LOG_DEBUG("JTAG reset with TLR instead of TRST");
  851. jtag_add_end_state(TAP_TLR);
  852. jtag_add_tlr();
  853. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  854. return;
  855. }
  856. if (jtag_trst)
  857. {
  858. /* we just asserted nTRST, so we're now in Test-Logic-Reset,
  859. * and inform possible listeners about this
  860. */
  861. LOG_DEBUG("TRST line asserted");
  862. cmd_queue_cur_state = TAP_TLR;
  863. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  864. }
  865. else
  866. {
  867. if (jtag_ntrst_delay)
  868. jtag_add_sleep(jtag_ntrst_delay * 1000);
  869. }
  870. }
  871. int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
  872. {
  873. jtag_command_t **last_cmd = jtag_get_last_command_p();
  874. /* allocate memory for a new list member */
  875. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  876. (*last_cmd)->next = NULL;
  877. last_comand_pointer = &((*last_cmd)->next);
  878. (*last_cmd)->type = JTAG_RESET;
  879. (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
  880. (*last_cmd)->cmd.reset->trst = req_trst;
  881. (*last_cmd)->cmd.reset->srst = req_srst;
  882. return ERROR_OK;
  883. }
  884. void jtag_add_end_state(enum tap_state state)
  885. {
  886. cmd_queue_end_state = state;
  887. if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SI))
  888. {
  889. LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
  890. }
  891. }
  892. int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
  893. {
  894. jtag_command_t **last_cmd = jtag_get_last_command_p();
  895. /* allocate memory for a new list member */
  896. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  897. (*last_cmd)->next = NULL;
  898. last_comand_pointer = &((*last_cmd)->next);
  899. (*last_cmd)->type = JTAG_SLEEP;
  900. (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
  901. (*last_cmd)->cmd.sleep->us = us;
  902. return ERROR_OK;
  903. }
  904. void jtag_add_sleep(u32 us)
  905. {
  906. keep_alive(); /* we might be running on a very slow JTAG clk */
  907. int retval=interface_jtag_add_sleep(us);
  908. if (retval!=ERROR_OK)
  909. jtag_error=retval;
  910. return;
  911. }
  912. int jtag_scan_size(scan_command_t *cmd)
  913. {
  914. int bit_count = 0;
  915. int i;
  916. /* count bits in scan command */
  917. for (i = 0; i < cmd->num_fields; i++)
  918. {
  919. bit_count += cmd->fields[i].num_bits;
  920. }
  921. return bit_count;
  922. }
  923. int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
  924. {
  925. int bit_count = 0;
  926. int i;
  927. bit_count = jtag_scan_size(cmd);
  928. *buffer = malloc(CEIL(bit_count, 8));
  929. bit_count = 0;
  930. for (i = 0; i < cmd->num_fields; i++)
  931. {
  932. if (cmd->fields[i].out_value)
  933. {
  934. #ifdef _DEBUG_JTAG_IO_
  935. char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
  936. #endif
  937. buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
  938. #ifdef _DEBUG_JTAG_IO_
  939. LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
  940. free(char_buf);
  941. #endif
  942. }
  943. bit_count += cmd->fields[i].num_bits;
  944. }
  945. return bit_count;
  946. }
  947. int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
  948. {
  949. int i;
  950. int bit_count = 0;
  951. int retval;
  952. /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
  953. retval = ERROR_OK;
  954. for (i = 0; i < cmd->num_fields; i++)
  955. {
  956. /* if neither in_value nor in_handler
  957. * are specified we don't have to examine this field
  958. */
  959. if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
  960. {
  961. int num_bits = cmd->fields[i].num_bits;
  962. u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
  963. #ifdef _DEBUG_JTAG_IO_
  964. char *char_buf;
  965. char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
  966. LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
  967. free(char_buf);
  968. #endif
  969. if (cmd->fields[i].in_value)
  970. {
  971. buf_cpy(captured, cmd->fields[i].in_value, num_bits);
  972. if (cmd->fields[i].in_handler)
  973. {
  974. if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
  975. {
  976. LOG_WARNING("in_handler reported a failed check");
  977. retval = ERROR_JTAG_QUEUE_FAILED;
  978. }
  979. }
  980. }
  981. /* no in_value specified, but a handler takes care of the scanned data */
  982. if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
  983. {
  984. if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
  985. {
  986. /* We're going to call the error:handler later, but if the in_handler
  987. * reported an error we report this failure upstream
  988. */
  989. LOG_WARNING("in_handler reported a failed check");
  990. retval = ERROR_JTAG_QUEUE_FAILED;
  991. }
  992. }
  993. free(captured);
  994. }
  995. bit_count += cmd->fields[i].num_bits;
  996. }
  997. return retval;
  998. }
  999. int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
  1000. {
  1001. int retval = ERROR_OK;
  1002. int num_bits = field->num_bits;
  1003. int compare_failed = 0;
  1004. if (field->in_check_mask)
  1005. compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
  1006. else
  1007. compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
  1008. if (compare_failed)
  1009. {
  1010. /* An error handler could have caught the failing check
  1011. * only report a problem when there wasn't a handler, or if the handler
  1012. * acknowledged the error
  1013. */
  1014. if (compare_failed)
  1015. {
  1016. char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
  1017. char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
  1018. if (field->in_check_mask)
  1019. {
  1020. char *in_check_mask_char;
  1021. in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
  1022. LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char, in_check_value_char, in_check_mask_char);
  1023. free(in_check_mask_char);
  1024. }
  1025. else
  1026. {
  1027. 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);
  1028. }
  1029. free(captured_char);
  1030. free(in_check_value_char);
  1031. retval = ERROR_JTAG_QUEUE_FAILED;
  1032. }
  1033. }
  1034. return retval;
  1035. }
  1036. /*
  1037. set up checking of this field using the in_handler. The values passed in must be valid until
  1038. after jtag_execute() has completed.
  1039. */
  1040. void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
  1041. {
  1042. if (value)
  1043. field->in_handler = jtag_check_value;
  1044. else
  1045. field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
  1046. field->in_handler_priv = NULL;
  1047. field->in_check_value = value;
  1048. field->in_check_mask = mask;
  1049. }
  1050. enum scan_type jtag_scan_type(scan_command_t *cmd)
  1051. {
  1052. int i;
  1053. int type = 0;
  1054. for (i = 0; i < cmd->num_fields; i++)
  1055. {
  1056. if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
  1057. type |= SCAN_IN;
  1058. if (cmd->fields[i].out_value)
  1059. type |= SCAN_OUT;
  1060. }
  1061. return type;
  1062. }
  1063. int MINIDRIVER(interface_jtag_execute_queue)(void)
  1064. {
  1065. int retval;
  1066. if (jtag==NULL)
  1067. {
  1068. LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
  1069. return ERROR_FAIL;
  1070. }
  1071. retval = jtag->execute_queue();
  1072. cmd_queue_free();
  1073. jtag_command_queue = NULL;
  1074. last_comand_pointer = &jtag_command_queue;
  1075. return retval;
  1076. }
  1077. int jtag_execute_queue(void)
  1078. {
  1079. int retval=interface_jtag_execute_queue();
  1080. if (retval==ERROR_OK)
  1081. {
  1082. retval=jtag_error;
  1083. }
  1084. jtag_error=ERROR_OK;
  1085. return retval;
  1086. }
  1087. int jtag_reset_callback(enum jtag_event event, void *priv)
  1088. {
  1089. jtag_device_t *device = priv;
  1090. LOG_DEBUG("-");
  1091. if (event == JTAG_TRST_ASSERTED)
  1092. {
  1093. buf_set_ones(device->cur_instr, device->ir_length);
  1094. device->bypass = 1;
  1095. }
  1096. return ERROR_OK;
  1097. }
  1098. void jtag_sleep(u32 us)
  1099. {
  1100. alive_sleep(us/1000);
  1101. }
  1102. /* Try to examine chain layout according to IEEE 1149.1 §12
  1103. */
  1104. int jtag_examine_chain(void)
  1105. {
  1106. jtag_device_t *device = jtag_devices;
  1107. scan_field_t field;
  1108. u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
  1109. int i;
  1110. int bit_count;
  1111. int device_count = 0;
  1112. u8 zero_check = 0x0;
  1113. u8 one_check = 0xff;
  1114. field.device = 0;
  1115. field.num_bits = sizeof(idcode_buffer) * 8;
  1116. field.out_value = idcode_buffer;
  1117. field.out_mask = NULL;
  1118. field.in_value = idcode_buffer;
  1119. field.in_check_value = NULL;
  1120. field.in_check_mask = NULL;
  1121. field.in_handler = NULL;
  1122. field.in_handler_priv = NULL;
  1123. for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
  1124. {
  1125. buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
  1126. }
  1127. jtag_add_plain_dr_scan(1, &field, TAP_TLR);
  1128. jtag_execute_queue();
  1129. for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
  1130. {
  1131. zero_check |= idcode_buffer[i];
  1132. one_check &= idcode_buffer[i];
  1133. }
  1134. /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
  1135. if ((zero_check == 0x00) || (one_check == 0xff))
  1136. {
  1137. LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
  1138. return ERROR_JTAG_INIT_FAILED;
  1139. }
  1140. for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
  1141. {
  1142. u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  1143. if ((idcode & 1) == 0)
  1144. {
  1145. /* LSB must not be 0, this indicates a device in bypass */
  1146. LOG_WARNING("Device does not have IDCODE");
  1147. idcode=0;
  1148. bit_count += 1;
  1149. }
  1150. else
  1151. {
  1152. u32 manufacturer;
  1153. u32 part;
  1154. u32 version;
  1155. if (idcode == 0x000000FF)
  1156. {
  1157. int unexpected=0;
  1158. /* End of chain (invalid manufacturer ID)
  1159. *
  1160. * The JTAG examine is the very first thing that happens
  1161. *
  1162. * A single JTAG device requires only 64 bits to be read back correctly.
  1163. *
  1164. * The code below adds a check that the rest of the data scanned (640 bits)
  1165. * are all as expected. This helps diagnose/catch problems with the JTAG chain
  1166. *
  1167. * earlier and gives more helpful/explicit error messages.
  1168. */
  1169. for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
  1170. {
  1171. idcode = buf_get_u32(idcode_buffer, bit_count, 32);
  1172. if (unexpected||(idcode != 0x000000FF))
  1173. {
  1174. LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
  1175. unexpected = 1;
  1176. }
  1177. }
  1178. break;
  1179. }
  1180. manufacturer = (idcode & 0xffe) >> 1;
  1181. part = (idcode & 0xffff000) >> 12;
  1182. version = (idcode & 0xf0000000) >> 28;
  1183. LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
  1184. idcode, manufacturer, part, version);
  1185. bit_count += 32;
  1186. }
  1187. if (device)
  1188. {
  1189. device->idcode = idcode;
  1190. device = device->next;
  1191. }
  1192. device_count++;
  1193. }
  1194. /* see if number of discovered devices matches configuration */
  1195. if (device_count != jtag_num_devices)
  1196. {
  1197. LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
  1198. device_count, jtag_num_devices);
  1199. LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
  1200. return ERROR_JTAG_INIT_FAILED;
  1201. }
  1202. return ERROR_OK;
  1203. }
  1204. int jtag_validate_chain(void)
  1205. {
  1206. jtag_device_t *device = jtag_devices;
  1207. int total_ir_length = 0;
  1208. u8 *ir_test = NULL;
  1209. scan_field_t field;
  1210. int chain_pos = 0;
  1211. while (device)
  1212. {
  1213. total_ir_length += device->ir_length;
  1214. device = device->next;
  1215. }
  1216. total_ir_length += 2;
  1217. ir_test = malloc(CEIL(total_ir_length, 8));
  1218. buf_set_ones(ir_test, total_ir_length);
  1219. field.device = 0;
  1220. field.num_bits = total_ir_length;
  1221. field.out_value = ir_test;
  1222. field.out_mask = NULL;
  1223. field.in_value = ir_test;
  1224. field.in_check_value = NULL;
  1225. field.in_check_mask = NULL;
  1226. field.in_handler = NULL;
  1227. field.in_handler_priv = NULL;
  1228. jtag_add_plain_ir_scan(1, &field, TAP_TLR);
  1229. jtag_execute_queue();
  1230. device = jtag_devices;
  1231. while (device)
  1232. {
  1233. if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
  1234. {
  1235. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  1236. LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
  1237. free(cbuf);
  1238. free(ir_test);
  1239. return ERROR_JTAG_INIT_FAILED;
  1240. }
  1241. chain_pos += device->ir_length;
  1242. device = device->next;
  1243. }
  1244. if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
  1245. {
  1246. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  1247. LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
  1248. free(cbuf);
  1249. free(ir_test);
  1250. return ERROR_JTAG_INIT_FAILED;
  1251. }
  1252. free(ir_test);
  1253. return ERROR_OK;
  1254. }
  1255. static int
  1256. jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
  1257. {
  1258. Jim_GetOptInfo goi;
  1259. int e;
  1260. Jim_Nvp *n;
  1261. struct command_context_s *context;
  1262. enum {
  1263. JTAG_CMD_INTERFACE,
  1264. JTAG_CMD_INIT_RESET,
  1265. };
  1266. const Jim_Nvp jtag_cmds[] = {
  1267. { .name = "interface" , .value = JTAG_CMD_INTERFACE },
  1268. { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
  1269. { .name = NULL, .value = -1 },
  1270. };
  1271. context = Jim_GetAssocData(interp, "context");
  1272. // go past the command
  1273. Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
  1274. e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
  1275. if( e != JIM_OK ){
  1276. Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
  1277. return e;
  1278. }
  1279. Jim_SetEmptyResult( goi.interp );
  1280. switch( n->value ){
  1281. case JTAG_CMD_INTERFACE:
  1282. // return the name of the interface
  1283. // TCL code might need to know the exact type...
  1284. // FUTURE: we allow this as a means to "set" the interface.
  1285. if( goi.argc != 0 ){
  1286. Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
  1287. return JIM_ERR;
  1288. }
  1289. Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
  1290. return JIM_OK;
  1291. case JTAG_CMD_INIT_RESET:
  1292. if( goi.argc != 0 ){
  1293. Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
  1294. return JIM_ERR;
  1295. }
  1296. e = jtag_init_reset(context);
  1297. if( e != ERROR_OK ){
  1298. Jim_SetResult_sprintf( goi.interp, "error: %d", e);
  1299. return JIM_ERR;
  1300. }
  1301. return JIM_OK;
  1302. }
  1303. return JIM_ERR;
  1304. }
  1305. int jtag_register_commands(struct command_context_s *cmd_ctx)
  1306. {
  1307. register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
  1308. register_command(cmd_ctx, NULL, "interface", handle_interface_command,
  1309. COMMAND_CONFIG, "try to configure interface");
  1310. register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
  1311. COMMAND_ANY, "set jtag speed (if supported)");
  1312. register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
  1313. COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
  1314. register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
  1315. COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
  1316. register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
  1317. COMMAND_CONFIG, NULL);
  1318. register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
  1319. COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
  1320. register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
  1321. COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
  1322. register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
  1323. COMMAND_EXEC, "print current scan chain configuration");
  1324. register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
  1325. COMMAND_EXEC, "finish JTAG operations in <tap_state>");
  1326. register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
  1327. COMMAND_EXEC, "toggle reset lines <trst> <srst>");
  1328. register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
  1329. COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
  1330. register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
  1331. COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
  1332. register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
  1333. register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
  1334. COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
  1335. return ERROR_OK;
  1336. }
  1337. int jtag_interface_init(struct command_context_s *cmd_ctx)
  1338. {
  1339. if (jtag)
  1340. return ERROR_OK;
  1341. if (!jtag_interface)
  1342. {
  1343. /* nothing was previously specified by "interface" command */
  1344. LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
  1345. return ERROR_JTAG_INVALID_INTERFACE;
  1346. }
  1347. if(hasKHz)
  1348. {
  1349. jtag_interface->khz(speed_khz, &jtag_speed);
  1350. hasKHz = 0;
  1351. }
  1352. if (jtag_interface->init() != ERROR_OK)
  1353. return ERROR_JTAG_INIT_FAILED;
  1354. jtag = jtag_interface;
  1355. return ERROR_OK;
  1356. }
  1357. static int jtag_init_inner(struct command_context_s *cmd_ctx)
  1358. {
  1359. jtag_device_t *device;
  1360. int retval;
  1361. LOG_DEBUG("Init JTAG chain");
  1362. device = jtag_devices;
  1363. jtag_ir_scan_size = 0;
  1364. jtag_num_devices = 0;
  1365. while (device != NULL)
  1366. {
  1367. jtag_ir_scan_size += device->ir_length;
  1368. jtag_num_devices++;
  1369. device = device->next;
  1370. }
  1371. jtag_add_tlr();
  1372. if ((retval=jtag_execute_queue())!=ERROR_OK)
  1373. return retval;
  1374. /* examine chain first, as this could discover the real chain layout */
  1375. if (jtag_examine_chain() != ERROR_OK)
  1376. {
  1377. LOG_ERROR("trying to validate configured JTAG chain anyway...");
  1378. }
  1379. if (jtag_validate_chain() != ERROR_OK)
  1380. {
  1381. LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
  1382. }
  1383. return ERROR_OK;
  1384. }
  1385. int jtag_init_reset(struct command_context_s *cmd_ctx)
  1386. {
  1387. int retval;
  1388. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1389. return retval;
  1390. LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
  1391. /* Reset can happen after a power cycle.
  1392. *
  1393. * Ideally we would only assert TRST or run TLR before the target reset.
  1394. *
  1395. * However w/srst_pulls_trst, trst is asserted together with the target
  1396. * reset whether we want it or not.
  1397. *
  1398. * NB! Some targets have JTAG circuitry disabled until a
  1399. * trst & srst has been asserted.
  1400. *
  1401. * NB! here we assume nsrst/ntrst delay are sufficient!
  1402. *
  1403. * NB! order matters!!!! srst *can* disconnect JTAG circuitry
  1404. *
  1405. */
  1406. jtag_add_reset(1, 0); /* TLR or TRST */
  1407. if (jtag_reset_config & RESET_HAS_SRST)
  1408. {
  1409. jtag_add_reset(1, 1);
  1410. if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
  1411. jtag_add_reset(0, 1);
  1412. }
  1413. jtag_add_reset(0, 0);
  1414. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1415. return retval;
  1416. /* Check that we can communication on the JTAG chain + eventually we want to
  1417. * be able to perform enumeration only after OpenOCD has started
  1418. * telnet and GDB server
  1419. *
  1420. * That would allow users to more easily perform any magic they need to before
  1421. * reset happens.
  1422. */
  1423. return jtag_init_inner(cmd_ctx);
  1424. }
  1425. int jtag_init(struct command_context_s *cmd_ctx)
  1426. {
  1427. int retval;
  1428. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  1429. return retval;
  1430. if (jtag_init_inner(cmd_ctx)==ERROR_OK)
  1431. {
  1432. return ERROR_OK;
  1433. }
  1434. return jtag_init_reset(cmd_ctx);
  1435. }
  1436. static int default_khz(int khz, int *jtag_speed)
  1437. {
  1438. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  1439. return ERROR_FAIL;
  1440. }
  1441. static int default_speed_div(int speed, int *khz)
  1442. {
  1443. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  1444. return ERROR_FAIL;
  1445. }
  1446. int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1447. {
  1448. int i;
  1449. int retval;
  1450. /* check whether the interface is already configured */
  1451. if (jtag_interface)
  1452. {
  1453. LOG_WARNING("Interface already configured, ignoring");
  1454. return ERROR_OK;
  1455. }
  1456. /* interface name is a mandatory argument */
  1457. if (argc < 1 || args[0][0] == '\0')
  1458. {
  1459. return ERROR_COMMAND_SYNTAX_ERROR;
  1460. }
  1461. for (i=0; jtag_interfaces[i]; i++)
  1462. {
  1463. if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
  1464. {
  1465. if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
  1466. {
  1467. return retval;
  1468. }
  1469. jtag_interface = jtag_interfaces[i];
  1470. if (jtag_interface->khz == NULL)
  1471. {
  1472. jtag_interface->khz = default_khz;
  1473. }
  1474. if (jtag_interface->speed_div == NULL)
  1475. {
  1476. jtag_interface->speed_div = default_speed_div;
  1477. }
  1478. return ERROR_OK;
  1479. }
  1480. }
  1481. /* no valid interface was found (i.e. the configuration option,
  1482. * didn't match one of the compiled-in interfaces
  1483. */
  1484. LOG_ERROR("No valid jtag interface found (%s)", args[0]);
  1485. LOG_ERROR("compiled-in jtag interfaces:");
  1486. for (i = 0; jtag_interfaces[i]; i++)
  1487. {
  1488. LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
  1489. }
  1490. return ERROR_JTAG_INVALID_INTERFACE;
  1491. }
  1492. int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1493. {
  1494. jtag_device_t **last_device_p = &jtag_devices;
  1495. if (*last_device_p)
  1496. {
  1497. while ((*last_device_p)->next)
  1498. last_device_p = &((*last_device_p)->next);
  1499. last_device_p = &((*last_device_p)->next);
  1500. }
  1501. if (argc < 3)
  1502. return ERROR_OK;
  1503. *last_device_p = malloc(sizeof(jtag_device_t));
  1504. (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
  1505. (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
  1506. buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
  1507. (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
  1508. buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
  1509. (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
  1510. (*last_device_p)->bypass = 1;
  1511. buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
  1512. (*last_device_p)->next = NULL;
  1513. jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
  1514. jtag_num_devices++;
  1515. return ERROR_OK;
  1516. }
  1517. int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1518. {
  1519. jtag_device_t *device = jtag_devices;
  1520. int device_count = 0;
  1521. while (device)
  1522. {
  1523. u32 expected, expected_mask, cur_instr;
  1524. expected = buf_get_u32(device->expected, 0, device->ir_length);
  1525. expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
  1526. cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
  1527. command_print(cmd_ctx, "%i: idcode: 0x%8.8x ir length %i, ir capture 0x%x, ir mask 0x%x, current instruction 0x%x", device_count, device->idcode, device->ir_length, expected, expected_mask, cur_instr);
  1528. device = device->next;
  1529. device_count++;
  1530. }
  1531. return ERROR_OK;
  1532. }
  1533. int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1534. {
  1535. if (argc < 1)
  1536. return ERROR_COMMAND_SYNTAX_ERROR;
  1537. if (argc >= 1)
  1538. {
  1539. if (strcmp(args[0], "none") == 0)
  1540. jtag_reset_config = RESET_NONE;
  1541. else if (strcmp(args[0], "trst_only") == 0)
  1542. jtag_reset_config = RESET_HAS_TRST;
  1543. else if (strcmp(args[0], "srst_only") == 0)
  1544. jtag_reset_config = RESET_HAS_SRST;
  1545. else if (strcmp(args[0], "trst_and_srst") == 0)
  1546. jtag_reset_config = RESET_TRST_AND_SRST;
  1547. else
  1548. {
  1549. LOG_ERROR("invalid reset_config argument, defaulting to none");
  1550. jtag_reset_config = RESET_NONE;
  1551. return ERROR_INVALID_ARGUMENTS;
  1552. }
  1553. }
  1554. if (argc >= 2)
  1555. {
  1556. if (strcmp(args[1], "separate") == 0)
  1557. {
  1558. /* seperate reset lines - default */
  1559. } else
  1560. {
  1561. if (strcmp(args[1], "srst_pulls_trst") == 0)
  1562. jtag_reset_config |= RESET_SRST_PULLS_TRST;
  1563. else if (strcmp(args[1], "trst_pulls_srst") == 0)
  1564. jtag_reset_config |= RESET_TRST_PULLS_SRST;
  1565. else if (strcmp(args[1], "combined") == 0)
  1566. jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  1567. else
  1568. {
  1569. LOG_ERROR("invalid reset_config argument, defaulting to none");
  1570. jtag_reset_config = RESET_NONE;
  1571. return ERROR_INVALID_ARGUMENTS;
  1572. }
  1573. }
  1574. }
  1575. if (argc >= 3)
  1576. {
  1577. if (strcmp(args[2], "trst_open_drain") == 0)
  1578. jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
  1579. else if (strcmp(args[2], "trst_push_pull") == 0)
  1580. jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
  1581. else
  1582. {
  1583. LOG_ERROR("invalid reset_config argument, defaulting to none");
  1584. jtag_reset_config = RESET_NONE;
  1585. return ERROR_INVALID_ARGUMENTS;
  1586. }
  1587. }
  1588. if (argc >= 4)
  1589. {
  1590. if (strcmp(args[3], "srst_push_pull") == 0)
  1591. jtag_reset_config |= RESET_SRST_PUSH_PULL;
  1592. else if (strcmp(args[3], "srst_open_drain") == 0)
  1593. jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
  1594. else
  1595. {
  1596. LOG_ERROR("invalid reset_config argument, defaulting to none");
  1597. jtag_reset_config = RESET_NONE;
  1598. return ERROR_INVALID_ARGUMENTS;
  1599. }
  1600. }
  1601. return ERROR_OK;
  1602. }
  1603. int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1604. {
  1605. if (argc < 1)
  1606. {
  1607. LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
  1608. exit(-1);
  1609. }
  1610. else
  1611. {
  1612. jtag_nsrst_delay = strtoul(args[0], NULL, 0);
  1613. }
  1614. return ERROR_OK;
  1615. }
  1616. int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1617. {
  1618. if (argc < 1)
  1619. {
  1620. LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
  1621. exit(-1);
  1622. }
  1623. else
  1624. {
  1625. jtag_ntrst_delay = strtoul(args[0], NULL, 0);
  1626. }
  1627. return ERROR_OK;
  1628. }
  1629. int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1630. {
  1631. int retval=ERROR_OK;
  1632. if (argc == 1)
  1633. {
  1634. LOG_DEBUG("handle jtag speed");
  1635. int cur_speed = 0;
  1636. cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
  1637. /* this command can be called during CONFIG,
  1638. * in which case jtag isn't initialized */
  1639. if (jtag)
  1640. {
  1641. retval=jtag->speed(cur_speed);
  1642. }
  1643. } else if (argc == 0)
  1644. {
  1645. } else
  1646. {
  1647. return ERROR_COMMAND_SYNTAX_ERROR;
  1648. }
  1649. command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
  1650. return retval;
  1651. }
  1652. int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1653. {
  1654. int retval=ERROR_OK;
  1655. LOG_DEBUG("handle jtag khz");
  1656. if(argc == 1)
  1657. {
  1658. speed_khz = strtoul(args[0], NULL, 0);
  1659. if (jtag != NULL)
  1660. {
  1661. int cur_speed = 0;
  1662. LOG_DEBUG("have interface set up");
  1663. int speed_div1;
  1664. if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
  1665. {
  1666. speed_khz = 0;
  1667. return retval;
  1668. }
  1669. cur_speed = jtag_speed = speed_div1;
  1670. retval=jtag->speed(cur_speed);
  1671. } else
  1672. {
  1673. hasKHz = 1;
  1674. }
  1675. } else if (argc==0)
  1676. {
  1677. } else
  1678. {
  1679. return ERROR_COMMAND_SYNTAX_ERROR;
  1680. }
  1681. if (jtag!=NULL)
  1682. {
  1683. if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
  1684. return retval;
  1685. }
  1686. command_print(cmd_ctx, "jtag_khz: %d", speed_khz);
  1687. return retval;
  1688. }
  1689. int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1690. {
  1691. enum tap_state state;
  1692. if (argc < 1)
  1693. {
  1694. return ERROR_COMMAND_SYNTAX_ERROR;
  1695. }
  1696. else
  1697. {
  1698. for (state = 0; state < 16; state++)
  1699. {
  1700. if (strcmp(args[0], tap_state_strings[state]) == 0)
  1701. {
  1702. jtag_add_end_state(state);
  1703. jtag_execute_queue();
  1704. }
  1705. }
  1706. }
  1707. command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
  1708. return ERROR_OK;
  1709. }
  1710. int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1711. {
  1712. int trst = -1;
  1713. int srst = -1;
  1714. if (argc < 2)
  1715. {
  1716. return ERROR_COMMAND_SYNTAX_ERROR;
  1717. }
  1718. if (args[0][0] == '1')
  1719. trst = 1;
  1720. else if (args[0][0] == '0')
  1721. trst = 0;
  1722. else
  1723. {
  1724. return ERROR_COMMAND_SYNTAX_ERROR;
  1725. }
  1726. if (args[1][0] == '1')
  1727. srst = 1;
  1728. else if (args[1][0] == '0')
  1729. srst = 0;
  1730. else
  1731. {
  1732. return ERROR_COMMAND_SYNTAX_ERROR;
  1733. }
  1734. if (jtag_interface_init(cmd_ctx) != ERROR_OK)
  1735. return ERROR_JTAG_INIT_FAILED;
  1736. jtag_add_reset(trst, srst);
  1737. jtag_execute_queue();
  1738. return ERROR_OK;
  1739. }
  1740. int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1741. {
  1742. if (argc < 1)
  1743. {
  1744. return ERROR_COMMAND_SYNTAX_ERROR;
  1745. }
  1746. jtag_add_runtest(strtol(args[0], NULL, 0), -1);
  1747. jtag_execute_queue();
  1748. return ERROR_OK;
  1749. }
  1750. int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1751. {
  1752. int i;
  1753. scan_field_t *fields;
  1754. if ((argc < 2) || (argc % 2))
  1755. {
  1756. return ERROR_COMMAND_SYNTAX_ERROR;
  1757. }
  1758. fields = malloc(sizeof(scan_field_t) * argc / 2);
  1759. for (i = 0; i < argc / 2; i++)
  1760. {
  1761. int device = strtoul(args[i*2], NULL, 0);
  1762. int field_size = jtag_get_device(device)->ir_length;
  1763. fields[i].device = device;
  1764. fields[i].out_value = malloc(CEIL(field_size, 8));
  1765. buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
  1766. fields[i].out_mask = NULL;
  1767. fields[i].in_value = NULL;
  1768. fields[i].in_check_mask = NULL;
  1769. fields[i].in_handler = NULL;
  1770. fields[i].in_handler_priv = NULL;
  1771. }
  1772. jtag_add_ir_scan(argc / 2, fields, -1);
  1773. jtag_execute_queue();
  1774. for (i = 0; i < argc / 2; i++)
  1775. free(fields[i].out_value);
  1776. free (fields);
  1777. return ERROR_OK;
  1778. }
  1779. int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  1780. {
  1781. int retval;
  1782. scan_field_t *fields;
  1783. int num_fields;
  1784. int field_count = 0;
  1785. int i, e;
  1786. long device;
  1787. /* args[1] = device
  1788. * args[2] = num_bits
  1789. * args[3] = hex string
  1790. * ... repeat num bits and hex string ...
  1791. */
  1792. if ((argc < 4) || ((argc % 2)!=0))
  1793. {
  1794. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  1795. return JIM_ERR;
  1796. }
  1797. for (i = 2; i < argc; i+=2)
  1798. {
  1799. long bits;
  1800. e = Jim_GetLong(interp, args[i], &bits);
  1801. if (e != JIM_OK)
  1802. return e;
  1803. }
  1804. e = Jim_GetLong(interp, args[1], &device);
  1805. if (e != JIM_OK)
  1806. return e;
  1807. num_fields=(argc-2)/2;
  1808. fields = malloc(sizeof(scan_field_t) * num_fields);
  1809. for (i = 2; i < argc; i+=2)
  1810. {
  1811. long bits;
  1812. int len;
  1813. const char *str;
  1814. Jim_GetLong(interp, args[i], &bits);
  1815. str = Jim_GetString(args[i+1], &len);
  1816. fields[field_count].device = device;
  1817. fields[field_count].num_bits = bits;
  1818. fields[field_count].out_value = malloc(CEIL(bits, 8));
  1819. str_to_buf(str, len, fields[field_count].out_value, bits, 0);
  1820. fields[field_count].out_mask = NULL;
  1821. fields[field_count].in_value = fields[field_count].out_value;
  1822. fields[field_count].in_check_mask = NULL;
  1823. fields[field_count].in_check_value = NULL;
  1824. fields[field_count].in_handler = NULL;
  1825. fields[field_count++].in_handler_priv = NULL;
  1826. }
  1827. jtag_add_dr_scan(num_fields, fields, -1);
  1828. retval = jtag_execute_queue();
  1829. if (retval != ERROR_OK)
  1830. {
  1831. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  1832. Jim_AppendStrings(interp, Jim_GetResult(interp), "drscan: jtag execute failed", NULL);
  1833. return JIM_ERR;
  1834. }
  1835. field_count=0;
  1836. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  1837. for (i = 2; i < argc; i+=2)
  1838. {
  1839. long bits;
  1840. char *str;
  1841. Jim_GetLong(interp, args[i], &bits);
  1842. str = buf_to_str(fields[field_count].in_value, bits, 16);
  1843. free(fields[field_count].out_value);
  1844. Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
  1845. free(str);
  1846. field_count++;
  1847. }
  1848. Jim_SetResult(interp, list);
  1849. free(fields);
  1850. return JIM_OK;
  1851. }
  1852. int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1853. {
  1854. if (argc == 1)
  1855. {
  1856. if (strcmp(args[0], "enable") == 0)
  1857. {
  1858. jtag_verify_capture_ir = 1;
  1859. }
  1860. else if (strcmp(args[0], "disable") == 0)
  1861. {
  1862. jtag_verify_capture_ir = 0;
  1863. } else
  1864. {
  1865. return ERROR_COMMAND_SYNTAX_ERROR;
  1866. }
  1867. } else if (argc != 0)
  1868. {
  1869. return ERROR_COMMAND_SYNTAX_ERROR;
  1870. }
  1871. command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
  1872. return ERROR_OK;
  1873. }