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".
36 #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 static int ft2232_execute_queue(void);
63 static int ft2232_speed(int speed);
64 static int ft2232_speed_div(int speed, int* khz);
65 static int ft2232_khz(int khz, int* jtag_speed);
66 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
67 static int ft2232_init(void);
68 static int ft2232_quit(void);
70 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
71 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
72 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
73 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
74 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
78 * Function ft2232_stableclocks
79 * will send out \a num_cycles on the TCK line while the TAP(s)
80 * are in a stable state. Calling code must ensure that current state is
81 * stable, that verification is not done in here.
82 * @param num_cycles is the count of clocks cycles to send.
83 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
85 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
88 static char * ft2232_device_desc_A = NULL;
89 static char* ft2232_device_desc = NULL;
90 static char* ft2232_serial = NULL;
91 static char* ft2232_layout = NULL;
92 static unsigned char ft2232_latency = 2;
95 /* vid = pid = 0 marks the end of the list */
96 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
97 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
99 typedef struct ft2232_layout_s
103 void (*reset)(int trst, int srst);
107 /* init procedures for supported layouts */
108 static int usbjtag_init(void);
109 static int jtagkey_init(void);
110 static int olimex_jtag_init(void);
111 static int flyswatter_init(void);
112 static int turtle_init(void);
113 static int comstick_init(void);
114 static int stm32stick_init(void);
115 static int axm0432_jtag_init(void);
116 static int sheevaplug_init(void);
117 static int icebear_jtag_init(void);
119 /* reset procedures for supported layouts */
120 static void usbjtag_reset(int trst, int srst);
121 static void jtagkey_reset(int trst, int srst);
122 static void olimex_jtag_reset(int trst, int srst);
123 static void flyswatter_reset(int trst, int srst);
124 static void turtle_reset(int trst, int srst);
125 static void comstick_reset(int trst, int srst);
126 static void stm32stick_reset(int trst, int srst);
127 static void axm0432_jtag_reset(int trst, int srst);
128 static void sheevaplug_reset(int trst, int srst);
129 static void icebear_jtag_reset(int trst, int srst);
131 /* blink procedures for layouts that support a blinking led */
132 static void olimex_jtag_blink(void);
133 static void flyswatter_jtag_blink(void);
134 static void turtle_jtag_blink(void);
136 ft2232_layout_t ft2232_layouts[] =
138 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
139 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
140 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
141 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
142 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
143 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
144 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
145 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
146 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
147 { "comstick", comstick_init, comstick_reset, NULL },
148 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
149 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
150 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
151 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
152 { NULL, NULL, NULL, NULL },
155 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
157 static ft2232_layout_t* layout;
158 static u8 low_output = 0x0;
159 static u8 low_direction = 0x0;
160 static u8 high_output = 0x0;
161 static u8 high_direction = 0x0;
163 #if BUILD_FT2232_FTD2XX == 1
164 static FT_HANDLE ftdih = NULL;
165 #elif BUILD_FT2232_LIBFTDI == 1
166 static struct ftdi_context ftdic;
170 static jtag_command_t* first_unsent; /* next command that has to be sent */
171 static int require_send;
173 static u8* ft2232_buffer = NULL;
174 static int ft2232_buffer_size = 0;
175 static int ft2232_read_pointer = 0;
176 static int ft2232_expect_read = 0;
178 #define FT2232_BUFFER_SIZE 131072
179 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
180 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
182 jtag_interface_t ft2232_interface =
185 .execute_queue = ft2232_execute_queue,
186 .speed = ft2232_speed,
187 .speed_div = ft2232_speed_div,
189 .register_commands = ft2232_register_commands,
194 static int ft2232_write(u8* buf, int size, u32* bytes_written)
196 #if BUILD_FT2232_FTD2XX == 1
198 DWORD dw_bytes_written;
199 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
201 *bytes_written = dw_bytes_written;
202 LOG_ERROR("FT_Write returned: %lu", status);
203 return ERROR_JTAG_DEVICE_ERROR;
207 *bytes_written = dw_bytes_written;
210 #elif BUILD_FT2232_LIBFTDI == 1
212 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
215 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
216 return ERROR_JTAG_DEVICE_ERROR;
220 *bytes_written = retval;
227 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
229 #if BUILD_FT2232_FTD2XX == 1
235 while ( (*bytes_read < size) && timeout-- )
237 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
238 *bytes_read, &dw_bytes_read) ) != FT_OK )
241 LOG_ERROR("FT_Read returned: %lu", status);
242 return ERROR_JTAG_DEVICE_ERROR;
244 *bytes_read += dw_bytes_read;
247 #elif BUILD_FT2232_LIBFTDI == 1
252 while ( (*bytes_read < size) && timeout-- )
254 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
257 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
258 return ERROR_JTAG_DEVICE_ERROR;
260 *bytes_read += retval;
265 if (*bytes_read < size)
267 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
268 return ERROR_JTAG_DEVICE_ERROR;
275 static int ft2232_speed(int speed)
281 buf[0] = 0x86; /* command "set divisor" */
282 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
283 buf[2] = (speed >> 8) & 0xff; /* valueH */
285 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
286 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
288 LOG_ERROR("couldn't set FT2232 TCK speed");
296 static int ft2232_speed_div(int speed, int* khz)
298 /* Take a look in the FT2232 manual,
299 * AN2232C-01 Command Processor for
300 * MPSSE and MCU Host Bus. Chapter 3.8 */
302 *khz = 6000 / (1 + speed);
308 static int ft2232_khz(int khz, int* jtag_speed)
312 LOG_ERROR("RCLK not supported");
316 /* Take a look in the FT2232 manual,
317 * AN2232C-01 Command Processor for
318 * MPSSE and MCU Host Bus. Chapter 3.8
320 * We will calc here with a multiplier
321 * of 10 for better rounding later. */
323 /* Calc speed, (6000 / khz) - 1 */
324 /* Use 65000 for better rounding */
325 *jtag_speed = (60000 / khz) - 10;
327 /* Add 0.9 for rounding */
330 /* Calc real speed */
331 *jtag_speed = *jtag_speed / 10;
333 /* Check if speed is greater than 0 */
339 /* Check max value */
340 if (*jtag_speed > 0xFFFF)
342 *jtag_speed = 0xFFFF;
349 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
351 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
352 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
353 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
354 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
355 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
356 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
357 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
358 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
359 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
360 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
365 void ft2232_end_state(tap_state_t state)
367 if (tap_is_state_stable(state))
368 tap_set_end_state(state);
371 LOG_ERROR("BUG: %i is not a valid end state", state);
377 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
379 int num_bytes = (scan_size + 7) / 8;
380 int bits_left = scan_size;
383 while (num_bytes-- > 1)
385 buffer[cur_byte] = BUFFER_READ;
390 buffer[cur_byte] = 0x0;
394 buffer[cur_byte] = BUFFER_READ >> 1;
397 buffer[cur_byte] = ( buffer[cur_byte] | ( (BUFFER_READ & 0x02) << 6 ) ) >> (8 - bits_left);
401 static void ft2232_debug_dump_buffer(void)
407 for (i = 0; i < ft2232_buffer_size; i++)
409 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
412 LOG_DEBUG("%s", line);
418 LOG_DEBUG("%s", line);
422 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
432 #ifdef _DEBUG_USB_IO_
433 struct timeval start, inter, inter2, end;
434 struct timeval d_inter, d_inter2, d_end;
437 #ifdef _DEBUG_USB_COMMS_
438 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
439 ft2232_debug_dump_buffer();
442 #ifdef _DEBUG_USB_IO_
443 gettimeofday(&start, NULL);
446 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
448 LOG_ERROR("couldn't write MPSSE commands to FT2232");
452 #ifdef _DEBUG_USB_IO_
453 gettimeofday(&inter, NULL);
456 if (ft2232_expect_read)
459 ft2232_buffer_size = 0;
461 #ifdef _DEBUG_USB_IO_
462 gettimeofday(&inter2, NULL);
465 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
467 LOG_ERROR("couldn't read from FT2232");
471 #ifdef _DEBUG_USB_IO_
472 gettimeofday(&end, NULL);
474 timeval_subtract(&d_inter, &inter, &start);
475 timeval_subtract(&d_inter2, &inter2, &start);
476 timeval_subtract(&d_end, &end, &start);
478 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
479 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
480 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
481 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
484 ft2232_buffer_size = bytes_read;
486 if (ft2232_expect_read != ft2232_buffer_size)
488 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
491 ft2232_debug_dump_buffer();
496 #ifdef _DEBUG_USB_COMMS_
497 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
498 ft2232_debug_dump_buffer();
502 ft2232_expect_read = 0;
503 ft2232_read_pointer = 0;
505 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
506 * that wasn't handled by a caller-provided error handler
516 type = jtag_scan_type(cmd->cmd.scan);
517 if (type != SCAN_OUT)
519 scan_size = jtag_scan_size(cmd->cmd.scan);
520 buffer = calloc(CEIL(scan_size, 8), 1);
521 ft2232_read_scan(type, buffer, scan_size);
522 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
523 retval = ERROR_JTAG_QUEUE_FAILED;
535 ft2232_buffer_size = 0;
541 static void ft2232_add_pathmove(pathmove_command_t* cmd)
543 int num_states = cmd->num_states;
548 u8 tms_byte = 0; /* zero this on each MPSSE batch */
552 int num_states_batch = num_states > 7 ? 7 : num_states;
554 /* command "Clock Data to TMS/CS Pin (no Read)" */
557 /* number of states remaining */
558 BUFFER_ADD = num_states_batch - 1;
560 while (num_states_batch--)
562 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
563 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
564 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
565 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
568 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
569 tap_get_state() ), tap_state_name(cmd->path[state_count]) );
573 tap_set_state(cmd->path[state_count]);
578 BUFFER_ADD = tms_byte;
581 tap_set_end_state(tap_get_state());
585 void ft2232_add_scan(int ir_scan, enum scan_type type, u8* buffer, int scan_size)
587 int num_bytes = (scan_size + 7) / 8;
588 int bits_left = scan_size;
592 if ( !( ( !ir_scan && (tap_get_state() == TAP_DRSHIFT) )
593 || ( ir_scan && (tap_get_state() == TAP_IRSHIFT) ) ) )
595 /* command "Clock Data to TMS/CS Pin (no Read)" */
598 BUFFER_ADD = 0x6; /* scan 7 bits */
603 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IRSHIFT);
604 tap_set_state(TAP_IRSHIFT);
608 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
609 tap_set_state(TAP_DRSHIFT);
611 /* LOG_DEBUG("added TMS scan (no read)"); */
614 /* add command for complete bytes */
615 while (num_bytes > 1)
620 /* Clock Data Bytes In and Out LSB First */
622 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
624 else if (type == SCAN_OUT)
626 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
628 /* LOG_DEBUG("added TDI bytes (o)"); */
630 else if (type == SCAN_IN)
632 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
634 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
637 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
638 num_bytes -= thisrun_bytes;
639 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
640 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
644 /* add complete bytes */
645 while (thisrun_bytes-- > 0)
647 BUFFER_ADD = buffer[cur_byte];
652 else /* (type == SCAN_IN) */
654 bits_left -= 8 * (thisrun_bytes);
658 /* the most signifcant bit is scanned during TAP movement */
660 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
664 /* process remaining bits but the last one */
669 /* Clock Data Bits In and Out LSB First */
671 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
673 else if (type == SCAN_OUT)
675 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
677 /* LOG_DEBUG("added TDI bits (o)"); */
679 else if (type == SCAN_IN)
681 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
683 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
685 BUFFER_ADD = bits_left - 2;
687 BUFFER_ADD = buffer[cur_byte];
690 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
691 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
695 /* Clock Data Bits In and Out LSB First */
697 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
699 else if (type == SCAN_OUT)
701 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
703 /* LOG_DEBUG("added TDI bits (o)"); */
705 else if (type == SCAN_IN)
707 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
709 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
712 BUFFER_ADD = last_bit;
716 /* move from Shift-IR/DR to end state */
717 if (type != SCAN_OUT)
719 /* Clock Data to TMS/CS Pin with Read */
721 /* LOG_DEBUG("added TMS scan (read)"); */
725 /* Clock Data to TMS/CS Pin (no Read) */
727 /* LOG_DEBUG("added TMS scan (no read)"); */
729 BUFFER_ADD = 0x6; /* scan 7 bits */
731 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
732 tap_set_state( tap_get_end_state() );
737 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
739 int num_bytes = (scan_size + 7) / 8;
740 int bits_left = scan_size;
743 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
744 u8* receive_pointer = receive_buffer;
748 int thisrun_read = 0;
752 LOG_ERROR("BUG: large IR scans are not supported");
756 if (tap_get_state() != TAP_DRSHIFT)
758 /* command "Clock Data to TMS/CS Pin (no Read)" */
761 BUFFER_ADD = 0x6; /* scan 7 bits */
764 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
765 tap_set_state(TAP_DRSHIFT);
768 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
770 LOG_ERROR("couldn't write MPSSE commands to FT2232");
773 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
774 ft2232_buffer_size = 0;
776 /* add command for complete bytes */
777 while (num_bytes > 1)
783 /* Clock Data Bytes In and Out LSB First */
785 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
787 else if (type == SCAN_OUT)
789 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
791 /* LOG_DEBUG("added TDI bytes (o)"); */
793 else if (type == SCAN_IN)
795 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
797 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
800 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
801 thisrun_read = thisrun_bytes;
802 num_bytes -= thisrun_bytes;
803 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
804 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
808 /* add complete bytes */
809 while (thisrun_bytes-- > 0)
811 BUFFER_ADD = buffer[cur_byte];
816 else /* (type == SCAN_IN) */
818 bits_left -= 8 * (thisrun_bytes);
821 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
823 LOG_ERROR("couldn't write MPSSE commands to FT2232");
826 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
827 ft2232_buffer_size = 0;
829 if (type != SCAN_OUT)
831 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
833 LOG_ERROR("couldn't read from FT2232");
836 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
837 receive_pointer += bytes_read;
843 /* the most signifcant bit is scanned during TAP movement */
845 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
849 /* process remaining bits but the last one */
854 /* Clock Data Bits In and Out LSB First */
856 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
858 else if (type == SCAN_OUT)
860 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
862 /* LOG_DEBUG("added TDI bits (o)"); */
864 else if (type == SCAN_IN)
866 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
868 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
870 BUFFER_ADD = bits_left - 2;
872 BUFFER_ADD = buffer[cur_byte];
874 if (type != SCAN_OUT)
878 if (tap_get_end_state() == TAP_DRSHIFT)
882 /* Clock Data Bits In and Out LSB First */
884 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
886 else if (type == SCAN_OUT)
888 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
890 /* LOG_DEBUG("added TDI bits (o)"); */
892 else if (type == SCAN_IN)
894 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
896 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
899 BUFFER_ADD = last_bit;
903 /* move from Shift-IR/DR to end state */
904 if (type != SCAN_OUT)
906 /* Clock Data to TMS/CS Pin with Read */
908 /* LOG_DEBUG("added TMS scan (read)"); */
912 /* Clock Data to TMS/CS Pin (no Read) */
914 /* LOG_DEBUG("added TMS scan (no read)"); */
917 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
918 tap_set_state( tap_get_end_state() );
921 if (type != SCAN_OUT)
924 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
926 LOG_ERROR("couldn't write MPSSE commands to FT2232");
929 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
930 ft2232_buffer_size = 0;
932 if (type != SCAN_OUT)
934 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
936 LOG_ERROR("couldn't read from FT2232");
939 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
940 receive_pointer += bytes_read;
947 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
949 int predicted_size = 3;
950 int num_bytes = (scan_size - 1) / 8;
952 if (tap_get_state() != TAP_DRSHIFT)
955 if (type == SCAN_IN) /* only from device to host */
958 predicted_size += CEIL(num_bytes, 65536) * 3;
959 /* remaining bits - 1 (up to 7) */
960 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
962 else /* host to device, or bidirectional */
965 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
966 /* remaining bits -1 (up to 7) */
967 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
970 return predicted_size;
974 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
976 int predicted_size = 0;
978 if (type != SCAN_OUT)
981 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
983 /* remaining bits - 1 */
984 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
986 /* last bit (from TMS scan) */
990 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
992 return predicted_size;
996 static void usbjtag_reset(int trst, int srst)
1000 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1001 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1003 low_output &= ~nTRST; /* switch output low */
1007 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1008 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1010 low_output |= nTRST; /* switch output high */
1015 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1016 low_output &= ~nSRST; /* switch output low */
1018 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1022 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1023 low_output |= nSRST; /* switch output high */
1025 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1028 /* command "set data bits low byte" */
1030 BUFFER_ADD = low_output;
1031 BUFFER_ADD = low_direction;
1035 static void jtagkey_reset(int trst, int srst)
1039 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1040 high_output &= ~nTRSTnOE;
1042 high_output &= ~nTRST;
1046 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1047 high_output |= nTRSTnOE;
1049 high_output |= nTRST;
1054 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1055 high_output &= ~nSRST;
1057 high_output &= ~nSRSTnOE;
1061 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1062 high_output |= nSRST;
1064 high_output |= nSRSTnOE;
1067 /* command "set data bits high byte" */
1069 BUFFER_ADD = high_output;
1070 BUFFER_ADD = high_direction;
1071 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1076 static void olimex_jtag_reset(int trst, int srst)
1080 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1081 high_output &= ~nTRSTnOE;
1083 high_output &= ~nTRST;
1087 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1088 high_output |= nTRSTnOE;
1090 high_output |= nTRST;
1095 high_output |= nSRST;
1099 high_output &= ~nSRST;
1102 /* command "set data bits high byte" */
1104 BUFFER_ADD = high_output;
1105 BUFFER_ADD = high_direction;
1106 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1111 static void axm0432_jtag_reset(int trst, int srst)
1115 tap_set_state(TAP_RESET);
1116 high_output &= ~nTRST;
1120 high_output |= nTRST;
1125 high_output &= ~nSRST;
1129 high_output |= nSRST;
1132 /* command "set data bits low byte" */
1134 BUFFER_ADD = high_output;
1135 BUFFER_ADD = high_direction;
1136 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1141 static void flyswatter_reset(int trst, int srst)
1145 low_output &= ~nTRST;
1149 low_output |= nTRST;
1154 low_output |= nSRST;
1158 low_output &= ~nSRST;
1161 /* command "set data bits low byte" */
1163 BUFFER_ADD = low_output;
1164 BUFFER_ADD = low_direction;
1165 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1169 static void turtle_reset(int trst, int srst)
1175 low_output |= nSRST;
1179 low_output &= ~nSRST;
1182 /* command "set data bits low byte" */
1184 BUFFER_ADD = low_output;
1185 BUFFER_ADD = low_direction;
1186 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1190 static void comstick_reset(int trst, int srst)
1194 high_output &= ~nTRST;
1198 high_output |= nTRST;
1203 high_output &= ~nSRST;
1207 high_output |= nSRST;
1210 /* command "set data bits high byte" */
1212 BUFFER_ADD = high_output;
1213 BUFFER_ADD = high_direction;
1214 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1219 static void stm32stick_reset(int trst, int srst)
1223 high_output &= ~nTRST;
1227 high_output |= nTRST;
1232 low_output &= ~nSRST;
1236 low_output |= nSRST;
1239 /* command "set data bits low byte" */
1241 BUFFER_ADD = low_output;
1242 BUFFER_ADD = low_direction;
1244 /* command "set data bits high byte" */
1246 BUFFER_ADD = high_output;
1247 BUFFER_ADD = high_direction;
1248 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1254 static void sheevaplug_reset(int trst, int srst)
1257 high_output &= ~nTRST;
1259 high_output |= nTRST;
1262 high_output &= ~nSRSTnOE;
1264 high_output |= nSRSTnOE;
1266 /* command "set data bits high byte" */
1268 BUFFER_ADD = high_output;
1269 BUFFER_ADD = high_direction;
1270 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1273 static int ft2232_execute_end_state(jtag_command_t *cmd)
1278 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
1280 if (cmd->cmd.end_state->end_state != TAP_INVALID)
1281 ft2232_end_state(cmd->cmd.end_state->end_state);
1287 static int ft2232_execute_runtest(jtag_command_t *cmd)
1291 int predicted_size = 0;
1294 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
1295 cmd->cmd.runtest->num_cycles,
1296 cmd->cmd.runtest->end_state);
1297 /* only send the maximum buffer size that FT2232C can handle */
1299 if (tap_get_state() != TAP_IDLE)
1300 predicted_size += 3;
1301 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1302 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1303 predicted_size += 3;
1304 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1305 predicted_size += 3;
1306 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1308 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1309 retval = ERROR_JTAG_QUEUE_FAILED;
1313 if (tap_get_state() != TAP_IDLE)
1315 /* command "Clock Data to TMS/CS Pin (no Read)" */
1317 BUFFER_ADD = 0x6; /* scan 7 bits */
1320 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IDLE);
1321 tap_set_state(TAP_IDLE);
1324 i = cmd->cmd.runtest->num_cycles;
1327 /* command "Clock Data to TMS/CS Pin (no Read)" */
1331 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1335 tap_set_state(TAP_IDLE);
1336 i -= (i > 7) ? 7 : i;
1337 /* LOG_DEBUG("added TMS scan (no read)"); */
1340 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1341 ft2232_end_state(cmd->cmd.runtest->end_state);
1343 if ( tap_get_state() != tap_get_end_state() )
1345 /* command "Clock Data to TMS/CS Pin (no Read)" */
1350 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1351 tap_set_state( tap_get_end_state() );
1352 /* LOG_DEBUG("added TMS scan (no read)"); */
1355 #ifdef _DEBUG_JTAG_IO_
1356 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1362 static int ft2232_execute_statemove(jtag_command_t *cmd)
1365 int predicted_size = 0;
1368 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1370 /* only send the maximum buffer size that FT2232C can handle */
1372 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1374 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1375 retval = ERROR_JTAG_QUEUE_FAILED;
1379 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1380 ft2232_end_state(cmd->cmd.statemove->end_state);
1382 /* command "Clock Data to TMS/CS Pin (no Read)" */
1385 BUFFER_ADD = 0x6; /* scan 7 bits */
1388 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1389 /* LOG_DEBUG("added TMS scan (no read)"); */
1390 tap_set_state( tap_get_end_state() );
1392 #ifdef _DEBUG_JTAG_IO_
1393 LOG_DEBUG( "statemove: %s", tap_state_name( tap_get_end_state() ) );
1399 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1402 int predicted_size = 0;
1405 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
1406 cmd->cmd.pathmove->num_states,
1407 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1408 /* only send the maximum buffer size that FT2232C can handle */
1409 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1410 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1412 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1413 retval = ERROR_JTAG_QUEUE_FAILED;
1417 ft2232_add_pathmove(cmd->cmd.pathmove);
1419 #ifdef _DEBUG_JTAG_IO_
1420 LOG_DEBUG( "pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1421 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]) );
1426 static int ft2232_execute_scan(jtag_command_t *cmd)
1430 int scan_size; /* size of IR or DR scan */
1431 enum scan_type type;
1432 int predicted_size = 0;
1435 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1436 type = jtag_scan_type(cmd->cmd.scan);
1437 predicted_size = ft2232_predict_scan_out(scan_size, type);
1438 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1440 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1441 /* unsent commands before this */
1442 if (first_unsent != cmd)
1443 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1444 retval = ERROR_JTAG_QUEUE_FAILED;
1446 /* current command */
1447 if (cmd->cmd.scan->end_state != TAP_INVALID)
1448 ft2232_end_state(cmd->cmd.scan->end_state);
1449 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1451 first_unsent = cmd->next;
1456 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1458 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1461 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1462 retval = ERROR_JTAG_QUEUE_FAILED;
1466 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1467 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1468 if (cmd->cmd.scan->end_state != TAP_INVALID)
1469 ft2232_end_state(cmd->cmd.scan->end_state);
1470 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1474 #ifdef _DEBUG_JTAG_IO_
1475 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1476 tap_state_name( tap_get_end_state() ) );
1482 static int ft2232_execute_reset(jtag_command_t *cmd)
1485 int predicted_size = 0;
1488 DEBUG_JTAG_IO("reset trst: %i srst %i",
1489 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1491 /* only send the maximum buffer size that FT2232C can handle */
1493 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1495 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1496 retval = ERROR_JTAG_QUEUE_FAILED;
1501 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1503 tap_set_state(TAP_RESET);
1505 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1508 #ifdef _DEBUG_JTAG_IO_
1509 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1514 static int ft2232_execute_sleep(jtag_command_t *cmd)
1519 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1521 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1522 retval = ERROR_JTAG_QUEUE_FAILED;
1523 first_unsent = cmd->next;
1524 jtag_sleep(cmd->cmd.sleep->us);
1525 #ifdef _DEBUG_JTAG_IO_
1526 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1532 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1537 /* this is only allowed while in a stable state. A check for a stable
1538 * state was done in jtag_add_clocks()
1540 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1541 retval = ERROR_JTAG_QUEUE_FAILED;
1542 #ifdef _DEBUG_JTAG_IO_
1543 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1549 static int ft2232_execute_command(jtag_command_t *cmd)
1556 case JTAG_END_STATE: retval = ft2232_execute_end_state(cmd); break;
1557 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1558 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1559 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1560 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1561 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1562 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1563 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1565 LOG_ERROR("BUG: unknown JTAG command type encountered");
1571 static int ft2232_execute_queue()
1573 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1576 first_unsent = cmd; /* next command that has to be sent */
1579 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1580 * that wasn't handled by a caller-provided error handler
1584 ft2232_buffer_size = 0;
1585 ft2232_expect_read = 0;
1587 /* blink, if the current layout has that feature */
1593 if (ft2232_execute_command(cmd) != ERROR_OK)
1594 retval = ERROR_JTAG_QUEUE_FAILED;
1595 /* Start reading input before FT2232 TX buffer fills up */
1597 if (ft2232_expect_read > 256)
1599 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1600 retval = ERROR_JTAG_QUEUE_FAILED;
1605 if (require_send > 0)
1606 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1607 retval = ERROR_JTAG_QUEUE_FAILED;
1613 #if BUILD_FT2232_FTD2XX == 1
1614 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1617 DWORD openex_flags = 0;
1618 char* openex_string = NULL;
1621 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1624 /* Add non-standard Vid/Pid to the linux driver */
1625 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1627 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1631 if (ft2232_device_desc && ft2232_serial)
1633 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1634 ft2232_device_desc = NULL;
1637 if (ft2232_device_desc)
1639 openex_string = ft2232_device_desc;
1640 openex_flags = FT_OPEN_BY_DESCRIPTION;
1642 else if (ft2232_serial)
1644 openex_string = ft2232_serial;
1645 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1649 LOG_ERROR("neither device description nor serial number specified");
1650 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1652 return ERROR_JTAG_INIT_FAILED;
1655 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1656 if( status != FT_OK ){
1657 // under Win32, the FTD2XX driver appends an "A" to the end
1658 // of the description, if we tried by the desc, then
1659 // try by the alternate "A" description.
1660 if( openex_string == ft2232_device_desc ){
1661 // Try the alternate method.
1662 openex_string = ft2232_device_desc_A;
1663 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1664 if( status == FT_OK ){
1665 // yea, the "alternate" method worked!
1667 // drat, give the user a meaningfull message.
1668 // telling the use we tried *BOTH* methods.
1669 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1671 ft2232_device_desc_A );
1676 if ( status != FT_OK )
1682 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1684 return ERROR_JTAG_INIT_FAILED;
1686 LOG_ERROR("unable to open ftdi device: %lu", status);
1687 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1688 if (status == FT_OK)
1690 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1693 for (i = 0; i < num_devices; i++)
1694 desc_array[i] = malloc(64);
1696 desc_array[num_devices] = NULL;
1698 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1700 if (status == FT_OK)
1702 LOG_ERROR("ListDevices: %lu\n", num_devices);
1703 for (i = 0; i < num_devices; i++)
1704 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1707 for (i = 0; i < num_devices; i++)
1708 free(desc_array[i]);
1714 LOG_ERROR("ListDevices: NONE\n");
1716 return ERROR_JTAG_INIT_FAILED;
1719 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1721 LOG_ERROR("unable to set latency timer: %lu", status);
1722 return ERROR_JTAG_INIT_FAILED;
1725 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1727 LOG_ERROR("unable to get latency timer: %lu", status);
1728 return ERROR_JTAG_INIT_FAILED;
1732 LOG_DEBUG("current latency timer: %i", latency_timer);
1735 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1737 LOG_ERROR("unable to set timeouts: %lu", status);
1738 return ERROR_JTAG_INIT_FAILED;
1741 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1743 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1744 return ERROR_JTAG_INIT_FAILED;
1751 static int ft2232_purge_ftd2xx(void)
1755 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1757 LOG_ERROR("error purging ftd2xx device: %lu", status);
1758 return ERROR_JTAG_INIT_FAILED;
1765 #endif /* BUILD_FT2232_FTD2XX == 1 */
1767 #if BUILD_FT2232_LIBFTDI == 1
1768 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1772 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1773 ft2232_layout, vid, pid);
1775 if (ftdi_init(&ftdic) < 0)
1776 return ERROR_JTAG_INIT_FAILED;
1778 /* context, vendor id, product id */
1779 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1783 LOG_WARNING("unable to open ftdi device (trying more): %s",
1786 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1788 return ERROR_JTAG_INIT_FAILED;
1791 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1793 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1794 return ERROR_JTAG_INIT_FAILED;
1797 if (ftdi_usb_reset(&ftdic) < 0)
1799 LOG_ERROR("unable to reset ftdi device");
1800 return ERROR_JTAG_INIT_FAILED;
1803 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1805 LOG_ERROR("unable to set latency timer");
1806 return ERROR_JTAG_INIT_FAILED;
1809 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1811 LOG_ERROR("unable to get latency timer");
1812 return ERROR_JTAG_INIT_FAILED;
1816 LOG_DEBUG("current latency timer: %i", latency_timer);
1819 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1825 static int ft2232_purge_libftdi(void)
1827 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1829 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1830 return ERROR_JTAG_INIT_FAILED;
1837 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1839 static int ft2232_init(void)
1844 ft2232_layout_t* cur_layout = ft2232_layouts;
1847 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1849 ft2232_layout = "usbjtag";
1850 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1853 while (cur_layout->name)
1855 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1857 layout = cur_layout;
1865 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1866 return ERROR_JTAG_INIT_FAILED;
1872 * "more indicates that there are more IDs to try, so we should
1873 * not print an error for an ID mismatch (but for anything
1876 * try_more indicates that the error code returned indicates an
1877 * ID mismatch (and nothing else) and that we should proceeed
1878 * with the next ID pair.
1880 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
1883 #if BUILD_FT2232_FTD2XX == 1
1884 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1886 #elif BUILD_FT2232_LIBFTDI == 1
1887 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1892 if (!more || !try_more)
1896 ft2232_buffer_size = 0;
1897 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1899 if (layout->init() != ERROR_OK)
1900 return ERROR_JTAG_INIT_FAILED;
1902 ft2232_speed(jtag_speed);
1904 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1905 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
1907 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1908 return ERROR_JTAG_INIT_FAILED;
1911 #if BUILD_FT2232_FTD2XX == 1
1912 return ft2232_purge_ftd2xx();
1913 #elif BUILD_FT2232_LIBFTDI == 1
1914 return ft2232_purge_libftdi();
1921 static int usbjtag_init(void)
1927 low_direction = 0x0b;
1929 if (strcmp(ft2232_layout, "usbjtag") == 0)
1936 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1943 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1950 low_direction = 0x8b;
1954 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1955 return ERROR_JTAG_INIT_FAILED;
1958 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1960 low_direction &= ~nTRSTnOE; /* nTRST input */
1961 low_output &= ~nTRST; /* nTRST = 0 */
1965 low_direction |= nTRSTnOE; /* nTRST output */
1966 low_output |= nTRST; /* nTRST = 1 */
1969 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1971 low_direction |= nSRSTnOE; /* nSRST output */
1972 low_output |= nSRST; /* nSRST = 1 */
1976 low_direction &= ~nSRSTnOE; /* nSRST input */
1977 low_output &= ~nSRST; /* nSRST = 0 */
1980 /* initialize low byte for jtag */
1981 buf[0] = 0x80; /* command "set data bits low byte" */
1982 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1983 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1984 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1986 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1988 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1989 return ERROR_JTAG_INIT_FAILED;
1996 static int axm0432_jtag_init(void)
2002 low_direction = 0x2b;
2004 /* initialize low byte for jtag */
2005 buf[0] = 0x80; /* command "set data bits low byte" */
2006 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2007 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2008 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2010 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2012 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2013 return ERROR_JTAG_INIT_FAILED;
2016 if (strcmp(layout->name, "axm0432_jtag") == 0)
2019 nTRSTnOE = 0x0; /* No output enable for TRST*/
2021 nSRSTnOE = 0x0; /* No output enable for SRST*/
2025 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2030 high_direction = 0x0c;
2032 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2034 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2038 high_output |= nTRST;
2041 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2043 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2047 high_output |= nSRST;
2050 /* initialize high port */
2051 buf[0] = 0x82; /* command "set data bits high byte" */
2052 buf[1] = high_output; /* value */
2053 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2054 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2056 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2058 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2059 return ERROR_JTAG_INIT_FAILED;
2066 static int jtagkey_init(void)
2072 low_direction = 0x1b;
2074 /* initialize low byte for jtag */
2075 buf[0] = 0x80; /* command "set data bits low byte" */
2076 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2077 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2078 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2080 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2082 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2083 return ERROR_JTAG_INIT_FAILED;
2086 if (strcmp(layout->name, "jtagkey") == 0)
2093 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2094 || (strcmp(layout->name, "oocdlink") == 0) )
2103 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2108 high_direction = 0x0f;
2110 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2112 high_output |= nTRSTnOE;
2113 high_output &= ~nTRST;
2117 high_output &= ~nTRSTnOE;
2118 high_output |= nTRST;
2121 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2123 high_output &= ~nSRSTnOE;
2124 high_output |= nSRST;
2128 high_output |= nSRSTnOE;
2129 high_output &= ~nSRST;
2132 /* initialize high port */
2133 buf[0] = 0x82; /* command "set data bits high byte" */
2134 buf[1] = high_output; /* value */
2135 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2136 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2138 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2140 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2141 return ERROR_JTAG_INIT_FAILED;
2148 static int olimex_jtag_init(void)
2154 low_direction = 0x1b;
2156 /* initialize low byte for jtag */
2157 buf[0] = 0x80; /* command "set data bits low byte" */
2158 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2159 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2160 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2162 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2164 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2165 return ERROR_JTAG_INIT_FAILED;
2171 nSRSTnOE = 0x00; /* no output enable for nSRST */
2174 high_direction = 0x0f;
2176 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2178 high_output |= nTRSTnOE;
2179 high_output &= ~nTRST;
2183 high_output &= ~nTRSTnOE;
2184 high_output |= nTRST;
2187 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2189 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2193 high_output &= ~nSRST;
2196 /* turn red LED on */
2197 high_output |= 0x08;
2199 /* initialize high port */
2200 buf[0] = 0x82; /* command "set data bits high byte" */
2201 buf[1] = high_output; /* value */
2202 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2203 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2205 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2207 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2208 return ERROR_JTAG_INIT_FAILED;
2215 static int flyswatter_init(void)
2221 low_direction = 0xfb;
2223 /* initialize low byte for jtag */
2224 buf[0] = 0x80; /* command "set data bits low byte" */
2225 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2226 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2227 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2229 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2231 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2232 return ERROR_JTAG_INIT_FAILED;
2236 nTRSTnOE = 0x0; /* not output enable for nTRST */
2238 nSRSTnOE = 0x00; /* no output enable for nSRST */
2241 high_direction = 0x0c;
2243 /* turn red LED3 on, LED2 off */
2244 high_output |= 0x08;
2246 /* initialize high port */
2247 buf[0] = 0x82; /* command "set data bits high byte" */
2248 buf[1] = high_output; /* value */
2249 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
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 'flyswatter' layout");
2255 return ERROR_JTAG_INIT_FAILED;
2262 static int turtle_init(void)
2268 low_direction = 0x5b;
2270 /* initialize low byte for jtag */
2271 buf[0] = 0x80; /* command "set data bits low byte" */
2272 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2273 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2274 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2276 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2278 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2279 return ERROR_JTAG_INIT_FAILED;
2285 high_direction = 0x0C;
2287 /* initialize high port */
2288 buf[0] = 0x82; /* command "set data bits high byte" */
2289 buf[1] = high_output;
2290 buf[2] = high_direction;
2291 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2293 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2295 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2296 return ERROR_JTAG_INIT_FAILED;
2303 static int comstick_init(void)
2309 low_direction = 0x0b;
2311 /* initialize low byte for jtag */
2312 buf[0] = 0x80; /* command "set data bits low byte" */
2313 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2314 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2315 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2317 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2319 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2320 return ERROR_JTAG_INIT_FAILED;
2324 nTRSTnOE = 0x00; /* no output enable for nTRST */
2326 nSRSTnOE = 0x00; /* no output enable for nSRST */
2329 high_direction = 0x03;
2331 /* initialize high port */
2332 buf[0] = 0x82; /* command "set data bits high byte" */
2333 buf[1] = high_output;
2334 buf[2] = high_direction;
2335 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2337 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2339 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2340 return ERROR_JTAG_INIT_FAILED;
2347 static int stm32stick_init(void)
2353 low_direction = 0x8b;
2355 /* initialize low byte for jtag */
2356 buf[0] = 0x80; /* command "set data bits low byte" */
2357 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2358 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2359 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2361 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2363 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2364 return ERROR_JTAG_INIT_FAILED;
2368 nTRSTnOE = 0x00; /* no output enable for nTRST */
2370 nSRSTnOE = 0x00; /* no output enable for nSRST */
2373 high_direction = 0x03;
2375 /* initialize high port */
2376 buf[0] = 0x82; /* command "set data bits high byte" */
2377 buf[1] = high_output;
2378 buf[2] = high_direction;
2379 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2381 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2383 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2384 return ERROR_JTAG_INIT_FAILED;
2391 static int sheevaplug_init(void)
2397 low_direction = 0x1b;
2399 /* initialize low byte for jtag */
2400 buf[0] = 0x80; /* command "set data bits low byte" */
2401 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2402 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2403 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2405 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2407 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2408 return ERROR_JTAG_INIT_FAILED;
2417 high_direction = 0x0f;
2419 /* nTRST is always push-pull */
2420 high_output &= ~nTRSTnOE;
2421 high_output |= nTRST;
2423 /* nSRST is always open-drain */
2424 high_output |= nSRSTnOE;
2425 high_output &= ~nSRST;
2427 /* initialize high port */
2428 buf[0] = 0x82; /* command "set data bits high byte" */
2429 buf[1] = high_output; /* value */
2430 buf[2] = high_direction; /* all outputs - xRST */
2431 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2433 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2435 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2436 return ERROR_JTAG_INIT_FAILED;
2442 static void olimex_jtag_blink(void)
2444 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2445 * ACBUS3 is bit 3 of the GPIOH port
2447 if (high_output & 0x08)
2449 /* set port pin high */
2450 high_output &= 0x07;
2454 /* set port pin low */
2455 high_output |= 0x08;
2459 BUFFER_ADD = high_output;
2460 BUFFER_ADD = high_direction;
2464 static void flyswatter_jtag_blink(void)
2467 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2469 high_output ^= 0x0c;
2472 BUFFER_ADD = high_output;
2473 BUFFER_ADD = high_direction;
2477 static void turtle_jtag_blink(void)
2480 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2482 if (high_output & 0x08)
2492 BUFFER_ADD = high_output;
2493 BUFFER_ADD = high_direction;
2497 static int ft2232_quit(void)
2499 #if BUILD_FT2232_FTD2XX == 1
2502 status = FT_Close(ftdih);
2503 #elif BUILD_FT2232_LIBFTDI == 1
2504 ftdi_disable_bitbang(&ftdic);
2506 ftdi_usb_close(&ftdic);
2508 ftdi_deinit(&ftdic);
2511 free(ft2232_buffer);
2512 ft2232_buffer = NULL;
2518 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2524 ft2232_device_desc = strdup(args[0]);
2525 cp = strchr( ft2232_device_desc, 0 );
2526 // under Win32, the FTD2XX driver appends an "A" to the end
2527 // of the description, this examines the given desc
2528 // and creates the 'missing' _A or non_A variable.
2529 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2530 // it was, so make this the "A" version.
2531 ft2232_device_desc_A = ft2232_device_desc;
2532 // and *CREATE* the non-A version.
2533 strcpy( buf, ft2232_device_desc );
2534 cp = strchr( buf, 0 );
2536 ft2232_device_desc = strdup( buf );
2538 // <space>A not defined
2540 sprintf( buf, "%s A", ft2232_device_desc );
2541 ft2232_device_desc_A = strdup( buf );
2546 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2553 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2557 ft2232_serial = strdup(args[0]);
2561 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2568 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2573 ft2232_layout = malloc(strlen(args[0]) + 1);
2574 strcpy(ft2232_layout, args[0]);
2580 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2584 if (argc > MAX_USB_IDS * 2)
2586 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2587 "(maximum is %d pairs)", MAX_USB_IDS);
2588 argc = MAX_USB_IDS * 2;
2590 if ( argc < 2 || (argc & 1) )
2592 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2597 for (i = 0; i + 1 < argc; i += 2)
2599 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2600 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2604 * Explicitly terminate, in case there are multiples instances of
2607 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2613 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2617 ft2232_latency = atoi(args[0]);
2621 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2628 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2632 /* 7 bits of either ones or zeros. */
2633 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2635 while (num_cycles > 0)
2637 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2638 * at most 7 bits per invocation. Here we invoke it potentially
2641 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2643 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2645 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2646 retval = ERROR_JTAG_QUEUE_FAILED;
2651 /* command "Clock Data to TMS/CS Pin (no Read)" */
2655 BUFFER_ADD = bitcount_per_command - 1;
2657 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2662 num_cycles -= bitcount_per_command;
2668 /* ---------------------------------------------------------------------
2669 * Support for IceBear JTAG adapter from Section5:
2670 * http://section5.ch/icebear
2672 * Author: Sten, debian@sansys-electronic.com
2675 /* Icebear pin layout
2677 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2678 * GND GND | 4 3| n.c.
2679 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2680 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2681 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2682 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2683 * ADBUS2 TDO |14 13| GND GND
2685 * ADBUS0 O L TCK ACBUS0 GND
2686 * ADBUS1 O L TDI ACBUS1 GND
2687 * ADBUS2 I TDO ACBUS2 n.c.
2688 * ADBUS3 O H TMS ACBUS3 n.c.
2694 static int icebear_jtag_init(void) {
2698 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2699 low_output = 0x08; /* high: TMS; low: TCK TDI */
2703 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2704 low_direction &= ~nTRST; /* nTRST high impedance */
2707 low_direction |= nTRST;
2708 low_output |= nTRST;
2711 low_direction |= nSRST;
2712 low_output |= nSRST;
2714 /* initialize low byte for jtag */
2715 buf[0] = 0x80; /* command "set data bits low byte" */
2716 buf[1] = low_output;
2717 buf[2] = low_direction;
2718 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2720 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2721 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2722 return ERROR_JTAG_INIT_FAILED;
2726 high_direction = 0x00;
2729 /* initialize high port */
2730 buf[0] = 0x82; /* command "set data bits high byte" */
2731 buf[1] = high_output; /* value */
2732 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2733 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2735 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2736 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2737 return ERROR_JTAG_INIT_FAILED;
2743 static void icebear_jtag_reset(int trst, int srst) {
2746 low_direction |= nTRST;
2747 low_output &= ~nTRST;
2749 else if (trst == 0) {
2750 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
2751 low_direction &= ~nTRST;
2753 low_output |= nTRST;
2757 low_output &= ~nSRST;
2759 else if (srst == 0) {
2760 low_output |= nSRST;
2763 /* command "set data bits low byte" */
2765 BUFFER_ADD = low_output;
2766 BUFFER_ADD = low_direction;
2767 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);