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
169 int length
; /* Number of bits to read */
170 scan_command_t
*command
; /* Corresponding scan command */
172 } pending_scan_result_t
;
174 #define MAX_PENDING_SCAN_RESULTS 256
176 static int pending_scan_results_length
;
177 static pending_scan_result_t pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
179 /* External interface functions */
180 static int vsllink_execute_queue(void);
181 static int vsllink_speed(int speed
);
182 static int vsllink_khz(int khz
, int *jtag_speed
);
183 static int vsllink_speed_div(int jtag_speed
, int *khz
);
184 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
);
185 static int vsllink_init(void);
186 static int vsllink_quit(void);
188 /* CLI command handler functions */
189 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
190 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
191 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
192 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
193 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
194 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
196 /* Queue command functions */
197 static void vsllink_end_state(tap_state_t state
);
198 static void vsllink_state_move_dma(void);
199 static void vsllink_state_move_normal(void);
200 static void (*vsllink_state_move
)(void);
201 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
);
202 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
);
203 static void (*vsllink_path_move
)(int num_states
, tap_state_t
*path
);
204 static void vsllink_runtest(int num_cycles
);
205 static void vsllink_stableclocks_dma(int num_cycles
, int tms
);
206 static void vsllink_stableclocks_normal(int num_cycles
, int tms
);
207 static void (*vsllink_stableclocks
)(int num_cycles
, int tms
);
208 static void vsllink_scan_dma(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, scan_command_t
*command
);
209 static void vsllink_scan_normal(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, scan_command_t
*command
);
210 static void (*vsllink_scan
)(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, scan_command_t
*command
);
211 static void vsllink_reset(int trst
, int srst
);
212 static void vsllink_simple_command(uint8_t command
);
213 static int vsllink_connect(void);
214 static int vsllink_disconnect(void);
216 /* VSLLink tap buffer functions */
217 static void vsllink_tap_append_step(int tms
, int tdi
);
218 static void vsllink_tap_init_dma(void);
219 static void vsllink_tap_init_normal(void);
220 static void (*vsllink_tap_init
)(void);
221 static int vsllink_tap_execute_dma(void);
222 static int vsllink_tap_execute_normal(void);
223 static int (*vsllink_tap_execute
)(void);
224 static void vsllink_tap_ensure_space_dma(int scans
, int length
);
225 static void vsllink_tap_ensure_space_normal(int scans
, int length
);
226 static void (*vsllink_tap_ensure_space
)(int scans
, int length
);
227 static void vsllink_tap_append_scan_dma(int length
, uint8_t *buffer
, scan_command_t
*command
);
228 static void vsllink_tap_append_scan_normal(int length
, uint8_t *buffer
, scan_command_t
*command
, int offset
);
230 /* VSLLink lowlevel functions */
231 typedef struct vsllink_jtag
233 struct usb_dev_handle
* usb_handle
;
236 static vsllink_jtag_t
*vsllink_usb_open(void);
237 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
);
238 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
);
239 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
);
240 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
);
242 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
243 static void vsllink_debug_buffer(uint8_t *buffer
, int length
);
246 static int vsllink_tms_data_len
= 0;
247 static uint8_t* vsllink_tms_cmd_pos
;
249 static int tap_length
= 0;
250 static int tap_buffer_size
= 0;
251 static uint8_t *tms_buffer
= NULL
;
252 static uint8_t *tdi_buffer
= NULL
;
253 static uint8_t *tdo_buffer
= NULL
;
256 static vsllink_jtag_t
* vsllink_jtag_handle
= NULL
;
258 /***************************************************************************/
259 /* External interface implementation */
261 jtag_interface_t vsllink_interface
=
264 .execute_queue
= vsllink_execute_queue
,
265 .speed
= vsllink_speed
,
267 .speed_div
= vsllink_speed_div
,
268 .register_commands
= vsllink_register_commands
,
269 .init
= vsllink_init
,
273 static void reset_command_pointer(void)
275 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
277 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
278 vsllink_usb_out_buffer_idx
= 3;
286 static int vsllink_execute_queue(void)
288 jtag_command_t
*cmd
= jtag_command_queue
;
293 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
295 reset_command_pointer();
301 DEBUG_JTAG_IO("runtest %i cycles, end in %s", cmd
->cmd
.runtest
->num_cycles
, \
302 tap_state_name(cmd
->cmd
.runtest
->end_state
));
304 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
305 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
309 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd
->cmd
.statemove
->end_state
));
311 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
312 vsllink_state_move();
316 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
317 cmd
->cmd
.pathmove
->num_states
, \
318 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
320 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
324 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
326 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
327 if (cmd
->cmd
.scan
->ir_scan
)
329 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
333 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
336 #ifdef _DEBUG_JTAG_IO_
337 vsllink_debug_buffer(buffer
, (scan_size
+ 7) >> 3);
340 type
= jtag_scan_type(cmd
->cmd
.scan
);
342 vsllink_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
, cmd
->cmd
.scan
);
346 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
348 vsllink_tap_execute();
350 if (cmd
->cmd
.reset
->trst
== 1)
352 tap_set_state(TAP_RESET
);
354 vsllink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
358 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
359 vsllink_tap_execute();
360 jtag_sleep(cmd
->cmd
.sleep
->us
);
363 case JTAG_STABLECLOCKS
:
364 DEBUG_JTAG_IO("add %d clocks", cmd
->cmd
.stableclocks
->num_cycles
);
365 switch (tap_get_state())
368 // tms should be '1' to stay in TAP_RESET mode
376 // in other mode, tms should be '0'
378 break; /* above stable states are OK */
380 LOG_ERROR("jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
381 tap_state_name(tap_get_state()) );
384 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
388 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
394 return vsllink_tap_execute();
397 static int vsllink_speed(int speed
)
401 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_SPEED
;
402 vsllink_usb_out_buffer
[1] = (speed
>> 0) & 0xff;
403 vsllink_usb_out_buffer
[2] = (speed
>> 8) & 0xFF;
405 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
413 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
414 return ERROR_JTAG_DEVICE_ERROR
;
420 static int vsllink_khz(int khz
, int *jtag_speed
)
427 static int vsllink_speed_div(int jtag_speed
, int *khz
)
434 static int vsllink_init(void)
436 int check_cnt
, to_tmp
;
438 char version_str
[100];
440 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
441 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
442 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
444 LOG_ERROR("Not enough memory");
448 vsllink_jtag_handle
= vsllink_usb_open();
450 if (vsllink_jtag_handle
== 0)
452 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
453 return ERROR_JTAG_INIT_FAILED
;
455 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid
, vsllink_usb_pid
);
457 to_tmp
= VSLLINK_USB_TIMEOUT
;
458 VSLLINK_USB_TIMEOUT
= 100;
460 while (check_cnt
< 5)
462 vsllink_simple_command(0x00);
463 result
= vsllink_usb_read(vsllink_jtag_handle
);
467 vsllink_usb_in_buffer
[result
] = 0;
468 VSLLINK_BufferSize
= vsllink_usb_in_buffer
[0] + (vsllink_usb_in_buffer
[1] << 8);
469 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(version_str
));
470 LOG_INFO("%s", version_str
);
472 // free the pre-alloc memroy
473 free(vsllink_usb_in_buffer
);
474 free(vsllink_usb_out_buffer
);
475 vsllink_usb_in_buffer
= NULL
;
476 vsllink_usb_out_buffer
= NULL
;
479 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
480 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
481 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
483 LOG_ERROR("Not enough memory");
488 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize
);
490 // alloc memory for dma mode
491 if (vsllink_mode
== VSLLINK_MODE_DMA
)
493 tap_buffer_size
= (VSLLINK_BufferSize
- 3) / 2;
494 tms_buffer
= (uint8_t*)malloc(tap_buffer_size
);
495 tdi_buffer
= (uint8_t*)malloc(tap_buffer_size
);
496 tdo_buffer
= (uint8_t*)malloc(tap_buffer_size
);
497 if ((tms_buffer
== NULL
) || (tdi_buffer
== NULL
) || (tdo_buffer
== NULL
))
499 LOG_ERROR("Not enough memory");
505 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
510 // It's dangerout to proced
511 LOG_ERROR("VSLLink initial failed");
514 VSLLINK_USB_TIMEOUT
= to_tmp
;
516 // connect to vsllink
518 // initialize function pointers
519 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
522 vsllink_state_move
= vsllink_state_move_normal
;
523 vsllink_path_move
= vsllink_path_move_normal
;
524 vsllink_stableclocks
= vsllink_stableclocks_normal
;
525 vsllink_scan
= vsllink_scan_normal
;
527 vsllink_tap_init
= vsllink_tap_init_normal
;
528 vsllink_tap_execute
= vsllink_tap_execute_normal
;
529 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_normal
;
531 LOG_INFO("vsllink run in NORMAL mode");
536 vsllink_state_move
= vsllink_state_move_dma
;
537 vsllink_path_move
= vsllink_path_move_dma
;
538 vsllink_stableclocks
= vsllink_stableclocks_dma
;
539 vsllink_scan
= vsllink_scan_dma
;
541 vsllink_tap_init
= vsllink_tap_init_dma
;
542 vsllink_tap_execute
= vsllink_tap_execute_dma
;
543 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_dma
;
545 LOG_INFO("vsllink run in DMA mode");
548 // Set SRST and TRST to output, Set USR1 and USR2 to input
549 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
550 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
551 vsllink_usb_out_buffer
[2] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
552 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
554 LOG_ERROR("VSLLink USB send data error");
560 LOG_INFO("VSLLink JTAG Interface ready");
567 static int vsllink_quit(void)
569 if ((vsllink_usb_in_buffer
!= NULL
) && (vsllink_usb_out_buffer
!= NULL
))
571 // Set all pins to input
572 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
573 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
574 vsllink_usb_out_buffer
[2] = 0;
575 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
577 LOG_ERROR("VSLLink USB send data error");
582 vsllink_disconnect();
583 vsllink_usb_close(vsllink_jtag_handle
);
584 vsllink_jtag_handle
= NULL
;
587 if (vsllink_usb_in_buffer
!= NULL
)
589 free(vsllink_usb_in_buffer
);
590 vsllink_usb_in_buffer
= NULL
;
592 if (vsllink_usb_out_buffer
!= NULL
)
594 free(vsllink_usb_out_buffer
);
595 vsllink_usb_out_buffer
= NULL
;
601 /***************************************************************************/
602 /* Queue command implementations */
603 static int vsllink_disconnect(void)
605 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
609 static int vsllink_connect(void)
611 char vsllink_str
[100];
613 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_CONN
;
614 vsllink_usb_out_buffer
[1] = vsllink_mode
;
615 vsllink_usb_message(vsllink_jtag_handle
, 2, 0);
616 if (vsllink_usb_read(vsllink_jtag_handle
) > 2)
618 strncpy(vsllink_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(vsllink_str
));
619 LOG_INFO("%s", vsllink_str
);
625 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
626 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
627 static void vsllink_append_tms(void)
629 uint8_t tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
631 insert_insignificant_operation_t
*insert
= \
632 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
634 if (((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_DRPAUSE
) && (tap_get_state() != TAP_IRPAUSE
)) || \
635 (vsllink_tms_data_len
<= 0) || (vsllink_tms_data_len
>= 8) || \
636 (vsllink_tms_cmd_pos
== NULL
))
638 LOG_ERROR("There MUST be some bugs in the driver");
642 tms2
= (tms_scan
& VSLLINK_BIT_MSK
[insert
->insert_position
]) << \
643 vsllink_tms_data_len
;
644 if (insert
->insert_value
== 1)
646 tms2
|= VSLLINK_BIT_MSK
[8 - vsllink_tms_data_len
] << \
647 (vsllink_tms_data_len
+ insert
->insert_position
);
649 tms2
|= (tms_scan
>> insert
->insert_position
) << \
650 (8 + insert
->insert_position
);
652 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (tms2
>> 0) & 0xff;
653 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms2
>> 8) & 0xff;
655 vsllink_tms_data_len
= 0;
656 vsllink_tms_cmd_pos
= NULL
;
659 static void vsllink_end_state(tap_state_t state
)
661 if (tap_is_state_stable(state
))
663 tap_set_end_state(state
);
667 LOG_ERROR("BUG: %i is not a valid end state", state
);
672 /* Goes to the end state. */
673 static void vsllink_state_move_normal(void)
675 if (vsllink_tms_data_len
> 0)
677 vsllink_append_tms();
681 vsllink_tap_ensure_space(0, 2);
683 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
;
684 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
687 tap_set_state(tap_get_end_state());
689 static void vsllink_state_move_dma(void)
691 int i
, insert_length
= (tap_length
% 8) ? (8 - (tap_length
% 8)) : 0;
692 insert_insignificant_operation_t
*insert
= \
693 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
694 uint8_t tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
696 if (tap_get_state() == TAP_RESET
)
698 vsllink_tap_ensure_space(0, 8);
700 for (i
= 0; i
< 8; i
++)
702 vsllink_tap_append_step(1, 0);
706 if (insert_length
> 0)
708 vsllink_tap_ensure_space(0, 16);
710 for (i
= 0; i
< insert
->insert_position
; i
++)
712 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
714 for (i
= 0; i
< insert_length
; i
++)
716 vsllink_tap_append_step(insert
->insert_value
, 0);
718 for (i
= insert
->insert_position
; i
< 8; i
++)
720 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
725 vsllink_tap_ensure_space(0, 8);
727 for (i
= 0; i
< 8; i
++)
729 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
733 tap_set_state(tap_get_end_state());
736 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
737 static void vsllink_add_path(int start
, int num
, tap_state_t
*path
)
741 for (i
= start
; i
< (start
+ num
); i
++)
747 vsllink_usb_out_buffer_idx
++;
749 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
752 if (path
[i
- start
] == tap_state_transition(tap_get_state(), true))
754 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= 1 << (i
& 7);
756 else if (path
[i
- start
] == tap_state_transition(tap_get_state(), false))
762 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
765 tap_set_state(path
[i
- start
]);
767 if ((i
> 0) && ((i
& 7) == 0))
769 vsllink_usb_out_buffer_idx
++;
770 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
773 tap_set_end_state(tap_get_state());
776 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
)
778 int i
, tms_len
, tms_cmd_pos
, path_idx
= 0;
780 if (vsllink_tms_data_len
> 0)
782 // there are vsllink_tms_data_len more tms bits to be shifted
783 // so there are vsllink_tms_data_len + num_states tms bits in all
784 tms_len
= vsllink_tms_data_len
+ num_states
;
787 // merge into last tms shift
790 // just append tms data to the last tms byte
791 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
793 else if (tms_len
== 8)
795 // end last tms shift command
796 (*vsllink_tms_cmd_pos
)--;
797 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
799 else if (tms_len
< 16)
801 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
803 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
804 // there is enought tms length in the current tms shift command
805 (*vsllink_tms_cmd_pos
)++;
806 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
810 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
811 // not enough tms length in the current tms shift command
812 // so a new command should be added
813 // first decrease byte length of last tms shift command
814 (*vsllink_tms_cmd_pos
)--;
815 // append tms data to the last tms byte
816 vsllink_add_path(vsllink_tms_data_len
, 8 - vsllink_tms_data_len
, path
);
817 path
+= 8 - vsllink_tms_data_len
;
818 // add new command(3 bytes)
819 vsllink_tap_ensure_space(0, 3);
820 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
821 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
822 vsllink_add_path(0, num_states
- (8 - vsllink_tms_data_len
), path
);
825 else if (tms_len
== 16)
827 // end last tms shift command
828 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
831 vsllink_tms_data_len
= (vsllink_tms_data_len
+ num_states
) & 7;
832 if (vsllink_tms_data_len
== 0)
834 vsllink_tms_cmd_pos
= NULL
;
840 vsllink_add_path(vsllink_tms_data_len
, 16 - vsllink_tms_data_len
, path
);
842 path
+= 16 - vsllink_tms_data_len
;
843 num_states
-= 16 - vsllink_tms_data_len
;
844 vsllink_tms_data_len
= 0;
845 vsllink_tms_cmd_pos
= NULL
;
851 // Normal operation, don't need to append tms data
852 vsllink_tms_data_len
= num_states
& 7;
854 while (num_states
> 0)
856 if (num_states
> ((VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8))
858 i
= (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8;
864 tms_len
= (i
+ 7) >> 3;
865 vsllink_tap_ensure_space(0, tms_len
+ 2);
866 tms_cmd_pos
= vsllink_usb_out_buffer_idx
;
867 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| (tms_len
- 1);
869 vsllink_add_path(0, i
, path
+ path_idx
);
875 if (vsllink_tms_data_len
> 0)
877 if (tms_len
< (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1))
879 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[tms_cmd_pos
];
880 (*vsllink_tms_cmd_pos
)++;
884 vsllink_usb_out_buffer
[tms_cmd_pos
]--;
886 tms_len
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
887 vsllink_tap_ensure_space(0, 3);
888 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
889 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
890 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = tms_len
;
895 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
)
901 if ((8 - (tap_length
& 7)) < num_states
)
903 j
= 8 - (tap_length
& 7);
909 for (i
= 0; i
< j
; i
++)
911 if (path
[i
] == tap_state_transition(tap_get_state(), false))
913 vsllink_tap_append_step(0, 0);
915 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
917 vsllink_tap_append_step(1, 0);
921 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
924 tap_set_state(path
[i
]);
931 vsllink_tap_ensure_space(0, num_states
);
933 for (i
= 0; i
< num_states
; i
++)
935 if (path
[j
+ i
] == tap_state_transition(tap_get_state(), false))
937 vsllink_tap_append_step(0, 0);
939 else if (path
[j
+ i
] == tap_state_transition(tap_get_state(), true))
941 vsllink_tap_append_step(1, 0);
945 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
948 tap_set_state(path
[j
+ i
]);
952 tap_set_end_state(tap_get_state());
955 static void vsllink_stableclocks_normal(int num_cycles
, int tms
)
958 uint16_t tms_append_byte
;
960 if (vsllink_tms_data_len
> 0)
962 // there are vsllink_tms_data_len more tms bits to be shifted
963 // so there are vsllink_tms_data_len + num_cycles tms bits in all
964 tms_len
= vsllink_tms_data_len
+ num_cycles
;
967 // append '1' for tms
968 tms_append_byte
= (uint16_t)((((1 << num_cycles
) - 1) << vsllink_tms_data_len
) & 0xFFFF);
972 // append '0' for tms
977 // merge into last tms shift
980 // just add to vsllink_tms_data_len
981 // same result if tun through
982 //vsllink_tms_data_len += num_cycles;
983 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= (uint8_t)(tms_append_byte
& 0xFF);
985 else if (tms_len
== 8)
987 // end last tms shift command
988 // just reduce it, and append last tms byte
989 (*vsllink_tms_cmd_pos
)--;
990 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
992 else if (tms_len
< 16)
994 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
996 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
997 // there is enought tms length in the current tms shift command
998 // increase the tms byte length by 1 and set the last byte to 0
999 (*vsllink_tms_cmd_pos
)++;
1000 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
1001 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (uint8_t)(tms_append_byte
>> 8);
1005 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1006 // not enough tms length in the current tms shift command
1007 // so a new command should be added
1008 // first decrease byte length of last tms shift command
1009 (*vsllink_tms_cmd_pos
)--;
1010 // append last tms byte and move the command pointer to the next empty position
1011 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
1012 // add new command(3 bytes)
1013 vsllink_tap_ensure_space(0, 3);
1014 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1015 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1016 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (uint8_t)(tms_append_byte
>> 8);
1019 else if (tms_len
== 16)
1021 // end last tms shift command
1022 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
1023 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (uint8_t)(tms_append_byte
>> 8);
1026 vsllink_tms_data_len
= tms_len
& 7;
1027 if (vsllink_tms_data_len
== 0)
1029 vsllink_tms_cmd_pos
= NULL
;
1035 // more shifts will be needed
1036 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
1037 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (uint8_t)(tms_append_byte
>> 8);
1039 num_cycles
-= 16 - vsllink_tms_data_len
;
1040 vsllink_tms_data_len
= 0;
1041 vsllink_tms_cmd_pos
= NULL
;
1044 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1046 if (vsllink_tms_data_len
> 0)
1052 LOG_ERROR("There MUST be some bugs in the driver");
1058 // get number of bytes left to be sent
1059 tms_len
= num_cycles
>> 3;
1062 vsllink_tap_ensure_space(1, 5);
1063 // if tms_len > 0, vsllink_tms_data_len == 0
1064 // so just add new command
1065 // LSB of the command byte is the tms value when do the shifting
1068 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
| 1;
1072 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
;
1074 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 0) & 0xff;
1075 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 8) & 0xff;
1076 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 16) & 0xff;
1077 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 24) & 0xff;
1079 vsllink_usb_in_want_length
+= 1;
1080 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= NULL
;
1081 pending_scan_results_length
++;
1083 if (tms_len
> 0xFFFF)
1085 vsllink_tap_execute();
1090 vsllink_tms_data_len
= num_cycles
& 7;
1091 if (vsllink_tms_data_len
> 0)
1093 vsllink_tap_ensure_space(0, 3);
1094 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1095 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1098 // append '1' for tms
1099 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (1 << vsllink_tms_data_len
) - 1;
1103 // append '0' for tms
1104 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0x00;
1109 static void vsllink_stableclocks_dma(int num_cycles
, int tms
)
1115 if ((8 - (tap_length
& 7)) < num_cycles
)
1117 cur_cycles
= 8 - (tap_length
& 7);
1121 cur_cycles
= num_cycles
;
1123 for (i
= 0; i
< cur_cycles
; i
++)
1125 vsllink_tap_append_step(tms
, 0);
1127 num_cycles
-= cur_cycles
;
1130 while (num_cycles
> 0)
1132 if (num_cycles
> 8 * tap_buffer_size
)
1134 cur_cycles
= 8 * tap_buffer_size
;
1138 cur_cycles
= num_cycles
;
1141 vsllink_tap_ensure_space(0, cur_cycles
);
1143 for (i
= 0; i
< cur_cycles
; i
++)
1145 vsllink_tap_append_step(tms
, 0);
1148 num_cycles
-= cur_cycles
;
1152 static void vsllink_runtest(int num_cycles
)
1154 tap_state_t saved_end_state
= tap_get_end_state();
1156 if (tap_get_state() != TAP_IDLE
)
1158 // enter into IDLE state
1159 vsllink_end_state(TAP_IDLE
);
1160 vsllink_state_move();
1163 vsllink_stableclocks(num_cycles
, 0);
1167 vsllink_end_state(saved_end_state
);
1168 tap_set_state(TAP_IDLE
);
1169 if (tap_get_end_state() != TAP_IDLE
)
1171 vsllink_state_move();
1175 static void vsllink_scan_normal(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, scan_command_t
*command
)
1177 tap_state_t saved_end_state
;
1178 uint8_t bits_left
, tms_tmp
, tdi_len
;
1181 if (0 == scan_size
)
1186 tdi_len
= ((scan_size
+ 7) >> 3);
1187 if ((tdi_len
+ 7) > VSLLINK_BufferSize
)
1189 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1193 saved_end_state
= tap_get_end_state();
1195 /* Move to appropriate scan state */
1196 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1198 if (vsllink_tms_data_len
> 0)
1200 if (tap_get_state() == tap_get_end_state())
1202 // already in IRSHIFT or DRSHIFT state
1203 // merge tms data in the last tms shift command into next scan command
1204 if (*vsllink_tms_cmd_pos
< 1)
1206 LOG_ERROR("There MUST be some bugs in the driver");
1209 else if (*vsllink_tms_cmd_pos
< 2)
1211 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1212 vsllink_usb_out_buffer_idx
--;
1216 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1217 *vsllink_tms_cmd_pos
-= 2;
1220 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1221 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1222 // which is merged from the last tms shift command
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
++] = tms_tmp
;
1227 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[0] << (8 - vsllink_tms_data_len
);
1229 for (i
= 0; i
< tdi_len
; i
++)
1231 buffer
[i
] >>= 8 - vsllink_tms_data_len
;
1234 buffer
[i
] += buffer
[i
+ 1] << vsllink_tms_data_len
;
1238 vsllink_tap_append_scan_normal(scan_size
- vsllink_tms_data_len
, buffer
, command
, vsllink_tms_data_len
);
1239 scan_size
-= 8 - vsllink_tms_data_len
;
1240 vsllink_tms_data_len
= 0;
1244 vsllink_state_move();
1245 vsllink_tap_ensure_space(1, tdi_len
+ 5);
1247 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
;
1248 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 0) & 0xff;
1249 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 8) & 0xff;
1251 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 0);
1256 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1258 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1259 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1260 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1)>> 8) & 0xff;
1261 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1262 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1264 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 8);
1266 vsllink_end_state(saved_end_state
);
1268 bits_left
= scan_size
& 0x07;
1269 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1273 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << (bits_left
- 1);
1277 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << 7;
1280 if (tap_get_state() != tap_get_end_state())
1282 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1286 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1289 tap_set_state(tap_get_end_state());
1291 static void vsllink_scan_dma(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, scan_command_t
*command
)
1293 tap_state_t saved_end_state
;
1295 saved_end_state
= tap_get_end_state();
1297 /* Move to appropriate scan state */
1298 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1300 vsllink_state_move();
1301 vsllink_end_state(saved_end_state
);
1304 vsllink_tap_append_scan_dma(scan_size
, buffer
, command
);
1306 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1307 while (tap_length
% 8 != 0)
1310 vsllink_tap_append_step(0, 0);
1313 if (tap_get_state() != tap_get_end_state())
1315 vsllink_state_move();
1319 static void vsllink_reset(int trst
, int srst
)
1323 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
1325 /* Signals are active low */
1326 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORT
;
1327 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
1328 vsllink_usb_out_buffer
[2] = 0;
1331 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
1335 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
1338 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
1341 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result
);
1345 static void vsllink_simple_command(uint8_t command
)
1349 DEBUG_JTAG_IO("0x%02x", command
);
1351 vsllink_usb_out_buffer
[0] = command
;
1352 result
= vsllink_usb_write(vsllink_jtag_handle
, 1);
1356 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command
, result
);
1360 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
)
1362 register_command(cmd_ctx
, NULL
, "vsllink_usb_vid", vsllink_handle_usb_vid_command
,
1363 COMMAND_CONFIG
, NULL
);
1364 register_command(cmd_ctx
, NULL
, "vsllink_usb_pid", vsllink_handle_usb_pid_command
,
1365 COMMAND_CONFIG
, NULL
);
1366 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command
,
1367 COMMAND_CONFIG
, NULL
);
1368 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command
,
1369 COMMAND_CONFIG
, NULL
);
1370 register_command(cmd_ctx
, NULL
, "vsllink_usb_interface", vsllink_handle_usb_interface_command
,
1371 COMMAND_CONFIG
, NULL
);
1372 register_command(cmd_ctx
, NULL
, "vsllink_mode", vsllink_handle_mode_command
,
1373 COMMAND_CONFIG
, NULL
);
1378 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1381 LOG_ERROR("parameter error, should be one parameter for VID");
1385 if (!strcmp(args
[0], "normal"))
1387 vsllink_mode
= VSLLINK_MODE_NORMAL
;
1389 else if (!strcmp(args
[0], "dma"))
1391 vsllink_mode
= VSLLINK_MODE_DMA
;
1395 LOG_ERROR("invalid vsllink_mode: %s", args
[0]);
1402 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1406 LOG_ERROR("parameter error, should be one parameter for VID");
1410 return parse_u16(args
[0], &vsllink_usb_vid
);
1413 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1417 LOG_ERROR("parameter error, should be one parameter for PID");
1420 return parse_u16(args
[0], &vsllink_usb_pid
);
1423 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1427 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1431 int retval
= parse_u8(args
[0], &vsllink_usb_bulkin
);
1432 if (ERROR_OK
== retval
)
1433 vsllink_usb_bulkin
|= 0x80;
1438 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1442 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1446 int retval
= parse_u8(args
[0], &vsllink_usb_bulkout
);
1447 if (ERROR_OK
== retval
)
1448 vsllink_usb_bulkout
&= ~0x80;
1453 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1457 LOG_ERROR("parameter error, should be one parameter for interface number");
1461 return parse_u8(args
[0], &vsllink_usb_interface
);
1464 /***************************************************************************/
1465 /* VSLLink tap functions */
1467 static void vsllink_tap_init_normal(void)
1469 vsllink_usb_out_buffer_idx
= 0;
1470 vsllink_usb_in_want_length
= 0;
1471 pending_scan_results_length
= 0;
1473 static void vsllink_tap_init_dma(void)
1476 pending_scan_results_length
= 0;
1479 static void vsllink_tap_ensure_space_normal(int scans
, int length
)
1481 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1482 int available_bytes
= VSLLINK_BufferSize
- vsllink_usb_out_buffer_idx
;
1484 if (scans
> available_scans
|| length
> available_bytes
)
1486 vsllink_tap_execute();
1489 static void vsllink_tap_ensure_space_dma(int scans
, int length
)
1491 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1492 int available_bytes
= tap_buffer_size
* 8 - tap_length
;
1494 if (scans
> available_scans
|| length
> available_bytes
)
1496 vsllink_tap_execute();
1500 static void vsllink_tap_append_step(int tms
, int tdi
)
1503 int index
= tap_length
/ 8;
1505 if (index
< tap_buffer_size
)
1507 int bit_index
= tap_length
% 8;
1508 uint8_t bit
= 1 << bit_index
;
1512 tms_buffer
[index
] |= bit
;
1516 tms_buffer
[index
] &= ~bit
;
1521 tdi_buffer
[index
] |= bit
;
1525 tdi_buffer
[index
] &= ~bit
;
1532 LOG_ERROR("buffer overflow, tap_length=%d", tap_length
);
1536 static void vsllink_tap_append_scan_normal(int length
, uint8_t *buffer
, scan_command_t
*command
, int offset
)
1538 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1543 vsllink_usb_in_want_length
+= ((length
+ 7) >> 3) + 1;
1547 vsllink_usb_in_want_length
+= (length
+ 7) >> 3;
1549 pending_scan_result
->length
= length
;
1550 pending_scan_result
->offset
= offset
;
1551 pending_scan_result
->command
= command
;
1552 pending_scan_result
->buffer
= buffer
;
1554 for (i
= 0; i
< ((length
+ 7) >> 3); i
++)
1556 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[i
];
1559 pending_scan_results_length
++;
1561 static void vsllink_tap_append_scan_dma(int length
, uint8_t *buffer
, scan_command_t
*command
)
1563 pending_scan_result_t
*pending_scan_result
;
1564 int len_tmp
, len_all
, i
;
1567 while (len_all
< length
)
1569 if ((length
- len_all
) > tap_buffer_size
* 8)
1571 len_tmp
= tap_buffer_size
* 8;
1575 len_tmp
= length
- len_all
;
1578 vsllink_tap_ensure_space(1, (len_tmp
+ 7) & ~7);
1580 pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1581 pending_scan_result
->offset
= tap_length
;
1582 pending_scan_result
->length
= len_tmp
;
1583 pending_scan_result
->command
= command
;
1584 pending_scan_result
->buffer
= buffer
+ len_all
/ 8;
1586 for (i
= 0; i
< len_tmp
; i
++)
1588 vsllink_tap_append_step(((len_all
+ i
) < length
-1 ? 0 : 1), (buffer
[(len_all
+ i
)/8] >> ((len_all
+ i
)%8)) & 1);
1591 pending_scan_results_length
++;
1596 /* Pad and send a tap sequence to the device, and receive the answer.
1597 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1598 static int vsllink_tap_execute_normal(void)
1604 if (vsllink_tms_data_len
> 0)
1606 if ((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_IRPAUSE
) && (tap_get_state() != TAP_DRPAUSE
))
1608 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1611 if (vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] & (1 << (vsllink_tms_data_len
- 1)))
1613 // last tms bit is '1'
1614 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= 0xFF << vsllink_tms_data_len
;
1615 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0xFF;
1616 vsllink_tms_data_len
= 0;
1620 // last tms bit is '0'
1621 vsllink_usb_out_buffer_idx
++;
1622 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1623 vsllink_tms_data_len
= 0;
1627 if (vsllink_usb_out_buffer_idx
> 3)
1629 if (vsllink_usb_out_buffer
[0] == VSLLINK_CMD_HW_JTAGSEQCMD
)
1631 vsllink_usb_out_buffer
[1] = (vsllink_usb_out_buffer_idx
>> 0) & 0xff;
1632 vsllink_usb_out_buffer
[2] = (vsllink_usb_out_buffer_idx
>> 8) & 0xff;
1635 result
= vsllink_usb_message(vsllink_jtag_handle
, vsllink_usb_out_buffer_idx
, vsllink_usb_in_want_length
);
1637 if (result
== vsllink_usb_in_want_length
)
1639 for (i
= 0; i
< pending_scan_results_length
; i
++)
1641 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1642 uint8_t *buffer
= pending_scan_result
->buffer
;
1643 int length
= pending_scan_result
->length
;
1644 int offset
= pending_scan_result
->offset
;
1645 scan_command_t
*command
= pending_scan_result
->command
;
1649 // IRSHIFT or DRSHIFT
1650 buf_set_buf(vsllink_usb_in_buffer
, first
* 8 + offset
, buffer
, 0, length
);
1651 first
+= (length
+ offset
+ 7) >> 3;
1653 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length
);
1654 #ifdef _DEBUG_JTAG_IO_
1655 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1658 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1661 return ERROR_JTAG_QUEUE_FAILED
;
1664 free(pending_scan_result
->buffer
);
1665 pending_scan_result
->buffer
= NULL
;
1675 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, vsllink_usb_in_want_length
);
1676 return ERROR_JTAG_QUEUE_FAILED
;
1681 reset_command_pointer();
1685 static int vsllink_tap_execute_dma(void)
1693 /* Pad last byte so that tap_length is divisible by 8 */
1694 while (tap_length
% 8 != 0)
1696 /* More of the last TMS value keeps us in the same state,
1697 * analogous to free-running JTAG interfaces. */
1698 vsllink_tap_append_step(last_tms
, 0);
1700 byte_length
= tap_length
/ 8;
1702 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGRAWCMD
;
1703 vsllink_usb_out_buffer
[1] = ((byte_length
* 2 + 3) >> 0) & 0xff; // package size
1704 vsllink_usb_out_buffer
[2] = ((byte_length
* 2 + 3) >> 8) & 0xff;
1706 memcpy(&vsllink_usb_out_buffer
[3], tdi_buffer
, byte_length
);
1707 memcpy(&vsllink_usb_out_buffer
[3 + byte_length
], tms_buffer
, byte_length
);
1709 result
= vsllink_usb_message(vsllink_jtag_handle
, 3 + 2 * byte_length
, byte_length
);
1710 if (result
== byte_length
)
1712 for (i
= 0; i
< pending_scan_results_length
; i
++)
1714 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1715 uint8_t *buffer
= pending_scan_result
->buffer
;
1716 int length
= pending_scan_result
->length
;
1717 int first
= pending_scan_result
->offset
;
1719 scan_command_t
*command
= pending_scan_result
->command
;
1720 buf_set_buf(vsllink_usb_in_buffer
, first
, buffer
, 0, length
);
1722 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length
, first
);
1723 #ifdef _DEBUG_JTAG_IO_
1724 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1727 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1730 return ERROR_JTAG_QUEUE_FAILED
;
1733 if (pending_scan_result
->buffer
!= NULL
)
1735 free(pending_scan_result
->buffer
);
1741 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, byte_length
);
1742 return ERROR_JTAG_QUEUE_FAILED
;
1751 /*****************************************************************************/
1752 /* VSLLink USB low-level functions */
1754 static vsllink_jtag_t
* vsllink_usb_open(void)
1756 struct usb_bus
*busses
;
1757 struct usb_bus
*bus
;
1758 struct usb_device
*dev
;
1761 vsllink_jtag_t
*result
;
1763 result
= (vsllink_jtag_t
*) malloc(sizeof(vsllink_jtag_t
));
1769 busses
= usb_get_busses();
1771 /* find vsllink_jtag device in usb bus */
1773 for (bus
= busses
; bus
; bus
= bus
->next
)
1775 for (dev
= bus
->devices
; dev
; dev
= dev
->next
)
1777 if ((dev
->descriptor
.idVendor
== vsllink_usb_vid
) && (dev
->descriptor
.idProduct
== vsllink_usb_pid
))
1779 result
->usb_handle
= usb_open(dev
);
1780 if (NULL
== result
->usb_handle
)
1782 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid
, vsllink_usb_pid
);
1786 /* usb_set_configuration required under win32 */
1787 ret
= usb_set_configuration(result
->usb_handle
, dev
->config
[0].bConfigurationValue
);
1790 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev
->config
[0].bConfigurationValue
, ret
);
1793 ret
= usb_claim_interface(result
->usb_handle
, vsllink_usb_interface
);
1796 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface
, ret
);
1802 * This makes problems under Mac OS X. And is not needed
1803 * under Windows. Hopefully this will not break a linux build
1805 usb_set_altinterface(result
->usb_handle
, 0);
1816 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
)
1820 ret
= usb_release_interface(vsllink_jtag
->usb_handle
, vsllink_usb_interface
);
1823 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface
, ret
);
1827 ret
= usb_close(vsllink_jtag
->usb_handle
);
1830 LOG_ERROR("fail to close usb, %d returned", ret
);
1837 /* Send a message and receive the reply. */
1838 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
)
1842 result
= vsllink_usb_write(vsllink_jtag
, out_length
);
1843 if (result
== out_length
)
1847 result
= vsllink_usb_read(vsllink_jtag
);
1848 if (result
== in_length
)
1854 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length
, result
);
1862 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length
, result
);
1867 /* Write data from out_buffer to USB. */
1868 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
)
1872 if (out_length
> VSLLINK_BufferSize
)
1874 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length
, VSLLINK_BufferSize
);
1878 result
= usb_bulk_write(vsllink_jtag
->usb_handle
, vsllink_usb_bulkout
, \
1879 (char *)vsllink_usb_out_buffer
, out_length
, VSLLINK_USB_TIMEOUT
);
1881 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length
, result
);
1883 #ifdef _DEBUG_USB_COMMS_
1884 LOG_DEBUG("USB out:");
1885 vsllink_debug_buffer(vsllink_usb_out_buffer
, out_length
);
1888 #ifdef _VSLLINK_IN_DEBUG_MODE_
1895 /* Read data from USB into in_buffer. */
1896 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
)
1898 int result
= usb_bulk_read(vsllink_jtag
->usb_handle
, vsllink_usb_bulkin
, \
1899 (char *)vsllink_usb_in_buffer
, VSLLINK_BufferSize
, VSLLINK_USB_TIMEOUT
);
1901 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result
);
1903 #ifdef _DEBUG_USB_COMMS_
1904 LOG_DEBUG("USB in:");
1905 vsllink_debug_buffer(vsllink_usb_in_buffer
, result
);
1910 #define BYTES_PER_LINE 16
1912 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1913 static void vsllink_debug_buffer(uint8_t *buffer
, int length
)
1920 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
)
1922 snprintf(line
, 5, "%04x", i
);
1923 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++)
1925 snprintf(s
, 4, " %02x", buffer
[j
]);
1928 LOG_DEBUG("%s", line
);
1931 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_