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 "replacements.h"
38 //#define _VSLLINK_IN_DEBUG_MODE_
40 #define VSLLINK_MODE_NORMAL 0
41 #define VSLLINK_MODE_DMA 1
43 static u16 vsllink_usb_vid
;
44 static u16 vsllink_usb_pid
;
45 static u8 vsllink_usb_bulkout
;
46 static u8 vsllink_usb_bulkin
;
47 static u8 vsllink_usb_interface
;
48 static u8 vsllink_mode
= VSLLINK_MODE_NORMAL
;
49 static int VSLLINK_USB_TIMEOUT
= 10000;
51 static int VSLLINK_BufferSize
= 1024;
53 /* Global USB buffers */
54 static int vsllink_usb_out_buffer_idx
;
55 static int vsllink_usb_in_want_length
;
56 static u8
* vsllink_usb_in_buffer
= NULL
;
57 static u8
* vsllink_usb_out_buffer
= NULL
;
59 /* Constants for VSLLink command */
60 #define VSLLINK_CMD_CONN 0x80
61 #define VSLLINK_CMD_DISCONN 0x81
62 #define VSLLINK_CMD_SET_SPEED 0x82
63 #define VSLLINK_CMD_SET_PORT 0x90
64 #define VSLLINK_CMD_GET_PORT 0x91
65 #define VSLLINK_CMD_SET_PORTDIR 0x92
66 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
67 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
68 #define VSLLINK_CMD_HW_SWDCMD 0xA2
69 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
71 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
72 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
73 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
75 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
76 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
78 #define JTAG_PINMSK_SRST (1 << 0)
79 #define JTAG_PINMSK_TRST (1 << 1)
80 #define JTAG_PINMSK_USR1 (1 << 2)
81 #define JTAG_PINMSK_USR2 (1 << 3)
82 #define JTAG_PINMSK_TCK (1 << 4)
83 #define JTAG_PINMSK_TMS (1 << 5)
84 #define JTAG_PINMSK_TDI (1 << 6)
85 #define JTAG_PINMSK_TDO (1 << 7)
88 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
90 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
98 * SD->SD and SI->SI have to be caught in interface specific code
100 static u8 VSLLINK_tap_move
[6][6] =
102 /* TLR RTI SD PD SI PI */
103 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
104 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
105 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
106 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
107 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
108 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
111 typedef struct insert_insignificant_operation
113 unsigned char insert_value
;
114 unsigned char insert_position
;
115 }insert_insignificant_operation_t
;
117 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[6][6] =
164 static u8 VSLLINK_BIT_MSK
[8] =
166 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
172 int length
; /* Number of bits to read */
173 scan_command_t
*command
; /* Corresponding scan command */
175 } pending_scan_result_t
;
177 #define MAX_PENDING_SCAN_RESULTS 256
179 static int pending_scan_results_length
;
180 static pending_scan_result_t pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
182 /* External interface functions */
183 static int vsllink_execute_queue(void);
184 static int vsllink_speed(int speed
);
185 static int vsllink_khz(int khz
, int *jtag_speed
);
186 static int vsllink_speed_div(int jtag_speed
, int *khz
);
187 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
);
188 static int vsllink_init(void);
189 static int vsllink_quit(void);
191 /* CLI command handler functions */
192 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
193 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
194 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
195 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
196 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
197 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
199 /* Queue command functions */
200 static void vsllink_end_state(tap_state_t state
);
201 static void vsllink_state_move_dma(void);
202 static void vsllink_state_move_normal(void);
203 static void (*vsllink_state_move
)(void);
204 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
);
205 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
);
206 static void (*vsllink_path_move
)(int num_states
, tap_state_t
*path
);
207 static void vsllink_runtest(int num_cycles
);
208 static void vsllink_stableclocks_dma(int num_cycles
, int tms
);
209 static void vsllink_stableclocks_normal(int num_cycles
, int tms
);
210 static void (*vsllink_stableclocks
)(int num_cycles
, int tms
);
211 static void vsllink_scan_dma(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
212 static void vsllink_scan_normal(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
213 static void (*vsllink_scan
)(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
214 static void vsllink_reset(int trst
, int srst
);
215 static void vsllink_simple_command(u8 command
);
216 static int vsllink_connect(void);
217 static int vsllink_disconnect(void);
219 /* VSLLink tap buffer functions */
220 static void vsllink_tap_append_step(int tms
, int tdi
);
221 static void vsllink_tap_init_dma(void);
222 static void vsllink_tap_init_normal(void);
223 static void (*vsllink_tap_init
)(void);
224 static int vsllink_tap_execute_dma(void);
225 static int vsllink_tap_execute_normal(void);
226 static int (*vsllink_tap_execute
)(void);
227 static void vsllink_tap_ensure_space_dma(int scans
, int length
);
228 static void vsllink_tap_ensure_space_normal(int scans
, int length
);
229 static void (*vsllink_tap_ensure_space
)(int scans
, int length
);
230 static void vsllink_tap_append_scan_dma(int length
, u8
*buffer
, scan_command_t
*command
);
231 static void vsllink_tap_append_scan_normal(int length
, u8
*buffer
, scan_command_t
*command
, int offset
);
233 /* VSLLink lowlevel functions */
234 typedef struct vsllink_jtag
236 struct usb_dev_handle
* usb_handle
;
239 static vsllink_jtag_t
*vsllink_usb_open(void);
240 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
);
241 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
);
242 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
);
243 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
);
245 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
246 static void vsllink_debug_buffer(u8
*buffer
, int length
);
249 static int vsllink_tms_data_len
= 0;
250 static u8
* vsllink_tms_cmd_pos
;
252 static int tap_length
= 0;
253 static int tap_buffer_size
= 0;
254 static u8
*tms_buffer
= NULL
;
255 static u8
*tdi_buffer
= NULL
;
256 static u8
*tdo_buffer
= NULL
;
259 static vsllink_jtag_t
* vsllink_jtag_handle
= NULL
;
261 /***************************************************************************/
262 /* External interface implementation */
264 jtag_interface_t vsllink_interface
=
267 .execute_queue
= vsllink_execute_queue
,
268 .speed
= vsllink_speed
,
270 .speed_div
= vsllink_speed_div
,
271 .register_commands
= vsllink_register_commands
,
272 .init
= vsllink_init
,
276 static void reset_command_pointer(void)
278 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
280 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
281 vsllink_usb_out_buffer_idx
= 3;
289 static int vsllink_execute_queue(void)
291 jtag_command_t
*cmd
= jtag_command_queue
;
296 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
298 reset_command_pointer();
304 DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd
->cmd
.end_state
->end_state
));
306 if (cmd
->cmd
.end_state
->end_state
!= TAP_INVALID
)
308 vsllink_end_state(cmd
->cmd
.end_state
->end_state
);
313 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd
->cmd
.runtest
->num_cycles
, \
314 tap_state_name(cmd
->cmd
.runtest
->end_state
));
316 if (cmd
->cmd
.runtest
->end_state
!= TAP_INVALID
)
318 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
320 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
324 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd
->cmd
.statemove
->end_state
));
326 if (cmd
->cmd
.statemove
->end_state
!= TAP_INVALID
)
328 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
330 vsllink_state_move();
334 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
335 cmd
->cmd
.pathmove
->num_states
, \
336 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
338 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
342 if (cmd
->cmd
.scan
->end_state
!= TAP_INVALID
)
344 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
347 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
348 if (cmd
->cmd
.scan
->ir_scan
)
350 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
354 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
357 #ifdef _DEBUG_JTAG_IO_
358 vsllink_debug_buffer(buffer
, (scan_size
+ 7) >> 3);
361 type
= jtag_scan_type(cmd
->cmd
.scan
);
363 vsllink_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
, cmd
->cmd
.scan
);
367 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
369 vsllink_tap_execute();
371 if (cmd
->cmd
.reset
->trst
== 1)
373 tap_set_state(TAP_RESET
);
375 vsllink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
379 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
380 vsllink_tap_execute();
381 jtag_sleep(cmd
->cmd
.sleep
->us
);
384 case JTAG_STABLECLOCKS
:
385 DEBUG_JTAG_IO("add %d clocks", cmd
->cmd
.stableclocks
->num_cycles
);
386 switch(tap_get_state())
389 // tms should be '1' to stay in TAP_RESET mode
397 // in other mode, tms should be '0'
399 break; /* above stable states are OK */
401 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
402 tap_state_name(tap_get_state()) );
405 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
409 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
415 return vsllink_tap_execute();
418 static int vsllink_speed(int speed
)
422 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_SPEED
;
423 vsllink_usb_out_buffer
[1] = (speed
>> 0) & 0xff;
424 vsllink_usb_out_buffer
[2] = (speed
>> 8) & 0xFF;
426 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
434 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
435 return ERROR_JTAG_DEVICE_ERROR
;
441 static int vsllink_khz(int khz
, int *jtag_speed
)
448 static int vsllink_speed_div(int jtag_speed
, int *khz
)
455 static int vsllink_init(void)
457 int check_cnt
, to_tmp
;
459 char version_str
[100];
461 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
462 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
463 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
465 LOG_ERROR("Not enough memory");
469 vsllink_jtag_handle
= vsllink_usb_open();
471 if (vsllink_jtag_handle
== 0)
473 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
474 return ERROR_JTAG_INIT_FAILED
;
476 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid
, vsllink_usb_pid
);
478 to_tmp
= VSLLINK_USB_TIMEOUT
;
479 VSLLINK_USB_TIMEOUT
= 100;
481 while (check_cnt
< 5)
483 vsllink_simple_command(0x00);
484 result
= vsllink_usb_read(vsllink_jtag_handle
);
488 vsllink_usb_in_buffer
[result
] = 0;
489 VSLLINK_BufferSize
= vsllink_usb_in_buffer
[0] + (vsllink_usb_in_buffer
[1] << 8);
490 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(version_str
));
491 LOG_INFO("%s", version_str
);
493 // free the pre-alloc memroy
494 free(vsllink_usb_in_buffer
);
495 free(vsllink_usb_out_buffer
);
496 vsllink_usb_in_buffer
= NULL
;
497 vsllink_usb_out_buffer
= NULL
;
500 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
501 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
502 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
504 LOG_ERROR("Not enough memory");
509 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize
);
511 // alloc memory for dma mode
512 if (vsllink_mode
== VSLLINK_MODE_DMA
)
514 tap_buffer_size
= (VSLLINK_BufferSize
- 3) / 2;
515 tms_buffer
= (u8
*)malloc(tap_buffer_size
);
516 tdi_buffer
= (u8
*)malloc(tap_buffer_size
);
517 tdo_buffer
= (u8
*)malloc(tap_buffer_size
);
518 if ((tms_buffer
== NULL
) || (tdi_buffer
== NULL
) || (tdo_buffer
== NULL
))
520 LOG_ERROR("Not enough memory");
526 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
531 // It's dangerout to proced
532 LOG_ERROR("VSLLink initial failed");
535 VSLLINK_USB_TIMEOUT
= to_tmp
;
537 // connect to vsllink
539 // initialize function pointers
540 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
543 vsllink_state_move
= vsllink_state_move_normal
;
544 vsllink_path_move
= vsllink_path_move_normal
;
545 vsllink_stableclocks
= vsllink_stableclocks_normal
;
546 vsllink_scan
= vsllink_scan_normal
;
548 vsllink_tap_init
= vsllink_tap_init_normal
;
549 vsllink_tap_execute
= vsllink_tap_execute_normal
;
550 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_normal
;
552 LOG_INFO("vsllink run in NORMAL mode");
557 vsllink_state_move
= vsllink_state_move_dma
;
558 vsllink_path_move
= vsllink_path_move_dma
;
559 vsllink_stableclocks
= vsllink_stableclocks_dma
;
560 vsllink_scan
= vsllink_scan_dma
;
562 vsllink_tap_init
= vsllink_tap_init_dma
;
563 vsllink_tap_execute
= vsllink_tap_execute_dma
;
564 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_dma
;
566 LOG_INFO("vsllink run in DMA mode");
569 // Set SRST and TRST to output, Set USR1 and USR2 to input
570 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
571 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
572 vsllink_usb_out_buffer
[2] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
573 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
575 LOG_ERROR("VSLLink USB send data error");
581 LOG_INFO("VSLLink JTAG Interface ready");
588 static int vsllink_quit(void)
590 if ((vsllink_usb_in_buffer
!= NULL
) && (vsllink_usb_out_buffer
!= NULL
))
592 // Set all pins to input
593 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
594 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
595 vsllink_usb_out_buffer
[2] = 0;
596 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
598 LOG_ERROR("VSLLink USB send data error");
603 vsllink_disconnect();
604 vsllink_usb_close(vsllink_jtag_handle
);
605 vsllink_jtag_handle
= NULL
;
608 if (vsllink_usb_in_buffer
!= NULL
)
610 free(vsllink_usb_in_buffer
);
611 vsllink_usb_in_buffer
= NULL
;
613 if (vsllink_usb_out_buffer
!= NULL
)
615 free(vsllink_usb_out_buffer
);
616 vsllink_usb_out_buffer
= NULL
;
622 /***************************************************************************/
623 /* Queue command implementations */
624 static int vsllink_disconnect(void)
626 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
630 static int vsllink_connect(void)
632 char vsllink_str
[100];
634 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_CONN
;
635 vsllink_usb_out_buffer
[1] = vsllink_mode
;
636 vsllink_usb_message(vsllink_jtag_handle
, 2, 0);
637 if (vsllink_usb_read(vsllink_jtag_handle
) > 2)
639 strncpy(vsllink_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(vsllink_str
));
640 LOG_INFO("%s", vsllink_str
);
646 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
647 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
648 static void vsllink_append_tms(void)
650 u8 tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
652 insert_insignificant_operation_t
*insert
= \
653 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
655 if (((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_DRPAUSE
) && (tap_get_state() != TAP_IRPAUSE
)) || \
656 (vsllink_tms_data_len
<= 0) || (vsllink_tms_data_len
>= 8) || \
657 (vsllink_tms_cmd_pos
== NULL
))
659 LOG_ERROR("There MUST be some bugs in the driver");
663 tms2
= (tms_scan
& VSLLINK_BIT_MSK
[insert
->insert_position
]) << \
664 vsllink_tms_data_len
;
665 if (insert
->insert_value
== 1)
667 tms2
|= VSLLINK_BIT_MSK
[8 - vsllink_tms_data_len
] << \
668 (vsllink_tms_data_len
+ insert
->insert_position
);
670 tms2
|= (tms_scan
>> insert
->insert_position
) << \
671 (8 + insert
->insert_position
);
673 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (tms2
>> 0) & 0xff;
674 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms2
>> 8) & 0xff;
676 vsllink_tms_data_len
= 0;
677 vsllink_tms_cmd_pos
= NULL
;
680 static void vsllink_end_state(tap_state_t state
)
682 if (tap_is_state_stable(state
))
684 tap_set_end_state(state
);
688 LOG_ERROR("BUG: %i is not a valid end state", state
);
693 /* Goes to the end state. */
694 static void vsllink_state_move_normal(void)
696 if (vsllink_tms_data_len
> 0)
698 vsllink_append_tms();
702 vsllink_tap_ensure_space(0, 2);
704 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
;
705 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
708 tap_set_state(tap_get_end_state());
710 static void vsllink_state_move_dma(void)
712 int i
, insert_length
= (tap_length
% 8) ? (8 - (tap_length
% 8)) : 0;
713 insert_insignificant_operation_t
*insert
= \
714 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
715 u8 tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
717 vsllink_tap_ensure_space(0, 8);
719 if (tap_get_state() == TAP_RESET
)
721 for (i
= 0; i
< 8; i
++)
723 vsllink_tap_append_step(1, 0);
727 if (insert_length
> 0)
729 vsllink_tap_ensure_space(0, 16);
731 for (i
= 0; i
< insert
->insert_position
; i
++)
733 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
735 for (i
= 0; i
< insert_length
; i
++)
737 vsllink_tap_append_step(insert
->insert_value
, 0);
739 for (i
= insert
->insert_position
; i
< 8; i
++)
741 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
746 vsllink_tap_ensure_space(0, 8);
748 for (i
= 0; i
< 8; i
++)
750 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
754 tap_set_state(tap_get_end_state());
757 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
758 static void vsllink_add_path(int start
, int num
, tap_state_t
*path
)
762 for (i
= start
; i
< (start
+ num
); i
++)
768 vsllink_usb_out_buffer_idx
++;
770 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
773 if (path
[i
- start
] == tap_state_transition(tap_get_state(), true))
775 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= 1 << (i
& 7);
777 else if (path
[i
- start
] == tap_state_transition(tap_get_state(), false))
783 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
786 tap_set_state(path
[i
- start
]);
788 if ((i
> 0) && ((i
& 7) == 0))
790 vsllink_usb_out_buffer_idx
++;
791 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
794 tap_set_end_state(tap_get_state());
797 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
)
799 int i
, tms_len
, tms_cmd_pos
, path_idx
= 0;
801 if (vsllink_tms_data_len
> 0)
803 // there are vsllink_tms_data_len more tms bits to be shifted
804 // so there are vsllink_tms_data_len + num_states tms bits in all
805 tms_len
= vsllink_tms_data_len
+ num_states
;
808 // merge into last tms shift
811 // just append tms data to the last tms byte
812 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
814 else if (tms_len
== 8)
816 // end last tms shift command
817 (*vsllink_tms_cmd_pos
)--;
818 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
820 else if (tms_len
< 16)
822 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
824 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
825 // there is enought tms length in the current tms shift command
826 (*vsllink_tms_cmd_pos
)++;
827 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
831 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
832 // not enough tms length in the current tms shift command
833 // so a new command should be added
834 // first decrease byte length of last tms shift command
835 (*vsllink_tms_cmd_pos
)--;
836 // append tms data to the last tms byte
837 vsllink_add_path(vsllink_tms_data_len
, 8 - vsllink_tms_data_len
, path
);
838 path
+= 8 - vsllink_tms_data_len
;
839 // add new command(3 bytes)
840 vsllink_tap_ensure_space(0, 3);
841 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
842 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
843 vsllink_add_path(0, num_states
- (8 - vsllink_tms_data_len
), path
);
846 else if (tms_len
== 16)
848 // end last tms shift command
849 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
852 vsllink_tms_data_len
= (vsllink_tms_data_len
+ num_states
) & 7;
853 if (vsllink_tms_data_len
== 0)
855 vsllink_tms_cmd_pos
= NULL
;
861 vsllink_add_path(vsllink_tms_data_len
, 16 - vsllink_tms_data_len
, path
);
863 path
+= 16 - vsllink_tms_data_len
;
864 num_states
-= 16 - vsllink_tms_data_len
;
865 vsllink_tms_data_len
= 0;
866 vsllink_tms_cmd_pos
= NULL
;
872 // Normal operation, don't need to append tms data
873 vsllink_tms_data_len
= num_states
& 7;
875 while (num_states
> 0)
877 if (num_states
> ((VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8))
879 i
= (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8;
885 tms_len
= (i
+ 7) >> 3;
886 vsllink_tap_ensure_space(0, tms_len
+ 2);
887 tms_cmd_pos
= vsllink_usb_out_buffer_idx
;
888 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| (tms_len
- 1);
890 vsllink_add_path(0, i
, path
+ path_idx
);
896 if (vsllink_tms_data_len
> 0)
898 if (tms_len
< (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1))
900 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[tms_cmd_pos
];
901 (*vsllink_tms_cmd_pos
)++;
905 vsllink_usb_out_buffer
[tms_cmd_pos
]--;
907 tms_len
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
908 vsllink_tap_ensure_space(0, 3);
909 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
910 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
911 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = tms_len
;
916 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
)
922 if ((8 - (tap_length
& 7)) < num_states
)
924 j
= 8 - (tap_length
& 7);
930 for (i
= 0; i
< j
; i
++)
932 if (path
[i
] == tap_state_transition(tap_get_state(), false))
934 vsllink_tap_append_step(0, 0);
936 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
938 vsllink_tap_append_step(1, 0);
942 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
945 tap_set_state(path
[i
]);
952 vsllink_tap_ensure_space(0, num_states
);
954 for (i
= 0; i
< num_states
; i
++)
956 if (path
[j
+ i
] == tap_state_transition(tap_get_state(), false))
958 vsllink_tap_append_step(0, 0);
960 else if (path
[j
+ i
] == tap_state_transition(tap_get_state(), true))
962 vsllink_tap_append_step(1, 0);
966 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
969 tap_set_state(path
[j
+ i
]);
973 tap_set_end_state(tap_get_state());
976 static void vsllink_stableclocks_normal(int num_cycles
, int tms
)
981 if (vsllink_tms_data_len
> 0)
983 // there are vsllink_tms_data_len more tms bits to be shifted
984 // so there are vsllink_tms_data_len + num_cycles tms bits in all
985 tms_len
= vsllink_tms_data_len
+ num_cycles
;
988 // append '1' for tms
989 tms_append_byte
= (u16
)((((1 << num_cycles
) - 1) << vsllink_tms_data_len
) & 0xFFFF);
993 // append '0' for tms
998 // merge into last tms shift
1001 // just add to vsllink_tms_data_len
1002 // same result if tun through
1003 //vsllink_tms_data_len += num_cycles;
1004 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= (u8
)(tms_append_byte
& 0xFF);
1006 else if (tms_len
== 8)
1008 // end last tms shift command
1009 // just reduce it, and append last tms byte
1010 (*vsllink_tms_cmd_pos
)--;
1011 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1013 else if (tms_len
< 16)
1015 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
1017 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1018 // there is enought tms length in the current tms shift command
1019 // increase the tms byte length by 1 and set the last byte to 0
1020 (*vsllink_tms_cmd_pos
)++;
1021 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1022 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (u8
)(tms_append_byte
>> 8);
1026 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1027 // not enough tms length in the current tms shift command
1028 // so a new command should be added
1029 // first decrease byte length of last tms shift command
1030 (*vsllink_tms_cmd_pos
)--;
1031 // append last tms byte and move the command pointer to the next empty position
1032 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1033 // add new command(3 bytes)
1034 vsllink_tap_ensure_space(0, 3);
1035 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1036 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1037 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (u8
)(tms_append_byte
>> 8);
1040 else if (tms_len
== 16)
1042 // end last tms shift command
1043 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1044 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (u8
)(tms_append_byte
>> 8);
1047 vsllink_tms_data_len
= tms_len
& 7;
1048 if (vsllink_tms_data_len
== 0)
1050 vsllink_tms_cmd_pos
= NULL
;
1056 // more shifts will be needed
1057 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1058 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (u8
)(tms_append_byte
>> 8);
1060 num_cycles
-= 16 - vsllink_tms_data_len
;
1061 vsllink_tms_data_len
= 0;
1062 vsllink_tms_cmd_pos
= NULL
;
1065 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1067 if (vsllink_tms_data_len
> 0)
1073 LOG_ERROR("There MUST be some bugs in the driver");
1079 // get number of bytes left to be sent
1080 tms_len
= num_cycles
>> 3;
1083 vsllink_tap_ensure_space(1, 5);
1084 // if tms_len > 0, vsllink_tms_data_len == 0
1085 // so just add new command
1086 // LSB of the command byte is the tms value when do the shifting
1089 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
| 1;
1093 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
;
1095 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 0) & 0xff;
1096 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 8) & 0xff;
1097 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 16) & 0xff;
1098 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 24) & 0xff;
1100 vsllink_usb_in_want_length
+= 1;
1101 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= NULL
;
1102 pending_scan_results_length
++;
1104 if (tms_len
> 0xFFFF)
1106 vsllink_tap_execute();
1111 vsllink_tms_data_len
= num_cycles
& 7;
1112 if (vsllink_tms_data_len
> 0)
1114 vsllink_tap_ensure_space(0, 3);
1115 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1116 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1119 // append '1' for tms
1120 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (1 << vsllink_tms_data_len
) - 1;
1124 // append '0' for tms
1125 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0x00;
1130 static void vsllink_stableclocks_dma(int num_cycles
, int tms
)
1136 if ((8 - (tap_length
& 7)) < num_cycles
)
1138 cur_cycles
= 8 - (tap_length
& 7);
1142 cur_cycles
= num_cycles
;
1144 for (i
= 0; i
< cur_cycles
; i
++)
1146 vsllink_tap_append_step(tms
, 0);
1148 num_cycles
-= cur_cycles
;
1151 while (num_cycles
> 0)
1153 if (num_cycles
> 8 * tap_buffer_size
)
1155 cur_cycles
= 8 * tap_buffer_size
;
1159 cur_cycles
= num_cycles
;
1162 vsllink_tap_ensure_space(0, cur_cycles
);
1164 for (i
= 0; i
< cur_cycles
; i
++)
1166 vsllink_tap_append_step(tms
, 0);
1169 num_cycles
-= cur_cycles
;
1173 static void vsllink_runtest(int num_cycles
)
1175 tap_state_t saved_end_state
= tap_get_end_state();
1177 if (tap_get_state() != TAP_IDLE
)
1179 // enter into IDLE state
1180 vsllink_end_state(TAP_IDLE
);
1181 vsllink_state_move();
1184 vsllink_stableclocks(num_cycles
, 0);
1188 vsllink_end_state(saved_end_state
);
1189 tap_set_state(TAP_IDLE
);
1190 if (tap_get_end_state() != TAP_IDLE
)
1192 vsllink_state_move();
1196 static void vsllink_scan_normal(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
1198 tap_state_t saved_end_state
;
1199 u8 bits_left
, tms_tmp
, tdi_len
;
1202 if (0 == scan_size
)
1207 tdi_len
= ((scan_size
+ 7) >> 3);
1208 if ((tdi_len
+ 7) > VSLLINK_BufferSize
)
1210 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1214 saved_end_state
= tap_get_end_state();
1216 /* Move to appropriate scan state */
1217 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1219 if (vsllink_tms_data_len
> 0)
1221 if (tap_get_state() == tap_get_end_state())
1223 // already in IRSHIFT or DRSHIFT state
1224 // merge tms data in the last tms shift command into next scan command
1225 if(*vsllink_tms_cmd_pos
< 1)
1227 LOG_ERROR("There MUST be some bugs in the driver");
1230 else if(*vsllink_tms_cmd_pos
< 2)
1232 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1233 vsllink_usb_out_buffer_idx
--;
1237 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1238 *vsllink_tms_cmd_pos
-= 2;
1241 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1242 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1243 // which is merged from the last tms shift command
1244 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1245 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1246 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 8) & 0xff;
1247 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = tms_tmp
;
1248 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[0] << (8 - vsllink_tms_data_len
);
1250 for (i
= 0; i
< tdi_len
; i
++)
1252 buffer
[i
] >>= 8 - vsllink_tms_data_len
;
1255 buffer
[i
] += buffer
[i
+ 1] << vsllink_tms_data_len
;
1259 vsllink_tap_append_scan_normal(scan_size
- vsllink_tms_data_len
, buffer
, command
, vsllink_tms_data_len
);
1260 scan_size
-= 8 - vsllink_tms_data_len
;
1261 vsllink_tms_data_len
= 0;
1265 vsllink_state_move();
1266 vsllink_tap_ensure_space(1, tdi_len
+ 5);
1268 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
;
1269 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 0) & 0xff;
1270 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 8) & 0xff;
1272 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 0);
1277 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1279 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1280 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1281 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1)>> 8) & 0xff;
1282 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1283 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1285 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 8);
1287 vsllink_end_state(saved_end_state
);
1289 bits_left
= scan_size
& 0x07;
1290 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1294 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << (bits_left
- 1);
1298 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << 7;
1301 if (tap_get_state() != tap_get_end_state())
1303 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1307 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1310 tap_set_state(tap_get_end_state());
1312 static void vsllink_scan_dma(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
1314 tap_state_t saved_end_state
;
1316 saved_end_state
= tap_get_end_state();
1318 /* Move to appropriate scan state */
1319 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1321 vsllink_state_move();
1322 vsllink_end_state(saved_end_state
);
1325 vsllink_tap_ensure_space(1, (scan_size
+ 7) & ~0x00000007);
1326 vsllink_tap_append_scan_dma(scan_size
, buffer
, command
);
1328 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1329 while (tap_length
% 8 != 0)
1332 vsllink_tap_append_step(0, 0);
1335 if (tap_get_state() != tap_get_end_state())
1337 vsllink_state_move();
1341 static void vsllink_reset(int trst
, int srst
)
1345 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
1347 /* Signals are active low */
1348 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORT
;
1349 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
1350 vsllink_usb_out_buffer
[2] = 0;
1353 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
1357 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
1360 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
1363 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result
);
1367 static void vsllink_simple_command(u8 command
)
1371 DEBUG_JTAG_IO("0x%02x", command
);
1373 vsllink_usb_out_buffer
[0] = command
;
1374 result
= vsllink_usb_write(vsllink_jtag_handle
, 1);
1378 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command
, result
);
1382 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
)
1384 register_command(cmd_ctx
, NULL
, "vsllink_usb_vid", vsllink_handle_usb_vid_command
,
1385 COMMAND_CONFIG
, NULL
);
1386 register_command(cmd_ctx
, NULL
, "vsllink_usb_pid", vsllink_handle_usb_pid_command
,
1387 COMMAND_CONFIG
, NULL
);
1388 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command
,
1389 COMMAND_CONFIG
, NULL
);
1390 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command
,
1391 COMMAND_CONFIG
, NULL
);
1392 register_command(cmd_ctx
, NULL
, "vsllink_usb_interface", vsllink_handle_usb_interface_command
,
1393 COMMAND_CONFIG
, NULL
);
1394 register_command(cmd_ctx
, NULL
, "vsllink_mode", vsllink_handle_mode_command
,
1395 COMMAND_CONFIG
, NULL
);
1400 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1403 LOG_ERROR("parameter error, should be one parameter for VID");
1407 if (!strcmp(args
[0], "normal"))
1409 vsllink_mode
= VSLLINK_MODE_NORMAL
;
1411 else if (!strcmp(args
[0], "dma"))
1413 vsllink_mode
= VSLLINK_MODE_DMA
;
1417 LOG_ERROR("invalid vsllink_mode: %s", args
[0]);
1424 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1428 LOG_ERROR("parameter error, should be one parameter for VID");
1432 vsllink_usb_vid
= strtol(args
[0], NULL
, 0);
1437 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1441 LOG_ERROR("parameter error, should be one parameter for PID");
1445 vsllink_usb_pid
= strtol(args
[0], NULL
, 0);
1450 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1454 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1458 vsllink_usb_bulkin
= strtol(args
[0], NULL
, 0) | 0x80;
1463 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1467 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1471 vsllink_usb_bulkout
= strtol(args
[0], NULL
, 0);
1476 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1480 LOG_ERROR("parameter error, should be one parameter for interface number");
1484 vsllink_usb_interface
= strtol(args
[0], NULL
, 0);
1489 /***************************************************************************/
1490 /* VSLLink tap functions */
1492 static void vsllink_tap_init_normal(void)
1494 vsllink_usb_out_buffer_idx
= 0;
1495 vsllink_usb_in_want_length
= 0;
1496 pending_scan_results_length
= 0;
1498 static void vsllink_tap_init_dma(void)
1501 pending_scan_results_length
= 0;
1504 static void vsllink_tap_ensure_space_normal(int scans
, int length
)
1506 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1507 int available_bytes
= VSLLINK_BufferSize
- vsllink_usb_out_buffer_idx
;
1509 if (scans
> available_scans
|| length
> available_bytes
)
1511 vsllink_tap_execute();
1514 static void vsllink_tap_ensure_space_dma(int scans
, int length
)
1516 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1517 int available_bytes
= tap_buffer_size
* 8 - tap_length
;
1519 if (scans
> available_scans
|| length
> available_bytes
)
1521 vsllink_tap_execute();
1525 static void vsllink_tap_append_step(int tms
, int tdi
)
1528 int index
= tap_length
/ 8;
1530 if (index
< tap_buffer_size
)
1532 int bit_index
= tap_length
% 8;
1533 u8 bit
= 1 << bit_index
;
1537 tms_buffer
[index
] |= bit
;
1541 tms_buffer
[index
] &= ~bit
;
1546 tdi_buffer
[index
] |= bit
;
1550 tdi_buffer
[index
] &= ~bit
;
1557 LOG_ERROR("buffer overflow, tap_length=%d", tap_length
);
1561 static void vsllink_tap_append_scan_normal(int length
, u8
*buffer
, scan_command_t
*command
, int offset
)
1563 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1568 vsllink_usb_in_want_length
+= ((length
+ 7) >> 3) + 1;
1572 vsllink_usb_in_want_length
+= (length
+ 7) >> 3;
1574 pending_scan_result
->length
= length
;
1575 pending_scan_result
->offset
= offset
;
1576 pending_scan_result
->command
= command
;
1577 pending_scan_result
->buffer
= buffer
;
1579 for (i
= 0; i
< ((length
+ 7) >> 3); i
++)
1581 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[i
];
1584 pending_scan_results_length
++;
1586 static void vsllink_tap_append_scan_dma(int length
, u8
*buffer
, scan_command_t
*command
)
1588 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1591 pending_scan_result
->offset
= tap_length
;
1592 pending_scan_result
->length
= length
;
1593 pending_scan_result
->command
= command
;
1594 pending_scan_result
->buffer
= buffer
;
1596 for (i
= 0; i
< length
; i
++)
1598 vsllink_tap_append_step((i
< length
-1 ? 0 : 1), (buffer
[i
/8] >> (i
%8)) & 1);
1601 pending_scan_results_length
++;
1604 /* Pad and send a tap sequence to the device, and receive the answer.
1605 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1606 static int vsllink_tap_execute_normal(void)
1612 if (vsllink_tms_data_len
> 0)
1614 if((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_IRPAUSE
) && (tap_get_state() != TAP_DRPAUSE
))
1616 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1619 if (vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] & (1 << (vsllink_tms_data_len
- 1)))
1621 // last tms bit is '1'
1622 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= 0xFF << vsllink_tms_data_len
;
1623 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0xFF;
1624 vsllink_tms_data_len
= 0;
1628 // last tms bit is '0'
1629 vsllink_usb_out_buffer_idx
++;
1630 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1631 vsllink_tms_data_len
= 0;
1635 if (vsllink_usb_out_buffer_idx
> 3)
1637 if (vsllink_usb_out_buffer
[0] == VSLLINK_CMD_HW_JTAGSEQCMD
)
1639 vsllink_usb_out_buffer
[1] = (vsllink_usb_out_buffer_idx
>> 0) & 0xff;
1640 vsllink_usb_out_buffer
[2] = (vsllink_usb_out_buffer_idx
>> 8) & 0xff;
1643 result
= vsllink_usb_message(vsllink_jtag_handle
, vsllink_usb_out_buffer_idx
, vsllink_usb_in_want_length
);
1645 if (result
== vsllink_usb_in_want_length
)
1647 for (i
= 0; i
< pending_scan_results_length
; i
++)
1649 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1650 u8
*buffer
= pending_scan_result
->buffer
;
1651 int length
= pending_scan_result
->length
;
1652 int offset
= pending_scan_result
->offset
;
1653 scan_command_t
*command
= pending_scan_result
->command
;
1657 // IRSHIFT or DRSHIFT
1658 buf_set_buf(vsllink_usb_in_buffer
, first
* 8 + offset
, buffer
, 0, length
);
1659 first
+= (length
+ offset
+ 7) >> 3;
1661 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length
);
1662 #ifdef _DEBUG_JTAG_IO_
1663 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1666 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1669 return ERROR_JTAG_QUEUE_FAILED
;
1672 free(pending_scan_result
->buffer
);
1673 pending_scan_result
->buffer
= NULL
;
1683 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, vsllink_usb_in_want_length
);
1684 return ERROR_JTAG_QUEUE_FAILED
;
1689 reset_command_pointer();
1693 static int vsllink_tap_execute_dma(void)
1701 /* Pad last byte so that tap_length is divisible by 8 */
1702 while (tap_length
% 8 != 0)
1704 /* More of the last TMS value keeps us in the same state,
1705 * analogous to free-running JTAG interfaces. */
1706 vsllink_tap_append_step(last_tms
, 0);
1708 byte_length
= tap_length
/ 8;
1710 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGRAWCMD
;
1711 vsllink_usb_out_buffer
[1] = ((byte_length
* 2 + 3) >> 0) & 0xff; // package size
1712 vsllink_usb_out_buffer
[2] = ((byte_length
* 2 + 3) >> 8) & 0xff;
1714 memcpy(&vsllink_usb_out_buffer
[3], tdi_buffer
, byte_length
);
1715 memcpy(&vsllink_usb_out_buffer
[3 + byte_length
], tms_buffer
, byte_length
);
1717 result
= vsllink_usb_message(vsllink_jtag_handle
, 3 + 2 * byte_length
, byte_length
);
1718 if (result
== byte_length
)
1720 for (i
= 0; i
< pending_scan_results_length
; i
++)
1722 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1723 u8
*buffer
= pending_scan_result
->buffer
;
1724 int length
= pending_scan_result
->length
;
1725 int first
= pending_scan_result
->offset
;
1727 scan_command_t
*command
= pending_scan_result
->command
;
1728 buf_set_buf(vsllink_usb_in_buffer
, first
, buffer
, 0, length
);
1730 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length
, first
);
1731 #ifdef _DEBUG_JTAG_IO_
1732 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1735 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1738 return ERROR_JTAG_QUEUE_FAILED
;
1741 if (pending_scan_result
->buffer
!= NULL
)
1743 free(pending_scan_result
->buffer
);
1749 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, byte_length
);
1750 return ERROR_JTAG_QUEUE_FAILED
;
1759 /*****************************************************************************/
1760 /* VSLLink USB low-level functions */
1762 static vsllink_jtag_t
* vsllink_usb_open(void)
1764 struct usb_bus
*busses
;
1765 struct usb_bus
*bus
;
1766 struct usb_device
*dev
;
1769 vsllink_jtag_t
*result
;
1771 result
= (vsllink_jtag_t
*) malloc(sizeof(vsllink_jtag_t
));
1777 busses
= usb_get_busses();
1779 /* find vsllink_jtag device in usb bus */
1781 for (bus
= busses
; bus
; bus
= bus
->next
)
1783 for (dev
= bus
->devices
; dev
; dev
= dev
->next
)
1785 if ((dev
->descriptor
.idVendor
== vsllink_usb_vid
) && (dev
->descriptor
.idProduct
== vsllink_usb_pid
))
1787 result
->usb_handle
= usb_open(dev
);
1788 if (NULL
== result
->usb_handle
)
1790 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid
, vsllink_usb_pid
);
1794 /* usb_set_configuration required under win32 */
1795 ret
= usb_set_configuration(result
->usb_handle
, dev
->config
[0].bConfigurationValue
);
1798 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev
->config
[0].bConfigurationValue
, ret
);
1801 ret
= usb_claim_interface(result
->usb_handle
, vsllink_usb_interface
);
1804 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface
, ret
);
1810 * This makes problems under Mac OS X. And is not needed
1811 * under Windows. Hopefully this will not break a linux build
1813 usb_set_altinterface(result
->usb_handle
, 0);
1824 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
)
1828 ret
= usb_release_interface(vsllink_jtag
->usb_handle
, vsllink_usb_interface
);
1831 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface
, ret
);
1835 ret
= usb_close(vsllink_jtag
->usb_handle
);
1838 LOG_ERROR("fail to close usb, %d returned", ret
);
1845 /* Send a message and receive the reply. */
1846 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
)
1850 result
= vsllink_usb_write(vsllink_jtag
, out_length
);
1851 if (result
== out_length
)
1855 result
= vsllink_usb_read(vsllink_jtag
);
1856 if (result
== in_length
)
1862 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length
, result
);
1870 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length
, result
);
1875 /* Write data from out_buffer to USB. */
1876 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
)
1880 if (out_length
> VSLLINK_BufferSize
)
1882 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length
, VSLLINK_BufferSize
);
1886 result
= usb_bulk_write(vsllink_jtag
->usb_handle
, vsllink_usb_bulkout
, \
1887 (char *)vsllink_usb_out_buffer
, out_length
, VSLLINK_USB_TIMEOUT
);
1889 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length
, result
);
1891 #ifdef _DEBUG_USB_COMMS_
1892 LOG_DEBUG("USB out:");
1893 vsllink_debug_buffer(vsllink_usb_out_buffer
, out_length
);
1896 #ifdef _VSLLINK_IN_DEBUG_MODE_
1903 /* Read data from USB into in_buffer. */
1904 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
)
1906 int result
= usb_bulk_read(vsllink_jtag
->usb_handle
, vsllink_usb_bulkin
, \
1907 (char *)vsllink_usb_in_buffer
, VSLLINK_BufferSize
, VSLLINK_USB_TIMEOUT
);
1909 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result
);
1911 #ifdef _DEBUG_USB_COMMS_
1912 LOG_DEBUG("USB in:");
1913 vsllink_debug_buffer(vsllink_usb_in_buffer
, result
);
1918 #define BYTES_PER_LINE 16
1920 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1921 static void vsllink_debug_buffer(u8
*buffer
, int length
)
1928 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
)
1930 snprintf(line
, 5, "%04x", i
);
1931 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++)
1933 snprintf(s
, 4, " %02x", buffer
[j
]);
1936 LOG_DEBUG("%s", line
);
1939 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_