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.
 
 
 
 
 
 

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