1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian <SimonQian@SimonQian.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 /* Versaloon is a programming tool for multiple MCUs.
21 * OpenOCD and MSP430 supports are distributed under GPLv2.
22 * You can find it at http://www.SimonQian.com/en/Versaloon.
34 //#define _VSLLINK_IN_DEBUG_MODE_
36 #define VSLLINK_MODE_NORMAL 0
37 #define VSLLINK_MODE_DMA 1
39 static u16 vsllink_usb_vid
;
40 static u16 vsllink_usb_pid
;
41 static u8 vsllink_usb_bulkout
;
42 static u8 vsllink_usb_bulkin
;
43 static u8 vsllink_usb_interface
;
44 static u8 vsllink_mode
= VSLLINK_MODE_NORMAL
;
45 static int VSLLINK_USB_TIMEOUT
= 10000;
47 static int VSLLINK_BufferSize
= 1024;
49 /* Global USB buffers */
50 static int vsllink_usb_out_buffer_idx
;
51 static int vsllink_usb_in_want_length
;
52 static u8
* vsllink_usb_in_buffer
= NULL
;
53 static u8
* vsllink_usb_out_buffer
= NULL
;
55 /* Constants for VSLLink command */
56 #define VSLLINK_CMD_CONN 0x80
57 #define VSLLINK_CMD_DISCONN 0x81
58 #define VSLLINK_CMD_SET_SPEED 0x82
59 #define VSLLINK_CMD_SET_PORT 0x90
60 #define VSLLINK_CMD_GET_PORT 0x91
61 #define VSLLINK_CMD_SET_PORTDIR 0x92
62 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
63 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
64 #define VSLLINK_CMD_HW_SWDCMD 0xA2
65 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
67 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
68 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
69 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
71 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
72 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
74 #define JTAG_PINMSK_SRST (1 << 0)
75 #define JTAG_PINMSK_TRST (1 << 1)
76 #define JTAG_PINMSK_USR1 (1 << 2)
77 #define JTAG_PINMSK_USR2 (1 << 3)
78 #define JTAG_PINMSK_TCK (1 << 4)
79 #define JTAG_PINMSK_TMS (1 << 5)
80 #define JTAG_PINMSK_TDI (1 << 6)
81 #define JTAG_PINMSK_TDO (1 << 7)
84 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
86 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
94 * SD->SD and SI->SI have to be caught in interface specific code
96 static u8 VSLLINK_tap_move
[6][6] =
98 /* TLR RTI SD PD SI PI */
99 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
100 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
101 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
102 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
103 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
104 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
107 typedef struct insert_insignificant_operation
109 unsigned char insert_value
;
110 unsigned char insert_position
;
111 }insert_insignificant_operation_t
;
113 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[6][6] =
160 static u8 VSLLINK_BIT_MSK
[8] =
162 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
168 int length
; /* Number of bits to read */
169 scan_command_t
*command
; /* Corresponding scan command */
171 } pending_scan_result_t
;
173 #define MAX_PENDING_SCAN_RESULTS 256
175 static int pending_scan_results_length
;
176 static pending_scan_result_t pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
178 /* External interface functions */
179 static int vsllink_execute_queue(void);
180 static int vsllink_speed(int speed
);
181 static int vsllink_khz(int khz
, int *jtag_speed
);
182 static int vsllink_speed_div(int jtag_speed
, int *khz
);
183 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
);
184 static int vsllink_init(void);
185 static int vsllink_quit(void);
187 /* CLI command handler functions */
188 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
189 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
190 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
191 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
192 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
193 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
195 /* Queue command functions */
196 static void vsllink_end_state(tap_state_t state
);
197 static void vsllink_state_move_dma(void);
198 static void vsllink_state_move_normal(void);
199 static void (*vsllink_state_move
)(void);
200 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
);
201 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
);
202 static void (*vsllink_path_move
)(int num_states
, tap_state_t
*path
);
203 static void vsllink_runtest(int num_cycles
);
204 static void vsllink_stableclocks_dma(int num_cycles
, int tms
);
205 static void vsllink_stableclocks_normal(int num_cycles
, int tms
);
206 static void (*vsllink_stableclocks
)(int num_cycles
, int tms
);
207 static void vsllink_scan_dma(bool ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
208 static void vsllink_scan_normal(bool ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
209 static void (*vsllink_scan
)(bool ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
210 static void vsllink_reset(int trst
, int srst
);
211 static void vsllink_simple_command(u8 command
);
212 static int vsllink_connect(void);
213 static int vsllink_disconnect(void);
215 /* VSLLink tap buffer functions */
216 static void vsllink_tap_append_step(int tms
, int tdi
);
217 static void vsllink_tap_init_dma(void);
218 static void vsllink_tap_init_normal(void);
219 static void (*vsllink_tap_init
)(void);
220 static int vsllink_tap_execute_dma(void);
221 static int vsllink_tap_execute_normal(void);
222 static int (*vsllink_tap_execute
)(void);
223 static void vsllink_tap_ensure_space_dma(int scans
, int length
);
224 static void vsllink_tap_ensure_space_normal(int scans
, int length
);
225 static void (*vsllink_tap_ensure_space
)(int scans
, int length
);
226 static void vsllink_tap_append_scan_dma(int length
, u8
*buffer
, scan_command_t
*command
);
227 static void vsllink_tap_append_scan_normal(int length
, u8
*buffer
, scan_command_t
*command
, int offset
);
229 /* VSLLink lowlevel functions */
230 typedef struct vsllink_jtag
232 struct usb_dev_handle
* usb_handle
;
235 static vsllink_jtag_t
*vsllink_usb_open(void);
236 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
);
237 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
);
238 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
);
239 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
);
241 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
242 static void vsllink_debug_buffer(u8
*buffer
, int length
);
245 static int vsllink_tms_data_len
= 0;
246 static u8
* vsllink_tms_cmd_pos
;
248 static int tap_length
= 0;
249 static int tap_buffer_size
= 0;
250 static u8
*tms_buffer
= NULL
;
251 static u8
*tdi_buffer
= NULL
;
252 static u8
*tdo_buffer
= NULL
;
255 static vsllink_jtag_t
* vsllink_jtag_handle
= NULL
;
257 /***************************************************************************/
258 /* External interface implementation */
260 jtag_interface_t vsllink_interface
=
263 .execute_queue
= vsllink_execute_queue
,
264 .speed
= vsllink_speed
,
266 .speed_div
= vsllink_speed_div
,
267 .register_commands
= vsllink_register_commands
,
268 .init
= vsllink_init
,
272 static void reset_command_pointer(void)
274 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
276 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
277 vsllink_usb_out_buffer_idx
= 3;
285 static int vsllink_execute_queue(void)
287 jtag_command_t
*cmd
= jtag_command_queue
;
292 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
294 reset_command_pointer();
300 DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd
->cmd
.end_state
->end_state
));
302 if (cmd
->cmd
.end_state
->end_state
!= TAP_INVALID
)
304 vsllink_end_state(cmd
->cmd
.end_state
->end_state
);
309 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd
->cmd
.runtest
->num_cycles
, \
310 tap_state_name(cmd
->cmd
.runtest
->end_state
));
312 if (cmd
->cmd
.runtest
->end_state
!= TAP_INVALID
)
314 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
316 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
320 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd
->cmd
.statemove
->end_state
));
322 if (cmd
->cmd
.statemove
->end_state
!= TAP_INVALID
)
324 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
326 vsllink_state_move();
330 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
331 cmd
->cmd
.pathmove
->num_states
, \
332 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
334 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
338 if (cmd
->cmd
.scan
->end_state
!= TAP_INVALID
)
340 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
343 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
344 if (cmd
->cmd
.scan
->ir_scan
)
346 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
350 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
353 #ifdef _DEBUG_JTAG_IO_
354 vsllink_debug_buffer(buffer
, (scan_size
+ 7) >> 3);
357 type
= jtag_scan_type(cmd
->cmd
.scan
);
359 vsllink_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
, cmd
->cmd
.scan
);
363 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
365 vsllink_tap_execute();
367 if (cmd
->cmd
.reset
->trst
== 1)
369 tap_set_state(TAP_RESET
);
371 vsllink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
375 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
376 vsllink_tap_execute();
377 jtag_sleep(cmd
->cmd
.sleep
->us
);
380 case JTAG_STABLECLOCKS
:
381 DEBUG_JTAG_IO("add %d clocks", cmd
->cmd
.stableclocks
->num_cycles
);
382 switch(tap_get_state())
385 // tms should be '1' to stay in TAP_RESET mode
393 // in other mode, tms should be '0'
395 break; /* above stable states are OK */
397 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
398 tap_state_name(tap_get_state()) );
401 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
405 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
411 return vsllink_tap_execute();
414 static int vsllink_speed(int speed
)
418 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_SPEED
;
419 vsllink_usb_out_buffer
[1] = (speed
>> 0) & 0xff;
420 vsllink_usb_out_buffer
[2] = (speed
>> 8) & 0xFF;
422 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
430 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
431 return ERROR_JTAG_DEVICE_ERROR
;
437 static int vsllink_khz(int khz
, int *jtag_speed
)
444 static int vsllink_speed_div(int jtag_speed
, int *khz
)
451 static int vsllink_init(void)
453 int check_cnt
, to_tmp
;
455 char version_str
[100];
457 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
458 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
459 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
461 LOG_ERROR("Not enough memory");
465 vsllink_jtag_handle
= vsllink_usb_open();
467 if (vsllink_jtag_handle
== 0)
469 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
470 return ERROR_JTAG_INIT_FAILED
;
472 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid
, vsllink_usb_pid
);
474 to_tmp
= VSLLINK_USB_TIMEOUT
;
475 VSLLINK_USB_TIMEOUT
= 100;
477 while (check_cnt
< 5)
479 vsllink_simple_command(0x00);
480 result
= vsllink_usb_read(vsllink_jtag_handle
);
484 vsllink_usb_in_buffer
[result
] = 0;
485 VSLLINK_BufferSize
= vsllink_usb_in_buffer
[0] + (vsllink_usb_in_buffer
[1] << 8);
486 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(version_str
));
487 LOG_INFO("%s", version_str
);
489 // free the pre-alloc memroy
490 free(vsllink_usb_in_buffer
);
491 free(vsllink_usb_out_buffer
);
492 vsllink_usb_in_buffer
= NULL
;
493 vsllink_usb_out_buffer
= NULL
;
496 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
497 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
498 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
500 LOG_ERROR("Not enough memory");
505 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize
);
507 // alloc memory for dma mode
508 if (vsllink_mode
== VSLLINK_MODE_DMA
)
510 tap_buffer_size
= (VSLLINK_BufferSize
- 3) / 2;
511 tms_buffer
= (u8
*)malloc(tap_buffer_size
);
512 tdi_buffer
= (u8
*)malloc(tap_buffer_size
);
513 tdo_buffer
= (u8
*)malloc(tap_buffer_size
);
514 if ((tms_buffer
== NULL
) || (tdi_buffer
== NULL
) || (tdo_buffer
== NULL
))
516 LOG_ERROR("Not enough memory");
522 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
527 // It's dangerout to proced
528 LOG_ERROR("VSLLink initial failed");
531 VSLLINK_USB_TIMEOUT
= to_tmp
;
533 // connect to vsllink
535 // initialize function pointers
536 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
539 vsllink_state_move
= vsllink_state_move_normal
;
540 vsllink_path_move
= vsllink_path_move_normal
;
541 vsllink_stableclocks
= vsllink_stableclocks_normal
;
542 vsllink_scan
= vsllink_scan_normal
;
544 vsllink_tap_init
= vsllink_tap_init_normal
;
545 vsllink_tap_execute
= vsllink_tap_execute_normal
;
546 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_normal
;
548 LOG_INFO("vsllink run in NORMAL mode");
553 vsllink_state_move
= vsllink_state_move_dma
;
554 vsllink_path_move
= vsllink_path_move_dma
;
555 vsllink_stableclocks
= vsllink_stableclocks_dma
;
556 vsllink_scan
= vsllink_scan_dma
;
558 vsllink_tap_init
= vsllink_tap_init_dma
;
559 vsllink_tap_execute
= vsllink_tap_execute_dma
;
560 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_dma
;
562 LOG_INFO("vsllink run in DMA mode");
565 // Set SRST and TRST to output, Set USR1 and USR2 to input
566 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
567 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
568 vsllink_usb_out_buffer
[2] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
569 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
571 LOG_ERROR("VSLLink USB send data error");
577 LOG_INFO("VSLLink JTAG Interface ready");
584 static int vsllink_quit(void)
586 if ((vsllink_usb_in_buffer
!= NULL
) && (vsllink_usb_out_buffer
!= NULL
))
588 // Set all pins to input
589 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
590 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
591 vsllink_usb_out_buffer
[2] = 0;
592 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
594 LOG_ERROR("VSLLink USB send data error");
599 vsllink_disconnect();
600 vsllink_usb_close(vsllink_jtag_handle
);
601 vsllink_jtag_handle
= NULL
;
604 if (vsllink_usb_in_buffer
!= NULL
)
606 free(vsllink_usb_in_buffer
);
607 vsllink_usb_in_buffer
= NULL
;
609 if (vsllink_usb_out_buffer
!= NULL
)
611 free(vsllink_usb_out_buffer
);
612 vsllink_usb_out_buffer
= NULL
;
618 /***************************************************************************/
619 /* Queue command implementations */
620 static int vsllink_disconnect(void)
622 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
626 static int vsllink_connect(void)
628 char vsllink_str
[100];
630 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_CONN
;
631 vsllink_usb_out_buffer
[1] = vsllink_mode
;
632 vsllink_usb_message(vsllink_jtag_handle
, 2, 0);
633 if (vsllink_usb_read(vsllink_jtag_handle
) > 2)
635 strncpy(vsllink_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(vsllink_str
));
636 LOG_INFO("%s", vsllink_str
);
642 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
643 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
644 static void vsllink_append_tms(void)
646 u8 tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
648 insert_insignificant_operation_t
*insert
= \
649 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
651 if (((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_DRPAUSE
) && (tap_get_state() != TAP_IRPAUSE
)) || \
652 (vsllink_tms_data_len
<= 0) || (vsllink_tms_data_len
>= 8) || \
653 (vsllink_tms_cmd_pos
== NULL
))
655 LOG_ERROR("There MUST be some bugs in the driver");
659 tms2
= (tms_scan
& VSLLINK_BIT_MSK
[insert
->insert_position
]) << \
660 vsllink_tms_data_len
;
661 if (insert
->insert_value
== 1)
663 tms2
|= VSLLINK_BIT_MSK
[8 - vsllink_tms_data_len
] << \
664 (vsllink_tms_data_len
+ insert
->insert_position
);
666 tms2
|= (tms_scan
>> insert
->insert_position
) << \
667 (8 + insert
->insert_position
);
669 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (tms2
>> 0) & 0xff;
670 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms2
>> 8) & 0xff;
672 vsllink_tms_data_len
= 0;
673 vsllink_tms_cmd_pos
= NULL
;
676 static void vsllink_end_state(tap_state_t state
)
678 if (tap_is_state_stable(state
))
680 tap_set_end_state(state
);
684 LOG_ERROR("BUG: %i is not a valid end state", state
);
689 /* Goes to the end state. */
690 static void vsllink_state_move_normal(void)
692 if (vsllink_tms_data_len
> 0)
694 vsllink_append_tms();
698 vsllink_tap_ensure_space(0, 2);
700 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
;
701 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
704 tap_set_state(tap_get_end_state());
706 static void vsllink_state_move_dma(void)
708 int i
, insert_length
= (tap_length
% 8) ? (8 - (tap_length
% 8)) : 0;
709 insert_insignificant_operation_t
*insert
= \
710 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
711 u8 tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
713 if (tap_get_state() == TAP_RESET
)
715 vsllink_tap_ensure_space(0, 8);
717 for (i
= 0; i
< 8; i
++)
719 vsllink_tap_append_step(1, 0);
723 if (insert_length
> 0)
725 vsllink_tap_ensure_space(0, 16);
727 for (i
= 0; i
< insert
->insert_position
; i
++)
729 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
731 for (i
= 0; i
< insert_length
; i
++)
733 vsllink_tap_append_step(insert
->insert_value
, 0);
735 for (i
= insert
->insert_position
; i
< 8; i
++)
737 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
742 vsllink_tap_ensure_space(0, 8);
744 for (i
= 0; i
< 8; i
++)
746 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
750 tap_set_state(tap_get_end_state());
753 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
754 static void vsllink_add_path(int start
, int num
, tap_state_t
*path
)
758 for (i
= start
; i
< (start
+ num
); i
++)
764 vsllink_usb_out_buffer_idx
++;
766 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
769 if (path
[i
- start
] == tap_state_transition(tap_get_state(), true))
771 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= 1 << (i
& 7);
773 else if (path
[i
- start
] == tap_state_transition(tap_get_state(), false))
779 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
782 tap_set_state(path
[i
- start
]);
784 if ((i
> 0) && ((i
& 7) == 0))
786 vsllink_usb_out_buffer_idx
++;
787 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
790 tap_set_end_state(tap_get_state());
793 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
)
795 int i
, tms_len
, tms_cmd_pos
, path_idx
= 0;
797 if (vsllink_tms_data_len
> 0)
799 // there are vsllink_tms_data_len more tms bits to be shifted
800 // so there are vsllink_tms_data_len + num_states tms bits in all
801 tms_len
= vsllink_tms_data_len
+ num_states
;
804 // merge into last tms shift
807 // just append tms data to the last tms byte
808 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
810 else if (tms_len
== 8)
812 // end last tms shift command
813 (*vsllink_tms_cmd_pos
)--;
814 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
816 else if (tms_len
< 16)
818 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
820 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
821 // there is enought tms length in the current tms shift command
822 (*vsllink_tms_cmd_pos
)++;
823 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
827 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
828 // not enough tms length in the current tms shift command
829 // so a new command should be added
830 // first decrease byte length of last tms shift command
831 (*vsllink_tms_cmd_pos
)--;
832 // append tms data to the last tms byte
833 vsllink_add_path(vsllink_tms_data_len
, 8 - vsllink_tms_data_len
, path
);
834 path
+= 8 - vsllink_tms_data_len
;
835 // add new command(3 bytes)
836 vsllink_tap_ensure_space(0, 3);
837 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
838 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
839 vsllink_add_path(0, num_states
- (8 - vsllink_tms_data_len
), path
);
842 else if (tms_len
== 16)
844 // end last tms shift command
845 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
848 vsllink_tms_data_len
= (vsllink_tms_data_len
+ num_states
) & 7;
849 if (vsllink_tms_data_len
== 0)
851 vsllink_tms_cmd_pos
= NULL
;
857 vsllink_add_path(vsllink_tms_data_len
, 16 - vsllink_tms_data_len
, path
);
859 path
+= 16 - vsllink_tms_data_len
;
860 num_states
-= 16 - vsllink_tms_data_len
;
861 vsllink_tms_data_len
= 0;
862 vsllink_tms_cmd_pos
= NULL
;
868 // Normal operation, don't need to append tms data
869 vsllink_tms_data_len
= num_states
& 7;
871 while (num_states
> 0)
873 if (num_states
> ((VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8))
875 i
= (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8;
881 tms_len
= (i
+ 7) >> 3;
882 vsllink_tap_ensure_space(0, tms_len
+ 2);
883 tms_cmd_pos
= vsllink_usb_out_buffer_idx
;
884 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| (tms_len
- 1);
886 vsllink_add_path(0, i
, path
+ path_idx
);
892 if (vsllink_tms_data_len
> 0)
894 if (tms_len
< (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1))
896 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[tms_cmd_pos
];
897 (*vsllink_tms_cmd_pos
)++;
901 vsllink_usb_out_buffer
[tms_cmd_pos
]--;
903 tms_len
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
904 vsllink_tap_ensure_space(0, 3);
905 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
906 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
907 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = tms_len
;
912 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
)
918 if ((8 - (tap_length
& 7)) < num_states
)
920 j
= 8 - (tap_length
& 7);
926 for (i
= 0; i
< j
; i
++)
928 if (path
[i
] == tap_state_transition(tap_get_state(), false))
930 vsllink_tap_append_step(0, 0);
932 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
934 vsllink_tap_append_step(1, 0);
938 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
941 tap_set_state(path
[i
]);
948 vsllink_tap_ensure_space(0, num_states
);
950 for (i
= 0; i
< num_states
; i
++)
952 if (path
[j
+ i
] == tap_state_transition(tap_get_state(), false))
954 vsllink_tap_append_step(0, 0);
956 else if (path
[j
+ i
] == tap_state_transition(tap_get_state(), true))
958 vsllink_tap_append_step(1, 0);
962 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
965 tap_set_state(path
[j
+ i
]);
969 tap_set_end_state(tap_get_state());
972 static void vsllink_stableclocks_normal(int num_cycles
, int tms
)
977 if (vsllink_tms_data_len
> 0)
979 // there are vsllink_tms_data_len more tms bits to be shifted
980 // so there are vsllink_tms_data_len + num_cycles tms bits in all
981 tms_len
= vsllink_tms_data_len
+ num_cycles
;
984 // append '1' for tms
985 tms_append_byte
= (u16
)((((1 << num_cycles
) - 1) << vsllink_tms_data_len
) & 0xFFFF);
989 // append '0' for tms
994 // merge into last tms shift
997 // just add to vsllink_tms_data_len
998 // same result if tun through
999 //vsllink_tms_data_len += num_cycles;
1000 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= (u8
)(tms_append_byte
& 0xFF);
1002 else if (tms_len
== 8)
1004 // end last tms shift command
1005 // just reduce it, and append last tms byte
1006 (*vsllink_tms_cmd_pos
)--;
1007 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1009 else if (tms_len
< 16)
1011 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
1013 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1014 // there is enought tms length in the current tms shift command
1015 // increase the tms byte length by 1 and set the last byte to 0
1016 (*vsllink_tms_cmd_pos
)++;
1017 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1018 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (u8
)(tms_append_byte
>> 8);
1022 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1023 // not enough tms length in the current tms shift command
1024 // so a new command should be added
1025 // first decrease byte length of last tms shift command
1026 (*vsllink_tms_cmd_pos
)--;
1027 // append last tms byte and move the command pointer to the next empty position
1028 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1029 // add new command(3 bytes)
1030 vsllink_tap_ensure_space(0, 3);
1031 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1032 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1033 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (u8
)(tms_append_byte
>> 8);
1036 else if (tms_len
== 16)
1038 // end last tms shift command
1039 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1040 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (u8
)(tms_append_byte
>> 8);
1043 vsllink_tms_data_len
= tms_len
& 7;
1044 if (vsllink_tms_data_len
== 0)
1046 vsllink_tms_cmd_pos
= NULL
;
1052 // more shifts will be needed
1053 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1054 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (u8
)(tms_append_byte
>> 8);
1056 num_cycles
-= 16 - vsllink_tms_data_len
;
1057 vsllink_tms_data_len
= 0;
1058 vsllink_tms_cmd_pos
= NULL
;
1061 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1063 if (vsllink_tms_data_len
> 0)
1069 LOG_ERROR("There MUST be some bugs in the driver");
1075 // get number of bytes left to be sent
1076 tms_len
= num_cycles
>> 3;
1079 vsllink_tap_ensure_space(1, 5);
1080 // if tms_len > 0, vsllink_tms_data_len == 0
1081 // so just add new command
1082 // LSB of the command byte is the tms value when do the shifting
1085 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
| 1;
1089 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
;
1091 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 0) & 0xff;
1092 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 8) & 0xff;
1093 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 16) & 0xff;
1094 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 24) & 0xff;
1096 vsllink_usb_in_want_length
+= 1;
1097 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= NULL
;
1098 pending_scan_results_length
++;
1100 if (tms_len
> 0xFFFF)
1102 vsllink_tap_execute();
1107 vsllink_tms_data_len
= num_cycles
& 7;
1108 if (vsllink_tms_data_len
> 0)
1110 vsllink_tap_ensure_space(0, 3);
1111 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1112 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1115 // append '1' for tms
1116 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (1 << vsllink_tms_data_len
) - 1;
1120 // append '0' for tms
1121 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0x00;
1126 static void vsllink_stableclocks_dma(int num_cycles
, int tms
)
1132 if ((8 - (tap_length
& 7)) < num_cycles
)
1134 cur_cycles
= 8 - (tap_length
& 7);
1138 cur_cycles
= num_cycles
;
1140 for (i
= 0; i
< cur_cycles
; i
++)
1142 vsllink_tap_append_step(tms
, 0);
1144 num_cycles
-= cur_cycles
;
1147 while (num_cycles
> 0)
1149 if (num_cycles
> 8 * tap_buffer_size
)
1151 cur_cycles
= 8 * tap_buffer_size
;
1155 cur_cycles
= num_cycles
;
1158 vsllink_tap_ensure_space(0, cur_cycles
);
1160 for (i
= 0; i
< cur_cycles
; i
++)
1162 vsllink_tap_append_step(tms
, 0);
1165 num_cycles
-= cur_cycles
;
1169 static void vsllink_runtest(int num_cycles
)
1171 tap_state_t saved_end_state
= tap_get_end_state();
1173 if (tap_get_state() != TAP_IDLE
)
1175 // enter into IDLE state
1176 vsllink_end_state(TAP_IDLE
);
1177 vsllink_state_move();
1180 vsllink_stableclocks(num_cycles
, 0);
1184 vsllink_end_state(saved_end_state
);
1185 tap_set_state(TAP_IDLE
);
1186 if (tap_get_end_state() != TAP_IDLE
)
1188 vsllink_state_move();
1192 static void vsllink_scan_normal(bool ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
1194 tap_state_t saved_end_state
;
1195 u8 bits_left
, tms_tmp
, tdi_len
;
1198 if (0 == scan_size
)
1203 tdi_len
= ((scan_size
+ 7) >> 3);
1204 if ((tdi_len
+ 7) > VSLLINK_BufferSize
)
1206 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1210 saved_end_state
= tap_get_end_state();
1212 /* Move to appropriate scan state */
1213 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1215 if (vsllink_tms_data_len
> 0)
1217 if (tap_get_state() == tap_get_end_state())
1219 // already in IRSHIFT or DRSHIFT state
1220 // merge tms data in the last tms shift command into next scan command
1221 if(*vsllink_tms_cmd_pos
< 1)
1223 LOG_ERROR("There MUST be some bugs in the driver");
1226 else if(*vsllink_tms_cmd_pos
< 2)
1228 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1229 vsllink_usb_out_buffer_idx
--;
1233 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1234 *vsllink_tms_cmd_pos
-= 2;
1237 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1238 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1239 // which is merged from the last tms shift command
1240 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1241 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1242 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 8) & 0xff;
1243 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = tms_tmp
;
1244 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[0] << (8 - vsllink_tms_data_len
);
1246 for (i
= 0; i
< tdi_len
; i
++)
1248 buffer
[i
] >>= 8 - vsllink_tms_data_len
;
1251 buffer
[i
] += buffer
[i
+ 1] << vsllink_tms_data_len
;
1255 vsllink_tap_append_scan_normal(scan_size
- vsllink_tms_data_len
, buffer
, command
, vsllink_tms_data_len
);
1256 scan_size
-= 8 - vsllink_tms_data_len
;
1257 vsllink_tms_data_len
= 0;
1261 vsllink_state_move();
1262 vsllink_tap_ensure_space(1, tdi_len
+ 5);
1264 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
;
1265 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 0) & 0xff;
1266 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 8) & 0xff;
1268 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 0);
1273 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1275 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1276 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1277 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1)>> 8) & 0xff;
1278 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1279 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1281 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 8);
1283 vsllink_end_state(saved_end_state
);
1285 bits_left
= scan_size
& 0x07;
1286 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1290 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << (bits_left
- 1);
1294 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << 7;
1297 if (tap_get_state() != tap_get_end_state())
1299 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1303 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1306 tap_set_state(tap_get_end_state());
1308 static void vsllink_scan_dma(bool ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
1310 tap_state_t saved_end_state
;
1312 saved_end_state
= tap_get_end_state();
1314 /* Move to appropriate scan state */
1315 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1317 vsllink_state_move();
1318 vsllink_end_state(saved_end_state
);
1321 vsllink_tap_append_scan_dma(scan_size
, buffer
, command
);
1323 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1324 while (tap_length
% 8 != 0)
1327 vsllink_tap_append_step(0, 0);
1330 if (tap_get_state() != tap_get_end_state())
1332 vsllink_state_move();
1336 static void vsllink_reset(int trst
, int srst
)
1340 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
1342 /* Signals are active low */
1343 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORT
;
1344 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
1345 vsllink_usb_out_buffer
[2] = 0;
1348 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
1352 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
1355 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
1358 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result
);
1362 static void vsllink_simple_command(u8 command
)
1366 DEBUG_JTAG_IO("0x%02x", command
);
1368 vsllink_usb_out_buffer
[0] = command
;
1369 result
= vsllink_usb_write(vsllink_jtag_handle
, 1);
1373 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command
, result
);
1377 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
)
1379 register_command(cmd_ctx
, NULL
, "vsllink_usb_vid", vsllink_handle_usb_vid_command
,
1380 COMMAND_CONFIG
, NULL
);
1381 register_command(cmd_ctx
, NULL
, "vsllink_usb_pid", vsllink_handle_usb_pid_command
,
1382 COMMAND_CONFIG
, NULL
);
1383 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command
,
1384 COMMAND_CONFIG
, NULL
);
1385 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command
,
1386 COMMAND_CONFIG
, NULL
);
1387 register_command(cmd_ctx
, NULL
, "vsllink_usb_interface", vsllink_handle_usb_interface_command
,
1388 COMMAND_CONFIG
, NULL
);
1389 register_command(cmd_ctx
, NULL
, "vsllink_mode", vsllink_handle_mode_command
,
1390 COMMAND_CONFIG
, NULL
);
1395 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1398 LOG_ERROR("parameter error, should be one parameter for VID");
1402 if (!strcmp(args
[0], "normal"))
1404 vsllink_mode
= VSLLINK_MODE_NORMAL
;
1406 else if (!strcmp(args
[0], "dma"))
1408 vsllink_mode
= VSLLINK_MODE_DMA
;
1412 LOG_ERROR("invalid vsllink_mode: %s", args
[0]);
1419 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1423 LOG_ERROR("parameter error, should be one parameter for VID");
1427 vsllink_usb_vid
= strtol(args
[0], NULL
, 0);
1432 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1436 LOG_ERROR("parameter error, should be one parameter for PID");
1440 vsllink_usb_pid
= strtol(args
[0], NULL
, 0);
1445 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1449 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1453 vsllink_usb_bulkin
= strtol(args
[0], NULL
, 0) | 0x80;
1458 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1462 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1466 vsllink_usb_bulkout
= strtol(args
[0], NULL
, 0);
1471 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1475 LOG_ERROR("parameter error, should be one parameter for interface number");
1479 vsllink_usb_interface
= strtol(args
[0], NULL
, 0);
1484 /***************************************************************************/
1485 /* VSLLink tap functions */
1487 static void vsllink_tap_init_normal(void)
1489 vsllink_usb_out_buffer_idx
= 0;
1490 vsllink_usb_in_want_length
= 0;
1491 pending_scan_results_length
= 0;
1493 static void vsllink_tap_init_dma(void)
1496 pending_scan_results_length
= 0;
1499 static void vsllink_tap_ensure_space_normal(int scans
, int length
)
1501 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1502 int available_bytes
= VSLLINK_BufferSize
- vsllink_usb_out_buffer_idx
;
1504 if (scans
> available_scans
|| length
> available_bytes
)
1506 vsllink_tap_execute();
1509 static void vsllink_tap_ensure_space_dma(int scans
, int length
)
1511 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1512 int available_bytes
= tap_buffer_size
* 8 - tap_length
;
1514 if (scans
> available_scans
|| length
> available_bytes
)
1516 vsllink_tap_execute();
1520 static void vsllink_tap_append_step(int tms
, int tdi
)
1523 int index
= tap_length
/ 8;
1525 if (index
< tap_buffer_size
)
1527 int bit_index
= tap_length
% 8;
1528 u8 bit
= 1 << bit_index
;
1532 tms_buffer
[index
] |= bit
;
1536 tms_buffer
[index
] &= ~bit
;
1541 tdi_buffer
[index
] |= bit
;
1545 tdi_buffer
[index
] &= ~bit
;
1552 LOG_ERROR("buffer overflow, tap_length=%d", tap_length
);
1556 static void vsllink_tap_append_scan_normal(int length
, u8
*buffer
, scan_command_t
*command
, int offset
)
1558 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1563 vsllink_usb_in_want_length
+= ((length
+ 7) >> 3) + 1;
1567 vsllink_usb_in_want_length
+= (length
+ 7) >> 3;
1569 pending_scan_result
->length
= length
;
1570 pending_scan_result
->offset
= offset
;
1571 pending_scan_result
->command
= command
;
1572 pending_scan_result
->buffer
= buffer
;
1574 for (i
= 0; i
< ((length
+ 7) >> 3); i
++)
1576 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[i
];
1579 pending_scan_results_length
++;
1581 static void vsllink_tap_append_scan_dma(int length
, u8
*buffer
, scan_command_t
*command
)
1583 pending_scan_result_t
*pending_scan_result
;
1584 int len_tmp
, len_all
, i
;
1587 while (len_all
< length
)
1589 if ((length
- len_all
) > tap_buffer_size
* 8)
1591 len_tmp
= tap_buffer_size
* 8;
1595 len_tmp
= length
- len_all
;
1598 vsllink_tap_ensure_space(1, (len_tmp
+ 7) & ~7);
1600 pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1601 pending_scan_result
->offset
= tap_length
;
1602 pending_scan_result
->length
= len_tmp
;
1603 pending_scan_result
->command
= command
;
1604 pending_scan_result
->buffer
= buffer
+ len_all
/ 8;
1606 for (i
= 0; i
< len_tmp
; i
++)
1608 vsllink_tap_append_step(((len_all
+i
) < length
-1 ? 0 : 1), (buffer
[(len_all
+i
)/8] >> ((len_all
+i
)%8)) & 1);
1611 pending_scan_results_length
++;
1616 /* Pad and send a tap sequence to the device, and receive the answer.
1617 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1618 static int vsllink_tap_execute_normal(void)
1624 if (vsllink_tms_data_len
> 0)
1626 if((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_IRPAUSE
) && (tap_get_state() != TAP_DRPAUSE
))
1628 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1631 if (vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] & (1 << (vsllink_tms_data_len
- 1)))
1633 // last tms bit is '1'
1634 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= 0xFF << vsllink_tms_data_len
;
1635 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0xFF;
1636 vsllink_tms_data_len
= 0;
1640 // last tms bit is '0'
1641 vsllink_usb_out_buffer_idx
++;
1642 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1643 vsllink_tms_data_len
= 0;
1647 if (vsllink_usb_out_buffer_idx
> 3)
1649 if (vsllink_usb_out_buffer
[0] == VSLLINK_CMD_HW_JTAGSEQCMD
)
1651 vsllink_usb_out_buffer
[1] = (vsllink_usb_out_buffer_idx
>> 0) & 0xff;
1652 vsllink_usb_out_buffer
[2] = (vsllink_usb_out_buffer_idx
>> 8) & 0xff;
1655 result
= vsllink_usb_message(vsllink_jtag_handle
, vsllink_usb_out_buffer_idx
, vsllink_usb_in_want_length
);
1657 if (result
== vsllink_usb_in_want_length
)
1659 for (i
= 0; i
< pending_scan_results_length
; i
++)
1661 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1662 u8
*buffer
= pending_scan_result
->buffer
;
1663 int length
= pending_scan_result
->length
;
1664 int offset
= pending_scan_result
->offset
;
1665 scan_command_t
*command
= pending_scan_result
->command
;
1669 // IRSHIFT or DRSHIFT
1670 buf_set_buf(vsllink_usb_in_buffer
, first
* 8 + offset
, buffer
, 0, length
);
1671 first
+= (length
+ offset
+ 7) >> 3;
1673 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length
);
1674 #ifdef _DEBUG_JTAG_IO_
1675 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1678 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1681 return ERROR_JTAG_QUEUE_FAILED
;
1684 free(pending_scan_result
->buffer
);
1685 pending_scan_result
->buffer
= NULL
;
1695 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, vsllink_usb_in_want_length
);
1696 return ERROR_JTAG_QUEUE_FAILED
;
1701 reset_command_pointer();
1705 static int vsllink_tap_execute_dma(void)
1713 /* Pad last byte so that tap_length is divisible by 8 */
1714 while (tap_length
% 8 != 0)
1716 /* More of the last TMS value keeps us in the same state,
1717 * analogous to free-running JTAG interfaces. */
1718 vsllink_tap_append_step(last_tms
, 0);
1720 byte_length
= tap_length
/ 8;
1722 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGRAWCMD
;
1723 vsllink_usb_out_buffer
[1] = ((byte_length
* 2 + 3) >> 0) & 0xff; // package size
1724 vsllink_usb_out_buffer
[2] = ((byte_length
* 2 + 3) >> 8) & 0xff;
1726 memcpy(&vsllink_usb_out_buffer
[3], tdi_buffer
, byte_length
);
1727 memcpy(&vsllink_usb_out_buffer
[3 + byte_length
], tms_buffer
, byte_length
);
1729 result
= vsllink_usb_message(vsllink_jtag_handle
, 3 + 2 * byte_length
, byte_length
);
1730 if (result
== byte_length
)
1732 for (i
= 0; i
< pending_scan_results_length
; i
++)
1734 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1735 u8
*buffer
= pending_scan_result
->buffer
;
1736 int length
= pending_scan_result
->length
;
1737 int first
= pending_scan_result
->offset
;
1739 scan_command_t
*command
= pending_scan_result
->command
;
1740 buf_set_buf(vsllink_usb_in_buffer
, first
, buffer
, 0, length
);
1742 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length
, first
);
1743 #ifdef _DEBUG_JTAG_IO_
1744 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1747 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1750 return ERROR_JTAG_QUEUE_FAILED
;
1753 if (pending_scan_result
->buffer
!= NULL
)
1755 free(pending_scan_result
->buffer
);
1761 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, byte_length
);
1762 return ERROR_JTAG_QUEUE_FAILED
;
1771 /*****************************************************************************/
1772 /* VSLLink USB low-level functions */
1774 static vsllink_jtag_t
* vsllink_usb_open(void)
1776 struct usb_bus
*busses
;
1777 struct usb_bus
*bus
;
1778 struct usb_device
*dev
;
1781 vsllink_jtag_t
*result
;
1783 result
= (vsllink_jtag_t
*) malloc(sizeof(vsllink_jtag_t
));
1789 busses
= usb_get_busses();
1791 /* find vsllink_jtag device in usb bus */
1793 for (bus
= busses
; bus
; bus
= bus
->next
)
1795 for (dev
= bus
->devices
; dev
; dev
= dev
->next
)
1797 if ((dev
->descriptor
.idVendor
== vsllink_usb_vid
) && (dev
->descriptor
.idProduct
== vsllink_usb_pid
))
1799 result
->usb_handle
= usb_open(dev
);
1800 if (NULL
== result
->usb_handle
)
1802 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid
, vsllink_usb_pid
);
1806 /* usb_set_configuration required under win32 */
1807 ret
= usb_set_configuration(result
->usb_handle
, dev
->config
[0].bConfigurationValue
);
1810 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev
->config
[0].bConfigurationValue
, ret
);
1813 ret
= usb_claim_interface(result
->usb_handle
, vsllink_usb_interface
);
1816 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface
, ret
);
1822 * This makes problems under Mac OS X. And is not needed
1823 * under Windows. Hopefully this will not break a linux build
1825 usb_set_altinterface(result
->usb_handle
, 0);
1836 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
)
1840 ret
= usb_release_interface(vsllink_jtag
->usb_handle
, vsllink_usb_interface
);
1843 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface
, ret
);
1847 ret
= usb_close(vsllink_jtag
->usb_handle
);
1850 LOG_ERROR("fail to close usb, %d returned", ret
);
1857 /* Send a message and receive the reply. */
1858 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
)
1862 result
= vsllink_usb_write(vsllink_jtag
, out_length
);
1863 if (result
== out_length
)
1867 result
= vsllink_usb_read(vsllink_jtag
);
1868 if (result
== in_length
)
1874 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length
, result
);
1882 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length
, result
);
1887 /* Write data from out_buffer to USB. */
1888 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
)
1892 if (out_length
> VSLLINK_BufferSize
)
1894 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length
, VSLLINK_BufferSize
);
1898 result
= usb_bulk_write(vsllink_jtag
->usb_handle
, vsllink_usb_bulkout
, \
1899 (char *)vsllink_usb_out_buffer
, out_length
, VSLLINK_USB_TIMEOUT
);
1901 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length
, result
);
1903 #ifdef _DEBUG_USB_COMMS_
1904 LOG_DEBUG("USB out:");
1905 vsllink_debug_buffer(vsllink_usb_out_buffer
, out_length
);
1908 #ifdef _VSLLINK_IN_DEBUG_MODE_
1915 /* Read data from USB into in_buffer. */
1916 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
)
1918 int result
= usb_bulk_read(vsllink_jtag
->usb_handle
, vsllink_usb_bulkin
, \
1919 (char *)vsllink_usb_in_buffer
, VSLLINK_BufferSize
, VSLLINK_USB_TIMEOUT
);
1921 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result
);
1923 #ifdef _DEBUG_USB_COMMS_
1924 LOG_DEBUG("USB in:");
1925 vsllink_debug_buffer(vsllink_usb_in_buffer
, result
);
1930 #define BYTES_PER_LINE 16
1932 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1933 static void vsllink_debug_buffer(u8
*buffer
, int length
)
1940 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
)
1942 snprintf(line
, 5, "%04x", i
);
1943 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++)
1945 snprintf(s
, 4, " %02x", buffer
[j
]);
1948 LOG_DEBUG("%s", line
);
1951 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_