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 lm3s811_jtag_init(void);
176 static int icdi_jtag_init(void);
177 static int olimex_jtag_init(void);
178 static int flyswatter_init(void);
179 static int turtle_init(void);
180 static int comstick_init(void);
181 static int stm32stick_init(void);
182 static int axm0432_jtag_init(void);
183 static int sheevaplug_init(void);
184 static int icebear_jtag_init(void);
185 static int cortino_jtag_init(void);
186 static int signalyzer_init(void);
187 static int signalyzer_h_init(void);
188 static int ktlink_init(void);
189 static int redbee_init(void);
191 /* reset procedures for supported layouts */
192 static void ftx23_reset(int trst
, int srst
);
193 static void jtagkey_reset(int trst
, int srst
);
194 static void olimex_jtag_reset(int trst
, int srst
);
195 static void flyswatter_reset(int trst
, int srst
);
196 static void turtle_reset(int trst
, int srst
);
197 static void comstick_reset(int trst
, int srst
);
198 static void stm32stick_reset(int trst
, int srst
);
199 static void axm0432_jtag_reset(int trst
, int srst
);
200 static void sheevaplug_reset(int trst
, int srst
);
201 static void icebear_jtag_reset(int trst
, int srst
);
202 static void signalyzer_h_reset(int trst
, int srst
);
203 static void ktlink_reset(int trst
, int srst
);
204 static void redbee_reset(int trst
, int srst
);
206 /* blink procedures for layouts that support a blinking led */
207 static void olimex_jtag_blink(void);
208 static void flyswatter_jtag_blink(void);
209 static void turtle_jtag_blink(void);
210 static void signalyzer_h_blink(void);
211 static void ktlink_blink(void);
213 static const struct ft2232_layout ft2232_layouts
[] =
216 .init
= usbjtag_init
,
217 .reset
= ftx23_reset
,
220 .init
= jtagkey_init
,
221 .reset
= jtagkey_reset
,
223 { .name
= "jtagkey_prototype_v1",
224 .init
= jtagkey_init
,
225 .reset
= jtagkey_reset
,
227 { .name
= "oocdlink",
228 .init
= jtagkey_init
,
229 .reset
= jtagkey_reset
,
231 { .name
= "signalyzer",
232 .init
= signalyzer_init
,
233 .reset
= ftx23_reset
,
235 { .name
= "evb_lm3s811",
236 .init
= lm3s811_jtag_init
,
237 .reset
= ftx23_reset
,
239 { .name
= "luminary_icdi",
240 .init
= icdi_jtag_init
,
241 .reset
= ftx23_reset
,
243 { .name
= "olimex-jtag",
244 .init
= olimex_jtag_init
,
245 .reset
= olimex_jtag_reset
,
246 .blink
= olimex_jtag_blink
248 { .name
= "flyswatter",
249 .init
= flyswatter_init
,
250 .reset
= flyswatter_reset
,
251 .blink
= flyswatter_jtag_blink
253 { .name
= "turtelizer2",
255 .reset
= turtle_reset
,
256 .blink
= turtle_jtag_blink
258 { .name
= "comstick",
259 .init
= comstick_init
,
260 .reset
= comstick_reset
,
262 { .name
= "stm32stick",
263 .init
= stm32stick_init
,
264 .reset
= stm32stick_reset
,
266 { .name
= "axm0432_jtag",
267 .init
= axm0432_jtag_init
,
268 .reset
= axm0432_jtag_reset
,
270 { .name
= "sheevaplug",
271 .init
= sheevaplug_init
,
272 .reset
= sheevaplug_reset
,
275 .init
= icebear_jtag_init
,
276 .reset
= icebear_jtag_reset
,
279 .init
= cortino_jtag_init
,
280 .reset
= comstick_reset
,
282 { .name
= "signalyzer-h",
283 .init
= signalyzer_h_init
,
284 .reset
= signalyzer_h_reset
,
285 .blink
= signalyzer_h_blink
289 .reset
= ktlink_reset
,
290 .blink
= ktlink_blink
292 { .name
= "redbee-econotag",
294 .reset
= redbee_reset
,
296 { .name
= "redbee-usb",
298 .reset
= redbee_reset
,
299 .channel
= INTERFACE_B
,
301 { .name
= NULL
, /* END OF TABLE */ },
304 /* bitmask used to drive nTRST; usually a GPIOLx signal */
305 static uint8_t nTRST
;
306 static uint8_t nTRSTnOE
;
307 /* bitmask used to drive nSRST; usually a GPIOLx signal */
308 static uint8_t nSRST
;
309 static uint8_t nSRSTnOE
;
311 /** the layout being used with this debug session */
312 static const struct ft2232_layout
*layout
;
314 /** default bitmask values ddriven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
315 static uint8_t low_output
= 0x0;
316 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
317 static uint8_t low_direction
= 0x0;
318 /** default value bitmask for CBUS GPIOH(0..4) */
319 static uint8_t high_output
= 0x0;
320 /** default direction bitmask for CBUS GPIOH(0..4) */
321 static uint8_t high_direction
= 0x0;
323 #if BUILD_FT2232_FTD2XX == 1
324 static FT_HANDLE ftdih
= NULL
;
325 static FT_DEVICE ftdi_device
= 0;
326 #elif BUILD_FT2232_LIBFTDI == 1
327 static struct ftdi_context ftdic
;
328 static enum ftdi_chip_type ftdi_device
;
331 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
332 static int require_send
;
334 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
336 "There is a significant difference between libftdi and libftd2xx. The latter
337 one allows to schedule up to 64*64 bytes of result data while libftdi fails
338 with more than 4*64. As a consequence, the FT2232 driver is forced to
339 perform around 16x more USB transactions for long command streams with TDO
340 capture when running with libftdi."
343 #define FT2232_BUFFER_SIZE 131072
344 a comment would have been nice.
347 #define FT2232_BUFFER_SIZE 131072
349 static uint8_t* ft2232_buffer
= NULL
;
350 static int ft2232_buffer_size
= 0;
351 static int ft2232_read_pointer
= 0;
352 static int ft2232_expect_read
= 0;
355 * Function buffer_write
356 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
357 * @param val is the byte to send.
359 static inline void buffer_write(uint8_t val
)
361 assert(ft2232_buffer
);
362 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
363 ft2232_buffer
[ft2232_buffer_size
++] = val
;
367 * Function buffer_read
368 * returns a byte from the byte buffer.
370 static inline uint8_t buffer_read(void)
372 assert(ft2232_buffer
);
373 assert(ft2232_read_pointer
< ft2232_buffer_size
);
374 return ft2232_buffer
[ft2232_read_pointer
++];
378 * Clocks out \a bit_count bits on the TMS line, starting with the least
379 * significant bit of tms_bits and progressing to more significant bits.
380 * Rigorous state transition logging is done here via tap_set_state().
382 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
383 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
384 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
385 * is often used for this, 0x4b.
387 * @param tms_bits Holds the sequence of bits to send.
388 * @param tms_count Tells how many bits in the sequence.
389 * @param tdi_bit A single bit to pass on to TDI before the first TCK
390 * cycle and held static for the duration of TMS clocking.
392 * See the MPSSE spec referenced above.
394 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
398 int tms_ndx
; /* bit index into tms_byte */
400 assert(tms_count
> 0);
402 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
403 mpsse_cmd
, tms_bits
, tms_count
);
405 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
407 bool bit
= tms_bits
& 1;
410 tms_byte
|= (1 << tms_ndx
);
412 /* always do state transitions in public view */
413 tap_set_state(tap_state_transition(tap_get_state(), bit
));
415 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
420 if (tms_ndx
== 7 || i
== tms_count
-1)
422 buffer_write(mpsse_cmd
);
423 buffer_write(tms_ndx
- 1);
425 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
426 TMS/CS and is held static for the duration of TMS/CS clocking.
428 buffer_write(tms_byte
| (tdi_bit
<< 7));
434 * Function get_tms_buffer_requirements
435 * returns what clock_tms() will consume if called with
438 static inline int get_tms_buffer_requirements(int bit_count
)
440 return ((bit_count
+ 6)/7) * 3;
444 * Function move_to_state
445 * moves the TAP controller from the current state to a
446 * \a goal_state through a path given by tap_get_tms_path(). State transition
447 * logging is performed by delegation to clock_tms().
449 * @param goal_state is the destination state for the move.
451 static void move_to_state(tap_state_t goal_state
)
453 tap_state_t start_state
= tap_get_state();
455 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
456 lookup of the required TMS pattern to move to this state from the
460 /* do the 2 lookups */
461 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
462 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
464 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
466 clock_tms(0x4b, tms_bits
, tms_count
, 0);
469 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
471 #if BUILD_FT2232_FTD2XX == 1
473 DWORD dw_bytes_written
;
474 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
476 *bytes_written
= dw_bytes_written
;
477 LOG_ERROR("FT_Write returned: %lu", status
);
478 return ERROR_JTAG_DEVICE_ERROR
;
482 *bytes_written
= dw_bytes_written
;
484 #elif BUILD_FT2232_LIBFTDI == 1
486 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
489 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
490 return ERROR_JTAG_DEVICE_ERROR
;
494 *bytes_written
= retval
;
498 if (*bytes_written
!= (uint32_t)size
)
500 return ERROR_JTAG_DEVICE_ERROR
;
506 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
508 #if BUILD_FT2232_FTD2XX == 1
514 while ((*bytes_read
< size
) && timeout
--)
516 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
517 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
520 LOG_ERROR("FT_Read returned: %lu", status
);
521 return ERROR_JTAG_DEVICE_ERROR
;
523 *bytes_read
+= dw_bytes_read
;
526 #elif BUILD_FT2232_LIBFTDI == 1
528 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
531 while ((*bytes_read
< size
) && timeout
--)
533 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
536 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
537 return ERROR_JTAG_DEVICE_ERROR
;
539 *bytes_read
+= retval
;
544 if (*bytes_read
< size
)
546 LOG_ERROR("couldn't read enough bytes from "
547 "FT2232 device (%i < %i)",
548 (unsigned)*bytes_read
,
550 return ERROR_JTAG_DEVICE_ERROR
;
556 static bool ft2232_device_is_highspeed(void)
558 #if BUILD_FT2232_FTD2XX == 1
559 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
560 #elif BUILD_FT2232_LIBFTDI == 1
561 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
566 * Commands that only apply to the FT2232H and FT4232H devices.
567 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
568 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
571 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
573 uint8_t buf
= enable
? 0x96 : 0x97;
574 LOG_DEBUG("%2.2x", buf
);
576 uint32_t bytes_written
;
579 if ((retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
581 LOG_ERROR("couldn't write command to %s adaptive clocking"
582 , enable
? "enable" : "disable");
590 * Enable/disable the clk divide by 5 of the 60MHz master clock.
591 * This result in a JTAG clock speed range of 91.553Hz-6MHz
592 * respective 457.763Hz-30MHz.
594 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
596 uint32_t bytes_written
;
597 uint8_t buf
= enable
? 0x8b : 0x8a;
599 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
601 LOG_ERROR("couldn't write command to %s clk divide by 5"
602 , enable
? "enable" : "disable");
603 return ERROR_JTAG_INIT_FAILED
;
605 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
606 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
611 static int ft2232_speed(int speed
)
615 uint32_t bytes_written
;
618 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
619 if (ft2232_device_is_highspeed())
620 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
621 else if (enable_adaptive_clocking
)
623 LOG_ERROR("ft2232 device %lu does not support RTCK"
624 , (long unsigned int)ftdi_device
);
628 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
631 buf
[0] = 0x86; /* command "set divisor" */
632 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
633 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
635 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
636 if ((retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
638 LOG_ERROR("couldn't set FT2232 TCK speed");
645 static int ft2232_speed_div(int speed
, int* khz
)
647 /* Take a look in the FT2232 manual,
648 * AN2232C-01 Command Processor for
649 * MPSSE and MCU Host Bus. Chapter 3.8 */
651 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
656 static int ft2232_khz(int khz
, int* jtag_speed
)
660 if (ft2232_device_is_highspeed())
662 *jtag_speed
= RTCK_SPEED
;
667 LOG_DEBUG("RCLK not supported");
672 /* Take a look in the FT2232 manual,
673 * AN2232C-01 Command Processor for
674 * MPSSE and MCU Host Bus. Chapter 3.8
676 * We will calc here with a multiplier
677 * of 10 for better rounding later. */
679 /* Calc speed, (ft2232_max_tck / khz) - 1 */
680 /* Use 65000 for better rounding */
681 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
683 /* Add 0.9 for rounding */
686 /* Calc real speed */
687 *jtag_speed
= *jtag_speed
/ 10;
689 /* Check if speed is greater than 0 */
695 /* Check max value */
696 if (*jtag_speed
> 0xFFFF)
698 *jtag_speed
= 0xFFFF;
704 static void ft2232_end_state(tap_state_t state
)
706 if (tap_is_state_stable(state
))
707 tap_set_end_state(state
);
710 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
715 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
717 int num_bytes
= (scan_size
+ 7) / 8;
718 int bits_left
= scan_size
;
721 while (num_bytes
-- > 1)
723 buffer
[cur_byte
++] = buffer_read();
727 buffer
[cur_byte
] = 0x0;
729 /* There is one more partial byte left from the clock data in/out instructions */
732 buffer
[cur_byte
] = buffer_read() >> 1;
734 /* 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 */
735 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
738 static void ft2232_debug_dump_buffer(void)
744 for (i
= 0; i
< ft2232_buffer_size
; i
++)
746 line_p
+= snprintf(line_p
, sizeof(line
) - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
749 LOG_DEBUG("%s", line
);
755 LOG_DEBUG("%s", line
);
758 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
760 struct jtag_command
* cmd
;
765 uint32_t bytes_written
= 0;
766 uint32_t bytes_read
= 0;
768 #ifdef _DEBUG_USB_IO_
769 struct timeval start
, inter
, inter2
, end
;
770 struct timeval d_inter
, d_inter2
, d_end
;
773 #ifdef _DEBUG_USB_COMMS_
774 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
775 ft2232_debug_dump_buffer();
778 #ifdef _DEBUG_USB_IO_
779 gettimeofday(&start
, NULL
);
782 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
784 LOG_ERROR("couldn't write MPSSE commands to FT2232");
788 #ifdef _DEBUG_USB_IO_
789 gettimeofday(&inter
, NULL
);
792 if (ft2232_expect_read
)
794 /* FIXME this "timeout" is never changed ... */
795 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
796 ft2232_buffer_size
= 0;
798 #ifdef _DEBUG_USB_IO_
799 gettimeofday(&inter2
, NULL
);
802 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
804 LOG_ERROR("couldn't read from FT2232");
808 #ifdef _DEBUG_USB_IO_
809 gettimeofday(&end
, NULL
);
811 timeval_subtract(&d_inter
, &inter
, &start
);
812 timeval_subtract(&d_inter2
, &inter2
, &start
);
813 timeval_subtract(&d_end
, &end
, &start
);
815 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
816 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
817 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
818 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
821 ft2232_buffer_size
= bytes_read
;
823 if (ft2232_expect_read
!= ft2232_buffer_size
)
825 LOG_ERROR("ft2232_expect_read (%i) != "
826 "ft2232_buffer_size (%i) "
830 LIBFTDI_READ_RETRY_COUNT
- timeout
);
831 ft2232_debug_dump_buffer();
836 #ifdef _DEBUG_USB_COMMS_
837 LOG_DEBUG("read buffer (%i retries): %i bytes",
838 LIBFTDI_READ_RETRY_COUNT
- timeout
,
840 ft2232_debug_dump_buffer();
844 ft2232_expect_read
= 0;
845 ft2232_read_pointer
= 0;
847 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
848 * that wasn't handled by a caller-provided error handler
858 type
= jtag_scan_type(cmd
->cmd
.scan
);
859 if (type
!= SCAN_OUT
)
861 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
862 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
863 ft2232_read_scan(type
, buffer
, scan_size
);
864 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
865 retval
= ERROR_JTAG_QUEUE_FAILED
;
877 ft2232_buffer_size
= 0;
883 * Function ft2232_add_pathmove
884 * moves the TAP controller from the current state to a new state through the
885 * given path, where path is an array of tap_state_t's.
887 * @param path is an array of tap_stat_t which gives the states to traverse through
888 * ending with the last state at path[num_states-1]
889 * @param num_states is the count of state steps to move through
891 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
895 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
899 /* this loop verifies that the path is legal and logs each state in the path */
902 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
904 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
906 /* command "Clock Data to TMS/CS Pin (no Read)" */
909 /* number of states remaining */
910 buffer_write(num_states_batch
- 1);
912 while (num_states_batch
--) {
913 /* either TMS=0 or TMS=1 must work ... */
914 if (tap_state_transition(tap_get_state(), false)
915 == path
[state_count
])
916 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
917 else if (tap_state_transition(tap_get_state(), true)
918 == path
[state_count
])
919 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
921 /* ... or else the caller goofed BADLY */
923 LOG_ERROR("BUG: %s -> %s isn't a valid "
924 "TAP state transition",
925 tap_state_name(tap_get_state()),
926 tap_state_name(path
[state_count
]));
930 tap_set_state(path
[state_count
]);
935 buffer_write(tms_byte
);
937 tap_set_end_state(tap_get_state());
940 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
942 int num_bytes
= (scan_size
+ 7) / 8;
943 int bits_left
= scan_size
;
949 if (tap_get_state() != TAP_DRSHIFT
)
951 move_to_state(TAP_DRSHIFT
);
956 if (tap_get_state() != TAP_IRSHIFT
)
958 move_to_state(TAP_IRSHIFT
);
962 /* add command for complete bytes */
963 while (num_bytes
> 1)
968 /* Clock Data Bytes In and Out LSB First */
970 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
972 else if (type
== SCAN_OUT
)
974 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
976 /* LOG_DEBUG("added TDI bytes (o)"); */
978 else if (type
== SCAN_IN
)
980 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
982 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
985 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
986 num_bytes
-= thisrun_bytes
;
988 buffer_write((uint8_t) (thisrun_bytes
- 1));
989 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
993 /* add complete bytes */
994 while (thisrun_bytes
-- > 0)
996 buffer_write(buffer
[cur_byte
++]);
1000 else /* (type == SCAN_IN) */
1002 bits_left
-= 8 * (thisrun_bytes
);
1006 /* the most signifcant bit is scanned during TAP movement */
1007 if (type
!= SCAN_IN
)
1008 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1012 /* process remaining bits but the last one */
1015 if (type
== SCAN_IO
)
1017 /* Clock Data Bits In and Out LSB First */
1019 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1021 else if (type
== SCAN_OUT
)
1023 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1025 /* LOG_DEBUG("added TDI bits (o)"); */
1027 else if (type
== SCAN_IN
)
1029 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1031 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1034 buffer_write(bits_left
- 2);
1035 if (type
!= SCAN_IN
)
1036 buffer_write(buffer
[cur_byte
]);
1039 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1040 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1042 if (type
== SCAN_IO
)
1044 /* Clock Data Bits In and Out LSB First */
1046 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1048 else if (type
== SCAN_OUT
)
1050 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1052 /* LOG_DEBUG("added TDI bits (o)"); */
1054 else if (type
== SCAN_IN
)
1056 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1058 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1061 buffer_write(last_bit
);
1069 /* move from Shift-IR/DR to end state */
1070 if (type
!= SCAN_OUT
)
1072 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1073 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1076 /* Clock Data to TMS/CS Pin with Read */
1081 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1082 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1083 /* Clock Data to TMS/CS Pin (no Read) */
1087 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1088 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1091 if (tap_get_state() != tap_get_end_state())
1093 move_to_state(tap_get_end_state());
1097 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1099 int num_bytes
= (scan_size
+ 7) / 8;
1100 int bits_left
= scan_size
;
1103 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1104 uint8_t* receive_pointer
= receive_buffer
;
1105 uint32_t bytes_written
;
1106 uint32_t bytes_read
;
1108 int thisrun_read
= 0;
1112 LOG_ERROR("BUG: large IR scans are not supported");
1116 if (tap_get_state() != TAP_DRSHIFT
)
1118 move_to_state(TAP_DRSHIFT
);
1121 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1123 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1126 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1127 ft2232_buffer_size
, (int)bytes_written
);
1128 ft2232_buffer_size
= 0;
1130 /* add command for complete bytes */
1131 while (num_bytes
> 1)
1135 if (type
== SCAN_IO
)
1137 /* Clock Data Bytes In and Out LSB First */
1139 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1141 else if (type
== SCAN_OUT
)
1143 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1145 /* LOG_DEBUG("added TDI bytes (o)"); */
1147 else if (type
== SCAN_IN
)
1149 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1151 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1154 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1155 thisrun_read
= thisrun_bytes
;
1156 num_bytes
-= thisrun_bytes
;
1157 buffer_write((uint8_t) (thisrun_bytes
- 1));
1158 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1160 if (type
!= SCAN_IN
)
1162 /* add complete bytes */
1163 while (thisrun_bytes
-- > 0)
1165 buffer_write(buffer
[cur_byte
]);
1170 else /* (type == SCAN_IN) */
1172 bits_left
-= 8 * (thisrun_bytes
);
1175 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1177 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1180 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1182 (int)bytes_written
);
1183 ft2232_buffer_size
= 0;
1185 if (type
!= SCAN_OUT
)
1187 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1189 LOG_ERROR("couldn't read from FT2232");
1192 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1195 receive_pointer
+= bytes_read
;
1201 /* the most signifcant bit is scanned during TAP movement */
1202 if (type
!= SCAN_IN
)
1203 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1207 /* process remaining bits but the last one */
1210 if (type
== SCAN_IO
)
1212 /* Clock Data Bits In and Out LSB First */
1214 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1216 else if (type
== SCAN_OUT
)
1218 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1220 /* LOG_DEBUG("added TDI bits (o)"); */
1222 else if (type
== SCAN_IN
)
1224 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1226 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1228 buffer_write(bits_left
- 2);
1229 if (type
!= SCAN_IN
)
1230 buffer_write(buffer
[cur_byte
]);
1232 if (type
!= SCAN_OUT
)
1236 if (tap_get_end_state() == TAP_DRSHIFT
)
1238 if (type
== SCAN_IO
)
1240 /* Clock Data Bits In and Out LSB First */
1242 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1244 else if (type
== SCAN_OUT
)
1246 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1248 /* LOG_DEBUG("added TDI bits (o)"); */
1250 else if (type
== SCAN_IN
)
1252 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1254 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1257 buffer_write(last_bit
);
1261 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1262 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1265 /* move from Shift-IR/DR to end state */
1266 if (type
!= SCAN_OUT
)
1268 /* Clock Data to TMS/CS Pin with Read */
1270 /* LOG_DEBUG("added TMS scan (read)"); */
1274 /* Clock Data to TMS/CS Pin (no Read) */
1276 /* LOG_DEBUG("added TMS scan (no read)"); */
1279 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1280 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1283 if (type
!= SCAN_OUT
)
1286 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1288 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1291 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1293 (int)bytes_written
);
1294 ft2232_buffer_size
= 0;
1296 if (type
!= SCAN_OUT
)
1298 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1300 LOG_ERROR("couldn't read from FT2232");
1303 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1306 receive_pointer
+= bytes_read
;
1312 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1314 int predicted_size
= 3;
1315 int num_bytes
= (scan_size
- 1) / 8;
1317 if (tap_get_state() != TAP_DRSHIFT
)
1318 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1320 if (type
== SCAN_IN
) /* only from device to host */
1322 /* complete bytes */
1323 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1325 /* remaining bits - 1 (up to 7) */
1326 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1328 else /* host to device, or bidirectional */
1330 /* complete bytes */
1331 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1333 /* remaining bits -1 (up to 7) */
1334 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1337 return predicted_size
;
1340 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1342 int predicted_size
= 0;
1344 if (type
!= SCAN_OUT
)
1346 /* complete bytes */
1347 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1349 /* remaining bits - 1 */
1350 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1352 /* last bit (from TMS scan) */
1353 predicted_size
+= 1;
1356 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1358 return predicted_size
;
1361 /* semi-generic FT2232/FT4232 reset code */
1362 static void ftx23_reset(int trst
, int srst
)
1364 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1367 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1368 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1370 low_output
&= ~nTRST
; /* switch output low */
1374 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1375 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1377 low_output
|= nTRST
; /* switch output high */
1382 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1383 low_output
&= ~nSRST
; /* switch output low */
1385 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1389 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1390 low_output
|= nSRST
; /* switch output high */
1392 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1395 /* command "set data bits low byte" */
1397 buffer_write(low_output
);
1398 buffer_write(low_direction
);
1401 static void jtagkey_reset(int trst
, int srst
)
1403 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1406 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1407 high_output
&= ~nTRSTnOE
;
1409 high_output
&= ~nTRST
;
1413 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1414 high_output
|= nTRSTnOE
;
1416 high_output
|= nTRST
;
1421 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1422 high_output
&= ~nSRST
;
1424 high_output
&= ~nSRSTnOE
;
1428 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1429 high_output
|= nSRST
;
1431 high_output
|= nSRSTnOE
;
1434 /* command "set data bits high byte" */
1436 buffer_write(high_output
);
1437 buffer_write(high_direction
);
1438 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1442 static void olimex_jtag_reset(int trst
, int srst
)
1444 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1447 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1448 high_output
&= ~nTRSTnOE
;
1450 high_output
&= ~nTRST
;
1454 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1455 high_output
|= nTRSTnOE
;
1457 high_output
|= nTRST
;
1462 high_output
|= nSRST
;
1466 high_output
&= ~nSRST
;
1469 /* command "set data bits high byte" */
1471 buffer_write(high_output
);
1472 buffer_write(high_direction
);
1473 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1477 static void axm0432_jtag_reset(int trst
, int srst
)
1481 tap_set_state(TAP_RESET
);
1482 high_output
&= ~nTRST
;
1486 high_output
|= nTRST
;
1491 high_output
&= ~nSRST
;
1495 high_output
|= nSRST
;
1498 /* command "set data bits low byte" */
1500 buffer_write(high_output
);
1501 buffer_write(high_direction
);
1502 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1506 static void flyswatter_reset(int trst
, int srst
)
1510 low_output
&= ~nTRST
;
1514 low_output
|= nTRST
;
1519 low_output
|= nSRST
;
1523 low_output
&= ~nSRST
;
1526 /* command "set data bits low byte" */
1528 buffer_write(low_output
);
1529 buffer_write(low_direction
);
1530 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1533 static void turtle_reset(int trst
, int srst
)
1539 low_output
|= nSRST
;
1543 low_output
&= ~nSRST
;
1546 /* command "set data bits low byte" */
1548 buffer_write(low_output
);
1549 buffer_write(low_direction
);
1550 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1553 static void comstick_reset(int trst
, int srst
)
1557 high_output
&= ~nTRST
;
1561 high_output
|= nTRST
;
1566 high_output
&= ~nSRST
;
1570 high_output
|= nSRST
;
1573 /* command "set data bits high byte" */
1575 buffer_write(high_output
);
1576 buffer_write(high_direction
);
1577 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1581 static void stm32stick_reset(int trst
, int srst
)
1585 high_output
&= ~nTRST
;
1589 high_output
|= nTRST
;
1594 low_output
&= ~nSRST
;
1598 low_output
|= nSRST
;
1601 /* command "set data bits low byte" */
1603 buffer_write(low_output
);
1604 buffer_write(low_direction
);
1606 /* command "set data bits high byte" */
1608 buffer_write(high_output
);
1609 buffer_write(high_direction
);
1610 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1614 static void sheevaplug_reset(int trst
, int srst
)
1617 high_output
&= ~nTRST
;
1619 high_output
|= nTRST
;
1622 high_output
&= ~nSRSTnOE
;
1624 high_output
|= nSRSTnOE
;
1626 /* command "set data bits high byte" */
1628 buffer_write(high_output
);
1629 buffer_write(high_direction
);
1630 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1633 static void redbee_reset(int trst
, int srst
)
1637 tap_set_state(TAP_RESET
);
1638 high_output
&= ~nTRST
;
1642 high_output
|= nTRST
;
1647 high_output
&= ~nSRST
;
1651 high_output
|= nSRST
;
1654 /* command "set data bits low byte" */
1656 buffer_write(high_output
);
1657 buffer_write(high_direction
);
1658 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1659 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1663 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1667 int predicted_size
= 0;
1670 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1671 cmd
->cmd
.runtest
->num_cycles
,
1672 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1674 /* only send the maximum buffer size that FT2232C can handle */
1676 if (tap_get_state() != TAP_IDLE
)
1677 predicted_size
+= 3;
1678 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1679 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1680 predicted_size
+= 3;
1681 if (tap_get_end_state() != TAP_IDLE
)
1682 predicted_size
+= 3;
1683 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1685 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1686 retval
= ERROR_JTAG_QUEUE_FAILED
;
1690 if (tap_get_state() != TAP_IDLE
)
1692 move_to_state(TAP_IDLE
);
1695 i
= cmd
->cmd
.runtest
->num_cycles
;
1698 /* there are no state transitions in this code, so omit state tracking */
1700 /* command "Clock Data to TMS/CS Pin (no Read)" */
1704 buffer_write((i
> 7) ? 6 : (i
- 1));
1709 i
-= (i
> 7) ? 7 : i
;
1710 /* LOG_DEBUG("added TMS scan (no read)"); */
1713 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1715 if (tap_get_state() != tap_get_end_state())
1717 move_to_state(tap_get_end_state());
1721 DEBUG_JTAG_IO("runtest: %i, end in %s",
1722 cmd
->cmd
.runtest
->num_cycles
,
1723 tap_state_name(tap_get_end_state()));
1727 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1729 int predicted_size
= 0;
1730 int retval
= ERROR_OK
;
1732 DEBUG_JTAG_IO("statemove end in %s",
1733 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1735 /* only send the maximum buffer size that FT2232C can handle */
1737 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1739 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1740 retval
= ERROR_JTAG_QUEUE_FAILED
;
1744 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1746 /* For TAP_RESET, ignore the current recorded state. It's often
1747 * wrong at server startup, and this transation is critical whenever
1750 if (tap_get_end_state() == TAP_RESET
) {
1751 clock_tms(0x4b, 0xff, 5, 0);
1754 /* shortest-path move to desired end state */
1755 } else if (tap_get_state() != tap_get_end_state())
1757 move_to_state(tap_get_end_state());
1765 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1766 * (or SWD) state machine.
1768 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1770 int retval
= ERROR_OK
;
1771 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1772 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1775 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1777 /* only send the maximum buffer size that FT2232C can handle */
1778 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1779 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1780 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1781 retval
= ERROR_JTAG_QUEUE_FAILED
;
1787 /* Shift out in batches of at most 6 bits; there's a report of an
1788 * FT2232 bug in this area, where shifting exactly 7 bits can make
1789 * problems with TMS signaling for the last clock cycle:
1791 * http://developer.intra2net.com/mailarchive/html/
1792 * libftdi/2009/msg00292.html
1794 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1796 * Note that pathmoves in JTAG are not often seven bits, so that
1797 * isn't a particularly likely situation outside of "special"
1798 * signaling such as switching between JTAG and SWD modes.
1801 if (num_bits
<= 6) {
1803 buffer_write(num_bits
- 1);
1804 buffer_write(*bits
& 0x3f);
1808 /* Yes, this is lazy ... we COULD shift out more data
1809 * bits per operation, but doing it in nybbles is easy
1813 buffer_write(*bits
& 0xf);
1816 count
= (num_bits
> 4) ? 4 : num_bits
;
1819 buffer_write(count
- 1);
1820 buffer_write((*bits
>> 4) & 0xf);
1830 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1832 int predicted_size
= 0;
1833 int retval
= ERROR_OK
;
1835 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1836 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1838 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1839 tap_state_name(tap_get_state()),
1840 tap_state_name(path
[num_states
-1]));
1842 /* only send the maximum buffer size that FT2232C can handle */
1843 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1844 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1846 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1847 retval
= ERROR_JTAG_QUEUE_FAILED
;
1853 ft2232_add_pathmove(path
, num_states
);
1859 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1862 int scan_size
; /* size of IR or DR scan */
1863 int predicted_size
= 0;
1864 int retval
= ERROR_OK
;
1866 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1868 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1870 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1872 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1873 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1875 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1876 /* unsent commands before this */
1877 if (first_unsent
!= cmd
)
1878 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1879 retval
= ERROR_JTAG_QUEUE_FAILED
;
1881 /* current command */
1882 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1883 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1885 first_unsent
= cmd
->next
;
1890 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1892 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1895 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1896 retval
= ERROR_JTAG_QUEUE_FAILED
;
1900 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1901 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1902 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1903 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1907 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1908 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1909 tap_state_name(tap_get_end_state()));
1914 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1917 int predicted_size
= 0;
1920 DEBUG_JTAG_IO("reset trst: %i srst %i",
1921 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1923 /* only send the maximum buffer size that FT2232C can handle */
1925 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1927 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1928 retval
= ERROR_JTAG_QUEUE_FAILED
;
1933 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1935 tap_set_state(TAP_RESET
);
1938 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1941 DEBUG_JTAG_IO("trst: %i, srst: %i",
1942 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1946 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1951 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1953 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1954 retval
= ERROR_JTAG_QUEUE_FAILED
;
1955 first_unsent
= cmd
->next
;
1956 jtag_sleep(cmd
->cmd
.sleep
->us
);
1957 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1959 tap_state_name(tap_get_state()));
1963 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1968 /* this is only allowed while in a stable state. A check for a stable
1969 * state was done in jtag_add_clocks()
1971 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1972 retval
= ERROR_JTAG_QUEUE_FAILED
;
1973 DEBUG_JTAG_IO("clocks %i while in %s",
1974 cmd
->cmd
.stableclocks
->num_cycles
,
1975 tap_state_name(tap_get_state()));
1979 static int ft2232_execute_command(struct jtag_command
*cmd
)
1985 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1986 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1987 case JTAG_TLR_RESET
: retval
= ft2232_execute_statemove(cmd
); break;
1988 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1989 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1990 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1991 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1993 retval
= ft2232_execute_tms(cmd
);
1996 LOG_ERROR("BUG: unknown JTAG command type encountered");
1997 retval
= ERROR_JTAG_QUEUE_FAILED
;
2003 static int ft2232_execute_queue(void)
2005 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
2008 first_unsent
= cmd
; /* next command that has to be sent */
2011 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2012 * that wasn't handled by a caller-provided error handler
2016 ft2232_buffer_size
= 0;
2017 ft2232_expect_read
= 0;
2019 /* blink, if the current layout has that feature */
2025 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2026 retval
= ERROR_JTAG_QUEUE_FAILED
;
2027 /* Start reading input before FT2232 TX buffer fills up */
2029 if (ft2232_expect_read
> 256)
2031 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2032 retval
= ERROR_JTAG_QUEUE_FAILED
;
2037 if (require_send
> 0)
2038 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2039 retval
= ERROR_JTAG_QUEUE_FAILED
;
2044 #if BUILD_FT2232_FTD2XX == 1
2045 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2049 char SerialNumber
[16];
2050 char Description
[64];
2051 DWORD openex_flags
= 0;
2052 char* openex_string
= NULL
;
2053 uint8_t latency_timer
;
2055 if (layout
== NULL
) {
2056 LOG_WARNING("No ft2232 layout specified'");
2057 return ERROR_JTAG_INIT_FAILED
;
2060 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2063 /* Add non-standard Vid/Pid to the linux driver */
2064 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2066 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2070 if (ft2232_device_desc
&& ft2232_serial
)
2072 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2073 ft2232_device_desc
= NULL
;
2076 if (ft2232_device_desc
)
2078 openex_string
= ft2232_device_desc
;
2079 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2081 else if (ft2232_serial
)
2083 openex_string
= ft2232_serial
;
2084 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2088 LOG_ERROR("neither device description nor serial number specified");
2089 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2091 return ERROR_JTAG_INIT_FAILED
;
2094 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2095 if (status
!= FT_OK
) {
2096 /* under Win32, the FTD2XX driver appends an "A" to the end
2097 * of the description, if we tried by the desc, then
2098 * try by the alternate "A" description. */
2099 if (openex_string
== ft2232_device_desc
) {
2100 /* Try the alternate method. */
2101 openex_string
= ft2232_device_desc_A
;
2102 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2103 if (status
== FT_OK
) {
2104 /* yea, the "alternate" method worked! */
2106 /* drat, give the user a meaningfull message.
2107 * telling the use we tried *BOTH* methods. */
2108 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2110 ft2232_device_desc_A
);
2115 if (status
!= FT_OK
)
2121 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2123 return ERROR_JTAG_INIT_FAILED
;
2125 LOG_ERROR("unable to open ftdi device: %lu", status
);
2126 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2127 if (status
== FT_OK
)
2129 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2132 for (i
= 0; i
< num_devices
; i
++)
2133 desc_array
[i
] = malloc(64);
2135 desc_array
[num_devices
] = NULL
;
2137 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2139 if (status
== FT_OK
)
2141 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2142 for (i
= 0; i
< num_devices
; i
++)
2143 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2146 for (i
= 0; i
< num_devices
; i
++)
2147 free(desc_array
[i
]);
2153 LOG_ERROR("ListDevices: NONE\n");
2155 return ERROR_JTAG_INIT_FAILED
;
2158 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2160 LOG_ERROR("unable to set latency timer: %lu", status
);
2161 return ERROR_JTAG_INIT_FAILED
;
2164 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2166 LOG_ERROR("unable to get latency timer: %lu", status
);
2167 return ERROR_JTAG_INIT_FAILED
;
2171 LOG_DEBUG("current latency timer: %i", latency_timer
);
2174 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2176 LOG_ERROR("unable to set timeouts: %lu", status
);
2177 return ERROR_JTAG_INIT_FAILED
;
2180 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2182 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2183 return ERROR_JTAG_INIT_FAILED
;
2186 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2188 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2189 return ERROR_JTAG_INIT_FAILED
;
2193 static const char* type_str
[] =
2194 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2195 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2196 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2197 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2198 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2199 LOG_INFO("deviceID: %lu", deviceID
);
2200 LOG_INFO("SerialNumber: %s", SerialNumber
);
2201 LOG_INFO("Description: %s", Description
);
2207 static int ft2232_purge_ftd2xx(void)
2211 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2213 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2214 return ERROR_JTAG_INIT_FAILED
;
2220 #endif /* BUILD_FT2232_FTD2XX == 1 */
2222 #if BUILD_FT2232_LIBFTDI == 1
2223 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2225 uint8_t latency_timer
;
2227 if (layout
== NULL
) {
2228 LOG_WARNING("No ft2232 layout specified'");
2229 return ERROR_JTAG_INIT_FAILED
;
2232 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2233 layout
->name
, vid
, pid
);
2235 if (ftdi_init(&ftdic
) < 0)
2236 return ERROR_JTAG_INIT_FAILED
;
2238 /* default to INTERFACE_A */
2239 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2241 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2243 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2244 return ERROR_JTAG_INIT_FAILED
;
2247 /* context, vendor id, product id */
2248 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2252 LOG_WARNING("unable to open ftdi device (trying more): %s",
2255 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2257 return ERROR_JTAG_INIT_FAILED
;
2260 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2261 if (ftdi_usb_reset(&ftdic
) < 0)
2263 LOG_ERROR("unable to reset ftdi device");
2264 return ERROR_JTAG_INIT_FAILED
;
2267 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2269 LOG_ERROR("unable to set latency timer");
2270 return ERROR_JTAG_INIT_FAILED
;
2273 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2275 LOG_ERROR("unable to get latency timer");
2276 return ERROR_JTAG_INIT_FAILED
;
2280 LOG_DEBUG("current latency timer: %i", latency_timer
);
2283 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2285 ftdi_device
= ftdic
.type
;
2286 static const char* type_str
[] =
2287 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2288 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2289 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2290 ? ftdi_device
: no_of_known_types
;
2291 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2295 static int ft2232_purge_libftdi(void)
2297 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2299 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2300 return ERROR_JTAG_INIT_FAILED
;
2306 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2308 static int ft2232_init(void)
2312 uint32_t bytes_written
;
2314 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2316 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2320 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2323 if (layout
== NULL
) {
2324 LOG_WARNING("No ft2232 layout specified'");
2325 return ERROR_JTAG_INIT_FAILED
;
2328 for (int i
= 0; 1; i
++)
2331 * "more indicates that there are more IDs to try, so we should
2332 * not print an error for an ID mismatch (but for anything
2335 * try_more indicates that the error code returned indicates an
2336 * ID mismatch (and nothing else) and that we should proceeed
2337 * with the next ID pair.
2339 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2342 #if BUILD_FT2232_FTD2XX == 1
2343 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2345 #elif BUILD_FT2232_LIBFTDI == 1
2346 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2347 more
, &try_more
, layout
->channel
);
2351 if (!more
|| !try_more
)
2355 ft2232_buffer_size
= 0;
2356 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2358 if (layout
->init() != ERROR_OK
)
2359 return ERROR_JTAG_INIT_FAILED
;
2361 if (ft2232_device_is_highspeed())
2363 #ifndef BUILD_FT2232_HIGHSPEED
2364 #if BUILD_FT2232_FTD2XX == 1
2365 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2366 #elif BUILD_FT2232_LIBFTDI == 1
2367 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2370 /* make sure the legacy mode is disabled */
2371 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2372 return ERROR_JTAG_INIT_FAILED
;
2375 ft2232_speed(jtag_get_speed());
2377 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2378 if ((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
)
2380 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2381 return ERROR_JTAG_INIT_FAILED
;
2384 #if BUILD_FT2232_FTD2XX == 1
2385 return ft2232_purge_ftd2xx();
2386 #elif BUILD_FT2232_LIBFTDI == 1
2387 return ft2232_purge_libftdi();
2393 /** Updates defaults for DBUS signals: the four JTAG signals
2394 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2396 static inline void ftx232_init_head(void)
2399 low_direction
= 0x0b;
2402 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2403 * the four GPIOL signals. Initialization covers value and direction,
2404 * as customized for each layout.
2406 static int ftx232_init_tail(void)
2409 uint32_t bytes_written
;
2411 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2412 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2414 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2415 low_output
&= ~nTRST
; /* nTRST = 0 */
2419 low_direction
|= nTRSTnOE
; /* nTRST output */
2420 low_output
|= nTRST
; /* nTRST = 1 */
2423 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2425 low_direction
|= nSRSTnOE
; /* nSRST output */
2426 low_output
|= nSRST
; /* nSRST = 1 */
2430 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2431 low_output
&= ~nSRST
; /* nSRST = 0 */
2434 /* initialize low byte for jtag */
2435 buf
[0] = 0x80; /* command "set data bits low byte" */
2436 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2437 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2438 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2440 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2442 LOG_ERROR("couldn't initialize FT2232 DBUS");
2443 return ERROR_JTAG_INIT_FAILED
;
2449 static int usbjtag_init(void)
2452 * NOTE: This is now _specific_ to the "usbjtag" layout.
2453 * Don't try cram any more layouts into this.
2462 return ftx232_init_tail();
2465 static int lm3s811_jtag_init(void)
2469 /* There are multiple revisions of LM3S811 eval boards:
2470 * - Rev B (and older?) boards have no SWO trace support.
2471 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2472 * they should use the "luminary_icdi" layout instead.
2479 low_direction
= 0x8b;
2481 return ftx232_init_tail();
2484 static int icdi_jtag_init(void)
2488 /* Most Luminary eval boards support SWO trace output,
2489 * and should use this "luminary_icdi" layout.
2496 low_direction
= 0xcb;
2498 return ftx232_init_tail();
2501 static int signalyzer_init(void)
2509 return ftx232_init_tail();
2512 static int axm0432_jtag_init(void)
2515 uint32_t bytes_written
;
2518 low_direction
= 0x2b;
2520 /* initialize low byte for jtag */
2521 buf
[0] = 0x80; /* command "set data bits low byte" */
2522 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2523 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2524 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2526 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2528 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2529 return ERROR_JTAG_INIT_FAILED
;
2532 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2535 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2537 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2541 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2546 high_direction
= 0x0c;
2548 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2549 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2551 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2555 high_output
|= nTRST
;
2558 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2560 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2564 high_output
|= nSRST
;
2567 /* initialize high port */
2568 buf
[0] = 0x82; /* command "set data bits high byte" */
2569 buf
[1] = high_output
; /* value */
2570 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2571 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2573 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2575 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2576 return ERROR_JTAG_INIT_FAILED
;
2582 static int redbee_init(void)
2585 uint32_t bytes_written
;
2588 low_direction
= 0x2b;
2590 /* initialize low byte for jtag */
2591 /* command "set data bits low byte" */
2593 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2594 buf
[2] = low_direction
;
2595 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2596 buf
[1] = low_output
;
2597 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2599 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2601 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2602 return ERROR_JTAG_INIT_FAILED
;
2606 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2608 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2611 high_direction
= 0x0c;
2613 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2614 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2616 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2620 high_output
|= nTRST
;
2623 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2625 LOG_ERROR("can't set nSRST to push-pull on redbee");
2629 high_output
|= nSRST
;
2632 /* initialize high port */
2633 buf
[0] = 0x82; /* command "set data bits high byte" */
2634 buf
[1] = high_output
; /* value */
2635 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2636 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2638 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2640 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2641 return ERROR_JTAG_INIT_FAILED
;
2647 static int jtagkey_init(void)
2650 uint32_t bytes_written
;
2653 low_direction
= 0x1b;
2655 /* initialize low byte for jtag */
2656 buf
[0] = 0x80; /* command "set data bits low byte" */
2657 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2658 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2659 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2661 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2663 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2664 return ERROR_JTAG_INIT_FAILED
;
2667 if (strcmp(layout
->name
, "jtagkey") == 0)
2674 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2675 || (strcmp(layout
->name
, "oocdlink") == 0))
2684 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2689 high_direction
= 0x0f;
2691 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2692 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2694 high_output
|= nTRSTnOE
;
2695 high_output
&= ~nTRST
;
2699 high_output
&= ~nTRSTnOE
;
2700 high_output
|= nTRST
;
2703 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2705 high_output
&= ~nSRSTnOE
;
2706 high_output
|= nSRST
;
2710 high_output
|= nSRSTnOE
;
2711 high_output
&= ~nSRST
;
2714 /* initialize high port */
2715 buf
[0] = 0x82; /* command "set data bits high byte" */
2716 buf
[1] = high_output
; /* value */
2717 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2718 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2720 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2722 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2723 return ERROR_JTAG_INIT_FAILED
;
2729 static int olimex_jtag_init(void)
2732 uint32_t bytes_written
;
2735 low_direction
= 0x1b;
2737 /* initialize low byte for jtag */
2738 buf
[0] = 0x80; /* command "set data bits low byte" */
2739 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2740 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2741 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2743 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2745 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2746 return ERROR_JTAG_INIT_FAILED
;
2752 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2755 high_direction
= 0x0f;
2757 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2758 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2760 high_output
|= nTRSTnOE
;
2761 high_output
&= ~nTRST
;
2765 high_output
&= ~nTRSTnOE
;
2766 high_output
|= nTRST
;
2769 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2771 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2775 high_output
&= ~nSRST
;
2778 /* turn red LED on */
2779 high_output
|= 0x08;
2781 /* initialize high port */
2782 buf
[0] = 0x82; /* command "set data bits high byte" */
2783 buf
[1] = high_output
; /* value */
2784 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2785 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2787 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2789 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2790 return ERROR_JTAG_INIT_FAILED
;
2796 static int flyswatter_init(void)
2799 uint32_t bytes_written
;
2802 low_direction
= 0xfb;
2804 /* initialize low byte for jtag */
2805 buf
[0] = 0x80; /* command "set data bits low byte" */
2806 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2807 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2808 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2810 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2812 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2813 return ERROR_JTAG_INIT_FAILED
;
2817 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2819 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2822 high_direction
= 0x0c;
2824 /* turn red LED3 on, LED2 off */
2825 high_output
|= 0x08;
2827 /* initialize high port */
2828 buf
[0] = 0x82; /* command "set data bits high byte" */
2829 buf
[1] = high_output
; /* value */
2830 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2831 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2833 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2835 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2836 return ERROR_JTAG_INIT_FAILED
;
2842 static int turtle_init(void)
2845 uint32_t bytes_written
;
2848 low_direction
= 0x5b;
2850 /* initialize low byte for jtag */
2851 buf
[0] = 0x80; /* command "set data bits low byte" */
2852 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2853 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2854 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2856 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2858 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2859 return ERROR_JTAG_INIT_FAILED
;
2865 high_direction
= 0x0C;
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
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2875 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2876 return ERROR_JTAG_INIT_FAILED
;
2882 static int comstick_init(void)
2885 uint32_t bytes_written
;
2888 low_direction
= 0x0b;
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
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2898 LOG_ERROR("couldn't initialize FT2232 with 'comstick' 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
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2918 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2919 return ERROR_JTAG_INIT_FAILED
;
2925 static int stm32stick_init(void)
2928 uint32_t bytes_written
;
2931 low_direction
= 0x8b;
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, nOE = out */
2937 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2939 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2941 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2942 return ERROR_JTAG_INIT_FAILED
;
2946 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2948 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2951 high_direction
= 0x03;
2953 /* initialize high port */
2954 buf
[0] = 0x82; /* command "set data bits high byte" */
2955 buf
[1] = high_output
;
2956 buf
[2] = high_direction
;
2957 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2959 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2961 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2962 return ERROR_JTAG_INIT_FAILED
;
2968 static int sheevaplug_init(void)
2971 uint32_t bytes_written
;
2974 low_direction
= 0x1b;
2976 /* initialize low byte for jtag */
2977 buf
[0] = 0x80; /* command "set data bits low byte" */
2978 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2979 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2980 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2982 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2984 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2985 return ERROR_JTAG_INIT_FAILED
;
2994 high_direction
= 0x0f;
2996 /* nTRST is always push-pull */
2997 high_output
&= ~nTRSTnOE
;
2998 high_output
|= nTRST
;
3000 /* nSRST is always open-drain */
3001 high_output
|= nSRSTnOE
;
3002 high_output
&= ~nSRST
;
3004 /* initialize high port */
3005 buf
[0] = 0x82; /* command "set data bits high byte" */
3006 buf
[1] = high_output
; /* value */
3007 buf
[2] = high_direction
; /* all outputs - xRST */
3008 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3010 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3012 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3013 return ERROR_JTAG_INIT_FAILED
;
3019 static int cortino_jtag_init(void)
3022 uint32_t bytes_written
;
3025 low_direction
= 0x1b;
3027 /* initialize low byte for jtag */
3028 buf
[0] = 0x80; /* command "set data bits low byte" */
3029 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3030 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3031 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3033 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3035 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3036 return ERROR_JTAG_INIT_FAILED
;
3040 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3042 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3045 high_direction
= 0x03;
3047 /* initialize high port */
3048 buf
[0] = 0x82; /* command "set data bits high byte" */
3049 buf
[1] = high_output
;
3050 buf
[2] = high_direction
;
3051 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3053 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3055 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3056 return ERROR_JTAG_INIT_FAILED
;
3062 static void olimex_jtag_blink(void)
3064 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3065 * ACBUS3 is bit 3 of the GPIOH port
3067 if (high_output
& 0x08)
3069 /* set port pin high */
3070 high_output
&= 0x07;
3074 /* set port pin low */
3075 high_output
|= 0x08;
3079 buffer_write(high_output
);
3080 buffer_write(high_direction
);
3083 static void flyswatter_jtag_blink(void)
3086 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3088 high_output
^= 0x0c;
3091 buffer_write(high_output
);
3092 buffer_write(high_direction
);
3095 static void turtle_jtag_blink(void)
3098 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3100 if (high_output
& 0x08)
3110 buffer_write(high_output
);
3111 buffer_write(high_direction
);
3114 static int ft2232_quit(void)
3116 #if BUILD_FT2232_FTD2XX == 1
3119 status
= FT_Close(ftdih
);
3120 #elif BUILD_FT2232_LIBFTDI == 1
3121 ftdi_usb_close(&ftdic
);
3123 ftdi_deinit(&ftdic
);
3126 free(ft2232_buffer
);
3127 ft2232_buffer
= NULL
;
3132 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3138 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3139 cp
= strchr(ft2232_device_desc
, 0);
3140 /* under Win32, the FTD2XX driver appends an "A" to the end
3141 * of the description, this examines the given desc
3142 * and creates the 'missing' _A or non_A variable. */
3143 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3144 /* it was, so make this the "A" version. */
3145 ft2232_device_desc_A
= ft2232_device_desc
;
3146 /* and *CREATE* the non-A version. */
3147 strcpy(buf
, ft2232_device_desc
);
3148 cp
= strchr(buf
, 0);
3150 ft2232_device_desc
= strdup(buf
);
3152 /* <space > A not defined
3154 sprintf(buf
, "%s A", ft2232_device_desc
);
3155 ft2232_device_desc_A
= strdup(buf
);
3160 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3166 COMMAND_HANDLER(ft2232_handle_serial_command
)
3170 ft2232_serial
= strdup(CMD_ARGV
[0]);
3174 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3180 COMMAND_HANDLER(ft2232_handle_layout_command
)
3182 if (CMD_ARGC
!= 1) {
3183 LOG_ERROR("Need exactly one argument to ft2232_layout");
3188 LOG_ERROR("already specified ft2232_layout %s",
3190 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3195 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3196 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3202 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3206 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3208 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3210 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3211 "(maximum is %d pairs)", MAX_USB_IDS
);
3212 CMD_ARGC
= MAX_USB_IDS
* 2;
3214 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3216 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3218 return ERROR_COMMAND_SYNTAX_ERROR
;
3219 /* remove the incomplete trailing id */
3224 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3226 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3227 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3231 * Explicitly terminate, in case there are multiples instances of
3234 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3239 COMMAND_HANDLER(ft2232_handle_latency_command
)
3243 ft2232_latency
= atoi(CMD_ARGV
[0]);
3247 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3253 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3257 /* 7 bits of either ones or zeros. */
3258 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3260 while (num_cycles
> 0)
3262 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3263 * at most 7 bits per invocation. Here we invoke it potentially
3266 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3268 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3270 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3271 retval
= ERROR_JTAG_QUEUE_FAILED
;
3276 /* there are no state transitions in this code, so omit state tracking */
3278 /* command "Clock Data to TMS/CS Pin (no Read)" */
3282 buffer_write(bitcount_per_command
- 1);
3284 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3289 num_cycles
-= bitcount_per_command
;
3295 /* ---------------------------------------------------------------------
3296 * Support for IceBear JTAG adapter from Section5:
3297 * http://section5.ch/icebear
3299 * Author: Sten, debian@sansys-electronic.com
3302 /* Icebear pin layout
3304 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3305 * GND GND | 4 3| n.c.
3306 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3307 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3308 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3309 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3310 * ADBUS2 TDO |14 13| GND GND
3312 * ADBUS0 O L TCK ACBUS0 GND
3313 * ADBUS1 O L TDI ACBUS1 GND
3314 * ADBUS2 I TDO ACBUS2 n.c.
3315 * ADBUS3 O H TMS ACBUS3 n.c.
3321 static int icebear_jtag_init(void) {
3323 uint32_t bytes_written
;
3325 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3326 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3330 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3331 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3332 low_direction
&= ~nTRST
; /* nTRST high impedance */
3335 low_direction
|= nTRST
;
3336 low_output
|= nTRST
;
3339 low_direction
|= nSRST
;
3340 low_output
|= nSRST
;
3342 /* initialize low byte for jtag */
3343 buf
[0] = 0x80; /* command "set data bits low byte" */
3344 buf
[1] = low_output
;
3345 buf
[2] = low_direction
;
3346 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3348 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3349 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3350 return ERROR_JTAG_INIT_FAILED
;
3354 high_direction
= 0x00;
3357 /* initialize high port */
3358 buf
[0] = 0x82; /* command "set data bits high byte" */
3359 buf
[1] = high_output
; /* value */
3360 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3361 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3363 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3364 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3365 return ERROR_JTAG_INIT_FAILED
;
3371 static void icebear_jtag_reset(int trst
, int srst
) {
3374 low_direction
|= nTRST
;
3375 low_output
&= ~nTRST
;
3377 else if (trst
== 0) {
3378 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3379 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3380 low_direction
&= ~nTRST
;
3382 low_output
|= nTRST
;
3386 low_output
&= ~nSRST
;
3388 else if (srst
== 0) {
3389 low_output
|= nSRST
;
3392 /* command "set data bits low byte" */
3394 buffer_write(low_output
);
3395 buffer_write(low_direction
);
3397 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3400 /* ---------------------------------------------------------------------
3401 * Support for Signalyzer H2 and Signalyzer H4
3402 * JTAG adapter from Xverve Technologies Inc.
3403 * http://www.signalyzer.com or http://www.xverve.com
3405 * Author: Oleg Seiljus, oleg@signalyzer.com
3407 static unsigned char signalyzer_h_side
;
3408 static unsigned int signalyzer_h_adapter_type
;
3410 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3412 #if BUILD_FT2232_FTD2XX == 1
3413 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3416 #define SIGNALYZER_COMMAND_ADDR 128
3417 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3419 #define SIGNALYZER_COMMAND_VERSION 0x41
3420 #define SIGNALYZER_COMMAND_RESET 0x42
3421 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3422 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3423 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3424 #define SIGNALYZER_COMMAND_LED_SET 0x53
3425 #define SIGNALYZER_COMMAND_ADC 0x54
3426 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3427 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3428 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3429 #define SIGNALYZER_COMMAND_I2C 0x58
3431 #define SIGNALYZER_CHAN_A 1
3432 #define SIGNALYZER_CHAN_B 2
3433 /* LEDS use channel C */
3434 #define SIGNALYZER_CHAN_C 4
3436 #define SIGNALYZER_LED_GREEN 1
3437 #define SIGNALYZER_LED_RED 2
3439 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3440 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3441 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3442 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3443 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3446 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3448 #if BUILD_FT2232_FTD2XX == 1
3449 return FT_WriteEE(ftdih
, address
, value
);
3450 #elif BUILD_FT2232_LIBFTDI == 1
3455 #if BUILD_FT2232_FTD2XX == 1
3456 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3458 return FT_ReadEE(ftdih
, address
, value
);
3462 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3463 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3465 unsigned char on_time
;
3466 unsigned char off_time
;
3468 if (on_time_ms
< 0xFFFF)
3469 on_time
= (unsigned char)(on_time_ms
/ 62);
3473 off_time
= (unsigned char)(off_time_ms
/ 62);
3475 #if BUILD_FT2232_FTD2XX == 1
3478 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3479 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3481 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3482 return ERROR_JTAG_DEVICE_ERROR
;
3485 if ((status
= signalyzer_h_ctrl_write(
3486 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3487 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3489 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3490 return ERROR_JTAG_DEVICE_ERROR
;
3493 if ((status
= signalyzer_h_ctrl_write(
3494 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3495 ((uint32_t)cycles
))) != FT_OK
)
3497 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3498 return ERROR_JTAG_DEVICE_ERROR
;
3501 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3502 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3504 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3505 return ERROR_JTAG_DEVICE_ERROR
;
3509 #elif BUILD_FT2232_LIBFTDI == 1
3512 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3513 ((uint32_t)(channel
<< 8) | led
))) < 0)
3515 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3516 ftdi_get_error_string(&ftdic
));
3517 return ERROR_JTAG_DEVICE_ERROR
;
3520 if ((retval
= signalyzer_h_ctrl_write(
3521 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3522 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3524 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3525 ftdi_get_error_string(&ftdic
));
3526 return ERROR_JTAG_DEVICE_ERROR
;
3529 if ((retval
= signalyzer_h_ctrl_write(
3530 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3531 (uint32_t)cycles
)) < 0)
3533 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3534 ftdi_get_error_string(&ftdic
));
3535 return ERROR_JTAG_DEVICE_ERROR
;
3538 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3539 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3541 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3542 ftdi_get_error_string(&ftdic
));
3543 return ERROR_JTAG_DEVICE_ERROR
;
3550 static int signalyzer_h_init(void)
3552 #if BUILD_FT2232_FTD2XX == 1
3559 uint16_t read_buf
[12] = { 0 };
3561 uint32_t bytes_written
;
3563 /* turn on center green led */
3564 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3565 0xFFFF, 0x00, 0x00);
3567 /* determine what channel config wants to open
3568 * TODO: change me... current implementation is made to work
3569 * with openocd description parsing.
3571 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3575 signalyzer_h_side
= *(end_of_desc
- 1);
3576 if (signalyzer_h_side
== 'B')
3577 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3579 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3583 LOG_ERROR("No Channel was specified");
3587 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3590 #if BUILD_FT2232_FTD2XX == 1
3591 /* read signalyzer versionining information */
3592 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3593 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3595 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3596 return ERROR_JTAG_DEVICE_ERROR
;
3599 for (i
= 0; i
< 10; i
++)
3601 if ((status
= signalyzer_h_ctrl_read(
3602 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3603 &read_buf
[i
])) != FT_OK
)
3605 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3607 return ERROR_JTAG_DEVICE_ERROR
;
3611 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3612 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3613 read_buf
[4], read_buf
[5], read_buf
[6]);
3615 /* set gpio register */
3616 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3617 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3619 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3620 return ERROR_JTAG_DEVICE_ERROR
;
3623 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3626 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3627 return ERROR_JTAG_DEVICE_ERROR
;
3630 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3631 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3633 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3634 return ERROR_JTAG_DEVICE_ERROR
;
3637 /* read adapter type information */
3638 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3639 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3641 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3642 return ERROR_JTAG_DEVICE_ERROR
;
3645 if ((status
= signalyzer_h_ctrl_write(
3646 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3648 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3649 return ERROR_JTAG_DEVICE_ERROR
;
3652 if ((status
= signalyzer_h_ctrl_write(
3653 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3655 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3656 return ERROR_JTAG_DEVICE_ERROR
;
3659 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3660 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3662 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3663 return ERROR_JTAG_DEVICE_ERROR
;
3668 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3669 &read_buf
[0])) != FT_OK
)
3671 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3672 return ERROR_JTAG_DEVICE_ERROR
;
3675 if (read_buf
[0] != 0x0498)
3676 signalyzer_h_adapter_type
= 0x0000;
3679 for (i
= 0; i
< 4; i
++)
3681 if ((status
= signalyzer_h_ctrl_read(
3682 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3683 &read_buf
[i
])) != FT_OK
)
3685 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3687 return ERROR_JTAG_DEVICE_ERROR
;
3691 signalyzer_h_adapter_type
= read_buf
[0];
3694 #elif BUILD_FT2232_LIBFTDI == 1
3695 /* currently libftdi does not allow reading individual eeprom
3696 * locations, therefore adapter type cannot be detected.
3697 * override with most common type
3699 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3702 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3704 /* ADAPTOR: EM_LT16_A */
3705 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3707 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3708 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3716 low_direction
= 0x1b;
3719 high_direction
= 0x0;
3721 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3723 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3724 low_output
&= ~nTRST
; /* nTRST = 0 */
3728 low_direction
|= nTRSTnOE
; /* nTRST output */
3729 low_output
|= nTRST
; /* nTRST = 1 */
3732 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3734 low_direction
|= nSRSTnOE
; /* nSRST output */
3735 low_output
|= nSRST
; /* nSRST = 1 */
3739 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3740 low_output
&= ~nSRST
; /* nSRST = 0 */
3743 #if BUILD_FT2232_FTD2XX == 1
3744 /* enable power to the module */
3745 if ((status
= signalyzer_h_ctrl_write(
3746 SIGNALYZER_DATA_BUFFER_ADDR
,
3747 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3750 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3752 return ERROR_JTAG_DEVICE_ERROR
;
3755 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3756 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3758 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3760 return ERROR_JTAG_DEVICE_ERROR
;
3763 /* set gpio mode register */
3764 if ((status
= signalyzer_h_ctrl_write(
3765 SIGNALYZER_DATA_BUFFER_ADDR
,
3766 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3768 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3770 return ERROR_JTAG_DEVICE_ERROR
;
3773 if ((status
= signalyzer_h_ctrl_write(
3774 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3777 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3779 return ERROR_JTAG_DEVICE_ERROR
;
3782 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3783 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3785 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3787 return ERROR_JTAG_DEVICE_ERROR
;
3790 /* set gpio register */
3791 if ((status
= signalyzer_h_ctrl_write(
3792 SIGNALYZER_DATA_BUFFER_ADDR
,
3793 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3795 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3797 return ERROR_JTAG_DEVICE_ERROR
;
3800 if ((status
= signalyzer_h_ctrl_write(
3801 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3804 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3806 return ERROR_JTAG_DEVICE_ERROR
;
3809 if ((status
= signalyzer_h_ctrl_write(
3810 SIGNALYZER_COMMAND_ADDR
,
3811 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3813 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3815 return ERROR_JTAG_DEVICE_ERROR
;
3820 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3821 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3822 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3823 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3824 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3826 if (signalyzer_h_adapter_type
3827 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3828 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3829 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3830 else if (signalyzer_h_adapter_type
3831 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3832 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3833 "(ARM JTAG with PSU) detected. (HW: %2x).",
3834 (read_buf
[1] >> 8));
3835 else if (signalyzer_h_adapter_type
3836 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3837 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3838 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3839 else if (signalyzer_h_adapter_type
3840 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3841 LOG_INFO("Signalyzer: EM-JTAG-P "
3842 "(Generic JTAG with PSU) detected. (HW: %2x).",
3843 (read_buf
[1] >> 8));
3851 low_direction
= 0x1b;
3854 high_direction
= 0x1f;
3856 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3858 high_output
|= nTRSTnOE
;
3859 high_output
&= ~nTRST
;
3863 high_output
&= ~nTRSTnOE
;
3864 high_output
|= nTRST
;
3867 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3869 high_output
&= ~nSRSTnOE
;
3870 high_output
|= nSRST
;
3874 high_output
|= nSRSTnOE
;
3875 high_output
&= ~nSRST
;
3878 #if BUILD_FT2232_FTD2XX == 1
3879 /* enable power to the module */
3880 if ((status
= signalyzer_h_ctrl_write(
3881 SIGNALYZER_DATA_BUFFER_ADDR
,
3882 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3885 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3887 return ERROR_JTAG_DEVICE_ERROR
;
3890 if ((status
= signalyzer_h_ctrl_write(
3891 SIGNALYZER_COMMAND_ADDR
,
3892 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3894 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3896 return ERROR_JTAG_DEVICE_ERROR
;
3899 /* set gpio mode register (IO_16 and IO_17 set as analog
3900 * inputs, other is gpio)
3902 if ((status
= signalyzer_h_ctrl_write(
3903 SIGNALYZER_DATA_BUFFER_ADDR
,
3904 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3906 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3908 return ERROR_JTAG_DEVICE_ERROR
;
3911 if ((status
= signalyzer_h_ctrl_write(
3912 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3915 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3917 return ERROR_JTAG_DEVICE_ERROR
;
3920 if ((status
= signalyzer_h_ctrl_write(
3921 SIGNALYZER_COMMAND_ADDR
,
3922 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3924 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3926 return ERROR_JTAG_DEVICE_ERROR
;
3929 /* set gpio register (all inputs, for -P modules,
3930 * PSU will be turned off)
3932 if ((status
= signalyzer_h_ctrl_write(
3933 SIGNALYZER_DATA_BUFFER_ADDR
,
3934 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3936 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3938 return ERROR_JTAG_DEVICE_ERROR
;
3941 if ((status
= signalyzer_h_ctrl_write(
3942 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3945 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3947 return ERROR_JTAG_DEVICE_ERROR
;
3950 if ((status
= signalyzer_h_ctrl_write(
3951 SIGNALYZER_COMMAND_ADDR
,
3952 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3954 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3956 return ERROR_JTAG_DEVICE_ERROR
;
3961 else if (signalyzer_h_adapter_type
== 0x0000)
3963 LOG_INFO("Signalyzer: No external modules were detected.");
3971 low_direction
= 0x1b;
3974 high_direction
= 0x0;
3976 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3978 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3979 low_output
&= ~nTRST
; /* nTRST = 0 */
3983 low_direction
|= nTRSTnOE
; /* nTRST output */
3984 low_output
|= nTRST
; /* nTRST = 1 */
3987 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3989 low_direction
|= nSRSTnOE
; /* nSRST output */
3990 low_output
|= nSRST
; /* nSRST = 1 */
3994 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3995 low_output
&= ~nSRST
; /* nSRST = 0 */
4000 LOG_ERROR("Unknown module type is detected: %.4x",
4001 signalyzer_h_adapter_type
);
4002 return ERROR_JTAG_DEVICE_ERROR
;
4005 /* initialize low byte of controller for jtag operation */
4007 buf
[1] = low_output
;
4008 buf
[2] = low_direction
;
4010 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4012 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4013 return ERROR_JTAG_INIT_FAILED
;
4016 #if BUILD_FT2232_FTD2XX == 1
4017 if (ftdi_device
== FT_DEVICE_2232H
)
4019 /* initialize high byte of controller for jtag operation */
4021 buf
[1] = high_output
;
4022 buf
[2] = high_direction
;
4024 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4026 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4027 return ERROR_JTAG_INIT_FAILED
;
4030 #elif BUILD_FT2232_LIBFTDI == 1
4031 if (ftdi_device
== TYPE_2232H
)
4033 /* initialize high byte of controller for jtag operation */
4035 buf
[1] = high_output
;
4036 buf
[2] = high_direction
;
4038 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4040 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4041 return ERROR_JTAG_INIT_FAILED
;
4048 static void signalyzer_h_reset(int trst
, int srst
)
4050 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4052 /* ADAPTOR: EM_LT16_A */
4053 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4057 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4058 /* switch to output pin (output is low) */
4059 low_direction
|= nTRSTnOE
;
4061 /* switch output low */
4062 low_output
&= ~nTRST
;
4066 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4067 /* switch to input pin (high-Z + internal
4068 * and external pullup) */
4069 low_direction
&= ~nTRSTnOE
;
4071 /* switch output high */
4072 low_output
|= nTRST
;
4077 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4078 /* switch output low */
4079 low_output
&= ~nSRST
;
4081 /* switch to output pin (output is low) */
4082 low_direction
|= nSRSTnOE
;
4086 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4087 /* switch output high */
4088 low_output
|= nSRST
;
4090 /* switch to input pin (high-Z) */
4091 low_direction
&= ~nSRSTnOE
;
4094 /* command "set data bits low byte" */
4096 buffer_write(low_output
);
4097 buffer_write(low_direction
);
4098 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4099 "low_direction: 0x%2.2x",
4100 trst
, srst
, low_output
, low_direction
);
4102 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4103 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4104 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4105 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4106 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4110 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4111 high_output
&= ~nTRSTnOE
;
4113 high_output
&= ~nTRST
;
4117 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4118 high_output
|= nTRSTnOE
;
4120 high_output
|= nTRST
;
4125 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4126 high_output
&= ~nSRST
;
4128 high_output
&= ~nSRSTnOE
;
4132 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4133 high_output
|= nSRST
;
4135 high_output
|= nSRSTnOE
;
4138 /* command "set data bits high byte" */
4140 buffer_write(high_output
);
4141 buffer_write(high_direction
);
4142 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4143 "high_direction: 0x%2.2x",
4144 trst
, srst
, high_output
, high_direction
);
4146 else if (signalyzer_h_adapter_type
== 0x0000)
4150 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4151 /* switch to output pin (output is low) */
4152 low_direction
|= nTRSTnOE
;
4154 /* switch output low */
4155 low_output
&= ~nTRST
;
4159 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4160 /* switch to input pin (high-Z + internal
4161 * and external pullup) */
4162 low_direction
&= ~nTRSTnOE
;
4164 /* switch output high */
4165 low_output
|= nTRST
;
4170 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4171 /* switch output low */
4172 low_output
&= ~nSRST
;
4174 /* switch to output pin (output is low) */
4175 low_direction
|= nSRSTnOE
;
4179 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4180 /* switch output high */
4181 low_output
|= nSRST
;
4183 /* switch to input pin (high-Z) */
4184 low_direction
&= ~nSRSTnOE
;
4187 /* command "set data bits low byte" */
4189 buffer_write(low_output
);
4190 buffer_write(low_direction
);
4191 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4192 "low_direction: 0x%2.2x",
4193 trst
, srst
, low_output
, low_direction
);
4197 static void signalyzer_h_blink(void)
4199 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4202 /********************************************************************
4203 * Support for KT-LINK
4204 * JTAG adapter from KRISTECH
4205 * http://www.kristech.eu
4206 *******************************************************************/
4207 static int ktlink_init(void)
4210 uint32_t bytes_written
;
4211 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4213 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4214 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4216 // initialize low port
4217 buf
[0] = 0x80; // command "set data bits low byte"
4218 buf
[1] = low_output
;
4219 buf
[2] = low_direction
;
4220 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4222 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4224 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4225 return ERROR_JTAG_INIT_FAILED
;
4233 high_output
= 0x80; // turn LED on
4234 high_direction
= 0xFF; // all outputs
4236 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4238 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4239 high_output
|= nTRSTnOE
;
4240 high_output
&= ~nTRST
;
4242 high_output
&= ~nTRSTnOE
;
4243 high_output
|= nTRST
;
4246 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4247 high_output
&= ~nSRSTnOE
;
4248 high_output
|= nSRST
;
4250 high_output
|= nSRSTnOE
;
4251 high_output
&= ~nSRST
;
4254 // initialize high port
4255 buf
[0] = 0x82; // command "set data bits high byte"
4256 buf
[1] = high_output
; // value
4257 buf
[2] = high_direction
;
4258 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4260 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4262 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4263 return ERROR_JTAG_INIT_FAILED
;
4269 static void ktlink_reset(int trst
, int srst
)
4271 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4274 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4275 high_output
&= ~nTRSTnOE
;
4277 high_output
&= ~nTRST
;
4278 } else if (trst
== 0) {
4279 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4280 high_output
|= nTRSTnOE
;
4282 high_output
|= nTRST
;
4286 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4287 high_output
&= ~nSRST
;
4289 high_output
&= ~nSRSTnOE
;
4290 } else if (srst
== 0) {
4291 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4292 high_output
|= nSRST
;
4294 high_output
|= nSRSTnOE
;
4297 buffer_write(0x82); // command "set data bits high byte"
4298 buffer_write(high_output
);
4299 buffer_write(high_direction
);
4300 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4303 static void ktlink_blink(void)
4305 /* LED connected to ACBUS7 */
4306 if (high_output
& 0x80)
4307 high_output
&= 0x7F;
4309 high_output
|= 0x80;
4311 buffer_write(0x82); // command "set data bits high byte"
4312 buffer_write(high_output
);
4313 buffer_write(high_direction
);
4316 static const struct command_registration ft2232_command_handlers
[] = {
4318 .name
= "ft2232_device_desc",
4319 .handler
= &ft2232_handle_device_desc_command
,
4320 .mode
= COMMAND_CONFIG
,
4321 .help
= "set the USB device description of the FTDI FT2232 device",
4322 .usage
= "description_string",
4325 .name
= "ft2232_serial",
4326 .handler
= &ft2232_handle_serial_command
,
4327 .mode
= COMMAND_CONFIG
,
4328 .help
= "set the serial number of the FTDI FT2232 device",
4329 .usage
= "serial_string",
4332 .name
= "ft2232_layout",
4333 .handler
= &ft2232_handle_layout_command
,
4334 .mode
= COMMAND_CONFIG
,
4335 .help
= "set the layout of the FT2232 GPIO signals used "
4336 "to control output-enables and reset signals",
4337 .usage
= "layout_name",
4340 .name
= "ft2232_vid_pid",
4341 .handler
= &ft2232_handle_vid_pid_command
,
4342 .mode
= COMMAND_CONFIG
,
4343 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4344 .usage
= "(vid pid)* ",
4347 .name
= "ft2232_latency",
4348 .handler
= &ft2232_handle_latency_command
,
4349 .mode
= COMMAND_CONFIG
,
4350 .help
= "set the FT2232 latency timer to a new value",
4353 COMMAND_REGISTRATION_DONE
4356 struct jtag_interface ft2232_interface
= {
4358 .supported
= DEBUG_CAP_TMS_SEQ
,
4359 .commands
= ft2232_command_handlers
,
4361 .init
= ft2232_init
,
4362 .quit
= ft2232_quit
,
4363 .speed
= ft2232_speed
,
4364 .speed_div
= ft2232_speed_div
,
4366 .execute_queue
= ft2232_execute_queue
,