1 /*******************************************************************************
2 * Driver for OpenJTAG Project (www.openjtag.org) *
3 * Compatible with libftdi driver. *
5 * Copyright (C) 2010 by Ivan Meleca <mileca@gmail.com> *
7 * Copyright (C) 2013 by Ryan Corbin, GlueLogix Inc. <corbin.ryan@gmail.com> *
8 * Updated to work with OpenOCD v0.7.0. Fixed libftdi read speed issue. *
10 * Based on usb_blaster.c *
11 * Copyright (C) 2009 Catalin Patulea *
12 * Copyright (C) 2006 Kolja Waschk *
15 * Copyright (C) 2008 by Spencer Oliver *
16 * spen@spen-soft.co.uk *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
30 ***************************************************************************/
32 /***************************************************************************
33 * Version 1.0 Tested on a MCBSTM32 board using a Cortex-M3 (stm32f103x), *
34 * GDB and Eclipse under Linux (Ubuntu 10.04) *
36 ***************************************************************************/
42 #include <jtag/interface.h>
43 #include <jtag/commands.h>
44 #include "usb_common.h"
47 * OpenJTAG-OpenOCD state conversion
49 typedef enum openjtag_tap_state {
50 OPENJTAG_TAP_INVALID = -1,
51 OPENJTAG_TAP_RESET = 0,
52 OPENJTAG_TAP_IDLE = 1,
53 OPENJTAG_TAP_SELECT_DR = 2,
54 OPENJTAG_TAP_CAPTURE_DR = 3,
55 OPENJTAG_TAP_SHIFT_DR = 4,
56 OPENJTAG_TAP_EXIT1_DR = 5,
57 OPENJTAG_TAP_PAUSE_DR = 6,
58 OPENJTAG_TAP_EXIT2_DR = 7,
59 OPENJTAG_TAP_UPDATE_DR = 8,
60 OPENJTAG_TAP_SELECT_IR = 9,
61 OPENJTAG_TAP_CAPURE_IR = 10,
62 OPENJTAG_TAP_SHIFT_IR = 11,
63 OPENJTAG_TAP_EXIT1_IR = 12,
64 OPENJTAG_TAP_PAUSE_IR = 13,
65 OPENJTAG_TAP_EXIT2_IR = 14,
66 OPENJTAG_TAP_UPDATE_IR = 15,
67 } openjtag_tap_state_t;
69 /* OPENJTAG access library includes */
72 /* OpenJTAG vid/pid */
73 static uint16_t openjtag_vid = 0x0403;
74 static uint16_t openjtag_pid = 0x6001;
76 static char *openjtag_device_desc;
78 static struct ftdi_context ftdic;
80 #define OPENJTAG_BUFFER_SIZE 504
81 #define OPENJTAG_MAX_PENDING_RESULTS 256
83 struct openjtag_scan_result {
84 uint32_t bits; /* Length in bits*/
85 struct scan_command *command; /* Corresponding scan command */
89 /* USB RX/TX buffers */
90 static int usb_tx_buf_offs;
91 static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE];
92 static uint32_t usb_rx_buf_len;
93 static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE];
95 /* Pending readings */
96 static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS];
97 static int openjtag_scan_result_count;
99 /* Openocd usb handler */
101 struct usb_dev_handle *usb_handle;
104 #ifdef _DEBUG_USB_COMMS_
106 #define DEBUG_TYPE_READ 0
107 #define DEBUG_TYPE_WRITE 1
108 #define DEBUG_TYPE_OCD_READ 2
109 #define DEBUG_TYPE_BUFFER 3
112 static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
120 case DEBUG_TYPE_READ:
121 sprintf(line, "USB READ %d bytes", length);
123 case DEBUG_TYPE_WRITE:
124 sprintf(line, "USB WRITE %d bytes", length);
126 case DEBUG_TYPE_OCD_READ:
127 sprintf(line, "TO OpenOCD %d bytes", length);
129 case DEBUG_TYPE_BUFFER:
130 sprintf(line, "Buffer %d bytes", length);
134 LOG_DEBUG("%s", line);
136 for (i = 0; i < length; i += LINE_LEN) {
138 case DEBUG_TYPE_READ:
139 sprintf(line, "USB READ: %04x", i);
141 case DEBUG_TYPE_WRITE:
142 sprintf(line, "USB WRITE: %04x", i);
144 case DEBUG_TYPE_OCD_READ:
145 sprintf(line, "TO OpenOCD: %04x", i);
147 case DEBUG_TYPE_BUFFER:
148 sprintf(line, "BUFFER: %04x", i);
152 for (j = i; j < i + LINE_LEN && j < length; j++) {
153 sprintf(s, " %02x", buffer[j]);
156 LOG_DEBUG("%s", line);
163 static int8_t openjtag_get_tap_state(int8_t state)
167 case TAP_DREXIT2: return OPENJTAG_TAP_EXIT2_DR;
168 case TAP_DREXIT1: return OPENJTAG_TAP_EXIT1_DR;
169 case TAP_DRSHIFT: return OPENJTAG_TAP_SHIFT_DR;
170 case TAP_DRPAUSE: return OPENJTAG_TAP_PAUSE_DR;
171 case TAP_IRSELECT: return OPENJTAG_TAP_SELECT_IR;
172 case TAP_DRUPDATE: return OPENJTAG_TAP_UPDATE_DR;
173 case TAP_DRCAPTURE: return OPENJTAG_TAP_CAPTURE_DR;
174 case TAP_DRSELECT: return OPENJTAG_TAP_SELECT_DR;
175 case TAP_IREXIT2: return OPENJTAG_TAP_EXIT2_IR;
176 case TAP_IREXIT1: return OPENJTAG_TAP_EXIT1_IR;
177 case TAP_IRSHIFT: return OPENJTAG_TAP_SHIFT_IR;
178 case TAP_IRPAUSE: return OPENJTAG_TAP_PAUSE_IR;
179 case TAP_IDLE: return OPENJTAG_TAP_IDLE;
180 case TAP_IRUPDATE: return OPENJTAG_TAP_UPDATE_IR;
181 case TAP_IRCAPTURE: return OPENJTAG_TAP_CAPURE_IR;
182 case TAP_RESET: return OPENJTAG_TAP_RESET;
184 default: return OPENJTAG_TAP_INVALID;
188 static int openjtag_buf_write(
189 uint8_t *buf, int size, uint32_t *bytes_written)
192 #ifdef _DEBUG_USB_COMMS_
193 openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
196 retval = ftdi_write_data(&ftdic, buf, size);
199 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
200 return ERROR_JTAG_DEVICE_ERROR;
203 *bytes_written += retval;
208 static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
215 while ((*bytes_read < qty) && timeout--) {
216 retval = ftdi_read_data(&ftdic, buf + *bytes_read,
220 DEBUG_JTAG_IO("ftdi_read_data: %s",
221 ftdi_get_error_string(&ftdic));
222 return ERROR_JTAG_DEVICE_ERROR;
224 *bytes_read += retval;
227 #ifdef _DEBUG_USB_COMMS_
228 openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
234 static int openjtag_sendcommand(uint8_t cmd)
237 return openjtag_buf_write(&cmd, 1, &written);
240 static int openjtag_speed(int speed)
270 LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
273 openjtag_sendcommand(clockcmd);
278 static int openjtag_init(void)
280 uint8_t latency_timer;
284 openjtag_scan_result_count = 0;
286 LOG_DEBUG("'openjtag' interface using libftdi");
288 /* Open by device description */
289 if (openjtag_device_desc == NULL) {
290 LOG_WARNING("no openjtag device description specified, "
291 "using default 'Open JTAG Project'");
292 openjtag_device_desc = "Open JTAG Project";
295 if (ftdi_init(&ftdic) < 0)
296 return ERROR_JTAG_INIT_FAILED;
298 /* context, vendor id, product id, description, serial id */
299 if (ftdi_usb_open_desc(&ftdic, openjtag_vid, openjtag_pid, openjtag_device_desc, NULL) < 0) {
300 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
301 return ERROR_JTAG_INIT_FAILED;
304 if (ftdi_usb_reset(&ftdic) < 0) {
305 LOG_ERROR("unable to reset ftdi device");
306 return ERROR_JTAG_INIT_FAILED;
309 if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
310 LOG_ERROR("unable to set latency timer");
311 return ERROR_JTAG_INIT_FAILED;
314 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
315 LOG_ERROR("unable to get latency timer");
316 return ERROR_JTAG_INIT_FAILED;
318 LOG_DEBUG("current latency timer: %u", latency_timer);
320 ftdi_disable_bitbang(&ftdic);
321 /* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
322 if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
323 LOG_ERROR("Can't set baud rate to max: %s",
324 ftdi_get_error_string(&ftdic));
325 return ERROR_JTAG_DEVICE_ERROR;
328 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
329 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
330 return ERROR_JTAG_INIT_FAILED;
334 openjtag_sendcommand(0xE0); /*Start at slowest adapter speed*/
337 openjtag_sendcommand(0x75);
342 static int openjtag_quit(void)
344 ftdi_usb_close(&ftdic);
350 static void openjtag_write_tap_buffer(void)
354 openjtag_buf_write(usb_tx_buf, usb_tx_buf_offs, &written);
355 openjtag_buf_read(usb_rx_buf, usb_tx_buf_offs, &usb_rx_buf_len);
360 static int openjtag_execute_tap_queue(void)
362 openjtag_write_tap_buffer();
366 if (openjtag_scan_result_count && usb_rx_buf_len) {
372 /* for every pending result */
373 while (res_count < openjtag_scan_result_count) {
376 len = openjtag_scan_result_buffer[res_count].bits;
380 uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
384 DEBUG_JTAG_IO("bits < 8 buf = 0x%X, will be 0x%X",
385 usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
386 buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
389 buffer[count] = usb_rx_buf[rx_offs];
397 #ifdef _DEBUG_USB_COMMS_
398 openjtag_debug_buffer(buffer,
399 DIV_ROUND_UP(openjtag_scan_result_buffer[res_count].bits, 8), DEBUG_TYPE_OCD_READ);
401 jtag_read_buffer(buffer, openjtag_scan_result_buffer[res_count].command);
403 if (openjtag_scan_result_buffer[res_count].buffer)
404 free(openjtag_scan_result_buffer[res_count].buffer);
410 openjtag_scan_result_count = 0;
415 static void openjtag_add_byte(char buf)
418 if (usb_tx_buf_offs == OPENJTAG_BUFFER_SIZE) {
419 DEBUG_JTAG_IO("Forcing execute_tap_queue");
420 DEBUG_JTAG_IO("TX Buff offs=%d", usb_tx_buf_offs);
421 openjtag_execute_tap_queue();
424 usb_tx_buf[usb_tx_buf_offs] = buf;
428 static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
431 /* Ensure space to send long chains */
432 /* We add two byte for each eight (or less) bits, one for command, one for data */
433 if (usb_tx_buf_offs + (DIV_ROUND_UP(length, 8) * 2) >= OPENJTAG_BUFFER_SIZE) {
434 DEBUG_JTAG_IO("Forcing execute_tap_queue from scan");
435 DEBUG_JTAG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
436 openjtag_execute_tap_queue();
439 openjtag_scan_result_buffer[openjtag_scan_result_count].bits = length;
440 openjtag_scan_result_buffer[openjtag_scan_result_count].command = scan_cmd;
441 openjtag_scan_result_buffer[openjtag_scan_result_count].buffer = buffer;
456 /* bits to transfer */
458 command |= bits << 5;
463 /* bits to transfer */
469 openjtag_add_byte(command);
470 openjtag_add_byte(buffer[count]);
474 openjtag_scan_result_count++;
477 static void openjtag_execute_reset(struct jtag_command *cmd)
480 DEBUG_JTAG_IO("reset trst: %i srst %i",
481 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
485 if (cmd->cmd.reset->trst) {
492 openjtag_add_byte(buf);
495 static void openjtag_execute_sleep(struct jtag_command *cmd)
497 jtag_sleep(cmd->cmd.sleep->us);
500 static void openjtag_set_state(uint8_t openocd_state)
502 int8_t state = openjtag_get_tap_state(openocd_state);
508 openjtag_add_byte(buf);
511 static void openjtag_execute_statemove(struct jtag_command *cmd)
513 DEBUG_JTAG_IO("state move to %i", cmd->cmd.statemove->end_state);
515 tap_set_end_state(cmd->cmd.statemove->end_state);
517 openjtag_set_state(cmd->cmd.statemove->end_state);
519 tap_set_state(tap_get_end_state());
523 static void openjtag_execute_scan(struct jtag_command *cmd)
526 int scan_size, old_state;
529 DEBUG_JTAG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
532 tap_set_end_state(cmd->cmd.scan->end_state);
533 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
535 #ifdef _DEBUG_USB_COMMS_
536 openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
539 old_state = tap_get_end_state();
540 openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
541 tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
542 tap_set_end_state(old_state);
544 openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
546 openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
547 tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
549 if (tap_get_state() != tap_get_end_state()) {
550 openjtag_set_state(tap_get_end_state());
551 tap_set_state(tap_get_end_state());
555 static void openjtag_execute_runtest(struct jtag_command *cmd)
558 tap_state_t end_state = cmd->cmd.runtest->end_state;
559 tap_set_end_state(end_state);
561 /* only do a state_move when we're not already in IDLE */
562 if (tap_get_state() != TAP_IDLE) {
563 openjtag_set_state(TAP_IDLE);
564 tap_set_state(TAP_IDLE);
567 if (cmd->cmd.runtest->num_cycles > 16)
568 LOG_WARNING("num_cycles > 16 on run test");
572 command |= ((cmd->cmd.runtest->num_cycles - 1) & 0x0F) << 4;
574 openjtag_add_byte(command);
576 tap_set_end_state(end_state);
577 if (tap_get_end_state() != tap_get_state()) {
578 openjtag_set_state(end_state);
579 tap_set_state(end_state);
583 static void openjtag_execute_command(struct jtag_command *cmd)
585 DEBUG_JTAG_IO("openjtag_execute_command %i", cmd->type);
588 openjtag_execute_reset(cmd);
591 openjtag_execute_sleep(cmd);
594 openjtag_execute_statemove(cmd);
597 openjtag_execute_scan(cmd);
600 openjtag_execute_runtest(cmd);
603 /* jlink_execute_pathmove(cmd); break; */
605 LOG_ERROR("BUG: unknown Open JTAG command type encountered");
610 static int openjtag_execute_queue(void)
612 struct jtag_command *cmd = jtag_command_queue;
614 while (cmd != NULL) {
615 openjtag_execute_command(cmd);
619 return openjtag_execute_tap_queue();
622 static int openjtag_speed_div(int speed, int *khz)
629 static int openjtag_khz(int khz, int *jtag_speed)
634 else if (khz >= 24000)
636 else if (khz >= 12000)
638 else if (khz >= 6000)
640 else if (khz >= 3000)
642 else if (khz >= 1500)
652 COMMAND_HANDLER(openjtag_handle_device_desc_command)
655 openjtag_device_desc = strdup(CMD_ARGV[0]);
657 LOG_ERROR("require exactly one argument to "
658 "openjtag_device_desc <description>");
663 static const struct command_registration openjtag_command_handlers[] = {
665 .name = "openjtag_device_desc",
666 .handler = openjtag_handle_device_desc_command,
667 .mode = COMMAND_CONFIG,
668 .help = "set the USB device description of the OpenJTAG",
669 .usage = "description-string",
671 COMMAND_REGISTRATION_DONE
674 struct jtag_interface openjtag_interface = {
676 .commands = openjtag_command_handlers,
678 .execute_queue = openjtag_execute_queue,
679 .speed = openjtag_speed,
680 .speed_div = openjtag_speed_div,
682 .init = openjtag_init,
683 .quit = openjtag_quit,