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 10000
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_TMSCLOCK 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_ndx(from)][tap_move_ndx(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(tap_state_t state);
206 void vsllink_state_move(void);
207 void vsllink_path_move(int num_states, tap_state_t *path);
208 void vsllink_runtest(int num_cycles);
209 void vsllink_stableclocks(int num_cycles, int tms);
210 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
211 void vsllink_reset(int trst, int srst);
212 void vsllink_simple_command(u8 command);
214 /* VSLLink tap buffer functions */
215 void vsllink_tap_init(void);
216 int vsllink_tap_execute(void);
217 void vsllink_tap_ensure_space(int scans, int bytes);
218 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
220 /* VSLLink lowlevel functions */
221 typedef struct vsllink_jtag
223 struct usb_dev_handle* usb_handle;
226 vsllink_jtag_t *vsllink_usb_open(void);
227 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
228 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
229 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
230 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
232 void vsllink_debug_buffer(u8 *buffer, int length);
234 static int vsllink_tms_data_len = 0;
235 static u8* vsllink_tms_cmd_pos;
237 vsllink_jtag_t* vsllink_jtag_handle;
239 /***************************************************************************/
240 /* External interface implementation */
242 jtag_interface_t vsllink_interface =
245 .execute_queue = vsllink_execute_queue,
246 .speed = vsllink_speed,
248 .speed_div = vsllink_speed_div,
249 .register_commands = vsllink_register_commands,
250 .init = vsllink_init,
254 int vsllink_execute_queue(void)
256 jtag_command_t *cmd = jtag_command_queue;
261 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
263 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
264 vsllink_usb_out_buffer_idx = 3;
270 DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd->cmd.end_state->end_state));
272 if (cmd->cmd.end_state->end_state != -1)
274 vsllink_end_state(cmd->cmd.end_state->end_state);
279 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
280 tap_state_name(cmd->cmd.runtest->end_state));
282 if (cmd->cmd.runtest->end_state != -1)
284 vsllink_end_state(cmd->cmd.runtest->end_state);
286 vsllink_runtest(cmd->cmd.runtest->num_cycles);
290 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
292 if (cmd->cmd.statemove->end_state != -1)
294 vsllink_end_state(cmd->cmd.statemove->end_state);
296 vsllink_state_move();
300 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
301 cmd->cmd.pathmove->num_states, \
302 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
304 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
308 if (cmd->cmd.scan->end_state != -1)
310 vsllink_end_state(cmd->cmd.scan->end_state);
313 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
314 if (cmd->cmd.scan->ir_scan)
316 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
320 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
323 #ifdef _DEBUG_JTAG_IO_
324 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
327 type = jtag_scan_type(cmd->cmd.scan);
329 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
333 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
335 vsllink_tap_execute();
337 if (cmd->cmd.reset->trst == 1)
339 tap_set_state(TAP_RESET);
341 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
343 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
344 vsllink_usb_out_buffer_idx = 3;
348 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
349 vsllink_tap_execute();
350 jtag_sleep(cmd->cmd.sleep->us);
353 case JTAG_STABLECLOCKS:
354 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
355 switch(tap_get_state())
358 // tms should be '1' to stay in TAP_RESET mode
366 // in other mode, tms should be '0'
368 break; /* above stable states are OK */
370 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
371 tap_state_name(tap_get_state()) );
374 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
378 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
384 return vsllink_tap_execute();
387 int vsllink_speed(int speed)
391 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
392 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
393 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
395 result = vsllink_usb_write(vsllink_jtag_handle, 3);
403 LOG_ERROR("VSLLink setting speed failed (%d)", result);
404 return ERROR_JTAG_DEVICE_ERROR;
410 int vsllink_khz(int khz, int *jtag_speed)
417 int vsllink_speed_div(int jtag_speed, int *khz)
424 int vsllink_register_commands(struct command_context_s *cmd_ctx)
426 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
427 COMMAND_CONFIG, NULL);
428 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
429 COMMAND_CONFIG, NULL);
430 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
431 COMMAND_CONFIG, NULL);
432 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
433 COMMAND_CONFIG, NULL);
438 int vsllink_init(void)
442 char version_str[100];
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");
452 vsllink_jtag_handle = vsllink_usb_open();
454 if (vsllink_jtag_handle == 0)
456 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
457 return ERROR_JTAG_INIT_FAILED;
461 while (check_cnt < 3)
463 vsllink_simple_command(VSLLINK_CMD_CONN);
464 result = vsllink_usb_read(vsllink_jtag_handle);
468 vsllink_usb_in_buffer[result] = 0;
469 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
470 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
471 LOG_INFO(version_str);
473 // free the pre-alloc memroy
474 free(vsllink_usb_in_buffer);
475 free(vsllink_usb_out_buffer);
476 vsllink_usb_in_buffer = NULL;
477 vsllink_usb_out_buffer = NULL;
480 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
481 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
482 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
484 LOG_ERROR("Not enough memory");
489 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
493 vsllink_simple_command(VSLLINK_CMD_DISCONN);
500 // It's dangerout to proced
501 LOG_ERROR("VSLLink initial failed");
505 // Set SRST and TRST to output, Set USR1 and USR2 to input
506 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
507 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
508 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
509 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
511 LOG_ERROR("VSLLink USB send data error");
517 LOG_INFO("VSLLink JTAG Interface ready");
524 int vsllink_quit(void)
526 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
528 // Set all pins to input
529 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
530 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
531 vsllink_usb_out_buffer[2] = 0;
532 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
534 LOG_ERROR("VSLLink USB send data error");
539 vsllink_simple_command(VSLLINK_CMD_DISCONN);
540 vsllink_usb_close(vsllink_jtag_handle);
543 if (vsllink_usb_in_buffer != NULL)
545 free(vsllink_usb_in_buffer);
547 if (vsllink_usb_out_buffer != NULL)
549 free(vsllink_usb_out_buffer);
554 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
555 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
556 void vsllink_append_tms(void)
558 u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
560 tap_state_t end_state = tap_get_end_state();
562 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
563 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
564 (vsllink_tms_cmd_pos == NULL))
566 LOG_ERROR("There MUST be some bugs in the driver");
570 tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position]) << \
571 vsllink_tms_data_len;
572 if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_value == 1)
574 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
575 (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position);
577 tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position) << \
578 (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position);
580 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
581 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
583 vsllink_tms_data_len = 0;
584 vsllink_tms_cmd_pos = NULL;
587 /***************************************************************************/
588 /* Queue command implementations */
590 void vsllink_end_state(tap_state_t state)
592 if (tap_is_state_stable(state))
594 tap_set_end_state(state);
598 LOG_ERROR("BUG: %i is not a valid end state", state);
603 /* Goes to the end state. */
604 void vsllink_state_move(void)
606 if (vsllink_tms_data_len > 0)
608 vsllink_append_tms();
612 vsllink_tap_ensure_space(0, 2);
614 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
615 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
618 tap_set_state(tap_get_end_state());
621 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
622 void vsllink_add_path(int start, int num, tap_state_t *path)
626 for (i = start; i < (start + num); i++)
632 vsllink_usb_out_buffer_idx++;
634 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
637 if (path[i - start] == tap_state_transition(tap_get_state(), true))
639 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
641 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
647 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
650 tap_set_state(path[i - start]);
652 if ((i > 0) && ((i & 7) == 0))
654 vsllink_usb_out_buffer_idx++;
655 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
658 tap_set_end_state(tap_get_state());
661 void vsllink_path_move(int num_states, tap_state_t *path)
663 int i, tms_len, tms_cmd_pos, path_idx = 0;
665 if (vsllink_tms_data_len > 0)
667 // there are vsllink_tms_data_len more tms bits to be shifted
668 // so there are vsllink_tms_data_len + num_states tms bits in all
669 tms_len = vsllink_tms_data_len + num_states;
672 // merge into last tms shift
675 // just append tms data to the last tms byte
676 vsllink_add_path(vsllink_tms_data_len, num_states, path);
678 else if (tms_len == 8)
680 // end last tms shift command
681 (*vsllink_tms_cmd_pos)--;
682 vsllink_add_path(vsllink_tms_data_len, num_states, path);
684 else if (tms_len < 16)
686 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
688 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
689 // there is enought tms length in the current tms shift command
690 (*vsllink_tms_cmd_pos)++;
691 vsllink_add_path(vsllink_tms_data_len, num_states, path);
695 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
696 // not enough tms length in the current tms shift command
697 // so a new command should be added
698 // first decrease byte length of last tms shift command
699 (*vsllink_tms_cmd_pos)--;
700 // append tms data to the last tms byte
701 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
702 path += 8 - vsllink_tms_data_len;
703 // add new command(3 bytes)
704 vsllink_tap_ensure_space(0, 3);
705 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
706 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
707 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
710 else if (tms_len == 16)
712 // end last tms shift command
713 vsllink_add_path(vsllink_tms_data_len, num_states, path);
716 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
717 if (vsllink_tms_data_len == 0)
719 vsllink_tms_cmd_pos = NULL;
725 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
727 path += 16 - vsllink_tms_data_len;
728 num_states -= 16 - vsllink_tms_data_len;
729 vsllink_tms_data_len = 0;
730 vsllink_tms_cmd_pos = NULL;
736 // Normal operation, don't need to append tms data
737 vsllink_tms_data_len = num_states & 7;
739 while (num_states > 0)
741 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
743 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
749 tms_len = (i + 7) >> 3;
750 vsllink_tap_ensure_space(0, tms_len + 2);
751 tms_cmd_pos = vsllink_usb_out_buffer_idx;
752 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
754 vsllink_add_path(0, i, path + path_idx);
760 if (vsllink_tms_data_len > 0)
762 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
764 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
765 (*vsllink_tms_cmd_pos)++;
769 vsllink_usb_out_buffer[tms_cmd_pos]--;
771 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
772 vsllink_tap_ensure_space(0, 3);
773 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
774 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
775 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
781 void vsllink_stableclocks(int num_cycles, int tms)
786 if (vsllink_tms_data_len > 0)
788 // there are vsllink_tms_data_len more tms bits to be shifted
789 // so there are vsllink_tms_data_len + num_cycles tms bits in all
790 tms_len = vsllink_tms_data_len + num_cycles;
793 // append '1' for tms
794 tms_append_byte = (u16)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
798 // append '0' for tms
803 // merge into last tms shift
806 // just add to vsllink_tms_data_len
807 // same result if tun through
808 //vsllink_tms_data_len += num_cycles;
809 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (u8)(tms_append_byte & 0xFF);
811 else if (tms_len == 8)
813 // end last tms shift command
814 // just reduce it, and append last tms byte
815 (*vsllink_tms_cmd_pos)--;
816 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
818 else if (tms_len < 16)
820 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
822 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
823 // there is enought tms length in the current tms shift command
824 // increase the tms byte length by 1 and set the last byte to 0
825 (*vsllink_tms_cmd_pos)++;
826 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
827 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
831 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
832 // not enough tms length in the current tms shift command
833 // so a new command should be added
834 // first decrease byte length of last tms shift command
835 (*vsllink_tms_cmd_pos)--;
836 // append last tms byte and move the command pointer to the next empty position
837 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
838 // add new command(3 bytes)
839 vsllink_tap_ensure_space(0, 3);
840 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
841 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
842 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
845 else if (tms_len == 16)
847 // end last tms shift command
848 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
849 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
852 vsllink_tms_data_len = tms_len & 7;
853 if (vsllink_tms_data_len == 0)
855 vsllink_tms_cmd_pos = NULL;
861 // more shifts will be needed
862 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
863 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
865 num_cycles -= 16 - vsllink_tms_data_len;
866 vsllink_tms_data_len = 0;
867 vsllink_tms_cmd_pos = NULL;
870 // from here vsllink_tms_data_len == 0 or num_cycles == 0
872 if (vsllink_tms_data_len > 0)
878 LOG_ERROR("There MUST be some bugs in the driver");
884 // get number of bytes left to be sent
885 tms_len = num_cycles >> 3;
888 vsllink_tap_ensure_space(1, 5);
889 // if tms_len > 0, vsllink_tms_data_len == 0
890 // so just add new command
891 // LSB of the command byte is the tms value when do the shifting
894 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
898 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
900 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
901 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
902 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
903 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
905 vsllink_usb_in_want_length += 1;
906 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
907 pending_scan_results_length++;
909 if (tms_len > 0xFFFF)
911 vsllink_tap_execute();
916 vsllink_tms_data_len = num_cycles & 7;
917 if (vsllink_tms_data_len > 0)
919 vsllink_tap_ensure_space(0, 3);
920 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
921 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
924 // append '1' for tms
925 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
929 // append '0' for tms
930 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
936 void vsllink_runtest(int num_cycles)
938 tap_state_t saved_end_state = tap_get_end_state();
940 if (tap_get_state() != TAP_IDLE)
942 // enter into IDLE state
943 vsllink_end_state(TAP_IDLE);
944 vsllink_state_move();
947 vsllink_stableclocks(num_cycles, 0);
951 vsllink_end_state(saved_end_state);
952 tap_set_state(TAP_IDLE);
953 if (tap_get_end_state() != TAP_IDLE)
955 vsllink_state_move();
959 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
961 tap_state_t saved_end_state;
962 u8 bits_left, tms_tmp, tdi_len;
970 tdi_len = ((scan_size + 7) >> 3);
971 if ((tdi_len + 7) > VSLLINK_BufferSize)
973 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
977 saved_end_state = tap_get_end_state();
979 /* Move to appropriate scan state */
980 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
982 if (vsllink_tms_data_len > 0)
984 if (tap_get_state() == tap_get_end_state())
986 // already in IRSHIFT or DRSHIFT state
987 // merge tms data in the last tms shift command into next scan command
988 if(*vsllink_tms_cmd_pos < 1)
990 LOG_ERROR("There MUST be some bugs in the driver");
993 else if(*vsllink_tms_cmd_pos < 2)
995 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
996 vsllink_usb_out_buffer_idx--;
1000 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1001 *vsllink_tms_cmd_pos -= 2;
1004 vsllink_tap_ensure_space(1, tdi_len + 7);
1005 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1006 // which is merged from the last tms shift command
1007 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1008 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1009 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1010 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1011 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1013 for (i = 0; i < tdi_len; i++)
1015 buffer[i] >>= 8 - vsllink_tms_data_len;
1018 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1022 vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1023 scan_size -= 8 - vsllink_tms_data_len;
1024 vsllink_tms_data_len = 0;
1028 vsllink_append_tms();
1029 vsllink_tap_ensure_space(1, tdi_len + 5);
1031 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1032 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1033 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1035 vsllink_tap_append_scan(scan_size, buffer, command, 0);
1040 vsllink_tap_ensure_space(1, tdi_len + 7);
1042 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1043 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1044 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1045 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1046 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1048 vsllink_tap_append_scan(scan_size, buffer, command, 8);
1050 vsllink_end_state(saved_end_state);
1052 bits_left = scan_size & 0x07;
1053 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1057 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1061 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1064 if (tap_get_state() != tap_get_end_state())
1066 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1070 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1073 tap_set_state(tap_get_end_state());
1076 void vsllink_reset(int trst, int srst)
1080 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1082 /* Signals are active low */
1083 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1084 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1085 vsllink_usb_out_buffer[2] = 0;
1088 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1092 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1095 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1098 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1102 void vsllink_simple_command(u8 command)
1106 DEBUG_JTAG_IO("0x%02x", command);
1108 vsllink_usb_out_buffer[0] = command;
1109 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1113 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1117 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1120 LOG_ERROR("parameter error, should be one parameter for VID");
1124 vsllink_vid = strtol(args[0], NULL, 0);
1129 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1132 LOG_ERROR("parameter error, should be one parameter for PID");
1136 vsllink_pid = strtol(args[0], NULL, 0);
1141 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1144 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1148 vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
1153 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1156 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1160 vsllink_bulkout = strtol(args[0], NULL, 0);
1165 /***************************************************************************/
1166 /* VSLLink tap functions */
1168 void vsllink_tap_init(void)
1170 vsllink_usb_out_buffer_idx = 0;
1171 vsllink_usb_in_want_length = 0;
1172 pending_scan_results_length = 0;
1175 void vsllink_tap_ensure_space(int scans, int bytes)
1177 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1178 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1180 if (scans > available_scans || bytes > available_bytes)
1182 vsllink_tap_execute();
1186 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
1188 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1193 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1197 vsllink_usb_in_want_length += (length + 7) >> 3;
1199 pending_scan_result->length = length;
1200 pending_scan_result->offset = offset;
1201 pending_scan_result->command = command;
1202 pending_scan_result->buffer = buffer;
1204 for (i = 0; i < ((length + 7) >> 3); i++)
1206 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1209 pending_scan_results_length++;
1212 /* Pad and send a tap sequence to the device, and receive the answer.
1213 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1214 int vsllink_tap_execute(void)
1220 if (vsllink_tms_data_len > 0)
1222 if((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1224 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1227 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1229 // last tms bit is '1'
1230 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1231 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1232 vsllink_tms_data_len = 0;
1236 // last tms bit is '0'
1237 vsllink_usb_out_buffer_idx++;
1238 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1239 vsllink_tms_data_len = 0;
1243 if (vsllink_usb_out_buffer_idx > 3)
1245 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1247 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1248 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1251 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1253 if (result == vsllink_usb_in_want_length)
1255 for (i = 0; i < pending_scan_results_length; i++)
1257 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1258 u8 *buffer = pending_scan_result->buffer;
1259 int length = pending_scan_result->length;
1260 int offset = pending_scan_result->offset;
1261 scan_command_t *command = pending_scan_result->command;
1265 // IRSHIFT or DRSHIFT
1266 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1267 first += (length + offset + 7) >> 3;
1269 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1270 #ifdef _DEBUG_JTAG_IO_
1271 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1274 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1277 return ERROR_JTAG_QUEUE_FAILED;
1280 free(pending_scan_result->buffer);
1281 pending_scan_result->buffer = NULL;
1291 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1292 return ERROR_JTAG_QUEUE_FAILED;
1298 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
1299 vsllink_usb_out_buffer_idx = 3;
1304 /*****************************************************************************/
1305 /* VSLLink USB low-level functions */
1307 vsllink_jtag_t* vsllink_usb_open(void)
1309 struct usb_bus *busses;
1310 struct usb_bus *bus;
1311 struct usb_device *dev;
1313 vsllink_jtag_t *result;
1315 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1321 busses = usb_get_busses();
1323 /* find vsllink_jtag device in usb bus */
1325 for (bus = busses; bus; bus = bus->next)
1327 for (dev = bus->devices; dev; dev = dev->next)
1329 if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
1331 result->usb_handle = usb_open(dev);
1333 /* usb_set_configuration required under win32 */
1334 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1335 usb_claim_interface(result->usb_handle, 0);
1339 * This makes problems under Mac OS X. And is not needed
1340 * under Windows. Hopefully this will not break a linux build
1342 usb_set_altinterface(result->usb_handle, 0);
1353 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1355 usb_close(vsllink_jtag->usb_handle);
1359 /* Send a message and receive the reply. */
1360 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1364 result = vsllink_usb_write(vsllink_jtag, out_length);
1365 if (result == out_length)
1369 result = vsllink_usb_read(vsllink_jtag);
1370 if (result == in_length )
1376 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1384 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1389 /* Write data from out_buffer to USB. */
1390 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1394 if (out_length > VSLLINK_BufferSize)
1396 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1400 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
1401 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1403 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1405 #ifdef _DEBUG_USB_COMMS_
1406 LOG_DEBUG("USB out:");
1407 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1410 #ifdef _VSLLINK_IN_DEBUG_MODE_
1417 /* Read data from USB into in_buffer. */
1418 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1420 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
1421 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1423 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1425 #ifdef _DEBUG_USB_COMMS_
1426 LOG_DEBUG("USB in:");
1427 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1432 #define BYTES_PER_LINE 16
1434 void vsllink_debug_buffer(u8 *buffer, int length)
1441 for (i = 0; i < length; i += BYTES_PER_LINE)
1443 snprintf(line, 5, "%04x", i);
1444 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1446 snprintf(s, 4, " %02x", buffer[j]);