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);
89 /* init procedures for supported layouts */
90 int usbjtag_init(void);
91 int jtagkey_init(void);
92 int olimex_jtag_init(void);
95 /* reset procedures for supported layouts */
96 void usbjtag_reset(int trst, int srst);
97 void jtagkey_reset(int trst, int srst);
98 void olimex_jtag_reset(int trst, int srst);
99 void m5960_reset(int trst, int srst);
101 /* blink procedures for layouts that support a blinking led */
102 void olimex_jtag_blink(void);
104 ft2232_layout_t ft2232_layouts[] =
106 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
107 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
108 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
109 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
110 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
111 {"m5960", m5960_init, m5960_reset, NULL},
115 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
117 static ft2232_layout_t *layout;
118 static u8 low_output = 0x0;
119 static u8 low_direction = 0x0;
120 static u8 high_output = 0x0;
121 static u8 high_direction = 0x0;
123 #if BUILD_FT2232_FTD2XX == 1
124 static FT_HANDLE ftdih = NULL;
125 #elif BUILD_FT2232_LIBFTDI == 1
126 static struct ftdi_context ftdic;
129 static u8 *ft2232_buffer = NULL;
130 static int ft2232_buffer_size = 0;
131 static int ft2232_read_pointer = 0;
132 static int ft2232_expect_read = 0;
133 #define FT2232_BUFFER_SIZE 131072
134 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
135 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
137 jtag_interface_t ft2232_interface =
142 .execute_queue = ft2232_execute_queue,
144 .support_pathmove = 1,
146 .speed = ft2232_speed,
147 .register_commands = ft2232_register_commands,
152 int ft2232_write(u8 *buf, int size, u32* bytes_written)
154 #if BUILD_FT2232_FTD2XX == 1
156 DWORD dw_bytes_written;
157 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
159 *bytes_written = dw_bytes_written;
160 ERROR("FT_Write returned: %i", status);
161 return ERROR_JTAG_DEVICE_ERROR;
165 *bytes_written = dw_bytes_written;
168 #elif BUILD_FT2232_LIBFTDI == 1
170 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
173 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
174 return ERROR_JTAG_DEVICE_ERROR;
178 *bytes_written = retval;
184 int ft2232_read(u8* buf, int size, u32* bytes_read)
186 #if BUILD_FT2232_FTD2XX == 1
189 if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
191 *bytes_read = dw_bytes_read;
192 ERROR("FT_Read returned: %i", status);
193 return ERROR_JTAG_DEVICE_ERROR;
195 *bytes_read = dw_bytes_read;
198 #elif BUILD_FT2232_LIBFTDI == 1
203 while ((*bytes_read < size) && timeout--)
205 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
208 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
209 return ERROR_JTAG_DEVICE_ERROR;
211 *bytes_read += retval;
217 int ft2232_speed(int speed)
223 buf[0] = 0x86; /* command "set divisor" */
224 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
225 buf[2] = (speed >> 8) & 0xff; /* valueH */
227 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
228 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
230 ERROR("couldn't set FT2232 TCK speed");
237 int ft2232_register_commands(struct command_context_s *cmd_ctx)
239 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
240 COMMAND_CONFIG, NULL);
241 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
242 COMMAND_CONFIG, NULL);
243 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
244 COMMAND_CONFIG, NULL);
248 void ft2232_end_state(state)
250 if (tap_move_map[state] != -1)
254 ERROR("BUG: %i is not a valid end state", state);
259 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
261 int num_bytes = ((scan_size + 7) / 8);
262 int bits_left = scan_size;
265 while(num_bytes-- > 1)
267 buffer[cur_byte] = BUFFER_READ;
272 buffer[cur_byte] = 0x0;
276 buffer[cur_byte] = BUFFER_READ >> 1;
279 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
283 void ft2232_debug_dump_buffer(void)
289 for (i = 0; i < ft2232_buffer_size; i++)
291 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
303 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
313 #ifdef _DEBUG_USB_IO_
314 struct timeval start, inter, inter2, end;
315 struct timeval d_inter, d_inter2, d_end;
318 #ifdef _DEBUG_USB_COMMS_
319 DEBUG("write buffer (size %i):", ft2232_buffer_size);
320 ft2232_debug_dump_buffer();
323 #ifdef _DEBUG_USB_IO_
324 gettimeofday(&start, NULL);
327 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
329 ERROR("couldn't write MPSSE commands to FT2232");
333 #ifdef _DEBUG_USB_IO_
334 gettimeofday(&inter, NULL);
337 if (ft2232_expect_read)
340 ft2232_buffer_size = 0;
342 #ifdef _DEBUG_USB_IO_
343 gettimeofday(&inter2, NULL);
346 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
348 ERROR("couldn't read from FT2232");
352 #ifdef _DEBUG_USB_IO_
353 gettimeofday(&end, NULL);
355 timeval_subtract(&d_inter, &inter, &start);
356 timeval_subtract(&d_inter2, &inter2, &start);
357 timeval_subtract(&d_end, &end, &start);
359 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);
363 ft2232_buffer_size = bytes_read;
365 if (ft2232_expect_read != ft2232_buffer_size)
367 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
368 ft2232_debug_dump_buffer();
373 #ifdef _DEBUG_USB_COMMS_
374 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
375 ft2232_debug_dump_buffer();
379 ft2232_expect_read = 0;
380 ft2232_read_pointer = 0;
388 type = jtag_scan_type(cmd->cmd.scan);
389 if (type != SCAN_OUT)
391 scan_size = jtag_scan_size(cmd->cmd.scan);
392 buffer = calloc(CEIL(scan_size, 8), 1);
393 ft2232_read_scan(type, buffer, scan_size);
394 jtag_read_buffer(buffer, cmd->cmd.scan);
404 ft2232_buffer_size = 0;
409 void ft2232_add_pathmove(pathmove_command_t *cmd)
411 int num_states = cmd->num_states;
421 /* command "Clock Data to TMS/CS Pin (no Read)" */
423 /* number of states remaining */
424 BUFFER_ADD = (num_states % 7) - 1;
426 while (num_states % 7)
428 if (tap_transitions[cur_state].low == cmd->path[state_count])
429 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
430 else if (tap_transitions[cur_state].high == cmd->path[state_count])
431 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
434 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
438 cur_state = cmd->path[state_count];
443 BUFFER_ADD = tms_byte;
446 end_state = cur_state;
449 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
451 int num_bytes = (scan_size + 7) / 8;
452 int bits_left = scan_size;
456 if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
458 /* command "Clock Data to TMS/CS Pin (no Read)" */
465 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
470 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
473 //DEBUG("added TMS scan (no read)");
476 /* add command for complete bytes */
481 /* Clock Data Bytes In and Out LSB First */
483 //DEBUG("added TDI bytes (io %i)", num_bytes);
485 else if (type == SCAN_OUT)
487 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
489 //DEBUG("added TDI bytes (o)");
491 else if (type == SCAN_IN)
493 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
495 //DEBUG("added TDI bytes (i %i)", num_bytes);
497 BUFFER_ADD = (num_bytes-2) & 0xff;
498 BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
502 /* add complete bytes */
503 while(num_bytes-- > 1)
505 BUFFER_ADD = buffer[cur_byte];
512 bits_left -= 8 * (num_bytes - 1);
515 /* the most signifcant bit is scanned during TAP movement */
517 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
521 /* process remaining bits but the last one */
526 /* Clock Data Bits In and Out LSB First */
528 //DEBUG("added TDI bits (io) %i", bits_left - 1);
530 else if (type == SCAN_OUT)
532 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
534 //DEBUG("added TDI bits (o)");
536 else if (type == SCAN_IN)
538 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
540 //DEBUG("added TDI bits (i %i)", bits_left - 1);
542 BUFFER_ADD = bits_left - 2;
544 BUFFER_ADD = buffer[cur_byte];
547 /* move from Shift-IR/DR to end state */
548 if (type != SCAN_OUT)
550 /* Clock Data to TMS/CS Pin with Read */
552 //DEBUG("added TMS scan (read)");
556 /* Clock Data to TMS/CS Pin (no Read) */
558 //DEBUG("added TMS scan (no read)");
561 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
562 cur_state = end_state;
566 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
568 int predicted_size = 3;
570 if (cur_state != TAP_SD)
573 if (type == SCAN_IN) /* only from device to host */
576 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
577 /* remaining bits - 1 (up to 7) */
578 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
580 else /* host to device, or bidirectional */
583 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
584 /* remaining bits -1 (up to 7) */
585 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
588 return predicted_size;
591 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
593 int predicted_size = 0;
595 if (type != SCAN_OUT)
598 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
599 /* remaining bits - 1 */
600 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
601 /* last bit (from TMS scan) */
605 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
607 return predicted_size;
610 void usbjtag_reset(int trst, int srst)
615 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
616 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
618 low_output &= ~nTRST; /* switch output low */
622 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
623 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
625 low_output |= nTRST; /* switch output high */
630 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
631 low_output &= ~nSRST; /* switch output low */
633 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
637 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
638 low_output |= nSRST; /* switch output high */
640 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
643 /* command "set data bits low byte" */
645 BUFFER_ADD = low_output;
646 BUFFER_ADD = low_direction;
650 void jtagkey_reset(int trst, int srst)
655 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
656 high_output &= ~nTRSTnOE;
658 high_output &= ~nTRST;
662 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
663 high_output |= nTRSTnOE;
665 high_output |= nTRST;
670 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
671 high_output &= ~nSRST;
673 high_output &= ~nSRSTnOE;
677 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
678 high_output |= nSRST;
680 high_output |= nSRSTnOE;
683 /* command "set data bits high byte" */
685 BUFFER_ADD = high_output;
686 BUFFER_ADD = high_direction;
687 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
690 void olimex_jtag_reset(int trst, int srst)
695 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
696 high_output &= ~nTRSTnOE;
698 high_output &= ~nTRST;
702 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
703 high_output |= nTRSTnOE;
705 high_output |= nTRST;
710 high_output |= nSRST;
714 high_output &= ~nSRST;
717 /* command "set data bits high byte" */
719 BUFFER_ADD = high_output;
720 BUFFER_ADD = high_direction;
721 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
724 void m5960_reset(int trst, int srst)
729 low_output &= ~nTRST;
742 low_output &= ~nSRST;
745 /* command "set data bits low byte" */
747 BUFFER_ADD = low_output;
748 BUFFER_ADD = low_direction;
749 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
752 int ft2232_execute_queue()
754 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
755 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
757 int scan_size; /* size of IR or DR scan */
760 int predicted_size = 0;
761 int require_send = 0;
763 ft2232_buffer_size = 0;
764 ft2232_expect_read = 0;
766 /* blink, if the current layout has that feature */
775 if (cmd->cmd.end_state->end_state != -1)
776 ft2232_end_state(cmd->cmd.end_state->end_state);
779 /* only send the maximum buffer size that FT2232C can handle */
781 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
783 ft2232_send_and_recv(first_unsent, cmd);
788 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
791 #ifdef _DEBUG_JTAG_IO_
792 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
796 /* only send the maximum buffer size that FT2232C can handle */
798 if (cur_state != TAP_RTI)
800 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
801 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
803 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
805 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
807 ft2232_send_and_recv(first_unsent, cmd);
811 if (cur_state != TAP_RTI)
813 /* command "Clock Data to TMS/CS Pin (no Read)" */
818 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
822 i = cmd->cmd.runtest->num_cycles;
825 /* command "Clock Data to TMS/CS Pin (no Read)" */
828 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
832 i -= (i > 7) ? 7 : i;
833 //DEBUG("added TMS scan (no read)");
835 if (cmd->cmd.runtest->end_state != -1)
836 ft2232_end_state(cmd->cmd.runtest->end_state);
837 if (cur_state != end_state)
839 /* command "Clock Data to TMS/CS Pin (no Read)" */
844 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
845 cur_state = end_state;
846 //DEBUG("added TMS scan (no read)");
849 #ifdef _DEBUG_JTAG_IO_
850 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
854 /* only send the maximum buffer size that FT2232C can handle */
856 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
858 ft2232_send_and_recv(first_unsent, cmd);
862 if (cmd->cmd.statemove->end_state != -1)
863 ft2232_end_state(cmd->cmd.statemove->end_state);
864 /* command "Clock Data to TMS/CS Pin (no Read)" */
869 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
870 //DEBUG("added TMS scan (no read)");
871 cur_state = end_state;
873 #ifdef _DEBUG_JTAG_IO_
874 DEBUG("statemove: %i", end_state);
878 /* only send the maximum buffer size that FT2232C can handle */
879 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
880 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
882 ft2232_send_and_recv(first_unsent, cmd);
886 ft2232_add_pathmove(cmd->cmd.pathmove);
888 #ifdef _DEBUG_JTAG_IO_
889 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
893 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
894 type = jtag_scan_type(cmd->cmd.scan);
895 predicted_size = ft2232_predict_scan_out(scan_size, type);
896 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
898 DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
899 ft2232_send_and_recv(first_unsent, cmd);
903 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
904 //DEBUG("new read size: %i", ft2232_expect_read);
905 if (cmd->cmd.scan->end_state != -1)
906 ft2232_end_state(cmd->cmd.scan->end_state);
907 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
911 #ifdef _DEBUG_JTAG_IO_
912 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
916 ft2232_send_and_recv(first_unsent, cmd);
917 first_unsent = cmd->next;
918 jtag_sleep(cmd->cmd.sleep->us);
919 #ifdef _DEBUG_JTAG_IO_
920 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
924 ERROR("BUG: unknown JTAG command type encountered");
930 if (require_send > 0)
931 ft2232_send_and_recv(first_unsent, cmd);
936 int ft2232_init(void)
943 #if BUILD_FT2232_FTD2XX == 1
947 ft2232_layout_t *cur_layout = ft2232_layouts;
949 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
951 ft2232_layout = "usbjtag";
952 WARNING("No ft2232 layout specified, using default 'usbjtag'");
955 while (cur_layout->name)
957 if (strcmp(cur_layout->name, ft2232_layout) == 0)
967 ERROR("No matching layout found for %s", ft2232_layout);
968 return ERROR_JTAG_INIT_FAILED;
971 #if BUILD_FT2232_FTD2XX == 1
972 DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout);
973 #elif BUILD_FT2232_LIBFTDI == 1
974 DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout);
977 #if BUILD_FT2232_FTD2XX == 1
978 /* Open by device description */
979 if (ft2232_device_desc == NULL)
981 WARNING("no ftd2xx device description specified, using default 'Dual RS232'");
982 ft2232_device_desc = "Dual RS232";
986 /* Add non-standard Vid/Pid to the linux driver */
987 if ((status = FT_SetVIDPID(ft2232_vid, ft2232_pid)) != FT_OK)
989 WARNING("couldn't add %4.4x:%4.4x", ft2232_vid, ft2232_pid);
993 if ((status = FT_OpenEx(ft2232_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
997 ERROR("unable to open ftdi device: %i", status);
998 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1001 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1004 for (i = 0; i < num_devices; i++)
1005 desc_array[i] = malloc(64);
1006 desc_array[num_devices] = NULL;
1008 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
1010 if (status == FT_OK)
1012 ERROR("ListDevices: %d\n", num_devices);
1013 for (i = 0; i < num_devices; i++)
1014 ERROR("%i: %s", i, desc_array[i]);
1017 for (i = 0; i < num_devices; i++)
1018 free(desc_array[i]);
1023 printf("ListDevices: NONE\n");
1025 return ERROR_JTAG_INIT_FAILED;
1028 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1030 ERROR("unable to set latency timer: %i", status);
1031 return ERROR_JTAG_INIT_FAILED;
1034 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1036 ERROR("unable to get latency timer: %i", status);
1037 return ERROR_JTAG_INIT_FAILED;
1041 DEBUG("current latency timer: %i", latency_timer);
1044 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1046 ERROR("unable to set timeouts: %i", status);
1047 return ERROR_JTAG_INIT_FAILED;
1050 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1052 ERROR("unable to enable bit i/o mode: %i", status);
1053 return ERROR_JTAG_INIT_FAILED;
1055 #elif BUILD_FT2232_LIBFTDI == 1
1056 if (ftdi_init(&ftdic) < 0)
1057 return ERROR_JTAG_INIT_FAILED;
1059 /* context, vendor id, product id */
1060 if (ftdi_usb_open(&ftdic, ft2232_vid, ft2232_pid) < 0)
1062 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1063 return ERROR_JTAG_INIT_FAILED;
1066 if (ftdi_usb_reset(&ftdic) < 0)
1068 ERROR("unable to reset ftdi device");
1069 return ERROR_JTAG_INIT_FAILED;
1072 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1074 ERROR("unable to set latency timer");
1075 return ERROR_JTAG_INIT_FAILED;
1078 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1080 ERROR("unable to get latency timer");
1081 return ERROR_JTAG_INIT_FAILED;
1085 DEBUG("current latency timer: %i", latency_timer);
1088 ftdic.bitbang_mode = 0; /* Reset controller */
1089 ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1091 ftdic.bitbang_mode = 2; /* MPSSE mode */
1092 ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1095 ft2232_buffer_size = 0;
1096 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1098 if (layout->init() != ERROR_OK)
1099 return ERROR_JTAG_INIT_FAILED;
1101 ft2232_speed(jtag_speed);
1103 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1104 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1106 ERROR("couldn't write to FT2232 to disable loopback");
1107 return ERROR_JTAG_INIT_FAILED;
1110 #if BUILD_FT2232_FTD2XX == 1
1111 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1113 ERROR("error purging ftd2xx device: %i", status);
1114 return ERROR_JTAG_INIT_FAILED;
1116 #elif BUILD_FT2232_LIBFTDI == 1
1117 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1119 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1120 return ERROR_JTAG_INIT_FAILED;
1127 int usbjtag_init(void)
1133 low_direction = 0x0b;
1135 if (strcmp(ft2232_layout, "usbjtag") == 0)
1142 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1151 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1152 return ERROR_JTAG_INIT_FAILED;
1155 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1157 low_direction &= ~nTRSTnOE; /* nTRST input */
1158 low_output &= ~nTRST; /* nTRST = 0 */
1162 low_direction |= nTRSTnOE; /* nTRST output */
1163 low_output |= nTRST; /* nTRST = 1 */
1166 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1168 low_direction |= nSRSTnOE; /* nSRST output */
1169 low_output |= nSRST; /* nSRST = 1 */
1173 low_direction &= ~nSRSTnOE; /* nSRST input */
1174 low_output &= ~nSRST; /* nSRST = 0 */
1177 /* initialize low byte for jtag */
1178 buf[0] = 0x80; /* command "set data bits low byte" */
1179 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1180 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1181 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1183 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1185 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1186 return ERROR_JTAG_INIT_FAILED;
1192 int jtagkey_init(void)
1198 low_direction = 0x1b;
1200 /* initialize low byte for jtag */
1201 buf[0] = 0x80; /* command "set data bits low byte" */
1202 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1203 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1204 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1206 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1208 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1209 return ERROR_JTAG_INIT_FAILED;
1212 if (strcmp(layout->name, "jtagkey") == 0)
1219 else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
1228 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1233 high_direction = 0x0f;
1235 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1237 high_output |= nTRSTnOE;
1238 high_output &= ~nTRST;
1242 high_output &= ~nTRSTnOE;
1243 high_output |= nTRST;
1246 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1248 high_output &= ~nSRSTnOE;
1249 high_output |= nSRST;
1253 high_output |= nSRSTnOE;
1254 high_output &= ~nSRST;
1257 /* initialize high port */
1258 buf[0] = 0x82; /* command "set data bits high byte" */
1259 buf[1] = high_output; /* value */
1260 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1261 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1263 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1265 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1266 return ERROR_JTAG_INIT_FAILED;
1272 int olimex_jtag_init(void)
1278 low_direction = 0x1b;
1280 /* initialize low byte for jtag */
1281 buf[0] = 0x80; /* command "set data bits low byte" */
1282 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1283 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1284 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1286 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1288 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1289 return ERROR_JTAG_INIT_FAILED;
1295 nSRSTnOE = 0x00; /* no output enable for nSRST */
1298 high_direction = 0x0f;
1300 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1302 high_output |= nTRSTnOE;
1303 high_output &= ~nTRST;
1307 high_output &= ~nTRSTnOE;
1308 high_output |= nTRST;
1311 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1313 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1317 high_output &= ~nSRST;
1320 /* turn red LED on */
1321 high_output |= 0x08;
1323 /* initialize high port */
1324 buf[0] = 0x82; /* command "set data bits high byte" */
1325 buf[1] = high_output; /* value */
1326 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1327 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1329 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1331 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1332 return ERROR_JTAG_INIT_FAILED;
1338 int m5960_init(void)
1344 low_direction = 0xfb;
1346 /* initialize low byte for jtag */
1347 buf[0] = 0x80; /* command "set data bits low byte" */
1348 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1349 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1350 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1352 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1354 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1355 return ERROR_JTAG_INIT_FAILED;
1359 nTRSTnOE = 0x0; /* not output enable for nTRST */
1361 nSRSTnOE = 0x00; /* no output enable for nSRST */
1364 high_direction = 0x0c;
1366 /* turn red LED1 on, LED2 off */
1367 high_output |= 0x08;
1369 /* initialize high port */
1370 buf[0] = 0x82; /* command "set data bits high byte" */
1371 buf[1] = high_output; /* value */
1372 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1373 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1375 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1377 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1378 return ERROR_JTAG_INIT_FAILED;
1384 void olimex_jtag_blink(void)
1386 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1387 * ACBUS3 is bit 3 of the GPIOH port
1389 if (high_output & 0x08)
1391 /* set port pin high */
1392 high_output &= 0x07;
1396 /* set port pin low */
1397 high_output |= 0x08;
1401 BUFFER_ADD = high_output;
1402 BUFFER_ADD = high_direction;
1405 int ft2232_quit(void)
1407 #if BUILD_FT2232_FTD2XX == 1
1410 status = FT_Close(ftdih);
1411 #elif BUILD_FT2232_LIBFTDI == 1
1412 ftdi_disable_bitbang(&ftdic);
1414 ftdi_usb_close(&ftdic);
1416 ftdi_deinit(&ftdic);
1419 free(ft2232_buffer);
1424 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1428 ft2232_device_desc = strdup(args[0]);
1432 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1438 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1443 ft2232_layout = malloc(strlen(args[0]) + 1);
1444 strcpy(ft2232_layout, args[0]);
1449 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1453 ft2232_vid = strtol(args[0], NULL, 0);
1454 ft2232_pid = strtol(args[1], NULL, 0);
1458 WARNING("incomplete ft2232_vid_pid configuration directive");