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, size, &dw_bytes_read)) != FT_OK)
211 ERROR("FT_Read returned: %lu", status);
212 return ERROR_JTAG_DEVICE_ERROR;
214 *bytes_read += dw_bytes_read;
216 #elif BUILD_FT2232_LIBFTDI == 1
221 while ((*bytes_read < size) && timeout--)
223 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
226 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
227 return ERROR_JTAG_DEVICE_ERROR;
229 *bytes_read += retval;
233 if (*bytes_read < size)
235 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
236 return ERROR_JTAG_DEVICE_ERROR;
242 int ft2232_speed(int speed)
248 buf[0] = 0x86; /* command "set divisor" */
249 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
250 buf[2] = (speed >> 8) & 0xff; /* valueH */
252 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
253 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
255 ERROR("couldn't set FT2232 TCK speed");
262 int ft2232_register_commands(struct command_context_s *cmd_ctx)
264 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
265 COMMAND_CONFIG, NULL);
266 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
267 COMMAND_CONFIG, NULL);
268 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
269 COMMAND_CONFIG, NULL);
270 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
271 COMMAND_CONFIG, NULL);
275 void ft2232_end_state(enum tap_state state)
277 if (tap_move_map[state] != -1)
281 ERROR("BUG: %i is not a valid end state", state);
286 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
288 int num_bytes = ((scan_size + 7) / 8);
289 int bits_left = scan_size;
292 while(num_bytes-- > 1)
294 buffer[cur_byte] = BUFFER_READ;
299 buffer[cur_byte] = 0x0;
303 buffer[cur_byte] = BUFFER_READ >> 1;
306 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
310 void ft2232_debug_dump_buffer(void)
316 for (i = 0; i < ft2232_buffer_size; i++)
318 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
330 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
340 #ifdef _DEBUG_USB_IO_
341 struct timeval start, inter, inter2, end;
342 struct timeval d_inter, d_inter2, d_end;
345 #ifdef _DEBUG_USB_COMMS_
346 DEBUG("write buffer (size %i):", ft2232_buffer_size);
347 ft2232_debug_dump_buffer();
350 #ifdef _DEBUG_USB_IO_
351 gettimeofday(&start, NULL);
354 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
356 ERROR("couldn't write MPSSE commands to FT2232");
360 #ifdef _DEBUG_USB_IO_
361 gettimeofday(&inter, NULL);
364 if (ft2232_expect_read)
367 ft2232_buffer_size = 0;
369 #ifdef _DEBUG_USB_IO_
370 gettimeofday(&inter2, NULL);
373 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
375 ERROR("couldn't read from FT2232");
379 #ifdef _DEBUG_USB_IO_
380 gettimeofday(&end, NULL);
382 timeval_subtract(&d_inter, &inter, &start);
383 timeval_subtract(&d_inter2, &inter2, &start);
384 timeval_subtract(&d_end, &end, &start);
386 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);
390 ft2232_buffer_size = bytes_read;
392 if (ft2232_expect_read != ft2232_buffer_size)
394 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
395 ft2232_debug_dump_buffer();
400 #ifdef _DEBUG_USB_COMMS_
401 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
402 ft2232_debug_dump_buffer();
406 ft2232_expect_read = 0;
407 ft2232_read_pointer = 0;
409 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
410 * that wasn't handled by a caller-provided error handler
420 type = jtag_scan_type(cmd->cmd.scan);
421 if (type != SCAN_OUT)
423 scan_size = jtag_scan_size(cmd->cmd.scan);
424 buffer = calloc(CEIL(scan_size, 8), 1);
425 ft2232_read_scan(type, buffer, scan_size);
426 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
427 retval = ERROR_JTAG_QUEUE_FAILED;
437 ft2232_buffer_size = 0;
442 void ft2232_add_pathmove(pathmove_command_t *cmd)
444 int num_states = cmd->num_states;
454 /* command "Clock Data to TMS/CS Pin (no Read)" */
456 /* number of states remaining */
457 BUFFER_ADD = (num_states % 7) - 1;
459 while (num_states % 7)
461 if (tap_transitions[cur_state].low == cmd->path[state_count])
462 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
463 else if (tap_transitions[cur_state].high == cmd->path[state_count])
464 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
467 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
471 cur_state = cmd->path[state_count];
476 BUFFER_ADD = tms_byte;
479 end_state = cur_state;
482 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
484 int num_bytes = (scan_size + 7) / 8;
485 int bits_left = scan_size;
489 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
491 /* command "Clock Data to TMS/CS Pin (no Read)" */
498 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
503 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
506 //DEBUG("added TMS scan (no read)");
509 /* add command for complete bytes */
510 while (num_bytes > 1)
515 /* Clock Data Bytes In and Out LSB First */
517 //DEBUG("added TDI bytes (io %i)", num_bytes);
519 else if (type == SCAN_OUT)
521 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
523 //DEBUG("added TDI bytes (o)");
525 else if (type == SCAN_IN)
527 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
529 //DEBUG("added TDI bytes (i %i)", num_bytes);
531 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
532 num_bytes -= thisrun_bytes;
533 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
534 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
537 /* add complete bytes */
538 while(thisrun_bytes-- > 0)
540 BUFFER_ADD = buffer[cur_byte];
545 else /* (type == SCAN_IN) */
547 bits_left -= 8 * (thisrun_bytes);
551 /* the most signifcant bit is scanned during TAP movement */
553 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
557 /* process remaining bits but the last one */
562 /* Clock Data Bits In and Out LSB First */
564 //DEBUG("added TDI bits (io) %i", bits_left - 1);
566 else if (type == SCAN_OUT)
568 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
570 //DEBUG("added TDI bits (o)");
572 else if (type == SCAN_IN)
574 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
576 //DEBUG("added TDI bits (i %i)", bits_left - 1);
578 BUFFER_ADD = bits_left - 2;
580 BUFFER_ADD = buffer[cur_byte];
583 if ((ir_scan && (end_state == TAP_SI)) ||
584 (!ir_scan && (end_state == TAP_SD)))
588 /* Clock Data Bits In and Out LSB First */
590 //DEBUG("added TDI bits (io) %i", bits_left - 1);
592 else if (type == SCAN_OUT)
594 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
596 //DEBUG("added TDI bits (o)");
598 else if (type == SCAN_IN)
600 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
602 //DEBUG("added TDI bits (i %i)", bits_left - 1);
605 BUFFER_ADD = last_bit;
609 /* move from Shift-IR/DR to end state */
610 if (type != SCAN_OUT)
612 /* Clock Data to TMS/CS Pin with Read */
614 //DEBUG("added TMS scan (read)");
618 /* Clock Data to TMS/CS Pin (no Read) */
620 //DEBUG("added TMS scan (no read)");
623 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
624 cur_state = end_state;
628 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
630 int num_bytes = (scan_size + 7) / 8;
631 int bits_left = scan_size;
634 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
635 u8 *receive_pointer = receive_buffer;
639 int thisrun_read = 0;
643 ERROR("BUG: large IR scans are not supported");
647 if (cur_state != TAP_SD)
649 /* command "Clock Data to TMS/CS Pin (no Read)" */
654 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
658 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
660 ERROR("couldn't write MPSSE commands to FT2232");
663 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
664 ft2232_buffer_size = 0;
666 /* add command for complete bytes */
667 while (num_bytes > 1)
673 /* Clock Data Bytes In and Out LSB First */
675 //DEBUG("added TDI bytes (io %i)", num_bytes);
677 else if (type == SCAN_OUT)
679 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
681 //DEBUG("added TDI bytes (o)");
683 else if (type == SCAN_IN)
685 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
687 //DEBUG("added TDI bytes (i %i)", num_bytes);
689 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
690 thisrun_read = thisrun_bytes;
691 num_bytes -= thisrun_bytes;
692 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
693 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
696 /* add complete bytes */
697 while(thisrun_bytes-- > 0)
699 BUFFER_ADD = buffer[cur_byte];
704 else /* (type == SCAN_IN) */
706 bits_left -= 8 * (thisrun_bytes);
709 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
711 ERROR("couldn't write MPSSE commands to FT2232");
714 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
715 ft2232_buffer_size = 0;
717 if (type != SCAN_OUT)
719 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
721 ERROR("couldn't read from FT2232");
724 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
725 receive_pointer += bytes_read;
731 /* the most signifcant bit is scanned during TAP movement */
733 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
737 /* process remaining bits but the last one */
742 /* Clock Data Bits In and Out LSB First */
744 //DEBUG("added TDI bits (io) %i", bits_left - 1);
746 else if (type == SCAN_OUT)
748 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
750 //DEBUG("added TDI bits (o)");
752 else if (type == SCAN_IN)
754 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
756 //DEBUG("added TDI bits (i %i)", bits_left - 1);
758 BUFFER_ADD = bits_left - 2;
760 BUFFER_ADD = buffer[cur_byte];
762 if (type != SCAN_OUT)
766 if (end_state == TAP_SD)
770 /* Clock Data Bits In and Out LSB First */
772 //DEBUG("added TDI bits (io) %i", bits_left - 1);
774 else if (type == SCAN_OUT)
776 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
778 //DEBUG("added TDI bits (o)");
780 else if (type == SCAN_IN)
782 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
784 //DEBUG("added TDI bits (i %i)", bits_left - 1);
787 BUFFER_ADD = last_bit;
791 /* move from Shift-IR/DR to end state */
792 if (type != SCAN_OUT)
794 /* Clock Data to TMS/CS Pin with Read */
796 //DEBUG("added TMS scan (read)");
800 /* Clock Data to TMS/CS Pin (no Read) */
802 //DEBUG("added TMS scan (no read)");
805 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
806 cur_state = end_state;
809 if (type != SCAN_OUT)
812 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
814 ERROR("couldn't write MPSSE commands to FT2232");
817 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
818 ft2232_buffer_size = 0;
820 if (type != SCAN_OUT)
822 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
824 ERROR("couldn't read from FT2232");
827 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
828 receive_pointer += bytes_read;
834 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
836 int predicted_size = 3;
837 int num_bytes = (scan_size - 1) / 8;
839 if (cur_state != TAP_SD)
842 if (type == SCAN_IN) /* only from device to host */
845 predicted_size += (CEIL(num_bytes, 65536)) * 3;
846 /* remaining bits - 1 (up to 7) */
847 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
849 else /* host to device, or bidirectional */
852 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
853 /* remaining bits -1 (up to 7) */
854 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
857 return predicted_size;
860 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
862 int predicted_size = 0;
864 if (type != SCAN_OUT)
867 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
868 /* remaining bits - 1 */
869 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
870 /* last bit (from TMS scan) */
874 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
876 return predicted_size;
879 void usbjtag_reset(int trst, int srst)
884 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
885 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
887 low_output &= ~nTRST; /* switch output low */
891 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
892 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
894 low_output |= nTRST; /* switch output high */
899 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
900 low_output &= ~nSRST; /* switch output low */
902 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
906 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
907 low_output |= nSRST; /* switch output high */
909 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
912 /* command "set data bits low byte" */
914 BUFFER_ADD = low_output;
915 BUFFER_ADD = low_direction;
919 void jtagkey_reset(int trst, int srst)
924 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
925 high_output &= ~nTRSTnOE;
927 high_output &= ~nTRST;
931 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
932 high_output |= nTRSTnOE;
934 high_output |= nTRST;
939 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
940 high_output &= ~nSRST;
942 high_output &= ~nSRSTnOE;
946 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
947 high_output |= nSRST;
949 high_output |= nSRSTnOE;
952 /* command "set data bits high byte" */
954 BUFFER_ADD = high_output;
955 BUFFER_ADD = high_direction;
956 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
959 void olimex_jtag_reset(int trst, int srst)
964 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
965 high_output &= ~nTRSTnOE;
967 high_output &= ~nTRST;
971 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
972 high_output |= nTRSTnOE;
974 high_output |= nTRST;
979 high_output |= nSRST;
983 high_output &= ~nSRST;
986 /* command "set data bits high byte" */
988 BUFFER_ADD = high_output;
989 BUFFER_ADD = high_direction;
990 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
993 void m5960_reset(int trst, int srst)
998 low_output &= ~nTRST;
1002 low_output |= nTRST;
1007 low_output |= nSRST;
1011 low_output &= ~nSRST;
1014 /* command "set data bits low byte" */
1016 BUFFER_ADD = low_output;
1017 BUFFER_ADD = low_direction;
1018 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1021 void turtle_reset(int trst, int srst)
1027 low_output |= nSRST;
1031 low_output &= ~nSRST;
1034 /* command "set data bits low byte" */
1036 BUFFER_ADD = low_output;
1037 BUFFER_ADD = low_direction;
1038 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1041 void comstick_reset(int trst, int srst)
1045 cur_state = TAP_TLR;
1046 high_output &= ~nTRST;
1050 high_output |= nTRST;
1055 high_output &= ~nSRST;
1059 high_output |= nSRST;
1062 /* command "set data bits high byte" */
1064 BUFFER_ADD = high_output;
1065 BUFFER_ADD = high_direction;
1066 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1069 int ft2232_execute_queue()
1071 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1072 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1074 int scan_size; /* size of IR or DR scan */
1075 enum scan_type type;
1077 int predicted_size = 0;
1078 int require_send = 0;
1081 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1082 * that wasn't handled by a caller-provided error handler
1086 ft2232_buffer_size = 0;
1087 ft2232_expect_read = 0;
1089 /* blink, if the current layout has that feature */
1097 case JTAG_END_STATE:
1098 if (cmd->cmd.end_state->end_state != -1)
1099 ft2232_end_state(cmd->cmd.end_state->end_state);
1102 /* only send the maximum buffer size that FT2232C can handle */
1104 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1106 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1107 retval = ERROR_JTAG_QUEUE_FAILED;
1112 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1115 #ifdef _DEBUG_JTAG_IO_
1116 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1120 /* only send the maximum buffer size that FT2232C can handle */
1122 if (cur_state != TAP_RTI)
1123 predicted_size += 3;
1124 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1125 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1126 predicted_size += 3;
1127 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1128 predicted_size += 3;
1129 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1131 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1132 retval = ERROR_JTAG_QUEUE_FAILED;
1136 if (cur_state != TAP_RTI)
1138 /* command "Clock Data to TMS/CS Pin (no Read)" */
1143 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1144 cur_state = TAP_RTI;
1147 i = cmd->cmd.runtest->num_cycles;
1150 /* command "Clock Data to TMS/CS Pin (no Read)" */
1153 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1156 cur_state = TAP_RTI;
1157 i -= (i > 7) ? 7 : i;
1158 //DEBUG("added TMS scan (no read)");
1160 if (cmd->cmd.runtest->end_state != -1)
1161 ft2232_end_state(cmd->cmd.runtest->end_state);
1162 if (cur_state != end_state)
1164 /* command "Clock Data to TMS/CS Pin (no Read)" */
1169 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1170 cur_state = end_state;
1171 //DEBUG("added TMS scan (no read)");
1174 #ifdef _DEBUG_JTAG_IO_
1175 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1178 case JTAG_STATEMOVE:
1179 /* only send the maximum buffer size that FT2232C can handle */
1181 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1183 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1184 retval = ERROR_JTAG_QUEUE_FAILED;
1188 if (cmd->cmd.statemove->end_state != -1)
1189 ft2232_end_state(cmd->cmd.statemove->end_state);
1190 /* command "Clock Data to TMS/CS Pin (no Read)" */
1195 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1196 //DEBUG("added TMS scan (no read)");
1197 cur_state = end_state;
1199 #ifdef _DEBUG_JTAG_IO_
1200 DEBUG("statemove: %i", end_state);
1204 /* only send the maximum buffer size that FT2232C can handle */
1205 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1206 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1208 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1209 retval = ERROR_JTAG_QUEUE_FAILED;
1213 ft2232_add_pathmove(cmd->cmd.pathmove);
1215 #ifdef _DEBUG_JTAG_IO_
1216 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1220 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1221 type = jtag_scan_type(cmd->cmd.scan);
1222 predicted_size = ft2232_predict_scan_out(scan_size, type);
1223 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1225 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1226 /* unsent commands before this */
1227 if (first_unsent != cmd)
1228 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1229 retval = ERROR_JTAG_QUEUE_FAILED;
1231 /* current command */
1232 if (cmd->cmd.scan->end_state != -1)
1233 ft2232_end_state(cmd->cmd.scan->end_state);
1234 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1236 first_unsent = cmd->next;
1241 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1243 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1244 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1245 retval = ERROR_JTAG_QUEUE_FAILED;
1249 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1250 //DEBUG("new read size: %i", ft2232_expect_read);
1251 if (cmd->cmd.scan->end_state != -1)
1252 ft2232_end_state(cmd->cmd.scan->end_state);
1253 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1257 #ifdef _DEBUG_JTAG_IO_
1258 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1262 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1263 retval = ERROR_JTAG_QUEUE_FAILED;
1264 first_unsent = cmd->next;
1265 jtag_sleep(cmd->cmd.sleep->us);
1266 #ifdef _DEBUG_JTAG_IO_
1267 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1271 ERROR("BUG: unknown JTAG command type encountered");
1277 if (require_send > 0)
1278 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1279 retval = ERROR_JTAG_QUEUE_FAILED;
1284 #if BUILD_FT2232_FTD2XX == 1
1285 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1288 DWORD openex_flags = 0;
1289 char *openex_string = NULL;
1292 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1293 ft2232_layout, vid, pid);
1296 /* Add non-standard Vid/Pid to the linux driver */
1297 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1299 WARNING("couldn't add %4.4x:%4.4x",
1304 if (ft2232_device_desc && ft2232_serial)
1306 WARNING("can't open by device description and serial number, giving precedence to serial");
1307 ft2232_device_desc = NULL;
1310 if (ft2232_device_desc)
1312 openex_string = ft2232_device_desc;
1313 openex_flags = FT_OPEN_BY_DESCRIPTION;
1315 else if (ft2232_serial)
1317 openex_string = ft2232_serial;
1318 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1322 ERROR("neither device description nor serial number specified");
1323 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1325 return ERROR_JTAG_INIT_FAILED;
1328 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1333 WARNING("unable to open ftdi device (trying more): %lu",
1336 return ERROR_JTAG_INIT_FAILED;
1338 ERROR("unable to open ftdi device: %lu", status);
1339 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1340 if (status == FT_OK)
1342 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1345 for (i = 0; i < num_devices; i++)
1346 desc_array[i] = malloc(64);
1347 desc_array[num_devices] = NULL;
1349 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1351 if (status == FT_OK)
1353 ERROR("ListDevices: %lu\n", num_devices);
1354 for (i = 0; i < num_devices; i++)
1355 ERROR("%i: %s", i, desc_array[i]);
1358 for (i = 0; i < num_devices; i++)
1359 free(desc_array[i]);
1364 printf("ListDevices: NONE\n");
1366 return ERROR_JTAG_INIT_FAILED;
1369 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1371 ERROR("unable to set latency timer: %lu", status);
1372 return ERROR_JTAG_INIT_FAILED;
1375 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1377 ERROR("unable to get latency timer: %lu", status);
1378 return ERROR_JTAG_INIT_FAILED;
1382 DEBUG("current latency timer: %i", latency_timer);
1385 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1387 ERROR("unable to set timeouts: %lu", status);
1388 return ERROR_JTAG_INIT_FAILED;
1391 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1393 ERROR("unable to enable bit i/o mode: %lu", status);
1394 return ERROR_JTAG_INIT_FAILED;
1400 static int ft2232_purge_ftd2xx(void)
1404 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1406 ERROR("error purging ftd2xx device: %lu", status);
1407 return ERROR_JTAG_INIT_FAILED;
1412 #endif /* BUILD_FT2232_FTD2XX == 1 */
1414 #if BUILD_FT2232_LIBFTDI == 1
1415 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1419 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1420 ft2232_layout, vid, pid);
1422 if (ftdi_init(&ftdic) < 0)
1423 return ERROR_JTAG_INIT_FAILED;
1425 /* context, vendor id, product id */
1426 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1427 ft2232_serial) < 0) {
1429 WARNING("unable to open ftdi device (trying more): %s",
1432 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1434 return ERROR_JTAG_INIT_FAILED;
1437 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1439 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1440 return ERROR_JTAG_INIT_FAILED;
1443 if (ftdi_usb_reset(&ftdic) < 0)
1445 ERROR("unable to reset ftdi device");
1446 return ERROR_JTAG_INIT_FAILED;
1449 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1451 ERROR("unable to set latency timer");
1452 return ERROR_JTAG_INIT_FAILED;
1455 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1457 ERROR("unable to get latency timer");
1458 return ERROR_JTAG_INIT_FAILED;
1462 DEBUG("current latency timer: %i", latency_timer);
1465 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1470 static int ft2232_purge_libftdi(void)
1472 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1474 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1475 return ERROR_JTAG_INIT_FAILED;
1480 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1482 int ft2232_init(void)
1487 ft2232_layout_t *cur_layout = ft2232_layouts;
1490 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1492 ft2232_layout = "usbjtag";
1493 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1496 while (cur_layout->name)
1498 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1500 layout = cur_layout;
1508 ERROR("No matching layout found for %s", ft2232_layout);
1509 return ERROR_JTAG_INIT_FAILED;
1512 for (i = 0; 1; i++) {
1514 * "more indicates that there are more IDs to try, so we should
1515 * not print an error for an ID mismatch (but for anything
1518 * try_more indicates that the error code returned indicates an
1519 * ID mismatch (and nothing else) and that we should proceeed
1520 * with the next ID pair.
1522 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1525 #if BUILD_FT2232_FTD2XX == 1
1526 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1528 #elif BUILD_FT2232_LIBFTDI == 1
1529 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1534 if (!more || !try_more)
1538 ft2232_buffer_size = 0;
1539 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1541 if (layout->init() != ERROR_OK)
1542 return ERROR_JTAG_INIT_FAILED;
1544 ft2232_speed(jtag_speed);
1546 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1547 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1549 ERROR("couldn't write to FT2232 to disable loopback");
1550 return ERROR_JTAG_INIT_FAILED;
1553 #if BUILD_FT2232_FTD2XX == 1
1554 return ft2232_purge_ftd2xx();
1555 #elif BUILD_FT2232_LIBFTDI == 1
1556 return ft2232_purge_libftdi();
1562 int usbjtag_init(void)
1568 low_direction = 0x0b;
1570 if (strcmp(ft2232_layout, "usbjtag") == 0)
1577 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1584 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1591 low_direction = 0x8b;
1595 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1596 return ERROR_JTAG_INIT_FAILED;
1599 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1601 low_direction &= ~nTRSTnOE; /* nTRST input */
1602 low_output &= ~nTRST; /* nTRST = 0 */
1606 low_direction |= nTRSTnOE; /* nTRST output */
1607 low_output |= nTRST; /* nTRST = 1 */
1610 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1612 low_direction |= nSRSTnOE; /* nSRST output */
1613 low_output |= nSRST; /* nSRST = 1 */
1617 low_direction &= ~nSRSTnOE; /* nSRST input */
1618 low_output &= ~nSRST; /* nSRST = 0 */
1621 /* initialize low byte for jtag */
1622 buf[0] = 0x80; /* command "set data bits low byte" */
1623 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1624 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1625 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1627 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1629 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1630 return ERROR_JTAG_INIT_FAILED;
1636 int jtagkey_init(void)
1642 low_direction = 0x1b;
1644 /* initialize low byte for jtag */
1645 buf[0] = 0x80; /* command "set data bits low byte" */
1646 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1647 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1648 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1650 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1652 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1653 return ERROR_JTAG_INIT_FAILED;
1656 if (strcmp(layout->name, "jtagkey") == 0)
1663 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1664 (strcmp(layout->name, "oocdlink") == 0))
1673 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1678 high_direction = 0x0f;
1680 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1682 high_output |= nTRSTnOE;
1683 high_output &= ~nTRST;
1687 high_output &= ~nTRSTnOE;
1688 high_output |= nTRST;
1691 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1693 high_output &= ~nSRSTnOE;
1694 high_output |= nSRST;
1698 high_output |= nSRSTnOE;
1699 high_output &= ~nSRST;
1702 /* initialize high port */
1703 buf[0] = 0x82; /* command "set data bits high byte" */
1704 buf[1] = high_output; /* value */
1705 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1706 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1708 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1710 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1711 return ERROR_JTAG_INIT_FAILED;
1717 int olimex_jtag_init(void)
1723 low_direction = 0x1b;
1725 /* initialize low byte for jtag */
1726 buf[0] = 0x80; /* command "set data bits low byte" */
1727 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1728 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1729 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1731 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1733 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1734 return ERROR_JTAG_INIT_FAILED;
1740 nSRSTnOE = 0x00; /* no output enable for nSRST */
1743 high_direction = 0x0f;
1745 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1747 high_output |= nTRSTnOE;
1748 high_output &= ~nTRST;
1752 high_output &= ~nTRSTnOE;
1753 high_output |= nTRST;
1756 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1758 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1762 high_output &= ~nSRST;
1765 /* turn red LED on */
1766 high_output |= 0x08;
1768 /* initialize high port */
1769 buf[0] = 0x82; /* command "set data bits high byte" */
1770 buf[1] = high_output; /* value */
1771 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1772 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1774 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1776 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1777 return ERROR_JTAG_INIT_FAILED;
1783 int m5960_init(void)
1789 low_direction = 0xfb;
1791 /* initialize low byte for jtag */
1792 buf[0] = 0x80; /* command "set data bits low byte" */
1793 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1794 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1795 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1797 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1799 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1800 return ERROR_JTAG_INIT_FAILED;
1804 nTRSTnOE = 0x0; /* not output enable for nTRST */
1806 nSRSTnOE = 0x00; /* no output enable for nSRST */
1809 high_direction = 0x0c;
1811 /* turn red LED1 on, LED2 off */
1812 high_output |= 0x08;
1814 /* initialize high port */
1815 buf[0] = 0x82; /* command "set data bits high byte" */
1816 buf[1] = high_output; /* value */
1817 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1818 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1820 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1822 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1823 return ERROR_JTAG_INIT_FAILED;
1829 int turtle_init(void)
1835 low_direction = 0x5b;
1837 /* initialize low byte for jtag */
1838 buf[0] = 0x80; /* command "set data bits low byte" */
1839 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1840 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1841 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1843 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1845 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1846 return ERROR_JTAG_INIT_FAILED;
1852 high_direction = 0x0C;
1854 /* initialize high port */
1855 buf[0] = 0x82; /* command "set data bits high byte" */
1856 buf[1] = high_output;
1857 buf[2] = high_direction;
1858 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1860 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1862 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1863 return ERROR_JTAG_INIT_FAILED;
1869 int comstick_init(void)
1875 low_direction = 0x0b;
1877 /* initialize low byte for jtag */
1878 buf[0] = 0x80; /* command "set data bits low byte" */
1879 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1880 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1881 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1883 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1885 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1886 return ERROR_JTAG_INIT_FAILED;
1890 nTRSTnOE = 0x00; /* no output enable for nTRST */
1892 nSRSTnOE = 0x00; /* no output enable for nSRST */
1895 high_direction = 0x03;
1897 /* initialize high port */
1898 buf[0] = 0x82; /* command "set data bits high byte" */
1899 buf[1] = high_output;
1900 buf[2] = high_direction;
1901 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1903 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1905 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1906 return ERROR_JTAG_INIT_FAILED;
1912 void olimex_jtag_blink(void)
1914 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1915 * ACBUS3 is bit 3 of the GPIOH port
1917 if (high_output & 0x08)
1919 /* set port pin high */
1920 high_output &= 0x07;
1924 /* set port pin low */
1925 high_output |= 0x08;
1929 BUFFER_ADD = high_output;
1930 BUFFER_ADD = high_direction;
1933 void turtle_jtag_blink(void)
1936 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
1938 if (high_output & 0x08)
1948 BUFFER_ADD = high_output;
1949 BUFFER_ADD = high_direction;
1953 int ft2232_quit(void)
1955 #if BUILD_FT2232_FTD2XX == 1
1958 status = FT_Close(ftdih);
1959 #elif BUILD_FT2232_LIBFTDI == 1
1960 ftdi_disable_bitbang(&ftdic);
1962 ftdi_usb_close(&ftdic);
1964 ftdi_deinit(&ftdic);
1967 free(ft2232_buffer);
1972 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1976 ft2232_device_desc = strdup(args[0]);
1980 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1986 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1990 ft2232_serial = strdup(args[0]);
1994 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2000 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2005 ft2232_layout = malloc(strlen(args[0]) + 1);
2006 strcpy(ft2232_layout, args[0]);
2011 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2015 if (argc > MAX_USB_IDS*2) {
2016 WARNING("ignoring extra IDs in ft2232_vid_pid "
2017 "(maximum is %d pairs)", MAX_USB_IDS);
2018 argc = MAX_USB_IDS*2;
2020 if (argc < 2 || (argc & 1))
2022 WARNING("incomplete ft2232_vid_pid configuration directive");
2027 for (i = 0; i+1 < argc; i += 2) {
2028 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2029 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2032 * Explicitly terminate, in case there are multiples instances of
2035 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;