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 */
63 /* The JLINK_TAP_SEQUENCE_COMMAND is obsolete *
64 /* and we should use EMU_CMD_HW_JTAG instead */
65 #define JLINK_TAP_SEQUENCE_COMMAND 0xcd
67 #define EMU_CMD_VERSION 0x01
68 #define EMU_CMD_SET_SPEED 0x05
69 #define EMU_CMD_GET_STATE 0x07
70 #define EMU_CMD_HW_JTAG 0xcf
71 #define EMU_CMD_HW_RESET0 0xdc
72 #define EMU_CMD_HW_RESET1 0xdd
73 #define EMU_CMD_HW_TRST0 0xde
74 #define EMU_CMD_HW_TRST1 0xdf
76 /* max speed 12MHz v5.0 jlink */
77 #define JLINK_MAX_SPEED 12000
79 /* External interface functions */
80 int jlink_execute_queue(void);
81 int jlink_speed(int speed);
82 int jlink_khz(int khz, int *jtag_speed);
83 int jlink_register_commands(struct command_context_s *cmd_ctx);
87 /* CLI command handler functions */
88 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
90 /* Queue command functions */
91 void jlink_end_state(enum tap_state state);
92 void jlink_state_move(void);
93 void jlink_path_move(int num_states, enum tap_state *path);
94 void jlink_runtest(int num_cycles);
95 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
96 void jlink_reset(int trst, int srst);
97 void jlink_simple_command(u8 command);
98 int jlink_get_status(void);
100 /* J-Link tap buffer functions */
101 void jlink_tap_init();
102 int jlink_tap_execute();
103 void jlink_tap_ensure_space(int scans, int bits);
104 void jlink_tap_append_step(int tms, int tdi);
105 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
107 /* Jlink lowlevel functions */
108 typedef struct jlink_jtag
110 struct usb_dev_handle* usb_handle;
113 jlink_jtag_t *jlink_usb_open(void);
114 void jlink_usb_close(jlink_jtag_t *jlink_jtag);
115 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
116 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
117 int jlink_usb_read(jlink_jtag_t *jlink_jtag);
119 #ifdef _DEBUG_USB_COMMS_
120 void jlink_debug_buffer(u8 *buffer, int length);
123 jlink_jtag_t* jlink_jtag_handle;
125 /***************************************************************************/
126 /* External interface implementation */
128 jtag_interface_t jlink_interface =
131 .execute_queue = jlink_execute_queue,
132 .speed = jlink_speed,
134 .register_commands = jlink_register_commands,
139 int jlink_execute_queue(void)
141 jtag_command_t *cmd = jtag_command_queue;
151 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
153 if (cmd->cmd.end_state->end_state != -1)
155 jlink_end_state(cmd->cmd.end_state->end_state);
160 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
161 cmd->cmd.runtest->end_state);
163 if (cmd->cmd.runtest->end_state != -1)
165 jlink_end_state(cmd->cmd.runtest->end_state);
167 jlink_runtest(cmd->cmd.runtest->num_cycles);
171 DEBUG_JTAG_IO("statemove end in %i",
172 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",
209 cmd->cmd.reset->trst,
210 cmd->cmd.reset->srst);
214 if (cmd->cmd.reset->trst == 1)
218 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
222 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
224 jtag_sleep(cmd->cmd.sleep->us);
228 LOG_ERROR("BUG: unknown JTAG command type encountered");
238 /* Sets speed in kHz. */
239 int jlink_speed(int speed)
243 // if ((speed == -1) || ((1 <= speed) && (speed <= JLINK_MAX_SPEED)))
244 if (speed <= JLINK_MAX_SPEED)
246 /* check for RTCK setting */
250 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
251 usb_out_buffer[1] = (speed >> 0) & 0xff;
252 usb_out_buffer[2] = (speed >> 8) & 0xff;
254 result = jlink_usb_write(jlink_jtag_handle, 3);
262 LOG_ERROR("J-Link setting speed failed (%d)", result);
263 return ERROR_JTAG_DEVICE_ERROR;
268 LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
274 int jlink_khz(int khz, int *jtag_speed)
281 int jlink_register_commands(struct command_context_s *cmd_ctx)
283 register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
294 jlink_jtag_handle = jlink_usb_open();
296 if (jlink_jtag_handle == 0)
298 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
299 return ERROR_JTAG_INIT_FAILED;
303 while (check_cnt < 3)
305 /* query hardware version */
306 jlink_simple_command(EMU_CMD_VERSION);
307 result = jlink_usb_read(jlink_jtag_handle);
312 len = buf_get_u32(usb_in_buffer, 0, 16);
315 result = jlink_usb_read(jlink_jtag_handle);
319 usb_in_buffer[result] = 0;
320 LOG_INFO(usb_in_buffer);
322 /* attempt to get status */
334 LOG_INFO("J-Link initial read failed, don't worry");
337 LOG_INFO("J-Link JTAG Interface ready");
347 jlink_usb_close(jlink_jtag_handle);
351 /***************************************************************************/
352 /* Queue command implementations */
354 void jlink_end_state(enum tap_state state)
356 if (tap_move_map[state] != -1)
362 LOG_ERROR("BUG: %i is not a valid end state", state);
367 /* Goes to the end state. */
368 void jlink_state_move(void)
372 u8 tms_scan = TAP_MOVE(cur_state, end_state);
374 for (i = 0; i < 7; i++)
376 tms = (tms_scan >> i) & 1;
377 jlink_tap_append_step(tms, 0);
380 cur_state = end_state;
383 void jlink_path_move(int num_states, enum tap_state *path)
387 for (i = 0; i < num_states; i++)
389 if (path[i] == tap_transitions[cur_state].low)
391 jlink_tap_append_step(0, 0);
393 else if (path[i] == tap_transitions[cur_state].high)
395 jlink_tap_append_step(1, 0);
399 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
406 end_state = cur_state;
409 void jlink_runtest(int num_cycles)
413 enum tap_state saved_end_state = end_state;
415 /* only do a state_move when we're not already in RTI */
416 if (cur_state != TAP_RTI)
418 jlink_end_state(TAP_RTI);
422 /* execute num_cycles */
423 for (i = 0; i < num_cycles; i++)
425 jlink_tap_append_step(0, 0);
428 /* finish in end_state */
429 jlink_end_state(saved_end_state);
430 if (cur_state != end_state)
436 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
438 enum tap_state saved_end_state;
440 jlink_tap_ensure_space(1, scan_size + 8);
442 saved_end_state = end_state;
444 /* Move to appropriate scan state */
445 jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
448 jlink_end_state(saved_end_state);
451 jlink_tap_append_scan(scan_size, buffer, command);
453 /* We are in Exit1, go to Pause */
454 jlink_tap_append_step(0, 0);
456 cur_state = ir_scan ? TAP_PI : TAP_PD;
458 if (cur_state != end_state)
464 void jlink_reset(int trst, int srst)
466 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
468 /* Signals are active low */
471 jlink_simple_command(EMU_CMD_HW_TRST1);
475 jlink_simple_command(EMU_CMD_HW_TRST0);
480 jlink_simple_command(EMU_CMD_HW_RESET1);
484 jlink_simple_command(EMU_CMD_HW_RESET0);
488 void jlink_simple_command(u8 command)
492 DEBUG_JTAG_IO("0x%02x", command);
494 usb_out_buffer[0] = command;
495 result = jlink_usb_write(jlink_jtag_handle, 1);
499 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
503 int jlink_get_status(void)
507 jlink_simple_command(EMU_CMD_GET_STATE);
508 result = jlink_usb_read(jlink_jtag_handle);
512 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
513 LOG_INFO("Vref = %d.%d TCK=%d TDI=%d TDO=%d TMS=%d SRST=%d TRST=%d\n", \
514 vref / 1000, vref % 1000, \
515 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
516 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
520 LOG_ERROR("Vref too low. Eventually the target isn't powered or disconnected?\n");
525 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
531 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
536 /* query hardware version */
537 jlink_simple_command(EMU_CMD_VERSION);
538 result = jlink_usb_read(jlink_jtag_handle);
542 len = buf_get_u32(usb_in_buffer, 0, 16);
543 result = jlink_usb_read(jlink_jtag_handle);
547 usb_in_buffer[result] = 0;
548 LOG_INFO(usb_in_buffer);
551 /* attempt to get status */
556 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
562 /***************************************************************************/
563 /* J-Link tap functions */
565 /* We use the maximal value observed */
566 #define JLINK_TAP_BUFFER_SIZE 390
568 static int tap_length;
569 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
570 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
571 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
575 int first; /* First bit position in tdo_buffer to read */
576 int length; /* Number of bits to read */
577 scan_command_t *command; /* Corresponding scan command */
579 } pending_scan_result_t;
581 #define MAX_PENDING_SCAN_RESULTS 16
583 static int pending_scan_results_length;
584 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
588 void jlink_tap_init()
591 pending_scan_results_length = 0;
594 void jlink_tap_ensure_space(int scans, int bits)
596 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
597 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
599 if (scans > available_scans || bits > available_bits)
605 void jlink_tap_append_step(int tms, int tdi)
608 int index = tap_length / 8;
610 if (index < JLINK_TAP_BUFFER_SIZE)
612 int bit_index = tap_length % 8;
613 u8 bit = 1 << bit_index;
617 tms_buffer[index] |= bit;
621 tms_buffer[index] &= ~bit;
626 tdi_buffer[index] |= bit;
630 tdi_buffer[index] &= ~bit;
637 LOG_ERROR("jlink_tap_append_step, overflow");
641 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
643 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
646 pending_scan_result->first = tap_length;
647 pending_scan_result->length = length;
648 pending_scan_result->command = command;
649 pending_scan_result->buffer = buffer;
651 for (i = 0; i < length; i++)
653 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
655 pending_scan_results_length++;
658 /* Pad and send a tap sequence to the device, and receive the answer.
659 * For the purpose of padding we assume that we are in idle or pause state. */
660 int jlink_tap_execute()
670 /* Pad last byte so that tap_length is divisible by 8 */
671 while (tap_length % 8 != 0)
673 /* More of the last TMS value keeps us in the same state,
674 * analogous to free-running JTAG interfaces. */
675 jlink_tap_append_step(last_tms, 0);
678 byte_length = tap_length / 8;
680 usb_out_buffer[0] = JLINK_TAP_SEQUENCE_COMMAND;
681 usb_out_buffer[1] = (tap_length >> 0) & 0xff;
682 usb_out_buffer[2] = (tap_length >> 8) & 0xff;
685 for (i = 0; i < byte_length; i++)
687 usb_out_buffer[tms_offset + i] = tms_buffer[i];
690 tdi_offset = tms_offset + byte_length;
691 for (i = 0; i < byte_length; i++)
693 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
696 result = jlink_usb_message(jlink_jtag_handle, 3 + 2 * byte_length, byte_length);
698 if (result == byte_length)
700 for (i = 0; i < byte_length; i++)
702 tdo_buffer[i] = usb_in_buffer[i];
705 for (i = 0; i < pending_scan_results_length; i++)
707 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
708 u8 *buffer = pending_scan_result->buffer;
709 int length = pending_scan_result->length;
710 int first = pending_scan_result->first;
711 scan_command_t *command = pending_scan_result->command;
714 buf_set_buf(tdo_buffer, first, buffer, 0, length);
716 DEBUG_JTAG_IO("pending scan result, length = %d", length);
718 #ifdef _DEBUG_USB_COMMS_
719 jlink_debug_buffer(buffer, byte_length);
722 if (jtag_read_buffer(buffer, command) != ERROR_OK)
725 return ERROR_JTAG_QUEUE_FAILED;
728 if (pending_scan_result->buffer != NULL)
730 free(pending_scan_result->buffer);
736 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
737 return ERROR_JTAG_QUEUE_FAILED;
746 /*****************************************************************************/
747 /* JLink USB low-level functions */
749 jlink_jtag_t* jlink_usb_open()
751 struct usb_bus *busses;
753 struct usb_device *dev;
755 jlink_jtag_t *result;
757 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
763 busses = usb_get_busses();
765 /* find jlink_jtag device in usb bus */
767 for (bus = busses; bus; bus = bus->next)
769 for (dev = bus->devices; dev; dev = dev->next)
771 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
773 result->usb_handle = usb_open(dev);
775 /* usb_set_configuration required under win32 */
776 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
777 usb_claim_interface(result->usb_handle, 0);
778 usb_set_altinterface(result->usb_handle, 0);
788 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
790 usb_close(jlink_jtag->usb_handle);
794 /* Send a message and receive the reply. */
795 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
799 result = jlink_usb_write(jlink_jtag, out_length);
800 if (result == out_length)
802 result = jlink_usb_read(jlink_jtag);
803 if (result == in_length)
809 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
815 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
820 /* Write data from out_buffer to USB. */
821 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
825 if (out_length > JLINK_OUT_BUFFER_SIZE)
827 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
831 result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
832 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
834 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
836 #ifdef _DEBUG_USB_COMMS_
837 jlink_debug_buffer(usb_out_buffer, out_length);
842 /* Read data from USB into in_buffer. */
843 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
845 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
846 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
848 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
850 #ifdef _DEBUG_USB_COMMS_
851 jlink_debug_buffer(usb_in_buffer, result);
856 #ifdef _DEBUG_USB_COMMS_
857 #define BYTES_PER_LINE 16
859 void jlink_debug_buffer(u8 *buffer, int length)
866 for (i = 0; i < length; i += BYTES_PER_LINE)
868 snprintf(line, 5, "%04x", i);
869 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
871 snprintf(s, 4, " %02x", buffer[j]);