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
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_JTAG 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_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",
174 cmd->cmd.statemove->end_state);
176 if (cmd->cmd.statemove->end_state != -1)
178 jlink_end_state(cmd->cmd.statemove->end_state);
184 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
185 cmd->cmd.pathmove->num_states,
186 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
188 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
192 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
194 if (cmd->cmd.scan->end_state != -1)
196 jlink_end_state(cmd->cmd.scan->end_state);
199 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
200 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
202 #ifdef _DEBUG_USB_COMMS_
203 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
205 type = jtag_scan_type(cmd->cmd.scan);
206 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
210 DEBUG_JTAG_IO("reset trst: %i srst %i",
211 cmd->cmd.reset->trst,
212 cmd->cmd.reset->srst);
216 if (cmd->cmd.reset->trst == 1)
220 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
224 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
226 jtag_sleep(cmd->cmd.sleep->us);
230 LOG_ERROR("BUG: unknown JTAG command type encountered");
236 return jlink_tap_execute();
239 /* Sets speed in kHz. */
240 int jlink_speed(int 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,
292 jlink_jtag_handle = jlink_usb_open();
294 if (jlink_jtag_handle == 0)
296 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
297 return ERROR_JTAG_INIT_FAILED;
301 while (check_cnt < 3)
303 if (jlink_get_version_info() == ERROR_OK)
305 /* attempt to get status */
315 LOG_INFO("J-Link initial read failed, don't worry");
318 LOG_INFO("J-Link JTAG Interface ready");
328 jlink_usb_close(jlink_jtag_handle);
332 /***************************************************************************/
333 /* Queue command implementations */
335 void jlink_end_state(enum tap_state state)
337 if (tap_move_map[state] != -1)
343 LOG_ERROR("BUG: %i is not a valid end state", state);
348 /* Goes to the end state. */
349 void jlink_state_move(void)
353 u8 tms_scan = TAP_MOVE(cur_state, end_state);
355 for (i = 0; i < 7; i++)
357 tms = (tms_scan >> i) & 1;
358 jlink_tap_append_step(tms, 0);
361 cur_state = end_state;
364 void jlink_path_move(int num_states, enum tap_state *path)
368 for (i = 0; i < num_states; i++)
370 if (path[i] == tap_transitions[cur_state].low)
372 jlink_tap_append_step(0, 0);
374 else if (path[i] == tap_transitions[cur_state].high)
376 jlink_tap_append_step(1, 0);
380 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
387 end_state = cur_state;
390 void jlink_runtest(int num_cycles)
394 enum tap_state saved_end_state = end_state;
396 /* only do a state_move when we're not already in RTI */
397 if (cur_state != TAP_RTI)
399 jlink_end_state(TAP_RTI);
403 /* execute num_cycles */
404 for (i = 0; i < num_cycles; i++)
406 jlink_tap_append_step(0, 0);
409 /* finish in end_state */
410 jlink_end_state(saved_end_state);
411 if (cur_state != end_state)
417 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
419 enum tap_state saved_end_state;
421 jlink_tap_ensure_space(1, scan_size + 8);
423 saved_end_state = end_state;
425 /* Move to appropriate scan state */
426 jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
429 jlink_end_state(saved_end_state);
432 jlink_tap_append_scan(scan_size, buffer, command);
434 /* We are in Exit1, go to Pause */
435 jlink_tap_append_step(0, 0);
437 cur_state = ir_scan ? TAP_PI : TAP_PD;
439 if (cur_state != end_state)
445 void jlink_reset(int trst, int srst)
447 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
449 /* Signals are active low */
452 jlink_simple_command(EMU_CMD_HW_TRST1);
456 jlink_simple_command(EMU_CMD_HW_TRST0);
461 jlink_simple_command(EMU_CMD_HW_RESET1);
465 jlink_simple_command(EMU_CMD_HW_RESET0);
469 void jlink_simple_command(u8 command)
473 DEBUG_JTAG_IO("0x%02x", command);
475 usb_out_buffer[0] = command;
476 result = jlink_usb_write(jlink_jtag_handle, 1);
480 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
484 int jlink_get_status(void)
488 jlink_simple_command(EMU_CMD_GET_STATE);
489 result = jlink_usb_read(jlink_jtag_handle);
493 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
494 LOG_INFO("Vref = %d.%d TCK=%d TDI=%d TDO=%d TMS=%d SRST=%d TRST=%d\n", \
495 vref / 1000, vref % 1000, \
496 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
497 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
501 LOG_ERROR("Vref too low. Eventually the target isn't powered or disconnected?\n");
506 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
512 int jlink_get_version_info(void)
517 /* query hardware version */
518 jlink_simple_command(EMU_CMD_VERSION);
519 result = jlink_usb_read(jlink_jtag_handle);
523 len = buf_get_u32(usb_in_buffer, 0, 16);
524 result = jlink_usb_read(jlink_jtag_handle);
528 usb_in_buffer[result] = 0;
529 LOG_INFO(usb_in_buffer);
534 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
535 return ERROR_JTAG_DEVICE_ERROR;
538 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
540 if (jlink_get_version_info() == ERROR_OK)
542 /* attempt to get status */
549 /***************************************************************************/
550 /* J-Link tap functions */
552 /* We use the maximal value observed */
553 #define JLINK_TAP_BUFFER_SIZE 390
555 static int tap_length;
556 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
557 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
558 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
562 int first; /* First bit position in tdo_buffer to read */
563 int length; /* Number of bits to read */
564 scan_command_t *command; /* Corresponding scan command */
566 } pending_scan_result_t;
568 #define MAX_PENDING_SCAN_RESULTS 16
570 static int pending_scan_results_length;
571 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
575 void jlink_tap_init()
578 pending_scan_results_length = 0;
581 void jlink_tap_ensure_space(int scans, int bits)
583 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
584 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
586 if (scans > available_scans || bits > available_bits)
592 void jlink_tap_append_step(int tms, int tdi)
595 int index = tap_length / 8;
597 if (index < JLINK_TAP_BUFFER_SIZE)
599 int bit_index = tap_length % 8;
600 u8 bit = 1 << bit_index;
604 tms_buffer[index] |= bit;
608 tms_buffer[index] &= ~bit;
613 tdi_buffer[index] |= bit;
617 tdi_buffer[index] &= ~bit;
624 LOG_ERROR("jlink_tap_append_step, overflow");
628 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
630 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
633 pending_scan_result->first = tap_length;
634 pending_scan_result->length = length;
635 pending_scan_result->command = command;
636 pending_scan_result->buffer = buffer;
638 for (i = 0; i < length; i++)
640 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
642 pending_scan_results_length++;
645 /* Pad and send a tap sequence to the device, and receive the answer.
646 * For the purpose of padding we assume that we are in idle or pause state. */
647 int jlink_tap_execute()
657 /* Pad last byte so that tap_length is divisible by 8 */
658 while (tap_length % 8 != 0)
660 /* More of the last TMS value keeps us in the same state,
661 * analogous to free-running JTAG interfaces. */
662 jlink_tap_append_step(last_tms, 0);
665 byte_length = tap_length / 8;
667 usb_out_buffer[0] = EMU_CMD_HW_JTAG;
668 usb_out_buffer[1] = 0;
669 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
670 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
673 for (i = 0; i < byte_length; i++)
675 usb_out_buffer[tms_offset + i] = tms_buffer[i];
678 tdi_offset = tms_offset + byte_length;
679 for (i = 0; i < byte_length; i++)
681 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
684 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
686 if (result == byte_length)
688 for (i = 0; i < byte_length; i++)
690 tdo_buffer[i] = usb_in_buffer[i];
693 for (i = 0; i < pending_scan_results_length; i++)
695 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
696 u8 *buffer = pending_scan_result->buffer;
697 int length = pending_scan_result->length;
698 int first = pending_scan_result->first;
699 scan_command_t *command = pending_scan_result->command;
702 buf_set_buf(tdo_buffer, first, buffer, 0, length);
704 DEBUG_JTAG_IO("pending scan result, length = %d", length);
706 #ifdef _DEBUG_USB_COMMS_
707 jlink_debug_buffer(buffer, byte_length);
710 if (jtag_read_buffer(buffer, command) != ERROR_OK)
713 return ERROR_JTAG_QUEUE_FAILED;
716 if (pending_scan_result->buffer != NULL)
718 free(pending_scan_result->buffer);
724 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
725 return ERROR_JTAG_QUEUE_FAILED;
734 /*****************************************************************************/
735 /* JLink USB low-level functions */
737 jlink_jtag_t* jlink_usb_open()
739 struct usb_bus *busses;
741 struct usb_device *dev;
743 jlink_jtag_t *result;
745 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
751 busses = usb_get_busses();
753 /* find jlink_jtag device in usb bus */
755 for (bus = busses; bus; bus = bus->next)
757 for (dev = bus->devices; dev; dev = dev->next)
759 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
761 result->usb_handle = usb_open(dev);
763 /* usb_set_configuration required under win32 */
764 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
765 usb_claim_interface(result->usb_handle, 0);
766 usb_set_altinterface(result->usb_handle, 0);
776 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
778 usb_close(jlink_jtag->usb_handle);
782 /* Send a message and receive the reply. */
783 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
788 result = jlink_usb_write(jlink_jtag, out_length);
789 if (result == out_length)
791 result = jlink_usb_read(jlink_jtag);
792 if (result == in_length)
794 /* Must read the result from the EMU too */
795 result2 = jlink_usb_read_result(jlink_jtag);
798 /* Check the result itself */
799 if (0 == usb_emu_result_buffer[0])
805 LOG_ERROR("jlink_usb_read_result len (requested=0, result=%d)", usb_emu_result_buffer[0]);
811 LOG_ERROR("jlink_usb_read_result len (requested=1, result=%d)", result2);
817 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
823 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
828 /* Write data from out_buffer to USB. */
829 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
833 if (out_length > JLINK_OUT_BUFFER_SIZE)
835 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
839 result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
840 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
842 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
844 #ifdef _DEBUG_USB_COMMS_
845 jlink_debug_buffer(usb_out_buffer, out_length);
850 /* Read data from USB into in_buffer. */
851 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
853 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
854 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
856 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
858 #ifdef _DEBUG_USB_COMMS_
859 jlink_debug_buffer(usb_in_buffer, result);
864 /* Read the result from the previous EMU cmd into result_buffer. */
865 int jlink_usb_read_result(jlink_jtag_t *jlink_jtag)
867 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
868 usb_emu_result_buffer, JLINK_EMU_RESULT_BUFFER_SIZE, JLINK_USB_TIMEOUT);
870 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
872 #ifdef _DEBUG_USB_COMMS_
873 jlink_debug_buffer(usb_emu_result_buffer, result);
879 #ifdef _DEBUG_USB_COMMS_
880 #define BYTES_PER_LINE 16
882 void jlink_debug_buffer(u8 *buffer, int length)
889 for (i = 0; i < length; i += BYTES_PER_LINE)
891 snprintf(line, 5, "%04x", i);
892 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
894 snprintf(s, 4, " %02x", buffer[j]);