3 /***************************************************************************
4 * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
5 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
31 #define USB_VID 0x15ba
32 #define USB_PID 0x001e
34 #define ARMJTAGEW_EPT_BULK_OUT 0x01u
35 #define ARMJTAGEW_EPT_BULK_IN 0x82u
37 #define ARMJTAGEW_USB_TIMEOUT 2000
39 #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
40 #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
43 /* USB command request codes. */
44 #define CMD_GET_VERSION 0x00
45 #define CMD_SELECT_DPIMPL 0x10
46 #define CMD_SET_TCK_FREQUENCY 0x11
47 #define CMD_GET_TCK_FREQUENCY 0x12
48 #define CMD_MEASURE_MAX_TCK_FREQ 0x15
49 #define CMD_MEASURE_RTCK_RESPONSE 0x16
50 #define CMD_TAP_SHIFT 0x17
51 #define CMD_SET_TAPHW_STATE 0x20
52 #define CMD_GET_TAPHW_STATE 0x21
53 #define CMD_TGPWR_SETUP 0x22
55 /* Global USB buffers */
56 static u8 usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
57 static u8 usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
59 /* External interface functions */
60 static int armjtagew_execute_queue(void);
61 static int armjtagew_speed(int speed);
62 static int armjtagew_khz(int khz, int *jtag_speed);
63 static int armjtagew_register_commands(struct command_context_s *cmd_ctx);
64 static int armjtagew_init(void);
65 static int armjtagew_quit(void);
67 /* CLI command handler functions */
68 static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 /* Queue command functions */
71 static void armjtagew_end_state(tap_state_t state);
72 static void armjtagew_state_move(void);
73 static void armjtagew_path_move(int num_states, tap_state_t *path);
74 static void armjtagew_runtest(int num_cycles);
75 static void armjtagew_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
76 static void armjtagew_reset(int trst, int srst);
77 //static void armjtagew_simple_command(u8 command);
78 static int armjtagew_get_status(void);
80 /* tap buffer functions */
81 static void armjtagew_tap_init(void);
82 static int armjtagew_tap_execute(void);
83 static void armjtagew_tap_ensure_space(int scans, int bits);
84 static void armjtagew_tap_append_step(int tms, int tdi);
85 static void armjtagew_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
87 /* ARM-JTAG-EW lowlevel functions */
88 typedef struct armjtagew_jtag
90 struct usb_dev_handle* usb_handle;
93 static armjtagew_jtag_t *armjtagew_usb_open(void);
94 static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag);
95 static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length);
96 static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length);
97 static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length);
99 /* helper functions */
100 static int armjtagew_get_version_info(void);
102 #ifdef _DEBUG_USB_COMMS_
103 static void armjtagew_debug_buffer(u8 *buffer, int length);
106 static armjtagew_jtag_t* armjtagew_jtag_handle;
110 /***************************************************************************/
111 /* External interface implementation */
113 jtag_interface_t armjtagew_interface =
115 .name = "arm-jtag-ew",
116 .execute_queue = armjtagew_execute_queue,
117 .speed = armjtagew_speed,
118 .khz = armjtagew_khz,
119 .register_commands = armjtagew_register_commands,
120 .init = armjtagew_init,
121 .quit = armjtagew_quit
125 static int armjtagew_execute_queue(void)
127 jtag_command_t *cmd = jtag_command_queue;
137 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
139 if (cmd->cmd.end_state->end_state != TAP_INVALID)
141 armjtagew_end_state(cmd->cmd.end_state->end_state);
146 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
147 cmd->cmd.runtest->end_state);
149 if (cmd->cmd.runtest->end_state != TAP_INVALID)
151 armjtagew_end_state(cmd->cmd.runtest->end_state);
153 armjtagew_runtest(cmd->cmd.runtest->num_cycles);
157 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
159 if (cmd->cmd.statemove->end_state != TAP_INVALID)
161 armjtagew_end_state(cmd->cmd.statemove->end_state);
163 armjtagew_state_move();
167 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
168 cmd->cmd.pathmove->num_states, \
169 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
171 armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
175 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
177 if (cmd->cmd.scan->end_state != TAP_INVALID)
179 armjtagew_end_state(cmd->cmd.scan->end_state);
182 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
183 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
185 #ifdef _DEBUG_USB_COMMS_
186 armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
188 type = jtag_scan_type(cmd->cmd.scan);
189 armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
193 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
195 armjtagew_tap_execute();
197 if (cmd->cmd.reset->trst == 1)
199 tap_set_state(TAP_RESET);
201 armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
205 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
206 armjtagew_tap_execute();
207 jtag_sleep(cmd->cmd.sleep->us);
211 LOG_ERROR("BUG: unknown JTAG command type encountered");
217 return armjtagew_tap_execute();
221 /* Sets speed in kHz. */
222 static int armjtagew_speed(int speed)
228 usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
229 buf_set_u32(usb_out_buffer+1, 0, 32, speed);
231 result = armjtagew_usb_message(armjtagew_jtag_handle, 4, 4);
235 LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
236 return ERROR_JTAG_DEVICE_ERROR;
239 usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
240 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4);
241 speed_real = (int)buf_get_u32(usb_in_buffer,0,32);
244 LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
245 return ERROR_JTAG_DEVICE_ERROR;
249 LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
256 static int armjtagew_khz(int khz, int *jtag_speed)
263 static int armjtagew_register_commands(struct command_context_s *cmd_ctx)
265 register_command(cmd_ctx, NULL, "armjtagew_info", armjtagew_handle_armjtagew_info_command, COMMAND_EXEC,
266 "query armjtagew info");
270 static int armjtagew_init(void)
274 armjtagew_jtag_handle = armjtagew_usb_open();
276 if (armjtagew_jtag_handle == 0)
278 LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
279 return ERROR_JTAG_INIT_FAILED;
283 while (check_cnt < 3)
285 if (armjtagew_get_version_info() == ERROR_OK)
287 /* attempt to get status */
288 armjtagew_get_status();
297 LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
300 LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
302 armjtagew_reset(0, 0);
303 armjtagew_tap_init();
308 static int armjtagew_quit(void)
310 armjtagew_usb_close(armjtagew_jtag_handle);
314 /***************************************************************************/
315 /* Queue command implementations */
317 static void armjtagew_end_state(tap_state_t state)
319 if (tap_is_state_stable(state))
321 tap_set_end_state(state);
325 LOG_ERROR("BUG: %i is not a valid end state", state);
330 /* Goes to the end state. */
331 static void armjtagew_state_move(void)
335 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
336 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
338 for (i = 0; i < tms_count; i++)
340 tms = (tms_scan >> i) & 1;
341 armjtagew_tap_append_step(tms, 0);
344 tap_set_state(tap_get_end_state());
347 static void armjtagew_path_move(int num_states, tap_state_t *path)
351 for (i = 0; i < num_states; i++)
354 * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
355 * Either handle that here, or update the documentation with examples
356 * how to fix that in the configuration files.
358 if (path[i] == tap_state_transition(tap_get_state(), false))
360 armjtagew_tap_append_step(0, 0);
362 else if (path[i] == tap_state_transition(tap_get_state(), true))
364 armjtagew_tap_append_step(1, 0);
368 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
372 tap_set_state(path[i]);
375 tap_set_end_state(tap_get_state());
378 static void armjtagew_runtest(int num_cycles)
382 tap_state_t saved_end_state = tap_get_end_state();
384 /* only do a state_move when we're not already in IDLE */
385 if (tap_get_state() != TAP_IDLE)
387 armjtagew_end_state(TAP_IDLE);
388 armjtagew_state_move();
391 /* execute num_cycles */
392 for (i = 0; i < num_cycles; i++)
394 armjtagew_tap_append_step(0, 0);
397 /* finish in end_state */
398 armjtagew_end_state(saved_end_state);
399 if (tap_get_state() != tap_get_end_state())
401 armjtagew_state_move();
405 static void armjtagew_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
407 tap_state_t saved_end_state;
409 armjtagew_tap_ensure_space(1, scan_size + 8);
411 saved_end_state = tap_get_end_state();
413 /* Move to appropriate scan state */
414 armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
416 armjtagew_state_move();
417 armjtagew_end_state(saved_end_state);
420 armjtagew_tap_append_scan(scan_size, buffer, command);
422 /* We are in Exit1, go to Pause */
423 armjtagew_tap_append_step(0, 0);
425 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
427 if (tap_get_state() != tap_get_end_state())
429 armjtagew_state_move();
433 static void armjtagew_reset(int trst, int srst)
435 const u8 trst_mask = (1u<<5);
436 const u8 srst_mask = (1u<<6);
442 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
447 outp_en &= ~srst_mask; /* tristate */
448 change_mask |= srst_mask;
453 outp_en |= srst_mask;
454 change_mask |= srst_mask;
460 outp_en &= ~trst_mask; /* tristate */
461 change_mask |= trst_mask;
466 outp_en |= trst_mask;
467 change_mask |= trst_mask;
470 usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
471 usb_out_buffer[1] = val;
472 usb_out_buffer[2] = outp_en;
473 usb_out_buffer[3] = change_mask;
474 result = armjtagew_usb_write(armjtagew_jtag_handle, 4);
477 LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
482 static int armjtagew_get_status(void)
486 usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
487 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 12);
491 unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
492 LOG_INFO("U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s\n", \
493 buf_get_u32(usb_in_buffer + 0, 0, 16), \
494 buf_get_u32(usb_in_buffer + 2, 0, 16), \
495 buf_get_u32(usb_in_buffer + 4, 0, 16), \
496 buf_get_u32(usb_in_buffer + 6, 0, 16), \
498 usb_in_buffer[11] ? "OVERCURRENT" : "OK", \
499 usb_in_buffer[10] ? "enabled" : "disabled");
503 LOG_ERROR("Vref too low. Check Target Power\n");
508 LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)\n", result);
514 static int armjtagew_get_version_info(void)
520 /* query hardware version */
521 usb_out_buffer[0] = CMD_GET_VERSION;
522 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4+15+256);
526 LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)\n", result);
527 return ERROR_JTAG_DEVICE_ERROR;
531 memcpy(sn, usb_in_buffer+4, 15);
533 memcpy(auxinfo, usb_in_buffer+4+15, 256);
536 LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
537 usb_in_buffer[1], usb_in_buffer[0], \
538 isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
543 static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
545 if (armjtagew_get_version_info() == ERROR_OK)
547 /* attempt to get status */
548 armjtagew_get_status();
554 /***************************************************************************/
555 /* ARM-JTAG-EW tap functions */
557 /* 2048 is the max value we can use here */
558 #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
560 static int tap_length;
561 static u8 tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
562 static u8 tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
563 static u8 tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
567 int first; /* First bit position in tdo_buffer to read */
568 int length; /* Number of bits to read */
569 scan_command_t *command; /* Corresponding scan command */
571 } pending_scan_result_t;
573 #define MAX_PENDING_SCAN_RESULTS 256
575 static int pending_scan_results_length;
576 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
580 static void armjtagew_tap_init(void)
583 pending_scan_results_length = 0;
586 static void armjtagew_tap_ensure_space(int scans, int bits)
588 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
589 int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
591 if (scans > available_scans || bits > available_bits)
593 armjtagew_tap_execute();
597 static void armjtagew_tap_append_step(int tms, int tdi)
600 int index = tap_length / 8;
602 if (index < ARMJTAGEW_TAP_BUFFER_SIZE)
604 int bit_index = tap_length % 8;
605 u8 bit = 1 << bit_index;
609 tms_buffer[index] |= bit;
613 tms_buffer[index] &= ~bit;
618 tdi_buffer[index] |= bit;
622 tdi_buffer[index] &= ~bit;
629 LOG_ERROR("armjtagew_tap_append_step, overflow");
633 void armjtagew_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
635 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
638 pending_scan_result->first = tap_length;
639 pending_scan_result->length = length;
640 pending_scan_result->command = command;
641 pending_scan_result->buffer = buffer;
643 for (i = 0; i < length; i++)
645 armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
647 pending_scan_results_length++;
650 /* Pad and send a tap sequence to the device, and receive the answer.
651 * For the purpose of padding we assume that we are in idle or pause state. */
652 static int armjtagew_tap_execute(void)
662 /* Pad last byte so that tap_length is divisible by 8 */
663 while (tap_length % 8 != 0)
665 /* More of the last TMS value keeps us in the same state,
666 * analogous to free-running JTAG interfaces. */
667 armjtagew_tap_append_step(last_tms, 0);
670 byte_length = tap_length / 8;
672 usb_out_buffer[0] = CMD_TAP_SHIFT;
673 buf_set_u32(usb_out_buffer+1, 0, 16, byte_length);
676 for (i = 0; i < byte_length; i++)
678 usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8);
681 tdi_offset = tms_offset + byte_length;
682 for (i = 0; i < byte_length; i++)
684 usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8);
687 result = armjtagew_usb_message(armjtagew_jtag_handle, 3 + 2 * byte_length, byte_length + 4);
693 stat = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
695 LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat);
696 return ERROR_JTAG_QUEUE_FAILED;
699 for (i = 0; i < byte_length; i++)
701 tdo_buffer[i] = flip_u32(usb_in_buffer[i],8);
704 for (i = 0; i < pending_scan_results_length; i++)
706 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
707 u8 *buffer = pending_scan_result->buffer;
708 int length = pending_scan_result->length;
709 int first = pending_scan_result->first;
710 scan_command_t *command = pending_scan_result->command;
713 buf_set_buf(tdo_buffer, first, buffer, 0, length);
715 DEBUG_JTAG_IO("pending scan result, length = %d", length);
717 #ifdef _DEBUG_USB_COMMS_
718 armjtagew_debug_buffer(buffer, byte_length);
721 if (jtag_read_buffer(buffer, command) != ERROR_OK)
723 armjtagew_tap_init();
724 return ERROR_JTAG_QUEUE_FAILED;
727 if (pending_scan_result->buffer != NULL)
729 free(pending_scan_result->buffer);
735 LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length);
736 return ERROR_JTAG_QUEUE_FAILED;
739 armjtagew_tap_init();
745 /*****************************************************************************/
746 /* JLink USB low-level functions */
748 static armjtagew_jtag_t* armjtagew_usb_open()
750 struct usb_bus *busses;
752 struct usb_device *dev;
754 armjtagew_jtag_t *result;
756 result = (armjtagew_jtag_t*) malloc(sizeof(armjtagew_jtag_t));
762 busses = usb_get_busses();
764 /* find armjtagew_jtag device in usb bus */
766 for (bus = busses; bus; bus = bus->next)
768 for (dev = bus->devices; dev; dev = dev->next)
770 if ((dev->descriptor.idVendor == USB_VID) && (dev->descriptor.idProduct == USB_PID))
772 result->usb_handle = usb_open(dev);
775 /* usb_set_configuration required under win32 */
776 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
778 usb_claim_interface(result->usb_handle, 0);
782 * This makes problems under Mac OS X. And is not needed
783 * under Windows. Hopefully this will not break a linux build
785 usb_set_altinterface(result->usb_handle, 0);
796 static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag)
798 usb_close(armjtagew_jtag->usb_handle);
799 free(armjtagew_jtag);
802 /* Send a message and receive the reply. */
803 static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length)
807 result = armjtagew_usb_write(armjtagew_jtag, out_length);
808 if (result == out_length)
810 result = armjtagew_usb_read(armjtagew_jtag, in_length);
811 if (result != in_length)
813 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
819 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
825 /* Write data from out_buffer to USB. */
826 static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length)
830 if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE)
832 LOG_ERROR("armjtagew_jtag_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE);
836 result = usb_bulk_write(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
837 (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
839 DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
841 #ifdef _DEBUG_USB_COMMS_
842 armjtagew_debug_buffer(usb_out_buffer, out_length);
847 /* Read data from USB into in_buffer. */
848 static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length)
850 int result = usb_bulk_read(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
851 (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
853 DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
855 #ifdef _DEBUG_USB_COMMS_
856 armjtagew_debug_buffer(usb_in_buffer, result);
862 #ifdef _DEBUG_USB_COMMS_
863 #define BYTES_PER_LINE 16
865 static void armjtagew_debug_buffer(u8 *buffer, int length)
872 for (i = 0; i < length; i += BYTES_PER_LINE)
874 snprintf(line, 5, "%04x", i);
875 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
877 snprintf(s, 4, " %02x", buffer[j]);
880 LOG_DEBUG("%s", line);