1 /***************************************************************************
2 * Copyright (C) 2010 Serge Vakulenko *
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, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <jtag/commands.h>
31 #include <helper/time_support.h>
32 #include "libusb1_common.h"
42 * Bit 7 (0x80, pin 6, RI ): unused.
43 * Bit 6 (0x40, pin 10,DCD): /SYSRST output.
44 * Bit 5 (0x20, pin 9, DSR): unused.
45 * Bit 4 (0x10, pin 2, DTR): /TRST output.
46 * Bit 3 (0x08, pin 11,CTS): TMS output.
47 * Bit 2 (0x04, pin 3, RTS): TDO input.
48 * Bit 1 (0x02, pin 5, RXD): TDI output.
49 * Bit 0 (0x01, pin 1, TXD): TCK output.
51 * Sync bit bang mode is implemented as described in FTDI Application
52 * Note AN232R-01: "Bit Bang Modes for the FT232R and FT245R".
56 #define READ_TDO (1 << 2)
58 #define NTRST (1 << 4)
59 #define NSYSRST (1 << 6)
68 #define SIO_RESET 0 /* Reset the port */
69 #define SIO_MODEM_CTRL 1 /* Set the modem control register */
70 #define SIO_SET_FLOW_CTRL 2 /* Set flow control register */
71 #define SIO_SET_BAUD_RATE 3 /* Set baud rate */
72 #define SIO_SET_DATA 4 /* Set the data characteristics of the port */
73 #define SIO_POLL_MODEM_STATUS 5
74 #define SIO_SET_EVENT_CHAR 6
75 #define SIO_SET_ERROR_CHAR 7
76 #define SIO_SET_LATENCY_TIMER 9
77 #define SIO_GET_LATENCY_TIMER 10
78 #define SIO_SET_BITMODE 11
79 #define SIO_READ_PINS 12
80 #define SIO_READ_EEPROM 0x90
81 #define SIO_WRITE_EEPROM 0x91
82 #define SIO_ERASE_EEPROM 0x92
84 #define FT232R_BUF_SIZE 4000
86 static char *ft232r_serial_desc;
87 static uint16_t ft232r_vid = 0x0403; /* FTDI */
88 static uint16_t ft232r_pid = 0x6001; /* FT232R */
89 static jtag_libusb_device_handle *adapter;
91 static uint8_t *ft232r_output;
92 static size_t ft232r_output_len;
95 * Perform sync bitbang output/input transaction.
96 * Before call, an array ft232r_output[] should be filled with data to send.
97 * Counter ft232r_output_len contains the number of bytes to send.
98 * On return, received data is put back to array ft232r_output[].
100 static int ft232r_send_recv(void)
102 /* FIFO TX buffer has 128 bytes.
103 * FIFO RX buffer has 256 bytes.
104 * First two bytes of received packet contain contain modem
105 * and line status and are ignored.
106 * Unfortunately, transfer sizes bigger than 64 bytes
107 * frequently cause hang ups. */
108 assert(ft232r_output_len > 0);
110 size_t total_written = 0;
111 size_t total_read = 0;
112 int rxfifo_free = 128;
114 while (total_read < ft232r_output_len) {
116 int bytes_to_write = ft232r_output_len - total_written;
117 if (bytes_to_write > 64)
119 if (bytes_to_write > rxfifo_free)
120 bytes_to_write = rxfifo_free;
122 if (bytes_to_write) {
123 int n = jtag_libusb_bulk_write(adapter, IN_EP,
124 (char *) ft232r_output + total_written,
125 bytes_to_write, 1000);
128 LOG_ERROR("usb bulk write failed");
129 return ERROR_JTAG_DEVICE_ERROR;
139 int n = jtag_libusb_bulk_read(adapter, OUT_EP,
141 sizeof(reply), 1000);
144 LOG_ERROR("usb bulk read failed");
145 return ERROR_JTAG_DEVICE_ERROR;
148 /* Copy data, ignoring first 2 bytes. */
149 memcpy(ft232r_output + total_read, reply + 2, n - 2);
150 int bytes_read = n - 2;
151 total_read += bytes_read;
152 rxfifo_free += bytes_read;
153 if (total_read > total_written) {
154 LOG_ERROR("read more bytes than wrote");
155 return ERROR_JTAG_DEVICE_ERROR;
159 ft232r_output_len = 0;
164 * Add one TCK/TMS/TDI sample to send buffer.
166 static void ft232r_write(int tck, int tms, int tdi)
168 unsigned out_value = NTRST | NSYSRST;
176 if (ft232r_output_len >= FT232R_BUF_SIZE) {
177 /* FIXME: should we just execute queue here? */
178 LOG_ERROR("ft232r_write: buffer overflow");
181 ft232r_output[ft232r_output_len++] = out_value;
185 * Control /TRST and /SYSRST pins.
186 * Perform immediate bitbang transaction.
188 static void ft232r_reset(int trst, int srst)
190 unsigned out_value = NTRST | NSYSRST;
191 LOG_DEBUG("ft232r_reset(%d,%d)", trst, srst);
194 out_value &= ~NTRST; /* switch /TRST low */
196 out_value |= NTRST; /* switch /TRST high */
199 out_value &= ~NSYSRST; /* switch /SYSRST low */
201 out_value |= NSYSRST; /* switch /SYSRST high */
203 if (ft232r_output_len >= FT232R_BUF_SIZE) {
204 /* FIXME: should we just execute queue here? */
205 LOG_ERROR("ft232r_write: buffer overflow");
209 ft232r_output[ft232r_output_len++] = out_value;
213 static int ft232r_speed(int divisor)
215 int baud = (divisor == 0) ? 3000000 :
216 (divisor == 1) ? 2000000 :
218 LOG_DEBUG("ft232r_speed(%d) rate %d bits/sec", divisor, baud);
220 if (jtag_libusb_control_transfer(adapter,
221 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
222 SIO_SET_BAUD_RATE, divisor, 0, 0, 0, 1000) != 0) {
223 LOG_ERROR("cannot set baud rate");
224 return ERROR_JTAG_DEVICE_ERROR;
229 static int ft232r_init(void)
231 uint16_t avids[] = {ft232r_vid, 0};
232 uint16_t apids[] = {ft232r_pid, 0};
233 if (jtag_libusb_open(avids, apids, ft232r_serial_desc, &adapter)) {
234 LOG_ERROR("ft232r not found: vid=%04x, pid=%04x, serial=%s\n",
235 ft232r_vid, ft232r_pid, (ft232r_serial_desc == NULL) ? "[any]" : ft232r_serial_desc);
236 return ERROR_JTAG_INIT_FAILED;
239 libusb_detach_kernel_driver(adapter, 0);
241 if (jtag_libusb_claim_interface(adapter, 0)) {
242 LOG_ERROR("unable to claim interface");
243 return ERROR_JTAG_INIT_FAILED;
246 /* Reset the device. */
247 if (jtag_libusb_control_transfer(adapter,
248 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
249 SIO_RESET, 0, 0, 0, 0, 1000) != 0) {
250 LOG_ERROR("unable to reset device");
251 return ERROR_JTAG_INIT_FAILED;
254 /* Sync bit bang mode. */
255 if (jtag_libusb_control_transfer(adapter,
256 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
257 SIO_SET_BITMODE, TCK | TDI | TMS | NTRST | NSYSRST | 0x400,
258 0, 0, 0, 1000) != 0) {
259 LOG_ERROR("cannot set sync bitbang mode");
260 return ERROR_JTAG_INIT_FAILED;
263 /* Exactly 500 nsec between updates. */
264 unsigned divisor = 1;
265 unsigned char latency_timer = 1;
267 /* Frequency divisor is 14-bit non-zero value. */
268 if (jtag_libusb_control_transfer(adapter,
269 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
270 SIO_SET_BAUD_RATE, divisor,
271 0, 0, 0, 1000) != 0) {
272 LOG_ERROR("cannot set baud rate");
273 return ERROR_JTAG_INIT_FAILED;
275 if (jtag_libusb_control_transfer(adapter,
276 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
277 SIO_SET_LATENCY_TIMER, latency_timer, 0, 0, 0, 1000) != 0) {
278 LOG_ERROR("unable to set latency timer");
279 return ERROR_JTAG_INIT_FAILED;
282 ft232r_output = malloc(FT232R_BUF_SIZE);
283 if (ft232r_output == NULL) {
284 LOG_ERROR("Unable to allocate memory for the buffer");
285 return ERROR_JTAG_INIT_FAILED;
291 static int ft232r_quit(void)
293 if (jtag_libusb_release_interface(adapter, 0) != 0)
294 LOG_ERROR("usb release interface failed");
296 jtag_libusb_close(adapter);
302 static int ft232r_speed_div(int divisor, int *khz)
304 /* Maximum 3 Mbaud for bit bang mode. */
307 else if (divisor == 1)
310 *khz = 3000 / divisor;
314 static int ft232r_khz(int khz, int *divisor)
317 LOG_DEBUG("RCLK not supported");
321 /* Calculate frequency divisor. */
323 *divisor = 0; /* Special case: 3 MHz */
325 *divisor = 1; /* Special case: 2 MHz */
327 *divisor = (2*3000 / khz + 1) / 2;
328 if (*divisor > 0x3FFF)
334 COMMAND_HANDLER(ft232r_handle_serial_desc_command)
337 ft232r_serial_desc = strdup(CMD_ARGV[0]);
339 LOG_ERROR("require exactly one argument to "
340 "ft232r_serial_desc <serial>");
344 COMMAND_HANDLER(ft232r_handle_vid_pid_command)
347 LOG_WARNING("ignoring extra IDs in ft232r_vid_pid "
348 "(maximum is 1 pair)");
352 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], ft232r_vid);
353 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], ft232r_pid);
355 LOG_WARNING("incomplete ft232r_vid_pid configuration");
360 static const struct command_registration ft232r_command_handlers[] = {
362 .name = "ft232r_serial_desc",
363 .handler = ft232r_handle_serial_desc_command,
364 .mode = COMMAND_CONFIG,
365 .help = "USB serial descriptor of the adapter",
366 .usage = "serial string",
369 .name = "ft232r_vid_pid",
370 .handler = ft232r_handle_vid_pid_command,
371 .mode = COMMAND_CONFIG,
372 .help = "USB VID and PID of the adapter",
375 COMMAND_REGISTRATION_DONE
379 * Synchronous bitbang protocol implementation.
382 static void syncbb_end_state(tap_state_t state)
384 if (tap_is_state_stable(state))
385 tap_set_end_state(state);
387 LOG_ERROR("BUG: %i is not a valid end state", state);
392 static void syncbb_state_move(int skip)
395 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
396 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
398 for (i = skip; i < tms_count; i++) {
399 tms = (tms_scan >> i) & 1;
400 ft232r_write(0, tms, 0);
401 ft232r_write(1, tms, 0);
403 ft232r_write(0, tms, 0);
405 tap_set_state(tap_get_end_state());
409 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
410 * (or SWD) state machine.
412 static int syncbb_execute_tms(struct jtag_command *cmd)
414 unsigned num_bits = cmd->cmd.tms->num_bits;
415 const uint8_t *bits = cmd->cmd.tms->bits;
417 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
420 for (unsigned i = 0; i < num_bits; i++) {
421 tms = ((bits[i/8] >> (i % 8)) & 1);
422 ft232r_write(0, tms, 0);
423 ft232r_write(1, tms, 0);
425 ft232r_write(0, tms, 0);
430 static void syncbb_path_move(struct pathmove_command *cmd)
432 int num_states = cmd->num_states;
438 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) {
440 } else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count]) {
443 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
444 tap_state_name(tap_get_state()),
445 tap_state_name(cmd->path[state_count]));
449 ft232r_write(0, tms, 0);
450 ft232r_write(1, tms, 0);
452 tap_set_state(cmd->path[state_count]);
457 ft232r_write(0, tms, 0);
459 tap_set_end_state(tap_get_state());
462 static void syncbb_runtest(int num_cycles)
466 tap_state_t saved_end_state = tap_get_end_state();
468 /* only do a state_move when we're not already in IDLE */
469 if (tap_get_state() != TAP_IDLE) {
470 syncbb_end_state(TAP_IDLE);
471 syncbb_state_move(0);
474 /* execute num_cycles */
475 for (i = 0; i < num_cycles; i++) {
476 ft232r_write(0, 0, 0);
477 ft232r_write(1, 0, 0);
479 ft232r_write(0, 0, 0);
481 /* finish in end_state */
482 syncbb_end_state(saved_end_state);
483 if (tap_get_state() != tap_get_end_state())
484 syncbb_state_move(0);
488 * Function syncbb_stableclocks
489 * issues a number of clock cycles while staying in a stable state.
490 * Because the TMS value required to stay in the RESET state is a 1, whereas
491 * the TMS value required to stay in any of the other stable states is a 0,
492 * this function checks the current stable state to decide on the value of TMS
495 static void syncbb_stableclocks(int num_cycles)
497 int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
500 /* send num_cycles clocks onto the cable */
501 for (i = 0; i < num_cycles; i++) {
502 ft232r_write(1, tms, 0);
503 ft232r_write(0, tms, 0);
507 static void syncbb_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
509 tap_state_t saved_end_state = tap_get_end_state();
510 int bit_cnt, bit0_index;
512 if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
514 syncbb_end_state(TAP_IRSHIFT);
516 syncbb_end_state(TAP_DRSHIFT);
518 syncbb_state_move(0);
519 syncbb_end_state(saved_end_state);
522 bit0_index = ft232r_output_len;
523 for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
524 int tms = (bit_cnt == scan_size-1) ? 1 : 0;
526 int bytec = bit_cnt/8;
527 int bcval = 1 << (bit_cnt % 8);
529 /* if we're just reading the scan, but don't care about the output
530 * default to outputting 'low', this also makes valgrind traces more readable,
531 * as it removes the dependency on an uninitialised value
534 if ((type != SCAN_IN) && (buffer[bytec] & bcval))
537 ft232r_write(0, tms, tdi);
538 ft232r_write(1, tms, tdi);
541 if (tap_get_state() != tap_get_end_state()) {
542 /* we *KNOW* the above loop transitioned out of
543 * the shift state, so we skip the first state
544 * and move directly to the end state.
546 syncbb_state_move(1);
550 if (type != SCAN_OUT)
551 for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
552 int bytec = bit_cnt/8;
553 int bcval = 1 << (bit_cnt % 8);
554 int val = ft232r_output[bit0_index + bit_cnt*2 + 1];
557 buffer[bytec] |= bcval;
559 buffer[bytec] &= ~bcval;
563 static int syncbb_execute_queue(void)
565 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
571 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
572 * that wasn't handled by a caller-provided error handler
576 /* ft232r_blink(1);*/
581 LOG_DEBUG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
583 if ((cmd->cmd.reset->trst == 1) ||
584 (cmd->cmd.reset->srst &&
585 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST))) {
586 tap_set_state(TAP_RESET);
588 ft232r_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
592 LOG_DEBUG_IO("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles,
593 tap_state_name(cmd->cmd.runtest->end_state));
595 syncbb_end_state(cmd->cmd.runtest->end_state);
596 syncbb_runtest(cmd->cmd.runtest->num_cycles);
599 case JTAG_STABLECLOCKS:
600 /* this is only allowed while in a stable state. A check for a stable
601 * state was done in jtag_add_clocks()
603 syncbb_stableclocks(cmd->cmd.stableclocks->num_cycles);
606 case JTAG_TLR_RESET: /* renamed from JTAG_STATEMOVE */
607 LOG_DEBUG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
609 syncbb_end_state(cmd->cmd.statemove->end_state);
610 syncbb_state_move(0);
614 LOG_DEBUG_IO("pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
615 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
617 syncbb_path_move(cmd->cmd.pathmove);
621 LOG_DEBUG_IO("%s scan end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
622 tap_state_name(cmd->cmd.scan->end_state));
624 syncbb_end_state(cmd->cmd.scan->end_state);
625 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
626 type = jtag_scan_type(cmd->cmd.scan);
627 syncbb_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
628 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
629 retval = ERROR_JTAG_QUEUE_FAILED;
635 LOG_DEBUG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
637 jtag_sleep(cmd->cmd.sleep->us);
641 retval = syncbb_execute_tms(cmd);
644 LOG_ERROR("BUG: unknown JTAG command type encountered");
647 if (ft232r_output_len > 0)
651 /* ft232r_blink(0);*/
656 struct jtag_interface ft232r_interface = {
658 .commands = ft232r_command_handlers,
659 .transports = jtag_only,
660 .supported = DEBUG_CAP_TMS_SEQ,
662 .execute_queue = syncbb_execute_queue,
664 .speed = ft232r_speed,
667 .speed_div = ft232r_speed_div,