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 <jtag/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 turtle_init(void);
181 static int comstick_init(void);
182 static int stm32stick_init(void);
183 static int axm0432_jtag_init(void);
184 static int sheevaplug_init(void);
185 static int icebear_jtag_init(void);
186 static int cortino_jtag_init(void);
187 static int signalyzer_init(void);
188 static int signalyzer_h_init(void);
189 static int ktlink_init(void);
190 static int redbee_init(void);
192 /* reset procedures for supported layouts */
193 static void ftx23_reset(int trst
, int srst
);
194 static void jtagkey_reset(int trst
, int srst
);
195 static void olimex_jtag_reset(int trst
, int srst
);
196 static void flyswatter_reset(int trst
, int srst
);
197 static void turtle_reset(int trst
, int srst
);
198 static void comstick_reset(int trst
, int srst
);
199 static void stm32stick_reset(int trst
, int srst
);
200 static void axm0432_jtag_reset(int trst
, int srst
);
201 static void sheevaplug_reset(int trst
, int srst
);
202 static void icebear_jtag_reset(int trst
, int srst
);
203 static void signalyzer_h_reset(int trst
, int srst
);
204 static void ktlink_reset(int trst
, int srst
);
205 static void redbee_reset(int trst
, int srst
);
207 /* blink procedures for layouts that support a blinking led */
208 static void olimex_jtag_blink(void);
209 static void flyswatter_jtag_blink(void);
210 static void turtle_jtag_blink(void);
211 static void signalyzer_h_blink(void);
212 static void ktlink_blink(void);
214 /* common transport support options */
216 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
218 static const struct ft2232_layout ft2232_layouts
[] =
221 .init
= usbjtag_init
,
222 .reset
= ftx23_reset
,
225 .init
= jtagkey_init
,
226 .reset
= jtagkey_reset
,
228 { .name
= "jtagkey_prototype_v1",
229 .init
= jtagkey_init
,
230 .reset
= jtagkey_reset
,
232 { .name
= "oocdlink",
233 .init
= jtagkey_init
,
234 .reset
= jtagkey_reset
,
236 { .name
= "signalyzer",
237 .init
= signalyzer_init
,
238 .reset
= ftx23_reset
,
240 { .name
= "evb_lm3s811",
241 .init
= lm3s811_jtag_init
,
242 .reset
= ftx23_reset
,
244 { .name
= "luminary_icdi",
245 .init
= icdi_jtag_init
,
246 .reset
= ftx23_reset
,
248 { .name
= "olimex-jtag",
249 .init
= olimex_jtag_init
,
250 .reset
= olimex_jtag_reset
,
251 .blink
= olimex_jtag_blink
253 { .name
= "flyswatter",
254 .init
= flyswatter_init
,
255 .reset
= flyswatter_reset
,
256 .blink
= flyswatter_jtag_blink
258 { .name
= "turtelizer2",
260 .reset
= turtle_reset
,
261 .blink
= turtle_jtag_blink
263 { .name
= "comstick",
264 .init
= comstick_init
,
265 .reset
= comstick_reset
,
267 { .name
= "stm32stick",
268 .init
= stm32stick_init
,
269 .reset
= stm32stick_reset
,
271 { .name
= "axm0432_jtag",
272 .init
= axm0432_jtag_init
,
273 .reset
= axm0432_jtag_reset
,
275 { .name
= "sheevaplug",
276 .init
= sheevaplug_init
,
277 .reset
= sheevaplug_reset
,
280 .init
= icebear_jtag_init
,
281 .reset
= icebear_jtag_reset
,
284 .init
= cortino_jtag_init
,
285 .reset
= comstick_reset
,
287 { .name
= "signalyzer-h",
288 .init
= signalyzer_h_init
,
289 .reset
= signalyzer_h_reset
,
290 .blink
= signalyzer_h_blink
294 .reset
= ktlink_reset
,
295 .blink
= ktlink_blink
297 { .name
= "redbee-econotag",
299 .reset
= redbee_reset
,
301 { .name
= "redbee-usb",
303 .reset
= redbee_reset
,
304 .channel
= INTERFACE_B
,
306 { .name
= NULL
, /* END OF TABLE */ },
309 /* bitmask used to drive nTRST; usually a GPIOLx signal */
310 static uint8_t nTRST
;
311 static uint8_t nTRSTnOE
;
312 /* bitmask used to drive nSRST; usually a GPIOLx signal */
313 static uint8_t nSRST
;
314 static uint8_t nSRSTnOE
;
316 /** the layout being used with this debug session */
317 static const struct ft2232_layout
*layout
;
319 /** default bitmask values ddriven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
320 static uint8_t low_output
= 0x0;
321 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
322 static uint8_t low_direction
= 0x0;
323 /** default value bitmask for CBUS GPIOH(0..4) */
324 static uint8_t high_output
= 0x0;
325 /** default direction bitmask for CBUS GPIOH(0..4) */
326 static uint8_t high_direction
= 0x0;
328 #if BUILD_FT2232_FTD2XX == 1
329 static FT_HANDLE ftdih
= NULL
;
330 static FT_DEVICE ftdi_device
= 0;
331 #elif BUILD_FT2232_LIBFTDI == 1
332 static struct ftdi_context ftdic
;
333 static enum ftdi_chip_type ftdi_device
;
336 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
337 static int require_send
;
339 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
341 "There is a significant difference between libftdi and libftd2xx. The latter
342 one allows to schedule up to 64*64 bytes of result data while libftdi fails
343 with more than 4*64. As a consequence, the FT2232 driver is forced to
344 perform around 16x more USB transactions for long command streams with TDO
345 capture when running with libftdi."
348 #define FT2232_BUFFER_SIZE 131072
349 a comment would have been nice.
352 #define FT2232_BUFFER_SIZE 131072
354 static uint8_t* ft2232_buffer
= NULL
;
355 static int ft2232_buffer_size
= 0;
356 static int ft2232_read_pointer
= 0;
357 static int ft2232_expect_read
= 0;
360 * Function buffer_write
361 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
362 * @param val is the byte to send.
364 static inline void buffer_write(uint8_t val
)
366 assert(ft2232_buffer
);
367 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
368 ft2232_buffer
[ft2232_buffer_size
++] = val
;
372 * Function buffer_read
373 * returns a byte from the byte buffer.
375 static inline uint8_t buffer_read(void)
377 assert(ft2232_buffer
);
378 assert(ft2232_read_pointer
< ft2232_buffer_size
);
379 return ft2232_buffer
[ft2232_read_pointer
++];
383 * Clocks out \a bit_count bits on the TMS line, starting with the least
384 * significant bit of tms_bits and progressing to more significant bits.
385 * Rigorous state transition logging is done here via tap_set_state().
387 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
388 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
389 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
390 * is often used for this, 0x4b.
392 * @param tms_bits Holds the sequence of bits to send.
393 * @param tms_count Tells how many bits in the sequence.
394 * @param tdi_bit A single bit to pass on to TDI before the first TCK
395 * cycle and held static for the duration of TMS clocking.
397 * See the MPSSE spec referenced above.
399 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
403 int tms_ndx
; /* bit index into tms_byte */
405 assert(tms_count
> 0);
407 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
408 mpsse_cmd
, tms_bits
, tms_count
);
410 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
412 bool bit
= tms_bits
& 1;
415 tms_byte
|= (1 << tms_ndx
);
417 /* always do state transitions in public view */
418 tap_set_state(tap_state_transition(tap_get_state(), bit
));
420 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
425 if (tms_ndx
== 7 || i
== tms_count
-1)
427 buffer_write(mpsse_cmd
);
428 buffer_write(tms_ndx
- 1);
430 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
431 TMS/CS and is held static for the duration of TMS/CS clocking.
433 buffer_write(tms_byte
| (tdi_bit
<< 7));
439 * Function get_tms_buffer_requirements
440 * returns what clock_tms() will consume if called with
443 static inline int get_tms_buffer_requirements(int bit_count
)
445 return ((bit_count
+ 6)/7) * 3;
449 * Function move_to_state
450 * moves the TAP controller from the current state to a
451 * \a goal_state through a path given by tap_get_tms_path(). State transition
452 * logging is performed by delegation to clock_tms().
454 * @param goal_state is the destination state for the move.
456 static void move_to_state(tap_state_t goal_state
)
458 tap_state_t start_state
= tap_get_state();
460 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
461 lookup of the required TMS pattern to move to this state from the
465 /* do the 2 lookups */
466 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
467 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
469 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
471 clock_tms(0x4b, tms_bits
, tms_count
, 0);
474 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
476 #if BUILD_FT2232_FTD2XX == 1
478 DWORD dw_bytes_written
;
479 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
481 *bytes_written
= dw_bytes_written
;
482 LOG_ERROR("FT_Write returned: %lu", status
);
483 return ERROR_JTAG_DEVICE_ERROR
;
487 *bytes_written
= dw_bytes_written
;
489 #elif BUILD_FT2232_LIBFTDI == 1
491 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
494 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
495 return ERROR_JTAG_DEVICE_ERROR
;
499 *bytes_written
= retval
;
503 if (*bytes_written
!= (uint32_t)size
)
505 return ERROR_JTAG_DEVICE_ERROR
;
511 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
513 #if BUILD_FT2232_FTD2XX == 1
519 while ((*bytes_read
< size
) && timeout
--)
521 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
522 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
525 LOG_ERROR("FT_Read returned: %lu", status
);
526 return ERROR_JTAG_DEVICE_ERROR
;
528 *bytes_read
+= dw_bytes_read
;
531 #elif BUILD_FT2232_LIBFTDI == 1
533 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
536 while ((*bytes_read
< size
) && timeout
--)
538 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
541 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
542 return ERROR_JTAG_DEVICE_ERROR
;
544 *bytes_read
+= retval
;
549 if (*bytes_read
< size
)
551 LOG_ERROR("couldn't read enough bytes from "
552 "FT2232 device (%i < %i)",
553 (unsigned)*bytes_read
,
555 return ERROR_JTAG_DEVICE_ERROR
;
561 static bool ft2232_device_is_highspeed(void)
563 #if BUILD_FT2232_FTD2XX == 1
564 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
565 #elif BUILD_FT2232_LIBFTDI == 1
566 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
571 * Commands that only apply to the FT2232H and FT4232H devices.
572 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
573 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
576 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
578 uint8_t buf
= enable
? 0x96 : 0x97;
579 LOG_DEBUG("%2.2x", buf
);
581 uint32_t bytes_written
;
584 if ((retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
586 LOG_ERROR("couldn't write command to %s adaptive clocking"
587 , enable
? "enable" : "disable");
595 * Enable/disable the clk divide by 5 of the 60MHz master clock.
596 * This result in a JTAG clock speed range of 91.553Hz-6MHz
597 * respective 457.763Hz-30MHz.
599 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
601 uint32_t bytes_written
;
602 uint8_t buf
= enable
? 0x8b : 0x8a;
604 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
606 LOG_ERROR("couldn't write command to %s clk divide by 5"
607 , enable
? "enable" : "disable");
608 return ERROR_JTAG_INIT_FAILED
;
610 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
611 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
616 static int ft2232_speed(int speed
)
620 uint32_t bytes_written
;
623 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
624 if (ft2232_device_is_highspeed())
625 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
626 else if (enable_adaptive_clocking
)
628 LOG_ERROR("ft2232 device %lu does not support RTCK"
629 , (long unsigned int)ftdi_device
);
633 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
636 buf
[0] = 0x86; /* command "set divisor" */
637 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
638 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
640 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
641 if ((retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
643 LOG_ERROR("couldn't set FT2232 TCK speed");
650 static int ft2232_speed_div(int speed
, int* khz
)
652 /* Take a look in the FT2232 manual,
653 * AN2232C-01 Command Processor for
654 * MPSSE and MCU Host Bus. Chapter 3.8 */
656 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
661 static int ft2232_khz(int khz
, int* jtag_speed
)
665 if (ft2232_device_is_highspeed())
667 *jtag_speed
= RTCK_SPEED
;
672 LOG_DEBUG("RCLK not supported");
677 /* Take a look in the FT2232 manual,
678 * AN2232C-01 Command Processor for
679 * MPSSE and MCU Host Bus. Chapter 3.8
681 * We will calc here with a multiplier
682 * of 10 for better rounding later. */
684 /* Calc speed, (ft2232_max_tck / khz) - 1 */
685 /* Use 65000 for better rounding */
686 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
688 /* Add 0.9 for rounding */
691 /* Calc real speed */
692 *jtag_speed
= *jtag_speed
/ 10;
694 /* Check if speed is greater than 0 */
700 /* Check max value */
701 if (*jtag_speed
> 0xFFFF)
703 *jtag_speed
= 0xFFFF;
709 static void ft2232_end_state(tap_state_t state
)
711 if (tap_is_state_stable(state
))
712 tap_set_end_state(state
);
715 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
720 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
722 int num_bytes
= (scan_size
+ 7) / 8;
723 int bits_left
= scan_size
;
726 while (num_bytes
-- > 1)
728 buffer
[cur_byte
++] = buffer_read();
732 buffer
[cur_byte
] = 0x0;
734 /* There is one more partial byte left from the clock data in/out instructions */
737 buffer
[cur_byte
] = buffer_read() >> 1;
739 /* 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 */
740 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
743 static void ft2232_debug_dump_buffer(void)
749 for (i
= 0; i
< ft2232_buffer_size
; i
++)
751 line_p
+= snprintf(line_p
, sizeof(line
) - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
754 LOG_DEBUG("%s", line
);
760 LOG_DEBUG("%s", line
);
763 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
765 struct jtag_command
* cmd
;
770 uint32_t bytes_written
= 0;
771 uint32_t bytes_read
= 0;
773 #ifdef _DEBUG_USB_IO_
774 struct timeval start
, inter
, inter2
, end
;
775 struct timeval d_inter
, d_inter2
, d_end
;
778 #ifdef _DEBUG_USB_COMMS_
779 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
780 ft2232_debug_dump_buffer();
783 #ifdef _DEBUG_USB_IO_
784 gettimeofday(&start
, NULL
);
787 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
789 LOG_ERROR("couldn't write MPSSE commands to FT2232");
793 #ifdef _DEBUG_USB_IO_
794 gettimeofday(&inter
, NULL
);
797 if (ft2232_expect_read
)
799 /* FIXME this "timeout" is never changed ... */
800 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
801 ft2232_buffer_size
= 0;
803 #ifdef _DEBUG_USB_IO_
804 gettimeofday(&inter2
, NULL
);
807 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
809 LOG_ERROR("couldn't read from FT2232");
813 #ifdef _DEBUG_USB_IO_
814 gettimeofday(&end
, NULL
);
816 timeval_subtract(&d_inter
, &inter
, &start
);
817 timeval_subtract(&d_inter2
, &inter2
, &start
);
818 timeval_subtract(&d_end
, &end
, &start
);
820 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
821 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
822 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
823 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
826 ft2232_buffer_size
= bytes_read
;
828 if (ft2232_expect_read
!= ft2232_buffer_size
)
830 LOG_ERROR("ft2232_expect_read (%i) != "
831 "ft2232_buffer_size (%i) "
835 LIBFTDI_READ_RETRY_COUNT
- timeout
);
836 ft2232_debug_dump_buffer();
841 #ifdef _DEBUG_USB_COMMS_
842 LOG_DEBUG("read buffer (%i retries): %i bytes",
843 LIBFTDI_READ_RETRY_COUNT
- timeout
,
845 ft2232_debug_dump_buffer();
849 ft2232_expect_read
= 0;
850 ft2232_read_pointer
= 0;
852 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
853 * that wasn't handled by a caller-provided error handler
863 type
= jtag_scan_type(cmd
->cmd
.scan
);
864 if (type
!= SCAN_OUT
)
866 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
867 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
868 ft2232_read_scan(type
, buffer
, scan_size
);
869 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
870 retval
= ERROR_JTAG_QUEUE_FAILED
;
882 ft2232_buffer_size
= 0;
888 * Function ft2232_add_pathmove
889 * moves the TAP controller from the current state to a new state through the
890 * given path, where path is an array of tap_state_t's.
892 * @param path is an array of tap_stat_t which gives the states to traverse through
893 * ending with the last state at path[num_states-1]
894 * @param num_states is the count of state steps to move through
896 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
900 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
904 /* this loop verifies that the path is legal and logs each state in the path */
907 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
909 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
911 /* command "Clock Data to TMS/CS Pin (no Read)" */
914 /* number of states remaining */
915 buffer_write(num_states_batch
- 1);
917 while (num_states_batch
--) {
918 /* either TMS=0 or TMS=1 must work ... */
919 if (tap_state_transition(tap_get_state(), false)
920 == path
[state_count
])
921 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
922 else if (tap_state_transition(tap_get_state(), true)
923 == path
[state_count
])
924 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
926 /* ... or else the caller goofed BADLY */
928 LOG_ERROR("BUG: %s -> %s isn't a valid "
929 "TAP state transition",
930 tap_state_name(tap_get_state()),
931 tap_state_name(path
[state_count
]));
935 tap_set_state(path
[state_count
]);
940 buffer_write(tms_byte
);
942 tap_set_end_state(tap_get_state());
945 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
947 int num_bytes
= (scan_size
+ 7) / 8;
948 int bits_left
= scan_size
;
954 if (tap_get_state() != TAP_DRSHIFT
)
956 move_to_state(TAP_DRSHIFT
);
961 if (tap_get_state() != TAP_IRSHIFT
)
963 move_to_state(TAP_IRSHIFT
);
967 /* add command for complete bytes */
968 while (num_bytes
> 1)
973 /* Clock Data Bytes In and Out LSB First */
975 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
977 else if (type
== SCAN_OUT
)
979 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
981 /* LOG_DEBUG("added TDI bytes (o)"); */
983 else if (type
== SCAN_IN
)
985 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
987 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
990 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
991 num_bytes
-= thisrun_bytes
;
993 buffer_write((uint8_t) (thisrun_bytes
- 1));
994 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
998 /* add complete bytes */
999 while (thisrun_bytes
-- > 0)
1001 buffer_write(buffer
[cur_byte
++]);
1005 else /* (type == SCAN_IN) */
1007 bits_left
-= 8 * (thisrun_bytes
);
1011 /* the most signifcant bit is scanned during TAP movement */
1012 if (type
!= SCAN_IN
)
1013 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1017 /* process remaining bits but the last one */
1020 if (type
== SCAN_IO
)
1022 /* Clock Data Bits In and Out LSB First */
1024 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1026 else if (type
== SCAN_OUT
)
1028 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1030 /* LOG_DEBUG("added TDI bits (o)"); */
1032 else if (type
== SCAN_IN
)
1034 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1036 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1039 buffer_write(bits_left
- 2);
1040 if (type
!= SCAN_IN
)
1041 buffer_write(buffer
[cur_byte
]);
1044 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1045 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1047 if (type
== SCAN_IO
)
1049 /* Clock Data Bits In and Out LSB First */
1051 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1053 else if (type
== SCAN_OUT
)
1055 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1057 /* LOG_DEBUG("added TDI bits (o)"); */
1059 else if (type
== SCAN_IN
)
1061 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1063 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1066 buffer_write(last_bit
);
1074 /* move from Shift-IR/DR to end state */
1075 if (type
!= SCAN_OUT
)
1077 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1078 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1081 /* Clock Data to TMS/CS Pin with Read */
1086 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1087 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1088 /* Clock Data to TMS/CS Pin (no Read) */
1092 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1093 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1096 if (tap_get_state() != tap_get_end_state())
1098 move_to_state(tap_get_end_state());
1102 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1104 int num_bytes
= (scan_size
+ 7) / 8;
1105 int bits_left
= scan_size
;
1108 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1109 uint8_t* receive_pointer
= receive_buffer
;
1110 uint32_t bytes_written
;
1111 uint32_t bytes_read
;
1113 int thisrun_read
= 0;
1117 LOG_ERROR("BUG: large IR scans are not supported");
1121 if (tap_get_state() != TAP_DRSHIFT
)
1123 move_to_state(TAP_DRSHIFT
);
1126 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1128 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1131 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1132 ft2232_buffer_size
, (int)bytes_written
);
1133 ft2232_buffer_size
= 0;
1135 /* add command for complete bytes */
1136 while (num_bytes
> 1)
1140 if (type
== SCAN_IO
)
1142 /* Clock Data Bytes In and Out LSB First */
1144 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1146 else if (type
== SCAN_OUT
)
1148 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1150 /* LOG_DEBUG("added TDI bytes (o)"); */
1152 else if (type
== SCAN_IN
)
1154 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1156 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1159 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1160 thisrun_read
= thisrun_bytes
;
1161 num_bytes
-= thisrun_bytes
;
1162 buffer_write((uint8_t) (thisrun_bytes
- 1));
1163 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1165 if (type
!= SCAN_IN
)
1167 /* add complete bytes */
1168 while (thisrun_bytes
-- > 0)
1170 buffer_write(buffer
[cur_byte
]);
1175 else /* (type == SCAN_IN) */
1177 bits_left
-= 8 * (thisrun_bytes
);
1180 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1182 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1185 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1187 (int)bytes_written
);
1188 ft2232_buffer_size
= 0;
1190 if (type
!= SCAN_OUT
)
1192 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1194 LOG_ERROR("couldn't read from FT2232");
1197 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1200 receive_pointer
+= bytes_read
;
1206 /* the most signifcant bit is scanned during TAP movement */
1207 if (type
!= SCAN_IN
)
1208 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1212 /* process remaining bits but the last one */
1215 if (type
== SCAN_IO
)
1217 /* Clock Data Bits In and Out LSB First */
1219 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1221 else if (type
== SCAN_OUT
)
1223 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1225 /* LOG_DEBUG("added TDI bits (o)"); */
1227 else if (type
== SCAN_IN
)
1229 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1231 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1233 buffer_write(bits_left
- 2);
1234 if (type
!= SCAN_IN
)
1235 buffer_write(buffer
[cur_byte
]);
1237 if (type
!= SCAN_OUT
)
1241 if (tap_get_end_state() == TAP_DRSHIFT
)
1243 if (type
== SCAN_IO
)
1245 /* Clock Data Bits In and Out LSB First */
1247 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1249 else if (type
== SCAN_OUT
)
1251 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1253 /* LOG_DEBUG("added TDI bits (o)"); */
1255 else if (type
== SCAN_IN
)
1257 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1259 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1262 buffer_write(last_bit
);
1266 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1267 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1270 /* move from Shift-IR/DR to end state */
1271 if (type
!= SCAN_OUT
)
1273 /* Clock Data to TMS/CS Pin with Read */
1275 /* LOG_DEBUG("added TMS scan (read)"); */
1279 /* Clock Data to TMS/CS Pin (no Read) */
1281 /* LOG_DEBUG("added TMS scan (no read)"); */
1284 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1285 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1288 if (type
!= SCAN_OUT
)
1291 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1293 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1296 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1298 (int)bytes_written
);
1299 ft2232_buffer_size
= 0;
1301 if (type
!= SCAN_OUT
)
1303 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1305 LOG_ERROR("couldn't read from FT2232");
1308 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1311 receive_pointer
+= bytes_read
;
1317 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1319 int predicted_size
= 3;
1320 int num_bytes
= (scan_size
- 1) / 8;
1322 if (tap_get_state() != TAP_DRSHIFT
)
1323 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1325 if (type
== SCAN_IN
) /* only from device to host */
1327 /* complete bytes */
1328 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1330 /* remaining bits - 1 (up to 7) */
1331 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1333 else /* host to device, or bidirectional */
1335 /* complete bytes */
1336 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1338 /* remaining bits -1 (up to 7) */
1339 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1342 return predicted_size
;
1345 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1347 int predicted_size
= 0;
1349 if (type
!= SCAN_OUT
)
1351 /* complete bytes */
1352 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1354 /* remaining bits - 1 */
1355 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1357 /* last bit (from TMS scan) */
1358 predicted_size
+= 1;
1361 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1363 return predicted_size
;
1366 /* semi-generic FT2232/FT4232 reset code */
1367 static void ftx23_reset(int trst
, int srst
)
1369 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1372 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1373 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1375 low_output
&= ~nTRST
; /* switch output low */
1379 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1380 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1382 low_output
|= nTRST
; /* switch output high */
1387 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1388 low_output
&= ~nSRST
; /* switch output low */
1390 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1394 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1395 low_output
|= nSRST
; /* switch output high */
1397 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1400 /* command "set data bits low byte" */
1402 buffer_write(low_output
);
1403 buffer_write(low_direction
);
1406 static void jtagkey_reset(int trst
, int srst
)
1408 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1411 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1412 high_output
&= ~nTRSTnOE
;
1414 high_output
&= ~nTRST
;
1418 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1419 high_output
|= nTRSTnOE
;
1421 high_output
|= nTRST
;
1426 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1427 high_output
&= ~nSRST
;
1429 high_output
&= ~nSRSTnOE
;
1433 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1434 high_output
|= nSRST
;
1436 high_output
|= nSRSTnOE
;
1439 /* command "set data bits high byte" */
1441 buffer_write(high_output
);
1442 buffer_write(high_direction
);
1443 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1447 static void olimex_jtag_reset(int trst
, int srst
)
1449 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1452 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1453 high_output
&= ~nTRSTnOE
;
1455 high_output
&= ~nTRST
;
1459 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1460 high_output
|= nTRSTnOE
;
1462 high_output
|= nTRST
;
1467 high_output
|= nSRST
;
1471 high_output
&= ~nSRST
;
1474 /* command "set data bits high byte" */
1476 buffer_write(high_output
);
1477 buffer_write(high_direction
);
1478 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1482 static void axm0432_jtag_reset(int trst
, int srst
)
1486 tap_set_state(TAP_RESET
);
1487 high_output
&= ~nTRST
;
1491 high_output
|= nTRST
;
1496 high_output
&= ~nSRST
;
1500 high_output
|= nSRST
;
1503 /* command "set data bits low byte" */
1505 buffer_write(high_output
);
1506 buffer_write(high_direction
);
1507 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1511 static void flyswatter_reset(int trst
, int srst
)
1515 low_output
&= ~nTRST
;
1519 low_output
|= nTRST
;
1524 low_output
|= nSRST
;
1528 low_output
&= ~nSRST
;
1531 /* command "set data bits low byte" */
1533 buffer_write(low_output
);
1534 buffer_write(low_direction
);
1535 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1538 static void turtle_reset(int trst
, int srst
)
1544 low_output
|= nSRST
;
1548 low_output
&= ~nSRST
;
1551 /* command "set data bits low byte" */
1553 buffer_write(low_output
);
1554 buffer_write(low_direction
);
1555 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1558 static void comstick_reset(int trst
, int srst
)
1562 high_output
&= ~nTRST
;
1566 high_output
|= nTRST
;
1571 high_output
&= ~nSRST
;
1575 high_output
|= nSRST
;
1578 /* command "set data bits high byte" */
1580 buffer_write(high_output
);
1581 buffer_write(high_direction
);
1582 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1586 static void stm32stick_reset(int trst
, int srst
)
1590 high_output
&= ~nTRST
;
1594 high_output
|= nTRST
;
1599 low_output
&= ~nSRST
;
1603 low_output
|= nSRST
;
1606 /* command "set data bits low byte" */
1608 buffer_write(low_output
);
1609 buffer_write(low_direction
);
1611 /* command "set data bits high byte" */
1613 buffer_write(high_output
);
1614 buffer_write(high_direction
);
1615 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1619 static void sheevaplug_reset(int trst
, int srst
)
1622 high_output
&= ~nTRST
;
1624 high_output
|= nTRST
;
1627 high_output
&= ~nSRSTnOE
;
1629 high_output
|= nSRSTnOE
;
1631 /* command "set data bits high byte" */
1633 buffer_write(high_output
);
1634 buffer_write(high_direction
);
1635 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1638 static void redbee_reset(int trst
, int srst
)
1642 tap_set_state(TAP_RESET
);
1643 high_output
&= ~nTRST
;
1647 high_output
|= nTRST
;
1652 high_output
&= ~nSRST
;
1656 high_output
|= nSRST
;
1659 /* command "set data bits low byte" */
1661 buffer_write(high_output
);
1662 buffer_write(high_direction
);
1663 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1664 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1668 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1672 int predicted_size
= 0;
1675 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1676 cmd
->cmd
.runtest
->num_cycles
,
1677 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1679 /* only send the maximum buffer size that FT2232C can handle */
1681 if (tap_get_state() != TAP_IDLE
)
1682 predicted_size
+= 3;
1683 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1684 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1685 predicted_size
+= 3;
1686 if (tap_get_end_state() != TAP_IDLE
)
1687 predicted_size
+= 3;
1688 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1690 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1691 retval
= ERROR_JTAG_QUEUE_FAILED
;
1695 if (tap_get_state() != TAP_IDLE
)
1697 move_to_state(TAP_IDLE
);
1700 i
= cmd
->cmd
.runtest
->num_cycles
;
1703 /* there are no state transitions in this code, so omit state tracking */
1705 /* command "Clock Data to TMS/CS Pin (no Read)" */
1709 buffer_write((i
> 7) ? 6 : (i
- 1));
1714 i
-= (i
> 7) ? 7 : i
;
1715 /* LOG_DEBUG("added TMS scan (no read)"); */
1718 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1720 if (tap_get_state() != tap_get_end_state())
1722 move_to_state(tap_get_end_state());
1726 DEBUG_JTAG_IO("runtest: %i, end in %s",
1727 cmd
->cmd
.runtest
->num_cycles
,
1728 tap_state_name(tap_get_end_state()));
1732 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1734 int predicted_size
= 0;
1735 int retval
= ERROR_OK
;
1737 DEBUG_JTAG_IO("statemove end in %s",
1738 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1740 /* only send the maximum buffer size that FT2232C can handle */
1742 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1744 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1745 retval
= ERROR_JTAG_QUEUE_FAILED
;
1749 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1751 /* For TAP_RESET, ignore the current recorded state. It's often
1752 * wrong at server startup, and this transation is critical whenever
1755 if (tap_get_end_state() == TAP_RESET
) {
1756 clock_tms(0x4b, 0xff, 5, 0);
1759 /* shortest-path move to desired end state */
1760 } else if (tap_get_state() != tap_get_end_state())
1762 move_to_state(tap_get_end_state());
1770 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1771 * (or SWD) state machine.
1773 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1775 int retval
= ERROR_OK
;
1776 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1777 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1780 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1782 /* only send the maximum buffer size that FT2232C can handle */
1783 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1784 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1785 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1786 retval
= ERROR_JTAG_QUEUE_FAILED
;
1792 /* Shift out in batches of at most 6 bits; there's a report of an
1793 * FT2232 bug in this area, where shifting exactly 7 bits can make
1794 * problems with TMS signaling for the last clock cycle:
1796 * http://developer.intra2net.com/mailarchive/html/
1797 * libftdi/2009/msg00292.html
1799 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1801 * Note that pathmoves in JTAG are not often seven bits, so that
1802 * isn't a particularly likely situation outside of "special"
1803 * signaling such as switching between JTAG and SWD modes.
1806 if (num_bits
<= 6) {
1808 buffer_write(num_bits
- 1);
1809 buffer_write(*bits
& 0x3f);
1813 /* Yes, this is lazy ... we COULD shift out more data
1814 * bits per operation, but doing it in nybbles is easy
1818 buffer_write(*bits
& 0xf);
1821 count
= (num_bits
> 4) ? 4 : num_bits
;
1824 buffer_write(count
- 1);
1825 buffer_write((*bits
>> 4) & 0xf);
1835 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1837 int predicted_size
= 0;
1838 int retval
= ERROR_OK
;
1840 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1841 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1843 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1844 tap_state_name(tap_get_state()),
1845 tap_state_name(path
[num_states
-1]));
1847 /* only send the maximum buffer size that FT2232C can handle */
1848 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1849 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1851 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1852 retval
= ERROR_JTAG_QUEUE_FAILED
;
1858 ft2232_add_pathmove(path
, num_states
);
1864 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1867 int scan_size
; /* size of IR or DR scan */
1868 int predicted_size
= 0;
1869 int retval
= ERROR_OK
;
1871 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1873 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1875 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1877 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1878 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1880 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1881 /* unsent commands before this */
1882 if (first_unsent
!= cmd
)
1883 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1884 retval
= ERROR_JTAG_QUEUE_FAILED
;
1886 /* current command */
1887 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1888 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1890 first_unsent
= cmd
->next
;
1895 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1897 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1900 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1901 retval
= ERROR_JTAG_QUEUE_FAILED
;
1905 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1906 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1907 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1908 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1912 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1913 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1914 tap_state_name(tap_get_end_state()));
1919 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1922 int predicted_size
= 0;
1925 DEBUG_JTAG_IO("reset trst: %i srst %i",
1926 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1928 /* only send the maximum buffer size that FT2232C can handle */
1930 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1932 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1933 retval
= ERROR_JTAG_QUEUE_FAILED
;
1938 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1940 tap_set_state(TAP_RESET
);
1943 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1946 DEBUG_JTAG_IO("trst: %i, srst: %i",
1947 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1951 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1956 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1958 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1959 retval
= ERROR_JTAG_QUEUE_FAILED
;
1960 first_unsent
= cmd
->next
;
1961 jtag_sleep(cmd
->cmd
.sleep
->us
);
1962 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1964 tap_state_name(tap_get_state()));
1968 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1973 /* this is only allowed while in a stable state. A check for a stable
1974 * state was done in jtag_add_clocks()
1976 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1977 retval
= ERROR_JTAG_QUEUE_FAILED
;
1978 DEBUG_JTAG_IO("clocks %i while in %s",
1979 cmd
->cmd
.stableclocks
->num_cycles
,
1980 tap_state_name(tap_get_state()));
1984 static int ft2232_execute_command(struct jtag_command
*cmd
)
1990 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1991 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1992 case JTAG_TLR_RESET
: retval
= ft2232_execute_statemove(cmd
); break;
1993 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1994 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1995 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1996 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1998 retval
= ft2232_execute_tms(cmd
);
2001 LOG_ERROR("BUG: unknown JTAG command type encountered");
2002 retval
= ERROR_JTAG_QUEUE_FAILED
;
2008 static int ft2232_execute_queue(void)
2010 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
2013 first_unsent
= cmd
; /* next command that has to be sent */
2016 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2017 * that wasn't handled by a caller-provided error handler
2021 ft2232_buffer_size
= 0;
2022 ft2232_expect_read
= 0;
2024 /* blink, if the current layout has that feature */
2030 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2031 retval
= ERROR_JTAG_QUEUE_FAILED
;
2032 /* Start reading input before FT2232 TX buffer fills up */
2034 if (ft2232_expect_read
> 256)
2036 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2037 retval
= ERROR_JTAG_QUEUE_FAILED
;
2042 if (require_send
> 0)
2043 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2044 retval
= ERROR_JTAG_QUEUE_FAILED
;
2049 #if BUILD_FT2232_FTD2XX == 1
2050 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2054 char SerialNumber
[16];
2055 char Description
[64];
2056 DWORD openex_flags
= 0;
2057 char* openex_string
= NULL
;
2058 uint8_t latency_timer
;
2060 if (layout
== NULL
) {
2061 LOG_WARNING("No ft2232 layout specified'");
2062 return ERROR_JTAG_INIT_FAILED
;
2065 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2068 /* Add non-standard Vid/Pid to the linux driver */
2069 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2071 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2075 if (ft2232_device_desc
&& ft2232_serial
)
2077 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2078 ft2232_device_desc
= NULL
;
2081 if (ft2232_device_desc
)
2083 openex_string
= ft2232_device_desc
;
2084 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2086 else if (ft2232_serial
)
2088 openex_string
= ft2232_serial
;
2089 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2093 LOG_ERROR("neither device description nor serial number specified");
2094 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2096 return ERROR_JTAG_INIT_FAILED
;
2099 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2100 if (status
!= FT_OK
) {
2101 /* under Win32, the FTD2XX driver appends an "A" to the end
2102 * of the description, if we tried by the desc, then
2103 * try by the alternate "A" description. */
2104 if (openex_string
== ft2232_device_desc
) {
2105 /* Try the alternate method. */
2106 openex_string
= ft2232_device_desc_A
;
2107 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2108 if (status
== FT_OK
) {
2109 /* yea, the "alternate" method worked! */
2111 /* drat, give the user a meaningfull message.
2112 * telling the use we tried *BOTH* methods. */
2113 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2115 ft2232_device_desc_A
);
2120 if (status
!= FT_OK
)
2126 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2128 return ERROR_JTAG_INIT_FAILED
;
2130 LOG_ERROR("unable to open ftdi device: %lu", status
);
2131 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2132 if (status
== FT_OK
)
2134 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2137 for (i
= 0; i
< num_devices
; i
++)
2138 desc_array
[i
] = malloc(64);
2140 desc_array
[num_devices
] = NULL
;
2142 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2144 if (status
== FT_OK
)
2146 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2147 for (i
= 0; i
< num_devices
; i
++)
2148 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2151 for (i
= 0; i
< num_devices
; i
++)
2152 free(desc_array
[i
]);
2158 LOG_ERROR("ListDevices: NONE\n");
2160 return ERROR_JTAG_INIT_FAILED
;
2163 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2165 LOG_ERROR("unable to set latency timer: %lu", status
);
2166 return ERROR_JTAG_INIT_FAILED
;
2169 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2171 LOG_ERROR("unable to get latency timer: %lu", status
);
2172 return ERROR_JTAG_INIT_FAILED
;
2176 LOG_DEBUG("current latency timer: %i", latency_timer
);
2179 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2181 LOG_ERROR("unable to set timeouts: %lu", status
);
2182 return ERROR_JTAG_INIT_FAILED
;
2185 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2187 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2188 return ERROR_JTAG_INIT_FAILED
;
2191 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2193 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2194 return ERROR_JTAG_INIT_FAILED
;
2198 static const char* type_str
[] =
2199 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2200 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2201 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2202 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2203 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2204 LOG_INFO("deviceID: %lu", deviceID
);
2205 LOG_INFO("SerialNumber: %s", SerialNumber
);
2206 LOG_INFO("Description: %s", Description
);
2212 static int ft2232_purge_ftd2xx(void)
2216 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2218 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2219 return ERROR_JTAG_INIT_FAILED
;
2225 #endif /* BUILD_FT2232_FTD2XX == 1 */
2227 #if BUILD_FT2232_LIBFTDI == 1
2228 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2230 uint8_t latency_timer
;
2232 if (layout
== NULL
) {
2233 LOG_WARNING("No ft2232 layout specified'");
2234 return ERROR_JTAG_INIT_FAILED
;
2237 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2238 layout
->name
, vid
, pid
);
2240 if (ftdi_init(&ftdic
) < 0)
2241 return ERROR_JTAG_INIT_FAILED
;
2243 /* default to INTERFACE_A */
2244 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2246 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2248 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2249 return ERROR_JTAG_INIT_FAILED
;
2252 /* context, vendor id, product id */
2253 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2257 LOG_WARNING("unable to open ftdi device (trying more): %s",
2260 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2262 return ERROR_JTAG_INIT_FAILED
;
2265 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2266 if (ftdi_usb_reset(&ftdic
) < 0)
2268 LOG_ERROR("unable to reset ftdi device");
2269 return ERROR_JTAG_INIT_FAILED
;
2272 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2274 LOG_ERROR("unable to set latency timer");
2275 return ERROR_JTAG_INIT_FAILED
;
2278 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2280 LOG_ERROR("unable to get latency timer");
2281 return ERROR_JTAG_INIT_FAILED
;
2285 LOG_DEBUG("current latency timer: %i", latency_timer
);
2288 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2290 ftdi_device
= ftdic
.type
;
2291 static const char* type_str
[] =
2292 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2293 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2294 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2295 ? ftdi_device
: no_of_known_types
;
2296 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2300 static int ft2232_purge_libftdi(void)
2302 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2304 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2305 return ERROR_JTAG_INIT_FAILED
;
2311 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2313 static int ft2232_init(void)
2317 uint32_t bytes_written
;
2319 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2321 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2325 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2328 if (layout
== NULL
) {
2329 LOG_WARNING("No ft2232 layout specified'");
2330 return ERROR_JTAG_INIT_FAILED
;
2333 for (int i
= 0; 1; i
++)
2336 * "more indicates that there are more IDs to try, so we should
2337 * not print an error for an ID mismatch (but for anything
2340 * try_more indicates that the error code returned indicates an
2341 * ID mismatch (and nothing else) and that we should proceeed
2342 * with the next ID pair.
2344 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2347 #if BUILD_FT2232_FTD2XX == 1
2348 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2350 #elif BUILD_FT2232_LIBFTDI == 1
2351 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2352 more
, &try_more
, layout
->channel
);
2356 if (!more
|| !try_more
)
2360 ft2232_buffer_size
= 0;
2361 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2363 if (layout
->init() != ERROR_OK
)
2364 return ERROR_JTAG_INIT_FAILED
;
2366 if (ft2232_device_is_highspeed())
2368 #ifndef BUILD_FT2232_HIGHSPEED
2369 #if BUILD_FT2232_FTD2XX == 1
2370 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2371 #elif BUILD_FT2232_LIBFTDI == 1
2372 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2375 /* make sure the legacy mode is disabled */
2376 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2377 return ERROR_JTAG_INIT_FAILED
;
2380 ft2232_speed(jtag_get_speed());
2382 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2383 if ((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
)
2385 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2386 return ERROR_JTAG_INIT_FAILED
;
2389 #if BUILD_FT2232_FTD2XX == 1
2390 return ft2232_purge_ftd2xx();
2391 #elif BUILD_FT2232_LIBFTDI == 1
2392 return ft2232_purge_libftdi();
2398 /** Updates defaults for DBUS signals: the four JTAG signals
2399 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2401 static inline void ftx232_dbus_init(void)
2404 low_direction
= 0x0b;
2407 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2408 * the four GPIOL signals. Initialization covers value and direction,
2409 * as customized for each layout.
2411 static int ftx232_dbus_write(void)
2414 uint32_t bytes_written
;
2416 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2417 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2419 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2420 low_output
&= ~nTRST
; /* nTRST = 0 */
2424 low_direction
|= nTRSTnOE
; /* nTRST output */
2425 low_output
|= nTRST
; /* nTRST = 1 */
2428 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2430 low_direction
|= nSRSTnOE
; /* nSRST output */
2431 low_output
|= nSRST
; /* nSRST = 1 */
2435 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2436 low_output
&= ~nSRST
; /* nSRST = 0 */
2439 /* initialize low byte for jtag */
2440 buf
[0] = 0x80; /* command "set data bits low byte" */
2441 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2442 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2443 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2445 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2447 LOG_ERROR("couldn't initialize FT2232 DBUS");
2448 return ERROR_JTAG_INIT_FAILED
;
2454 static int usbjtag_init(void)
2457 * NOTE: This is now _specific_ to the "usbjtag" layout.
2458 * Don't try cram any more layouts into this.
2467 return ftx232_dbus_write();
2470 static int lm3s811_jtag_init(void)
2474 /* There are multiple revisions of LM3S811 eval boards:
2475 * - Rev B (and older?) boards have no SWO trace support.
2476 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2477 * they should use the "luminary_icdi" layout instead.
2484 low_direction
= 0x8b;
2486 return ftx232_dbus_write();
2489 static int icdi_jtag_init(void)
2493 /* Most Luminary eval boards support SWO trace output,
2494 * and should use this "luminary_icdi" layout.
2501 low_direction
= 0xcb;
2503 return ftx232_dbus_write();
2506 static int signalyzer_init(void)
2514 return ftx232_dbus_write();
2517 static int axm0432_jtag_init(void)
2520 uint32_t bytes_written
;
2523 low_direction
= 0x2b;
2525 /* initialize low byte for jtag */
2526 buf
[0] = 0x80; /* command "set data bits low byte" */
2527 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2528 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2529 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2531 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2533 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2534 return ERROR_JTAG_INIT_FAILED
;
2537 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2540 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2542 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2546 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2551 high_direction
= 0x0c;
2553 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2554 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2556 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2560 high_output
|= nTRST
;
2563 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2565 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2569 high_output
|= nSRST
;
2572 /* initialize high port */
2573 buf
[0] = 0x82; /* command "set data bits high byte" */
2574 buf
[1] = high_output
; /* value */
2575 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2576 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2578 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2580 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2581 return ERROR_JTAG_INIT_FAILED
;
2587 static int redbee_init(void)
2590 uint32_t bytes_written
;
2593 low_direction
= 0x2b;
2595 /* initialize low byte for jtag */
2596 /* command "set data bits low byte" */
2598 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2599 buf
[2] = low_direction
;
2600 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2601 buf
[1] = low_output
;
2602 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2604 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2606 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2607 return ERROR_JTAG_INIT_FAILED
;
2611 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2613 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2616 high_direction
= 0x0c;
2618 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2619 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2621 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2625 high_output
|= nTRST
;
2628 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2630 LOG_ERROR("can't set nSRST to push-pull on redbee");
2634 high_output
|= nSRST
;
2637 /* initialize high port */
2638 buf
[0] = 0x82; /* command "set data bits high byte" */
2639 buf
[1] = high_output
; /* value */
2640 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2641 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2643 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2645 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2646 return ERROR_JTAG_INIT_FAILED
;
2652 static int jtagkey_init(void)
2655 uint32_t bytes_written
;
2658 low_direction
= 0x1b;
2660 /* initialize low byte for jtag */
2661 buf
[0] = 0x80; /* command "set data bits low byte" */
2662 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2663 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2664 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2666 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2668 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2669 return ERROR_JTAG_INIT_FAILED
;
2672 if (strcmp(layout
->name
, "jtagkey") == 0)
2679 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2680 || (strcmp(layout
->name
, "oocdlink") == 0))
2689 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2694 high_direction
= 0x0f;
2696 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2697 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2699 high_output
|= nTRSTnOE
;
2700 high_output
&= ~nTRST
;
2704 high_output
&= ~nTRSTnOE
;
2705 high_output
|= nTRST
;
2708 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2710 high_output
&= ~nSRSTnOE
;
2711 high_output
|= nSRST
;
2715 high_output
|= nSRSTnOE
;
2716 high_output
&= ~nSRST
;
2719 /* initialize high port */
2720 buf
[0] = 0x82; /* command "set data bits high byte" */
2721 buf
[1] = high_output
; /* value */
2722 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2723 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2725 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2727 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2728 return ERROR_JTAG_INIT_FAILED
;
2734 static int olimex_jtag_init(void)
2737 uint32_t bytes_written
;
2740 low_direction
= 0x1b;
2742 /* initialize low byte for jtag */
2743 buf
[0] = 0x80; /* command "set data bits low byte" */
2744 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2745 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2746 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2748 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2750 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2751 return ERROR_JTAG_INIT_FAILED
;
2757 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2760 high_direction
= 0x0f;
2762 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2763 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2765 high_output
|= nTRSTnOE
;
2766 high_output
&= ~nTRST
;
2770 high_output
&= ~nTRSTnOE
;
2771 high_output
|= nTRST
;
2774 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2776 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2780 high_output
&= ~nSRST
;
2783 /* turn red LED on */
2784 high_output
|= 0x08;
2786 /* initialize high port */
2787 buf
[0] = 0x82; /* command "set data bits high byte" */
2788 buf
[1] = high_output
; /* value */
2789 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2790 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2792 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2794 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2795 return ERROR_JTAG_INIT_FAILED
;
2801 static int flyswatter_init(void)
2804 uint32_t bytes_written
;
2807 low_direction
= 0xfb;
2809 /* initialize low byte for jtag */
2810 buf
[0] = 0x80; /* command "set data bits low byte" */
2811 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2812 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2813 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2815 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2817 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2818 return ERROR_JTAG_INIT_FAILED
;
2822 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2824 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2827 high_direction
= 0x0c;
2829 /* turn red LED3 on, LED2 off */
2830 high_output
|= 0x08;
2832 /* initialize high port */
2833 buf
[0] = 0x82; /* command "set data bits high byte" */
2834 buf
[1] = high_output
; /* value */
2835 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2836 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2838 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2840 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2841 return ERROR_JTAG_INIT_FAILED
;
2847 static int turtle_init(void)
2850 uint32_t bytes_written
;
2853 low_direction
= 0x5b;
2855 /* initialize low byte for jtag */
2856 buf
[0] = 0x80; /* command "set data bits low byte" */
2857 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2858 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2859 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2861 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2863 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2864 return ERROR_JTAG_INIT_FAILED
;
2870 high_direction
= 0x0C;
2872 /* initialize high port */
2873 buf
[0] = 0x82; /* command "set data bits high byte" */
2874 buf
[1] = high_output
;
2875 buf
[2] = high_direction
;
2876 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2878 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2880 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2881 return ERROR_JTAG_INIT_FAILED
;
2887 static int comstick_init(void)
2890 uint32_t bytes_written
;
2893 low_direction
= 0x0b;
2895 /* initialize low byte for jtag */
2896 buf
[0] = 0x80; /* command "set data bits low byte" */
2897 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2898 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2899 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2901 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2903 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2904 return ERROR_JTAG_INIT_FAILED
;
2908 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2910 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2913 high_direction
= 0x03;
2915 /* initialize high port */
2916 buf
[0] = 0x82; /* command "set data bits high byte" */
2917 buf
[1] = high_output
;
2918 buf
[2] = high_direction
;
2919 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2921 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2923 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2924 return ERROR_JTAG_INIT_FAILED
;
2930 static int stm32stick_init(void)
2933 uint32_t bytes_written
;
2936 low_direction
= 0x8b;
2938 /* initialize low byte for jtag */
2939 buf
[0] = 0x80; /* command "set data bits low byte" */
2940 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2941 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2942 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2944 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2946 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2947 return ERROR_JTAG_INIT_FAILED
;
2951 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2953 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2956 high_direction
= 0x03;
2958 /* initialize high port */
2959 buf
[0] = 0x82; /* command "set data bits high byte" */
2960 buf
[1] = high_output
;
2961 buf
[2] = high_direction
;
2962 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2964 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2966 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2967 return ERROR_JTAG_INIT_FAILED
;
2973 static int sheevaplug_init(void)
2976 uint32_t bytes_written
;
2979 low_direction
= 0x1b;
2981 /* initialize low byte for jtag */
2982 buf
[0] = 0x80; /* command "set data bits low byte" */
2983 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2984 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2985 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2987 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2989 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2990 return ERROR_JTAG_INIT_FAILED
;
2999 high_direction
= 0x0f;
3001 /* nTRST is always push-pull */
3002 high_output
&= ~nTRSTnOE
;
3003 high_output
|= nTRST
;
3005 /* nSRST is always open-drain */
3006 high_output
|= nSRSTnOE
;
3007 high_output
&= ~nSRST
;
3009 /* initialize high port */
3010 buf
[0] = 0x82; /* command "set data bits high byte" */
3011 buf
[1] = high_output
; /* value */
3012 buf
[2] = high_direction
; /* all outputs - xRST */
3013 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3015 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3017 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3018 return ERROR_JTAG_INIT_FAILED
;
3024 static int cortino_jtag_init(void)
3027 uint32_t bytes_written
;
3030 low_direction
= 0x1b;
3032 /* initialize low byte for jtag */
3033 buf
[0] = 0x80; /* command "set data bits low byte" */
3034 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3035 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3036 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3038 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3040 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3041 return ERROR_JTAG_INIT_FAILED
;
3045 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3047 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3050 high_direction
= 0x03;
3052 /* initialize high port */
3053 buf
[0] = 0x82; /* command "set data bits high byte" */
3054 buf
[1] = high_output
;
3055 buf
[2] = high_direction
;
3056 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3058 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3060 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3061 return ERROR_JTAG_INIT_FAILED
;
3067 static void olimex_jtag_blink(void)
3069 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3070 * ACBUS3 is bit 3 of the GPIOH port
3072 if (high_output
& 0x08)
3074 /* set port pin high */
3075 high_output
&= 0x07;
3079 /* set port pin low */
3080 high_output
|= 0x08;
3084 buffer_write(high_output
);
3085 buffer_write(high_direction
);
3088 static void flyswatter_jtag_blink(void)
3091 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3093 high_output
^= 0x0c;
3096 buffer_write(high_output
);
3097 buffer_write(high_direction
);
3100 static void turtle_jtag_blink(void)
3103 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3105 if (high_output
& 0x08)
3115 buffer_write(high_output
);
3116 buffer_write(high_direction
);
3119 static int ft2232_quit(void)
3121 #if BUILD_FT2232_FTD2XX == 1
3124 status
= FT_Close(ftdih
);
3125 #elif BUILD_FT2232_LIBFTDI == 1
3126 ftdi_usb_close(&ftdic
);
3128 ftdi_deinit(&ftdic
);
3131 free(ft2232_buffer
);
3132 ft2232_buffer
= NULL
;
3137 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3143 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3144 cp
= strchr(ft2232_device_desc
, 0);
3145 /* under Win32, the FTD2XX driver appends an "A" to the end
3146 * of the description, this examines the given desc
3147 * and creates the 'missing' _A or non_A variable. */
3148 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3149 /* it was, so make this the "A" version. */
3150 ft2232_device_desc_A
= ft2232_device_desc
;
3151 /* and *CREATE* the non-A version. */
3152 strcpy(buf
, ft2232_device_desc
);
3153 cp
= strchr(buf
, 0);
3155 ft2232_device_desc
= strdup(buf
);
3157 /* <space > A not defined
3159 sprintf(buf
, "%s A", ft2232_device_desc
);
3160 ft2232_device_desc_A
= strdup(buf
);
3165 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3171 COMMAND_HANDLER(ft2232_handle_serial_command
)
3175 ft2232_serial
= strdup(CMD_ARGV
[0]);
3179 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3185 COMMAND_HANDLER(ft2232_handle_layout_command
)
3187 if (CMD_ARGC
!= 1) {
3188 LOG_ERROR("Need exactly one argument to ft2232_layout");
3193 LOG_ERROR("already specified ft2232_layout %s",
3195 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3200 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3201 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3207 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3211 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3213 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3215 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3216 "(maximum is %d pairs)", MAX_USB_IDS
);
3217 CMD_ARGC
= MAX_USB_IDS
* 2;
3219 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3221 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3223 return ERROR_COMMAND_SYNTAX_ERROR
;
3224 /* remove the incomplete trailing id */
3229 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3231 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3232 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3236 * Explicitly terminate, in case there are multiples instances of
3239 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3244 COMMAND_HANDLER(ft2232_handle_latency_command
)
3248 ft2232_latency
= atoi(CMD_ARGV
[0]);
3252 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3258 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3262 /* 7 bits of either ones or zeros. */
3263 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3265 while (num_cycles
> 0)
3267 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3268 * at most 7 bits per invocation. Here we invoke it potentially
3271 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3273 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3275 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3276 retval
= ERROR_JTAG_QUEUE_FAILED
;
3281 /* there are no state transitions in this code, so omit state tracking */
3283 /* command "Clock Data to TMS/CS Pin (no Read)" */
3287 buffer_write(bitcount_per_command
- 1);
3289 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3294 num_cycles
-= bitcount_per_command
;
3300 /* ---------------------------------------------------------------------
3301 * Support for IceBear JTAG adapter from Section5:
3302 * http://section5.ch/icebear
3304 * Author: Sten, debian@sansys-electronic.com
3307 /* Icebear pin layout
3309 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3310 * GND GND | 4 3| n.c.
3311 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3312 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3313 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3314 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3315 * ADBUS2 TDO |14 13| GND GND
3317 * ADBUS0 O L TCK ACBUS0 GND
3318 * ADBUS1 O L TDI ACBUS1 GND
3319 * ADBUS2 I TDO ACBUS2 n.c.
3320 * ADBUS3 O H TMS ACBUS3 n.c.
3326 static int icebear_jtag_init(void) {
3328 uint32_t bytes_written
;
3330 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3331 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3335 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3336 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3337 low_direction
&= ~nTRST
; /* nTRST high impedance */
3340 low_direction
|= nTRST
;
3341 low_output
|= nTRST
;
3344 low_direction
|= nSRST
;
3345 low_output
|= nSRST
;
3347 /* initialize low byte for jtag */
3348 buf
[0] = 0x80; /* command "set data bits low byte" */
3349 buf
[1] = low_output
;
3350 buf
[2] = low_direction
;
3351 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3353 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3354 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3355 return ERROR_JTAG_INIT_FAILED
;
3359 high_direction
= 0x00;
3362 /* initialize high port */
3363 buf
[0] = 0x82; /* command "set data bits high byte" */
3364 buf
[1] = high_output
; /* value */
3365 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3366 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3368 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3369 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3370 return ERROR_JTAG_INIT_FAILED
;
3376 static void icebear_jtag_reset(int trst
, int srst
) {
3379 low_direction
|= nTRST
;
3380 low_output
&= ~nTRST
;
3382 else if (trst
== 0) {
3383 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3384 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3385 low_direction
&= ~nTRST
;
3387 low_output
|= nTRST
;
3391 low_output
&= ~nSRST
;
3393 else if (srst
== 0) {
3394 low_output
|= nSRST
;
3397 /* command "set data bits low byte" */
3399 buffer_write(low_output
);
3400 buffer_write(low_direction
);
3402 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3405 /* ---------------------------------------------------------------------
3406 * Support for Signalyzer H2 and Signalyzer H4
3407 * JTAG adapter from Xverve Technologies Inc.
3408 * http://www.signalyzer.com or http://www.xverve.com
3410 * Author: Oleg Seiljus, oleg@signalyzer.com
3412 static unsigned char signalyzer_h_side
;
3413 static unsigned int signalyzer_h_adapter_type
;
3415 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3417 #if BUILD_FT2232_FTD2XX == 1
3418 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3421 #define SIGNALYZER_COMMAND_ADDR 128
3422 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3424 #define SIGNALYZER_COMMAND_VERSION 0x41
3425 #define SIGNALYZER_COMMAND_RESET 0x42
3426 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3427 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3428 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3429 #define SIGNALYZER_COMMAND_LED_SET 0x53
3430 #define SIGNALYZER_COMMAND_ADC 0x54
3431 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3432 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3433 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3434 #define SIGNALYZER_COMMAND_I2C 0x58
3436 #define SIGNALYZER_CHAN_A 1
3437 #define SIGNALYZER_CHAN_B 2
3438 /* LEDS use channel C */
3439 #define SIGNALYZER_CHAN_C 4
3441 #define SIGNALYZER_LED_GREEN 1
3442 #define SIGNALYZER_LED_RED 2
3444 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3445 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3446 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3447 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3448 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3451 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3453 #if BUILD_FT2232_FTD2XX == 1
3454 return FT_WriteEE(ftdih
, address
, value
);
3455 #elif BUILD_FT2232_LIBFTDI == 1
3460 #if BUILD_FT2232_FTD2XX == 1
3461 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3463 return FT_ReadEE(ftdih
, address
, value
);
3467 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3468 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3470 unsigned char on_time
;
3471 unsigned char off_time
;
3473 if (on_time_ms
< 0xFFFF)
3474 on_time
= (unsigned char)(on_time_ms
/ 62);
3478 off_time
= (unsigned char)(off_time_ms
/ 62);
3480 #if BUILD_FT2232_FTD2XX == 1
3483 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3484 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3486 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3487 return ERROR_JTAG_DEVICE_ERROR
;
3490 if ((status
= signalyzer_h_ctrl_write(
3491 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3492 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3494 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3495 return ERROR_JTAG_DEVICE_ERROR
;
3498 if ((status
= signalyzer_h_ctrl_write(
3499 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3500 ((uint32_t)cycles
))) != FT_OK
)
3502 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3503 return ERROR_JTAG_DEVICE_ERROR
;
3506 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3507 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3509 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3510 return ERROR_JTAG_DEVICE_ERROR
;
3514 #elif BUILD_FT2232_LIBFTDI == 1
3517 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3518 ((uint32_t)(channel
<< 8) | led
))) < 0)
3520 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3521 ftdi_get_error_string(&ftdic
));
3522 return ERROR_JTAG_DEVICE_ERROR
;
3525 if ((retval
= signalyzer_h_ctrl_write(
3526 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3527 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3529 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3530 ftdi_get_error_string(&ftdic
));
3531 return ERROR_JTAG_DEVICE_ERROR
;
3534 if ((retval
= signalyzer_h_ctrl_write(
3535 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3536 (uint32_t)cycles
)) < 0)
3538 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3539 ftdi_get_error_string(&ftdic
));
3540 return ERROR_JTAG_DEVICE_ERROR
;
3543 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3544 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3546 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3547 ftdi_get_error_string(&ftdic
));
3548 return ERROR_JTAG_DEVICE_ERROR
;
3555 static int signalyzer_h_init(void)
3557 #if BUILD_FT2232_FTD2XX == 1
3564 uint16_t read_buf
[12] = { 0 };
3566 uint32_t bytes_written
;
3568 /* turn on center green led */
3569 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3570 0xFFFF, 0x00, 0x00);
3572 /* determine what channel config wants to open
3573 * TODO: change me... current implementation is made to work
3574 * with openocd description parsing.
3576 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3580 signalyzer_h_side
= *(end_of_desc
- 1);
3581 if (signalyzer_h_side
== 'B')
3582 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3584 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3588 LOG_ERROR("No Channel was specified");
3592 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3595 #if BUILD_FT2232_FTD2XX == 1
3596 /* read signalyzer versionining information */
3597 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3598 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3600 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3601 return ERROR_JTAG_DEVICE_ERROR
;
3604 for (i
= 0; i
< 10; i
++)
3606 if ((status
= signalyzer_h_ctrl_read(
3607 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3608 &read_buf
[i
])) != FT_OK
)
3610 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3612 return ERROR_JTAG_DEVICE_ERROR
;
3616 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3617 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3618 read_buf
[4], read_buf
[5], read_buf
[6]);
3620 /* set gpio register */
3621 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3622 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3624 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3625 return ERROR_JTAG_DEVICE_ERROR
;
3628 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3631 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3632 return ERROR_JTAG_DEVICE_ERROR
;
3635 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3636 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3638 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3639 return ERROR_JTAG_DEVICE_ERROR
;
3642 /* read adapter type information */
3643 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3644 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3646 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3647 return ERROR_JTAG_DEVICE_ERROR
;
3650 if ((status
= signalyzer_h_ctrl_write(
3651 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3653 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3654 return ERROR_JTAG_DEVICE_ERROR
;
3657 if ((status
= signalyzer_h_ctrl_write(
3658 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3660 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3661 return ERROR_JTAG_DEVICE_ERROR
;
3664 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3665 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3667 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3668 return ERROR_JTAG_DEVICE_ERROR
;
3673 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3674 &read_buf
[0])) != FT_OK
)
3676 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3677 return ERROR_JTAG_DEVICE_ERROR
;
3680 if (read_buf
[0] != 0x0498)
3681 signalyzer_h_adapter_type
= 0x0000;
3684 for (i
= 0; i
< 4; i
++)
3686 if ((status
= signalyzer_h_ctrl_read(
3687 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3688 &read_buf
[i
])) != FT_OK
)
3690 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3692 return ERROR_JTAG_DEVICE_ERROR
;
3696 signalyzer_h_adapter_type
= read_buf
[0];
3699 #elif BUILD_FT2232_LIBFTDI == 1
3700 /* currently libftdi does not allow reading individual eeprom
3701 * locations, therefore adapter type cannot be detected.
3702 * override with most common type
3704 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3707 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3709 /* ADAPTOR: EM_LT16_A */
3710 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3712 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3713 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3721 low_direction
= 0x1b;
3724 high_direction
= 0x0;
3726 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3728 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3729 low_output
&= ~nTRST
; /* nTRST = 0 */
3733 low_direction
|= nTRSTnOE
; /* nTRST output */
3734 low_output
|= nTRST
; /* nTRST = 1 */
3737 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3739 low_direction
|= nSRSTnOE
; /* nSRST output */
3740 low_output
|= nSRST
; /* nSRST = 1 */
3744 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3745 low_output
&= ~nSRST
; /* nSRST = 0 */
3748 #if BUILD_FT2232_FTD2XX == 1
3749 /* enable power to the module */
3750 if ((status
= signalyzer_h_ctrl_write(
3751 SIGNALYZER_DATA_BUFFER_ADDR
,
3752 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3755 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3757 return ERROR_JTAG_DEVICE_ERROR
;
3760 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3761 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3763 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3765 return ERROR_JTAG_DEVICE_ERROR
;
3768 /* set gpio mode register */
3769 if ((status
= signalyzer_h_ctrl_write(
3770 SIGNALYZER_DATA_BUFFER_ADDR
,
3771 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3773 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3775 return ERROR_JTAG_DEVICE_ERROR
;
3778 if ((status
= signalyzer_h_ctrl_write(
3779 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3782 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3784 return ERROR_JTAG_DEVICE_ERROR
;
3787 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3788 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3790 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3792 return ERROR_JTAG_DEVICE_ERROR
;
3795 /* set gpio register */
3796 if ((status
= signalyzer_h_ctrl_write(
3797 SIGNALYZER_DATA_BUFFER_ADDR
,
3798 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3800 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3802 return ERROR_JTAG_DEVICE_ERROR
;
3805 if ((status
= signalyzer_h_ctrl_write(
3806 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3809 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3811 return ERROR_JTAG_DEVICE_ERROR
;
3814 if ((status
= signalyzer_h_ctrl_write(
3815 SIGNALYZER_COMMAND_ADDR
,
3816 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3818 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3820 return ERROR_JTAG_DEVICE_ERROR
;
3825 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3826 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3827 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3828 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3829 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3831 if (signalyzer_h_adapter_type
3832 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3833 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3834 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3835 else if (signalyzer_h_adapter_type
3836 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3837 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3838 "(ARM JTAG with PSU) detected. (HW: %2x).",
3839 (read_buf
[1] >> 8));
3840 else if (signalyzer_h_adapter_type
3841 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3842 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3843 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3844 else if (signalyzer_h_adapter_type
3845 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3846 LOG_INFO("Signalyzer: EM-JTAG-P "
3847 "(Generic JTAG with PSU) detected. (HW: %2x).",
3848 (read_buf
[1] >> 8));
3856 low_direction
= 0x1b;
3859 high_direction
= 0x1f;
3861 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3863 high_output
|= nTRSTnOE
;
3864 high_output
&= ~nTRST
;
3868 high_output
&= ~nTRSTnOE
;
3869 high_output
|= nTRST
;
3872 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3874 high_output
&= ~nSRSTnOE
;
3875 high_output
|= nSRST
;
3879 high_output
|= nSRSTnOE
;
3880 high_output
&= ~nSRST
;
3883 #if BUILD_FT2232_FTD2XX == 1
3884 /* enable power to the module */
3885 if ((status
= signalyzer_h_ctrl_write(
3886 SIGNALYZER_DATA_BUFFER_ADDR
,
3887 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3890 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3892 return ERROR_JTAG_DEVICE_ERROR
;
3895 if ((status
= signalyzer_h_ctrl_write(
3896 SIGNALYZER_COMMAND_ADDR
,
3897 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3899 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3901 return ERROR_JTAG_DEVICE_ERROR
;
3904 /* set gpio mode register (IO_16 and IO_17 set as analog
3905 * inputs, other is gpio)
3907 if ((status
= signalyzer_h_ctrl_write(
3908 SIGNALYZER_DATA_BUFFER_ADDR
,
3909 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3911 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3913 return ERROR_JTAG_DEVICE_ERROR
;
3916 if ((status
= signalyzer_h_ctrl_write(
3917 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3920 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3922 return ERROR_JTAG_DEVICE_ERROR
;
3925 if ((status
= signalyzer_h_ctrl_write(
3926 SIGNALYZER_COMMAND_ADDR
,
3927 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3929 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3931 return ERROR_JTAG_DEVICE_ERROR
;
3934 /* set gpio register (all inputs, for -P modules,
3935 * PSU will be turned off)
3937 if ((status
= signalyzer_h_ctrl_write(
3938 SIGNALYZER_DATA_BUFFER_ADDR
,
3939 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3941 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3943 return ERROR_JTAG_DEVICE_ERROR
;
3946 if ((status
= signalyzer_h_ctrl_write(
3947 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3950 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3952 return ERROR_JTAG_DEVICE_ERROR
;
3955 if ((status
= signalyzer_h_ctrl_write(
3956 SIGNALYZER_COMMAND_ADDR
,
3957 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3959 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3961 return ERROR_JTAG_DEVICE_ERROR
;
3966 else if (signalyzer_h_adapter_type
== 0x0000)
3968 LOG_INFO("Signalyzer: No external modules were detected.");
3976 low_direction
= 0x1b;
3979 high_direction
= 0x0;
3981 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3983 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3984 low_output
&= ~nTRST
; /* nTRST = 0 */
3988 low_direction
|= nTRSTnOE
; /* nTRST output */
3989 low_output
|= nTRST
; /* nTRST = 1 */
3992 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3994 low_direction
|= nSRSTnOE
; /* nSRST output */
3995 low_output
|= nSRST
; /* nSRST = 1 */
3999 low_direction
&= ~nSRSTnOE
; /* nSRST input */
4000 low_output
&= ~nSRST
; /* nSRST = 0 */
4005 LOG_ERROR("Unknown module type is detected: %.4x",
4006 signalyzer_h_adapter_type
);
4007 return ERROR_JTAG_DEVICE_ERROR
;
4010 /* initialize low byte of controller for jtag operation */
4012 buf
[1] = low_output
;
4013 buf
[2] = low_direction
;
4015 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4017 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4018 return ERROR_JTAG_INIT_FAILED
;
4021 #if BUILD_FT2232_FTD2XX == 1
4022 if (ftdi_device
== FT_DEVICE_2232H
)
4024 /* initialize high byte of controller for jtag operation */
4026 buf
[1] = high_output
;
4027 buf
[2] = high_direction
;
4029 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4031 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4032 return ERROR_JTAG_INIT_FAILED
;
4035 #elif BUILD_FT2232_LIBFTDI == 1
4036 if (ftdi_device
== TYPE_2232H
)
4038 /* initialize high byte of controller for jtag operation */
4040 buf
[1] = high_output
;
4041 buf
[2] = high_direction
;
4043 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4045 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4046 return ERROR_JTAG_INIT_FAILED
;
4053 static void signalyzer_h_reset(int trst
, int srst
)
4055 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4057 /* ADAPTOR: EM_LT16_A */
4058 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4062 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4063 /* switch to output pin (output is low) */
4064 low_direction
|= nTRSTnOE
;
4066 /* switch output low */
4067 low_output
&= ~nTRST
;
4071 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4072 /* switch to input pin (high-Z + internal
4073 * and external pullup) */
4074 low_direction
&= ~nTRSTnOE
;
4076 /* switch output high */
4077 low_output
|= nTRST
;
4082 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4083 /* switch output low */
4084 low_output
&= ~nSRST
;
4086 /* switch to output pin (output is low) */
4087 low_direction
|= nSRSTnOE
;
4091 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4092 /* switch output high */
4093 low_output
|= nSRST
;
4095 /* switch to input pin (high-Z) */
4096 low_direction
&= ~nSRSTnOE
;
4099 /* command "set data bits low byte" */
4101 buffer_write(low_output
);
4102 buffer_write(low_direction
);
4103 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4104 "low_direction: 0x%2.2x",
4105 trst
, srst
, low_output
, low_direction
);
4107 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4108 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4109 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4110 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4111 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4115 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4116 high_output
&= ~nTRSTnOE
;
4118 high_output
&= ~nTRST
;
4122 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4123 high_output
|= nTRSTnOE
;
4125 high_output
|= nTRST
;
4130 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4131 high_output
&= ~nSRST
;
4133 high_output
&= ~nSRSTnOE
;
4137 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4138 high_output
|= nSRST
;
4140 high_output
|= nSRSTnOE
;
4143 /* command "set data bits high byte" */
4145 buffer_write(high_output
);
4146 buffer_write(high_direction
);
4147 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4148 "high_direction: 0x%2.2x",
4149 trst
, srst
, high_output
, high_direction
);
4151 else if (signalyzer_h_adapter_type
== 0x0000)
4155 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4156 /* switch to output pin (output is low) */
4157 low_direction
|= nTRSTnOE
;
4159 /* switch output low */
4160 low_output
&= ~nTRST
;
4164 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4165 /* switch to input pin (high-Z + internal
4166 * and external pullup) */
4167 low_direction
&= ~nTRSTnOE
;
4169 /* switch output high */
4170 low_output
|= nTRST
;
4175 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4176 /* switch output low */
4177 low_output
&= ~nSRST
;
4179 /* switch to output pin (output is low) */
4180 low_direction
|= nSRSTnOE
;
4184 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4185 /* switch output high */
4186 low_output
|= nSRST
;
4188 /* switch to input pin (high-Z) */
4189 low_direction
&= ~nSRSTnOE
;
4192 /* command "set data bits low byte" */
4194 buffer_write(low_output
);
4195 buffer_write(low_direction
);
4196 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4197 "low_direction: 0x%2.2x",
4198 trst
, srst
, low_output
, low_direction
);
4202 static void signalyzer_h_blink(void)
4204 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4207 /********************************************************************
4208 * Support for KT-LINK
4209 * JTAG adapter from KRISTECH
4210 * http://www.kristech.eu
4211 *******************************************************************/
4212 static int ktlink_init(void)
4215 uint32_t bytes_written
;
4216 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4218 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4219 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4221 // initialize low port
4222 buf
[0] = 0x80; // command "set data bits low byte"
4223 buf
[1] = low_output
;
4224 buf
[2] = low_direction
;
4225 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4227 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4229 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4230 return ERROR_JTAG_INIT_FAILED
;
4238 high_output
= 0x80; // turn LED on
4239 high_direction
= 0xFF; // all outputs
4241 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4243 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4244 high_output
|= nTRSTnOE
;
4245 high_output
&= ~nTRST
;
4247 high_output
&= ~nTRSTnOE
;
4248 high_output
|= nTRST
;
4251 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4252 high_output
&= ~nSRSTnOE
;
4253 high_output
|= nSRST
;
4255 high_output
|= nSRSTnOE
;
4256 high_output
&= ~nSRST
;
4259 // initialize high port
4260 buf
[0] = 0x82; // command "set data bits high byte"
4261 buf
[1] = high_output
; // value
4262 buf
[2] = high_direction
;
4263 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4265 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4267 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4268 return ERROR_JTAG_INIT_FAILED
;
4274 static void ktlink_reset(int trst
, int srst
)
4276 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4279 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4280 high_output
&= ~nTRSTnOE
;
4282 high_output
&= ~nTRST
;
4283 } else if (trst
== 0) {
4284 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4285 high_output
|= nTRSTnOE
;
4287 high_output
|= nTRST
;
4291 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4292 high_output
&= ~nSRST
;
4294 high_output
&= ~nSRSTnOE
;
4295 } else if (srst
== 0) {
4296 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4297 high_output
|= nSRST
;
4299 high_output
|= nSRSTnOE
;
4302 buffer_write(0x82); // command "set data bits high byte"
4303 buffer_write(high_output
);
4304 buffer_write(high_direction
);
4305 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4308 static void ktlink_blink(void)
4310 /* LED connected to ACBUS7 */
4311 if (high_output
& 0x80)
4312 high_output
&= 0x7F;
4314 high_output
|= 0x80;
4316 buffer_write(0x82); // command "set data bits high byte"
4317 buffer_write(high_output
);
4318 buffer_write(high_direction
);
4321 static const struct command_registration ft2232_command_handlers
[] = {
4323 .name
= "ft2232_device_desc",
4324 .handler
= &ft2232_handle_device_desc_command
,
4325 .mode
= COMMAND_CONFIG
,
4326 .help
= "set the USB device description of the FTDI FT2232 device",
4327 .usage
= "description_string",
4330 .name
= "ft2232_serial",
4331 .handler
= &ft2232_handle_serial_command
,
4332 .mode
= COMMAND_CONFIG
,
4333 .help
= "set the serial number of the FTDI FT2232 device",
4334 .usage
= "serial_string",
4337 .name
= "ft2232_layout",
4338 .handler
= &ft2232_handle_layout_command
,
4339 .mode
= COMMAND_CONFIG
,
4340 .help
= "set the layout of the FT2232 GPIO signals used "
4341 "to control output-enables and reset signals",
4342 .usage
= "layout_name",
4345 .name
= "ft2232_vid_pid",
4346 .handler
= &ft2232_handle_vid_pid_command
,
4347 .mode
= COMMAND_CONFIG
,
4348 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4349 .usage
= "(vid pid)* ",
4352 .name
= "ft2232_latency",
4353 .handler
= &ft2232_handle_latency_command
,
4354 .mode
= COMMAND_CONFIG
,
4355 .help
= "set the FT2232 latency timer to a new value",
4358 COMMAND_REGISTRATION_DONE
4361 struct jtag_interface ft2232_interface
= {
4363 .supported
= DEBUG_CAP_TMS_SEQ
,
4364 .commands
= ft2232_command_handlers
,
4365 .transports
= jtag_only
,
4367 .init
= ft2232_init
,
4368 .quit
= ft2232_quit
,
4369 .speed
= ft2232_speed
,
4370 .speed_div
= ft2232_speed_div
,
4372 .execute_queue
= ft2232_execute_queue
,