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.
 
 
 
 
 
 

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