1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <helper/time_support.h>
92 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
93 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
94 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
95 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
98 /* FT2232 access library includes */
99 #if BUILD_FT2232_FTD2XX == 1
111 #elif BUILD_FT2232_LIBFTDI == 1
115 /* max TCK for the high speed devices 30000 kHz */
116 #define FTDI_2232H_4232H_MAX_TCK 30000
117 /* max TCK for the full speed devices 6000 kHz */
118 #define FTDI_2232C_MAX_TCK 6000
119 /* this speed value tells that RTCK is requested */
120 #define RTCK_SPEED -1
123 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
124 * errors with a retry count of 100. Increasing it solves the problem for me.
127 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
128 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
131 #define LIBFTDI_READ_RETRY_COUNT 2000
133 #ifndef BUILD_FT2232_HIGHSPEED
134 #if BUILD_FT2232_FTD2XX == 1
135 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
136 #elif BUILD_FT2232_LIBFTDI == 1
137 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
142 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
143 * stable state. Calling code must ensure that current state is stable,
144 * that verification is not done in here.
146 * @param num_cycles The number of clocks cycles to send.
147 * @param cmd The command to send.
149 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
151 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
153 static char * ft2232_device_desc_A
= NULL
;
154 static char* ft2232_device_desc
= NULL
;
155 static char* ft2232_serial
= NULL
;
156 static uint8_t ft2232_latency
= 2;
157 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
159 #define MAX_USB_IDS 8
160 /* vid = pid = 0 marks the end of the list */
161 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
162 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
164 struct ft2232_layout
{
167 void (*reset
)(int trst
, int srst
);
172 /* init procedures for supported layouts */
173 static int usbjtag_init(void);
174 static int jtagkey_init(void);
175 static int olimex_jtag_init(void);
176 static int flyswatter_init(void);
177 static int turtle_init(void);
178 static int comstick_init(void);
179 static int stm32stick_init(void);
180 static int axm0432_jtag_init(void);
181 static int sheevaplug_init(void);
182 static int icebear_jtag_init(void);
183 static int cortino_jtag_init(void);
184 static int signalyzer_h_init(void);
185 static int ktlink_init(void);
186 static int redbee_init(void);
188 /* reset procedures for supported layouts */
189 static void usbjtag_reset(int trst
, int srst
);
190 static void jtagkey_reset(int trst
, int srst
);
191 static void olimex_jtag_reset(int trst
, int srst
);
192 static void flyswatter_reset(int trst
, int srst
);
193 static void turtle_reset(int trst
, int srst
);
194 static void comstick_reset(int trst
, int srst
);
195 static void stm32stick_reset(int trst
, int srst
);
196 static void axm0432_jtag_reset(int trst
, int srst
);
197 static void sheevaplug_reset(int trst
, int srst
);
198 static void icebear_jtag_reset(int trst
, int srst
);
199 static void signalyzer_h_reset(int trst
, int srst
);
200 static void ktlink_reset(int trst
, int srst
);
201 static void redbee_reset(int trst
, int srst
);
203 /* blink procedures for layouts that support a blinking led */
204 static void olimex_jtag_blink(void);
205 static void flyswatter_jtag_blink(void);
206 static void turtle_jtag_blink(void);
207 static void signalyzer_h_blink(void);
208 static void ktlink_blink(void);
210 static const struct ft2232_layout ft2232_layouts
[] =
213 .init
= usbjtag_init
,
214 .reset
= usbjtag_reset
,
217 .init
= jtagkey_init
,
218 .reset
= jtagkey_reset
,
220 { .name
= "jtagkey_prototype_v1",
221 .init
= jtagkey_init
,
222 .reset
= jtagkey_reset
,
224 { .name
= "oocdlink",
225 .init
= jtagkey_init
,
226 .reset
= jtagkey_reset
,
228 { .name
= "signalyzer",
229 .init
= usbjtag_init
,
230 .reset
= usbjtag_reset
,
232 { .name
= "evb_lm3s811",
233 .init
= usbjtag_init
,
234 .reset
= usbjtag_reset
,
236 { .name
= "luminary_icdi",
237 .init
= usbjtag_init
,
238 .reset
= usbjtag_reset
,
240 { .name
= "olimex-jtag",
241 .init
= olimex_jtag_init
,
242 .reset
= olimex_jtag_reset
,
243 .blink
= olimex_jtag_blink
245 { .name
= "flyswatter",
246 .init
= flyswatter_init
,
247 .reset
= flyswatter_reset
,
248 .blink
= flyswatter_jtag_blink
250 { .name
= "turtelizer2",
252 .reset
= turtle_reset
,
253 .blink
= turtle_jtag_blink
255 { .name
= "comstick",
256 .init
= comstick_init
,
257 .reset
= comstick_reset
,
259 { .name
= "stm32stick",
260 .init
= stm32stick_init
,
261 .reset
= stm32stick_reset
,
263 { .name
= "axm0432_jtag",
264 .init
= axm0432_jtag_init
,
265 .reset
= axm0432_jtag_reset
,
267 { .name
= "sheevaplug",
268 .init
= sheevaplug_init
,
269 .reset
= sheevaplug_reset
,
272 .init
= icebear_jtag_init
,
273 .reset
= icebear_jtag_reset
,
276 .init
= cortino_jtag_init
,
277 .reset
= comstick_reset
,
279 { .name
= "signalyzer-h",
280 .init
= signalyzer_h_init
,
281 .reset
= signalyzer_h_reset
,
282 .blink
= signalyzer_h_blink
286 .reset
= ktlink_reset
,
287 .blink
= ktlink_blink
289 { .name
= "redbee-econotag",
291 .reset
= redbee_reset
,
293 { .name
= "redbee-usb",
295 .reset
= redbee_reset
,
296 .channel
= INTERFACE_B
,
298 { .name
= NULL
, /* END OF TABLE */ },
301 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
303 /** the layout being used with this debug session */
304 static const struct ft2232_layout
*layout
;
306 static uint8_t low_output
= 0x0;
307 static uint8_t low_direction
= 0x0;
308 static uint8_t high_output
= 0x0;
309 static uint8_t high_direction
= 0x0;
311 #if BUILD_FT2232_FTD2XX == 1
312 static FT_HANDLE ftdih
= NULL
;
313 static FT_DEVICE ftdi_device
= 0;
314 #elif BUILD_FT2232_LIBFTDI == 1
315 static struct ftdi_context ftdic
;
316 static enum ftdi_chip_type ftdi_device
;
319 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
320 static int require_send
;
322 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
324 "There is a significant difference between libftdi and libftd2xx. The latter
325 one allows to schedule up to 64*64 bytes of result data while libftdi fails
326 with more than 4*64. As a consequence, the FT2232 driver is forced to
327 perform around 16x more USB transactions for long command streams with TDO
328 capture when running with libftdi."
331 #define FT2232_BUFFER_SIZE 131072
332 a comment would have been nice.
335 #define FT2232_BUFFER_SIZE 131072
337 static uint8_t* ft2232_buffer
= NULL
;
338 static int ft2232_buffer_size
= 0;
339 static int ft2232_read_pointer
= 0;
340 static int ft2232_expect_read
= 0;
343 * Function buffer_write
344 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
345 * @param val is the byte to send.
347 static inline void buffer_write(uint8_t val
)
349 assert(ft2232_buffer
);
350 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
351 ft2232_buffer
[ft2232_buffer_size
++] = val
;
355 * Function buffer_read
356 * returns a byte from the byte buffer.
358 static inline uint8_t buffer_read(void)
360 assert(ft2232_buffer
);
361 assert(ft2232_read_pointer
< ft2232_buffer_size
);
362 return ft2232_buffer
[ft2232_read_pointer
++];
366 * Clocks out \a bit_count bits on the TMS line, starting with the least
367 * significant bit of tms_bits and progressing to more significant bits.
368 * Rigorous state transition logging is done here via tap_set_state().
370 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
371 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
372 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
373 * is often used for this, 0x4b.
375 * @param tms_bits Holds the sequence of bits to send.
376 * @param tms_count Tells how many bits in the sequence.
377 * @param tdi_bit A single bit to pass on to TDI before the first TCK
378 * cycle and held static for the duration of TMS clocking.
380 * See the MPSSE spec referenced above.
382 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
386 int tms_ndx
; /* bit index into tms_byte */
388 assert(tms_count
> 0);
390 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
391 mpsse_cmd
, tms_bits
, tms_count
);
393 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
395 bool bit
= tms_bits
& 1;
398 tms_byte
|= (1 << tms_ndx
);
400 /* always do state transitions in public view */
401 tap_set_state(tap_state_transition(tap_get_state(), bit
));
403 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
408 if (tms_ndx
== 7 || i
== tms_count
-1)
410 buffer_write(mpsse_cmd
);
411 buffer_write(tms_ndx
- 1);
413 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
414 TMS/CS and is held static for the duration of TMS/CS clocking.
416 buffer_write(tms_byte
| (tdi_bit
<< 7));
422 * Function get_tms_buffer_requirements
423 * returns what clock_tms() will consume if called with
426 static inline int get_tms_buffer_requirements(int bit_count
)
428 return ((bit_count
+ 6)/7) * 3;
432 * Function move_to_state
433 * moves the TAP controller from the current state to a
434 * \a goal_state through a path given by tap_get_tms_path(). State transition
435 * logging is performed by delegation to clock_tms().
437 * @param goal_state is the destination state for the move.
439 static void move_to_state(tap_state_t goal_state
)
441 tap_state_t start_state
= tap_get_state();
443 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
444 lookup of the required TMS pattern to move to this state from the
448 /* do the 2 lookups */
449 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
450 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
452 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
454 clock_tms(0x4b, tms_bits
, tms_count
, 0);
457 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
459 #if BUILD_FT2232_FTD2XX == 1
461 DWORD dw_bytes_written
;
462 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
464 *bytes_written
= dw_bytes_written
;
465 LOG_ERROR("FT_Write returned: %lu", status
);
466 return ERROR_JTAG_DEVICE_ERROR
;
470 *bytes_written
= dw_bytes_written
;
473 #elif BUILD_FT2232_LIBFTDI == 1
475 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
478 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
479 return ERROR_JTAG_DEVICE_ERROR
;
483 *bytes_written
= retval
;
489 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
491 #if BUILD_FT2232_FTD2XX == 1
497 while ((*bytes_read
< size
) && timeout
--)
499 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
500 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
503 LOG_ERROR("FT_Read returned: %lu", status
);
504 return ERROR_JTAG_DEVICE_ERROR
;
506 *bytes_read
+= dw_bytes_read
;
509 #elif BUILD_FT2232_LIBFTDI == 1
511 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
514 while ((*bytes_read
< size
) && timeout
--)
516 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
519 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
520 return ERROR_JTAG_DEVICE_ERROR
;
522 *bytes_read
+= retval
;
527 if (*bytes_read
< size
)
529 LOG_ERROR("couldn't read enough bytes from "
530 "FT2232 device (%i < %i)",
531 (unsigned)*bytes_read
,
533 return ERROR_JTAG_DEVICE_ERROR
;
539 static bool ft2232_device_is_highspeed(void)
541 #if BUILD_FT2232_FTD2XX == 1
542 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
543 #elif BUILD_FT2232_LIBFTDI == 1
544 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
549 * Commands that only apply to the FT2232H and FT4232H devices.
550 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
551 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
554 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
556 uint8_t buf
= enable
? 0x96 : 0x97;
557 LOG_DEBUG("%2.2x", buf
);
559 uint32_t bytes_written
;
560 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
561 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
563 LOG_ERROR("couldn't write command to %s adaptive clocking"
564 , enable
? "enable" : "disable");
572 * Enable/disable the clk divide by 5 of the 60MHz master clock.
573 * This result in a JTAG clock speed range of 91.553Hz-6MHz
574 * respective 457.763Hz-30MHz.
576 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
578 uint32_t bytes_written
;
579 uint8_t buf
= enable
? 0x8b : 0x8a;
580 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
581 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
583 LOG_ERROR("couldn't write command to %s clk divide by 5"
584 , enable
? "enable" : "disable");
585 return ERROR_JTAG_INIT_FAILED
;
587 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
588 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
593 static int ft2232_speed(int speed
)
597 uint32_t bytes_written
;
600 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
601 if (ft2232_device_is_highspeed())
602 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
603 else if (enable_adaptive_clocking
)
605 LOG_ERROR("ft2232 device %lu does not support RTCK"
606 , (long unsigned int)ftdi_device
);
610 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
613 buf
[0] = 0x86; /* command "set divisor" */
614 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
615 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
617 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
618 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
620 LOG_ERROR("couldn't set FT2232 TCK speed");
627 static int ft2232_speed_div(int speed
, int* khz
)
629 /* Take a look in the FT2232 manual,
630 * AN2232C-01 Command Processor for
631 * MPSSE and MCU Host Bus. Chapter 3.8 */
633 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
638 static int ft2232_khz(int khz
, int* jtag_speed
)
642 if (ft2232_device_is_highspeed())
644 *jtag_speed
= RTCK_SPEED
;
649 LOG_DEBUG("RCLK not supported");
654 /* Take a look in the FT2232 manual,
655 * AN2232C-01 Command Processor for
656 * MPSSE and MCU Host Bus. Chapter 3.8
658 * We will calc here with a multiplier
659 * of 10 for better rounding later. */
661 /* Calc speed, (ft2232_max_tck / khz) - 1 */
662 /* Use 65000 for better rounding */
663 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
665 /* Add 0.9 for rounding */
668 /* Calc real speed */
669 *jtag_speed
= *jtag_speed
/ 10;
671 /* Check if speed is greater than 0 */
677 /* Check max value */
678 if (*jtag_speed
> 0xFFFF)
680 *jtag_speed
= 0xFFFF;
686 static void ft2232_end_state(tap_state_t state
)
688 if (tap_is_state_stable(state
))
689 tap_set_end_state(state
);
692 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
697 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
699 int num_bytes
= (scan_size
+ 7) / 8;
700 int bits_left
= scan_size
;
703 while (num_bytes
-- > 1)
705 buffer
[cur_byte
++] = buffer_read();
709 buffer
[cur_byte
] = 0x0;
711 /* There is one more partial byte left from the clock data in/out instructions */
714 buffer
[cur_byte
] = buffer_read() >> 1;
716 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
717 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
720 static void ft2232_debug_dump_buffer(void)
726 for (i
= 0; i
< ft2232_buffer_size
; i
++)
728 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
731 LOG_DEBUG("%s", line
);
737 LOG_DEBUG("%s", line
);
740 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
742 struct jtag_command
* cmd
;
747 uint32_t bytes_written
= 0;
748 uint32_t bytes_read
= 0;
750 #ifdef _DEBUG_USB_IO_
751 struct timeval start
, inter
, inter2
, end
;
752 struct timeval d_inter
, d_inter2
, d_end
;
755 #ifdef _DEBUG_USB_COMMS_
756 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
757 ft2232_debug_dump_buffer();
760 #ifdef _DEBUG_USB_IO_
761 gettimeofday(&start
, NULL
);
764 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
766 LOG_ERROR("couldn't write MPSSE commands to FT2232");
770 #ifdef _DEBUG_USB_IO_
771 gettimeofday(&inter
, NULL
);
774 if (ft2232_expect_read
)
776 /* FIXME this "timeout" is never changed ... */
777 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
778 ft2232_buffer_size
= 0;
780 #ifdef _DEBUG_USB_IO_
781 gettimeofday(&inter2
, NULL
);
784 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
786 LOG_ERROR("couldn't read from FT2232");
790 #ifdef _DEBUG_USB_IO_
791 gettimeofday(&end
, NULL
);
793 timeval_subtract(&d_inter
, &inter
, &start
);
794 timeval_subtract(&d_inter2
, &inter2
, &start
);
795 timeval_subtract(&d_end
, &end
, &start
);
797 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
798 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
799 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
800 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
803 ft2232_buffer_size
= bytes_read
;
805 if (ft2232_expect_read
!= ft2232_buffer_size
)
807 LOG_ERROR("ft2232_expect_read (%i) != "
808 "ft2232_buffer_size (%i) "
812 LIBFTDI_READ_RETRY_COUNT
- timeout
);
813 ft2232_debug_dump_buffer();
818 #ifdef _DEBUG_USB_COMMS_
819 LOG_DEBUG("read buffer (%i retries): %i bytes",
820 LIBFTDI_READ_RETRY_COUNT
- timeout
,
822 ft2232_debug_dump_buffer();
826 ft2232_expect_read
= 0;
827 ft2232_read_pointer
= 0;
829 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
830 * that wasn't handled by a caller-provided error handler
840 type
= jtag_scan_type(cmd
->cmd
.scan
);
841 if (type
!= SCAN_OUT
)
843 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
844 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
845 ft2232_read_scan(type
, buffer
, scan_size
);
846 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
847 retval
= ERROR_JTAG_QUEUE_FAILED
;
859 ft2232_buffer_size
= 0;
865 * Function ft2232_add_pathmove
866 * moves the TAP controller from the current state to a new state through the
867 * given path, where path is an array of tap_state_t's.
869 * @param path is an array of tap_stat_t which gives the states to traverse through
870 * ending with the last state at path[num_states-1]
871 * @param num_states is the count of state steps to move through
873 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
877 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
881 /* this loop verifies that the path is legal and logs each state in the path */
884 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
886 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
888 /* command "Clock Data to TMS/CS Pin (no Read)" */
891 /* number of states remaining */
892 buffer_write(num_states_batch
- 1);
894 while (num_states_batch
--) {
895 /* either TMS=0 or TMS=1 must work ... */
896 if (tap_state_transition(tap_get_state(), false)
897 == path
[state_count
])
898 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
899 else if (tap_state_transition(tap_get_state(), true)
900 == path
[state_count
])
901 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
903 /* ... or else the caller goofed BADLY */
905 LOG_ERROR("BUG: %s -> %s isn't a valid "
906 "TAP state transition",
907 tap_state_name(tap_get_state()),
908 tap_state_name(path
[state_count
]));
912 tap_set_state(path
[state_count
]);
917 buffer_write(tms_byte
);
919 tap_set_end_state(tap_get_state());
922 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
924 int num_bytes
= (scan_size
+ 7) / 8;
925 int bits_left
= scan_size
;
931 if (tap_get_state() != TAP_DRSHIFT
)
933 move_to_state(TAP_DRSHIFT
);
938 if (tap_get_state() != TAP_IRSHIFT
)
940 move_to_state(TAP_IRSHIFT
);
944 /* add command for complete bytes */
945 while (num_bytes
> 1)
950 /* Clock Data Bytes In and Out LSB First */
952 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
954 else if (type
== SCAN_OUT
)
956 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
958 /* LOG_DEBUG("added TDI bytes (o)"); */
960 else if (type
== SCAN_IN
)
962 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
964 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
967 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
968 num_bytes
-= thisrun_bytes
;
970 buffer_write((uint8_t) (thisrun_bytes
- 1));
971 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
975 /* add complete bytes */
976 while (thisrun_bytes
-- > 0)
978 buffer_write(buffer
[cur_byte
++]);
982 else /* (type == SCAN_IN) */
984 bits_left
-= 8 * (thisrun_bytes
);
988 /* the most signifcant bit is scanned during TAP movement */
990 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
994 /* process remaining bits but the last one */
999 /* Clock Data Bits In and Out LSB First */
1001 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1003 else if (type
== SCAN_OUT
)
1005 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1007 /* LOG_DEBUG("added TDI bits (o)"); */
1009 else if (type
== SCAN_IN
)
1011 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1013 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1016 buffer_write(bits_left
- 2);
1017 if (type
!= SCAN_IN
)
1018 buffer_write(buffer
[cur_byte
]);
1021 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1022 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1024 if (type
== SCAN_IO
)
1026 /* Clock Data Bits In and Out LSB First */
1028 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1030 else if (type
== SCAN_OUT
)
1032 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1034 /* LOG_DEBUG("added TDI bits (o)"); */
1036 else if (type
== SCAN_IN
)
1038 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1040 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1043 buffer_write(last_bit
);
1051 /* move from Shift-IR/DR to end state */
1052 if (type
!= SCAN_OUT
)
1054 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1055 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1058 /* Clock Data to TMS/CS Pin with Read */
1063 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1064 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1065 /* Clock Data to TMS/CS Pin (no Read) */
1069 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1070 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1073 if (tap_get_state() != tap_get_end_state())
1075 move_to_state(tap_get_end_state());
1079 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1081 int num_bytes
= (scan_size
+ 7) / 8;
1082 int bits_left
= scan_size
;
1085 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1086 uint8_t* receive_pointer
= receive_buffer
;
1087 uint32_t bytes_written
;
1088 uint32_t bytes_read
;
1090 int thisrun_read
= 0;
1094 LOG_ERROR("BUG: large IR scans are not supported");
1098 if (tap_get_state() != TAP_DRSHIFT
)
1100 move_to_state(TAP_DRSHIFT
);
1103 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1105 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1108 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1109 ft2232_buffer_size
, (int)bytes_written
);
1110 ft2232_buffer_size
= 0;
1112 /* add command for complete bytes */
1113 while (num_bytes
> 1)
1117 if (type
== SCAN_IO
)
1119 /* Clock Data Bytes In and Out LSB First */
1121 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1123 else if (type
== SCAN_OUT
)
1125 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1127 /* LOG_DEBUG("added TDI bytes (o)"); */
1129 else if (type
== SCAN_IN
)
1131 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1133 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1136 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1137 thisrun_read
= thisrun_bytes
;
1138 num_bytes
-= thisrun_bytes
;
1139 buffer_write((uint8_t) (thisrun_bytes
- 1));
1140 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1142 if (type
!= SCAN_IN
)
1144 /* add complete bytes */
1145 while (thisrun_bytes
-- > 0)
1147 buffer_write(buffer
[cur_byte
]);
1152 else /* (type == SCAN_IN) */
1154 bits_left
-= 8 * (thisrun_bytes
);
1157 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1159 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1162 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1164 (int)bytes_written
);
1165 ft2232_buffer_size
= 0;
1167 if (type
!= SCAN_OUT
)
1169 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1171 LOG_ERROR("couldn't read from FT2232");
1174 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1177 receive_pointer
+= bytes_read
;
1183 /* the most signifcant bit is scanned during TAP movement */
1184 if (type
!= SCAN_IN
)
1185 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1189 /* process remaining bits but the last one */
1192 if (type
== SCAN_IO
)
1194 /* Clock Data Bits In and Out LSB First */
1196 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1198 else if (type
== SCAN_OUT
)
1200 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1202 /* LOG_DEBUG("added TDI bits (o)"); */
1204 else if (type
== SCAN_IN
)
1206 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1208 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1210 buffer_write(bits_left
- 2);
1211 if (type
!= SCAN_IN
)
1212 buffer_write(buffer
[cur_byte
]);
1214 if (type
!= SCAN_OUT
)
1218 if (tap_get_end_state() == TAP_DRSHIFT
)
1220 if (type
== SCAN_IO
)
1222 /* Clock Data Bits In and Out LSB First */
1224 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1226 else if (type
== SCAN_OUT
)
1228 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1230 /* LOG_DEBUG("added TDI bits (o)"); */
1232 else if (type
== SCAN_IN
)
1234 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1236 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1239 buffer_write(last_bit
);
1243 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1244 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1247 /* move from Shift-IR/DR to end state */
1248 if (type
!= SCAN_OUT
)
1250 /* Clock Data to TMS/CS Pin with Read */
1252 /* LOG_DEBUG("added TMS scan (read)"); */
1256 /* Clock Data to TMS/CS Pin (no Read) */
1258 /* LOG_DEBUG("added TMS scan (no read)"); */
1261 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1262 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1265 if (type
!= SCAN_OUT
)
1268 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1270 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1273 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1275 (int)bytes_written
);
1276 ft2232_buffer_size
= 0;
1278 if (type
!= SCAN_OUT
)
1280 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1282 LOG_ERROR("couldn't read from FT2232");
1285 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1288 receive_pointer
+= bytes_read
;
1294 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1296 int predicted_size
= 3;
1297 int num_bytes
= (scan_size
- 1) / 8;
1299 if (tap_get_state() != TAP_DRSHIFT
)
1300 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1302 if (type
== SCAN_IN
) /* only from device to host */
1304 /* complete bytes */
1305 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1307 /* remaining bits - 1 (up to 7) */
1308 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1310 else /* host to device, or bidirectional */
1312 /* complete bytes */
1313 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1315 /* remaining bits -1 (up to 7) */
1316 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1319 return predicted_size
;
1322 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1324 int predicted_size
= 0;
1326 if (type
!= SCAN_OUT
)
1328 /* complete bytes */
1329 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1331 /* remaining bits - 1 */
1332 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1334 /* last bit (from TMS scan) */
1335 predicted_size
+= 1;
1338 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1340 return predicted_size
;
1343 static void usbjtag_reset(int trst
, int srst
)
1345 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1348 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1349 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1351 low_output
&= ~nTRST
; /* switch output low */
1355 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1356 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1358 low_output
|= nTRST
; /* switch output high */
1363 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1364 low_output
&= ~nSRST
; /* switch output low */
1366 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1370 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1371 low_output
|= nSRST
; /* switch output high */
1373 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1376 /* command "set data bits low byte" */
1378 buffer_write(low_output
);
1379 buffer_write(low_direction
);
1382 static void jtagkey_reset(int trst
, int srst
)
1384 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1387 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1388 high_output
&= ~nTRSTnOE
;
1390 high_output
&= ~nTRST
;
1394 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1395 high_output
|= nTRSTnOE
;
1397 high_output
|= nTRST
;
1402 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1403 high_output
&= ~nSRST
;
1405 high_output
&= ~nSRSTnOE
;
1409 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1410 high_output
|= nSRST
;
1412 high_output
|= nSRSTnOE
;
1415 /* command "set data bits high byte" */
1417 buffer_write(high_output
);
1418 buffer_write(high_direction
);
1419 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1423 static void olimex_jtag_reset(int trst
, int srst
)
1425 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1428 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1429 high_output
&= ~nTRSTnOE
;
1431 high_output
&= ~nTRST
;
1435 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1436 high_output
|= nTRSTnOE
;
1438 high_output
|= nTRST
;
1443 high_output
|= nSRST
;
1447 high_output
&= ~nSRST
;
1450 /* command "set data bits high byte" */
1452 buffer_write(high_output
);
1453 buffer_write(high_direction
);
1454 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1458 static void axm0432_jtag_reset(int trst
, int srst
)
1462 tap_set_state(TAP_RESET
);
1463 high_output
&= ~nTRST
;
1467 high_output
|= nTRST
;
1472 high_output
&= ~nSRST
;
1476 high_output
|= nSRST
;
1479 /* command "set data bits low byte" */
1481 buffer_write(high_output
);
1482 buffer_write(high_direction
);
1483 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1487 static void flyswatter_reset(int trst
, int srst
)
1491 low_output
&= ~nTRST
;
1495 low_output
|= nTRST
;
1500 low_output
|= nSRST
;
1504 low_output
&= ~nSRST
;
1507 /* command "set data bits low byte" */
1509 buffer_write(low_output
);
1510 buffer_write(low_direction
);
1511 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1514 static void turtle_reset(int trst
, int srst
)
1520 low_output
|= nSRST
;
1524 low_output
&= ~nSRST
;
1527 /* command "set data bits low byte" */
1529 buffer_write(low_output
);
1530 buffer_write(low_direction
);
1531 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1534 static void comstick_reset(int trst
, int srst
)
1538 high_output
&= ~nTRST
;
1542 high_output
|= nTRST
;
1547 high_output
&= ~nSRST
;
1551 high_output
|= nSRST
;
1554 /* command "set data bits high byte" */
1556 buffer_write(high_output
);
1557 buffer_write(high_direction
);
1558 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1562 static void stm32stick_reset(int trst
, int srst
)
1566 high_output
&= ~nTRST
;
1570 high_output
|= nTRST
;
1575 low_output
&= ~nSRST
;
1579 low_output
|= nSRST
;
1582 /* command "set data bits low byte" */
1584 buffer_write(low_output
);
1585 buffer_write(low_direction
);
1587 /* command "set data bits high byte" */
1589 buffer_write(high_output
);
1590 buffer_write(high_direction
);
1591 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1595 static void sheevaplug_reset(int trst
, int srst
)
1598 high_output
&= ~nTRST
;
1600 high_output
|= nTRST
;
1603 high_output
&= ~nSRSTnOE
;
1605 high_output
|= nSRSTnOE
;
1607 /* command "set data bits high byte" */
1609 buffer_write(high_output
);
1610 buffer_write(high_direction
);
1611 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1614 static void redbee_reset(int trst
, int srst
)
1618 tap_set_state(TAP_RESET
);
1619 high_output
&= ~nTRST
;
1623 high_output
|= nTRST
;
1628 high_output
&= ~nSRST
;
1632 high_output
|= nSRST
;
1635 /* command "set data bits low byte" */
1637 buffer_write(high_output
);
1638 buffer_write(high_direction
);
1639 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1640 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1644 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1648 int predicted_size
= 0;
1651 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1652 cmd
->cmd
.runtest
->num_cycles
,
1653 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1655 /* only send the maximum buffer size that FT2232C can handle */
1657 if (tap_get_state() != TAP_IDLE
)
1658 predicted_size
+= 3;
1659 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1660 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1661 predicted_size
+= 3;
1662 if (tap_get_end_state() != TAP_IDLE
)
1663 predicted_size
+= 3;
1664 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1666 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1667 retval
= ERROR_JTAG_QUEUE_FAILED
;
1671 if (tap_get_state() != TAP_IDLE
)
1673 move_to_state(TAP_IDLE
);
1676 i
= cmd
->cmd
.runtest
->num_cycles
;
1679 /* there are no state transitions in this code, so omit state tracking */
1681 /* command "Clock Data to TMS/CS Pin (no Read)" */
1685 buffer_write((i
> 7) ? 6 : (i
- 1));
1690 i
-= (i
> 7) ? 7 : i
;
1691 /* LOG_DEBUG("added TMS scan (no read)"); */
1694 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1696 if (tap_get_state() != tap_get_end_state())
1698 move_to_state(tap_get_end_state());
1702 DEBUG_JTAG_IO("runtest: %i, end in %s",
1703 cmd
->cmd
.runtest
->num_cycles
,
1704 tap_state_name(tap_get_end_state()));
1708 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1710 int predicted_size
= 0;
1711 int retval
= ERROR_OK
;
1713 DEBUG_JTAG_IO("statemove end in %s",
1714 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1716 /* only send the maximum buffer size that FT2232C can handle */
1718 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1720 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1721 retval
= ERROR_JTAG_QUEUE_FAILED
;
1725 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1727 /* For TAP_RESET, ignore the current recorded state. It's often
1728 * wrong at server startup, and this transation is critical whenever
1731 if (tap_get_end_state() == TAP_RESET
) {
1732 clock_tms(0x4b, 0xff, 5, 0);
1735 /* shortest-path move to desired end state */
1736 } else if (tap_get_state() != tap_get_end_state())
1738 move_to_state(tap_get_end_state());
1746 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1747 * (or SWD) state machine.
1749 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1751 int retval
= ERROR_OK
;
1752 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1753 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1756 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1758 /* only send the maximum buffer size that FT2232C can handle */
1759 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1760 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1761 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1762 retval
= ERROR_JTAG_QUEUE_FAILED
;
1768 /* Shift out in batches of at most 6 bits; there's a report of an
1769 * FT2232 bug in this area, where shifting exactly 7 bits can make
1770 * problems with TMS signaling for the last clock cycle:
1772 * http://developer.intra2net.com/mailarchive/html/
1773 * libftdi/2009/msg00292.html
1775 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1777 * Note that pathmoves in JTAG are not often seven bits, so that
1778 * isn't a particularly likely situation outside of "special"
1779 * signaling such as switching between JTAG and SWD modes.
1782 if (num_bits
<= 6) {
1784 buffer_write(num_bits
- 1);
1785 buffer_write(*bits
& 0x3f);
1789 /* Yes, this is lazy ... we COULD shift out more data
1790 * bits per operation, but doing it in nybbles is easy
1794 buffer_write(*bits
& 0xf);
1797 count
= (num_bits
> 4) ? 4 : num_bits
;
1800 buffer_write(count
- 1);
1801 buffer_write((*bits
>> 4) & 0xf);
1811 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1813 int predicted_size
= 0;
1814 int retval
= ERROR_OK
;
1816 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1817 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1819 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1820 tap_state_name(tap_get_state()),
1821 tap_state_name(path
[num_states
-1]));
1823 /* only send the maximum buffer size that FT2232C can handle */
1824 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1825 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1827 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1828 retval
= ERROR_JTAG_QUEUE_FAILED
;
1834 ft2232_add_pathmove(path
, num_states
);
1840 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1843 int scan_size
; /* size of IR or DR scan */
1844 int predicted_size
= 0;
1845 int retval
= ERROR_OK
;
1847 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1849 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1851 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1853 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1854 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1856 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1857 /* unsent commands before this */
1858 if (first_unsent
!= cmd
)
1859 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1860 retval
= ERROR_JTAG_QUEUE_FAILED
;
1862 /* current command */
1863 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1864 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1866 first_unsent
= cmd
->next
;
1871 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1873 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1876 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1877 retval
= ERROR_JTAG_QUEUE_FAILED
;
1881 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1882 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1883 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1884 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1888 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1889 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1890 tap_state_name(tap_get_end_state()));
1895 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1898 int predicted_size
= 0;
1901 DEBUG_JTAG_IO("reset trst: %i srst %i",
1902 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1904 /* only send the maximum buffer size that FT2232C can handle */
1906 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1908 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1909 retval
= ERROR_JTAG_QUEUE_FAILED
;
1914 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1916 tap_set_state(TAP_RESET
);
1919 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1922 DEBUG_JTAG_IO("trst: %i, srst: %i",
1923 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1927 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1932 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1934 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1935 retval
= ERROR_JTAG_QUEUE_FAILED
;
1936 first_unsent
= cmd
->next
;
1937 jtag_sleep(cmd
->cmd
.sleep
->us
);
1938 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1940 tap_state_name(tap_get_state()));
1944 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1949 /* this is only allowed while in a stable state. A check for a stable
1950 * state was done in jtag_add_clocks()
1952 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1953 retval
= ERROR_JTAG_QUEUE_FAILED
;
1954 DEBUG_JTAG_IO("clocks %i while in %s",
1955 cmd
->cmd
.stableclocks
->num_cycles
,
1956 tap_state_name(tap_get_state()));
1960 static int ft2232_execute_command(struct jtag_command
*cmd
)
1966 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1967 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1968 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1969 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1970 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1971 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1972 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1974 retval
= ft2232_execute_tms(cmd
);
1977 LOG_ERROR("BUG: unknown JTAG command type encountered");
1978 retval
= ERROR_JTAG_QUEUE_FAILED
;
1984 static int ft2232_execute_queue(void)
1986 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
1989 first_unsent
= cmd
; /* next command that has to be sent */
1992 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1993 * that wasn't handled by a caller-provided error handler
1997 ft2232_buffer_size
= 0;
1998 ft2232_expect_read
= 0;
2000 /* blink, if the current layout has that feature */
2006 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2007 retval
= ERROR_JTAG_QUEUE_FAILED
;
2008 /* Start reading input before FT2232 TX buffer fills up */
2010 if (ft2232_expect_read
> 256)
2012 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2013 retval
= ERROR_JTAG_QUEUE_FAILED
;
2018 if (require_send
> 0)
2019 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2020 retval
= ERROR_JTAG_QUEUE_FAILED
;
2025 #if BUILD_FT2232_FTD2XX == 1
2026 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2030 char SerialNumber
[16];
2031 char Description
[64];
2032 DWORD openex_flags
= 0;
2033 char* openex_string
= NULL
;
2034 uint8_t latency_timer
;
2036 if (layout
== NULL
) {
2037 LOG_WARNING("No ft2232 layout specified'");
2038 return ERROR_JTAG_INIT_FAILED
;
2041 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2044 /* Add non-standard Vid/Pid to the linux driver */
2045 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2047 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2051 if (ft2232_device_desc
&& ft2232_serial
)
2053 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2054 ft2232_device_desc
= NULL
;
2057 if (ft2232_device_desc
)
2059 openex_string
= ft2232_device_desc
;
2060 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2062 else if (ft2232_serial
)
2064 openex_string
= ft2232_serial
;
2065 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2069 LOG_ERROR("neither device description nor serial number specified");
2070 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2072 return ERROR_JTAG_INIT_FAILED
;
2075 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2076 if (status
!= FT_OK
) {
2077 /* under Win32, the FTD2XX driver appends an "A" to the end
2078 * of the description, if we tried by the desc, then
2079 * try by the alternate "A" description. */
2080 if (openex_string
== ft2232_device_desc
) {
2081 /* Try the alternate method. */
2082 openex_string
= ft2232_device_desc_A
;
2083 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2084 if (status
== FT_OK
) {
2085 /* yea, the "alternate" method worked! */
2087 /* drat, give the user a meaningfull message.
2088 * telling the use we tried *BOTH* methods. */
2089 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2091 ft2232_device_desc_A
);
2096 if (status
!= FT_OK
)
2102 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2104 return ERROR_JTAG_INIT_FAILED
;
2106 LOG_ERROR("unable to open ftdi device: %lu", status
);
2107 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2108 if (status
== FT_OK
)
2110 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2113 for (i
= 0; i
< num_devices
; i
++)
2114 desc_array
[i
] = malloc(64);
2116 desc_array
[num_devices
] = NULL
;
2118 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2120 if (status
== FT_OK
)
2122 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2123 for (i
= 0; i
< num_devices
; i
++)
2124 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2127 for (i
= 0; i
< num_devices
; i
++)
2128 free(desc_array
[i
]);
2134 LOG_ERROR("ListDevices: NONE\n");
2136 return ERROR_JTAG_INIT_FAILED
;
2139 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2141 LOG_ERROR("unable to set latency timer: %lu", status
);
2142 return ERROR_JTAG_INIT_FAILED
;
2145 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2147 LOG_ERROR("unable to get latency timer: %lu", status
);
2148 return ERROR_JTAG_INIT_FAILED
;
2152 LOG_DEBUG("current latency timer: %i", latency_timer
);
2155 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2157 LOG_ERROR("unable to set timeouts: %lu", status
);
2158 return ERROR_JTAG_INIT_FAILED
;
2161 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2163 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2164 return ERROR_JTAG_INIT_FAILED
;
2167 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2169 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2170 return ERROR_JTAG_INIT_FAILED
;
2174 static const char* type_str
[] =
2175 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2176 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2177 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2178 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2179 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2180 LOG_INFO("deviceID: %lu", deviceID
);
2181 LOG_INFO("SerialNumber: %s", SerialNumber
);
2182 LOG_INFO("Description: %s", Description
);
2188 static int ft2232_purge_ftd2xx(void)
2192 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2194 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2195 return ERROR_JTAG_INIT_FAILED
;
2201 #endif /* BUILD_FT2232_FTD2XX == 1 */
2203 #if BUILD_FT2232_LIBFTDI == 1
2204 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2206 uint8_t latency_timer
;
2208 if (layout
== NULL
) {
2209 LOG_WARNING("No ft2232 layout specified'");
2210 return ERROR_JTAG_INIT_FAILED
;
2213 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2214 layout
->name
, vid
, pid
);
2216 if (ftdi_init(&ftdic
) < 0)
2217 return ERROR_JTAG_INIT_FAILED
;
2219 /* default to INTERFACE_A */
2220 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2222 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2224 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2225 return ERROR_JTAG_INIT_FAILED
;
2228 /* context, vendor id, product id */
2229 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2233 LOG_WARNING("unable to open ftdi device (trying more): %s",
2236 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2238 return ERROR_JTAG_INIT_FAILED
;
2241 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2242 if (ftdi_usb_reset(&ftdic
) < 0)
2244 LOG_ERROR("unable to reset ftdi device");
2245 return ERROR_JTAG_INIT_FAILED
;
2248 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2250 LOG_ERROR("unable to set latency timer");
2251 return ERROR_JTAG_INIT_FAILED
;
2254 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2256 LOG_ERROR("unable to get latency timer");
2257 return ERROR_JTAG_INIT_FAILED
;
2261 LOG_DEBUG("current latency timer: %i", latency_timer
);
2264 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2266 ftdi_device
= ftdic
.type
;
2267 static const char* type_str
[] =
2268 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2269 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2270 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2271 ? ftdi_device
: no_of_known_types
;
2272 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2276 static int ft2232_purge_libftdi(void)
2278 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2280 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2281 return ERROR_JTAG_INIT_FAILED
;
2287 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2289 static int ft2232_init(void)
2293 uint32_t bytes_written
;
2295 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2297 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2301 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2304 if (layout
== NULL
) {
2305 LOG_WARNING("No ft2232 layout specified'");
2306 return ERROR_JTAG_INIT_FAILED
;
2309 for (int i
= 0; 1; i
++)
2312 * "more indicates that there are more IDs to try, so we should
2313 * not print an error for an ID mismatch (but for anything
2316 * try_more indicates that the error code returned indicates an
2317 * ID mismatch (and nothing else) and that we should proceeed
2318 * with the next ID pair.
2320 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2323 #if BUILD_FT2232_FTD2XX == 1
2324 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2326 #elif BUILD_FT2232_LIBFTDI == 1
2327 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2328 more
, &try_more
, layout
->channel
);
2332 if (!more
|| !try_more
)
2336 ft2232_buffer_size
= 0;
2337 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2339 if (layout
->init() != ERROR_OK
)
2340 return ERROR_JTAG_INIT_FAILED
;
2342 if (ft2232_device_is_highspeed())
2344 #ifndef BUILD_FT2232_HIGHSPEED
2345 #if BUILD_FT2232_FTD2XX == 1
2346 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2347 #elif BUILD_FT2232_LIBFTDI == 1
2348 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2351 /* make sure the legacy mode is disabled */
2352 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2353 return ERROR_JTAG_INIT_FAILED
;
2356 ft2232_speed(jtag_get_speed());
2358 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2359 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2361 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2362 return ERROR_JTAG_INIT_FAILED
;
2365 #if BUILD_FT2232_FTD2XX == 1
2366 return ft2232_purge_ftd2xx();
2367 #elif BUILD_FT2232_LIBFTDI == 1
2368 return ft2232_purge_libftdi();
2374 static int usbjtag_init(void)
2377 uint32_t bytes_written
;
2378 char *ft2232_layout
= layout
->name
;
2381 low_direction
= 0x0b;
2383 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2390 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2397 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2399 /* There are multiple revisions of LM3S811 eval boards:
2400 * - Rev B (and older?) boards have no SWO trace support.
2401 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2402 * they should use the "luminary_icdi" layout instead.
2409 low_direction
= 0x8b;
2411 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2413 /* Most Luminary eval boards support SWO trace output,
2414 * and should use this "luminary_icdi" layout.
2421 low_direction
= 0xcb;
2425 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2426 return ERROR_JTAG_INIT_FAILED
;
2429 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2430 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2432 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2433 low_output
&= ~nTRST
; /* nTRST = 0 */
2437 low_direction
|= nTRSTnOE
; /* nTRST output */
2438 low_output
|= nTRST
; /* nTRST = 1 */
2441 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2443 low_direction
|= nSRSTnOE
; /* nSRST output */
2444 low_output
|= nSRST
; /* nSRST = 1 */
2448 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2449 low_output
&= ~nSRST
; /* nSRST = 0 */
2452 /* initialize low byte for jtag */
2453 buf
[0] = 0x80; /* command "set data bits low byte" */
2454 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2455 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2456 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2458 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2460 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2461 return ERROR_JTAG_INIT_FAILED
;
2467 static int axm0432_jtag_init(void)
2470 uint32_t bytes_written
;
2473 low_direction
= 0x2b;
2475 /* initialize low byte for jtag */
2476 buf
[0] = 0x80; /* command "set data bits low byte" */
2477 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2478 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2479 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2481 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2483 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2484 return ERROR_JTAG_INIT_FAILED
;
2487 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2490 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2492 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2496 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2501 high_direction
= 0x0c;
2503 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2504 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2506 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2510 high_output
|= nTRST
;
2513 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2515 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2519 high_output
|= nSRST
;
2522 /* initialize high port */
2523 buf
[0] = 0x82; /* command "set data bits high byte" */
2524 buf
[1] = high_output
; /* value */
2525 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2526 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2528 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2530 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2531 return ERROR_JTAG_INIT_FAILED
;
2537 static int redbee_init(void)
2540 uint32_t bytes_written
;
2543 low_direction
= 0x2b;
2545 /* initialize low byte for jtag */
2546 /* command "set data bits low byte" */
2548 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2549 buf
[2] = low_direction
;
2550 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2551 buf
[1] = low_output
;
2552 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2554 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2555 || (bytes_written
!= 3))
2557 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2558 return ERROR_JTAG_INIT_FAILED
;
2562 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2564 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2567 high_direction
= 0x0c;
2569 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2570 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2572 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2576 high_output
|= nTRST
;
2579 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2581 LOG_ERROR("can't set nSRST to push-pull on redbee");
2585 high_output
|= nSRST
;
2588 /* initialize high port */
2589 buf
[0] = 0x82; /* command "set data bits high byte" */
2590 buf
[1] = high_output
; /* value */
2591 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2592 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2594 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2595 || (bytes_written
!= 3))
2597 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2598 return ERROR_JTAG_INIT_FAILED
;
2604 static int jtagkey_init(void)
2607 uint32_t bytes_written
;
2610 low_direction
= 0x1b;
2612 /* initialize low byte for jtag */
2613 buf
[0] = 0x80; /* command "set data bits low byte" */
2614 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2615 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2616 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2618 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2620 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2621 return ERROR_JTAG_INIT_FAILED
;
2624 if (strcmp(layout
->name
, "jtagkey") == 0)
2631 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2632 || (strcmp(layout
->name
, "oocdlink") == 0))
2641 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2646 high_direction
= 0x0f;
2648 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2649 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2651 high_output
|= nTRSTnOE
;
2652 high_output
&= ~nTRST
;
2656 high_output
&= ~nTRSTnOE
;
2657 high_output
|= nTRST
;
2660 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2662 high_output
&= ~nSRSTnOE
;
2663 high_output
|= nSRST
;
2667 high_output
|= nSRSTnOE
;
2668 high_output
&= ~nSRST
;
2671 /* initialize high port */
2672 buf
[0] = 0x82; /* command "set data bits high byte" */
2673 buf
[1] = high_output
; /* value */
2674 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2675 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2677 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2679 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2680 return ERROR_JTAG_INIT_FAILED
;
2686 static int olimex_jtag_init(void)
2689 uint32_t bytes_written
;
2692 low_direction
= 0x1b;
2694 /* initialize low byte for jtag */
2695 buf
[0] = 0x80; /* command "set data bits low byte" */
2696 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2697 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2698 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2700 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2702 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2703 return ERROR_JTAG_INIT_FAILED
;
2709 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2712 high_direction
= 0x0f;
2714 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2715 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2717 high_output
|= nTRSTnOE
;
2718 high_output
&= ~nTRST
;
2722 high_output
&= ~nTRSTnOE
;
2723 high_output
|= nTRST
;
2726 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2728 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2732 high_output
&= ~nSRST
;
2735 /* turn red LED on */
2736 high_output
|= 0x08;
2738 /* initialize high port */
2739 buf
[0] = 0x82; /* command "set data bits high byte" */
2740 buf
[1] = high_output
; /* value */
2741 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2742 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2744 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2746 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2747 return ERROR_JTAG_INIT_FAILED
;
2753 static int flyswatter_init(void)
2756 uint32_t bytes_written
;
2759 low_direction
= 0xfb;
2761 /* initialize low byte for jtag */
2762 buf
[0] = 0x80; /* command "set data bits low byte" */
2763 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2764 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2765 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2767 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2769 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2770 return ERROR_JTAG_INIT_FAILED
;
2774 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2776 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2779 high_direction
= 0x0c;
2781 /* turn red LED3 on, LED2 off */
2782 high_output
|= 0x08;
2784 /* initialize high port */
2785 buf
[0] = 0x82; /* command "set data bits high byte" */
2786 buf
[1] = high_output
; /* value */
2787 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2788 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2790 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2792 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2793 return ERROR_JTAG_INIT_FAILED
;
2799 static int turtle_init(void)
2802 uint32_t bytes_written
;
2805 low_direction
= 0x5b;
2807 /* initialize low byte for jtag */
2808 buf
[0] = 0x80; /* command "set data bits low byte" */
2809 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2810 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2811 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2813 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2815 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2816 return ERROR_JTAG_INIT_FAILED
;
2822 high_direction
= 0x0C;
2824 /* initialize high port */
2825 buf
[0] = 0x82; /* command "set data bits high byte" */
2826 buf
[1] = high_output
;
2827 buf
[2] = high_direction
;
2828 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2830 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2832 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2833 return ERROR_JTAG_INIT_FAILED
;
2839 static int comstick_init(void)
2842 uint32_t bytes_written
;
2845 low_direction
= 0x0b;
2847 /* initialize low byte for jtag */
2848 buf
[0] = 0x80; /* command "set data bits low byte" */
2849 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2850 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2851 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2853 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2855 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2856 return ERROR_JTAG_INIT_FAILED
;
2860 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2862 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2865 high_direction
= 0x03;
2867 /* initialize high port */
2868 buf
[0] = 0x82; /* command "set data bits high byte" */
2869 buf
[1] = high_output
;
2870 buf
[2] = high_direction
;
2871 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2873 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2875 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2876 return ERROR_JTAG_INIT_FAILED
;
2882 static int stm32stick_init(void)
2885 uint32_t bytes_written
;
2888 low_direction
= 0x8b;
2890 /* initialize low byte for jtag */
2891 buf
[0] = 0x80; /* command "set data bits low byte" */
2892 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2893 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2894 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2896 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2898 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2899 return ERROR_JTAG_INIT_FAILED
;
2903 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2905 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2908 high_direction
= 0x03;
2910 /* initialize high port */
2911 buf
[0] = 0x82; /* command "set data bits high byte" */
2912 buf
[1] = high_output
;
2913 buf
[2] = high_direction
;
2914 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2916 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2918 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2919 return ERROR_JTAG_INIT_FAILED
;
2925 static int sheevaplug_init(void)
2928 uint32_t bytes_written
;
2931 low_direction
= 0x1b;
2933 /* initialize low byte for jtag */
2934 buf
[0] = 0x80; /* command "set data bits low byte" */
2935 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2936 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2937 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2939 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2941 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2942 return ERROR_JTAG_INIT_FAILED
;
2951 high_direction
= 0x0f;
2953 /* nTRST is always push-pull */
2954 high_output
&= ~nTRSTnOE
;
2955 high_output
|= nTRST
;
2957 /* nSRST is always open-drain */
2958 high_output
|= nSRSTnOE
;
2959 high_output
&= ~nSRST
;
2961 /* initialize high port */
2962 buf
[0] = 0x82; /* command "set data bits high byte" */
2963 buf
[1] = high_output
; /* value */
2964 buf
[2] = high_direction
; /* all outputs - xRST */
2965 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2967 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2969 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2970 return ERROR_JTAG_INIT_FAILED
;
2976 static int cortino_jtag_init(void)
2979 uint32_t bytes_written
;
2982 low_direction
= 0x1b;
2984 /* initialize low byte for jtag */
2985 buf
[0] = 0x80; /* command "set data bits low byte" */
2986 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2987 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2988 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2990 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2992 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2993 return ERROR_JTAG_INIT_FAILED
;
2997 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2999 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3002 high_direction
= 0x03;
3004 /* initialize high port */
3005 buf
[0] = 0x82; /* command "set data bits high byte" */
3006 buf
[1] = high_output
;
3007 buf
[2] = high_direction
;
3008 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3010 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3012 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3013 return ERROR_JTAG_INIT_FAILED
;
3019 static void olimex_jtag_blink(void)
3021 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3022 * ACBUS3 is bit 3 of the GPIOH port
3024 if (high_output
& 0x08)
3026 /* set port pin high */
3027 high_output
&= 0x07;
3031 /* set port pin low */
3032 high_output
|= 0x08;
3036 buffer_write(high_output
);
3037 buffer_write(high_direction
);
3040 static void flyswatter_jtag_blink(void)
3043 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3045 high_output
^= 0x0c;
3048 buffer_write(high_output
);
3049 buffer_write(high_direction
);
3052 static void turtle_jtag_blink(void)
3055 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3057 if (high_output
& 0x08)
3067 buffer_write(high_output
);
3068 buffer_write(high_direction
);
3071 static int ft2232_quit(void)
3073 #if BUILD_FT2232_FTD2XX == 1
3076 status
= FT_Close(ftdih
);
3077 #elif BUILD_FT2232_LIBFTDI == 1
3078 ftdi_usb_close(&ftdic
);
3080 ftdi_deinit(&ftdic
);
3083 free(ft2232_buffer
);
3084 ft2232_buffer
= NULL
;
3089 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3095 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3096 cp
= strchr(ft2232_device_desc
, 0);
3097 /* under Win32, the FTD2XX driver appends an "A" to the end
3098 * of the description, this examines the given desc
3099 * and creates the 'missing' _A or non_A variable. */
3100 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3101 /* it was, so make this the "A" version. */
3102 ft2232_device_desc_A
= ft2232_device_desc
;
3103 /* and *CREATE* the non-A version. */
3104 strcpy(buf
, ft2232_device_desc
);
3105 cp
= strchr(buf
, 0);
3107 ft2232_device_desc
= strdup(buf
);
3109 /* <space > A not defined
3111 sprintf(buf
, "%s A", ft2232_device_desc
);
3112 ft2232_device_desc_A
= strdup(buf
);
3117 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3123 COMMAND_HANDLER(ft2232_handle_serial_command
)
3127 ft2232_serial
= strdup(CMD_ARGV
[0]);
3131 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3137 COMMAND_HANDLER(ft2232_handle_layout_command
)
3139 if (CMD_ARGC
!= 1) {
3140 LOG_ERROR("Need exactly one argument to ft2232_layout");
3145 LOG_ERROR("already specified ft2232_layout %s",
3147 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3152 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3153 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3159 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3163 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3165 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3167 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3168 "(maximum is %d pairs)", MAX_USB_IDS
);
3169 CMD_ARGC
= MAX_USB_IDS
* 2;
3171 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3173 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3175 return ERROR_COMMAND_SYNTAX_ERROR
;
3176 /* remove the incomplete trailing id */
3181 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3183 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3184 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3188 * Explicitly terminate, in case there are multiples instances of
3191 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3196 COMMAND_HANDLER(ft2232_handle_latency_command
)
3200 ft2232_latency
= atoi(CMD_ARGV
[0]);
3204 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3210 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3214 /* 7 bits of either ones or zeros. */
3215 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3217 while (num_cycles
> 0)
3219 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3220 * at most 7 bits per invocation. Here we invoke it potentially
3223 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3225 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3227 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3228 retval
= ERROR_JTAG_QUEUE_FAILED
;
3233 /* there are no state transitions in this code, so omit state tracking */
3235 /* command "Clock Data to TMS/CS Pin (no Read)" */
3239 buffer_write(bitcount_per_command
- 1);
3241 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3246 num_cycles
-= bitcount_per_command
;
3252 /* ---------------------------------------------------------------------
3253 * Support for IceBear JTAG adapter from Section5:
3254 * http://section5.ch/icebear
3256 * Author: Sten, debian@sansys-electronic.com
3259 /* Icebear pin layout
3261 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3262 * GND GND | 4 3| n.c.
3263 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3264 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3265 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3266 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3267 * ADBUS2 TDO |14 13| GND GND
3269 * ADBUS0 O L TCK ACBUS0 GND
3270 * ADBUS1 O L TDI ACBUS1 GND
3271 * ADBUS2 I TDO ACBUS2 n.c.
3272 * ADBUS3 O H TMS ACBUS3 n.c.
3278 static int icebear_jtag_init(void) {
3280 uint32_t bytes_written
;
3282 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3283 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3287 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3288 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3289 low_direction
&= ~nTRST
; /* nTRST high impedance */
3292 low_direction
|= nTRST
;
3293 low_output
|= nTRST
;
3296 low_direction
|= nSRST
;
3297 low_output
|= nSRST
;
3299 /* initialize low byte for jtag */
3300 buf
[0] = 0x80; /* command "set data bits low byte" */
3301 buf
[1] = low_output
;
3302 buf
[2] = low_direction
;
3303 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3305 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3306 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3307 return ERROR_JTAG_INIT_FAILED
;
3311 high_direction
= 0x00;
3314 /* initialize high port */
3315 buf
[0] = 0x82; /* command "set data bits high byte" */
3316 buf
[1] = high_output
; /* value */
3317 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3318 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3320 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3321 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3322 return ERROR_JTAG_INIT_FAILED
;
3328 static void icebear_jtag_reset(int trst
, int srst
) {
3331 low_direction
|= nTRST
;
3332 low_output
&= ~nTRST
;
3334 else if (trst
== 0) {
3335 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3336 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3337 low_direction
&= ~nTRST
;
3339 low_output
|= nTRST
;
3343 low_output
&= ~nSRST
;
3345 else if (srst
== 0) {
3346 low_output
|= nSRST
;
3349 /* command "set data bits low byte" */
3351 buffer_write(low_output
);
3352 buffer_write(low_direction
);
3354 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3357 /* ---------------------------------------------------------------------
3358 * Support for Signalyzer H2 and Signalyzer H4
3359 * JTAG adapter from Xverve Technologies Inc.
3360 * http://www.signalyzer.com or http://www.xverve.com
3362 * Author: Oleg Seiljus, oleg@signalyzer.com
3364 static unsigned char signalyzer_h_side
;
3365 static unsigned int signalyzer_h_adapter_type
;
3367 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3369 #if BUILD_FT2232_FTD2XX == 1
3370 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3373 #define SIGNALYZER_COMMAND_ADDR 128
3374 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3376 #define SIGNALYZER_COMMAND_VERSION 0x41
3377 #define SIGNALYZER_COMMAND_RESET 0x42
3378 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3379 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3380 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3381 #define SIGNALYZER_COMMAND_LED_SET 0x53
3382 #define SIGNALYZER_COMMAND_ADC 0x54
3383 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3384 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3385 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3386 #define SIGNALYZER_COMMAND_I2C 0x58
3388 #define SIGNALYZER_CHAN_A 1
3389 #define SIGNALYZER_CHAN_B 2
3390 /* LEDS use channel C */
3391 #define SIGNALYZER_CHAN_C 4
3393 #define SIGNALYZER_LED_GREEN 1
3394 #define SIGNALYZER_LED_RED 2
3396 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3397 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3398 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3399 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3400 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3403 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3405 #if BUILD_FT2232_FTD2XX == 1
3406 return FT_WriteEE(ftdih
, address
, value
);
3407 #elif BUILD_FT2232_LIBFTDI == 1
3412 #if BUILD_FT2232_FTD2XX == 1
3413 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3415 return FT_ReadEE(ftdih
, address
, value
);
3419 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3420 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3422 unsigned char on_time
;
3423 unsigned char off_time
;
3425 if (on_time_ms
< 0xFFFF)
3426 on_time
= (unsigned char)(on_time_ms
/ 62);
3430 off_time
= (unsigned char)(off_time_ms
/ 62);
3432 #if BUILD_FT2232_FTD2XX == 1
3435 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3436 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3438 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3439 return ERROR_JTAG_DEVICE_ERROR
;
3442 if ((status
= signalyzer_h_ctrl_write(
3443 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3444 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3446 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3447 return ERROR_JTAG_DEVICE_ERROR
;
3450 if ((status
= signalyzer_h_ctrl_write(
3451 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3452 ((uint32_t)cycles
))) != FT_OK
)
3454 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3455 return ERROR_JTAG_DEVICE_ERROR
;
3458 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3459 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3461 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3462 return ERROR_JTAG_DEVICE_ERROR
;
3466 #elif BUILD_FT2232_LIBFTDI == 1
3469 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3470 ((uint32_t)(channel
<< 8) | led
))) < 0)
3472 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3473 ftdi_get_error_string(&ftdic
));
3474 return ERROR_JTAG_DEVICE_ERROR
;
3477 if ((retval
= signalyzer_h_ctrl_write(
3478 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3479 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3481 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3482 ftdi_get_error_string(&ftdic
));
3483 return ERROR_JTAG_DEVICE_ERROR
;
3486 if ((retval
= signalyzer_h_ctrl_write(
3487 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3488 (uint32_t)cycles
)) < 0)
3490 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3491 ftdi_get_error_string(&ftdic
));
3492 return ERROR_JTAG_DEVICE_ERROR
;
3495 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3496 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3498 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3499 ftdi_get_error_string(&ftdic
));
3500 return ERROR_JTAG_DEVICE_ERROR
;
3507 static int signalyzer_h_init(void)
3509 #if BUILD_FT2232_FTD2XX == 1
3516 uint16_t read_buf
[12] = { 0 };
3518 uint32_t bytes_written
;
3520 /* turn on center green led */
3521 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3522 0xFFFF, 0x00, 0x00);
3524 /* determine what channel config wants to open
3525 * TODO: change me... current implementation is made to work
3526 * with openocd description parsing.
3528 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3532 signalyzer_h_side
= *(end_of_desc
- 1);
3533 if (signalyzer_h_side
== 'B')
3534 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3536 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3540 LOG_ERROR("No Channel was specified");
3544 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3547 #if BUILD_FT2232_FTD2XX == 1
3548 /* read signalyzer versionining information */
3549 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3550 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3552 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3553 return ERROR_JTAG_DEVICE_ERROR
;
3556 for (i
= 0; i
< 10; i
++)
3558 if ((status
= signalyzer_h_ctrl_read(
3559 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3560 &read_buf
[i
])) != FT_OK
)
3562 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3564 return ERROR_JTAG_DEVICE_ERROR
;
3568 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3569 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3570 read_buf
[4], read_buf
[5], read_buf
[6]);
3572 /* set gpio register */
3573 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3574 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3576 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3577 return ERROR_JTAG_DEVICE_ERROR
;
3580 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3583 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3584 return ERROR_JTAG_DEVICE_ERROR
;
3587 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3588 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3590 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3591 return ERROR_JTAG_DEVICE_ERROR
;
3594 /* read adapter type information */
3595 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3596 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3599 return ERROR_JTAG_DEVICE_ERROR
;
3602 if ((status
= signalyzer_h_ctrl_write(
3603 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3605 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3606 return ERROR_JTAG_DEVICE_ERROR
;
3609 if ((status
= signalyzer_h_ctrl_write(
3610 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3612 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3613 return ERROR_JTAG_DEVICE_ERROR
;
3616 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3617 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3619 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3620 return ERROR_JTAG_DEVICE_ERROR
;
3625 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3626 &read_buf
[0])) != FT_OK
)
3628 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3629 return ERROR_JTAG_DEVICE_ERROR
;
3632 if (read_buf
[0] != 0x0498)
3633 signalyzer_h_adapter_type
= 0x0000;
3636 for (i
= 0; i
< 4; i
++)
3638 if ((status
= signalyzer_h_ctrl_read(
3639 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3640 &read_buf
[i
])) != FT_OK
)
3642 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3644 return ERROR_JTAG_DEVICE_ERROR
;
3648 signalyzer_h_adapter_type
= read_buf
[0];
3651 #elif BUILD_FT2232_LIBFTDI == 1
3652 /* currently libftdi does not allow reading individual eeprom
3653 * locations, therefore adapter type cannot be detected.
3654 * override with most common type
3656 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3659 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3661 /* ADAPTOR: EM_LT16_A */
3662 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3664 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3665 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3673 low_direction
= 0x1b;
3676 high_direction
= 0x0;
3678 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3680 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3681 low_output
&= ~nTRST
; /* nTRST = 0 */
3685 low_direction
|= nTRSTnOE
; /* nTRST output */
3686 low_output
|= nTRST
; /* nTRST = 1 */
3689 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3691 low_direction
|= nSRSTnOE
; /* nSRST output */
3692 low_output
|= nSRST
; /* nSRST = 1 */
3696 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3697 low_output
&= ~nSRST
; /* nSRST = 0 */
3700 #if BUILD_FT2232_FTD2XX == 1
3701 /* enable power to the module */
3702 if ((status
= signalyzer_h_ctrl_write(
3703 SIGNALYZER_DATA_BUFFER_ADDR
,
3704 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3707 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3709 return ERROR_JTAG_DEVICE_ERROR
;
3712 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3713 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3715 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3717 return ERROR_JTAG_DEVICE_ERROR
;
3720 /* set gpio mode register */
3721 if ((status
= signalyzer_h_ctrl_write(
3722 SIGNALYZER_DATA_BUFFER_ADDR
,
3723 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3725 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3727 return ERROR_JTAG_DEVICE_ERROR
;
3730 if ((status
= signalyzer_h_ctrl_write(
3731 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3734 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3736 return ERROR_JTAG_DEVICE_ERROR
;
3739 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3740 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3742 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3744 return ERROR_JTAG_DEVICE_ERROR
;
3747 /* set gpio register */
3748 if ((status
= signalyzer_h_ctrl_write(
3749 SIGNALYZER_DATA_BUFFER_ADDR
,
3750 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3752 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3754 return ERROR_JTAG_DEVICE_ERROR
;
3757 if ((status
= signalyzer_h_ctrl_write(
3758 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3761 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3763 return ERROR_JTAG_DEVICE_ERROR
;
3766 if ((status
= signalyzer_h_ctrl_write(
3767 SIGNALYZER_COMMAND_ADDR
,
3768 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3770 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3772 return ERROR_JTAG_DEVICE_ERROR
;
3777 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3778 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3779 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3780 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3781 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3783 if (signalyzer_h_adapter_type
3784 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3785 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3786 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3787 else if (signalyzer_h_adapter_type
3788 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3789 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3790 "(ARM JTAG with PSU) detected. (HW: %2x).",
3791 (read_buf
[1] >> 8));
3792 else if (signalyzer_h_adapter_type
3793 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3794 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3795 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3796 else if (signalyzer_h_adapter_type
3797 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3798 LOG_INFO("Signalyzer: EM-JTAG-P "
3799 "(Generic JTAG with PSU) detected. (HW: %2x).",
3800 (read_buf
[1] >> 8));
3808 low_direction
= 0x1b;
3811 high_direction
= 0x1f;
3813 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3815 high_output
|= nTRSTnOE
;
3816 high_output
&= ~nTRST
;
3820 high_output
&= ~nTRSTnOE
;
3821 high_output
|= nTRST
;
3824 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3826 high_output
&= ~nSRSTnOE
;
3827 high_output
|= nSRST
;
3831 high_output
|= nSRSTnOE
;
3832 high_output
&= ~nSRST
;
3835 #if BUILD_FT2232_FTD2XX == 1
3836 /* enable power to the module */
3837 if ((status
= signalyzer_h_ctrl_write(
3838 SIGNALYZER_DATA_BUFFER_ADDR
,
3839 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3842 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3844 return ERROR_JTAG_DEVICE_ERROR
;
3847 if ((status
= signalyzer_h_ctrl_write(
3848 SIGNALYZER_COMMAND_ADDR
,
3849 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3853 return ERROR_JTAG_DEVICE_ERROR
;
3856 /* set gpio mode register (IO_16 and IO_17 set as analog
3857 * inputs, other is gpio)
3859 if ((status
= signalyzer_h_ctrl_write(
3860 SIGNALYZER_DATA_BUFFER_ADDR
,
3861 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3863 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3865 return ERROR_JTAG_DEVICE_ERROR
;
3868 if ((status
= signalyzer_h_ctrl_write(
3869 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3872 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3874 return ERROR_JTAG_DEVICE_ERROR
;
3877 if ((status
= signalyzer_h_ctrl_write(
3878 SIGNALYZER_COMMAND_ADDR
,
3879 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3881 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3883 return ERROR_JTAG_DEVICE_ERROR
;
3886 /* set gpio register (all inputs, for -P modules,
3887 * PSU will be turned off)
3889 if ((status
= signalyzer_h_ctrl_write(
3890 SIGNALYZER_DATA_BUFFER_ADDR
,
3891 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3893 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3895 return ERROR_JTAG_DEVICE_ERROR
;
3898 if ((status
= signalyzer_h_ctrl_write(
3899 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3902 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3904 return ERROR_JTAG_DEVICE_ERROR
;
3907 if ((status
= signalyzer_h_ctrl_write(
3908 SIGNALYZER_COMMAND_ADDR
,
3909 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3911 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3913 return ERROR_JTAG_DEVICE_ERROR
;
3918 else if (signalyzer_h_adapter_type
== 0x0000)
3920 LOG_INFO("Signalyzer: No external modules were detected.");
3928 low_direction
= 0x1b;
3931 high_direction
= 0x0;
3933 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3935 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3936 low_output
&= ~nTRST
; /* nTRST = 0 */
3940 low_direction
|= nTRSTnOE
; /* nTRST output */
3941 low_output
|= nTRST
; /* nTRST = 1 */
3944 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3946 low_direction
|= nSRSTnOE
; /* nSRST output */
3947 low_output
|= nSRST
; /* nSRST = 1 */
3951 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3952 low_output
&= ~nSRST
; /* nSRST = 0 */
3957 LOG_ERROR("Unknown module type is detected: %.4x",
3958 signalyzer_h_adapter_type
);
3959 return ERROR_JTAG_DEVICE_ERROR
;
3962 /* initialize low byte of controller for jtag operation */
3964 buf
[1] = low_output
;
3965 buf
[2] = low_direction
;
3967 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3968 || (bytes_written
!= 3))
3970 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3971 return ERROR_JTAG_INIT_FAILED
;
3974 #if BUILD_FT2232_FTD2XX == 1
3975 if (ftdi_device
== FT_DEVICE_2232H
)
3977 /* initialize high byte of controller for jtag operation */
3979 buf
[1] = high_output
;
3980 buf
[2] = high_direction
;
3982 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3983 || (bytes_written
!= 3))
3985 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3986 return ERROR_JTAG_INIT_FAILED
;
3989 #elif BUILD_FT2232_LIBFTDI == 1
3990 if (ftdi_device
== TYPE_2232H
)
3992 /* initialize high byte of controller for jtag operation */
3994 buf
[1] = high_output
;
3995 buf
[2] = high_direction
;
3997 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3998 || (bytes_written
!= 3))
4000 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4001 return ERROR_JTAG_INIT_FAILED
;
4008 static void signalyzer_h_reset(int trst
, int srst
)
4010 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4012 /* ADAPTOR: EM_LT16_A */
4013 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4017 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4018 /* switch to output pin (output is low) */
4019 low_direction
|= nTRSTnOE
;
4021 /* switch output low */
4022 low_output
&= ~nTRST
;
4026 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4027 /* switch to input pin (high-Z + internal
4028 * and external pullup) */
4029 low_direction
&= ~nTRSTnOE
;
4031 /* switch output high */
4032 low_output
|= nTRST
;
4037 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4038 /* switch output low */
4039 low_output
&= ~nSRST
;
4041 /* switch to output pin (output is low) */
4042 low_direction
|= nSRSTnOE
;
4046 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4047 /* switch output high */
4048 low_output
|= nSRST
;
4050 /* switch to input pin (high-Z) */
4051 low_direction
&= ~nSRSTnOE
;
4054 /* command "set data bits low byte" */
4056 buffer_write(low_output
);
4057 buffer_write(low_direction
);
4058 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4059 "low_direction: 0x%2.2x",
4060 trst
, srst
, low_output
, low_direction
);
4062 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4063 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4064 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4065 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4066 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4070 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4071 high_output
&= ~nTRSTnOE
;
4073 high_output
&= ~nTRST
;
4077 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4078 high_output
|= nTRSTnOE
;
4080 high_output
|= nTRST
;
4085 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4086 high_output
&= ~nSRST
;
4088 high_output
&= ~nSRSTnOE
;
4092 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4093 high_output
|= nSRST
;
4095 high_output
|= nSRSTnOE
;
4098 /* command "set data bits high byte" */
4100 buffer_write(high_output
);
4101 buffer_write(high_direction
);
4102 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4103 "high_direction: 0x%2.2x",
4104 trst
, srst
, high_output
, high_direction
);
4106 else if (signalyzer_h_adapter_type
== 0x0000)
4110 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4111 /* switch to output pin (output is low) */
4112 low_direction
|= nTRSTnOE
;
4114 /* switch output low */
4115 low_output
&= ~nTRST
;
4119 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4120 /* switch to input pin (high-Z + internal
4121 * and external pullup) */
4122 low_direction
&= ~nTRSTnOE
;
4124 /* switch output high */
4125 low_output
|= nTRST
;
4130 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4131 /* switch output low */
4132 low_output
&= ~nSRST
;
4134 /* switch to output pin (output is low) */
4135 low_direction
|= nSRSTnOE
;
4139 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4140 /* switch output high */
4141 low_output
|= nSRST
;
4143 /* switch to input pin (high-Z) */
4144 low_direction
&= ~nSRSTnOE
;
4147 /* command "set data bits low byte" */
4149 buffer_write(low_output
);
4150 buffer_write(low_direction
);
4151 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4152 "low_direction: 0x%2.2x",
4153 trst
, srst
, low_output
, low_direction
);
4157 static void signalyzer_h_blink(void)
4159 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4162 /********************************************************************
4163 * Support for KT-LINK
4164 * JTAG adapter from KRISTECH
4165 * http://www.kristech.eu
4166 *******************************************************************/
4167 static int ktlink_init(void)
4170 uint32_t bytes_written
;
4171 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4173 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4174 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4176 // initialize low port
4177 buf
[0] = 0x80; // command "set data bits low byte"
4178 buf
[1] = low_output
;
4179 buf
[2] = low_direction
;
4180 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4182 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4184 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4185 return ERROR_JTAG_INIT_FAILED
;
4193 high_output
= 0x80; // turn LED on
4194 high_direction
= 0xFF; // all outputs
4196 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4198 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4199 high_output
|= nTRSTnOE
;
4200 high_output
&= ~nTRST
;
4202 high_output
&= ~nTRSTnOE
;
4203 high_output
|= nTRST
;
4206 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4207 high_output
&= ~nSRSTnOE
;
4208 high_output
|= nSRST
;
4210 high_output
|= nSRSTnOE
;
4211 high_output
&= ~nSRST
;
4214 // initialize high port
4215 buf
[0] = 0x82; // command "set data bits high byte"
4216 buf
[1] = high_output
; // value
4217 buf
[2] = high_direction
;
4218 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4220 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4222 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4223 return ERROR_JTAG_INIT_FAILED
;
4229 static void ktlink_reset(int trst
, int srst
)
4231 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4234 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4235 high_output
&= ~nTRSTnOE
;
4237 high_output
&= ~nTRST
;
4238 } else if (trst
== 0) {
4239 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4240 high_output
|= nTRSTnOE
;
4242 high_output
|= nTRST
;
4246 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4247 high_output
&= ~nSRST
;
4249 high_output
&= ~nSRSTnOE
;
4250 } else if (srst
== 0) {
4251 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4252 high_output
|= nSRST
;
4254 high_output
|= nSRSTnOE
;
4257 buffer_write(0x82); // command "set data bits high byte"
4258 buffer_write(high_output
);
4259 buffer_write(high_direction
);
4260 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4263 static void ktlink_blink(void)
4265 /* LED connected to ACBUS7 */
4266 if (high_output
& 0x80)
4267 high_output
&= 0x7F;
4269 high_output
|= 0x80;
4271 buffer_write(0x82); // command "set data bits high byte"
4272 buffer_write(high_output
);
4273 buffer_write(high_direction
);
4276 static const struct command_registration ft2232_command_handlers
[] = {
4278 .name
= "ft2232_device_desc",
4279 .handler
= &ft2232_handle_device_desc_command
,
4280 .mode
= COMMAND_CONFIG
,
4281 .help
= "set the USB device description of the FTDI FT2232 device",
4282 .usage
= "description_string",
4285 .name
= "ft2232_serial",
4286 .handler
= &ft2232_handle_serial_command
,
4287 .mode
= COMMAND_CONFIG
,
4288 .help
= "set the serial number of the FTDI FT2232 device",
4289 .usage
= "serial_string",
4292 .name
= "ft2232_layout",
4293 .handler
= &ft2232_handle_layout_command
,
4294 .mode
= COMMAND_CONFIG
,
4295 .help
= "set the layout of the FT2232 GPIO signals used "
4296 "to control output-enables and reset signals",
4297 .usage
= "layout_name",
4300 .name
= "ft2232_vid_pid",
4301 .handler
= &ft2232_handle_vid_pid_command
,
4302 .mode
= COMMAND_CONFIG
,
4303 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4304 .usage
= "(vid pid)* ",
4307 .name
= "ft2232_latency",
4308 .handler
= &ft2232_handle_latency_command
,
4309 .mode
= COMMAND_CONFIG
,
4310 .help
= "set the FT2232 latency timer to a new value",
4313 COMMAND_REGISTRATION_DONE
4316 struct jtag_interface ft2232_interface
= {
4318 .supported
= DEBUG_CAP_TMS_SEQ
,
4319 .commands
= ft2232_command_handlers
,
4321 .init
= ft2232_init
,
4322 .quit
= ft2232_quit
,
4323 .speed
= ft2232_speed
,
4324 .speed_div
= ft2232_speed_div
,
4326 .execute_queue
= ft2232_execute_queue
,