1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
43 /* FT2232 access library includes */
44 #if BUILD_FT2232_FTD2XX == 1
46 #elif BUILD_FT2232_LIBFTDI == 1
53 /* enable this to debug io latency
56 #define _DEBUG_USB_IO_
59 /* enable this to debug communication
62 #define _DEBUG_USB_COMMS_
65 int ft2232_execute_queue(void);
67 int ft2232_speed(int speed
);
68 int ft2232_register_commands(struct command_context_s
*cmd_ctx
);
69 int ft2232_init(void);
70 int ft2232_quit(void);
72 int ft2232_handle_device_desc_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
73 int ft2232_handle_serial_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
74 int ft2232_handle_layout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
75 int ft2232_handle_vid_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
76 int ft2232_handle_latency_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
78 char *ft2232_device_desc
= NULL
;
79 char *ft2232_serial
= NULL
;
80 char *ft2232_layout
= NULL
;
81 unsigned char ft2232_latency
= 2;
84 /* vid = pid = 0 marks the end of the list */
85 static u16 ft2232_vid
[MAX_USB_IDS
+1] = { 0x0403, 0 };
86 static u16 ft2232_pid
[MAX_USB_IDS
+1] = { 0x6010, 0 };
88 typedef struct ft2232_layout_s
92 void(*reset
)(int trst
, int srst
);
96 /* init procedures for supported layouts */
97 int usbjtag_init(void);
98 int jtagkey_init(void);
99 int olimex_jtag_init(void);
100 int flyswatter_init(void);
101 int turtle_init(void);
102 int comstick_init(void);
103 int stm32stick_init(void);
105 /* reset procedures for supported layouts */
106 void usbjtag_reset(int trst
, int srst
);
107 void jtagkey_reset(int trst
, int srst
);
108 void olimex_jtag_reset(int trst
, int srst
);
109 void flyswatter_reset(int trst
, int srst
);
110 void turtle_reset(int trst
, int srst
);
111 void comstick_reset(int trst
, int srst
);
112 void stm32stick_reset(int trst
, int srst
);
114 /* blink procedures for layouts that support a blinking led */
115 void olimex_jtag_blink(void);
116 void turtle_jtag_blink(void);
118 ft2232_layout_t ft2232_layouts
[] =
120 {"usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
121 {"jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
122 {"jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
123 {"oocdlink", jtagkey_init
, jtagkey_reset
, NULL
},
124 {"signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
125 {"evb_lm3s811", usbjtag_init
, usbjtag_reset
, NULL
},
126 {"olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
127 {"flyswatter", flyswatter_init
, flyswatter_reset
, NULL
},
128 {"turtelizer2", turtle_init
, turtle_reset
, turtle_jtag_blink
},
129 {"comstick", comstick_init
, comstick_reset
, NULL
},
130 {"stm32stick", stm32stick_init
, stm32stick_reset
, NULL
},
134 static u8 nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
136 static ft2232_layout_t
*layout
;
137 static u8 low_output
= 0x0;
138 static u8 low_direction
= 0x0;
139 static u8 high_output
= 0x0;
140 static u8 high_direction
= 0x0;
142 #if BUILD_FT2232_FTD2XX == 1
143 static FT_HANDLE ftdih
= NULL
;
144 #elif BUILD_FT2232_LIBFTDI == 1
145 static struct ftdi_context ftdic
;
148 static u8
*ft2232_buffer
= NULL
;
149 static int ft2232_buffer_size
= 0;
150 static int ft2232_read_pointer
= 0;
151 static int ft2232_expect_read
= 0;
152 #define FT2232_BUFFER_SIZE 131072
153 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
154 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
156 jtag_interface_t ft2232_interface
=
161 .execute_queue
= ft2232_execute_queue
,
163 .speed
= ft2232_speed
,
164 .register_commands
= ft2232_register_commands
,
169 int ft2232_write(u8
*buf
, int size
, u32
* bytes_written
)
171 #if BUILD_FT2232_FTD2XX == 1
173 DWORD dw_bytes_written
;
174 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
176 *bytes_written
= dw_bytes_written
;
177 ERROR("FT_Write returned: %lu", status
);
178 return ERROR_JTAG_DEVICE_ERROR
;
182 *bytes_written
= dw_bytes_written
;
185 #elif BUILD_FT2232_LIBFTDI == 1
187 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
190 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
191 return ERROR_JTAG_DEVICE_ERROR
;
195 *bytes_written
= retval
;
201 int ft2232_read(u8
* buf
, int size
, u32
* bytes_read
)
203 #if BUILD_FT2232_FTD2XX == 1
209 while ((*bytes_read
< size
) && timeout
--)
211 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
212 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
215 ERROR("FT_Read returned: %lu", status
);
216 return ERROR_JTAG_DEVICE_ERROR
;
218 *bytes_read
+= dw_bytes_read
;
220 #elif BUILD_FT2232_LIBFTDI == 1
225 while ((*bytes_read
< size
) && timeout
--)
227 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
230 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
231 return ERROR_JTAG_DEVICE_ERROR
;
233 *bytes_read
+= retval
;
237 if (*bytes_read
< size
)
239 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read
, size
);
240 return ERROR_JTAG_DEVICE_ERROR
;
246 int ft2232_speed(int speed
)
252 buf
[0] = 0x86; /* command "set divisor" */
253 buf
[1] = speed
& 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
254 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
256 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
257 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
259 ERROR("couldn't set FT2232 TCK speed");
268 int ft2232_register_commands(struct command_context_s
*cmd_ctx
)
270 register_command(cmd_ctx
, NULL
, "ft2232_device_desc", ft2232_handle_device_desc_command
,
271 COMMAND_CONFIG
, NULL
);
272 register_command(cmd_ctx
, NULL
, "ft2232_serial", ft2232_handle_serial_command
,
273 COMMAND_CONFIG
, NULL
);
274 register_command(cmd_ctx
, NULL
, "ft2232_layout", ft2232_handle_layout_command
,
275 COMMAND_CONFIG
, NULL
);
276 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid", ft2232_handle_vid_pid_command
,
277 COMMAND_CONFIG
, NULL
);
278 register_command(cmd_ctx
, NULL
, "ft2232_latency", ft2232_handle_latency_command
,
279 COMMAND_CONFIG
, NULL
);
283 void ft2232_end_state(enum tap_state state
)
285 if (tap_move_map
[state
] != -1)
289 ERROR("BUG: %i is not a valid end state", state
);
294 void ft2232_read_scan(enum scan_type type
, u8
* buffer
, int scan_size
)
296 int num_bytes
= ((scan_size
+ 7) / 8);
297 int bits_left
= scan_size
;
300 while(num_bytes
-- > 1)
302 buffer
[cur_byte
] = BUFFER_READ
;
307 buffer
[cur_byte
] = 0x0;
311 buffer
[cur_byte
] = BUFFER_READ
>> 1;
314 buffer
[cur_byte
] = (buffer
[cur_byte
] | ((BUFFER_READ
& 0x02) << 6)) >> (8 - bits_left
);
318 void ft2232_debug_dump_buffer(void)
324 for (i
= 0; i
< ft2232_buffer_size
; i
++)
326 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
338 int ft2232_send_and_recv(jtag_command_t
*first
, jtag_command_t
*last
)
348 #ifdef _DEBUG_USB_IO_
349 struct timeval start
, inter
, inter2
, end
;
350 struct timeval d_inter
, d_inter2
, d_end
;
353 #ifdef _DEBUG_USB_COMMS_
354 DEBUG("write buffer (size %i):", ft2232_buffer_size
);
355 ft2232_debug_dump_buffer();
358 #ifdef _DEBUG_USB_IO_
359 gettimeofday(&start
, NULL
);
362 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
364 ERROR("couldn't write MPSSE commands to FT2232");
368 #ifdef _DEBUG_USB_IO_
369 gettimeofday(&inter
, NULL
);
372 if (ft2232_expect_read
)
375 ft2232_buffer_size
= 0;
377 #ifdef _DEBUG_USB_IO_
378 gettimeofday(&inter2
, NULL
);
381 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
383 ERROR("couldn't read from FT2232");
387 #ifdef _DEBUG_USB_IO_
388 gettimeofday(&end
, NULL
);
390 timeval_subtract(&d_inter
, &inter
, &start
);
391 timeval_subtract(&d_inter2
, &inter2
, &start
);
392 timeval_subtract(&d_end
, &end
, &start
);
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
);
398 ft2232_buffer_size
= bytes_read
;
400 if (ft2232_expect_read
!= ft2232_buffer_size
)
402 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read
, ft2232_buffer_size
, 100 - timeout
);
403 ft2232_debug_dump_buffer();
408 #ifdef _DEBUG_USB_COMMS_
409 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout
, ft2232_buffer_size
);
410 ft2232_debug_dump_buffer();
414 ft2232_expect_read
= 0;
415 ft2232_read_pointer
= 0;
417 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
418 * that wasn't handled by a caller-provided error handler
428 type
= jtag_scan_type(cmd
->cmd
.scan
);
429 if (type
!= SCAN_OUT
)
431 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
432 buffer
= calloc(CEIL(scan_size
, 8), 1);
433 ft2232_read_scan(type
, buffer
, scan_size
);
434 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
435 retval
= ERROR_JTAG_QUEUE_FAILED
;
445 ft2232_buffer_size
= 0;
450 void ft2232_add_pathmove(pathmove_command_t
*cmd
)
452 int num_states
= cmd
->num_states
;
462 int num_states_batch
= num_states
> 7 ?
7 : num_states
;
464 /* command "Clock Data to TMS/CS Pin (no Read)" */
466 /* number of states remaining */
467 BUFFER_ADD
= num_states_batch
- 1;
469 while (num_states_batch
--)
471 if (tap_transitions
[cur_state
].low
== cmd
->path
[state_count
])
472 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
473 else if (tap_transitions
[cur_state
].high
== cmd
->path
[state_count
])
474 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
477 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[cmd
->path
[state_count
]]);
481 cur_state
= cmd
->path
[state_count
];
486 BUFFER_ADD
= tms_byte
;
489 end_state
= cur_state
;
492 void ft2232_add_scan(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
)
494 int num_bytes
= (scan_size
+ 7) / 8;
495 int bits_left
= scan_size
;
499 if (!((!ir_scan
&& (cur_state
== TAP_SD
)) || (ir_scan
&& (cur_state
== TAP_SI
))))
501 /* command "Clock Data to TMS/CS Pin (no Read)" */
508 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SI
);
513 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SD
);
516 /* DEBUG("added TMS scan (no read)"); */
519 /* add command for complete bytes */
520 while (num_bytes
> 1)
525 /* Clock Data Bytes In and Out LSB First */
527 /* DEBUG("added TDI bytes (io %i)", num_bytes); */
529 else if (type
== SCAN_OUT
)
531 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
533 /* DEBUG("added TDI bytes (o)"); */
535 else if (type
== SCAN_IN
)
537 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
539 /* DEBUG("added TDI bytes (i %i)", num_bytes); */
541 thisrun_bytes
= (num_bytes
> 65537) ?
65536 : (num_bytes
- 1);
542 num_bytes
-= thisrun_bytes
;
543 BUFFER_ADD
= (thisrun_bytes
- 1) & 0xff;
544 BUFFER_ADD
= ((thisrun_bytes
- 1) >> 8) & 0xff;
547 /* add complete bytes */
548 while(thisrun_bytes
-- > 0)
550 BUFFER_ADD
= buffer
[cur_byte
];
555 else /* (type == SCAN_IN) */
557 bits_left
-= 8 * (thisrun_bytes
);
561 /* the most signifcant bit is scanned during TAP movement */
563 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
567 /* process remaining bits but the last one */
572 /* Clock Data Bits In and Out LSB First */
574 /* DEBUG("added TDI bits (io) %i", bits_left - 1); */
576 else if (type
== SCAN_OUT
)
578 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
580 /* DEBUG("added TDI bits (o)"); */
582 else if (type
== SCAN_IN
)
584 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
586 /* DEBUG("added TDI bits (i %i)", bits_left - 1); */
588 BUFFER_ADD
= bits_left
- 2;
590 BUFFER_ADD
= buffer
[cur_byte
];
593 if ((ir_scan
&& (end_state
== TAP_SI
)) ||
594 (!ir_scan
&& (end_state
== TAP_SD
)))
598 /* Clock Data Bits In and Out LSB First */
600 /* DEBUG("added TDI bits (io) %i", bits_left - 1); */
602 else if (type
== SCAN_OUT
)
604 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
606 /* DEBUG("added TDI bits (o)"); */
608 else if (type
== SCAN_IN
)
610 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
612 /* DEBUG("added TDI bits (i %i)", bits_left - 1); */
615 BUFFER_ADD
= last_bit
;
619 /* move from Shift-IR/DR to end state */
620 if (type
!= SCAN_OUT
)
622 /* Clock Data to TMS/CS Pin with Read */
624 /* DEBUG("added TMS scan (read)"); */
628 /* Clock Data to TMS/CS Pin (no Read) */
630 /* DEBUG("added TMS scan (no read)"); */
633 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
) | (last_bit
<< 7);
634 cur_state
= end_state
;
638 int ft2232_large_scan(scan_command_t
*cmd
, enum scan_type type
, u8
*buffer
, int scan_size
)
640 int num_bytes
= (scan_size
+ 7) / 8;
641 int bits_left
= scan_size
;
644 u8
*receive_buffer
= malloc(CEIL(scan_size
, 8));
645 u8
*receive_pointer
= receive_buffer
;
649 int thisrun_read
= 0;
653 ERROR("BUG: large IR scans are not supported");
657 if (cur_state
!= TAP_SD
)
659 /* command "Clock Data to TMS/CS Pin (no Read)" */
664 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SD
);
668 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
670 ERROR("couldn't write MPSSE commands to FT2232");
673 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
674 ft2232_buffer_size
= 0;
676 /* add command for complete bytes */
677 while (num_bytes
> 1)
683 /* Clock Data Bytes In and Out LSB First */
685 /* DEBUG("added TDI bytes (io %i)", num_bytes); */
687 else if (type
== SCAN_OUT
)
689 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
691 /* DEBUG("added TDI bytes (o)"); */
693 else if (type
== SCAN_IN
)
695 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
697 /* DEBUG("added TDI bytes (i %i)", num_bytes); */
699 thisrun_bytes
= (num_bytes
> 65537) ?
65536 : (num_bytes
- 1);
700 thisrun_read
= thisrun_bytes
;
701 num_bytes
-= thisrun_bytes
;
702 BUFFER_ADD
= (thisrun_bytes
- 1) & 0xff;
703 BUFFER_ADD
= ((thisrun_bytes
- 1) >> 8) & 0xff;
706 /* add complete bytes */
707 while(thisrun_bytes
-- > 0)
709 BUFFER_ADD
= buffer
[cur_byte
];
714 else /* (type == SCAN_IN) */
716 bits_left
-= 8 * (thisrun_bytes
);
719 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
721 ERROR("couldn't write MPSSE commands to FT2232");
724 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
725 ft2232_buffer_size
= 0;
727 if (type
!= SCAN_OUT
)
729 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
731 ERROR("couldn't read from FT2232");
734 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read
, bytes_read
);
735 receive_pointer
+= bytes_read
;
741 /* the most signifcant bit is scanned during TAP movement */
743 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
747 /* process remaining bits but the last one */
752 /* Clock Data Bits In and Out LSB First */
754 /* DEBUG("added TDI bits (io) %i", bits_left - 1); */
756 else if (type
== SCAN_OUT
)
758 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
760 /* DEBUG("added TDI bits (o)"); */
762 else if (type
== SCAN_IN
)
764 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
766 /* DEBUG("added TDI bits (i %i)", bits_left - 1); */
768 BUFFER_ADD
= bits_left
- 2;
770 BUFFER_ADD
= buffer
[cur_byte
];
772 if (type
!= SCAN_OUT
)
776 if (end_state
== TAP_SD
)
780 /* Clock Data Bits In and Out LSB First */
782 /* DEBUG("added TDI bits (io) %i", bits_left - 1); */
784 else if (type
== SCAN_OUT
)
786 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
788 /* DEBUG("added TDI bits (o)"); */
790 else if (type
== SCAN_IN
)
792 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
794 /* DEBUG("added TDI bits (i %i)", bits_left - 1); */
797 BUFFER_ADD
= last_bit
;
801 /* move from Shift-IR/DR to end state */
802 if (type
!= SCAN_OUT
)
804 /* Clock Data to TMS/CS Pin with Read */
806 /* DEBUG("added TMS scan (read)"); */
810 /* Clock Data to TMS/CS Pin (no Read) */
812 /* DEBUG("added TMS scan (no read)"); */
815 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
) | (last_bit
<< 7);
816 cur_state
= end_state
;
819 if (type
!= SCAN_OUT
)
822 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
824 ERROR("couldn't write MPSSE commands to FT2232");
827 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
828 ft2232_buffer_size
= 0;
830 if (type
!= SCAN_OUT
)
832 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
834 ERROR("couldn't read from FT2232");
837 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read
, bytes_read
);
838 receive_pointer
+= bytes_read
;
844 int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
846 int predicted_size
= 3;
847 int num_bytes
= (scan_size
- 1) / 8;
849 if (cur_state
!= TAP_SD
)
852 if (type
== SCAN_IN
) /* only from device to host */
855 predicted_size
+= (CEIL(num_bytes
, 65536)) * 3;
856 /* remaining bits - 1 (up to 7) */
857 predicted_size
+= ((scan_size
- 1) % 8) ?
2 : 0;
859 else /* host to device, or bidirectional */
862 predicted_size
+= num_bytes
+ (CEIL(num_bytes
, 65536)) * 3;
863 /* remaining bits -1 (up to 7) */
864 predicted_size
+= ((scan_size
- 1) % 8) ?
3 : 0;
867 return predicted_size
;
870 int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
872 int predicted_size
= 0;
874 if (type
!= SCAN_OUT
)
877 predicted_size
+= (CEIL(scan_size
, 8) > 1) ?
(CEIL(scan_size
, 8) - 1) : 0;
878 /* remaining bits - 1 */
879 predicted_size
+= ((scan_size
- 1) % 8) ?
1 : 0;
880 /* last bit (from TMS scan) */
884 /* DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
886 return predicted_size
;
889 void usbjtag_reset(int trst
, int srst
)
894 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
895 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
897 low_output
&= ~nTRST
; /* switch output low */
901 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
902 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
904 low_output
|= nTRST
; /* switch output high */
909 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
910 low_output
&= ~nSRST
; /* switch output low */
912 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
916 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
917 low_output
|= nSRST
; /* switch output high */
919 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
922 /* command "set data bits low byte" */
924 BUFFER_ADD
= low_output
;
925 BUFFER_ADD
= low_direction
;
929 void jtagkey_reset(int trst
, int srst
)
934 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
935 high_output
&= ~nTRSTnOE
;
937 high_output
&= ~nTRST
;
941 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
942 high_output
|= nTRSTnOE
;
944 high_output
|= nTRST
;
949 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
950 high_output
&= ~nSRST
;
952 high_output
&= ~nSRSTnOE
;
956 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
957 high_output
|= nSRST
;
959 high_output
|= nSRSTnOE
;
962 /* command "set data bits high byte" */
964 BUFFER_ADD
= high_output
;
965 BUFFER_ADD
= high_direction
;
966 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
969 void olimex_jtag_reset(int trst
, int srst
)
974 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
975 high_output
&= ~nTRSTnOE
;
977 high_output
&= ~nTRST
;
981 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
982 high_output
|= nTRSTnOE
;
984 high_output
|= nTRST
;
989 high_output
|= nSRST
;
993 high_output
&= ~nSRST
;
996 /* command "set data bits high byte" */
998 BUFFER_ADD
= high_output
;
999 BUFFER_ADD
= high_direction
;
1000 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1003 void flyswatter_reset(int trst
, int srst
)
1007 cur_state
= TAP_TLR
;
1008 low_output
&= ~nTRST
;
1012 low_output
|= nTRST
;
1017 low_output
|= nSRST
;
1021 low_output
&= ~nSRST
;
1024 /* command "set data bits low byte" */
1026 BUFFER_ADD
= low_output
;
1027 BUFFER_ADD
= low_direction
;
1028 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1031 void turtle_reset(int trst
, int srst
)
1037 low_output
|= nSRST
;
1041 low_output
&= ~nSRST
;
1044 /* command "set data bits low byte" */
1046 BUFFER_ADD
= low_output
;
1047 BUFFER_ADD
= low_direction
;
1048 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1051 void comstick_reset(int trst
, int srst
)
1055 cur_state
= TAP_TLR
;
1056 high_output
&= ~nTRST
;
1060 high_output
|= nTRST
;
1065 high_output
&= ~nSRST
;
1069 high_output
|= nSRST
;
1072 /* command "set data bits high byte" */
1074 BUFFER_ADD
= high_output
;
1075 BUFFER_ADD
= high_direction
;
1076 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1079 void stm32stick_reset(int trst
, int srst
)
1083 cur_state
= TAP_TLR
;
1084 high_output
&= ~nTRST
;
1088 high_output
|= nTRST
;
1093 low_output
&= ~nSRST
;
1097 low_output
|= nSRST
;
1100 /* command "set data bits low byte" */
1102 BUFFER_ADD
= low_output
;
1103 BUFFER_ADD
= low_direction
;
1105 /* command "set data bits high byte" */
1107 BUFFER_ADD
= high_output
;
1108 BUFFER_ADD
= high_direction
;
1109 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1112 int ft2232_execute_queue()
1114 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
1115 jtag_command_t
*first_unsent
= cmd
; /* next command that has to be sent */
1117 int scan_size
; /* size of IR or DR scan */
1118 enum scan_type type
;
1120 int predicted_size
= 0;
1121 int require_send
= 0;
1124 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1125 * that wasn't handled by a caller-provided error handler
1129 ft2232_buffer_size
= 0;
1130 ft2232_expect_read
= 0;
1132 /* blink, if the current layout has that feature */
1140 case JTAG_END_STATE
:
1141 if (cmd
->cmd
.end_state
->end_state
!= -1)
1142 ft2232_end_state(cmd
->cmd
.end_state
->end_state
);
1145 /* only send the maximum buffer size that FT2232C can handle */
1147 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1149 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1150 retval
= ERROR_JTAG_QUEUE_FAILED
;
1155 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1158 #ifdef _DEBUG_JTAG_IO_
1159 DEBUG("trst: %i, srst: %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1163 /* only send the maximum buffer size that FT2232C can handle */
1165 if (cur_state
!= TAP_RTI
)
1166 predicted_size
+= 3;
1167 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
1168 if ((cmd
->cmd
.runtest
->end_state
!= -1) && (cmd
->cmd
.runtest
->end_state
!= TAP_RTI
))
1169 predicted_size
+= 3;
1170 if ((cmd
->cmd
.runtest
->end_state
== -1) && (end_state
!= TAP_RTI
))
1171 predicted_size
+= 3;
1172 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1174 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1175 retval
= ERROR_JTAG_QUEUE_FAILED
;
1179 if (cur_state
!= TAP_RTI
)
1181 /* command "Clock Data to TMS/CS Pin (no Read)" */
1186 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_RTI
);
1187 cur_state
= TAP_RTI
;
1190 i
= cmd
->cmd
.runtest
->num_cycles
;
1193 /* command "Clock Data to TMS/CS Pin (no Read)" */
1196 BUFFER_ADD
= (i
> 7) ?
6 : (i
- 1);
1199 cur_state
= TAP_RTI
;
1200 i
-= (i
> 7) ?
7 : i
;
1201 /* DEBUG("added TMS scan (no read)"); */
1203 if (cmd
->cmd
.runtest
->end_state
!= -1)
1204 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1205 if (cur_state
!= end_state
)
1207 /* command "Clock Data to TMS/CS Pin (no Read)" */
1212 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
);
1213 cur_state
= end_state
;
1214 /* DEBUG("added TMS scan (no read)"); */
1217 #ifdef _DEBUG_JTAG_IO_
1218 DEBUG("runtest: %i, end in %i", cmd
->cmd
.runtest
->num_cycles
, end_state
);
1221 case JTAG_STATEMOVE
:
1222 /* only send the maximum buffer size that FT2232C can handle */
1224 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1226 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1227 retval
= ERROR_JTAG_QUEUE_FAILED
;
1231 if (cmd
->cmd
.statemove
->end_state
!= -1)
1232 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1233 /* command "Clock Data to TMS/CS Pin (no Read)" */
1238 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
);
1239 /* DEBUG("added TMS scan (no read)"); */
1240 cur_state
= end_state
;
1242 #ifdef _DEBUG_JTAG_IO_
1243 DEBUG("statemove: %i", end_state
);
1247 /* only send the maximum buffer size that FT2232C can handle */
1248 predicted_size
= 3 * CEIL(cmd
->cmd
.pathmove
->num_states
, 7);
1249 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1251 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1252 retval
= ERROR_JTAG_QUEUE_FAILED
;
1256 ft2232_add_pathmove(cmd
->cmd
.pathmove
);
1258 #ifdef _DEBUG_JTAG_IO_
1259 DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1263 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1264 type
= jtag_scan_type(cmd
->cmd
.scan
);
1265 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1266 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1268 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1269 /* unsent commands before this */
1270 if (first_unsent
!= cmd
)
1271 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1272 retval
= ERROR_JTAG_QUEUE_FAILED
;
1274 /* current command */
1275 if (cmd
->cmd
.scan
->end_state
!= -1)
1276 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1277 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1279 first_unsent
= cmd
->next
;
1284 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1286 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent
, cmd
);
1287 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1288 retval
= ERROR_JTAG_QUEUE_FAILED
;
1292 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1293 /* DEBUG("new read size: %i", ft2232_expect_read); */
1294 if (cmd
->cmd
.scan
->end_state
!= -1)
1295 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1296 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1300 #ifdef _DEBUG_JTAG_IO_
1301 DEBUG("%s scan, %i bit, end in %i", (cmd
->cmd
.scan
->ir_scan
) ?
"IR" : "DR", scan_size
, end_state
);
1305 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1306 retval
= ERROR_JTAG_QUEUE_FAILED
;
1307 first_unsent
= cmd
->next
;
1308 jtag_sleep(cmd
->cmd
.sleep
->us
);
1309 #ifdef _DEBUG_JTAG_IO_
1310 DEBUG("sleep %i usec", cmd
->cmd
.sleep
->us
);
1314 ERROR("BUG: unknown JTAG command type encountered");
1320 if (require_send
> 0)
1321 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1322 retval
= ERROR_JTAG_QUEUE_FAILED
;
1327 #if BUILD_FT2232_FTD2XX == 1
1328 static int ft2232_init_ftd2xx(u16 vid
, u16 pid
, int more
, int *try_more
)
1331 DWORD openex_flags
= 0;
1332 char *openex_string
= NULL
;
1335 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1336 ft2232_layout
, vid
, pid
);
1339 /* Add non-standard Vid/Pid to the linux driver */
1340 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1342 WARNING("couldn't add %4.4x:%4.4x",
1347 if (ft2232_device_desc
&& ft2232_serial
)
1349 WARNING("can't open by device description and serial number, giving precedence to serial");
1350 ft2232_device_desc
= NULL
;
1353 if (ft2232_device_desc
)
1355 openex_string
= ft2232_device_desc
;
1356 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1358 else if (ft2232_serial
)
1360 openex_string
= ft2232_serial
;
1361 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1365 ERROR("neither device description nor serial number specified");
1366 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1368 return ERROR_JTAG_INIT_FAILED
;
1371 if ((status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
)) != FT_OK
)
1376 WARNING("unable to open ftdi device (trying more): %lu",
1379 return ERROR_JTAG_INIT_FAILED
;
1381 ERROR("unable to open ftdi device: %lu", status
);
1382 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1383 if (status
== FT_OK
)
1385 char **desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1388 for (i
= 0; i
< num_devices
; i
++)
1389 desc_array
[i
] = malloc(64);
1390 desc_array
[num_devices
] = NULL
;
1392 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1394 if (status
== FT_OK
)
1396 ERROR("ListDevices: %lu\n", num_devices
);
1397 for (i
= 0; i
< num_devices
; i
++)
1398 ERROR("%i: %s", i
, desc_array
[i
]);
1401 for (i
= 0; i
< num_devices
; i
++)
1402 free(desc_array
[i
]);
1407 ERROR("ListDevices: NONE\n");
1409 return ERROR_JTAG_INIT_FAILED
;
1412 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1414 ERROR("unable to set latency timer: %lu", status
);
1415 return ERROR_JTAG_INIT_FAILED
;
1418 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1420 ERROR("unable to get latency timer: %lu", status
);
1421 return ERROR_JTAG_INIT_FAILED
;
1425 DEBUG("current latency timer: %i", latency_timer
);
1428 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1430 ERROR("unable to set timeouts: %lu", status
);
1431 return ERROR_JTAG_INIT_FAILED
;
1434 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1436 ERROR("unable to enable bit i/o mode: %lu", status
);
1437 return ERROR_JTAG_INIT_FAILED
;
1443 static int ft2232_purge_ftd2xx(void)
1447 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1449 ERROR("error purging ftd2xx device: %lu", status
);
1450 return ERROR_JTAG_INIT_FAILED
;
1455 #endif /* BUILD_FT2232_FTD2XX == 1 */
1457 #if BUILD_FT2232_LIBFTDI == 1
1458 static int ft2232_init_libftdi(u16 vid
, u16 pid
, int more
, int *try_more
)
1462 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1463 ft2232_layout
, vid
, pid
);
1465 if (ftdi_init(&ftdic
) < 0)
1466 return ERROR_JTAG_INIT_FAILED
;
1468 /* context, vendor id, product id */
1469 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
1470 ft2232_serial
) < 0) {
1472 WARNING("unable to open ftdi device (trying more): %s",
1475 ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
1477 return ERROR_JTAG_INIT_FAILED
;
1480 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
1482 ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
1483 return ERROR_JTAG_INIT_FAILED
;
1486 if (ftdi_usb_reset(&ftdic
) < 0)
1488 ERROR("unable to reset ftdi device");
1489 return ERROR_JTAG_INIT_FAILED
;
1492 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
1494 ERROR("unable to set latency timer");
1495 return ERROR_JTAG_INIT_FAILED
;
1498 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
1500 ERROR("unable to get latency timer");
1501 return ERROR_JTAG_INIT_FAILED
;
1505 DEBUG("current latency timer: %i", latency_timer
);
1508 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
1513 static int ft2232_purge_libftdi(void)
1515 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
1517 ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
1518 return ERROR_JTAG_INIT_FAILED
;
1523 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1525 int ft2232_init(void)
1530 ft2232_layout_t
*cur_layout
= ft2232_layouts
;
1533 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
1535 ft2232_layout
= "usbjtag";
1536 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1539 while (cur_layout
->name
)
1541 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
1543 layout
= cur_layout
;
1551 ERROR("No matching layout found for %s", ft2232_layout
);
1552 return ERROR_JTAG_INIT_FAILED
;
1555 for (i
= 0; 1; i
++) {
1557 * "more indicates that there are more IDs to try, so we should
1558 * not print an error for an ID mismatch (but for anything
1561 * try_more indicates that the error code returned indicates an
1562 * ID mismatch (and nothing else) and that we should proceeed
1563 * with the next ID pair.
1565 int more
= ft2232_vid
[i
+1] || ft2232_pid
[i
+1];
1568 #if BUILD_FT2232_FTD2XX == 1
1569 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
1571 #elif BUILD_FT2232_LIBFTDI == 1
1572 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
1577 if (!more
|| !try_more
)
1581 ft2232_buffer_size
= 0;
1582 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
1584 if (layout
->init() != ERROR_OK
)
1585 return ERROR_JTAG_INIT_FAILED
;
1587 ft2232_speed(jtag_speed
);
1589 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1590 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
1592 ERROR("couldn't write to FT2232 to disable loopback");
1593 return ERROR_JTAG_INIT_FAILED
;
1596 #if BUILD_FT2232_FTD2XX == 1
1597 return ft2232_purge_ftd2xx();
1598 #elif BUILD_FT2232_LIBFTDI == 1
1599 return ft2232_purge_libftdi();
1605 int usbjtag_init(void)
1611 low_direction
= 0x0b;
1613 if (strcmp(ft2232_layout
, "usbjtag") == 0)
1620 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
1627 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
1634 low_direction
= 0x8b;
1638 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
1639 return ERROR_JTAG_INIT_FAILED
;
1642 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1644 low_direction
&= ~nTRSTnOE
; /* nTRST input */
1645 low_output
&= ~nTRST
; /* nTRST = 0 */
1649 low_direction
|= nTRSTnOE
; /* nTRST output */
1650 low_output
|= nTRST
; /* nTRST = 1 */
1653 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1655 low_direction
|= nSRSTnOE
; /* nSRST output */
1656 low_output
|= nSRST
; /* nSRST = 1 */
1660 low_direction
&= ~nSRSTnOE
; /* nSRST input */
1661 low_output
&= ~nSRST
; /* nSRST = 0 */
1664 /* initialize low byte for jtag */
1665 buf
[0] = 0x80; /* command "set data bits low byte" */
1666 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1667 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1668 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1670 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1672 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1673 return ERROR_JTAG_INIT_FAILED
;
1679 int jtagkey_init(void)
1685 low_direction
= 0x1b;
1687 /* initialize low byte for jtag */
1688 buf
[0] = 0x80; /* command "set data bits low byte" */
1689 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1690 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1691 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1693 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1695 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1696 return ERROR_JTAG_INIT_FAILED
;
1699 if (strcmp(layout
->name
, "jtagkey") == 0)
1706 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0) ||
1707 (strcmp(layout
->name
, "oocdlink") == 0))
1716 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1721 high_direction
= 0x0f;
1723 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1725 high_output
|= nTRSTnOE
;
1726 high_output
&= ~nTRST
;
1730 high_output
&= ~nTRSTnOE
;
1731 high_output
|= nTRST
;
1734 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1736 high_output
&= ~nSRSTnOE
;
1737 high_output
|= nSRST
;
1741 high_output
|= nSRSTnOE
;
1742 high_output
&= ~nSRST
;
1745 /* initialize high port */
1746 buf
[0] = 0x82; /* command "set data bits high byte" */
1747 buf
[1] = high_output
; /* value */
1748 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1749 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1751 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1753 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1754 return ERROR_JTAG_INIT_FAILED
;
1760 int olimex_jtag_init(void)
1766 low_direction
= 0x1b;
1768 /* initialize low byte for jtag */
1769 buf
[0] = 0x80; /* command "set data bits low byte" */
1770 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1771 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1772 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1774 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1776 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1777 return ERROR_JTAG_INIT_FAILED
;
1783 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1786 high_direction
= 0x0f;
1788 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1790 high_output
|= nTRSTnOE
;
1791 high_output
&= ~nTRST
;
1795 high_output
&= ~nTRSTnOE
;
1796 high_output
|= nTRST
;
1799 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1801 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1805 high_output
&= ~nSRST
;
1808 /* turn red LED on */
1809 high_output
|= 0x08;
1811 /* initialize high port */
1812 buf
[0] = 0x82; /* command "set data bits high byte" */
1813 buf
[1] = high_output
; /* value */
1814 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1815 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1817 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1819 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1820 return ERROR_JTAG_INIT_FAILED
;
1826 int flyswatter_init(void)
1832 low_direction
= 0xfb;
1834 /* initialize low byte for jtag */
1835 buf
[0] = 0x80; /* command "set data bits low byte" */
1836 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1837 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1838 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1840 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1842 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1843 return ERROR_JTAG_INIT_FAILED
;
1847 nTRSTnOE
= 0x0; /* not output enable for nTRST */
1849 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1852 high_direction
= 0x0c;
1854 /* turn red LED1 on, LED2 off */
1855 high_output
|= 0x08;
1857 /* initialize high port */
1858 buf
[0] = 0x82; /* command "set data bits high byte" */
1859 buf
[1] = high_output
; /* value */
1860 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1861 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1863 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1865 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1866 return ERROR_JTAG_INIT_FAILED
;
1872 int turtle_init(void)
1878 low_direction
= 0x5b;
1880 /* initialize low byte for jtag */
1881 buf
[0] = 0x80; /* command "set data bits low byte" */
1882 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1883 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1884 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1886 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1888 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1889 return ERROR_JTAG_INIT_FAILED
;
1895 high_direction
= 0x0C;
1897 /* initialize high port */
1898 buf
[0] = 0x82; /* command "set data bits high byte" */
1899 buf
[1] = high_output
;
1900 buf
[2] = high_direction
;
1901 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1903 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1905 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1906 return ERROR_JTAG_INIT_FAILED
;
1912 int comstick_init(void)
1918 low_direction
= 0x0b;
1920 /* initialize low byte for jtag */
1921 buf
[0] = 0x80; /* command "set data bits low byte" */
1922 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1923 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1924 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1926 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1928 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1929 return ERROR_JTAG_INIT_FAILED
;
1933 nTRSTnOE
= 0x00; /* no output enable for nTRST */
1935 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1938 high_direction
= 0x03;
1940 /* initialize high port */
1941 buf
[0] = 0x82; /* command "set data bits high byte" */
1942 buf
[1] = high_output
;
1943 buf
[2] = high_direction
;
1944 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1946 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1948 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1949 return ERROR_JTAG_INIT_FAILED
;
1955 int stm32stick_init(void)
1961 low_direction
= 0x8b;
1963 /* initialize low byte for jtag */
1964 buf
[0] = 0x80; /* command "set data bits low byte" */
1965 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1966 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1967 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1969 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1971 ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1972 return ERROR_JTAG_INIT_FAILED
;
1976 nTRSTnOE
= 0x00; /* no output enable for nTRST */
1978 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1981 high_direction
= 0x03;
1983 /* initialize high port */
1984 buf
[0] = 0x82; /* command "set data bits high byte" */
1985 buf
[1] = high_output
;
1986 buf
[2] = high_direction
;
1987 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1989 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1991 ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1992 return ERROR_JTAG_INIT_FAILED
;
1998 void olimex_jtag_blink(void)
2000 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2001 * ACBUS3 is bit 3 of the GPIOH port
2003 if (high_output
& 0x08)
2005 /* set port pin high */
2006 high_output
&= 0x07;
2010 /* set port pin low */
2011 high_output
|= 0x08;
2015 BUFFER_ADD
= high_output
;
2016 BUFFER_ADD
= high_direction
;
2019 void turtle_jtag_blink(void)
2022 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2024 if (high_output
& 0x08)
2034 BUFFER_ADD
= high_output
;
2035 BUFFER_ADD
= high_direction
;
2039 int ft2232_quit(void)
2041 #if BUILD_FT2232_FTD2XX == 1
2044 status
= FT_Close(ftdih
);
2045 #elif BUILD_FT2232_LIBFTDI == 1
2046 ftdi_disable_bitbang(&ftdic
);
2048 ftdi_usb_close(&ftdic
);
2050 ftdi_deinit(&ftdic
);
2053 free(ft2232_buffer
);
2054 ft2232_buffer
= NULL
;
2059 int ft2232_handle_device_desc_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2063 ft2232_device_desc
= strdup(args
[0]);
2067 ERROR("expected exactly one argument to ft2232_device_desc <description>");
2073 int ft2232_handle_serial_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2077 ft2232_serial
= strdup(args
[0]);
2081 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2087 int ft2232_handle_layout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2092 ft2232_layout
= malloc(strlen(args
[0]) + 1);
2093 strcpy(ft2232_layout
, args
[0]);
2098 int ft2232_handle_vid_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2102 if (argc
> MAX_USB_IDS
*2) {
2103 WARNING("ignoring extra IDs in ft2232_vid_pid "
2104 "(maximum is %d pairs)", MAX_USB_IDS
);
2105 argc
= MAX_USB_IDS
*2;
2107 if (argc
< 2 || (argc
& 1))
2109 WARNING("incomplete ft2232_vid_pid configuration directive");
2114 for (i
= 0; i
+1 < argc
; i
+= 2) {
2115 ft2232_vid
[i
>> 1] = strtol(args
[i
], NULL
, 0);
2116 ft2232_pid
[i
>> 1] = strtol(args
[i
+1], NULL
, 0);
2119 * Explicitly terminate, in case there are multiples instances of
2122 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2127 int ft2232_handle_latency_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2131 ft2232_latency
= atoi(args
[0]);
2135 ERROR("expected exactly one argument to ft2232_latency <ms>");