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 2064
55 #define JLINK_OUT_BUFFER_SIZE 2064
57 /* Global USB buffers */
58 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
59 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
61 /* Constants for JLink command */
62 #define JLINK_FIRMWARE_VERSION 0x01
63 #define JLINK_SPEED_COMMAND 0x05
64 #define JLINK_GET_STATUS_COMMAND 0x07
65 #define JLINK_TAP_SEQUENCE_COMMAND 0xcd
66 #define JLINK_GET_SERIAL 0xe6
67 #define JLINK_SET_SRST_LOW_COMMAND 0xdc
68 #define JLINK_SET_SRST_HIGH_COMMAND 0xdd
69 #define JLINK_SET_TRST_LOW_COMMAND 0xde
70 #define JLINK_SET_TRST_HIGH_COMMAND 0xdf
71 #define JLINK_HARDWARE_VERSION 0xe8
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();
99 int jlink_tap_execute();
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);
116 #ifdef _DEBUG_USB_COMMS_
117 void jlink_debug_buffer(u8 *buffer, int length);
120 jlink_jtag_t* jlink_jtag_handle;
122 /***************************************************************************/
123 /* External interface implementation */
125 jtag_interface_t jlink_interface =
128 .execute_queue = jlink_execute_queue,
129 .speed = jlink_speed,
131 .register_commands = jlink_register_commands,
136 int jlink_execute_queue(void)
138 jtag_command_t *cmd = jtag_command_queue;
148 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
150 if (cmd->cmd.end_state->end_state != -1)
152 jlink_end_state(cmd->cmd.end_state->end_state);
157 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
158 cmd->cmd.runtest->end_state);
160 if (cmd->cmd.runtest->end_state != -1)
162 jlink_end_state(cmd->cmd.runtest->end_state);
164 jlink_runtest(cmd->cmd.runtest->num_cycles);
168 DEBUG_JTAG_IO("statemove end in %i",
169 cmd->cmd.statemove->end_state);
171 if (cmd->cmd.statemove->end_state != -1)
173 jlink_end_state(cmd->cmd.statemove->end_state);
179 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
180 cmd->cmd.pathmove->num_states,
181 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
183 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
187 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
189 if (cmd->cmd.scan->end_state != -1)
191 jlink_end_state(cmd->cmd.scan->end_state);
194 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
195 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
197 #ifdef _DEBUG_USB_COMMS_
198 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
200 type = jtag_scan_type(cmd->cmd.scan);
201 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
205 DEBUG_JTAG_IO("reset trst: %i srst %i",
206 cmd->cmd.reset->trst,
207 cmd->cmd.reset->srst);
211 if (cmd->cmd.reset->trst == 1)
215 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
219 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
221 jtag_sleep(cmd->cmd.sleep->us);
225 LOG_ERROR("BUG: unknown JTAG command type encountered");
235 /* Sets speed in kHz. */
236 int jlink_speed(int speed)
240 // if ((speed == -1) || ((1 <= speed) && (speed <= JLINK_MAX_SPEED)))
241 if (speed <= JLINK_MAX_SPEED)
243 /* check for RTCK setting */
247 usb_out_buffer[0] = JLINK_SPEED_COMMAND;
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;
297 result = jlink_usb_read(jlink_jtag_handle);
298 if (result != 2 || usb_in_buffer[0] != 0x07 || usb_in_buffer[1] != 0x00)
300 LOG_INFO("J-Link initial read failed, don't worry");
303 LOG_INFO("J-Link JTAG Interface ready");
308 /* query jlink status */
316 jlink_usb_close(jlink_jtag_handle);
320 /***************************************************************************/
321 /* Queue command implementations */
323 void jlink_end_state(enum tap_state state)
325 if (tap_move_map[state] != -1)
331 LOG_ERROR("BUG: %i is not a valid end state", state);
336 /* Goes to the end state. */
337 void jlink_state_move(void)
341 u8 tms_scan = TAP_MOVE(cur_state, end_state);
343 for (i = 0; i < 7; i++)
345 tms = (tms_scan >> i) & 1;
346 jlink_tap_append_step(tms, 0);
349 cur_state = end_state;
352 void jlink_path_move(int num_states, enum tap_state *path)
356 for (i = 0; i < num_states; i++)
358 if (path[i] == tap_transitions[cur_state].low)
360 jlink_tap_append_step(0, 0);
362 else if (path[i] == tap_transitions[cur_state].high)
364 jlink_tap_append_step(1, 0);
368 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
375 end_state = cur_state;
378 void jlink_runtest(int num_cycles)
382 enum tap_state saved_end_state = end_state;
384 /* only do a state_move when we're not already in RTI */
385 if (cur_state != TAP_RTI)
387 jlink_end_state(TAP_RTI);
391 /* execute num_cycles */
392 for (i = 0; i < num_cycles; i++)
394 jlink_tap_append_step(0, 0);
397 /* finish in end_state */
398 jlink_end_state(saved_end_state);
399 if (cur_state != end_state)
405 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
407 enum tap_state saved_end_state;
409 jlink_tap_ensure_space(1, scan_size + 8);
411 saved_end_state = end_state;
413 /* Move to appropriate scan state */
414 jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
417 jlink_end_state(saved_end_state);
420 jlink_tap_append_scan(scan_size, buffer, command);
422 /* We are in Exit1, go to Pause */
423 jlink_tap_append_step(0, 0);
425 cur_state = ir_scan ? TAP_PI : TAP_PD;
427 if (cur_state != end_state)
433 void jlink_reset(int trst, int srst)
435 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
437 /* Signals are active low */
440 jlink_simple_command(JLINK_SET_TRST_HIGH_COMMAND);
444 jlink_simple_command(JLINK_SET_TRST_LOW_COMMAND);
449 jlink_simple_command(JLINK_SET_SRST_HIGH_COMMAND);
453 jlink_simple_command(JLINK_SET_SRST_LOW_COMMAND);
457 void jlink_simple_command(u8 command)
461 DEBUG_JTAG_IO("0x%02x", command);
463 usb_out_buffer[0] = command;
464 result = jlink_usb_write(jlink_jtag_handle, 1);
468 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
472 int jlink_get_status(void)
476 jlink_simple_command(JLINK_GET_STATUS_COMMAND);
477 result = jlink_usb_read(jlink_jtag_handle);
481 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
482 LOG_INFO("Vref = %d.%d TCK=%d TDI=%d TDO=%d TMS=%d SRST=%d TRST=%d\n", \
483 vref / 1000, vref % 1000, \
484 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
485 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
489 LOG_ERROR("Vref too low. Eventually the target isn't powered or disconnected?\n");
494 LOG_ERROR("J-Link command JLINK_GET_STATUS_COMMAND failed (%d)\n", result);
500 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
505 /* query hardware version */
506 jlink_simple_command(JLINK_FIRMWARE_VERSION);
507 result = jlink_usb_read(jlink_jtag_handle);
511 len = buf_get_u32(usb_in_buffer, 0, 16);
512 result = jlink_usb_read(jlink_jtag_handle);
516 usb_in_buffer[result] = 0;
517 LOG_INFO(usb_in_buffer);
520 /* attempt to get status */
525 LOG_ERROR("J-Link command JLINK_FIRMWARE_VERSION failed (%d)\n", result);
531 /***************************************************************************/
532 /* J-Link tap functions */
534 /* We use the maximal value observed */
535 #define JLINK_TAP_BUFFER_SIZE 390
537 static int tap_length;
538 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
539 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
540 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
544 int first; /* First bit position in tdo_buffer to read */
545 int length; /* Number of bits to read */
546 scan_command_t *command; /* Corresponding scan command */
548 } pending_scan_result_t;
550 #define MAX_PENDING_SCAN_RESULTS 16
552 static int pending_scan_results_length;
553 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
557 void jlink_tap_init()
560 pending_scan_results_length = 0;
563 void jlink_tap_ensure_space(int scans, int bits)
565 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
566 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
568 if (scans > available_scans || bits > available_bits)
574 void jlink_tap_append_step(int tms, int tdi)
577 int index = tap_length / 8;
579 if (index < JLINK_TAP_BUFFER_SIZE)
581 int bit_index = tap_length % 8;
582 u8 bit = 1 << bit_index;
586 tms_buffer[index] |= bit;
590 tms_buffer[index] &= ~bit;
595 tdi_buffer[index] |= bit;
599 tdi_buffer[index] &= ~bit;
606 LOG_ERROR("jlink_tap_append_step, overflow");
610 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
612 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
615 pending_scan_result->first = tap_length;
616 pending_scan_result->length = length;
617 pending_scan_result->command = command;
618 pending_scan_result->buffer = buffer;
620 for (i = 0; i < length; i++)
622 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
624 pending_scan_results_length++;
627 /* Pad and send a tap sequence to the device, and receive the answer.
628 * For the purpose of padding we assume that we are in idle or pause state. */
629 int jlink_tap_execute()
639 /* Pad last byte so that tap_length is divisible by 8 */
640 while (tap_length % 8 != 0)
642 /* More of the last TMS value keeps us in the same state,
643 * analogous to free-running JTAG interfaces. */
644 jlink_tap_append_step(last_tms, 0);
647 byte_length = tap_length / 8;
649 usb_out_buffer[0] = JLINK_TAP_SEQUENCE_COMMAND;
650 usb_out_buffer[1] = (tap_length >> 0) & 0xff;
651 usb_out_buffer[2] = (tap_length >> 8) & 0xff;
654 for (i = 0; i < byte_length; i++)
656 usb_out_buffer[tms_offset + i] = tms_buffer[i];
659 tdi_offset = tms_offset + byte_length;
660 for (i = 0; i < byte_length; i++)
662 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
665 result = jlink_usb_message(jlink_jtag_handle, 3 + 2 * byte_length, byte_length);
667 if (result == byte_length)
669 for (i = 0; i < byte_length; i++)
671 tdo_buffer[i] = usb_in_buffer[i];
674 for (i = 0; i < pending_scan_results_length; i++)
676 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
677 u8 *buffer = pending_scan_result->buffer;
678 int length = pending_scan_result->length;
679 int first = pending_scan_result->first;
680 scan_command_t *command = pending_scan_result->command;
683 buf_set_buf(tdo_buffer, first, buffer, 0, length);
685 DEBUG_JTAG_IO("pending scan result, length = %d", length);
687 #ifdef _DEBUG_USB_COMMS_
688 jlink_debug_buffer(buffer, byte_length);
691 if (jtag_read_buffer(buffer, command) != ERROR_OK)
694 return ERROR_JTAG_QUEUE_FAILED;
697 if (pending_scan_result->buffer != NULL)
699 free(pending_scan_result->buffer);
705 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
706 return ERROR_JTAG_QUEUE_FAILED;
715 /*****************************************************************************/
716 /* JLink USB low-level functions */
718 jlink_jtag_t* jlink_usb_open()
720 struct usb_bus *busses;
722 struct usb_device *dev;
724 jlink_jtag_t *result;
726 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
732 busses = usb_get_busses();
734 /* find jlink_jtag device in usb bus */
736 for (bus = busses; bus; bus = bus->next)
738 for (dev = bus->devices; dev; dev = dev->next)
740 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
742 result->usb_handle = usb_open(dev);
744 /* usb_set_configuration required under win32 */
745 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
746 usb_claim_interface(result->usb_handle, 0);
747 usb_set_altinterface(result->usb_handle, 0);
757 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
759 usb_close(jlink_jtag->usb_handle);
763 /* Send a message and receive the reply. */
764 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
768 result = jlink_usb_write(jlink_jtag, out_length);
769 if (result == out_length)
771 result = jlink_usb_read(jlink_jtag);
772 if (result == in_length)
778 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
784 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
789 /* Write data from out_buffer to USB. */
790 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
794 if (out_length > JLINK_OUT_BUFFER_SIZE)
796 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
800 result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
801 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
803 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
805 #ifdef _DEBUG_USB_COMMS_
806 jlink_debug_buffer(usb_out_buffer, out_length);
811 /* Read data from USB into in_buffer. */
812 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
814 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
815 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
817 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
819 #ifdef _DEBUG_USB_COMMS_
820 jlink_debug_buffer(usb_in_buffer, result);
825 #ifdef _DEBUG_USB_COMMS_
826 #define BYTES_PER_LINE 16
828 void jlink_debug_buffer(u8 *buffer, int length)
835 for (i = 0; i < length; i += BYTES_PER_LINE)
837 snprintf(line, 5, "%04x", i);
838 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
840 snprintf(s, 4, " %02x", buffer[j]);