1 /***************************************************************************
\r
2 * Copyright (C) 2004, 2006 by Dominic Rath *
\r
3 * Dominic.Rath@gmx.de *
\r
5 * This program is free software; you can redistribute it and/or modify *
\r
6 * it under the terms of the GNU General Public License as published by *
\r
7 * the Free Software Foundation; either version 2 of the License, or *
\r
8 * (at your option) any later version. *
\r
10 * This program is distributed in the hope that it will be useful, *
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
13 * GNU General Public License for more details. *
\r
15 * You should have received a copy of the GNU General Public License *
\r
16 * along with this program; if not, write to the *
\r
17 * Free Software Foundation, Inc., *
\r
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
19 ***************************************************************************/
\r
20 #ifdef HAVE_CONFIG_H
\r
25 #include "windows.h"
\r
29 #include "replacements.h"
\r
31 /* project specific includes */
\r
35 #include "configuration.h"
\r
36 #include "time_support.h"
\r
38 /* system includes */
\r
43 /* FT2232 access library includes */
\r
44 #if BUILD_FT2232_FTD2XX == 1
\r
46 #elif BUILD_FT2232_LIBFTDI == 1
\r
50 #include <sys/time.h>
\r
53 /* enable this to debug io latency
\r
56 #define _DEBUG_USB_IO_
\r
59 /* enable this to debug communication
\r
62 #define _DEBUG_USB_COMMS_
\r
65 int ft2232_execute_queue(void);
\r
67 int ft2232_speed(int speed);
\r
68 int ft2232_register_commands(struct command_context_s *cmd_ctx);
\r
69 int ft2232_init(void);
\r
70 int ft2232_quit(void);
\r
72 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
73 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
74 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
75 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
76 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
78 char *ft2232_device_desc = NULL;
\r
79 char *ft2232_serial = NULL;
\r
80 char *ft2232_layout = NULL;
\r
81 unsigned char ft2232_latency = 2;
\r
83 #define MAX_USB_IDS 8
\r
84 /* vid = pid = 0 marks the end of the list */
\r
85 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
\r
86 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
\r
88 typedef struct ft2232_layout_s
\r
92 void(*reset)(int trst, int srst);
\r
96 /* init procedures for supported layouts */
\r
97 int usbjtag_init(void);
\r
98 int jtagkey_init(void);
\r
99 int olimex_jtag_init(void);
\r
100 int flyswatter_init(void);
\r
101 int turtle_init(void);
\r
102 int comstick_init(void);
\r
103 int stm32stick_init(void);
\r
105 /* reset procedures for supported layouts */
\r
106 void usbjtag_reset(int trst, int srst);
\r
107 void jtagkey_reset(int trst, int srst);
\r
108 void olimex_jtag_reset(int trst, int srst);
\r
109 void flyswatter_reset(int trst, int srst);
\r
110 void turtle_reset(int trst, int srst);
\r
111 void comstick_reset(int trst, int srst);
\r
112 void stm32stick_reset(int trst, int srst);
\r
114 /* blink procedures for layouts that support a blinking led */
\r
115 void olimex_jtag_blink(void);
\r
116 void turtle_jtag_blink(void);
\r
118 ft2232_layout_t ft2232_layouts[] =
\r
120 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
\r
121 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
\r
122 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
\r
123 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
\r
124 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
\r
125 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
\r
126 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
\r
127 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
\r
128 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
\r
129 {"comstick", comstick_init, comstick_reset, NULL},
\r
130 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
\r
131 {NULL, NULL, NULL},
\r
134 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
\r
136 static ft2232_layout_t *layout;
\r
137 static u8 low_output = 0x0;
\r
138 static u8 low_direction = 0x0;
\r
139 static u8 high_output = 0x0;
\r
140 static u8 high_direction = 0x0;
\r
142 #if BUILD_FT2232_FTD2XX == 1
\r
143 static FT_HANDLE ftdih = NULL;
\r
144 #elif BUILD_FT2232_LIBFTDI == 1
\r
145 static struct ftdi_context ftdic;
\r
148 static u8 *ft2232_buffer = NULL;
\r
149 static int ft2232_buffer_size = 0;
\r
150 static int ft2232_read_pointer = 0;
\r
151 static int ft2232_expect_read = 0;
\r
152 #define FT2232_BUFFER_SIZE 131072
\r
153 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
\r
154 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
\r
156 jtag_interface_t ft2232_interface =
\r
161 .execute_queue = ft2232_execute_queue,
\r
163 .speed = ft2232_speed,
\r
164 .register_commands = ft2232_register_commands,
\r
165 .init = ft2232_init,
\r
166 .quit = ft2232_quit,
\r
169 int ft2232_write(u8 *buf, int size, u32* bytes_written)
\r
171 #if BUILD_FT2232_FTD2XX == 1
\r
173 DWORD dw_bytes_written;
\r
174 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
\r
176 *bytes_written = dw_bytes_written;
\r
177 ERROR("FT_Write returned: %lu", status);
\r
178 return ERROR_JTAG_DEVICE_ERROR;
\r
182 *bytes_written = dw_bytes_written;
\r
185 #elif BUILD_FT2232_LIBFTDI == 1
\r
187 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
\r
189 *bytes_written = 0;
\r
190 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
\r
191 return ERROR_JTAG_DEVICE_ERROR;
\r
195 *bytes_written = retval;
\r
201 int ft2232_read(u8* buf, int size, u32* bytes_read)
\r
203 #if BUILD_FT2232_FTD2XX == 1
\r
204 DWORD dw_bytes_read;
\r
209 while ((*bytes_read < size) && timeout--)
\r
211 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
\r
212 *bytes_read, &dw_bytes_read)) != FT_OK)
\r
215 ERROR("FT_Read returned: %lu", status);
\r
216 return ERROR_JTAG_DEVICE_ERROR;
\r
218 *bytes_read += dw_bytes_read;
\r
220 #elif BUILD_FT2232_LIBFTDI == 1
\r
225 while ((*bytes_read < size) && timeout--)
\r
227 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
\r
230 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
\r
231 return ERROR_JTAG_DEVICE_ERROR;
\r
233 *bytes_read += retval;
\r
237 if (*bytes_read < size)
\r
239 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
\r
240 return ERROR_JTAG_DEVICE_ERROR;
\r
246 int ft2232_speed(int speed)
\r
252 buf[0] = 0x86; /* command "set divisor" */
\r
253 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
\r
254 buf[2] = (speed >> 8) & 0xff; /* valueH */
\r
256 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
257 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
259 ERROR("couldn't set FT2232 TCK speed");
\r
263 jtag_speed = speed;
\r
268 int ft2232_register_commands(struct command_context_s *cmd_ctx)
\r
270 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
\r
271 COMMAND_CONFIG, NULL);
\r
272 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
\r
273 COMMAND_CONFIG, NULL);
\r
274 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
\r
275 COMMAND_CONFIG, NULL);
\r
276 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
\r
277 COMMAND_CONFIG, NULL);
\r
278 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
\r
279 COMMAND_CONFIG, NULL);
\r
283 void ft2232_end_state(enum tap_state state)
\r
285 if (tap_move_map[state] != -1)
\r
289 ERROR("BUG: %i is not a valid end state", state);
\r
294 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
\r
296 int num_bytes = ((scan_size + 7) / 8);
\r
297 int bits_left = scan_size;
\r
300 while(num_bytes-- > 1)
\r
302 buffer[cur_byte] = BUFFER_READ;
\r
307 buffer[cur_byte] = 0x0;
\r
311 buffer[cur_byte] = BUFFER_READ >> 1;
\r
314 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
\r
318 void ft2232_debug_dump_buffer(void)
\r
322 char *line_p = line;
\r
324 for (i = 0; i < ft2232_buffer_size; i++)
\r
326 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
\r
334 if (line_p != line)
\r
338 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
\r
340 jtag_command_t *cmd;
\r
343 enum scan_type type;
\r
348 #ifdef _DEBUG_USB_IO_
\r
349 struct timeval start, inter, inter2, end;
\r
350 struct timeval d_inter, d_inter2, d_end;
\r
353 #ifdef _DEBUG_USB_COMMS_
\r
354 DEBUG("write buffer (size %i):", ft2232_buffer_size);
\r
355 ft2232_debug_dump_buffer();
\r
358 #ifdef _DEBUG_USB_IO_
\r
359 gettimeofday(&start, NULL);
\r
362 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
\r
364 ERROR("couldn't write MPSSE commands to FT2232");
\r
368 #ifdef _DEBUG_USB_IO_
\r
369 gettimeofday(&inter, NULL);
\r
372 if (ft2232_expect_read)
\r
375 ft2232_buffer_size = 0;
\r
377 #ifdef _DEBUG_USB_IO_
\r
378 gettimeofday(&inter2, NULL);
\r
381 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
\r
383 ERROR("couldn't read from FT2232");
\r
387 #ifdef _DEBUG_USB_IO_
\r
388 gettimeofday(&end, NULL);
\r
390 timeval_subtract(&d_inter, &inter, &start);
\r
391 timeval_subtract(&d_inter2, &inter2, &start);
\r
392 timeval_subtract(&d_end, &end, &start);
\r
394 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);
\r
398 ft2232_buffer_size = bytes_read;
\r
400 if (ft2232_expect_read != ft2232_buffer_size)
\r
402 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
\r
403 ft2232_debug_dump_buffer();
\r
408 #ifdef _DEBUG_USB_COMMS_
\r
409 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
\r
410 ft2232_debug_dump_buffer();
\r
414 ft2232_expect_read = 0;
\r
415 ft2232_read_pointer = 0;
\r
417 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
\r
418 * that wasn't handled by a caller-provided error handler
\r
423 while (cmd != last)
\r
428 type = jtag_scan_type(cmd->cmd.scan);
\r
429 if (type != SCAN_OUT)
\r
431 scan_size = jtag_scan_size(cmd->cmd.scan);
\r
432 buffer = calloc(CEIL(scan_size, 8), 1);
\r
433 ft2232_read_scan(type, buffer, scan_size);
\r
434 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
\r
435 retval = ERROR_JTAG_QUEUE_FAILED;
\r
445 ft2232_buffer_size = 0;
\r
450 void ft2232_add_pathmove(pathmove_command_t *cmd)
\r
452 int num_states = cmd->num_states;
\r
462 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
464 /* number of states remaining */
\r
465 BUFFER_ADD = (num_states % 7) - 1;
\r
467 while (num_states % 7)
\r
469 if (tap_transitions[cur_state].low == cmd->path[state_count])
\r
470 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
\r
471 else if (tap_transitions[cur_state].high == cmd->path[state_count])
\r
472 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
\r
475 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
\r
479 cur_state = cmd->path[state_count];
\r
484 BUFFER_ADD = tms_byte;
\r
487 end_state = cur_state;
\r
490 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
\r
492 int num_bytes = (scan_size + 7) / 8;
\r
493 int bits_left = scan_size;
\r
497 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
\r
499 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
503 /* TMS data bits */
\r
506 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
\r
507 cur_state = TAP_SI;
\r
511 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
\r
512 cur_state = TAP_SD;
\r
514 //DEBUG("added TMS scan (no read)");
\r
517 /* add command for complete bytes */
\r
518 while (num_bytes > 1)
\r
521 if (type == SCAN_IO)
\r
523 /* Clock Data Bytes In and Out LSB First */
\r
525 //DEBUG("added TDI bytes (io %i)", num_bytes);
\r
527 else if (type == SCAN_OUT)
\r
529 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
\r
531 //DEBUG("added TDI bytes (o)");
\r
533 else if (type == SCAN_IN)
\r
535 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
\r
537 //DEBUG("added TDI bytes (i %i)", num_bytes);
\r
539 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
\r
540 num_bytes -= thisrun_bytes;
\r
541 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
\r
542 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
\r
543 if (type != SCAN_IN)
\r
545 /* add complete bytes */
\r
546 while(thisrun_bytes-- > 0)
\r
548 BUFFER_ADD = buffer[cur_byte];
\r
553 else /* (type == SCAN_IN) */
\r
555 bits_left -= 8 * (thisrun_bytes);
\r
559 /* the most signifcant bit is scanned during TAP movement */
\r
560 if (type != SCAN_IN)
\r
561 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
\r
565 /* process remaining bits but the last one */
\r
568 if (type == SCAN_IO)
\r
570 /* Clock Data Bits In and Out LSB First */
\r
572 //DEBUG("added TDI bits (io) %i", bits_left - 1);
\r
574 else if (type == SCAN_OUT)
\r
576 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
\r
578 //DEBUG("added TDI bits (o)");
\r
580 else if (type == SCAN_IN)
\r
582 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
\r
584 //DEBUG("added TDI bits (i %i)", bits_left - 1);
\r
586 BUFFER_ADD = bits_left - 2;
\r
587 if (type != SCAN_IN)
\r
588 BUFFER_ADD = buffer[cur_byte];
\r
591 if ((ir_scan && (end_state == TAP_SI)) ||
\r
592 (!ir_scan && (end_state == TAP_SD)))
\r
594 if (type == SCAN_IO)
\r
596 /* Clock Data Bits In and Out LSB First */
\r
598 //DEBUG("added TDI bits (io) %i", bits_left - 1);
\r
600 else if (type == SCAN_OUT)
\r
602 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
\r
604 //DEBUG("added TDI bits (o)");
\r
606 else if (type == SCAN_IN)
\r
608 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
\r
610 //DEBUG("added TDI bits (i %i)", bits_left - 1);
\r
613 BUFFER_ADD = last_bit;
\r
617 /* move from Shift-IR/DR to end state */
\r
618 if (type != SCAN_OUT)
\r
620 /* Clock Data to TMS/CS Pin with Read */
\r
622 //DEBUG("added TMS scan (read)");
\r
626 /* Clock Data to TMS/CS Pin (no Read) */
\r
628 //DEBUG("added TMS scan (no read)");
\r
631 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
\r
632 cur_state = end_state;
\r
636 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
\r
638 int num_bytes = (scan_size + 7) / 8;
\r
639 int bits_left = scan_size;
\r
642 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
\r
643 u8 *receive_pointer = receive_buffer;
\r
647 int thisrun_read = 0;
\r
651 ERROR("BUG: large IR scans are not supported");
\r
655 if (cur_state != TAP_SD)
\r
657 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
661 /* TMS data bits */
\r
662 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
\r
663 cur_state = TAP_SD;
\r
666 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
\r
668 ERROR("couldn't write MPSSE commands to FT2232");
\r
671 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
\r
672 ft2232_buffer_size = 0;
\r
674 /* add command for complete bytes */
\r
675 while (num_bytes > 1)
\r
679 if (type == SCAN_IO)
\r
681 /* Clock Data Bytes In and Out LSB First */
\r
683 //DEBUG("added TDI bytes (io %i)", num_bytes);
\r
685 else if (type == SCAN_OUT)
\r
687 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
\r
689 //DEBUG("added TDI bytes (o)");
\r
691 else if (type == SCAN_IN)
\r
693 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
\r
695 //DEBUG("added TDI bytes (i %i)", num_bytes);
\r
697 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
\r
698 thisrun_read = thisrun_bytes;
\r
699 num_bytes -= thisrun_bytes;
\r
700 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
\r
701 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
\r
702 if (type != SCAN_IN)
\r
704 /* add complete bytes */
\r
705 while(thisrun_bytes-- > 0)
\r
707 BUFFER_ADD = buffer[cur_byte];
\r
712 else /* (type == SCAN_IN) */
\r
714 bits_left -= 8 * (thisrun_bytes);
\r
717 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
\r
719 ERROR("couldn't write MPSSE commands to FT2232");
\r
722 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
\r
723 ft2232_buffer_size = 0;
\r
725 if (type != SCAN_OUT)
\r
727 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
\r
729 ERROR("couldn't read from FT2232");
\r
732 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
\r
733 receive_pointer += bytes_read;
\r
739 /* the most signifcant bit is scanned during TAP movement */
\r
740 if (type != SCAN_IN)
\r
741 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
\r
745 /* process remaining bits but the last one */
\r
748 if (type == SCAN_IO)
\r
750 /* Clock Data Bits In and Out LSB First */
\r
752 //DEBUG("added TDI bits (io) %i", bits_left - 1);
\r
754 else if (type == SCAN_OUT)
\r
756 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
\r
758 //DEBUG("added TDI bits (o)");
\r
760 else if (type == SCAN_IN)
\r
762 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
\r
764 //DEBUG("added TDI bits (i %i)", bits_left - 1);
\r
766 BUFFER_ADD = bits_left - 2;
\r
767 if (type != SCAN_IN)
\r
768 BUFFER_ADD = buffer[cur_byte];
\r
770 if (type != SCAN_OUT)
\r
774 if (end_state == TAP_SD)
\r
776 if (type == SCAN_IO)
\r
778 /* Clock Data Bits In and Out LSB First */
\r
780 //DEBUG("added TDI bits (io) %i", bits_left - 1);
\r
782 else if (type == SCAN_OUT)
\r
784 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
\r
786 //DEBUG("added TDI bits (o)");
\r
788 else if (type == SCAN_IN)
\r
790 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
\r
792 //DEBUG("added TDI bits (i %i)", bits_left - 1);
\r
795 BUFFER_ADD = last_bit;
\r
799 /* move from Shift-IR/DR to end state */
\r
800 if (type != SCAN_OUT)
\r
802 /* Clock Data to TMS/CS Pin with Read */
\r
804 //DEBUG("added TMS scan (read)");
\r
808 /* Clock Data to TMS/CS Pin (no Read) */
\r
810 //DEBUG("added TMS scan (no read)");
\r
813 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
\r
814 cur_state = end_state;
\r
817 if (type != SCAN_OUT)
\r
820 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
\r
822 ERROR("couldn't write MPSSE commands to FT2232");
\r
825 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
\r
826 ft2232_buffer_size = 0;
\r
828 if (type != SCAN_OUT)
\r
830 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
\r
832 ERROR("couldn't read from FT2232");
\r
835 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
\r
836 receive_pointer += bytes_read;
\r
842 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
\r
844 int predicted_size = 3;
\r
845 int num_bytes = (scan_size - 1) / 8;
\r
847 if (cur_state != TAP_SD)
\r
848 predicted_size += 3;
\r
850 if (type == SCAN_IN) /* only from device to host */
\r
852 /* complete bytes */
\r
853 predicted_size += (CEIL(num_bytes, 65536)) * 3;
\r
854 /* remaining bits - 1 (up to 7) */
\r
855 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
\r
857 else /* host to device, or bidirectional */
\r
859 /* complete bytes */
\r
860 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
\r
861 /* remaining bits -1 (up to 7) */
\r
862 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
\r
865 return predicted_size;
\r
868 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
\r
870 int predicted_size = 0;
\r
872 if (type != SCAN_OUT)
\r
874 /* complete bytes */
\r
875 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
\r
876 /* remaining bits - 1 */
\r
877 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
\r
878 /* last bit (from TMS scan) */
\r
879 predicted_size += 1;
\r
882 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
\r
884 return predicted_size;
\r
887 void usbjtag_reset(int trst, int srst)
\r
891 cur_state = TAP_TLR;
\r
892 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
893 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
\r
895 low_output &= ~nTRST; /* switch output low */
\r
897 else if (trst == 0)
\r
899 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
900 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
\r
902 low_output |= nTRST; /* switch output high */
\r
907 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
908 low_output &= ~nSRST; /* switch output low */
\r
910 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
\r
912 else if (srst == 0)
\r
914 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
915 low_output |= nSRST; /* switch output high */
\r
917 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
\r
920 /* command "set data bits low byte" */
\r
922 BUFFER_ADD = low_output;
\r
923 BUFFER_ADD = low_direction;
\r
927 void jtagkey_reset(int trst, int srst)
\r
931 cur_state = TAP_TLR;
\r
932 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
933 high_output &= ~nTRSTnOE;
\r
935 high_output &= ~nTRST;
\r
937 else if (trst == 0)
\r
939 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
940 high_output |= nTRSTnOE;
\r
942 high_output |= nTRST;
\r
947 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
948 high_output &= ~nSRST;
\r
950 high_output &= ~nSRSTnOE;
\r
952 else if (srst == 0)
\r
954 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
955 high_output |= nSRST;
\r
957 high_output |= nSRSTnOE;
\r
960 /* command "set data bits high byte" */
\r
962 BUFFER_ADD = high_output;
\r
963 BUFFER_ADD = high_direction;
\r
964 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
\r
967 void olimex_jtag_reset(int trst, int srst)
\r
971 cur_state = TAP_TLR;
\r
972 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
973 high_output &= ~nTRSTnOE;
\r
975 high_output &= ~nTRST;
\r
977 else if (trst == 0)
\r
979 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
980 high_output |= nTRSTnOE;
\r
982 high_output |= nTRST;
\r
987 high_output |= nSRST;
\r
989 else if (srst == 0)
\r
991 high_output &= ~nSRST;
\r
994 /* command "set data bits high byte" */
\r
996 BUFFER_ADD = high_output;
\r
997 BUFFER_ADD = high_direction;
\r
998 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
\r
1001 void flyswatter_reset(int trst, int srst)
\r
1005 cur_state = TAP_TLR;
\r
1006 low_output &= ~nTRST;
\r
1008 else if (trst == 0)
\r
1010 low_output |= nTRST;
\r
1015 low_output |= nSRST;
\r
1017 else if (srst == 0)
\r
1019 low_output &= ~nSRST;
\r
1022 /* command "set data bits low byte" */
\r
1023 BUFFER_ADD = 0x80;
\r
1024 BUFFER_ADD = low_output;
\r
1025 BUFFER_ADD = low_direction;
\r
1026 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
\r
1029 void turtle_reset(int trst, int srst)
\r
1035 low_output |= nSRST;
\r
1037 else if (srst == 0)
\r
1039 low_output &= ~nSRST;
\r
1042 /* command "set data bits low byte" */
\r
1043 BUFFER_ADD = 0x80;
\r
1044 BUFFER_ADD = low_output;
\r
1045 BUFFER_ADD = low_direction;
\r
1046 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
\r
1049 void comstick_reset(int trst, int srst)
\r
1053 cur_state = TAP_TLR;
\r
1054 high_output &= ~nTRST;
\r
1056 else if (trst == 0)
\r
1058 high_output |= nTRST;
\r
1063 high_output &= ~nSRST;
\r
1065 else if (srst == 0)
\r
1067 high_output |= nSRST;
\r
1070 /* command "set data bits high byte" */
\r
1071 BUFFER_ADD = 0x82;
\r
1072 BUFFER_ADD = high_output;
\r
1073 BUFFER_ADD = high_direction;
\r
1074 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
\r
1077 void stm32stick_reset(int trst, int srst)
\r
1081 cur_state = TAP_TLR;
\r
1082 high_output &= ~nTRST;
\r
1084 else if (trst == 0)
\r
1086 high_output |= nTRST;
\r
1091 low_output &= ~nSRST;
\r
1093 else if (srst == 0)
\r
1095 low_output |= nSRST;
\r
1098 /* command "set data bits low byte" */
\r
1099 BUFFER_ADD = 0x80;
\r
1100 BUFFER_ADD = low_output;
\r
1101 BUFFER_ADD = low_direction;
\r
1103 /* command "set data bits high byte" */
\r
1104 BUFFER_ADD = 0x82;
\r
1105 BUFFER_ADD = high_output;
\r
1106 BUFFER_ADD = high_direction;
\r
1107 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
\r
1110 int ft2232_execute_queue()
\r
1112 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
\r
1113 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
\r
1115 int scan_size; /* size of IR or DR scan */
\r
1116 enum scan_type type;
\r
1118 int predicted_size = 0;
\r
1119 int require_send = 0;
\r
1122 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
\r
1123 * that wasn't handled by a caller-provided error handler
\r
1125 retval = ERROR_OK;
\r
1127 ft2232_buffer_size = 0;
\r
1128 ft2232_expect_read = 0;
\r
1130 /* blink, if the current layout has that feature */
\r
1131 if (layout->blink)
\r
1138 case JTAG_END_STATE:
\r
1139 if (cmd->cmd.end_state->end_state != -1)
\r
1140 ft2232_end_state(cmd->cmd.end_state->end_state);
\r
1143 /* only send the maximum buffer size that FT2232C can handle */
\r
1144 predicted_size = 3;
\r
1145 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
\r
1147 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1148 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1150 first_unsent = cmd;
\r
1153 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
\r
1156 #ifdef _DEBUG_JTAG_IO_
\r
1157 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
\r
1160 case JTAG_RUNTEST:
\r
1161 /* only send the maximum buffer size that FT2232C can handle */
\r
1162 predicted_size = 0;
\r
1163 if (cur_state != TAP_RTI)
\r
1164 predicted_size += 3;
\r
1165 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
\r
1166 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
\r
1167 predicted_size += 3;
\r
1168 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
\r
1169 predicted_size += 3;
\r
1170 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
\r
1172 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1173 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1175 first_unsent = cmd;
\r
1177 if (cur_state != TAP_RTI)
\r
1179 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
1180 BUFFER_ADD = 0x4b;
\r
1183 /* TMS data bits */
\r
1184 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
\r
1185 cur_state = TAP_RTI;
\r
1188 i = cmd->cmd.runtest->num_cycles;
\r
1191 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
1192 BUFFER_ADD = 0x4b;
\r
1194 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
\r
1195 /* TMS data bits */
\r
1197 cur_state = TAP_RTI;
\r
1198 i -= (i > 7) ? 7 : i;
\r
1199 //DEBUG("added TMS scan (no read)");
\r
1201 if (cmd->cmd.runtest->end_state != -1)
\r
1202 ft2232_end_state(cmd->cmd.runtest->end_state);
\r
1203 if (cur_state != end_state)
\r
1205 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
1206 BUFFER_ADD = 0x4b;
\r
1209 /* TMS data bits */
\r
1210 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
\r
1211 cur_state = end_state;
\r
1212 //DEBUG("added TMS scan (no read)");
\r
1215 #ifdef _DEBUG_JTAG_IO_
\r
1216 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
\r
1219 case JTAG_STATEMOVE:
\r
1220 /* only send the maximum buffer size that FT2232C can handle */
\r
1221 predicted_size = 3;
\r
1222 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
\r
1224 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1225 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1227 first_unsent = cmd;
\r
1229 if (cmd->cmd.statemove->end_state != -1)
\r
1230 ft2232_end_state(cmd->cmd.statemove->end_state);
\r
1231 /* command "Clock Data to TMS/CS Pin (no Read)" */
\r
1232 BUFFER_ADD = 0x4b;
\r
1235 /* TMS data bits */
\r
1236 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
\r
1237 //DEBUG("added TMS scan (no read)");
\r
1238 cur_state = end_state;
\r
1240 #ifdef _DEBUG_JTAG_IO_
\r
1241 DEBUG("statemove: %i", end_state);
\r
1244 case JTAG_PATHMOVE:
\r
1245 /* only send the maximum buffer size that FT2232C can handle */
\r
1246 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
\r
1247 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
\r
1249 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1250 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1252 first_unsent = cmd;
\r
1254 ft2232_add_pathmove(cmd->cmd.pathmove);
\r
1256 #ifdef _DEBUG_JTAG_IO_
\r
1257 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
\r
1261 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
\r
1262 type = jtag_scan_type(cmd->cmd.scan);
\r
1263 predicted_size = ft2232_predict_scan_out(scan_size, type);
\r
1264 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
\r
1266 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
\r
1267 /* unsent commands before this */
\r
1268 if (first_unsent != cmd)
\r
1269 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1270 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1272 /* current command */
\r
1273 if (cmd->cmd.scan->end_state != -1)
\r
1274 ft2232_end_state(cmd->cmd.scan->end_state);
\r
1275 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
\r
1277 first_unsent = cmd->next;
\r
1282 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
\r
1284 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
\r
1285 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1286 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1288 first_unsent = cmd;
\r
1290 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
\r
1291 //DEBUG("new read size: %i", ft2232_expect_read);
\r
1292 if (cmd->cmd.scan->end_state != -1)
\r
1293 ft2232_end_state(cmd->cmd.scan->end_state);
\r
1294 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
\r
1298 #ifdef _DEBUG_JTAG_IO_
\r
1299 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
\r
1303 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1304 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1305 first_unsent = cmd->next;
\r
1306 jtag_sleep(cmd->cmd.sleep->us);
\r
1307 #ifdef _DEBUG_JTAG_IO_
\r
1308 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
\r
1312 ERROR("BUG: unknown JTAG command type encountered");
\r
1318 if (require_send > 0)
\r
1319 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
\r
1320 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1325 #if BUILD_FT2232_FTD2XX == 1
\r
1326 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
\r
1329 DWORD openex_flags = 0;
\r
1330 char *openex_string = NULL;
\r
1333 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
\r
1334 ft2232_layout, vid, pid);
\r
1337 /* Add non-standard Vid/Pid to the linux driver */
\r
1338 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
\r
1340 WARNING("couldn't add %4.4x:%4.4x",
\r
1345 if (ft2232_device_desc && ft2232_serial)
\r
1347 WARNING("can't open by device description and serial number, giving precedence to serial");
\r
1348 ft2232_device_desc = NULL;
\r
1351 if (ft2232_device_desc)
\r
1353 openex_string = ft2232_device_desc;
\r
1354 openex_flags = FT_OPEN_BY_DESCRIPTION;
\r
1356 else if (ft2232_serial)
\r
1358 openex_string = ft2232_serial;
\r
1359 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
\r
1363 ERROR("neither device description nor serial number specified");
\r
1364 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
\r
1366 return ERROR_JTAG_INIT_FAILED;
\r
1369 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
\r
1371 DWORD num_devices;
\r
1374 WARNING("unable to open ftdi device (trying more): %lu",
\r
1377 return ERROR_JTAG_INIT_FAILED;
\r
1379 ERROR("unable to open ftdi device: %lu", status);
\r
1380 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
\r
1381 if (status == FT_OK)
\r
1383 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
\r
1386 for (i = 0; i < num_devices; i++)
\r
1387 desc_array[i] = malloc(64);
\r
1388 desc_array[num_devices] = NULL;
\r
1390 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
\r
1392 if (status == FT_OK)
\r
1394 ERROR("ListDevices: %lu\n", num_devices);
\r
1395 for (i = 0; i < num_devices; i++)
\r
1396 ERROR("%i: %s", i, desc_array[i]);
\r
1399 for (i = 0; i < num_devices; i++)
\r
1400 free(desc_array[i]);
\r
1405 ERROR("ListDevices: NONE\n");
\r
1407 return ERROR_JTAG_INIT_FAILED;
\r
1410 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
\r
1412 ERROR("unable to set latency timer: %lu", status);
\r
1413 return ERROR_JTAG_INIT_FAILED;
\r
1416 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
\r
1418 ERROR("unable to get latency timer: %lu", status);
\r
1419 return ERROR_JTAG_INIT_FAILED;
\r
1423 DEBUG("current latency timer: %i", latency_timer);
\r
1426 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
\r
1428 ERROR("unable to set timeouts: %lu", status);
\r
1429 return ERROR_JTAG_INIT_FAILED;
\r
1432 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
\r
1434 ERROR("unable to enable bit i/o mode: %lu", status);
\r
1435 return ERROR_JTAG_INIT_FAILED;
\r
1441 static int ft2232_purge_ftd2xx(void)
\r
1445 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
\r
1447 ERROR("error purging ftd2xx device: %lu", status);
\r
1448 return ERROR_JTAG_INIT_FAILED;
\r
1453 #endif /* BUILD_FT2232_FTD2XX == 1 */
\r
1455 #if BUILD_FT2232_LIBFTDI == 1
\r
1456 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
\r
1460 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
\r
1461 ft2232_layout, vid, pid);
\r
1463 if (ftdi_init(&ftdic) < 0)
\r
1464 return ERROR_JTAG_INIT_FAILED;
\r
1466 /* context, vendor id, product id */
\r
1467 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
\r
1468 ft2232_serial) < 0) {
\r
1470 WARNING("unable to open ftdi device (trying more): %s",
\r
1473 ERROR("unable to open ftdi device: %s", ftdic.error_str);
\r
1475 return ERROR_JTAG_INIT_FAILED;
\r
1478 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
\r
1480 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
\r
1481 return ERROR_JTAG_INIT_FAILED;
\r
1484 if (ftdi_usb_reset(&ftdic) < 0)
\r
1486 ERROR("unable to reset ftdi device");
\r
1487 return ERROR_JTAG_INIT_FAILED;
\r
1490 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
\r
1492 ERROR("unable to set latency timer");
\r
1493 return ERROR_JTAG_INIT_FAILED;
\r
1496 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
\r
1498 ERROR("unable to get latency timer");
\r
1499 return ERROR_JTAG_INIT_FAILED;
\r
1503 DEBUG("current latency timer: %i", latency_timer);
\r
1506 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
\r
1511 static int ft2232_purge_libftdi(void)
\r
1513 if (ftdi_usb_purge_buffers(&ftdic) < 0)
\r
1515 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
\r
1516 return ERROR_JTAG_INIT_FAILED;
\r
1521 #endif /* BUILD_FT2232_LIBFTDI == 1 */
\r
1523 int ft2232_init(void)
\r
1527 u32 bytes_written;
\r
1528 ft2232_layout_t *cur_layout = ft2232_layouts;
\r
1531 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
\r
1533 ft2232_layout = "usbjtag";
\r
1534 WARNING("No ft2232 layout specified, using default 'usbjtag'");
\r
1537 while (cur_layout->name)
\r
1539 if (strcmp(cur_layout->name, ft2232_layout) == 0)
\r
1541 layout = cur_layout;
\r
1549 ERROR("No matching layout found for %s", ft2232_layout);
\r
1550 return ERROR_JTAG_INIT_FAILED;
\r
1553 for (i = 0; 1; i++) {
\r
1555 * "more indicates that there are more IDs to try, so we should
\r
1556 * not print an error for an ID mismatch (but for anything
\r
1557 * else, we should).
\r
1559 * try_more indicates that the error code returned indicates an
\r
1560 * ID mismatch (and nothing else) and that we should proceeed
\r
1561 * with the next ID pair.
\r
1563 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
\r
1566 #if BUILD_FT2232_FTD2XX == 1
\r
1567 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
\r
1569 #elif BUILD_FT2232_LIBFTDI == 1
\r
1570 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
\r
1575 if (!more || !try_more)
\r
1579 ft2232_buffer_size = 0;
\r
1580 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
\r
1582 if (layout->init() != ERROR_OK)
\r
1583 return ERROR_JTAG_INIT_FAILED;
\r
1585 ft2232_speed(jtag_speed);
\r
1587 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
\r
1588 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
\r
1590 ERROR("couldn't write to FT2232 to disable loopback");
\r
1591 return ERROR_JTAG_INIT_FAILED;
\r
1594 #if BUILD_FT2232_FTD2XX == 1
\r
1595 return ft2232_purge_ftd2xx();
\r
1596 #elif BUILD_FT2232_LIBFTDI == 1
\r
1597 return ft2232_purge_libftdi();
\r
1603 int usbjtag_init(void)
\r
1606 u32 bytes_written;
\r
1608 low_output = 0x08;
\r
1609 low_direction = 0x0b;
\r
1611 if (strcmp(ft2232_layout, "usbjtag") == 0)
\r
1618 else if (strcmp(ft2232_layout, "signalyzer") == 0)
\r
1625 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
\r
1631 low_output = 0x88;
\r
1632 low_direction = 0x8b;
\r
1636 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
\r
1637 return ERROR_JTAG_INIT_FAILED;
\r
1640 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
1642 low_direction &= ~nTRSTnOE; /* nTRST input */
\r
1643 low_output &= ~nTRST; /* nTRST = 0 */
\r
1647 low_direction |= nTRSTnOE; /* nTRST output */
\r
1648 low_output |= nTRST; /* nTRST = 1 */
\r
1651 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
1653 low_direction |= nSRSTnOE; /* nSRST output */
\r
1654 low_output |= nSRST; /* nSRST = 1 */
\r
1658 low_direction &= ~nSRSTnOE; /* nSRST input */
\r
1659 low_output &= ~nSRST; /* nSRST = 0 */
\r
1662 /* initialize low byte for jtag */
\r
1663 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1664 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
\r
1665 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
\r
1666 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1668 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1670 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
\r
1671 return ERROR_JTAG_INIT_FAILED;
\r
1677 int jtagkey_init(void)
\r
1680 u32 bytes_written;
\r
1682 low_output = 0x08;
\r
1683 low_direction = 0x1b;
\r
1685 /* initialize low byte for jtag */
\r
1686 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1687 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
\r
1688 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
\r
1689 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1691 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1693 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
\r
1694 return ERROR_JTAG_INIT_FAILED;
\r
1697 if (strcmp(layout->name, "jtagkey") == 0)
\r
1704 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
\r
1705 (strcmp(layout->name, "oocdlink") == 0))
\r
1714 ERROR("BUG: jtagkey_init called for non jtagkey layout");
\r
1718 high_output = 0x0;
\r
1719 high_direction = 0x0f;
\r
1721 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
1723 high_output |= nTRSTnOE;
\r
1724 high_output &= ~nTRST;
\r
1728 high_output &= ~nTRSTnOE;
\r
1729 high_output |= nTRST;
\r
1732 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
1734 high_output &= ~nSRSTnOE;
\r
1735 high_output |= nSRST;
\r
1739 high_output |= nSRSTnOE;
\r
1740 high_output &= ~nSRST;
\r
1743 /* initialize high port */
\r
1744 buf[0] = 0x82; /* command "set data bits high byte" */
\r
1745 buf[1] = high_output; /* value */
\r
1746 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
\r
1747 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1749 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1751 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
\r
1752 return ERROR_JTAG_INIT_FAILED;
\r
1758 int olimex_jtag_init(void)
\r
1761 u32 bytes_written;
\r
1763 low_output = 0x08;
\r
1764 low_direction = 0x1b;
\r
1766 /* initialize low byte for jtag */
\r
1767 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1768 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
\r
1769 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
\r
1770 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1772 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1774 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
\r
1775 return ERROR_JTAG_INIT_FAILED;
\r
1781 nSRSTnOE = 0x00; /* no output enable for nSRST */
\r
1783 high_output = 0x0;
\r
1784 high_direction = 0x0f;
\r
1786 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
\r
1788 high_output |= nTRSTnOE;
\r
1789 high_output &= ~nTRST;
\r
1793 high_output &= ~nTRSTnOE;
\r
1794 high_output |= nTRST;
\r
1797 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
\r
1799 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
\r
1803 high_output &= ~nSRST;
\r
1806 /* turn red LED on */
\r
1807 high_output |= 0x08;
\r
1809 /* initialize high port */
\r
1810 buf[0] = 0x82; /* command "set data bits high byte" */
\r
1811 buf[1] = high_output; /* value */
\r
1812 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
\r
1813 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1815 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1817 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
\r
1818 return ERROR_JTAG_INIT_FAILED;
\r
1824 int flyswatter_init(void)
\r
1827 u32 bytes_written;
\r
1829 low_output = 0x18;
\r
1830 low_direction = 0xfb;
\r
1832 /* initialize low byte for jtag */
\r
1833 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1834 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
\r
1835 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
\r
1836 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1838 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1840 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
\r
1841 return ERROR_JTAG_INIT_FAILED;
\r
1845 nTRSTnOE = 0x0; /* not output enable for nTRST */
\r
1847 nSRSTnOE = 0x00; /* no output enable for nSRST */
\r
1849 high_output = 0x00;
\r
1850 high_direction = 0x0c;
\r
1852 /* turn red LED1 on, LED2 off */
\r
1853 high_output |= 0x08;
\r
1855 /* initialize high port */
\r
1856 buf[0] = 0x82; /* command "set data bits high byte" */
\r
1857 buf[1] = high_output; /* value */
\r
1858 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
\r
1859 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1861 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1863 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
\r
1864 return ERROR_JTAG_INIT_FAILED;
\r
1870 int turtle_init(void)
\r
1873 u32 bytes_written;
\r
1875 low_output = 0x08;
\r
1876 low_direction = 0x5b;
\r
1878 /* initialize low byte for jtag */
\r
1879 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1880 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
\r
1881 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
\r
1882 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1884 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1886 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
\r
1887 return ERROR_JTAG_INIT_FAILED;
\r
1892 high_output = 0x00;
\r
1893 high_direction = 0x0C;
\r
1895 /* initialize high port */
\r
1896 buf[0] = 0x82; /* command "set data bits high byte" */
\r
1897 buf[1] = high_output;
\r
1898 buf[2] = high_direction;
\r
1899 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1901 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1903 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
\r
1904 return ERROR_JTAG_INIT_FAILED;
\r
1910 int comstick_init(void)
\r
1913 u32 bytes_written;
\r
1915 low_output = 0x08;
\r
1916 low_direction = 0x0b;
\r
1918 /* initialize low byte for jtag */
\r
1919 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1920 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
\r
1921 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
\r
1922 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1924 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1926 ERROR("couldn't initialize FT2232 with 'comstick' layout");
\r
1927 return ERROR_JTAG_INIT_FAILED;
\r
1931 nTRSTnOE = 0x00; /* no output enable for nTRST */
\r
1933 nSRSTnOE = 0x00; /* no output enable for nSRST */
\r
1935 high_output = 0x03;
\r
1936 high_direction = 0x03;
\r
1938 /* initialize high port */
\r
1939 buf[0] = 0x82; /* command "set data bits high byte" */
\r
1940 buf[1] = high_output;
\r
1941 buf[2] = high_direction;
\r
1942 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1944 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1946 ERROR("couldn't initialize FT2232 with 'comstick' layout");
\r
1947 return ERROR_JTAG_INIT_FAILED;
\r
1953 int stm32stick_init(void)
\r
1956 u32 bytes_written;
\r
1958 low_output = 0x88;
\r
1959 low_direction = 0x8b;
\r
1961 /* initialize low byte for jtag */
\r
1962 buf[0] = 0x80; /* command "set data bits low byte" */
\r
1963 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
\r
1964 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
\r
1965 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1967 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1969 ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
\r
1970 return ERROR_JTAG_INIT_FAILED;
\r
1974 nTRSTnOE = 0x00; /* no output enable for nTRST */
\r
1976 nSRSTnOE = 0x00; /* no output enable for nSRST */
\r
1978 high_output = 0x01;
\r
1979 high_direction = 0x03;
\r
1981 /* initialize high port */
\r
1982 buf[0] = 0x82; /* command "set data bits high byte" */
\r
1983 buf[1] = high_output;
\r
1984 buf[2] = high_direction;
\r
1985 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
\r
1987 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
\r
1989 ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
\r
1990 return ERROR_JTAG_INIT_FAILED;
\r
1996 void olimex_jtag_blink(void)
\r
1998 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
\r
1999 * ACBUS3 is bit 3 of the GPIOH port
\r
2001 if (high_output & 0x08)
\r
2003 /* set port pin high */
\r
2004 high_output &= 0x07;
\r
2008 /* set port pin low */
\r
2009 high_output |= 0x08;
\r
2012 BUFFER_ADD = 0x82;
\r
2013 BUFFER_ADD = high_output;
\r
2014 BUFFER_ADD = high_direction;
\r
2017 void turtle_jtag_blink(void)
\r
2020 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
\r
2022 if (high_output & 0x08)
\r
2024 high_output = 0x04;
\r
2028 high_output = 0x08;
\r
2031 BUFFER_ADD = 0x82;
\r
2032 BUFFER_ADD = high_output;
\r
2033 BUFFER_ADD = high_direction;
\r
2037 int ft2232_quit(void)
\r
2039 #if BUILD_FT2232_FTD2XX == 1
\r
2042 status = FT_Close(ftdih);
\r
2043 #elif BUILD_FT2232_LIBFTDI == 1
\r
2044 ftdi_disable_bitbang(&ftdic);
\r
2046 ftdi_usb_close(&ftdic);
\r
2048 ftdi_deinit(&ftdic);
\r
2051 free(ft2232_buffer);
\r
2052 ft2232_buffer = NULL;
\r
2057 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
2061 ft2232_device_desc = strdup(args[0]);
\r
2065 ERROR("expected exactly one argument to ft2232_device_desc <description>");
\r
2071 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
2075 ft2232_serial = strdup(args[0]);
\r
2079 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
\r
2085 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
2090 ft2232_layout = malloc(strlen(args[0]) + 1);
\r
2091 strcpy(ft2232_layout, args[0]);
\r
2096 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
2100 if (argc > MAX_USB_IDS*2) {
\r
2101 WARNING("ignoring extra IDs in ft2232_vid_pid "
\r
2102 "(maximum is %d pairs)", MAX_USB_IDS);
\r
2103 argc = MAX_USB_IDS*2;
\r
2105 if (argc < 2 || (argc & 1))
\r
2107 WARNING("incomplete ft2232_vid_pid configuration directive");
\r
2112 for (i = 0; i+1 < argc; i += 2) {
\r
2113 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
\r
2114 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
\r
2117 * Explicitly terminate, in case there are multiples instances of
\r
2120 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
\r
2125 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
2129 ft2232_latency = atoi(args[0]);
\r
2133 ERROR("expected exactly one argument to ft2232_latency <ms>");
\r