1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include "replacements.h"
34 /* enable this to debug communication
37 #define _DEBUG_USB_COMMS_
40 #ifdef _DEBUG_JTAG_IO_
41 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
43 #define DEBUG_JTAG_IO(expr ...)
49 #define JLINK_WRITE_ENDPOINT 0x02
50 #define JLINK_READ_ENDPOINT 0x81
52 #define JLINK_USB_TIMEOUT 100
54 #define JLINK_IN_BUFFER_SIZE 8192
55 #define JLINK_OUT_BUFFER_SIZE 8192
56 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
59 /* Global USB buffers */
60 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
61 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
62 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
64 /* Constants for JLink command */
65 #define EMU_CMD_VERSION 0x01
66 #define EMU_CMD_SET_SPEED 0x05
67 #define EMU_CMD_GET_STATE 0x07
68 #define EMU_CMD_HW_JTAG3 0xcf
69 #define EMU_CMD_HW_RESET0 0xdc
70 #define EMU_CMD_HW_RESET1 0xdd
71 #define EMU_CMD_HW_TRST0 0xde
72 #define EMU_CMD_HW_TRST1 0xdf
74 /* max speed 12MHz v5.0 jlink */
75 #define JLINK_MAX_SPEED 12000
77 /* External interface functions */
78 int jlink_execute_queue(void);
79 int jlink_speed(int speed);
80 int jlink_khz(int khz, int *jtag_speed);
81 int jlink_register_commands(struct command_context_s *cmd_ctx);
85 /* CLI command handler functions */
86 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
88 /* Queue command functions */
89 void jlink_end_state(enum tap_state state);
90 void jlink_state_move(void);
91 void jlink_path_move(int num_states, enum tap_state *path);
92 void jlink_runtest(int num_cycles);
93 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
94 void jlink_reset(int trst, int srst);
95 void jlink_simple_command(u8 command);
96 int jlink_get_status(void);
98 /* J-Link tap buffer functions */
99 void jlink_tap_init();
100 int jlink_tap_execute();
101 void jlink_tap_ensure_space(int scans, int bits);
102 void jlink_tap_append_step(int tms, int tdi);
103 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
105 /* Jlink lowlevel functions */
106 typedef struct jlink_jtag
108 struct usb_dev_handle* usb_handle;
111 jlink_jtag_t *jlink_usb_open(void);
112 void jlink_usb_close(jlink_jtag_t *jlink_jtag);
113 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
114 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
115 int jlink_usb_read(jlink_jtag_t *jlink_jtag);
116 int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
118 /* helper functions */
119 int jlink_get_version_info(void);
121 #ifdef _DEBUG_USB_COMMS_
122 void jlink_debug_buffer(u8 *buffer, int length);
125 jlink_jtag_t* jlink_jtag_handle;
127 /***************************************************************************/
128 /* External interface implementation */
130 jtag_interface_t jlink_interface =
133 .execute_queue = jlink_execute_queue,
134 .speed = jlink_speed,
136 .register_commands = jlink_register_commands,
141 int jlink_execute_queue(void)
143 jtag_command_t *cmd = jtag_command_queue;
153 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
155 if (cmd->cmd.end_state->end_state != -1)
157 jlink_end_state(cmd->cmd.end_state->end_state);
162 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
163 cmd->cmd.runtest->end_state);
165 if (cmd->cmd.runtest->end_state != -1)
167 jlink_end_state(cmd->cmd.runtest->end_state);
169 jlink_runtest(cmd->cmd.runtest->num_cycles);
173 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
175 if (cmd->cmd.statemove->end_state != -1)
177 jlink_end_state(cmd->cmd.statemove->end_state);
183 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
184 cmd->cmd.pathmove->num_states, \
185 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
187 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
191 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
193 if (cmd->cmd.scan->end_state != -1)
195 jlink_end_state(cmd->cmd.scan->end_state);
198 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
199 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
201 #ifdef _DEBUG_USB_COMMS_
202 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
204 type = jtag_scan_type(cmd->cmd.scan);
205 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
209 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
213 if (cmd->cmd.reset->trst == 1)
217 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
221 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
223 jtag_sleep(cmd->cmd.sleep->us);
227 LOG_ERROR("BUG: unknown JTAG command type encountered");
233 return jlink_tap_execute();
236 /* Sets speed in kHz. */
237 int jlink_speed(int speed)
241 if (speed <= JLINK_MAX_SPEED)
243 /* check for RTCK setting */
247 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
248 usb_out_buffer[1] = (speed >> 0) & 0xff;
249 usb_out_buffer[2] = (speed >> 8) & 0xff;
251 result = jlink_usb_write(jlink_jtag_handle, 3);
259 LOG_ERROR("J-Link setting speed failed (%d)", result);
260 return ERROR_JTAG_DEVICE_ERROR;
265 LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
271 int jlink_khz(int khz, int *jtag_speed)
278 int jlink_register_commands(struct command_context_s *cmd_ctx)
280 register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
289 jlink_jtag_handle = jlink_usb_open();
291 if (jlink_jtag_handle == 0)
293 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
294 return ERROR_JTAG_INIT_FAILED;
298 while (check_cnt < 3)
300 if (jlink_get_version_info() == ERROR_OK)
302 /* attempt to get status */
312 LOG_INFO("J-Link initial read failed, don't worry");
315 LOG_INFO("J-Link JTAG Interface ready");
325 jlink_usb_close(jlink_jtag_handle);
329 /***************************************************************************/
330 /* Queue command implementations */
332 void jlink_end_state(enum tap_state state)
334 if (tap_move_map[state] != -1)
340 LOG_ERROR("BUG: %i is not a valid end state", state);
345 /* Goes to the end state. */
346 void jlink_state_move(void)
350 u8 tms_scan = TAP_MOVE(cur_state, end_state);
352 for (i = 0; i < 7; i++)
354 tms = (tms_scan >> i) & 1;
355 jlink_tap_append_step(tms, 0);
358 cur_state = end_state;
361 void jlink_path_move(int num_states, enum tap_state *path)
365 for (i = 0; i < num_states; i++)
367 if (path[i] == tap_transitions[cur_state].low)
369 jlink_tap_append_step(0, 0);
371 else if (path[i] == tap_transitions[cur_state].high)
373 jlink_tap_append_step(1, 0);
377 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
384 end_state = cur_state;
387 void jlink_runtest(int num_cycles)
391 enum tap_state saved_end_state = end_state;
393 /* only do a state_move when we're not already in RTI */
394 if (cur_state != TAP_RTI)
396 jlink_end_state(TAP_RTI);
400 /* execute num_cycles */
401 for (i = 0; i < num_cycles; i++)
403 jlink_tap_append_step(0, 0);
406 /* finish in end_state */
407 jlink_end_state(saved_end_state);
408 if (cur_state != end_state)
414 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
416 enum tap_state saved_end_state;
418 jlink_tap_ensure_space(1, scan_size + 8);
420 saved_end_state = end_state;
422 /* Move to appropriate scan state */
423 jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
426 jlink_end_state(saved_end_state);
429 jlink_tap_append_scan(scan_size, buffer, command);
431 /* We are in Exit1, go to Pause */
432 jlink_tap_append_step(0, 0);
434 cur_state = ir_scan ? TAP_PI : TAP_PD;
436 if (cur_state != end_state)
442 void jlink_reset(int trst, int srst)
444 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
446 /* Signals are active low */
449 jlink_simple_command(EMU_CMD_HW_RESET1);
453 jlink_simple_command(EMU_CMD_HW_RESET0);
458 jlink_simple_command(EMU_CMD_HW_TRST1);
462 jlink_simple_command(EMU_CMD_HW_TRST0);
466 void jlink_simple_command(u8 command)
470 DEBUG_JTAG_IO("0x%02x", command);
472 usb_out_buffer[0] = command;
473 result = jlink_usb_write(jlink_jtag_handle, 1);
477 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
481 int jlink_get_status(void)
485 jlink_simple_command(EMU_CMD_GET_STATE);
486 result = jlink_usb_read(jlink_jtag_handle);
490 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
491 LOG_INFO("Vref = %d.%d TCK=%d TDI=%d TDO=%d TMS=%d SRST=%d TRST=%d\n", \
492 vref / 1000, vref % 1000, \
493 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
494 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
498 LOG_ERROR("Vref too low. Eventually the target isn't powered or disconnected?\n");
503 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
509 int jlink_get_version_info(void)
514 /* query hardware version */
515 jlink_simple_command(EMU_CMD_VERSION);
516 result = jlink_usb_read(jlink_jtag_handle);
520 len = buf_get_u32(usb_in_buffer, 0, 16);
521 result = jlink_usb_read(jlink_jtag_handle);
525 usb_in_buffer[result] = 0;
526 LOG_INFO(usb_in_buffer);
531 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
532 return ERROR_JTAG_DEVICE_ERROR;
535 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
537 if (jlink_get_version_info() == ERROR_OK)
539 /* attempt to get status */
546 /***************************************************************************/
547 /* J-Link tap functions */
549 /* 2048 is the max value we can use here */
550 #define JLINK_TAP_BUFFER_SIZE 2048
552 static int tap_length;
553 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
554 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
555 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
559 int first; /* First bit position in tdo_buffer to read */
560 int length; /* Number of bits to read */
561 scan_command_t *command; /* Corresponding scan command */
563 } pending_scan_result_t;
565 #define MAX_PENDING_SCAN_RESULTS 256
567 static int pending_scan_results_length;
568 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
572 void jlink_tap_init()
575 pending_scan_results_length = 0;
578 void jlink_tap_ensure_space(int scans, int bits)
580 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
581 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
583 if (scans > available_scans || bits > available_bits)
589 void jlink_tap_append_step(int tms, int tdi)
592 int index = tap_length / 8;
594 if (index < JLINK_TAP_BUFFER_SIZE)
596 int bit_index = tap_length % 8;
597 u8 bit = 1 << bit_index;
601 tms_buffer[index] |= bit;
605 tms_buffer[index] &= ~bit;
610 tdi_buffer[index] |= bit;
614 tdi_buffer[index] &= ~bit;
621 LOG_ERROR("jlink_tap_append_step, overflow");
625 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
627 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
630 pending_scan_result->first = tap_length;
631 pending_scan_result->length = length;
632 pending_scan_result->command = command;
633 pending_scan_result->buffer = buffer;
635 for (i = 0; i < length; i++)
637 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
639 pending_scan_results_length++;
642 /* Pad and send a tap sequence to the device, and receive the answer.
643 * For the purpose of padding we assume that we are in idle or pause state. */
644 int jlink_tap_execute()
654 /* Pad last byte so that tap_length is divisible by 8 */
655 while (tap_length % 8 != 0)
657 /* More of the last TMS value keeps us in the same state,
658 * analogous to free-running JTAG interfaces. */
659 jlink_tap_append_step(last_tms, 0);
662 byte_length = tap_length / 8;
664 usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
665 usb_out_buffer[1] = 0;
666 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
667 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
670 for (i = 0; i < byte_length; i++)
672 usb_out_buffer[tms_offset + i] = tms_buffer[i];
675 tdi_offset = tms_offset + byte_length;
676 for (i = 0; i < byte_length; i++)
678 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
681 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
683 if (result == byte_length)
685 for (i = 0; i < byte_length; i++)
687 tdo_buffer[i] = usb_in_buffer[i];
690 for (i = 0; i < pending_scan_results_length; i++)
692 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
693 u8 *buffer = pending_scan_result->buffer;
694 int length = pending_scan_result->length;
695 int first = pending_scan_result->first;
696 scan_command_t *command = pending_scan_result->command;
699 buf_set_buf(tdo_buffer, first, buffer, 0, length);
701 DEBUG_JTAG_IO("pending scan result, length = %d", length);
703 #ifdef _DEBUG_USB_COMMS_
704 jlink_debug_buffer(buffer, byte_length);
707 if (jtag_read_buffer(buffer, command) != ERROR_OK)
710 return ERROR_JTAG_QUEUE_FAILED;
713 if (pending_scan_result->buffer != NULL)
715 free(pending_scan_result->buffer);
721 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
722 return ERROR_JTAG_QUEUE_FAILED;
731 /*****************************************************************************/
732 /* JLink USB low-level functions */
734 jlink_jtag_t* jlink_usb_open()
736 struct usb_bus *busses;
738 struct usb_device *dev;
740 jlink_jtag_t *result;
742 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
748 busses = usb_get_busses();
750 /* find jlink_jtag device in usb bus */
752 for (bus = busses; bus; bus = bus->next)
754 for (dev = bus->devices; dev; dev = dev->next)
756 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
758 result->usb_handle = usb_open(dev);
760 /* usb_set_configuration required under win32 */
761 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
762 usb_claim_interface(result->usb_handle, 0);
766 * This makes problems under Mac OS X. And is not needed
767 * under Windows. Hopefully this will not break a linux build
769 usb_set_altinterface(result->usb_handle, 0);
780 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
782 usb_close(jlink_jtag->usb_handle);
786 /* Send a message and receive the reply. */
787 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
792 result = jlink_usb_write(jlink_jtag, out_length);
793 if (result == out_length)
795 result = jlink_usb_read(jlink_jtag);
796 if (result == in_length || result == in_length+1)
798 if (result == in_length)
800 /* Must read the result from the EMU too */
801 result2 = jlink_usb_read_emu_result(jlink_jtag);
804 /* Check the result itself */
805 if (0 == usb_emu_result_buffer[0])
811 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_emu_result_buffer[0]);
817 LOG_ERROR("jlink_usb_read_emu_result len (requested=1, result=%d)", result2);
823 /* Check the result itself */
824 if (0 == usb_in_buffer[result])
830 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_in_buffer[result]);
837 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
843 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
848 /* Write data from out_buffer to USB. */
849 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
853 if (out_length > JLINK_OUT_BUFFER_SIZE)
855 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
859 result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
860 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
862 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
864 #ifdef _DEBUG_USB_COMMS_
865 jlink_debug_buffer(usb_out_buffer, out_length);
870 /* Read data from USB into in_buffer. */
871 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
873 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
874 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
876 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
878 #ifdef _DEBUG_USB_COMMS_
879 jlink_debug_buffer(usb_in_buffer, result);
884 /* Read the result from the previous EMU cmd into result_buffer. */
885 int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
887 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
888 usb_emu_result_buffer, JLINK_EMU_RESULT_BUFFER_SIZE, JLINK_USB_TIMEOUT);
890 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
892 #ifdef _DEBUG_USB_COMMS_
893 jlink_debug_buffer(usb_emu_result_buffer, result);
898 #ifdef _DEBUG_USB_COMMS_
899 #define BYTES_PER_LINE 16
901 void jlink_debug_buffer(u8 *buffer, int length)
908 for (i = 0; i < length; i += BYTES_PER_LINE)
910 snprintf(line, 5, "%04x", i);
911 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
913 snprintf(s, 4, " %02x", buffer[j]);