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 <jtag/interface.h>
30 #include <jtag/commands.h>
31 #include "usb_common.h"
33 //#define _VSLLINK_IN_DEBUG_MODE_
35 #define VSLLINK_MODE_NORMAL 0
36 #define VSLLINK_MODE_DMA 1
38 static uint16_t vsllink_usb_vid
;
39 static uint16_t vsllink_usb_pid
;
40 static uint8_t vsllink_usb_bulkout
;
41 static uint8_t vsllink_usb_bulkin
;
42 static uint8_t vsllink_usb_interface
;
43 static uint8_t vsllink_mode
= VSLLINK_MODE_NORMAL
;
44 static int VSLLINK_USB_TIMEOUT
= 10000;
46 static int VSLLINK_BufferSize
= 1024;
48 /* Global USB buffers */
49 static int vsllink_usb_out_buffer_idx
;
50 static int vsllink_usb_in_want_length
;
51 static uint8_t* vsllink_usb_in_buffer
= NULL
;
52 static uint8_t* vsllink_usb_out_buffer
= NULL
;
54 /* Constants for VSLLink command */
55 #define VSLLINK_CMD_CONN 0x80
56 #define VSLLINK_CMD_DISCONN 0x81
57 #define VSLLINK_CMD_SET_SPEED 0x82
58 #define VSLLINK_CMD_SET_PORT 0x90
59 #define VSLLINK_CMD_GET_PORT 0x91
60 #define VSLLINK_CMD_SET_PORTDIR 0x92
61 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
62 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
63 #define VSLLINK_CMD_HW_SWDCMD 0xA2
64 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
66 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
67 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
68 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
70 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
71 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
73 #define JTAG_PINMSK_SRST (1 << 0)
74 #define JTAG_PINMSK_TRST (1 << 1)
75 #define JTAG_PINMSK_USR1 (1 << 2)
76 #define JTAG_PINMSK_USR2 (1 << 3)
77 #define JTAG_PINMSK_TCK (1 << 4)
78 #define JTAG_PINMSK_TMS (1 << 5)
79 #define JTAG_PINMSK_TDI (1 << 6)
80 #define JTAG_PINMSK_TDO (1 << 7)
83 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
85 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
93 * SD->SD and SI->SI have to be caught in interface specific code
95 static uint8_t VSLLINK_tap_move
[6][6] =
97 /* TLR RTI SD PD SI PI */
98 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
99 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
100 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
101 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
102 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
103 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
106 struct insert_insignificant_operation
{
107 unsigned char insert_value
;
108 unsigned char insert_position
;
111 static struct insert_insignificant_operation VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[6][6] =
158 static uint8_t VSLLINK_BIT_MSK
[8] =
160 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
163 struct pending_scan_result
{
165 int length
; /* Number of bits to read */
166 struct scan_command
*command
; /* Corresponding scan command */
170 #define MAX_PENDING_SCAN_RESULTS 256
172 static int pending_scan_results_length
;
173 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
175 /* Queue command functions */
176 static void vsllink_end_state(tap_state_t state
);
177 static void vsllink_state_move_dma(void);
178 static void vsllink_state_move_normal(void);
179 static void (*vsllink_state_move
)(void);
180 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
);
181 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
);
182 static void (*vsllink_path_move
)(int num_states
, tap_state_t
*path
);
183 static void vsllink_runtest(int num_cycles
);
184 static void vsllink_stableclocks_dma(int num_cycles
, int tms
);
185 static void vsllink_stableclocks_normal(int num_cycles
, int tms
);
186 static void (*vsllink_stableclocks
)(int num_cycles
, int tms
);
187 static void vsllink_scan_dma(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
188 static void vsllink_scan_normal(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
189 static void (*vsllink_scan
)(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
190 static void vsllink_reset(int trst
, int srst
);
191 static void vsllink_simple_command(uint8_t command
);
192 static int vsllink_connect(void);
193 static int vsllink_disconnect(void);
195 /* VSLLink tap buffer functions */
196 static void vsllink_tap_append_step(int tms
, int tdi
);
197 static void vsllink_tap_init_dma(void);
198 static void vsllink_tap_init_normal(void);
199 static void (*vsllink_tap_init
)(void);
200 static int vsllink_tap_execute_dma(void);
201 static int vsllink_tap_execute_normal(void);
202 static int (*vsllink_tap_execute
)(void);
203 static void vsllink_tap_ensure_space_dma(int scans
, int length
);
204 static void vsllink_tap_ensure_space_normal(int scans
, int length
);
205 static void (*vsllink_tap_ensure_space
)(int scans
, int length
);
206 static void vsllink_tap_append_scan_dma(int length
, uint8_t *buffer
, struct scan_command
*command
);
207 static void vsllink_tap_append_scan_normal(int length
, uint8_t *buffer
, struct scan_command
*command
, int offset
);
209 /* VSLLink lowlevel functions */
211 struct usb_dev_handle
* usb_handle
;
214 static struct vsllink
*vsllink_usb_open(void);
215 static void vsllink_usb_close(struct vsllink
*vsllink
);
216 static int vsllink_usb_message(struct vsllink
*vsllink
, int out_length
, int in_length
);
217 static int vsllink_usb_write(struct vsllink
*vsllink
, int out_length
);
218 static int vsllink_usb_read(struct vsllink
*vsllink
);
220 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
221 static void vsllink_debug_buffer(uint8_t *buffer
, int length
);
224 static int vsllink_tms_data_len
= 0;
225 static uint8_t* vsllink_tms_cmd_pos
;
227 static int tap_length
= 0;
228 static int tap_buffer_size
= 0;
229 static uint8_t *tms_buffer
= NULL
;
230 static uint8_t *tdi_buffer
= NULL
;
231 static uint8_t *tdo_buffer
= NULL
;
234 static struct vsllink
* vsllink_handle
= NULL
;
236 static void reset_command_pointer(void)
238 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
240 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
241 vsllink_usb_out_buffer_idx
= 3;
249 static int vsllink_execute_queue(void)
251 struct jtag_command
*cmd
= jtag_command_queue
;
256 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
258 reset_command_pointer();
264 DEBUG_JTAG_IO("runtest %i cycles, end in %s", cmd
->cmd
.runtest
->num_cycles
, \
265 tap_state_name(cmd
->cmd
.runtest
->end_state
));
267 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
268 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
272 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd
->cmd
.statemove
->end_state
));
274 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
275 vsllink_state_move();
279 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
280 cmd
->cmd
.pathmove
->num_states
, \
281 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
283 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
287 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
289 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
290 if (cmd
->cmd
.scan
->ir_scan
)
292 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
296 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size
, tap_state_name(cmd
->cmd
.scan
->end_state
));
299 #ifdef _DEBUG_JTAG_IO_
300 vsllink_debug_buffer(buffer
, (scan_size
+ 7) >> 3);
303 type
= jtag_scan_type(cmd
->cmd
.scan
);
305 vsllink_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
, cmd
->cmd
.scan
);
309 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
311 vsllink_tap_execute();
313 if (cmd
->cmd
.reset
->trst
== 1)
315 tap_set_state(TAP_RESET
);
317 vsllink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
321 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
322 vsllink_tap_execute();
323 jtag_sleep(cmd
->cmd
.sleep
->us
);
326 case JTAG_STABLECLOCKS
:
327 DEBUG_JTAG_IO("add %d clocks", cmd
->cmd
.stableclocks
->num_cycles
);
328 switch (tap_get_state())
331 // tms should be '1' to stay in TAP_RESET mode
339 // in other mode, tms should be '0'
341 break; /* above stable states are OK */
343 LOG_ERROR("jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
344 tap_state_name(tap_get_state()));
347 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
351 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
357 return vsllink_tap_execute();
360 static int vsllink_speed(int speed
)
364 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_SPEED
;
365 vsllink_usb_out_buffer
[1] = (speed
>> 0) & 0xff;
366 vsllink_usb_out_buffer
[2] = (speed
>> 8) & 0xFF;
368 result
= vsllink_usb_write(vsllink_handle
, 3);
376 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
377 return ERROR_JTAG_DEVICE_ERROR
;
383 static int vsllink_khz(int khz
, int *jtag_speed
)
390 static int vsllink_speed_div(int jtag_speed
, int *khz
)
397 static int vsllink_init(void)
399 int check_cnt
, to_tmp
;
401 char version_str
[100];
403 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
404 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
405 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
407 LOG_ERROR("Not enough memory");
411 vsllink_handle
= vsllink_usb_open();
413 if (vsllink_handle
== 0)
415 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
416 return ERROR_JTAG_INIT_FAILED
;
418 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid
, vsllink_usb_pid
);
420 to_tmp
= VSLLINK_USB_TIMEOUT
;
421 VSLLINK_USB_TIMEOUT
= 100;
423 while (check_cnt
< 5)
425 vsllink_simple_command(0x00);
426 result
= vsllink_usb_read(vsllink_handle
);
430 vsllink_usb_in_buffer
[result
] = 0;
431 VSLLINK_BufferSize
= vsllink_usb_in_buffer
[0] + (vsllink_usb_in_buffer
[1] << 8);
432 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(version_str
));
433 LOG_INFO("%s", version_str
);
435 // free the pre-alloc memroy
436 free(vsllink_usb_in_buffer
);
437 free(vsllink_usb_out_buffer
);
438 vsllink_usb_in_buffer
= NULL
;
439 vsllink_usb_out_buffer
= NULL
;
442 vsllink_usb_in_buffer
= malloc(VSLLINK_BufferSize
);
443 vsllink_usb_out_buffer
= malloc(VSLLINK_BufferSize
);
444 if ((vsllink_usb_in_buffer
== NULL
) || (vsllink_usb_out_buffer
== NULL
))
446 LOG_ERROR("Not enough memory");
451 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize
);
453 // alloc memory for dma mode
454 if (vsllink_mode
== VSLLINK_MODE_DMA
)
456 tap_buffer_size
= (VSLLINK_BufferSize
- 3) / 2;
457 tms_buffer
= (uint8_t*)malloc(tap_buffer_size
);
458 tdi_buffer
= (uint8_t*)malloc(tap_buffer_size
);
459 tdo_buffer
= (uint8_t*)malloc(tap_buffer_size
);
460 if ((tms_buffer
== NULL
) || (tdi_buffer
== NULL
) || (tdo_buffer
== NULL
))
462 LOG_ERROR("Not enough memory");
468 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
473 // It's dangerout to proced
474 LOG_ERROR("VSLLink initial failed");
477 VSLLINK_USB_TIMEOUT
= to_tmp
;
479 // connect to vsllink
481 // initialize function pointers
482 if (vsllink_mode
== VSLLINK_MODE_NORMAL
)
485 vsllink_state_move
= vsllink_state_move_normal
;
486 vsllink_path_move
= vsllink_path_move_normal
;
487 vsllink_stableclocks
= vsllink_stableclocks_normal
;
488 vsllink_scan
= vsllink_scan_normal
;
490 vsllink_tap_init
= vsllink_tap_init_normal
;
491 vsllink_tap_execute
= vsllink_tap_execute_normal
;
492 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_normal
;
494 LOG_INFO("vsllink run in NORMAL mode");
499 vsllink_state_move
= vsllink_state_move_dma
;
500 vsllink_path_move
= vsllink_path_move_dma
;
501 vsllink_stableclocks
= vsllink_stableclocks_dma
;
502 vsllink_scan
= vsllink_scan_dma
;
504 vsllink_tap_init
= vsllink_tap_init_dma
;
505 vsllink_tap_execute
= vsllink_tap_execute_dma
;
506 vsllink_tap_ensure_space
= vsllink_tap_ensure_space_dma
;
508 LOG_INFO("vsllink run in DMA mode");
511 // Set SRST and TRST to output, Set USR1 and USR2 to input
512 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
513 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
514 vsllink_usb_out_buffer
[2] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
515 if (vsllink_usb_write(vsllink_handle
, 3) != 3)
517 LOG_ERROR("VSLLink USB send data error");
523 LOG_INFO("VSLLink JTAG Interface ready");
530 static int vsllink_quit(void)
532 if ((vsllink_usb_in_buffer
!= NULL
) && (vsllink_usb_out_buffer
!= NULL
))
534 // Set all pins to input
535 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
536 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
| JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
537 vsllink_usb_out_buffer
[2] = 0;
538 if (vsllink_usb_write(vsllink_handle
, 3) != 3)
540 LOG_ERROR("VSLLink USB send data error");
545 vsllink_disconnect();
546 vsllink_usb_close(vsllink_handle
);
547 vsllink_handle
= NULL
;
550 if (vsllink_usb_in_buffer
!= NULL
)
552 free(vsllink_usb_in_buffer
);
553 vsllink_usb_in_buffer
= NULL
;
555 if (vsllink_usb_out_buffer
!= NULL
)
557 free(vsllink_usb_out_buffer
);
558 vsllink_usb_out_buffer
= NULL
;
564 /***************************************************************************/
565 /* Queue command implementations */
566 static int vsllink_disconnect(void)
568 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
572 static int vsllink_connect(void)
574 char vsllink_str
[100];
576 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_CONN
;
577 vsllink_usb_out_buffer
[1] = vsllink_mode
;
578 vsllink_usb_message(vsllink_handle
, 2, 0);
579 if (vsllink_usb_read(vsllink_handle
) > 2)
581 strncpy(vsllink_str
, (char *)vsllink_usb_in_buffer
+ 2, sizeof(vsllink_str
));
582 LOG_INFO("%s", vsllink_str
);
588 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
589 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
590 static void vsllink_append_tms(void)
592 uint8_t tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
594 struct insert_insignificant_operation
*insert
= \
595 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
597 if (((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_DRPAUSE
) && (tap_get_state() != TAP_IRPAUSE
)) || \
598 (vsllink_tms_data_len
<= 0) || (vsllink_tms_data_len
>= 8) || \
599 (vsllink_tms_cmd_pos
== NULL
))
601 LOG_ERROR("There MUST be some bugs in the driver");
605 tms2
= (tms_scan
& VSLLINK_BIT_MSK
[insert
->insert_position
]) << \
606 vsllink_tms_data_len
;
607 if (insert
->insert_value
== 1)
609 tms2
|= VSLLINK_BIT_MSK
[8 - vsllink_tms_data_len
] << \
610 (vsllink_tms_data_len
+ insert
->insert_position
);
612 tms2
|= (tms_scan
>> insert
->insert_position
) << \
613 (8 + insert
->insert_position
);
615 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (tms2
>> 0) & 0xff;
616 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms2
>> 8) & 0xff;
618 vsllink_tms_data_len
= 0;
619 vsllink_tms_cmd_pos
= NULL
;
622 static void vsllink_end_state(tap_state_t state
)
624 if (tap_is_state_stable(state
))
626 tap_set_end_state(state
);
630 LOG_ERROR("BUG: %i is not a valid end state", state
);
635 /* Goes to the end state. */
636 static void vsllink_state_move_normal(void)
638 if (vsllink_tms_data_len
> 0)
640 vsllink_append_tms();
644 vsllink_tap_ensure_space(0, 2);
646 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
;
647 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
650 tap_set_state(tap_get_end_state());
652 static void vsllink_state_move_dma(void)
654 int i
, insert_length
= (tap_length
% 8) ? (8 - (tap_length
% 8)) : 0;
655 struct insert_insignificant_operation
*insert
= \
656 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
657 uint8_t tms_scan
= VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
659 if (tap_get_state() == TAP_RESET
)
661 vsllink_tap_ensure_space(0, 8);
663 for (i
= 0; i
< 8; i
++)
665 vsllink_tap_append_step(1, 0);
669 if (insert_length
> 0)
671 vsllink_tap_ensure_space(0, 16);
673 for (i
= 0; i
< insert
->insert_position
; i
++)
675 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
677 for (i
= 0; i
< insert_length
; i
++)
679 vsllink_tap_append_step(insert
->insert_value
, 0);
681 for (i
= insert
->insert_position
; i
< 8; i
++)
683 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
688 vsllink_tap_ensure_space(0, 8);
690 for (i
= 0; i
< 8; i
++)
692 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
696 tap_set_state(tap_get_end_state());
699 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
700 static void vsllink_add_path(int start
, int num
, tap_state_t
*path
)
704 for (i
= start
; i
< (start
+ num
); i
++)
710 vsllink_usb_out_buffer_idx
++;
712 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
715 if (path
[i
- start
] == tap_state_transition(tap_get_state(), true))
717 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= 1 << (i
& 7);
719 else if (path
[i
- start
] == tap_state_transition(tap_get_state(), false))
725 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
728 tap_set_state(path
[i
- start
]);
730 if ((i
> 0) && ((i
& 7) == 0))
732 vsllink_usb_out_buffer_idx
++;
733 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
736 tap_set_end_state(tap_get_state());
739 static void vsllink_path_move_normal(int num_states
, tap_state_t
*path
)
741 int i
, tms_len
, tms_cmd_pos
, path_idx
= 0;
743 if (vsllink_tms_data_len
> 0)
745 // there are vsllink_tms_data_len more tms bits to be shifted
746 // so there are vsllink_tms_data_len + num_states tms bits in all
747 tms_len
= vsllink_tms_data_len
+ num_states
;
750 // merge into last tms shift
753 // just append tms data to the last tms byte
754 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
756 else if (tms_len
== 8)
758 // end last tms shift command
759 (*vsllink_tms_cmd_pos
)--;
760 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
762 else if (tms_len
< 16)
764 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
766 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
767 // there is enought tms length in the current tms shift command
768 (*vsllink_tms_cmd_pos
)++;
769 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
773 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
774 // not enough tms length in the current tms shift command
775 // so a new command should be added
776 // first decrease byte length of last tms shift command
777 (*vsllink_tms_cmd_pos
)--;
778 // append tms data to the last tms byte
779 vsllink_add_path(vsllink_tms_data_len
, 8 - vsllink_tms_data_len
, path
);
780 path
+= 8 - vsllink_tms_data_len
;
781 // add new command(3 bytes)
782 vsllink_tap_ensure_space(0, 3);
783 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
784 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
785 vsllink_add_path(0, num_states
- (8 - vsllink_tms_data_len
), path
);
788 else if (tms_len
== 16)
790 // end last tms shift command
791 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
794 vsllink_tms_data_len
= (vsllink_tms_data_len
+ num_states
) & 7;
795 if (vsllink_tms_data_len
== 0)
797 vsllink_tms_cmd_pos
= NULL
;
803 vsllink_add_path(vsllink_tms_data_len
, 16 - vsllink_tms_data_len
, path
);
805 path
+= 16 - vsllink_tms_data_len
;
806 num_states
-= 16 - vsllink_tms_data_len
;
807 vsllink_tms_data_len
= 0;
808 vsllink_tms_cmd_pos
= NULL
;
814 // Normal operation, don't need to append tms data
815 vsllink_tms_data_len
= num_states
& 7;
817 while (num_states
> 0)
819 if (num_states
> ((VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8))
821 i
= (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8;
827 tms_len
= (i
+ 7) >> 3;
828 vsllink_tap_ensure_space(0, tms_len
+ 2);
829 tms_cmd_pos
= vsllink_usb_out_buffer_idx
;
830 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| (tms_len
- 1);
832 vsllink_add_path(0, i
, path
+ path_idx
);
838 if (vsllink_tms_data_len
> 0)
840 if (tms_len
< (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1))
842 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[tms_cmd_pos
];
843 (*vsllink_tms_cmd_pos
)++;
847 vsllink_usb_out_buffer
[tms_cmd_pos
]--;
849 tms_len
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
850 vsllink_tap_ensure_space(0, 3);
851 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
852 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
853 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = tms_len
;
858 static void vsllink_path_move_dma(int num_states
, tap_state_t
*path
)
864 if ((8 - (tap_length
& 7)) < num_states
)
866 j
= 8 - (tap_length
& 7);
872 for (i
= 0; i
< j
; i
++)
874 if (path
[i
] == tap_state_transition(tap_get_state(), false))
876 vsllink_tap_append_step(0, 0);
878 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
880 vsllink_tap_append_step(1, 0);
884 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
887 tap_set_state(path
[i
]);
894 vsllink_tap_ensure_space(0, num_states
);
896 for (i
= 0; i
< num_states
; i
++)
898 if (path
[j
+ i
] == tap_state_transition(tap_get_state(), false))
900 vsllink_tap_append_step(0, 0);
902 else if (path
[j
+ i
] == tap_state_transition(tap_get_state(), true))
904 vsllink_tap_append_step(1, 0);
908 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
911 tap_set_state(path
[j
+ i
]);
915 tap_set_end_state(tap_get_state());
918 static void vsllink_stableclocks_normal(int num_cycles
, int tms
)
921 uint16_t tms_append_byte
;
923 if (vsllink_tms_data_len
> 0)
925 // there are vsllink_tms_data_len more tms bits to be shifted
926 // so there are vsllink_tms_data_len + num_cycles tms bits in all
927 tms_len
= vsllink_tms_data_len
+ num_cycles
;
930 // append '1' for tms
931 tms_append_byte
= (uint16_t)((((1 << num_cycles
) - 1) << vsllink_tms_data_len
) & 0xFFFF);
935 // append '0' for tms
940 // merge into last tms shift
943 // just add to vsllink_tms_data_len
944 // same result if tun through
945 //vsllink_tms_data_len += num_cycles;
946 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= (uint8_t)(tms_append_byte
& 0xFF);
948 else if (tms_len
== 8)
950 // end last tms shift command
951 // just reduce it, and append last tms byte
952 (*vsllink_tms_cmd_pos
)--;
953 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
955 else if (tms_len
< 16)
957 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
959 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
960 // there is enought tms length in the current tms shift command
961 // increase the tms byte length by 1 and set the last byte to 0
962 (*vsllink_tms_cmd_pos
)++;
963 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
964 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (uint8_t)(tms_append_byte
>> 8);
968 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
969 // not enough tms length in the current tms shift command
970 // so a new command should be added
971 // first decrease byte length of last tms shift command
972 (*vsllink_tms_cmd_pos
)--;
973 // append last tms byte and move the command pointer to the next empty position
974 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
975 // add new command(3 bytes)
976 vsllink_tap_ensure_space(0, 3);
977 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
978 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
979 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (uint8_t)(tms_append_byte
>> 8);
982 else if (tms_len
== 16)
984 // end last tms shift command
985 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
986 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (uint8_t)(tms_append_byte
>> 8);
989 vsllink_tms_data_len
= tms_len
& 7;
990 if (vsllink_tms_data_len
== 0)
992 vsllink_tms_cmd_pos
= NULL
;
998 // more shifts will be needed
999 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= (uint8_t)(tms_append_byte
& 0xFF);
1000 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (uint8_t)(tms_append_byte
>> 8);
1002 num_cycles
-= 16 - vsllink_tms_data_len
;
1003 vsllink_tms_data_len
= 0;
1004 vsllink_tms_cmd_pos
= NULL
;
1007 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1009 if (vsllink_tms_data_len
> 0)
1015 LOG_ERROR("There MUST be some bugs in the driver");
1021 // get number of bytes left to be sent
1022 tms_len
= num_cycles
>> 3;
1025 vsllink_tap_ensure_space(1, 5);
1026 // if tms_len > 0, vsllink_tms_data_len == 0
1027 // so just add new command
1028 // LSB of the command byte is the tms value when do the shifting
1031 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
| 1;
1035 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
;
1037 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 0) & 0xff;
1038 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 8) & 0xff;
1039 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 16) & 0xff;
1040 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tms_len
>> 24) & 0xff;
1042 vsllink_usb_in_want_length
+= 1;
1043 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= NULL
;
1044 pending_scan_results_length
++;
1046 if (tms_len
> 0xFFFF)
1048 vsllink_tap_execute();
1053 vsllink_tms_data_len
= num_cycles
& 7;
1054 if (vsllink_tms_data_len
> 0)
1056 vsllink_tap_ensure_space(0, 3);
1057 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1058 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSBYTE
| 1;
1061 // append '1' for tms
1062 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (1 << vsllink_tms_data_len
) - 1;
1066 // append '0' for tms
1067 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0x00;
1072 static void vsllink_stableclocks_dma(int num_cycles
, int tms
)
1078 if ((8 - (tap_length
& 7)) < num_cycles
)
1080 cur_cycles
= 8 - (tap_length
& 7);
1084 cur_cycles
= num_cycles
;
1086 for (i
= 0; i
< cur_cycles
; i
++)
1088 vsllink_tap_append_step(tms
, 0);
1090 num_cycles
-= cur_cycles
;
1093 while (num_cycles
> 0)
1095 if (num_cycles
> 8 * tap_buffer_size
)
1097 cur_cycles
= 8 * tap_buffer_size
;
1101 cur_cycles
= num_cycles
;
1104 vsllink_tap_ensure_space(0, cur_cycles
);
1106 for (i
= 0; i
< cur_cycles
; i
++)
1108 vsllink_tap_append_step(tms
, 0);
1111 num_cycles
-= cur_cycles
;
1115 static void vsllink_runtest(int num_cycles
)
1117 tap_state_t saved_end_state
= tap_get_end_state();
1119 if (tap_get_state() != TAP_IDLE
)
1121 // enter into IDLE state
1122 vsllink_end_state(TAP_IDLE
);
1123 vsllink_state_move();
1126 vsllink_stableclocks(num_cycles
, 0);
1130 vsllink_end_state(saved_end_state
);
1131 tap_set_state(TAP_IDLE
);
1132 if (tap_get_end_state() != TAP_IDLE
)
1134 vsllink_state_move();
1138 static void vsllink_scan_normal(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
)
1140 tap_state_t saved_end_state
;
1141 uint8_t bits_left
, tms_tmp
, tdi_len
;
1149 tdi_len
= ((scan_size
+ 7) >> 3);
1150 if ((tdi_len
+ 7) > VSLLINK_BufferSize
)
1152 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1156 saved_end_state
= tap_get_end_state();
1158 /* Move to appropriate scan state */
1159 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1161 if (vsllink_tms_data_len
> 0)
1163 if (tap_get_state() == tap_get_end_state())
1165 // already in IRSHIFT or DRSHIFT state
1166 // merge tms data in the last tms shift command into next scan command
1167 if (*vsllink_tms_cmd_pos
< 1)
1169 LOG_ERROR("There MUST be some bugs in the driver");
1172 else if (*vsllink_tms_cmd_pos
< 2)
1174 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1175 vsllink_usb_out_buffer_idx
--;
1179 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1180 *vsllink_tms_cmd_pos
-= 2;
1183 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1184 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1185 // which is merged from the last tms shift command
1186 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1187 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1188 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 8) & 0xff;
1189 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = tms_tmp
;
1190 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[0] << (8 - vsllink_tms_data_len
);
1192 for (i
= 0; i
< tdi_len
; i
++)
1194 buffer
[i
] >>= 8 - vsllink_tms_data_len
;
1197 buffer
[i
] += buffer
[i
+ 1] << vsllink_tms_data_len
;
1201 vsllink_tap_append_scan_normal(scan_size
- vsllink_tms_data_len
, buffer
, command
, vsllink_tms_data_len
);
1202 scan_size
-= 8 - vsllink_tms_data_len
;
1203 vsllink_tms_data_len
= 0;
1207 vsllink_state_move();
1208 vsllink_tap_ensure_space(1, tdi_len
+ 5);
1210 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
;
1211 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 0) & 0xff;
1212 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = (tdi_len
>> 8) & 0xff;
1214 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 0);
1219 vsllink_tap_ensure_space(1, tdi_len
+ 7);
1221 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_SCAN
| 1;
1222 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1) >> 0) & 0xff;
1223 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = ((tdi_len
+ 1)>> 8) & 0xff;
1224 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1225 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1227 vsllink_tap_append_scan_normal(scan_size
, buffer
, command
, 8);
1229 vsllink_end_state(saved_end_state
);
1231 bits_left
= scan_size
& 0x07;
1232 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1236 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << (bits_left
- 1);
1240 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << 7;
1243 if (tap_get_state() != tap_get_end_state())
1245 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1249 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1252 tap_set_state(tap_get_end_state());
1254 static void vsllink_scan_dma(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
)
1256 tap_state_t saved_end_state
;
1258 saved_end_state
= tap_get_end_state();
1260 /* Move to appropriate scan state */
1261 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1263 vsllink_state_move();
1264 vsllink_end_state(saved_end_state
);
1267 vsllink_tap_append_scan_dma(scan_size
, buffer
, command
);
1269 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1270 while (tap_length
% 8 != 0)
1273 vsllink_tap_append_step(0, 0);
1276 if (tap_get_state() != tap_get_end_state())
1278 vsllink_state_move();
1282 static void vsllink_reset(int trst
, int srst
)
1286 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
1288 /* Signals are active low */
1289 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORT
;
1290 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
1291 vsllink_usb_out_buffer
[2] = 0;
1294 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
1298 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
1301 result
= vsllink_usb_write(vsllink_handle
, 3);
1304 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result
);
1308 static void vsllink_simple_command(uint8_t command
)
1312 DEBUG_JTAG_IO("0x%02x", command
);
1314 vsllink_usb_out_buffer
[0] = command
;
1315 result
= vsllink_usb_write(vsllink_handle
, 1);
1319 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command
, result
);
1323 COMMAND_HANDLER(vsllink_handle_mode_command
)
1325 if (CMD_ARGC
!= 1) {
1326 LOG_ERROR("parameter error, should be one parameter for VID");
1330 if (!strcmp(CMD_ARGV
[0], "normal"))
1332 vsllink_mode
= VSLLINK_MODE_NORMAL
;
1334 else if (!strcmp(CMD_ARGV
[0], "dma"))
1336 vsllink_mode
= VSLLINK_MODE_DMA
;
1340 LOG_ERROR("invalid vsllink_mode: %s", CMD_ARGV
[0]);
1347 COMMAND_HANDLER(vsllink_handle_usb_vid_command
)
1351 LOG_ERROR("parameter error, should be one parameter for VID");
1355 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], vsllink_usb_vid
);
1359 COMMAND_HANDLER(vsllink_handle_usb_pid_command
)
1363 LOG_ERROR("parameter error, should be one parameter for PID");
1366 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], vsllink_usb_pid
);
1370 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command
)
1374 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1378 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], vsllink_usb_bulkin
);
1380 vsllink_usb_bulkin
|= 0x80;
1385 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command
)
1389 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1393 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], vsllink_usb_bulkout
);
1395 vsllink_usb_bulkout
&= ~0x80;
1400 COMMAND_HANDLER(vsllink_handle_usb_interface_command
)
1404 LOG_ERROR("parameter error, should be one parameter for interface number");
1408 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], vsllink_usb_interface
);
1412 /***************************************************************************/
1413 /* VSLLink tap functions */
1415 static void vsllink_tap_init_normal(void)
1417 vsllink_usb_out_buffer_idx
= 0;
1418 vsllink_usb_in_want_length
= 0;
1419 pending_scan_results_length
= 0;
1421 static void vsllink_tap_init_dma(void)
1424 pending_scan_results_length
= 0;
1427 static void vsllink_tap_ensure_space_normal(int scans
, int length
)
1429 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1430 int available_bytes
= VSLLINK_BufferSize
- vsllink_usb_out_buffer_idx
;
1432 if (scans
> available_scans
|| length
> available_bytes
)
1434 vsllink_tap_execute();
1437 static void vsllink_tap_ensure_space_dma(int scans
, int length
)
1439 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1440 int available_bytes
= tap_buffer_size
* 8 - tap_length
;
1442 if (scans
> available_scans
|| length
> available_bytes
)
1444 vsllink_tap_execute();
1448 static void vsllink_tap_append_step(int tms
, int tdi
)
1451 int index
= tap_length
/ 8;
1453 if (index
< tap_buffer_size
)
1455 int bit_index
= tap_length
% 8;
1456 uint8_t bit
= 1 << bit_index
;
1460 tms_buffer
[index
] |= bit
;
1464 tms_buffer
[index
] &= ~bit
;
1469 tdi_buffer
[index
] |= bit
;
1473 tdi_buffer
[index
] &= ~bit
;
1480 LOG_ERROR("buffer overflow, tap_length=%d", tap_length
);
1484 static void vsllink_tap_append_scan_normal(int length
, uint8_t *buffer
, struct scan_command
*command
, int offset
)
1486 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1491 vsllink_usb_in_want_length
+= ((length
+ 7) >> 3) + 1;
1495 vsllink_usb_in_want_length
+= (length
+ 7) >> 3;
1497 pending_scan_result
->length
= length
;
1498 pending_scan_result
->offset
= offset
;
1499 pending_scan_result
->command
= command
;
1500 pending_scan_result
->buffer
= buffer
;
1502 for (i
= 0; i
< ((length
+ 7) >> 3); i
++)
1504 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[i
];
1507 pending_scan_results_length
++;
1509 static void vsllink_tap_append_scan_dma(int length
, uint8_t *buffer
, struct scan_command
*command
)
1511 struct pending_scan_result
*pending_scan_result
;
1512 int len_tmp
, len_all
, i
;
1515 while (len_all
< length
)
1517 if ((length
- len_all
) > tap_buffer_size
* 8)
1519 len_tmp
= tap_buffer_size
* 8;
1523 len_tmp
= length
- len_all
;
1526 vsllink_tap_ensure_space(1, (len_tmp
+ 7) & ~7);
1528 pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1529 pending_scan_result
->offset
= tap_length
;
1530 pending_scan_result
->length
= len_tmp
;
1531 pending_scan_result
->command
= command
;
1532 pending_scan_result
->buffer
= buffer
+ len_all
/ 8;
1534 for (i
= 0; i
< len_tmp
; i
++)
1536 vsllink_tap_append_step(((len_all
+ i
) < length
-1 ? 0 : 1), (buffer
[(len_all
+ i
)/8] >> ((len_all
+ i
)%8)) & 1);
1539 pending_scan_results_length
++;
1544 /* Pad and send a tap sequence to the device, and receive the answer.
1545 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1546 static int vsllink_tap_execute_normal(void)
1552 if (vsllink_tms_data_len
> 0)
1554 if ((tap_get_state() != TAP_RESET
) && (tap_get_state() != TAP_IDLE
) && (tap_get_state() != TAP_IRPAUSE
) && (tap_get_state() != TAP_DRPAUSE
))
1556 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1559 if (vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] & (1 << (vsllink_tms_data_len
- 1)))
1561 // last tms bit is '1'
1562 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] |= 0xFF << vsllink_tms_data_len
;
1563 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0xFF;
1564 vsllink_tms_data_len
= 0;
1568 // last tms bit is '0'
1569 vsllink_usb_out_buffer_idx
++;
1570 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1571 vsllink_tms_data_len
= 0;
1575 if (vsllink_usb_out_buffer_idx
> 3)
1577 if (vsllink_usb_out_buffer
[0] == VSLLINK_CMD_HW_JTAGSEQCMD
)
1579 vsllink_usb_out_buffer
[1] = (vsllink_usb_out_buffer_idx
>> 0) & 0xff;
1580 vsllink_usb_out_buffer
[2] = (vsllink_usb_out_buffer_idx
>> 8) & 0xff;
1583 result
= vsllink_usb_message(vsllink_handle
, vsllink_usb_out_buffer_idx
, vsllink_usb_in_want_length
);
1585 if (result
== vsllink_usb_in_want_length
)
1587 for (i
= 0; i
< pending_scan_results_length
; i
++)
1589 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1590 uint8_t *buffer
= pending_scan_result
->buffer
;
1591 int length
= pending_scan_result
->length
;
1592 int offset
= pending_scan_result
->offset
;
1593 struct scan_command
*command
= pending_scan_result
->command
;
1597 // IRSHIFT or DRSHIFT
1598 buf_set_buf(vsllink_usb_in_buffer
, first
* 8 + offset
, buffer
, 0, length
);
1599 first
+= (length
+ offset
+ 7) >> 3;
1601 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length
);
1602 #ifdef _DEBUG_JTAG_IO_
1603 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1606 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1609 return ERROR_JTAG_QUEUE_FAILED
;
1612 free(pending_scan_result
->buffer
);
1613 pending_scan_result
->buffer
= NULL
;
1623 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, vsllink_usb_in_want_length
);
1624 return ERROR_JTAG_QUEUE_FAILED
;
1629 reset_command_pointer();
1633 static int vsllink_tap_execute_dma(void)
1641 /* Pad last byte so that tap_length is divisible by 8 */
1642 while (tap_length
% 8 != 0)
1644 /* More of the last TMS value keeps us in the same state,
1645 * analogous to free-running JTAG interfaces. */
1646 vsllink_tap_append_step(last_tms
, 0);
1648 byte_length
= tap_length
/ 8;
1650 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGRAWCMD
;
1651 vsllink_usb_out_buffer
[1] = ((byte_length
* 2 + 3) >> 0) & 0xff; // package size
1652 vsllink_usb_out_buffer
[2] = ((byte_length
* 2 + 3) >> 8) & 0xff;
1654 memcpy(&vsllink_usb_out_buffer
[3], tdi_buffer
, byte_length
);
1655 memcpy(&vsllink_usb_out_buffer
[3 + byte_length
], tms_buffer
, byte_length
);
1657 result
= vsllink_usb_message(vsllink_handle
, 3 + 2 * byte_length
, byte_length
);
1658 if (result
== byte_length
)
1660 for (i
= 0; i
< pending_scan_results_length
; i
++)
1662 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1663 uint8_t *buffer
= pending_scan_result
->buffer
;
1664 int length
= pending_scan_result
->length
;
1665 int first
= pending_scan_result
->offset
;
1667 struct scan_command
*command
= pending_scan_result
->command
;
1668 buf_set_buf(vsllink_usb_in_buffer
, first
, buffer
, 0, length
);
1670 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length
, first
);
1671 #ifdef _DEBUG_JTAG_IO_
1672 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1675 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1678 return ERROR_JTAG_QUEUE_FAILED
;
1681 if (pending_scan_result
->buffer
!= NULL
)
1683 free(pending_scan_result
->buffer
);
1689 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, byte_length
);
1690 return ERROR_JTAG_QUEUE_FAILED
;
1699 /*****************************************************************************/
1700 /* VSLLink USB low-level functions */
1702 static struct vsllink
* vsllink_usb_open(void)
1706 const uint16_t vids
[] = { vsllink_usb_vid
, 0 };
1707 const uint16_t pids
[] = { vsllink_usb_pid
, 0 };
1708 struct usb_dev_handle
*dev
;
1709 if (jtag_usb_open(vids
, pids
, &dev
) != ERROR_OK
)
1712 /* usb_set_configuration required under win32 */
1713 struct usb_device
*udev
= usb_device(dev
);
1714 int ret
= usb_set_configuration(dev
, udev
->config
[0].bConfigurationValue
);
1717 LOG_ERROR("fail to set configuration to %d (error %d)."
1718 "Not enough permissions for the device?",
1719 udev
->config
[0].bConfigurationValue
, ret
);
1722 ret
= usb_claim_interface(dev
, vsllink_usb_interface
);
1725 LOG_ERROR("fail to claim interface %d, %d returned",
1726 vsllink_usb_interface
, ret
);
1731 * This makes problems under Mac OS X. And is not needed
1732 * under Windows. Hopefully this will not break a linux build
1734 usb_set_altinterface(dev
, 0);
1737 struct vsllink
*result
= malloc(sizeof(struct vsllink
));
1738 result
->usb_handle
= dev
;
1742 static void vsllink_usb_close(struct vsllink
*vsllink
)
1746 ret
= usb_release_interface(vsllink
->usb_handle
, vsllink_usb_interface
);
1749 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface
, ret
);
1753 ret
= usb_close(vsllink
->usb_handle
);
1756 LOG_ERROR("fail to close usb, %d returned", ret
);
1763 /* Send a message and receive the reply. */
1764 static int vsllink_usb_message(struct vsllink
*vsllink
, int out_length
, int in_length
)
1768 result
= vsllink_usb_write(vsllink
, out_length
);
1769 if (result
== out_length
)
1773 result
= vsllink_usb_read(vsllink
);
1774 if (result
== in_length
)
1780 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length
, result
);
1788 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length
, result
);
1793 /* Write data from out_buffer to USB. */
1794 static int vsllink_usb_write(struct vsllink
*vsllink
, int out_length
)
1798 if (out_length
> VSLLINK_BufferSize
)
1800 LOG_ERROR("vsllink_write illegal out_length=%d (max=%d)", out_length
, VSLLINK_BufferSize
);
1804 result
= usb_bulk_write(vsllink
->usb_handle
, vsllink_usb_bulkout
, \
1805 (char *)vsllink_usb_out_buffer
, out_length
, VSLLINK_USB_TIMEOUT
);
1807 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length
, result
);
1809 #ifdef _DEBUG_USB_COMMS_
1810 LOG_DEBUG("USB out:");
1811 vsllink_debug_buffer(vsllink_usb_out_buffer
, out_length
);
1814 #ifdef _VSLLINK_IN_DEBUG_MODE_
1821 /* Read data from USB into in_buffer. */
1822 static int vsllink_usb_read(struct vsllink
*vsllink
)
1824 int result
= usb_bulk_read(vsllink
->usb_handle
, vsllink_usb_bulkin
, \
1825 (char *)vsllink_usb_in_buffer
, VSLLINK_BufferSize
, VSLLINK_USB_TIMEOUT
);
1827 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result
);
1829 #ifdef _DEBUG_USB_COMMS_
1830 LOG_DEBUG("USB in:");
1831 vsllink_debug_buffer(vsllink_usb_in_buffer
, result
);
1836 #define BYTES_PER_LINE 16
1838 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1839 static void vsllink_debug_buffer(uint8_t *buffer
, int length
)
1846 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
)
1848 snprintf(line
, 5, "%04x", i
);
1849 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++)
1851 snprintf(s
, 4, " %02x", buffer
[j
]);
1854 LOG_DEBUG("%s", line
);
1857 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_
1859 static const struct command_registration vsllink_command_handlers
[] = {
1861 .name
= "vsllink_usb_vid",
1862 .handler
= &vsllink_handle_usb_vid_command
,
1863 .mode
= COMMAND_CONFIG
,
1866 .name
= "vsllink_usb_pid",
1867 .handler
= &vsllink_handle_usb_pid_command
,
1868 .mode
= COMMAND_CONFIG
,
1871 .name
= "vsllink_usb_bulkin",
1872 .handler
= &vsllink_handle_usb_bulkin_command
,
1873 .mode
= COMMAND_CONFIG
,
1876 .name
= "vsllink_usb_bulkout",
1877 .handler
= &vsllink_handle_usb_bulkout_command
,
1878 .mode
= COMMAND_CONFIG
,
1881 .name
= "vsllink_usb_interface",
1882 .handler
= &vsllink_handle_usb_interface_command
,
1883 .mode
= COMMAND_CONFIG
,
1886 .name
= "vsllink_mode",
1887 .handler
= &vsllink_handle_mode_command
,
1888 .mode
= COMMAND_CONFIG
,
1890 COMMAND_REGISTRATION_DONE
1893 struct jtag_interface vsllink_interface
= {
1895 .commands
= vsllink_command_handlers
,
1897 .init
= vsllink_init
,
1898 .quit
= vsllink_quit
,
1900 .speed
= vsllink_speed
,
1901 .speed_div
= vsllink_speed_div
,
1902 .execute_queue
= vsllink_execute_queue
,