1 /***************************************************************************
2 * Copyright (C) 2007 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 ...)
54 #define VSLLINK_WRITE_ENDPOINT 0x02
55 #define VSLLINK_READ_ENDPOINT 0x82
57 u16 vsllink_vid = VID;
58 u16 vsllink_pid = PID;
59 u8 vsllink_bulkout = VSLLINK_WRITE_ENDPOINT;
60 u8 vsllink_bulkin = VSLLINK_READ_ENDPOINT;
62 #define VSLLINK_USB_TIMEOUT 1000
64 static int VSLLINK_BufferSize = 1024;
66 /* Global USB buffers */
67 static int vsllink_usb_out_buffer_idx;
68 static int vsllink_usb_in_want_length;
69 static u8* vsllink_usb_in_buffer = NULL;
70 static u8* vsllink_usb_out_buffer = NULL;
72 /* Constants for VSLLink command */
73 #define VSLLINK_CMD_CONN 0x80
74 #define VSLLINK_CMD_DISCONN 0x81
75 #define VSLLINK_CMD_SET_SPEED 0x82
76 #define VSLLINK_CMD_SET_PORT 0x90
77 #define VSLLINK_CMD_GET_PORT 0x91
78 #define VSLLINK_CMD_SET_PORTDIR 0x92
79 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
81 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
82 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
84 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
85 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
87 #define JTAG_PINMSK_SRST (1 << 0)
88 #define JTAG_PINMSK_TRST (1 << 1)
89 #define JTAG_PINMSK_USR1 (1 << 2)
90 #define JTAG_PINMSK_USR2 (1 << 3)
91 #define JTAG_PINMSK_TCK (1 << 4)
92 #define JTAG_PINMSK_TMS (1 << 5)
93 #define JTAG_PINMSK_TDI (1 << 6)
94 #define JTAG_PINMSK_TDO (1 << 7)
97 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_map[from]][tap_move_map[to]]
99 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
100 * 0: Test-Logic-Reset
107 * SD->SD and SI->SI have to be caught in interface specific code
109 u8 VSLLINK_tap_move[6][6] =
111 /* TLR RTI SD PD SI PI */
112 {0xff, 0x00, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
113 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
114 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
115 {0xff, 0x60, 0x40, 0x17, 0x3c, 0x2f}, /* PD */
116 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
117 {0xff, 0x60, 0x38, 0x17, 0x40, 0x2f} /* PI */
120 u8 VSLLINK_TAP_MOVE_FROM_E1[6] =
122 // TLR RTI SD PD SI PI
123 0xff, 0x60, 0x38, 0x5c, 0x3c, 0x5E
126 u8 VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[7][6][2] =
173 u8 VSLLINK_BIT_MSK[8] =
175 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
178 /* External interface functions */
179 int vsllink_execute_queue(void);
180 int vsllink_speed(int speed);
181 int vsllink_khz(int khz, int *jtag_speed);
182 int vsllink_speed_div(int jtag_speed, int *khz);
183 int vsllink_register_commands(struct command_context_s *cmd_ctx);
184 int vsllink_init(void);
185 int vsllink_quit(void);
187 /* CLI command handler functions */
188 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
189 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
190 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
191 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
193 /* Queue command functions */
194 void vsllink_end_state(enum tap_state state);
195 void vsllink_state_move(void);
196 void vsllink_path_move(int num_states, enum tap_state *path);
197 void vsllink_runtest(int num_cycles);
198 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
199 void vsllink_reset(int trst, int srst);
200 void vsllink_simple_command(u8 command);
202 /* VSLLink tap buffer functions */
203 void vsllink_tap_init();
204 int vsllink_tap_execute();
205 void vsllink_tap_ensure_space(int scans, int bytes);
206 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
208 /* VSLLink lowlevel functions */
209 typedef struct vsllink_jtag
211 struct usb_dev_handle* usb_handle;
214 vsllink_jtag_t *vsllink_usb_open(void);
215 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
216 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
217 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
218 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
220 void vsllink_debug_buffer(u8 *buffer, int length);
222 int vsllink_tms_data_len = 0;
223 u8* vsllink_tms_cmd_pos;
225 vsllink_jtag_t* vsllink_jtag_handle;
227 /***************************************************************************/
228 /* External interface implementation */
230 jtag_interface_t vsllink_interface =
233 .execute_queue = vsllink_execute_queue,
234 .speed = vsllink_speed,
236 .speed_div = vsllink_speed_div,
237 .register_commands = vsllink_register_commands,
238 .init = vsllink_init,
242 int vsllink_execute_queue(void)
244 jtag_command_t *cmd = jtag_command_queue;
249 DEBUG_JTAG_IO("--------------------------------------------------------------------------------");
251 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
252 vsllink_usb_out_buffer_idx = 3;
258 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
260 if (cmd->cmd.end_state->end_state != -1)
262 vsllink_end_state(cmd->cmd.end_state->end_state);
267 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
268 cmd->cmd.runtest->end_state);
270 if (cmd->cmd.runtest->end_state != -1)
272 vsllink_end_state(cmd->cmd.runtest->end_state);
274 vsllink_runtest(cmd->cmd.runtest->num_cycles);
278 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
280 if (cmd->cmd.statemove->end_state != -1)
282 vsllink_end_state(cmd->cmd.statemove->end_state);
284 vsllink_state_move();
288 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
289 cmd->cmd.pathmove->num_states, \
290 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
292 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
296 if (cmd->cmd.scan->end_state != -1)
298 vsllink_end_state(cmd->cmd.scan->end_state);
301 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
302 if (cmd->cmd.scan->ir_scan)
304 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %d:", scan_size, cmd->cmd.scan->end_state);
308 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %d:", scan_size, cmd->cmd.scan->end_state);
311 #ifdef _DEBUG_JTAG_IO_
312 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
315 type = jtag_scan_type(cmd->cmd.scan);
317 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
321 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
323 vsllink_tap_execute();
325 if (cmd->cmd.reset->trst == 1)
327 cur_state = TAP_RESET;
329 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
331 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
332 vsllink_usb_out_buffer_idx = 3;
336 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
337 vsllink_tap_execute();
338 jtag_sleep(cmd->cmd.sleep->us);
342 LOG_ERROR("BUG: unknown JTAG command type encountered");
348 return vsllink_tap_execute();
351 int vsllink_speed(int speed)
355 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
356 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
357 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
359 result = vsllink_usb_write(vsllink_jtag_handle, 3);
367 LOG_ERROR("VSLLink setting speed failed (%d)", result);
368 return ERROR_JTAG_DEVICE_ERROR;
374 int vsllink_khz(int khz, int *jtag_speed)
381 int vsllink_speed_div(int jtag_speed, int *khz)
388 int vsllink_register_commands(struct command_context_s *cmd_ctx)
390 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
391 COMMAND_CONFIG, NULL);
392 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
393 COMMAND_CONFIG, NULL);
394 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
395 COMMAND_CONFIG, NULL);
396 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
397 COMMAND_CONFIG, NULL);
402 int vsllink_init(void)
406 char version_str[100];
408 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
409 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
410 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
412 LOG_ERROR("Not enough memory");
416 vsllink_jtag_handle = vsllink_usb_open();
418 if (vsllink_jtag_handle == 0)
420 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
421 return ERROR_JTAG_INIT_FAILED;
425 while (check_cnt < 3)
427 vsllink_simple_command(VSLLINK_CMD_CONN);
428 result = vsllink_usb_read(vsllink_jtag_handle);
432 vsllink_usb_in_buffer[result] = 0;
433 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
434 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
435 LOG_INFO(version_str);
437 // free the pre-alloc memroy
438 free(vsllink_usb_in_buffer);
439 free(vsllink_usb_out_buffer);
440 vsllink_usb_in_buffer = NULL;
441 vsllink_usb_out_buffer = NULL;
444 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
445 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
446 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
448 LOG_ERROR("Not enough memory");
453 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
457 vsllink_simple_command(VSLLINK_CMD_DISCONN);
464 // It's dangerout to proced
465 LOG_ERROR("VSLLink initial failed");
469 // Set SRST and TRST to output, Set USR1 and USR2 to input
470 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
471 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
472 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
473 result = vsllink_usb_write(vsllink_jtag_handle, 3);
476 LOG_ERROR("VSLLink USB send data error");
482 LOG_INFO("VSLLink JTAG Interface ready");
489 int vsllink_quit(void)
491 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
493 vsllink_simple_command(VSLLINK_CMD_DISCONN);
494 vsllink_usb_close(vsllink_jtag_handle);
497 if (vsllink_usb_in_buffer != NULL)
499 free(vsllink_usb_in_buffer);
501 if (vsllink_usb_out_buffer != NULL)
503 free(vsllink_usb_out_buffer);
508 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
509 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set.
510 void VSLLINK_add_tms_from_RTI(enum tap_state state)
512 u8 tms_scan = VSLLINK_TAP_MOVE(TAP_IDLE, state);
515 if ((cur_state != TAP_IDLE) || (state == TAP_IDLE) || (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || (vsllink_tms_cmd_pos == NULL))
517 LOG_ERROR("There MUST be some bugs in the driver");
521 tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]]) << vsllink_tms_data_len;
522 if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][0] == 1)
524 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]);
526 tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]) << (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]);
528 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
529 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
531 vsllink_tms_data_len = 0;
532 vsllink_tms_cmd_pos = NULL;
535 /***************************************************************************/
536 /* Queue command implementations */
538 void vsllink_end_state(enum tap_state state)
540 if (tap_move_map[state] != -1)
546 LOG_ERROR("BUG: %i is not a valid end state", state);
551 /* Goes to the end state. */
552 void vsllink_state_move(void)
554 if (vsllink_tms_data_len > 0)
556 VSLLINK_add_tms_from_RTI(end_state);
560 vsllink_tap_ensure_space(0, 2);
562 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
563 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
566 cur_state = end_state;
569 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
570 void vsllink_add_path(int start, int num, enum tap_state *path)
574 for (i = start; i < (start + num); i++)
580 vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
584 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
588 if (path[i - start] == tap_transitions[cur_state].high)
590 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
594 LOG_ERROR("BUG: %d -> %d isn't a valid TAP transition", cur_state, path[i]);
600 end_state = cur_state;
603 void vsllink_path_move(int num_states, enum tap_state *path)
605 int i, tms_len, tms_cmd_pos, path_idx = 0;
607 if (vsllink_tms_data_len > 0)
609 if ((vsllink_tms_data_len + num_states) < 8)
611 vsllink_add_path(vsllink_tms_data_len, num_states, path);
614 else if ((vsllink_tms_data_len + num_states) < 16)
616 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) \
617 < VSLLINK_CMDJTAGSEQ_LENMSK)
619 *vsllink_tms_cmd_pos++;
620 vsllink_add_path(vsllink_tms_data_len, num_states, path);
624 // need a new VSLLINK_CMDJTAGSEQ_TMSBYTE command
625 // if vsllink_tms_data_len > 0, length of VSLLINK_CMDJTAGSEQ_TMSBYTE MUST be > 1(tms_len > 2)
626 *vsllink_tms_cmd_pos--;
627 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
628 vsllink_usb_out_buffer_idx++;
629 vsllink_tap_ensure_space(0, 3);
630 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
631 vsllink_tms_cmd_pos = vsllink_usb_out_buffer + vsllink_usb_out_buffer_idx;
632 vsllink_add_path(vsllink_tms_data_len, num_states + vsllink_tms_data_len - 8, path + 8 - vsllink_tms_data_len);
634 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
639 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
640 path_idx = 16 - vsllink_tms_data_len;
641 vsllink_usb_out_buffer_idx++;
643 num_states -= 16 - vsllink_tms_data_len;
644 path += 16 - vsllink_tms_data_len;
645 vsllink_tms_data_len = 0;
646 vsllink_tms_cmd_pos = NULL;
652 // Normal operation, don't need to append tms data
653 vsllink_tms_data_len = num_states & 7;
655 while (num_states > 0)
657 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
659 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
665 tms_len = (i + 7) >> 3;
666 vsllink_tap_ensure_space(0, tms_len + 2);
667 tms_cmd_pos = vsllink_usb_out_buffer_idx;
668 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
670 vsllink_add_path(0, i, path + path_idx);
676 if (vsllink_tms_data_len > 0)
678 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
680 vsllink_usb_out_buffer[tms_cmd_pos]++;
681 vsllink_usb_out_buffer = vsllink_usb_out_buffer + tms_cmd_pos;
685 vsllink_usb_out_buffer[tms_cmd_pos]--;
686 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
687 vsllink_tap_ensure_space(0, 3);
688 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
689 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
695 void vsllink_runtest(int num_cycles)
698 int tms_len, first_tms = 0, tms_cmd_pos;
699 enum tap_state saved_end_state = end_state;
701 if (cur_state != TAP_IDLE)
703 vsllink_end_state(TAP_IDLE);
705 if (vsllink_tms_data_len > 0)
707 VSLLINK_add_tms_from_RTI(end_state);
715 if (vsllink_tms_data_len > 0)
717 // cur_state == TAP_IDLE
718 if ((vsllink_tms_data_len + num_cycles) < 8)
720 vsllink_tms_data_len += num_cycles;
723 else if ((vsllink_tms_data_len + num_cycles) < 16)
725 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) \
726 < VSLLINK_CMDJTAGSEQ_LENMSK)
728 *vsllink_tms_cmd_pos++;
729 vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
733 // need a new VSLLINK_CMDJTAGSEQ_TMSBYTE command
734 // if vsllink_tms_data_len > 0, length of VSLLINK_CMDJTAGSEQ_TMSBYTE MUST be > 1(tms_len > 2)
735 *vsllink_tms_cmd_pos--;
736 vsllink_tap_ensure_space(0, 3);
737 vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
738 vsllink_tms_cmd_pos = vsllink_usb_out_buffer + vsllink_usb_out_buffer_idx;
739 vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
741 vsllink_tms_data_len = (vsllink_tms_data_len + num_cycles) & 7;
746 vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
747 vsllink_usb_out_buffer_idx++;
749 num_cycles -= 16 - vsllink_tms_data_len;
750 vsllink_tms_data_len = 0;
751 vsllink_tms_cmd_pos = NULL;
755 tms_len = ((num_cycles + 7) >> 3) + first_tms;
758 // Normal operation, don't need to append tms data
759 vsllink_tms_data_len = num_cycles & 7;
761 if (vsllink_tms_data_len > 0)
765 // tms_len includes the length of tms byte to append
767 // Make sure there is enough space
768 // 1 more byte maybe needed for the last tms move
769 vsllink_tap_ensure_space(0, (tms_len / VSLLINK_CMDJTAGSEQ_LENMSK) + tms_len + 1);
773 if (tms_len > (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
775 i = VSLLINK_CMDJTAGSEQ_LENMSK + 1;
782 tms_cmd_pos = vsllink_usb_out_buffer_idx;
784 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (i - 1);
788 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
799 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
805 // post process vsllink_usb_out_buffer_idx
806 if (vsllink_tms_data_len > 0)
808 vsllink_usb_out_buffer_idx -= 2;
812 vsllink_end_state(saved_end_state);
813 cur_state = TAP_IDLE;
814 if (saved_end_state != TAP_IDLE)
816 if (vsllink_tms_data_len > 0)
818 VSLLINK_add_tms_from_RTI(end_state);
822 if (i < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
824 vsllink_usb_out_buffer[tms_cmd_pos]++;
825 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(TAP_IDLE, end_state);
829 vsllink_tap_ensure_space(0, 2);
830 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
831 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(TAP_IDLE, end_state);
834 cur_state = saved_end_state;
837 if (vsllink_tms_data_len > 0)
839 vsllink_tms_cmd_pos = vsllink_usb_out_buffer + tms_cmd_pos;
844 // Set end_state if no RTI shifts
845 vsllink_end_state(saved_end_state);
846 cur_state = TAP_IDLE;
847 if (saved_end_state != TAP_IDLE)
849 vsllink_tap_ensure_space(0, 2);
850 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
851 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(TAP_IDLE, end_state);
853 cur_state = saved_end_state;
858 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
860 enum tap_state saved_end_state;
861 u8 bits_left, tms_tmp, tdi_len;
864 tdi_len = ((scan_size + 7) >> 3);
865 if ((tdi_len + 7) > VSLLINK_BufferSize)
867 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
871 saved_end_state = end_state;
873 /* Move to appropriate scan state */
874 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
876 if (vsllink_tms_data_len > 0)
878 if (cur_state == end_state)
880 *vsllink_tms_cmd_pos--;
881 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
882 vsllink_tap_ensure_space(1, tdi_len + 7);
884 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
885 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
886 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
887 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
888 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
890 for (i = 0; i < tdi_len; i++)
892 buffer[i] >>= 8 - vsllink_tms_data_len;
895 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
899 vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
900 scan_size -= 8 - vsllink_tms_data_len;
904 VSLLINK_add_tms_from_RTI(end_state);
905 vsllink_tap_ensure_space(1, tdi_len + 5);
907 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
908 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
909 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
911 vsllink_tap_append_scan(scan_size, buffer, command, 0);
916 vsllink_tap_ensure_space(1, tdi_len + 7);
918 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
919 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
920 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
921 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
922 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
924 vsllink_tap_append_scan(scan_size, buffer, command, 8);
926 vsllink_end_state(saved_end_state);
928 bits_left = scan_size & 0x07;
929 cur_state = ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE;
933 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
937 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
940 if (cur_state != end_state)
942 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE_FROM_E1[tap_move_map[end_state]];
946 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
949 cur_state = end_state;
952 void vsllink_reset(int trst, int srst)
956 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
958 /* Signals are active low */
959 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
960 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
961 vsllink_usb_out_buffer[2] = 0;
964 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
968 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
971 result = vsllink_usb_write(vsllink_jtag_handle, 3);
974 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)");
978 void vsllink_simple_command(u8 command)
982 DEBUG_JTAG_IO("0x%02x", command);
984 vsllink_usb_out_buffer[0] = command;
985 result = vsllink_usb_write(vsllink_jtag_handle, 1);
989 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
993 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
996 LOG_ERROR("parameter error, should be one parameter for VID");
1000 vsllink_vid = strtol(args[0], NULL, 0);
1005 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1008 LOG_ERROR("parameter error, should be one parameter for PID");
1012 vsllink_pid = strtol(args[0], NULL, 0);
1017 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1020 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1024 vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
1029 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1032 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1036 vsllink_bulkout = strtol(args[0], NULL, 0);
1041 /***************************************************************************/
1042 /* VSLLink tap functions */
1046 int length; /* Number of bits to read */
1048 scan_command_t *command; /* Corresponding scan command */
1050 } pending_scan_result_t;
1052 #define MAX_PENDING_SCAN_RESULTS 256
1054 static int pending_scan_results_length;
1055 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1057 static int last_tms;
1059 void vsllink_tap_init()
1061 vsllink_usb_out_buffer_idx = 0;
1062 vsllink_usb_in_want_length = 0;
1063 pending_scan_results_length = 0;
1066 void vsllink_tap_ensure_space(int scans, int bytes)
1068 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1069 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1071 if (scans > available_scans || bytes > available_bytes)
1073 vsllink_tap_execute();
1074 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
1075 vsllink_usb_out_buffer_idx = 3;
1079 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
1081 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1086 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1090 vsllink_usb_in_want_length += (length + 7) >> 3;
1092 pending_scan_result->length = length;
1093 pending_scan_result->offset = offset;
1094 pending_scan_result->command = command;
1095 pending_scan_result->buffer = buffer;
1097 for (i = 0; i < ((length + 7) >> 3); i++)
1099 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1102 pending_scan_results_length++;
1105 /* Pad and send a tap sequence to the device, and receive the answer.
1106 * For the purpose of padding we assume that we are in idle or pause state. */
1107 int vsllink_tap_execute()
1113 if (vsllink_tms_data_len > 0)
1115 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1117 // last tms bit is '1'
1118 // the only possible state is TLR, no need to control the number of shifts in RLT
1119 // There MUST be some errors in the code
1120 LOG_ERROR("last tms bit is '1'");
1125 // last tms bit is '0'
1126 vsllink_usb_out_buffer_idx++;
1127 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1128 vsllink_tms_data_len = 0;
1132 if (vsllink_usb_out_buffer_idx > 3)
1134 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1136 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1137 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1140 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1142 if (result == vsllink_usb_in_want_length)
1144 for (i = 0; i < pending_scan_results_length; i++)
1146 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1147 u8 *buffer = pending_scan_result->buffer;
1148 int length = pending_scan_result->length;
1149 int offset = pending_scan_result->offset;
1150 scan_command_t *command = pending_scan_result->command;
1152 /* Copy to buffer */
1153 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1154 first += (length + offset + 7) >> 3;
1156 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1157 #ifdef _DEBUG_JTAG_IO_
1158 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1161 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1164 return ERROR_JTAG_QUEUE_FAILED;
1167 if (pending_scan_result->buffer != NULL)
1169 free(pending_scan_result->buffer);
1170 pending_scan_result->buffer = NULL;
1176 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1177 return ERROR_JTAG_QUEUE_FAILED;
1186 /*****************************************************************************/
1187 /* VSLLink USB low-level functions */
1189 vsllink_jtag_t* vsllink_usb_open()
1191 struct usb_bus *busses;
1192 struct usb_bus *bus;
1193 struct usb_device *dev;
1195 vsllink_jtag_t *result;
1197 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1203 busses = usb_get_busses();
1205 /* find vsllink_jtag device in usb bus */
1207 for (bus = busses; bus; bus = bus->next)
1209 for (dev = bus->devices; dev; dev = dev->next)
1211 if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
1213 result->usb_handle = usb_open(dev);
1215 /* usb_set_configuration required under win32 */
1216 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1217 usb_claim_interface(result->usb_handle, 0);
1221 * This makes problems under Mac OS X. And is not needed
1222 * under Windows. Hopefully this will not break a linux build
1224 usb_set_altinterface(result->usb_handle, 0);
1235 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1237 usb_close(vsllink_jtag->usb_handle);
1241 /* Send a message and receive the reply. */
1242 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1247 result = vsllink_usb_write(vsllink_jtag, out_length);
1248 if (result == out_length)
1252 result = vsllink_usb_read(vsllink_jtag);
1253 if (result == in_length )
1259 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1267 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1272 /* Write data from out_buffer to USB. */
1273 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1277 if (out_length > VSLLINK_BufferSize)
1279 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1283 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
1284 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1286 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1288 #ifdef _DEBUG_USB_COMMS_
1289 LOG_DEBUG("USB out:");
1290 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1293 #ifdef _VSLLINK_IN_DEBUG_MODE_
1300 /* Read data from USB into in_buffer. */
1301 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1303 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
1304 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1306 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1308 #ifdef _DEBUG_USB_COMMS_
1309 LOG_DEBUG("USB in:");
1310 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1315 #define BYTES_PER_LINE 16
1317 void vsllink_debug_buffer(u8 *buffer, int length)
1324 for (i = 0; i < length; i += BYTES_PER_LINE)
1326 snprintf(line, 5, "%04x", i);
1327 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1329 snprintf(s, 4, " %02x", buffer[j]);