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 /* enable this to view USB communication
43 #define _DEBUG_USB_COMMS_
46 #ifdef _DEBUG_JTAG_IO_
47 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
49 #define DEBUG_JTAG_IO(expr ...)
52 #define VSLLINK_MODE_NORMAL 0
53 #define VSLLINK_MODE_DMA 1
55 static u16 vsllink_usb_vid
;
56 static u16 vsllink_usb_pid
;
57 static u8 vsllink_usb_bulkout
;
58 static u8 vsllink_usb_bulkin
;
59 static u8 vsllink_usb_interface
;
60 static u8 vsllink_mode
= VSLLINK_MODE_NORMAL
;
61 static int VSLLINK_USB_TIMEOUT
= 10000;
63 static int VSLLINK_BufferSize
= 1024;
65 /* Global USB buffers */
66 static int vsllink_usb_out_buffer_idx
;
67 static int vsllink_usb_in_want_length
;
68 static u8
* vsllink_usb_in_buffer
= NULL
;
69 static u8
* vsllink_usb_out_buffer
= NULL
;
71 /* Constants for VSLLink command */
72 #define VSLLINK_CMD_CONN 0x80
73 #define VSLLINK_CMD_DISCONN 0x81
74 #define VSLLINK_CMD_SET_SPEED 0x82
75 #define VSLLINK_CMD_SET_PORT 0x90
76 #define VSLLINK_CMD_GET_PORT 0x91
77 #define VSLLINK_CMD_SET_PORTDIR 0x92
78 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
79 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
80 #define VSLLINK_CMD_HW_SWDCMD 0xA2
81 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
83 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
84 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
85 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
87 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
88 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
90 #define JTAG_PINMSK_SRST (1 << 0)
91 #define JTAG_PINMSK_TRST (1 << 1)
92 #define JTAG_PINMSK_USR1 (1 << 2)
93 #define JTAG_PINMSK_USR2 (1 << 3)
94 #define JTAG_PINMSK_TCK (1 << 4)
95 #define JTAG_PINMSK_TMS (1 << 5)
96 #define JTAG_PINMSK_TDI (1 << 6)
97 #define JTAG_PINMSK_TDO (1 << 7)
100 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
102 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
103 * 0: Test-Logic-Reset
110 * SD->SD and SI->SI have to be caught in interface specific code
112 static u8 VSLLINK_tap_move
[6][6] =
114 /* TLR RTI SD PD SI PI */
115 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
116 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
117 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
118 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
119 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
120 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
123 typedef struct insert_insignificant_operation
125 unsigned char insert_value
;
126 unsigned char insert_position
;
127 }insert_insignificant_operation_t
;
129 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[6][6] =
176 static u8 VSLLINK_BIT_MSK
[8] =
178 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
184 int length
; /* Number of bits to read */
185 scan_command_t
*command
; /* Corresponding scan command */
187 } pending_scan_result_t
;
189 #define MAX_PENDING_SCAN_RESULTS 256
191 static int pending_scan_results_length
;
192 static pending_scan_result_t pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
194 /* External interface functions */
195 static int vsllink_execute_queue(void);
196 static int vsllink_speed(int speed
);
197 static int vsllink_khz(int khz
, int *jtag_speed
);
198 static int vsllink_speed_div(int jtag_speed
, int *khz
);
199 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
);
200 static int vsllink_init(void);
201 static int vsllink_quit(void);
203 /* CLI command handler functions */
204 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
205 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
206 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
207 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
208 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
209 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
211 /* Queue command functions */
212 static void vsllink_end_state(tap_state_t state
);
213 static void vsllink_state_move_dma(void);
214 static void vsllink_state_move_normal(void);
215 static void (*vsllink_state_move
)(void);
216 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
);
217 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
);
218 static void (*vsllink_path_move
)(int num_states
, tap_state_t
*path
);
219 static void vsllink_runtest(int num_cycles
);
220 static void vsllink_stableclocks_dma(int num_cycles
, int tms
);
221 static void vsllink_stableclocks_normal(int num_cycles
, int tms
);
222 static void (*vsllink_stableclocks
)(int num_cycles
, int tms
);
223 static void vsllink_scan_dma(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
224 static void vsllink_scan_normal(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
225 static void (*vsllink_scan
)(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
);
226 static void vsllink_reset(int trst
, int srst
);
227 static void vsllink_simple_command(u8 command
);
228 static int vsllink_connect(void);
229 static int vsllink_disconnect(void);
231 /* VSLLink tap buffer functions */
232 static void vsllink_tap_append_step(int tms
, int tdi
);
233 static void vsllink_tap_init_dma(void);
234 static void vsllink_tap_init_normal(void);
235 static void (*vsllink_tap_init
)(void);
236 static int vsllink_tap_execute_dma(void);
237 static int vsllink_tap_execute_normal(void);
238 static int (*vsllink_tap_execute
)(void);
239 static void vsllink_tap_ensure_space_dma(int scans
, int length
);
240 static void vsllink_tap_ensure_space_normal(int scans
, int length
);
241 static void (*vsllink_tap_ensure_space
)(int scans
, int length
);
242 static void vsllink_tap_append_scan_dma(int length
, u8
*buffer
, scan_command_t
*command
);
243 static void vsllink_tap_append_scan_normal(int length
, u8
*buffer
, scan_command_t
*command
, int offset
);
245 /* VSLLink lowlevel functions */
246 typedef struct vsllink_jtag
248 struct usb_dev_handle
* usb_handle
;
251 static vsllink_jtag_t
*vsllink_usb_open(void);
252 static void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
);
253 static int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
);
254 static int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
);
255 static int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
);
257 #ifdef _DEBUG_USB_COMMS_
258 static void vsllink_debug_buffer(u8
*buffer
, int length
);
261 static int vsllink_tms_data_len
= 0;
262 static u8
* vsllink_tms_cmd_pos
;
264 static int tap_length
= 0;
265 static int tap_buffer_size
= 0;
266 static u8
*tms_buffer
= NULL
;
267 static u8
*tdi_buffer
= NULL
;
268 static u8
*tdo_buffer
= NULL
;
271 static vsllink_jtag_t
* vsllink_jtag_handle
= NULL
;
273 /***************************************************************************/
274 /* External interface implementation */
276 jtag_interface_t vsllink_interface
=
279 .execute_queue
= vsllink_execute_queue
,
280 .speed
= vsllink_speed
,
282 .speed_div
= vsllink_speed_div
,
283 .register_commands
= vsllink_register_commands
,
284 .init
= vsllink_init
,
288 static void reset_command_pointer(void)
290 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
292 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
293 vsllink_usb_out_buffer_idx
= 3;
301 static int vsllink_execute_queue(void)
303 jtag_command_t
*cmd
= jtag_command_queue
;
308 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
310 reset_command_pointer();
316 DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd
->cmd
.end_state
->end_state
));
318 if (cmd
->cmd
.end_state
->end_state
!= TAP_INVALID
)
320 vsllink_end_state(cmd
->cmd
.end_state
->end_state
);
325 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd
->cmd
.runtest
->num_cycles
, \
326 tap_state_name(cmd
->cmd
.runtest
->end_state
));
328 if (cmd
->cmd
.runtest
->end_state
!= TAP_INVALID
)
330 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
332 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
336 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd
->cmd
.statemove
->end_state
));
338 if (cmd
->cmd
.statemove
->end_state
!= TAP_INVALID
)
340 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
342 vsllink_state_move();
346 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
347 cmd
->cmd
.pathmove
->num_states
, \
348 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
350 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
354 if (cmd
->cmd
.scan
->end_state
!= TAP_INVALID
)
356 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
359 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
360 if (cmd
->cmd
.scan
->ir_scan
)
362 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
366 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
369 #ifdef _DEBUG_JTAG_IO_
370 vsllink_debug_buffer(buffer
, (scan_size
+ 7) >> 3);
373 type
= jtag_scan_type(cmd
->cmd
.scan
);
375 vsllink_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
, cmd
->cmd
.scan
);
379 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
381 vsllink_tap_execute();
383 if (cmd
->cmd
.reset
->trst
== 1)
385 tap_set_state(TAP_RESET
);
387 vsllink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
391 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
392 vsllink_tap_execute();
393 jtag_sleep(cmd
->cmd
.sleep
->us
);
396 case JTAG_STABLECLOCKS
:
397 DEBUG_JTAG_IO("add %d clocks", cmd
->cmd
.stableclocks
->num_cycles
);
398 switch(tap_get_state())
401 // tms should be '1' to stay in TAP_RESET mode
409 // in other mode, tms should be '0'
411 break; /* above stable states are OK */
413 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
414 tap_state_name(tap_get_state()) );
417 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
421 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
427 return vsllink_tap_execute();
430 static int vsllink_speed(int speed
)
434 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_SPEED
;
435 vsllink_usb_out_buffer
[1] = (speed
>> 0) & 0xff;
436 vsllink_usb_out_buffer
[2] = (speed
>> 8) & 0xFF;
438 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
446 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
447 return ERROR_JTAG_DEVICE_ERROR
;
453 static int vsllink_khz(int khz
, int *jtag_speed
)
460 static int vsllink_speed_div(int jtag_speed
, int *khz
)
467 static int vsllink_init(void)
469 int check_cnt
, to_tmp
;
471 char version_str
[100];
473 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
474 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
475 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
477 LOG_ERROR("Not enough memory");
481 vsllink_jtag_handle
= vsllink_usb_open();
483 if (vsllink_jtag_handle
== 0)
485 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
486 return ERROR_JTAG_INIT_FAILED
;
488 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid
, vsllink_usb_pid
);
490 to_tmp
= VSLLINK_USB_TIMEOUT
;
491 VSLLINK_USB_TIMEOUT
= 100;
493 while (check_cnt
< 5)
495 vsllink_simple_command(0x00);
496 result
= vsllink_usb_read(vsllink_jtag_handle
);
500 vsllink_usb_in_buffer
[result
] = 0;
501 VSLLINK_BufferSize
= vsllink_usb_in_buffer
[0] + (vsllink_usb_in_buffer
[1] << 8);
502 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(version_str
));
503 LOG_INFO("%s", version_str
);
505 // free the pre-alloc memroy
506 free(vsllink_usb_in_buffer
);
507 free(vsllink_usb_out_buffer
);
508 vsllink_usb_in_buffer
= NULL
;
509 vsllink_usb_out_buffer
= NULL
;
512 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
513 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
514 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
516 LOG_ERROR("Not enough memory");
521 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize
);
523 // alloc memory for dma mode
524 if (vsllink_mode
== VSLLINK_MODE_DMA
)
526 tap_buffer_size
= (VSLLINK_BufferSize
- 3) / 2;
527 tms_buffer
= (u8
*)malloc(tap_buffer_size
);
528 tdi_buffer
= (u8
*)malloc(tap_buffer_size
);
529 tdo_buffer
= (u8
*)malloc(tap_buffer_size
);
530 if ((tms_buffer
== NULL
) || (tdi_buffer
== NULL
) || (tdo_buffer
== NULL
))
532 LOG_ERROR("Not enough memory");
538 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
543 // It's dangerout to proced
544 LOG_ERROR("VSLLink initial failed");
547 VSLLINK_USB_TIMEOUT
= to_tmp
;
549 // connect to vsllink
551 // initialize function pointers
552 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
555 vsllink_state_move
= vsllink_state_move_normal
;
556 vsllink_path_move
= vsllink_path_move_normal
;
557 vsllink_stableclocks
= vsllink_stableclocks_normal
;
558 vsllink_scan
= vsllink_scan_normal
;
560 vsllink_tap_init
= vsllink_tap_init_normal
;
561 vsllink_tap_execute
= vsllink_tap_execute_normal
;
562 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_normal
;
564 LOG_INFO("vsllink run in NORMAL mode");
569 vsllink_state_move
= vsllink_state_move_dma
;
570 vsllink_path_move
= vsllink_path_move_dma
;
571 vsllink_stableclocks
= vsllink_stableclocks_dma
;
572 vsllink_scan
= vsllink_scan_dma
;
574 vsllink_tap_init
= vsllink_tap_init_dma
;
575 vsllink_tap_execute
= vsllink_tap_execute_dma
;
576 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_dma
;
578 LOG_INFO("vsllink run in DMA mode");
581 // Set SRST and TRST to output, Set USR1 and USR2 to input
582 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
583 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
584 vsllink_usb_out_buffer
[2] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
585 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
587 LOG_ERROR("VSLLink USB send data error");
593 LOG_INFO("VSLLink JTAG Interface ready");
600 static int vsllink_quit(void)
602 if ((vsllink_usb_in_buffer
!= NULL
) && (vsllink_usb_out_buffer
!= NULL
))
604 // Set all pins to input
605 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
606 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
607 vsllink_usb_out_buffer
[2] = 0;
608 if (vsllink_usb_write(vsllink_jtag_handle
, 3) != 3)
610 LOG_ERROR("VSLLink USB send data error");
615 vsllink_disconnect();
616 vsllink_usb_close(vsllink_jtag_handle
);
617 vsllink_jtag_handle
= NULL
;
620 if (vsllink_usb_in_buffer
!= NULL
)
622 free(vsllink_usb_in_buffer
);
623 vsllink_usb_in_buffer
= NULL
;
625 if (vsllink_usb_out_buffer
!= NULL
)
627 free(vsllink_usb_out_buffer
);
628 vsllink_usb_out_buffer
= NULL
;
634 /***************************************************************************/
635 /* Queue command implementations */
636 static int vsllink_disconnect(void)
638 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
642 static int vsllink_connect(void)
644 char vsllink_str
[100];
646 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_CONN
;
647 vsllink_usb_out_buffer
[1] = vsllink_mode
;
648 vsllink_usb_message(vsllink_jtag_handle
, 2, 0);
649 if (vsllink_usb_read(vsllink_jtag_handle
) > 2)
651 strncpy(vsllink_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(vsllink_str
));
652 LOG_INFO("%s", vsllink_str
);
658 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
659 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
660 static void vsllink_append_tms(void)
662 u8 tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
664 insert_insignificant_operation_t
*insert
= \
665 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
667 if (((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_DRPAUSE
) && (tap_get_state() != TAP_IRPAUSE
)) || \
668 (vsllink_tms_data_len
<= 0) || (vsllink_tms_data_len
>= 8) || \
669 (vsllink_tms_cmd_pos
== NULL
))
671 LOG_ERROR("There MUST be some bugs in the driver");
675 tms2
= (tms_scan
& VSLLINK_BIT_MSK
[insert
->insert_position
]) << \
676 vsllink_tms_data_len
;
677 if (insert
->insert_value
== 1)
679 tms2
|= VSLLINK_BIT_MSK
[8 - vsllink_tms_data_len
] << \
680 (vsllink_tms_data_len
+ insert
->insert_position
);
682 tms2
|= (tms_scan
>> insert
->insert_position
) << \
683 (8 + insert
->insert_position
);
685 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (tms2
>> 0) & 0xff;
686 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms2
>> 8) & 0xff;
688 vsllink_tms_data_len
= 0;
689 vsllink_tms_cmd_pos
= NULL
;
692 static void vsllink_end_state(tap_state_t state
)
694 if (tap_is_state_stable(state
))
696 tap_set_end_state(state
);
700 LOG_ERROR("BUG: %i is not a valid end state", state
);
705 /* Goes to the end state. */
706 static void vsllink_state_move_normal(void)
708 if (vsllink_tms_data_len
> 0)
710 vsllink_append_tms();
714 vsllink_tap_ensure_space(0, 2);
716 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
;
717 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
720 tap_set_state(tap_get_end_state());
722 static void vsllink_state_move_dma(void)
724 int i
, insert_length
= (tap_length
% 8) ? (8 - (tap_length
% 8)) : 0;
725 insert_insignificant_operation_t
*insert
= \
726 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
727 u8 tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
729 vsllink_tap_ensure_space(0, 8);
731 if (tap_get_state() == TAP_RESET
)
733 for (i
= 0; i
< 8; i
++)
735 vsllink_tap_append_step(1, 0);
739 if (insert_length
> 0)
741 vsllink_tap_ensure_space(0, 16);
743 for (i
= 0; i
< insert
->insert_position
; i
++)
745 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
747 for (i
= 0; i
< insert_length
; i
++)
749 vsllink_tap_append_step(insert
->insert_value
, 0);
751 for (i
= insert
->insert_position
; i
< 8; i
++)
753 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
758 vsllink_tap_ensure_space(0, 8);
760 for (i
= 0; i
< 8; i
++)
762 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
766 tap_set_state(tap_get_end_state());
769 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
770 static void vsllink_add_path(int start
, int num
, tap_state_t
*path
)
774 for (i
= start
; i
< (start
+ num
); i
++)
780 vsllink_usb_out_buffer_idx
++;
782 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
785 if (path
[i
- start
] == tap_state_transition(tap_get_state(), true))
787 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= 1 << (i
& 7);
789 else if (path
[i
- start
] == tap_state_transition(tap_get_state(), false))
795 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
798 tap_set_state(path
[i
- start
]);
800 if ((i
> 0) && ((i
& 7) == 0))
802 vsllink_usb_out_buffer_idx
++;
803 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
806 tap_set_end_state(tap_get_state());
809 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
)
811 int i
, tms_len
, tms_cmd_pos
, path_idx
= 0;
813 if (vsllink_tms_data_len
> 0)
815 // there are vsllink_tms_data_len more tms bits to be shifted
816 // so there are vsllink_tms_data_len + num_states tms bits in all
817 tms_len
= vsllink_tms_data_len
+ num_states
;
820 // merge into last tms shift
823 // just append tms data to the last tms byte
824 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
826 else if (tms_len
== 8)
828 // end last tms shift command
829 (*vsllink_tms_cmd_pos
)--;
830 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
832 else if (tms_len
< 16)
834 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
836 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
837 // there is enought tms length in the current tms shift command
838 (*vsllink_tms_cmd_pos
)++;
839 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
843 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
844 // not enough tms length in the current tms shift command
845 // so a new command should be added
846 // first decrease byte length of last tms shift command
847 (*vsllink_tms_cmd_pos
)--;
848 // append tms data to the last tms byte
849 vsllink_add_path(vsllink_tms_data_len
, 8 - vsllink_tms_data_len
, path
);
850 path
+= 8 - vsllink_tms_data_len
;
851 // add new command(3 bytes)
852 vsllink_tap_ensure_space(0, 3);
853 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
854 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
855 vsllink_add_path(0, num_states
- (8 - vsllink_tms_data_len
), path
);
858 else if (tms_len
== 16)
860 // end last tms shift command
861 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
864 vsllink_tms_data_len
= (vsllink_tms_data_len
+ num_states
) & 7;
865 if (vsllink_tms_data_len
== 0)
867 vsllink_tms_cmd_pos
= NULL
;
873 vsllink_add_path(vsllink_tms_data_len
, 16 - vsllink_tms_data_len
, path
);
875 path
+= 16 - vsllink_tms_data_len
;
876 num_states
-= 16 - vsllink_tms_data_len
;
877 vsllink_tms_data_len
= 0;
878 vsllink_tms_cmd_pos
= NULL
;
884 // Normal operation, don't need to append tms data
885 vsllink_tms_data_len
= num_states
& 7;
887 while (num_states
> 0)
889 if (num_states
> ((VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8))
891 i
= (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8;
897 tms_len
= (i
+ 7) >> 3;
898 vsllink_tap_ensure_space(0, tms_len
+ 2);
899 tms_cmd_pos
= vsllink_usb_out_buffer_idx
;
900 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| (tms_len
- 1);
902 vsllink_add_path(0, i
, path
+ path_idx
);
908 if (vsllink_tms_data_len
> 0)
910 if (tms_len
< (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1))
912 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[tms_cmd_pos
];
913 (*vsllink_tms_cmd_pos
)++;
917 vsllink_usb_out_buffer
[tms_cmd_pos
]--;
919 tms_len
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
920 vsllink_tap_ensure_space(0, 3);
921 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
922 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
923 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = tms_len
;
928 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
)
934 if ((8 - (tap_length
& 7)) < num_states
)
936 j
= 8 - (tap_length
& 7);
942 for (i
= 0; i
< j
; i
++)
944 if (path
[i
] == tap_state_transition(tap_get_state(), false))
946 vsllink_tap_append_step(0, 0);
948 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
950 vsllink_tap_append_step(1, 0);
954 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
957 tap_set_state(path
[i
]);
964 vsllink_tap_ensure_space(0, num_states
);
966 for (i
= 0; i
< num_states
; i
++)
968 if (path
[j
+ i
] == tap_state_transition(tap_get_state(), false))
970 vsllink_tap_append_step(0, 0);
972 else if (path
[j
+ i
] == tap_state_transition(tap_get_state(), true))
974 vsllink_tap_append_step(1, 0);
978 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
981 tap_set_state(path
[j
+ i
]);
985 tap_set_end_state(tap_get_state());
988 static void vsllink_stableclocks_normal(int num_cycles
, int tms
)
993 if (vsllink_tms_data_len
> 0)
995 // there are vsllink_tms_data_len more tms bits to be shifted
996 // so there are vsllink_tms_data_len + num_cycles tms bits in all
997 tms_len
= vsllink_tms_data_len
+ num_cycles
;
1000 // append '1' for tms
1001 tms_append_byte
= (u16
)((((1 << num_cycles
) - 1) << vsllink_tms_data_len
) & 0xFFFF);
1005 // append '0' for tms
1006 tms_append_byte
= 0;
1010 // merge into last tms shift
1013 // just add to vsllink_tms_data_len
1014 // same result if tun through
1015 //vsllink_tms_data_len += num_cycles;
1016 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= (u8
)(tms_append_byte
& 0xFF);
1018 else if (tms_len
== 8)
1020 // end last tms shift command
1021 // just reduce it, and append last tms byte
1022 (*vsllink_tms_cmd_pos
)--;
1023 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1025 else if (tms_len
< 16)
1027 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
1029 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1030 // there is enought tms length in the current tms shift command
1031 // increase the tms byte length by 1 and set the last byte to 0
1032 (*vsllink_tms_cmd_pos
)++;
1033 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1034 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (u8
)(tms_append_byte
>> 8);
1038 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1039 // not enough tms length in the current tms shift command
1040 // so a new command should be added
1041 // first decrease byte length of last tms shift command
1042 (*vsllink_tms_cmd_pos
)--;
1043 // append last tms byte and move the command pointer to the next empty position
1044 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1045 // add new command(3 bytes)
1046 vsllink_tap_ensure_space(0, 3);
1047 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1048 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1049 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (u8
)(tms_append_byte
>> 8);
1052 else if (tms_len
== 16)
1054 // end last tms shift command
1055 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1056 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (u8
)(tms_append_byte
>> 8);
1059 vsllink_tms_data_len
= tms_len
& 7;
1060 if (vsllink_tms_data_len
== 0)
1062 vsllink_tms_cmd_pos
= NULL
;
1068 // more shifts will be needed
1069 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (u8
)(tms_append_byte
& 0xFF);
1070 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (u8
)(tms_append_byte
>> 8);
1072 num_cycles
-= 16 - vsllink_tms_data_len
;
1073 vsllink_tms_data_len
= 0;
1074 vsllink_tms_cmd_pos
= NULL
;
1077 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1079 if (vsllink_tms_data_len
> 0)
1085 LOG_ERROR("There MUST be some bugs in the driver");
1091 // get number of bytes left to be sent
1092 tms_len
= num_cycles
>> 3;
1095 vsllink_tap_ensure_space(1, 5);
1096 // if tms_len > 0, vsllink_tms_data_len == 0
1097 // so just add new command
1098 // LSB of the command byte is the tms value when do the shifting
1101 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
| 1;
1105 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
;
1107 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 0) & 0xff;
1108 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 8) & 0xff;
1109 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 16) & 0xff;
1110 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 24) & 0xff;
1112 vsllink_usb_in_want_length
+= 1;
1113 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= NULL
;
1114 pending_scan_results_length
++;
1116 if (tms_len
> 0xFFFF)
1118 vsllink_tap_execute();
1123 vsllink_tms_data_len
= num_cycles
& 7;
1124 if (vsllink_tms_data_len
> 0)
1126 vsllink_tap_ensure_space(0, 3);
1127 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1128 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1131 // append '1' for tms
1132 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (1 << vsllink_tms_data_len
) - 1;
1136 // append '0' for tms
1137 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0x00;
1142 static void vsllink_stableclocks_dma(int num_cycles
, int tms
)
1148 if ((8 - (tap_length
& 7)) < num_cycles
)
1150 cur_cycles
= 8 - (tap_length
& 7);
1154 cur_cycles
= num_cycles
;
1156 for (i
= 0; i
< cur_cycles
; i
++)
1158 vsllink_tap_append_step(tms
, 0);
1160 num_cycles
-= cur_cycles
;
1163 while (num_cycles
> 0)
1165 if (num_cycles
> 8 * tap_buffer_size
)
1167 cur_cycles
= 8 * tap_buffer_size
;
1171 cur_cycles
= num_cycles
;
1174 vsllink_tap_ensure_space(0, cur_cycles
);
1176 for (i
= 0; i
< cur_cycles
; i
++)
1178 vsllink_tap_append_step(tms
, 0);
1181 num_cycles
-= cur_cycles
;
1185 static void vsllink_runtest(int num_cycles
)
1187 tap_state_t saved_end_state
= tap_get_end_state();
1189 if (tap_get_state() != TAP_IDLE
)
1191 // enter into IDLE state
1192 vsllink_end_state(TAP_IDLE
);
1193 vsllink_state_move();
1196 vsllink_stableclocks(num_cycles
, 0);
1200 vsllink_end_state(saved_end_state
);
1201 tap_set_state(TAP_IDLE
);
1202 if (tap_get_end_state() != TAP_IDLE
)
1204 vsllink_state_move();
1208 static void vsllink_scan_normal(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
1210 tap_state_t saved_end_state
;
1211 u8 bits_left
, tms_tmp
, tdi_len
;
1214 if (0 == scan_size
)
1219 tdi_len
= ((scan_size
+ 7) >> 3);
1220 if ((tdi_len
+ 7) > VSLLINK_BufferSize
)
1222 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1226 saved_end_state
= tap_get_end_state();
1228 /* Move to appropriate scan state */
1229 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1231 if (vsllink_tms_data_len
> 0)
1233 if (tap_get_state() == tap_get_end_state())
1235 // already in IRSHIFT or DRSHIFT state
1236 // merge tms data in the last tms shift command into next scan command
1237 if(*vsllink_tms_cmd_pos
< 1)
1239 LOG_ERROR("There MUST be some bugs in the driver");
1242 else if(*vsllink_tms_cmd_pos
< 2)
1244 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1245 vsllink_usb_out_buffer_idx
--;
1249 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1250 *vsllink_tms_cmd_pos
-= 2;
1253 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1254 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1255 // which is merged from the last tms shift command
1256 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1257 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1258 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1)>> 8) & 0xff;
1259 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = tms_tmp
;
1260 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[0] << (8 - vsllink_tms_data_len
);
1262 for (i
= 0; i
< tdi_len
; i
++)
1264 buffer
[i
] >>= 8 - vsllink_tms_data_len
;
1267 buffer
[i
] += buffer
[i
+ 1] << vsllink_tms_data_len
;
1271 vsllink_tap_append_scan_normal(scan_size
- vsllink_tms_data_len
, buffer
, command
, vsllink_tms_data_len
);
1272 scan_size
-= 8 - vsllink_tms_data_len
;
1273 vsllink_tms_data_len
= 0;
1277 vsllink_state_move();
1278 vsllink_tap_ensure_space(1, tdi_len
+ 5);
1280 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
;
1281 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 0) & 0xff;
1282 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 8) & 0xff;
1284 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 0);
1289 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1291 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1292 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1293 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1)>> 8) & 0xff;
1294 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1295 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1297 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 8);
1299 vsllink_end_state(saved_end_state
);
1301 bits_left
= scan_size
& 0x07;
1302 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1306 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << (bits_left
- 1);
1310 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << 7;
1313 if (tap_get_state() != tap_get_end_state())
1315 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1319 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1322 tap_set_state(tap_get_end_state());
1324 static void vsllink_scan_dma(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
1326 tap_state_t saved_end_state
;
1328 saved_end_state
= tap_get_end_state();
1330 /* Move to appropriate scan state */
1331 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1333 vsllink_state_move();
1334 vsllink_end_state(saved_end_state
);
1337 vsllink_tap_ensure_space(1, (scan_size
+ 7) & ~0x00000007);
1338 vsllink_tap_append_scan_dma(scan_size
, buffer
, command
);
1340 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1341 while (tap_length
% 8 != 0)
1344 vsllink_tap_append_step(0, 0);
1347 if (tap_get_state() != tap_get_end_state())
1349 vsllink_state_move();
1353 static void vsllink_reset(int trst
, int srst
)
1357 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
1359 /* Signals are active low */
1360 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORT
;
1361 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
1362 vsllink_usb_out_buffer
[2] = 0;
1365 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
1369 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
1372 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
1375 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result
);
1379 static void vsllink_simple_command(u8 command
)
1383 DEBUG_JTAG_IO("0x%02x", command
);
1385 vsllink_usb_out_buffer
[0] = command
;
1386 result
= vsllink_usb_write(vsllink_jtag_handle
, 1);
1390 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command
, result
);
1394 static int vsllink_register_commands(struct command_context_s
*cmd_ctx
)
1396 register_command(cmd_ctx
, NULL
, "vsllink_usb_vid", vsllink_handle_usb_vid_command
,
1397 COMMAND_CONFIG
, NULL
);
1398 register_command(cmd_ctx
, NULL
, "vsllink_usb_pid", vsllink_handle_usb_pid_command
,
1399 COMMAND_CONFIG
, NULL
);
1400 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command
,
1401 COMMAND_CONFIG
, NULL
);
1402 register_command(cmd_ctx
, NULL
, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command
,
1403 COMMAND_CONFIG
, NULL
);
1404 register_command(cmd_ctx
, NULL
, "vsllink_usb_interface", vsllink_handle_usb_interface_command
,
1405 COMMAND_CONFIG
, NULL
);
1406 register_command(cmd_ctx
, NULL
, "vsllink_mode", vsllink_handle_mode_command
,
1407 COMMAND_CONFIG
, NULL
);
1412 static int vsllink_handle_mode_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1415 LOG_ERROR("parameter error, should be one parameter for VID");
1419 if (!strcmp(args
[0], "normal"))
1421 vsllink_mode
= VSLLINK_MODE_NORMAL
;
1423 else if (!strcmp(args
[0], "dma"))
1425 vsllink_mode
= VSLLINK_MODE_DMA
;
1429 LOG_ERROR("invalid vsllink_mode: %s", args
[0]);
1436 static int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1440 LOG_ERROR("parameter error, should be one parameter for VID");
1444 vsllink_usb_vid
= strtol(args
[0], NULL
, 0);
1449 static int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1453 LOG_ERROR("parameter error, should be one parameter for PID");
1457 vsllink_usb_pid
= strtol(args
[0], NULL
, 0);
1462 static int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1466 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1470 vsllink_usb_bulkin
= strtol(args
[0], NULL
, 0) | 0x80;
1475 static int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1479 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1483 vsllink_usb_bulkout
= strtol(args
[0], NULL
, 0);
1488 static int vsllink_handle_usb_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1492 LOG_ERROR("parameter error, should be one parameter for interface number");
1496 vsllink_usb_interface
= strtol(args
[0], NULL
, 0);
1501 /***************************************************************************/
1502 /* VSLLink tap functions */
1504 static void vsllink_tap_init_normal(void)
1506 vsllink_usb_out_buffer_idx
= 0;
1507 vsllink_usb_in_want_length
= 0;
1508 pending_scan_results_length
= 0;
1510 static void vsllink_tap_init_dma(void)
1513 pending_scan_results_length
= 0;
1516 static void vsllink_tap_ensure_space_normal(int scans
, int length
)
1518 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1519 int available_bytes
= VSLLINK_BufferSize
- vsllink_usb_out_buffer_idx
;
1521 if (scans
> available_scans
|| length
> available_bytes
)
1523 vsllink_tap_execute();
1526 static void vsllink_tap_ensure_space_dma(int scans
, int length
)
1528 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1529 int available_bytes
= tap_buffer_size
* 8 - tap_length
;
1531 if (scans
> available_scans
|| length
> available_bytes
)
1533 vsllink_tap_execute();
1537 static void vsllink_tap_append_step(int tms
, int tdi
)
1540 int index
= tap_length
/ 8;
1542 if (index
< tap_buffer_size
)
1544 int bit_index
= tap_length
% 8;
1545 u8 bit
= 1 << bit_index
;
1549 tms_buffer
[index
] |= bit
;
1553 tms_buffer
[index
] &= ~bit
;
1558 tdi_buffer
[index
] |= bit
;
1562 tdi_buffer
[index
] &= ~bit
;
1569 LOG_ERROR("buffer overflow, tap_length=%d", tap_length
);
1573 static void vsllink_tap_append_scan_normal(int length
, u8
*buffer
, scan_command_t
*command
, int offset
)
1575 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1580 vsllink_usb_in_want_length
+= ((length
+ 7) >> 3) + 1;
1584 vsllink_usb_in_want_length
+= (length
+ 7) >> 3;
1586 pending_scan_result
->length
= length
;
1587 pending_scan_result
->offset
= offset
;
1588 pending_scan_result
->command
= command
;
1589 pending_scan_result
->buffer
= buffer
;
1591 for (i
= 0; i
< ((length
+ 7) >> 3); i
++)
1593 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[i
];
1596 pending_scan_results_length
++;
1598 static void vsllink_tap_append_scan_dma(int length
, u8
*buffer
, scan_command_t
*command
)
1600 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1603 pending_scan_result
->offset
= tap_length
;
1604 pending_scan_result
->length
= length
;
1605 pending_scan_result
->command
= command
;
1606 pending_scan_result
->buffer
= buffer
;
1608 for (i
= 0; i
< length
; i
++)
1610 vsllink_tap_append_step((i
< length
-1 ? 0 : 1), (buffer
[i
/8] >> (i
%8)) & 1);
1613 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 #ifdef _DEBUG_USB_COMMS_
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_