1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
43 /* FT2232 access library includes */
44 #if BUILD_FT2232_FTD2XX == 1
46 #elif BUILD_FT2232_LIBFTDI == 1
53 /* enable this to debug io latency
56 #define _DEBUG_USB_IO_
59 /* enable this to debug communication
62 #define _DEBUG_USB_COMMS_
65 int ft2232_execute_queue(void);
67 int ft2232_speed(int speed);
68 int ft2232_register_commands(struct command_context_s *cmd_ctx);
69 int ft2232_init(void);
70 int ft2232_quit(void);
72 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 char *ft2232_device_desc = NULL;
78 char *ft2232_serial = NULL;
79 char *ft2232_layout = NULL;
82 /* vid = pid = 0 marks the end of the list */
83 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
84 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
86 typedef struct ft2232_layout_s
90 void(*reset)(int trst, int srst);
94 /* init procedures for supported layouts */
95 int usbjtag_init(void);
96 int jtagkey_init(void);
97 int olimex_jtag_init(void);
99 int turtle_init(void);
100 int comstick_init(void);
102 /* reset procedures for supported layouts */
103 void usbjtag_reset(int trst, int srst);
104 void jtagkey_reset(int trst, int srst);
105 void olimex_jtag_reset(int trst, int srst);
106 void m5960_reset(int trst, int srst);
107 void turtle_reset(int trst, int srst);
108 void comstick_reset(int trst, int srst);
110 /* blink procedures for layouts that support a blinking led */
111 void olimex_jtag_blink(void);
112 void turtle_jtag_blink(void);
114 ft2232_layout_t ft2232_layouts[] =
116 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
117 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
118 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
119 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
120 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
121 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
122 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
123 {"m5960", m5960_init, m5960_reset, NULL},
124 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
125 {"comstick", comstick_init, comstick_reset, NULL},
129 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
131 static ft2232_layout_t *layout;
132 static u8 low_output = 0x0;
133 static u8 low_direction = 0x0;
134 static u8 high_output = 0x0;
135 static u8 high_direction = 0x0;
137 #if BUILD_FT2232_FTD2XX == 1
138 static FT_HANDLE ftdih = NULL;
139 #elif BUILD_FT2232_LIBFTDI == 1
140 static struct ftdi_context ftdic;
143 static u8 *ft2232_buffer = NULL;
144 static int ft2232_buffer_size = 0;
145 static int ft2232_read_pointer = 0;
146 static int ft2232_expect_read = 0;
147 #define FT2232_BUFFER_SIZE 131072
148 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
149 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
151 jtag_interface_t ft2232_interface =
156 .execute_queue = ft2232_execute_queue,
158 .support_pathmove = 1,
160 .speed = ft2232_speed,
161 .register_commands = ft2232_register_commands,
166 int ft2232_write(u8 *buf, int size, u32* bytes_written)
168 #if BUILD_FT2232_FTD2XX == 1
170 DWORD dw_bytes_written;
171 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
173 *bytes_written = dw_bytes_written;
174 ERROR("FT_Write returned: %lu", status);
175 return ERROR_JTAG_DEVICE_ERROR;
179 *bytes_written = dw_bytes_written;
182 #elif BUILD_FT2232_LIBFTDI == 1
184 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
187 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
188 return ERROR_JTAG_DEVICE_ERROR;
192 *bytes_written = retval;
198 int ft2232_read(u8* buf, int size, u32* bytes_read)
200 #if BUILD_FT2232_FTD2XX == 1
206 while ((*bytes_read < size) && timeout--)
208 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
209 *bytes_read, &dw_bytes_read)) != FT_OK)
212 ERROR("FT_Read returned: %lu", status);
213 return ERROR_JTAG_DEVICE_ERROR;
215 *bytes_read += dw_bytes_read;
217 #elif BUILD_FT2232_LIBFTDI == 1
222 while ((*bytes_read < size) && timeout--)
224 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
227 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
228 return ERROR_JTAG_DEVICE_ERROR;
230 *bytes_read += retval;
234 if (*bytes_read < size)
236 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
237 return ERROR_JTAG_DEVICE_ERROR;
243 int ft2232_speed(int speed)
249 buf[0] = 0x86; /* command "set divisor" */
250 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
251 buf[2] = (speed >> 8) & 0xff; /* valueH */
253 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
254 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
256 ERROR("couldn't set FT2232 TCK speed");
263 int ft2232_register_commands(struct command_context_s *cmd_ctx)
265 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
266 COMMAND_CONFIG, NULL);
267 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
268 COMMAND_CONFIG, NULL);
269 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
270 COMMAND_CONFIG, NULL);
271 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
272 COMMAND_CONFIG, NULL);
276 void ft2232_end_state(enum tap_state state)
278 if (tap_move_map[state] != -1)
282 ERROR("BUG: %i is not a valid end state", state);
287 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
289 int num_bytes = ((scan_size + 7) / 8);
290 int bits_left = scan_size;
293 while(num_bytes-- > 1)
295 buffer[cur_byte] = BUFFER_READ;
300 buffer[cur_byte] = 0x0;
304 buffer[cur_byte] = BUFFER_READ >> 1;
307 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
311 void ft2232_debug_dump_buffer(void)
317 for (i = 0; i < ft2232_buffer_size; i++)
319 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
331 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
341 #ifdef _DEBUG_USB_IO_
342 struct timeval start, inter, inter2, end;
343 struct timeval d_inter, d_inter2, d_end;
346 #ifdef _DEBUG_USB_COMMS_
347 DEBUG("write buffer (size %i):", ft2232_buffer_size);
348 ft2232_debug_dump_buffer();
351 #ifdef _DEBUG_USB_IO_
352 gettimeofday(&start, NULL);
355 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
357 ERROR("couldn't write MPSSE commands to FT2232");
361 #ifdef _DEBUG_USB_IO_
362 gettimeofday(&inter, NULL);
365 if (ft2232_expect_read)
368 ft2232_buffer_size = 0;
370 #ifdef _DEBUG_USB_IO_
371 gettimeofday(&inter2, NULL);
374 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
376 ERROR("couldn't read from FT2232");
380 #ifdef _DEBUG_USB_IO_
381 gettimeofday(&end, NULL);
383 timeval_subtract(&d_inter, &inter, &start);
384 timeval_subtract(&d_inter2, &inter2, &start);
385 timeval_subtract(&d_end, &end, &start);
387 INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
391 ft2232_buffer_size = bytes_read;
393 if (ft2232_expect_read != ft2232_buffer_size)
395 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
396 ft2232_debug_dump_buffer();
401 #ifdef _DEBUG_USB_COMMS_
402 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
403 ft2232_debug_dump_buffer();
407 ft2232_expect_read = 0;
408 ft2232_read_pointer = 0;
410 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
411 * that wasn't handled by a caller-provided error handler
421 type = jtag_scan_type(cmd->cmd.scan);
422 if (type != SCAN_OUT)
424 scan_size = jtag_scan_size(cmd->cmd.scan);
425 buffer = calloc(CEIL(scan_size, 8), 1);
426 ft2232_read_scan(type, buffer, scan_size);
427 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
428 retval = ERROR_JTAG_QUEUE_FAILED;
438 ft2232_buffer_size = 0;
443 void ft2232_add_pathmove(pathmove_command_t *cmd)
445 int num_states = cmd->num_states;
455 /* command "Clock Data to TMS/CS Pin (no Read)" */
457 /* number of states remaining */
458 BUFFER_ADD = (num_states % 7) - 1;
460 while (num_states % 7)
462 if (tap_transitions[cur_state].low == cmd->path[state_count])
463 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
464 else if (tap_transitions[cur_state].high == cmd->path[state_count])
465 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
468 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
472 cur_state = cmd->path[state_count];
477 BUFFER_ADD = tms_byte;
480 end_state = cur_state;
483 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
485 int num_bytes = (scan_size + 7) / 8;
486 int bits_left = scan_size;
490 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
492 /* command "Clock Data to TMS/CS Pin (no Read)" */
499 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
504 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
507 //DEBUG("added TMS scan (no read)");
510 /* add command for complete bytes */
511 while (num_bytes > 1)
516 /* Clock Data Bytes In and Out LSB First */
518 //DEBUG("added TDI bytes (io %i)", num_bytes);
520 else if (type == SCAN_OUT)
522 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
524 //DEBUG("added TDI bytes (o)");
526 else if (type == SCAN_IN)
528 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
530 //DEBUG("added TDI bytes (i %i)", num_bytes);
532 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
533 num_bytes -= thisrun_bytes;
534 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
535 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
538 /* add complete bytes */
539 while(thisrun_bytes-- > 0)
541 BUFFER_ADD = buffer[cur_byte];
546 else /* (type == SCAN_IN) */
548 bits_left -= 8 * (thisrun_bytes);
552 /* the most signifcant bit is scanned during TAP movement */
554 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
558 /* process remaining bits but the last one */
563 /* Clock Data Bits In and Out LSB First */
565 //DEBUG("added TDI bits (io) %i", bits_left - 1);
567 else if (type == SCAN_OUT)
569 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
571 //DEBUG("added TDI bits (o)");
573 else if (type == SCAN_IN)
575 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
577 //DEBUG("added TDI bits (i %i)", bits_left - 1);
579 BUFFER_ADD = bits_left - 2;
581 BUFFER_ADD = buffer[cur_byte];
584 if ((ir_scan && (end_state == TAP_SI)) ||
585 (!ir_scan && (end_state == TAP_SD)))
589 /* Clock Data Bits In and Out LSB First */
591 //DEBUG("added TDI bits (io) %i", bits_left - 1);
593 else if (type == SCAN_OUT)
595 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
597 //DEBUG("added TDI bits (o)");
599 else if (type == SCAN_IN)
601 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
603 //DEBUG("added TDI bits (i %i)", bits_left - 1);
606 BUFFER_ADD = last_bit;
610 /* move from Shift-IR/DR to end state */
611 if (type != SCAN_OUT)
613 /* Clock Data to TMS/CS Pin with Read */
615 //DEBUG("added TMS scan (read)");
619 /* Clock Data to TMS/CS Pin (no Read) */
621 //DEBUG("added TMS scan (no read)");
624 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
625 cur_state = end_state;
629 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
631 int num_bytes = (scan_size + 7) / 8;
632 int bits_left = scan_size;
635 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
636 u8 *receive_pointer = receive_buffer;
640 int thisrun_read = 0;
644 ERROR("BUG: large IR scans are not supported");
648 if (cur_state != TAP_SD)
650 /* command "Clock Data to TMS/CS Pin (no Read)" */
655 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
659 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
661 ERROR("couldn't write MPSSE commands to FT2232");
664 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
665 ft2232_buffer_size = 0;
667 /* add command for complete bytes */
668 while (num_bytes > 1)
674 /* Clock Data Bytes In and Out LSB First */
676 //DEBUG("added TDI bytes (io %i)", num_bytes);
678 else if (type == SCAN_OUT)
680 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
682 //DEBUG("added TDI bytes (o)");
684 else if (type == SCAN_IN)
686 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
688 //DEBUG("added TDI bytes (i %i)", num_bytes);
690 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
691 thisrun_read = thisrun_bytes;
692 num_bytes -= thisrun_bytes;
693 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
694 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
697 /* add complete bytes */
698 while(thisrun_bytes-- > 0)
700 BUFFER_ADD = buffer[cur_byte];
705 else /* (type == SCAN_IN) */
707 bits_left -= 8 * (thisrun_bytes);
710 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
712 ERROR("couldn't write MPSSE commands to FT2232");
715 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
716 ft2232_buffer_size = 0;
718 if (type != SCAN_OUT)
720 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
722 ERROR("couldn't read from FT2232");
725 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
726 receive_pointer += bytes_read;
732 /* the most signifcant bit is scanned during TAP movement */
734 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
738 /* process remaining bits but the last one */
743 /* Clock Data Bits In and Out LSB First */
745 //DEBUG("added TDI bits (io) %i", bits_left - 1);
747 else if (type == SCAN_OUT)
749 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
751 //DEBUG("added TDI bits (o)");
753 else if (type == SCAN_IN)
755 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
757 //DEBUG("added TDI bits (i %i)", bits_left - 1);
759 BUFFER_ADD = bits_left - 2;
761 BUFFER_ADD = buffer[cur_byte];
763 if (type != SCAN_OUT)
767 if (end_state == TAP_SD)
771 /* Clock Data Bits In and Out LSB First */
773 //DEBUG("added TDI bits (io) %i", bits_left - 1);
775 else if (type == SCAN_OUT)
777 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
779 //DEBUG("added TDI bits (o)");
781 else if (type == SCAN_IN)
783 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
785 //DEBUG("added TDI bits (i %i)", bits_left - 1);
788 BUFFER_ADD = last_bit;
792 /* move from Shift-IR/DR to end state */
793 if (type != SCAN_OUT)
795 /* Clock Data to TMS/CS Pin with Read */
797 //DEBUG("added TMS scan (read)");
801 /* Clock Data to TMS/CS Pin (no Read) */
803 //DEBUG("added TMS scan (no read)");
806 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
807 cur_state = end_state;
810 if (type != SCAN_OUT)
813 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
815 ERROR("couldn't write MPSSE commands to FT2232");
818 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
819 ft2232_buffer_size = 0;
821 if (type != SCAN_OUT)
823 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
825 ERROR("couldn't read from FT2232");
828 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
829 receive_pointer += bytes_read;
835 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
837 int predicted_size = 3;
838 int num_bytes = (scan_size - 1) / 8;
840 if (cur_state != TAP_SD)
843 if (type == SCAN_IN) /* only from device to host */
846 predicted_size += (CEIL(num_bytes, 65536)) * 3;
847 /* remaining bits - 1 (up to 7) */
848 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
850 else /* host to device, or bidirectional */
853 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
854 /* remaining bits -1 (up to 7) */
855 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
858 return predicted_size;
861 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
863 int predicted_size = 0;
865 if (type != SCAN_OUT)
868 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
869 /* remaining bits - 1 */
870 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
871 /* last bit (from TMS scan) */
875 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
877 return predicted_size;
880 void usbjtag_reset(int trst, int srst)
885 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
886 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
888 low_output &= ~nTRST; /* switch output low */
892 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
893 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
895 low_output |= nTRST; /* switch output high */
900 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
901 low_output &= ~nSRST; /* switch output low */
903 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
907 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
908 low_output |= nSRST; /* switch output high */
910 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
913 /* command "set data bits low byte" */
915 BUFFER_ADD = low_output;
916 BUFFER_ADD = low_direction;
920 void jtagkey_reset(int trst, int srst)
925 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
926 high_output &= ~nTRSTnOE;
928 high_output &= ~nTRST;
932 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
933 high_output |= nTRSTnOE;
935 high_output |= nTRST;
940 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
941 high_output &= ~nSRST;
943 high_output &= ~nSRSTnOE;
947 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
948 high_output |= nSRST;
950 high_output |= nSRSTnOE;
953 /* command "set data bits high byte" */
955 BUFFER_ADD = high_output;
956 BUFFER_ADD = high_direction;
957 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
960 void olimex_jtag_reset(int trst, int srst)
965 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
966 high_output &= ~nTRSTnOE;
968 high_output &= ~nTRST;
972 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
973 high_output |= nTRSTnOE;
975 high_output |= nTRST;
980 high_output |= nSRST;
984 high_output &= ~nSRST;
987 /* command "set data bits high byte" */
989 BUFFER_ADD = high_output;
990 BUFFER_ADD = high_direction;
991 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
994 void m5960_reset(int trst, int srst)
999 low_output &= ~nTRST;
1003 low_output |= nTRST;
1008 low_output |= nSRST;
1012 low_output &= ~nSRST;
1015 /* command "set data bits low byte" */
1017 BUFFER_ADD = low_output;
1018 BUFFER_ADD = low_direction;
1019 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1022 void turtle_reset(int trst, int srst)
1028 low_output |= nSRST;
1032 low_output &= ~nSRST;
1035 /* command "set data bits low byte" */
1037 BUFFER_ADD = low_output;
1038 BUFFER_ADD = low_direction;
1039 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1042 void comstick_reset(int trst, int srst)
1046 cur_state = TAP_TLR;
1047 high_output &= ~nTRST;
1051 high_output |= nTRST;
1056 high_output &= ~nSRST;
1060 high_output |= nSRST;
1063 /* command "set data bits high byte" */
1065 BUFFER_ADD = high_output;
1066 BUFFER_ADD = high_direction;
1067 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1070 int ft2232_execute_queue()
1072 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1073 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1075 int scan_size; /* size of IR or DR scan */
1076 enum scan_type type;
1078 int predicted_size = 0;
1079 int require_send = 0;
1082 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1083 * that wasn't handled by a caller-provided error handler
1087 ft2232_buffer_size = 0;
1088 ft2232_expect_read = 0;
1090 /* blink, if the current layout has that feature */
1098 case JTAG_END_STATE:
1099 if (cmd->cmd.end_state->end_state != -1)
1100 ft2232_end_state(cmd->cmd.end_state->end_state);
1103 /* only send the maximum buffer size that FT2232C can handle */
1105 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1107 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1108 retval = ERROR_JTAG_QUEUE_FAILED;
1113 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1116 #ifdef _DEBUG_JTAG_IO_
1117 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1121 /* only send the maximum buffer size that FT2232C can handle */
1123 if (cur_state != TAP_RTI)
1124 predicted_size += 3;
1125 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1126 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1127 predicted_size += 3;
1128 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1129 predicted_size += 3;
1130 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1132 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1133 retval = ERROR_JTAG_QUEUE_FAILED;
1137 if (cur_state != TAP_RTI)
1139 /* command "Clock Data to TMS/CS Pin (no Read)" */
1144 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1145 cur_state = TAP_RTI;
1148 i = cmd->cmd.runtest->num_cycles;
1151 /* command "Clock Data to TMS/CS Pin (no Read)" */
1154 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1157 cur_state = TAP_RTI;
1158 i -= (i > 7) ? 7 : i;
1159 //DEBUG("added TMS scan (no read)");
1161 if (cmd->cmd.runtest->end_state != -1)
1162 ft2232_end_state(cmd->cmd.runtest->end_state);
1163 if (cur_state != end_state)
1165 /* command "Clock Data to TMS/CS Pin (no Read)" */
1170 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1171 cur_state = end_state;
1172 //DEBUG("added TMS scan (no read)");
1175 #ifdef _DEBUG_JTAG_IO_
1176 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1179 case JTAG_STATEMOVE:
1180 /* only send the maximum buffer size that FT2232C can handle */
1182 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1184 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1185 retval = ERROR_JTAG_QUEUE_FAILED;
1189 if (cmd->cmd.statemove->end_state != -1)
1190 ft2232_end_state(cmd->cmd.statemove->end_state);
1191 /* command "Clock Data to TMS/CS Pin (no Read)" */
1196 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1197 //DEBUG("added TMS scan (no read)");
1198 cur_state = end_state;
1200 #ifdef _DEBUG_JTAG_IO_
1201 DEBUG("statemove: %i", end_state);
1205 /* only send the maximum buffer size that FT2232C can handle */
1206 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1207 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1209 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1210 retval = ERROR_JTAG_QUEUE_FAILED;
1214 ft2232_add_pathmove(cmd->cmd.pathmove);
1216 #ifdef _DEBUG_JTAG_IO_
1217 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1221 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1222 type = jtag_scan_type(cmd->cmd.scan);
1223 predicted_size = ft2232_predict_scan_out(scan_size, type);
1224 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1226 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1227 /* unsent commands before this */
1228 if (first_unsent != cmd)
1229 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1230 retval = ERROR_JTAG_QUEUE_FAILED;
1232 /* current command */
1233 if (cmd->cmd.scan->end_state != -1)
1234 ft2232_end_state(cmd->cmd.scan->end_state);
1235 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1237 first_unsent = cmd->next;
1242 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1244 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1245 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1246 retval = ERROR_JTAG_QUEUE_FAILED;
1250 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1251 //DEBUG("new read size: %i", ft2232_expect_read);
1252 if (cmd->cmd.scan->end_state != -1)
1253 ft2232_end_state(cmd->cmd.scan->end_state);
1254 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1258 #ifdef _DEBUG_JTAG_IO_
1259 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1263 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1264 retval = ERROR_JTAG_QUEUE_FAILED;
1265 first_unsent = cmd->next;
1266 jtag_sleep(cmd->cmd.sleep->us);
1267 #ifdef _DEBUG_JTAG_IO_
1268 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1272 ERROR("BUG: unknown JTAG command type encountered");
1278 if (require_send > 0)
1279 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1280 retval = ERROR_JTAG_QUEUE_FAILED;
1285 #if BUILD_FT2232_FTD2XX == 1
1286 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1289 DWORD openex_flags = 0;
1290 char *openex_string = NULL;
1293 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1294 ft2232_layout, vid, pid);
1297 /* Add non-standard Vid/Pid to the linux driver */
1298 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1300 WARNING("couldn't add %4.4x:%4.4x",
1305 if (ft2232_device_desc && ft2232_serial)
1307 WARNING("can't open by device description and serial number, giving precedence to serial");
1308 ft2232_device_desc = NULL;
1311 if (ft2232_device_desc)
1313 openex_string = ft2232_device_desc;
1314 openex_flags = FT_OPEN_BY_DESCRIPTION;
1316 else if (ft2232_serial)
1318 openex_string = ft2232_serial;
1319 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1323 ERROR("neither device description nor serial number specified");
1324 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1326 return ERROR_JTAG_INIT_FAILED;
1329 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1334 WARNING("unable to open ftdi device (trying more): %lu",
1337 return ERROR_JTAG_INIT_FAILED;
1339 ERROR("unable to open ftdi device: %lu", status);
1340 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1341 if (status == FT_OK)
1343 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1346 for (i = 0; i < num_devices; i++)
1347 desc_array[i] = malloc(64);
1348 desc_array[num_devices] = NULL;
1350 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1352 if (status == FT_OK)
1354 ERROR("ListDevices: %lu\n", num_devices);
1355 for (i = 0; i < num_devices; i++)
1356 ERROR("%i: %s", i, desc_array[i]);
1359 for (i = 0; i < num_devices; i++)
1360 free(desc_array[i]);
1365 printf("ListDevices: NONE\n");
1367 return ERROR_JTAG_INIT_FAILED;
1370 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1372 ERROR("unable to set latency timer: %lu", status);
1373 return ERROR_JTAG_INIT_FAILED;
1376 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1378 ERROR("unable to get latency timer: %lu", status);
1379 return ERROR_JTAG_INIT_FAILED;
1383 DEBUG("current latency timer: %i", latency_timer);
1386 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1388 ERROR("unable to set timeouts: %lu", status);
1389 return ERROR_JTAG_INIT_FAILED;
1392 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1394 ERROR("unable to enable bit i/o mode: %lu", status);
1395 return ERROR_JTAG_INIT_FAILED;
1401 static int ft2232_purge_ftd2xx(void)
1405 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1407 ERROR("error purging ftd2xx device: %lu", status);
1408 return ERROR_JTAG_INIT_FAILED;
1413 #endif /* BUILD_FT2232_FTD2XX == 1 */
1415 #if BUILD_FT2232_LIBFTDI == 1
1416 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1420 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1421 ft2232_layout, vid, pid);
1423 if (ftdi_init(&ftdic) < 0)
1424 return ERROR_JTAG_INIT_FAILED;
1426 /* context, vendor id, product id */
1427 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1428 ft2232_serial) < 0) {
1430 WARNING("unable to open ftdi device (trying more): %s",
1433 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1435 return ERROR_JTAG_INIT_FAILED;
1438 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1440 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1441 return ERROR_JTAG_INIT_FAILED;
1444 if (ftdi_usb_reset(&ftdic) < 0)
1446 ERROR("unable to reset ftdi device");
1447 return ERROR_JTAG_INIT_FAILED;
1450 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1452 ERROR("unable to set latency timer");
1453 return ERROR_JTAG_INIT_FAILED;
1456 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1458 ERROR("unable to get latency timer");
1459 return ERROR_JTAG_INIT_FAILED;
1463 DEBUG("current latency timer: %i", latency_timer);
1466 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1471 static int ft2232_purge_libftdi(void)
1473 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1475 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1476 return ERROR_JTAG_INIT_FAILED;
1481 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1483 int ft2232_init(void)
1488 ft2232_layout_t *cur_layout = ft2232_layouts;
1491 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1493 ft2232_layout = "usbjtag";
1494 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1497 while (cur_layout->name)
1499 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1501 layout = cur_layout;
1509 ERROR("No matching layout found for %s", ft2232_layout);
1510 return ERROR_JTAG_INIT_FAILED;
1513 for (i = 0; 1; i++) {
1515 * "more indicates that there are more IDs to try, so we should
1516 * not print an error for an ID mismatch (but for anything
1519 * try_more indicates that the error code returned indicates an
1520 * ID mismatch (and nothing else) and that we should proceeed
1521 * with the next ID pair.
1523 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1526 #if BUILD_FT2232_FTD2XX == 1
1527 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1529 #elif BUILD_FT2232_LIBFTDI == 1
1530 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1535 if (!more || !try_more)
1539 ft2232_buffer_size = 0;
1540 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1542 if (layout->init() != ERROR_OK)
1543 return ERROR_JTAG_INIT_FAILED;
1545 ft2232_speed(jtag_speed);
1547 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1548 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1550 ERROR("couldn't write to FT2232 to disable loopback");
1551 return ERROR_JTAG_INIT_FAILED;
1554 #if BUILD_FT2232_FTD2XX == 1
1555 return ft2232_purge_ftd2xx();
1556 #elif BUILD_FT2232_LIBFTDI == 1
1557 return ft2232_purge_libftdi();
1563 int usbjtag_init(void)
1569 low_direction = 0x0b;
1571 if (strcmp(ft2232_layout, "usbjtag") == 0)
1578 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1585 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1592 low_direction = 0x8b;
1596 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1597 return ERROR_JTAG_INIT_FAILED;
1600 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1602 low_direction &= ~nTRSTnOE; /* nTRST input */
1603 low_output &= ~nTRST; /* nTRST = 0 */
1607 low_direction |= nTRSTnOE; /* nTRST output */
1608 low_output |= nTRST; /* nTRST = 1 */
1611 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1613 low_direction |= nSRSTnOE; /* nSRST output */
1614 low_output |= nSRST; /* nSRST = 1 */
1618 low_direction &= ~nSRSTnOE; /* nSRST input */
1619 low_output &= ~nSRST; /* nSRST = 0 */
1622 /* initialize low byte for jtag */
1623 buf[0] = 0x80; /* command "set data bits low byte" */
1624 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1625 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1626 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1628 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1630 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1631 return ERROR_JTAG_INIT_FAILED;
1637 int jtagkey_init(void)
1643 low_direction = 0x1b;
1645 /* initialize low byte for jtag */
1646 buf[0] = 0x80; /* command "set data bits low byte" */
1647 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1648 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1649 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1651 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1653 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1654 return ERROR_JTAG_INIT_FAILED;
1657 if (strcmp(layout->name, "jtagkey") == 0)
1664 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1665 (strcmp(layout->name, "oocdlink") == 0))
1674 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1679 high_direction = 0x0f;
1681 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1683 high_output |= nTRSTnOE;
1684 high_output &= ~nTRST;
1688 high_output &= ~nTRSTnOE;
1689 high_output |= nTRST;
1692 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1694 high_output &= ~nSRSTnOE;
1695 high_output |= nSRST;
1699 high_output |= nSRSTnOE;
1700 high_output &= ~nSRST;
1703 /* initialize high port */
1704 buf[0] = 0x82; /* command "set data bits high byte" */
1705 buf[1] = high_output; /* value */
1706 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1707 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1709 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1711 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1712 return ERROR_JTAG_INIT_FAILED;
1718 int olimex_jtag_init(void)
1724 low_direction = 0x1b;
1726 /* initialize low byte for jtag */
1727 buf[0] = 0x80; /* command "set data bits low byte" */
1728 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1729 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1730 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1732 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1734 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1735 return ERROR_JTAG_INIT_FAILED;
1741 nSRSTnOE = 0x00; /* no output enable for nSRST */
1744 high_direction = 0x0f;
1746 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1748 high_output |= nTRSTnOE;
1749 high_output &= ~nTRST;
1753 high_output &= ~nTRSTnOE;
1754 high_output |= nTRST;
1757 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1759 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1763 high_output &= ~nSRST;
1766 /* turn red LED on */
1767 high_output |= 0x08;
1769 /* initialize high port */
1770 buf[0] = 0x82; /* command "set data bits high byte" */
1771 buf[1] = high_output; /* value */
1772 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1773 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1775 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1777 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1778 return ERROR_JTAG_INIT_FAILED;
1784 int m5960_init(void)
1790 low_direction = 0xfb;
1792 /* initialize low byte for jtag */
1793 buf[0] = 0x80; /* command "set data bits low byte" */
1794 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1795 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1796 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1798 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1800 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1801 return ERROR_JTAG_INIT_FAILED;
1805 nTRSTnOE = 0x0; /* not output enable for nTRST */
1807 nSRSTnOE = 0x00; /* no output enable for nSRST */
1810 high_direction = 0x0c;
1812 /* turn red LED1 on, LED2 off */
1813 high_output |= 0x08;
1815 /* initialize high port */
1816 buf[0] = 0x82; /* command "set data bits high byte" */
1817 buf[1] = high_output; /* value */
1818 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1819 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1821 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1823 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1824 return ERROR_JTAG_INIT_FAILED;
1830 int turtle_init(void)
1836 low_direction = 0x5b;
1838 /* initialize low byte for jtag */
1839 buf[0] = 0x80; /* command "set data bits low byte" */
1840 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1841 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1842 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1844 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1846 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1847 return ERROR_JTAG_INIT_FAILED;
1853 high_direction = 0x0C;
1855 /* initialize high port */
1856 buf[0] = 0x82; /* command "set data bits high byte" */
1857 buf[1] = high_output;
1858 buf[2] = high_direction;
1859 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1861 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1863 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1864 return ERROR_JTAG_INIT_FAILED;
1870 int comstick_init(void)
1876 low_direction = 0x0b;
1878 /* initialize low byte for jtag */
1879 buf[0] = 0x80; /* command "set data bits low byte" */
1880 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1881 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1882 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1884 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1886 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1887 return ERROR_JTAG_INIT_FAILED;
1891 nTRSTnOE = 0x00; /* no output enable for nTRST */
1893 nSRSTnOE = 0x00; /* no output enable for nSRST */
1896 high_direction = 0x03;
1898 /* initialize high port */
1899 buf[0] = 0x82; /* command "set data bits high byte" */
1900 buf[1] = high_output;
1901 buf[2] = high_direction;
1902 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1904 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1906 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1907 return ERROR_JTAG_INIT_FAILED;
1913 void olimex_jtag_blink(void)
1915 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1916 * ACBUS3 is bit 3 of the GPIOH port
1918 if (high_output & 0x08)
1920 /* set port pin high */
1921 high_output &= 0x07;
1925 /* set port pin low */
1926 high_output |= 0x08;
1930 BUFFER_ADD = high_output;
1931 BUFFER_ADD = high_direction;
1934 void turtle_jtag_blink(void)
1937 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
1939 if (high_output & 0x08)
1949 BUFFER_ADD = high_output;
1950 BUFFER_ADD = high_direction;
1954 int ft2232_quit(void)
1956 #if BUILD_FT2232_FTD2XX == 1
1959 status = FT_Close(ftdih);
1960 #elif BUILD_FT2232_LIBFTDI == 1
1961 ftdi_disable_bitbang(&ftdic);
1963 ftdi_usb_close(&ftdic);
1965 ftdi_deinit(&ftdic);
1968 free(ft2232_buffer);
1973 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1977 ft2232_device_desc = strdup(args[0]);
1981 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1987 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1991 ft2232_serial = strdup(args[0]);
1995 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2001 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2006 ft2232_layout = malloc(strlen(args[0]) + 1);
2007 strcpy(ft2232_layout, args[0]);
2012 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2016 if (argc > MAX_USB_IDS*2) {
2017 WARNING("ignoring extra IDs in ft2232_vid_pid "
2018 "(maximum is %d pairs)", MAX_USB_IDS);
2019 argc = MAX_USB_IDS*2;
2021 if (argc < 2 || (argc & 1))
2023 WARNING("incomplete ft2232_vid_pid configuration directive");
2028 for (i = 0; i+1 < argc; i += 2) {
2029 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2030 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2033 * Explicitly terminate, in case there are multiples instances of
2036 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;