1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
28 #include "replacements.h"
40 #define JLINK_WRITE_ENDPOINT 0x02
41 #define JLINK_READ_ENDPOINT 0x81
43 #define JLINK_USB_TIMEOUT 1000
45 #define JLINK_IN_BUFFER_SIZE 8192
46 #define JLINK_OUT_BUFFER_SIZE 8192
47 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
49 /* Global USB buffers */
50 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
51 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
52 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
54 /* Constants for JLink command */
55 #define EMU_CMD_VERSION 0x01
56 #define EMU_CMD_SET_SPEED 0x05
57 #define EMU_CMD_GET_STATE 0x07
58 #define EMU_CMD_HW_JTAG3 0xcf
59 #define EMU_CMD_HW_RESET0 0xdc
60 #define EMU_CMD_HW_RESET1 0xdd
61 #define EMU_CMD_HW_TRST0 0xde
62 #define EMU_CMD_HW_TRST1 0xdf
64 /* max speed 12MHz v5.0 jlink */
65 #define JLINK_MAX_SPEED 12000
67 /* External interface functions */
68 static int jlink_execute_queue(void);
69 static int jlink_speed(int speed);
70 static int jlink_speed_div(int speed, int* khz);
71 static int jlink_khz(int khz, int *jtag_speed);
72 static int jlink_register_commands(struct command_context_s *cmd_ctx);
73 static int jlink_init(void);
74 static int jlink_quit(void);
76 /* CLI command handler functions */
77 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 /* Queue command functions */
80 static void jlink_end_state(tap_state_t state);
81 static void jlink_state_move(void);
82 static void jlink_path_move(int num_states, tap_state_t *path);
83 static void jlink_runtest(int num_cycles);
84 static void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
85 static void jlink_reset(int trst, int srst);
86 static void jlink_simple_command(u8 command);
87 static int jlink_get_status(void);
89 /* J-Link tap buffer functions */
90 static void jlink_tap_init(void);
91 static int jlink_tap_execute(void);
92 static void jlink_tap_ensure_space(int scans, int bits);
93 static void jlink_tap_append_step(int tms, int tdi);
94 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
96 /* Jlink lowlevel functions */
97 typedef struct jlink_jtag
99 struct usb_dev_handle* usb_handle;
102 static jlink_jtag_t *jlink_usb_open(void);
103 static void jlink_usb_close(jlink_jtag_t *jlink_jtag);
104 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
105 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
106 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size);
107 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
109 /* helper functions */
110 static int jlink_get_version_info(void);
112 #ifdef _DEBUG_USB_COMMS_
113 static void jlink_debug_buffer(u8 *buffer, int length);
116 static jlink_jtag_t* jlink_jtag_handle;
118 /***************************************************************************/
119 /* External interface implementation */
121 jtag_interface_t jlink_interface =
124 .execute_queue = jlink_execute_queue,
125 .speed = jlink_speed,
126 .speed_div = jlink_speed_div,
128 .register_commands = jlink_register_commands,
133 static int jlink_execute_queue(void)
135 jtag_command_t *cmd = jtag_command_queue;
145 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
147 if (cmd->cmd.end_state->end_state != TAP_INVALID)
149 jlink_end_state(cmd->cmd.end_state->end_state);
154 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
155 cmd->cmd.runtest->end_state);
157 if (cmd->cmd.runtest->end_state != TAP_INVALID)
159 jlink_end_state(cmd->cmd.runtest->end_state);
161 jlink_runtest(cmd->cmd.runtest->num_cycles);
165 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
167 if (cmd->cmd.statemove->end_state != TAP_INVALID)
169 jlink_end_state(cmd->cmd.statemove->end_state);
175 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
176 cmd->cmd.pathmove->num_states, \
177 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
179 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
183 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
185 if (cmd->cmd.scan->end_state != TAP_INVALID)
187 jlink_end_state(cmd->cmd.scan->end_state);
190 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
191 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
193 #ifdef _DEBUG_USB_COMMS_
194 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
196 type = jtag_scan_type(cmd->cmd.scan);
197 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
201 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
205 if (cmd->cmd.reset->trst == 1)
207 tap_set_state(TAP_RESET);
209 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
213 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
215 jtag_sleep(cmd->cmd.sleep->us);
219 LOG_ERROR("BUG: unknown JTAG command type encountered");
225 return jlink_tap_execute();
228 /* Sets speed in kHz. */
229 static int jlink_speed(int speed)
233 if (speed <= JLINK_MAX_SPEED)
235 /* check for RTCK setting */
239 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
240 usb_out_buffer[1] = (speed >> 0) & 0xff;
241 usb_out_buffer[2] = (speed >> 8) & 0xff;
243 result = jlink_usb_write(jlink_jtag_handle, 3);
251 LOG_ERROR("J-Link setting speed failed (%d)", result);
252 return ERROR_JTAG_DEVICE_ERROR;
257 LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
263 static int jlink_speed_div(int speed, int* khz)
270 static int jlink_khz(int khz, int *jtag_speed)
277 static int jlink_register_commands(struct command_context_s *cmd_ctx)
279 register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
284 static int jlink_init(void)
288 jlink_jtag_handle = jlink_usb_open();
290 if (jlink_jtag_handle == 0)
292 LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
293 return ERROR_JTAG_INIT_FAILED;
297 while (check_cnt < 3)
299 if (jlink_get_version_info() == ERROR_OK)
301 /* attempt to get status */
311 LOG_INFO("J-Link initial read failed, don't worry");
314 LOG_INFO("J-Link JTAG Interface ready");
322 static int jlink_quit(void)
324 jlink_usb_close(jlink_jtag_handle);
328 /***************************************************************************/
329 /* Queue command implementations */
331 static void jlink_end_state(tap_state_t state)
333 if (tap_is_state_stable(state))
335 tap_set_end_state(state);
339 LOG_ERROR("BUG: %i is not a valid end state", state);
344 /* Goes to the end state. */
345 static void jlink_state_move(void)
349 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
351 for (i = 0; i < 7; i++)
353 tms = (tms_scan >> i) & 1;
354 jlink_tap_append_step(tms, 0);
357 tap_set_state(tap_get_end_state());
360 static void jlink_path_move(int num_states, tap_state_t *path)
364 for (i = 0; i < num_states; i++)
366 if (path[i] == tap_state_transition(tap_get_state(), false))
368 jlink_tap_append_step(0, 0);
370 else if (path[i] == tap_state_transition(tap_get_state(), true))
372 jlink_tap_append_step(1, 0);
376 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
380 tap_set_state(path[i]);
383 tap_set_end_state(tap_get_state());
386 static void jlink_runtest(int num_cycles)
390 tap_state_t saved_end_state = tap_get_end_state();
392 /* only do a state_move when we're not already in IDLE */
393 if (tap_get_state() != TAP_IDLE)
395 jlink_end_state(TAP_IDLE);
399 /* execute num_cycles */
400 for (i = 0; i < num_cycles; i++)
402 jlink_tap_append_step(0, 0);
405 /* finish in end_state */
406 jlink_end_state(saved_end_state);
407 if (tap_get_state() != tap_get_end_state())
413 static void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
415 tap_state_t saved_end_state;
417 jlink_tap_ensure_space(1, scan_size + 8);
419 saved_end_state = tap_get_end_state();
421 /* Move to appropriate scan state */
422 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
424 /* Only move if we're not already there */
425 if (tap_get_state() != tap_get_end_state())
428 jlink_end_state(saved_end_state);
431 jlink_tap_append_scan(scan_size, buffer, command);
433 /* We are in Exit1, go to Pause */
434 jlink_tap_append_step(0, 0);
436 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
438 if (tap_get_state() != tap_get_end_state())
444 static void jlink_reset(int trst, int srst)
446 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
448 /* Signals are active low */
451 jlink_simple_command(EMU_CMD_HW_RESET1);
455 jlink_simple_command(EMU_CMD_HW_RESET0);
460 jlink_simple_command(EMU_CMD_HW_TRST1);
464 jlink_simple_command(EMU_CMD_HW_TRST0);
468 static void jlink_simple_command(u8 command)
472 DEBUG_JTAG_IO("0x%02x", command);
474 usb_out_buffer[0] = command;
475 result = jlink_usb_write(jlink_jtag_handle, 1);
479 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
483 static int jlink_get_status(void)
487 jlink_simple_command(EMU_CMD_GET_STATE);
488 result = jlink_usb_read(jlink_jtag_handle, 8);
492 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
493 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
494 vref / 1000, vref % 1000, \
495 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
496 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
500 LOG_ERROR("Vref too low. Check Target Power\n");
505 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
511 static int jlink_get_version_info(void)
516 /* query hardware version */
517 jlink_simple_command(EMU_CMD_VERSION);
518 result = jlink_usb_read(jlink_jtag_handle, 2);
522 len = buf_get_u32(usb_in_buffer, 0, 16);
523 result = jlink_usb_read(jlink_jtag_handle, len);
527 usb_in_buffer[result] = 0;
528 LOG_INFO("%s", (char *)usb_in_buffer);
533 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
534 return ERROR_JTAG_DEVICE_ERROR;
537 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
539 if (jlink_get_version_info() == ERROR_OK)
541 /* attempt to get status */
548 /***************************************************************************/
549 /* J-Link tap functions */
551 /* 2048 is the max value we can use here */
552 #define JLINK_TAP_BUFFER_SIZE 2048
554 static int tap_length;
555 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
556 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
557 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
561 int first; /* First bit position in tdo_buffer to read */
562 int length; /* Number of bits to read */
563 scan_command_t *command; /* Corresponding scan command */
565 } pending_scan_result_t;
567 #define MAX_PENDING_SCAN_RESULTS 256
569 static int pending_scan_results_length;
570 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
574 static void jlink_tap_init(void)
577 pending_scan_results_length = 0;
580 static void jlink_tap_ensure_space(int scans, int bits)
582 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
583 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
585 if (scans > available_scans || bits > available_bits)
591 static void jlink_tap_append_step(int tms, int tdi)
594 int index = tap_length / 8;
596 if (index < JLINK_TAP_BUFFER_SIZE)
598 int bit_index = tap_length % 8;
599 u8 bit = 1 << bit_index;
603 tms_buffer[index] |= bit;
607 tms_buffer[index] &= ~bit;
612 tdi_buffer[index] |= bit;
616 tdi_buffer[index] &= ~bit;
623 LOG_ERROR("jlink_tap_append_step, overflow");
627 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
629 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
632 pending_scan_result->first = tap_length;
633 pending_scan_result->length = length;
634 pending_scan_result->command = command;
635 pending_scan_result->buffer = buffer;
637 for (i = 0; i < length; i++)
639 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
641 pending_scan_results_length++;
644 /* Pad and send a tap sequence to the device, and receive the answer.
645 * For the purpose of padding we assume that we are in idle or pause state. */
646 static int jlink_tap_execute(void)
656 /* Pad last byte so that tap_length is divisible by 8 */
657 while (tap_length % 8 != 0)
659 /* More of the last TMS value keeps us in the same state,
660 * analogous to free-running JTAG interfaces. */
661 jlink_tap_append_step(last_tms, 0);
664 byte_length = tap_length / 8;
666 usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
667 usb_out_buffer[1] = 0;
668 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
669 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
672 for (i = 0; i < byte_length; i++)
674 usb_out_buffer[tms_offset + i] = tms_buffer[i];
677 tdi_offset = tms_offset + byte_length;
678 for (i = 0; i < byte_length; i++)
680 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
683 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
685 if (result == byte_length)
687 for (i = 0; i < byte_length; i++)
689 tdo_buffer[i] = usb_in_buffer[i];
692 for (i = 0; i < pending_scan_results_length; i++)
694 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
695 u8 *buffer = pending_scan_result->buffer;
696 int length = pending_scan_result->length;
697 int first = pending_scan_result->first;
698 scan_command_t *command = pending_scan_result->command;
701 buf_set_buf(tdo_buffer, first, buffer, 0, length);
703 DEBUG_JTAG_IO("pending scan result, length = %d", length);
705 #ifdef _DEBUG_USB_COMMS_
706 jlink_debug_buffer(buffer, byte_length);
709 if (jtag_read_buffer(buffer, command) != ERROR_OK)
712 return ERROR_JTAG_QUEUE_FAILED;
715 if (pending_scan_result->buffer != NULL)
717 free(pending_scan_result->buffer);
723 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
724 return ERROR_JTAG_QUEUE_FAILED;
733 /*****************************************************************************/
734 /* JLink USB low-level functions */
736 static jlink_jtag_t* jlink_usb_open()
738 struct usb_bus *busses;
740 struct usb_device *dev;
742 jlink_jtag_t *result;
744 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
750 busses = usb_get_busses();
752 /* find jlink_jtag device in usb bus */
754 for (bus = busses; bus; bus = bus->next)
756 for (dev = bus->devices; dev; dev = dev->next)
758 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
760 result->usb_handle = usb_open(dev);
762 /* usb_set_configuration required under win32 */
763 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
764 usb_claim_interface(result->usb_handle, 0);
768 * This makes problems under Mac OS X. And is not needed
769 * under Windows. Hopefully this will not break a linux build
771 usb_set_altinterface(result->usb_handle, 0);
782 static void jlink_usb_close(jlink_jtag_t *jlink_jtag)
784 usb_close(jlink_jtag->usb_handle);
788 /* Send a message and receive the reply. */
789 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
794 result = jlink_usb_write(jlink_jtag, out_length);
795 if (result == out_length)
797 result = jlink_usb_read(jlink_jtag, in_length);
798 if (result == in_length || result == in_length+1)
800 if (result == in_length)
802 /* Must read the result from the EMU too */
803 result2 = jlink_usb_read_emu_result(jlink_jtag);
806 /* Check the result itself */
807 if (0 == usb_emu_result_buffer[0])
813 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_emu_result_buffer[0]);
819 LOG_ERROR("jlink_usb_read_emu_result len (requested=1, result=%d)", result2);
825 /* Check the result itself */
826 if (0 == usb_in_buffer[result-1])
832 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_in_buffer[result]);
839 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
845 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
850 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
851 static int usb_bulk_with_retries(
852 int (*f)(usb_dev_handle *, int, char *, int, int),
853 usb_dev_handle *dev, int ep,
854 char *bytes, int size, int timeout)
856 int rc = 0, tries = 3, this_size;
858 while (tries && size) {
860 this_size = f(dev, ep, bytes, size, timeout);
872 static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
873 char *bytes, int size, int timeout)
875 return usb_bulk_with_retries(&usb_bulk_write,
876 dev, ep, bytes, size, timeout);
878 static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
879 char *bytes, int size, int timeout)
881 return usb_bulk_with_retries(&usb_bulk_read,
882 dev, ep, bytes, size, timeout);
885 /* Write data from out_buffer to USB. */
886 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
890 if (out_length > JLINK_OUT_BUFFER_SIZE)
892 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
896 result = usb_bulk_write_ex(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT,
897 (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
899 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
901 #ifdef _DEBUG_USB_COMMS_
902 jlink_debug_buffer(usb_out_buffer, out_length);
907 /* Read data from USB into in_buffer. */
908 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size)
910 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT,
911 (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
913 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
915 #ifdef _DEBUG_USB_COMMS_
916 jlink_debug_buffer(usb_in_buffer, result);
921 /* Read the result from the previous EMU cmd into result_buffer. */
922 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
924 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT,
925 (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
928 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
930 #ifdef _DEBUG_USB_COMMS_
931 jlink_debug_buffer(usb_emu_result_buffer, result);
936 #ifdef _DEBUG_USB_COMMS_
937 #define BYTES_PER_LINE 16
939 static void jlink_debug_buffer(u8 *buffer, int length)
946 for (i = 0; i < length; i += BYTES_PER_LINE)
948 snprintf(line, 5, "%04x", i);
949 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
951 snprintf(s, 4, " %02x", buffer[j]);
954 LOG_DEBUG("%s", line);