1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
25 /* This code uses information contained in the MPSSE specification which was
27 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
28 * Hereafter this is called the "MPSSE Spec".
40 #include "replacements.h"
42 /* project specific includes */
46 #include "configuration.h"
47 #include "time_support.h"
54 /* FT2232 access library includes */
55 #if BUILD_FT2232_FTD2XX == 1
57 #elif BUILD_FT2232_LIBFTDI == 1
61 /* enable this to debug io latency
64 #define _DEBUG_USB_IO_
67 /* enable this to debug communication
70 #define _DEBUG_USB_COMMS_
73 int ft2232_execute_queue(void);
75 int ft2232_speed(int speed);
76 int ft2232_speed_div(int speed, int* khz);
77 int ft2232_khz(int khz, int* jtag_speed);
78 int ft2232_register_commands(struct command_context_s* cmd_ctx);
79 int ft2232_init(void);
80 int ft2232_quit(void);
82 int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
83 int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
84 int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
85 int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
86 int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
90 * Function ft2232_stableclocks
91 * will send out \a num_cycles on the TCK line while the TAP(s)
92 * are in a stable state. Calling code must ensure that current state is
93 * stable, that verification is not done in here.
94 * @param num_cycles is the count of clocks cycles to send.
95 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
97 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
100 char* ft2232_device_desc = NULL;
101 char* ft2232_serial = NULL;
102 char* ft2232_layout = NULL;
103 unsigned char ft2232_latency = 2;
105 #define MAX_USB_IDS 8
106 /* vid = pid = 0 marks the end of the list */
107 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
108 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
110 typedef struct ft2232_layout_s
114 void (*reset)(int trst, int srst);
118 /* init procedures for supported layouts */
119 int usbjtag_init(void);
120 int jtagkey_init(void);
121 int olimex_jtag_init(void);
122 int flyswatter_init(void);
123 int turtle_init(void);
124 int comstick_init(void);
125 int stm32stick_init(void);
126 int axm0432_jtag_init(void);
127 int sheevaplug_init(void);
129 /* reset procedures for supported layouts */
130 void usbjtag_reset(int trst, int srst);
131 void jtagkey_reset(int trst, int srst);
132 void olimex_jtag_reset(int trst, int srst);
133 void flyswatter_reset(int trst, int srst);
134 void turtle_reset(int trst, int srst);
135 void comstick_reset(int trst, int srst);
136 void stm32stick_reset(int trst, int srst);
137 void axm0432_jtag_reset(int trst, int srst);
138 void sheevaplug_reset(int trst, int srst);
140 /* blink procedures for layouts that support a blinking led */
141 void olimex_jtag_blink(void);
142 void flyswatter_jtag_blink(void);
143 void turtle_jtag_blink(void);
145 ft2232_layout_t ft2232_layouts[] =
147 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
148 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
149 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
150 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
151 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
152 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
153 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
154 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
155 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
156 { "comstick", comstick_init, comstick_reset, NULL },
157 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
158 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
159 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
160 { NULL, NULL, NULL },
163 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
165 static ft2232_layout_t* layout;
166 static u8 low_output = 0x0;
167 static u8 low_direction = 0x0;
168 static u8 high_output = 0x0;
169 static u8 high_direction = 0x0;
171 #if BUILD_FT2232_FTD2XX == 1
172 static FT_HANDLE ftdih = NULL;
173 #elif BUILD_FT2232_LIBFTDI == 1
174 static struct ftdi_context ftdic;
178 static jtag_command_t* first_unsent; /* next command that has to be sent */
179 static int require_send;
181 static u8* ft2232_buffer = NULL;
182 static int ft2232_buffer_size = 0;
183 static int ft2232_read_pointer = 0;
184 static int ft2232_expect_read = 0;
186 #define FT2232_BUFFER_SIZE 131072
187 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
188 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
190 jtag_interface_t ft2232_interface =
193 .execute_queue = ft2232_execute_queue,
194 .speed = ft2232_speed,
195 .speed_div = ft2232_speed_div,
197 .register_commands = ft2232_register_commands,
202 int ft2232_write(u8* buf, int size, u32* bytes_written)
204 #if BUILD_FT2232_FTD2XX == 1
206 DWORD dw_bytes_written;
207 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
209 *bytes_written = dw_bytes_written;
210 LOG_ERROR("FT_Write returned: %lu", status);
211 return ERROR_JTAG_DEVICE_ERROR;
215 *bytes_written = dw_bytes_written;
218 #elif BUILD_FT2232_LIBFTDI == 1
220 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
223 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
224 return ERROR_JTAG_DEVICE_ERROR;
228 *bytes_written = retval;
235 int ft2232_read(u8* buf, int size, u32* bytes_read)
237 #if BUILD_FT2232_FTD2XX == 1
243 while ( (*bytes_read < size) && timeout-- )
245 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
246 *bytes_read, &dw_bytes_read) ) != FT_OK )
249 LOG_ERROR("FT_Read returned: %lu", status);
250 return ERROR_JTAG_DEVICE_ERROR;
252 *bytes_read += dw_bytes_read;
255 #elif BUILD_FT2232_LIBFTDI == 1
260 while ( (*bytes_read < size) && timeout-- )
262 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
265 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
266 return ERROR_JTAG_DEVICE_ERROR;
268 *bytes_read += retval;
273 if (*bytes_read < size)
275 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
276 return ERROR_JTAG_DEVICE_ERROR;
283 int ft2232_speed(int speed)
289 buf[0] = 0x86; /* command "set divisor" */
290 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
291 buf[2] = (speed >> 8) & 0xff; /* valueH */
293 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
294 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
296 LOG_ERROR("couldn't set FT2232 TCK speed");
304 int ft2232_speed_div(int speed, int* khz)
306 /* Take a look in the FT2232 manual,
307 * AN2232C-01 Command Processor for
308 * MPSSE and MCU Host Bus. Chapter 3.8 */
310 *khz = 6000 / (1 + speed);
316 int ft2232_khz(int khz, int* jtag_speed)
320 LOG_ERROR("RCLK not supported");
324 /* Take a look in the FT2232 manual,
325 * AN2232C-01 Command Processor for
326 * MPSSE and MCU Host Bus. Chapter 3.8
328 * We will calc here with a multiplier
329 * of 10 for better rounding later. */
331 /* Calc speed, (6000 / khz) - 1 */
332 /* Use 65000 for better rounding */
333 *jtag_speed = (60000 / khz) - 10;
335 /* Add 0.9 for rounding */
338 /* Calc real speed */
339 *jtag_speed = *jtag_speed / 10;
341 /* Check if speed is greater than 0 */
347 /* Check max value */
348 if (*jtag_speed > 0xFFFF)
350 *jtag_speed = 0xFFFF;
357 int ft2232_register_commands(struct command_context_s* cmd_ctx)
359 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
360 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
361 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
362 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
363 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
364 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
365 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
366 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
367 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
368 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
373 void ft2232_end_state(tap_state_t state)
375 if (tap_is_state_stable(state))
376 tap_set_end_state(state);
379 LOG_ERROR("BUG: %i is not a valid end state", state);
385 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
387 int num_bytes = (scan_size + 7) / 8;
388 int bits_left = scan_size;
391 while (num_bytes-- > 1)
393 buffer[cur_byte] = BUFFER_READ;
398 buffer[cur_byte] = 0x0;
402 buffer[cur_byte] = BUFFER_READ >> 1;
405 buffer[cur_byte] = ( buffer[cur_byte] | ( (BUFFER_READ & 0x02) << 6 ) ) >> (8 - bits_left);
409 void ft2232_debug_dump_buffer(void)
415 for (i = 0; i < ft2232_buffer_size; i++)
417 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
420 LOG_DEBUG("%s", line);
426 LOG_DEBUG("%s", line);
430 int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
440 #ifdef _DEBUG_USB_IO_
441 struct timeval start, inter, inter2, end;
442 struct timeval d_inter, d_inter2, d_end;
445 #ifdef _DEBUG_USB_COMMS_
446 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
447 ft2232_debug_dump_buffer();
450 #ifdef _DEBUG_USB_IO_
451 gettimeofday(&start, NULL);
454 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
456 LOG_ERROR("couldn't write MPSSE commands to FT2232");
460 #ifdef _DEBUG_USB_IO_
461 gettimeofday(&inter, NULL);
464 if (ft2232_expect_read)
467 ft2232_buffer_size = 0;
469 #ifdef _DEBUG_USB_IO_
470 gettimeofday(&inter2, NULL);
473 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
475 LOG_ERROR("couldn't read from FT2232");
479 #ifdef _DEBUG_USB_IO_
480 gettimeofday(&end, NULL);
482 timeval_subtract(&d_inter, &inter, &start);
483 timeval_subtract(&d_inter2, &inter2, &start);
484 timeval_subtract(&d_end, &end, &start);
486 LOG_INFO("inter: %i.%06i, inter2: %i.%06i end: %i.%06i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec,
487 d_inter2.tv_usec, d_end.tv_sec,
491 ft2232_buffer_size = bytes_read;
493 if (ft2232_expect_read != ft2232_buffer_size)
495 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
498 ft2232_debug_dump_buffer();
503 #ifdef _DEBUG_USB_COMMS_
504 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
505 ft2232_debug_dump_buffer();
509 ft2232_expect_read = 0;
510 ft2232_read_pointer = 0;
512 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
513 * that wasn't handled by a caller-provided error handler
523 type = jtag_scan_type(cmd->cmd.scan);
524 if (type != SCAN_OUT)
526 scan_size = jtag_scan_size(cmd->cmd.scan);
527 buffer = calloc(CEIL(scan_size, 8), 1);
528 ft2232_read_scan(type, buffer, scan_size);
529 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
530 retval = ERROR_JTAG_QUEUE_FAILED;
542 ft2232_buffer_size = 0;
548 void ft2232_add_pathmove(pathmove_command_t* cmd)
550 int num_states = cmd->num_states;
555 u8 tms_byte = 0; /* zero this on each MPSSE batch */
559 int num_states_batch = num_states > 7 ? 7 : num_states;
561 /* command "Clock Data to TMS/CS Pin (no Read)" */
564 /* number of states remaining */
565 BUFFER_ADD = num_states_batch - 1;
567 while (num_states_batch--)
569 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
570 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
571 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
572 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
575 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
576 tap_get_state() ), tap_state_name(cmd->path[state_count]) );
580 tap_set_state(cmd->path[state_count]);
585 BUFFER_ADD = tms_byte;
588 tap_set_end_state(tap_get_state());
592 void ft2232_add_scan(int ir_scan, enum scan_type type, u8* buffer, int scan_size)
594 int num_bytes = (scan_size + 7) / 8;
595 int bits_left = scan_size;
599 if ( !( ( !ir_scan && (tap_get_state() == TAP_DRSHIFT) )
600 || ( ir_scan && (tap_get_state() == TAP_IRSHIFT) ) ) )
602 /* command "Clock Data to TMS/CS Pin (no Read)" */
605 BUFFER_ADD = 0x6; /* scan 7 bits */
610 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IRSHIFT);
611 tap_set_state(TAP_IRSHIFT);
615 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
616 tap_set_state(TAP_DRSHIFT);
618 /* LOG_DEBUG("added TMS scan (no read)"); */
621 /* add command for complete bytes */
622 while (num_bytes > 1)
627 /* Clock Data Bytes In and Out LSB First */
629 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
631 else if (type == SCAN_OUT)
633 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
635 /* LOG_DEBUG("added TDI bytes (o)"); */
637 else if (type == SCAN_IN)
639 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
641 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
644 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
645 num_bytes -= thisrun_bytes;
646 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
647 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
651 /* add complete bytes */
652 while (thisrun_bytes-- > 0)
654 BUFFER_ADD = buffer[cur_byte];
659 else /* (type == SCAN_IN) */
661 bits_left -= 8 * (thisrun_bytes);
665 /* the most signifcant bit is scanned during TAP movement */
667 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
671 /* process remaining bits but the last one */
676 /* Clock Data Bits In and Out LSB First */
678 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
680 else if (type == SCAN_OUT)
682 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
684 /* LOG_DEBUG("added TDI bits (o)"); */
686 else if (type == SCAN_IN)
688 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
690 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
692 BUFFER_ADD = bits_left - 2;
694 BUFFER_ADD = buffer[cur_byte];
697 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
698 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
702 /* Clock Data Bits In and Out LSB First */
704 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
706 else if (type == SCAN_OUT)
708 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
710 /* LOG_DEBUG("added TDI bits (o)"); */
712 else if (type == SCAN_IN)
714 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
716 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
719 BUFFER_ADD = last_bit;
723 /* move from Shift-IR/DR to end state */
724 if (type != SCAN_OUT)
726 /* Clock Data to TMS/CS Pin with Read */
728 /* LOG_DEBUG("added TMS scan (read)"); */
732 /* Clock Data to TMS/CS Pin (no Read) */
734 /* LOG_DEBUG("added TMS scan (no read)"); */
736 BUFFER_ADD = 0x6; /* scan 7 bits */
738 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
739 tap_set_state( tap_get_end_state() );
744 int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
746 int num_bytes = (scan_size + 7) / 8;
747 int bits_left = scan_size;
750 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
751 u8* receive_pointer = receive_buffer;
755 int thisrun_read = 0;
759 LOG_ERROR("BUG: large IR scans are not supported");
763 if (tap_get_state() != TAP_DRSHIFT)
765 /* command "Clock Data to TMS/CS Pin (no Read)" */
768 BUFFER_ADD = 0x6; /* scan 7 bits */
771 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
772 tap_set_state(TAP_DRSHIFT);
775 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
777 LOG_ERROR("couldn't write MPSSE commands to FT2232");
780 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
781 ft2232_buffer_size = 0;
783 /* add command for complete bytes */
784 while (num_bytes > 1)
790 /* Clock Data Bytes In and Out LSB First */
792 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
794 else if (type == SCAN_OUT)
796 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
798 /* LOG_DEBUG("added TDI bytes (o)"); */
800 else if (type == SCAN_IN)
802 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
804 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
807 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
808 thisrun_read = thisrun_bytes;
809 num_bytes -= thisrun_bytes;
810 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
811 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
815 /* add complete bytes */
816 while (thisrun_bytes-- > 0)
818 BUFFER_ADD = buffer[cur_byte];
823 else /* (type == SCAN_IN) */
825 bits_left -= 8 * (thisrun_bytes);
828 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
830 LOG_ERROR("couldn't write MPSSE commands to FT2232");
833 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
834 ft2232_buffer_size = 0;
836 if (type != SCAN_OUT)
838 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
840 LOG_ERROR("couldn't read from FT2232");
843 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
844 receive_pointer += bytes_read;
850 /* the most signifcant bit is scanned during TAP movement */
852 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
856 /* process remaining bits but the last one */
861 /* Clock Data Bits In and Out LSB First */
863 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
865 else if (type == SCAN_OUT)
867 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
869 /* LOG_DEBUG("added TDI bits (o)"); */
871 else if (type == SCAN_IN)
873 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
875 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
877 BUFFER_ADD = bits_left - 2;
879 BUFFER_ADD = buffer[cur_byte];
881 if (type != SCAN_OUT)
885 if (tap_get_end_state() == TAP_DRSHIFT)
889 /* Clock Data Bits In and Out LSB First */
891 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
893 else if (type == SCAN_OUT)
895 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
897 /* LOG_DEBUG("added TDI bits (o)"); */
899 else if (type == SCAN_IN)
901 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
903 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
906 BUFFER_ADD = last_bit;
910 /* move from Shift-IR/DR to end state */
911 if (type != SCAN_OUT)
913 /* Clock Data to TMS/CS Pin with Read */
915 /* LOG_DEBUG("added TMS scan (read)"); */
919 /* Clock Data to TMS/CS Pin (no Read) */
921 /* LOG_DEBUG("added TMS scan (no read)"); */
924 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
925 tap_set_state( tap_get_end_state() );
928 if (type != SCAN_OUT)
931 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
933 LOG_ERROR("couldn't write MPSSE commands to FT2232");
936 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
937 ft2232_buffer_size = 0;
939 if (type != SCAN_OUT)
941 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
943 LOG_ERROR("couldn't read from FT2232");
946 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
947 receive_pointer += bytes_read;
954 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
956 int predicted_size = 3;
957 int num_bytes = (scan_size - 1) / 8;
959 if (tap_get_state() != TAP_DRSHIFT)
962 if (type == SCAN_IN) /* only from device to host */
965 predicted_size += CEIL(num_bytes, 65536) * 3;
966 /* remaining bits - 1 (up to 7) */
967 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
969 else /* host to device, or bidirectional */
972 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
973 /* remaining bits -1 (up to 7) */
974 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
977 return predicted_size;
981 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
983 int predicted_size = 0;
985 if (type != SCAN_OUT)
988 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
990 /* remaining bits - 1 */
991 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
993 /* last bit (from TMS scan) */
997 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
999 return predicted_size;
1003 void usbjtag_reset(int trst, int srst)
1007 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1008 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1010 low_output &= ~nTRST; /* switch output low */
1014 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1015 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1017 low_output |= nTRST; /* switch output high */
1022 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1023 low_output &= ~nSRST; /* switch output low */
1025 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1029 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1030 low_output |= nSRST; /* switch output high */
1032 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1035 /* command "set data bits low byte" */
1037 BUFFER_ADD = low_output;
1038 BUFFER_ADD = low_direction;
1042 void jtagkey_reset(int trst, int srst)
1046 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1047 high_output &= ~nTRSTnOE;
1049 high_output &= ~nTRST;
1053 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1054 high_output |= nTRSTnOE;
1056 high_output |= nTRST;
1061 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1062 high_output &= ~nSRST;
1064 high_output &= ~nSRSTnOE;
1068 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1069 high_output |= nSRST;
1071 high_output |= nSRSTnOE;
1074 /* command "set data bits high byte" */
1076 BUFFER_ADD = high_output;
1077 BUFFER_ADD = high_direction;
1078 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1083 void olimex_jtag_reset(int trst, int srst)
1087 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1088 high_output &= ~nTRSTnOE;
1090 high_output &= ~nTRST;
1094 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1095 high_output |= nTRSTnOE;
1097 high_output |= nTRST;
1102 high_output |= nSRST;
1106 high_output &= ~nSRST;
1109 /* command "set data bits high byte" */
1111 BUFFER_ADD = high_output;
1112 BUFFER_ADD = high_direction;
1113 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1118 void axm0432_jtag_reset(int trst, int srst)
1122 tap_set_state(TAP_RESET);
1123 high_output &= ~nTRST;
1127 high_output |= nTRST;
1132 high_output &= ~nSRST;
1136 high_output |= nSRST;
1139 /* command "set data bits low byte" */
1141 BUFFER_ADD = high_output;
1142 BUFFER_ADD = high_direction;
1143 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1148 void flyswatter_reset(int trst, int srst)
1152 low_output &= ~nTRST;
1156 low_output |= nTRST;
1161 low_output |= nSRST;
1165 low_output &= ~nSRST;
1168 /* command "set data bits low byte" */
1170 BUFFER_ADD = low_output;
1171 BUFFER_ADD = low_direction;
1172 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1176 void turtle_reset(int trst, int srst)
1182 low_output |= nSRST;
1186 low_output &= ~nSRST;
1189 /* command "set data bits low byte" */
1191 BUFFER_ADD = low_output;
1192 BUFFER_ADD = low_direction;
1193 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1197 void comstick_reset(int trst, int srst)
1201 high_output &= ~nTRST;
1205 high_output |= nTRST;
1210 high_output &= ~nSRST;
1214 high_output |= nSRST;
1217 /* command "set data bits high byte" */
1219 BUFFER_ADD = high_output;
1220 BUFFER_ADD = high_direction;
1221 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1226 void stm32stick_reset(int trst, int srst)
1230 high_output &= ~nTRST;
1234 high_output |= nTRST;
1239 low_output &= ~nSRST;
1243 low_output |= nSRST;
1246 /* command "set data bits low byte" */
1248 BUFFER_ADD = low_output;
1249 BUFFER_ADD = low_direction;
1251 /* command "set data bits high byte" */
1253 BUFFER_ADD = high_output;
1254 BUFFER_ADD = high_direction;
1255 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1261 void sheevaplug_reset(int trst, int srst)
1264 high_output &= ~nTRST;
1266 high_output |= nTRST;
1269 high_output &= ~nSRSTnOE;
1271 high_output |= nSRSTnOE;
1273 /* command "set data bits high byte" */
1275 BUFFER_ADD = high_output;
1276 BUFFER_ADD = high_direction;
1277 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1280 int ft2232_execute_queue()
1282 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1284 int scan_size; /* size of IR or DR scan */
1285 enum scan_type type;
1287 int predicted_size = 0;
1290 first_unsent = cmd; /* next command that has to be sent */
1293 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1294 * that wasn't handled by a caller-provided error handler
1298 ft2232_buffer_size = 0;
1299 ft2232_expect_read = 0;
1301 /* blink, if the current layout has that feature */
1309 case JTAG_END_STATE:
1310 if (cmd->cmd.end_state->end_state != -1)
1311 ft2232_end_state(cmd->cmd.end_state->end_state);
1315 /* only send the maximum buffer size that FT2232C can handle */
1317 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1319 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1320 retval = ERROR_JTAG_QUEUE_FAILED;
1325 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1327 tap_set_state(TAP_RESET);
1329 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1332 #ifdef _DEBUG_JTAG_IO_
1333 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1338 /* only send the maximum buffer size that FT2232C can handle */
1340 if (tap_get_state() != TAP_IDLE)
1341 predicted_size += 3;
1342 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1343 if ( (cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1344 predicted_size += 3;
1345 if ( (cmd->cmd.runtest->end_state == -1) && (tap_get_end_state() != TAP_IDLE) )
1346 predicted_size += 3;
1347 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1349 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1350 retval = ERROR_JTAG_QUEUE_FAILED;
1354 if (tap_get_state() != TAP_IDLE)
1356 /* command "Clock Data to TMS/CS Pin (no Read)" */
1358 BUFFER_ADD = 0x6; /* scan 7 bits */
1361 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IDLE);
1362 tap_set_state(TAP_IDLE);
1365 i = cmd->cmd.runtest->num_cycles;
1368 /* command "Clock Data to TMS/CS Pin (no Read)" */
1372 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1376 tap_set_state(TAP_IDLE);
1377 i -= (i > 7) ? 7 : i;
1378 /* LOG_DEBUG("added TMS scan (no read)"); */
1381 if (cmd->cmd.runtest->end_state != -1)
1382 ft2232_end_state(cmd->cmd.runtest->end_state);
1384 if ( tap_get_state() != tap_get_end_state() )
1386 /* command "Clock Data to TMS/CS Pin (no Read)" */
1391 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1392 tap_set_state( tap_get_end_state() );
1393 /* LOG_DEBUG("added TMS scan (no read)"); */
1396 #ifdef _DEBUG_JTAG_IO_
1397 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1401 case JTAG_STATEMOVE:
1402 /* only send the maximum buffer size that FT2232C can handle */
1404 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1406 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1407 retval = ERROR_JTAG_QUEUE_FAILED;
1411 if (cmd->cmd.statemove->end_state != -1)
1412 ft2232_end_state(cmd->cmd.statemove->end_state);
1414 /* command "Clock Data to TMS/CS Pin (no Read)" */
1417 BUFFER_ADD = 0x6; /* scan 7 bits */
1420 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1421 /* LOG_DEBUG("added TMS scan (no read)"); */
1422 tap_set_state( tap_get_end_state() );
1424 #ifdef _DEBUG_JTAG_IO_
1425 LOG_DEBUG( "statemove: %s", tap_state_name( tap_get_end_state() ) );
1430 /* only send the maximum buffer size that FT2232C can handle */
1431 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1432 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1434 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1435 retval = ERROR_JTAG_QUEUE_FAILED;
1439 ft2232_add_pathmove(cmd->cmd.pathmove);
1441 #ifdef _DEBUG_JTAG_IO_
1442 LOG_DEBUG( "pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1443 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]) );
1448 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1449 type = jtag_scan_type(cmd->cmd.scan);
1450 predicted_size = ft2232_predict_scan_out(scan_size, type);
1451 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1453 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1454 /* unsent commands before this */
1455 if (first_unsent != cmd)
1456 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1457 retval = ERROR_JTAG_QUEUE_FAILED;
1459 /* current command */
1460 if (cmd->cmd.scan->end_state != -1)
1461 ft2232_end_state(cmd->cmd.scan->end_state);
1462 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1464 first_unsent = cmd->next;
1469 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1471 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1474 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1475 retval = ERROR_JTAG_QUEUE_FAILED;
1479 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1480 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1481 if (cmd->cmd.scan->end_state != -1)
1482 ft2232_end_state(cmd->cmd.scan->end_state);
1483 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1487 #ifdef _DEBUG_JTAG_IO_
1488 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1489 tap_state_name( tap_get_end_state() ) );
1494 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1495 retval = ERROR_JTAG_QUEUE_FAILED;
1496 first_unsent = cmd->next;
1497 jtag_sleep(cmd->cmd.sleep->us);
1498 #ifdef _DEBUG_JTAG_IO_
1499 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1503 case JTAG_STABLECLOCKS:
1505 /* this is only allowed while in a stable state. A check for a stable
1506 * state was done in jtag_add_clocks()
1508 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1509 retval = ERROR_JTAG_QUEUE_FAILED;
1510 #ifdef _DEBUG_JTAG_IO_
1511 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1516 LOG_ERROR("BUG: unknown JTAG command type encountered");
1523 if (require_send > 0)
1524 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1525 retval = ERROR_JTAG_QUEUE_FAILED;
1531 #if BUILD_FT2232_FTD2XX == 1
1532 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1535 DWORD openex_flags = 0;
1536 char* openex_string = NULL;
1539 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1542 /* Add non-standard Vid/Pid to the linux driver */
1543 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1545 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1549 if (ft2232_device_desc && ft2232_serial)
1551 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1552 ft2232_device_desc = NULL;
1555 if (ft2232_device_desc)
1557 openex_string = ft2232_device_desc;
1558 openex_flags = FT_OPEN_BY_DESCRIPTION;
1560 else if (ft2232_serial)
1562 openex_string = ft2232_serial;
1563 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1567 LOG_ERROR("neither device description nor serial number specified");
1568 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1570 return ERROR_JTAG_INIT_FAILED;
1573 if ( ( status = FT_OpenEx(openex_string, openex_flags, &ftdih) ) != FT_OK )
1579 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1581 return ERROR_JTAG_INIT_FAILED;
1583 LOG_ERROR("unable to open ftdi device: %lu", status);
1584 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1585 if (status == FT_OK)
1587 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1590 for (i = 0; i < num_devices; i++)
1591 desc_array[i] = malloc(64);
1593 desc_array[num_devices] = NULL;
1595 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1597 if (status == FT_OK)
1599 LOG_ERROR("ListDevices: %lu\n", num_devices);
1600 for (i = 0; i < num_devices; i++)
1601 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1604 for (i = 0; i < num_devices; i++)
1605 free(desc_array[i]);
1611 LOG_ERROR("ListDevices: NONE\n");
1613 return ERROR_JTAG_INIT_FAILED;
1616 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1618 LOG_ERROR("unable to set latency timer: %lu", status);
1619 return ERROR_JTAG_INIT_FAILED;
1622 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1624 LOG_ERROR("unable to get latency timer: %lu", status);
1625 return ERROR_JTAG_INIT_FAILED;
1629 LOG_DEBUG("current latency timer: %i", latency_timer);
1632 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1634 LOG_ERROR("unable to set timeouts: %lu", status);
1635 return ERROR_JTAG_INIT_FAILED;
1638 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1640 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1641 return ERROR_JTAG_INIT_FAILED;
1648 static int ft2232_purge_ftd2xx(void)
1652 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1654 LOG_ERROR("error purging ftd2xx device: %lu", status);
1655 return ERROR_JTAG_INIT_FAILED;
1662 #endif /* BUILD_FT2232_FTD2XX == 1 */
1664 #if BUILD_FT2232_LIBFTDI == 1
1665 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1669 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1670 ft2232_layout, vid, pid);
1672 if (ftdi_init(&ftdic) < 0)
1673 return ERROR_JTAG_INIT_FAILED;
1675 /* context, vendor id, product id */
1676 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1680 LOG_WARNING("unable to open ftdi device (trying more): %s",
1683 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1685 return ERROR_JTAG_INIT_FAILED;
1688 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1690 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1691 return ERROR_JTAG_INIT_FAILED;
1694 if (ftdi_usb_reset(&ftdic) < 0)
1696 LOG_ERROR("unable to reset ftdi device");
1697 return ERROR_JTAG_INIT_FAILED;
1700 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1702 LOG_ERROR("unable to set latency timer");
1703 return ERROR_JTAG_INIT_FAILED;
1706 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1708 LOG_ERROR("unable to get latency timer");
1709 return ERROR_JTAG_INIT_FAILED;
1713 LOG_DEBUG("current latency timer: %i", latency_timer);
1716 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1722 static int ft2232_purge_libftdi(void)
1724 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1726 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1727 return ERROR_JTAG_INIT_FAILED;
1734 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1736 int ft2232_init(void)
1741 ft2232_layout_t* cur_layout = ft2232_layouts;
1744 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1746 ft2232_layout = "usbjtag";
1747 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1750 while (cur_layout->name)
1752 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1754 layout = cur_layout;
1762 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1763 return ERROR_JTAG_INIT_FAILED;
1769 * "more indicates that there are more IDs to try, so we should
1770 * not print an error for an ID mismatch (but for anything
1773 * try_more indicates that the error code returned indicates an
1774 * ID mismatch (and nothing else) and that we should proceeed
1775 * with the next ID pair.
1777 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
1780 #if BUILD_FT2232_FTD2XX == 1
1781 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1783 #elif BUILD_FT2232_LIBFTDI == 1
1784 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1789 if (!more || !try_more)
1793 ft2232_buffer_size = 0;
1794 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1796 if (layout->init() != ERROR_OK)
1797 return ERROR_JTAG_INIT_FAILED;
1799 ft2232_speed(jtag_speed);
1801 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1802 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
1804 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1805 return ERROR_JTAG_INIT_FAILED;
1808 #if BUILD_FT2232_FTD2XX == 1
1809 return ft2232_purge_ftd2xx();
1810 #elif BUILD_FT2232_LIBFTDI == 1
1811 return ft2232_purge_libftdi();
1818 int usbjtag_init(void)
1824 low_direction = 0x0b;
1826 if (strcmp(ft2232_layout, "usbjtag") == 0)
1833 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1840 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1847 low_direction = 0x8b;
1851 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1852 return ERROR_JTAG_INIT_FAILED;
1855 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1857 low_direction &= ~nTRSTnOE; /* nTRST input */
1858 low_output &= ~nTRST; /* nTRST = 0 */
1862 low_direction |= nTRSTnOE; /* nTRST output */
1863 low_output |= nTRST; /* nTRST = 1 */
1866 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1868 low_direction |= nSRSTnOE; /* nSRST output */
1869 low_output |= nSRST; /* nSRST = 1 */
1873 low_direction &= ~nSRSTnOE; /* nSRST input */
1874 low_output &= ~nSRST; /* nSRST = 0 */
1877 /* initialize low byte for jtag */
1878 buf[0] = 0x80; /* command "set data bits low byte" */
1879 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1880 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1881 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1883 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1885 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1886 return ERROR_JTAG_INIT_FAILED;
1893 int axm0432_jtag_init(void)
1899 low_direction = 0x2b;
1901 /* initialize low byte for jtag */
1902 buf[0] = 0x80; /* command "set data bits low byte" */
1903 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1904 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1905 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1907 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1909 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1910 return ERROR_JTAG_INIT_FAILED;
1913 if (strcmp(layout->name, "axm0432_jtag") == 0)
1916 nTRSTnOE = 0x0; /* No output enable for TRST*/
1918 nSRSTnOE = 0x0; /* No output enable for SRST*/
1922 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1927 high_direction = 0x0c;
1929 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1931 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1935 high_output |= nTRST;
1938 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1940 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1944 high_output |= nSRST;
1947 /* initialize high port */
1948 buf[0] = 0x82; /* command "set data bits high byte" */
1949 buf[1] = high_output; /* value */
1950 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1951 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1953 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1955 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1956 return ERROR_JTAG_INIT_FAILED;
1963 int jtagkey_init(void)
1969 low_direction = 0x1b;
1971 /* initialize low byte for jtag */
1972 buf[0] = 0x80; /* command "set data bits low byte" */
1973 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1974 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1975 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1977 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1979 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1980 return ERROR_JTAG_INIT_FAILED;
1983 if (strcmp(layout->name, "jtagkey") == 0)
1990 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
1991 || (strcmp(layout->name, "oocdlink") == 0) )
2000 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2005 high_direction = 0x0f;
2007 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2009 high_output |= nTRSTnOE;
2010 high_output &= ~nTRST;
2014 high_output &= ~nTRSTnOE;
2015 high_output |= nTRST;
2018 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2020 high_output &= ~nSRSTnOE;
2021 high_output |= nSRST;
2025 high_output |= nSRSTnOE;
2026 high_output &= ~nSRST;
2029 /* initialize high port */
2030 buf[0] = 0x82; /* command "set data bits high byte" */
2031 buf[1] = high_output; /* value */
2032 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2033 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2035 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2037 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2038 return ERROR_JTAG_INIT_FAILED;
2045 int olimex_jtag_init(void)
2051 low_direction = 0x1b;
2053 /* initialize low byte for jtag */
2054 buf[0] = 0x80; /* command "set data bits low byte" */
2055 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2056 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2057 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2059 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2061 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2062 return ERROR_JTAG_INIT_FAILED;
2068 nSRSTnOE = 0x00; /* no output enable for nSRST */
2071 high_direction = 0x0f;
2073 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2075 high_output |= nTRSTnOE;
2076 high_output &= ~nTRST;
2080 high_output &= ~nTRSTnOE;
2081 high_output |= nTRST;
2084 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2086 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2090 high_output &= ~nSRST;
2093 /* turn red LED on */
2094 high_output |= 0x08;
2096 /* initialize high port */
2097 buf[0] = 0x82; /* command "set data bits high byte" */
2098 buf[1] = high_output; /* value */
2099 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2100 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2102 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2104 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2105 return ERROR_JTAG_INIT_FAILED;
2112 int flyswatter_init(void)
2118 low_direction = 0xfb;
2120 /* initialize low byte for jtag */
2121 buf[0] = 0x80; /* command "set data bits low byte" */
2122 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2123 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2124 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2126 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2128 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2129 return ERROR_JTAG_INIT_FAILED;
2133 nTRSTnOE = 0x0; /* not output enable for nTRST */
2135 nSRSTnOE = 0x00; /* no output enable for nSRST */
2138 high_direction = 0x0c;
2140 /* turn red LED3 on, LED2 off */
2141 high_output |= 0x08;
2143 /* initialize high port */
2144 buf[0] = 0x82; /* command "set data bits high byte" */
2145 buf[1] = high_output; /* value */
2146 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2147 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2149 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2151 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2152 return ERROR_JTAG_INIT_FAILED;
2159 int turtle_init(void)
2165 low_direction = 0x5b;
2167 /* initialize low byte for jtag */
2168 buf[0] = 0x80; /* command "set data bits low byte" */
2169 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2170 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2171 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2173 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2175 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2176 return ERROR_JTAG_INIT_FAILED;
2182 high_direction = 0x0C;
2184 /* initialize high port */
2185 buf[0] = 0x82; /* command "set data bits high byte" */
2186 buf[1] = high_output;
2187 buf[2] = high_direction;
2188 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2190 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2192 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2193 return ERROR_JTAG_INIT_FAILED;
2200 int comstick_init(void)
2206 low_direction = 0x0b;
2208 /* initialize low byte for jtag */
2209 buf[0] = 0x80; /* command "set data bits low byte" */
2210 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2211 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2212 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2214 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2216 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2217 return ERROR_JTAG_INIT_FAILED;
2221 nTRSTnOE = 0x00; /* no output enable for nTRST */
2223 nSRSTnOE = 0x00; /* no output enable for nSRST */
2226 high_direction = 0x03;
2228 /* initialize high port */
2229 buf[0] = 0x82; /* command "set data bits high byte" */
2230 buf[1] = high_output;
2231 buf[2] = high_direction;
2232 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2234 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2236 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2237 return ERROR_JTAG_INIT_FAILED;
2244 int stm32stick_init(void)
2250 low_direction = 0x8b;
2252 /* initialize low byte for jtag */
2253 buf[0] = 0x80; /* command "set data bits low byte" */
2254 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2255 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2256 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2258 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2260 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2261 return ERROR_JTAG_INIT_FAILED;
2265 nTRSTnOE = 0x00; /* no output enable for nTRST */
2267 nSRSTnOE = 0x00; /* no output enable for nSRST */
2270 high_direction = 0x03;
2272 /* initialize high port */
2273 buf[0] = 0x82; /* command "set data bits high byte" */
2274 buf[1] = high_output;
2275 buf[2] = high_direction;
2276 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2278 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2280 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2281 return ERROR_JTAG_INIT_FAILED;
2288 int sheevaplug_init(void)
2294 low_direction = 0x1b;
2296 /* initialize low byte for jtag */
2297 buf[0] = 0x80; /* command "set data bits low byte" */
2298 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2299 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2300 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2302 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2304 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2305 return ERROR_JTAG_INIT_FAILED;
2314 high_direction = 0x0f;
2316 /* nTRST is always push-pull */
2317 high_output &= ~nTRSTnOE;
2318 high_output |= nTRST;
2320 /* nSRST is always open-drain */
2321 high_output |= nSRSTnOE;
2322 high_output &= ~nSRST;
2324 /* initialize high port */
2325 buf[0] = 0x82; /* command "set data bits high byte" */
2326 buf[1] = high_output; /* value */
2327 buf[2] = high_direction; /* all outputs - xRST */
2328 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2330 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2332 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2333 return ERROR_JTAG_INIT_FAILED;
2339 void olimex_jtag_blink(void)
2341 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2342 * ACBUS3 is bit 3 of the GPIOH port
2344 if (high_output & 0x08)
2346 /* set port pin high */
2347 high_output &= 0x07;
2351 /* set port pin low */
2352 high_output |= 0x08;
2356 BUFFER_ADD = high_output;
2357 BUFFER_ADD = high_direction;
2361 void flyswatter_jtag_blink(void)
2364 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2366 high_output ^= 0x0c;
2369 BUFFER_ADD = high_output;
2370 BUFFER_ADD = high_direction;
2374 void turtle_jtag_blink(void)
2377 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2379 if (high_output & 0x08)
2389 BUFFER_ADD = high_output;
2390 BUFFER_ADD = high_direction;
2394 int ft2232_quit(void)
2396 #if BUILD_FT2232_FTD2XX == 1
2399 status = FT_Close(ftdih);
2400 #elif BUILD_FT2232_LIBFTDI == 1
2401 ftdi_disable_bitbang(&ftdic);
2403 ftdi_usb_close(&ftdic);
2405 ftdi_deinit(&ftdic);
2408 free(ft2232_buffer);
2409 ft2232_buffer = NULL;
2415 int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2419 ft2232_device_desc = strdup(args[0]);
2423 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2430 int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2434 ft2232_serial = strdup(args[0]);
2438 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2445 int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2450 ft2232_layout = malloc(strlen(args[0]) + 1);
2451 strcpy(ft2232_layout, args[0]);
2457 int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2461 if (argc > MAX_USB_IDS * 2)
2463 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2464 "(maximum is %d pairs)", MAX_USB_IDS);
2465 argc = MAX_USB_IDS * 2;
2467 if ( argc < 2 || (argc & 1) )
2469 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2474 for (i = 0; i + 1 < argc; i += 2)
2476 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2477 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2481 * Explicitly terminate, in case there are multiples instances of
2484 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2490 int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2494 ft2232_latency = atoi(args[0]);
2498 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2505 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2509 /* 7 bits of either ones or zeros. */
2510 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2512 while (num_cycles > 0)
2514 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2515 * at most 7 bits per invocation. Here we invoke it potentially
2518 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2520 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2522 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2523 retval = ERROR_JTAG_QUEUE_FAILED;
2528 /* command "Clock Data to TMS/CS Pin (no Read)" */
2532 BUFFER_ADD = bitcount_per_command - 1;
2534 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2539 num_cycles -= bitcount_per_command;