1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
30 /* This code uses information contained in the MPSSE specification which was
32 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
33 * Hereafter this is called the "MPSSE Spec".
35 * The datasheet for the ftdichip.com's FT2232D part is here:
36 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
43 /* project specific includes */
44 #include "interface.h"
46 #include "time_support.h"
54 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
55 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
56 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
57 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
60 /* FT2232 access library includes */
61 #if BUILD_FT2232_FTD2XX == 1
63 #elif BUILD_FT2232_LIBFTDI == 1
67 /* max TCK for the high speed devices 30000 kHz */
68 #define FTDI_2232H_4232H_MAX_TCK 30000
69 /* max TCK for the full speed devices 6000 kHz */
70 #define FTDI_2232C_MAX_TCK 6000
71 /* this speed value tells that RTCK is requested */
74 #ifndef BUILD_FT2232_HIGHSPEED
75 #if BUILD_FT2232_FTD2XX == 1
76 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
77 #elif BUILD_FT2232_LIBFTDI == 1
78 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
82 static int ft2232_execute_queue(void);
83 static int ft2232_speed(int speed);
84 static int ft2232_speed_div(int speed, int* khz);
85 static int ft2232_khz(int khz, int* jtag_speed);
86 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
87 static int ft2232_init(void);
88 static int ft2232_quit(void);
90 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
91 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
92 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
93 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
94 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
97 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
98 * stable state. Calling code must ensure that current state is stable,
99 * that verification is not done in here.
101 * @param num_cycles The number of clocks cycles to send.
102 * @param cmd The command to send.
104 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
106 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
108 static char * ft2232_device_desc_A = NULL;
109 static char* ft2232_device_desc = NULL;
110 static char* ft2232_serial = NULL;
111 static char* ft2232_layout = NULL;
112 static uint8_t ft2232_latency = 2;
113 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
115 #define MAX_USB_IDS 8
116 /* vid = pid = 0 marks the end of the list */
117 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
118 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
120 typedef struct ft2232_layout_s
124 void (*reset)(int trst, int srst);
128 /* init procedures for supported layouts */
129 static int usbjtag_init(void);
130 static int jtagkey_init(void);
131 static int olimex_jtag_init(void);
132 static int flyswatter_init(void);
133 static int turtle_init(void);
134 static int comstick_init(void);
135 static int stm32stick_init(void);
136 static int axm0432_jtag_init(void);
137 static int sheevaplug_init(void);
138 static int icebear_jtag_init(void);
139 static int cortino_jtag_init(void);
141 /* reset procedures for supported layouts */
142 static void usbjtag_reset(int trst, int srst);
143 static void jtagkey_reset(int trst, int srst);
144 static void olimex_jtag_reset(int trst, int srst);
145 static void flyswatter_reset(int trst, int srst);
146 static void turtle_reset(int trst, int srst);
147 static void comstick_reset(int trst, int srst);
148 static void stm32stick_reset(int trst, int srst);
149 static void axm0432_jtag_reset(int trst, int srst);
150 static void sheevaplug_reset(int trst, int srst);
151 static void icebear_jtag_reset(int trst, int srst);
153 /* blink procedures for layouts that support a blinking led */
154 static void olimex_jtag_blink(void);
155 static void flyswatter_jtag_blink(void);
156 static void turtle_jtag_blink(void);
158 static const ft2232_layout_t ft2232_layouts[] =
160 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
161 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
162 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
163 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
164 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
165 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
166 { "luminary_icdi", usbjtag_init, usbjtag_reset, NULL },
167 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
168 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
169 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
170 { "comstick", comstick_init, comstick_reset, NULL },
171 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
172 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
173 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
174 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
175 { "cortino", cortino_jtag_init, comstick_reset, NULL },
176 { NULL, NULL, NULL, NULL },
179 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
181 static const ft2232_layout_t *layout;
182 static uint8_t low_output = 0x0;
183 static uint8_t low_direction = 0x0;
184 static uint8_t high_output = 0x0;
185 static uint8_t high_direction = 0x0;
187 #if BUILD_FT2232_FTD2XX == 1
188 static FT_HANDLE ftdih = NULL;
189 static FT_DEVICE ftdi_device = 0;
190 #elif BUILD_FT2232_LIBFTDI == 1
191 static struct ftdi_context ftdic;
192 static enum ftdi_chip_type ftdi_device;
195 static jtag_command_t* first_unsent; /* next command that has to be sent */
196 static int require_send;
198 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
200 "There is a significant difference between libftdi and libftd2xx. The latter
201 one allows to schedule up to 64*64 bytes of result data while libftdi fails
202 with more than 4*64. As a consequence, the FT2232 driver is forced to
203 perform around 16x more USB transactions for long command streams with TDO
204 capture when running with libftdi."
207 #define FT2232_BUFFER_SIZE 131072
208 a comment would have been nice.
211 #define FT2232_BUFFER_SIZE 131072
213 static uint8_t* ft2232_buffer = NULL;
214 static int ft2232_buffer_size = 0;
215 static int ft2232_read_pointer = 0;
216 static int ft2232_expect_read = 0;
219 * Function buffer_write
220 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
221 * @param val is the byte to send.
223 static inline void buffer_write(uint8_t val)
225 assert(ft2232_buffer);
226 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
227 ft2232_buffer[ft2232_buffer_size++] = val;
231 * Function buffer_read
232 * returns a byte from the byte buffer.
234 static inline uint8_t buffer_read(void)
236 assert(ft2232_buffer);
237 assert(ft2232_read_pointer < ft2232_buffer_size);
238 return ft2232_buffer[ft2232_read_pointer++];
242 * Clocks out \a bit_count bits on the TMS line, starting with the least
243 * significant bit of tms_bits and progressing to more significant bits.
244 * Rigorous state transition logging is done here via tap_set_state().
246 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
247 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
248 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
249 * is often used for this, 0x4b.
251 * @param tms_bits Holds the sequence of bits to send.
252 * @param tms_count Tells how many bits in the sequence.
253 * @param tdi_bit A single bit to pass on to TDI before the first TCK
254 * cycle and held static for the duration of TMS clocking.
256 * See the MPSSE spec referenced above.
258 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
262 int tms_ndx; /* bit index into tms_byte */
264 assert(tms_count > 0);
266 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
267 mpsse_cmd, tms_bits, tms_count);
269 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
271 bool bit = tms_bits & 1;
274 tms_byte |= (1 << tms_ndx);
276 /* always do state transitions in public view */
277 tap_set_state(tap_state_transition(tap_get_state(), bit));
279 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
284 if (tms_ndx == 7 || i == tms_count-1)
286 buffer_write(mpsse_cmd);
287 buffer_write(tms_ndx - 1);
289 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
290 TMS/CS and is held static for the duration of TMS/CS clocking.
292 buffer_write(tms_byte | (tdi_bit << 7));
298 * Function get_tms_buffer_requirements
299 * returns what clock_tms() will consume if called with
302 static inline int get_tms_buffer_requirements(int bit_count)
304 return ((bit_count + 6)/7) * 3;
308 * Function move_to_state
309 * moves the TAP controller from the current state to a
310 * \a goal_state through a path given by tap_get_tms_path(). State transition
311 * logging is performed by delegation to clock_tms().
313 * @param goal_state is the destination state for the move.
315 static void move_to_state(tap_state_t goal_state)
317 tap_state_t start_state = tap_get_state();
319 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
320 lookup of the required TMS pattern to move to this state from the
324 /* do the 2 lookups */
325 int tms_bits = tap_get_tms_path(start_state, goal_state);
326 int tms_count = tap_get_tms_path_len(start_state, goal_state);
328 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
330 clock_tms(0x4b, tms_bits, tms_count, 0);
333 jtag_interface_t ft2232_interface =
336 .execute_queue = ft2232_execute_queue,
337 .speed = ft2232_speed,
338 .speed_div = ft2232_speed_div,
340 .register_commands = ft2232_register_commands,
345 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
347 #if BUILD_FT2232_FTD2XX == 1
349 DWORD dw_bytes_written;
350 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
352 *bytes_written = dw_bytes_written;
353 LOG_ERROR("FT_Write returned: %lu", status);
354 return ERROR_JTAG_DEVICE_ERROR;
358 *bytes_written = dw_bytes_written;
361 #elif BUILD_FT2232_LIBFTDI == 1
363 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
366 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
367 return ERROR_JTAG_DEVICE_ERROR;
371 *bytes_written = retval;
377 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
379 #if BUILD_FT2232_FTD2XX == 1
385 while ((*bytes_read < size) && timeout--)
387 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
388 *bytes_read, &dw_bytes_read)) != FT_OK)
391 LOG_ERROR("FT_Read returned: %lu", status);
392 return ERROR_JTAG_DEVICE_ERROR;
394 *bytes_read += dw_bytes_read;
397 #elif BUILD_FT2232_LIBFTDI == 1
402 while ((*bytes_read < size) && timeout--)
404 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
407 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
408 return ERROR_JTAG_DEVICE_ERROR;
410 *bytes_read += retval;
415 if (*bytes_read < size)
417 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)",
418 (unsigned int)(*bytes_read),
420 return ERROR_JTAG_DEVICE_ERROR;
426 static bool ft2232_device_is_highspeed(void)
428 #if BUILD_FT2232_FTD2XX == 1
429 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
430 #elif BUILD_FT2232_LIBFTDI == 1
431 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
436 * Commands that only apply to the FT2232H and FT4232H devices.
437 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
438 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
441 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
443 uint8_t buf = enable ? 0x96 : 0x97;
444 LOG_DEBUG("%2.2x", buf);
446 uint32_t bytes_written;
447 int retval = ft2232_write(&buf, 1, &bytes_written);
448 if ((ERROR_OK != retval) || (bytes_written != 1))
450 LOG_ERROR("couldn't write command to %s adaptive clocking"
451 , enable ? "enable" : "disable");
459 * Enable/disable the clk divide by 5 of the 60MHz master clock.
460 * This result in a JTAG clock speed range of 91.553Hz-6MHz
461 * respective 457.763Hz-30MHz.
463 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
465 uint32_t bytes_written;
466 uint8_t buf = enable ? 0x8b : 0x8a;
467 int retval = ft2232_write(&buf, 1, &bytes_written);
468 if ((ERROR_OK != retval) || (bytes_written != 1))
470 LOG_ERROR("couldn't write command to %s clk divide by 5"
471 , enable ? "enable" : "disable");
472 return ERROR_JTAG_INIT_FAILED;
474 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
475 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
480 static int ft2232_speed(int speed)
484 uint32_t bytes_written;
487 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
488 if (ft2232_device_is_highspeed())
489 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
490 else if (enable_adaptive_clocking)
492 LOG_ERROR("ft2232 device %lu does not support RTCK"
493 , (long unsigned int)ftdi_device);
497 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
500 buf[0] = 0x86; /* command "set divisor" */
501 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
502 buf[2] = (speed >> 8) & 0xff; /* valueH */
504 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
505 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
507 LOG_ERROR("couldn't set FT2232 TCK speed");
514 static int ft2232_speed_div(int speed, int* khz)
516 /* Take a look in the FT2232 manual,
517 * AN2232C-01 Command Processor for
518 * MPSSE and MCU Host Bus. Chapter 3.8 */
520 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
525 static int ft2232_khz(int khz, int* jtag_speed)
529 if (ft2232_device_is_highspeed())
531 *jtag_speed = RTCK_SPEED;
536 LOG_DEBUG("RCLK not supported");
541 /* Take a look in the FT2232 manual,
542 * AN2232C-01 Command Processor for
543 * MPSSE and MCU Host Bus. Chapter 3.8
545 * We will calc here with a multiplier
546 * of 10 for better rounding later. */
548 /* Calc speed, (ft2232_max_tck / khz) - 1 */
549 /* Use 65000 for better rounding */
550 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
552 /* Add 0.9 for rounding */
555 /* Calc real speed */
556 *jtag_speed = *jtag_speed / 10;
558 /* Check if speed is greater than 0 */
564 /* Check max value */
565 if (*jtag_speed > 0xFFFF)
567 *jtag_speed = 0xFFFF;
573 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
575 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
576 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
577 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
578 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
579 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
580 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
581 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
582 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
583 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
584 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
588 static void ft2232_end_state(tap_state_t state)
590 if (tap_is_state_stable(state))
591 tap_set_end_state(state);
594 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
599 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
601 int num_bytes = (scan_size + 7) / 8;
602 int bits_left = scan_size;
605 while (num_bytes-- > 1)
607 buffer[cur_byte++] = buffer_read();
611 buffer[cur_byte] = 0x0;
613 /* There is one more partial byte left from the clock data in/out instructions */
616 buffer[cur_byte] = buffer_read() >> 1;
618 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
619 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
622 static void ft2232_debug_dump_buffer(void)
628 for (i = 0; i < ft2232_buffer_size; i++)
630 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
633 LOG_DEBUG("%s", line);
639 LOG_DEBUG("%s", line);
642 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
649 uint32_t bytes_written = 0;
650 uint32_t bytes_read = 0;
652 #ifdef _DEBUG_USB_IO_
653 struct timeval start, inter, inter2, end;
654 struct timeval d_inter, d_inter2, d_end;
657 #ifdef _DEBUG_USB_COMMS_
658 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
659 ft2232_debug_dump_buffer();
662 #ifdef _DEBUG_USB_IO_
663 gettimeofday(&start, NULL);
666 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
668 LOG_ERROR("couldn't write MPSSE commands to FT2232");
672 #ifdef _DEBUG_USB_IO_
673 gettimeofday(&inter, NULL);
676 if (ft2232_expect_read)
679 ft2232_buffer_size = 0;
681 #ifdef _DEBUG_USB_IO_
682 gettimeofday(&inter2, NULL);
685 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
687 LOG_ERROR("couldn't read from FT2232");
691 #ifdef _DEBUG_USB_IO_
692 gettimeofday(&end, NULL);
694 timeval_subtract(&d_inter, &inter, &start);
695 timeval_subtract(&d_inter2, &inter2, &start);
696 timeval_subtract(&d_end, &end, &start);
698 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
699 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
700 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
701 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
704 ft2232_buffer_size = bytes_read;
706 if (ft2232_expect_read != ft2232_buffer_size)
708 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
711 ft2232_debug_dump_buffer();
716 #ifdef _DEBUG_USB_COMMS_
717 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
718 ft2232_debug_dump_buffer();
722 ft2232_expect_read = 0;
723 ft2232_read_pointer = 0;
725 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
726 * that wasn't handled by a caller-provided error handler
736 type = jtag_scan_type(cmd->cmd.scan);
737 if (type != SCAN_OUT)
739 scan_size = jtag_scan_size(cmd->cmd.scan);
740 buffer = calloc(CEIL(scan_size, 8), 1);
741 ft2232_read_scan(type, buffer, scan_size);
742 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
743 retval = ERROR_JTAG_QUEUE_FAILED;
755 ft2232_buffer_size = 0;
761 * Function ft2232_add_pathmove
762 * moves the TAP controller from the current state to a new state through the
763 * given path, where path is an array of tap_state_t's.
765 * @param path is an array of tap_stat_t which gives the states to traverse through
766 * ending with the last state at path[num_states-1]
767 * @param num_states is the count of state steps to move through
769 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
773 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
777 /* this loop verifies that the path is legal and logs each state in the path */
780 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
782 int num_states_batch = num_states > 7 ? 7 : num_states;
784 /* command "Clock Data to TMS/CS Pin (no Read)" */
787 /* number of states remaining */
788 buffer_write(num_states_batch - 1);
790 while (num_states_batch--) {
791 /* either TMS=0 or TMS=1 must work ... */
792 if (tap_state_transition(tap_get_state(), false)
793 == path[state_count])
794 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
795 else if (tap_state_transition(tap_get_state(), true)
796 == path[state_count])
797 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
799 /* ... or else the caller goofed BADLY */
801 LOG_ERROR("BUG: %s -> %s isn't a valid "
802 "TAP state transition",
803 tap_state_name(tap_get_state()),
804 tap_state_name(path[state_count]));
808 tap_set_state(path[state_count]);
813 buffer_write(tms_byte);
815 tap_set_end_state(tap_get_state());
818 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
820 int num_bytes = (scan_size + 7) / 8;
821 int bits_left = scan_size;
827 if (tap_get_state() != TAP_DRSHIFT)
829 move_to_state(TAP_DRSHIFT);
834 if (tap_get_state() != TAP_IRSHIFT)
836 move_to_state(TAP_IRSHIFT);
840 /* add command for complete bytes */
841 while (num_bytes > 1)
846 /* Clock Data Bytes In and Out LSB First */
848 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
850 else if (type == SCAN_OUT)
852 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
854 /* LOG_DEBUG("added TDI bytes (o)"); */
856 else if (type == SCAN_IN)
858 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
860 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
863 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
864 num_bytes -= thisrun_bytes;
866 buffer_write((uint8_t) (thisrun_bytes - 1));
867 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
871 /* add complete bytes */
872 while (thisrun_bytes-- > 0)
874 buffer_write(buffer[cur_byte++]);
878 else /* (type == SCAN_IN) */
880 bits_left -= 8 * (thisrun_bytes);
884 /* the most signifcant bit is scanned during TAP movement */
886 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
890 /* process remaining bits but the last one */
895 /* Clock Data Bits In and Out LSB First */
897 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
899 else if (type == SCAN_OUT)
901 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
903 /* LOG_DEBUG("added TDI bits (o)"); */
905 else if (type == SCAN_IN)
907 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
909 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
912 buffer_write(bits_left - 2);
914 buffer_write(buffer[cur_byte]);
917 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
918 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
922 /* Clock Data Bits In and Out LSB First */
924 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
926 else if (type == SCAN_OUT)
928 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
930 /* LOG_DEBUG("added TDI bits (o)"); */
932 else if (type == SCAN_IN)
934 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
936 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
939 buffer_write(last_bit);
947 /* move from Shift-IR/DR to end state */
948 if (type != SCAN_OUT)
950 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
951 /* This must be coordinated with the bit shifts in ft2232_read_scan */
954 /* Clock Data to TMS/CS Pin with Read */
959 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
960 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
961 /* Clock Data to TMS/CS Pin (no Read) */
965 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
966 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
969 if (tap_get_state() != tap_get_end_state())
971 move_to_state(tap_get_end_state());
975 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
977 int num_bytes = (scan_size + 7) / 8;
978 int bits_left = scan_size;
981 uint8_t* receive_buffer = malloc(CEIL(scan_size, 8));
982 uint8_t* receive_pointer = receive_buffer;
983 uint32_t bytes_written;
986 int thisrun_read = 0;
990 LOG_ERROR("BUG: large IR scans are not supported");
994 if (tap_get_state() != TAP_DRSHIFT)
996 move_to_state(TAP_DRSHIFT);
999 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1001 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1004 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1005 ft2232_buffer_size, (int)bytes_written);
1006 ft2232_buffer_size = 0;
1008 /* add command for complete bytes */
1009 while (num_bytes > 1)
1013 if (type == SCAN_IO)
1015 /* Clock Data Bytes In and Out LSB First */
1017 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1019 else if (type == SCAN_OUT)
1021 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1023 /* LOG_DEBUG("added TDI bytes (o)"); */
1025 else if (type == SCAN_IN)
1027 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1029 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1032 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1033 thisrun_read = thisrun_bytes;
1034 num_bytes -= thisrun_bytes;
1035 buffer_write((uint8_t) (thisrun_bytes - 1));
1036 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1038 if (type != SCAN_IN)
1040 /* add complete bytes */
1041 while (thisrun_bytes-- > 0)
1043 buffer_write(buffer[cur_byte]);
1048 else /* (type == SCAN_IN) */
1050 bits_left -= 8 * (thisrun_bytes);
1053 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1055 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1058 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1060 (int)bytes_written);
1061 ft2232_buffer_size = 0;
1063 if (type != SCAN_OUT)
1065 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1067 LOG_ERROR("couldn't read from FT2232");
1070 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1073 receive_pointer += bytes_read;
1079 /* the most signifcant bit is scanned during TAP movement */
1080 if (type != SCAN_IN)
1081 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1085 /* process remaining bits but the last one */
1088 if (type == SCAN_IO)
1090 /* Clock Data Bits In and Out LSB First */
1092 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1094 else if (type == SCAN_OUT)
1096 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1098 /* LOG_DEBUG("added TDI bits (o)"); */
1100 else if (type == SCAN_IN)
1102 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1104 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1106 buffer_write(bits_left - 2);
1107 if (type != SCAN_IN)
1108 buffer_write(buffer[cur_byte]);
1110 if (type != SCAN_OUT)
1114 if (tap_get_end_state() == TAP_DRSHIFT)
1116 if (type == SCAN_IO)
1118 /* Clock Data Bits In and Out LSB First */
1120 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1122 else if (type == SCAN_OUT)
1124 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1126 /* LOG_DEBUG("added TDI bits (o)"); */
1128 else if (type == SCAN_IN)
1130 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1132 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1135 buffer_write(last_bit);
1139 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1140 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1143 /* move from Shift-IR/DR to end state */
1144 if (type != SCAN_OUT)
1146 /* Clock Data to TMS/CS Pin with Read */
1148 /* LOG_DEBUG("added TMS scan (read)"); */
1152 /* Clock Data to TMS/CS Pin (no Read) */
1154 /* LOG_DEBUG("added TMS scan (no read)"); */
1157 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1158 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1161 if (type != SCAN_OUT)
1164 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1166 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1169 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1171 (int)bytes_written);
1172 ft2232_buffer_size = 0;
1174 if (type != SCAN_OUT)
1176 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1178 LOG_ERROR("couldn't read from FT2232");
1181 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1184 receive_pointer += bytes_read;
1190 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1192 int predicted_size = 3;
1193 int num_bytes = (scan_size - 1) / 8;
1195 if (tap_get_state() != TAP_DRSHIFT)
1196 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1198 if (type == SCAN_IN) /* only from device to host */
1200 /* complete bytes */
1201 predicted_size += CEIL(num_bytes, 65536) * 3;
1203 /* remaining bits - 1 (up to 7) */
1204 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1206 else /* host to device, or bidirectional */
1208 /* complete bytes */
1209 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
1211 /* remaining bits -1 (up to 7) */
1212 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1215 return predicted_size;
1218 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1220 int predicted_size = 0;
1222 if (type != SCAN_OUT)
1224 /* complete bytes */
1225 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
1227 /* remaining bits - 1 */
1228 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1230 /* last bit (from TMS scan) */
1231 predicted_size += 1;
1234 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1236 return predicted_size;
1239 static void usbjtag_reset(int trst, int srst)
1241 enum reset_types jtag_reset_config = jtag_get_reset_config();
1244 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1245 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1247 low_output &= ~nTRST; /* switch output low */
1251 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1252 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1254 low_output |= nTRST; /* switch output high */
1259 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1260 low_output &= ~nSRST; /* switch output low */
1262 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1266 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1267 low_output |= nSRST; /* switch output high */
1269 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1272 /* command "set data bits low byte" */
1274 buffer_write(low_output);
1275 buffer_write(low_direction);
1278 static void jtagkey_reset(int trst, int srst)
1280 enum reset_types jtag_reset_config = jtag_get_reset_config();
1283 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1284 high_output &= ~nTRSTnOE;
1286 high_output &= ~nTRST;
1290 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1291 high_output |= nTRSTnOE;
1293 high_output |= nTRST;
1298 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1299 high_output &= ~nSRST;
1301 high_output &= ~nSRSTnOE;
1305 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1306 high_output |= nSRST;
1308 high_output |= nSRSTnOE;
1311 /* command "set data bits high byte" */
1313 buffer_write(high_output);
1314 buffer_write(high_direction);
1315 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1319 static void olimex_jtag_reset(int trst, int srst)
1321 enum reset_types jtag_reset_config = jtag_get_reset_config();
1324 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1325 high_output &= ~nTRSTnOE;
1327 high_output &= ~nTRST;
1331 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1332 high_output |= nTRSTnOE;
1334 high_output |= nTRST;
1339 high_output |= nSRST;
1343 high_output &= ~nSRST;
1346 /* command "set data bits high byte" */
1348 buffer_write(high_output);
1349 buffer_write(high_direction);
1350 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1354 static void axm0432_jtag_reset(int trst, int srst)
1358 tap_set_state(TAP_RESET);
1359 high_output &= ~nTRST;
1363 high_output |= nTRST;
1368 high_output &= ~nSRST;
1372 high_output |= nSRST;
1375 /* command "set data bits low byte" */
1377 buffer_write(high_output);
1378 buffer_write(high_direction);
1379 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1383 static void flyswatter_reset(int trst, int srst)
1387 low_output &= ~nTRST;
1391 low_output |= nTRST;
1396 low_output |= nSRST;
1400 low_output &= ~nSRST;
1403 /* command "set data bits low byte" */
1405 buffer_write(low_output);
1406 buffer_write(low_direction);
1407 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1410 static void turtle_reset(int trst, int srst)
1416 low_output |= nSRST;
1420 low_output &= ~nSRST;
1423 /* command "set data bits low byte" */
1425 buffer_write(low_output);
1426 buffer_write(low_direction);
1427 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1430 static void comstick_reset(int trst, int srst)
1434 high_output &= ~nTRST;
1438 high_output |= nTRST;
1443 high_output &= ~nSRST;
1447 high_output |= nSRST;
1450 /* command "set data bits high byte" */
1452 buffer_write(high_output);
1453 buffer_write(high_direction);
1454 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1458 static void stm32stick_reset(int trst, int srst)
1462 high_output &= ~nTRST;
1466 high_output |= nTRST;
1471 low_output &= ~nSRST;
1475 low_output |= nSRST;
1478 /* command "set data bits low byte" */
1480 buffer_write(low_output);
1481 buffer_write(low_direction);
1483 /* command "set data bits high byte" */
1485 buffer_write(high_output);
1486 buffer_write(high_direction);
1487 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1491 static void sheevaplug_reset(int trst, int srst)
1494 high_output &= ~nTRST;
1496 high_output |= nTRST;
1499 high_output &= ~nSRSTnOE;
1501 high_output |= nSRSTnOE;
1503 /* command "set data bits high byte" */
1505 buffer_write(high_output);
1506 buffer_write(high_direction);
1507 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1510 static int ft2232_execute_runtest(jtag_command_t *cmd)
1514 int predicted_size = 0;
1517 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1518 cmd->cmd.runtest->num_cycles,
1519 tap_state_name(cmd->cmd.runtest->end_state));
1521 /* only send the maximum buffer size that FT2232C can handle */
1523 if (tap_get_state() != TAP_IDLE)
1524 predicted_size += 3;
1525 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1526 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1527 predicted_size += 3;
1528 if (tap_get_end_state() != TAP_IDLE)
1529 predicted_size += 3;
1530 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1532 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1533 retval = ERROR_JTAG_QUEUE_FAILED;
1537 if (tap_get_state() != TAP_IDLE)
1539 move_to_state(TAP_IDLE);
1542 i = cmd->cmd.runtest->num_cycles;
1545 /* there are no state transitions in this code, so omit state tracking */
1547 /* command "Clock Data to TMS/CS Pin (no Read)" */
1551 buffer_write((i > 7) ? 6 : (i - 1));
1555 tap_set_state(TAP_IDLE);
1557 i -= (i > 7) ? 7 : i;
1558 /* LOG_DEBUG("added TMS scan (no read)"); */
1561 ft2232_end_state(cmd->cmd.runtest->end_state);
1563 if (tap_get_state() != tap_get_end_state())
1565 move_to_state(tap_get_end_state());
1569 DEBUG_JTAG_IO("runtest: %i, end in %s",
1570 cmd->cmd.runtest->num_cycles,
1571 tap_state_name(tap_get_end_state()));
1575 static int ft2232_execute_statemove(jtag_command_t *cmd)
1577 int predicted_size = 0;
1578 int retval = ERROR_OK;
1580 DEBUG_JTAG_IO("statemove end in %s",
1581 tap_state_name(cmd->cmd.statemove->end_state));
1583 /* only send the maximum buffer size that FT2232C can handle */
1585 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1587 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1588 retval = ERROR_JTAG_QUEUE_FAILED;
1592 ft2232_end_state(cmd->cmd.statemove->end_state);
1594 /* For TAP_RESET, ignore the current recorded state. It's often
1595 * wrong at server startup, and this transation is critical whenever
1598 if (tap_get_end_state() == TAP_RESET) {
1599 clock_tms(0x4b, 0xff, 5, 0);
1602 /* shortest-path move to desired end state */
1603 } else if (tap_get_state() != tap_get_end_state())
1605 move_to_state(tap_get_end_state());
1612 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1614 int predicted_size = 0;
1615 int retval = ERROR_OK;
1617 tap_state_t* path = cmd->cmd.pathmove->path;
1618 int num_states = cmd->cmd.pathmove->num_states;
1620 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1621 tap_state_name(tap_get_state()),
1622 tap_state_name(path[num_states-1]));
1624 /* only send the maximum buffer size that FT2232C can handle */
1625 predicted_size = 3 * CEIL(num_states, 7);
1626 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1628 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1629 retval = ERROR_JTAG_QUEUE_FAILED;
1635 ft2232_add_pathmove(path, num_states);
1641 static int ft2232_execute_scan(jtag_command_t *cmd)
1644 int scan_size; /* size of IR or DR scan */
1645 int predicted_size = 0;
1646 int retval = ERROR_OK;
1648 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1650 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1652 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1654 predicted_size = ft2232_predict_scan_out(scan_size, type);
1655 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1657 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1658 /* unsent commands before this */
1659 if (first_unsent != cmd)
1660 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1661 retval = ERROR_JTAG_QUEUE_FAILED;
1663 /* current command */
1664 ft2232_end_state(cmd->cmd.scan->end_state);
1665 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1667 first_unsent = cmd->next;
1672 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1674 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1677 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1678 retval = ERROR_JTAG_QUEUE_FAILED;
1682 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1683 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1684 ft2232_end_state(cmd->cmd.scan->end_state);
1685 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1689 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1690 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1691 tap_state_name(tap_get_end_state()));
1696 static int ft2232_execute_reset(jtag_command_t *cmd)
1699 int predicted_size = 0;
1702 DEBUG_JTAG_IO("reset trst: %i srst %i",
1703 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1705 /* only send the maximum buffer size that FT2232C can handle */
1707 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1709 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1710 retval = ERROR_JTAG_QUEUE_FAILED;
1715 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1717 tap_set_state(TAP_RESET);
1720 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1723 DEBUG_JTAG_IO("trst: %i, srst: %i",
1724 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1728 static int ft2232_execute_sleep(jtag_command_t *cmd)
1733 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1735 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1736 retval = ERROR_JTAG_QUEUE_FAILED;
1737 first_unsent = cmd->next;
1738 jtag_sleep(cmd->cmd.sleep->us);
1739 DEBUG_JTAG_IO("sleep %i usec while in %s",
1741 tap_state_name(tap_get_state()));
1745 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1750 /* this is only allowed while in a stable state. A check for a stable
1751 * state was done in jtag_add_clocks()
1753 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1754 retval = ERROR_JTAG_QUEUE_FAILED;
1755 DEBUG_JTAG_IO("clocks %i while in %s",
1756 cmd->cmd.stableclocks->num_cycles,
1757 tap_state_name(tap_get_state()));
1761 static int ft2232_execute_command(jtag_command_t *cmd)
1768 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1769 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1770 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1771 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1772 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1773 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1774 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1776 LOG_ERROR("BUG: unknown JTAG command type encountered");
1782 static int ft2232_execute_queue()
1784 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1787 first_unsent = cmd; /* next command that has to be sent */
1790 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1791 * that wasn't handled by a caller-provided error handler
1795 ft2232_buffer_size = 0;
1796 ft2232_expect_read = 0;
1798 /* blink, if the current layout has that feature */
1804 if (ft2232_execute_command(cmd) != ERROR_OK)
1805 retval = ERROR_JTAG_QUEUE_FAILED;
1806 /* Start reading input before FT2232 TX buffer fills up */
1808 if (ft2232_expect_read > 256)
1810 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1811 retval = ERROR_JTAG_QUEUE_FAILED;
1816 if (require_send > 0)
1817 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1818 retval = ERROR_JTAG_QUEUE_FAILED;
1823 #if BUILD_FT2232_FTD2XX == 1
1824 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1828 char SerialNumber[16];
1829 char Description[64];
1830 DWORD openex_flags = 0;
1831 char* openex_string = NULL;
1832 uint8_t latency_timer;
1834 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1837 /* Add non-standard Vid/Pid to the linux driver */
1838 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1840 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1844 if (ft2232_device_desc && ft2232_serial)
1846 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1847 ft2232_device_desc = NULL;
1850 if (ft2232_device_desc)
1852 openex_string = ft2232_device_desc;
1853 openex_flags = FT_OPEN_BY_DESCRIPTION;
1855 else if (ft2232_serial)
1857 openex_string = ft2232_serial;
1858 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1862 LOG_ERROR("neither device description nor serial number specified");
1863 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1865 return ERROR_JTAG_INIT_FAILED;
1868 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1869 if (status != FT_OK) {
1870 /* under Win32, the FTD2XX driver appends an "A" to the end
1871 * of the description, if we tried by the desc, then
1872 * try by the alternate "A" description. */
1873 if (openex_string == ft2232_device_desc) {
1874 /* Try the alternate method. */
1875 openex_string = ft2232_device_desc_A;
1876 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1877 if (status == FT_OK) {
1878 /* yea, the "alternate" method worked! */
1880 /* drat, give the user a meaningfull message.
1881 * telling the use we tried *BOTH* methods. */
1882 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1884 ft2232_device_desc_A);
1889 if (status != FT_OK)
1895 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1897 return ERROR_JTAG_INIT_FAILED;
1899 LOG_ERROR("unable to open ftdi device: %lu", status);
1900 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1901 if (status == FT_OK)
1903 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1906 for (i = 0; i < num_devices; i++)
1907 desc_array[i] = malloc(64);
1909 desc_array[num_devices] = NULL;
1911 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1913 if (status == FT_OK)
1915 LOG_ERROR("ListDevices: %lu\n", num_devices);
1916 for (i = 0; i < num_devices; i++)
1917 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1920 for (i = 0; i < num_devices; i++)
1921 free(desc_array[i]);
1927 LOG_ERROR("ListDevices: NONE\n");
1929 return ERROR_JTAG_INIT_FAILED;
1932 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1934 LOG_ERROR("unable to set latency timer: %lu", status);
1935 return ERROR_JTAG_INIT_FAILED;
1938 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1940 LOG_ERROR("unable to get latency timer: %lu", status);
1941 return ERROR_JTAG_INIT_FAILED;
1945 LOG_DEBUG("current latency timer: %i", latency_timer);
1948 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1950 LOG_ERROR("unable to set timeouts: %lu", status);
1951 return ERROR_JTAG_INIT_FAILED;
1954 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1956 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1957 return ERROR_JTAG_INIT_FAILED;
1960 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1962 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1963 return ERROR_JTAG_INIT_FAILED;
1967 static const char* type_str[] =
1968 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1969 unsigned no_of_known_types = sizeof(type_str) / sizeof(type_str[0]) - 1;
1970 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
1971 ? ftdi_device : FT_DEVICE_UNKNOWN;
1972 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
1973 LOG_INFO("deviceID: %lu", deviceID);
1974 LOG_INFO("SerialNumber: %s", SerialNumber);
1975 LOG_INFO("Description: %s", Description);
1981 static int ft2232_purge_ftd2xx(void)
1985 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1987 LOG_ERROR("error purging ftd2xx device: %lu", status);
1988 return ERROR_JTAG_INIT_FAILED;
1994 #endif /* BUILD_FT2232_FTD2XX == 1 */
1996 #if BUILD_FT2232_LIBFTDI == 1
1997 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
1999 uint8_t latency_timer;
2001 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2002 ft2232_layout, vid, pid);
2004 if (ftdi_init(&ftdic) < 0)
2005 return ERROR_JTAG_INIT_FAILED;
2007 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
2009 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2010 return ERROR_JTAG_INIT_FAILED;
2013 /* context, vendor id, product id */
2014 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2018 LOG_WARNING("unable to open ftdi device (trying more): %s",
2021 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2023 return ERROR_JTAG_INIT_FAILED;
2026 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2027 if (ftdi_usb_reset(&ftdic) < 0)
2029 LOG_ERROR("unable to reset ftdi device");
2030 return ERROR_JTAG_INIT_FAILED;
2033 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2035 LOG_ERROR("unable to set latency timer");
2036 return ERROR_JTAG_INIT_FAILED;
2039 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2041 LOG_ERROR("unable to get latency timer");
2042 return ERROR_JTAG_INIT_FAILED;
2046 LOG_DEBUG("current latency timer: %i", latency_timer);
2049 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2051 ftdi_device = ftdic.type;
2052 static const char* type_str[] =
2053 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2054 unsigned no_of_known_types = sizeof(type_str) / sizeof(type_str[0]) - 1;
2055 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2056 ? ftdi_device : no_of_known_types;
2057 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2061 static int ft2232_purge_libftdi(void)
2063 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2065 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2066 return ERROR_JTAG_INIT_FAILED;
2072 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2074 static int ft2232_init(void)
2078 uint32_t bytes_written;
2079 const ft2232_layout_t* cur_layout = ft2232_layouts;
2082 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2084 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2088 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2091 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2093 ft2232_layout = "usbjtag";
2094 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2097 while (cur_layout->name)
2099 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2101 layout = cur_layout;
2109 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2110 return ERROR_JTAG_INIT_FAILED;
2116 * "more indicates that there are more IDs to try, so we should
2117 * not print an error for an ID mismatch (but for anything
2120 * try_more indicates that the error code returned indicates an
2121 * ID mismatch (and nothing else) and that we should proceeed
2122 * with the next ID pair.
2124 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2127 #if BUILD_FT2232_FTD2XX == 1
2128 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2130 #elif BUILD_FT2232_LIBFTDI == 1
2131 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2136 if (!more || !try_more)
2140 ft2232_buffer_size = 0;
2141 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2143 if (layout->init() != ERROR_OK)
2144 return ERROR_JTAG_INIT_FAILED;
2146 if (ft2232_device_is_highspeed())
2148 #ifndef BUILD_FT2232_HIGHSPEED
2149 #if BUILD_FT2232_FTD2XX == 1
2150 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2151 #elif BUILD_FT2232_LIBFTDI == 1
2152 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2155 /* make sure the legacy mode is disabled */
2156 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2157 return ERROR_JTAG_INIT_FAILED;
2160 ft2232_speed(jtag_get_speed());
2162 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2163 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2165 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2166 return ERROR_JTAG_INIT_FAILED;
2169 #if BUILD_FT2232_FTD2XX == 1
2170 return ft2232_purge_ftd2xx();
2171 #elif BUILD_FT2232_LIBFTDI == 1
2172 return ft2232_purge_libftdi();
2178 static int usbjtag_init(void)
2181 uint32_t bytes_written;
2184 low_direction = 0x0b;
2186 if (strcmp(ft2232_layout, "usbjtag") == 0)
2193 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2200 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2207 low_direction = 0x8b;
2209 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2216 low_direction = 0xcb;
2220 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2221 return ERROR_JTAG_INIT_FAILED;
2224 enum reset_types jtag_reset_config = jtag_get_reset_config();
2225 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2227 low_direction &= ~nTRSTnOE; /* nTRST input */
2228 low_output &= ~nTRST; /* nTRST = 0 */
2232 low_direction |= nTRSTnOE; /* nTRST output */
2233 low_output |= nTRST; /* nTRST = 1 */
2236 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2238 low_direction |= nSRSTnOE; /* nSRST output */
2239 low_output |= nSRST; /* nSRST = 1 */
2243 low_direction &= ~nSRSTnOE; /* nSRST input */
2244 low_output &= ~nSRST; /* nSRST = 0 */
2247 /* initialize low byte for jtag */
2248 buf[0] = 0x80; /* command "set data bits low byte" */
2249 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2250 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2251 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2253 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2255 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2256 return ERROR_JTAG_INIT_FAILED;
2262 static int axm0432_jtag_init(void)
2265 uint32_t bytes_written;
2268 low_direction = 0x2b;
2270 /* initialize low byte for jtag */
2271 buf[0] = 0x80; /* command "set data bits low byte" */
2272 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2273 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2274 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2276 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2278 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2279 return ERROR_JTAG_INIT_FAILED;
2282 if (strcmp(layout->name, "axm0432_jtag") == 0)
2285 nTRSTnOE = 0x0; /* No output enable for TRST*/
2287 nSRSTnOE = 0x0; /* No output enable for SRST*/
2291 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2296 high_direction = 0x0c;
2298 enum reset_types jtag_reset_config = jtag_get_reset_config();
2299 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2301 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2305 high_output |= nTRST;
2308 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2310 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2314 high_output |= nSRST;
2317 /* initialize high port */
2318 buf[0] = 0x82; /* command "set data bits high byte" */
2319 buf[1] = high_output; /* value */
2320 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2321 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2323 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2325 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2326 return ERROR_JTAG_INIT_FAILED;
2332 static int jtagkey_init(void)
2335 uint32_t bytes_written;
2338 low_direction = 0x1b;
2340 /* initialize low byte for jtag */
2341 buf[0] = 0x80; /* command "set data bits low byte" */
2342 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2343 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2344 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2346 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2348 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2349 return ERROR_JTAG_INIT_FAILED;
2352 if (strcmp(layout->name, "jtagkey") == 0)
2359 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2360 || (strcmp(layout->name, "oocdlink") == 0))
2369 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2374 high_direction = 0x0f;
2376 enum reset_types jtag_reset_config = jtag_get_reset_config();
2377 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2379 high_output |= nTRSTnOE;
2380 high_output &= ~nTRST;
2384 high_output &= ~nTRSTnOE;
2385 high_output |= nTRST;
2388 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2390 high_output &= ~nSRSTnOE;
2391 high_output |= nSRST;
2395 high_output |= nSRSTnOE;
2396 high_output &= ~nSRST;
2399 /* initialize high port */
2400 buf[0] = 0x82; /* command "set data bits high byte" */
2401 buf[1] = high_output; /* value */
2402 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2403 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2405 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2407 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2408 return ERROR_JTAG_INIT_FAILED;
2414 static int olimex_jtag_init(void)
2417 uint32_t bytes_written;
2420 low_direction = 0x1b;
2422 /* initialize low byte for jtag */
2423 buf[0] = 0x80; /* command "set data bits low byte" */
2424 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2425 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2426 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2428 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2430 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2431 return ERROR_JTAG_INIT_FAILED;
2437 nSRSTnOE = 0x00; /* no output enable for nSRST */
2440 high_direction = 0x0f;
2442 enum reset_types jtag_reset_config = jtag_get_reset_config();
2443 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2445 high_output |= nTRSTnOE;
2446 high_output &= ~nTRST;
2450 high_output &= ~nTRSTnOE;
2451 high_output |= nTRST;
2454 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2456 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2460 high_output &= ~nSRST;
2463 /* turn red LED on */
2464 high_output |= 0x08;
2466 /* initialize high port */
2467 buf[0] = 0x82; /* command "set data bits high byte" */
2468 buf[1] = high_output; /* value */
2469 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2470 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2472 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2474 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2475 return ERROR_JTAG_INIT_FAILED;
2481 static int flyswatter_init(void)
2484 uint32_t bytes_written;
2487 low_direction = 0xfb;
2489 /* initialize low byte for jtag */
2490 buf[0] = 0x80; /* command "set data bits low byte" */
2491 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2492 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2493 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2495 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2497 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2498 return ERROR_JTAG_INIT_FAILED;
2502 nTRSTnOE = 0x0; /* not output enable for nTRST */
2504 nSRSTnOE = 0x00; /* no output enable for nSRST */
2507 high_direction = 0x0c;
2509 /* turn red LED3 on, LED2 off */
2510 high_output |= 0x08;
2512 /* initialize high port */
2513 buf[0] = 0x82; /* command "set data bits high byte" */
2514 buf[1] = high_output; /* value */
2515 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2516 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2518 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2520 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2521 return ERROR_JTAG_INIT_FAILED;
2527 static int turtle_init(void)
2530 uint32_t bytes_written;
2533 low_direction = 0x5b;
2535 /* initialize low byte for jtag */
2536 buf[0] = 0x80; /* command "set data bits low byte" */
2537 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2538 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2539 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2541 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2543 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2544 return ERROR_JTAG_INIT_FAILED;
2550 high_direction = 0x0C;
2552 /* initialize high port */
2553 buf[0] = 0x82; /* command "set data bits high byte" */
2554 buf[1] = high_output;
2555 buf[2] = high_direction;
2556 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2558 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2560 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2561 return ERROR_JTAG_INIT_FAILED;
2567 static int comstick_init(void)
2570 uint32_t bytes_written;
2573 low_direction = 0x0b;
2575 /* initialize low byte for jtag */
2576 buf[0] = 0x80; /* command "set data bits low byte" */
2577 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2578 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2579 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2581 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2583 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2584 return ERROR_JTAG_INIT_FAILED;
2588 nTRSTnOE = 0x00; /* no output enable for nTRST */
2590 nSRSTnOE = 0x00; /* no output enable for nSRST */
2593 high_direction = 0x03;
2595 /* initialize high port */
2596 buf[0] = 0x82; /* command "set data bits high byte" */
2597 buf[1] = high_output;
2598 buf[2] = high_direction;
2599 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2601 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2603 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2604 return ERROR_JTAG_INIT_FAILED;
2610 static int stm32stick_init(void)
2613 uint32_t bytes_written;
2616 low_direction = 0x8b;
2618 /* initialize low byte for jtag */
2619 buf[0] = 0x80; /* command "set data bits low byte" */
2620 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2621 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2622 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2624 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2626 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2627 return ERROR_JTAG_INIT_FAILED;
2631 nTRSTnOE = 0x00; /* no output enable for nTRST */
2633 nSRSTnOE = 0x00; /* no output enable for nSRST */
2636 high_direction = 0x03;
2638 /* initialize high port */
2639 buf[0] = 0x82; /* command "set data bits high byte" */
2640 buf[1] = high_output;
2641 buf[2] = high_direction;
2642 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2644 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2646 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2647 return ERROR_JTAG_INIT_FAILED;
2653 static int sheevaplug_init(void)
2656 uint32_t bytes_written;
2659 low_direction = 0x1b;
2661 /* initialize low byte for jtag */
2662 buf[0] = 0x80; /* command "set data bits low byte" */
2663 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2664 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2665 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2667 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2669 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2670 return ERROR_JTAG_INIT_FAILED;
2679 high_direction = 0x0f;
2681 /* nTRST is always push-pull */
2682 high_output &= ~nTRSTnOE;
2683 high_output |= nTRST;
2685 /* nSRST is always open-drain */
2686 high_output |= nSRSTnOE;
2687 high_output &= ~nSRST;
2689 /* initialize high port */
2690 buf[0] = 0x82; /* command "set data bits high byte" */
2691 buf[1] = high_output; /* value */
2692 buf[2] = high_direction; /* all outputs - xRST */
2693 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2695 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2697 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2698 return ERROR_JTAG_INIT_FAILED;
2704 static int cortino_jtag_init(void)
2707 uint32_t bytes_written;
2710 low_direction = 0x1b;
2712 /* initialize low byte for jtag */
2713 buf[0] = 0x80; /* command "set data bits low byte" */
2714 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2715 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2716 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2718 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2720 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2721 return ERROR_JTAG_INIT_FAILED;
2725 nTRSTnOE = 0x00; /* no output enable for nTRST */
2727 nSRSTnOE = 0x00; /* no output enable for nSRST */
2730 high_direction = 0x03;
2732 /* initialize high port */
2733 buf[0] = 0x82; /* command "set data bits high byte" */
2734 buf[1] = high_output;
2735 buf[2] = high_direction;
2736 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2738 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2740 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2741 return ERROR_JTAG_INIT_FAILED;
2747 static void olimex_jtag_blink(void)
2749 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2750 * ACBUS3 is bit 3 of the GPIOH port
2752 if (high_output & 0x08)
2754 /* set port pin high */
2755 high_output &= 0x07;
2759 /* set port pin low */
2760 high_output |= 0x08;
2764 buffer_write(high_output);
2765 buffer_write(high_direction);
2768 static void flyswatter_jtag_blink(void)
2771 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2773 high_output ^= 0x0c;
2776 buffer_write(high_output);
2777 buffer_write(high_direction);
2780 static void turtle_jtag_blink(void)
2783 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2785 if (high_output & 0x08)
2795 buffer_write(high_output);
2796 buffer_write(high_direction);
2799 static int ft2232_quit(void)
2801 #if BUILD_FT2232_FTD2XX == 1
2804 status = FT_Close(ftdih);
2805 #elif BUILD_FT2232_LIBFTDI == 1
2806 ftdi_usb_close(&ftdic);
2808 ftdi_deinit(&ftdic);
2811 free(ft2232_buffer);
2812 ft2232_buffer = NULL;
2817 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2823 ft2232_device_desc = strdup(args[0]);
2824 cp = strchr(ft2232_device_desc, 0);
2825 /* under Win32, the FTD2XX driver appends an "A" to the end
2826 * of the description, this examines the given desc
2827 * and creates the 'missing' _A or non_A variable. */
2828 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2829 /* it was, so make this the "A" version. */
2830 ft2232_device_desc_A = ft2232_device_desc;
2831 /* and *CREATE* the non-A version. */
2832 strcpy(buf, ft2232_device_desc);
2833 cp = strchr(buf, 0);
2835 ft2232_device_desc = strdup(buf);
2837 /* <space > A not defined
2839 sprintf(buf, "%s A", ft2232_device_desc);
2840 ft2232_device_desc_A = strdup(buf);
2845 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2851 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2855 ft2232_serial = strdup(args[0]);
2859 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2865 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2870 ft2232_layout = malloc(strlen(args[0]) + 1);
2871 strcpy(ft2232_layout, args[0]);
2876 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2878 if (argc > MAX_USB_IDS * 2)
2880 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2881 "(maximum is %d pairs)", MAX_USB_IDS);
2882 argc = MAX_USB_IDS * 2;
2884 if (argc < 2 || (argc & 1))
2886 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2888 return ERROR_COMMAND_SYNTAX_ERROR;
2889 /* remove the incomplete trailing id */
2894 int retval = ERROR_OK;
2895 for (i = 0; i < argc; i += 2)
2897 retval = parse_u16(args[i], &ft2232_vid[i >> 1]);
2898 if (ERROR_OK != retval)
2900 retval = parse_u16(args[i + 1], &ft2232_pid[i >> 1]);
2901 if (ERROR_OK != retval)
2906 * Explicitly terminate, in case there are multiples instances of
2909 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2914 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2918 ft2232_latency = atoi(args[0]);
2922 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2928 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2932 /* 7 bits of either ones or zeros. */
2933 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2935 while (num_cycles > 0)
2937 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2938 * at most 7 bits per invocation. Here we invoke it potentially
2941 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2943 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2945 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2946 retval = ERROR_JTAG_QUEUE_FAILED;
2951 /* there are no state transitions in this code, so omit state tracking */
2953 /* command "Clock Data to TMS/CS Pin (no Read)" */
2957 buffer_write(bitcount_per_command - 1);
2959 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2964 num_cycles -= bitcount_per_command;
2970 /* ---------------------------------------------------------------------
2971 * Support for IceBear JTAG adapter from Section5:
2972 * http://section5.ch/icebear
2974 * Author: Sten, debian@sansys-electronic.com
2977 /* Icebear pin layout
2979 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2980 * GND GND | 4 3| n.c.
2981 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2982 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2983 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2984 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2985 * ADBUS2 TDO |14 13| GND GND
2987 * ADBUS0 O L TCK ACBUS0 GND
2988 * ADBUS1 O L TDI ACBUS1 GND
2989 * ADBUS2 I TDO ACBUS2 n.c.
2990 * ADBUS3 O H TMS ACBUS3 n.c.
2996 static int icebear_jtag_init(void) {
2998 uint32_t bytes_written;
3000 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3001 low_output = 0x08; /* high: TMS; low: TCK TDI */
3005 enum reset_types jtag_reset_config = jtag_get_reset_config();
3006 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3007 low_direction &= ~nTRST; /* nTRST high impedance */
3010 low_direction |= nTRST;
3011 low_output |= nTRST;
3014 low_direction |= nSRST;
3015 low_output |= nSRST;
3017 /* initialize low byte for jtag */
3018 buf[0] = 0x80; /* command "set data bits low byte" */
3019 buf[1] = low_output;
3020 buf[2] = low_direction;
3021 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3023 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3024 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3025 return ERROR_JTAG_INIT_FAILED;
3029 high_direction = 0x00;
3032 /* initialize high port */
3033 buf[0] = 0x82; /* command "set data bits high byte" */
3034 buf[1] = high_output; /* value */
3035 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3036 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3038 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3039 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3040 return ERROR_JTAG_INIT_FAILED;
3046 static void icebear_jtag_reset(int trst, int srst) {
3049 low_direction |= nTRST;
3050 low_output &= ~nTRST;
3052 else if (trst == 0) {
3053 enum reset_types jtag_reset_config = jtag_get_reset_config();
3054 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3055 low_direction &= ~nTRST;
3057 low_output |= nTRST;
3061 low_output &= ~nSRST;
3063 else if (srst == 0) {
3064 low_output |= nSRST;
3067 /* command "set data bits low byte" */
3069 buffer_write(low_output);
3070 buffer_write(low_direction);
3072 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);