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.
 
 
 
 
 
 

1393 lines
38 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 5000
  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_TMS0BYTE 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_map[from]][tap_move_map[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(enum tap_state state);
  177. void vsllink_state_move(void);
  178. void vsllink_path_move(int num_states, enum tap_state *path);
  179. void vsllink_runtest(int num_cycles);
  180. void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
  181. void vsllink_reset(int trst, int srst);
  182. void vsllink_simple_command(u8 command);
  183. /* VSLLink tap buffer functions */
  184. void vsllink_tap_init(void);
  185. int vsllink_tap_execute(void);
  186. void vsllink_tap_ensure_space(int scans, int bytes);
  187. void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
  188. /* VSLLink lowlevel functions */
  189. typedef struct vsllink_jtag
  190. {
  191. struct usb_dev_handle* usb_handle;
  192. } vsllink_jtag_t;
  193. vsllink_jtag_t *vsllink_usb_open(void);
  194. void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
  195. int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
  196. int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
  197. int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
  198. void vsllink_debug_buffer(u8 *buffer, int length);
  199. static int vsllink_tms_data_len = 0;
  200. static u8* vsllink_tms_cmd_pos;
  201. vsllink_jtag_t* vsllink_jtag_handle;
  202. /***************************************************************************/
  203. /* External interface implementation */
  204. jtag_interface_t vsllink_interface =
  205. {
  206. .name = "vsllink",
  207. .execute_queue = vsllink_execute_queue,
  208. .speed = vsllink_speed,
  209. .khz = vsllink_khz,
  210. .speed_div = vsllink_speed_div,
  211. .register_commands = vsllink_register_commands,
  212. .init = vsllink_init,
  213. .quit = vsllink_quit
  214. };
  215. int vsllink_execute_queue(void)
  216. {
  217. jtag_command_t *cmd = jtag_command_queue;
  218. int scan_size;
  219. enum scan_type type;
  220. u8 *buffer;
  221. DEBUG_JTAG_IO("--------------------------------------------------------------------------------");
  222. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  223. vsllink_usb_out_buffer_idx = 3;
  224. while (cmd != NULL)
  225. {
  226. switch (cmd->type)
  227. {
  228. case JTAG_END_STATE:
  229. DEBUG_JTAG_IO("end_state: %s", jtag_state_name(cmd->cmd.end_state->end_state));
  230. if (cmd->cmd.end_state->end_state != -1)
  231. {
  232. vsllink_end_state(cmd->cmd.end_state->end_state);
  233. }
  234. break;
  235. case JTAG_RUNTEST:
  236. DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
  237. jtag_state_name(cmd->cmd.runtest->end_state));
  238. if (cmd->cmd.runtest->end_state != -1)
  239. {
  240. vsllink_end_state(cmd->cmd.runtest->end_state);
  241. }
  242. vsllink_runtest(cmd->cmd.runtest->num_cycles);
  243. break;
  244. case JTAG_STATEMOVE:
  245. DEBUG_JTAG_IO("statemove end in %s", jtag_state_name(cmd->cmd.statemove->end_state));
  246. if (cmd->cmd.statemove->end_state != -1)
  247. {
  248. vsllink_end_state(cmd->cmd.statemove->end_state);
  249. }
  250. vsllink_state_move();
  251. break;
  252. case JTAG_PATHMOVE:
  253. DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
  254. cmd->cmd.pathmove->num_states, \
  255. jtag_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
  256. vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  257. break;
  258. case JTAG_SCAN:
  259. if (cmd->cmd.scan->end_state != -1)
  260. {
  261. vsllink_end_state(cmd->cmd.scan->end_state);
  262. }
  263. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  264. if (cmd->cmd.scan->ir_scan)
  265. {
  266. DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, jtag_state_name(cmd->cmd.scan->end_state));
  267. }
  268. else
  269. {
  270. DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, jtag_state_name(cmd->cmd.scan->end_state));
  271. }
  272. #ifdef _DEBUG_JTAG_IO_
  273. vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
  274. #endif
  275. type = jtag_scan_type(cmd->cmd.scan);
  276. vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
  277. break;
  278. case JTAG_RESET:
  279. DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  280. vsllink_tap_execute();
  281. if (cmd->cmd.reset->trst == 1)
  282. {
  283. cur_state = TAP_RESET;
  284. }
  285. vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  286. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  287. vsllink_usb_out_buffer_idx = 3;
  288. break;
  289. case JTAG_SLEEP:
  290. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  291. vsllink_tap_execute();
  292. jtag_sleep(cmd->cmd.sleep->us);
  293. break;
  294. default:
  295. LOG_ERROR("BUG: unknown JTAG command type encountered");
  296. exit(-1);
  297. }
  298. cmd = cmd->next;
  299. }
  300. return vsllink_tap_execute();
  301. }
  302. int vsllink_speed(int speed)
  303. {
  304. int result;
  305. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
  306. vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
  307. vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
  308. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  309. if (result == 3)
  310. {
  311. return ERROR_OK;
  312. }
  313. else
  314. {
  315. LOG_ERROR("VSLLink setting speed failed (%d)", result);
  316. return ERROR_JTAG_DEVICE_ERROR;
  317. }
  318. return ERROR_OK;
  319. }
  320. int vsllink_khz(int khz, int *jtag_speed)
  321. {
  322. *jtag_speed = khz;
  323. return ERROR_OK;
  324. }
  325. int vsllink_speed_div(int jtag_speed, int *khz)
  326. {
  327. *khz = jtag_speed;
  328. return ERROR_OK;
  329. }
  330. int vsllink_register_commands(struct command_context_s *cmd_ctx)
  331. {
  332. register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
  333. COMMAND_CONFIG, NULL);
  334. register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
  335. COMMAND_CONFIG, NULL);
  336. register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
  337. COMMAND_CONFIG, NULL);
  338. register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
  339. COMMAND_CONFIG, NULL);
  340. return ERROR_OK;
  341. }
  342. int vsllink_init(void)
  343. {
  344. int check_cnt;
  345. int result;
  346. char version_str[100];
  347. vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
  348. vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
  349. if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
  350. {
  351. LOG_ERROR("Not enough memory");
  352. exit(-1);
  353. }
  354. vsllink_jtag_handle = vsllink_usb_open();
  355. if (vsllink_jtag_handle == 0)
  356. {
  357. LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
  358. return ERROR_JTAG_INIT_FAILED;
  359. }
  360. check_cnt = 0;
  361. while (check_cnt < 3)
  362. {
  363. vsllink_simple_command(VSLLINK_CMD_CONN);
  364. result = vsllink_usb_read(vsllink_jtag_handle);
  365. if (result > 2)
  366. {
  367. vsllink_usb_in_buffer[result] = 0;
  368. VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
  369. strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
  370. LOG_INFO(version_str);
  371. // free the pre-alloc memroy
  372. free(vsllink_usb_in_buffer);
  373. free(vsllink_usb_out_buffer);
  374. vsllink_usb_in_buffer = NULL;
  375. vsllink_usb_out_buffer = NULL;
  376. // alloc new memory
  377. vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
  378. vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
  379. if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
  380. {
  381. LOG_ERROR("Not enough memory");
  382. exit(-1);
  383. }
  384. else
  385. {
  386. LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
  387. }
  388. break;
  389. }
  390. vsllink_simple_command(VSLLINK_CMD_DISCONN);
  391. check_cnt++;
  392. }
  393. if (check_cnt == 3)
  394. {
  395. // It's dangerout to proced
  396. LOG_ERROR("VSLLink initial failed");
  397. exit(-1);
  398. }
  399. // Set SRST and TRST to output, Set USR1 and USR2 to input
  400. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
  401. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
  402. vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
  403. if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
  404. {
  405. LOG_ERROR("VSLLink USB send data error");
  406. exit(-1);
  407. }
  408. vsllink_reset(0, 0);
  409. LOG_INFO("VSLLink JTAG Interface ready");
  410. vsllink_tap_init();
  411. return ERROR_OK;
  412. }
  413. int vsllink_quit(void)
  414. {
  415. if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
  416. {
  417. // Set all pins to input
  418. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
  419. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
  420. vsllink_usb_out_buffer[2] = 0;
  421. if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
  422. {
  423. LOG_ERROR("VSLLink USB send data error");
  424. exit(-1);
  425. }
  426. // disconnect
  427. vsllink_simple_command(VSLLINK_CMD_DISCONN);
  428. vsllink_usb_close(vsllink_jtag_handle);
  429. }
  430. if (vsllink_usb_in_buffer != NULL)
  431. {
  432. free(vsllink_usb_in_buffer);
  433. }
  434. if (vsllink_usb_out_buffer != NULL)
  435. {
  436. free(vsllink_usb_out_buffer);
  437. }
  438. return ERROR_OK;
  439. }
  440. // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
  441. // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
  442. void vsllink_append_tms(void)
  443. {
  444. u8 tms_scan = VSLLINK_TAP_MOVE(cur_state, end_state);
  445. u16 tms2;
  446. if (((cur_state != TAP_RESET) && (cur_state != TAP_IDLE) && (cur_state != TAP_DRPAUSE) && (cur_state != TAP_IRPAUSE)) || \
  447. (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
  448. (vsllink_tms_cmd_pos == NULL))
  449. {
  450. LOG_ERROR("There MUST be some bugs in the driver");
  451. exit(-1);
  452. }
  453. tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position]) << \
  454. vsllink_tms_data_len;
  455. if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_value == 1)
  456. {
  457. tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
  458. (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position);
  459. }
  460. tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position) << \
  461. (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position);
  462. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
  463. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
  464. vsllink_tms_data_len = 0;
  465. vsllink_tms_cmd_pos = NULL;
  466. }
  467. /***************************************************************************/
  468. /* Queue command implementations */
  469. void vsllink_end_state(enum tap_state state)
  470. {
  471. if (tap_move_map[state] != -1)
  472. {
  473. end_state = state;
  474. }
  475. else
  476. {
  477. LOG_ERROR("BUG: %i is not a valid end state", state);
  478. exit(-1);
  479. }
  480. }
  481. /* Goes to the end state. */
  482. void vsllink_state_move(void)
  483. {
  484. if (vsllink_tms_data_len > 0)
  485. {
  486. vsllink_append_tms();
  487. }
  488. else
  489. {
  490. vsllink_tap_ensure_space(0, 2);
  491. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
  492. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
  493. }
  494. cur_state = end_state;
  495. }
  496. // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
  497. void vsllink_add_path(int start, int num, enum tap_state *path)
  498. {
  499. int i;
  500. for (i = start; i < (start + num); i++)
  501. {
  502. if ((i & 7) == 0)
  503. {
  504. if (i > 0)
  505. {
  506. vsllink_usb_out_buffer_idx++;
  507. }
  508. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  509. }
  510. if (path[i - start] == tap_transitions[cur_state].high)
  511. {
  512. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
  513. }
  514. else if (path[i - start] == tap_transitions[cur_state].low)
  515. {
  516. // nothing to do
  517. }
  518. else
  519. {
  520. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(path[i]));
  521. exit(-1);
  522. }
  523. cur_state = path[i - start];
  524. }
  525. if ((i > 0) && ((i & 7) == 0))
  526. {
  527. vsllink_usb_out_buffer_idx++;
  528. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  529. }
  530. end_state = cur_state;
  531. }
  532. void vsllink_path_move(int num_states, enum tap_state *path)
  533. {
  534. int i, tms_len, tms_cmd_pos, path_idx = 0;
  535. if (vsllink_tms_data_len > 0)
  536. {
  537. // there are vsllink_tms_data_len more tms bits to be shifted
  538. // so there are vsllink_tms_data_len + num_states tms bits in all
  539. tms_len = vsllink_tms_data_len + num_states;
  540. if (tms_len <= 16)
  541. {
  542. // merge into last tms shift
  543. if (tms_len < 8)
  544. {
  545. // just append tms data to the last tms byte
  546. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  547. }
  548. else if (tms_len == 8)
  549. {
  550. // end last tms shift command
  551. (*vsllink_tms_cmd_pos)--;
  552. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  553. }
  554. else if (tms_len < 16)
  555. {
  556. if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
  557. {
  558. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  559. // there is enought tms length in the current tms shift command
  560. (*vsllink_tms_cmd_pos)++;
  561. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  562. }
  563. else
  564. {
  565. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  566. // not enough tms length in the current tms shift command
  567. // so a new command should be added
  568. // first decrease byte length of last tms shift command
  569. (*vsllink_tms_cmd_pos)--;
  570. // append tms data to the last tms byte
  571. vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
  572. path += 8 - vsllink_tms_data_len;
  573. // add new command(3 bytes)
  574. vsllink_tap_ensure_space(0, 3);
  575. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  576. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  577. vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
  578. }
  579. }
  580. else if (tms_len == 16)
  581. {
  582. // end last tms shift command
  583. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  584. }
  585. vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
  586. if (vsllink_tms_data_len == 0)
  587. {
  588. vsllink_tms_cmd_pos = NULL;
  589. }
  590. num_states = 0;
  591. }
  592. else
  593. {
  594. vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
  595. path += 16 - vsllink_tms_data_len;
  596. num_states -= 16 - vsllink_tms_data_len;
  597. vsllink_tms_data_len = 0;
  598. vsllink_tms_cmd_pos = NULL;
  599. }
  600. }
  601. if (num_states > 0)
  602. {
  603. // Normal operation, don't need to append tms data
  604. vsllink_tms_data_len = num_states & 7;
  605. while (num_states > 0)
  606. {
  607. if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
  608. {
  609. i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
  610. }
  611. else
  612. {
  613. i = num_states;
  614. }
  615. tms_len = (i + 7) >> 3;
  616. vsllink_tap_ensure_space(0, tms_len + 2);
  617. tms_cmd_pos = vsllink_usb_out_buffer_idx;
  618. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
  619. vsllink_add_path(0, i, path + path_idx);
  620. path_idx += i;
  621. num_states -= i;
  622. }
  623. if (vsllink_tms_data_len > 0)
  624. {
  625. if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
  626. {
  627. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
  628. (*vsllink_tms_cmd_pos)++;
  629. }
  630. else
  631. {
  632. vsllink_usb_out_buffer[tms_cmd_pos]--;
  633. tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  634. vsllink_tap_ensure_space(0, 3);
  635. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  636. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  637. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
  638. }
  639. }
  640. }
  641. }
  642. void vsllink_runtest(int num_cycles)
  643. {
  644. int tms_len;
  645. enum tap_state saved_end_state = end_state;
  646. if (cur_state != TAP_IDLE)
  647. {
  648. // enter into IDLE state
  649. vsllink_end_state(TAP_IDLE);
  650. vsllink_state_move();
  651. }
  652. else
  653. {
  654. // cur_state == TAP_IDLE
  655. if (vsllink_tms_data_len > 0)
  656. {
  657. // there are vsllink_tms_data_len more tms bits to be shifted
  658. // so there are vsllink_tms_data_len + num_cycles tms bits in all
  659. tms_len = vsllink_tms_data_len + num_cycles;
  660. if (tms_len <= 16)
  661. {
  662. // merge into last tms shift
  663. if (tms_len < 8)
  664. {
  665. // just add to vsllink_tms_data_len
  666. // same result if tun through
  667. //vsllink_tms_data_len += num_cycles;
  668. }
  669. else if (tms_len == 8)
  670. {
  671. // end last tms shift command
  672. // just reduce it, no need to append_tms
  673. (*vsllink_tms_cmd_pos)--;
  674. vsllink_usb_out_buffer_idx++;
  675. }
  676. else if (tms_len < 16)
  677. {
  678. if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
  679. {
  680. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  681. // there is enought tms length in the current tms shift command
  682. // increase the tms byte length by 1 and set the last byte to 0
  683. (*vsllink_tms_cmd_pos)++;
  684. vsllink_usb_out_buffer_idx++;
  685. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  686. }
  687. else
  688. {
  689. // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
  690. // not enough tms length in the current tms shift command
  691. // so a new command should be added
  692. // first decrease byte length of last tms shift command
  693. (*vsllink_tms_cmd_pos)--;
  694. // move the command pointer to the next empty position
  695. vsllink_usb_out_buffer_idx++;
  696. // add new command(3 bytes)
  697. vsllink_tap_ensure_space(0, 3);
  698. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  699. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  700. vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
  701. }
  702. }
  703. else if (tms_len == 16)
  704. {
  705. // end last tms shift command
  706. vsllink_usb_out_buffer_idx++;
  707. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  708. }
  709. vsllink_tms_data_len = (vsllink_tms_data_len + num_cycles) & 7;
  710. if (vsllink_tms_data_len == 0)
  711. {
  712. vsllink_tms_cmd_pos = NULL;
  713. }
  714. num_cycles = 0;
  715. }
  716. else
  717. {
  718. vsllink_usb_out_buffer_idx++;
  719. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  720. num_cycles -= 16 - vsllink_tms_data_len;
  721. vsllink_tms_data_len = 0;
  722. vsllink_tms_cmd_pos = NULL;
  723. }
  724. }
  725. }
  726. // from here vsllink_tms_data_len == 0 or num_cycles == 0
  727. if (vsllink_tms_data_len > 0)
  728. {
  729. // num_cycles == 0
  730. // no need to shift
  731. if (num_cycles > 0)
  732. {
  733. LOG_ERROR("There MUST be some bugs in the driver");
  734. exit(-1);
  735. }
  736. }
  737. else
  738. {
  739. // get number of bytes left to be sent
  740. tms_len = num_cycles >> 3;
  741. if (tms_len > 0)
  742. {
  743. vsllink_tap_ensure_space(1, 5);
  744. // if tms_len > 0, vsllink_tms_data_len == 0
  745. // so just add new command
  746. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMS0BYTE;
  747. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
  748. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
  749. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
  750. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
  751. vsllink_usb_in_want_length += 1;
  752. pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
  753. pending_scan_results_length++;
  754. if (tms_len > 0xFFFF)
  755. {
  756. vsllink_tap_execute();
  757. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  758. vsllink_usb_out_buffer_idx = 3;
  759. }
  760. }
  761. vsllink_tms_data_len = num_cycles & 7;
  762. if (vsllink_tms_data_len > 0)
  763. {
  764. vsllink_tap_ensure_space(0, 3);
  765. vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  766. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  767. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  768. }
  769. }
  770. // post-process
  771. // set end_state
  772. vsllink_end_state(saved_end_state);
  773. cur_state = TAP_IDLE;
  774. if (end_state != TAP_IDLE)
  775. {
  776. vsllink_state_move();
  777. }
  778. }
  779. void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
  780. {
  781. enum tap_state saved_end_state;
  782. u8 bits_left, tms_tmp, tdi_len;
  783. int i;
  784. if (0 == scan_size )
  785. {
  786. return;
  787. }
  788. tdi_len = ((scan_size + 7) >> 3);
  789. if ((tdi_len + 7) > VSLLINK_BufferSize)
  790. {
  791. LOG_ERROR("Your implementation of VSLLink has not enough buffer");
  792. exit(-1);
  793. }
  794. saved_end_state = end_state;
  795. /* Move to appropriate scan state */
  796. vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  797. if (vsllink_tms_data_len > 0)
  798. {
  799. if (cur_state == end_state)
  800. {
  801. // already in IRSHIFT or DRSHIFT state
  802. // merge tms data in the last tms shift command into next scan command
  803. if(*vsllink_tms_cmd_pos < 1)
  804. {
  805. LOG_ERROR("There MUST be some bugs in the driver");
  806. exit(-1);
  807. }
  808. else if(*vsllink_tms_cmd_pos < 2)
  809. {
  810. tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  811. vsllink_usb_out_buffer_idx--;
  812. }
  813. else
  814. {
  815. tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  816. *vsllink_tms_cmd_pos -= 2;
  817. }
  818. vsllink_tap_ensure_space(1, tdi_len + 7);
  819. // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
  820. // which is merged from the last tms shift command
  821. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
  822. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
  823. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
  824. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
  825. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
  826. for (i = 0; i < tdi_len; i++)
  827. {
  828. buffer[i] >>= 8 - vsllink_tms_data_len;
  829. if (i != tdi_len)
  830. {
  831. buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
  832. }
  833. }
  834. vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
  835. scan_size -= 8 - vsllink_tms_data_len;
  836. vsllink_tms_data_len = 0;
  837. }
  838. else
  839. {
  840. vsllink_append_tms();
  841. vsllink_tap_ensure_space(1, tdi_len + 5);
  842. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
  843. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
  844. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
  845. vsllink_tap_append_scan(scan_size, buffer, command, 0);
  846. }
  847. }
  848. else
  849. {
  850. vsllink_tap_ensure_space(1, tdi_len + 7);
  851. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
  852. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
  853. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
  854. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
  855. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  856. vsllink_tap_append_scan(scan_size, buffer, command, 8);
  857. }
  858. vsllink_end_state(saved_end_state);
  859. bits_left = scan_size & 0x07;
  860. cur_state = ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE;
  861. if (bits_left > 0)
  862. {
  863. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
  864. }
  865. else
  866. {
  867. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
  868. }
  869. if (cur_state != end_state)
  870. {
  871. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
  872. }
  873. else
  874. {
  875. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  876. }
  877. cur_state = end_state;
  878. }
  879. void vsllink_reset(int trst, int srst)
  880. {
  881. int result;
  882. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  883. /* Signals are active low */
  884. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
  885. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
  886. vsllink_usb_out_buffer[2] = 0;
  887. if (srst == 0)
  888. {
  889. vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
  890. }
  891. if (trst == 0)
  892. {
  893. vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
  894. }
  895. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  896. if (result != 3)
  897. {
  898. LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
  899. }
  900. }
  901. void vsllink_simple_command(u8 command)
  902. {
  903. int result;
  904. DEBUG_JTAG_IO("0x%02x", command);
  905. vsllink_usb_out_buffer[0] = command;
  906. result = vsllink_usb_write(vsllink_jtag_handle, 1);
  907. if (result != 1)
  908. {
  909. LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
  910. }
  911. }
  912. int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  913. {
  914. if (argc != 1) {
  915. LOG_ERROR("parameter error, should be one parameter for VID");
  916. return ERROR_OK;
  917. }
  918. vsllink_vid = strtol(args[0], NULL, 0);
  919. return ERROR_OK;
  920. }
  921. int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  922. {
  923. if (argc != 1) {
  924. LOG_ERROR("parameter error, should be one parameter for PID");
  925. return ERROR_OK;
  926. }
  927. vsllink_pid = strtol(args[0], NULL, 0);
  928. return ERROR_OK;
  929. }
  930. int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  931. {
  932. if (argc != 1) {
  933. LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
  934. return ERROR_OK;
  935. }
  936. vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
  937. return ERROR_OK;
  938. }
  939. int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  940. {
  941. if (argc != 1) {
  942. LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
  943. return ERROR_OK;
  944. }
  945. vsllink_bulkout = strtol(args[0], NULL, 0);
  946. return ERROR_OK;
  947. }
  948. /***************************************************************************/
  949. /* VSLLink tap functions */
  950. void vsllink_tap_init(void)
  951. {
  952. vsllink_usb_out_buffer_idx = 0;
  953. vsllink_usb_in_want_length = 0;
  954. pending_scan_results_length = 0;
  955. }
  956. void vsllink_tap_ensure_space(int scans, int bytes)
  957. {
  958. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  959. int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
  960. if (scans > available_scans || bytes > available_bytes)
  961. {
  962. vsllink_tap_execute();
  963. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  964. vsllink_usb_out_buffer_idx = 3;
  965. }
  966. }
  967. void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
  968. {
  969. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
  970. int i;
  971. if (offset > 0)
  972. {
  973. vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
  974. }
  975. else
  976. {
  977. vsllink_usb_in_want_length += (length + 7) >> 3;
  978. }
  979. pending_scan_result->length = length;
  980. pending_scan_result->offset = offset;
  981. pending_scan_result->command = command;
  982. pending_scan_result->buffer = buffer;
  983. for (i = 0; i < ((length + 7) >> 3); i++)
  984. {
  985. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
  986. }
  987. pending_scan_results_length++;
  988. }
  989. /* Pad and send a tap sequence to the device, and receive the answer.
  990. * For the purpose of padding we assume that we are in reset or idle or pause state. */
  991. int vsllink_tap_execute(void)
  992. {
  993. int i;
  994. int result;
  995. int first = 0;
  996. if (vsllink_tms_data_len > 0)
  997. {
  998. if((cur_state != TAP_RESET) && (cur_state != TAP_IDLE) && (cur_state != TAP_IRPAUSE) && (cur_state != TAP_DRPAUSE))
  999. {
  1000. LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", jtag_state_name(cur_state));
  1001. }
  1002. if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
  1003. {
  1004. // last tms bit is '1'
  1005. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
  1006. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
  1007. vsllink_tms_data_len = 0;
  1008. }
  1009. else
  1010. {
  1011. // last tms bit is '0'
  1012. vsllink_usb_out_buffer_idx++;
  1013. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  1014. vsllink_tms_data_len = 0;
  1015. }
  1016. }
  1017. if (vsllink_usb_out_buffer_idx > 3)
  1018. {
  1019. if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
  1020. {
  1021. vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
  1022. vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
  1023. }
  1024. result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
  1025. if (result == vsllink_usb_in_want_length)
  1026. {
  1027. for (i = 0; i < pending_scan_results_length; i++)
  1028. {
  1029. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
  1030. u8 *buffer = pending_scan_result->buffer;
  1031. int length = pending_scan_result->length;
  1032. int offset = pending_scan_result->offset;
  1033. scan_command_t *command = pending_scan_result->command;
  1034. if (buffer != NULL)
  1035. {
  1036. // IRSHIFT or DRSHIFT
  1037. buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
  1038. first += (length + offset + 7) >> 3;
  1039. DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
  1040. #ifdef _DEBUG_JTAG_IO_
  1041. vsllink_debug_buffer(buffer, (length + 7) >> 3);
  1042. #endif
  1043. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  1044. {
  1045. vsllink_tap_init();
  1046. return ERROR_JTAG_QUEUE_FAILED;
  1047. }
  1048. free(pending_scan_result->buffer);
  1049. pending_scan_result->buffer = NULL;
  1050. }
  1051. else
  1052. {
  1053. first++;
  1054. }
  1055. }
  1056. }
  1057. else
  1058. {
  1059. LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
  1060. return ERROR_JTAG_QUEUE_FAILED;
  1061. }
  1062. vsllink_tap_init();
  1063. }
  1064. return ERROR_OK;
  1065. }
  1066. /*****************************************************************************/
  1067. /* VSLLink USB low-level functions */
  1068. vsllink_jtag_t* vsllink_usb_open(void)
  1069. {
  1070. struct usb_bus *busses;
  1071. struct usb_bus *bus;
  1072. struct usb_device *dev;
  1073. vsllink_jtag_t *result;
  1074. result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
  1075. usb_init();
  1076. usb_find_busses();
  1077. usb_find_devices();
  1078. busses = usb_get_busses();
  1079. /* find vsllink_jtag device in usb bus */
  1080. for (bus = busses; bus; bus = bus->next)
  1081. {
  1082. for (dev = bus->devices; dev; dev = dev->next)
  1083. {
  1084. if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
  1085. {
  1086. result->usb_handle = usb_open(dev);
  1087. /* usb_set_configuration required under win32 */
  1088. usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
  1089. usb_claim_interface(result->usb_handle, 0);
  1090. #if 0
  1091. /*
  1092. * This makes problems under Mac OS X. And is not needed
  1093. * under Windows. Hopefully this will not break a linux build
  1094. */
  1095. usb_set_altinterface(result->usb_handle, 0);
  1096. #endif
  1097. return result;
  1098. }
  1099. }
  1100. }
  1101. free(result);
  1102. return NULL;
  1103. }
  1104. void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
  1105. {
  1106. usb_close(vsllink_jtag->usb_handle);
  1107. free(vsllink_jtag);
  1108. }
  1109. /* Send a message and receive the reply. */
  1110. int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
  1111. {
  1112. int result;
  1113. result = vsllink_usb_write(vsllink_jtag, out_length);
  1114. if (result == out_length)
  1115. {
  1116. if (in_length > 0)
  1117. {
  1118. result = vsllink_usb_read(vsllink_jtag);
  1119. if (result == in_length )
  1120. {
  1121. return result;
  1122. }
  1123. else
  1124. {
  1125. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
  1126. return -1;
  1127. }
  1128. }
  1129. return 0;
  1130. }
  1131. else
  1132. {
  1133. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
  1134. return -1;
  1135. }
  1136. }
  1137. /* Write data from out_buffer to USB. */
  1138. int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
  1139. {
  1140. int result;
  1141. if (out_length > VSLLINK_BufferSize)
  1142. {
  1143. LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
  1144. return -1;
  1145. }
  1146. result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
  1147. (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
  1148. DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
  1149. #ifdef _DEBUG_USB_COMMS_
  1150. LOG_DEBUG("USB out:");
  1151. vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
  1152. #endif
  1153. #ifdef _VSLLINK_IN_DEBUG_MODE_
  1154. usleep(100000);
  1155. #endif
  1156. return result;
  1157. }
  1158. /* Read data from USB into in_buffer. */
  1159. int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
  1160. {
  1161. int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
  1162. (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
  1163. DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
  1164. #ifdef _DEBUG_USB_COMMS_
  1165. LOG_DEBUG("USB in:");
  1166. vsllink_debug_buffer(vsllink_usb_in_buffer, result);
  1167. #endif
  1168. return result;
  1169. }
  1170. #define BYTES_PER_LINE 16
  1171. void vsllink_debug_buffer(u8 *buffer, int length)
  1172. {
  1173. char line[81];
  1174. char s[4];
  1175. int i;
  1176. int j;
  1177. for (i = 0; i < length; i += BYTES_PER_LINE)
  1178. {
  1179. snprintf(line, 5, "%04x", i);
  1180. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  1181. {
  1182. snprintf(s, 4, " %02x", buffer[j]);
  1183. strcat(line, s);
  1184. }
  1185. LOG_DEBUG(line);
  1186. }
  1187. }