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.
 
 
 
 
 
 

1694 lines
45 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "replacements.h"
  24. #include "jtag.h"
  25. #include "command.h"
  26. #include "log.h"
  27. #include "interpreter.h"
  28. #include "stdlib.h"
  29. #include "string.h"
  30. #include <unistd.h>
  31. char* tap_state_strings[16] =
  32. {
  33. "tlr",
  34. "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
  35. "rti",
  36. "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
  37. };
  38. typedef struct cmd_queue_page_s
  39. {
  40. void *address;
  41. size_t used;
  42. struct cmd_queue_page_s *next;
  43. } cmd_queue_page_t;
  44. #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
  45. static cmd_queue_page_t *cmd_queue_pages = NULL;
  46. /* tap_move[i][j]: tap movement command to go from state i to state j
  47. * 0: Test-Logic-Reset
  48. * 1: Run-Test/Idle
  49. * 2: Shift-DR
  50. * 3: Pause-DR
  51. * 4: Shift-IR
  52. * 5: Pause-IR
  53. */
  54. u8 tap_move[6][6] =
  55. {
  56. /* TLR RTI SD PD SI PI */
  57. {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
  58. {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
  59. {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
  60. {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
  61. {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
  62. {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
  63. };
  64. int tap_move_map[16] = {
  65. 0, -1, -1, 2, -1, 3, -1, -1,
  66. 1, -1, -1, 4, -1, 5, -1, -1
  67. };
  68. tap_transition_t tap_transitions[16] =
  69. {
  70. {TAP_TLR, TAP_RTI}, /* TLR */
  71. {TAP_SIS, TAP_CD}, /* SDS */
  72. {TAP_E1D, TAP_SD}, /* CD */
  73. {TAP_E1D, TAP_SD}, /* SD */
  74. {TAP_UD, TAP_PD}, /* E1D */
  75. {TAP_E2D, TAP_PD}, /* PD */
  76. {TAP_UD, TAP_SD}, /* E2D */
  77. {TAP_SDS, TAP_RTI}, /* UD */
  78. {TAP_SDS, TAP_RTI}, /* RTI */
  79. {TAP_TLR, TAP_CI}, /* SIS */
  80. {TAP_E1I, TAP_SI}, /* CI */
  81. {TAP_E1I, TAP_SI}, /* SI */
  82. {TAP_UI, TAP_PI}, /* E1I */
  83. {TAP_E2I, TAP_PI}, /* PI */
  84. {TAP_UI, TAP_SI}, /* E2I */
  85. {TAP_SDS, TAP_RTI} /* UI */
  86. };
  87. enum tap_state end_state = TAP_TLR;
  88. enum tap_state cur_state = TAP_TLR;
  89. int jtag_trst = 0;
  90. int jtag_srst = 0;
  91. jtag_command_t *jtag_command_queue = NULL;
  92. jtag_command_t **last_comand_pointer = &jtag_command_queue;
  93. jtag_device_t *jtag_devices = NULL;
  94. int jtag_num_devices = 0;
  95. int jtag_ir_scan_size = 0;
  96. enum reset_types jtag_reset_config = RESET_NONE;
  97. enum tap_state cmd_queue_end_state = TAP_TLR;
  98. enum tap_state cmd_queue_cur_state = TAP_TLR;
  99. int jtag_verify_capture_ir = 1;
  100. /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
  101. int jtag_nsrst_delay = 0; /* default to no nSRST delay */
  102. int jtag_ntrst_delay = 0; /* default to no nTRST delay */
  103. /* callbacks to inform high-level handlers about JTAG state changes */
  104. jtag_event_callback_t *jtag_event_callbacks;
  105. /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
  106. */
  107. #if BUILD_PARPORT == 1
  108. extern jtag_interface_t parport_interface;
  109. #endif
  110. #if BUILD_FT2232_FTD2XX == 1
  111. extern jtag_interface_t ft2232_interface;
  112. #endif
  113. #if BUILD_FT2232_LIBFTDI == 1
  114. extern jtag_interface_t ft2232_interface;
  115. #endif
  116. #if BUILD_AMTJTAGACCEL == 1
  117. extern jtag_interface_t amt_jtagaccel_interface;
  118. #endif
  119. #if BUILD_EP93XX == 1
  120. extern jtag_interface_t ep93xx_interface;
  121. #endif
  122. #if BUILD_AT91RM9200 == 1
  123. extern jtag_interface_t at91rm9200_interface;
  124. #endif
  125. #if BUILD_GW16012 == 1
  126. extern jtag_interface_t gw16012_interface;
  127. #endif
  128. jtag_interface_t *jtag_interfaces[] = {
  129. #if BUILD_PARPORT == 1
  130. &parport_interface,
  131. #endif
  132. #if BUILD_FT2232_FTD2XX == 1
  133. &ft2232_interface,
  134. #endif
  135. #if BUILD_FT2232_LIBFTDI == 1
  136. &ft2232_interface,
  137. #endif
  138. #if BUILD_AMTJTAGACCEL == 1
  139. &amt_jtagaccel_interface,
  140. #endif
  141. #if BUILD_EP93XX == 1
  142. &ep93xx_interface,
  143. #endif
  144. #if BUILD_AT91RM9200 == 1
  145. &at91rm9200_interface,
  146. #endif
  147. #if BUILD_GW16012 == 1
  148. &gw16012_interface,
  149. #endif
  150. NULL,
  151. };
  152. jtag_interface_t *jtag = NULL;
  153. /* configuration */
  154. char* jtag_interface = NULL;
  155. int jtag_speed = -1;
  156. /* forward declarations */
  157. /* jtag commands */
  158. int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  159. int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  160. int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  161. int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  162. int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  163. int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  164. int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  165. int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  166. int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  167. int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  168. int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  169. int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  170. int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  171. int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  172. int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
  173. {
  174. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  175. if (callback == NULL)
  176. {
  177. return ERROR_INVALID_ARGUMENTS;
  178. }
  179. if (*callbacks_p)
  180. {
  181. while ((*callbacks_p)->next)
  182. callbacks_p = &((*callbacks_p)->next);
  183. callbacks_p = &((*callbacks_p)->next);
  184. }
  185. (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
  186. (*callbacks_p)->callback = callback;
  187. (*callbacks_p)->priv = priv;
  188. (*callbacks_p)->next = NULL;
  189. return ERROR_OK;
  190. }
  191. int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
  192. {
  193. jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
  194. if (callback == NULL)
  195. {
  196. return ERROR_INVALID_ARGUMENTS;
  197. }
  198. while (*callbacks_p)
  199. {
  200. jtag_event_callback_t **next = &((*callbacks_p)->next);
  201. if ((*callbacks_p)->callback == callback)
  202. {
  203. free(*callbacks_p);
  204. *callbacks_p = *next;
  205. }
  206. callbacks_p = next;
  207. }
  208. return ERROR_OK;
  209. }
  210. int jtag_call_event_callbacks(enum jtag_event event)
  211. {
  212. jtag_event_callback_t *callback = jtag_event_callbacks;
  213. DEBUG("jtag event: %i", event);
  214. while (callback)
  215. {
  216. callback->callback(event, callback->priv);
  217. callback = callback->next;
  218. }
  219. return ERROR_OK;
  220. }
  221. /* returns a pointer to the pointer of the last command in queue
  222. * this may be a pointer to the root pointer (jtag_command_queue)
  223. * or to the next member of the last but one command
  224. */
  225. jtag_command_t** jtag_get_last_command_p(void)
  226. {
  227. /* jtag_command_t *cmd = jtag_command_queue;
  228. if (cmd)
  229. while (cmd->next)
  230. cmd = cmd->next;
  231. else
  232. return &jtag_command_queue;
  233. return &cmd->next;*/
  234. return last_comand_pointer;
  235. }
  236. /* returns a pointer to the n-th device in the scan chain */
  237. jtag_device_t* jtag_get_device(int num)
  238. {
  239. jtag_device_t *device = jtag_devices;
  240. int i = 0;
  241. while (device)
  242. {
  243. if (num == i)
  244. return device;
  245. device = device->next;
  246. i++;
  247. }
  248. return NULL;
  249. }
  250. void* cmd_queue_alloc(size_t size)
  251. {
  252. cmd_queue_page_t **p_page = &cmd_queue_pages;
  253. int offset;
  254. if (*p_page)
  255. {
  256. while ((*p_page)->next)
  257. p_page = &((*p_page)->next);
  258. if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
  259. p_page = &((*p_page)->next);
  260. }
  261. if (!*p_page)
  262. {
  263. *p_page = malloc(sizeof(cmd_queue_page_t));
  264. (*p_page)->used = 0;
  265. (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
  266. (*p_page)->next = NULL;
  267. }
  268. offset = (*p_page)->used;
  269. (*p_page)->used += size;
  270. return ((*p_page)->address) + offset;
  271. }
  272. void cmd_queue_free()
  273. {
  274. cmd_queue_page_t *page = cmd_queue_pages;
  275. while (page)
  276. {
  277. cmd_queue_page_t *last = page;
  278. free(page->address);
  279. page = page->next;
  280. free(last);
  281. }
  282. cmd_queue_pages = NULL;
  283. }
  284. int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  285. {
  286. jtag_command_t **last_cmd;
  287. jtag_device_t *device;
  288. int i, j;
  289. int scan_size = 0;
  290. /* int changed = 0; */
  291. if (jtag_trst == 1)
  292. {
  293. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  294. return ERROR_JTAG_TRST_ASSERTED;
  295. }
  296. /*
  297. for (i=0; i<num_fields; i++)
  298. {
  299. device = jtag_get_device(fields[i].device);
  300. if (device)
  301. {
  302. if (buf_cmp(device->cur_instr, fields[i].out_value, device->ir_length))
  303. changed = 1;
  304. }
  305. else
  306. {
  307. ERROR("inexistant device specified for ir scan");
  308. return ERROR_INVALID_ARGUMENTS;
  309. }
  310. }
  311. if (!changed)
  312. return ERROR_OK;
  313. */
  314. last_cmd = jtag_get_last_command_p();
  315. /* allocate memory for a new list member */
  316. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  317. (*last_cmd)->next = NULL;
  318. last_comand_pointer = &((*last_cmd)->next);
  319. (*last_cmd)->type = JTAG_SCAN;
  320. /* allocate memory for ir scan command */
  321. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  322. (*last_cmd)->cmd.scan->ir_scan = 1;
  323. (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
  324. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
  325. (*last_cmd)->cmd.scan->end_state = state;
  326. if (state != -1)
  327. cmd_queue_end_state = state;
  328. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  329. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  330. if (cmd_queue_end_state == TAP_TLR)
  331. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  332. cmd_queue_cur_state = cmd_queue_end_state;
  333. for (i=0; i < jtag_num_devices; i++)
  334. {
  335. int found = 0;
  336. device = jtag_get_device(i);
  337. scan_size = device->ir_length;
  338. (*last_cmd)->cmd.scan->fields[i].device = i;
  339. (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
  340. (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
  341. if (jtag_verify_capture_ir)
  342. {
  343. (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(device->expected, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  344. (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(device->expected_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  345. }
  346. else
  347. {
  348. (*last_cmd)->cmd.scan->fields[i].in_check_value = NULL;
  349. (*last_cmd)->cmd.scan->fields[i].in_check_mask = NULL;
  350. }
  351. (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
  352. (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
  353. /* search the list */
  354. for (j=0; j < num_fields; j++)
  355. {
  356. if (i == fields[j].device)
  357. {
  358. found = 1;
  359. (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  360. (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  361. device->bypass = 0;
  362. break;
  363. }
  364. }
  365. if (!found)
  366. {
  367. /* if a device isn't listed, set it to BYPASS */
  368. (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  369. (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
  370. device->bypass = 1;
  371. }
  372. /* update device information */
  373. buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
  374. }
  375. return ERROR_OK;
  376. }
  377. int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  378. {
  379. jtag_command_t **last_cmd;
  380. int i;
  381. if (jtag_trst == 1)
  382. {
  383. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  384. return ERROR_JTAG_TRST_ASSERTED;
  385. }
  386. last_cmd = jtag_get_last_command_p();
  387. /* allocate memory for a new list member */
  388. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  389. (*last_cmd)->next = NULL;
  390. last_comand_pointer = &((*last_cmd)->next);
  391. (*last_cmd)->type = JTAG_SCAN;
  392. /* allocate memory for ir scan command */
  393. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  394. (*last_cmd)->cmd.scan->ir_scan = 1;
  395. (*last_cmd)->cmd.scan->num_fields = num_fields;
  396. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
  397. (*last_cmd)->cmd.scan->end_state = state;
  398. if (state != -1)
  399. cmd_queue_end_state = state;
  400. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  401. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  402. if (cmd_queue_end_state == TAP_TLR)
  403. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  404. cmd_queue_cur_state = cmd_queue_end_state;
  405. for (i = 0; i < num_fields; i++)
  406. {
  407. int num_bits = fields[i].num_bits;
  408. int num_bytes = CEIL(fields[i].num_bits, 8);
  409. (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
  410. (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
  411. (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
  412. (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
  413. (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
  414. (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
  415. (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
  416. (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
  417. (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
  418. }
  419. return ERROR_OK;
  420. }
  421. int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  422. {
  423. int i, j;
  424. int bypass_devices = 0;
  425. int field_count = 0;
  426. jtag_command_t **last_cmd = jtag_get_last_command_p();
  427. jtag_device_t *device = jtag_devices;
  428. int scan_size;
  429. if (jtag_trst == 1)
  430. {
  431. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  432. return ERROR_JTAG_TRST_ASSERTED;
  433. }
  434. /* count devices in bypass */
  435. while (device)
  436. {
  437. if (device->bypass)
  438. bypass_devices++;
  439. device = device->next;
  440. }
  441. /* allocate memory for a new list member */
  442. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  443. last_comand_pointer = &((*last_cmd)->next);
  444. (*last_cmd)->next = NULL;
  445. (*last_cmd)->type = JTAG_SCAN;
  446. /* allocate memory for dr scan command */
  447. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  448. (*last_cmd)->cmd.scan->ir_scan = 0;
  449. (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
  450. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
  451. (*last_cmd)->cmd.scan->end_state = state;
  452. if (state != -1)
  453. cmd_queue_end_state = state;
  454. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  455. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  456. if (cmd_queue_end_state == TAP_TLR)
  457. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  458. cmd_queue_cur_state = cmd_queue_end_state;
  459. for (i=0; i < jtag_num_devices; i++)
  460. {
  461. int found = 0;
  462. (*last_cmd)->cmd.scan->fields[field_count].device = i;
  463. for (j=0; j < num_fields; j++)
  464. {
  465. if (i == fields[j].device)
  466. {
  467. found = 1;
  468. scan_size = fields[j].num_bits;
  469. (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
  470. (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  471. (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  472. (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
  473. (*last_cmd)->cmd.scan->fields[field_count].in_check_value = buf_cpy(fields[j].in_check_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  474. (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = buf_cpy(fields[j].in_check_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
  475. (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
  476. (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
  477. }
  478. }
  479. if (!found)
  480. {
  481. /* if a device isn't listed, the BYPASS register should be selected */
  482. if (!jtag_get_device(i)->bypass)
  483. {
  484. ERROR("BUG: no scan data for a device not in BYPASS");
  485. exit(-1);
  486. }
  487. /* program the scan field to 1 bit length, and ignore it's value */
  488. (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
  489. (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
  490. (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
  491. (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
  492. (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
  493. (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
  494. (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
  495. (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
  496. }
  497. else
  498. {
  499. /* if a device is listed, the BYPASS register must not be selected */
  500. if (jtag_get_device(i)->bypass)
  501. {
  502. ERROR("BUG: scan data for a device in BYPASS");
  503. exit(-1);
  504. }
  505. }
  506. }
  507. return ERROR_OK;
  508. }
  509. int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
  510. {
  511. int i;
  512. jtag_command_t **last_cmd = jtag_get_last_command_p();
  513. if (jtag_trst == 1)
  514. {
  515. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  516. return ERROR_JTAG_TRST_ASSERTED;
  517. }
  518. /* allocate memory for a new list member */
  519. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  520. last_comand_pointer = &((*last_cmd)->next);
  521. (*last_cmd)->next = NULL;
  522. (*last_cmd)->type = JTAG_SCAN;
  523. /* allocate memory for scan command */
  524. (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
  525. (*last_cmd)->cmd.scan->ir_scan = 0;
  526. (*last_cmd)->cmd.scan->num_fields = num_fields;
  527. (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
  528. (*last_cmd)->cmd.scan->end_state = state;
  529. if (state != -1)
  530. cmd_queue_end_state = state;
  531. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  532. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  533. if (cmd_queue_end_state == TAP_TLR)
  534. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  535. cmd_queue_cur_state = cmd_queue_end_state;
  536. for (i = 0; i < num_fields; i++)
  537. {
  538. int num_bits = fields[i].num_bits;
  539. int num_bytes = CEIL(fields[i].num_bits, 8);
  540. (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
  541. (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
  542. (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
  543. (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
  544. (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
  545. (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
  546. (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
  547. (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
  548. (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
  549. }
  550. return ERROR_OK;
  551. }
  552. int jtag_add_statemove(enum tap_state state)
  553. {
  554. jtag_command_t **last_cmd = jtag_get_last_command_p();
  555. if (jtag_trst == 1)
  556. {
  557. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  558. return ERROR_JTAG_TRST_ASSERTED;
  559. }
  560. /* allocate memory for a new list member */
  561. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  562. last_comand_pointer = &((*last_cmd)->next);
  563. (*last_cmd)->next = NULL;
  564. (*last_cmd)->type = JTAG_STATEMOVE;
  565. (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  566. (*last_cmd)->cmd.statemove->end_state = state;
  567. if (state != -1)
  568. cmd_queue_end_state = state;
  569. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  570. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  571. if (cmd_queue_end_state == TAP_TLR)
  572. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  573. cmd_queue_cur_state = cmd_queue_end_state;
  574. return ERROR_OK;
  575. }
  576. int jtag_add_pathmove(int num_states, enum tap_state *path)
  577. {
  578. jtag_command_t **last_cmd = jtag_get_last_command_p();
  579. int i;
  580. if (jtag_trst == 1)
  581. {
  582. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  583. return ERROR_JTAG_TRST_ASSERTED;
  584. }
  585. /* the last state has to be a stable state */
  586. if (tap_move_map[path[num_states - 1]] == -1)
  587. {
  588. ERROR("TAP path doesn't finish in a stable state");
  589. return ERROR_JTAG_NOT_IMPLEMENTED;
  590. }
  591. if (jtag->support_pathmove)
  592. {
  593. /* allocate memory for a new list member */
  594. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  595. last_comand_pointer = &((*last_cmd)->next);
  596. (*last_cmd)->next = NULL;
  597. (*last_cmd)->type = JTAG_RUNTEST;
  598. (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
  599. (*last_cmd)->cmd.pathmove->num_states = num_states;
  600. (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
  601. for (i = 0; i < num_states; i++)
  602. (*last_cmd)->cmd.pathmove->path[i] = path[i];
  603. }
  604. else
  605. {
  606. /* validate the desired path, and see if it fits a default path */
  607. int begin = 0;
  608. int end = 0;
  609. int j;
  610. for (i = 0; i < num_states; i++)
  611. {
  612. for (j = i; j < num_states; j++)
  613. {
  614. if (tap_move_map[path[j]] != -1)
  615. {
  616. end = j;
  617. break;
  618. }
  619. }
  620. if (begin - end <= 7) /* a default path spans no more than 7 states */
  621. {
  622. jtag_add_statemove(path[end]);
  623. }
  624. else
  625. {
  626. ERROR("encountered a TAP path that can't be fulfilled by default paths");
  627. return ERROR_JTAG_NOT_IMPLEMENTED;
  628. }
  629. i = end;
  630. }
  631. }
  632. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  633. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  634. if (cmd_queue_end_state == TAP_TLR)
  635. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  636. cmd_queue_cur_state = path[num_states - 1];
  637. return ERROR_OK;
  638. }
  639. int jtag_add_runtest(int num_cycles, enum tap_state state)
  640. {
  641. jtag_command_t **last_cmd = jtag_get_last_command_p();
  642. if (jtag_trst == 1)
  643. {
  644. WARNING("JTAG command queued, while TRST is low (TAP in reset)");
  645. return ERROR_JTAG_TRST_ASSERTED;
  646. }
  647. /* allocate memory for a new list member */
  648. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  649. (*last_cmd)->next = NULL;
  650. last_comand_pointer = &((*last_cmd)->next);
  651. (*last_cmd)->type = JTAG_RUNTEST;
  652. (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
  653. (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
  654. (*last_cmd)->cmd.runtest->end_state = state;
  655. if (state != -1)
  656. cmd_queue_end_state = state;
  657. if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
  658. jtag_call_event_callbacks(JTAG_TRST_RELEASED);
  659. if (cmd_queue_end_state == TAP_TLR)
  660. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  661. cmd_queue_cur_state = cmd_queue_end_state;
  662. return ERROR_OK;
  663. }
  664. int jtag_add_reset(int req_trst, int req_srst)
  665. {
  666. int trst_with_tms = 0;
  667. jtag_command_t **last_cmd = jtag_get_last_command_p();
  668. if (req_trst == -1)
  669. req_trst = jtag_trst;
  670. if (req_srst == -1)
  671. req_srst = jtag_srst;
  672. /* Make sure that jtag_reset_config allows the requested reset */
  673. /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
  674. if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
  675. return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
  676. /* if TRST pulls SRST, we reset with TAP T-L-R */
  677. if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
  678. {
  679. req_trst = 0;
  680. trst_with_tms = 1;
  681. }
  682. if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
  683. {
  684. ERROR("requested nSRST assertion, but the current configuration doesn't support this");
  685. return ERROR_JTAG_RESET_CANT_SRST;
  686. }
  687. if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
  688. {
  689. req_trst = 0;
  690. trst_with_tms = 1;
  691. }
  692. /* allocate memory for a new list member */
  693. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  694. (*last_cmd)->next = NULL;
  695. last_comand_pointer = &((*last_cmd)->next);
  696. (*last_cmd)->type = JTAG_RESET;
  697. (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
  698. (*last_cmd)->cmd.reset->trst = req_trst;
  699. (*last_cmd)->cmd.reset->srst = req_srst;
  700. jtag_trst = req_trst;
  701. jtag_srst = req_srst;
  702. if (jtag_srst)
  703. {
  704. jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
  705. }
  706. else
  707. {
  708. jtag_call_event_callbacks(JTAG_SRST_RELEASED);
  709. if (jtag_nsrst_delay)
  710. jtag_add_sleep(jtag_nsrst_delay * 1000);
  711. }
  712. if (trst_with_tms)
  713. {
  714. last_cmd = &((*last_cmd)->next);
  715. /* allocate memory for a new list member */
  716. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  717. (*last_cmd)->next = NULL;
  718. last_comand_pointer = &((*last_cmd)->next);
  719. (*last_cmd)->type = JTAG_STATEMOVE;
  720. (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  721. (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
  722. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  723. cmd_queue_cur_state = TAP_TLR;
  724. cmd_queue_end_state = TAP_TLR;
  725. return ERROR_OK;
  726. }
  727. else
  728. {
  729. if (jtag_trst)
  730. {
  731. /* we just asserted nTRST, so we're now in Test-Logic-Reset,
  732. * and inform possible listeners about this
  733. */
  734. cmd_queue_cur_state = TAP_TLR;
  735. jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
  736. }
  737. else
  738. {
  739. /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
  740. * but we might want to add a delay to give the TAP time to settle
  741. */
  742. if (jtag_ntrst_delay)
  743. jtag_add_sleep(jtag_ntrst_delay * 1000);
  744. }
  745. }
  746. return ERROR_OK;
  747. }
  748. int jtag_add_end_state(enum tap_state state)
  749. {
  750. jtag_command_t **last_cmd = jtag_get_last_command_p();
  751. /* allocate memory for a new list member */
  752. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  753. (*last_cmd)->next = NULL;
  754. last_comand_pointer = &((*last_cmd)->next);
  755. (*last_cmd)->type = JTAG_END_STATE;
  756. (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
  757. (*last_cmd)->cmd.end_state->end_state = state;
  758. if (state != -1)
  759. cmd_queue_end_state = state;
  760. return ERROR_OK;
  761. }
  762. int jtag_add_sleep(u32 us)
  763. {
  764. jtag_command_t **last_cmd = jtag_get_last_command_p();
  765. /* allocate memory for a new list member */
  766. *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  767. (*last_cmd)->next = NULL;
  768. last_comand_pointer = &((*last_cmd)->next);
  769. (*last_cmd)->type = JTAG_SLEEP;
  770. (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
  771. (*last_cmd)->cmd.sleep->us = us;
  772. return ERROR_OK;
  773. }
  774. int jtag_scan_size(scan_command_t *cmd)
  775. {
  776. int bit_count = 0;
  777. int i;
  778. /* count bits in scan command */
  779. for (i=0; i<cmd->num_fields; i++)
  780. {
  781. bit_count += cmd->fields[i].num_bits;
  782. }
  783. return bit_count;
  784. }
  785. int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
  786. {
  787. int bit_count = 0;
  788. int i;
  789. bit_count = jtag_scan_size(cmd);
  790. *buffer = malloc(CEIL(bit_count, 8));
  791. bit_count = 0;
  792. for (i = 0; i < cmd->num_fields; i++)
  793. {
  794. if (cmd->fields[i].out_value)
  795. {
  796. #ifdef _DEBUG_JTAG_IO_
  797. char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
  798. #endif
  799. buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
  800. #ifdef _DEBUG_JTAG_IO_
  801. DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
  802. free(char_buf);
  803. #endif
  804. }
  805. bit_count += cmd->fields[i].num_bits;
  806. }
  807. return bit_count;
  808. }
  809. int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
  810. {
  811. int i;
  812. int bit_count = 0;
  813. int retval = ERROR_OK;
  814. for (i=0; i < cmd->num_fields; i++)
  815. {
  816. /* if neither in_value, in_check_value nor in_handler
  817. * are specified we don't have to examine this field
  818. */
  819. if (cmd->fields[i].in_value || cmd->fields[i].in_check_value || cmd->fields[i].in_handler)
  820. {
  821. int num_bits = cmd->fields[i].num_bits;
  822. u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
  823. #ifdef _DEBUG_JTAG_IO_
  824. char *char_buf;
  825. char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
  826. DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
  827. free(char_buf);
  828. #endif
  829. if (cmd->fields[i].in_value)
  830. {
  831. buf_cpy(captured, cmd->fields[i].in_value, num_bits);
  832. if (cmd->fields[i].in_handler)
  833. {
  834. if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv) != ERROR_OK)
  835. {
  836. /* TODO: error reporting */
  837. WARNING("in_handler reported a failed check");
  838. retval = ERROR_JTAG_QUEUE_FAILED;
  839. }
  840. }
  841. }
  842. /* no in_value specified, but a handler takes care of the scanned data */
  843. if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
  844. {
  845. if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv) != ERROR_OK)
  846. {
  847. /* TODO: error reporting */
  848. WARNING("in_handler reported a failed check");
  849. retval = ERROR_JTAG_QUEUE_FAILED;
  850. }
  851. }
  852. if (cmd->fields[i].in_check_value)
  853. {
  854. if ((cmd->fields[i].in_check_mask && buf_cmp_mask(captured, cmd->fields[i].in_check_value, cmd->fields[i].in_check_mask, num_bits))
  855. || (!cmd->fields[i].in_check_mask && buf_cmp(captured, cmd->fields[i].in_check_mask, num_bits)))
  856. {
  857. char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
  858. char *in_check_value_char = buf_to_str(cmd->fields[i].in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
  859. char *in_check_mask_char = buf_to_str(cmd->fields[i].in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
  860. /* TODO: error reporting */
  861. 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);
  862. retval = ERROR_JTAG_QUEUE_FAILED;
  863. free(captured_char);
  864. free(in_check_value_char);
  865. free(in_check_mask_char);
  866. }
  867. }
  868. free(captured);
  869. }
  870. bit_count += cmd->fields[i].num_bits;
  871. }
  872. return retval;
  873. }
  874. enum scan_type jtag_scan_type(scan_command_t *cmd)
  875. {
  876. int i;
  877. int type = 0;
  878. for (i=0; i < cmd->num_fields; i++)
  879. {
  880. if (cmd->fields[i].in_check_value || cmd->fields[i].in_value || cmd->fields[i].in_handler)
  881. type |= SCAN_IN;
  882. if (cmd->fields[i].out_value)
  883. type |= SCAN_OUT;
  884. }
  885. return type;
  886. }
  887. int jtag_execute_queue(void)
  888. {
  889. int retval;
  890. retval = jtag->execute_queue();
  891. cmd_queue_free();
  892. jtag_command_queue = NULL;
  893. last_comand_pointer = &jtag_command_queue;
  894. return retval;
  895. }
  896. int jtag_cancel_queue(void)
  897. {
  898. cmd_queue_free();
  899. jtag_command_queue = NULL;
  900. last_comand_pointer = &jtag_command_queue;
  901. return ERROR_OK;
  902. }
  903. int jtag_reset_callback(enum jtag_event event, void *priv)
  904. {
  905. jtag_device_t *device = priv;
  906. DEBUG("");
  907. if (event == JTAG_TRST_ASSERTED)
  908. {
  909. buf_set_ones(device->cur_instr, device->ir_length);
  910. device->bypass = 1;
  911. }
  912. return ERROR_OK;
  913. }
  914. void jtag_sleep(u32 us)
  915. {
  916. usleep(us);
  917. }
  918. int jtag_validate_chain()
  919. {
  920. jtag_device_t *device = jtag_devices;
  921. int total_ir_length = 0;
  922. u8 *ir_test = NULL;
  923. scan_field_t field;
  924. int chain_pos = 0;
  925. while (device)
  926. {
  927. total_ir_length += device->ir_length;
  928. device = device->next;
  929. }
  930. total_ir_length += 2;
  931. ir_test = malloc(CEIL(total_ir_length, 8));
  932. buf_set_ones(ir_test, total_ir_length);
  933. field.device = 0;
  934. field.num_bits = total_ir_length;
  935. field.out_value = ir_test;
  936. field.out_mask = NULL;
  937. field.in_value = ir_test;
  938. field.in_check_value = NULL;
  939. field.in_check_mask = NULL;
  940. field.in_handler = NULL;
  941. field.in_handler_priv = NULL;
  942. jtag_add_plain_ir_scan(1, &field, TAP_TLR);
  943. jtag_execute_queue();
  944. device = jtag_devices;
  945. while (device)
  946. {
  947. if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
  948. {
  949. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  950. ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
  951. free(cbuf);
  952. exit(-1);
  953. }
  954. chain_pos += device->ir_length;
  955. device = device->next;
  956. }
  957. if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
  958. {
  959. char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
  960. ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
  961. free(cbuf);
  962. exit(-1);
  963. }
  964. free(ir_test);
  965. return ERROR_OK;
  966. }
  967. int jtag_register_commands(struct command_context_s *cmd_ctx)
  968. {
  969. register_command(cmd_ctx, NULL, "interface", handle_interface_command,
  970. COMMAND_CONFIG, NULL);
  971. register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
  972. COMMAND_ANY, "set jtag speed (if supported) <speed>");
  973. register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
  974. COMMAND_CONFIG, NULL);
  975. register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
  976. COMMAND_CONFIG, NULL);
  977. register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
  978. COMMAND_CONFIG, NULL);
  979. register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
  980. COMMAND_CONFIG, NULL);
  981. register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
  982. COMMAND_EXEC, "print current scan chain configuration");
  983. register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
  984. COMMAND_EXEC, "finish JTAG operations in <tap_state>");
  985. register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
  986. COMMAND_EXEC, "toggle reset lines <trst> <srst>");
  987. register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
  988. COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
  989. register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
  990. COMMAND_EXEC, "move to current endstate or [tap_state]");
  991. register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
  992. COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
  993. register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
  994. COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
  995. register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
  996. COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
  997. return ERROR_OK;
  998. }
  999. int jtag_init(struct command_context_s *cmd_ctx)
  1000. {
  1001. int i;
  1002. DEBUG("");
  1003. if (jtag_speed == -1)
  1004. jtag_speed = 0;
  1005. if (jtag_interface && (jtag_interface[0] != 0))
  1006. /* configuration var 'jtag_interface' is set, and not empty */
  1007. for (i = 0; jtag_interfaces[i]; i++)
  1008. {
  1009. if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
  1010. {
  1011. jtag_device_t *device;
  1012. device = jtag_devices;
  1013. if (jtag_interfaces[i]->init() != ERROR_OK)
  1014. return ERROR_JTAG_INIT_FAILED;
  1015. jtag = jtag_interfaces[i];
  1016. jtag_ir_scan_size = 0;
  1017. jtag_num_devices = 0;
  1018. while (device != NULL)
  1019. {
  1020. jtag_ir_scan_size += device->ir_length;
  1021. jtag_num_devices++;
  1022. device = device->next;
  1023. }
  1024. jtag_add_statemove(TAP_TLR);
  1025. jtag_execute_queue();
  1026. jtag_validate_chain();
  1027. return ERROR_OK;
  1028. }
  1029. }
  1030. /* no valid interface was found (i.e. the configuration option,
  1031. * didn't match one of the compiled-in interfaces
  1032. */
  1033. ERROR("No valid jtag interface found (%s)", jtag_interface);
  1034. ERROR("compiled-in jtag interfaces:");
  1035. for (i = 0; jtag_interfaces[i]; i++)
  1036. {
  1037. ERROR("%i: %s", i, jtag_interfaces[i]->name);
  1038. }
  1039. jtag = NULL;
  1040. return ERROR_JTAG_INVALID_INTERFACE;
  1041. }
  1042. int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1043. {
  1044. int i;
  1045. /* only if the configuration var isn't overwritten from cmdline */
  1046. if (!jtag_interface)
  1047. {
  1048. if (args[0] && (args[0][0] != 0))
  1049. {
  1050. for (i=0; jtag_interfaces[i]; i++)
  1051. {
  1052. if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
  1053. {
  1054. if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
  1055. exit(-1);
  1056. jtag_interface = jtag_interfaces[i]->name;
  1057. return ERROR_OK;
  1058. }
  1059. }
  1060. }
  1061. /* remember the requested interface name, so we can complain about it later */
  1062. jtag_interface = strdup(args[0]);
  1063. DEBUG("'interface' command didn't specify a valid interface");
  1064. }
  1065. return ERROR_OK;
  1066. }
  1067. int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1068. {
  1069. jtag_device_t **last_device_p = &jtag_devices;
  1070. if (*last_device_p)
  1071. {
  1072. while ((*last_device_p)->next)
  1073. last_device_p = &((*last_device_p)->next);
  1074. last_device_p = &((*last_device_p)->next);
  1075. }
  1076. if (argc < 3)
  1077. return ERROR_OK;
  1078. *last_device_p = malloc(sizeof(jtag_device_t));
  1079. (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
  1080. (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
  1081. buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
  1082. (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
  1083. buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
  1084. (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
  1085. (*last_device_p)->bypass = 1;
  1086. buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
  1087. (*last_device_p)->next = NULL;
  1088. jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
  1089. jtag_num_devices++;
  1090. return ERROR_OK;
  1091. }
  1092. int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1093. {
  1094. jtag_device_t *device = jtag_devices;
  1095. int device_count = 0;
  1096. while (device)
  1097. {
  1098. u32 expected, expected_mask, cur_instr;
  1099. expected = buf_get_u32(device->expected, 0, device->ir_length);
  1100. expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
  1101. cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
  1102. 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);
  1103. device = device->next;
  1104. device_count++;
  1105. }
  1106. return ERROR_OK;
  1107. }
  1108. int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1109. {
  1110. if (argc >= 1)
  1111. {
  1112. if (strcmp(args[0], "none") == 0)
  1113. jtag_reset_config = RESET_NONE;
  1114. else if (strcmp(args[0], "trst_only") == 0)
  1115. jtag_reset_config = RESET_HAS_TRST;
  1116. else if (strcmp(args[0], "srst_only") == 0)
  1117. jtag_reset_config = RESET_HAS_SRST;
  1118. else if (strcmp(args[0], "trst_and_srst") == 0)
  1119. jtag_reset_config = RESET_TRST_AND_SRST;
  1120. else
  1121. {
  1122. ERROR("invalid reset_config argument");
  1123. exit(-1);
  1124. }
  1125. }
  1126. if (argc >= 2)
  1127. {
  1128. if (strcmp(args[1], "srst_pulls_trst") == 0)
  1129. jtag_reset_config |= RESET_SRST_PULLS_TRST;
  1130. else if (strcmp(args[1], "trst_pulls_srst") == 0)
  1131. jtag_reset_config |= RESET_TRST_PULLS_SRST;
  1132. else if (strcmp(args[1], "combined") == 0)
  1133. jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  1134. else if (strcmp(args[1], "separate") == 0)
  1135. jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
  1136. else
  1137. {
  1138. ERROR("invalid reset_config argument");
  1139. exit(-1);
  1140. }
  1141. }
  1142. if (argc >= 3)
  1143. {
  1144. if (strcmp(args[2], "trst_open_drain") == 0)
  1145. jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
  1146. else if (strcmp(args[2], "trst_push_pull") == 0)
  1147. jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
  1148. else
  1149. {
  1150. ERROR("invalid reset_config argument");
  1151. exit(-1);
  1152. }
  1153. }
  1154. if (argc >= 4)
  1155. {
  1156. if (strcmp(args[3], "srst_push_pull") == 0)
  1157. jtag_reset_config |= RESET_SRST_PUSH_PULL;
  1158. else if (strcmp(args[3], "srst_open_drain") == 0)
  1159. jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
  1160. else
  1161. {
  1162. ERROR("invalid reset_config argument");
  1163. exit(-1);
  1164. }
  1165. }
  1166. return ERROR_OK;
  1167. }
  1168. int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1169. {
  1170. if (argc < 1)
  1171. {
  1172. ERROR("jtag_nsrst_delay <ms> command takes one required argument");
  1173. exit(-1);
  1174. }
  1175. else
  1176. {
  1177. jtag_nsrst_delay = strtoul(args[0], NULL, 0);
  1178. }
  1179. return ERROR_OK;
  1180. }
  1181. int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1182. {
  1183. if (argc < 1)
  1184. {
  1185. ERROR("jtag_ntrst_delay <ms> command takes one required argument");
  1186. exit(-1);
  1187. }
  1188. else
  1189. {
  1190. jtag_ntrst_delay = strtoul(args[0], NULL, 0);
  1191. }
  1192. return ERROR_OK;
  1193. }
  1194. int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1195. {
  1196. if (argc == 0)
  1197. command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
  1198. if (argc > 0)
  1199. {
  1200. /* this command can be called during CONFIG,
  1201. * in which case jtag isn't initialized */
  1202. if (jtag)
  1203. jtag->speed(strtoul(args[0], NULL, 0));
  1204. else
  1205. jtag_speed = strtoul(args[0], NULL, 0);
  1206. }
  1207. return ERROR_OK;
  1208. }
  1209. int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1210. {
  1211. enum tap_state state;
  1212. if (argc < 1)
  1213. {
  1214. command_print(cmd_ctx, "usage: endstate <tap_state>");
  1215. return ERROR_OK;
  1216. }
  1217. for (state = 0; state < 16; state++)
  1218. {
  1219. if (strcmp(args[0], tap_state_strings[state]) == 0)
  1220. {
  1221. jtag_add_end_state(state);
  1222. jtag_execute_queue();
  1223. }
  1224. }
  1225. return ERROR_OK;
  1226. }
  1227. int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1228. {
  1229. int trst = -1;
  1230. int srst = -1;
  1231. char *usage = "usage: jtag_reset <trst> <srst>";
  1232. int retval;
  1233. if (argc < 1)
  1234. {
  1235. command_print(cmd_ctx, usage);
  1236. return ERROR_OK;
  1237. }
  1238. if (args[0][0] == '1')
  1239. trst = 1;
  1240. else if (args[0][0] == '0')
  1241. trst = 0;
  1242. else
  1243. {
  1244. command_print(cmd_ctx, usage);
  1245. return ERROR_OK;
  1246. }
  1247. if (args[1][0] == '1')
  1248. srst = 1;
  1249. else if (args[1][0] == '0')
  1250. srst = 0;
  1251. else
  1252. {
  1253. command_print(cmd_ctx, usage);
  1254. return ERROR_OK;
  1255. }
  1256. if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
  1257. {
  1258. switch (retval)
  1259. {
  1260. case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
  1261. command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
  1262. break;
  1263. case ERROR_JTAG_RESET_CANT_SRST:
  1264. command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
  1265. break;
  1266. default:
  1267. command_print(cmd_ctx, "unknown error");
  1268. }
  1269. }
  1270. jtag_execute_queue();
  1271. return ERROR_OK;
  1272. }
  1273. int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1274. {
  1275. if (argc < 1)
  1276. {
  1277. command_print(cmd_ctx, "usage: runtest <num_cycles>");
  1278. return ERROR_OK;
  1279. }
  1280. jtag_add_runtest(strtol(args[0], NULL, 0), -1);
  1281. jtag_execute_queue();
  1282. return ERROR_OK;
  1283. }
  1284. int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1285. {
  1286. enum tap_state state;
  1287. state = -1;
  1288. if (argc == 1)
  1289. {
  1290. for (state = 0; state < 16; state++)
  1291. {
  1292. if (strcmp(args[0], tap_state_strings[state]) == 0)
  1293. {
  1294. break;
  1295. }
  1296. }
  1297. }
  1298. jtag_add_statemove(state);
  1299. jtag_execute_queue();
  1300. return ERROR_OK;
  1301. }
  1302. int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1303. {
  1304. int i;
  1305. scan_field_t *fields;
  1306. if ((argc < 2) || (argc % 2))
  1307. {
  1308. command_print(cmd_ctx, "usage: irscan <device> <instr> [dev2] [instr2] ...");
  1309. return ERROR_OK;
  1310. }
  1311. fields = malloc(sizeof(scan_field_t) * argc / 2);
  1312. for (i = 0; i < argc / 2; i++)
  1313. {
  1314. int device = strtoul(args[i*2], NULL, 0);
  1315. int field_size = jtag_get_device(device)->ir_length;
  1316. fields[i].device = device;
  1317. fields[i].out_value = malloc(CEIL(field_size, 8));
  1318. buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
  1319. fields[i].out_mask = NULL;
  1320. fields[i].in_value = NULL;
  1321. fields[i].in_check_mask = NULL;
  1322. fields[i].in_handler = NULL;
  1323. fields[i].in_handler_priv = NULL;
  1324. }
  1325. jtag_add_ir_scan(argc / 2, fields, -1);
  1326. jtag_execute_queue();
  1327. for (i = 0; i < argc / 2; i++)
  1328. free(fields[i].out_value);
  1329. free (fields);
  1330. return ERROR_OK;
  1331. }
  1332. int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1333. {
  1334. scan_field_t *fields;
  1335. int num_fields = 0;
  1336. int field_count = 0;
  1337. var_t *var;
  1338. int i, j;
  1339. if ((argc < 2) || (argc % 2))
  1340. {
  1341. command_print(cmd_ctx, "usage: drscan <device> <var> [dev2] [var2]");
  1342. return ERROR_OK;
  1343. }
  1344. for (i = 0; i < argc; i+=2)
  1345. {
  1346. var = get_var_by_namenum(args[i+1]);
  1347. if (var)
  1348. {
  1349. num_fields += var->num_fields;
  1350. }
  1351. else
  1352. {
  1353. command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
  1354. return ERROR_OK;
  1355. }
  1356. }
  1357. fields = malloc(sizeof(scan_field_t) * num_fields);
  1358. for (i = 0; i < argc; i+=2)
  1359. {
  1360. var = get_var_by_namenum(args[i+1]);
  1361. for (j = 0; j < var->num_fields; j++)
  1362. {
  1363. fields[field_count].device = strtol(args[i], NULL, 0);
  1364. fields[field_count].num_bits = var->fields[j].num_bits;
  1365. fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
  1366. buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
  1367. fields[field_count].out_mask = NULL;
  1368. fields[field_count].in_value = fields[field_count].out_value;
  1369. fields[field_count].in_check_mask = NULL;
  1370. fields[field_count].in_check_value = NULL;
  1371. fields[field_count].in_handler = field_le_to_host;
  1372. fields[field_count++].in_handler_priv = &(var->fields[j]);
  1373. }
  1374. }
  1375. jtag_add_dr_scan(num_fields, fields, -1);
  1376. jtag_execute_queue();
  1377. for (i = 0; i < argc / 2; i++)
  1378. free(fields[i].out_value);
  1379. free(fields);
  1380. return ERROR_OK;
  1381. }
  1382. int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1383. {
  1384. if (argc == 0)
  1385. {
  1386. command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
  1387. return ERROR_OK;
  1388. }
  1389. if (strcmp(args[0], "enable") == 0)
  1390. {
  1391. jtag_verify_capture_ir = 1;
  1392. }
  1393. else if (strcmp(args[0], "disable") == 0)
  1394. {
  1395. jtag_verify_capture_ir = 0;
  1396. }
  1397. return ERROR_OK;
  1398. }