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 ...)
57 #define VSLLINK_USB_TIMEOUT 5000
59 static int VSLLINK_BufferSize = 1024;
61 /* Global USB buffers */
62 static int vsllink_usb_out_buffer_idx;
63 static int vsllink_usb_in_want_length;
64 static u8* vsllink_usb_in_buffer = NULL;
65 static u8* vsllink_usb_out_buffer = NULL;
67 /* Constants for VSLLink command */
68 #define VSLLINK_CMD_CONN 0x80
69 #define VSLLINK_CMD_DISCONN 0x81
70 #define VSLLINK_CMD_SET_SPEED 0x82
71 #define VSLLINK_CMD_SET_PORT 0x90
72 #define VSLLINK_CMD_GET_PORT 0x91
73 #define VSLLINK_CMD_SET_PORTDIR 0x92
74 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
75 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
76 #define VSLLINK_CMD_HW_SWDCMD 0xA2
78 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
79 #define VSLLINK_CMDJTAGSEQ_TMS0BYTE 0x40
80 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
82 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
83 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
85 #define JTAG_PINMSK_SRST (1 << 0)
86 #define JTAG_PINMSK_TRST (1 << 1)
87 #define JTAG_PINMSK_USR1 (1 << 2)
88 #define JTAG_PINMSK_USR2 (1 << 3)
89 #define JTAG_PINMSK_TCK (1 << 4)
90 #define JTAG_PINMSK_TMS (1 << 5)
91 #define JTAG_PINMSK_TDI (1 << 6)
92 #define JTAG_PINMSK_TDO (1 << 7)
95 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_map[from]][tap_move_map[to]]
97 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
105 * SD->SD and SI->SI have to be caught in interface specific code
107 u8 VSLLINK_tap_move[6][6] =
109 /* TLR RTI SD PD SI PI */
110 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
111 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
112 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
113 {0xff, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
114 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
115 {0xff, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
118 typedef struct insert_insignificant_operation
120 unsigned char insert_value;
121 unsigned char insert_position;
122 }insert_insignificant_operation_t;
124 insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
171 u8 VSLLINK_BIT_MSK[8] =
173 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
178 int length; /* Number of bits to read */
180 scan_command_t *command; /* Corresponding scan command */
182 } pending_scan_result_t;
184 #define MAX_PENDING_SCAN_RESULTS 256
186 static int pending_scan_results_length;
187 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
189 /* External interface functions */
190 int vsllink_execute_queue(void);
191 int vsllink_speed(int speed);
192 int vsllink_khz(int khz, int *jtag_speed);
193 int vsllink_speed_div(int jtag_speed, int *khz);
194 int vsllink_register_commands(struct command_context_s *cmd_ctx);
195 int vsllink_init(void);
196 int vsllink_quit(void);
198 /* CLI command handler functions */
199 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
200 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
201 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
202 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
204 /* Queue command functions */
205 void vsllink_end_state(enum tap_state state);
206 void vsllink_state_move(void);
207 void vsllink_path_move(int num_states, enum tap_state *path);
208 void vsllink_runtest(int num_cycles);
209 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
210 void vsllink_reset(int trst, int srst);
211 void vsllink_simple_command(u8 command);
213 /* VSLLink tap buffer functions */
214 void vsllink_tap_init(void);
215 int vsllink_tap_execute(void);
216 void vsllink_tap_ensure_space(int scans, int bytes);
217 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
219 /* VSLLink lowlevel functions */
220 typedef struct vsllink_jtag
222 struct usb_dev_handle* usb_handle;
225 vsllink_jtag_t *vsllink_usb_open(void);
226 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
227 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
228 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
229 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
231 void vsllink_debug_buffer(u8 *buffer, int length);
233 static int vsllink_tms_data_len = 0;
234 static u8* vsllink_tms_cmd_pos;
236 vsllink_jtag_t* vsllink_jtag_handle;
238 /***************************************************************************/
239 /* External interface implementation */
241 jtag_interface_t vsllink_interface =
244 .execute_queue = vsllink_execute_queue,
245 .speed = vsllink_speed,
247 .speed_div = vsllink_speed_div,
248 .register_commands = vsllink_register_commands,
249 .init = vsllink_init,
253 int vsllink_execute_queue(void)
255 jtag_command_t *cmd = jtag_command_queue;
260 DEBUG_JTAG_IO("--------------------------------------------------------------------------------");
262 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
263 vsllink_usb_out_buffer_idx = 3;
269 DEBUG_JTAG_IO("end_state: %s", jtag_state_name(cmd->cmd.end_state->end_state));
271 if (cmd->cmd.end_state->end_state != -1)
273 vsllink_end_state(cmd->cmd.end_state->end_state);
278 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
279 jtag_state_name(cmd->cmd.runtest->end_state));
281 if (cmd->cmd.runtest->end_state != -1)
283 vsllink_end_state(cmd->cmd.runtest->end_state);
285 vsllink_runtest(cmd->cmd.runtest->num_cycles);
289 DEBUG_JTAG_IO("statemove end in %s", jtag_state_name(cmd->cmd.statemove->end_state));
291 if (cmd->cmd.statemove->end_state != -1)
293 vsllink_end_state(cmd->cmd.statemove->end_state);
295 vsllink_state_move();
299 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
300 cmd->cmd.pathmove->num_states, \
301 jtag_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
303 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
307 if (cmd->cmd.scan->end_state != -1)
309 vsllink_end_state(cmd->cmd.scan->end_state);
312 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
313 if (cmd->cmd.scan->ir_scan)
315 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, jtag_state_name(cmd->cmd.scan->end_state));
319 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, jtag_state_name(cmd->cmd.scan->end_state));
322 #ifdef _DEBUG_JTAG_IO_
323 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
326 type = jtag_scan_type(cmd->cmd.scan);
328 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
332 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
334 vsllink_tap_execute();
336 if (cmd->cmd.reset->trst == 1)
338 cur_state = TAP_RESET;
340 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
342 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
343 vsllink_usb_out_buffer_idx = 3;
347 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
348 vsllink_tap_execute();
349 jtag_sleep(cmd->cmd.sleep->us);
353 LOG_ERROR("BUG: unknown JTAG command type encountered");
359 return vsllink_tap_execute();
362 int vsllink_speed(int speed)
366 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
367 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
368 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
370 result = vsllink_usb_write(vsllink_jtag_handle, 3);
378 LOG_ERROR("VSLLink setting speed failed (%d)", result);
379 return ERROR_JTAG_DEVICE_ERROR;
385 int vsllink_khz(int khz, int *jtag_speed)
392 int vsllink_speed_div(int jtag_speed, int *khz)
399 int vsllink_register_commands(struct command_context_s *cmd_ctx)
401 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
402 COMMAND_CONFIG, NULL);
403 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
404 COMMAND_CONFIG, NULL);
405 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
406 COMMAND_CONFIG, NULL);
407 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
408 COMMAND_CONFIG, NULL);
413 int vsllink_init(void)
417 char version_str[100];
419 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
420 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
421 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
423 LOG_ERROR("Not enough memory");
427 vsllink_jtag_handle = vsllink_usb_open();
429 if (vsllink_jtag_handle == 0)
431 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
432 return ERROR_JTAG_INIT_FAILED;
436 while (check_cnt < 3)
438 vsllink_simple_command(VSLLINK_CMD_CONN);
439 result = vsllink_usb_read(vsllink_jtag_handle);
443 vsllink_usb_in_buffer[result] = 0;
444 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
445 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
446 LOG_INFO(version_str);
448 // free the pre-alloc memroy
449 free(vsllink_usb_in_buffer);
450 free(vsllink_usb_out_buffer);
451 vsllink_usb_in_buffer = NULL;
452 vsllink_usb_out_buffer = NULL;
455 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
456 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
457 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
459 LOG_ERROR("Not enough memory");
464 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
468 vsllink_simple_command(VSLLINK_CMD_DISCONN);
475 // It's dangerout to proced
476 LOG_ERROR("VSLLink initial failed");
480 // Set SRST and TRST to output, Set USR1 and USR2 to input
481 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
482 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
483 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
484 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
486 LOG_ERROR("VSLLink USB send data error");
492 LOG_INFO("VSLLink JTAG Interface ready");
499 int vsllink_quit(void)
501 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
503 // Set all pins to input
504 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
505 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
506 vsllink_usb_out_buffer[2] = 0;
507 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
509 LOG_ERROR("VSLLink USB send data error");
514 vsllink_simple_command(VSLLINK_CMD_DISCONN);
515 vsllink_usb_close(vsllink_jtag_handle);
518 if (vsllink_usb_in_buffer != NULL)
520 free(vsllink_usb_in_buffer);
522 if (vsllink_usb_out_buffer != NULL)
524 free(vsllink_usb_out_buffer);
529 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
530 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
531 void vsllink_append_tms(void)
533 u8 tms_scan = VSLLINK_TAP_MOVE(cur_state, end_state);
536 if (((cur_state != TAP_RESET) && (cur_state != TAP_IDLE) && (cur_state != TAP_DRPAUSE) && (cur_state != TAP_IRPAUSE)) || \
537 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
538 (vsllink_tms_cmd_pos == NULL))
540 LOG_ERROR("There MUST be some bugs in the driver");
544 tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position]) << \
545 vsllink_tms_data_len;
546 if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_value == 1)
548 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
549 (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position);
551 tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position) << \
552 (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position);
554 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
555 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
557 vsllink_tms_data_len = 0;
558 vsllink_tms_cmd_pos = NULL;
561 /***************************************************************************/
562 /* Queue command implementations */
564 void vsllink_end_state(enum tap_state state)
566 if (tap_move_map[state] != -1)
572 LOG_ERROR("BUG: %i is not a valid end state", state);
577 /* Goes to the end state. */
578 void vsllink_state_move(void)
580 if (vsllink_tms_data_len > 0)
582 vsllink_append_tms();
586 vsllink_tap_ensure_space(0, 2);
588 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
589 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
592 cur_state = end_state;
595 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
596 void vsllink_add_path(int start, int num, enum tap_state *path)
600 for (i = start; i < (start + num); i++)
606 vsllink_usb_out_buffer_idx++;
608 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
611 if (path[i - start] == tap_transitions[cur_state].high)
613 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
615 else if (path[i - start] == tap_transitions[cur_state].low)
621 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(path[i]));
624 cur_state = path[i - start];
626 if ((i > 0) && ((i & 7) == 0))
628 vsllink_usb_out_buffer_idx++;
629 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
632 end_state = cur_state;
635 void vsllink_path_move(int num_states, enum tap_state *path)
637 int i, tms_len, tms_cmd_pos, path_idx = 0;
639 if (vsllink_tms_data_len > 0)
641 // there are vsllink_tms_data_len more tms bits to be shifted
642 // so there are vsllink_tms_data_len + num_states tms bits in all
643 tms_len = vsllink_tms_data_len + num_states;
646 // merge into last tms shift
649 // just append tms data to the last tms byte
650 vsllink_add_path(vsllink_tms_data_len, num_states, path);
652 else if (tms_len == 8)
654 // end last tms shift command
655 (*vsllink_tms_cmd_pos)--;
656 vsllink_add_path(vsllink_tms_data_len, num_states, path);
658 else if (tms_len < 16)
660 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
662 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
663 // there is enought tms length in the current tms shift command
664 (*vsllink_tms_cmd_pos)++;
665 vsllink_add_path(vsllink_tms_data_len, num_states, path);
669 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
670 // not enough tms length in the current tms shift command
671 // so a new command should be added
672 // first decrease byte length of last tms shift command
673 (*vsllink_tms_cmd_pos)--;
674 // append tms data to the last tms byte
675 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
676 path += 8 - vsllink_tms_data_len;
677 // add new command(3 bytes)
678 vsllink_tap_ensure_space(0, 3);
679 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
680 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
681 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
684 else if (tms_len == 16)
686 // end last tms shift command
687 vsllink_add_path(vsllink_tms_data_len, num_states, path);
690 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
691 if (vsllink_tms_data_len == 0)
693 vsllink_tms_cmd_pos = NULL;
699 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
701 path += 16 - vsllink_tms_data_len;
702 num_states -= 16 - vsllink_tms_data_len;
703 vsllink_tms_data_len = 0;
704 vsllink_tms_cmd_pos = NULL;
710 // Normal operation, don't need to append tms data
711 vsllink_tms_data_len = num_states & 7;
713 while (num_states > 0)
715 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
717 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
723 tms_len = (i + 7) >> 3;
724 vsllink_tap_ensure_space(0, tms_len + 2);
725 tms_cmd_pos = vsllink_usb_out_buffer_idx;
726 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
728 vsllink_add_path(0, i, path + path_idx);
734 if (vsllink_tms_data_len > 0)
736 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
738 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
739 (*vsllink_tms_cmd_pos)++;
743 vsllink_usb_out_buffer[tms_cmd_pos]--;
745 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
746 vsllink_tap_ensure_space(0, 3);
747 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
748 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
749 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
755 void vsllink_runtest(int num_cycles)
758 enum tap_state saved_end_state = end_state;
760 if (cur_state != TAP_IDLE)
762 // enter into IDLE state
763 vsllink_end_state(TAP_IDLE);
764 vsllink_state_move();
768 // cur_state == TAP_IDLE
769 if (vsllink_tms_data_len > 0)
771 // there are vsllink_tms_data_len more tms bits to be shifted
772 // so there are vsllink_tms_data_len + num_cycles tms bits in all
773 tms_len = vsllink_tms_data_len + num_cycles;
776 // merge into last tms shift
779 // just add to vsllink_tms_data_len
780 // same result if tun through
781 //vsllink_tms_data_len += num_cycles;
783 else if (tms_len == 8)
785 // end last tms shift command
786 // just reduce it, no need to append_tms
787 (*vsllink_tms_cmd_pos)--;
788 vsllink_usb_out_buffer_idx++;
790 else if (tms_len < 16)
792 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
794 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
795 // there is enought tms length in the current tms shift command
796 // increase the tms byte length by 1 and set the last byte to 0
797 (*vsllink_tms_cmd_pos)++;
798 vsllink_usb_out_buffer_idx++;
799 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
803 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
804 // not enough tms length in the current tms shift command
805 // so a new command should be added
806 // first decrease byte length of last tms shift command
807 (*vsllink_tms_cmd_pos)--;
808 // move the command pointer to the next empty position
809 vsllink_usb_out_buffer_idx++;
810 // add new command(3 bytes)
811 vsllink_tap_ensure_space(0, 3);
812 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
813 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
814 vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
817 else if (tms_len == 16)
819 // end last tms shift command
820 vsllink_usb_out_buffer_idx++;
821 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
824 vsllink_tms_data_len = (vsllink_tms_data_len + num_cycles) & 7;
825 if (vsllink_tms_data_len == 0)
827 vsllink_tms_cmd_pos = NULL;
833 vsllink_usb_out_buffer_idx++;
834 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
836 num_cycles -= 16 - vsllink_tms_data_len;
837 vsllink_tms_data_len = 0;
838 vsllink_tms_cmd_pos = NULL;
842 // from here vsllink_tms_data_len == 0 or num_cycles == 0
844 if (vsllink_tms_data_len > 0)
850 LOG_ERROR("There MUST be some bugs in the driver");
856 // get number of bytes left to be sent
857 tms_len = num_cycles >> 3;
860 vsllink_tap_ensure_space(1, 5);
861 // if tms_len > 0, vsllink_tms_data_len == 0
862 // so just add new command
863 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMS0BYTE;
864 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
865 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
866 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
867 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
869 vsllink_usb_in_want_length += 1;
870 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
871 pending_scan_results_length++;
873 if (tms_len > 0xFFFF)
875 vsllink_tap_execute();
876 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
877 vsllink_usb_out_buffer_idx = 3;
881 vsllink_tms_data_len = num_cycles & 7;
882 if (vsllink_tms_data_len > 0)
884 vsllink_tap_ensure_space(0, 3);
885 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
886 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
887 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
893 vsllink_end_state(saved_end_state);
894 cur_state = TAP_IDLE;
895 if (end_state != TAP_IDLE)
897 vsllink_state_move();
901 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
903 enum tap_state saved_end_state;
904 u8 bits_left, tms_tmp, tdi_len;
912 tdi_len = ((scan_size + 7) >> 3);
913 if ((tdi_len + 7) > VSLLINK_BufferSize)
915 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
919 saved_end_state = end_state;
921 /* Move to appropriate scan state */
922 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
924 if (vsllink_tms_data_len > 0)
926 if (cur_state == end_state)
928 // already in IRSHIFT or DRSHIFT state
929 // merge tms data in the last tms shift command into next scan command
930 if(*vsllink_tms_cmd_pos < 1)
932 LOG_ERROR("There MUST be some bugs in the driver");
935 else if(*vsllink_tms_cmd_pos < 2)
937 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
938 vsllink_usb_out_buffer_idx--;
942 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
943 *vsllink_tms_cmd_pos -= 2;
946 vsllink_tap_ensure_space(1, tdi_len + 7);
947 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
948 // which is merged from the last tms shift command
949 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
950 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
951 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
952 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
953 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
955 for (i = 0; i < tdi_len; i++)
957 buffer[i] >>= 8 - vsllink_tms_data_len;
960 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
964 vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
965 scan_size -= 8 - vsllink_tms_data_len;
966 vsllink_tms_data_len = 0;
970 vsllink_append_tms();
971 vsllink_tap_ensure_space(1, tdi_len + 5);
973 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
974 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
975 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
977 vsllink_tap_append_scan(scan_size, buffer, command, 0);
982 vsllink_tap_ensure_space(1, tdi_len + 7);
984 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
985 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
986 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
987 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
988 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
990 vsllink_tap_append_scan(scan_size, buffer, command, 8);
992 vsllink_end_state(saved_end_state);
994 bits_left = scan_size & 0x07;
995 cur_state = ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE;
999 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1003 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1006 if (cur_state != end_state)
1008 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
1012 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1015 cur_state = end_state;
1018 void vsllink_reset(int trst, int srst)
1022 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1024 /* Signals are active low */
1025 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1026 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1027 vsllink_usb_out_buffer[2] = 0;
1030 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1034 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1037 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1040 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1044 void vsllink_simple_command(u8 command)
1048 DEBUG_JTAG_IO("0x%02x", command);
1050 vsllink_usb_out_buffer[0] = command;
1051 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1055 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1059 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1062 LOG_ERROR("parameter error, should be one parameter for VID");
1066 vsllink_vid = strtol(args[0], NULL, 0);
1071 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1074 LOG_ERROR("parameter error, should be one parameter for PID");
1078 vsllink_pid = strtol(args[0], NULL, 0);
1083 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1086 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1090 vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
1095 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1098 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1102 vsllink_bulkout = strtol(args[0], NULL, 0);
1107 /***************************************************************************/
1108 /* VSLLink tap functions */
1110 void vsllink_tap_init(void)
1112 vsllink_usb_out_buffer_idx = 0;
1113 vsllink_usb_in_want_length = 0;
1114 pending_scan_results_length = 0;
1117 void vsllink_tap_ensure_space(int scans, int bytes)
1119 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1120 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1122 if (scans > available_scans || bytes > available_bytes)
1124 vsllink_tap_execute();
1125 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
1126 vsllink_usb_out_buffer_idx = 3;
1130 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
1132 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1137 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1141 vsllink_usb_in_want_length += (length + 7) >> 3;
1143 pending_scan_result->length = length;
1144 pending_scan_result->offset = offset;
1145 pending_scan_result->command = command;
1146 pending_scan_result->buffer = buffer;
1148 for (i = 0; i < ((length + 7) >> 3); i++)
1150 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1153 pending_scan_results_length++;
1156 /* Pad and send a tap sequence to the device, and receive the answer.
1157 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1158 int vsllink_tap_execute(void)
1164 if (vsllink_tms_data_len > 0)
1166 if((cur_state != TAP_RESET) && (cur_state != TAP_IDLE) && (cur_state != TAP_IRPAUSE) && (cur_state != TAP_DRPAUSE))
1168 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", jtag_state_name(cur_state));
1171 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1173 // last tms bit is '1'
1174 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1175 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1176 vsllink_tms_data_len = 0;
1180 // last tms bit is '0'
1181 vsllink_usb_out_buffer_idx++;
1182 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1183 vsllink_tms_data_len = 0;
1187 if (vsllink_usb_out_buffer_idx > 3)
1189 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1191 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1192 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1195 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1197 if (result == vsllink_usb_in_want_length)
1199 for (i = 0; i < pending_scan_results_length; i++)
1201 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1202 u8 *buffer = pending_scan_result->buffer;
1203 int length = pending_scan_result->length;
1204 int offset = pending_scan_result->offset;
1205 scan_command_t *command = pending_scan_result->command;
1209 // IRSHIFT or DRSHIFT
1210 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1211 first += (length + offset + 7) >> 3;
1213 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1214 #ifdef _DEBUG_JTAG_IO_
1215 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1218 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1221 return ERROR_JTAG_QUEUE_FAILED;
1224 free(pending_scan_result->buffer);
1225 pending_scan_result->buffer = NULL;
1235 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1236 return ERROR_JTAG_QUEUE_FAILED;
1245 /*****************************************************************************/
1246 /* VSLLink USB low-level functions */
1248 vsllink_jtag_t* vsllink_usb_open(void)
1250 struct usb_bus *busses;
1251 struct usb_bus *bus;
1252 struct usb_device *dev;
1254 vsllink_jtag_t *result;
1256 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1262 busses = usb_get_busses();
1264 /* find vsllink_jtag device in usb bus */
1266 for (bus = busses; bus; bus = bus->next)
1268 for (dev = bus->devices; dev; dev = dev->next)
1270 if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
1272 result->usb_handle = usb_open(dev);
1274 /* usb_set_configuration required under win32 */
1275 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1276 usb_claim_interface(result->usb_handle, 0);
1280 * This makes problems under Mac OS X. And is not needed
1281 * under Windows. Hopefully this will not break a linux build
1283 usb_set_altinterface(result->usb_handle, 0);
1294 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1296 usb_close(vsllink_jtag->usb_handle);
1300 /* Send a message and receive the reply. */
1301 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1305 result = vsllink_usb_write(vsllink_jtag, out_length);
1306 if (result == out_length)
1310 result = vsllink_usb_read(vsllink_jtag);
1311 if (result == in_length )
1317 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1325 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1330 /* Write data from out_buffer to USB. */
1331 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1335 if (out_length > VSLLINK_BufferSize)
1337 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1341 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
1342 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1344 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1346 #ifdef _DEBUG_USB_COMMS_
1347 LOG_DEBUG("USB out:");
1348 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1351 #ifdef _VSLLINK_IN_DEBUG_MODE_
1358 /* Read data from USB into in_buffer. */
1359 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1361 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
1362 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1364 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1366 #ifdef _DEBUG_USB_COMMS_
1367 LOG_DEBUG("USB in:");
1368 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1373 #define BYTES_PER_LINE 16
1375 void vsllink_debug_buffer(u8 *buffer, int length)
1382 for (i = 0; i < length; i += BYTES_PER_LINE)
1384 snprintf(line, 5, "%04x", i);
1385 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1387 snprintf(s, 4, " %02x", buffer[j]);