1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian <SimonQian@SimonQian.com> *
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. *
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. *
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 ***************************************************************************/
20 /* Versaloon is a programming tool for multiple MCUs.
21 * OpenOCD and MSP430 supports are distributed under GPLv2.
22 * You can find it at http://www.SimonQian.com/en/Versaloon.
34 //#define _VSLLINK_IN_DEBUG_MODE_
36 #define VSLLINK_MODE_NORMAL 0
37 #define VSLLINK_MODE_DMA 1
39 static u16 vsllink_usb_vid;
40 static u16 vsllink_usb_pid;
41 static u8 vsllink_usb_bulkout;
42 static u8 vsllink_usb_bulkin;
43 static u8 vsllink_usb_interface;
44 static u8 vsllink_mode = VSLLINK_MODE_NORMAL;
45 static int VSLLINK_USB_TIMEOUT = 10000;
47 static int VSLLINK_BufferSize = 1024;
49 /* Global USB buffers */
50 static int vsllink_usb_out_buffer_idx;
51 static int vsllink_usb_in_want_length;
52 static u8* vsllink_usb_in_buffer = NULL;
53 static u8* vsllink_usb_out_buffer = NULL;
55 /* Constants for VSLLink command */
56 #define VSLLINK_CMD_CONN 0x80
57 #define VSLLINK_CMD_DISCONN 0x81
58 #define VSLLINK_CMD_SET_SPEED 0x82
59 #define VSLLINK_CMD_SET_PORT 0x90
60 #define VSLLINK_CMD_GET_PORT 0x91
61 #define VSLLINK_CMD_SET_PORTDIR 0x92
62 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
63 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
64 #define VSLLINK_CMD_HW_SWDCMD 0xA2
65 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
67 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
68 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
69 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
71 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
72 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
74 #define JTAG_PINMSK_SRST (1 << 0)
75 #define JTAG_PINMSK_TRST (1 << 1)
76 #define JTAG_PINMSK_USR1 (1 << 2)
77 #define JTAG_PINMSK_USR2 (1 << 3)
78 #define JTAG_PINMSK_TCK (1 << 4)
79 #define JTAG_PINMSK_TMS (1 << 5)
80 #define JTAG_PINMSK_TDI (1 << 6)
81 #define JTAG_PINMSK_TDO (1 << 7)
84 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
86 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
94 * SD->SD and SI->SI have to be caught in interface specific code
96 static u8 VSLLINK_tap_move[6][6] =
98 /* TLR RTI SD PD SI PI */
99 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
100 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
101 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
102 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
103 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
104 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
107 typedef struct insert_insignificant_operation
109 unsigned char insert_value;
110 unsigned char insert_position;
111 }insert_insignificant_operation_t;
113 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
160 static u8 VSLLINK_BIT_MSK[8] =
162 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
168 int length; /* Number of bits to read */
169 scan_command_t *command; /* Corresponding scan command */
171 } pending_scan_result_t;
173 #define MAX_PENDING_SCAN_RESULTS 256
175 static int pending_scan_results_length;
176 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
178 /* External interface functions */
179 static int vsllink_execute_queue(void);
180 static int vsllink_speed(int speed);
181 static int vsllink_khz(int khz, int *jtag_speed);
182 static int vsllink_speed_div(int jtag_speed, int *khz);
183 static int vsllink_register_commands(struct command_context_s *cmd_ctx);
184 static int vsllink_init(void);
185 static int vsllink_quit(void);
187 /* CLI command handler functions */
188 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
189 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
190 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
191 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
192 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
193 static int vsllink_handle_mode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
195 /* Queue command functions */
196 static void vsllink_end_state(tap_state_t state);
197 static void vsllink_state_move_dma(void);
198 static void vsllink_state_move_normal(void);
199 static void (*vsllink_state_move)(void);
200 static void vsllink_path_move_dma(int num_states, tap_state_t *path);
201 static void vsllink_path_move_normal(int num_states, tap_state_t *path);
202 static void (*vsllink_path_move)(int num_states, tap_state_t *path);
203 static void vsllink_runtest(int num_cycles);
204 static void vsllink_stableclocks_dma(int num_cycles, int tms);
205 static void vsllink_stableclocks_normal(int num_cycles, int tms);
206 static void (*vsllink_stableclocks)(int num_cycles, int tms);
207 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
208 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
209 static void (*vsllink_scan)(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
210 static void vsllink_reset(int trst, int srst);
211 static void vsllink_simple_command(u8 command);
212 static int vsllink_connect(void);
213 static int vsllink_disconnect(void);
215 /* VSLLink tap buffer functions */
216 static void vsllink_tap_append_step(int tms, int tdi);
217 static void vsllink_tap_init_dma(void);
218 static void vsllink_tap_init_normal(void);
219 static void (*vsllink_tap_init)(void);
220 static int vsllink_tap_execute_dma(void);
221 static int vsllink_tap_execute_normal(void);
222 static int (*vsllink_tap_execute)(void);
223 static void vsllink_tap_ensure_space_dma(int scans, int length);
224 static void vsllink_tap_ensure_space_normal(int scans, int length);
225 static void (*vsllink_tap_ensure_space)(int scans, int length);
226 static void vsllink_tap_append_scan_dma(int length, u8 *buffer, scan_command_t *command);
227 static void vsllink_tap_append_scan_normal(int length, u8 *buffer, scan_command_t *command, int offset);
229 /* VSLLink lowlevel functions */
230 typedef struct vsllink_jtag
232 struct usb_dev_handle* usb_handle;
235 static vsllink_jtag_t *vsllink_usb_open(void);
236 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
237 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
238 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
239 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
241 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
242 static void vsllink_debug_buffer(u8 *buffer, int length);
245 static int vsllink_tms_data_len = 0;
246 static u8* vsllink_tms_cmd_pos;
248 static int tap_length = 0;
249 static int tap_buffer_size = 0;
250 static u8 *tms_buffer = NULL;
251 static u8 *tdi_buffer = NULL;
252 static u8 *tdo_buffer = NULL;
255 static vsllink_jtag_t* vsllink_jtag_handle = NULL;
257 /***************************************************************************/
258 /* External interface implementation */
260 jtag_interface_t vsllink_interface =
263 .execute_queue = vsllink_execute_queue,
264 .speed = vsllink_speed,
266 .speed_div = vsllink_speed_div,
267 .register_commands = vsllink_register_commands,
268 .init = vsllink_init,
272 static void reset_command_pointer(void)
274 if (vsllink_mode == VSLLINK_MODE_NORMAL)
276 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
277 vsllink_usb_out_buffer_idx = 3;
285 static int vsllink_execute_queue(void)
287 jtag_command_t *cmd = jtag_command_queue;
292 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
294 reset_command_pointer();
300 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
301 tap_state_name(cmd->cmd.runtest->end_state));
303 if (cmd->cmd.runtest->end_state != TAP_INVALID)
305 vsllink_end_state(cmd->cmd.runtest->end_state);
307 vsllink_runtest(cmd->cmd.runtest->num_cycles);
311 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
313 if (cmd->cmd.statemove->end_state != TAP_INVALID)
315 vsllink_end_state(cmd->cmd.statemove->end_state);
317 vsllink_state_move();
321 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
322 cmd->cmd.pathmove->num_states, \
323 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
325 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
329 if (cmd->cmd.scan->end_state != TAP_INVALID)
331 vsllink_end_state(cmd->cmd.scan->end_state);
334 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
335 if (cmd->cmd.scan->ir_scan)
337 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
341 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
344 #ifdef _DEBUG_JTAG_IO_
345 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
348 type = jtag_scan_type(cmd->cmd.scan);
350 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
354 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
356 vsllink_tap_execute();
358 if (cmd->cmd.reset->trst == 1)
360 tap_set_state(TAP_RESET);
362 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
366 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
367 vsllink_tap_execute();
368 jtag_sleep(cmd->cmd.sleep->us);
371 case JTAG_STABLECLOCKS:
372 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
373 switch(tap_get_state())
376 // tms should be '1' to stay in TAP_RESET mode
384 // in other mode, tms should be '0'
386 break; /* above stable states are OK */
388 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
389 tap_state_name(tap_get_state()) );
392 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
396 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
402 return vsllink_tap_execute();
405 static int vsllink_speed(int speed)
409 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
410 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
411 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
413 result = vsllink_usb_write(vsllink_jtag_handle, 3);
421 LOG_ERROR("VSLLink setting speed failed (%d)", result);
422 return ERROR_JTAG_DEVICE_ERROR;
428 static int vsllink_khz(int khz, int *jtag_speed)
435 static int vsllink_speed_div(int jtag_speed, int *khz)
442 static int vsllink_init(void)
444 int check_cnt, to_tmp;
446 char version_str[100];
448 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
449 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
450 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
452 LOG_ERROR("Not enough memory");
456 vsllink_jtag_handle = vsllink_usb_open();
458 if (vsllink_jtag_handle == 0)
460 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
461 return ERROR_JTAG_INIT_FAILED;
463 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid, vsllink_usb_pid);
465 to_tmp = VSLLINK_USB_TIMEOUT;
466 VSLLINK_USB_TIMEOUT = 100;
468 while (check_cnt < 5)
470 vsllink_simple_command(0x00);
471 result = vsllink_usb_read(vsllink_jtag_handle);
475 vsllink_usb_in_buffer[result] = 0;
476 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
477 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
478 LOG_INFO("%s", version_str);
480 // free the pre-alloc memroy
481 free(vsllink_usb_in_buffer);
482 free(vsllink_usb_out_buffer);
483 vsllink_usb_in_buffer = NULL;
484 vsllink_usb_out_buffer = NULL;
487 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
488 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
489 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
491 LOG_ERROR("Not enough memory");
496 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
498 // alloc memory for dma mode
499 if (vsllink_mode == VSLLINK_MODE_DMA)
501 tap_buffer_size = (VSLLINK_BufferSize - 3) / 2;
502 tms_buffer = (u8*)malloc(tap_buffer_size);
503 tdi_buffer = (u8*)malloc(tap_buffer_size);
504 tdo_buffer = (u8*)malloc(tap_buffer_size);
505 if ((tms_buffer == NULL) || (tdi_buffer == NULL) || (tdo_buffer == NULL))
507 LOG_ERROR("Not enough memory");
513 vsllink_simple_command(VSLLINK_CMD_DISCONN);
518 // It's dangerout to proced
519 LOG_ERROR("VSLLink initial failed");
522 VSLLINK_USB_TIMEOUT = to_tmp;
524 // connect to vsllink
526 // initialize function pointers
527 if (vsllink_mode == VSLLINK_MODE_NORMAL)
530 vsllink_state_move = vsllink_state_move_normal;
531 vsllink_path_move = vsllink_path_move_normal;
532 vsllink_stableclocks = vsllink_stableclocks_normal;
533 vsllink_scan = vsllink_scan_normal;
535 vsllink_tap_init = vsllink_tap_init_normal;
536 vsllink_tap_execute = vsllink_tap_execute_normal;
537 vsllink_tap_ensure_space = vsllink_tap_ensure_space_normal;
539 LOG_INFO("vsllink run in NORMAL mode");
544 vsllink_state_move = vsllink_state_move_dma;
545 vsllink_path_move = vsllink_path_move_dma;
546 vsllink_stableclocks = vsllink_stableclocks_dma;
547 vsllink_scan = vsllink_scan_dma;
549 vsllink_tap_init = vsllink_tap_init_dma;
550 vsllink_tap_execute = vsllink_tap_execute_dma;
551 vsllink_tap_ensure_space = vsllink_tap_ensure_space_dma;
553 LOG_INFO("vsllink run in DMA mode");
556 // Set SRST and TRST to output, Set USR1 and USR2 to input
557 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
558 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
559 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
560 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
562 LOG_ERROR("VSLLink USB send data error");
568 LOG_INFO("VSLLink JTAG Interface ready");
575 static int vsllink_quit(void)
577 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
579 // Set all pins to input
580 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
581 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
582 vsllink_usb_out_buffer[2] = 0;
583 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
585 LOG_ERROR("VSLLink USB send data error");
590 vsllink_disconnect();
591 vsllink_usb_close(vsllink_jtag_handle);
592 vsllink_jtag_handle = NULL;
595 if (vsllink_usb_in_buffer != NULL)
597 free(vsllink_usb_in_buffer);
598 vsllink_usb_in_buffer = NULL;
600 if (vsllink_usb_out_buffer != NULL)
602 free(vsllink_usb_out_buffer);
603 vsllink_usb_out_buffer = NULL;
609 /***************************************************************************/
610 /* Queue command implementations */
611 static int vsllink_disconnect(void)
613 vsllink_simple_command(VSLLINK_CMD_DISCONN);
617 static int vsllink_connect(void)
619 char vsllink_str[100];
621 vsllink_usb_out_buffer[0] = VSLLINK_CMD_CONN;
622 vsllink_usb_out_buffer[1] = vsllink_mode;
623 vsllink_usb_message(vsllink_jtag_handle, 2, 0);
624 if (vsllink_usb_read(vsllink_jtag_handle) > 2)
626 strncpy(vsllink_str, (char *)vsllink_usb_in_buffer + 2, sizeof(vsllink_str));
627 LOG_INFO("%s", vsllink_str);
633 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
634 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
635 static void vsllink_append_tms(void)
637 u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
639 insert_insignificant_operation_t *insert = \
640 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
642 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
643 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
644 (vsllink_tms_cmd_pos == NULL))
646 LOG_ERROR("There MUST be some bugs in the driver");
650 tms2 = (tms_scan & VSLLINK_BIT_MSK[insert->insert_position]) << \
651 vsllink_tms_data_len;
652 if (insert->insert_value == 1)
654 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
655 (vsllink_tms_data_len + insert->insert_position);
657 tms2 |= (tms_scan >> insert->insert_position) << \
658 (8 + insert->insert_position);
660 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
661 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
663 vsllink_tms_data_len = 0;
664 vsllink_tms_cmd_pos = NULL;
667 static void vsllink_end_state(tap_state_t state)
669 if (tap_is_state_stable(state))
671 tap_set_end_state(state);
675 LOG_ERROR("BUG: %i is not a valid end state", state);
680 /* Goes to the end state. */
681 static void vsllink_state_move_normal(void)
683 if (vsllink_tms_data_len > 0)
685 vsllink_append_tms();
689 vsllink_tap_ensure_space(0, 2);
691 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
692 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
695 tap_set_state(tap_get_end_state());
697 static void vsllink_state_move_dma(void)
699 int i, insert_length = (tap_length % 8) ? (8 - (tap_length % 8)) : 0;
700 insert_insignificant_operation_t *insert = \
701 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
702 u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
704 if (tap_get_state() == TAP_RESET)
706 vsllink_tap_ensure_space(0, 8);
708 for (i = 0; i < 8; i++)
710 vsllink_tap_append_step(1, 0);
714 if (insert_length > 0)
716 vsllink_tap_ensure_space(0, 16);
718 for (i = 0; i < insert->insert_position; i++)
720 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
722 for (i = 0; i < insert_length; i++)
724 vsllink_tap_append_step(insert->insert_value, 0);
726 for (i = insert->insert_position; i < 8; i++)
728 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
733 vsllink_tap_ensure_space(0, 8);
735 for (i = 0; i < 8; i++)
737 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
741 tap_set_state(tap_get_end_state());
744 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
745 static void vsllink_add_path(int start, int num, tap_state_t *path)
749 for (i = start; i < (start + num); i++)
755 vsllink_usb_out_buffer_idx++;
757 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
760 if (path[i - start] == tap_state_transition(tap_get_state(), true))
762 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
764 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
770 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
773 tap_set_state(path[i - start]);
775 if ((i > 0) && ((i & 7) == 0))
777 vsllink_usb_out_buffer_idx++;
778 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
781 tap_set_end_state(tap_get_state());
784 static void vsllink_path_move_normal(int num_states, tap_state_t *path)
786 int i, tms_len, tms_cmd_pos, path_idx = 0;
788 if (vsllink_tms_data_len > 0)
790 // there are vsllink_tms_data_len more tms bits to be shifted
791 // so there are vsllink_tms_data_len + num_states tms bits in all
792 tms_len = vsllink_tms_data_len + num_states;
795 // merge into last tms shift
798 // just append tms data to the last tms byte
799 vsllink_add_path(vsllink_tms_data_len, num_states, path);
801 else if (tms_len == 8)
803 // end last tms shift command
804 (*vsllink_tms_cmd_pos)--;
805 vsllink_add_path(vsllink_tms_data_len, num_states, path);
807 else if (tms_len < 16)
809 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
811 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
812 // there is enought tms length in the current tms shift command
813 (*vsllink_tms_cmd_pos)++;
814 vsllink_add_path(vsllink_tms_data_len, num_states, path);
818 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
819 // not enough tms length in the current tms shift command
820 // so a new command should be added
821 // first decrease byte length of last tms shift command
822 (*vsllink_tms_cmd_pos)--;
823 // append tms data to the last tms byte
824 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
825 path += 8 - vsllink_tms_data_len;
826 // add new command(3 bytes)
827 vsllink_tap_ensure_space(0, 3);
828 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
829 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
830 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
833 else if (tms_len == 16)
835 // end last tms shift command
836 vsllink_add_path(vsllink_tms_data_len, num_states, path);
839 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
840 if (vsllink_tms_data_len == 0)
842 vsllink_tms_cmd_pos = NULL;
848 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
850 path += 16 - vsllink_tms_data_len;
851 num_states -= 16 - vsllink_tms_data_len;
852 vsllink_tms_data_len = 0;
853 vsllink_tms_cmd_pos = NULL;
859 // Normal operation, don't need to append tms data
860 vsllink_tms_data_len = num_states & 7;
862 while (num_states > 0)
864 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
866 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
872 tms_len = (i + 7) >> 3;
873 vsllink_tap_ensure_space(0, tms_len + 2);
874 tms_cmd_pos = vsllink_usb_out_buffer_idx;
875 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
877 vsllink_add_path(0, i, path + path_idx);
883 if (vsllink_tms_data_len > 0)
885 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
887 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
888 (*vsllink_tms_cmd_pos)++;
892 vsllink_usb_out_buffer[tms_cmd_pos]--;
894 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
895 vsllink_tap_ensure_space(0, 3);
896 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
897 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
898 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
903 static void vsllink_path_move_dma(int num_states, tap_state_t *path)
909 if ((8 - (tap_length & 7)) < num_states)
911 j = 8 - (tap_length & 7);
917 for (i = 0; i < j; i++)
919 if (path[i] == tap_state_transition(tap_get_state(), false))
921 vsllink_tap_append_step(0, 0);
923 else if (path[i] == tap_state_transition(tap_get_state(), true))
925 vsllink_tap_append_step(1, 0);
929 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
932 tap_set_state(path[i]);
939 vsllink_tap_ensure_space(0, num_states);
941 for (i = 0; i < num_states; i++)
943 if (path[j + i] == tap_state_transition(tap_get_state(), false))
945 vsllink_tap_append_step(0, 0);
947 else if (path[j + i] == tap_state_transition(tap_get_state(), true))
949 vsllink_tap_append_step(1, 0);
953 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
956 tap_set_state(path[j + i]);
960 tap_set_end_state(tap_get_state());
963 static void vsllink_stableclocks_normal(int num_cycles, int tms)
968 if (vsllink_tms_data_len > 0)
970 // there are vsllink_tms_data_len more tms bits to be shifted
971 // so there are vsllink_tms_data_len + num_cycles tms bits in all
972 tms_len = vsllink_tms_data_len + num_cycles;
975 // append '1' for tms
976 tms_append_byte = (u16)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
980 // append '0' for tms
985 // merge into last tms shift
988 // just add to vsllink_tms_data_len
989 // same result if tun through
990 //vsllink_tms_data_len += num_cycles;
991 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (u8)(tms_append_byte & 0xFF);
993 else if (tms_len == 8)
995 // end last tms shift command
996 // just reduce it, and append last tms byte
997 (*vsllink_tms_cmd_pos)--;
998 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1000 else if (tms_len < 16)
1002 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
1004 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1005 // there is enought tms length in the current tms shift command
1006 // increase the tms byte length by 1 and set the last byte to 0
1007 (*vsllink_tms_cmd_pos)++;
1008 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1009 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
1013 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1014 // not enough tms length in the current tms shift command
1015 // so a new command should be added
1016 // first decrease byte length of last tms shift command
1017 (*vsllink_tms_cmd_pos)--;
1018 // append last tms byte and move the command pointer to the next empty position
1019 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1020 // add new command(3 bytes)
1021 vsllink_tap_ensure_space(0, 3);
1022 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1023 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1024 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
1027 else if (tms_len == 16)
1029 // end last tms shift command
1030 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1031 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
1034 vsllink_tms_data_len = tms_len & 7;
1035 if (vsllink_tms_data_len == 0)
1037 vsllink_tms_cmd_pos = NULL;
1043 // more shifts will be needed
1044 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1045 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
1047 num_cycles -= 16 - vsllink_tms_data_len;
1048 vsllink_tms_data_len = 0;
1049 vsllink_tms_cmd_pos = NULL;
1052 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1054 if (vsllink_tms_data_len > 0)
1060 LOG_ERROR("There MUST be some bugs in the driver");
1066 // get number of bytes left to be sent
1067 tms_len = num_cycles >> 3;
1070 vsllink_tap_ensure_space(1, 5);
1071 // if tms_len > 0, vsllink_tms_data_len == 0
1072 // so just add new command
1073 // LSB of the command byte is the tms value when do the shifting
1076 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
1080 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
1082 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
1083 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
1084 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
1085 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
1087 vsllink_usb_in_want_length += 1;
1088 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
1089 pending_scan_results_length++;
1091 if (tms_len > 0xFFFF)
1093 vsllink_tap_execute();
1098 vsllink_tms_data_len = num_cycles & 7;
1099 if (vsllink_tms_data_len > 0)
1101 vsllink_tap_ensure_space(0, 3);
1102 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1103 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1106 // append '1' for tms
1107 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
1111 // append '0' for tms
1112 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
1117 static void vsllink_stableclocks_dma(int num_cycles, int tms)
1123 if ((8 - (tap_length & 7)) < num_cycles)
1125 cur_cycles = 8 - (tap_length & 7);
1129 cur_cycles = num_cycles;
1131 for (i = 0; i < cur_cycles; i++)
1133 vsllink_tap_append_step(tms, 0);
1135 num_cycles -= cur_cycles;
1138 while (num_cycles > 0)
1140 if (num_cycles > 8 * tap_buffer_size)
1142 cur_cycles = 8 * tap_buffer_size;
1146 cur_cycles = num_cycles;
1149 vsllink_tap_ensure_space(0, cur_cycles);
1151 for (i = 0; i < cur_cycles; i++)
1153 vsllink_tap_append_step(tms, 0);
1156 num_cycles -= cur_cycles;
1160 static void vsllink_runtest(int num_cycles)
1162 tap_state_t saved_end_state = tap_get_end_state();
1164 if (tap_get_state() != TAP_IDLE)
1166 // enter into IDLE state
1167 vsllink_end_state(TAP_IDLE);
1168 vsllink_state_move();
1171 vsllink_stableclocks(num_cycles, 0);
1175 vsllink_end_state(saved_end_state);
1176 tap_set_state(TAP_IDLE);
1177 if (tap_get_end_state() != TAP_IDLE)
1179 vsllink_state_move();
1183 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
1185 tap_state_t saved_end_state;
1186 u8 bits_left, tms_tmp, tdi_len;
1189 if (0 == scan_size )
1194 tdi_len = ((scan_size + 7) >> 3);
1195 if ((tdi_len + 7) > VSLLINK_BufferSize)
1197 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1201 saved_end_state = tap_get_end_state();
1203 /* Move to appropriate scan state */
1204 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1206 if (vsllink_tms_data_len > 0)
1208 if (tap_get_state() == tap_get_end_state())
1210 // already in IRSHIFT or DRSHIFT state
1211 // merge tms data in the last tms shift command into next scan command
1212 if(*vsllink_tms_cmd_pos < 1)
1214 LOG_ERROR("There MUST be some bugs in the driver");
1217 else if(*vsllink_tms_cmd_pos < 2)
1219 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1220 vsllink_usb_out_buffer_idx--;
1224 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1225 *vsllink_tms_cmd_pos -= 2;
1228 vsllink_tap_ensure_space(1, tdi_len + 7);
1229 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1230 // which is merged from the last tms shift command
1231 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1232 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1233 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 8) & 0xff;
1234 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1235 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1237 for (i = 0; i < tdi_len; i++)
1239 buffer[i] >>= 8 - vsllink_tms_data_len;
1242 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1246 vsllink_tap_append_scan_normal(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1247 scan_size -= 8 - vsllink_tms_data_len;
1248 vsllink_tms_data_len = 0;
1252 vsllink_state_move();
1253 vsllink_tap_ensure_space(1, tdi_len + 5);
1255 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1256 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1257 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1259 vsllink_tap_append_scan_normal(scan_size, buffer, command, 0);
1264 vsllink_tap_ensure_space(1, tdi_len + 7);
1266 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1267 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1268 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1269 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1270 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1272 vsllink_tap_append_scan_normal(scan_size, buffer, command, 8);
1274 vsllink_end_state(saved_end_state);
1276 bits_left = scan_size & 0x07;
1277 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1281 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1285 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1288 if (tap_get_state() != tap_get_end_state())
1290 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1294 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1297 tap_set_state(tap_get_end_state());
1299 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
1301 tap_state_t saved_end_state;
1303 saved_end_state = tap_get_end_state();
1305 /* Move to appropriate scan state */
1306 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1308 vsllink_state_move();
1309 vsllink_end_state(saved_end_state);
1312 vsllink_tap_append_scan_dma(scan_size, buffer, command);
1314 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1315 while (tap_length % 8 != 0)
1318 vsllink_tap_append_step(0, 0);
1321 if (tap_get_state() != tap_get_end_state())
1323 vsllink_state_move();
1327 static void vsllink_reset(int trst, int srst)
1331 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1333 /* Signals are active low */
1334 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1335 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1336 vsllink_usb_out_buffer[2] = 0;
1339 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1343 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1346 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1349 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1353 static void vsllink_simple_command(u8 command)
1357 DEBUG_JTAG_IO("0x%02x", command);
1359 vsllink_usb_out_buffer[0] = command;
1360 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1364 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1368 static int vsllink_register_commands(struct command_context_s *cmd_ctx)
1370 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
1371 COMMAND_CONFIG, NULL);
1372 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
1373 COMMAND_CONFIG, NULL);
1374 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
1375 COMMAND_CONFIG, NULL);
1376 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
1377 COMMAND_CONFIG, NULL);
1378 register_command(cmd_ctx, NULL, "vsllink_usb_interface", vsllink_handle_usb_interface_command,
1379 COMMAND_CONFIG, NULL);
1380 register_command(cmd_ctx, NULL, "vsllink_mode", vsllink_handle_mode_command,
1381 COMMAND_CONFIG, NULL);
1386 static int vsllink_handle_mode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1389 LOG_ERROR("parameter error, should be one parameter for VID");
1393 if (!strcmp(args[0], "normal"))
1395 vsllink_mode = VSLLINK_MODE_NORMAL;
1397 else if (!strcmp(args[0], "dma"))
1399 vsllink_mode = VSLLINK_MODE_DMA;
1403 LOG_ERROR("invalid vsllink_mode: %s", args[0]);
1410 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1414 LOG_ERROR("parameter error, should be one parameter for VID");
1418 vsllink_usb_vid = strtol(args[0], NULL, 0);
1423 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1427 LOG_ERROR("parameter error, should be one parameter for PID");
1431 vsllink_usb_pid = strtol(args[0], NULL, 0);
1436 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1440 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1444 vsllink_usb_bulkin = strtol(args[0], NULL, 0) | 0x80;
1449 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1453 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1457 vsllink_usb_bulkout = strtol(args[0], NULL, 0);
1462 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1466 LOG_ERROR("parameter error, should be one parameter for interface number");
1470 vsllink_usb_interface = strtol(args[0], NULL, 0);
1475 /***************************************************************************/
1476 /* VSLLink tap functions */
1478 static void vsllink_tap_init_normal(void)
1480 vsllink_usb_out_buffer_idx = 0;
1481 vsllink_usb_in_want_length = 0;
1482 pending_scan_results_length = 0;
1484 static void vsllink_tap_init_dma(void)
1487 pending_scan_results_length = 0;
1490 static void vsllink_tap_ensure_space_normal(int scans, int length)
1492 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1493 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1495 if (scans > available_scans || length > available_bytes)
1497 vsllink_tap_execute();
1500 static void vsllink_tap_ensure_space_dma(int scans, int length)
1502 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1503 int available_bytes = tap_buffer_size * 8 - tap_length;
1505 if (scans > available_scans || length > available_bytes)
1507 vsllink_tap_execute();
1511 static void vsllink_tap_append_step(int tms, int tdi)
1514 int index = tap_length / 8;
1516 if (index < tap_buffer_size)
1518 int bit_index = tap_length % 8;
1519 u8 bit = 1 << bit_index;
1523 tms_buffer[index] |= bit;
1527 tms_buffer[index] &= ~bit;
1532 tdi_buffer[index] |= bit;
1536 tdi_buffer[index] &= ~bit;
1543 LOG_ERROR("buffer overflow, tap_length=%d", tap_length);
1547 static void vsllink_tap_append_scan_normal(int length, u8 *buffer, scan_command_t *command, int offset)
1549 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1554 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1558 vsllink_usb_in_want_length += (length + 7) >> 3;
1560 pending_scan_result->length = length;
1561 pending_scan_result->offset = offset;
1562 pending_scan_result->command = command;
1563 pending_scan_result->buffer = buffer;
1565 for (i = 0; i < ((length + 7) >> 3); i++)
1567 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1570 pending_scan_results_length++;
1572 static void vsllink_tap_append_scan_dma(int length, u8 *buffer, scan_command_t *command)
1574 pending_scan_result_t *pending_scan_result;
1575 int len_tmp, len_all, i;
1578 while (len_all < length)
1580 if ((length - len_all) > tap_buffer_size * 8)
1582 len_tmp = tap_buffer_size * 8;
1586 len_tmp = length - len_all;
1589 vsllink_tap_ensure_space(1, (len_tmp + 7) & ~7);
1591 pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1592 pending_scan_result->offset = tap_length;
1593 pending_scan_result->length = len_tmp;
1594 pending_scan_result->command = command;
1595 pending_scan_result->buffer = buffer + len_all / 8;
1597 for (i = 0; i < len_tmp; i++)
1599 vsllink_tap_append_step(((len_all+i) < length-1 ? 0 : 1), (buffer[(len_all+i)/8] >> ((len_all+i)%8)) & 1);
1602 pending_scan_results_length++;
1607 /* Pad and send a tap sequence to the device, and receive the answer.
1608 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1609 static int vsllink_tap_execute_normal(void)
1615 if (vsllink_tms_data_len > 0)
1617 if((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1619 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1622 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1624 // last tms bit is '1'
1625 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1626 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1627 vsllink_tms_data_len = 0;
1631 // last tms bit is '0'
1632 vsllink_usb_out_buffer_idx++;
1633 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1634 vsllink_tms_data_len = 0;
1638 if (vsllink_usb_out_buffer_idx > 3)
1640 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1642 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1643 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1646 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1648 if (result == vsllink_usb_in_want_length)
1650 for (i = 0; i < pending_scan_results_length; i++)
1652 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1653 u8 *buffer = pending_scan_result->buffer;
1654 int length = pending_scan_result->length;
1655 int offset = pending_scan_result->offset;
1656 scan_command_t *command = pending_scan_result->command;
1660 // IRSHIFT or DRSHIFT
1661 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1662 first += (length + offset + 7) >> 3;
1664 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1665 #ifdef _DEBUG_JTAG_IO_
1666 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1669 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1672 return ERROR_JTAG_QUEUE_FAILED;
1675 free(pending_scan_result->buffer);
1676 pending_scan_result->buffer = NULL;
1686 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1687 return ERROR_JTAG_QUEUE_FAILED;
1692 reset_command_pointer();
1696 static int vsllink_tap_execute_dma(void)
1704 /* Pad last byte so that tap_length is divisible by 8 */
1705 while (tap_length % 8 != 0)
1707 /* More of the last TMS value keeps us in the same state,
1708 * analogous to free-running JTAG interfaces. */
1709 vsllink_tap_append_step(last_tms, 0);
1711 byte_length = tap_length / 8;
1713 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
1714 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff; // package size
1715 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
1717 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
1718 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
1720 result = vsllink_usb_message(vsllink_jtag_handle, 3 + 2 * byte_length, byte_length);
1721 if (result == byte_length)
1723 for (i = 0; i < pending_scan_results_length; i++)
1725 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1726 u8 *buffer = pending_scan_result->buffer;
1727 int length = pending_scan_result->length;
1728 int first = pending_scan_result->offset;
1730 scan_command_t *command = pending_scan_result->command;
1731 buf_set_buf(vsllink_usb_in_buffer, first, buffer, 0, length);
1733 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length, first);
1734 #ifdef _DEBUG_JTAG_IO_
1735 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1738 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1741 return ERROR_JTAG_QUEUE_FAILED;
1744 if (pending_scan_result->buffer != NULL)
1746 free(pending_scan_result->buffer);
1752 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, byte_length);
1753 return ERROR_JTAG_QUEUE_FAILED;
1762 /*****************************************************************************/
1763 /* VSLLink USB low-level functions */
1765 static vsllink_jtag_t* vsllink_usb_open(void)
1767 struct usb_bus *busses;
1768 struct usb_bus *bus;
1769 struct usb_device *dev;
1772 vsllink_jtag_t *result;
1774 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1780 busses = usb_get_busses();
1782 /* find vsllink_jtag device in usb bus */
1784 for (bus = busses; bus; bus = bus->next)
1786 for (dev = bus->devices; dev; dev = dev->next)
1788 if ((dev->descriptor.idVendor == vsllink_usb_vid) && (dev->descriptor.idProduct == vsllink_usb_pid))
1790 result->usb_handle = usb_open(dev);
1791 if (NULL == result->usb_handle)
1793 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid, vsllink_usb_pid);
1797 /* usb_set_configuration required under win32 */
1798 ret = usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1801 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev->config[0].bConfigurationValue, ret);
1804 ret = usb_claim_interface(result->usb_handle, vsllink_usb_interface);
1807 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface, ret);
1813 * This makes problems under Mac OS X. And is not needed
1814 * under Windows. Hopefully this will not break a linux build
1816 usb_set_altinterface(result->usb_handle, 0);
1827 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1831 ret = usb_release_interface(vsllink_jtag->usb_handle, vsllink_usb_interface);
1834 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface, ret);
1838 ret = usb_close(vsllink_jtag->usb_handle);
1841 LOG_ERROR("fail to close usb, %d returned", ret);
1848 /* Send a message and receive the reply. */
1849 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1853 result = vsllink_usb_write(vsllink_jtag, out_length);
1854 if (result == out_length)
1858 result = vsllink_usb_read(vsllink_jtag);
1859 if (result == in_length )
1865 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1873 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1878 /* Write data from out_buffer to USB. */
1879 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1883 if (out_length > VSLLINK_BufferSize)
1885 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1889 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_usb_bulkout, \
1890 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1892 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1894 #ifdef _DEBUG_USB_COMMS_
1895 LOG_DEBUG("USB out:");
1896 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1899 #ifdef _VSLLINK_IN_DEBUG_MODE_
1906 /* Read data from USB into in_buffer. */
1907 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1909 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_usb_bulkin, \
1910 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1912 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1914 #ifdef _DEBUG_USB_COMMS_
1915 LOG_DEBUG("USB in:");
1916 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1921 #define BYTES_PER_LINE 16
1923 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1924 static void vsllink_debug_buffer(u8 *buffer, int length)
1931 for (i = 0; i < length; i += BYTES_PER_LINE)
1933 snprintf(line, 5, "%04x", i);
1934 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1936 snprintf(s, 4, " %02x", buffer[j]);
1939 LOG_DEBUG("%s", line);
1942 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_