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 ***************************************************************************/
31 #include "replacements.h"
33 /* project specific includes */
37 #include "configuration.h"
38 #include "time_support.h"
45 /* FT2232 access library includes */
46 #if BUILD_FT2232_FTD2XX == 1
48 #elif BUILD_FT2232_LIBFTDI == 1
52 /* enable this to debug io latency
55 #define _DEBUG_USB_IO_
58 /* enable this to debug communication
61 #define _DEBUG_USB_COMMS_
64 int ft2232_execute_queue(void);
66 int ft2232_speed(int speed);
67 int ft2232_speed_div(int speed, int *khz);
68 int ft2232_khz(int khz, int *jtag_speed);
69 int ft2232_register_commands(struct command_context_s *cmd_ctx);
70 int ft2232_init(void);
71 int ft2232_quit(void);
73 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
81 * Function ft2232_stableclocks
82 * will send out \a num_cycles on the TCK line while the TAP(s)
83 * are in a stable state. Calling code must ensure that current state is
84 * stable, that verification is not done in here.
85 * @param num_cycles is the count of clocks cycles to send.
86 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
88 static int ft2232_stableclocks(int num_cycles, jtag_command_t *cmd);
91 char *ft2232_device_desc = NULL;
92 char *ft2232_serial = NULL;
93 char *ft2232_layout = NULL;
94 unsigned char ft2232_latency = 2;
97 /* vid = pid = 0 marks the end of the list */
98 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
99 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
101 typedef struct ft2232_layout_s
105 void(*reset)(int trst, int srst);
109 /* init procedures for supported layouts */
110 int usbjtag_init(void);
111 int jtagkey_init(void);
112 int olimex_jtag_init(void);
113 int flyswatter_init(void);
114 int turtle_init(void);
115 int comstick_init(void);
116 int stm32stick_init(void);
117 int axm0432_jtag_init(void);
119 /* reset procedures for supported layouts */
120 void usbjtag_reset(int trst, int srst);
121 void jtagkey_reset(int trst, int srst);
122 void olimex_jtag_reset(int trst, int srst);
123 void flyswatter_reset(int trst, int srst);
124 void turtle_reset(int trst, int srst);
125 void comstick_reset(int trst, int srst);
126 void stm32stick_reset(int trst, int srst);
127 void axm0432_jtag_reset(int trst, int srst);
129 /* blink procedures for layouts that support a blinking led */
130 void olimex_jtag_blink(void);
131 void turtle_jtag_blink(void);
133 ft2232_layout_t ft2232_layouts[] =
135 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
136 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
137 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
138 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
139 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
140 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
141 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
142 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
143 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
144 {"comstick", comstick_init, comstick_reset, NULL},
145 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
146 {"axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL},
150 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
152 static ft2232_layout_t *layout;
153 static u8 low_output = 0x0;
154 static u8 low_direction = 0x0;
155 static u8 high_output = 0x0;
156 static u8 high_direction = 0x0;
158 #if BUILD_FT2232_FTD2XX == 1
159 static FT_HANDLE ftdih = NULL;
160 #elif BUILD_FT2232_LIBFTDI == 1
161 static struct ftdi_context ftdic;
165 static jtag_command_t *first_unsent; /* next command that has to be sent */
166 static int require_send;
168 static u8 *ft2232_buffer = NULL;
169 static int ft2232_buffer_size = 0;
170 static int ft2232_read_pointer = 0;
171 static int ft2232_expect_read = 0;
173 #define FT2232_BUFFER_SIZE 131072
174 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
175 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
177 jtag_interface_t ft2232_interface =
180 .execute_queue = ft2232_execute_queue,
181 .speed = ft2232_speed,
182 .speed_div = ft2232_speed_div,
184 .register_commands = ft2232_register_commands,
189 int ft2232_write(u8 *buf, int size, u32* bytes_written)
191 #if BUILD_FT2232_FTD2XX == 1
193 DWORD dw_bytes_written;
194 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
196 *bytes_written = dw_bytes_written;
197 LOG_ERROR("FT_Write returned: %lu", status);
198 return ERROR_JTAG_DEVICE_ERROR;
202 *bytes_written = dw_bytes_written;
205 #elif BUILD_FT2232_LIBFTDI == 1
207 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
210 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
211 return ERROR_JTAG_DEVICE_ERROR;
215 *bytes_written = retval;
221 int ft2232_read(u8* buf, int size, u32* bytes_read)
223 #if BUILD_FT2232_FTD2XX == 1
229 while ((*bytes_read < size) && timeout--)
231 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
232 *bytes_read, &dw_bytes_read)) != FT_OK)
235 LOG_ERROR("FT_Read returned: %lu", status);
236 return ERROR_JTAG_DEVICE_ERROR;
238 *bytes_read += dw_bytes_read;
240 #elif BUILD_FT2232_LIBFTDI == 1
245 while ((*bytes_read < size) && timeout--)
247 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
250 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
251 return ERROR_JTAG_DEVICE_ERROR;
253 *bytes_read += retval;
257 if (*bytes_read < size)
259 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
260 return ERROR_JTAG_DEVICE_ERROR;
266 int ft2232_speed(int speed)
272 buf[0] = 0x86; /* command "set divisor" */
273 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
274 buf[2] = (speed >> 8) & 0xff; /* valueH */
276 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
277 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
279 LOG_ERROR("couldn't set FT2232 TCK speed");
286 int ft2232_speed_div(int speed, int *khz)
288 /* Take a look in the FT2232 manual,
289 * AN2232C-01 Command Processor for
290 * MPSSE and MCU Host Bus. Chapter 3.8 */
292 *khz = 6000 / (1+speed);
297 int ft2232_khz(int khz, int *jtag_speed)
301 LOG_ERROR("RCLK not supported");
304 /* Take a look in the FT2232 manual,
305 * AN2232C-01 Command Processor for
306 * MPSSE and MCU Host Bus. Chapter 3.8
308 * We will calc here with a multiplier
309 * of 10 for better rounding later. */
311 /* Calc speed, (6000 / khz) - 1 */
312 /* Use 65000 for better rounding */
313 *jtag_speed = (60000 / khz) - 10;
315 /* Add 0.9 for rounding */
318 /* Calc real speed */
319 *jtag_speed = *jtag_speed / 10;
321 /* Check if speed is greater than 0 */
327 /* Check max value */
328 if (*jtag_speed > 0xFFFF)
330 *jtag_speed = 0xFFFF;
336 int ft2232_register_commands(struct command_context_s *cmd_ctx)
338 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
339 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
340 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
341 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
342 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
343 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
344 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
345 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
346 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
347 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
351 void ft2232_end_state(enum tap_state state)
353 if (tap_move_map[state] != -1)
357 LOG_ERROR("BUG: %i is not a valid end state", state);
362 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
364 int num_bytes = (scan_size + 7) / 8;
365 int bits_left = scan_size;
368 while(num_bytes-- > 1)
370 buffer[cur_byte] = BUFFER_READ;
375 buffer[cur_byte] = 0x0;
379 buffer[cur_byte] = BUFFER_READ >> 1;
382 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
386 void ft2232_debug_dump_buffer(void)
392 for (i = 0; i < ft2232_buffer_size; i++)
394 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
397 LOG_DEBUG("%s", line);
403 LOG_DEBUG("%s", line);
406 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
416 #ifdef _DEBUG_USB_IO_
417 struct timeval start, inter, inter2, end;
418 struct timeval d_inter, d_inter2, d_end;
421 #ifdef _DEBUG_USB_COMMS_
422 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
423 ft2232_debug_dump_buffer();
426 #ifdef _DEBUG_USB_IO_
427 gettimeofday(&start, NULL);
430 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
432 LOG_ERROR("couldn't write MPSSE commands to FT2232");
436 #ifdef _DEBUG_USB_IO_
437 gettimeofday(&inter, NULL);
440 if (ft2232_expect_read)
443 ft2232_buffer_size = 0;
445 #ifdef _DEBUG_USB_IO_
446 gettimeofday(&inter2, NULL);
449 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
451 LOG_ERROR("couldn't read from FT2232");
455 #ifdef _DEBUG_USB_IO_
456 gettimeofday(&end, NULL);
458 timeval_subtract(&d_inter, &inter, &start);
459 timeval_subtract(&d_inter2, &inter2, &start);
460 timeval_subtract(&d_end, &end, &start);
462 LOG_INFO("inter: %i.%06i, inter2: %i.%06i end: %i.%06i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
466 ft2232_buffer_size = bytes_read;
468 if (ft2232_expect_read != ft2232_buffer_size)
470 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
471 ft2232_debug_dump_buffer();
476 #ifdef _DEBUG_USB_COMMS_
477 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
478 ft2232_debug_dump_buffer();
482 ft2232_expect_read = 0;
483 ft2232_read_pointer = 0;
485 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
486 * that wasn't handled by a caller-provided error handler
496 type = jtag_scan_type(cmd->cmd.scan);
497 if (type != SCAN_OUT)
499 scan_size = jtag_scan_size(cmd->cmd.scan);
500 buffer = calloc(CEIL(scan_size, 8), 1);
501 ft2232_read_scan(type, buffer, scan_size);
502 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
503 retval = ERROR_JTAG_QUEUE_FAILED;
513 ft2232_buffer_size = 0;
518 void ft2232_add_pathmove(pathmove_command_t *cmd)
520 int num_states = cmd->num_states;
529 int num_states_batch = num_states > 7 ? 7 : num_states;
532 /* command "Clock Data to TMS/CS Pin (no Read)" */
534 /* number of states remaining */
535 BUFFER_ADD = num_states_batch - 1;
537 while (num_states_batch--)
539 if (tap_transitions[cur_state].low == cmd->path[state_count])
540 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
541 else if (tap_transitions[cur_state].high == cmd->path[state_count])
542 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
545 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(cmd->path[state_count]));
549 cur_state = cmd->path[state_count];
554 BUFFER_ADD = tms_byte;
557 end_state = cur_state;
560 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
562 int num_bytes = (scan_size + 7) / 8;
563 int bits_left = scan_size;
567 if (!((!ir_scan && (cur_state == TAP_DRSHIFT)) || (ir_scan && (cur_state == TAP_IRSHIFT))))
569 /* command "Clock Data to TMS/CS Pin (no Read)" */
576 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IRSHIFT);
577 cur_state = TAP_IRSHIFT;
581 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
582 cur_state = TAP_DRSHIFT;
584 /* LOG_DEBUG("added TMS scan (no read)"); */
587 /* add command for complete bytes */
588 while (num_bytes > 1)
593 /* Clock Data Bytes In and Out LSB First */
595 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
597 else if (type == SCAN_OUT)
599 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
601 /* LOG_DEBUG("added TDI bytes (o)"); */
603 else if (type == SCAN_IN)
605 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
607 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
609 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
610 num_bytes -= thisrun_bytes;
611 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
612 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
615 /* add complete bytes */
616 while(thisrun_bytes-- > 0)
618 BUFFER_ADD = buffer[cur_byte];
623 else /* (type == SCAN_IN) */
625 bits_left -= 8 * (thisrun_bytes);
629 /* the most signifcant bit is scanned during TAP movement */
631 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
635 /* process remaining bits but the last one */
640 /* Clock Data Bits In and Out LSB First */
642 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
644 else if (type == SCAN_OUT)
646 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
648 /* LOG_DEBUG("added TDI bits (o)"); */
650 else if (type == SCAN_IN)
652 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
654 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
656 BUFFER_ADD = bits_left - 2;
658 BUFFER_ADD = buffer[cur_byte];
661 if ((ir_scan && (end_state == TAP_IRSHIFT)) ||
662 (!ir_scan && (end_state == TAP_DRSHIFT)))
666 /* Clock Data Bits In and Out LSB First */
668 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
670 else if (type == SCAN_OUT)
672 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
674 /* LOG_DEBUG("added TDI bits (o)"); */
676 else if (type == SCAN_IN)
678 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
680 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
683 BUFFER_ADD = last_bit;
687 /* move from Shift-IR/DR to end state */
688 if (type != SCAN_OUT)
690 /* Clock Data to TMS/CS Pin with Read */
692 /* LOG_DEBUG("added TMS scan (read)"); */
696 /* Clock Data to TMS/CS Pin (no Read) */
698 /* LOG_DEBUG("added TMS scan (no read)"); */
701 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
702 cur_state = end_state;
706 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
708 int num_bytes = (scan_size + 7) / 8;
709 int bits_left = scan_size;
712 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
713 u8 *receive_pointer = receive_buffer;
717 int thisrun_read = 0;
721 LOG_ERROR("BUG: large IR scans are not supported");
725 if (cur_state != TAP_DRSHIFT)
727 /* command "Clock Data to TMS/CS Pin (no Read)" */
732 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
733 cur_state = TAP_DRSHIFT;
736 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
738 LOG_ERROR("couldn't write MPSSE commands to FT2232");
741 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
742 ft2232_buffer_size = 0;
744 /* add command for complete bytes */
745 while (num_bytes > 1)
751 /* Clock Data Bytes In and Out LSB First */
753 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
755 else if (type == SCAN_OUT)
757 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
759 /* LOG_DEBUG("added TDI bytes (o)"); */
761 else if (type == SCAN_IN)
763 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
765 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
767 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
768 thisrun_read = thisrun_bytes;
769 num_bytes -= thisrun_bytes;
770 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
771 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
774 /* add complete bytes */
775 while(thisrun_bytes-- > 0)
777 BUFFER_ADD = buffer[cur_byte];
782 else /* (type == SCAN_IN) */
784 bits_left -= 8 * (thisrun_bytes);
787 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
789 LOG_ERROR("couldn't write MPSSE commands to FT2232");
792 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
793 ft2232_buffer_size = 0;
795 if (type != SCAN_OUT)
797 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
799 LOG_ERROR("couldn't read from FT2232");
802 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
803 receive_pointer += bytes_read;
809 /* the most signifcant bit is scanned during TAP movement */
811 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
815 /* process remaining bits but the last one */
820 /* Clock Data Bits In and Out LSB First */
822 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
824 else if (type == SCAN_OUT)
826 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
828 /* LOG_DEBUG("added TDI bits (o)"); */
830 else if (type == SCAN_IN)
832 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
834 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
836 BUFFER_ADD = bits_left - 2;
838 BUFFER_ADD = buffer[cur_byte];
840 if (type != SCAN_OUT)
844 if (end_state == TAP_DRSHIFT)
848 /* Clock Data Bits In and Out LSB First */
850 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
852 else if (type == SCAN_OUT)
854 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
856 /* LOG_DEBUG("added TDI bits (o)"); */
858 else if (type == SCAN_IN)
860 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
862 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
865 BUFFER_ADD = last_bit;
869 /* move from Shift-IR/DR to end state */
870 if (type != SCAN_OUT)
872 /* Clock Data to TMS/CS Pin with Read */
874 /* LOG_DEBUG("added TMS scan (read)"); */
878 /* Clock Data to TMS/CS Pin (no Read) */
880 /* LOG_DEBUG("added TMS scan (no read)"); */
883 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
884 cur_state = end_state;
887 if (type != SCAN_OUT)
890 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
892 LOG_ERROR("couldn't write MPSSE commands to FT2232");
895 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
896 ft2232_buffer_size = 0;
898 if (type != SCAN_OUT)
900 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
902 LOG_ERROR("couldn't read from FT2232");
905 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
906 receive_pointer += bytes_read;
912 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
914 int predicted_size = 3;
915 int num_bytes = (scan_size - 1) / 8;
917 if (cur_state != TAP_DRSHIFT)
920 if (type == SCAN_IN) /* only from device to host */
923 predicted_size += CEIL(num_bytes, 65536) * 3;
924 /* remaining bits - 1 (up to 7) */
925 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
927 else /* host to device, or bidirectional */
930 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
931 /* remaining bits -1 (up to 7) */
932 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
935 return predicted_size;
938 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
940 int predicted_size = 0;
942 if (type != SCAN_OUT)
945 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
946 /* remaining bits - 1 */
947 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
948 /* last bit (from TMS scan) */
952 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
954 return predicted_size;
957 void usbjtag_reset(int trst, int srst)
961 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
962 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
964 low_output &= ~nTRST; /* switch output low */
968 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
969 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
971 low_output |= nTRST; /* switch output high */
976 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
977 low_output &= ~nSRST; /* switch output low */
979 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
983 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
984 low_output |= nSRST; /* switch output high */
986 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
989 /* command "set data bits low byte" */
991 BUFFER_ADD = low_output;
992 BUFFER_ADD = low_direction;
996 void jtagkey_reset(int trst, int srst)
1000 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1001 high_output &= ~nTRSTnOE;
1003 high_output &= ~nTRST;
1007 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1008 high_output |= nTRSTnOE;
1010 high_output |= nTRST;
1015 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1016 high_output &= ~nSRST;
1018 high_output &= ~nSRSTnOE;
1022 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1023 high_output |= nSRST;
1025 high_output |= nSRSTnOE;
1028 /* command "set data bits high byte" */
1030 BUFFER_ADD = high_output;
1031 BUFFER_ADD = high_direction;
1032 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1035 void olimex_jtag_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 high_output |= nSRST;
1058 high_output &= ~nSRST;
1061 /* command "set data bits high byte" */
1063 BUFFER_ADD = high_output;
1064 BUFFER_ADD = high_direction;
1065 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1068 void axm0432_jtag_reset(int trst, int srst)
1072 cur_state = TAP_RESET;
1073 high_output &= ~nTRST;
1077 high_output |= nTRST;
1082 high_output &= ~nSRST;
1086 high_output |= nSRST;
1089 /* command "set data bits low byte" */
1091 BUFFER_ADD = high_output;
1092 BUFFER_ADD = high_direction;
1093 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1096 void flyswatter_reset(int trst, int srst)
1100 low_output &= ~nTRST;
1104 low_output |= nTRST;
1109 low_output |= nSRST;
1113 low_output &= ~nSRST;
1116 /* command "set data bits low byte" */
1118 BUFFER_ADD = low_output;
1119 BUFFER_ADD = low_direction;
1120 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1123 void turtle_reset(int trst, int srst)
1129 low_output |= nSRST;
1133 low_output &= ~nSRST;
1136 /* command "set data bits low byte" */
1138 BUFFER_ADD = low_output;
1139 BUFFER_ADD = low_direction;
1140 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1143 void comstick_reset(int trst, int srst)
1147 high_output &= ~nTRST;
1151 high_output |= nTRST;
1156 high_output &= ~nSRST;
1160 high_output |= nSRST;
1163 /* command "set data bits high byte" */
1165 BUFFER_ADD = high_output;
1166 BUFFER_ADD = high_direction;
1167 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1170 void stm32stick_reset(int trst, int srst)
1174 high_output &= ~nTRST;
1178 high_output |= nTRST;
1183 low_output &= ~nSRST;
1187 low_output |= nSRST;
1190 /* command "set data bits low byte" */
1192 BUFFER_ADD = low_output;
1193 BUFFER_ADD = low_direction;
1195 /* command "set data bits high byte" */
1197 BUFFER_ADD = high_output;
1198 BUFFER_ADD = high_direction;
1199 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1202 int ft2232_execute_queue()
1204 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1206 int scan_size; /* size of IR or DR scan */
1207 enum scan_type type;
1209 int predicted_size = 0;
1212 first_unsent = cmd; /* next command that has to be sent */
1215 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1216 * that wasn't handled by a caller-provided error handler
1220 ft2232_buffer_size = 0;
1221 ft2232_expect_read = 0;
1223 /* blink, if the current layout has that feature */
1231 case JTAG_END_STATE:
1232 if (cmd->cmd.end_state->end_state != -1)
1233 ft2232_end_state(cmd->cmd.end_state->end_state);
1237 /* only send the maximum buffer size that FT2232C can handle */
1239 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1241 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1242 retval = ERROR_JTAG_QUEUE_FAILED;
1247 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1249 cur_state = TAP_RESET;
1251 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1254 #ifdef _DEBUG_JTAG_IO_
1255 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1260 /* only send the maximum buffer size that FT2232C can handle */
1262 if (cur_state != TAP_IDLE)
1263 predicted_size += 3;
1264 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1265 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_IDLE))
1266 predicted_size += 3;
1267 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_IDLE))
1268 predicted_size += 3;
1269 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1271 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1272 retval = ERROR_JTAG_QUEUE_FAILED;
1276 if (cur_state != TAP_IDLE)
1278 /* command "Clock Data to TMS/CS Pin (no Read)" */
1283 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IDLE);
1284 cur_state = TAP_IDLE;
1287 i = cmd->cmd.runtest->num_cycles;
1290 /* command "Clock Data to TMS/CS Pin (no Read)" */
1293 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1296 cur_state = TAP_IDLE;
1297 i -= (i > 7) ? 7 : i;
1298 /* LOG_DEBUG("added TMS scan (no read)"); */
1300 if (cmd->cmd.runtest->end_state != -1)
1301 ft2232_end_state(cmd->cmd.runtest->end_state);
1302 if (cur_state != end_state)
1304 /* command "Clock Data to TMS/CS Pin (no Read)" */
1309 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1310 cur_state = end_state;
1311 /* LOG_DEBUG("added TMS scan (no read)"); */
1314 #ifdef _DEBUG_JTAG_IO_
1315 LOG_DEBUG("runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, jtag_state_name(end_state));
1319 case JTAG_STATEMOVE:
1320 /* only send the maximum buffer size that FT2232C can handle */
1322 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1324 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1325 retval = ERROR_JTAG_QUEUE_FAILED;
1329 if (cmd->cmd.statemove->end_state != -1)
1330 ft2232_end_state(cmd->cmd.statemove->end_state);
1331 /* command "Clock Data to TMS/CS Pin (no Read)" */
1336 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1337 /* LOG_DEBUG("added TMS scan (no read)"); */
1338 cur_state = end_state;
1340 #ifdef _DEBUG_JTAG_IO_
1341 LOG_DEBUG("statemove: %s", jtag_state_name(end_state));
1346 /* only send the maximum buffer size that FT2232C can handle */
1347 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1348 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1350 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1351 retval = ERROR_JTAG_QUEUE_FAILED;
1355 ft2232_add_pathmove(cmd->cmd.pathmove);
1357 #ifdef _DEBUG_JTAG_IO_
1358 LOG_DEBUG("pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1359 jtag_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
1364 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1365 type = jtag_scan_type(cmd->cmd.scan);
1366 predicted_size = ft2232_predict_scan_out(scan_size, type);
1367 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1369 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1370 /* unsent commands before this */
1371 if (first_unsent != cmd)
1372 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1373 retval = ERROR_JTAG_QUEUE_FAILED;
1375 /* current command */
1376 if (cmd->cmd.scan->end_state != -1)
1377 ft2232_end_state(cmd->cmd.scan->end_state);
1378 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1380 first_unsent = cmd->next;
1385 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1387 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1388 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1389 retval = ERROR_JTAG_QUEUE_FAILED;
1393 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1394 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1395 if (cmd->cmd.scan->end_state != -1)
1396 ft2232_end_state(cmd->cmd.scan->end_state);
1397 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1401 #ifdef _DEBUG_JTAG_IO_
1402 LOG_DEBUG("%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1403 jtag_state_name(end_state));
1408 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1409 retval = ERROR_JTAG_QUEUE_FAILED;
1410 first_unsent = cmd->next;
1411 jtag_sleep(cmd->cmd.sleep->us);
1412 #ifdef _DEBUG_JTAG_IO_
1413 LOG_DEBUG("sleep %i usec while in %s", cmd->cmd.sleep->us, jtag_state_name(cur_state));
1417 case JTAG_STABLECLOCKS:
1418 /* "if (tap_move_map[cur_state] != -1)" is of no help when cur_state==TAP_IDLE */
1427 break; /* above stable states are OK */
1429 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
1430 jtag_state_name(cur_state) );
1431 retval = ERROR_JTAG_QUEUE_FAILED;
1434 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1435 retval = ERROR_JTAG_QUEUE_FAILED;
1436 #ifdef _DEBUG_JTAG_IO_
1437 LOG_DEBUG("clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, jtag_state_name(cur_state));
1442 LOG_ERROR("BUG: unknown JTAG command type encountered");
1448 if (require_send > 0)
1449 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1450 retval = ERROR_JTAG_QUEUE_FAILED;
1455 #if BUILD_FT2232_FTD2XX == 1
1456 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1459 DWORD openex_flags = 0;
1460 char *openex_string = NULL;
1463 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",ft2232_layout, vid, pid);
1466 /* Add non-standard Vid/Pid to the linux driver */
1467 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1469 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1473 if (ft2232_device_desc && ft2232_serial)
1475 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1476 ft2232_device_desc = NULL;
1479 if (ft2232_device_desc)
1481 openex_string = ft2232_device_desc;
1482 openex_flags = FT_OPEN_BY_DESCRIPTION;
1484 else if (ft2232_serial)
1486 openex_string = ft2232_serial;
1487 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1491 LOG_ERROR("neither device description nor serial number specified");
1492 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1494 return ERROR_JTAG_INIT_FAILED;
1497 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1502 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1504 return ERROR_JTAG_INIT_FAILED;
1506 LOG_ERROR("unable to open ftdi device: %lu", status);
1507 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1508 if (status == FT_OK)
1510 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1513 for (i = 0; i < num_devices; i++)
1514 desc_array[i] = malloc(64);
1515 desc_array[num_devices] = NULL;
1517 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1519 if (status == FT_OK)
1521 LOG_ERROR("ListDevices: %lu\n", num_devices);
1522 for (i = 0; i < num_devices; i++)
1523 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1526 for (i = 0; i < num_devices; i++)
1527 free(desc_array[i]);
1532 LOG_ERROR("ListDevices: NONE\n");
1534 return ERROR_JTAG_INIT_FAILED;
1537 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1539 LOG_ERROR("unable to set latency timer: %lu", status);
1540 return ERROR_JTAG_INIT_FAILED;
1543 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1545 LOG_ERROR("unable to get latency timer: %lu", status);
1546 return ERROR_JTAG_INIT_FAILED;
1550 LOG_DEBUG("current latency timer: %i", latency_timer);
1553 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1555 LOG_ERROR("unable to set timeouts: %lu", status);
1556 return ERROR_JTAG_INIT_FAILED;
1559 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1561 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1562 return ERROR_JTAG_INIT_FAILED;
1568 static int ft2232_purge_ftd2xx(void)
1572 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1574 LOG_ERROR("error purging ftd2xx device: %lu", status);
1575 return ERROR_JTAG_INIT_FAILED;
1580 #endif /* BUILD_FT2232_FTD2XX == 1 */
1582 #if BUILD_FT2232_LIBFTDI == 1
1583 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1587 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1588 ft2232_layout, vid, pid);
1590 if (ftdi_init(&ftdic) < 0)
1591 return ERROR_JTAG_INIT_FAILED;
1593 /* context, vendor id, product id */
1594 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1595 ft2232_serial) < 0) {
1597 LOG_WARNING("unable to open ftdi device (trying more): %s",
1600 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1602 return ERROR_JTAG_INIT_FAILED;
1605 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1607 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1608 return ERROR_JTAG_INIT_FAILED;
1611 if (ftdi_usb_reset(&ftdic) < 0)
1613 LOG_ERROR("unable to reset ftdi device");
1614 return ERROR_JTAG_INIT_FAILED;
1617 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1619 LOG_ERROR("unable to set latency timer");
1620 return ERROR_JTAG_INIT_FAILED;
1623 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1625 LOG_ERROR("unable to get latency timer");
1626 return ERROR_JTAG_INIT_FAILED;
1630 LOG_DEBUG("current latency timer: %i", latency_timer);
1633 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1638 static int ft2232_purge_libftdi(void)
1640 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1642 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1643 return ERROR_JTAG_INIT_FAILED;
1648 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1650 int ft2232_init(void)
1655 ft2232_layout_t *cur_layout = ft2232_layouts;
1658 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1660 ft2232_layout = "usbjtag";
1661 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1664 while (cur_layout->name)
1666 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1668 layout = cur_layout;
1676 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1677 return ERROR_JTAG_INIT_FAILED;
1680 for (i = 0; 1; i++) {
1682 * "more indicates that there are more IDs to try, so we should
1683 * not print an error for an ID mismatch (but for anything
1686 * try_more indicates that the error code returned indicates an
1687 * ID mismatch (and nothing else) and that we should proceeed
1688 * with the next ID pair.
1690 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1693 #if BUILD_FT2232_FTD2XX == 1
1694 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1696 #elif BUILD_FT2232_LIBFTDI == 1
1697 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1702 if (!more || !try_more)
1706 ft2232_buffer_size = 0;
1707 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1709 if (layout->init() != ERROR_OK)
1710 return ERROR_JTAG_INIT_FAILED;
1712 ft2232_speed(jtag_speed);
1714 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1715 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1717 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1718 return ERROR_JTAG_INIT_FAILED;
1721 #if BUILD_FT2232_FTD2XX == 1
1722 return ft2232_purge_ftd2xx();
1723 #elif BUILD_FT2232_LIBFTDI == 1
1724 return ft2232_purge_libftdi();
1730 int usbjtag_init(void)
1736 low_direction = 0x0b;
1738 if (strcmp(ft2232_layout, "usbjtag") == 0)
1745 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1752 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1759 low_direction = 0x8b;
1763 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1764 return ERROR_JTAG_INIT_FAILED;
1767 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1769 low_direction &= ~nTRSTnOE; /* nTRST input */
1770 low_output &= ~nTRST; /* nTRST = 0 */
1774 low_direction |= nTRSTnOE; /* nTRST output */
1775 low_output |= nTRST; /* nTRST = 1 */
1778 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1780 low_direction |= nSRSTnOE; /* nSRST output */
1781 low_output |= nSRST; /* nSRST = 1 */
1785 low_direction &= ~nSRSTnOE; /* nSRST input */
1786 low_output &= ~nSRST; /* nSRST = 0 */
1789 /* initialize low byte for jtag */
1790 buf[0] = 0x80; /* command "set data bits low byte" */
1791 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1792 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1793 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1795 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1797 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1798 return ERROR_JTAG_INIT_FAILED;
1804 int axm0432_jtag_init(void)
1810 low_direction = 0x2b;
1812 /* initialize low byte for jtag */
1813 buf[0] = 0x80; /* command "set data bits low byte" */
1814 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1815 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1816 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1818 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1820 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1821 return ERROR_JTAG_INIT_FAILED;
1824 if (strcmp(layout->name, "axm0432_jtag") == 0)
1827 nTRSTnOE = 0x0; /* No output enable for TRST*/
1829 nSRSTnOE = 0x0; /* No output enable for SRST*/
1833 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1838 high_direction = 0x0c;
1840 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1842 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1846 high_output |= nTRST;
1849 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1851 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1855 high_output |= nSRST;
1858 /* initialize high port */
1859 buf[0] = 0x82; /* command "set data bits high byte" */
1860 buf[1] = high_output; /* value */
1861 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1862 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1864 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1866 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1867 return ERROR_JTAG_INIT_FAILED;
1873 int jtagkey_init(void)
1879 low_direction = 0x1b;
1881 /* initialize low byte for jtag */
1882 buf[0] = 0x80; /* command "set data bits low byte" */
1883 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1884 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1885 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1887 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1889 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1890 return ERROR_JTAG_INIT_FAILED;
1893 if (strcmp(layout->name, "jtagkey") == 0)
1900 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1901 (strcmp(layout->name, "oocdlink") == 0))
1910 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1915 high_direction = 0x0f;
1917 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1919 high_output |= nTRSTnOE;
1920 high_output &= ~nTRST;
1924 high_output &= ~nTRSTnOE;
1925 high_output |= nTRST;
1928 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1930 high_output &= ~nSRSTnOE;
1931 high_output |= nSRST;
1935 high_output |= nSRSTnOE;
1936 high_output &= ~nSRST;
1939 /* initialize high port */
1940 buf[0] = 0x82; /* command "set data bits high byte" */
1941 buf[1] = high_output; /* value */
1942 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1943 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1945 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1947 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1948 return ERROR_JTAG_INIT_FAILED;
1954 int olimex_jtag_init(void)
1960 low_direction = 0x1b;
1962 /* initialize low byte for jtag */
1963 buf[0] = 0x80; /* command "set data bits low byte" */
1964 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1965 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1966 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1968 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1970 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1971 return ERROR_JTAG_INIT_FAILED;
1977 nSRSTnOE = 0x00; /* no output enable for nSRST */
1980 high_direction = 0x0f;
1982 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1984 high_output |= nTRSTnOE;
1985 high_output &= ~nTRST;
1989 high_output &= ~nTRSTnOE;
1990 high_output |= nTRST;
1993 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1995 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1999 high_output &= ~nSRST;
2002 /* turn red LED on */
2003 high_output |= 0x08;
2005 /* initialize high port */
2006 buf[0] = 0x82; /* command "set data bits high byte" */
2007 buf[1] = high_output; /* value */
2008 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2009 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2011 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2013 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2014 return ERROR_JTAG_INIT_FAILED;
2020 int flyswatter_init(void)
2026 low_direction = 0xfb;
2028 /* initialize low byte for jtag */
2029 buf[0] = 0x80; /* command "set data bits low byte" */
2030 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2031 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2032 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2034 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2036 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2037 return ERROR_JTAG_INIT_FAILED;
2041 nTRSTnOE = 0x0; /* not output enable for nTRST */
2043 nSRSTnOE = 0x00; /* no output enable for nSRST */
2046 high_direction = 0x0c;
2048 /* turn red LED1 on, LED2 off */
2049 high_output |= 0x08;
2051 /* initialize high port */
2052 buf[0] = 0x82; /* command "set data bits high byte" */
2053 buf[1] = high_output; /* value */
2054 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2055 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2057 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2059 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2060 return ERROR_JTAG_INIT_FAILED;
2066 int turtle_init(void)
2072 low_direction = 0x5b;
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 'turtelizer2' layout");
2083 return ERROR_JTAG_INIT_FAILED;
2089 high_direction = 0x0C;
2091 /* initialize high port */
2092 buf[0] = 0x82; /* command "set data bits high byte" */
2093 buf[1] = high_output;
2094 buf[2] = high_direction;
2095 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2097 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2099 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2100 return ERROR_JTAG_INIT_FAILED;
2106 int comstick_init(void)
2112 low_direction = 0x0b;
2114 /* initialize low byte for jtag */
2115 buf[0] = 0x80; /* command "set data bits low byte" */
2116 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2117 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2118 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2120 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2122 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2123 return ERROR_JTAG_INIT_FAILED;
2127 nTRSTnOE = 0x00; /* no output enable for nTRST */
2129 nSRSTnOE = 0x00; /* no output enable for nSRST */
2132 high_direction = 0x03;
2134 /* initialize high port */
2135 buf[0] = 0x82; /* command "set data bits high byte" */
2136 buf[1] = high_output;
2137 buf[2] = high_direction;
2138 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2140 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2142 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2143 return ERROR_JTAG_INIT_FAILED;
2149 int stm32stick_init(void)
2155 low_direction = 0x8b;
2157 /* initialize low byte for jtag */
2158 buf[0] = 0x80; /* command "set data bits low byte" */
2159 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2160 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2161 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2163 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2165 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2166 return ERROR_JTAG_INIT_FAILED;
2170 nTRSTnOE = 0x00; /* no output enable for nTRST */
2172 nSRSTnOE = 0x00; /* no output enable for nSRST */
2175 high_direction = 0x03;
2177 /* initialize high port */
2178 buf[0] = 0x82; /* command "set data bits high byte" */
2179 buf[1] = high_output;
2180 buf[2] = high_direction;
2181 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2183 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2185 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2186 return ERROR_JTAG_INIT_FAILED;
2192 void olimex_jtag_blink(void)
2194 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2195 * ACBUS3 is bit 3 of the GPIOH port
2197 if (high_output & 0x08)
2199 /* set port pin high */
2200 high_output &= 0x07;
2204 /* set port pin low */
2205 high_output |= 0x08;
2209 BUFFER_ADD = high_output;
2210 BUFFER_ADD = high_direction;
2213 void turtle_jtag_blink(void)
2216 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2218 if (high_output & 0x08)
2228 BUFFER_ADD = high_output;
2229 BUFFER_ADD = high_direction;
2232 int ft2232_quit(void)
2234 #if BUILD_FT2232_FTD2XX == 1
2237 status = FT_Close(ftdih);
2238 #elif BUILD_FT2232_LIBFTDI == 1
2239 ftdi_disable_bitbang(&ftdic);
2241 ftdi_usb_close(&ftdic);
2243 ftdi_deinit(&ftdic);
2246 free(ft2232_buffer);
2247 ft2232_buffer = NULL;
2252 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2256 ft2232_device_desc = strdup(args[0]);
2260 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2266 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2270 ft2232_serial = strdup(args[0]);
2274 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2280 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2285 ft2232_layout = malloc(strlen(args[0]) + 1);
2286 strcpy(ft2232_layout, args[0]);
2291 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2295 if (argc > MAX_USB_IDS*2) {
2296 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2297 "(maximum is %d pairs)", MAX_USB_IDS);
2298 argc = MAX_USB_IDS*2;
2300 if (argc < 2 || (argc & 1))
2302 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2307 for (i = 0; i+1 < argc; i += 2) {
2308 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2309 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2312 * Explicitly terminate, in case there are multiples instances of
2315 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2320 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2324 ft2232_latency = atoi(args[0]);
2328 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2335 static int ft2232_stableclocks(int num_cycles, jtag_command_t *cmd)
2339 while (num_cycles > 0)
2341 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2342 * at most 7 bits per invocation. Here we invoke it potentially
2345 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2347 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2349 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2350 retval = ERROR_JTAG_QUEUE_FAILED;
2355 /* command "Clock Data to TMS/CS Pin (no Read)" */
2359 BUFFER_ADD = bitcount_per_command - 1;
2361 /* TMS data bits are all zeros to stay in the current stable state */
2366 num_cycles -= bitcount_per_command;