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));
1556 i -= (i > 7) ? 7 : i;
1557 /* LOG_DEBUG("added TMS scan (no read)"); */
1560 ft2232_end_state(cmd->cmd.runtest->end_state);
1562 if (tap_get_state() != tap_get_end_state())
1564 move_to_state(tap_get_end_state());
1568 DEBUG_JTAG_IO("runtest: %i, end in %s",
1569 cmd->cmd.runtest->num_cycles,
1570 tap_state_name(tap_get_end_state()));
1574 static int ft2232_execute_statemove(jtag_command_t *cmd)
1576 int predicted_size = 0;
1577 int retval = ERROR_OK;
1579 DEBUG_JTAG_IO("statemove end in %s",
1580 tap_state_name(cmd->cmd.statemove->end_state));
1582 /* only send the maximum buffer size that FT2232C can handle */
1584 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1586 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1587 retval = ERROR_JTAG_QUEUE_FAILED;
1591 ft2232_end_state(cmd->cmd.statemove->end_state);
1593 /* For TAP_RESET, ignore the current recorded state. It's often
1594 * wrong at server startup, and this transation is critical whenever
1597 if (tap_get_end_state() == TAP_RESET) {
1598 clock_tms(0x4b, 0xff, 5, 0);
1601 /* shortest-path move to desired end state */
1602 } else if (tap_get_state() != tap_get_end_state())
1604 move_to_state(tap_get_end_state());
1611 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1613 int predicted_size = 0;
1614 int retval = ERROR_OK;
1616 tap_state_t* path = cmd->cmd.pathmove->path;
1617 int num_states = cmd->cmd.pathmove->num_states;
1619 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1620 tap_state_name(tap_get_state()),
1621 tap_state_name(path[num_states-1]));
1623 /* only send the maximum buffer size that FT2232C can handle */
1624 predicted_size = 3 * CEIL(num_states, 7);
1625 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1627 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1628 retval = ERROR_JTAG_QUEUE_FAILED;
1634 ft2232_add_pathmove(path, num_states);
1640 static int ft2232_execute_scan(jtag_command_t *cmd)
1643 int scan_size; /* size of IR or DR scan */
1644 int predicted_size = 0;
1645 int retval = ERROR_OK;
1647 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1649 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1651 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1653 predicted_size = ft2232_predict_scan_out(scan_size, type);
1654 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1656 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1657 /* unsent commands before this */
1658 if (first_unsent != cmd)
1659 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1660 retval = ERROR_JTAG_QUEUE_FAILED;
1662 /* current command */
1663 ft2232_end_state(cmd->cmd.scan->end_state);
1664 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1666 first_unsent = cmd->next;
1671 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1673 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1676 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1677 retval = ERROR_JTAG_QUEUE_FAILED;
1681 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1682 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1683 ft2232_end_state(cmd->cmd.scan->end_state);
1684 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1688 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1689 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1690 tap_state_name(tap_get_end_state()));
1695 static int ft2232_execute_reset(jtag_command_t *cmd)
1698 int predicted_size = 0;
1701 DEBUG_JTAG_IO("reset trst: %i srst %i",
1702 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1704 /* only send the maximum buffer size that FT2232C can handle */
1706 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1708 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1709 retval = ERROR_JTAG_QUEUE_FAILED;
1714 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1716 tap_set_state(TAP_RESET);
1719 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1722 DEBUG_JTAG_IO("trst: %i, srst: %i",
1723 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1727 static int ft2232_execute_sleep(jtag_command_t *cmd)
1732 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1734 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1735 retval = ERROR_JTAG_QUEUE_FAILED;
1736 first_unsent = cmd->next;
1737 jtag_sleep(cmd->cmd.sleep->us);
1738 DEBUG_JTAG_IO("sleep %i usec while in %s",
1740 tap_state_name(tap_get_state()));
1744 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1749 /* this is only allowed while in a stable state. A check for a stable
1750 * state was done in jtag_add_clocks()
1752 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1753 retval = ERROR_JTAG_QUEUE_FAILED;
1754 DEBUG_JTAG_IO("clocks %i while in %s",
1755 cmd->cmd.stableclocks->num_cycles,
1756 tap_state_name(tap_get_state()));
1760 static int ft2232_execute_command(jtag_command_t *cmd)
1767 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1768 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1769 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1770 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1771 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1772 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1773 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1775 LOG_ERROR("BUG: unknown JTAG command type encountered");
1781 static int ft2232_execute_queue()
1783 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1786 first_unsent = cmd; /* next command that has to be sent */
1789 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1790 * that wasn't handled by a caller-provided error handler
1794 ft2232_buffer_size = 0;
1795 ft2232_expect_read = 0;
1797 /* blink, if the current layout has that feature */
1803 if (ft2232_execute_command(cmd) != ERROR_OK)
1804 retval = ERROR_JTAG_QUEUE_FAILED;
1805 /* Start reading input before FT2232 TX buffer fills up */
1807 if (ft2232_expect_read > 256)
1809 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1810 retval = ERROR_JTAG_QUEUE_FAILED;
1815 if (require_send > 0)
1816 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1817 retval = ERROR_JTAG_QUEUE_FAILED;
1822 #if BUILD_FT2232_FTD2XX == 1
1823 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1827 char SerialNumber[16];
1828 char Description[64];
1829 DWORD openex_flags = 0;
1830 char* openex_string = NULL;
1831 uint8_t latency_timer;
1833 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1836 /* Add non-standard Vid/Pid to the linux driver */
1837 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1839 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1843 if (ft2232_device_desc && ft2232_serial)
1845 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1846 ft2232_device_desc = NULL;
1849 if (ft2232_device_desc)
1851 openex_string = ft2232_device_desc;
1852 openex_flags = FT_OPEN_BY_DESCRIPTION;
1854 else if (ft2232_serial)
1856 openex_string = ft2232_serial;
1857 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1861 LOG_ERROR("neither device description nor serial number specified");
1862 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1864 return ERROR_JTAG_INIT_FAILED;
1867 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1868 if (status != FT_OK) {
1869 /* under Win32, the FTD2XX driver appends an "A" to the end
1870 * of the description, if we tried by the desc, then
1871 * try by the alternate "A" description. */
1872 if (openex_string == ft2232_device_desc) {
1873 /* Try the alternate method. */
1874 openex_string = ft2232_device_desc_A;
1875 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1876 if (status == FT_OK) {
1877 /* yea, the "alternate" method worked! */
1879 /* drat, give the user a meaningfull message.
1880 * telling the use we tried *BOTH* methods. */
1881 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1883 ft2232_device_desc_A);
1888 if (status != FT_OK)
1894 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1896 return ERROR_JTAG_INIT_FAILED;
1898 LOG_ERROR("unable to open ftdi device: %lu", status);
1899 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1900 if (status == FT_OK)
1902 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1905 for (i = 0; i < num_devices; i++)
1906 desc_array[i] = malloc(64);
1908 desc_array[num_devices] = NULL;
1910 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1912 if (status == FT_OK)
1914 LOG_ERROR("ListDevices: %lu\n", num_devices);
1915 for (i = 0; i < num_devices; i++)
1916 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1919 for (i = 0; i < num_devices; i++)
1920 free(desc_array[i]);
1926 LOG_ERROR("ListDevices: NONE\n");
1928 return ERROR_JTAG_INIT_FAILED;
1931 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1933 LOG_ERROR("unable to set latency timer: %lu", status);
1934 return ERROR_JTAG_INIT_FAILED;
1937 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1939 LOG_ERROR("unable to get latency timer: %lu", status);
1940 return ERROR_JTAG_INIT_FAILED;
1944 LOG_DEBUG("current latency timer: %i", latency_timer);
1947 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1949 LOG_ERROR("unable to set timeouts: %lu", status);
1950 return ERROR_JTAG_INIT_FAILED;
1953 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1955 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1956 return ERROR_JTAG_INIT_FAILED;
1959 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1961 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1962 return ERROR_JTAG_INIT_FAILED;
1966 static const char* type_str[] =
1967 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1968 unsigned no_of_known_types = sizeof(type_str) / sizeof(type_str[0]) - 1;
1969 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
1970 ? ftdi_device : FT_DEVICE_UNKNOWN;
1971 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
1972 LOG_INFO("deviceID: %lu", deviceID);
1973 LOG_INFO("SerialNumber: %s", SerialNumber);
1974 LOG_INFO("Description: %s", Description);
1980 static int ft2232_purge_ftd2xx(void)
1984 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1986 LOG_ERROR("error purging ftd2xx device: %lu", status);
1987 return ERROR_JTAG_INIT_FAILED;
1993 #endif /* BUILD_FT2232_FTD2XX == 1 */
1995 #if BUILD_FT2232_LIBFTDI == 1
1996 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
1998 uint8_t latency_timer;
2000 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2001 ft2232_layout, vid, pid);
2003 if (ftdi_init(&ftdic) < 0)
2004 return ERROR_JTAG_INIT_FAILED;
2006 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
2008 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2009 return ERROR_JTAG_INIT_FAILED;
2012 /* context, vendor id, product id */
2013 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2017 LOG_WARNING("unable to open ftdi device (trying more): %s",
2020 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2022 return ERROR_JTAG_INIT_FAILED;
2025 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2026 if (ftdi_usb_reset(&ftdic) < 0)
2028 LOG_ERROR("unable to reset ftdi device");
2029 return ERROR_JTAG_INIT_FAILED;
2032 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2034 LOG_ERROR("unable to set latency timer");
2035 return ERROR_JTAG_INIT_FAILED;
2038 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2040 LOG_ERROR("unable to get latency timer");
2041 return ERROR_JTAG_INIT_FAILED;
2045 LOG_DEBUG("current latency timer: %i", latency_timer);
2048 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2050 ftdi_device = ftdic.type;
2051 static const char* type_str[] =
2052 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2053 unsigned no_of_known_types = sizeof(type_str) / sizeof(type_str[0]) - 1;
2054 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2055 ? ftdi_device : no_of_known_types;
2056 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2060 static int ft2232_purge_libftdi(void)
2062 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2064 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2065 return ERROR_JTAG_INIT_FAILED;
2071 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2073 static int ft2232_init(void)
2077 uint32_t bytes_written;
2078 const ft2232_layout_t* cur_layout = ft2232_layouts;
2081 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2083 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2087 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2090 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2092 ft2232_layout = "usbjtag";
2093 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2096 while (cur_layout->name)
2098 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2100 layout = cur_layout;
2108 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2109 return ERROR_JTAG_INIT_FAILED;
2115 * "more indicates that there are more IDs to try, so we should
2116 * not print an error for an ID mismatch (but for anything
2119 * try_more indicates that the error code returned indicates an
2120 * ID mismatch (and nothing else) and that we should proceeed
2121 * with the next ID pair.
2123 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2126 #if BUILD_FT2232_FTD2XX == 1
2127 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2129 #elif BUILD_FT2232_LIBFTDI == 1
2130 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2135 if (!more || !try_more)
2139 ft2232_buffer_size = 0;
2140 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2142 if (layout->init() != ERROR_OK)
2143 return ERROR_JTAG_INIT_FAILED;
2145 if (ft2232_device_is_highspeed())
2147 #ifndef BUILD_FT2232_HIGHSPEED
2148 #if BUILD_FT2232_FTD2XX == 1
2149 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2150 #elif BUILD_FT2232_LIBFTDI == 1
2151 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2154 /* make sure the legacy mode is disabled */
2155 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2156 return ERROR_JTAG_INIT_FAILED;
2159 ft2232_speed(jtag_get_speed());
2161 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2162 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2164 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2165 return ERROR_JTAG_INIT_FAILED;
2168 #if BUILD_FT2232_FTD2XX == 1
2169 return ft2232_purge_ftd2xx();
2170 #elif BUILD_FT2232_LIBFTDI == 1
2171 return ft2232_purge_libftdi();
2177 static int usbjtag_init(void)
2180 uint32_t bytes_written;
2183 low_direction = 0x0b;
2185 if (strcmp(ft2232_layout, "usbjtag") == 0)
2192 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2199 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2206 low_direction = 0x8b;
2208 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2215 low_direction = 0xcb;
2219 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2220 return ERROR_JTAG_INIT_FAILED;
2223 enum reset_types jtag_reset_config = jtag_get_reset_config();
2224 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2226 low_direction &= ~nTRSTnOE; /* nTRST input */
2227 low_output &= ~nTRST; /* nTRST = 0 */
2231 low_direction |= nTRSTnOE; /* nTRST output */
2232 low_output |= nTRST; /* nTRST = 1 */
2235 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2237 low_direction |= nSRSTnOE; /* nSRST output */
2238 low_output |= nSRST; /* nSRST = 1 */
2242 low_direction &= ~nSRSTnOE; /* nSRST input */
2243 low_output &= ~nSRST; /* nSRST = 0 */
2246 /* initialize low byte for jtag */
2247 buf[0] = 0x80; /* command "set data bits low byte" */
2248 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2249 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2250 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2252 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2254 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2255 return ERROR_JTAG_INIT_FAILED;
2261 static int axm0432_jtag_init(void)
2264 uint32_t bytes_written;
2267 low_direction = 0x2b;
2269 /* initialize low byte for jtag */
2270 buf[0] = 0x80; /* command "set data bits low byte" */
2271 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2272 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2273 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2275 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2277 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2278 return ERROR_JTAG_INIT_FAILED;
2281 if (strcmp(layout->name, "axm0432_jtag") == 0)
2284 nTRSTnOE = 0x0; /* No output enable for TRST*/
2286 nSRSTnOE = 0x0; /* No output enable for SRST*/
2290 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2295 high_direction = 0x0c;
2297 enum reset_types jtag_reset_config = jtag_get_reset_config();
2298 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2300 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2304 high_output |= nTRST;
2307 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2309 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2313 high_output |= nSRST;
2316 /* initialize high port */
2317 buf[0] = 0x82; /* command "set data bits high byte" */
2318 buf[1] = high_output; /* value */
2319 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2320 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2322 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2324 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2325 return ERROR_JTAG_INIT_FAILED;
2331 static int jtagkey_init(void)
2334 uint32_t bytes_written;
2337 low_direction = 0x1b;
2339 /* initialize low byte for jtag */
2340 buf[0] = 0x80; /* command "set data bits low byte" */
2341 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2342 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2343 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2345 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2347 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2348 return ERROR_JTAG_INIT_FAILED;
2351 if (strcmp(layout->name, "jtagkey") == 0)
2358 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2359 || (strcmp(layout->name, "oocdlink") == 0))
2368 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2373 high_direction = 0x0f;
2375 enum reset_types jtag_reset_config = jtag_get_reset_config();
2376 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2378 high_output |= nTRSTnOE;
2379 high_output &= ~nTRST;
2383 high_output &= ~nTRSTnOE;
2384 high_output |= nTRST;
2387 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2389 high_output &= ~nSRSTnOE;
2390 high_output |= nSRST;
2394 high_output |= nSRSTnOE;
2395 high_output &= ~nSRST;
2398 /* initialize high port */
2399 buf[0] = 0x82; /* command "set data bits high byte" */
2400 buf[1] = high_output; /* value */
2401 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2402 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2404 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2406 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2407 return ERROR_JTAG_INIT_FAILED;
2413 static int olimex_jtag_init(void)
2416 uint32_t bytes_written;
2419 low_direction = 0x1b;
2421 /* initialize low byte for jtag */
2422 buf[0] = 0x80; /* command "set data bits low byte" */
2423 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2424 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2425 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2427 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2429 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2430 return ERROR_JTAG_INIT_FAILED;
2436 nSRSTnOE = 0x00; /* no output enable for nSRST */
2439 high_direction = 0x0f;
2441 enum reset_types jtag_reset_config = jtag_get_reset_config();
2442 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2444 high_output |= nTRSTnOE;
2445 high_output &= ~nTRST;
2449 high_output &= ~nTRSTnOE;
2450 high_output |= nTRST;
2453 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2455 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2459 high_output &= ~nSRST;
2462 /* turn red LED on */
2463 high_output |= 0x08;
2465 /* initialize high port */
2466 buf[0] = 0x82; /* command "set data bits high byte" */
2467 buf[1] = high_output; /* value */
2468 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2469 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2471 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2473 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2474 return ERROR_JTAG_INIT_FAILED;
2480 static int flyswatter_init(void)
2483 uint32_t bytes_written;
2486 low_direction = 0xfb;
2488 /* initialize low byte for jtag */
2489 buf[0] = 0x80; /* command "set data bits low byte" */
2490 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2491 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2492 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2494 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2496 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2497 return ERROR_JTAG_INIT_FAILED;
2501 nTRSTnOE = 0x0; /* not output enable for nTRST */
2503 nSRSTnOE = 0x00; /* no output enable for nSRST */
2506 high_direction = 0x0c;
2508 /* turn red LED3 on, LED2 off */
2509 high_output |= 0x08;
2511 /* initialize high port */
2512 buf[0] = 0x82; /* command "set data bits high byte" */
2513 buf[1] = high_output; /* value */
2514 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2515 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2517 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2519 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2520 return ERROR_JTAG_INIT_FAILED;
2526 static int turtle_init(void)
2529 uint32_t bytes_written;
2532 low_direction = 0x5b;
2534 /* initialize low byte for jtag */
2535 buf[0] = 0x80; /* command "set data bits low byte" */
2536 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2537 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2538 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2540 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2542 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2543 return ERROR_JTAG_INIT_FAILED;
2549 high_direction = 0x0C;
2551 /* initialize high port */
2552 buf[0] = 0x82; /* command "set data bits high byte" */
2553 buf[1] = high_output;
2554 buf[2] = high_direction;
2555 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2557 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2559 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2560 return ERROR_JTAG_INIT_FAILED;
2566 static int comstick_init(void)
2569 uint32_t bytes_written;
2572 low_direction = 0x0b;
2574 /* initialize low byte for jtag */
2575 buf[0] = 0x80; /* command "set data bits low byte" */
2576 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2577 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2578 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2580 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2582 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2583 return ERROR_JTAG_INIT_FAILED;
2587 nTRSTnOE = 0x00; /* no output enable for nTRST */
2589 nSRSTnOE = 0x00; /* no output enable for nSRST */
2592 high_direction = 0x03;
2594 /* initialize high port */
2595 buf[0] = 0x82; /* command "set data bits high byte" */
2596 buf[1] = high_output;
2597 buf[2] = high_direction;
2598 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2600 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2602 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2603 return ERROR_JTAG_INIT_FAILED;
2609 static int stm32stick_init(void)
2612 uint32_t bytes_written;
2615 low_direction = 0x8b;
2617 /* initialize low byte for jtag */
2618 buf[0] = 0x80; /* command "set data bits low byte" */
2619 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2620 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2621 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2623 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2625 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2626 return ERROR_JTAG_INIT_FAILED;
2630 nTRSTnOE = 0x00; /* no output enable for nTRST */
2632 nSRSTnOE = 0x00; /* no output enable for nSRST */
2635 high_direction = 0x03;
2637 /* initialize high port */
2638 buf[0] = 0x82; /* command "set data bits high byte" */
2639 buf[1] = high_output;
2640 buf[2] = high_direction;
2641 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2643 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2645 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2646 return ERROR_JTAG_INIT_FAILED;
2652 static int sheevaplug_init(void)
2655 uint32_t bytes_written;
2658 low_direction = 0x1b;
2660 /* initialize low byte for jtag */
2661 buf[0] = 0x80; /* command "set data bits low byte" */
2662 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2663 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2664 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2666 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2668 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2669 return ERROR_JTAG_INIT_FAILED;
2678 high_direction = 0x0f;
2680 /* nTRST is always push-pull */
2681 high_output &= ~nTRSTnOE;
2682 high_output |= nTRST;
2684 /* nSRST is always open-drain */
2685 high_output |= nSRSTnOE;
2686 high_output &= ~nSRST;
2688 /* initialize high port */
2689 buf[0] = 0x82; /* command "set data bits high byte" */
2690 buf[1] = high_output; /* value */
2691 buf[2] = high_direction; /* all outputs - xRST */
2692 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2694 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2696 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2697 return ERROR_JTAG_INIT_FAILED;
2703 static int cortino_jtag_init(void)
2706 uint32_t bytes_written;
2709 low_direction = 0x1b;
2711 /* initialize low byte for jtag */
2712 buf[0] = 0x80; /* command "set data bits low byte" */
2713 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2714 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2715 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2717 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2719 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2720 return ERROR_JTAG_INIT_FAILED;
2724 nTRSTnOE = 0x00; /* no output enable for nTRST */
2726 nSRSTnOE = 0x00; /* no output enable for nSRST */
2729 high_direction = 0x03;
2731 /* initialize high port */
2732 buf[0] = 0x82; /* command "set data bits high byte" */
2733 buf[1] = high_output;
2734 buf[2] = high_direction;
2735 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2737 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2739 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2740 return ERROR_JTAG_INIT_FAILED;
2746 static void olimex_jtag_blink(void)
2748 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2749 * ACBUS3 is bit 3 of the GPIOH port
2751 if (high_output & 0x08)
2753 /* set port pin high */
2754 high_output &= 0x07;
2758 /* set port pin low */
2759 high_output |= 0x08;
2763 buffer_write(high_output);
2764 buffer_write(high_direction);
2767 static void flyswatter_jtag_blink(void)
2770 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2772 high_output ^= 0x0c;
2775 buffer_write(high_output);
2776 buffer_write(high_direction);
2779 static void turtle_jtag_blink(void)
2782 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2784 if (high_output & 0x08)
2794 buffer_write(high_output);
2795 buffer_write(high_direction);
2798 static int ft2232_quit(void)
2800 #if BUILD_FT2232_FTD2XX == 1
2803 status = FT_Close(ftdih);
2804 #elif BUILD_FT2232_LIBFTDI == 1
2805 ftdi_usb_close(&ftdic);
2807 ftdi_deinit(&ftdic);
2810 free(ft2232_buffer);
2811 ft2232_buffer = NULL;
2816 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2822 ft2232_device_desc = strdup(args[0]);
2823 cp = strchr(ft2232_device_desc, 0);
2824 /* under Win32, the FTD2XX driver appends an "A" to the end
2825 * of the description, this examines the given desc
2826 * and creates the 'missing' _A or non_A variable. */
2827 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2828 /* it was, so make this the "A" version. */
2829 ft2232_device_desc_A = ft2232_device_desc;
2830 /* and *CREATE* the non-A version. */
2831 strcpy(buf, ft2232_device_desc);
2832 cp = strchr(buf, 0);
2834 ft2232_device_desc = strdup(buf);
2836 /* <space > A not defined
2838 sprintf(buf, "%s A", ft2232_device_desc);
2839 ft2232_device_desc_A = strdup(buf);
2844 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2850 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2854 ft2232_serial = strdup(args[0]);
2858 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2864 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2869 ft2232_layout = malloc(strlen(args[0]) + 1);
2870 strcpy(ft2232_layout, args[0]);
2875 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2877 if (argc > MAX_USB_IDS * 2)
2879 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2880 "(maximum is %d pairs)", MAX_USB_IDS);
2881 argc = MAX_USB_IDS * 2;
2883 if (argc < 2 || (argc & 1))
2885 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2887 return ERROR_COMMAND_SYNTAX_ERROR;
2888 /* remove the incomplete trailing id */
2893 int retval = ERROR_OK;
2894 for (i = 0; i < argc; i += 2)
2896 retval = parse_u16(args[i], &ft2232_vid[i >> 1]);
2897 if (ERROR_OK != retval)
2899 retval = parse_u16(args[i + 1], &ft2232_pid[i >> 1]);
2900 if (ERROR_OK != retval)
2905 * Explicitly terminate, in case there are multiples instances of
2908 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2913 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2917 ft2232_latency = atoi(args[0]);
2921 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2927 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2931 /* 7 bits of either ones or zeros. */
2932 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2934 while (num_cycles > 0)
2936 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2937 * at most 7 bits per invocation. Here we invoke it potentially
2940 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2942 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2944 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2945 retval = ERROR_JTAG_QUEUE_FAILED;
2950 /* there are no state transitions in this code, so omit state tracking */
2952 /* command "Clock Data to TMS/CS Pin (no Read)" */
2956 buffer_write(bitcount_per_command - 1);
2958 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2963 num_cycles -= bitcount_per_command;
2969 /* ---------------------------------------------------------------------
2970 * Support for IceBear JTAG adapter from Section5:
2971 * http://section5.ch/icebear
2973 * Author: Sten, debian@sansys-electronic.com
2976 /* Icebear pin layout
2978 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2979 * GND GND | 4 3| n.c.
2980 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2981 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2982 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2983 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2984 * ADBUS2 TDO |14 13| GND GND
2986 * ADBUS0 O L TCK ACBUS0 GND
2987 * ADBUS1 O L TDI ACBUS1 GND
2988 * ADBUS2 I TDO ACBUS2 n.c.
2989 * ADBUS3 O H TMS ACBUS3 n.c.
2995 static int icebear_jtag_init(void) {
2997 uint32_t bytes_written;
2999 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3000 low_output = 0x08; /* high: TMS; low: TCK TDI */
3004 enum reset_types jtag_reset_config = jtag_get_reset_config();
3005 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3006 low_direction &= ~nTRST; /* nTRST high impedance */
3009 low_direction |= nTRST;
3010 low_output |= nTRST;
3013 low_direction |= nSRST;
3014 low_output |= nSRST;
3016 /* initialize low byte for jtag */
3017 buf[0] = 0x80; /* command "set data bits low byte" */
3018 buf[1] = low_output;
3019 buf[2] = low_direction;
3020 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3022 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3023 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3024 return ERROR_JTAG_INIT_FAILED;
3028 high_direction = 0x00;
3031 /* initialize high port */
3032 buf[0] = 0x82; /* command "set data bits high byte" */
3033 buf[1] = high_output; /* value */
3034 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3035 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3037 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3038 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3039 return ERROR_JTAG_INIT_FAILED;
3045 static void icebear_jtag_reset(int trst, int srst) {
3048 low_direction |= nTRST;
3049 low_output &= ~nTRST;
3051 else if (trst == 0) {
3052 enum reset_types jtag_reset_config = jtag_get_reset_config();
3053 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3054 low_direction &= ~nTRST;
3056 low_output |= nTRST;
3060 low_output &= ~nSRST;
3062 else if (srst == 0) {
3063 low_output |= nSRST;
3066 /* command "set data bits low byte" */
3068 buffer_write(low_output);
3069 buffer_write(low_direction);
3071 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);