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
58 /* Global USB buffers */
59 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
60 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
61 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
63 /* Constants for JLink command */
64 #define EMU_CMD_VERSION 0x01
65 #define EMU_CMD_SET_SPEED 0x05
66 #define EMU_CMD_GET_STATE 0x07
67 #define EMU_CMD_HW_JTAG3 0xcf
68 #define EMU_CMD_HW_RESET0 0xdc
69 #define EMU_CMD_HW_RESET1 0xdd
70 #define EMU_CMD_HW_TRST0 0xde
71 #define EMU_CMD_HW_TRST1 0xdf
73 /* max speed 12MHz v5.0 jlink */
74 #define JLINK_MAX_SPEED 12000
76 /* External interface functions */
77 int jlink_execute_queue(void);
78 int jlink_speed(int speed);
79 int jlink_khz(int khz, int *jtag_speed);
80 int jlink_register_commands(struct command_context_s *cmd_ctx);
84 /* CLI command handler functions */
85 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
87 /* Queue command functions */
88 void jlink_end_state(enum tap_state state);
89 void jlink_state_move(void);
90 void jlink_path_move(int num_states, enum tap_state *path);
91 void jlink_runtest(int num_cycles);
92 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
93 void jlink_reset(int trst, int srst);
94 void jlink_simple_command(u8 command);
95 int jlink_get_status(void);
97 /* J-Link tap buffer functions */
98 void jlink_tap_init(void);
99 int jlink_tap_execute(void);
100 void jlink_tap_ensure_space(int scans, int bits);
101 void jlink_tap_append_step(int tms, int tdi);
102 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
104 /* Jlink lowlevel functions */
105 typedef struct jlink_jtag
107 struct usb_dev_handle* usb_handle;
110 jlink_jtag_t *jlink_usb_open(void);
111 void jlink_usb_close(jlink_jtag_t *jlink_jtag);
112 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
113 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
114 int jlink_usb_read(jlink_jtag_t *jlink_jtag);
115 int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
117 /* helper functions */
118 int jlink_get_version_info(void);
120 #ifdef _DEBUG_USB_COMMS_
121 void jlink_debug_buffer(u8 *buffer, int length);
124 jlink_jtag_t* jlink_jtag_handle;
126 /***************************************************************************/
127 /* External interface implementation */
129 jtag_interface_t jlink_interface =
132 .execute_queue = jlink_execute_queue,
133 .speed = jlink_speed,
135 .register_commands = jlink_register_commands,
140 int jlink_execute_queue(void)
142 jtag_command_t *cmd = jtag_command_queue;
152 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
154 if (cmd->cmd.end_state->end_state != -1)
156 jlink_end_state(cmd->cmd.end_state->end_state);
161 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
162 cmd->cmd.runtest->end_state);
164 if (cmd->cmd.runtest->end_state != -1)
166 jlink_end_state(cmd->cmd.runtest->end_state);
168 jlink_runtest(cmd->cmd.runtest->num_cycles);
172 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
174 if (cmd->cmd.statemove->end_state != -1)
176 jlink_end_state(cmd->cmd.statemove->end_state);
182 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
183 cmd->cmd.pathmove->num_states, \
184 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
186 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
190 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
192 if (cmd->cmd.scan->end_state != -1)
194 jlink_end_state(cmd->cmd.scan->end_state);
197 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
198 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
200 #ifdef _DEBUG_USB_COMMS_
201 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
203 type = jtag_scan_type(cmd->cmd.scan);
204 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
208 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
212 if (cmd->cmd.reset->trst == 1)
216 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
220 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
222 jtag_sleep(cmd->cmd.sleep->us);
226 LOG_ERROR("BUG: unknown JTAG command type encountered");
232 return jlink_tap_execute();
235 /* Sets speed in kHz. */
236 int jlink_speed(int speed)
240 if (speed <= JLINK_MAX_SPEED)
242 /* check for RTCK setting */
246 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
247 usb_out_buffer[1] = (speed >> 0) & 0xff;
248 usb_out_buffer[2] = (speed >> 8) & 0xff;
250 result = jlink_usb_write(jlink_jtag_handle, 3);
258 LOG_ERROR("J-Link setting speed failed (%d)", result);
259 return ERROR_JTAG_DEVICE_ERROR;
264 LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
270 int jlink_khz(int khz, int *jtag_speed)
277 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,
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");
324 jlink_usb_close(jlink_jtag_handle);
328 /***************************************************************************/
329 /* Queue command implementations */
331 void jlink_end_state(enum tap_state state)
333 if (tap_move_map[state] != -1)
339 LOG_ERROR("BUG: %i is not a valid end state", state);
344 /* Goes to the end state. */
345 void jlink_state_move(void)
349 u8 tms_scan = TAP_MOVE(cur_state, end_state);
351 for (i = 0; i < 7; i++)
353 tms = (tms_scan >> i) & 1;
354 jlink_tap_append_step(tms, 0);
357 cur_state = end_state;
360 void jlink_path_move(int num_states, enum tap_state *path)
364 for (i = 0; i < num_states; i++)
366 if (path[i] == tap_transitions[cur_state].low)
368 jlink_tap_append_step(0, 0);
370 else if (path[i] == tap_transitions[cur_state].high)
372 jlink_tap_append_step(1, 0);
376 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
383 end_state = cur_state;
386 void jlink_runtest(int num_cycles)
390 enum tap_state saved_end_state = end_state;
392 /* only do a state_move when we're not already in RTI */
393 if (cur_state != TAP_RTI)
395 jlink_end_state(TAP_RTI);
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 (cur_state != end_state)
413 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
415 enum tap_state saved_end_state;
417 jlink_tap_ensure_space(1, scan_size + 8);
419 saved_end_state = end_state;
421 /* Move to appropriate scan state */
422 jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
425 jlink_end_state(saved_end_state);
428 jlink_tap_append_scan(scan_size, buffer, command);
430 /* We are in Exit1, go to Pause */
431 jlink_tap_append_step(0, 0);
433 cur_state = ir_scan ? TAP_PI : TAP_PD;
435 if (cur_state != end_state)
441 void jlink_reset(int trst, int srst)
443 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
445 /* Signals are active low */
448 jlink_simple_command(EMU_CMD_HW_RESET1);
452 jlink_simple_command(EMU_CMD_HW_RESET0);
457 jlink_simple_command(EMU_CMD_HW_TRST1);
461 jlink_simple_command(EMU_CMD_HW_TRST0);
465 void jlink_simple_command(u8 command)
469 DEBUG_JTAG_IO("0x%02x", command);
471 usb_out_buffer[0] = command;
472 result = jlink_usb_write(jlink_jtag_handle, 1);
476 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
480 int jlink_get_status(void)
484 jlink_simple_command(EMU_CMD_GET_STATE);
485 result = jlink_usb_read(jlink_jtag_handle);
489 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
490 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
491 vref / 1000, vref % 1000, \
492 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
493 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
497 LOG_ERROR("Vref too low. Check Target Power\n");
502 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
508 int jlink_get_version_info(void)
513 /* query hardware version */
514 jlink_simple_command(EMU_CMD_VERSION);
515 result = jlink_usb_read(jlink_jtag_handle);
519 len = buf_get_u32(usb_in_buffer, 0, 16);
520 result = jlink_usb_read(jlink_jtag_handle);
524 usb_in_buffer[result] = 0;
525 LOG_INFO(usb_in_buffer);
530 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
531 return ERROR_JTAG_DEVICE_ERROR;
534 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
536 if (jlink_get_version_info() == ERROR_OK)
538 /* attempt to get status */
545 /***************************************************************************/
546 /* J-Link tap functions */
548 /* 2048 is the max value we can use here */
549 #define JLINK_TAP_BUFFER_SIZE 2048
551 static int tap_length;
552 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
553 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
554 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
558 int first; /* First bit position in tdo_buffer to read */
559 int length; /* Number of bits to read */
560 scan_command_t *command; /* Corresponding scan command */
562 } pending_scan_result_t;
564 #define MAX_PENDING_SCAN_RESULTS 256
566 static int pending_scan_results_length;
567 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
571 void jlink_tap_init(void)
574 pending_scan_results_length = 0;
577 void jlink_tap_ensure_space(int scans, int bits)
579 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
580 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
582 if (scans > available_scans || bits > available_bits)
588 void jlink_tap_append_step(int tms, int tdi)
591 int index = tap_length / 8;
593 if (index < JLINK_TAP_BUFFER_SIZE)
595 int bit_index = tap_length % 8;
596 u8 bit = 1 << bit_index;
600 tms_buffer[index] |= bit;
604 tms_buffer[index] &= ~bit;
609 tdi_buffer[index] |= bit;
613 tdi_buffer[index] &= ~bit;
620 LOG_ERROR("jlink_tap_append_step, overflow");
624 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
626 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
629 pending_scan_result->first = tap_length;
630 pending_scan_result->length = length;
631 pending_scan_result->command = command;
632 pending_scan_result->buffer = buffer;
634 for (i = 0; i < length; i++)
636 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
638 pending_scan_results_length++;
641 /* Pad and send a tap sequence to the device, and receive the answer.
642 * For the purpose of padding we assume that we are in idle or pause state. */
643 int jlink_tap_execute(void)
653 /* Pad last byte so that tap_length is divisible by 8 */
654 while (tap_length % 8 != 0)
656 /* More of the last TMS value keeps us in the same state,
657 * analogous to free-running JTAG interfaces. */
658 jlink_tap_append_step(last_tms, 0);
661 byte_length = tap_length / 8;
663 usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
664 usb_out_buffer[1] = 0;
665 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
666 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
669 for (i = 0; i < byte_length; i++)
671 usb_out_buffer[tms_offset + i] = tms_buffer[i];
674 tdi_offset = tms_offset + byte_length;
675 for (i = 0; i < byte_length; i++)
677 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
680 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
682 if (result == byte_length)
684 for (i = 0; i < byte_length; i++)
686 tdo_buffer[i] = usb_in_buffer[i];
689 for (i = 0; i < pending_scan_results_length; i++)
691 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
692 u8 *buffer = pending_scan_result->buffer;
693 int length = pending_scan_result->length;
694 int first = pending_scan_result->first;
695 scan_command_t *command = pending_scan_result->command;
698 buf_set_buf(tdo_buffer, first, buffer, 0, length);
700 DEBUG_JTAG_IO("pending scan result, length = %d", length);
702 #ifdef _DEBUG_USB_COMMS_
703 jlink_debug_buffer(buffer, byte_length);
706 if (jtag_read_buffer(buffer, command) != ERROR_OK)
709 return ERROR_JTAG_QUEUE_FAILED;
712 if (pending_scan_result->buffer != NULL)
714 free(pending_scan_result->buffer);
720 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
721 return ERROR_JTAG_QUEUE_FAILED;
730 /*****************************************************************************/
731 /* JLink USB low-level functions */
733 jlink_jtag_t* jlink_usb_open()
735 struct usb_bus *busses;
737 struct usb_device *dev;
739 jlink_jtag_t *result;
741 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
747 busses = usb_get_busses();
749 /* find jlink_jtag device in usb bus */
751 for (bus = busses; bus; bus = bus->next)
753 for (dev = bus->devices; dev; dev = dev->next)
755 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
757 result->usb_handle = usb_open(dev);
759 /* usb_set_configuration required under win32 */
760 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
761 usb_claim_interface(result->usb_handle, 0);
765 * This makes problems under Mac OS X. And is not needed
766 * under Windows. Hopefully this will not break a linux build
768 usb_set_altinterface(result->usb_handle, 0);
779 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
781 usb_close(jlink_jtag->usb_handle);
785 /* Send a message and receive the reply. */
786 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
791 result = jlink_usb_write(jlink_jtag, out_length);
792 if (result == out_length)
794 result = jlink_usb_read(jlink_jtag);
795 if (result == in_length || result == in_length+1)
797 if (result == in_length)
799 /* Must read the result from the EMU too */
800 result2 = jlink_usb_read_emu_result(jlink_jtag);
803 /* Check the result itself */
804 if (0 == usb_emu_result_buffer[0])
810 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_emu_result_buffer[0]);
816 LOG_ERROR("jlink_usb_read_emu_result len (requested=1, result=%d)", result2);
822 /* Check the result itself */
823 if (0 == usb_in_buffer[result-1])
829 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_in_buffer[result]);
836 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
842 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
847 /* Write data from out_buffer to USB. */
848 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
852 if (out_length > JLINK_OUT_BUFFER_SIZE)
854 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
858 result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
859 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
861 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
863 #ifdef _DEBUG_USB_COMMS_
864 jlink_debug_buffer(usb_out_buffer, out_length);
869 /* Read data from USB into in_buffer. */
870 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
872 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
873 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
875 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
877 #ifdef _DEBUG_USB_COMMS_
878 jlink_debug_buffer(usb_in_buffer, result);
883 /* Read the result from the previous EMU cmd into result_buffer. */
884 int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
886 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
887 usb_emu_result_buffer, JLINK_EMU_RESULT_BUFFER_SIZE, JLINK_USB_TIMEOUT);
889 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
891 #ifdef _DEBUG_USB_COMMS_
892 jlink_debug_buffer(usb_emu_result_buffer, result);
897 #ifdef _DEBUG_USB_COMMS_
898 #define BYTES_PER_LINE 16
900 void jlink_debug_buffer(u8 *buffer, int length)
907 for (i = 0; i < length; i += BYTES_PER_LINE)
909 snprintf(line, 5, "%04x", i);
910 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
912 snprintf(s, 4, " %02x", buffer[j]);