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
/ 8;
718 int bits_left
= scan_size
% 8;
721 for (cur_byte
= 0; cur_byte
< num_bytes
; cur_byte
++)
723 buffer
[cur_byte
] = buffer_read();
726 /* Manage partial byte left from the clock data in/out instructions, if any */
729 buffer
[cur_byte
] = buffer_read() >> 1;
733 buffer
[cur_byte
] = 0x0;
735 /* 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 */
736 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
739 static void ft2232_debug_dump_buffer(void)
745 for (i
= 0; i
< ft2232_buffer_size
; i
++)
747 line_p
+= snprintf(line_p
, sizeof(line
) - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
750 LOG_DEBUG("%s", line
);
756 LOG_DEBUG("%s", line
);
759 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
761 struct jtag_command
* cmd
;
766 uint32_t bytes_written
= 0;
767 uint32_t bytes_read
= 0;
769 #ifdef _DEBUG_USB_IO_
770 struct timeval start
, inter
, inter2
, end
;
771 struct timeval d_inter
, d_inter2
, d_end
;
774 #ifdef _DEBUG_USB_COMMS_
775 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
776 ft2232_debug_dump_buffer();
779 #ifdef _DEBUG_USB_IO_
780 gettimeofday(&start
, NULL
);
783 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
785 LOG_ERROR("couldn't write MPSSE commands to FT2232");
789 #ifdef _DEBUG_USB_IO_
790 gettimeofday(&inter
, NULL
);
793 if (ft2232_expect_read
)
795 /* FIXME this "timeout" is never changed ... */
796 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
797 ft2232_buffer_size
= 0;
799 #ifdef _DEBUG_USB_IO_
800 gettimeofday(&inter2
, NULL
);
803 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
805 LOG_ERROR("couldn't read from FT2232");
809 #ifdef _DEBUG_USB_IO_
810 gettimeofday(&end
, NULL
);
812 timeval_subtract(&d_inter
, &inter
, &start
);
813 timeval_subtract(&d_inter2
, &inter2
, &start
);
814 timeval_subtract(&d_end
, &end
, &start
);
816 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
817 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
818 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
819 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
822 ft2232_buffer_size
= bytes_read
;
824 if (ft2232_expect_read
!= ft2232_buffer_size
)
826 LOG_ERROR("ft2232_expect_read (%i) != "
827 "ft2232_buffer_size (%i) "
831 LIBFTDI_READ_RETRY_COUNT
- timeout
);
832 ft2232_debug_dump_buffer();
837 #ifdef _DEBUG_USB_COMMS_
838 LOG_DEBUG("read buffer (%i retries): %i bytes",
839 LIBFTDI_READ_RETRY_COUNT
- timeout
,
841 ft2232_debug_dump_buffer();
845 ft2232_expect_read
= 0;
846 ft2232_read_pointer
= 0;
848 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
849 * that wasn't handled by a caller-provided error handler
859 type
= jtag_scan_type(cmd
->cmd
.scan
);
860 if (type
!= SCAN_OUT
)
862 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
863 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
864 ft2232_read_scan(type
, buffer
, scan_size
);
865 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
866 retval
= ERROR_JTAG_QUEUE_FAILED
;
878 ft2232_buffer_size
= 0;
884 * Function ft2232_add_pathmove
885 * moves the TAP controller from the current state to a new state through the
886 * given path, where path is an array of tap_state_t's.
888 * @param path is an array of tap_stat_t which gives the states to traverse through
889 * ending with the last state at path[num_states-1]
890 * @param num_states is the count of state steps to move through
892 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
896 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
900 /* this loop verifies that the path is legal and logs each state in the path */
903 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
905 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
907 /* command "Clock Data to TMS/CS Pin (no Read)" */
910 /* number of states remaining */
911 buffer_write(num_states_batch
- 1);
913 while (num_states_batch
--) {
914 /* either TMS=0 or TMS=1 must work ... */
915 if (tap_state_transition(tap_get_state(), false)
916 == path
[state_count
])
917 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
918 else if (tap_state_transition(tap_get_state(), true)
919 == path
[state_count
])
920 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
922 /* ... or else the caller goofed BADLY */
924 LOG_ERROR("BUG: %s -> %s isn't a valid "
925 "TAP state transition",
926 tap_state_name(tap_get_state()),
927 tap_state_name(path
[state_count
]));
931 tap_set_state(path
[state_count
]);
936 buffer_write(tms_byte
);
938 tap_set_end_state(tap_get_state());
941 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
943 int num_bytes
= (scan_size
+ 7) / 8;
944 int bits_left
= scan_size
;
950 if (tap_get_state() != TAP_DRSHIFT
)
952 move_to_state(TAP_DRSHIFT
);
957 if (tap_get_state() != TAP_IRSHIFT
)
959 move_to_state(TAP_IRSHIFT
);
963 /* add command for complete bytes */
964 while (num_bytes
> 1)
969 /* Clock Data Bytes In and Out LSB First */
971 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
973 else if (type
== SCAN_OUT
)
975 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
977 /* LOG_DEBUG("added TDI bytes (o)"); */
979 else if (type
== SCAN_IN
)
981 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
983 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
986 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
987 num_bytes
-= thisrun_bytes
;
989 buffer_write((uint8_t) (thisrun_bytes
- 1));
990 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
994 /* add complete bytes */
995 while (thisrun_bytes
-- > 0)
997 buffer_write(buffer
[cur_byte
++]);
1001 else /* (type == SCAN_IN) */
1003 bits_left
-= 8 * (thisrun_bytes
);
1007 /* the most signifcant bit is scanned during TAP movement */
1008 if (type
!= SCAN_IN
)
1009 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1013 /* process remaining bits but the last one */
1016 if (type
== SCAN_IO
)
1018 /* Clock Data Bits In and Out LSB First */
1020 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1022 else if (type
== SCAN_OUT
)
1024 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1026 /* LOG_DEBUG("added TDI bits (o)"); */
1028 else if (type
== SCAN_IN
)
1030 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1032 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1035 buffer_write(bits_left
- 2);
1036 if (type
!= SCAN_IN
)
1037 buffer_write(buffer
[cur_byte
]);
1040 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1041 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1043 if (type
== SCAN_IO
)
1045 /* Clock Data Bits In and Out LSB First */
1047 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1049 else if (type
== SCAN_OUT
)
1051 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1053 /* LOG_DEBUG("added TDI bits (o)"); */
1055 else if (type
== SCAN_IN
)
1057 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1059 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1062 buffer_write(last_bit
);
1070 /* move from Shift-IR/DR to end state */
1071 if (type
!= SCAN_OUT
)
1073 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1074 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1077 /* Clock Data to TMS/CS Pin with Read */
1082 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1083 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1084 /* Clock Data to TMS/CS Pin (no Read) */
1088 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1089 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1092 if (tap_get_state() != tap_get_end_state())
1094 move_to_state(tap_get_end_state());
1098 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1100 int num_bytes
= (scan_size
+ 7) / 8;
1101 int bits_left
= scan_size
;
1104 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1105 uint8_t* receive_pointer
= receive_buffer
;
1106 uint32_t bytes_written
;
1107 uint32_t bytes_read
;
1109 int thisrun_read
= 0;
1113 LOG_ERROR("BUG: large IR scans are not supported");
1117 if (tap_get_state() != TAP_DRSHIFT
)
1119 move_to_state(TAP_DRSHIFT
);
1122 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1124 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1127 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1128 ft2232_buffer_size
, (int)bytes_written
);
1129 ft2232_buffer_size
= 0;
1131 /* add command for complete bytes */
1132 while (num_bytes
> 1)
1136 if (type
== SCAN_IO
)
1138 /* Clock Data Bytes In and Out LSB First */
1140 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1142 else if (type
== SCAN_OUT
)
1144 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1146 /* LOG_DEBUG("added TDI bytes (o)"); */
1148 else if (type
== SCAN_IN
)
1150 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1152 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1155 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1156 thisrun_read
= thisrun_bytes
;
1157 num_bytes
-= thisrun_bytes
;
1158 buffer_write((uint8_t) (thisrun_bytes
- 1));
1159 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1161 if (type
!= SCAN_IN
)
1163 /* add complete bytes */
1164 while (thisrun_bytes
-- > 0)
1166 buffer_write(buffer
[cur_byte
]);
1171 else /* (type == SCAN_IN) */
1173 bits_left
-= 8 * (thisrun_bytes
);
1176 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1178 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1181 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1183 (int)bytes_written
);
1184 ft2232_buffer_size
= 0;
1186 if (type
!= SCAN_OUT
)
1188 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1190 LOG_ERROR("couldn't read from FT2232");
1193 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1196 receive_pointer
+= bytes_read
;
1202 /* the most signifcant bit is scanned during TAP movement */
1203 if (type
!= SCAN_IN
)
1204 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1208 /* process remaining bits but the last one */
1211 if (type
== SCAN_IO
)
1213 /* Clock Data Bits In and Out LSB First */
1215 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1217 else if (type
== SCAN_OUT
)
1219 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1221 /* LOG_DEBUG("added TDI bits (o)"); */
1223 else if (type
== SCAN_IN
)
1225 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1227 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1229 buffer_write(bits_left
- 2);
1230 if (type
!= SCAN_IN
)
1231 buffer_write(buffer
[cur_byte
]);
1233 if (type
!= SCAN_OUT
)
1237 if (tap_get_end_state() == TAP_DRSHIFT
)
1239 if (type
== SCAN_IO
)
1241 /* Clock Data Bits In and Out LSB First */
1243 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1245 else if (type
== SCAN_OUT
)
1247 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1249 /* LOG_DEBUG("added TDI bits (o)"); */
1251 else if (type
== SCAN_IN
)
1253 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1255 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1258 buffer_write(last_bit
);
1262 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1263 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1266 /* move from Shift-IR/DR to end state */
1267 if (type
!= SCAN_OUT
)
1269 /* Clock Data to TMS/CS Pin with Read */
1271 /* LOG_DEBUG("added TMS scan (read)"); */
1275 /* Clock Data to TMS/CS Pin (no Read) */
1277 /* LOG_DEBUG("added TMS scan (no read)"); */
1280 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1281 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1284 if (type
!= SCAN_OUT
)
1287 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1289 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1292 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1294 (int)bytes_written
);
1295 ft2232_buffer_size
= 0;
1297 if (type
!= SCAN_OUT
)
1299 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1301 LOG_ERROR("couldn't read from FT2232");
1304 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1307 receive_pointer
+= bytes_read
;
1313 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1315 int predicted_size
= 3;
1316 int num_bytes
= (scan_size
- 1) / 8;
1318 if (tap_get_state() != TAP_DRSHIFT
)
1319 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1321 if (type
== SCAN_IN
) /* only from device to host */
1323 /* complete bytes */
1324 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1326 /* remaining bits - 1 (up to 7) */
1327 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1329 else /* host to device, or bidirectional */
1331 /* complete bytes */
1332 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1334 /* remaining bits -1 (up to 7) */
1335 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1338 return predicted_size
;
1341 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1343 int predicted_size
= 0;
1345 if (type
!= SCAN_OUT
)
1347 /* complete bytes */
1348 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1350 /* remaining bits - 1 */
1351 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1353 /* last bit (from TMS scan) */
1354 predicted_size
+= 1;
1357 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1359 return predicted_size
;
1362 /* semi-generic FT2232/FT4232 reset code */
1363 static void ftx23_reset(int trst
, int srst
)
1365 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1368 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1369 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1371 low_output
&= ~nTRST
; /* switch output low */
1375 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1376 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1378 low_output
|= nTRST
; /* switch output high */
1383 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1384 low_output
&= ~nSRST
; /* switch output low */
1386 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1390 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1391 low_output
|= nSRST
; /* switch output high */
1393 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1396 /* command "set data bits low byte" */
1398 buffer_write(low_output
);
1399 buffer_write(low_direction
);
1402 static void jtagkey_reset(int trst
, int srst
)
1404 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1407 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1408 high_output
&= ~nTRSTnOE
;
1410 high_output
&= ~nTRST
;
1414 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1415 high_output
|= nTRSTnOE
;
1417 high_output
|= nTRST
;
1422 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1423 high_output
&= ~nSRST
;
1425 high_output
&= ~nSRSTnOE
;
1429 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1430 high_output
|= nSRST
;
1432 high_output
|= nSRSTnOE
;
1435 /* command "set data bits high byte" */
1437 buffer_write(high_output
);
1438 buffer_write(high_direction
);
1439 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1443 static void olimex_jtag_reset(int trst
, int srst
)
1445 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1448 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1449 high_output
&= ~nTRSTnOE
;
1451 high_output
&= ~nTRST
;
1455 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1456 high_output
|= nTRSTnOE
;
1458 high_output
|= nTRST
;
1463 high_output
|= nSRST
;
1467 high_output
&= ~nSRST
;
1470 /* command "set data bits high byte" */
1472 buffer_write(high_output
);
1473 buffer_write(high_direction
);
1474 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1478 static void axm0432_jtag_reset(int trst
, int srst
)
1482 tap_set_state(TAP_RESET
);
1483 high_output
&= ~nTRST
;
1487 high_output
|= nTRST
;
1492 high_output
&= ~nSRST
;
1496 high_output
|= nSRST
;
1499 /* command "set data bits low byte" */
1501 buffer_write(high_output
);
1502 buffer_write(high_direction
);
1503 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1507 static void flyswatter_reset(int trst
, int srst
)
1511 low_output
&= ~nTRST
;
1515 low_output
|= nTRST
;
1520 low_output
|= nSRST
;
1524 low_output
&= ~nSRST
;
1527 /* command "set data bits low byte" */
1529 buffer_write(low_output
);
1530 buffer_write(low_direction
);
1531 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1534 static void turtle_reset(int trst
, int srst
)
1540 low_output
|= nSRST
;
1544 low_output
&= ~nSRST
;
1547 /* command "set data bits low byte" */
1549 buffer_write(low_output
);
1550 buffer_write(low_direction
);
1551 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1554 static void comstick_reset(int trst
, int srst
)
1558 high_output
&= ~nTRST
;
1562 high_output
|= nTRST
;
1567 high_output
&= ~nSRST
;
1571 high_output
|= nSRST
;
1574 /* command "set data bits high byte" */
1576 buffer_write(high_output
);
1577 buffer_write(high_direction
);
1578 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1582 static void stm32stick_reset(int trst
, int srst
)
1586 high_output
&= ~nTRST
;
1590 high_output
|= nTRST
;
1595 low_output
&= ~nSRST
;
1599 low_output
|= nSRST
;
1602 /* command "set data bits low byte" */
1604 buffer_write(low_output
);
1605 buffer_write(low_direction
);
1607 /* command "set data bits high byte" */
1609 buffer_write(high_output
);
1610 buffer_write(high_direction
);
1611 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1615 static void sheevaplug_reset(int trst
, int srst
)
1618 high_output
&= ~nTRST
;
1620 high_output
|= nTRST
;
1623 high_output
&= ~nSRSTnOE
;
1625 high_output
|= nSRSTnOE
;
1627 /* command "set data bits high byte" */
1629 buffer_write(high_output
);
1630 buffer_write(high_direction
);
1631 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1634 static void redbee_reset(int trst
, int srst
)
1638 tap_set_state(TAP_RESET
);
1639 high_output
&= ~nTRST
;
1643 high_output
|= nTRST
;
1648 high_output
&= ~nSRST
;
1652 high_output
|= nSRST
;
1655 /* command "set data bits low byte" */
1657 buffer_write(high_output
);
1658 buffer_write(high_direction
);
1659 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1660 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1664 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1668 int predicted_size
= 0;
1671 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1672 cmd
->cmd
.runtest
->num_cycles
,
1673 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1675 /* only send the maximum buffer size that FT2232C can handle */
1677 if (tap_get_state() != TAP_IDLE
)
1678 predicted_size
+= 3;
1679 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1680 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1681 predicted_size
+= 3;
1682 if (tap_get_end_state() != TAP_IDLE
)
1683 predicted_size
+= 3;
1684 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1686 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1687 retval
= ERROR_JTAG_QUEUE_FAILED
;
1691 if (tap_get_state() != TAP_IDLE
)
1693 move_to_state(TAP_IDLE
);
1696 i
= cmd
->cmd
.runtest
->num_cycles
;
1699 /* there are no state transitions in this code, so omit state tracking */
1701 /* command "Clock Data to TMS/CS Pin (no Read)" */
1705 buffer_write((i
> 7) ? 6 : (i
- 1));
1710 i
-= (i
> 7) ? 7 : i
;
1711 /* LOG_DEBUG("added TMS scan (no read)"); */
1714 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1716 if (tap_get_state() != tap_get_end_state())
1718 move_to_state(tap_get_end_state());
1722 DEBUG_JTAG_IO("runtest: %i, end in %s",
1723 cmd
->cmd
.runtest
->num_cycles
,
1724 tap_state_name(tap_get_end_state()));
1728 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1730 int predicted_size
= 0;
1731 int retval
= ERROR_OK
;
1733 DEBUG_JTAG_IO("statemove end in %s",
1734 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1736 /* only send the maximum buffer size that FT2232C can handle */
1738 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1740 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1741 retval
= ERROR_JTAG_QUEUE_FAILED
;
1745 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1747 /* For TAP_RESET, ignore the current recorded state. It's often
1748 * wrong at server startup, and this transation is critical whenever
1751 if (tap_get_end_state() == TAP_RESET
) {
1752 clock_tms(0x4b, 0xff, 5, 0);
1755 /* shortest-path move to desired end state */
1756 } else if (tap_get_state() != tap_get_end_state())
1758 move_to_state(tap_get_end_state());
1766 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1767 * (or SWD) state machine.
1769 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1771 int retval
= ERROR_OK
;
1772 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1773 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1776 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1778 /* only send the maximum buffer size that FT2232C can handle */
1779 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1780 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1781 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1782 retval
= ERROR_JTAG_QUEUE_FAILED
;
1788 /* Shift out in batches of at most 6 bits; there's a report of an
1789 * FT2232 bug in this area, where shifting exactly 7 bits can make
1790 * problems with TMS signaling for the last clock cycle:
1792 * http://developer.intra2net.com/mailarchive/html/
1793 * libftdi/2009/msg00292.html
1795 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1797 * Note that pathmoves in JTAG are not often seven bits, so that
1798 * isn't a particularly likely situation outside of "special"
1799 * signaling such as switching between JTAG and SWD modes.
1802 if (num_bits
<= 6) {
1804 buffer_write(num_bits
- 1);
1805 buffer_write(*bits
& 0x3f);
1809 /* Yes, this is lazy ... we COULD shift out more data
1810 * bits per operation, but doing it in nybbles is easy
1814 buffer_write(*bits
& 0xf);
1817 count
= (num_bits
> 4) ? 4 : num_bits
;
1820 buffer_write(count
- 1);
1821 buffer_write((*bits
>> 4) & 0xf);
1831 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1833 int predicted_size
= 0;
1834 int retval
= ERROR_OK
;
1836 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1837 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1839 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1840 tap_state_name(tap_get_state()),
1841 tap_state_name(path
[num_states
-1]));
1843 /* only send the maximum buffer size that FT2232C can handle */
1844 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1845 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1847 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1848 retval
= ERROR_JTAG_QUEUE_FAILED
;
1854 ft2232_add_pathmove(path
, num_states
);
1860 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1863 int scan_size
; /* size of IR or DR scan */
1864 int predicted_size
= 0;
1865 int retval
= ERROR_OK
;
1867 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1869 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1871 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1873 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1874 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1876 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1877 /* unsent commands before this */
1878 if (first_unsent
!= cmd
)
1879 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1880 retval
= ERROR_JTAG_QUEUE_FAILED
;
1882 /* current command */
1883 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1884 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1886 first_unsent
= cmd
->next
;
1891 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1893 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1896 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1897 retval
= ERROR_JTAG_QUEUE_FAILED
;
1901 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1902 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1903 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1904 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1908 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1909 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1910 tap_state_name(tap_get_end_state()));
1915 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1918 int predicted_size
= 0;
1921 DEBUG_JTAG_IO("reset trst: %i srst %i",
1922 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1924 /* only send the maximum buffer size that FT2232C can handle */
1926 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1928 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1929 retval
= ERROR_JTAG_QUEUE_FAILED
;
1934 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1936 tap_set_state(TAP_RESET
);
1939 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1942 DEBUG_JTAG_IO("trst: %i, srst: %i",
1943 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1947 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1952 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1954 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1955 retval
= ERROR_JTAG_QUEUE_FAILED
;
1956 first_unsent
= cmd
->next
;
1957 jtag_sleep(cmd
->cmd
.sleep
->us
);
1958 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1960 tap_state_name(tap_get_state()));
1964 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1969 /* this is only allowed while in a stable state. A check for a stable
1970 * state was done in jtag_add_clocks()
1972 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1973 retval
= ERROR_JTAG_QUEUE_FAILED
;
1974 DEBUG_JTAG_IO("clocks %i while in %s",
1975 cmd
->cmd
.stableclocks
->num_cycles
,
1976 tap_state_name(tap_get_state()));
1980 static int ft2232_execute_command(struct jtag_command
*cmd
)
1986 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1987 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1988 case JTAG_TLR_RESET
: retval
= ft2232_execute_statemove(cmd
); break;
1989 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1990 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1991 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1992 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1994 retval
= ft2232_execute_tms(cmd
);
1997 LOG_ERROR("BUG: unknown JTAG command type encountered");
1998 retval
= ERROR_JTAG_QUEUE_FAILED
;
2004 static int ft2232_execute_queue(void)
2006 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
2009 first_unsent
= cmd
; /* next command that has to be sent */
2012 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2013 * that wasn't handled by a caller-provided error handler
2017 ft2232_buffer_size
= 0;
2018 ft2232_expect_read
= 0;
2020 /* blink, if the current layout has that feature */
2026 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2027 retval
= ERROR_JTAG_QUEUE_FAILED
;
2028 /* Start reading input before FT2232 TX buffer fills up */
2030 if (ft2232_expect_read
> 256)
2032 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2033 retval
= ERROR_JTAG_QUEUE_FAILED
;
2038 if (require_send
> 0)
2039 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2040 retval
= ERROR_JTAG_QUEUE_FAILED
;
2045 #if BUILD_FT2232_FTD2XX == 1
2046 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2050 char SerialNumber
[16];
2051 char Description
[64];
2052 DWORD openex_flags
= 0;
2053 char* openex_string
= NULL
;
2054 uint8_t latency_timer
;
2056 if (layout
== NULL
) {
2057 LOG_WARNING("No ft2232 layout specified'");
2058 return ERROR_JTAG_INIT_FAILED
;
2061 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2064 /* Add non-standard Vid/Pid to the linux driver */
2065 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2067 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2071 if (ft2232_device_desc
&& ft2232_serial
)
2073 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2074 ft2232_device_desc
= NULL
;
2077 if (ft2232_device_desc
)
2079 openex_string
= ft2232_device_desc
;
2080 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2082 else if (ft2232_serial
)
2084 openex_string
= ft2232_serial
;
2085 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2089 LOG_ERROR("neither device description nor serial number specified");
2090 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2092 return ERROR_JTAG_INIT_FAILED
;
2095 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2096 if (status
!= FT_OK
) {
2097 /* under Win32, the FTD2XX driver appends an "A" to the end
2098 * of the description, if we tried by the desc, then
2099 * try by the alternate "A" description. */
2100 if (openex_string
== ft2232_device_desc
) {
2101 /* Try the alternate method. */
2102 openex_string
= ft2232_device_desc_A
;
2103 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2104 if (status
== FT_OK
) {
2105 /* yea, the "alternate" method worked! */
2107 /* drat, give the user a meaningfull message.
2108 * telling the use we tried *BOTH* methods. */
2109 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2111 ft2232_device_desc_A
);
2116 if (status
!= FT_OK
)
2122 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2124 return ERROR_JTAG_INIT_FAILED
;
2126 LOG_ERROR("unable to open ftdi device: %lu", status
);
2127 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2128 if (status
== FT_OK
)
2130 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2133 for (i
= 0; i
< num_devices
; i
++)
2134 desc_array
[i
] = malloc(64);
2136 desc_array
[num_devices
] = NULL
;
2138 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2140 if (status
== FT_OK
)
2142 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2143 for (i
= 0; i
< num_devices
; i
++)
2144 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2147 for (i
= 0; i
< num_devices
; i
++)
2148 free(desc_array
[i
]);
2154 LOG_ERROR("ListDevices: NONE\n");
2156 return ERROR_JTAG_INIT_FAILED
;
2159 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2161 LOG_ERROR("unable to set latency timer: %lu", status
);
2162 return ERROR_JTAG_INIT_FAILED
;
2165 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2167 LOG_ERROR("unable to get latency timer: %lu", status
);
2168 return ERROR_JTAG_INIT_FAILED
;
2172 LOG_DEBUG("current latency timer: %i", latency_timer
);
2175 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2177 LOG_ERROR("unable to set timeouts: %lu", status
);
2178 return ERROR_JTAG_INIT_FAILED
;
2181 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2183 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2184 return ERROR_JTAG_INIT_FAILED
;
2187 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2189 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2190 return ERROR_JTAG_INIT_FAILED
;
2194 static const char* type_str
[] =
2195 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2196 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2197 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2198 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2199 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2200 LOG_INFO("deviceID: %lu", deviceID
);
2201 LOG_INFO("SerialNumber: %s", SerialNumber
);
2202 LOG_INFO("Description: %s", Description
);
2208 static int ft2232_purge_ftd2xx(void)
2212 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2214 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2215 return ERROR_JTAG_INIT_FAILED
;
2221 #endif /* BUILD_FT2232_FTD2XX == 1 */
2223 #if BUILD_FT2232_LIBFTDI == 1
2224 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2226 uint8_t latency_timer
;
2228 if (layout
== NULL
) {
2229 LOG_WARNING("No ft2232 layout specified'");
2230 return ERROR_JTAG_INIT_FAILED
;
2233 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2234 layout
->name
, vid
, pid
);
2236 if (ftdi_init(&ftdic
) < 0)
2237 return ERROR_JTAG_INIT_FAILED
;
2239 /* default to INTERFACE_A */
2240 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2242 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2244 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2245 return ERROR_JTAG_INIT_FAILED
;
2248 /* context, vendor id, product id */
2249 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2253 LOG_WARNING("unable to open ftdi device (trying more): %s",
2256 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2258 return ERROR_JTAG_INIT_FAILED
;
2261 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2262 if (ftdi_usb_reset(&ftdic
) < 0)
2264 LOG_ERROR("unable to reset ftdi device");
2265 return ERROR_JTAG_INIT_FAILED
;
2268 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2270 LOG_ERROR("unable to set latency timer");
2271 return ERROR_JTAG_INIT_FAILED
;
2274 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2276 LOG_ERROR("unable to get latency timer");
2277 return ERROR_JTAG_INIT_FAILED
;
2281 LOG_DEBUG("current latency timer: %i", latency_timer
);
2284 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2286 ftdi_device
= ftdic
.type
;
2287 static const char* type_str
[] =
2288 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2289 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2290 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2291 ? ftdi_device
: no_of_known_types
;
2292 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2296 static int ft2232_purge_libftdi(void)
2298 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2300 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2301 return ERROR_JTAG_INIT_FAILED
;
2307 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2309 static int ft2232_init(void)
2313 uint32_t bytes_written
;
2315 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2317 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2321 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2324 if (layout
== NULL
) {
2325 LOG_WARNING("No ft2232 layout specified'");
2326 return ERROR_JTAG_INIT_FAILED
;
2329 for (int i
= 0; 1; i
++)
2332 * "more indicates that there are more IDs to try, so we should
2333 * not print an error for an ID mismatch (but for anything
2336 * try_more indicates that the error code returned indicates an
2337 * ID mismatch (and nothing else) and that we should proceeed
2338 * with the next ID pair.
2340 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2343 #if BUILD_FT2232_FTD2XX == 1
2344 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2346 #elif BUILD_FT2232_LIBFTDI == 1
2347 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2348 more
, &try_more
, layout
->channel
);
2352 if (!more
|| !try_more
)
2356 ft2232_buffer_size
= 0;
2357 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2359 if (layout
->init() != ERROR_OK
)
2360 return ERROR_JTAG_INIT_FAILED
;
2362 if (ft2232_device_is_highspeed())
2364 #ifndef BUILD_FT2232_HIGHSPEED
2365 #if BUILD_FT2232_FTD2XX == 1
2366 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2367 #elif BUILD_FT2232_LIBFTDI == 1
2368 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2371 /* make sure the legacy mode is disabled */
2372 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2373 return ERROR_JTAG_INIT_FAILED
;
2376 ft2232_speed(jtag_get_speed());
2378 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2379 if ((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
)
2381 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2382 return ERROR_JTAG_INIT_FAILED
;
2385 #if BUILD_FT2232_FTD2XX == 1
2386 return ft2232_purge_ftd2xx();
2387 #elif BUILD_FT2232_LIBFTDI == 1
2388 return ft2232_purge_libftdi();
2394 /** Updates defaults for DBUS signals: the four JTAG signals
2395 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2397 static inline void ftx232_init_head(void)
2400 low_direction
= 0x0b;
2403 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2404 * the four GPIOL signals. Initialization covers value and direction,
2405 * as customized for each layout.
2407 static int ftx232_init_tail(void)
2410 uint32_t bytes_written
;
2412 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2413 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2415 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2416 low_output
&= ~nTRST
; /* nTRST = 0 */
2420 low_direction
|= nTRSTnOE
; /* nTRST output */
2421 low_output
|= nTRST
; /* nTRST = 1 */
2424 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2426 low_direction
|= nSRSTnOE
; /* nSRST output */
2427 low_output
|= nSRST
; /* nSRST = 1 */
2431 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2432 low_output
&= ~nSRST
; /* nSRST = 0 */
2435 /* initialize low byte for jtag */
2436 buf
[0] = 0x80; /* command "set data bits low byte" */
2437 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2438 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2439 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2441 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2443 LOG_ERROR("couldn't initialize FT2232 DBUS");
2444 return ERROR_JTAG_INIT_FAILED
;
2450 static int usbjtag_init(void)
2453 * NOTE: This is now _specific_ to the "usbjtag" layout.
2454 * Don't try cram any more layouts into this.
2463 return ftx232_init_tail();
2466 static int lm3s811_jtag_init(void)
2470 /* There are multiple revisions of LM3S811 eval boards:
2471 * - Rev B (and older?) boards have no SWO trace support.
2472 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2473 * they should use the "luminary_icdi" layout instead.
2480 low_direction
= 0x8b;
2482 return ftx232_init_tail();
2485 static int icdi_jtag_init(void)
2489 /* Most Luminary eval boards support SWO trace output,
2490 * and should use this "luminary_icdi" layout.
2497 low_direction
= 0xcb;
2499 return ftx232_init_tail();
2502 static int signalyzer_init(void)
2510 return ftx232_init_tail();
2513 static int axm0432_jtag_init(void)
2516 uint32_t bytes_written
;
2519 low_direction
= 0x2b;
2521 /* initialize low byte for jtag */
2522 buf
[0] = 0x80; /* command "set data bits low byte" */
2523 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2524 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2525 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2527 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2529 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2530 return ERROR_JTAG_INIT_FAILED
;
2533 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2536 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2538 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2542 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2547 high_direction
= 0x0c;
2549 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2550 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2552 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2556 high_output
|= nTRST
;
2559 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2561 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2565 high_output
|= nSRST
;
2568 /* initialize high port */
2569 buf
[0] = 0x82; /* command "set data bits high byte" */
2570 buf
[1] = high_output
; /* value */
2571 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2572 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2574 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2576 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2577 return ERROR_JTAG_INIT_FAILED
;
2583 static int redbee_init(void)
2586 uint32_t bytes_written
;
2589 low_direction
= 0x2b;
2591 /* initialize low byte for jtag */
2592 /* command "set data bits low byte" */
2594 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2595 buf
[2] = low_direction
;
2596 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2597 buf
[1] = low_output
;
2598 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2600 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2602 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2603 return ERROR_JTAG_INIT_FAILED
;
2607 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2609 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2612 high_direction
= 0x0c;
2614 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2615 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2617 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2621 high_output
|= nTRST
;
2624 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2626 LOG_ERROR("can't set nSRST to push-pull on redbee");
2630 high_output
|= nSRST
;
2633 /* initialize high port */
2634 buf
[0] = 0x82; /* command "set data bits high byte" */
2635 buf
[1] = high_output
; /* value */
2636 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2637 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2639 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2641 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2642 return ERROR_JTAG_INIT_FAILED
;
2648 static int jtagkey_init(void)
2651 uint32_t bytes_written
;
2654 low_direction
= 0x1b;
2656 /* initialize low byte for jtag */
2657 buf
[0] = 0x80; /* command "set data bits low byte" */
2658 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2659 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2660 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2662 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2664 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2665 return ERROR_JTAG_INIT_FAILED
;
2668 if (strcmp(layout
->name
, "jtagkey") == 0)
2675 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2676 || (strcmp(layout
->name
, "oocdlink") == 0))
2685 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2690 high_direction
= 0x0f;
2692 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2693 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2695 high_output
|= nTRSTnOE
;
2696 high_output
&= ~nTRST
;
2700 high_output
&= ~nTRSTnOE
;
2701 high_output
|= nTRST
;
2704 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2706 high_output
&= ~nSRSTnOE
;
2707 high_output
|= nSRST
;
2711 high_output
|= nSRSTnOE
;
2712 high_output
&= ~nSRST
;
2715 /* initialize high port */
2716 buf
[0] = 0x82; /* command "set data bits high byte" */
2717 buf
[1] = high_output
; /* value */
2718 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2719 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2721 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2723 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2724 return ERROR_JTAG_INIT_FAILED
;
2730 static int olimex_jtag_init(void)
2733 uint32_t bytes_written
;
2736 low_direction
= 0x1b;
2738 /* initialize low byte for jtag */
2739 buf
[0] = 0x80; /* command "set data bits low byte" */
2740 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2741 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2742 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2744 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2746 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2747 return ERROR_JTAG_INIT_FAILED
;
2753 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2756 high_direction
= 0x0f;
2758 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2759 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2761 high_output
|= nTRSTnOE
;
2762 high_output
&= ~nTRST
;
2766 high_output
&= ~nTRSTnOE
;
2767 high_output
|= nTRST
;
2770 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2772 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2776 high_output
&= ~nSRST
;
2779 /* turn red LED on */
2780 high_output
|= 0x08;
2782 /* initialize high port */
2783 buf
[0] = 0x82; /* command "set data bits high byte" */
2784 buf
[1] = high_output
; /* value */
2785 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2786 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2788 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2790 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2791 return ERROR_JTAG_INIT_FAILED
;
2797 static int flyswatter_init(void)
2800 uint32_t bytes_written
;
2803 low_direction
= 0xfb;
2805 /* initialize low byte for jtag */
2806 buf
[0] = 0x80; /* command "set data bits low byte" */
2807 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2808 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2809 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2811 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2813 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2814 return ERROR_JTAG_INIT_FAILED
;
2818 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2820 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2823 high_direction
= 0x0c;
2825 /* turn red LED3 on, LED2 off */
2826 high_output
|= 0x08;
2828 /* initialize high port */
2829 buf
[0] = 0x82; /* command "set data bits high byte" */
2830 buf
[1] = high_output
; /* value */
2831 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2832 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2834 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2836 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2837 return ERROR_JTAG_INIT_FAILED
;
2843 static int turtle_init(void)
2846 uint32_t bytes_written
;
2849 low_direction
= 0x5b;
2851 /* initialize low byte for jtag */
2852 buf
[0] = 0x80; /* command "set data bits low byte" */
2853 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2854 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2855 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2857 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2859 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2860 return ERROR_JTAG_INIT_FAILED
;
2866 high_direction
= 0x0C;
2868 /* initialize high port */
2869 buf
[0] = 0x82; /* command "set data bits high byte" */
2870 buf
[1] = high_output
;
2871 buf
[2] = high_direction
;
2872 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2874 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2876 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2877 return ERROR_JTAG_INIT_FAILED
;
2883 static int comstick_init(void)
2886 uint32_t bytes_written
;
2889 low_direction
= 0x0b;
2891 /* initialize low byte for jtag */
2892 buf
[0] = 0x80; /* command "set data bits low byte" */
2893 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2894 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2895 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2897 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2899 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2900 return ERROR_JTAG_INIT_FAILED
;
2904 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2906 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2909 high_direction
= 0x03;
2911 /* initialize high port */
2912 buf
[0] = 0x82; /* command "set data bits high byte" */
2913 buf
[1] = high_output
;
2914 buf
[2] = high_direction
;
2915 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2917 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2919 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2920 return ERROR_JTAG_INIT_FAILED
;
2926 static int stm32stick_init(void)
2929 uint32_t bytes_written
;
2932 low_direction
= 0x8b;
2934 /* initialize low byte for jtag */
2935 buf
[0] = 0x80; /* command "set data bits low byte" */
2936 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2937 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2938 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2940 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2942 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2943 return ERROR_JTAG_INIT_FAILED
;
2947 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2949 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2952 high_direction
= 0x03;
2954 /* initialize high port */
2955 buf
[0] = 0x82; /* command "set data bits high byte" */
2956 buf
[1] = high_output
;
2957 buf
[2] = high_direction
;
2958 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2960 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2962 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2963 return ERROR_JTAG_INIT_FAILED
;
2969 static int sheevaplug_init(void)
2972 uint32_t bytes_written
;
2975 low_direction
= 0x1b;
2977 /* initialize low byte for jtag */
2978 buf
[0] = 0x80; /* command "set data bits low byte" */
2979 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2980 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2981 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2983 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2985 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2986 return ERROR_JTAG_INIT_FAILED
;
2995 high_direction
= 0x0f;
2997 /* nTRST is always push-pull */
2998 high_output
&= ~nTRSTnOE
;
2999 high_output
|= nTRST
;
3001 /* nSRST is always open-drain */
3002 high_output
|= nSRSTnOE
;
3003 high_output
&= ~nSRST
;
3005 /* initialize high port */
3006 buf
[0] = 0x82; /* command "set data bits high byte" */
3007 buf
[1] = high_output
; /* value */
3008 buf
[2] = high_direction
; /* all outputs - xRST */
3009 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3011 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3013 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3014 return ERROR_JTAG_INIT_FAILED
;
3020 static int cortino_jtag_init(void)
3023 uint32_t bytes_written
;
3026 low_direction
= 0x1b;
3028 /* initialize low byte for jtag */
3029 buf
[0] = 0x80; /* command "set data bits low byte" */
3030 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3031 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3032 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3034 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3036 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3037 return ERROR_JTAG_INIT_FAILED
;
3041 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3043 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3046 high_direction
= 0x03;
3048 /* initialize high port */
3049 buf
[0] = 0x82; /* command "set data bits high byte" */
3050 buf
[1] = high_output
;
3051 buf
[2] = high_direction
;
3052 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3054 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3056 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3057 return ERROR_JTAG_INIT_FAILED
;
3063 static void olimex_jtag_blink(void)
3065 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3066 * ACBUS3 is bit 3 of the GPIOH port
3068 if (high_output
& 0x08)
3070 /* set port pin high */
3071 high_output
&= 0x07;
3075 /* set port pin low */
3076 high_output
|= 0x08;
3080 buffer_write(high_output
);
3081 buffer_write(high_direction
);
3084 static void flyswatter_jtag_blink(void)
3087 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3089 high_output
^= 0x0c;
3092 buffer_write(high_output
);
3093 buffer_write(high_direction
);
3096 static void turtle_jtag_blink(void)
3099 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3101 if (high_output
& 0x08)
3111 buffer_write(high_output
);
3112 buffer_write(high_direction
);
3115 static int ft2232_quit(void)
3117 #if BUILD_FT2232_FTD2XX == 1
3120 status
= FT_Close(ftdih
);
3121 #elif BUILD_FT2232_LIBFTDI == 1
3122 ftdi_usb_close(&ftdic
);
3124 ftdi_deinit(&ftdic
);
3127 free(ft2232_buffer
);
3128 ft2232_buffer
= NULL
;
3133 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3139 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3140 cp
= strchr(ft2232_device_desc
, 0);
3141 /* under Win32, the FTD2XX driver appends an "A" to the end
3142 * of the description, this examines the given desc
3143 * and creates the 'missing' _A or non_A variable. */
3144 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3145 /* it was, so make this the "A" version. */
3146 ft2232_device_desc_A
= ft2232_device_desc
;
3147 /* and *CREATE* the non-A version. */
3148 strcpy(buf
, ft2232_device_desc
);
3149 cp
= strchr(buf
, 0);
3151 ft2232_device_desc
= strdup(buf
);
3153 /* <space > A not defined
3155 sprintf(buf
, "%s A", ft2232_device_desc
);
3156 ft2232_device_desc_A
= strdup(buf
);
3161 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3167 COMMAND_HANDLER(ft2232_handle_serial_command
)
3171 ft2232_serial
= strdup(CMD_ARGV
[0]);
3175 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3181 COMMAND_HANDLER(ft2232_handle_layout_command
)
3183 if (CMD_ARGC
!= 1) {
3184 LOG_ERROR("Need exactly one argument to ft2232_layout");
3189 LOG_ERROR("already specified ft2232_layout %s",
3191 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3196 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3197 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3203 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3207 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3209 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3211 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3212 "(maximum is %d pairs)", MAX_USB_IDS
);
3213 CMD_ARGC
= MAX_USB_IDS
* 2;
3215 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3217 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3219 return ERROR_COMMAND_SYNTAX_ERROR
;
3220 /* remove the incomplete trailing id */
3225 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3227 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3228 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3232 * Explicitly terminate, in case there are multiples instances of
3235 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3240 COMMAND_HANDLER(ft2232_handle_latency_command
)
3244 ft2232_latency
= atoi(CMD_ARGV
[0]);
3248 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3254 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3258 /* 7 bits of either ones or zeros. */
3259 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3261 while (num_cycles
> 0)
3263 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3264 * at most 7 bits per invocation. Here we invoke it potentially
3267 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3269 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3271 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3272 retval
= ERROR_JTAG_QUEUE_FAILED
;
3277 /* there are no state transitions in this code, so omit state tracking */
3279 /* command "Clock Data to TMS/CS Pin (no Read)" */
3283 buffer_write(bitcount_per_command
- 1);
3285 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3290 num_cycles
-= bitcount_per_command
;
3296 /* ---------------------------------------------------------------------
3297 * Support for IceBear JTAG adapter from Section5:
3298 * http://section5.ch/icebear
3300 * Author: Sten, debian@sansys-electronic.com
3303 /* Icebear pin layout
3305 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3306 * GND GND | 4 3| n.c.
3307 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3308 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3309 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3310 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3311 * ADBUS2 TDO |14 13| GND GND
3313 * ADBUS0 O L TCK ACBUS0 GND
3314 * ADBUS1 O L TDI ACBUS1 GND
3315 * ADBUS2 I TDO ACBUS2 n.c.
3316 * ADBUS3 O H TMS ACBUS3 n.c.
3322 static int icebear_jtag_init(void) {
3324 uint32_t bytes_written
;
3326 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3327 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3331 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3332 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3333 low_direction
&= ~nTRST
; /* nTRST high impedance */
3336 low_direction
|= nTRST
;
3337 low_output
|= nTRST
;
3340 low_direction
|= nSRST
;
3341 low_output
|= nSRST
;
3343 /* initialize low byte for jtag */
3344 buf
[0] = 0x80; /* command "set data bits low byte" */
3345 buf
[1] = low_output
;
3346 buf
[2] = low_direction
;
3347 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3349 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3350 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3351 return ERROR_JTAG_INIT_FAILED
;
3355 high_direction
= 0x00;
3358 /* initialize high port */
3359 buf
[0] = 0x82; /* command "set data bits high byte" */
3360 buf
[1] = high_output
; /* value */
3361 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3362 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3364 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3365 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3366 return ERROR_JTAG_INIT_FAILED
;
3372 static void icebear_jtag_reset(int trst
, int srst
) {
3375 low_direction
|= nTRST
;
3376 low_output
&= ~nTRST
;
3378 else if (trst
== 0) {
3379 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3380 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3381 low_direction
&= ~nTRST
;
3383 low_output
|= nTRST
;
3387 low_output
&= ~nSRST
;
3389 else if (srst
== 0) {
3390 low_output
|= nSRST
;
3393 /* command "set data bits low byte" */
3395 buffer_write(low_output
);
3396 buffer_write(low_direction
);
3398 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3401 /* ---------------------------------------------------------------------
3402 * Support for Signalyzer H2 and Signalyzer H4
3403 * JTAG adapter from Xverve Technologies Inc.
3404 * http://www.signalyzer.com or http://www.xverve.com
3406 * Author: Oleg Seiljus, oleg@signalyzer.com
3408 static unsigned char signalyzer_h_side
;
3409 static unsigned int signalyzer_h_adapter_type
;
3411 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3413 #if BUILD_FT2232_FTD2XX == 1
3414 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3417 #define SIGNALYZER_COMMAND_ADDR 128
3418 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3420 #define SIGNALYZER_COMMAND_VERSION 0x41
3421 #define SIGNALYZER_COMMAND_RESET 0x42
3422 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3423 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3424 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3425 #define SIGNALYZER_COMMAND_LED_SET 0x53
3426 #define SIGNALYZER_COMMAND_ADC 0x54
3427 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3428 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3429 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3430 #define SIGNALYZER_COMMAND_I2C 0x58
3432 #define SIGNALYZER_CHAN_A 1
3433 #define SIGNALYZER_CHAN_B 2
3434 /* LEDS use channel C */
3435 #define SIGNALYZER_CHAN_C 4
3437 #define SIGNALYZER_LED_GREEN 1
3438 #define SIGNALYZER_LED_RED 2
3440 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3441 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3442 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3443 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3444 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3447 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3449 #if BUILD_FT2232_FTD2XX == 1
3450 return FT_WriteEE(ftdih
, address
, value
);
3451 #elif BUILD_FT2232_LIBFTDI == 1
3456 #if BUILD_FT2232_FTD2XX == 1
3457 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3459 return FT_ReadEE(ftdih
, address
, value
);
3463 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3464 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3466 unsigned char on_time
;
3467 unsigned char off_time
;
3469 if (on_time_ms
< 0xFFFF)
3470 on_time
= (unsigned char)(on_time_ms
/ 62);
3474 off_time
= (unsigned char)(off_time_ms
/ 62);
3476 #if BUILD_FT2232_FTD2XX == 1
3479 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3480 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3482 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3483 return ERROR_JTAG_DEVICE_ERROR
;
3486 if ((status
= signalyzer_h_ctrl_write(
3487 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3488 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3490 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3491 return ERROR_JTAG_DEVICE_ERROR
;
3494 if ((status
= signalyzer_h_ctrl_write(
3495 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3496 ((uint32_t)cycles
))) != FT_OK
)
3498 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3499 return ERROR_JTAG_DEVICE_ERROR
;
3502 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3503 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3505 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3506 return ERROR_JTAG_DEVICE_ERROR
;
3510 #elif BUILD_FT2232_LIBFTDI == 1
3513 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3514 ((uint32_t)(channel
<< 8) | led
))) < 0)
3516 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3517 ftdi_get_error_string(&ftdic
));
3518 return ERROR_JTAG_DEVICE_ERROR
;
3521 if ((retval
= signalyzer_h_ctrl_write(
3522 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3523 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3525 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3526 ftdi_get_error_string(&ftdic
));
3527 return ERROR_JTAG_DEVICE_ERROR
;
3530 if ((retval
= signalyzer_h_ctrl_write(
3531 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3532 (uint32_t)cycles
)) < 0)
3534 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3535 ftdi_get_error_string(&ftdic
));
3536 return ERROR_JTAG_DEVICE_ERROR
;
3539 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3540 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3542 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3543 ftdi_get_error_string(&ftdic
));
3544 return ERROR_JTAG_DEVICE_ERROR
;
3551 static int signalyzer_h_init(void)
3553 #if BUILD_FT2232_FTD2XX == 1
3560 uint16_t read_buf
[12] = { 0 };
3562 uint32_t bytes_written
;
3564 /* turn on center green led */
3565 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3566 0xFFFF, 0x00, 0x00);
3568 /* determine what channel config wants to open
3569 * TODO: change me... current implementation is made to work
3570 * with openocd description parsing.
3572 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3576 signalyzer_h_side
= *(end_of_desc
- 1);
3577 if (signalyzer_h_side
== 'B')
3578 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3580 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3584 LOG_ERROR("No Channel was specified");
3588 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3591 #if BUILD_FT2232_FTD2XX == 1
3592 /* read signalyzer versionining information */
3593 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3594 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3596 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3597 return ERROR_JTAG_DEVICE_ERROR
;
3600 for (i
= 0; i
< 10; i
++)
3602 if ((status
= signalyzer_h_ctrl_read(
3603 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3604 &read_buf
[i
])) != FT_OK
)
3606 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3608 return ERROR_JTAG_DEVICE_ERROR
;
3612 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3613 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3614 read_buf
[4], read_buf
[5], read_buf
[6]);
3616 /* set gpio register */
3617 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3618 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3620 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3621 return ERROR_JTAG_DEVICE_ERROR
;
3624 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3627 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3628 return ERROR_JTAG_DEVICE_ERROR
;
3631 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3632 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3635 return ERROR_JTAG_DEVICE_ERROR
;
3638 /* read adapter type information */
3639 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3640 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3642 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3643 return ERROR_JTAG_DEVICE_ERROR
;
3646 if ((status
= signalyzer_h_ctrl_write(
3647 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3649 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3650 return ERROR_JTAG_DEVICE_ERROR
;
3653 if ((status
= signalyzer_h_ctrl_write(
3654 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3656 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3657 return ERROR_JTAG_DEVICE_ERROR
;
3660 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3661 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3663 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3664 return ERROR_JTAG_DEVICE_ERROR
;
3669 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3670 &read_buf
[0])) != FT_OK
)
3672 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3673 return ERROR_JTAG_DEVICE_ERROR
;
3676 if (read_buf
[0] != 0x0498)
3677 signalyzer_h_adapter_type
= 0x0000;
3680 for (i
= 0; i
< 4; i
++)
3682 if ((status
= signalyzer_h_ctrl_read(
3683 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3684 &read_buf
[i
])) != FT_OK
)
3686 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3688 return ERROR_JTAG_DEVICE_ERROR
;
3692 signalyzer_h_adapter_type
= read_buf
[0];
3695 #elif BUILD_FT2232_LIBFTDI == 1
3696 /* currently libftdi does not allow reading individual eeprom
3697 * locations, therefore adapter type cannot be detected.
3698 * override with most common type
3700 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3703 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3705 /* ADAPTOR: EM_LT16_A */
3706 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3708 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3709 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3717 low_direction
= 0x1b;
3720 high_direction
= 0x0;
3722 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3724 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3725 low_output
&= ~nTRST
; /* nTRST = 0 */
3729 low_direction
|= nTRSTnOE
; /* nTRST output */
3730 low_output
|= nTRST
; /* nTRST = 1 */
3733 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3735 low_direction
|= nSRSTnOE
; /* nSRST output */
3736 low_output
|= nSRST
; /* nSRST = 1 */
3740 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3741 low_output
&= ~nSRST
; /* nSRST = 0 */
3744 #if BUILD_FT2232_FTD2XX == 1
3745 /* enable power to the module */
3746 if ((status
= signalyzer_h_ctrl_write(
3747 SIGNALYZER_DATA_BUFFER_ADDR
,
3748 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3751 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3753 return ERROR_JTAG_DEVICE_ERROR
;
3756 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3757 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3759 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3761 return ERROR_JTAG_DEVICE_ERROR
;
3764 /* set gpio mode register */
3765 if ((status
= signalyzer_h_ctrl_write(
3766 SIGNALYZER_DATA_BUFFER_ADDR
,
3767 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3769 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3771 return ERROR_JTAG_DEVICE_ERROR
;
3774 if ((status
= signalyzer_h_ctrl_write(
3775 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3778 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3780 return ERROR_JTAG_DEVICE_ERROR
;
3783 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3784 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3786 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3788 return ERROR_JTAG_DEVICE_ERROR
;
3791 /* set gpio register */
3792 if ((status
= signalyzer_h_ctrl_write(
3793 SIGNALYZER_DATA_BUFFER_ADDR
,
3794 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3796 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3798 return ERROR_JTAG_DEVICE_ERROR
;
3801 if ((status
= signalyzer_h_ctrl_write(
3802 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3805 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3807 return ERROR_JTAG_DEVICE_ERROR
;
3810 if ((status
= signalyzer_h_ctrl_write(
3811 SIGNALYZER_COMMAND_ADDR
,
3812 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3814 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3816 return ERROR_JTAG_DEVICE_ERROR
;
3821 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3822 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3823 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3824 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3825 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3827 if (signalyzer_h_adapter_type
3828 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3829 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3830 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3831 else if (signalyzer_h_adapter_type
3832 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3833 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3834 "(ARM JTAG with PSU) detected. (HW: %2x).",
3835 (read_buf
[1] >> 8));
3836 else if (signalyzer_h_adapter_type
3837 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3838 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3839 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3840 else if (signalyzer_h_adapter_type
3841 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3842 LOG_INFO("Signalyzer: EM-JTAG-P "
3843 "(Generic JTAG with PSU) detected. (HW: %2x).",
3844 (read_buf
[1] >> 8));
3852 low_direction
= 0x1b;
3855 high_direction
= 0x1f;
3857 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3859 high_output
|= nTRSTnOE
;
3860 high_output
&= ~nTRST
;
3864 high_output
&= ~nTRSTnOE
;
3865 high_output
|= nTRST
;
3868 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3870 high_output
&= ~nSRSTnOE
;
3871 high_output
|= nSRST
;
3875 high_output
|= nSRSTnOE
;
3876 high_output
&= ~nSRST
;
3879 #if BUILD_FT2232_FTD2XX == 1
3880 /* enable power to the module */
3881 if ((status
= signalyzer_h_ctrl_write(
3882 SIGNALYZER_DATA_BUFFER_ADDR
,
3883 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3886 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3888 return ERROR_JTAG_DEVICE_ERROR
;
3891 if ((status
= signalyzer_h_ctrl_write(
3892 SIGNALYZER_COMMAND_ADDR
,
3893 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3895 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3897 return ERROR_JTAG_DEVICE_ERROR
;
3900 /* set gpio mode register (IO_16 and IO_17 set as analog
3901 * inputs, other is gpio)
3903 if ((status
= signalyzer_h_ctrl_write(
3904 SIGNALYZER_DATA_BUFFER_ADDR
,
3905 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3907 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3909 return ERROR_JTAG_DEVICE_ERROR
;
3912 if ((status
= signalyzer_h_ctrl_write(
3913 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3916 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3918 return ERROR_JTAG_DEVICE_ERROR
;
3921 if ((status
= signalyzer_h_ctrl_write(
3922 SIGNALYZER_COMMAND_ADDR
,
3923 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3925 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3927 return ERROR_JTAG_DEVICE_ERROR
;
3930 /* set gpio register (all inputs, for -P modules,
3931 * PSU will be turned off)
3933 if ((status
= signalyzer_h_ctrl_write(
3934 SIGNALYZER_DATA_BUFFER_ADDR
,
3935 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3937 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3939 return ERROR_JTAG_DEVICE_ERROR
;
3942 if ((status
= signalyzer_h_ctrl_write(
3943 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3946 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3948 return ERROR_JTAG_DEVICE_ERROR
;
3951 if ((status
= signalyzer_h_ctrl_write(
3952 SIGNALYZER_COMMAND_ADDR
,
3953 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3955 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3957 return ERROR_JTAG_DEVICE_ERROR
;
3962 else if (signalyzer_h_adapter_type
== 0x0000)
3964 LOG_INFO("Signalyzer: No external modules were detected.");
3972 low_direction
= 0x1b;
3975 high_direction
= 0x0;
3977 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3979 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3980 low_output
&= ~nTRST
; /* nTRST = 0 */
3984 low_direction
|= nTRSTnOE
; /* nTRST output */
3985 low_output
|= nTRST
; /* nTRST = 1 */
3988 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3990 low_direction
|= nSRSTnOE
; /* nSRST output */
3991 low_output
|= nSRST
; /* nSRST = 1 */
3995 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3996 low_output
&= ~nSRST
; /* nSRST = 0 */
4001 LOG_ERROR("Unknown module type is detected: %.4x",
4002 signalyzer_h_adapter_type
);
4003 return ERROR_JTAG_DEVICE_ERROR
;
4006 /* initialize low byte of controller for jtag operation */
4008 buf
[1] = low_output
;
4009 buf
[2] = low_direction
;
4011 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4013 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4014 return ERROR_JTAG_INIT_FAILED
;
4017 #if BUILD_FT2232_FTD2XX == 1
4018 if (ftdi_device
== FT_DEVICE_2232H
)
4020 /* initialize high byte of controller for jtag operation */
4022 buf
[1] = high_output
;
4023 buf
[2] = high_direction
;
4025 if ((retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
4027 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4028 return ERROR_JTAG_INIT_FAILED
;
4031 #elif BUILD_FT2232_LIBFTDI == 1
4032 if (ftdi_device
== TYPE_2232H
)
4034 /* initialize high byte of controller for jtag operation */
4036 buf
[1] = high_output
;
4037 buf
[2] = high_direction
;
4039 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4041 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4042 return ERROR_JTAG_INIT_FAILED
;
4049 static void signalyzer_h_reset(int trst
, int srst
)
4051 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4053 /* ADAPTOR: EM_LT16_A */
4054 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4058 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4059 /* switch to output pin (output is low) */
4060 low_direction
|= nTRSTnOE
;
4062 /* switch output low */
4063 low_output
&= ~nTRST
;
4067 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4068 /* switch to input pin (high-Z + internal
4069 * and external pullup) */
4070 low_direction
&= ~nTRSTnOE
;
4072 /* switch output high */
4073 low_output
|= nTRST
;
4078 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4079 /* switch output low */
4080 low_output
&= ~nSRST
;
4082 /* switch to output pin (output is low) */
4083 low_direction
|= nSRSTnOE
;
4087 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4088 /* switch output high */
4089 low_output
|= nSRST
;
4091 /* switch to input pin (high-Z) */
4092 low_direction
&= ~nSRSTnOE
;
4095 /* command "set data bits low byte" */
4097 buffer_write(low_output
);
4098 buffer_write(low_direction
);
4099 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4100 "low_direction: 0x%2.2x",
4101 trst
, srst
, low_output
, low_direction
);
4103 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4104 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4105 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4106 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4107 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4111 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4112 high_output
&= ~nTRSTnOE
;
4114 high_output
&= ~nTRST
;
4118 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4119 high_output
|= nTRSTnOE
;
4121 high_output
|= nTRST
;
4126 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4127 high_output
&= ~nSRST
;
4129 high_output
&= ~nSRSTnOE
;
4133 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4134 high_output
|= nSRST
;
4136 high_output
|= nSRSTnOE
;
4139 /* command "set data bits high byte" */
4141 buffer_write(high_output
);
4142 buffer_write(high_direction
);
4143 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4144 "high_direction: 0x%2.2x",
4145 trst
, srst
, high_output
, high_direction
);
4147 else if (signalyzer_h_adapter_type
== 0x0000)
4151 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4152 /* switch to output pin (output is low) */
4153 low_direction
|= nTRSTnOE
;
4155 /* switch output low */
4156 low_output
&= ~nTRST
;
4160 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4161 /* switch to input pin (high-Z + internal
4162 * and external pullup) */
4163 low_direction
&= ~nTRSTnOE
;
4165 /* switch output high */
4166 low_output
|= nTRST
;
4171 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4172 /* switch output low */
4173 low_output
&= ~nSRST
;
4175 /* switch to output pin (output is low) */
4176 low_direction
|= nSRSTnOE
;
4180 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4181 /* switch output high */
4182 low_output
|= nSRST
;
4184 /* switch to input pin (high-Z) */
4185 low_direction
&= ~nSRSTnOE
;
4188 /* command "set data bits low byte" */
4190 buffer_write(low_output
);
4191 buffer_write(low_direction
);
4192 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4193 "low_direction: 0x%2.2x",
4194 trst
, srst
, low_output
, low_direction
);
4198 static void signalyzer_h_blink(void)
4200 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4203 /********************************************************************
4204 * Support for KT-LINK
4205 * JTAG adapter from KRISTECH
4206 * http://www.kristech.eu
4207 *******************************************************************/
4208 static int ktlink_init(void)
4211 uint32_t bytes_written
;
4212 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4214 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4215 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4217 // initialize low port
4218 buf
[0] = 0x80; // command "set data bits low byte"
4219 buf
[1] = low_output
;
4220 buf
[2] = low_direction
;
4221 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4223 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4225 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4226 return ERROR_JTAG_INIT_FAILED
;
4234 high_output
= 0x80; // turn LED on
4235 high_direction
= 0xFF; // all outputs
4237 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4239 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4240 high_output
|= nTRSTnOE
;
4241 high_output
&= ~nTRST
;
4243 high_output
&= ~nTRSTnOE
;
4244 high_output
|= nTRST
;
4247 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4248 high_output
&= ~nSRSTnOE
;
4249 high_output
|= nSRST
;
4251 high_output
|= nSRSTnOE
;
4252 high_output
&= ~nSRST
;
4255 // initialize high port
4256 buf
[0] = 0x82; // command "set data bits high byte"
4257 buf
[1] = high_output
; // value
4258 buf
[2] = high_direction
;
4259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4261 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4263 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4264 return ERROR_JTAG_INIT_FAILED
;
4270 static void ktlink_reset(int trst
, int srst
)
4272 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4275 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4276 high_output
&= ~nTRSTnOE
;
4278 high_output
&= ~nTRST
;
4279 } else if (trst
== 0) {
4280 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4281 high_output
|= nTRSTnOE
;
4283 high_output
|= nTRST
;
4287 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4288 high_output
&= ~nSRST
;
4290 high_output
&= ~nSRSTnOE
;
4291 } else if (srst
== 0) {
4292 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4293 high_output
|= nSRST
;
4295 high_output
|= nSRSTnOE
;
4298 buffer_write(0x82); // command "set data bits high byte"
4299 buffer_write(high_output
);
4300 buffer_write(high_direction
);
4301 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4304 static void ktlink_blink(void)
4306 /* LED connected to ACBUS7 */
4307 if (high_output
& 0x80)
4308 high_output
&= 0x7F;
4310 high_output
|= 0x80;
4312 buffer_write(0x82); // command "set data bits high byte"
4313 buffer_write(high_output
);
4314 buffer_write(high_direction
);
4317 static const struct command_registration ft2232_command_handlers
[] = {
4319 .name
= "ft2232_device_desc",
4320 .handler
= &ft2232_handle_device_desc_command
,
4321 .mode
= COMMAND_CONFIG
,
4322 .help
= "set the USB device description of the FTDI FT2232 device",
4323 .usage
= "description_string",
4326 .name
= "ft2232_serial",
4327 .handler
= &ft2232_handle_serial_command
,
4328 .mode
= COMMAND_CONFIG
,
4329 .help
= "set the serial number of the FTDI FT2232 device",
4330 .usage
= "serial_string",
4333 .name
= "ft2232_layout",
4334 .handler
= &ft2232_handle_layout_command
,
4335 .mode
= COMMAND_CONFIG
,
4336 .help
= "set the layout of the FT2232 GPIO signals used "
4337 "to control output-enables and reset signals",
4338 .usage
= "layout_name",
4341 .name
= "ft2232_vid_pid",
4342 .handler
= &ft2232_handle_vid_pid_command
,
4343 .mode
= COMMAND_CONFIG
,
4344 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4345 .usage
= "(vid pid)* ",
4348 .name
= "ft2232_latency",
4349 .handler
= &ft2232_handle_latency_command
,
4350 .mode
= COMMAND_CONFIG
,
4351 .help
= "set the FT2232 latency timer to a new value",
4354 COMMAND_REGISTRATION_DONE
4357 struct jtag_interface ft2232_interface
= {
4359 .supported
= DEBUG_CAP_TMS_SEQ
,
4360 .commands
= ft2232_command_handlers
,
4362 .init
= ft2232_init
,
4363 .quit
= ft2232_quit
,
4364 .speed
= ft2232_speed
,
4365 .speed_div
= ft2232_speed_div
,
4367 .execute_queue
= ft2232_execute_queue
,