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.
29 #include "interface.h"
35 //#define _VSLLINK_IN_DEBUG_MODE_
37 #define VSLLINK_MODE_NORMAL 0
38 #define VSLLINK_MODE_DMA 1
40 static uint16_t vsllink_usb_vid;
41 static uint16_t vsllink_usb_pid;
42 static uint8_t vsllink_usb_bulkout;
43 static uint8_t vsllink_usb_bulkin;
44 static uint8_t vsllink_usb_interface;
45 static uint8_t vsllink_mode = VSLLINK_MODE_NORMAL;
46 static int VSLLINK_USB_TIMEOUT = 10000;
48 static int VSLLINK_BufferSize = 1024;
50 /* Global USB buffers */
51 static int vsllink_usb_out_buffer_idx;
52 static int vsllink_usb_in_want_length;
53 static uint8_t* vsllink_usb_in_buffer = NULL;
54 static uint8_t* vsllink_usb_out_buffer = NULL;
56 /* Constants for VSLLink command */
57 #define VSLLINK_CMD_CONN 0x80
58 #define VSLLINK_CMD_DISCONN 0x81
59 #define VSLLINK_CMD_SET_SPEED 0x82
60 #define VSLLINK_CMD_SET_PORT 0x90
61 #define VSLLINK_CMD_GET_PORT 0x91
62 #define VSLLINK_CMD_SET_PORTDIR 0x92
63 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
64 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
65 #define VSLLINK_CMD_HW_SWDCMD 0xA2
66 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
68 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
69 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
70 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
72 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
73 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
75 #define JTAG_PINMSK_SRST (1 << 0)
76 #define JTAG_PINMSK_TRST (1 << 1)
77 #define JTAG_PINMSK_USR1 (1 << 2)
78 #define JTAG_PINMSK_USR2 (1 << 3)
79 #define JTAG_PINMSK_TCK (1 << 4)
80 #define JTAG_PINMSK_TMS (1 << 5)
81 #define JTAG_PINMSK_TDI (1 << 6)
82 #define JTAG_PINMSK_TDO (1 << 7)
85 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
87 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
95 * SD->SD and SI->SI have to be caught in interface specific code
97 static uint8_t VSLLINK_tap_move[6][6] =
99 /* TLR RTI SD PD SI PI */
100 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
101 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
102 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
103 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
104 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
105 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
108 typedef struct insert_insignificant_operation
110 unsigned char insert_value;
111 unsigned char insert_position;
112 }insert_insignificant_operation_t;
114 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
161 static uint8_t VSLLINK_BIT_MSK[8] =
163 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
166 struct pending_scan_result {
168 int length; /* Number of bits to read */
169 scan_command_t *command; /* Corresponding scan command */
173 #define MAX_PENDING_SCAN_RESULTS 256
175 static int pending_scan_results_length;
176 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
178 /* Queue command functions */
179 static void vsllink_end_state(tap_state_t state);
180 static void vsllink_state_move_dma(void);
181 static void vsllink_state_move_normal(void);
182 static void (*vsllink_state_move)(void);
183 static void vsllink_path_move_dma(int num_states, tap_state_t *path);
184 static void vsllink_path_move_normal(int num_states, tap_state_t *path);
185 static void (*vsllink_path_move)(int num_states, tap_state_t *path);
186 static void vsllink_runtest(int num_cycles);
187 static void vsllink_stableclocks_dma(int num_cycles, int tms);
188 static void vsllink_stableclocks_normal(int num_cycles, int tms);
189 static void (*vsllink_stableclocks)(int num_cycles, int tms);
190 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
191 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
192 static void (*vsllink_scan)(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
193 static void vsllink_reset(int trst, int srst);
194 static void vsllink_simple_command(uint8_t command);
195 static int vsllink_connect(void);
196 static int vsllink_disconnect(void);
198 /* VSLLink tap buffer functions */
199 static void vsllink_tap_append_step(int tms, int tdi);
200 static void vsllink_tap_init_dma(void);
201 static void vsllink_tap_init_normal(void);
202 static void (*vsllink_tap_init)(void);
203 static int vsllink_tap_execute_dma(void);
204 static int vsllink_tap_execute_normal(void);
205 static int (*vsllink_tap_execute)(void);
206 static void vsllink_tap_ensure_space_dma(int scans, int length);
207 static void vsllink_tap_ensure_space_normal(int scans, int length);
208 static void (*vsllink_tap_ensure_space)(int scans, int length);
209 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, scan_command_t *command);
210 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, scan_command_t *command, int offset);
212 /* VSLLink lowlevel functions */
214 struct usb_dev_handle* usb_handle;
217 static struct vsllink *vsllink_usb_open(void);
218 static void vsllink_usb_close(struct vsllink *vsllink);
219 static int vsllink_usb_message(struct vsllink *vsllink, int out_length, int in_length);
220 static int vsllink_usb_write(struct vsllink *vsllink, int out_length);
221 static int vsllink_usb_read(struct vsllink *vsllink);
223 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
224 static void vsllink_debug_buffer(uint8_t *buffer, int length);
227 static int vsllink_tms_data_len = 0;
228 static uint8_t* vsllink_tms_cmd_pos;
230 static int tap_length = 0;
231 static int tap_buffer_size = 0;
232 static uint8_t *tms_buffer = NULL;
233 static uint8_t *tdi_buffer = NULL;
234 static uint8_t *tdo_buffer = NULL;
237 static struct vsllink* vsllink_handle = NULL;
239 static void reset_command_pointer(void)
241 if (vsllink_mode == VSLLINK_MODE_NORMAL)
243 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
244 vsllink_usb_out_buffer_idx = 3;
252 static int vsllink_execute_queue(void)
254 jtag_command_t *cmd = jtag_command_queue;
259 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
261 reset_command_pointer();
267 DEBUG_JTAG_IO("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
268 tap_state_name(cmd->cmd.runtest->end_state));
270 vsllink_end_state(cmd->cmd.runtest->end_state);
271 vsllink_runtest(cmd->cmd.runtest->num_cycles);
275 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
277 vsllink_end_state(cmd->cmd.statemove->end_state);
278 vsllink_state_move();
282 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
283 cmd->cmd.pathmove->num_states, \
284 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
286 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
290 vsllink_end_state(cmd->cmd.scan->end_state);
292 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
293 if (cmd->cmd.scan->ir_scan)
295 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
299 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
302 #ifdef _DEBUG_JTAG_IO_
303 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
306 type = jtag_scan_type(cmd->cmd.scan);
308 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
312 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
314 vsllink_tap_execute();
316 if (cmd->cmd.reset->trst == 1)
318 tap_set_state(TAP_RESET);
320 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
324 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
325 vsllink_tap_execute();
326 jtag_sleep(cmd->cmd.sleep->us);
329 case JTAG_STABLECLOCKS:
330 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
331 switch (tap_get_state())
334 // tms should be '1' to stay in TAP_RESET mode
342 // in other mode, tms should be '0'
344 break; /* above stable states are OK */
346 LOG_ERROR("jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
347 tap_state_name(tap_get_state()));
350 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
354 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
360 return vsllink_tap_execute();
363 static int vsllink_speed(int speed)
367 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
368 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
369 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
371 result = vsllink_usb_write(vsllink_handle, 3);
379 LOG_ERROR("VSLLink setting speed failed (%d)", result);
380 return ERROR_JTAG_DEVICE_ERROR;
386 static int vsllink_khz(int khz, int *jtag_speed)
393 static int vsllink_speed_div(int jtag_speed, int *khz)
400 static int vsllink_init(void)
402 int check_cnt, to_tmp;
404 char version_str[100];
406 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
407 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
408 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
410 LOG_ERROR("Not enough memory");
414 vsllink_handle = vsllink_usb_open();
416 if (vsllink_handle == 0)
418 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
419 return ERROR_JTAG_INIT_FAILED;
421 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid, vsllink_usb_pid);
423 to_tmp = VSLLINK_USB_TIMEOUT;
424 VSLLINK_USB_TIMEOUT = 100;
426 while (check_cnt < 5)
428 vsllink_simple_command(0x00);
429 result = vsllink_usb_read(vsllink_handle);
433 vsllink_usb_in_buffer[result] = 0;
434 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
435 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
436 LOG_INFO("%s", version_str);
438 // free the pre-alloc memroy
439 free(vsllink_usb_in_buffer);
440 free(vsllink_usb_out_buffer);
441 vsllink_usb_in_buffer = NULL;
442 vsllink_usb_out_buffer = NULL;
445 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
446 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
447 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
449 LOG_ERROR("Not enough memory");
454 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
456 // alloc memory for dma mode
457 if (vsllink_mode == VSLLINK_MODE_DMA)
459 tap_buffer_size = (VSLLINK_BufferSize - 3) / 2;
460 tms_buffer = (uint8_t*)malloc(tap_buffer_size);
461 tdi_buffer = (uint8_t*)malloc(tap_buffer_size);
462 tdo_buffer = (uint8_t*)malloc(tap_buffer_size);
463 if ((tms_buffer == NULL) || (tdi_buffer == NULL) || (tdo_buffer == NULL))
465 LOG_ERROR("Not enough memory");
471 vsllink_simple_command(VSLLINK_CMD_DISCONN);
476 // It's dangerout to proced
477 LOG_ERROR("VSLLink initial failed");
480 VSLLINK_USB_TIMEOUT = to_tmp;
482 // connect to vsllink
484 // initialize function pointers
485 if (vsllink_mode == VSLLINK_MODE_NORMAL)
488 vsllink_state_move = vsllink_state_move_normal;
489 vsllink_path_move = vsllink_path_move_normal;
490 vsllink_stableclocks = vsllink_stableclocks_normal;
491 vsllink_scan = vsllink_scan_normal;
493 vsllink_tap_init = vsllink_tap_init_normal;
494 vsllink_tap_execute = vsllink_tap_execute_normal;
495 vsllink_tap_ensure_space = vsllink_tap_ensure_space_normal;
497 LOG_INFO("vsllink run in NORMAL mode");
502 vsllink_state_move = vsllink_state_move_dma;
503 vsllink_path_move = vsllink_path_move_dma;
504 vsllink_stableclocks = vsllink_stableclocks_dma;
505 vsllink_scan = vsllink_scan_dma;
507 vsllink_tap_init = vsllink_tap_init_dma;
508 vsllink_tap_execute = vsllink_tap_execute_dma;
509 vsllink_tap_ensure_space = vsllink_tap_ensure_space_dma;
511 LOG_INFO("vsllink run in DMA mode");
514 // Set SRST and TRST to output, Set USR1 and USR2 to input
515 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
516 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
517 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
518 if (vsllink_usb_write(vsllink_handle, 3) != 3)
520 LOG_ERROR("VSLLink USB send data error");
526 LOG_INFO("VSLLink JTAG Interface ready");
533 static int vsllink_quit(void)
535 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
537 // Set all pins to input
538 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
539 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
540 vsllink_usb_out_buffer[2] = 0;
541 if (vsllink_usb_write(vsllink_handle, 3) != 3)
543 LOG_ERROR("VSLLink USB send data error");
548 vsllink_disconnect();
549 vsllink_usb_close(vsllink_handle);
550 vsllink_handle = NULL;
553 if (vsllink_usb_in_buffer != NULL)
555 free(vsllink_usb_in_buffer);
556 vsllink_usb_in_buffer = NULL;
558 if (vsllink_usb_out_buffer != NULL)
560 free(vsllink_usb_out_buffer);
561 vsllink_usb_out_buffer = NULL;
567 /***************************************************************************/
568 /* Queue command implementations */
569 static int vsllink_disconnect(void)
571 vsllink_simple_command(VSLLINK_CMD_DISCONN);
575 static int vsllink_connect(void)
577 char vsllink_str[100];
579 vsllink_usb_out_buffer[0] = VSLLINK_CMD_CONN;
580 vsllink_usb_out_buffer[1] = vsllink_mode;
581 vsllink_usb_message(vsllink_handle, 2, 0);
582 if (vsllink_usb_read(vsllink_handle) > 2)
584 strncpy(vsllink_str, (char *)vsllink_usb_in_buffer + 2, sizeof(vsllink_str));
585 LOG_INFO("%s", vsllink_str);
591 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
592 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
593 static void vsllink_append_tms(void)
595 uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
597 insert_insignificant_operation_t *insert = \
598 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
600 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
601 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
602 (vsllink_tms_cmd_pos == NULL))
604 LOG_ERROR("There MUST be some bugs in the driver");
608 tms2 = (tms_scan & VSLLINK_BIT_MSK[insert->insert_position]) << \
609 vsllink_tms_data_len;
610 if (insert->insert_value == 1)
612 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
613 (vsllink_tms_data_len + insert->insert_position);
615 tms2 |= (tms_scan >> insert->insert_position) << \
616 (8 + insert->insert_position);
618 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
619 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
621 vsllink_tms_data_len = 0;
622 vsllink_tms_cmd_pos = NULL;
625 static void vsllink_end_state(tap_state_t state)
627 if (tap_is_state_stable(state))
629 tap_set_end_state(state);
633 LOG_ERROR("BUG: %i is not a valid end state", state);
638 /* Goes to the end state. */
639 static void vsllink_state_move_normal(void)
641 if (vsllink_tms_data_len > 0)
643 vsllink_append_tms();
647 vsllink_tap_ensure_space(0, 2);
649 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
650 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
653 tap_set_state(tap_get_end_state());
655 static void vsllink_state_move_dma(void)
657 int i, insert_length = (tap_length % 8) ? (8 - (tap_length % 8)) : 0;
658 insert_insignificant_operation_t *insert = \
659 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
660 uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
662 if (tap_get_state() == TAP_RESET)
664 vsllink_tap_ensure_space(0, 8);
666 for (i = 0; i < 8; i++)
668 vsllink_tap_append_step(1, 0);
672 if (insert_length > 0)
674 vsllink_tap_ensure_space(0, 16);
676 for (i = 0; i < insert->insert_position; i++)
678 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
680 for (i = 0; i < insert_length; i++)
682 vsllink_tap_append_step(insert->insert_value, 0);
684 for (i = insert->insert_position; i < 8; i++)
686 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
691 vsllink_tap_ensure_space(0, 8);
693 for (i = 0; i < 8; i++)
695 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
699 tap_set_state(tap_get_end_state());
702 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
703 static void vsllink_add_path(int start, int num, tap_state_t *path)
707 for (i = start; i < (start + num); i++)
713 vsllink_usb_out_buffer_idx++;
715 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
718 if (path[i - start] == tap_state_transition(tap_get_state(), true))
720 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
722 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
728 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
731 tap_set_state(path[i - start]);
733 if ((i > 0) && ((i & 7) == 0))
735 vsllink_usb_out_buffer_idx++;
736 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
739 tap_set_end_state(tap_get_state());
742 static void vsllink_path_move_normal(int num_states, tap_state_t *path)
744 int i, tms_len, tms_cmd_pos, path_idx = 0;
746 if (vsllink_tms_data_len > 0)
748 // there are vsllink_tms_data_len more tms bits to be shifted
749 // so there are vsllink_tms_data_len + num_states tms bits in all
750 tms_len = vsllink_tms_data_len + num_states;
753 // merge into last tms shift
756 // just append tms data to the last tms byte
757 vsllink_add_path(vsllink_tms_data_len, num_states, path);
759 else if (tms_len == 8)
761 // end last tms shift command
762 (*vsllink_tms_cmd_pos)--;
763 vsllink_add_path(vsllink_tms_data_len, num_states, path);
765 else if (tms_len < 16)
767 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
769 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
770 // there is enought tms length in the current tms shift command
771 (*vsllink_tms_cmd_pos)++;
772 vsllink_add_path(vsllink_tms_data_len, num_states, path);
776 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
777 // not enough tms length in the current tms shift command
778 // so a new command should be added
779 // first decrease byte length of last tms shift command
780 (*vsllink_tms_cmd_pos)--;
781 // append tms data to the last tms byte
782 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
783 path += 8 - vsllink_tms_data_len;
784 // add new command(3 bytes)
785 vsllink_tap_ensure_space(0, 3);
786 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
787 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
788 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
791 else if (tms_len == 16)
793 // end last tms shift command
794 vsllink_add_path(vsllink_tms_data_len, num_states, path);
797 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
798 if (vsllink_tms_data_len == 0)
800 vsllink_tms_cmd_pos = NULL;
806 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
808 path += 16 - vsllink_tms_data_len;
809 num_states -= 16 - vsllink_tms_data_len;
810 vsllink_tms_data_len = 0;
811 vsllink_tms_cmd_pos = NULL;
817 // Normal operation, don't need to append tms data
818 vsllink_tms_data_len = num_states & 7;
820 while (num_states > 0)
822 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
824 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
830 tms_len = (i + 7) >> 3;
831 vsllink_tap_ensure_space(0, tms_len + 2);
832 tms_cmd_pos = vsllink_usb_out_buffer_idx;
833 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
835 vsllink_add_path(0, i, path + path_idx);
841 if (vsllink_tms_data_len > 0)
843 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
845 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
846 (*vsllink_tms_cmd_pos)++;
850 vsllink_usb_out_buffer[tms_cmd_pos]--;
852 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
853 vsllink_tap_ensure_space(0, 3);
854 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
855 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
856 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
861 static void vsllink_path_move_dma(int num_states, tap_state_t *path)
867 if ((8 - (tap_length & 7)) < num_states)
869 j = 8 - (tap_length & 7);
875 for (i = 0; i < j; i++)
877 if (path[i] == tap_state_transition(tap_get_state(), false))
879 vsllink_tap_append_step(0, 0);
881 else if (path[i] == tap_state_transition(tap_get_state(), true))
883 vsllink_tap_append_step(1, 0);
887 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
890 tap_set_state(path[i]);
897 vsllink_tap_ensure_space(0, num_states);
899 for (i = 0; i < num_states; i++)
901 if (path[j + i] == tap_state_transition(tap_get_state(), false))
903 vsllink_tap_append_step(0, 0);
905 else if (path[j + i] == tap_state_transition(tap_get_state(), true))
907 vsllink_tap_append_step(1, 0);
911 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
914 tap_set_state(path[j + i]);
918 tap_set_end_state(tap_get_state());
921 static void vsllink_stableclocks_normal(int num_cycles, int tms)
924 uint16_t tms_append_byte;
926 if (vsllink_tms_data_len > 0)
928 // there are vsllink_tms_data_len more tms bits to be shifted
929 // so there are vsllink_tms_data_len + num_cycles tms bits in all
930 tms_len = vsllink_tms_data_len + num_cycles;
933 // append '1' for tms
934 tms_append_byte = (uint16_t)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
938 // append '0' for tms
943 // merge into last tms shift
946 // just add to vsllink_tms_data_len
947 // same result if tun through
948 //vsllink_tms_data_len += num_cycles;
949 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (uint8_t)(tms_append_byte & 0xFF);
951 else if (tms_len == 8)
953 // end last tms shift command
954 // just reduce it, and append last tms byte
955 (*vsllink_tms_cmd_pos)--;
956 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
958 else if (tms_len < 16)
960 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
962 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
963 // there is enought tms length in the current tms shift command
964 // increase the tms byte length by 1 and set the last byte to 0
965 (*vsllink_tms_cmd_pos)++;
966 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
967 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
971 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
972 // not enough tms length in the current tms shift command
973 // so a new command should be added
974 // first decrease byte length of last tms shift command
975 (*vsllink_tms_cmd_pos)--;
976 // append last tms byte and move the command pointer to the next empty position
977 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
978 // add new command(3 bytes)
979 vsllink_tap_ensure_space(0, 3);
980 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
981 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
982 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
985 else if (tms_len == 16)
987 // end last tms shift command
988 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
989 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
992 vsllink_tms_data_len = tms_len & 7;
993 if (vsllink_tms_data_len == 0)
995 vsllink_tms_cmd_pos = NULL;
1001 // more shifts will be needed
1002 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
1003 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
1005 num_cycles -= 16 - vsllink_tms_data_len;
1006 vsllink_tms_data_len = 0;
1007 vsllink_tms_cmd_pos = NULL;
1010 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1012 if (vsllink_tms_data_len > 0)
1018 LOG_ERROR("There MUST be some bugs in the driver");
1024 // get number of bytes left to be sent
1025 tms_len = num_cycles >> 3;
1028 vsllink_tap_ensure_space(1, 5);
1029 // if tms_len > 0, vsllink_tms_data_len == 0
1030 // so just add new command
1031 // LSB of the command byte is the tms value when do the shifting
1034 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
1038 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
1040 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
1041 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
1042 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
1043 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
1045 vsllink_usb_in_want_length += 1;
1046 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
1047 pending_scan_results_length++;
1049 if (tms_len > 0xFFFF)
1051 vsllink_tap_execute();
1056 vsllink_tms_data_len = num_cycles & 7;
1057 if (vsllink_tms_data_len > 0)
1059 vsllink_tap_ensure_space(0, 3);
1060 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1061 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1064 // append '1' for tms
1065 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
1069 // append '0' for tms
1070 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
1075 static void vsllink_stableclocks_dma(int num_cycles, int tms)
1081 if ((8 - (tap_length & 7)) < num_cycles)
1083 cur_cycles = 8 - (tap_length & 7);
1087 cur_cycles = num_cycles;
1089 for (i = 0; i < cur_cycles; i++)
1091 vsllink_tap_append_step(tms, 0);
1093 num_cycles -= cur_cycles;
1096 while (num_cycles > 0)
1098 if (num_cycles > 8 * tap_buffer_size)
1100 cur_cycles = 8 * tap_buffer_size;
1104 cur_cycles = num_cycles;
1107 vsllink_tap_ensure_space(0, cur_cycles);
1109 for (i = 0; i < cur_cycles; i++)
1111 vsllink_tap_append_step(tms, 0);
1114 num_cycles -= cur_cycles;
1118 static void vsllink_runtest(int num_cycles)
1120 tap_state_t saved_end_state = tap_get_end_state();
1122 if (tap_get_state() != TAP_IDLE)
1124 // enter into IDLE state
1125 vsllink_end_state(TAP_IDLE);
1126 vsllink_state_move();
1129 vsllink_stableclocks(num_cycles, 0);
1133 vsllink_end_state(saved_end_state);
1134 tap_set_state(TAP_IDLE);
1135 if (tap_get_end_state() != TAP_IDLE)
1137 vsllink_state_move();
1141 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command)
1143 tap_state_t saved_end_state;
1144 uint8_t bits_left, tms_tmp, tdi_len;
1152 tdi_len = ((scan_size + 7) >> 3);
1153 if ((tdi_len + 7) > VSLLINK_BufferSize)
1155 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1159 saved_end_state = tap_get_end_state();
1161 /* Move to appropriate scan state */
1162 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1164 if (vsllink_tms_data_len > 0)
1166 if (tap_get_state() == tap_get_end_state())
1168 // already in IRSHIFT or DRSHIFT state
1169 // merge tms data in the last tms shift command into next scan command
1170 if (*vsllink_tms_cmd_pos < 1)
1172 LOG_ERROR("There MUST be some bugs in the driver");
1175 else if (*vsllink_tms_cmd_pos < 2)
1177 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1178 vsllink_usb_out_buffer_idx--;
1182 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1183 *vsllink_tms_cmd_pos -= 2;
1186 vsllink_tap_ensure_space(1, tdi_len + 7);
1187 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1188 // which is merged from the last tms shift command
1189 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1190 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1191 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 8) & 0xff;
1192 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1193 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1195 for (i = 0; i < tdi_len; i++)
1197 buffer[i] >>= 8 - vsllink_tms_data_len;
1200 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1204 vsllink_tap_append_scan_normal(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1205 scan_size -= 8 - vsllink_tms_data_len;
1206 vsllink_tms_data_len = 0;
1210 vsllink_state_move();
1211 vsllink_tap_ensure_space(1, tdi_len + 5);
1213 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1214 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1215 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1217 vsllink_tap_append_scan_normal(scan_size, buffer, command, 0);
1222 vsllink_tap_ensure_space(1, tdi_len + 7);
1224 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1225 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1226 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1227 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1228 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1230 vsllink_tap_append_scan_normal(scan_size, buffer, command, 8);
1232 vsllink_end_state(saved_end_state);
1234 bits_left = scan_size & 0x07;
1235 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1239 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1243 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1246 if (tap_get_state() != tap_get_end_state())
1248 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1252 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1255 tap_set_state(tap_get_end_state());
1257 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command)
1259 tap_state_t saved_end_state;
1261 saved_end_state = tap_get_end_state();
1263 /* Move to appropriate scan state */
1264 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1266 vsllink_state_move();
1267 vsllink_end_state(saved_end_state);
1270 vsllink_tap_append_scan_dma(scan_size, buffer, command);
1272 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1273 while (tap_length % 8 != 0)
1276 vsllink_tap_append_step(0, 0);
1279 if (tap_get_state() != tap_get_end_state())
1281 vsllink_state_move();
1285 static void vsllink_reset(int trst, int srst)
1289 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1291 /* Signals are active low */
1292 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1293 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1294 vsllink_usb_out_buffer[2] = 0;
1297 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1301 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1304 result = vsllink_usb_write(vsllink_handle, 3);
1307 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1311 static void vsllink_simple_command(uint8_t command)
1315 DEBUG_JTAG_IO("0x%02x", command);
1317 vsllink_usb_out_buffer[0] = command;
1318 result = vsllink_usb_write(vsllink_handle, 1);
1322 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1326 COMMAND_HANDLER(vsllink_handle_mode_command)
1329 LOG_ERROR("parameter error, should be one parameter for VID");
1333 if (!strcmp(args[0], "normal"))
1335 vsllink_mode = VSLLINK_MODE_NORMAL;
1337 else if (!strcmp(args[0], "dma"))
1339 vsllink_mode = VSLLINK_MODE_DMA;
1343 LOG_ERROR("invalid vsllink_mode: %s", args[0]);
1350 COMMAND_HANDLER(vsllink_handle_usb_vid_command)
1354 LOG_ERROR("parameter error, should be one parameter for VID");
1358 COMMAND_PARSE_NUMBER(u16, args[0], vsllink_usb_vid);
1362 COMMAND_HANDLER(vsllink_handle_usb_pid_command)
1366 LOG_ERROR("parameter error, should be one parameter for PID");
1369 COMMAND_PARSE_NUMBER(u16, args[0], vsllink_usb_pid);
1373 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
1377 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1381 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_bulkin);
1383 vsllink_usb_bulkin |= 0x80;
1388 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
1392 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1396 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_bulkout);
1398 vsllink_usb_bulkout &= ~0x80;
1403 COMMAND_HANDLER(vsllink_handle_usb_interface_command)
1407 LOG_ERROR("parameter error, should be one parameter for interface number");
1411 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_interface);
1415 /***************************************************************************/
1416 /* VSLLink tap functions */
1418 static void vsllink_tap_init_normal(void)
1420 vsllink_usb_out_buffer_idx = 0;
1421 vsllink_usb_in_want_length = 0;
1422 pending_scan_results_length = 0;
1424 static void vsllink_tap_init_dma(void)
1427 pending_scan_results_length = 0;
1430 static void vsllink_tap_ensure_space_normal(int scans, int length)
1432 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1433 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1435 if (scans > available_scans || length > available_bytes)
1437 vsllink_tap_execute();
1440 static void vsllink_tap_ensure_space_dma(int scans, int length)
1442 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1443 int available_bytes = tap_buffer_size * 8 - tap_length;
1445 if (scans > available_scans || length > available_bytes)
1447 vsllink_tap_execute();
1451 static void vsllink_tap_append_step(int tms, int tdi)
1454 int index = tap_length / 8;
1456 if (index < tap_buffer_size)
1458 int bit_index = tap_length % 8;
1459 uint8_t bit = 1 << bit_index;
1463 tms_buffer[index] |= bit;
1467 tms_buffer[index] &= ~bit;
1472 tdi_buffer[index] |= bit;
1476 tdi_buffer[index] &= ~bit;
1483 LOG_ERROR("buffer overflow, tap_length=%d", tap_length);
1487 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, scan_command_t *command, int offset)
1489 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1494 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1498 vsllink_usb_in_want_length += (length + 7) >> 3;
1500 pending_scan_result->length = length;
1501 pending_scan_result->offset = offset;
1502 pending_scan_result->command = command;
1503 pending_scan_result->buffer = buffer;
1505 for (i = 0; i < ((length + 7) >> 3); i++)
1507 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1510 pending_scan_results_length++;
1512 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, scan_command_t *command)
1514 struct pending_scan_result *pending_scan_result;
1515 int len_tmp, len_all, i;
1518 while (len_all < length)
1520 if ((length - len_all) > tap_buffer_size * 8)
1522 len_tmp = tap_buffer_size * 8;
1526 len_tmp = length - len_all;
1529 vsllink_tap_ensure_space(1, (len_tmp + 7) & ~7);
1531 pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1532 pending_scan_result->offset = tap_length;
1533 pending_scan_result->length = len_tmp;
1534 pending_scan_result->command = command;
1535 pending_scan_result->buffer = buffer + len_all / 8;
1537 for (i = 0; i < len_tmp; i++)
1539 vsllink_tap_append_step(((len_all + i) < length-1 ? 0 : 1), (buffer[(len_all + i)/8] >> ((len_all + i)%8)) & 1);
1542 pending_scan_results_length++;
1547 /* Pad and send a tap sequence to the device, and receive the answer.
1548 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1549 static int vsllink_tap_execute_normal(void)
1555 if (vsllink_tms_data_len > 0)
1557 if ((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1559 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1562 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1564 // last tms bit is '1'
1565 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1566 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1567 vsllink_tms_data_len = 0;
1571 // last tms bit is '0'
1572 vsllink_usb_out_buffer_idx++;
1573 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1574 vsllink_tms_data_len = 0;
1578 if (vsllink_usb_out_buffer_idx > 3)
1580 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1582 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1583 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1586 result = vsllink_usb_message(vsllink_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1588 if (result == vsllink_usb_in_want_length)
1590 for (i = 0; i < pending_scan_results_length; i++)
1592 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
1593 uint8_t *buffer = pending_scan_result->buffer;
1594 int length = pending_scan_result->length;
1595 int offset = pending_scan_result->offset;
1596 scan_command_t *command = pending_scan_result->command;
1600 // IRSHIFT or DRSHIFT
1601 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1602 first += (length + offset + 7) >> 3;
1604 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1605 #ifdef _DEBUG_JTAG_IO_
1606 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1609 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1612 return ERROR_JTAG_QUEUE_FAILED;
1615 free(pending_scan_result->buffer);
1616 pending_scan_result->buffer = NULL;
1626 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1627 return ERROR_JTAG_QUEUE_FAILED;
1632 reset_command_pointer();
1636 static int vsllink_tap_execute_dma(void)
1644 /* Pad last byte so that tap_length is divisible by 8 */
1645 while (tap_length % 8 != 0)
1647 /* More of the last TMS value keeps us in the same state,
1648 * analogous to free-running JTAG interfaces. */
1649 vsllink_tap_append_step(last_tms, 0);
1651 byte_length = tap_length / 8;
1653 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
1654 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff; // package size
1655 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
1657 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
1658 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
1660 result = vsllink_usb_message(vsllink_handle, 3 + 2 * byte_length, byte_length);
1661 if (result == byte_length)
1663 for (i = 0; i < pending_scan_results_length; i++)
1665 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
1666 uint8_t *buffer = pending_scan_result->buffer;
1667 int length = pending_scan_result->length;
1668 int first = pending_scan_result->offset;
1670 scan_command_t *command = pending_scan_result->command;
1671 buf_set_buf(vsllink_usb_in_buffer, first, buffer, 0, length);
1673 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length, first);
1674 #ifdef _DEBUG_JTAG_IO_
1675 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1678 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1681 return ERROR_JTAG_QUEUE_FAILED;
1684 if (pending_scan_result->buffer != NULL)
1686 free(pending_scan_result->buffer);
1692 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, byte_length);
1693 return ERROR_JTAG_QUEUE_FAILED;
1702 /*****************************************************************************/
1703 /* VSLLink USB low-level functions */
1705 static struct vsllink* vsllink_usb_open(void)
1707 struct usb_bus *busses;
1708 struct usb_bus *bus;
1709 struct usb_device *dev;
1712 struct vsllink *result;
1714 result = (struct vsllink*) malloc(sizeof(struct vsllink));
1720 busses = usb_get_busses();
1722 /* find vsllink device in usb bus */
1724 for (bus = busses; bus; bus = bus->next)
1726 for (dev = bus->devices; dev; dev = dev->next)
1728 if ((dev->descriptor.idVendor == vsllink_usb_vid) && (dev->descriptor.idProduct == vsllink_usb_pid))
1730 result->usb_handle = usb_open(dev);
1731 if (NULL == result->usb_handle)
1733 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid, vsllink_usb_pid);
1737 /* usb_set_configuration required under win32 */
1738 ret = usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1741 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev->config[0].bConfigurationValue, ret);
1744 ret = usb_claim_interface(result->usb_handle, vsllink_usb_interface);
1747 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface, ret);
1753 * This makes problems under Mac OS X. And is not needed
1754 * under Windows. Hopefully this will not break a linux build
1756 usb_set_altinterface(result->usb_handle, 0);
1767 static void vsllink_usb_close(struct vsllink *vsllink)
1771 ret = usb_release_interface(vsllink->usb_handle, vsllink_usb_interface);
1774 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface, ret);
1778 ret = usb_close(vsllink->usb_handle);
1781 LOG_ERROR("fail to close usb, %d returned", ret);
1788 /* Send a message and receive the reply. */
1789 static int vsllink_usb_message(struct vsllink *vsllink, int out_length, int in_length)
1793 result = vsllink_usb_write(vsllink, out_length);
1794 if (result == out_length)
1798 result = vsllink_usb_read(vsllink);
1799 if (result == in_length)
1805 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1813 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1818 /* Write data from out_buffer to USB. */
1819 static int vsllink_usb_write(struct vsllink *vsllink, int out_length)
1823 if (out_length > VSLLINK_BufferSize)
1825 LOG_ERROR("vsllink_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1829 result = usb_bulk_write(vsllink->usb_handle, vsllink_usb_bulkout, \
1830 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1832 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1834 #ifdef _DEBUG_USB_COMMS_
1835 LOG_DEBUG("USB out:");
1836 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1839 #ifdef _VSLLINK_IN_DEBUG_MODE_
1846 /* Read data from USB into in_buffer. */
1847 static int vsllink_usb_read(struct vsllink *vsllink)
1849 int result = usb_bulk_read(vsllink->usb_handle, vsllink_usb_bulkin, \
1850 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1852 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1854 #ifdef _DEBUG_USB_COMMS_
1855 LOG_DEBUG("USB in:");
1856 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1861 #define BYTES_PER_LINE 16
1863 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1864 static void vsllink_debug_buffer(uint8_t *buffer, int length)
1871 for (i = 0; i < length; i += BYTES_PER_LINE)
1873 snprintf(line, 5, "%04x", i);
1874 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1876 snprintf(s, 4, " %02x", buffer[j]);
1879 LOG_DEBUG("%s", line);
1882 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_
1884 static int vsllink_register_commands(struct command_context_s *cmd_ctx)
1886 register_command(cmd_ctx, NULL, "vsllink_usb_vid",
1887 vsllink_handle_usb_vid_command, COMMAND_CONFIG,
1889 register_command(cmd_ctx, NULL, "vsllink_usb_pid",
1890 vsllink_handle_usb_pid_command, COMMAND_CONFIG,
1892 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin",
1893 vsllink_handle_usb_bulkin_command, COMMAND_CONFIG,
1895 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout",
1896 vsllink_handle_usb_bulkout_command, COMMAND_CONFIG,
1898 register_command(cmd_ctx, NULL, "vsllink_usb_interface",
1899 vsllink_handle_usb_interface_command, COMMAND_CONFIG,
1901 register_command(cmd_ctx, NULL, "vsllink_mode",
1902 vsllink_handle_mode_command, COMMAND_CONFIG,
1908 struct jtag_interface vsllink_interface = {
1910 .register_commands = &vsllink_register_commands,
1911 .init = &vsllink_init,
1912 .quit = &vsllink_quit,
1913 .khz = &vsllink_khz,
1914 .speed = &vsllink_speed,
1915 .speed_div = &vsllink_speed_div,
1916 .execute_queue = &vsllink_execute_queue,