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 <transport/transport.h>
85 #include <helper/time_support.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
154 static char * ft2232_device_desc_A
= NULL
;
155 static char* ft2232_device_desc
= NULL
;
156 static char* ft2232_serial
= NULL
;
157 static uint8_t ft2232_latency
= 2;
158 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
165 struct ft2232_layout
{
168 void (*reset
)(int trst
, int srst
);
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter_init(void);
180 static int minimodule_init(void);
181 static int turtle_init(void);
182 static int comstick_init(void);
183 static int stm32stick_init(void);
184 static int axm0432_jtag_init(void);
185 static int sheevaplug_init(void);
186 static int icebear_jtag_init(void);
187 static int cortino_jtag_init(void);
188 static int signalyzer_init(void);
189 static int signalyzer_h_init(void);
190 static int ktlink_init(void);
191 static int redbee_init(void);
192 static int lisa_l_init(void);
193 static int flossjtag_init(void);
194 static int xds100v2_init(void);
196 /* reset procedures for supported layouts */
197 static void ftx23_reset(int trst
, int srst
);
198 static void jtagkey_reset(int trst
, int srst
);
199 static void olimex_jtag_reset(int trst
, int srst
);
200 static void flyswatter_reset(int trst
, int srst
);
201 static void minimodule_reset(int trst
, int srst
);
202 static void turtle_reset(int trst
, int srst
);
203 static void comstick_reset(int trst
, int srst
);
204 static void stm32stick_reset(int trst
, int srst
);
205 static void axm0432_jtag_reset(int trst
, int srst
);
206 static void sheevaplug_reset(int trst
, int srst
);
207 static void icebear_jtag_reset(int trst
, int srst
);
208 static void signalyzer_h_reset(int trst
, int srst
);
209 static void ktlink_reset(int trst
, int srst
);
210 static void redbee_reset(int trst
, int srst
);
211 static void xds100v2_reset(int trst
, int srst
);
213 /* blink procedures for layouts that support a blinking led */
214 static void olimex_jtag_blink(void);
215 static void flyswatter_jtag_blink(void);
216 static void turtle_jtag_blink(void);
217 static void signalyzer_h_blink(void);
218 static void ktlink_blink(void);
219 static void lisa_l_blink(void);
220 static void flossjtag_blink(void);
222 /* common transport support options */
224 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
226 static const struct ft2232_layout ft2232_layouts
[] =
229 .init
= usbjtag_init
,
230 .reset
= ftx23_reset
,
233 .init
= jtagkey_init
,
234 .reset
= jtagkey_reset
,
236 { .name
= "jtagkey_prototype_v1",
237 .init
= jtagkey_init
,
238 .reset
= jtagkey_reset
,
240 { .name
= "oocdlink",
241 .init
= jtagkey_init
,
242 .reset
= jtagkey_reset
,
244 { .name
= "signalyzer",
245 .init
= signalyzer_init
,
246 .reset
= ftx23_reset
,
248 { .name
= "evb_lm3s811",
249 .init
= lm3s811_jtag_init
,
250 .reset
= ftx23_reset
,
252 { .name
= "luminary_icdi",
253 .init
= icdi_jtag_init
,
254 .reset
= ftx23_reset
,
256 { .name
= "olimex-jtag",
257 .init
= olimex_jtag_init
,
258 .reset
= olimex_jtag_reset
,
259 .blink
= olimex_jtag_blink
261 { .name
= "flyswatter",
262 .init
= flyswatter_init
,
263 .reset
= flyswatter_reset
,
264 .blink
= flyswatter_jtag_blink
266 { .name
= "minimodule",
267 .init
= minimodule_init
,
268 .reset
= minimodule_reset
,
270 { .name
= "turtelizer2",
272 .reset
= turtle_reset
,
273 .blink
= turtle_jtag_blink
275 { .name
= "comstick",
276 .init
= comstick_init
,
277 .reset
= comstick_reset
,
279 { .name
= "stm32stick",
280 .init
= stm32stick_init
,
281 .reset
= stm32stick_reset
,
283 { .name
= "axm0432_jtag",
284 .init
= axm0432_jtag_init
,
285 .reset
= axm0432_jtag_reset
,
287 { .name
= "sheevaplug",
288 .init
= sheevaplug_init
,
289 .reset
= sheevaplug_reset
,
292 .init
= icebear_jtag_init
,
293 .reset
= icebear_jtag_reset
,
296 .init
= cortino_jtag_init
,
297 .reset
= comstick_reset
,
299 { .name
= "signalyzer-h",
300 .init
= signalyzer_h_init
,
301 .reset
= signalyzer_h_reset
,
302 .blink
= signalyzer_h_blink
306 .reset
= ktlink_reset
,
307 .blink
= ktlink_blink
309 { .name
= "redbee-econotag",
311 .reset
= redbee_reset
,
313 { .name
= "redbee-usb",
315 .reset
= redbee_reset
,
316 .channel
= INTERFACE_B
,
320 .reset
= ftx23_reset
,
321 .blink
= lisa_l_blink
,
322 .channel
= INTERFACE_B
,
324 { .name
= "flossjtag",
325 .init
= flossjtag_init
,
326 .reset
= ftx23_reset
,
327 .blink
= flossjtag_blink
,
329 { .name
= "xds100v2",
330 .init
= xds100v2_init
,
331 .reset
= xds100v2_reset
,
333 { .name
= NULL
, /* END OF TABLE */ },
336 /* bitmask used to drive nTRST; usually a GPIOLx signal */
337 static uint8_t nTRST
;
338 static uint8_t nTRSTnOE
;
339 /* bitmask used to drive nSRST; usually a GPIOLx signal */
340 static uint8_t nSRST
;
341 static uint8_t nSRSTnOE
;
343 /** the layout being used with this debug session */
344 static const struct ft2232_layout
*layout
;
346 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
347 static uint8_t low_output
= 0x0;
349 /* note that direction bit == 1 means that signal is an output */
351 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
352 static uint8_t low_direction
= 0x0;
353 /** default value bitmask for CBUS GPIOH(0..4) */
354 static uint8_t high_output
= 0x0;
355 /** default direction bitmask for CBUS GPIOH(0..4) */
356 static uint8_t high_direction
= 0x0;
358 #if BUILD_FT2232_FTD2XX == 1
359 static FT_HANDLE ftdih
= NULL
;
360 static FT_DEVICE ftdi_device
= 0;
361 #elif BUILD_FT2232_LIBFTDI == 1
362 static struct ftdi_context ftdic
;
363 static enum ftdi_chip_type ftdi_device
;
366 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
367 static int require_send
;
369 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
371 "There is a significant difference between libftdi and libftd2xx. The latter
372 one allows to schedule up to 64*64 bytes of result data while libftdi fails
373 with more than 4*64. As a consequence, the FT2232 driver is forced to
374 perform around 16x more USB transactions for long command streams with TDO
375 capture when running with libftdi."
378 #define FT2232_BUFFER_SIZE 131072
379 a comment would have been nice.
382 #if BUILD_FT2232_FTD2XX == 1
383 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
385 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
388 #define FT2232_BUFFER_SIZE 131072
390 static uint8_t* ft2232_buffer
= NULL
;
391 static int ft2232_buffer_size
= 0;
392 static int ft2232_read_pointer
= 0;
393 static int ft2232_expect_read
= 0;
396 * Function buffer_write
397 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
398 * @param val is the byte to send.
400 static inline void buffer_write(uint8_t val
)
402 assert(ft2232_buffer
);
403 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
404 ft2232_buffer
[ft2232_buffer_size
++] = val
;
408 * Function buffer_read
409 * returns a byte from the byte buffer.
411 static inline uint8_t buffer_read(void)
413 assert(ft2232_buffer
);
414 assert(ft2232_read_pointer
< ft2232_buffer_size
);
415 return ft2232_buffer
[ft2232_read_pointer
++];
419 * Clocks out \a bit_count bits on the TMS line, starting with the least
420 * significant bit of tms_bits and progressing to more significant bits.
421 * Rigorous state transition logging is done here via tap_set_state().
423 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
424 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
425 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
426 * is often used for this, 0x4b.
428 * @param tms_bits Holds the sequence of bits to send.
429 * @param tms_count Tells how many bits in the sequence.
430 * @param tdi_bit A single bit to pass on to TDI before the first TCK
431 * cycle and held static for the duration of TMS clocking.
433 * See the MPSSE spec referenced above.
435 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
439 int tms_ndx
; /* bit index into tms_byte */
441 assert(tms_count
> 0);
443 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
444 mpsse_cmd
, tms_bits
, tms_count
);
446 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
448 bool bit
= tms_bits
& 1;
451 tms_byte
|= (1 << tms_ndx
);
453 /* always do state transitions in public view */
454 tap_set_state(tap_state_transition(tap_get_state(), bit
));
456 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
461 if (tms_ndx
== 7 || i
== tms_count
-1)
463 buffer_write(mpsse_cmd
);
464 buffer_write(tms_ndx
- 1);
466 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
467 TMS/CS and is held static for the duration of TMS/CS clocking.
469 buffer_write(tms_byte
| (tdi_bit
<< 7));
475 * Function get_tms_buffer_requirements
476 * returns what clock_tms() will consume if called with
479 static inline int get_tms_buffer_requirements(int bit_count
)
481 return ((bit_count
+ 6)/7) * 3;
485 * Function move_to_state
486 * moves the TAP controller from the current state to a
487 * \a goal_state through a path given by tap_get_tms_path(). State transition
488 * logging is performed by delegation to clock_tms().
490 * @param goal_state is the destination state for the move.
492 static void move_to_state(tap_state_t goal_state
)
494 tap_state_t start_state
= tap_get_state();
496 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
497 lookup of the required TMS pattern to move to this state from the
501 /* do the 2 lookups */
502 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
503 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
505 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
507 clock_tms(0x4b, tms_bits
, tms_count
, 0);
510 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
512 #if BUILD_FT2232_FTD2XX == 1
514 DWORD dw_bytes_written
= 0;
515 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
517 *bytes_written
= dw_bytes_written
;
518 LOG_ERROR("FT_Write returned: %" PRIu32
, status
);
519 return ERROR_JTAG_DEVICE_ERROR
;
523 *bytes_written
= dw_bytes_written
;
525 #elif BUILD_FT2232_LIBFTDI == 1
527 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
530 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
531 return ERROR_JTAG_DEVICE_ERROR
;
535 *bytes_written
= retval
;
539 if (*bytes_written
!= (uint32_t)size
)
541 return ERROR_JTAG_DEVICE_ERROR
;
547 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
549 #if BUILD_FT2232_FTD2XX == 1
555 while ((*bytes_read
< size
) && timeout
--)
557 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
558 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
561 LOG_ERROR("FT_Read returned: %" PRIu32
, status
);
562 return ERROR_JTAG_DEVICE_ERROR
;
564 *bytes_read
+= dw_bytes_read
;
567 #elif BUILD_FT2232_LIBFTDI == 1
569 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
572 while ((*bytes_read
< size
) && timeout
--)
574 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
577 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
578 return ERROR_JTAG_DEVICE_ERROR
;
580 *bytes_read
+= retval
;
585 if (*bytes_read
< size
)
587 LOG_ERROR("couldn't read enough bytes from "
588 "FT2232 device (%i < %i)",
589 (unsigned)*bytes_read
,
591 return ERROR_JTAG_DEVICE_ERROR
;
597 static bool ft2232_device_is_highspeed(void)
599 #if BUILD_FT2232_FTD2XX == 1
600 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
601 #elif BUILD_FT2232_LIBFTDI == 1
602 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
607 * Commands that only apply to the FT2232H and FT4232H devices.
608 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
609 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
612 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
614 uint8_t buf
= enable
? 0x96 : 0x97;
615 LOG_DEBUG("%2.2x", buf
);
617 uint32_t bytes_written
;
620 if ((retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
622 LOG_ERROR("couldn't write command to %s adaptive clocking"
623 , enable
? "enable" : "disable");
631 * Enable/disable the clk divide by 5 of the 60MHz master clock.
632 * This result in a JTAG clock speed range of 91.553Hz-6MHz
633 * respective 457.763Hz-30MHz.
635 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
637 uint32_t bytes_written
;
638 uint8_t buf
= enable
? 0x8b : 0x8a;
640 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
642 LOG_ERROR("couldn't write command to %s clk divide by 5"
643 , enable
? "enable" : "disable");
644 return ERROR_JTAG_INIT_FAILED
;
646 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
647 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
652 static int ft2232_speed(int speed
)
656 uint32_t bytes_written
;
659 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
660 if (ft2232_device_is_highspeed())
661 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
662 else if (enable_adaptive_clocking
)
664 LOG_ERROR("ft2232 device %lu does not support RTCK"
665 , (long unsigned int)ftdi_device
);
669 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
672 buf
[0] = 0x86; /* command "set divisor" */
673 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
674 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
676 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
677 if ((retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
679 LOG_ERROR("couldn't set FT2232 TCK speed");
686 static int ft2232_speed_div(int speed
, int* khz
)
688 /* Take a look in the FT2232 manual,
689 * AN2232C-01 Command Processor for
690 * MPSSE and MCU Host Bus. Chapter 3.8 */
692 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
697 static int ft2232_khz(int khz
, int* jtag_speed
)
701 if (ft2232_device_is_highspeed())
703 *jtag_speed
= RTCK_SPEED
;
708 LOG_DEBUG("RCLK not supported");
713 /* Take a look in the FT2232 manual,
714 * AN2232C-01 Command Processor for
715 * MPSSE and MCU Host Bus. Chapter 3.8
717 * We will calc here with a multiplier
718 * of 10 for better rounding later. */
720 /* Calc speed, (ft2232_max_tck / khz) - 1 */
721 /* Use 65000 for better rounding */
722 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
724 /* Add 0.9 for rounding */
727 /* Calc real speed */
728 *jtag_speed
= *jtag_speed
/ 10;
730 /* Check if speed is greater than 0 */
736 /* Check max value */
737 if (*jtag_speed
> 0xFFFF)
739 *jtag_speed
= 0xFFFF;
745 static void ft2232_end_state(tap_state_t state
)
747 if (tap_is_state_stable(state
))
748 tap_set_end_state(state
);
751 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
756 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
758 int num_bytes
= (scan_size
+ 7) / 8;
759 int bits_left
= scan_size
;
762 while (num_bytes
-- > 1)
764 buffer
[cur_byte
++] = buffer_read();
768 buffer
[cur_byte
] = 0x0;
770 /* There is one more partial byte left from the clock data in/out instructions */
773 buffer
[cur_byte
] = buffer_read() >> 1;
775 /* 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 */
776 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
779 static void ft2232_debug_dump_buffer(void)
785 for (i
= 0; i
< ft2232_buffer_size
; i
++)
787 line_p
+= snprintf(line_p
, sizeof(line
) - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
790 LOG_DEBUG("%s", line
);
796 LOG_DEBUG("%s", line
);
799 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
801 struct jtag_command
* cmd
;
806 uint32_t bytes_written
= 0;
807 uint32_t bytes_read
= 0;
809 #ifdef _DEBUG_USB_IO_
810 struct timeval start
, inter
, inter2
, end
;
811 struct timeval d_inter
, d_inter2
, d_end
;
814 #ifdef _DEBUG_USB_COMMS_
815 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
816 ft2232_debug_dump_buffer();
819 #ifdef _DEBUG_USB_IO_
820 gettimeofday(&start
, NULL
);
823 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
825 LOG_ERROR("couldn't write MPSSE commands to FT2232");
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&inter
, NULL
);
833 if (ft2232_expect_read
)
835 /* FIXME this "timeout" is never changed ... */
836 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
837 ft2232_buffer_size
= 0;
839 #ifdef _DEBUG_USB_IO_
840 gettimeofday(&inter2
, NULL
);
843 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
845 LOG_ERROR("couldn't read from FT2232");
849 #ifdef _DEBUG_USB_IO_
850 gettimeofday(&end
, NULL
);
852 timeval_subtract(&d_inter
, &inter
, &start
);
853 timeval_subtract(&d_inter2
, &inter2
, &start
);
854 timeval_subtract(&d_end
, &end
, &start
);
856 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
857 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
858 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
859 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
862 ft2232_buffer_size
= bytes_read
;
864 if (ft2232_expect_read
!= ft2232_buffer_size
)
866 LOG_ERROR("ft2232_expect_read (%i) != "
867 "ft2232_buffer_size (%i) "
871 LIBFTDI_READ_RETRY_COUNT
- timeout
);
872 ft2232_debug_dump_buffer();
877 #ifdef _DEBUG_USB_COMMS_
878 LOG_DEBUG("read buffer (%i retries): %i bytes",
879 LIBFTDI_READ_RETRY_COUNT
- timeout
,
881 ft2232_debug_dump_buffer();
885 ft2232_expect_read
= 0;
886 ft2232_read_pointer
= 0;
888 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
889 * that wasn't handled by a caller-provided error handler
899 type
= jtag_scan_type(cmd
->cmd
.scan
);
900 if (type
!= SCAN_OUT
)
902 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
903 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
904 ft2232_read_scan(type
, buffer
, scan_size
);
905 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
906 retval
= ERROR_JTAG_QUEUE_FAILED
;
918 ft2232_buffer_size
= 0;
924 * Function ft2232_add_pathmove
925 * moves the TAP controller from the current state to a new state through the
926 * given path, where path is an array of tap_state_t's.
928 * @param path is an array of tap_stat_t which gives the states to traverse through
929 * ending with the last state at path[num_states-1]
930 * @param num_states is the count of state steps to move through
932 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
936 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
940 /* this loop verifies that the path is legal and logs each state in the path */
943 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
945 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
947 /* command "Clock Data to TMS/CS Pin (no Read)" */
950 /* number of states remaining */
951 buffer_write(num_states_batch
- 1);
953 while (num_states_batch
--) {
954 /* either TMS=0 or TMS=1 must work ... */
955 if (tap_state_transition(tap_get_state(), false)
956 == path
[state_count
])
957 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
958 else if (tap_state_transition(tap_get_state(), true)
959 == path
[state_count
])
960 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
962 /* ... or else the caller goofed BADLY */
964 LOG_ERROR("BUG: %s -> %s isn't a valid "
965 "TAP state transition",
966 tap_state_name(tap_get_state()),
967 tap_state_name(path
[state_count
]));
971 tap_set_state(path
[state_count
]);
976 buffer_write(tms_byte
);
978 tap_set_end_state(tap_get_state());
981 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
983 int num_bytes
= (scan_size
+ 7) / 8;
984 int bits_left
= scan_size
;
990 if (tap_get_state() != TAP_DRSHIFT
)
992 move_to_state(TAP_DRSHIFT
);
997 if (tap_get_state() != TAP_IRSHIFT
)
999 move_to_state(TAP_IRSHIFT
);
1003 /* add command for complete bytes */
1004 while (num_bytes
> 1)
1007 if (type
== SCAN_IO
)
1009 /* Clock Data Bytes In and Out LSB First */
1011 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1013 else if (type
== SCAN_OUT
)
1015 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1017 /* LOG_DEBUG("added TDI bytes (o)"); */
1019 else if (type
== SCAN_IN
)
1021 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1023 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1026 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1027 num_bytes
-= thisrun_bytes
;
1029 buffer_write((uint8_t) (thisrun_bytes
- 1));
1030 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1032 if (type
!= SCAN_IN
)
1034 /* add complete bytes */
1035 while (thisrun_bytes
-- > 0)
1037 buffer_write(buffer
[cur_byte
++]);
1041 else /* (type == SCAN_IN) */
1043 bits_left
-= 8 * (thisrun_bytes
);
1047 /* the most signifcant bit is scanned during TAP movement */
1048 if (type
!= SCAN_IN
)
1049 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1053 /* process remaining bits but the last one */
1056 if (type
== SCAN_IO
)
1058 /* Clock Data Bits In and Out LSB First */
1060 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1062 else if (type
== SCAN_OUT
)
1064 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1066 /* LOG_DEBUG("added TDI bits (o)"); */
1068 else if (type
== SCAN_IN
)
1070 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1072 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1075 buffer_write(bits_left
- 2);
1076 if (type
!= SCAN_IN
)
1077 buffer_write(buffer
[cur_byte
]);
1080 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1081 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1083 if (type
== SCAN_IO
)
1085 /* Clock Data Bits In and Out LSB First */
1087 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1089 else if (type
== SCAN_OUT
)
1091 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1093 /* LOG_DEBUG("added TDI bits (o)"); */
1095 else if (type
== SCAN_IN
)
1097 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1099 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1102 buffer_write(last_bit
);
1110 /* move from Shift-IR/DR to end state */
1111 if (type
!= SCAN_OUT
)
1113 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1114 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1117 /* Clock Data to TMS/CS Pin with Read */
1122 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1123 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1124 /* Clock Data to TMS/CS Pin (no Read) */
1128 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1129 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1132 if (tap_get_state() != tap_get_end_state())
1134 move_to_state(tap_get_end_state());
1138 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1140 int num_bytes
= (scan_size
+ 7) / 8;
1141 int bits_left
= scan_size
;
1144 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1145 uint8_t* receive_pointer
= receive_buffer
;
1146 uint32_t bytes_written
;
1147 uint32_t bytes_read
;
1149 int thisrun_read
= 0;
1153 LOG_ERROR("BUG: large IR scans are not supported");
1157 if (tap_get_state() != TAP_DRSHIFT
)
1159 move_to_state(TAP_DRSHIFT
);
1162 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1164 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1167 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1168 ft2232_buffer_size
, (int)bytes_written
);
1169 ft2232_buffer_size
= 0;
1171 /* add command for complete bytes */
1172 while (num_bytes
> 1)
1176 if (type
== SCAN_IO
)
1178 /* Clock Data Bytes In and Out LSB First */
1180 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1182 else if (type
== SCAN_OUT
)
1184 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1186 /* LOG_DEBUG("added TDI bytes (o)"); */
1188 else if (type
== SCAN_IN
)
1190 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1192 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1195 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1196 thisrun_read
= thisrun_bytes
;
1197 num_bytes
-= thisrun_bytes
;
1198 buffer_write((uint8_t) (thisrun_bytes
- 1));
1199 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1201 if (type
!= SCAN_IN
)
1203 /* add complete bytes */
1204 while (thisrun_bytes
-- > 0)
1206 buffer_write(buffer
[cur_byte
]);
1211 else /* (type == SCAN_IN) */
1213 bits_left
-= 8 * (thisrun_bytes
);
1216 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1218 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1221 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1223 (int)bytes_written
);
1224 ft2232_buffer_size
= 0;
1226 if (type
!= SCAN_OUT
)
1228 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1230 LOG_ERROR("couldn't read from FT2232");
1233 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1236 receive_pointer
+= bytes_read
;
1242 /* the most signifcant bit is scanned during TAP movement */
1243 if (type
!= SCAN_IN
)
1244 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1248 /* process remaining bits but the last one */
1251 if (type
== SCAN_IO
)
1253 /* Clock Data Bits In and Out LSB First */
1255 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1257 else if (type
== SCAN_OUT
)
1259 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1261 /* LOG_DEBUG("added TDI bits (o)"); */
1263 else if (type
== SCAN_IN
)
1265 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1267 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1269 buffer_write(bits_left
- 2);
1270 if (type
!= SCAN_IN
)
1271 buffer_write(buffer
[cur_byte
]);
1273 if (type
!= SCAN_OUT
)
1277 if (tap_get_end_state() == TAP_DRSHIFT
)
1279 if (type
== SCAN_IO
)
1281 /* Clock Data Bits In and Out LSB First */
1283 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1285 else if (type
== SCAN_OUT
)
1287 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1289 /* LOG_DEBUG("added TDI bits (o)"); */
1291 else if (type
== SCAN_IN
)
1293 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1295 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1298 buffer_write(last_bit
);
1302 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1303 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1306 /* move from Shift-IR/DR to end state */
1307 if (type
!= SCAN_OUT
)
1309 /* Clock Data to TMS/CS Pin with Read */
1311 /* LOG_DEBUG("added TMS scan (read)"); */
1315 /* Clock Data to TMS/CS Pin (no Read) */
1317 /* LOG_DEBUG("added TMS scan (no read)"); */
1320 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1321 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1324 if (type
!= SCAN_OUT
)
1327 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1329 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1332 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1334 (int)bytes_written
);
1335 ft2232_buffer_size
= 0;
1337 if (type
!= SCAN_OUT
)
1339 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1341 LOG_ERROR("couldn't read from FT2232");
1344 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1347 receive_pointer
+= bytes_read
;
1353 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1355 int predicted_size
= 3;
1356 int num_bytes
= (scan_size
- 1) / 8;
1358 if (tap_get_state() != TAP_DRSHIFT
)
1359 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1361 if (type
== SCAN_IN
) /* only from device to host */
1363 /* complete bytes */
1364 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1366 /* remaining bits - 1 (up to 7) */
1367 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1369 else /* host to device, or bidirectional */
1371 /* complete bytes */
1372 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1374 /* remaining bits -1 (up to 7) */
1375 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1378 return predicted_size
;
1381 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1383 int predicted_size
= 0;
1385 if (type
!= SCAN_OUT
)
1387 /* complete bytes */
1388 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1390 /* remaining bits - 1 */
1391 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1393 /* last bit (from TMS scan) */
1394 predicted_size
+= 1;
1397 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1399 return predicted_size
;
1402 /* semi-generic FT2232/FT4232 reset code */
1403 static void ftx23_reset(int trst
, int srst
)
1405 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1408 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1409 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1411 low_output
&= ~nTRST
; /* switch output low */
1415 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1416 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1418 low_output
|= nTRST
; /* switch output high */
1423 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1424 low_output
&= ~nSRST
; /* switch output low */
1426 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1430 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1431 low_output
|= nSRST
; /* switch output high */
1433 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1436 /* command "set data bits low byte" */
1438 buffer_write(low_output
);
1439 buffer_write(low_direction
);
1442 static void jtagkey_reset(int trst
, int srst
)
1444 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1447 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1448 high_output
&= ~nTRSTnOE
;
1450 high_output
&= ~nTRST
;
1454 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1455 high_output
|= nTRSTnOE
;
1457 high_output
|= nTRST
;
1462 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1463 high_output
&= ~nSRST
;
1465 high_output
&= ~nSRSTnOE
;
1469 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1470 high_output
|= nSRST
;
1472 high_output
|= nSRSTnOE
;
1475 /* command "set data bits high byte" */
1477 buffer_write(high_output
);
1478 buffer_write(high_direction
);
1479 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1483 static void olimex_jtag_reset(int trst
, int srst
)
1485 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1488 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1489 high_output
&= ~nTRSTnOE
;
1491 high_output
&= ~nTRST
;
1495 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1496 high_output
|= nTRSTnOE
;
1498 high_output
|= nTRST
;
1503 high_output
|= nSRST
;
1507 high_output
&= ~nSRST
;
1510 /* command "set data bits high byte" */
1512 buffer_write(high_output
);
1513 buffer_write(high_direction
);
1514 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1518 static void axm0432_jtag_reset(int trst
, int srst
)
1522 tap_set_state(TAP_RESET
);
1523 high_output
&= ~nTRST
;
1527 high_output
|= nTRST
;
1532 high_output
&= ~nSRST
;
1536 high_output
|= nSRST
;
1539 /* command "set data bits low byte" */
1541 buffer_write(high_output
);
1542 buffer_write(high_direction
);
1543 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1547 static void flyswatter_reset(int trst
, int srst
)
1551 low_output
&= ~nTRST
;
1555 low_output
|= nTRST
;
1560 low_output
|= nSRST
;
1564 low_output
&= ~nSRST
;
1567 /* command "set data bits low byte" */
1569 buffer_write(low_output
);
1570 buffer_write(low_direction
);
1571 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1574 static void minimodule_reset(int trst
, int srst
)
1578 low_output
&= ~nSRST
;
1582 low_output
|= nSRST
;
1585 /* command "set data bits low byte" */
1587 buffer_write(low_output
);
1588 buffer_write(low_direction
);
1589 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1592 static void turtle_reset(int trst
, int srst
)
1598 low_output
|= nSRST
;
1602 low_output
&= ~nSRST
;
1605 /* command "set data bits low byte" */
1607 buffer_write(low_output
);
1608 buffer_write(low_direction
);
1609 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1612 static void comstick_reset(int trst
, int srst
)
1616 high_output
&= ~nTRST
;
1620 high_output
|= nTRST
;
1625 high_output
&= ~nSRST
;
1629 high_output
|= nSRST
;
1632 /* command "set data bits high byte" */
1634 buffer_write(high_output
);
1635 buffer_write(high_direction
);
1636 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1640 static void stm32stick_reset(int trst
, int srst
)
1644 high_output
&= ~nTRST
;
1648 high_output
|= nTRST
;
1653 low_output
&= ~nSRST
;
1657 low_output
|= nSRST
;
1660 /* command "set data bits low byte" */
1662 buffer_write(low_output
);
1663 buffer_write(low_direction
);
1665 /* command "set data bits high byte" */
1667 buffer_write(high_output
);
1668 buffer_write(high_direction
);
1669 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1673 static void sheevaplug_reset(int trst
, int srst
)
1676 high_output
&= ~nTRST
;
1678 high_output
|= nTRST
;
1681 high_output
&= ~nSRSTnOE
;
1683 high_output
|= nSRSTnOE
;
1685 /* command "set data bits high byte" */
1687 buffer_write(high_output
);
1688 buffer_write(high_direction
);
1689 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1692 static void redbee_reset(int trst
, int srst
)
1696 tap_set_state(TAP_RESET
);
1697 high_output
&= ~nTRST
;
1701 high_output
|= nTRST
;
1706 high_output
&= ~nSRST
;
1710 high_output
|= nSRST
;
1713 /* command "set data bits low byte" */
1715 buffer_write(high_output
);
1716 buffer_write(high_direction
);
1717 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1718 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1722 static void xds100v2_reset(int trst
, int srst
)
1726 tap_set_state(TAP_RESET
);
1727 high_output
&= ~nTRST
;
1731 high_output
|= nTRST
;
1736 high_output
|= nSRST
;
1740 high_output
&= ~nSRST
;
1743 /* command "set data bits low byte" */
1745 buffer_write(high_output
);
1746 buffer_write(high_direction
);
1747 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1748 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1752 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1756 int predicted_size
= 0;
1759 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1760 cmd
->cmd
.runtest
->num_cycles
,
1761 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1763 /* only send the maximum buffer size that FT2232C can handle */
1765 if (tap_get_state() != TAP_IDLE
)
1766 predicted_size
+= 3;
1767 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1768 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1769 predicted_size
+= 3;
1770 if (tap_get_end_state() != TAP_IDLE
)
1771 predicted_size
+= 3;
1772 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1774 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1775 retval
= ERROR_JTAG_QUEUE_FAILED
;
1779 if (tap_get_state() != TAP_IDLE
)
1781 move_to_state(TAP_IDLE
);
1784 i
= cmd
->cmd
.runtest
->num_cycles
;
1787 /* there are no state transitions in this code, so omit state tracking */
1789 /* command "Clock Data to TMS/CS Pin (no Read)" */
1793 buffer_write((i
> 7) ? 6 : (i
- 1));
1798 i
-= (i
> 7) ? 7 : i
;
1799 /* LOG_DEBUG("added TMS scan (no read)"); */
1802 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1804 if (tap_get_state() != tap_get_end_state())
1806 move_to_state(tap_get_end_state());
1810 DEBUG_JTAG_IO("runtest: %i, end in %s",
1811 cmd
->cmd
.runtest
->num_cycles
,
1812 tap_state_name(tap_get_end_state()));
1816 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1818 int predicted_size
= 0;
1819 int retval
= ERROR_OK
;
1821 DEBUG_JTAG_IO("statemove end in %s",
1822 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1824 /* only send the maximum buffer size that FT2232C can handle */
1826 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1828 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1829 retval
= ERROR_JTAG_QUEUE_FAILED
;
1833 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1835 /* For TAP_RESET, ignore the current recorded state. It's often
1836 * wrong at server startup, and this transation is critical whenever
1839 if (tap_get_end_state() == TAP_RESET
) {
1840 clock_tms(0x4b, 0xff, 5, 0);
1843 /* shortest-path move to desired end state */
1844 } else if (tap_get_state() != tap_get_end_state())
1846 move_to_state(tap_get_end_state());
1854 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1855 * (or SWD) state machine.
1857 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1859 int retval
= ERROR_OK
;
1860 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1861 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1864 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1866 /* only send the maximum buffer size that FT2232C can handle */
1867 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1868 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1869 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1870 retval
= ERROR_JTAG_QUEUE_FAILED
;
1876 /* Shift out in batches of at most 6 bits; there's a report of an
1877 * FT2232 bug in this area, where shifting exactly 7 bits can make
1878 * problems with TMS signaling for the last clock cycle:
1880 * http://developer.intra2net.com/mailarchive/html/
1881 * libftdi/2009/msg00292.html
1883 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1885 * Note that pathmoves in JTAG are not often seven bits, so that
1886 * isn't a particularly likely situation outside of "special"
1887 * signaling such as switching between JTAG and SWD modes.
1890 if (num_bits
<= 6) {
1892 buffer_write(num_bits
- 1);
1893 buffer_write(*bits
& 0x3f);
1897 /* Yes, this is lazy ... we COULD shift out more data
1898 * bits per operation, but doing it in nybbles is easy
1902 buffer_write(*bits
& 0xf);
1905 count
= (num_bits
> 4) ? 4 : num_bits
;
1908 buffer_write(count
- 1);
1909 buffer_write((*bits
>> 4) & 0xf);
1919 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1921 int predicted_size
= 0;
1922 int retval
= ERROR_OK
;
1924 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1925 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1927 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1928 tap_state_name(tap_get_state()),
1929 tap_state_name(path
[num_states
-1]));
1931 /* only send the maximum buffer size that FT2232C can handle */
1932 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1933 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1935 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1936 retval
= ERROR_JTAG_QUEUE_FAILED
;
1942 ft2232_add_pathmove(path
, num_states
);
1948 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1951 int scan_size
; /* size of IR or DR scan */
1952 int predicted_size
= 0;
1953 int retval
= ERROR_OK
;
1955 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1957 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1959 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1961 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1962 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1964 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1965 /* unsent commands before this */
1966 if (first_unsent
!= cmd
)
1967 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1968 retval
= ERROR_JTAG_QUEUE_FAILED
;
1970 /* current command */
1971 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1972 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1974 first_unsent
= cmd
->next
;
1979 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1981 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1984 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1985 retval
= ERROR_JTAG_QUEUE_FAILED
;
1989 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1990 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1991 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1992 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1996 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1997 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1998 tap_state_name(tap_get_end_state()));
2003 static int ft2232_execute_reset(struct jtag_command
*cmd
)
2006 int predicted_size
= 0;
2009 DEBUG_JTAG_IO("reset trst: %i srst %i",
2010 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
2012 /* only send the maximum buffer size that FT2232C can handle */
2014 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
2016 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2017 retval
= ERROR_JTAG_QUEUE_FAILED
;
2022 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
2024 tap_set_state(TAP_RESET
);
2027 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
2030 DEBUG_JTAG_IO("trst: %i, srst: %i",
2031 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
2035 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
2040 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
2042 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2043 retval
= ERROR_JTAG_QUEUE_FAILED
;
2044 first_unsent
= cmd
->next
;
2045 jtag_sleep(cmd
->cmd
.sleep
->us
);
2046 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
2048 tap_state_name(tap_get_state()));
2052 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
2057 /* this is only allowed while in a stable state. A check for a stable
2058 * state was done in jtag_add_clocks()
2060 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
2061 retval
= ERROR_JTAG_QUEUE_FAILED
;
2062 DEBUG_JTAG_IO("clocks %i while in %s",
2063 cmd
->cmd
.stableclocks
->num_cycles
,
2064 tap_state_name(tap_get_state()));
2068 static int ft2232_execute_command(struct jtag_command
*cmd
)
2074 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
2075 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
2076 case JTAG_TLR_RESET
: retval
= ft2232_execute_statemove(cmd
); break;
2077 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
2078 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
2079 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
2080 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
2082 retval
= ft2232_execute_tms(cmd
);
2085 LOG_ERROR("BUG: unknown JTAG command type encountered");
2086 retval
= ERROR_JTAG_QUEUE_FAILED
;
2092 static int ft2232_execute_queue(void)
2094 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
2097 first_unsent
= cmd
; /* next command that has to be sent */
2100 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2101 * that wasn't handled by a caller-provided error handler
2105 ft2232_buffer_size
= 0;
2106 ft2232_expect_read
= 0;
2108 /* blink, if the current layout has that feature */
2114 /* fill the write buffer with the desired command */
2115 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2116 retval
= ERROR_JTAG_QUEUE_FAILED
;
2117 /* Start reading input before FT2232 TX buffer fills up.
2118 * Sometimes this happens because we don't know the
2119 * length of the last command before we execute it. So
2120 * we simple inform the user.
2124 if (ft2232_expect_read
>= FT2232_BUFFER_READ_QUEUE_SIZE
)
2126 if (ft2232_expect_read
> (FT2232_BUFFER_READ_QUEUE_SIZE
+1) )
2127 LOG_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read
,(FT2232_BUFFER_READ_QUEUE_SIZE
+1));
2128 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2129 retval
= ERROR_JTAG_QUEUE_FAILED
;
2134 if (require_send
> 0)
2135 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2136 retval
= ERROR_JTAG_QUEUE_FAILED
;
2141 #if BUILD_FT2232_FTD2XX == 1
2142 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2146 char SerialNumber
[16];
2147 char Description
[64];
2148 DWORD openex_flags
= 0;
2149 char* openex_string
= NULL
;
2150 uint8_t latency_timer
;
2152 if (layout
== NULL
) {
2153 LOG_WARNING("No ft2232 layout specified'");
2154 return ERROR_JTAG_INIT_FAILED
;
2157 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2160 /* Add non-standard Vid/Pid to the linux driver */
2161 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2163 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2167 if (ft2232_device_desc
&& ft2232_serial
)
2169 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2170 ft2232_device_desc
= NULL
;
2173 if (ft2232_device_desc
)
2175 openex_string
= ft2232_device_desc
;
2176 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2178 else if (ft2232_serial
)
2180 openex_string
= ft2232_serial
;
2181 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2185 LOG_ERROR("neither device description nor serial number specified");
2186 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2188 return ERROR_JTAG_INIT_FAILED
;
2191 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2192 if (status
!= FT_OK
) {
2193 /* under Win32, the FTD2XX driver appends an "A" to the end
2194 * of the description, if we tried by the desc, then
2195 * try by the alternate "A" description. */
2196 if (openex_string
== ft2232_device_desc
) {
2197 /* Try the alternate method. */
2198 openex_string
= ft2232_device_desc_A
;
2199 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2200 if (status
== FT_OK
) {
2201 /* yea, the "alternate" method worked! */
2203 /* drat, give the user a meaningfull message.
2204 * telling the use we tried *BOTH* methods. */
2205 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2207 ft2232_device_desc_A
);
2212 if (status
!= FT_OK
)
2218 LOG_WARNING("unable to open ftdi device (trying more): %" PRIu32
, status
);
2220 return ERROR_JTAG_INIT_FAILED
;
2222 LOG_ERROR("unable to open ftdi device: %" PRIu32
, status
);
2223 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2224 if (status
== FT_OK
)
2226 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2229 for (i
= 0; i
< num_devices
; i
++)
2230 desc_array
[i
] = malloc(64);
2232 desc_array
[num_devices
] = NULL
;
2234 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2236 if (status
== FT_OK
)
2238 LOG_ERROR("ListDevices: %" PRIu32
, num_devices
);
2239 for (i
= 0; i
< num_devices
; i
++)
2240 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2243 for (i
= 0; i
< num_devices
; i
++)
2244 free(desc_array
[i
]);
2250 LOG_ERROR("ListDevices: NONE");
2252 return ERROR_JTAG_INIT_FAILED
;
2255 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2257 LOG_ERROR("unable to set latency timer: %" PRIu32
, status
);
2258 return ERROR_JTAG_INIT_FAILED
;
2261 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2263 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2264 * so ignore errors if using this driver version */
2267 status
= FT_GetDriverVersion(ftdih
, &dw_version
);
2268 LOG_ERROR("unable to get latency timer: %" PRIu32
, status
);
2270 if ((status
== FT_OK
) && (dw_version
== 0x10004)) {
2271 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2272 "with FT_GetLatencyTimer, upgrade to a newer version");
2275 return ERROR_JTAG_INIT_FAILED
;
2280 LOG_DEBUG("current latency timer: %i", latency_timer
);
2283 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2285 LOG_ERROR("unable to set timeouts: %" PRIu32
, status
);
2286 return ERROR_JTAG_INIT_FAILED
;
2289 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2291 LOG_ERROR("unable to enable bit i/o mode: %" PRIu32
, status
);
2292 return ERROR_JTAG_INIT_FAILED
;
2295 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2297 LOG_ERROR("unable to get FT_GetDeviceInfo: %" PRIu32
, status
);
2298 return ERROR_JTAG_INIT_FAILED
;
2302 static const char* type_str
[] =
2303 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2304 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2305 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2306 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2307 LOG_INFO("device: %" PRIu32
" \"%s\"", ftdi_device
, type_str
[type_index
]);
2308 LOG_INFO("deviceID: %" PRIu32
, deviceID
);
2309 LOG_INFO("SerialNumber: %s", SerialNumber
);
2310 LOG_INFO("Description: %s", Description
);
2316 static int ft2232_purge_ftd2xx(void)
2320 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2322 LOG_ERROR("error purging ftd2xx device: %" PRIu32
, status
);
2323 return ERROR_JTAG_INIT_FAILED
;
2329 #endif /* BUILD_FT2232_FTD2XX == 1 */
2331 #if BUILD_FT2232_LIBFTDI == 1
2332 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2334 uint8_t latency_timer
;
2336 if (layout
== NULL
) {
2337 LOG_WARNING("No ft2232 layout specified'");
2338 return ERROR_JTAG_INIT_FAILED
;
2341 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2342 layout
->name
, vid
, pid
);
2344 if (ftdi_init(&ftdic
) < 0)
2345 return ERROR_JTAG_INIT_FAILED
;
2347 /* default to INTERFACE_A */
2348 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2350 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2352 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2353 return ERROR_JTAG_INIT_FAILED
;
2356 /* context, vendor id, product id */
2357 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2361 LOG_WARNING("unable to open ftdi device (trying more): %s",
2364 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2366 return ERROR_JTAG_INIT_FAILED
;
2369 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2370 if (ftdi_usb_reset(&ftdic
) < 0)
2372 LOG_ERROR("unable to reset ftdi device");
2373 return ERROR_JTAG_INIT_FAILED
;
2376 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2378 LOG_ERROR("unable to set latency timer");
2379 return ERROR_JTAG_INIT_FAILED
;
2382 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2384 LOG_ERROR("unable to get latency timer");
2385 return ERROR_JTAG_INIT_FAILED
;
2389 LOG_DEBUG("current latency timer: %i", latency_timer
);
2392 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2394 ftdi_device
= ftdic
.type
;
2395 static const char* type_str
[] =
2396 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2397 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2398 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2399 ? ftdi_device
: no_of_known_types
;
2400 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2404 static int ft2232_purge_libftdi(void)
2406 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2408 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2409 return ERROR_JTAG_INIT_FAILED
;
2415 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2417 static int ft2232_set_data_bits_low_byte( uint8_t value
, uint8_t direction
)
2420 uint32_t bytes_written
;
2422 buf
[0] = 0x80; /* command "set data bits low byte" */
2423 buf
[1] = value
; /* value */
2424 buf
[2] = direction
; /* direction */
2426 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2428 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2430 LOG_ERROR("couldn't initialize data bits low byte");
2431 return ERROR_JTAG_INIT_FAILED
;
2437 static int ft2232_set_data_bits_high_byte( uint8_t value
, uint8_t direction
)
2440 uint32_t bytes_written
;
2442 buf
[0] = 0x82; /* command "set data bits high byte" */
2443 buf
[1] = value
; /* value */
2444 buf
[2] = direction
; /* direction */
2446 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2448 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2450 LOG_ERROR("couldn't initialize data bits high byte");
2451 return ERROR_JTAG_INIT_FAILED
;
2457 static int ft2232_init(void)
2461 uint32_t bytes_written
;
2463 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2465 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2469 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2472 if (layout
== NULL
) {
2473 LOG_WARNING("No ft2232 layout specified'");
2474 return ERROR_JTAG_INIT_FAILED
;
2477 for (int i
= 0; 1; i
++)
2480 * "more indicates that there are more IDs to try, so we should
2481 * not print an error for an ID mismatch (but for anything
2484 * try_more indicates that the error code returned indicates an
2485 * ID mismatch (and nothing else) and that we should proceeed
2486 * with the next ID pair.
2488 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2491 #if BUILD_FT2232_FTD2XX == 1
2492 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2494 #elif BUILD_FT2232_LIBFTDI == 1
2495 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2496 more
, &try_more
, layout
->channel
);
2500 if (!more
|| !try_more
)
2504 ft2232_buffer_size
= 0;
2505 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2507 if (layout
->init() != ERROR_OK
)
2508 return ERROR_JTAG_INIT_FAILED
;
2510 if (ft2232_device_is_highspeed())
2512 #ifndef BUILD_FT2232_HIGHSPEED
2513 #if BUILD_FT2232_FTD2XX == 1
2514 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2515 #elif BUILD_FT2232_LIBFTDI == 1
2516 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2519 /* make sure the legacy mode is disabled */
2520 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2521 return ERROR_JTAG_INIT_FAILED
;
2524 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2525 if ((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
)
2527 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2528 return ERROR_JTAG_INIT_FAILED
;
2531 #if BUILD_FT2232_FTD2XX == 1
2532 return ft2232_purge_ftd2xx();
2533 #elif BUILD_FT2232_LIBFTDI == 1
2534 return ft2232_purge_libftdi();
2540 /** Updates defaults for DBUS signals: the four JTAG signals
2541 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2543 static inline void ftx232_dbus_init(void)
2546 low_direction
= 0x0b;
2549 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2550 * the four GPIOL signals. Initialization covers value and direction,
2551 * as customized for each layout.
2553 static int ftx232_dbus_write(void)
2555 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2556 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2558 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2559 low_output
&= ~nTRST
; /* nTRST = 0 */
2563 low_direction
|= nTRSTnOE
; /* nTRST output */
2564 low_output
|= nTRST
; /* nTRST = 1 */
2567 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2569 low_direction
|= nSRSTnOE
; /* nSRST output */
2570 low_output
|= nSRST
; /* nSRST = 1 */
2574 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2575 low_output
&= ~nSRST
; /* nSRST = 0 */
2578 /* initialize low byte for jtag */
2579 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2581 LOG_ERROR("couldn't initialize FT2232 DBUS");
2582 return ERROR_JTAG_INIT_FAILED
;
2588 static int usbjtag_init(void)
2591 * NOTE: This is now _specific_ to the "usbjtag" layout.
2592 * Don't try cram any more layouts into this.
2601 return ftx232_dbus_write();
2604 static int lm3s811_jtag_init(void)
2608 /* There are multiple revisions of LM3S811 eval boards:
2609 * - Rev B (and older?) boards have no SWO trace support.
2610 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2611 * they should use the "luminary_icdi" layout instead.
2618 low_direction
= 0x8b;
2620 return ftx232_dbus_write();
2623 static int icdi_jtag_init(void)
2627 /* Most Luminary eval boards support SWO trace output,
2628 * and should use this "luminary_icdi" layout.
2630 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2631 * to switch between JTAG and SWD, or switch the ft2232 UART
2632 * on the second MPSSE channel/interface (BDBUS)
2633 * between (i) the stellaris UART (on Luminary boards)
2634 * or (ii) SWO trace data (generic).
2636 * We come up in JTAG mode and may switch to SWD later (with
2637 * SWO/trace option if SWD is active).
2644 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2645 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2646 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2649 /* GPIOs on second channel/interface (UART) ... */
2650 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2651 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2652 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2657 nSRSTnOE
= ICDI_SRST
;
2659 low_direction
|= ICDI_JTAG_EN
| ICDI_DBG_ENn
;
2660 low_output
|= ICDI_JTAG_EN
;
2661 low_output
&= ~ICDI_DBG_ENn
;
2663 return ftx232_dbus_write();
2666 static int signalyzer_init(void)
2674 return ftx232_dbus_write();
2677 static int axm0432_jtag_init(void)
2680 low_direction
= 0x2b;
2682 /* initialize low byte for jtag */
2683 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2685 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2686 return ERROR_JTAG_INIT_FAILED
;
2689 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2692 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2694 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2698 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2703 high_direction
= 0x0c;
2705 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2706 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2708 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2712 high_output
|= nTRST
;
2715 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2717 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2721 high_output
|= nSRST
;
2724 /* initialize high byte for jtag */
2725 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2727 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2728 return ERROR_JTAG_INIT_FAILED
;
2734 static int redbee_init(void)
2737 low_direction
= 0x2b;
2739 /* initialize low byte for jtag */
2740 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2742 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2743 return ERROR_JTAG_INIT_FAILED
;
2747 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2749 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2752 high_direction
= 0x0c;
2754 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2755 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2757 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2761 high_output
|= nTRST
;
2764 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2766 LOG_ERROR("can't set nSRST to push-pull on redbee");
2770 high_output
|= nSRST
;
2773 /* initialize high byte for jtag */
2774 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2776 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2777 return ERROR_JTAG_INIT_FAILED
;
2783 static int jtagkey_init(void)
2786 low_direction
= 0x1b;
2788 /* initialize low byte for jtag */
2789 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2791 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2792 return ERROR_JTAG_INIT_FAILED
;
2795 if (strcmp(layout
->name
, "jtagkey") == 0)
2802 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2803 || (strcmp(layout
->name
, "oocdlink") == 0))
2812 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2817 high_direction
= 0x0f;
2819 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2820 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2822 high_output
|= nTRSTnOE
;
2823 high_output
&= ~nTRST
;
2827 high_output
&= ~nTRSTnOE
;
2828 high_output
|= nTRST
;
2831 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2833 high_output
&= ~nSRSTnOE
;
2834 high_output
|= nSRST
;
2838 high_output
|= nSRSTnOE
;
2839 high_output
&= ~nSRST
;
2842 /* initialize high byte for jtag */
2843 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2845 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2846 return ERROR_JTAG_INIT_FAILED
;
2852 static int olimex_jtag_init(void)
2855 low_direction
= 0x1b;
2857 /* initialize low byte for jtag */
2858 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2860 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2861 return ERROR_JTAG_INIT_FAILED
;
2867 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2870 high_direction
= 0x0f;
2872 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2873 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2875 high_output
|= nTRSTnOE
;
2876 high_output
&= ~nTRST
;
2880 high_output
&= ~nTRSTnOE
;
2881 high_output
|= nTRST
;
2884 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2886 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2890 high_output
&= ~nSRST
;
2893 /* turn red LED on */
2894 high_output
|= 0x08;
2896 /* initialize high byte for jtag */
2897 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2899 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2900 return ERROR_JTAG_INIT_FAILED
;
2906 static int flyswatter_init(void)
2909 low_direction
= 0xfb;
2911 /* initialize low byte for jtag */
2912 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2914 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2915 return ERROR_JTAG_INIT_FAILED
;
2919 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2921 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2924 high_direction
= 0x0c;
2926 /* turn red LED3 on, LED2 off */
2927 high_output
|= 0x08;
2929 /* initialize high byte for jtag */
2930 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2932 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2933 return ERROR_JTAG_INIT_FAILED
;
2939 static int minimodule_init(void)
2941 low_output
= 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18)
2942 low_direction
= 0xfb;//0xfb;
2944 /* initialize low byte for jtag */
2945 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2947 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2948 return ERROR_JTAG_INIT_FAILED
;
2955 high_direction
= 0x05;
2957 /* turn red LED3 on, LED2 off */
2958 //high_output |= 0x08;
2960 /* initialize high byte for jtag */
2961 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2963 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2964 return ERROR_JTAG_INIT_FAILED
;
2970 static int turtle_init(void)
2973 low_direction
= 0x5b;
2975 /* initialize low byte for jtag */
2976 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
2978 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2979 return ERROR_JTAG_INIT_FAILED
;
2985 high_direction
= 0x0C;
2987 /* initialize high byte for jtag */
2988 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
2990 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2991 return ERROR_JTAG_INIT_FAILED
;
2997 static int comstick_init(void)
3000 low_direction
= 0x0b;
3002 /* initialize low byte for jtag */
3003 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
3005 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3006 return ERROR_JTAG_INIT_FAILED
;
3010 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3012 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3015 high_direction
= 0x03;
3017 /* initialize high byte for jtag */
3018 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3020 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3021 return ERROR_JTAG_INIT_FAILED
;
3027 static int stm32stick_init(void)
3030 low_direction
= 0x8b;
3032 /* initialize low byte for jtag */
3033 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
3035 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3036 return ERROR_JTAG_INIT_FAILED
;
3040 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3042 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3045 high_direction
= 0x03;
3047 /* initialize high byte for jtag */
3048 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3050 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3051 return ERROR_JTAG_INIT_FAILED
;
3057 static int sheevaplug_init(void)
3060 low_direction
= 0x1b;
3062 /* initialize low byte for jtag */
3063 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
3065 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3066 return ERROR_JTAG_INIT_FAILED
;
3075 high_direction
= 0x0f;
3077 /* nTRST is always push-pull */
3078 high_output
&= ~nTRSTnOE
;
3079 high_output
|= nTRST
;
3081 /* nSRST is always open-drain */
3082 high_output
|= nSRSTnOE
;
3083 high_output
&= ~nSRST
;
3085 /* initialize high byte for jtag */
3086 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3088 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3089 return ERROR_JTAG_INIT_FAILED
;
3095 static int cortino_jtag_init(void)
3098 low_direction
= 0x1b;
3100 /* initialize low byte for jtag */
3101 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
3103 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3104 return ERROR_JTAG_INIT_FAILED
;
3108 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3110 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3113 high_direction
= 0x03;
3115 /* initialize high byte for jtag */
3116 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3118 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3119 return ERROR_JTAG_INIT_FAILED
;
3125 static int lisa_l_init(void)
3135 high_direction
= 0x18;
3137 /* initialize high byte for jtag */
3138 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3140 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3141 return ERROR_JTAG_INIT_FAILED
;
3144 return ftx232_dbus_write();
3147 static int flossjtag_init(void)
3157 high_direction
= 0x18;
3159 /* initialize high byte for jtag */
3160 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3162 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3163 return ERROR_JTAG_INIT_FAILED
;
3166 return ftx232_dbus_write();
3169 static int xds100v2_init(void)
3172 low_direction
= 0x7B;
3174 /* initialize low byte for jtag */
3175 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
3177 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3178 return ERROR_JTAG_INIT_FAILED
;
3182 nTRSTnOE
= 0x0; /* not output enable for nTRST */
3183 nSRST
= 0x00; /* TODO: SRST is not supported yet */
3184 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3187 high_direction
= 0x59;
3189 /* initialize high byte for jtag */
3190 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3192 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3193 return ERROR_JTAG_INIT_FAILED
;
3197 high_direction
= 0x59;
3199 /* initialize high byte for jtag */
3200 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
3202 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3203 return ERROR_JTAG_INIT_FAILED
;
3209 static void olimex_jtag_blink(void)
3211 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3212 * ACBUS3 is bit 3 of the GPIOH port
3214 high_output
^= 0x08;
3217 buffer_write(high_output
);
3218 buffer_write(high_direction
);
3221 static void flyswatter_jtag_blink(void)
3224 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3226 high_output
^= 0x0c;
3229 buffer_write(high_output
);
3230 buffer_write(high_direction
);
3233 static void turtle_jtag_blink(void)
3236 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3238 if (high_output
& 0x08)
3248 buffer_write(high_output
);
3249 buffer_write(high_direction
);
3252 static void lisa_l_blink(void)
3255 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3257 if (high_output
& 0x10)
3267 buffer_write(high_output
);
3268 buffer_write(high_direction
);
3271 static void flossjtag_blink(void)
3274 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3276 if (high_output
& 0x10)
3286 buffer_write(high_output
);
3287 buffer_write(high_direction
);
3290 static int ft2232_quit(void)
3292 #if BUILD_FT2232_FTD2XX == 1
3295 status
= FT_Close(ftdih
);
3296 #elif BUILD_FT2232_LIBFTDI == 1
3297 ftdi_usb_close(&ftdic
);
3299 ftdi_deinit(&ftdic
);
3302 free(ft2232_buffer
);
3303 ft2232_buffer
= NULL
;
3308 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3314 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3315 cp
= strchr(ft2232_device_desc
, 0);
3316 /* under Win32, the FTD2XX driver appends an "A" to the end
3317 * of the description, this examines the given desc
3318 * and creates the 'missing' _A or non_A variable. */
3319 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3320 /* it was, so make this the "A" version. */
3321 ft2232_device_desc_A
= ft2232_device_desc
;
3322 /* and *CREATE* the non-A version. */
3323 strcpy(buf
, ft2232_device_desc
);
3324 cp
= strchr(buf
, 0);
3326 ft2232_device_desc
= strdup(buf
);
3328 /* <space > A not defined
3330 sprintf(buf
, "%s A", ft2232_device_desc
);
3331 ft2232_device_desc_A
= strdup(buf
);
3336 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3342 COMMAND_HANDLER(ft2232_handle_serial_command
)
3346 ft2232_serial
= strdup(CMD_ARGV
[0]);
3350 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3356 COMMAND_HANDLER(ft2232_handle_layout_command
)
3358 if (CMD_ARGC
!= 1) {
3359 LOG_ERROR("Need exactly one argument to ft2232_layout");
3364 LOG_ERROR("already specified ft2232_layout %s",
3366 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3371 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3372 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3378 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3382 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3384 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3386 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3387 "(maximum is %d pairs)", MAX_USB_IDS
);
3388 CMD_ARGC
= MAX_USB_IDS
* 2;
3390 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3392 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3394 return ERROR_COMMAND_SYNTAX_ERROR
;
3395 /* remove the incomplete trailing id */
3400 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3402 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3403 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3407 * Explicitly terminate, in case there are multiples instances of
3410 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3415 COMMAND_HANDLER(ft2232_handle_latency_command
)
3419 ft2232_latency
= atoi(CMD_ARGV
[0]);
3423 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3429 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3433 /* 7 bits of either ones or zeros. */
3434 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3436 while (num_cycles
> 0)
3438 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3439 * at most 7 bits per invocation. Here we invoke it potentially
3442 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3444 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3446 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3447 retval
= ERROR_JTAG_QUEUE_FAILED
;
3452 /* there are no state transitions in this code, so omit state tracking */
3454 /* command "Clock Data to TMS/CS Pin (no Read)" */
3458 buffer_write(bitcount_per_command
- 1);
3460 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3465 num_cycles
-= bitcount_per_command
;
3471 /* ---------------------------------------------------------------------
3472 * Support for IceBear JTAG adapter from Section5:
3473 * http://section5.ch/icebear
3475 * Author: Sten, debian@sansys-electronic.com
3478 /* Icebear pin layout
3480 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3481 * GND GND | 4 3| n.c.
3482 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3483 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3484 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3485 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3486 * ADBUS2 TDO |14 13| GND GND
3488 * ADBUS0 O L TCK ACBUS0 GND
3489 * ADBUS1 O L TDI ACBUS1 GND
3490 * ADBUS2 I TDO ACBUS2 n.c.
3491 * ADBUS3 O H TMS ACBUS3 n.c.
3497 static int icebear_jtag_init(void) {
3498 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3499 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3503 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3504 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3505 low_direction
&= ~nTRST
; /* nTRST high impedance */
3508 low_direction
|= nTRST
;
3509 low_output
|= nTRST
;
3512 low_direction
|= nSRST
;
3513 low_output
|= nSRST
;
3515 /* initialize low byte for jtag */
3516 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
) {
3517 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3518 return ERROR_JTAG_INIT_FAILED
;
3522 high_direction
= 0x00;
3524 /* initialize high byte for jtag */
3525 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
) {
3526 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3527 return ERROR_JTAG_INIT_FAILED
;
3533 static void icebear_jtag_reset(int trst
, int srst
) {
3536 low_direction
|= nTRST
;
3537 low_output
&= ~nTRST
;
3539 else if (trst
== 0) {
3540 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3541 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3542 low_direction
&= ~nTRST
;
3544 low_output
|= nTRST
;
3548 low_output
&= ~nSRST
;
3550 else if (srst
== 0) {
3551 low_output
|= nSRST
;
3554 /* command "set data bits low byte" */
3556 buffer_write(low_output
);
3557 buffer_write(low_direction
);
3559 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3562 /* ---------------------------------------------------------------------
3563 * Support for Signalyzer H2 and Signalyzer H4
3564 * JTAG adapter from Xverve Technologies Inc.
3565 * http://www.signalyzer.com or http://www.xverve.com
3567 * Author: Oleg Seiljus, oleg@signalyzer.com
3569 static unsigned char signalyzer_h_side
;
3570 static unsigned int signalyzer_h_adapter_type
;
3572 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3574 #if BUILD_FT2232_FTD2XX == 1
3575 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3578 #define SIGNALYZER_COMMAND_ADDR 128
3579 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3581 #define SIGNALYZER_COMMAND_VERSION 0x41
3582 #define SIGNALYZER_COMMAND_RESET 0x42
3583 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3584 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3585 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3586 #define SIGNALYZER_COMMAND_LED_SET 0x53
3587 #define SIGNALYZER_COMMAND_ADC 0x54
3588 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3589 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3590 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3591 #define SIGNALYZER_COMMAND_I2C 0x58
3593 #define SIGNALYZER_CHAN_A 1
3594 #define SIGNALYZER_CHAN_B 2
3595 /* LEDS use channel C */
3596 #define SIGNALYZER_CHAN_C 4
3598 #define SIGNALYZER_LED_GREEN 1
3599 #define SIGNALYZER_LED_RED 2
3601 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3602 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3603 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3604 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3605 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3608 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3610 #if BUILD_FT2232_FTD2XX == 1
3611 return FT_WriteEE(ftdih
, address
, value
);
3612 #elif BUILD_FT2232_LIBFTDI == 1
3617 #if BUILD_FT2232_FTD2XX == 1
3618 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3620 return FT_ReadEE(ftdih
, address
, value
);
3624 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3625 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3627 unsigned char on_time
;
3628 unsigned char off_time
;
3630 if (on_time_ms
< 0xFFFF)
3631 on_time
= (unsigned char)(on_time_ms
/ 62);
3635 off_time
= (unsigned char)(off_time_ms
/ 62);
3637 #if BUILD_FT2232_FTD2XX == 1
3640 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3641 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3643 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3644 return ERROR_JTAG_DEVICE_ERROR
;
3647 if ((status
= signalyzer_h_ctrl_write(
3648 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3649 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3651 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3652 return ERROR_JTAG_DEVICE_ERROR
;
3655 if ((status
= signalyzer_h_ctrl_write(
3656 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3657 ((uint32_t)cycles
))) != FT_OK
)
3659 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3660 return ERROR_JTAG_DEVICE_ERROR
;
3663 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3664 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3666 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3667 return ERROR_JTAG_DEVICE_ERROR
;
3671 #elif BUILD_FT2232_LIBFTDI == 1
3674 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3675 ((uint32_t)(channel
<< 8) | led
))) < 0)
3677 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3678 ftdi_get_error_string(&ftdic
));
3679 return ERROR_JTAG_DEVICE_ERROR
;
3682 if ((retval
= signalyzer_h_ctrl_write(
3683 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3684 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3686 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3687 ftdi_get_error_string(&ftdic
));
3688 return ERROR_JTAG_DEVICE_ERROR
;
3691 if ((retval
= signalyzer_h_ctrl_write(
3692 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3693 (uint32_t)cycles
)) < 0)
3695 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3696 ftdi_get_error_string(&ftdic
));
3697 return ERROR_JTAG_DEVICE_ERROR
;
3700 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3701 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3703 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3704 ftdi_get_error_string(&ftdic
));
3705 return ERROR_JTAG_DEVICE_ERROR
;
3712 static int signalyzer_h_init(void)
3714 #if BUILD_FT2232_FTD2XX == 1
3721 uint16_t read_buf
[12] = { 0 };
3723 /* turn on center green led */
3724 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3725 0xFFFF, 0x00, 0x00);
3727 /* determine what channel config wants to open
3728 * TODO: change me... current implementation is made to work
3729 * with openocd description parsing.
3731 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3735 signalyzer_h_side
= *(end_of_desc
- 1);
3736 if (signalyzer_h_side
== 'B')
3737 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3739 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3743 LOG_ERROR("No Channel was specified");
3747 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3750 #if BUILD_FT2232_FTD2XX == 1
3751 /* read signalyzer versionining information */
3752 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3753 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3755 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3756 return ERROR_JTAG_DEVICE_ERROR
;
3759 for (i
= 0; i
< 10; i
++)
3761 if ((status
= signalyzer_h_ctrl_read(
3762 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3763 &read_buf
[i
])) != FT_OK
)
3765 LOG_ERROR("signalyzer_h_ctrl_read returned: %" PRIu32
,
3767 return ERROR_JTAG_DEVICE_ERROR
;
3771 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3772 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3773 read_buf
[4], read_buf
[5], read_buf
[6]);
3775 /* set gpio register */
3776 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3777 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3779 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3780 return ERROR_JTAG_DEVICE_ERROR
;
3783 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3786 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3787 return ERROR_JTAG_DEVICE_ERROR
;
3790 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3791 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3793 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3794 return ERROR_JTAG_DEVICE_ERROR
;
3797 /* read adapter type information */
3798 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3799 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3801 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3802 return ERROR_JTAG_DEVICE_ERROR
;
3805 if ((status
= signalyzer_h_ctrl_write(
3806 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3808 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3809 return ERROR_JTAG_DEVICE_ERROR
;
3812 if ((status
= signalyzer_h_ctrl_write(
3813 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3815 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3816 return ERROR_JTAG_DEVICE_ERROR
;
3819 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3820 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3822 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
, status
);
3823 return ERROR_JTAG_DEVICE_ERROR
;
3828 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3829 &read_buf
[0])) != FT_OK
)
3831 LOG_ERROR("signalyzer_h_ctrl_read returned: %" PRIu32
, status
);
3832 return ERROR_JTAG_DEVICE_ERROR
;
3835 if (read_buf
[0] != 0x0498)
3836 signalyzer_h_adapter_type
= 0x0000;
3839 for (i
= 0; i
< 4; i
++)
3841 if ((status
= signalyzer_h_ctrl_read(
3842 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3843 &read_buf
[i
])) != FT_OK
)
3845 LOG_ERROR("signalyzer_h_ctrl_read returned: %" PRIu32
,
3847 return ERROR_JTAG_DEVICE_ERROR
;
3851 signalyzer_h_adapter_type
= read_buf
[0];
3854 #elif BUILD_FT2232_LIBFTDI == 1
3855 /* currently libftdi does not allow reading individual eeprom
3856 * locations, therefore adapter type cannot be detected.
3857 * override with most common type
3859 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3862 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3864 /* ADAPTOR: EM_LT16_A */
3865 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3867 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3868 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3876 low_direction
= 0x1b;
3879 high_direction
= 0x0;
3881 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3883 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3884 low_output
&= ~nTRST
; /* nTRST = 0 */
3888 low_direction
|= nTRSTnOE
; /* nTRST output */
3889 low_output
|= nTRST
; /* nTRST = 1 */
3892 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3894 low_direction
|= nSRSTnOE
; /* nSRST output */
3895 low_output
|= nSRST
; /* nSRST = 1 */
3899 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3900 low_output
&= ~nSRST
; /* nSRST = 0 */
3903 #if BUILD_FT2232_FTD2XX == 1
3904 /* enable power to the module */
3905 if ((status
= signalyzer_h_ctrl_write(
3906 SIGNALYZER_DATA_BUFFER_ADDR
,
3907 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3910 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3912 return ERROR_JTAG_DEVICE_ERROR
;
3915 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3916 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3918 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3920 return ERROR_JTAG_DEVICE_ERROR
;
3923 /* set gpio mode register */
3924 if ((status
= signalyzer_h_ctrl_write(
3925 SIGNALYZER_DATA_BUFFER_ADDR
,
3926 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3928 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3930 return ERROR_JTAG_DEVICE_ERROR
;
3933 if ((status
= signalyzer_h_ctrl_write(
3934 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3937 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3939 return ERROR_JTAG_DEVICE_ERROR
;
3942 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3943 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3945 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3947 return ERROR_JTAG_DEVICE_ERROR
;
3950 /* set gpio register */
3951 if ((status
= signalyzer_h_ctrl_write(
3952 SIGNALYZER_DATA_BUFFER_ADDR
,
3953 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3955 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3957 return ERROR_JTAG_DEVICE_ERROR
;
3960 if ((status
= signalyzer_h_ctrl_write(
3961 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3964 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3966 return ERROR_JTAG_DEVICE_ERROR
;
3969 if ((status
= signalyzer_h_ctrl_write(
3970 SIGNALYZER_COMMAND_ADDR
,
3971 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3973 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
3975 return ERROR_JTAG_DEVICE_ERROR
;
3980 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3981 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3982 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3983 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3984 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3986 if (signalyzer_h_adapter_type
3987 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3988 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3989 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3990 else if (signalyzer_h_adapter_type
3991 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3992 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3993 "(ARM JTAG with PSU) detected. (HW: %2x).",
3994 (read_buf
[1] >> 8));
3995 else if (signalyzer_h_adapter_type
3996 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3997 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3998 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3999 else if (signalyzer_h_adapter_type
4000 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
4001 LOG_INFO("Signalyzer: EM-JTAG-P "
4002 "(Generic JTAG with PSU) detected. (HW: %2x).",
4003 (read_buf
[1] >> 8));
4011 low_direction
= 0x1b;
4014 high_direction
= 0x1f;
4016 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4018 high_output
|= nTRSTnOE
;
4019 high_output
&= ~nTRST
;
4023 high_output
&= ~nTRSTnOE
;
4024 high_output
|= nTRST
;
4027 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4029 high_output
&= ~nSRSTnOE
;
4030 high_output
|= nSRST
;
4034 high_output
|= nSRSTnOE
;
4035 high_output
&= ~nSRST
;
4038 #if BUILD_FT2232_FTD2XX == 1
4039 /* enable power to the module */
4040 if ((status
= signalyzer_h_ctrl_write(
4041 SIGNALYZER_DATA_BUFFER_ADDR
,
4042 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
4045 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4047 return ERROR_JTAG_DEVICE_ERROR
;
4050 if ((status
= signalyzer_h_ctrl_write(
4051 SIGNALYZER_COMMAND_ADDR
,
4052 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
4054 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4056 return ERROR_JTAG_DEVICE_ERROR
;
4059 /* set gpio mode register (IO_16 and IO_17 set as analog
4060 * inputs, other is gpio)
4062 if ((status
= signalyzer_h_ctrl_write(
4063 SIGNALYZER_DATA_BUFFER_ADDR
,
4064 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
4066 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4068 return ERROR_JTAG_DEVICE_ERROR
;
4071 if ((status
= signalyzer_h_ctrl_write(
4072 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
4075 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4077 return ERROR_JTAG_DEVICE_ERROR
;
4080 if ((status
= signalyzer_h_ctrl_write(
4081 SIGNALYZER_COMMAND_ADDR
,
4082 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
4084 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4086 return ERROR_JTAG_DEVICE_ERROR
;
4089 /* set gpio register (all inputs, for -P modules,
4090 * PSU will be turned off)
4092 if ((status
= signalyzer_h_ctrl_write(
4093 SIGNALYZER_DATA_BUFFER_ADDR
,
4094 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
4096 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4098 return ERROR_JTAG_DEVICE_ERROR
;
4101 if ((status
= signalyzer_h_ctrl_write(
4102 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
4105 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4107 return ERROR_JTAG_DEVICE_ERROR
;
4110 if ((status
= signalyzer_h_ctrl_write(
4111 SIGNALYZER_COMMAND_ADDR
,
4112 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
4114 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32
,
4116 return ERROR_JTAG_DEVICE_ERROR
;
4121 else if (signalyzer_h_adapter_type
== 0x0000)
4123 LOG_INFO("Signalyzer: No external modules were detected.");
4131 low_direction
= 0x1b;
4134 high_direction
= 0x0;
4136 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4138 low_direction
&= ~nTRSTnOE
; /* nTRST input */
4139 low_output
&= ~nTRST
; /* nTRST = 0 */
4143 low_direction
|= nTRSTnOE
; /* nTRST output */
4144 low_output
|= nTRST
; /* nTRST = 1 */
4147 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4149 low_direction
|= nSRSTnOE
; /* nSRST output */
4150 low_output
|= nSRST
; /* nSRST = 1 */
4154 low_direction
&= ~nSRSTnOE
; /* nSRST input */
4155 low_output
&= ~nSRST
; /* nSRST = 0 */
4160 LOG_ERROR("Unknown module type is detected: %.4x",
4161 signalyzer_h_adapter_type
);
4162 return ERROR_JTAG_DEVICE_ERROR
;
4165 /* initialize low byte of controller for jtag operation */
4166 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
4168 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4169 return ERROR_JTAG_INIT_FAILED
;
4172 #if BUILD_FT2232_FTD2XX == 1
4173 if (ftdi_device
== FT_DEVICE_2232H
)
4175 /* initialize high byte of controller for jtag operation */
4176 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
4178 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4179 return ERROR_JTAG_INIT_FAILED
;
4182 #elif BUILD_FT2232_LIBFTDI == 1
4183 if (ftdi_device
== TYPE_2232H
)
4185 /* initialize high byte of controller for jtag operation */
4186 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
4188 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4189 return ERROR_JTAG_INIT_FAILED
;
4196 static void signalyzer_h_reset(int trst
, int srst
)
4198 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4200 /* ADAPTOR: EM_LT16_A */
4201 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4205 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4206 /* switch to output pin (output is low) */
4207 low_direction
|= nTRSTnOE
;
4209 /* switch output low */
4210 low_output
&= ~nTRST
;
4214 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4215 /* switch to input pin (high-Z + internal
4216 * and external pullup) */
4217 low_direction
&= ~nTRSTnOE
;
4219 /* switch output high */
4220 low_output
|= nTRST
;
4225 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4226 /* switch output low */
4227 low_output
&= ~nSRST
;
4229 /* switch to output pin (output is low) */
4230 low_direction
|= nSRSTnOE
;
4234 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4235 /* switch output high */
4236 low_output
|= nSRST
;
4238 /* switch to input pin (high-Z) */
4239 low_direction
&= ~nSRSTnOE
;
4242 /* command "set data bits low byte" */
4244 buffer_write(low_output
);
4245 buffer_write(low_direction
);
4246 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4247 "low_direction: 0x%2.2x",
4248 trst
, srst
, low_output
, low_direction
);
4250 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4251 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4252 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4253 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4254 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4258 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4259 high_output
&= ~nTRSTnOE
;
4261 high_output
&= ~nTRST
;
4265 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4266 high_output
|= nTRSTnOE
;
4268 high_output
|= nTRST
;
4273 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4274 high_output
&= ~nSRST
;
4276 high_output
&= ~nSRSTnOE
;
4280 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4281 high_output
|= nSRST
;
4283 high_output
|= nSRSTnOE
;
4286 /* command "set data bits high byte" */
4288 buffer_write(high_output
);
4289 buffer_write(high_direction
);
4290 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4291 "high_direction: 0x%2.2x",
4292 trst
, srst
, high_output
, high_direction
);
4294 else if (signalyzer_h_adapter_type
== 0x0000)
4298 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4299 /* switch to output pin (output is low) */
4300 low_direction
|= nTRSTnOE
;
4302 /* switch output low */
4303 low_output
&= ~nTRST
;
4307 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4308 /* switch to input pin (high-Z + internal
4309 * and external pullup) */
4310 low_direction
&= ~nTRSTnOE
;
4312 /* switch output high */
4313 low_output
|= nTRST
;
4318 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4319 /* switch output low */
4320 low_output
&= ~nSRST
;
4322 /* switch to output pin (output is low) */
4323 low_direction
|= nSRSTnOE
;
4327 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4328 /* switch output high */
4329 low_output
|= nSRST
;
4331 /* switch to input pin (high-Z) */
4332 low_direction
&= ~nSRSTnOE
;
4335 /* command "set data bits low byte" */
4337 buffer_write(low_output
);
4338 buffer_write(low_direction
);
4339 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4340 "low_direction: 0x%2.2x",
4341 trst
, srst
, low_output
, low_direction
);
4345 static void signalyzer_h_blink(void)
4347 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4350 /********************************************************************
4351 * Support for KT-LINK
4352 * JTAG adapter from KRISTECH
4353 * http://www.kristech.eu
4354 *******************************************************************/
4355 static int ktlink_init(void)
4357 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4359 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4360 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4362 /* initialize low byte for jtag */
4363 if (ft2232_set_data_bits_low_byte(low_output
,low_direction
) != ERROR_OK
)
4365 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4366 return ERROR_JTAG_INIT_FAILED
;
4374 high_output
= 0x80; // turn LED on
4375 high_direction
= 0xFF; // all outputs
4377 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4379 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4380 high_output
|= nTRSTnOE
;
4381 high_output
&= ~nTRST
;
4383 high_output
&= ~nTRSTnOE
;
4384 high_output
|= nTRST
;
4387 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4388 high_output
&= ~nSRSTnOE
;
4389 high_output
|= nSRST
;
4391 high_output
|= nSRSTnOE
;
4392 high_output
&= ~nSRST
;
4395 /* initialize high byte for jtag */
4396 if (ft2232_set_data_bits_high_byte(high_output
,high_direction
) != ERROR_OK
)
4398 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4399 return ERROR_JTAG_INIT_FAILED
;
4405 static void ktlink_reset(int trst
, int srst
)
4407 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4410 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4411 high_output
&= ~nTRSTnOE
;
4413 high_output
&= ~nTRST
;
4414 } else if (trst
== 0) {
4415 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4416 high_output
|= nTRSTnOE
;
4418 high_output
|= nTRST
;
4422 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4423 high_output
&= ~nSRST
;
4425 high_output
&= ~nSRSTnOE
;
4426 } else if (srst
== 0) {
4427 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4428 high_output
|= nSRST
;
4430 high_output
|= nSRSTnOE
;
4433 buffer_write(0x82); // command "set data bits high byte"
4434 buffer_write(high_output
);
4435 buffer_write(high_direction
);
4436 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4439 static void ktlink_blink(void)
4441 /* LED connected to ACBUS7 */
4442 high_output
^= 0x80;
4444 buffer_write(0x82); // command "set data bits high byte"
4445 buffer_write(high_output
);
4446 buffer_write(high_direction
);
4449 static const struct command_registration ft2232_command_handlers
[] = {
4451 .name
= "ft2232_device_desc",
4452 .handler
= &ft2232_handle_device_desc_command
,
4453 .mode
= COMMAND_CONFIG
,
4454 .help
= "set the USB device description of the FTDI FT2232 device",
4455 .usage
= "description_string",
4458 .name
= "ft2232_serial",
4459 .handler
= &ft2232_handle_serial_command
,
4460 .mode
= COMMAND_CONFIG
,
4461 .help
= "set the serial number of the FTDI FT2232 device",
4462 .usage
= "serial_string",
4465 .name
= "ft2232_layout",
4466 .handler
= &ft2232_handle_layout_command
,
4467 .mode
= COMMAND_CONFIG
,
4468 .help
= "set the layout of the FT2232 GPIO signals used "
4469 "to control output-enables and reset signals",
4470 .usage
= "layout_name",
4473 .name
= "ft2232_vid_pid",
4474 .handler
= &ft2232_handle_vid_pid_command
,
4475 .mode
= COMMAND_CONFIG
,
4476 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4477 .usage
= "(vid pid)* ",
4480 .name
= "ft2232_latency",
4481 .handler
= &ft2232_handle_latency_command
,
4482 .mode
= COMMAND_CONFIG
,
4483 .help
= "set the FT2232 latency timer to a new value",
4486 COMMAND_REGISTRATION_DONE
4489 struct jtag_interface ft2232_interface
= {
4491 .supported
= DEBUG_CAP_TMS_SEQ
,
4492 .commands
= ft2232_command_handlers
,
4493 .transports
= jtag_only
,
4495 .init
= ft2232_init
,
4496 .quit
= ft2232_quit
,
4497 .speed
= ft2232_speed
,
4498 .speed_div
= ft2232_speed_div
,
4500 .execute_queue
= ft2232_execute_queue
,