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 struct insert_insignificant_operation {
109 unsigned char insert_value;
110 unsigned char insert_position;
113 static struct insert_insignificant_operation VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
160 static uint8_t VSLLINK_BIT_MSK[8] =
162 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
165 struct pending_scan_result {
167 int length; /* Number of bits to read */
168 struct scan_command *command; /* Corresponding scan command */
172 #define MAX_PENDING_SCAN_RESULTS 256
174 static int pending_scan_results_length;
175 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
177 /* Queue command functions */
178 static void vsllink_end_state(tap_state_t state);
179 static void vsllink_state_move_dma(void);
180 static void vsllink_state_move_normal(void);
181 static void (*vsllink_state_move)(void);
182 static void vsllink_path_move_dma(int num_states, tap_state_t *path);
183 static void vsllink_path_move_normal(int num_states, tap_state_t *path);
184 static void (*vsllink_path_move)(int num_states, tap_state_t *path);
185 static void vsllink_runtest(int num_cycles);
186 static void vsllink_stableclocks_dma(int num_cycles, int tms);
187 static void vsllink_stableclocks_normal(int num_cycles, int tms);
188 static void (*vsllink_stableclocks)(int num_cycles, int tms);
189 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command);
190 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command);
191 static void (*vsllink_scan)(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command);
192 static void vsllink_reset(int trst, int srst);
193 static void vsllink_simple_command(uint8_t command);
194 static int vsllink_connect(void);
195 static int vsllink_disconnect(void);
197 /* VSLLink tap buffer functions */
198 static void vsllink_tap_append_step(int tms, int tdi);
199 static void vsllink_tap_init_dma(void);
200 static void vsllink_tap_init_normal(void);
201 static void (*vsllink_tap_init)(void);
202 static int vsllink_tap_execute_dma(void);
203 static int vsllink_tap_execute_normal(void);
204 static int (*vsllink_tap_execute)(void);
205 static void vsllink_tap_ensure_space_dma(int scans, int length);
206 static void vsllink_tap_ensure_space_normal(int scans, int length);
207 static void (*vsllink_tap_ensure_space)(int scans, int length);
208 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, struct scan_command *command);
209 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, struct scan_command *command, int offset);
211 /* VSLLink lowlevel functions */
213 struct usb_dev_handle* usb_handle;
216 static struct vsllink *vsllink_usb_open(void);
217 static void vsllink_usb_close(struct vsllink *vsllink);
218 static int vsllink_usb_message(struct vsllink *vsllink, int out_length, int in_length);
219 static int vsllink_usb_write(struct vsllink *vsllink, int out_length);
220 static int vsllink_usb_read(struct vsllink *vsllink);
222 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
223 static void vsllink_debug_buffer(uint8_t *buffer, int length);
226 static int vsllink_tms_data_len = 0;
227 static uint8_t* vsllink_tms_cmd_pos;
229 static int tap_length = 0;
230 static int tap_buffer_size = 0;
231 static uint8_t *tms_buffer = NULL;
232 static uint8_t *tdi_buffer = NULL;
233 static uint8_t *tdo_buffer = NULL;
236 static struct vsllink* vsllink_handle = NULL;
238 static void reset_command_pointer(void)
240 if (vsllink_mode == VSLLINK_MODE_NORMAL)
242 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
243 vsllink_usb_out_buffer_idx = 3;
251 static int vsllink_execute_queue(void)
253 struct jtag_command *cmd = jtag_command_queue;
258 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
260 reset_command_pointer();
266 DEBUG_JTAG_IO("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
267 tap_state_name(cmd->cmd.runtest->end_state));
269 vsllink_end_state(cmd->cmd.runtest->end_state);
270 vsllink_runtest(cmd->cmd.runtest->num_cycles);
274 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
276 vsllink_end_state(cmd->cmd.statemove->end_state);
277 vsllink_state_move();
281 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
282 cmd->cmd.pathmove->num_states, \
283 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
285 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
289 vsllink_end_state(cmd->cmd.scan->end_state);
291 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
292 if (cmd->cmd.scan->ir_scan)
294 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
298 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
301 #ifdef _DEBUG_JTAG_IO_
302 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
305 type = jtag_scan_type(cmd->cmd.scan);
307 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
311 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
313 vsllink_tap_execute();
315 if (cmd->cmd.reset->trst == 1)
317 tap_set_state(TAP_RESET);
319 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
323 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
324 vsllink_tap_execute();
325 jtag_sleep(cmd->cmd.sleep->us);
328 case JTAG_STABLECLOCKS:
329 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
330 switch (tap_get_state())
333 // tms should be '1' to stay in TAP_RESET mode
341 // in other mode, tms should be '0'
343 break; /* above stable states are OK */
345 LOG_ERROR("jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
346 tap_state_name(tap_get_state()));
349 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
353 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
359 return vsllink_tap_execute();
362 static int vsllink_speed(int speed)
366 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
367 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
368 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
370 result = vsllink_usb_write(vsllink_handle, 3);
378 LOG_ERROR("VSLLink setting speed failed (%d)", result);
379 return ERROR_JTAG_DEVICE_ERROR;
385 static int vsllink_khz(int khz, int *jtag_speed)
392 static int vsllink_speed_div(int jtag_speed, int *khz)
399 static int vsllink_init(void)
401 int check_cnt, to_tmp;
403 char version_str[100];
405 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
406 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
407 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
409 LOG_ERROR("Not enough memory");
413 vsllink_handle = vsllink_usb_open();
415 if (vsllink_handle == 0)
417 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
418 return ERROR_JTAG_INIT_FAILED;
420 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid, vsllink_usb_pid);
422 to_tmp = VSLLINK_USB_TIMEOUT;
423 VSLLINK_USB_TIMEOUT = 100;
425 while (check_cnt < 5)
427 vsllink_simple_command(0x00);
428 result = vsllink_usb_read(vsllink_handle);
432 vsllink_usb_in_buffer[result] = 0;
433 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
434 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
435 LOG_INFO("%s", version_str);
437 // free the pre-alloc memroy
438 free(vsllink_usb_in_buffer);
439 free(vsllink_usb_out_buffer);
440 vsllink_usb_in_buffer = NULL;
441 vsllink_usb_out_buffer = NULL;
444 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
445 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
446 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
448 LOG_ERROR("Not enough memory");
453 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
455 // alloc memory for dma mode
456 if (vsllink_mode == VSLLINK_MODE_DMA)
458 tap_buffer_size = (VSLLINK_BufferSize - 3) / 2;
459 tms_buffer = (uint8_t*)malloc(tap_buffer_size);
460 tdi_buffer = (uint8_t*)malloc(tap_buffer_size);
461 tdo_buffer = (uint8_t*)malloc(tap_buffer_size);
462 if ((tms_buffer == NULL) || (tdi_buffer == NULL) || (tdo_buffer == NULL))
464 LOG_ERROR("Not enough memory");
470 vsllink_simple_command(VSLLINK_CMD_DISCONN);
475 // It's dangerout to proced
476 LOG_ERROR("VSLLink initial failed");
479 VSLLINK_USB_TIMEOUT = to_tmp;
481 // connect to vsllink
483 // initialize function pointers
484 if (vsllink_mode == VSLLINK_MODE_NORMAL)
487 vsllink_state_move = vsllink_state_move_normal;
488 vsllink_path_move = vsllink_path_move_normal;
489 vsllink_stableclocks = vsllink_stableclocks_normal;
490 vsllink_scan = vsllink_scan_normal;
492 vsllink_tap_init = vsllink_tap_init_normal;
493 vsllink_tap_execute = vsllink_tap_execute_normal;
494 vsllink_tap_ensure_space = vsllink_tap_ensure_space_normal;
496 LOG_INFO("vsllink run in NORMAL mode");
501 vsllink_state_move = vsllink_state_move_dma;
502 vsllink_path_move = vsllink_path_move_dma;
503 vsllink_stableclocks = vsllink_stableclocks_dma;
504 vsllink_scan = vsllink_scan_dma;
506 vsllink_tap_init = vsllink_tap_init_dma;
507 vsllink_tap_execute = vsllink_tap_execute_dma;
508 vsllink_tap_ensure_space = vsllink_tap_ensure_space_dma;
510 LOG_INFO("vsllink run in DMA mode");
513 // Set SRST and TRST to output, Set USR1 and USR2 to input
514 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
515 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
516 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
517 if (vsllink_usb_write(vsllink_handle, 3) != 3)
519 LOG_ERROR("VSLLink USB send data error");
525 LOG_INFO("VSLLink JTAG Interface ready");
532 static int vsllink_quit(void)
534 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
536 // Set all pins to input
537 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
538 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
539 vsllink_usb_out_buffer[2] = 0;
540 if (vsllink_usb_write(vsllink_handle, 3) != 3)
542 LOG_ERROR("VSLLink USB send data error");
547 vsllink_disconnect();
548 vsllink_usb_close(vsllink_handle);
549 vsllink_handle = NULL;
552 if (vsllink_usb_in_buffer != NULL)
554 free(vsllink_usb_in_buffer);
555 vsllink_usb_in_buffer = NULL;
557 if (vsllink_usb_out_buffer != NULL)
559 free(vsllink_usb_out_buffer);
560 vsllink_usb_out_buffer = NULL;
566 /***************************************************************************/
567 /* Queue command implementations */
568 static int vsllink_disconnect(void)
570 vsllink_simple_command(VSLLINK_CMD_DISCONN);
574 static int vsllink_connect(void)
576 char vsllink_str[100];
578 vsllink_usb_out_buffer[0] = VSLLINK_CMD_CONN;
579 vsllink_usb_out_buffer[1] = vsllink_mode;
580 vsllink_usb_message(vsllink_handle, 2, 0);
581 if (vsllink_usb_read(vsllink_handle) > 2)
583 strncpy(vsllink_str, (char *)vsllink_usb_in_buffer + 2, sizeof(vsllink_str));
584 LOG_INFO("%s", vsllink_str);
590 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
591 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
592 static void vsllink_append_tms(void)
594 uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
596 struct insert_insignificant_operation *insert = \
597 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
599 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
600 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
601 (vsllink_tms_cmd_pos == NULL))
603 LOG_ERROR("There MUST be some bugs in the driver");
607 tms2 = (tms_scan & VSLLINK_BIT_MSK[insert->insert_position]) << \
608 vsllink_tms_data_len;
609 if (insert->insert_value == 1)
611 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
612 (vsllink_tms_data_len + insert->insert_position);
614 tms2 |= (tms_scan >> insert->insert_position) << \
615 (8 + insert->insert_position);
617 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
618 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
620 vsllink_tms_data_len = 0;
621 vsllink_tms_cmd_pos = NULL;
624 static void vsllink_end_state(tap_state_t state)
626 if (tap_is_state_stable(state))
628 tap_set_end_state(state);
632 LOG_ERROR("BUG: %i is not a valid end state", state);
637 /* Goes to the end state. */
638 static void vsllink_state_move_normal(void)
640 if (vsllink_tms_data_len > 0)
642 vsllink_append_tms();
646 vsllink_tap_ensure_space(0, 2);
648 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
649 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
652 tap_set_state(tap_get_end_state());
654 static void vsllink_state_move_dma(void)
656 int i, insert_length = (tap_length % 8) ? (8 - (tap_length % 8)) : 0;
657 struct insert_insignificant_operation *insert = \
658 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
659 uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
661 if (tap_get_state() == TAP_RESET)
663 vsllink_tap_ensure_space(0, 8);
665 for (i = 0; i < 8; i++)
667 vsllink_tap_append_step(1, 0);
671 if (insert_length > 0)
673 vsllink_tap_ensure_space(0, 16);
675 for (i = 0; i < insert->insert_position; i++)
677 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
679 for (i = 0; i < insert_length; i++)
681 vsllink_tap_append_step(insert->insert_value, 0);
683 for (i = insert->insert_position; i < 8; i++)
685 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
690 vsllink_tap_ensure_space(0, 8);
692 for (i = 0; i < 8; i++)
694 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
698 tap_set_state(tap_get_end_state());
701 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
702 static void vsllink_add_path(int start, int num, tap_state_t *path)
706 for (i = start; i < (start + num); i++)
712 vsllink_usb_out_buffer_idx++;
714 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
717 if (path[i - start] == tap_state_transition(tap_get_state(), true))
719 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
721 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
727 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
730 tap_set_state(path[i - start]);
732 if ((i > 0) && ((i & 7) == 0))
734 vsllink_usb_out_buffer_idx++;
735 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
738 tap_set_end_state(tap_get_state());
741 static void vsllink_path_move_normal(int num_states, tap_state_t *path)
743 int i, tms_len, tms_cmd_pos, path_idx = 0;
745 if (vsllink_tms_data_len > 0)
747 // there are vsllink_tms_data_len more tms bits to be shifted
748 // so there are vsllink_tms_data_len + num_states tms bits in all
749 tms_len = vsllink_tms_data_len + num_states;
752 // merge into last tms shift
755 // just append tms data to the last tms byte
756 vsllink_add_path(vsllink_tms_data_len, num_states, path);
758 else if (tms_len == 8)
760 // end last tms shift command
761 (*vsllink_tms_cmd_pos)--;
762 vsllink_add_path(vsllink_tms_data_len, num_states, path);
764 else if (tms_len < 16)
766 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
768 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
769 // there is enought tms length in the current tms shift command
770 (*vsllink_tms_cmd_pos)++;
771 vsllink_add_path(vsllink_tms_data_len, num_states, path);
775 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
776 // not enough tms length in the current tms shift command
777 // so a new command should be added
778 // first decrease byte length of last tms shift command
779 (*vsllink_tms_cmd_pos)--;
780 // append tms data to the last tms byte
781 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
782 path += 8 - vsllink_tms_data_len;
783 // add new command(3 bytes)
784 vsllink_tap_ensure_space(0, 3);
785 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
786 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
787 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
790 else if (tms_len == 16)
792 // end last tms shift command
793 vsllink_add_path(vsllink_tms_data_len, num_states, path);
796 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
797 if (vsllink_tms_data_len == 0)
799 vsllink_tms_cmd_pos = NULL;
805 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
807 path += 16 - vsllink_tms_data_len;
808 num_states -= 16 - vsllink_tms_data_len;
809 vsllink_tms_data_len = 0;
810 vsllink_tms_cmd_pos = NULL;
816 // Normal operation, don't need to append tms data
817 vsllink_tms_data_len = num_states & 7;
819 while (num_states > 0)
821 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
823 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
829 tms_len = (i + 7) >> 3;
830 vsllink_tap_ensure_space(0, tms_len + 2);
831 tms_cmd_pos = vsllink_usb_out_buffer_idx;
832 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
834 vsllink_add_path(0, i, path + path_idx);
840 if (vsllink_tms_data_len > 0)
842 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
844 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
845 (*vsllink_tms_cmd_pos)++;
849 vsllink_usb_out_buffer[tms_cmd_pos]--;
851 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
852 vsllink_tap_ensure_space(0, 3);
853 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
854 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
855 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
860 static void vsllink_path_move_dma(int num_states, tap_state_t *path)
866 if ((8 - (tap_length & 7)) < num_states)
868 j = 8 - (tap_length & 7);
874 for (i = 0; i < j; i++)
876 if (path[i] == tap_state_transition(tap_get_state(), false))
878 vsllink_tap_append_step(0, 0);
880 else if (path[i] == tap_state_transition(tap_get_state(), true))
882 vsllink_tap_append_step(1, 0);
886 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
889 tap_set_state(path[i]);
896 vsllink_tap_ensure_space(0, num_states);
898 for (i = 0; i < num_states; i++)
900 if (path[j + i] == tap_state_transition(tap_get_state(), false))
902 vsllink_tap_append_step(0, 0);
904 else if (path[j + i] == tap_state_transition(tap_get_state(), true))
906 vsllink_tap_append_step(1, 0);
910 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
913 tap_set_state(path[j + i]);
917 tap_set_end_state(tap_get_state());
920 static void vsllink_stableclocks_normal(int num_cycles, int tms)
923 uint16_t tms_append_byte;
925 if (vsllink_tms_data_len > 0)
927 // there are vsllink_tms_data_len more tms bits to be shifted
928 // so there are vsllink_tms_data_len + num_cycles tms bits in all
929 tms_len = vsllink_tms_data_len + num_cycles;
932 // append '1' for tms
933 tms_append_byte = (uint16_t)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
937 // append '0' for tms
942 // merge into last tms shift
945 // just add to vsllink_tms_data_len
946 // same result if tun through
947 //vsllink_tms_data_len += num_cycles;
948 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (uint8_t)(tms_append_byte & 0xFF);
950 else if (tms_len == 8)
952 // end last tms shift command
953 // just reduce it, and append last tms byte
954 (*vsllink_tms_cmd_pos)--;
955 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
957 else if (tms_len < 16)
959 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
961 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
962 // there is enought tms length in the current tms shift command
963 // increase the tms byte length by 1 and set the last byte to 0
964 (*vsllink_tms_cmd_pos)++;
965 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
966 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
970 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
971 // not enough tms length in the current tms shift command
972 // so a new command should be added
973 // first decrease byte length of last tms shift command
974 (*vsllink_tms_cmd_pos)--;
975 // append last tms byte and move the command pointer to the next empty position
976 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
977 // add new command(3 bytes)
978 vsllink_tap_ensure_space(0, 3);
979 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
980 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
981 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
984 else if (tms_len == 16)
986 // end last tms shift command
987 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
988 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
991 vsllink_tms_data_len = tms_len & 7;
992 if (vsllink_tms_data_len == 0)
994 vsllink_tms_cmd_pos = NULL;
1000 // more shifts will be needed
1001 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
1002 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
1004 num_cycles -= 16 - vsllink_tms_data_len;
1005 vsllink_tms_data_len = 0;
1006 vsllink_tms_cmd_pos = NULL;
1009 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1011 if (vsllink_tms_data_len > 0)
1017 LOG_ERROR("There MUST be some bugs in the driver");
1023 // get number of bytes left to be sent
1024 tms_len = num_cycles >> 3;
1027 vsllink_tap_ensure_space(1, 5);
1028 // if tms_len > 0, vsllink_tms_data_len == 0
1029 // so just add new command
1030 // LSB of the command byte is the tms value when do the shifting
1033 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
1037 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
1039 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
1040 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
1041 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
1042 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
1044 vsllink_usb_in_want_length += 1;
1045 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
1046 pending_scan_results_length++;
1048 if (tms_len > 0xFFFF)
1050 vsllink_tap_execute();
1055 vsllink_tms_data_len = num_cycles & 7;
1056 if (vsllink_tms_data_len > 0)
1058 vsllink_tap_ensure_space(0, 3);
1059 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1060 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1063 // append '1' for tms
1064 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
1068 // append '0' for tms
1069 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
1074 static void vsllink_stableclocks_dma(int num_cycles, int tms)
1080 if ((8 - (tap_length & 7)) < num_cycles)
1082 cur_cycles = 8 - (tap_length & 7);
1086 cur_cycles = num_cycles;
1088 for (i = 0; i < cur_cycles; i++)
1090 vsllink_tap_append_step(tms, 0);
1092 num_cycles -= cur_cycles;
1095 while (num_cycles > 0)
1097 if (num_cycles > 8 * tap_buffer_size)
1099 cur_cycles = 8 * tap_buffer_size;
1103 cur_cycles = num_cycles;
1106 vsllink_tap_ensure_space(0, cur_cycles);
1108 for (i = 0; i < cur_cycles; i++)
1110 vsllink_tap_append_step(tms, 0);
1113 num_cycles -= cur_cycles;
1117 static void vsllink_runtest(int num_cycles)
1119 tap_state_t saved_end_state = tap_get_end_state();
1121 if (tap_get_state() != TAP_IDLE)
1123 // enter into IDLE state
1124 vsllink_end_state(TAP_IDLE);
1125 vsllink_state_move();
1128 vsllink_stableclocks(num_cycles, 0);
1132 vsllink_end_state(saved_end_state);
1133 tap_set_state(TAP_IDLE);
1134 if (tap_get_end_state() != TAP_IDLE)
1136 vsllink_state_move();
1140 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
1142 tap_state_t saved_end_state;
1143 uint8_t bits_left, tms_tmp, tdi_len;
1151 tdi_len = ((scan_size + 7) >> 3);
1152 if ((tdi_len + 7) > VSLLINK_BufferSize)
1154 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1158 saved_end_state = tap_get_end_state();
1160 /* Move to appropriate scan state */
1161 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1163 if (vsllink_tms_data_len > 0)
1165 if (tap_get_state() == tap_get_end_state())
1167 // already in IRSHIFT or DRSHIFT state
1168 // merge tms data in the last tms shift command into next scan command
1169 if (*vsllink_tms_cmd_pos < 1)
1171 LOG_ERROR("There MUST be some bugs in the driver");
1174 else if (*vsllink_tms_cmd_pos < 2)
1176 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1177 vsllink_usb_out_buffer_idx--;
1181 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1182 *vsllink_tms_cmd_pos -= 2;
1185 vsllink_tap_ensure_space(1, tdi_len + 7);
1186 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1187 // which is merged from the last tms shift command
1188 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1189 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1190 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 8) & 0xff;
1191 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1192 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1194 for (i = 0; i < tdi_len; i++)
1196 buffer[i] >>= 8 - vsllink_tms_data_len;
1199 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1203 vsllink_tap_append_scan_normal(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1204 scan_size -= 8 - vsllink_tms_data_len;
1205 vsllink_tms_data_len = 0;
1209 vsllink_state_move();
1210 vsllink_tap_ensure_space(1, tdi_len + 5);
1212 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1213 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1214 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1216 vsllink_tap_append_scan_normal(scan_size, buffer, command, 0);
1221 vsllink_tap_ensure_space(1, tdi_len + 7);
1223 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1224 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1225 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1226 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1227 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1229 vsllink_tap_append_scan_normal(scan_size, buffer, command, 8);
1231 vsllink_end_state(saved_end_state);
1233 bits_left = scan_size & 0x07;
1234 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1238 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1242 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1245 if (tap_get_state() != tap_get_end_state())
1247 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1251 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1254 tap_set_state(tap_get_end_state());
1256 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
1258 tap_state_t saved_end_state;
1260 saved_end_state = tap_get_end_state();
1262 /* Move to appropriate scan state */
1263 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1265 vsllink_state_move();
1266 vsllink_end_state(saved_end_state);
1269 vsllink_tap_append_scan_dma(scan_size, buffer, command);
1271 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1272 while (tap_length % 8 != 0)
1275 vsllink_tap_append_step(0, 0);
1278 if (tap_get_state() != tap_get_end_state())
1280 vsllink_state_move();
1284 static void vsllink_reset(int trst, int srst)
1288 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1290 /* Signals are active low */
1291 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1292 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1293 vsllink_usb_out_buffer[2] = 0;
1296 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1300 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1303 result = vsllink_usb_write(vsllink_handle, 3);
1306 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1310 static void vsllink_simple_command(uint8_t command)
1314 DEBUG_JTAG_IO("0x%02x", command);
1316 vsllink_usb_out_buffer[0] = command;
1317 result = vsllink_usb_write(vsllink_handle, 1);
1321 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1325 COMMAND_HANDLER(vsllink_handle_mode_command)
1328 LOG_ERROR("parameter error, should be one parameter for VID");
1332 if (!strcmp(args[0], "normal"))
1334 vsllink_mode = VSLLINK_MODE_NORMAL;
1336 else if (!strcmp(args[0], "dma"))
1338 vsllink_mode = VSLLINK_MODE_DMA;
1342 LOG_ERROR("invalid vsllink_mode: %s", args[0]);
1349 COMMAND_HANDLER(vsllink_handle_usb_vid_command)
1353 LOG_ERROR("parameter error, should be one parameter for VID");
1357 COMMAND_PARSE_NUMBER(u16, args[0], vsllink_usb_vid);
1361 COMMAND_HANDLER(vsllink_handle_usb_pid_command)
1365 LOG_ERROR("parameter error, should be one parameter for PID");
1368 COMMAND_PARSE_NUMBER(u16, args[0], vsllink_usb_pid);
1372 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
1376 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1380 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_bulkin);
1382 vsllink_usb_bulkin |= 0x80;
1387 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
1391 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1395 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_bulkout);
1397 vsllink_usb_bulkout &= ~0x80;
1402 COMMAND_HANDLER(vsllink_handle_usb_interface_command)
1406 LOG_ERROR("parameter error, should be one parameter for interface number");
1410 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_interface);
1414 /***************************************************************************/
1415 /* VSLLink tap functions */
1417 static void vsllink_tap_init_normal(void)
1419 vsllink_usb_out_buffer_idx = 0;
1420 vsllink_usb_in_want_length = 0;
1421 pending_scan_results_length = 0;
1423 static void vsllink_tap_init_dma(void)
1426 pending_scan_results_length = 0;
1429 static void vsllink_tap_ensure_space_normal(int scans, int length)
1431 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1432 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1434 if (scans > available_scans || length > available_bytes)
1436 vsllink_tap_execute();
1439 static void vsllink_tap_ensure_space_dma(int scans, int length)
1441 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1442 int available_bytes = tap_buffer_size * 8 - tap_length;
1444 if (scans > available_scans || length > available_bytes)
1446 vsllink_tap_execute();
1450 static void vsllink_tap_append_step(int tms, int tdi)
1453 int index = tap_length / 8;
1455 if (index < tap_buffer_size)
1457 int bit_index = tap_length % 8;
1458 uint8_t bit = 1 << bit_index;
1462 tms_buffer[index] |= bit;
1466 tms_buffer[index] &= ~bit;
1471 tdi_buffer[index] |= bit;
1475 tdi_buffer[index] &= ~bit;
1482 LOG_ERROR("buffer overflow, tap_length=%d", tap_length);
1486 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, struct scan_command *command, int offset)
1488 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1493 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1497 vsllink_usb_in_want_length += (length + 7) >> 3;
1499 pending_scan_result->length = length;
1500 pending_scan_result->offset = offset;
1501 pending_scan_result->command = command;
1502 pending_scan_result->buffer = buffer;
1504 for (i = 0; i < ((length + 7) >> 3); i++)
1506 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1509 pending_scan_results_length++;
1511 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, struct scan_command *command)
1513 struct pending_scan_result *pending_scan_result;
1514 int len_tmp, len_all, i;
1517 while (len_all < length)
1519 if ((length - len_all) > tap_buffer_size * 8)
1521 len_tmp = tap_buffer_size * 8;
1525 len_tmp = length - len_all;
1528 vsllink_tap_ensure_space(1, (len_tmp + 7) & ~7);
1530 pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1531 pending_scan_result->offset = tap_length;
1532 pending_scan_result->length = len_tmp;
1533 pending_scan_result->command = command;
1534 pending_scan_result->buffer = buffer + len_all / 8;
1536 for (i = 0; i < len_tmp; i++)
1538 vsllink_tap_append_step(((len_all + i) < length-1 ? 0 : 1), (buffer[(len_all + i)/8] >> ((len_all + i)%8)) & 1);
1541 pending_scan_results_length++;
1546 /* Pad and send a tap sequence to the device, and receive the answer.
1547 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1548 static int vsllink_tap_execute_normal(void)
1554 if (vsllink_tms_data_len > 0)
1556 if ((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1558 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1561 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1563 // last tms bit is '1'
1564 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1565 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1566 vsllink_tms_data_len = 0;
1570 // last tms bit is '0'
1571 vsllink_usb_out_buffer_idx++;
1572 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1573 vsllink_tms_data_len = 0;
1577 if (vsllink_usb_out_buffer_idx > 3)
1579 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1581 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1582 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1585 result = vsllink_usb_message(vsllink_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1587 if (result == vsllink_usb_in_want_length)
1589 for (i = 0; i < pending_scan_results_length; i++)
1591 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
1592 uint8_t *buffer = pending_scan_result->buffer;
1593 int length = pending_scan_result->length;
1594 int offset = pending_scan_result->offset;
1595 struct scan_command *command = pending_scan_result->command;
1599 // IRSHIFT or DRSHIFT
1600 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1601 first += (length + offset + 7) >> 3;
1603 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1604 #ifdef _DEBUG_JTAG_IO_
1605 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1608 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1611 return ERROR_JTAG_QUEUE_FAILED;
1614 free(pending_scan_result->buffer);
1615 pending_scan_result->buffer = NULL;
1625 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1626 return ERROR_JTAG_QUEUE_FAILED;
1631 reset_command_pointer();
1635 static int vsllink_tap_execute_dma(void)
1643 /* Pad last byte so that tap_length is divisible by 8 */
1644 while (tap_length % 8 != 0)
1646 /* More of the last TMS value keeps us in the same state,
1647 * analogous to free-running JTAG interfaces. */
1648 vsllink_tap_append_step(last_tms, 0);
1650 byte_length = tap_length / 8;
1652 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
1653 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff; // package size
1654 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
1656 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
1657 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
1659 result = vsllink_usb_message(vsllink_handle, 3 + 2 * byte_length, byte_length);
1660 if (result == byte_length)
1662 for (i = 0; i < pending_scan_results_length; i++)
1664 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
1665 uint8_t *buffer = pending_scan_result->buffer;
1666 int length = pending_scan_result->length;
1667 int first = pending_scan_result->offset;
1669 struct scan_command *command = pending_scan_result->command;
1670 buf_set_buf(vsllink_usb_in_buffer, first, buffer, 0, length);
1672 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length, first);
1673 #ifdef _DEBUG_JTAG_IO_
1674 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1677 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1680 return ERROR_JTAG_QUEUE_FAILED;
1683 if (pending_scan_result->buffer != NULL)
1685 free(pending_scan_result->buffer);
1691 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, byte_length);
1692 return ERROR_JTAG_QUEUE_FAILED;
1701 /*****************************************************************************/
1702 /* VSLLink USB low-level functions */
1704 static struct vsllink* vsllink_usb_open(void)
1706 struct usb_bus *busses;
1707 struct usb_bus *bus;
1708 struct usb_device *dev;
1711 struct vsllink *result;
1713 result = (struct vsllink*) malloc(sizeof(struct vsllink));
1719 busses = usb_get_busses();
1721 /* find vsllink device in usb bus */
1723 for (bus = busses; bus; bus = bus->next)
1725 for (dev = bus->devices; dev; dev = dev->next)
1727 if ((dev->descriptor.idVendor == vsllink_usb_vid) && (dev->descriptor.idProduct == vsllink_usb_pid))
1729 result->usb_handle = usb_open(dev);
1730 if (NULL == result->usb_handle)
1732 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid, vsllink_usb_pid);
1736 /* usb_set_configuration required under win32 */
1737 ret = usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1740 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev->config[0].bConfigurationValue, ret);
1743 ret = usb_claim_interface(result->usb_handle, vsllink_usb_interface);
1746 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface, ret);
1752 * This makes problems under Mac OS X. And is not needed
1753 * under Windows. Hopefully this will not break a linux build
1755 usb_set_altinterface(result->usb_handle, 0);
1766 static void vsllink_usb_close(struct vsllink *vsllink)
1770 ret = usb_release_interface(vsllink->usb_handle, vsllink_usb_interface);
1773 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface, ret);
1777 ret = usb_close(vsllink->usb_handle);
1780 LOG_ERROR("fail to close usb, %d returned", ret);
1787 /* Send a message and receive the reply. */
1788 static int vsllink_usb_message(struct vsllink *vsllink, int out_length, int in_length)
1792 result = vsllink_usb_write(vsllink, out_length);
1793 if (result == out_length)
1797 result = vsllink_usb_read(vsllink);
1798 if (result == in_length)
1804 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1812 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1817 /* Write data from out_buffer to USB. */
1818 static int vsllink_usb_write(struct vsllink *vsllink, int out_length)
1822 if (out_length > VSLLINK_BufferSize)
1824 LOG_ERROR("vsllink_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1828 result = usb_bulk_write(vsllink->usb_handle, vsllink_usb_bulkout, \
1829 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1831 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1833 #ifdef _DEBUG_USB_COMMS_
1834 LOG_DEBUG("USB out:");
1835 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1838 #ifdef _VSLLINK_IN_DEBUG_MODE_
1845 /* Read data from USB into in_buffer. */
1846 static int vsllink_usb_read(struct vsllink *vsllink)
1848 int result = usb_bulk_read(vsllink->usb_handle, vsllink_usb_bulkin, \
1849 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1851 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1853 #ifdef _DEBUG_USB_COMMS_
1854 LOG_DEBUG("USB in:");
1855 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1860 #define BYTES_PER_LINE 16
1862 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1863 static void vsllink_debug_buffer(uint8_t *buffer, int length)
1870 for (i = 0; i < length; i += BYTES_PER_LINE)
1872 snprintf(line, 5, "%04x", i);
1873 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1875 snprintf(s, 4, " %02x", buffer[j]);
1878 LOG_DEBUG("%s", line);
1881 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_
1883 static int vsllink_register_commands(struct command_context *cmd_ctx)
1885 register_command(cmd_ctx, NULL, "vsllink_usb_vid",
1886 vsllink_handle_usb_vid_command, COMMAND_CONFIG,
1888 register_command(cmd_ctx, NULL, "vsllink_usb_pid",
1889 vsllink_handle_usb_pid_command, COMMAND_CONFIG,
1891 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin",
1892 vsllink_handle_usb_bulkin_command, COMMAND_CONFIG,
1894 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout",
1895 vsllink_handle_usb_bulkout_command, COMMAND_CONFIG,
1897 register_command(cmd_ctx, NULL, "vsllink_usb_interface",
1898 vsllink_handle_usb_interface_command, COMMAND_CONFIG,
1900 register_command(cmd_ctx, NULL, "vsllink_mode",
1901 vsllink_handle_mode_command, COMMAND_CONFIG,
1907 struct jtag_interface vsllink_interface = {
1909 .register_commands = &vsllink_register_commands,
1910 .init = &vsllink_init,
1911 .quit = &vsllink_quit,
1912 .khz = &vsllink_khz,
1913 .speed = &vsllink_speed,
1914 .speed_div = &vsllink_speed_div,
1915 .execute_queue = &vsllink_execute_queue,