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;
80 u16 ft2232_vid = 0x0403;
81 u16 ft2232_pid = 0x6010;
83 typedef struct ft2232_layout_s
87 void(*reset)(int trst, int srst);
91 /* init procedures for supported layouts */
92 int usbjtag_init(void);
93 int jtagkey_init(void);
94 int olimex_jtag_init(void);
97 /* reset procedures for supported layouts */
98 void usbjtag_reset(int trst, int srst);
99 void jtagkey_reset(int trst, int srst);
100 void olimex_jtag_reset(int trst, int srst);
101 void m5960_reset(int trst, int srst);
103 /* blink procedures for layouts that support a blinking led */
104 void olimex_jtag_blink(void);
106 ft2232_layout_t ft2232_layouts[] =
108 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
109 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
110 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
111 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
112 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
113 {"m5960", m5960_init, m5960_reset, NULL},
117 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
119 static ft2232_layout_t *layout;
120 static u8 low_output = 0x0;
121 static u8 low_direction = 0x0;
122 static u8 high_output = 0x0;
123 static u8 high_direction = 0x0;
125 #if BUILD_FT2232_FTD2XX == 1
126 static FT_HANDLE ftdih = NULL;
127 #elif BUILD_FT2232_LIBFTDI == 1
128 static struct ftdi_context ftdic;
131 static u8 *ft2232_buffer = NULL;
132 static int ft2232_buffer_size = 0;
133 static int ft2232_read_pointer = 0;
134 static int ft2232_expect_read = 0;
135 #define FT2232_BUFFER_SIZE 131072
136 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
137 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
139 jtag_interface_t ft2232_interface =
144 .execute_queue = ft2232_execute_queue,
146 .support_pathmove = 1,
148 .speed = ft2232_speed,
149 .register_commands = ft2232_register_commands,
154 int ft2232_write(u8 *buf, int size, u32* bytes_written)
156 #if BUILD_FT2232_FTD2XX == 1
158 DWORD dw_bytes_written;
159 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
161 *bytes_written = dw_bytes_written;
162 ERROR("FT_Write returned: %i", status);
163 return ERROR_JTAG_DEVICE_ERROR;
167 *bytes_written = dw_bytes_written;
170 #elif BUILD_FT2232_LIBFTDI == 1
172 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
175 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
176 return ERROR_JTAG_DEVICE_ERROR;
180 *bytes_written = retval;
186 int ft2232_read(u8* buf, int size, u32* bytes_read)
188 #if BUILD_FT2232_FTD2XX == 1
191 if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
193 *bytes_read = dw_bytes_read;
194 ERROR("FT_Read returned: %i", status);
195 return ERROR_JTAG_DEVICE_ERROR;
197 *bytes_read = dw_bytes_read;
200 #elif BUILD_FT2232_LIBFTDI == 1
205 while ((*bytes_read < size) && timeout--)
207 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
210 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
211 return ERROR_JTAG_DEVICE_ERROR;
213 *bytes_read += retval;
219 int ft2232_speed(int speed)
225 buf[0] = 0x86; /* command "set divisor" */
226 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
227 buf[2] = (speed >> 8) & 0xff; /* valueH */
229 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
230 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
232 ERROR("couldn't set FT2232 TCK speed");
239 int ft2232_register_commands(struct command_context_s *cmd_ctx)
241 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
242 COMMAND_CONFIG, NULL);
243 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
244 COMMAND_CONFIG, NULL);
245 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
246 COMMAND_CONFIG, NULL);
247 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
248 COMMAND_CONFIG, NULL);
252 void ft2232_end_state(state)
254 if (tap_move_map[state] != -1)
258 ERROR("BUG: %i is not a valid end state", state);
263 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
265 int num_bytes = ((scan_size + 7) / 8);
266 int bits_left = scan_size;
269 while(num_bytes-- > 1)
271 buffer[cur_byte] = BUFFER_READ;
276 buffer[cur_byte] = 0x0;
280 buffer[cur_byte] = BUFFER_READ >> 1;
283 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
287 void ft2232_debug_dump_buffer(void)
293 for (i = 0; i < ft2232_buffer_size; i++)
295 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
307 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
317 #ifdef _DEBUG_USB_IO_
318 struct timeval start, inter, inter2, end;
319 struct timeval d_inter, d_inter2, d_end;
322 #ifdef _DEBUG_USB_COMMS_
323 DEBUG("write buffer (size %i):", ft2232_buffer_size);
324 ft2232_debug_dump_buffer();
327 #ifdef _DEBUG_USB_IO_
328 gettimeofday(&start, NULL);
331 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
333 ERROR("couldn't write MPSSE commands to FT2232");
337 #ifdef _DEBUG_USB_IO_
338 gettimeofday(&inter, NULL);
341 if (ft2232_expect_read)
344 ft2232_buffer_size = 0;
346 #ifdef _DEBUG_USB_IO_
347 gettimeofday(&inter2, NULL);
350 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
352 ERROR("couldn't read from FT2232");
356 #ifdef _DEBUG_USB_IO_
357 gettimeofday(&end, NULL);
359 timeval_subtract(&d_inter, &inter, &start);
360 timeval_subtract(&d_inter2, &inter2, &start);
361 timeval_subtract(&d_end, &end, &start);
363 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);
367 ft2232_buffer_size = bytes_read;
369 if (ft2232_expect_read != ft2232_buffer_size)
371 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
372 ft2232_debug_dump_buffer();
377 #ifdef _DEBUG_USB_COMMS_
378 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
379 ft2232_debug_dump_buffer();
383 ft2232_expect_read = 0;
384 ft2232_read_pointer = 0;
392 type = jtag_scan_type(cmd->cmd.scan);
393 if (type != SCAN_OUT)
395 scan_size = jtag_scan_size(cmd->cmd.scan);
396 buffer = calloc(CEIL(scan_size, 8), 1);
397 ft2232_read_scan(type, buffer, scan_size);
398 jtag_read_buffer(buffer, cmd->cmd.scan);
408 ft2232_buffer_size = 0;
413 void ft2232_add_pathmove(pathmove_command_t *cmd)
415 int num_states = cmd->num_states;
425 /* command "Clock Data to TMS/CS Pin (no Read)" */
427 /* number of states remaining */
428 BUFFER_ADD = (num_states % 7) - 1;
430 while (num_states % 7)
432 if (tap_transitions[cur_state].low == cmd->path[state_count])
433 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
434 else if (tap_transitions[cur_state].high == cmd->path[state_count])
435 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
438 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
442 cur_state = cmd->path[state_count];
447 BUFFER_ADD = tms_byte;
450 end_state = cur_state;
453 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
455 int num_bytes = (scan_size + 7) / 8;
456 int bits_left = scan_size;
460 if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
462 /* command "Clock Data to TMS/CS Pin (no Read)" */
469 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
474 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
477 //DEBUG("added TMS scan (no read)");
480 /* add command for complete bytes */
485 /* Clock Data Bytes In and Out LSB First */
487 //DEBUG("added TDI bytes (io %i)", num_bytes);
489 else if (type == SCAN_OUT)
491 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
493 //DEBUG("added TDI bytes (o)");
495 else if (type == SCAN_IN)
497 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
499 //DEBUG("added TDI bytes (i %i)", num_bytes);
501 BUFFER_ADD = (num_bytes-2) & 0xff;
502 BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
506 /* add complete bytes */
507 while(num_bytes-- > 1)
509 BUFFER_ADD = buffer[cur_byte];
516 bits_left -= 8 * (num_bytes - 1);
519 /* the most signifcant bit is scanned during TAP movement */
521 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
525 /* process remaining bits but the last one */
530 /* Clock Data Bits In and Out LSB First */
532 //DEBUG("added TDI bits (io) %i", bits_left - 1);
534 else if (type == SCAN_OUT)
536 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
538 //DEBUG("added TDI bits (o)");
540 else if (type == SCAN_IN)
542 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
544 //DEBUG("added TDI bits (i %i)", bits_left - 1);
546 BUFFER_ADD = bits_left - 2;
548 BUFFER_ADD = buffer[cur_byte];
551 /* move from Shift-IR/DR to end state */
552 if (type != SCAN_OUT)
554 /* Clock Data to TMS/CS Pin with Read */
556 //DEBUG("added TMS scan (read)");
560 /* Clock Data to TMS/CS Pin (no Read) */
562 //DEBUG("added TMS scan (no read)");
565 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
566 cur_state = end_state;
570 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
572 int predicted_size = 3;
574 if (cur_state != TAP_SD)
577 if (type == SCAN_IN) /* only from device to host */
580 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
581 /* remaining bits - 1 (up to 7) */
582 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
584 else /* host to device, or bidirectional */
587 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
588 /* remaining bits -1 (up to 7) */
589 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
592 return predicted_size;
595 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
597 int predicted_size = 0;
599 if (type != SCAN_OUT)
602 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
603 /* remaining bits - 1 */
604 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
605 /* last bit (from TMS scan) */
609 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
611 return predicted_size;
614 void usbjtag_reset(int trst, int srst)
619 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
620 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
622 low_output &= ~nTRST; /* switch output low */
626 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
627 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
629 low_output |= nTRST; /* switch output high */
634 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
635 low_output &= ~nSRST; /* switch output low */
637 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
641 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
642 low_output |= nSRST; /* switch output high */
644 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
647 /* command "set data bits low byte" */
649 BUFFER_ADD = low_output;
650 BUFFER_ADD = low_direction;
654 void jtagkey_reset(int trst, int srst)
659 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
660 high_output &= ~nTRSTnOE;
662 high_output &= ~nTRST;
666 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
667 high_output |= nTRSTnOE;
669 high_output |= nTRST;
674 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
675 high_output &= ~nSRST;
677 high_output &= ~nSRSTnOE;
681 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
682 high_output |= nSRST;
684 high_output |= nSRSTnOE;
687 /* command "set data bits high byte" */
689 BUFFER_ADD = high_output;
690 BUFFER_ADD = high_direction;
691 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
694 void olimex_jtag_reset(int trst, int srst)
699 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
700 high_output &= ~nTRSTnOE;
702 high_output &= ~nTRST;
706 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
707 high_output |= nTRSTnOE;
709 high_output |= nTRST;
714 high_output |= nSRST;
718 high_output &= ~nSRST;
721 /* command "set data bits high byte" */
723 BUFFER_ADD = high_output;
724 BUFFER_ADD = high_direction;
725 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
728 void m5960_reset(int trst, int srst)
733 low_output &= ~nTRST;
746 low_output &= ~nSRST;
749 /* command "set data bits low byte" */
751 BUFFER_ADD = low_output;
752 BUFFER_ADD = low_direction;
753 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
756 int ft2232_execute_queue()
758 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
759 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
761 int scan_size; /* size of IR or DR scan */
764 int predicted_size = 0;
765 int require_send = 0;
767 ft2232_buffer_size = 0;
768 ft2232_expect_read = 0;
770 /* blink, if the current layout has that feature */
779 if (cmd->cmd.end_state->end_state != -1)
780 ft2232_end_state(cmd->cmd.end_state->end_state);
783 /* only send the maximum buffer size that FT2232C can handle */
785 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
787 ft2232_send_and_recv(first_unsent, cmd);
792 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
795 #ifdef _DEBUG_JTAG_IO_
796 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
800 /* only send the maximum buffer size that FT2232C can handle */
802 if (cur_state != TAP_RTI)
804 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
805 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
807 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
809 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
811 ft2232_send_and_recv(first_unsent, cmd);
815 if (cur_state != TAP_RTI)
817 /* command "Clock Data to TMS/CS Pin (no Read)" */
822 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
826 i = cmd->cmd.runtest->num_cycles;
829 /* command "Clock Data to TMS/CS Pin (no Read)" */
832 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
836 i -= (i > 7) ? 7 : i;
837 //DEBUG("added TMS scan (no read)");
839 if (cmd->cmd.runtest->end_state != -1)
840 ft2232_end_state(cmd->cmd.runtest->end_state);
841 if (cur_state != end_state)
843 /* command "Clock Data to TMS/CS Pin (no Read)" */
848 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
849 cur_state = end_state;
850 //DEBUG("added TMS scan (no read)");
853 #ifdef _DEBUG_JTAG_IO_
854 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
858 /* only send the maximum buffer size that FT2232C can handle */
860 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
862 ft2232_send_and_recv(first_unsent, cmd);
866 if (cmd->cmd.statemove->end_state != -1)
867 ft2232_end_state(cmd->cmd.statemove->end_state);
868 /* command "Clock Data to TMS/CS Pin (no Read)" */
873 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
874 //DEBUG("added TMS scan (no read)");
875 cur_state = end_state;
877 #ifdef _DEBUG_JTAG_IO_
878 DEBUG("statemove: %i", end_state);
882 /* only send the maximum buffer size that FT2232C can handle */
883 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
884 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
886 ft2232_send_and_recv(first_unsent, cmd);
890 ft2232_add_pathmove(cmd->cmd.pathmove);
892 #ifdef _DEBUG_JTAG_IO_
893 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
897 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
898 type = jtag_scan_type(cmd->cmd.scan);
899 predicted_size = ft2232_predict_scan_out(scan_size, type);
900 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
902 DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
903 ft2232_send_and_recv(first_unsent, cmd);
907 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
908 //DEBUG("new read size: %i", ft2232_expect_read);
909 if (cmd->cmd.scan->end_state != -1)
910 ft2232_end_state(cmd->cmd.scan->end_state);
911 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
915 #ifdef _DEBUG_JTAG_IO_
916 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
920 ft2232_send_and_recv(first_unsent, cmd);
921 first_unsent = cmd->next;
922 jtag_sleep(cmd->cmd.sleep->us);
923 #ifdef _DEBUG_JTAG_IO_
924 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
928 ERROR("BUG: unknown JTAG command type encountered");
934 if (require_send > 0)
935 ft2232_send_and_recv(first_unsent, cmd);
940 int ft2232_init(void)
947 #if BUILD_FT2232_FTD2XX == 1
949 DWORD openex_flags = 0;
950 char *openex_string = NULL;
953 ft2232_layout_t *cur_layout = ft2232_layouts;
955 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
957 ft2232_layout = "usbjtag";
958 WARNING("No ft2232 layout specified, using default 'usbjtag'");
961 while (cur_layout->name)
963 if (strcmp(cur_layout->name, ft2232_layout) == 0)
973 ERROR("No matching layout found for %s", ft2232_layout);
974 return ERROR_JTAG_INIT_FAILED;
977 #if BUILD_FT2232_FTD2XX == 1
978 DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout);
979 #elif BUILD_FT2232_LIBFTDI == 1
980 DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout);
983 #if BUILD_FT2232_FTD2XX == 1
985 /* Add non-standard Vid/Pid to the linux driver */
986 if ((status = FT_SetVIDPID(ft2232_vid, ft2232_pid)) != FT_OK)
988 WARNING("couldn't add %4.4x:%4.4x", ft2232_vid, ft2232_pid);
992 if (ft2232_device_desc && ft2232_serial)
994 WARNING("can't open by device description and serial number, giving precedence to serial");
995 ft2232_device_desc = NULL;
997 else if (ft2232_device_desc)
999 openex_string = ft2232_device_desc;
1000 openex_flags = FT_OPEN_BY_DESCRIPTION;
1002 else if (ft2232_serial)
1004 openex_string = ft2232_serial;
1005 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1009 ERROR("neither device description nor serial number specified");
1010 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1012 return ERROR_JTAG_INIT_FAILED;
1015 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1019 ERROR("unable to open ftdi device: %i", status);
1020 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1021 if (status == FT_OK)
1023 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1026 for (i = 0; i < num_devices; i++)
1027 desc_array[i] = malloc(64);
1028 desc_array[num_devices] = NULL;
1030 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1032 if (status == FT_OK)
1034 ERROR("ListDevices: %d\n", num_devices);
1035 for (i = 0; i < num_devices; i++)
1036 ERROR("%i: %s", i, desc_array[i]);
1039 for (i = 0; i < num_devices; i++)
1040 free(desc_array[i]);
1045 printf("ListDevices: NONE\n");
1047 return ERROR_JTAG_INIT_FAILED;
1050 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1052 ERROR("unable to set latency timer: %i", status);
1053 return ERROR_JTAG_INIT_FAILED;
1056 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1058 ERROR("unable to get latency timer: %i", status);
1059 return ERROR_JTAG_INIT_FAILED;
1063 DEBUG("current latency timer: %i", latency_timer);
1066 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1068 ERROR("unable to set timeouts: %i", status);
1069 return ERROR_JTAG_INIT_FAILED;
1072 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1074 ERROR("unable to enable bit i/o mode: %i", status);
1075 return ERROR_JTAG_INIT_FAILED;
1077 #elif BUILD_FT2232_LIBFTDI == 1
1078 if (ftdi_init(&ftdic) < 0)
1079 return ERROR_JTAG_INIT_FAILED;
1081 /* context, vendor id, product id */
1082 if (ftdi_usb_open(&ftdic, ft2232_vid, ft2232_pid) < 0)
1084 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1085 return ERROR_JTAG_INIT_FAILED;
1088 if (ftdi_usb_reset(&ftdic) < 0)
1090 ERROR("unable to reset ftdi device");
1091 return ERROR_JTAG_INIT_FAILED;
1094 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1096 ERROR("unable to set latency timer");
1097 return ERROR_JTAG_INIT_FAILED;
1100 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1102 ERROR("unable to get latency timer");
1103 return ERROR_JTAG_INIT_FAILED;
1107 DEBUG("current latency timer: %i", latency_timer);
1110 ftdic.bitbang_mode = 0; /* Reset controller */
1111 ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1113 ftdic.bitbang_mode = 2; /* MPSSE mode */
1114 ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1117 ft2232_buffer_size = 0;
1118 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1120 if (layout->init() != ERROR_OK)
1121 return ERROR_JTAG_INIT_FAILED;
1123 ft2232_speed(jtag_speed);
1125 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1126 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1128 ERROR("couldn't write to FT2232 to disable loopback");
1129 return ERROR_JTAG_INIT_FAILED;
1132 #if BUILD_FT2232_FTD2XX == 1
1133 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1135 ERROR("error purging ftd2xx device: %i", status);
1136 return ERROR_JTAG_INIT_FAILED;
1138 #elif BUILD_FT2232_LIBFTDI == 1
1139 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1141 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1142 return ERROR_JTAG_INIT_FAILED;
1149 int usbjtag_init(void)
1155 low_direction = 0x0b;
1157 if (strcmp(ft2232_layout, "usbjtag") == 0)
1164 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1173 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1174 return ERROR_JTAG_INIT_FAILED;
1177 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1179 low_direction &= ~nTRSTnOE; /* nTRST input */
1180 low_output &= ~nTRST; /* nTRST = 0 */
1184 low_direction |= nTRSTnOE; /* nTRST output */
1185 low_output |= nTRST; /* nTRST = 1 */
1188 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1190 low_direction |= nSRSTnOE; /* nSRST output */
1191 low_output |= nSRST; /* nSRST = 1 */
1195 low_direction &= ~nSRSTnOE; /* nSRST input */
1196 low_output &= ~nSRST; /* nSRST = 0 */
1199 /* initialize low byte for jtag */
1200 buf[0] = 0x80; /* command "set data bits low byte" */
1201 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1202 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1203 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1205 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1207 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1208 return ERROR_JTAG_INIT_FAILED;
1214 int jtagkey_init(void)
1220 low_direction = 0x1b;
1222 /* initialize low byte for jtag */
1223 buf[0] = 0x80; /* command "set data bits low byte" */
1224 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1225 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1226 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1228 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1230 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1231 return ERROR_JTAG_INIT_FAILED;
1234 if (strcmp(layout->name, "jtagkey") == 0)
1241 else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
1250 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1255 high_direction = 0x0f;
1257 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1259 high_output |= nTRSTnOE;
1260 high_output &= ~nTRST;
1264 high_output &= ~nTRSTnOE;
1265 high_output |= nTRST;
1268 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1270 high_output &= ~nSRSTnOE;
1271 high_output |= nSRST;
1275 high_output |= nSRSTnOE;
1276 high_output &= ~nSRST;
1279 /* initialize high port */
1280 buf[0] = 0x82; /* command "set data bits high byte" */
1281 buf[1] = high_output; /* value */
1282 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1283 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1285 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1287 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1288 return ERROR_JTAG_INIT_FAILED;
1294 int olimex_jtag_init(void)
1300 low_direction = 0x1b;
1302 /* initialize low byte for jtag */
1303 buf[0] = 0x80; /* command "set data bits low byte" */
1304 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1305 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1306 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1308 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1310 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1311 return ERROR_JTAG_INIT_FAILED;
1317 nSRSTnOE = 0x00; /* no output enable for nSRST */
1320 high_direction = 0x0f;
1322 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1324 high_output |= nTRSTnOE;
1325 high_output &= ~nTRST;
1329 high_output &= ~nTRSTnOE;
1330 high_output |= nTRST;
1333 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1335 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1339 high_output &= ~nSRST;
1342 /* turn red LED on */
1343 high_output |= 0x08;
1345 /* initialize high port */
1346 buf[0] = 0x82; /* command "set data bits high byte" */
1347 buf[1] = high_output; /* value */
1348 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1349 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1351 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1353 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1354 return ERROR_JTAG_INIT_FAILED;
1360 int m5960_init(void)
1366 low_direction = 0xfb;
1368 /* initialize low byte for jtag */
1369 buf[0] = 0x80; /* command "set data bits low byte" */
1370 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1371 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1372 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1374 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1376 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1377 return ERROR_JTAG_INIT_FAILED;
1381 nTRSTnOE = 0x0; /* not output enable for nTRST */
1383 nSRSTnOE = 0x00; /* no output enable for nSRST */
1386 high_direction = 0x0c;
1388 /* turn red LED1 on, LED2 off */
1389 high_output |= 0x08;
1391 /* initialize high port */
1392 buf[0] = 0x82; /* command "set data bits high byte" */
1393 buf[1] = high_output; /* value */
1394 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1395 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1397 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1399 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1400 return ERROR_JTAG_INIT_FAILED;
1406 void olimex_jtag_blink(void)
1408 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1409 * ACBUS3 is bit 3 of the GPIOH port
1411 if (high_output & 0x08)
1413 /* set port pin high */
1414 high_output &= 0x07;
1418 /* set port pin low */
1419 high_output |= 0x08;
1423 BUFFER_ADD = high_output;
1424 BUFFER_ADD = high_direction;
1427 int ft2232_quit(void)
1429 #if BUILD_FT2232_FTD2XX == 1
1432 status = FT_Close(ftdih);
1433 #elif BUILD_FT2232_LIBFTDI == 1
1434 ftdi_disable_bitbang(&ftdic);
1436 ftdi_usb_close(&ftdic);
1438 ftdi_deinit(&ftdic);
1441 free(ft2232_buffer);
1446 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1450 ft2232_device_desc = strdup(args[0]);
1454 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1460 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1464 ft2232_serial = strdup(args[0]);
1468 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
1474 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1479 ft2232_layout = malloc(strlen(args[0]) + 1);
1480 strcpy(ft2232_layout, args[0]);
1485 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1489 ft2232_vid = strtol(args[0], NULL, 0);
1490 ft2232_pid = strtol(args[1], NULL, 0);
1494 WARNING("incomplete ft2232_vid_pid configuration directive");