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_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 char *ft2232_device_desc = NULL;
77 char *ft2232_layout = NULL;
78 u16 ft2232_vid = 0x0403;
79 u16 ft2232_pid = 0x6010;
81 typedef struct ft2232_layout_s
85 void(*reset)(int trst, int srst);
88 int usbjtag_init(void);
89 int jtagkey_init(void);
90 void usbjtag_reset(int trst, int srst);
91 void jtagkey_reset(int trst, int srst);
93 ft2232_layout_t ft2232_layouts[] =
95 {"usbjtag", usbjtag_init, usbjtag_reset},
96 {"jtagkey", jtagkey_init, jtagkey_reset},
97 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset},
101 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
103 static ft2232_layout_t *layout;
104 static u8 low_output = 0x0;
105 static u8 low_direction = 0x0;
106 static u8 high_output = 0x0;
107 static u8 high_direction = 0x0;
109 #if BUILD_FT2232_FTD2XX == 1
110 static FT_HANDLE ftdih = NULL;
111 #elif BUILD_FT2232_LIBFTDI == 1
112 static struct ftdi_context ftdic;
115 static u8 *ft2232_buffer = NULL;
116 static int ft2232_buffer_size = 0;
117 static int ft2232_read_pointer = 0;
118 static int ft2232_expect_read = 0;
119 #define FT2232_BUFFER_SIZE 131072
120 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
121 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
123 jtag_interface_t ft2232_interface =
128 .execute_queue = ft2232_execute_queue,
130 .support_statemove = 1,
132 .speed = ft2232_speed,
133 .register_commands = ft2232_register_commands,
138 int ft2232_write(u8 *buf, int size, u32* bytes_written)
140 #if BUILD_FT2232_FTD2XX == 1
142 DWORD dw_bytes_written;
143 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
145 *bytes_written = dw_bytes_written;
146 ERROR("FT_Write returned: %i", status);
147 return ERROR_JTAG_DEVICE_ERROR;
151 *bytes_written = dw_bytes_written;
154 #elif BUILD_FT2232_LIBFTDI == 1
156 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
159 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
160 return ERROR_JTAG_DEVICE_ERROR;
164 *bytes_written = retval;
170 int ft2232_read(u8* buf, int size, u32* bytes_read)
172 #if BUILD_FT2232_FTD2XX == 1
175 if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
177 *bytes_read = dw_bytes_read;
178 ERROR("FT_Read returned: %i", status);
179 return ERROR_JTAG_DEVICE_ERROR;
181 *bytes_read = dw_bytes_read;
184 #elif BUILD_FT2232_LIBFTDI == 1
189 while ((*bytes_read < size) && timeout--)
191 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
194 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
195 return ERROR_JTAG_DEVICE_ERROR;
197 *bytes_read += retval;
203 int ft2232_speed(int speed)
209 buf[0] = 0x86; /* command "set divisor" */
210 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
211 buf[2] = (speed >> 8) & 0xff; /* valueH */
213 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
214 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
216 ERROR("couldn't set FT2232 TCK speed");
223 int ft2232_register_commands(struct command_context_s *cmd_ctx)
225 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
226 COMMAND_CONFIG, NULL);
227 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
228 COMMAND_CONFIG, NULL);
229 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
230 COMMAND_CONFIG, NULL);
234 void ft2232_end_state(state)
236 if (tap_move_map[state] != -1)
240 ERROR("BUG: %i is not a valid end state", state);
245 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
247 int num_bytes = ((scan_size + 7) / 8);
248 int bits_left = scan_size;
251 while(num_bytes-- > 1)
253 buffer[cur_byte] = BUFFER_READ;
258 buffer[cur_byte] = 0x0;
262 buffer[cur_byte] = BUFFER_READ >> 1;
265 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
269 void ft2232_debug_dump_buffer(void)
275 for (i = 0; i < ft2232_buffer_size; i++)
277 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
289 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
299 #ifdef _DEBUG_USB_IO_
300 struct timeval start, inter, inter2, end;
301 struct timeval d_inter, d_inter2, d_end;
304 #ifdef _DEBUG_USB_COMMS_
305 DEBUG("write buffer (size %i):", ft2232_buffer_size);
306 ft2232_debug_dump_buffer();
309 #ifdef _DEBUG_USB_IO_
310 gettimeofday(&start, NULL);
313 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
315 ERROR("couldn't write MPSSE commands to FT2232");
319 #ifdef _DEBUG_USB_IO_
320 gettimeofday(&inter, NULL);
323 if (ft2232_expect_read)
326 ft2232_buffer_size = 0;
328 #ifdef _DEBUG_USB_IO_
329 gettimeofday(&inter2, NULL);
332 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
334 ERROR("couldn't read from FT2232");
338 #ifdef _DEBUG_USB_IO_
339 gettimeofday(&end, NULL);
341 timeval_subtract(&d_inter, &inter, &start);
342 timeval_subtract(&d_inter2, &inter2, &start);
343 timeval_subtract(&d_end, &end, &start);
345 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);
349 ft2232_buffer_size = bytes_read;
351 if (ft2232_expect_read != ft2232_buffer_size)
353 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
354 ft2232_debug_dump_buffer();
359 #ifdef _DEBUG_USB_COMMS_
360 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
361 ft2232_debug_dump_buffer();
365 ft2232_expect_read = 0;
366 ft2232_read_pointer = 0;
374 type = jtag_scan_type(cmd->cmd.scan);
375 if (type != SCAN_OUT)
377 scan_size = jtag_scan_size(cmd->cmd.scan);
378 buffer = calloc(CEIL(scan_size, 8), 1);
379 ft2232_read_scan(type, buffer, scan_size);
380 jtag_read_buffer(buffer, cmd->cmd.scan);
390 ft2232_buffer_size = 0;
395 void ft2232_add_pathmove(pathmove_command_t *cmd)
397 int num_states = cmd->num_states;
407 /* command "Clock Data to TMS/CS Pin (no Read)" */
409 /* number of states remaining */
410 BUFFER_ADD = (num_states % 7) - 1;
412 while (num_states % 7)
414 if (tap_transitions[cur_state].low == cmd->path[state_count])
415 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
416 else if (tap_transitions[cur_state].high == cmd->path[state_count])
417 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
420 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
424 cur_state = cmd->path[state_count];
429 BUFFER_ADD = tms_byte;
432 end_state = cur_state;
435 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
437 int num_bytes = (scan_size + 7) / 8;
438 int bits_left = scan_size;
442 if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
444 /* command "Clock Data to TMS/CS Pin (no Read)" */
451 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
456 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
459 //DEBUG("added TMS scan (no read)");
462 /* add command for complete bytes */
467 /* Clock Data Bytes In and Out LSB First */
469 //DEBUG("added TDI bytes (io %i)", num_bytes);
471 else if (type == SCAN_OUT)
473 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
475 //DEBUG("added TDI bytes (o)");
477 else if (type == SCAN_IN)
479 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
481 //DEBUG("added TDI bytes (i %i)", num_bytes);
483 BUFFER_ADD = (num_bytes-2) & 0xff;
484 BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
488 /* add complete bytes */
489 while(num_bytes-- > 1)
491 BUFFER_ADD = buffer[cur_byte];
498 bits_left -= 8 * (num_bytes - 1);
501 /* the most signifcant bit is scanned during TAP movement */
503 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
507 /* process remaining bits but the last one */
512 /* Clock Data Bits In and Out LSB First */
514 //DEBUG("added TDI bits (io) %i", bits_left - 1);
516 else if (type == SCAN_OUT)
518 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
520 //DEBUG("added TDI bits (o)");
522 else if (type == SCAN_IN)
524 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
526 //DEBUG("added TDI bits (i %i)", bits_left - 1);
528 BUFFER_ADD = bits_left - 2;
530 BUFFER_ADD = buffer[cur_byte];
533 /* move from Shift-IR/DR to end state */
534 if (type != SCAN_OUT)
536 /* Clock Data to TMS/CS Pin with Read */
538 //DEBUG("added TMS scan (read)");
542 /* Clock Data to TMS/CS Pin (no Read) */
544 //DEBUG("added TMS scan (no read)");
547 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
548 cur_state = end_state;
552 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
554 int predicted_size = 3;
556 if (cur_state != TAP_SD)
559 if (type == SCAN_IN) /* only from device to host */
562 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
563 /* remaining bits - 1 (up to 7) */
564 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
566 else /* host to device, or bidirectional */
569 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
570 /* remaining bits -1 (up to 7) */
571 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
574 return predicted_size;
577 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
579 int predicted_size = 0;
581 if (type != SCAN_OUT)
584 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
585 /* remaining bits - 1 */
586 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
587 /* last bit (from TMS scan) */
591 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
593 return predicted_size;
596 void usbjtag_reset(int trst, int srst)
601 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
602 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
604 low_output &= ~nTRST; /* switch output low */
608 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
609 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
611 low_output |= nTRST; /* switch output high */
616 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
617 low_output &= ~nSRST; /* switch output low */
619 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
623 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
624 low_output |= nSRST; /* switch output high */
626 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
629 /* command "set data bits low byte" */
631 BUFFER_ADD = low_output;
632 BUFFER_ADD = low_direction;
636 void jtagkey_reset(int trst, int srst)
641 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
642 high_output &= ~nTRSTnOE;
644 high_output &= ~nTRST;
648 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
649 high_output |= nTRSTnOE;
651 high_output |= nTRST;
656 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
657 high_output &= ~nSRST;
659 high_output &= ~nSRSTnOE;
663 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
664 high_output |= nSRST;
666 high_output |= nSRSTnOE;
669 /* command "set data bits high byte" */
671 BUFFER_ADD = high_output;
672 BUFFER_ADD = high_direction;
673 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
676 int ft2232_execute_queue()
678 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
679 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
681 int scan_size; /* size of IR or DR scan */
684 int predicted_size = 0;
685 int require_send = 0;
687 ft2232_buffer_size = 0;
688 ft2232_expect_read = 0;
695 if (cmd->cmd.end_state->end_state != -1)
696 ft2232_end_state(cmd->cmd.end_state->end_state);
699 /* only send the maximum buffer size that FT2232C can handle */
701 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
703 ft2232_send_and_recv(first_unsent, cmd);
708 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
711 #ifdef _DEBUG_JTAG_IO_
712 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
716 /* only send the maximum buffer size that FT2232C can handle */
718 if (cur_state != TAP_RTI)
720 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
721 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
723 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
725 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
727 ft2232_send_and_recv(first_unsent, cmd);
731 if (cur_state != TAP_RTI)
733 /* command "Clock Data to TMS/CS Pin (no Read)" */
738 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
742 i = cmd->cmd.runtest->num_cycles;
745 /* command "Clock Data to TMS/CS Pin (no Read)" */
748 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
752 i -= (i > 7) ? 7 : i;
753 //DEBUG("added TMS scan (no read)");
755 if (cmd->cmd.runtest->end_state != -1)
756 ft2232_end_state(cmd->cmd.runtest->end_state);
757 if (cur_state != end_state)
759 /* command "Clock Data to TMS/CS Pin (no Read)" */
764 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
765 cur_state = end_state;
766 //DEBUG("added TMS scan (no read)");
769 #ifdef _DEBUG_JTAG_IO_
770 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
774 /* only send the maximum buffer size that FT2232C can handle */
776 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
778 ft2232_send_and_recv(first_unsent, cmd);
782 if (cmd->cmd.statemove->end_state != -1)
783 ft2232_end_state(cmd->cmd.statemove->end_state);
784 /* command "Clock Data to TMS/CS Pin (no Read)" */
789 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
790 //DEBUG("added TMS scan (no read)");
791 cur_state = end_state;
793 #ifdef _DEBUG_JTAG_IO_
794 DEBUG("statemove: %i", end_state);
798 /* only send the maximum buffer size that FT2232C can handle */
799 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
800 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
802 ft2232_send_and_recv(first_unsent, cmd);
806 ft2232_add_pathmove(cmd->cmd.pathmove);
808 #ifdef _DEBUG_JTAG_IO_
809 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
813 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
814 type = jtag_scan_type(cmd->cmd.scan);
815 predicted_size = ft2232_predict_scan_out(scan_size, type);
816 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
818 DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
819 ft2232_send_and_recv(first_unsent, cmd);
823 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
824 //DEBUG("new read size: %i", ft2232_expect_read);
825 if (cmd->cmd.scan->end_state != -1)
826 ft2232_end_state(cmd->cmd.scan->end_state);
827 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
831 #ifdef _DEBUG_JTAG_IO_
832 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
836 ft2232_send_and_recv(first_unsent, cmd);
837 first_unsent = cmd->next;
838 jtag_sleep(cmd->cmd.sleep->us);
839 #ifdef _DEBUG_JTAG_IO_
840 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
844 ERROR("BUG: unknown JTAG command type encountered");
850 if (require_send > 0)
851 ft2232_send_and_recv(first_unsent, cmd);
856 int ft2232_init(void)
863 #if BUILD_FT2232_FTD2XX == 1
867 ft2232_layout_t *cur_layout = ft2232_layouts;
869 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
871 ft2232_layout = "usbjtag";
872 WARNING("No ft2232 layout specified, using default 'usbjtag'");
875 while (cur_layout->name)
877 if (strcmp(cur_layout->name, ft2232_layout) == 0)
887 ERROR("No matching layout found for %s", ft2232_layout);
888 return ERROR_JTAG_INIT_FAILED;
891 #if BUILD_FT2232_FTD2XX == 1
892 DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout);
893 #elif BUILD_FT2232_LIBFTDI == 1
894 DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout);
897 if (ft2232_device_desc == NULL)
899 WARNING("no ftd2xx device description specified, using default 'Dual RS232'");
900 ft2232_device_desc = "Dual RS232";
903 #if BUILD_FT2232_FTD2XX == 1
906 /* Add non-standard Vid/Pid to the linux driver */
907 if ((status = FT_SetVIDPID(ft2232_vid, ft2232_pid)) != FT_OK)
909 WARNING("couldn't add %4.4x:%4.4x", ft2232_vid, ft2232_pid);
913 if ((status = FT_OpenEx(ft2232_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
917 ERROR("unable to open ftdi device: %i", status);
918 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
921 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
924 for (i = 0; i < num_devices; i++)
925 desc_array[i] = malloc(64);
926 desc_array[num_devices] = NULL;
928 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
932 ERROR("ListDevices: %d\n", num_devices);
933 for (i = 0; i < num_devices; i++)
934 ERROR("%i: %s", i, desc_array[i]);
937 for (i = 0; i < num_devices; i++)
943 printf("ListDevices: NONE\n");
945 return ERROR_JTAG_INIT_FAILED;
948 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
950 ERROR("unable to set latency timer: %i", status);
951 return ERROR_JTAG_INIT_FAILED;
954 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
956 ERROR("unable to get latency timer: %i", status);
957 return ERROR_JTAG_INIT_FAILED;
961 DEBUG("current latency timer: %i", latency_timer);
964 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
966 ERROR("unable to enable bit i/o mode: %i", status);
967 return ERROR_JTAG_INIT_FAILED;
969 #elif BUILD_FT2232_LIBFTDI == 1
970 if (ftdi_init(&ftdic) < 0)
971 return ERROR_JTAG_INIT_FAILED;
973 /* context, vendor id, product id */
974 if (ftdi_usb_open(&ftdic, ft2232_vid, ft2232_pid) < 0)
976 ERROR("unable to open ftdi device: %s", ftdic.error_str);
977 return ERROR_JTAG_INIT_FAILED;
980 if (ftdi_usb_reset(&ftdic) < 0)
982 ERROR("unable to reset ftdi device");
983 return ERROR_JTAG_INIT_FAILED;
986 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
988 ERROR("unable to set latency timer");
989 return ERROR_JTAG_INIT_FAILED;
992 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
994 ERROR("unable to get latency timer");
995 return ERROR_JTAG_INIT_FAILED;
999 DEBUG("current latency timer: %i", latency_timer);
1002 ftdic.bitbang_mode = 0; /* Reset controller */
1003 ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1005 ftdic.bitbang_mode = 2; /* MPSSE mode */
1006 ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1009 ft2232_buffer_size = 0;
1010 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1012 if (layout->init() != ERROR_OK)
1013 return ERROR_JTAG_INIT_FAILED;
1015 ft2232_speed(jtag_speed);
1017 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1018 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1020 ERROR("couldn't write to FT2232 to disable loopback");
1021 return ERROR_JTAG_INIT_FAILED;
1024 #if BUILD_FT2232_FTD2XX == 1
1025 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1027 ERROR("error purging ftd2xx device: %i", status);
1028 return ERROR_JTAG_INIT_FAILED;
1030 #elif BUILD_FT2232_LIBFTDI == 1
1031 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1033 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1034 return ERROR_JTAG_INIT_FAILED;
1041 int usbjtag_init(void)
1047 low_direction = 0x0b;
1054 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1056 low_direction &= ~nTRSTnOE; /* nTRST input */
1057 low_output &= ~nTRST; /* nTRST = 0 */
1061 low_direction |= nTRSTnOE; /* nTRST output */
1062 low_output |= nTRST; /* nTRST = 1 */
1065 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1067 low_direction |= nSRSTnOE; /* nSRST output */
1068 low_output |= nSRST; /* nSRST = 1 */
1072 low_direction &= ~nSRSTnOE; /* nSRST input */
1073 low_output &= ~nSRST; /* nSRST = 0 */
1076 /* initialize low byte for jtag */
1077 buf[0] = 0x80; /* command "set data bits low byte" */
1078 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1079 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1080 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1082 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1084 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1085 return ERROR_JTAG_INIT_FAILED;
1091 int jtagkey_init(void)
1097 low_direction = 0x1b;
1099 /* initialize low byte for jtag */
1100 buf[0] = 0x80; /* command "set data bits low byte" */
1101 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1102 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1103 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1105 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1107 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1108 return ERROR_JTAG_INIT_FAILED;
1111 if (strcmp(layout->name, "jtagkey") == 0)
1118 else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
1127 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1132 high_direction = 0x0f;
1134 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1136 high_output |= nTRSTnOE;
1137 high_output &= ~nTRST;
1141 high_output &= ~nTRSTnOE;
1142 high_output |= nTRST;
1145 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1147 high_output &= ~nSRSTnOE;
1148 high_output |= nSRST;
1152 high_output |= nSRSTnOE;
1153 high_output &= ~nSRST;
1156 /* initialize high port */
1157 buf[0] = 0x82; /* command "set data bits low byte" */
1158 buf[1] = high_output; /* value */
1159 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1160 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1162 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1164 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1165 return ERROR_JTAG_INIT_FAILED;
1171 int ft2232_quit(void)
1173 #if BUILD_FT2232_FTD2XX == 1
1176 status = FT_Close(ftdih);
1177 #elif BUILD_FT2232_LIBFTDI == 1
1178 ftdi_disable_bitbang(&ftdic);
1180 ftdi_usb_close(&ftdic);
1182 ftdi_deinit(&ftdic);
1185 free(ft2232_buffer);
1190 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1194 ft2232_device_desc = strdup(args[0]);
1198 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1204 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1209 ft2232_layout = malloc(strlen(args[0]) + 1);
1210 strcpy(ft2232_layout, args[0]);
1215 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1219 ft2232_vid = strtol(args[0], NULL, 0);
1220 ft2232_pid = strtol(args[1], NULL, 0);
1224 WARNING("incomplete ft2232_vid_pid configuration directive");