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.
 
 
 
 
 
 

1452 lines
39 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Simon Qian <SimonQian@SimonQian.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. /* Versaloon is a programming tool for multiple MCUs.
  20. * OpenOCD and MSP430 supports are distributed under GPLv2.
  21. * You can find it at http://www.SimonQian.com/en/Versaloon.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "replacements.h"
  27. #include "jtag.h"
  28. #include <usb.h>
  29. #include <string.h>
  30. #include "log.h"
  31. //#define _VSLLINK_IN_DEBUG_MODE_
  32. /* enable this to view USB communication
  33. */
  34. #if 0
  35. #define _DEBUG_USB_COMMS_
  36. #endif
  37. #ifdef _DEBUG_JTAG_IO_
  38. #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
  39. #else
  40. #define DEBUG_JTAG_IO(expr ...)
  41. #endif
  42. u16 vsllink_vid;
  43. u16 vsllink_pid;
  44. u8 vsllink_bulkout;
  45. u8 vsllink_bulkin;
  46. #define VSLLINK_USB_TIMEOUT 10000
  47. static int VSLLINK_BufferSize = 1024;
  48. /* Global USB buffers */
  49. static int vsllink_usb_out_buffer_idx;
  50. static int vsllink_usb_in_want_length;
  51. static u8* vsllink_usb_in_buffer = NULL;
  52. static u8* vsllink_usb_out_buffer = NULL;
  53. /* Constants for VSLLink command */
  54. #define VSLLINK_CMD_CONN 0x80
  55. #define VSLLINK_CMD_DISCONN 0x81
  56. #define VSLLINK_CMD_SET_SPEED 0x82
  57. #define VSLLINK_CMD_SET_PORT 0x90
  58. #define VSLLINK_CMD_GET_PORT 0x91
  59. #define VSLLINK_CMD_SET_PORTDIR 0x92
  60. #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
  61. #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
  62. #define VSLLINK_CMD_HW_SWDCMD 0xA2
  63. #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
  64. #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
  65. #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
  66. #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
  67. #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
  68. #define JTAG_PINMSK_SRST (1 << 0)
  69. #define JTAG_PINMSK_TRST (1 << 1)
  70. #define JTAG_PINMSK_USR1 (1 << 2)
  71. #define JTAG_PINMSK_USR2 (1 << 3)
  72. #define JTAG_PINMSK_TCK (1 << 4)
  73. #define JTAG_PINMSK_TMS (1 << 5)
  74. #define JTAG_PINMSK_TDI (1 << 6)
  75. #define JTAG_PINMSK_TDO (1 << 7)
  76. #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
  77. /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
  78. * 0: Test-Logic-Reset
  79. * 1: Run-Test/Idle
  80. * 2: Shift-DR
  81. * 3: Pause-DR
  82. * 4: Shift-IR
  83. * 5: Pause-IR
  84. *
  85. * SD->SD and SI->SI have to be caught in interface specific code
  86. */
  87. u8 VSLLINK_tap_move[6][6] =
  88. {
  89. /* TLR RTI SD PD SI PI */
  90. {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
  91. {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
  92. {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
  93. {0xff, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
  94. {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
  95. {0xff, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
  96. };
  97. typedef struct insert_insignificant_operation
  98. {
  99. unsigned char insert_value;
  100. unsigned char insert_position;
  101. }insert_insignificant_operation_t;
  102. insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
  103. {
  104. /* stuff offset */
  105. {/* TLR */
  106. {1, 0,}, /* TLR */
  107. {1, 0,}, /* RTI */
  108. {1, 0,}, /* SD */
  109. {1, 0,}, /* PD */
  110. {1, 0,}, /* SI */
  111. {1, 0,}}, /* PI */
  112. {/* RTI */
  113. {1, 0,}, /* TLR */
  114. {0, 0,}, /* RTI */
  115. {0, 4,}, /* SD */
  116. {0, 7,}, /* PD */
  117. {0, 5,}, /* SI */
  118. {0, 7,}}, /* PI */
  119. {/* SD */
  120. {0, 0,}, /* TLR */
  121. {0, 0,}, /* RTI */
  122. {0, 0,}, /* SD */
  123. {0, 0,}, /* PD */
  124. {0, 0,}, /* SI */
  125. {0, 0,}}, /* PI */
  126. {/* PD */
  127. {0, 0,}, /* TLR */
  128. {0, 0,}, /* RTI */
  129. {0, 0,}, /* SD */
  130. {0, 0,}, /* PD */
  131. {0, 0,}, /* SI */
  132. {0, 0,}}, /* PI */
  133. {/* SI */
  134. {0, 0,}, /* TLR */
  135. {0, 0,}, /* RTI */
  136. {0, 0,}, /* SD */
  137. {0, 0,}, /* PD */
  138. {0, 0,}, /* SI */
  139. {0, 0,}}, /* PI */
  140. {/* PI */
  141. {0, 0,}, /* TLR */
  142. {0, 0,}, /* RTI */
  143. {0, 0,}, /* SD */
  144. {0, 0,}, /* PD */
  145. {0, 0,}, /* SI */
  146. {0, 0,}}, /* PI */
  147. };
  148. u8 VSLLINK_BIT_MSK[8] =
  149. {
  150. 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
  151. };
  152. typedef struct
  153. {
  154. int length; /* Number of bits to read */
  155. int offset;
  156. scan_command_t *command; /* Corresponding scan command */
  157. u8 *buffer;
  158. } pending_scan_result_t;
  159. #define MAX_PENDING_SCAN_RESULTS 256
  160. static int pending_scan_results_length;
  161. static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  162. /* External interface functions */
  163. int vsllink_execute_queue(void);
  164. int vsllink_speed(int speed);
  165. int vsllink_khz(int khz, int *jtag_speed);
  166. int vsllink_speed_div(int jtag_speed, int *khz);
  167. int vsllink_register_commands(struct command_context_s *cmd_ctx);
  168. int vsllink_init(void);
  169. int vsllink_quit(void);
  170. /* CLI command handler functions */
  171. int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  172. int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  173. int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  174. int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  175. /* Queue command functions */
  176. void vsllink_end_state(tap_state_t state);
  177. void vsllink_state_move(void);
  178. void vsllink_path_move(int num_states, tap_state_t *path);
  179. void vsllink_runtest(int num_cycles);
  180. void vsllink_stableclocks(int num_cycles, int tms);
  181. void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
  182. void vsllink_reset(int trst, int srst);
  183. void vsllink_simple_command(u8 command);
  184. /* VSLLink tap buffer functions */
  185. void vsllink_tap_init(void);
  186. int vsllink_tap_execute(void);
  187. void vsllink_tap_ensure_space(int scans, int bytes);
  188. void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
  189. /* VSLLink lowlevel functions */
  190. typedef struct vsllink_jtag
  191. {
  192. struct usb_dev_handle* usb_handle;
  193. } vsllink_jtag_t;
  194. vsllink_jtag_t *vsllink_usb_open(void);
  195. void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
  196. int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
  197. int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
  198. int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
  199. void vsllink_debug_buffer(u8 *buffer, int length);
  200. static int vsllink_tms_data_len = 0;
  201. static u8* vsllink_tms_cmd_pos;
  202. vsllink_jtag_t* vsllink_jtag_handle;
  203. /***************************************************************************/
  204. /* External interface implementation */
  205. jtag_interface_t vsllink_interface =
  206. {
  207. .name = "vsllink",
  208. .execute_queue = vsllink_execute_queue,
  209. .speed = vsllink_speed,
  210. .khz = vsllink_khz,
  211. .speed_div = vsllink_speed_div,
  212. .register_commands = vsllink_register_commands,
  213. .init = vsllink_init,
  214. .quit = vsllink_quit
  215. };
  216. int vsllink_execute_queue(void)
  217. {
  218. jtag_command_t *cmd = jtag_command_queue;
  219. int scan_size;
  220. enum scan_type type;
  221. u8 *buffer;
  222. DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
  223. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  224. vsllink_usb_out_buffer_idx = 3;
  225. while (cmd != NULL)
  226. {
  227. switch (cmd->type)
  228. {
  229. case JTAG_END_STATE:
  230. DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd->cmd.end_state->end_state));
  231. if (cmd->cmd.end_state->end_state != -1)
  232. {
  233. vsllink_end_state(cmd->cmd.end_state->end_state);
  234. }
  235. break;
  236. case JTAG_RUNTEST:
  237. DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
  238. tap_state_name(cmd->cmd.runtest->end_state));
  239. if (cmd->cmd.runtest->end_state != -1)
  240. {
  241. vsllink_end_state(cmd->cmd.runtest->end_state);
  242. }
  243. vsllink_runtest(cmd->cmd.runtest->num_cycles);
  244. break;
  245. case JTAG_STATEMOVE:
  246. DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
  247. if (cmd->cmd.statemove->end_state != -1)
  248. {
  249. vsllink_end_state(cmd->cmd.statemove->end_state);
  250. }
  251. vsllink_state_move();
  252. break;
  253. case JTAG_PATHMOVE:
  254. DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
  255. cmd->cmd.pathmove->num_states, \
  256. tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
  257. vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  258. break;
  259. case JTAG_SCAN:
  260. if (cmd->cmd.scan->end_state != -1)
  261. {
  262. vsllink_end_state(cmd->cmd.scan->end_state);
  263. }
  264. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  265. if (cmd->cmd.scan->ir_scan)
  266. {
  267. DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
  268. }
  269. else
  270. {
  271. DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
  272. }
  273. #ifdef _DEBUG_JTAG_IO_
  274. vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
  275. #endif
  276. type = jtag_scan_type(cmd->cmd.scan);
  277. vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
  278. break;
  279. case JTAG_RESET:
  280. DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  281. vsllink_tap_execute();
  282. if (cmd->cmd.reset->trst == 1)
  283. {
  284. tap_set_state(TAP_RESET);
  285. }
  286. vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  287. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  288. vsllink_usb_out_buffer_idx = 3;
  289. break;
  290. case JTAG_SLEEP:
  291. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  292. vsllink_tap_execute();
  293. jtag_sleep(cmd->cmd.sleep->us);
  294. break;
  295. case JTAG_STABLECLOCKS:
  296. DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
  297. switch(tap_get_state())
  298. {
  299. case TAP_RESET:
  300. // tms should be '1' to stay in TAP_RESET mode
  301. scan_size = 1;
  302. break;
  303. case TAP_DRSHIFT:
  304. case TAP_IDLE:
  305. case TAP_DRPAUSE:
  306. case TAP_IRSHIFT:
  307. case TAP_IRPAUSE:
  308. // in other mode, tms should be '0'
  309. scan_size = 0;
  310. break; /* above stable states are OK */
  311. default:
  312. LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
  313. tap_state_name(tap_get_state()) );
  314. exit(-1);
  315. }
  316. vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
  317. break;
  318. default:
  319. LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
  320. exit(-1);
  321. }
  322. cmd = cmd->next;
  323. }
  324. return vsllink_tap_execute();
  325. }
  326. int vsllink_speed(int speed)
  327. {
  328. int result;
  329. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
  330. vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
  331. vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
  332. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  333. if (result == 3)
  334. {
  335. return ERROR_OK;
  336. }
  337. else
  338. {
  339. LOG_ERROR("VSLLink setting speed failed (%d)", result);
  340. return ERROR_JTAG_DEVICE_ERROR;
  341. }
  342. return ERROR_OK;
  343. }
  344. int vsllink_khz(int khz, int *jtag_speed)
  345. {
  346. *jtag_speed = khz;
  347. return ERROR_OK;
  348. }
  349. int vsllink_speed_div(int jtag_speed, int *khz)
  350. {
  351. *khz = jtag_speed;
  352. return ERROR_OK;
  353. }
  354. int vsllink_register_commands(struct command_context_s *cmd_ctx)
  355. {
  356. register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
  357. COMMAND_CONFIG, NULL);
  358. register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
  359. COMMAND_CONFIG, NULL);
  360. register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
  361. COMMAND_CONFIG, NULL);
  362. register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
  363. COMMAND_CONFIG, NULL);
  364. return ERROR_OK;
  365. }
  366. int vsllink_init(void)
  367. {
  368. int check_cnt;
  369. int result;
  370. char version_str[100];
  371. vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
  372. vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
  373. if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
  374. {
  375. LOG_ERROR("Not enough memory");
  376. exit(-1);
  377. }
  378. vsllink_jtag_handle = vsllink_usb_open();
  379. if (vsllink_jtag_handle == 0)
  380. {
  381. LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
  382. return ERROR_JTAG_INIT_FAILED;
  383. }
  384. check_cnt = 0;
  385. while (check_cnt < 3)
  386. {
  387. vsllink_simple_command(VSLLINK_CMD_CONN);
  388. result = vsllink_usb_read(vsllink_jtag_handle);
  389. if (result > 2)
  390. {
  391. vsllink_usb_in_buffer[result] = 0;
  392. VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
  393. strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
  394. LOG_INFO(version_str);
  395. // free the pre-alloc memroy
  396. free(vsllink_usb_in_buffer);
  397. free(vsllink_usb_out_buffer);
  398. vsllink_usb_in_buffer = NULL;
  399. vsllink_usb_out_buffer = NULL;
  400. // alloc new memory
  401. vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
  402. vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
  403. if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
  404. {
  405. LOG_ERROR("Not enough memory");
  406. exit(-1);
  407. }
  408. else
  409. {
  410. LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
  411. }
  412. break;
  413. }
  414. vsllink_simple_command(VSLLINK_CMD_DISCONN);
  415. check_cnt++;
  416. }
  417. if (check_cnt == 3)
  418. {
  419. // It's dangerout to proced
  420. LOG_ERROR("VSLLink initial failed");
  421. exit(-1);
  422. }
  423. // Set SRST and TRST to output, Set USR1 and USR2 to input
  424. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
  425. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
  426. vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
  427. if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
  428. {
  429. LOG_ERROR("VSLLink USB send data error");
  430. exit(-1);
  431. }
  432. vsllink_reset(0, 0);
  433. LOG_INFO("VSLLink JTAG Interface ready");
  434. vsllink_tap_init();
  435. return ERROR_OK;
  436. }
  437. int vsllink_quit(void)
  438. {
  439. if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
  440. {
  441. // Set all pins to input
  442. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
  443. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
  444. vsllink_usb_out_buffer[2] = 0;
  445. if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
  446. {
  447. LOG_ERROR("VSLLink USB send data error");
  448. exit(-1);
  449. }
  450. // disconnect
  451. vsllink_simple_command(VSLLINK_CMD_DISCONN);
  452. vsllink_usb_close(vsllink_jtag_handle);
  453. }
  454. if (vsllink_usb_in_buffer != NULL)
  455. {
  456. free(vsllink_usb_in_buffer);
  457. }
  458. if (vsllink_usb_out_buffer != NULL)
  459. {
  460. free(vsllink_usb_out_buffer);
  461. }
  462. return ERROR_OK;
  463. }
  464. // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
  465. // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
  466. void vsllink_append_tms(void)
  467. {
  468. u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
  469. u16 tms2;
  470. tap_state_t end_state = tap_get_end_state();
  471. if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
  472. (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
  473. (vsllink_tms_cmd_pos == NULL))
  474. {
  475. LOG_ERROR("There MUST be some bugs in the driver");
  476. exit(-1);
  477. }
  478. tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position]) << \
  479. vsllink_tms_data_len;
  480. if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_value == 1)
  481. {
  482. tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
  483. (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position);
  484. }
  485. tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position) << \
  486. (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position);
  487. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
  488. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
  489. vsllink_tms_data_len = 0;
  490. vsllink_tms_cmd_pos = NULL;
  491. }
  492. /***************************************************************************/
  493. /* Queue command implementations */
  494. void vsllink_end_state(tap_state_t state)
  495. {
  496. if (tap_is_state_stable(state))
  497. {
  498. tap_set_end_state(state);
  499. }
  500. else
  501. {
  502. LOG_ERROR("BUG: %i is not a valid end state", state);
  503. exit(-1);
  504. }
  505. }
  506. /* Goes to the end state. */
  507. void vsllink_state_move(void)
  508. {
  509. if (vsllink_tms_data_len > 0)
  510. {
  511. vsllink_append_tms();
  512. }
  513. else
  514. {
  515. vsllink_tap_ensure_space(0, 2);
  516. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
  517. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
  518. }
  519. tap_set_state(tap_get_end_state());
  520. }
  521. // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
  522. void vsllink_add_path(int start, int num, tap_state_t *path)
  523. {
  524. int i;
  525. for (i = start; i < (start + num); i++)
  526. {
  527. if ((i & 7) == 0)
  528. {
  529. if (i > 0)
  530. {
  531. vsllink_usb_out_buffer_idx++;
  532. }
  533. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  534. }
  535. if (path[i - start] == tap_state_transition(tap_get_state(), TRUE))
  536. {
  537. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
  538. }
  539. else if (path[i - start] == tap_state_transition(tap_get_state(), FALSE))
  540. {
  541. // nothing to do
  542. }
  543. else
  544. {
  545. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
  546. exit(-1);
  547. }
  548. tap_set_state(path[i - start]);
  549. }
  550. if ((i > 0) && ((i & 7) == 0))
  551. {
  552. vsllink_usb_out_buffer_idx++;
  553. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  554. }
  555. tap_set_end_state(tap_get_state());
  556. }
  557. void vsllink_path_move(int num_states, tap_state_t *path)
  558. {
  559. int i, tms_len, tms_cmd_pos, path_idx = 0;
  560. if (vsllink_tms_data_len > 0)
  561. {
  562. // there are vsllink_tms_data_len more tms bits to be shifted
  563. // so there are vsllink_tms_data_len + num_states tms bits in all
  564. tms_len = vsllink_tms_data_len + num_states;
  565. if (tms_len <= 16)
  566. {
  567. // merge into last tms shift
  568. if (tms_len < 8)
  569. {
  570. // just append tms data to the last tms byte
  571. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  572. }
  573. else if (tms_len == 8)
  574. {
  575. // end last tms shift command
  576. (*vsllink_tms_cmd_pos)--;
  577. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  578. }
  579. else if (tms_len < 16)
  580. {
  581. if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
  582. {
  583. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  584. // there is enought tms length in the current tms shift command
  585. (*vsllink_tms_cmd_pos)++;
  586. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  587. }
  588. else
  589. {
  590. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  591. // not enough tms length in the current tms shift command
  592. // so a new command should be added
  593. // first decrease byte length of last tms shift command
  594. (*vsllink_tms_cmd_pos)--;
  595. // append tms data to the last tms byte
  596. vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
  597. path += 8 - vsllink_tms_data_len;
  598. // add new command(3 bytes)
  599. vsllink_tap_ensure_space(0, 3);
  600. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  601. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  602. vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
  603. }
  604. }
  605. else if (tms_len == 16)
  606. {
  607. // end last tms shift command
  608. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  609. }
  610. vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
  611. if (vsllink_tms_data_len == 0)
  612. {
  613. vsllink_tms_cmd_pos = NULL;
  614. }
  615. num_states = 0;
  616. }
  617. else
  618. {
  619. vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
  620. path += 16 - vsllink_tms_data_len;
  621. num_states -= 16 - vsllink_tms_data_len;
  622. vsllink_tms_data_len = 0;
  623. vsllink_tms_cmd_pos = NULL;
  624. }
  625. }
  626. if (num_states > 0)
  627. {
  628. // Normal operation, don't need to append tms data
  629. vsllink_tms_data_len = num_states & 7;
  630. while (num_states > 0)
  631. {
  632. if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
  633. {
  634. i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
  635. }
  636. else
  637. {
  638. i = num_states;
  639. }
  640. tms_len = (i + 7) >> 3;
  641. vsllink_tap_ensure_space(0, tms_len + 2);
  642. tms_cmd_pos = vsllink_usb_out_buffer_idx;
  643. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
  644. vsllink_add_path(0, i, path + path_idx);
  645. path_idx += i;
  646. num_states -= i;
  647. }
  648. if (vsllink_tms_data_len > 0)
  649. {
  650. if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
  651. {
  652. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
  653. (*vsllink_tms_cmd_pos)++;
  654. }
  655. else
  656. {
  657. vsllink_usb_out_buffer[tms_cmd_pos]--;
  658. tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  659. vsllink_tap_ensure_space(0, 3);
  660. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  661. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  662. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
  663. }
  664. }
  665. }
  666. }
  667. void vsllink_stableclocks(int num_cycles, int tms)
  668. {
  669. int tms_len;
  670. u16 tms_append_byte;
  671. if (vsllink_tms_data_len > 0)
  672. {
  673. // there are vsllink_tms_data_len more tms bits to be shifted
  674. // so there are vsllink_tms_data_len + num_cycles tms bits in all
  675. tms_len = vsllink_tms_data_len + num_cycles;
  676. if (tms > 0)
  677. {
  678. // append '1' for tms
  679. tms_append_byte = (u16)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
  680. }
  681. else
  682. {
  683. // append '0' for tms
  684. tms_append_byte = 0;
  685. }
  686. if (tms_len <= 16)
  687. {
  688. // merge into last tms shift
  689. if (tms_len < 8)
  690. {
  691. // just add to vsllink_tms_data_len
  692. // same result if tun through
  693. //vsllink_tms_data_len += num_cycles;
  694. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (u8)(tms_append_byte & 0xFF);
  695. }
  696. else if (tms_len == 8)
  697. {
  698. // end last tms shift command
  699. // just reduce it, and append last tms byte
  700. (*vsllink_tms_cmd_pos)--;
  701. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
  702. }
  703. else if (tms_len < 16)
  704. {
  705. if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
  706. {
  707. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  708. // there is enought tms length in the current tms shift command
  709. // increase the tms byte length by 1 and set the last byte to 0
  710. (*vsllink_tms_cmd_pos)++;
  711. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
  712. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
  713. }
  714. else
  715. {
  716. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  717. // not enough tms length in the current tms shift command
  718. // so a new command should be added
  719. // first decrease byte length of last tms shift command
  720. (*vsllink_tms_cmd_pos)--;
  721. // append last tms byte and move the command pointer to the next empty position
  722. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
  723. // add new command(3 bytes)
  724. vsllink_tap_ensure_space(0, 3);
  725. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  726. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  727. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
  728. }
  729. }
  730. else if (tms_len == 16)
  731. {
  732. // end last tms shift command
  733. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
  734. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
  735. }
  736. vsllink_tms_data_len = tms_len & 7;
  737. if (vsllink_tms_data_len == 0)
  738. {
  739. vsllink_tms_cmd_pos = NULL;
  740. }
  741. num_cycles = 0;
  742. }
  743. else
  744. {
  745. // more shifts will be needed
  746. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
  747. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
  748. num_cycles -= 16 - vsllink_tms_data_len;
  749. vsllink_tms_data_len = 0;
  750. vsllink_tms_cmd_pos = NULL;
  751. }
  752. }
  753. // from here vsllink_tms_data_len == 0 or num_cycles == 0
  754. if (vsllink_tms_data_len > 0)
  755. {
  756. // num_cycles == 0
  757. // no need to shift
  758. if (num_cycles > 0)
  759. {
  760. LOG_ERROR("There MUST be some bugs in the driver");
  761. exit(-1);
  762. }
  763. }
  764. else
  765. {
  766. // get number of bytes left to be sent
  767. tms_len = num_cycles >> 3;
  768. if (tms_len > 0)
  769. {
  770. vsllink_tap_ensure_space(1, 5);
  771. // if tms_len > 0, vsllink_tms_data_len == 0
  772. // so just add new command
  773. // LSB of the command byte is the tms value when do the shifting
  774. if (tms > 0)
  775. {
  776. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
  777. }
  778. else
  779. {
  780. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
  781. }
  782. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
  783. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
  784. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
  785. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
  786. vsllink_usb_in_want_length += 1;
  787. pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
  788. pending_scan_results_length++;
  789. if (tms_len > 0xFFFF)
  790. {
  791. vsllink_tap_execute();
  792. }
  793. }
  794. // post-process
  795. vsllink_tms_data_len = num_cycles & 7;
  796. if (vsllink_tms_data_len > 0)
  797. {
  798. vsllink_tap_ensure_space(0, 3);
  799. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  800. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  801. if (tms > 0)
  802. {
  803. // append '1' for tms
  804. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
  805. }
  806. else
  807. {
  808. // append '0' for tms
  809. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
  810. }
  811. }
  812. }
  813. }
  814. void vsllink_runtest(int num_cycles)
  815. {
  816. tap_state_t saved_end_state = tap_get_end_state();
  817. if (tap_get_state() != TAP_IDLE)
  818. {
  819. // enter into IDLE state
  820. vsllink_end_state(TAP_IDLE);
  821. vsllink_state_move();
  822. }
  823. vsllink_stableclocks(num_cycles, 0);
  824. // post-process
  825. // set end_state
  826. vsllink_end_state(saved_end_state);
  827. tap_set_state(TAP_IDLE);
  828. if (tap_get_end_state() != TAP_IDLE)
  829. {
  830. vsllink_state_move();
  831. }
  832. }
  833. void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
  834. {
  835. tap_state_t saved_end_state;
  836. u8 bits_left, tms_tmp, tdi_len;
  837. int i;
  838. if (0 == scan_size )
  839. {
  840. return;
  841. }
  842. tdi_len = ((scan_size + 7) >> 3);
  843. if ((tdi_len + 7) > VSLLINK_BufferSize)
  844. {
  845. LOG_ERROR("Your implementation of VSLLink has not enough buffer");
  846. exit(-1);
  847. }
  848. saved_end_state = tap_get_end_state();
  849. /* Move to appropriate scan state */
  850. vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  851. if (vsllink_tms_data_len > 0)
  852. {
  853. if (tap_get_state() == tap_get_end_state())
  854. {
  855. // already in IRSHIFT or DRSHIFT state
  856. // merge tms data in the last tms shift command into next scan command
  857. if(*vsllink_tms_cmd_pos < 1)
  858. {
  859. LOG_ERROR("There MUST be some bugs in the driver");
  860. exit(-1);
  861. }
  862. else if(*vsllink_tms_cmd_pos < 2)
  863. {
  864. tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  865. vsllink_usb_out_buffer_idx--;
  866. }
  867. else
  868. {
  869. tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  870. *vsllink_tms_cmd_pos -= 2;
  871. }
  872. vsllink_tap_ensure_space(1, tdi_len + 7);
  873. // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
  874. // which is merged from the last tms shift command
  875. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
  876. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
  877. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
  878. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
  879. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
  880. for (i = 0; i < tdi_len; i++)
  881. {
  882. buffer[i] >>= 8 - vsllink_tms_data_len;
  883. if (i != tdi_len)
  884. {
  885. buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
  886. }
  887. }
  888. vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
  889. scan_size -= 8 - vsllink_tms_data_len;
  890. vsllink_tms_data_len = 0;
  891. }
  892. else
  893. {
  894. vsllink_append_tms();
  895. vsllink_tap_ensure_space(1, tdi_len + 5);
  896. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
  897. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
  898. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
  899. vsllink_tap_append_scan(scan_size, buffer, command, 0);
  900. }
  901. }
  902. else
  903. {
  904. vsllink_tap_ensure_space(1, tdi_len + 7);
  905. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
  906. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
  907. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
  908. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
  909. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  910. vsllink_tap_append_scan(scan_size, buffer, command, 8);
  911. }
  912. vsllink_end_state(saved_end_state);
  913. bits_left = scan_size & 0x07;
  914. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  915. if (bits_left > 0)
  916. {
  917. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
  918. }
  919. else
  920. {
  921. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
  922. }
  923. if (tap_get_state() != tap_get_end_state())
  924. {
  925. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
  926. }
  927. else
  928. {
  929. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  930. }
  931. tap_set_state(tap_get_end_state());
  932. }
  933. void vsllink_reset(int trst, int srst)
  934. {
  935. int result;
  936. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  937. /* Signals are active low */
  938. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
  939. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
  940. vsllink_usb_out_buffer[2] = 0;
  941. if (srst == 0)
  942. {
  943. vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
  944. }
  945. if (trst == 0)
  946. {
  947. vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
  948. }
  949. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  950. if (result != 3)
  951. {
  952. LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
  953. }
  954. }
  955. void vsllink_simple_command(u8 command)
  956. {
  957. int result;
  958. DEBUG_JTAG_IO("0x%02x", command);
  959. vsllink_usb_out_buffer[0] = command;
  960. result = vsllink_usb_write(vsllink_jtag_handle, 1);
  961. if (result != 1)
  962. {
  963. LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
  964. }
  965. }
  966. int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  967. {
  968. if (argc != 1) {
  969. LOG_ERROR("parameter error, should be one parameter for VID");
  970. return ERROR_OK;
  971. }
  972. vsllink_vid = strtol(args[0], NULL, 0);
  973. return ERROR_OK;
  974. }
  975. int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  976. {
  977. if (argc != 1) {
  978. LOG_ERROR("parameter error, should be one parameter for PID");
  979. return ERROR_OK;
  980. }
  981. vsllink_pid = strtol(args[0], NULL, 0);
  982. return ERROR_OK;
  983. }
  984. int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  985. {
  986. if (argc != 1) {
  987. LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
  988. return ERROR_OK;
  989. }
  990. vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
  991. return ERROR_OK;
  992. }
  993. int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  994. {
  995. if (argc != 1) {
  996. LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
  997. return ERROR_OK;
  998. }
  999. vsllink_bulkout = strtol(args[0], NULL, 0);
  1000. return ERROR_OK;
  1001. }
  1002. /***************************************************************************/
  1003. /* VSLLink tap functions */
  1004. void vsllink_tap_init(void)
  1005. {
  1006. vsllink_usb_out_buffer_idx = 0;
  1007. vsllink_usb_in_want_length = 0;
  1008. pending_scan_results_length = 0;
  1009. }
  1010. void vsllink_tap_ensure_space(int scans, int bytes)
  1011. {
  1012. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  1013. int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
  1014. if (scans > available_scans || bytes > available_bytes)
  1015. {
  1016. vsllink_tap_execute();
  1017. }
  1018. }
  1019. void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
  1020. {
  1021. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
  1022. int i;
  1023. if (offset > 0)
  1024. {
  1025. vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
  1026. }
  1027. else
  1028. {
  1029. vsllink_usb_in_want_length += (length + 7) >> 3;
  1030. }
  1031. pending_scan_result->length = length;
  1032. pending_scan_result->offset = offset;
  1033. pending_scan_result->command = command;
  1034. pending_scan_result->buffer = buffer;
  1035. for (i = 0; i < ((length + 7) >> 3); i++)
  1036. {
  1037. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
  1038. }
  1039. pending_scan_results_length++;
  1040. }
  1041. /* Pad and send a tap sequence to the device, and receive the answer.
  1042. * For the purpose of padding we assume that we are in reset or idle or pause state. */
  1043. int vsllink_tap_execute(void)
  1044. {
  1045. int i;
  1046. int result;
  1047. int first = 0;
  1048. if (vsllink_tms_data_len > 0)
  1049. {
  1050. if((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
  1051. {
  1052. LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
  1053. }
  1054. if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
  1055. {
  1056. // last tms bit is '1'
  1057. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
  1058. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
  1059. vsllink_tms_data_len = 0;
  1060. }
  1061. else
  1062. {
  1063. // last tms bit is '0'
  1064. vsllink_usb_out_buffer_idx++;
  1065. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  1066. vsllink_tms_data_len = 0;
  1067. }
  1068. }
  1069. if (vsllink_usb_out_buffer_idx > 3)
  1070. {
  1071. if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
  1072. {
  1073. vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
  1074. vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
  1075. }
  1076. result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
  1077. if (result == vsllink_usb_in_want_length)
  1078. {
  1079. for (i = 0; i < pending_scan_results_length; i++)
  1080. {
  1081. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
  1082. u8 *buffer = pending_scan_result->buffer;
  1083. int length = pending_scan_result->length;
  1084. int offset = pending_scan_result->offset;
  1085. scan_command_t *command = pending_scan_result->command;
  1086. if (buffer != NULL)
  1087. {
  1088. // IRSHIFT or DRSHIFT
  1089. buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
  1090. first += (length + offset + 7) >> 3;
  1091. DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
  1092. #ifdef _DEBUG_JTAG_IO_
  1093. vsllink_debug_buffer(buffer, (length + 7) >> 3);
  1094. #endif
  1095. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  1096. {
  1097. vsllink_tap_init();
  1098. return ERROR_JTAG_QUEUE_FAILED;
  1099. }
  1100. free(pending_scan_result->buffer);
  1101. pending_scan_result->buffer = NULL;
  1102. }
  1103. else
  1104. {
  1105. first++;
  1106. }
  1107. }
  1108. }
  1109. else
  1110. {
  1111. LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
  1112. return ERROR_JTAG_QUEUE_FAILED;
  1113. }
  1114. vsllink_tap_init();
  1115. }
  1116. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  1117. vsllink_usb_out_buffer_idx = 3;
  1118. return ERROR_OK;
  1119. }
  1120. /*****************************************************************************/
  1121. /* VSLLink USB low-level functions */
  1122. vsllink_jtag_t* vsllink_usb_open(void)
  1123. {
  1124. struct usb_bus *busses;
  1125. struct usb_bus *bus;
  1126. struct usb_device *dev;
  1127. vsllink_jtag_t *result;
  1128. result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
  1129. usb_init();
  1130. usb_find_busses();
  1131. usb_find_devices();
  1132. busses = usb_get_busses();
  1133. /* find vsllink_jtag device in usb bus */
  1134. for (bus = busses; bus; bus = bus->next)
  1135. {
  1136. for (dev = bus->devices; dev; dev = dev->next)
  1137. {
  1138. if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
  1139. {
  1140. result->usb_handle = usb_open(dev);
  1141. /* usb_set_configuration required under win32 */
  1142. usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
  1143. usb_claim_interface(result->usb_handle, 0);
  1144. #if 0
  1145. /*
  1146. * This makes problems under Mac OS X. And is not needed
  1147. * under Windows. Hopefully this will not break a linux build
  1148. */
  1149. usb_set_altinterface(result->usb_handle, 0);
  1150. #endif
  1151. return result;
  1152. }
  1153. }
  1154. }
  1155. free(result);
  1156. return NULL;
  1157. }
  1158. void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
  1159. {
  1160. usb_close(vsllink_jtag->usb_handle);
  1161. free(vsllink_jtag);
  1162. }
  1163. /* Send a message and receive the reply. */
  1164. int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
  1165. {
  1166. int result;
  1167. result = vsllink_usb_write(vsllink_jtag, out_length);
  1168. if (result == out_length)
  1169. {
  1170. if (in_length > 0)
  1171. {
  1172. result = vsllink_usb_read(vsllink_jtag);
  1173. if (result == in_length )
  1174. {
  1175. return result;
  1176. }
  1177. else
  1178. {
  1179. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
  1180. return -1;
  1181. }
  1182. }
  1183. return 0;
  1184. }
  1185. else
  1186. {
  1187. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
  1188. return -1;
  1189. }
  1190. }
  1191. /* Write data from out_buffer to USB. */
  1192. int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
  1193. {
  1194. int result;
  1195. if (out_length > VSLLINK_BufferSize)
  1196. {
  1197. LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
  1198. return -1;
  1199. }
  1200. result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
  1201. (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
  1202. DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
  1203. #ifdef _DEBUG_USB_COMMS_
  1204. LOG_DEBUG("USB out:");
  1205. vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
  1206. #endif
  1207. #ifdef _VSLLINK_IN_DEBUG_MODE_
  1208. usleep(100000);
  1209. #endif
  1210. return result;
  1211. }
  1212. /* Read data from USB into in_buffer. */
  1213. int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
  1214. {
  1215. int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
  1216. (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
  1217. DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
  1218. #ifdef _DEBUG_USB_COMMS_
  1219. LOG_DEBUG("USB in:");
  1220. vsllink_debug_buffer(vsllink_usb_in_buffer, result);
  1221. #endif
  1222. return result;
  1223. }
  1224. #define BYTES_PER_LINE 16
  1225. void vsllink_debug_buffer(u8 *buffer, int length)
  1226. {
  1227. char line[81];
  1228. char s[4];
  1229. int i;
  1230. int j;
  1231. for (i = 0; i < length; i += BYTES_PER_LINE)
  1232. {
  1233. snprintf(line, 5, "%04x", i);
  1234. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  1235. {
  1236. snprintf(s, 4, " %02x", buffer[j]);
  1237. strcat(line, s);
  1238. }
  1239. LOG_DEBUG(line);
  1240. }
  1241. }