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
,
307 .init
= usbjtag_init
,
308 .reset
= ftx23_reset
,
309 .channel
= INTERFACE_B
,
311 { .name
= NULL
, /* END OF TABLE */ },
314 /* bitmask used to drive nTRST; usually a GPIOLx signal */
315 static uint8_t nTRST
;
316 static uint8_t nTRSTnOE
;
317 /* bitmask used to drive nSRST; usually a GPIOLx signal */
318 static uint8_t nSRST
;
319 static uint8_t nSRSTnOE
;
321 /** the layout being used with this debug session */
322 static const struct ft2232_layout
*layout
;
324 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
325 static uint8_t low_output
= 0x0;
327 /* note that direction bit == 1 means that signal is an output */
329 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
330 static uint8_t low_direction
= 0x0;
331 /** default value bitmask for CBUS GPIOH(0..4) */
332 static uint8_t high_output
= 0x0;
333 /** default direction bitmask for CBUS GPIOH(0..4) */
334 static uint8_t high_direction
= 0x0;
336 #if BUILD_FT2232_FTD2XX == 1
337 static FT_HANDLE ftdih
= NULL
;
338 static FT_DEVICE ftdi_device
= 0;
339 #elif BUILD_FT2232_LIBFTDI == 1
340 static struct ftdi_context ftdic
;
341 static enum ftdi_chip_type ftdi_device
;
344 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
345 static int require_send
;
347 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
349 "There is a significant difference between libftdi and libftd2xx. The latter
350 one allows to schedule up to 64*64 bytes of result data while libftdi fails
351 with more than 4*64. As a consequence, the FT2232 driver is forced to
352 perform around 16x more USB transactions for long command streams with TDO
353 capture when running with libftdi."
356 #define FT2232_BUFFER_SIZE 131072
357 a comment would have been nice.
360 #define FT2232_BUFFER_SIZE 131072
362 static uint8_t* ft2232_buffer
= NULL
;
363 static int ft2232_buffer_size
= 0;
364 static int ft2232_read_pointer
= 0;
365 static int ft2232_expect_read
= 0;
368 * Function buffer_write
369 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
370 * @param val is the byte to send.
372 static inline void buffer_write(uint8_t val
)
374 assert(ft2232_buffer
);
375 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
376 ft2232_buffer
[ft2232_buffer_size
++] = val
;
380 * Function buffer_read
381 * returns a byte from the byte buffer.
383 static inline uint8_t buffer_read(void)
385 assert(ft2232_buffer
);
386 assert(ft2232_read_pointer
< ft2232_buffer_size
);
387 return ft2232_buffer
[ft2232_read_pointer
++];
391 * Clocks out \a bit_count bits on the TMS line, starting with the least
392 * significant bit of tms_bits and progressing to more significant bits.
393 * Rigorous state transition logging is done here via tap_set_state().
395 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
396 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
397 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
398 * is often used for this, 0x4b.
400 * @param tms_bits Holds the sequence of bits to send.
401 * @param tms_count Tells how many bits in the sequence.
402 * @param tdi_bit A single bit to pass on to TDI before the first TCK
403 * cycle and held static for the duration of TMS clocking.
405 * See the MPSSE spec referenced above.
407 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
411 int tms_ndx
; /* bit index into tms_byte */
413 assert(tms_count
> 0);
415 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
416 mpsse_cmd
, tms_bits
, tms_count
);
418 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
420 bool bit
= tms_bits
& 1;
423 tms_byte
|= (1 << tms_ndx
);
425 /* always do state transitions in public view */
426 tap_set_state(tap_state_transition(tap_get_state(), bit
));
428 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
433 if (tms_ndx
== 7 || i
== tms_count
-1)
435 buffer_write(mpsse_cmd
);
436 buffer_write(tms_ndx
- 1);
438 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
439 TMS/CS and is held static for the duration of TMS/CS clocking.
441 buffer_write(tms_byte
| (tdi_bit
<< 7));
447 * Function get_tms_buffer_requirements
448 * returns what clock_tms() will consume if called with
451 static inline int get_tms_buffer_requirements(int bit_count
)
453 return ((bit_count
+ 6)/7) * 3;
457 * Function move_to_state
458 * moves the TAP controller from the current state to a
459 * \a goal_state through a path given by tap_get_tms_path(). State transition
460 * logging is performed by delegation to clock_tms().
462 * @param goal_state is the destination state for the move.
464 static void move_to_state(tap_state_t goal_state
)
466 tap_state_t start_state
= tap_get_state();
468 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
469 lookup of the required TMS pattern to move to this state from the
473 /* do the 2 lookups */
474 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
475 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
477 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
479 clock_tms(0x4b, tms_bits
, tms_count
, 0);
482 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
484 #if BUILD_FT2232_FTD2XX == 1
486 DWORD dw_bytes_written
;
487 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
489 *bytes_written
= dw_bytes_written
;
490 LOG_ERROR("FT_Write returned: %lu", status
);
491 return ERROR_JTAG_DEVICE_ERROR
;
495 *bytes_written
= dw_bytes_written
;
497 #elif BUILD_FT2232_LIBFTDI == 1
499 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
502 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
503 return ERROR_JTAG_DEVICE_ERROR
;
507 *bytes_written
= retval
;
511 if (*bytes_written
!= (uint32_t)size
)
513 return ERROR_JTAG_DEVICE_ERROR
;
519 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
521 #if BUILD_FT2232_FTD2XX == 1
527 while ((*bytes_read
< size
) && timeout
--)
529 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
530 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
533 LOG_ERROR("FT_Read returned: %lu", status
);
534 return ERROR_JTAG_DEVICE_ERROR
;
536 *bytes_read
+= dw_bytes_read
;
539 #elif BUILD_FT2232_LIBFTDI == 1
541 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
544 while ((*bytes_read
< size
) && timeout
--)
546 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
549 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
550 return ERROR_JTAG_DEVICE_ERROR
;
552 *bytes_read
+= retval
;
557 if (*bytes_read
< size
)
559 LOG_ERROR("couldn't read enough bytes from "
560 "FT2232 device (%i < %i)",
561 (unsigned)*bytes_read
,
563 return ERROR_JTAG_DEVICE_ERROR
;
569 static bool ft2232_device_is_highspeed(void)
571 #if BUILD_FT2232_FTD2XX == 1
572 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
573 #elif BUILD_FT2232_LIBFTDI == 1
574 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
579 * Commands that only apply to the FT2232H and FT4232H devices.
580 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
581 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
584 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
586 uint8_t buf
= enable
? 0x96 : 0x97;
587 LOG_DEBUG("%2.2x", buf
);
589 uint32_t bytes_written
;
592 if ((retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
594 LOG_ERROR("couldn't write command to %s adaptive clocking"
595 , enable
? "enable" : "disable");
603 * Enable/disable the clk divide by 5 of the 60MHz master clock.
604 * This result in a JTAG clock speed range of 91.553Hz-6MHz
605 * respective 457.763Hz-30MHz.
607 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
609 uint32_t bytes_written
;
610 uint8_t buf
= enable
? 0x8b : 0x8a;
612 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
614 LOG_ERROR("couldn't write command to %s clk divide by 5"
615 , enable
? "enable" : "disable");
616 return ERROR_JTAG_INIT_FAILED
;
618 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
619 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
624 static int ft2232_speed(int speed
)
628 uint32_t bytes_written
;
631 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
632 if (ft2232_device_is_highspeed())
633 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
634 else if (enable_adaptive_clocking
)
636 LOG_ERROR("ft2232 device %lu does not support RTCK"
637 , (long unsigned int)ftdi_device
);
641 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
644 buf
[0] = 0x86; /* command "set divisor" */
645 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
646 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
648 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
649 if ((retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
651 LOG_ERROR("couldn't set FT2232 TCK speed");
658 static int ft2232_speed_div(int speed
, int* khz
)
660 /* Take a look in the FT2232 manual,
661 * AN2232C-01 Command Processor for
662 * MPSSE and MCU Host Bus. Chapter 3.8 */
664 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
669 static int ft2232_khz(int khz
, int* jtag_speed
)
673 if (ft2232_device_is_highspeed())
675 *jtag_speed
= RTCK_SPEED
;
680 LOG_DEBUG("RCLK not supported");
685 /* Take a look in the FT2232 manual,
686 * AN2232C-01 Command Processor for
687 * MPSSE and MCU Host Bus. Chapter 3.8
689 * We will calc here with a multiplier
690 * of 10 for better rounding later. */
692 /* Calc speed, (ft2232_max_tck / khz) - 1 */
693 /* Use 65000 for better rounding */
694 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
696 /* Add 0.9 for rounding */
699 /* Calc real speed */
700 *jtag_speed
= *jtag_speed
/ 10;
702 /* Check if speed is greater than 0 */
708 /* Check max value */
709 if (*jtag_speed
> 0xFFFF)
711 *jtag_speed
= 0xFFFF;
717 static void ft2232_end_state(tap_state_t state
)
719 if (tap_is_state_stable(state
))
720 tap_set_end_state(state
);
723 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
728 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
730 int num_bytes
= (scan_size
+ 7) / 8;
731 int bits_left
= scan_size
;
734 while (num_bytes
-- > 1)
736 buffer
[cur_byte
++] = buffer_read();
740 buffer
[cur_byte
] = 0x0;
742 /* There is one more partial byte left from the clock data in/out instructions */
745 buffer
[cur_byte
] = buffer_read() >> 1;
747 /* 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 */
748 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
751 static void ft2232_debug_dump_buffer(void)
757 for (i
= 0; i
< ft2232_buffer_size
; i
++)
759 line_p
+= snprintf(line_p
, sizeof(line
) - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
762 LOG_DEBUG("%s", line
);
768 LOG_DEBUG("%s", line
);
771 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
773 struct jtag_command
* cmd
;
778 uint32_t bytes_written
= 0;
779 uint32_t bytes_read
= 0;
781 #ifdef _DEBUG_USB_IO_
782 struct timeval start
, inter
, inter2
, end
;
783 struct timeval d_inter
, d_inter2
, d_end
;
786 #ifdef _DEBUG_USB_COMMS_
787 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
788 ft2232_debug_dump_buffer();
791 #ifdef _DEBUG_USB_IO_
792 gettimeofday(&start
, NULL
);
795 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
797 LOG_ERROR("couldn't write MPSSE commands to FT2232");
801 #ifdef _DEBUG_USB_IO_
802 gettimeofday(&inter
, NULL
);
805 if (ft2232_expect_read
)
807 /* FIXME this "timeout" is never changed ... */
808 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
809 ft2232_buffer_size
= 0;
811 #ifdef _DEBUG_USB_IO_
812 gettimeofday(&inter2
, NULL
);
815 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
817 LOG_ERROR("couldn't read from FT2232");
821 #ifdef _DEBUG_USB_IO_
822 gettimeofday(&end
, NULL
);
824 timeval_subtract(&d_inter
, &inter
, &start
);
825 timeval_subtract(&d_inter2
, &inter2
, &start
);
826 timeval_subtract(&d_end
, &end
, &start
);
828 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
829 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
830 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
831 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
834 ft2232_buffer_size
= bytes_read
;
836 if (ft2232_expect_read
!= ft2232_buffer_size
)
838 LOG_ERROR("ft2232_expect_read (%i) != "
839 "ft2232_buffer_size (%i) "
843 LIBFTDI_READ_RETRY_COUNT
- timeout
);
844 ft2232_debug_dump_buffer();
849 #ifdef _DEBUG_USB_COMMS_
850 LOG_DEBUG("read buffer (%i retries): %i bytes",
851 LIBFTDI_READ_RETRY_COUNT
- timeout
,
853 ft2232_debug_dump_buffer();
857 ft2232_expect_read
= 0;
858 ft2232_read_pointer
= 0;
860 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
861 * that wasn't handled by a caller-provided error handler
871 type
= jtag_scan_type(cmd
->cmd
.scan
);
872 if (type
!= SCAN_OUT
)
874 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
875 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
876 ft2232_read_scan(type
, buffer
, scan_size
);
877 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
878 retval
= ERROR_JTAG_QUEUE_FAILED
;
890 ft2232_buffer_size
= 0;
896 * Function ft2232_add_pathmove
897 * moves the TAP controller from the current state to a new state through the
898 * given path, where path is an array of tap_state_t's.
900 * @param path is an array of tap_stat_t which gives the states to traverse through
901 * ending with the last state at path[num_states-1]
902 * @param num_states is the count of state steps to move through
904 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
908 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
912 /* this loop verifies that the path is legal and logs each state in the path */
915 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
917 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
919 /* command "Clock Data to TMS/CS Pin (no Read)" */
922 /* number of states remaining */
923 buffer_write(num_states_batch
- 1);
925 while (num_states_batch
--) {
926 /* either TMS=0 or TMS=1 must work ... */
927 if (tap_state_transition(tap_get_state(), false)
928 == path
[state_count
])
929 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
930 else if (tap_state_transition(tap_get_state(), true)
931 == path
[state_count
])
932 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
934 /* ... or else the caller goofed BADLY */
936 LOG_ERROR("BUG: %s -> %s isn't a valid "
937 "TAP state transition",
938 tap_state_name(tap_get_state()),
939 tap_state_name(path
[state_count
]));
943 tap_set_state(path
[state_count
]);
948 buffer_write(tms_byte
);
950 tap_set_end_state(tap_get_state());
953 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
955 int num_bytes
= (scan_size
+ 7) / 8;
956 int bits_left
= scan_size
;
962 if (tap_get_state() != TAP_DRSHIFT
)
964 move_to_state(TAP_DRSHIFT
);
969 if (tap_get_state() != TAP_IRSHIFT
)
971 move_to_state(TAP_IRSHIFT
);
975 /* add command for complete bytes */
976 while (num_bytes
> 1)
981 /* Clock Data Bytes In and Out LSB First */
983 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
985 else if (type
== SCAN_OUT
)
987 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
989 /* LOG_DEBUG("added TDI bytes (o)"); */
991 else if (type
== SCAN_IN
)
993 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
995 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
998 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
999 num_bytes
-= thisrun_bytes
;
1001 buffer_write((uint8_t) (thisrun_bytes
- 1));
1002 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1004 if (type
!= SCAN_IN
)
1006 /* add complete bytes */
1007 while (thisrun_bytes
-- > 0)
1009 buffer_write(buffer
[cur_byte
++]);
1013 else /* (type == SCAN_IN) */
1015 bits_left
-= 8 * (thisrun_bytes
);
1019 /* the most signifcant bit is scanned during TAP movement */
1020 if (type
!= SCAN_IN
)
1021 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1025 /* process remaining bits but the last one */
1028 if (type
== SCAN_IO
)
1030 /* Clock Data Bits In and Out LSB First */
1032 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1034 else if (type
== SCAN_OUT
)
1036 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1038 /* LOG_DEBUG("added TDI bits (o)"); */
1040 else if (type
== SCAN_IN
)
1042 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1044 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1047 buffer_write(bits_left
- 2);
1048 if (type
!= SCAN_IN
)
1049 buffer_write(buffer
[cur_byte
]);
1052 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1053 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1055 if (type
== SCAN_IO
)
1057 /* Clock Data Bits In and Out LSB First */
1059 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1061 else if (type
== SCAN_OUT
)
1063 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1065 /* LOG_DEBUG("added TDI bits (o)"); */
1067 else if (type
== SCAN_IN
)
1069 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1071 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1074 buffer_write(last_bit
);
1082 /* move from Shift-IR/DR to end state */
1083 if (type
!= SCAN_OUT
)
1085 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1086 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1089 /* Clock Data to TMS/CS Pin with Read */
1094 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1095 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1096 /* Clock Data to TMS/CS Pin (no Read) */
1100 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1101 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1104 if (tap_get_state() != tap_get_end_state())
1106 move_to_state(tap_get_end_state());
1110 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1112 int num_bytes
= (scan_size
+ 7) / 8;
1113 int bits_left
= scan_size
;
1116 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1117 uint8_t* receive_pointer
= receive_buffer
;
1118 uint32_t bytes_written
;
1119 uint32_t bytes_read
;
1121 int thisrun_read
= 0;
1125 LOG_ERROR("BUG: large IR scans are not supported");
1129 if (tap_get_state() != TAP_DRSHIFT
)
1131 move_to_state(TAP_DRSHIFT
);
1134 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1136 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1139 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1140 ft2232_buffer_size
, (int)bytes_written
);
1141 ft2232_buffer_size
= 0;
1143 /* add command for complete bytes */
1144 while (num_bytes
> 1)
1148 if (type
== SCAN_IO
)
1150 /* Clock Data Bytes In and Out LSB First */
1152 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1154 else if (type
== SCAN_OUT
)
1156 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1158 /* LOG_DEBUG("added TDI bytes (o)"); */
1160 else if (type
== SCAN_IN
)
1162 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1164 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1167 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1168 thisrun_read
= thisrun_bytes
;
1169 num_bytes
-= thisrun_bytes
;
1170 buffer_write((uint8_t) (thisrun_bytes
- 1));
1171 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1173 if (type
!= SCAN_IN
)
1175 /* add complete bytes */
1176 while (thisrun_bytes
-- > 0)
1178 buffer_write(buffer
[cur_byte
]);
1183 else /* (type == SCAN_IN) */
1185 bits_left
-= 8 * (thisrun_bytes
);
1188 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1190 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1193 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1195 (int)bytes_written
);
1196 ft2232_buffer_size
= 0;
1198 if (type
!= SCAN_OUT
)
1200 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1202 LOG_ERROR("couldn't read from FT2232");
1205 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1208 receive_pointer
+= bytes_read
;
1214 /* the most signifcant bit is scanned during TAP movement */
1215 if (type
!= SCAN_IN
)
1216 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1220 /* process remaining bits but the last one */
1223 if (type
== SCAN_IO
)
1225 /* Clock Data Bits In and Out LSB First */
1227 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1229 else if (type
== SCAN_OUT
)
1231 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1233 /* LOG_DEBUG("added TDI bits (o)"); */
1235 else if (type
== SCAN_IN
)
1237 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1239 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1241 buffer_write(bits_left
- 2);
1242 if (type
!= SCAN_IN
)
1243 buffer_write(buffer
[cur_byte
]);
1245 if (type
!= SCAN_OUT
)
1249 if (tap_get_end_state() == TAP_DRSHIFT
)
1251 if (type
== SCAN_IO
)
1253 /* Clock Data Bits In and Out LSB First */
1255 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1257 else if (type
== SCAN_OUT
)
1259 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1261 /* LOG_DEBUG("added TDI bits (o)"); */
1263 else if (type
== SCAN_IN
)
1265 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1267 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1270 buffer_write(last_bit
);
1274 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1275 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1278 /* move from Shift-IR/DR to end state */
1279 if (type
!= SCAN_OUT
)
1281 /* Clock Data to TMS/CS Pin with Read */
1283 /* LOG_DEBUG("added TMS scan (read)"); */
1287 /* Clock Data to TMS/CS Pin (no Read) */
1289 /* LOG_DEBUG("added TMS scan (no read)"); */
1292 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1293 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1296 if (type
!= SCAN_OUT
)
1299 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1301 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1304 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1306 (int)bytes_written
);
1307 ft2232_buffer_size
= 0;
1309 if (type
!= SCAN_OUT
)
1311 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1313 LOG_ERROR("couldn't read from FT2232");
1316 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1319 receive_pointer
+= bytes_read
;
1325 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1327 int predicted_size
= 3;
1328 int num_bytes
= (scan_size
- 1) / 8;
1330 if (tap_get_state() != TAP_DRSHIFT
)
1331 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1333 if (type
== SCAN_IN
) /* only from device to host */
1335 /* complete bytes */
1336 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1338 /* remaining bits - 1 (up to 7) */
1339 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1341 else /* host to device, or bidirectional */
1343 /* complete bytes */
1344 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1346 /* remaining bits -1 (up to 7) */
1347 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1350 return predicted_size
;
1353 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1355 int predicted_size
= 0;
1357 if (type
!= SCAN_OUT
)
1359 /* complete bytes */
1360 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1362 /* remaining bits - 1 */
1363 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1365 /* last bit (from TMS scan) */
1366 predicted_size
+= 1;
1369 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1371 return predicted_size
;
1374 /* semi-generic FT2232/FT4232 reset code */
1375 static void ftx23_reset(int trst
, int srst
)
1377 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1380 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1381 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1383 low_output
&= ~nTRST
; /* switch output low */
1387 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1388 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1390 low_output
|= nTRST
; /* switch output high */
1395 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1396 low_output
&= ~nSRST
; /* switch output low */
1398 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1402 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1403 low_output
|= nSRST
; /* switch output high */
1405 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1408 /* command "set data bits low byte" */
1410 buffer_write(low_output
);
1411 buffer_write(low_direction
);
1414 static void jtagkey_reset(int trst
, int srst
)
1416 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1419 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1420 high_output
&= ~nTRSTnOE
;
1422 high_output
&= ~nTRST
;
1426 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1427 high_output
|= nTRSTnOE
;
1429 high_output
|= nTRST
;
1434 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1435 high_output
&= ~nSRST
;
1437 high_output
&= ~nSRSTnOE
;
1441 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1442 high_output
|= nSRST
;
1444 high_output
|= nSRSTnOE
;
1447 /* command "set data bits high byte" */
1449 buffer_write(high_output
);
1450 buffer_write(high_direction
);
1451 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1455 static void olimex_jtag_reset(int trst
, int srst
)
1457 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1460 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1461 high_output
&= ~nTRSTnOE
;
1463 high_output
&= ~nTRST
;
1467 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1468 high_output
|= nTRSTnOE
;
1470 high_output
|= nTRST
;
1475 high_output
|= nSRST
;
1479 high_output
&= ~nSRST
;
1482 /* command "set data bits high byte" */
1484 buffer_write(high_output
);
1485 buffer_write(high_direction
);
1486 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1490 static void axm0432_jtag_reset(int trst
, int srst
)
1494 tap_set_state(TAP_RESET
);
1495 high_output
&= ~nTRST
;
1499 high_output
|= nTRST
;
1504 high_output
&= ~nSRST
;
1508 high_output
|= nSRST
;
1511 /* command "set data bits low byte" */
1513 buffer_write(high_output
);
1514 buffer_write(high_direction
);
1515 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1519 static void flyswatter_reset(int trst
, int srst
)
1523 low_output
&= ~nTRST
;
1527 low_output
|= nTRST
;
1532 low_output
|= nSRST
;
1536 low_output
&= ~nSRST
;
1539 /* command "set data bits low byte" */
1541 buffer_write(low_output
);
1542 buffer_write(low_direction
);
1543 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1546 static void turtle_reset(int trst
, int srst
)
1552 low_output
|= nSRST
;
1556 low_output
&= ~nSRST
;
1559 /* command "set data bits low byte" */
1561 buffer_write(low_output
);
1562 buffer_write(low_direction
);
1563 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1566 static void comstick_reset(int trst
, int srst
)
1570 high_output
&= ~nTRST
;
1574 high_output
|= nTRST
;
1579 high_output
&= ~nSRST
;
1583 high_output
|= nSRST
;
1586 /* command "set data bits high byte" */
1588 buffer_write(high_output
);
1589 buffer_write(high_direction
);
1590 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1594 static void stm32stick_reset(int trst
, int srst
)
1598 high_output
&= ~nTRST
;
1602 high_output
|= nTRST
;
1607 low_output
&= ~nSRST
;
1611 low_output
|= nSRST
;
1614 /* command "set data bits low byte" */
1616 buffer_write(low_output
);
1617 buffer_write(low_direction
);
1619 /* command "set data bits high byte" */
1621 buffer_write(high_output
);
1622 buffer_write(high_direction
);
1623 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1627 static void sheevaplug_reset(int trst
, int srst
)
1630 high_output
&= ~nTRST
;
1632 high_output
|= nTRST
;
1635 high_output
&= ~nSRSTnOE
;
1637 high_output
|= nSRSTnOE
;
1639 /* command "set data bits high byte" */
1641 buffer_write(high_output
);
1642 buffer_write(high_direction
);
1643 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1646 static void redbee_reset(int trst
, int srst
)
1650 tap_set_state(TAP_RESET
);
1651 high_output
&= ~nTRST
;
1655 high_output
|= nTRST
;
1660 high_output
&= ~nSRST
;
1664 high_output
|= nSRST
;
1667 /* command "set data bits low byte" */
1669 buffer_write(high_output
);
1670 buffer_write(high_direction
);
1671 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1672 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1676 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1680 int predicted_size
= 0;
1683 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1684 cmd
->cmd
.runtest
->num_cycles
,
1685 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1687 /* only send the maximum buffer size that FT2232C can handle */
1689 if (tap_get_state() != TAP_IDLE
)
1690 predicted_size
+= 3;
1691 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1692 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1693 predicted_size
+= 3;
1694 if (tap_get_end_state() != TAP_IDLE
)
1695 predicted_size
+= 3;
1696 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1698 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1699 retval
= ERROR_JTAG_QUEUE_FAILED
;
1703 if (tap_get_state() != TAP_IDLE
)
1705 move_to_state(TAP_IDLE
);
1708 i
= cmd
->cmd
.runtest
->num_cycles
;
1711 /* there are no state transitions in this code, so omit state tracking */
1713 /* command "Clock Data to TMS/CS Pin (no Read)" */
1717 buffer_write((i
> 7) ? 6 : (i
- 1));
1722 i
-= (i
> 7) ? 7 : i
;
1723 /* LOG_DEBUG("added TMS scan (no read)"); */
1726 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1728 if (tap_get_state() != tap_get_end_state())
1730 move_to_state(tap_get_end_state());
1734 DEBUG_JTAG_IO("runtest: %i, end in %s",
1735 cmd
->cmd
.runtest
->num_cycles
,
1736 tap_state_name(tap_get_end_state()));
1740 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1742 int predicted_size
= 0;
1743 int retval
= ERROR_OK
;
1745 DEBUG_JTAG_IO("statemove end in %s",
1746 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1748 /* only send the maximum buffer size that FT2232C can handle */
1750 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1752 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1753 retval
= ERROR_JTAG_QUEUE_FAILED
;
1757 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1759 /* For TAP_RESET, ignore the current recorded state. It's often
1760 * wrong at server startup, and this transation is critical whenever
1763 if (tap_get_end_state() == TAP_RESET
) {
1764 clock_tms(0x4b, 0xff, 5, 0);
1767 /* shortest-path move to desired end state */
1768 } else if (tap_get_state() != tap_get_end_state())
1770 move_to_state(tap_get_end_state());
1778 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1779 * (or SWD) state machine.
1781 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1783 int retval
= ERROR_OK
;
1784 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1785 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1788 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1790 /* only send the maximum buffer size that FT2232C can handle */
1791 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1792 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1793 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1794 retval
= ERROR_JTAG_QUEUE_FAILED
;
1800 /* Shift out in batches of at most 6 bits; there's a report of an
1801 * FT2232 bug in this area, where shifting exactly 7 bits can make
1802 * problems with TMS signaling for the last clock cycle:
1804 * http://developer.intra2net.com/mailarchive/html/
1805 * libftdi/2009/msg00292.html
1807 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1809 * Note that pathmoves in JTAG are not often seven bits, so that
1810 * isn't a particularly likely situation outside of "special"
1811 * signaling such as switching between JTAG and SWD modes.
1814 if (num_bits
<= 6) {
1816 buffer_write(num_bits
- 1);
1817 buffer_write(*bits
& 0x3f);
1821 /* Yes, this is lazy ... we COULD shift out more data
1822 * bits per operation, but doing it in nybbles is easy
1826 buffer_write(*bits
& 0xf);
1829 count
= (num_bits
> 4) ? 4 : num_bits
;
1832 buffer_write(count
- 1);
1833 buffer_write((*bits
>> 4) & 0xf);
1843 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1845 int predicted_size
= 0;
1846 int retval
= ERROR_OK
;
1848 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1849 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1851 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1852 tap_state_name(tap_get_state()),
1853 tap_state_name(path
[num_states
-1]));
1855 /* only send the maximum buffer size that FT2232C can handle */
1856 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1857 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1859 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1860 retval
= ERROR_JTAG_QUEUE_FAILED
;
1866 ft2232_add_pathmove(path
, num_states
);
1872 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1875 int scan_size
; /* size of IR or DR scan */
1876 int predicted_size
= 0;
1877 int retval
= ERROR_OK
;
1879 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1881 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1883 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1885 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1886 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1888 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1889 /* unsent commands before this */
1890 if (first_unsent
!= cmd
)
1891 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1892 retval
= ERROR_JTAG_QUEUE_FAILED
;
1894 /* current command */
1895 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1896 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1898 first_unsent
= cmd
->next
;
1903 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1905 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1908 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1909 retval
= ERROR_JTAG_QUEUE_FAILED
;
1913 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1914 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1915 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1916 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1920 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1921 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1922 tap_state_name(tap_get_end_state()));
1927 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1930 int predicted_size
= 0;
1933 DEBUG_JTAG_IO("reset trst: %i srst %i",
1934 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1936 /* only send the maximum buffer size that FT2232C can handle */
1938 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1940 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1941 retval
= ERROR_JTAG_QUEUE_FAILED
;
1946 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1948 tap_set_state(TAP_RESET
);
1951 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1954 DEBUG_JTAG_IO("trst: %i, srst: %i",
1955 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1959 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1964 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1966 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1967 retval
= ERROR_JTAG_QUEUE_FAILED
;
1968 first_unsent
= cmd
->next
;
1969 jtag_sleep(cmd
->cmd
.sleep
->us
);
1970 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1972 tap_state_name(tap_get_state()));
1976 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1981 /* this is only allowed while in a stable state. A check for a stable
1982 * state was done in jtag_add_clocks()
1984 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1985 retval
= ERROR_JTAG_QUEUE_FAILED
;
1986 DEBUG_JTAG_IO("clocks %i while in %s",
1987 cmd
->cmd
.stableclocks
->num_cycles
,
1988 tap_state_name(tap_get_state()));
1992 static int ft2232_execute_command(struct jtag_command
*cmd
)
1998 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1999 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
2000 case JTAG_TLR_RESET
: retval
= ft2232_execute_statemove(cmd
); break;
2001 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
2002 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
2003 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
2004 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
2006 retval
= ft2232_execute_tms(cmd
);
2009 LOG_ERROR("BUG: unknown JTAG command type encountered");
2010 retval
= ERROR_JTAG_QUEUE_FAILED
;
2016 static int ft2232_execute_queue(void)
2018 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
2021 first_unsent
= cmd
; /* next command that has to be sent */
2024 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2025 * that wasn't handled by a caller-provided error handler
2029 ft2232_buffer_size
= 0;
2030 ft2232_expect_read
= 0;
2032 /* blink, if the current layout has that feature */
2038 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2039 retval
= ERROR_JTAG_QUEUE_FAILED
;
2040 /* Start reading input before FT2232 TX buffer fills up */
2042 if (ft2232_expect_read
> 256)
2044 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2045 retval
= ERROR_JTAG_QUEUE_FAILED
;
2050 if (require_send
> 0)
2051 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2052 retval
= ERROR_JTAG_QUEUE_FAILED
;
2057 #if BUILD_FT2232_FTD2XX == 1
2058 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2062 char SerialNumber
[16];
2063 char Description
[64];
2064 DWORD openex_flags
= 0;
2065 char* openex_string
= NULL
;
2066 uint8_t latency_timer
;
2068 if (layout
== NULL
) {
2069 LOG_WARNING("No ft2232 layout specified'");
2070 return ERROR_JTAG_INIT_FAILED
;
2073 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2076 /* Add non-standard Vid/Pid to the linux driver */
2077 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2079 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2083 if (ft2232_device_desc
&& ft2232_serial
)
2085 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2086 ft2232_device_desc
= NULL
;
2089 if (ft2232_device_desc
)
2091 openex_string
= ft2232_device_desc
;
2092 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2094 else if (ft2232_serial
)
2096 openex_string
= ft2232_serial
;
2097 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2101 LOG_ERROR("neither device description nor serial number specified");
2102 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2104 return ERROR_JTAG_INIT_FAILED
;
2107 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2108 if (status
!= FT_OK
) {
2109 /* under Win32, the FTD2XX driver appends an "A" to the end
2110 * of the description, if we tried by the desc, then
2111 * try by the alternate "A" description. */
2112 if (openex_string
== ft2232_device_desc
) {
2113 /* Try the alternate method. */
2114 openex_string
= ft2232_device_desc_A
;
2115 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2116 if (status
== FT_OK
) {
2117 /* yea, the "alternate" method worked! */
2119 /* drat, give the user a meaningfull message.
2120 * telling the use we tried *BOTH* methods. */
2121 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2123 ft2232_device_desc_A
);
2128 if (status
!= FT_OK
)
2134 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2136 return ERROR_JTAG_INIT_FAILED
;
2138 LOG_ERROR("unable to open ftdi device: %lu", status
);
2139 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2140 if (status
== FT_OK
)
2142 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2145 for (i
= 0; i
< num_devices
; i
++)
2146 desc_array
[i
] = malloc(64);
2148 desc_array
[num_devices
] = NULL
;
2150 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2152 if (status
== FT_OK
)
2154 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2155 for (i
= 0; i
< num_devices
; i
++)
2156 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2159 for (i
= 0; i
< num_devices
; i
++)
2160 free(desc_array
[i
]);
2166 LOG_ERROR("ListDevices: NONE\n");
2168 return ERROR_JTAG_INIT_FAILED
;
2171 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2173 LOG_ERROR("unable to set latency timer: %lu", status
);
2174 return ERROR_JTAG_INIT_FAILED
;
2177 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2179 LOG_ERROR("unable to get latency timer: %lu", status
);
2180 return ERROR_JTAG_INIT_FAILED
;
2184 LOG_DEBUG("current latency timer: %i", latency_timer
);
2187 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2189 LOG_ERROR("unable to set timeouts: %lu", status
);
2190 return ERROR_JTAG_INIT_FAILED
;
2193 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2195 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2196 return ERROR_JTAG_INIT_FAILED
;
2199 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2201 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2202 return ERROR_JTAG_INIT_FAILED
;
2206 static const char* type_str
[] =
2207 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2208 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2209 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2210 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2211 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2212 LOG_INFO("deviceID: %lu", deviceID
);
2213 LOG_INFO("SerialNumber: %s", SerialNumber
);
2214 LOG_INFO("Description: %s", Description
);
2220 static int ft2232_purge_ftd2xx(void)
2224 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2226 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2227 return ERROR_JTAG_INIT_FAILED
;
2233 #endif /* BUILD_FT2232_FTD2XX == 1 */
2235 #if BUILD_FT2232_LIBFTDI == 1
2236 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2238 uint8_t latency_timer
;
2240 if (layout
== NULL
) {
2241 LOG_WARNING("No ft2232 layout specified'");
2242 return ERROR_JTAG_INIT_FAILED
;
2245 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2246 layout
->name
, vid
, pid
);
2248 if (ftdi_init(&ftdic
) < 0)
2249 return ERROR_JTAG_INIT_FAILED
;
2251 /* default to INTERFACE_A */
2252 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2254 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2256 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2257 return ERROR_JTAG_INIT_FAILED
;
2260 /* context, vendor id, product id */
2261 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2265 LOG_WARNING("unable to open ftdi device (trying more): %s",
2268 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2270 return ERROR_JTAG_INIT_FAILED
;
2273 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2274 if (ftdi_usb_reset(&ftdic
) < 0)
2276 LOG_ERROR("unable to reset ftdi device");
2277 return ERROR_JTAG_INIT_FAILED
;
2280 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2282 LOG_ERROR("unable to set latency timer");
2283 return ERROR_JTAG_INIT_FAILED
;
2286 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2288 LOG_ERROR("unable to get latency timer");
2289 return ERROR_JTAG_INIT_FAILED
;
2293 LOG_DEBUG("current latency timer: %i", latency_timer
);
2296 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2298 ftdi_device
= ftdic
.type
;
2299 static const char* type_str
[] =
2300 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2301 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2302 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2303 ? ftdi_device
: no_of_known_types
;
2304 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2308 static int ft2232_purge_libftdi(void)
2310 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2312 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2313 return ERROR_JTAG_INIT_FAILED
;
2319 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2321 static int ft2232_init(void)
2325 uint32_t bytes_written
;
2327 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2329 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2333 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2336 if (layout
== NULL
) {
2337 LOG_WARNING("No ft2232 layout specified'");
2338 return ERROR_JTAG_INIT_FAILED
;
2341 for (int i
= 0; 1; i
++)
2344 * "more indicates that there are more IDs to try, so we should
2345 * not print an error for an ID mismatch (but for anything
2348 * try_more indicates that the error code returned indicates an
2349 * ID mismatch (and nothing else) and that we should proceeed
2350 * with the next ID pair.
2352 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2355 #if BUILD_FT2232_FTD2XX == 1
2356 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2358 #elif BUILD_FT2232_LIBFTDI == 1
2359 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2360 more
, &try_more
, layout
->channel
);
2364 if (!more
|| !try_more
)
2368 ft2232_buffer_size
= 0;
2369 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2371 if (layout
->init() != ERROR_OK
)
2372 return ERROR_JTAG_INIT_FAILED
;
2374 if (ft2232_device_is_highspeed())
2376 #ifndef BUILD_FT2232_HIGHSPEED
2377 #if BUILD_FT2232_FTD2XX == 1
2378 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2379 #elif BUILD_FT2232_LIBFTDI == 1
2380 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2383 /* make sure the legacy mode is disabled */
2384 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2385 return ERROR_JTAG_INIT_FAILED
;
2388 ft2232_speed(jtag_get_speed());
2390 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2391 if ((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
)
2393 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2394 return ERROR_JTAG_INIT_FAILED
;
2397 #if BUILD_FT2232_FTD2XX == 1
2398 return ft2232_purge_ftd2xx();
2399 #elif BUILD_FT2232_LIBFTDI == 1
2400 return ft2232_purge_libftdi();
2406 /** Updates defaults for DBUS signals: the four JTAG signals
2407 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2409 static inline void ftx232_dbus_init(void)
2412 low_direction
= 0x0b;
2415 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2416 * the four GPIOL signals. Initialization covers value and direction,
2417 * as customized for each layout.
2419 static int ftx232_dbus_write(void)
2422 uint32_t bytes_written
;
2424 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2425 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2427 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2428 low_output
&= ~nTRST
; /* nTRST = 0 */
2432 low_direction
|= nTRSTnOE
; /* nTRST output */
2433 low_output
|= nTRST
; /* nTRST = 1 */
2436 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2438 low_direction
|= nSRSTnOE
; /* nSRST output */
2439 low_output
|= nSRST
; /* nSRST = 1 */
2443 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2444 low_output
&= ~nSRST
; /* nSRST = 0 */
2447 /* initialize low byte for jtag */
2448 buf
[0] = 0x80; /* command "set data bits low byte" */
2449 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2450 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2451 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2453 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2455 LOG_ERROR("couldn't initialize FT2232 DBUS");
2456 return ERROR_JTAG_INIT_FAILED
;
2462 static int usbjtag_init(void)
2465 * NOTE: This is now _specific_ to the "usbjtag" layout.
2466 * Don't try cram any more layouts into this.
2475 return ftx232_dbus_write();
2478 static int lm3s811_jtag_init(void)
2482 /* There are multiple revisions of LM3S811 eval boards:
2483 * - Rev B (and older?) boards have no SWO trace support.
2484 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2485 * they should use the "luminary_icdi" layout instead.
2492 low_direction
= 0x8b;
2494 return ftx232_dbus_write();
2497 static int icdi_jtag_init(void)
2501 /* Most Luminary eval boards support SWO trace output,
2502 * and should use this "luminary_icdi" layout.
2504 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2505 * to switch between JTAG and SWD, or switch the ft2232 UART
2506 * on the second MPSSE channel/interface (BDBUS)
2507 * between (i) the stellaris UART (on Luminary boards)
2508 * or (ii) SWO trace data (generic).
2510 * We come up in JTAG mode and may switch to SWD later (with
2511 * SWO/trace option if SWD is active).
2518 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2519 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2520 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2523 /* GPIOs on second channel/interface (UART) ... */
2524 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2525 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2526 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2531 nSRSTnOE
= ICDI_SRST
;
2533 low_direction
|= ICDI_JTAG_EN
| ICDI_DBG_ENn
;
2534 low_output
|= ICDI_JTAG_EN
;
2535 low_output
&= ~ICDI_DBG_ENn
;
2537 return ftx232_dbus_write();
2540 static int signalyzer_init(void)
2548 return ftx232_dbus_write();
2551 static int axm0432_jtag_init(void)
2554 uint32_t bytes_written
;
2557 low_direction
= 0x2b;
2559 /* initialize low byte for jtag */
2560 buf
[0] = 0x80; /* command "set data bits low byte" */
2561 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2562 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2563 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2565 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2567 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2568 return ERROR_JTAG_INIT_FAILED
;
2571 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2574 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2576 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2580 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2585 high_direction
= 0x0c;
2587 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2588 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2590 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2594 high_output
|= nTRST
;
2597 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2599 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2603 high_output
|= nSRST
;
2606 /* initialize high port */
2607 buf
[0] = 0x82; /* command "set data bits high byte" */
2608 buf
[1] = high_output
; /* value */
2609 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2610 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2612 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2614 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2615 return ERROR_JTAG_INIT_FAILED
;
2621 static int redbee_init(void)
2624 uint32_t bytes_written
;
2627 low_direction
= 0x2b;
2629 /* initialize low byte for jtag */
2630 /* command "set data bits low byte" */
2632 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2633 buf
[2] = low_direction
;
2634 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2635 buf
[1] = low_output
;
2636 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2638 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2640 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2641 return ERROR_JTAG_INIT_FAILED
;
2645 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2647 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2650 high_direction
= 0x0c;
2652 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2653 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2655 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2659 high_output
|= nTRST
;
2662 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2664 LOG_ERROR("can't set nSRST to push-pull on redbee");
2668 high_output
|= nSRST
;
2671 /* initialize high port */
2672 buf
[0] = 0x82; /* command "set data bits high byte" */
2673 buf
[1] = high_output
; /* value */
2674 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2675 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2677 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2679 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2680 return ERROR_JTAG_INIT_FAILED
;
2686 static int jtagkey_init(void)
2689 uint32_t bytes_written
;
2692 low_direction
= 0x1b;
2694 /* initialize low byte for jtag */
2695 buf
[0] = 0x80; /* command "set data bits low byte" */
2696 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2697 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2698 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2700 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2702 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2703 return ERROR_JTAG_INIT_FAILED
;
2706 if (strcmp(layout
->name
, "jtagkey") == 0)
2713 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2714 || (strcmp(layout
->name
, "oocdlink") == 0))
2723 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2728 high_direction
= 0x0f;
2730 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2731 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2733 high_output
|= nTRSTnOE
;
2734 high_output
&= ~nTRST
;
2738 high_output
&= ~nTRSTnOE
;
2739 high_output
|= nTRST
;
2742 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2744 high_output
&= ~nSRSTnOE
;
2745 high_output
|= nSRST
;
2749 high_output
|= nSRSTnOE
;
2750 high_output
&= ~nSRST
;
2753 /* initialize high port */
2754 buf
[0] = 0x82; /* command "set data bits high byte" */
2755 buf
[1] = high_output
; /* value */
2756 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2757 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2759 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2761 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2762 return ERROR_JTAG_INIT_FAILED
;
2768 static int olimex_jtag_init(void)
2771 uint32_t bytes_written
;
2774 low_direction
= 0x1b;
2776 /* initialize low byte for jtag */
2777 buf
[0] = 0x80; /* command "set data bits low byte" */
2778 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2779 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2780 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2782 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2784 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2785 return ERROR_JTAG_INIT_FAILED
;
2791 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2794 high_direction
= 0x0f;
2796 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2797 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2799 high_output
|= nTRSTnOE
;
2800 high_output
&= ~nTRST
;
2804 high_output
&= ~nTRSTnOE
;
2805 high_output
|= nTRST
;
2808 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2810 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2814 high_output
&= ~nSRST
;
2817 /* turn red LED on */
2818 high_output
|= 0x08;
2820 /* initialize high port */
2821 buf
[0] = 0x82; /* command "set data bits high byte" */
2822 buf
[1] = high_output
; /* value */
2823 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2824 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2826 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2828 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2829 return ERROR_JTAG_INIT_FAILED
;
2835 static int flyswatter_init(void)
2838 uint32_t bytes_written
;
2841 low_direction
= 0xfb;
2843 /* initialize low byte for jtag */
2844 buf
[0] = 0x80; /* command "set data bits low byte" */
2845 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2846 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2847 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2849 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2851 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2852 return ERROR_JTAG_INIT_FAILED
;
2856 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2858 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2861 high_direction
= 0x0c;
2863 /* turn red LED3 on, LED2 off */
2864 high_output
|= 0x08;
2866 /* initialize high port */
2867 buf
[0] = 0x82; /* command "set data bits high byte" */
2868 buf
[1] = high_output
; /* value */
2869 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2870 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2872 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2874 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2875 return ERROR_JTAG_INIT_FAILED
;
2881 static int turtle_init(void)
2884 uint32_t bytes_written
;
2887 low_direction
= 0x5b;
2889 /* initialize low byte for jtag */
2890 buf
[0] = 0x80; /* command "set data bits low byte" */
2891 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2892 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2893 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2895 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2897 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2898 return ERROR_JTAG_INIT_FAILED
;
2904 high_direction
= 0x0C;
2906 /* initialize high port */
2907 buf
[0] = 0x82; /* command "set data bits high byte" */
2908 buf
[1] = high_output
;
2909 buf
[2] = high_direction
;
2910 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2912 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2914 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2915 return ERROR_JTAG_INIT_FAILED
;
2921 static int comstick_init(void)
2924 uint32_t bytes_written
;
2927 low_direction
= 0x0b;
2929 /* initialize low byte for jtag */
2930 buf
[0] = 0x80; /* command "set data bits low byte" */
2931 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2932 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2933 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2935 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2937 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2938 return ERROR_JTAG_INIT_FAILED
;
2942 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2944 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2947 high_direction
= 0x03;
2949 /* initialize high port */
2950 buf
[0] = 0x82; /* command "set data bits high byte" */
2951 buf
[1] = high_output
;
2952 buf
[2] = high_direction
;
2953 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2955 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2957 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2958 return ERROR_JTAG_INIT_FAILED
;
2964 static int stm32stick_init(void)
2967 uint32_t bytes_written
;
2970 low_direction
= 0x8b;
2972 /* initialize low byte for jtag */
2973 buf
[0] = 0x80; /* command "set data bits low byte" */
2974 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2975 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2976 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2978 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2980 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2981 return ERROR_JTAG_INIT_FAILED
;
2985 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2987 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2990 high_direction
= 0x03;
2992 /* initialize high port */
2993 buf
[0] = 0x82; /* command "set data bits high byte" */
2994 buf
[1] = high_output
;
2995 buf
[2] = high_direction
;
2996 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2998 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3000 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3001 return ERROR_JTAG_INIT_FAILED
;
3007 static int sheevaplug_init(void)
3010 uint32_t bytes_written
;
3013 low_direction
= 0x1b;
3015 /* initialize low byte for jtag */
3016 buf
[0] = 0x80; /* command "set data bits low byte" */
3017 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3018 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3019 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3021 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3023 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3024 return ERROR_JTAG_INIT_FAILED
;
3033 high_direction
= 0x0f;
3035 /* nTRST is always push-pull */
3036 high_output
&= ~nTRSTnOE
;
3037 high_output
|= nTRST
;
3039 /* nSRST is always open-drain */
3040 high_output
|= nSRSTnOE
;
3041 high_output
&= ~nSRST
;
3043 /* initialize high port */
3044 buf
[0] = 0x82; /* command "set data bits high byte" */
3045 buf
[1] = high_output
; /* value */
3046 buf
[2] = high_direction
; /* all outputs - xRST */
3047 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3049 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3051 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3052 return ERROR_JTAG_INIT_FAILED
;
3058 static int cortino_jtag_init(void)
3061 uint32_t bytes_written
;
3064 low_direction
= 0x1b;
3066 /* initialize low byte for jtag */
3067 buf
[0] = 0x80; /* command "set data bits low byte" */
3068 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3069 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3070 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3072 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3074 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3075 return ERROR_JTAG_INIT_FAILED
;
3079 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3081 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3084 high_direction
= 0x03;
3086 /* initialize high port */
3087 buf
[0] = 0x82; /* command "set data bits high byte" */
3088 buf
[1] = high_output
;
3089 buf
[2] = high_direction
;
3090 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3092 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3094 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3095 return ERROR_JTAG_INIT_FAILED
;
3101 static void olimex_jtag_blink(void)
3103 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3104 * ACBUS3 is bit 3 of the GPIOH port
3106 if (high_output
& 0x08)
3108 /* set port pin high */
3109 high_output
&= 0x07;
3113 /* set port pin low */
3114 high_output
|= 0x08;
3118 buffer_write(high_output
);
3119 buffer_write(high_direction
);
3122 static void flyswatter_jtag_blink(void)
3125 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3127 high_output
^= 0x0c;
3130 buffer_write(high_output
);
3131 buffer_write(high_direction
);
3134 static void turtle_jtag_blink(void)
3137 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3139 if (high_output
& 0x08)
3149 buffer_write(high_output
);
3150 buffer_write(high_direction
);
3153 static int ft2232_quit(void)
3155 #if BUILD_FT2232_FTD2XX == 1
3158 status
= FT_Close(ftdih
);
3159 #elif BUILD_FT2232_LIBFTDI == 1
3160 ftdi_usb_close(&ftdic
);
3162 ftdi_deinit(&ftdic
);
3165 free(ft2232_buffer
);
3166 ft2232_buffer
= NULL
;
3171 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3177 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3178 cp
= strchr(ft2232_device_desc
, 0);
3179 /* under Win32, the FTD2XX driver appends an "A" to the end
3180 * of the description, this examines the given desc
3181 * and creates the 'missing' _A or non_A variable. */
3182 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3183 /* it was, so make this the "A" version. */
3184 ft2232_device_desc_A
= ft2232_device_desc
;
3185 /* and *CREATE* the non-A version. */
3186 strcpy(buf
, ft2232_device_desc
);
3187 cp
= strchr(buf
, 0);
3189 ft2232_device_desc
= strdup(buf
);
3191 /* <space > A not defined
3193 sprintf(buf
, "%s A", ft2232_device_desc
);
3194 ft2232_device_desc_A
= strdup(buf
);
3199 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3205 COMMAND_HANDLER(ft2232_handle_serial_command
)
3209 ft2232_serial
= strdup(CMD_ARGV
[0]);
3213 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3219 COMMAND_HANDLER(ft2232_handle_layout_command
)
3221 if (CMD_ARGC
!= 1) {
3222 LOG_ERROR("Need exactly one argument to ft2232_layout");
3227 LOG_ERROR("already specified ft2232_layout %s",
3229 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3234 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3235 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3241 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3245 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3247 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3249 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3250 "(maximum is %d pairs)", MAX_USB_IDS
);
3251 CMD_ARGC
= MAX_USB_IDS
* 2;
3253 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3255 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3257 return ERROR_COMMAND_SYNTAX_ERROR
;
3258 /* remove the incomplete trailing id */
3263 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3265 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3266 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3270 * Explicitly terminate, in case there are multiples instances of
3273 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3278 COMMAND_HANDLER(ft2232_handle_latency_command
)
3282 ft2232_latency
= atoi(CMD_ARGV
[0]);
3286 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3292 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3296 /* 7 bits of either ones or zeros. */
3297 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3299 while (num_cycles
> 0)
3301 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3302 * at most 7 bits per invocation. Here we invoke it potentially
3305 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3307 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3309 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3310 retval
= ERROR_JTAG_QUEUE_FAILED
;
3315 /* there are no state transitions in this code, so omit state tracking */
3317 /* command "Clock Data to TMS/CS Pin (no Read)" */
3321 buffer_write(bitcount_per_command
- 1);
3323 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3328 num_cycles
-= bitcount_per_command
;
3334 /* ---------------------------------------------------------------------
3335 * Support for IceBear JTAG adapter from Section5:
3336 * http://section5.ch/icebear
3338 * Author: Sten, debian@sansys-electronic.com
3341 /* Icebear pin layout
3343 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3344 * GND GND | 4 3| n.c.
3345 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3346 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3347 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3348 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3349 * ADBUS2 TDO |14 13| GND GND
3351 * ADBUS0 O L TCK ACBUS0 GND
3352 * ADBUS1 O L TDI ACBUS1 GND
3353 * ADBUS2 I TDO ACBUS2 n.c.
3354 * ADBUS3 O H TMS ACBUS3 n.c.
3360 static int icebear_jtag_init(void) {
3362 uint32_t bytes_written
;
3364 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3365 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3369 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3370 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3371 low_direction
&= ~nTRST
; /* nTRST high impedance */
3374 low_direction
|= nTRST
;
3375 low_output
|= nTRST
;
3378 low_direction
|= nSRST
;
3379 low_output
|= nSRST
;
3381 /* initialize low byte for jtag */
3382 buf
[0] = 0x80; /* command "set data bits low byte" */
3383 buf
[1] = low_output
;
3384 buf
[2] = low_direction
;
3385 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3387 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3388 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3389 return ERROR_JTAG_INIT_FAILED
;
3393 high_direction
= 0x00;
3396 /* initialize high port */
3397 buf
[0] = 0x82; /* command "set data bits high byte" */
3398 buf
[1] = high_output
; /* value */
3399 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3400 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3402 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3403 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3404 return ERROR_JTAG_INIT_FAILED
;
3410 static void icebear_jtag_reset(int trst
, int srst
) {
3413 low_direction
|= nTRST
;
3414 low_output
&= ~nTRST
;
3416 else if (trst
== 0) {
3417 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3418 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3419 low_direction
&= ~nTRST
;
3421 low_output
|= nTRST
;
3425 low_output
&= ~nSRST
;
3427 else if (srst
== 0) {
3428 low_output
|= nSRST
;
3431 /* command "set data bits low byte" */
3433 buffer_write(low_output
);
3434 buffer_write(low_direction
);
3436 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3439 /* ---------------------------------------------------------------------
3440 * Support for Signalyzer H2 and Signalyzer H4
3441 * JTAG adapter from Xverve Technologies Inc.
3442 * http://www.signalyzer.com or http://www.xverve.com
3444 * Author: Oleg Seiljus, oleg@signalyzer.com
3446 static unsigned char signalyzer_h_side
;
3447 static unsigned int signalyzer_h_adapter_type
;
3449 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3451 #if BUILD_FT2232_FTD2XX == 1
3452 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3455 #define SIGNALYZER_COMMAND_ADDR 128
3456 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3458 #define SIGNALYZER_COMMAND_VERSION 0x41
3459 #define SIGNALYZER_COMMAND_RESET 0x42
3460 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3461 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3462 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3463 #define SIGNALYZER_COMMAND_LED_SET 0x53
3464 #define SIGNALYZER_COMMAND_ADC 0x54
3465 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3466 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3467 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3468 #define SIGNALYZER_COMMAND_I2C 0x58
3470 #define SIGNALYZER_CHAN_A 1
3471 #define SIGNALYZER_CHAN_B 2
3472 /* LEDS use channel C */
3473 #define SIGNALYZER_CHAN_C 4
3475 #define SIGNALYZER_LED_GREEN 1
3476 #define SIGNALYZER_LED_RED 2
3478 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3479 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3480 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3481 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3482 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3485 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3487 #if BUILD_FT2232_FTD2XX == 1
3488 return FT_WriteEE(ftdih
, address
, value
);
3489 #elif BUILD_FT2232_LIBFTDI == 1
3494 #if BUILD_FT2232_FTD2XX == 1
3495 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3497 return FT_ReadEE(ftdih
, address
, value
);
3501 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3502 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3504 unsigned char on_time
;
3505 unsigned char off_time
;
3507 if (on_time_ms
< 0xFFFF)
3508 on_time
= (unsigned char)(on_time_ms
/ 62);
3512 off_time
= (unsigned char)(off_time_ms
/ 62);
3514 #if BUILD_FT2232_FTD2XX == 1
3517 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3518 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3520 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3521 return ERROR_JTAG_DEVICE_ERROR
;
3524 if ((status
= signalyzer_h_ctrl_write(
3525 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3526 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3528 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3529 return ERROR_JTAG_DEVICE_ERROR
;
3532 if ((status
= signalyzer_h_ctrl_write(
3533 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3534 ((uint32_t)cycles
))) != FT_OK
)
3536 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3537 return ERROR_JTAG_DEVICE_ERROR
;
3540 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3541 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3543 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3544 return ERROR_JTAG_DEVICE_ERROR
;
3548 #elif BUILD_FT2232_LIBFTDI == 1
3551 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3552 ((uint32_t)(channel
<< 8) | led
))) < 0)
3554 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3555 ftdi_get_error_string(&ftdic
));
3556 return ERROR_JTAG_DEVICE_ERROR
;
3559 if ((retval
= signalyzer_h_ctrl_write(
3560 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3561 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3563 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3564 ftdi_get_error_string(&ftdic
));
3565 return ERROR_JTAG_DEVICE_ERROR
;
3568 if ((retval
= signalyzer_h_ctrl_write(
3569 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3570 (uint32_t)cycles
)) < 0)
3572 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3573 ftdi_get_error_string(&ftdic
));
3574 return ERROR_JTAG_DEVICE_ERROR
;
3577 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3578 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3580 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3581 ftdi_get_error_string(&ftdic
));
3582 return ERROR_JTAG_DEVICE_ERROR
;
3589 static int signalyzer_h_init(void)
3591 #if BUILD_FT2232_FTD2XX == 1
3598 uint16_t read_buf
[12] = { 0 };
3600 uint32_t bytes_written
;
3602 /* turn on center green led */
3603 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3604 0xFFFF, 0x00, 0x00);
3606 /* determine what channel config wants to open
3607 * TODO: change me... current implementation is made to work
3608 * with openocd description parsing.
3610 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3614 signalyzer_h_side
= *(end_of_desc
- 1);
3615 if (signalyzer_h_side
== 'B')
3616 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3618 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3622 LOG_ERROR("No Channel was specified");
3626 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3629 #if BUILD_FT2232_FTD2XX == 1
3630 /* read signalyzer versionining information */
3631 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3632 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3635 return ERROR_JTAG_DEVICE_ERROR
;
3638 for (i
= 0; i
< 10; i
++)
3640 if ((status
= signalyzer_h_ctrl_read(
3641 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3642 &read_buf
[i
])) != FT_OK
)
3644 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3646 return ERROR_JTAG_DEVICE_ERROR
;
3650 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3651 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3652 read_buf
[4], read_buf
[5], read_buf
[6]);
3654 /* set gpio register */
3655 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3656 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3658 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3659 return ERROR_JTAG_DEVICE_ERROR
;
3662 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3665 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3666 return ERROR_JTAG_DEVICE_ERROR
;
3669 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3670 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3672 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3673 return ERROR_JTAG_DEVICE_ERROR
;
3676 /* read adapter type information */
3677 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3678 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3680 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3681 return ERROR_JTAG_DEVICE_ERROR
;
3684 if ((status
= signalyzer_h_ctrl_write(
3685 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3687 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3688 return ERROR_JTAG_DEVICE_ERROR
;
3691 if ((status
= signalyzer_h_ctrl_write(
3692 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3694 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3695 return ERROR_JTAG_DEVICE_ERROR
;
3698 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3699 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3701 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3702 return ERROR_JTAG_DEVICE_ERROR
;
3707 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3708 &read_buf
[0])) != FT_OK
)
3710 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3711 return ERROR_JTAG_DEVICE_ERROR
;
3714 if (read_buf
[0] != 0x0498)
3715 signalyzer_h_adapter_type
= 0x0000;
3718 for (i
= 0; i
< 4; i
++)
3720 if ((status
= signalyzer_h_ctrl_read(
3721 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3722 &read_buf
[i
])) != FT_OK
)
3724 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3726 return ERROR_JTAG_DEVICE_ERROR
;
3730 signalyzer_h_adapter_type
= read_buf
[0];
3733 #elif BUILD_FT2232_LIBFTDI == 1
3734 /* currently libftdi does not allow reading individual eeprom
3735 * locations, therefore adapter type cannot be detected.
3736 * override with most common type
3738 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3741 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3743 /* ADAPTOR: EM_LT16_A */
3744 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3746 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3747 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3755 low_direction
= 0x1b;
3758 high_direction
= 0x0;
3760 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3762 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3763 low_output
&= ~nTRST
; /* nTRST = 0 */
3767 low_direction
|= nTRSTnOE
; /* nTRST output */
3768 low_output
|= nTRST
; /* nTRST = 1 */
3771 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3773 low_direction
|= nSRSTnOE
; /* nSRST output */
3774 low_output
|= nSRST
; /* nSRST = 1 */
3778 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3779 low_output
&= ~nSRST
; /* nSRST = 0 */
3782 #if BUILD_FT2232_FTD2XX == 1
3783 /* enable power to the module */
3784 if ((status
= signalyzer_h_ctrl_write(
3785 SIGNALYZER_DATA_BUFFER_ADDR
,
3786 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3789 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3791 return ERROR_JTAG_DEVICE_ERROR
;
3794 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3795 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3797 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3799 return ERROR_JTAG_DEVICE_ERROR
;
3802 /* set gpio mode register */
3803 if ((status
= signalyzer_h_ctrl_write(
3804 SIGNALYZER_DATA_BUFFER_ADDR
,
3805 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3807 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3809 return ERROR_JTAG_DEVICE_ERROR
;
3812 if ((status
= signalyzer_h_ctrl_write(
3813 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3816 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3818 return ERROR_JTAG_DEVICE_ERROR
;
3821 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3822 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3824 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3826 return ERROR_JTAG_DEVICE_ERROR
;
3829 /* set gpio register */
3830 if ((status
= signalyzer_h_ctrl_write(
3831 SIGNALYZER_DATA_BUFFER_ADDR
,
3832 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3834 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3836 return ERROR_JTAG_DEVICE_ERROR
;
3839 if ((status
= signalyzer_h_ctrl_write(
3840 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3843 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3845 return ERROR_JTAG_DEVICE_ERROR
;
3848 if ((status
= signalyzer_h_ctrl_write(
3849 SIGNALYZER_COMMAND_ADDR
,
3850 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3852 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3854 return ERROR_JTAG_DEVICE_ERROR
;
3859 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3860 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3861 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3862 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3863 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3865 if (signalyzer_h_adapter_type
3866 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3867 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3868 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3869 else if (signalyzer_h_adapter_type
3870 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3871 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3872 "(ARM JTAG with PSU) detected. (HW: %2x).",
3873 (read_buf
[1] >> 8));
3874 else if (signalyzer_h_adapter_type
3875 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3876 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3877 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3878 else if (signalyzer_h_adapter_type
3879 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3880 LOG_INFO("Signalyzer: EM-JTAG-P "
3881 "(Generic JTAG with PSU) detected. (HW: %2x).",
3882 (read_buf
[1] >> 8));
3890 low_direction
= 0x1b;
3893 high_direction
= 0x1f;
3895 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3897 high_output
|= nTRSTnOE
;
3898 high_output
&= ~nTRST
;
3902 high_output
&= ~nTRSTnOE
;
3903 high_output
|= nTRST
;
3906 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3908 high_output
&= ~nSRSTnOE
;
3909 high_output
|= nSRST
;
3913 high_output
|= nSRSTnOE
;
3914 high_output
&= ~nSRST
;
3917 #if BUILD_FT2232_FTD2XX == 1
3918 /* enable power to the module */
3919 if ((status
= signalyzer_h_ctrl_write(
3920 SIGNALYZER_DATA_BUFFER_ADDR
,
3921 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3924 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3926 return ERROR_JTAG_DEVICE_ERROR
;
3929 if ((status
= signalyzer_h_ctrl_write(
3930 SIGNALYZER_COMMAND_ADDR
,
3931 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3933 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3935 return ERROR_JTAG_DEVICE_ERROR
;
3938 /* set gpio mode register (IO_16 and IO_17 set as analog
3939 * inputs, other is gpio)
3941 if ((status
= signalyzer_h_ctrl_write(
3942 SIGNALYZER_DATA_BUFFER_ADDR
,
3943 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3945 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3947 return ERROR_JTAG_DEVICE_ERROR
;
3950 if ((status
= signalyzer_h_ctrl_write(
3951 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3954 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3956 return ERROR_JTAG_DEVICE_ERROR
;
3959 if ((status
= signalyzer_h_ctrl_write(
3960 SIGNALYZER_COMMAND_ADDR
,
3961 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3963 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3965 return ERROR_JTAG_DEVICE_ERROR
;
3968 /* set gpio register (all inputs, for -P modules,
3969 * PSU will be turned off)
3971 if ((status
= signalyzer_h_ctrl_write(
3972 SIGNALYZER_DATA_BUFFER_ADDR
,
3973 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3975 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3977 return ERROR_JTAG_DEVICE_ERROR
;
3980 if ((status
= signalyzer_h_ctrl_write(
3981 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3984 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3986 return ERROR_JTAG_DEVICE_ERROR
;
3989 if ((status
= signalyzer_h_ctrl_write(
3990 SIGNALYZER_COMMAND_ADDR
,
3991 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3993 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3995 return ERROR_JTAG_DEVICE_ERROR
;
4000 else if (signalyzer_h_adapter_type
== 0x0000)
4002 LOG_INFO("Signalyzer: No external modules were detected.");
4010 low_direction
= 0x1b;
4013 high_direction
= 0x0;
4015 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4017 low_direction
&= ~nTRSTnOE
; /* nTRST input */
4018 low_output
&= ~nTRST
; /* nTRST = 0 */
4022 low_direction
|= nTRSTnOE
; /* nTRST output */
4023 low_output
|= nTRST
; /* nTRST = 1 */
4026 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4028 low_direction
|= nSRSTnOE
; /* nSRST output */
4029 low_output
|= nSRST
; /* nSRST = 1 */
4033 low_direction
&= ~nSRSTnOE
; /* nSRST input */
4034 low_output
&= ~nSRST
; /* nSRST = 0 */
4039 LOG_ERROR("Unknown module type is detected: %.4x",
4040 signalyzer_h_adapter_type
);
4041 return ERROR_JTAG_DEVICE_ERROR
;
4044 /* initialize low byte of controller for jtag operation */
4046 buf
[1] = low_output
;
4047 buf
[2] = low_direction
;
4049 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4051 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4052 return ERROR_JTAG_INIT_FAILED
;
4055 #if BUILD_FT2232_FTD2XX == 1
4056 if (ftdi_device
== FT_DEVICE_2232H
)
4058 /* initialize high byte of controller for jtag operation */
4060 buf
[1] = high_output
;
4061 buf
[2] = high_direction
;
4063 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4065 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4066 return ERROR_JTAG_INIT_FAILED
;
4069 #elif BUILD_FT2232_LIBFTDI == 1
4070 if (ftdi_device
== TYPE_2232H
)
4072 /* initialize high byte of controller for jtag operation */
4074 buf
[1] = high_output
;
4075 buf
[2] = high_direction
;
4077 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4079 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4080 return ERROR_JTAG_INIT_FAILED
;
4087 static void signalyzer_h_reset(int trst
, int srst
)
4089 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4091 /* ADAPTOR: EM_LT16_A */
4092 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4096 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4097 /* switch to output pin (output is low) */
4098 low_direction
|= nTRSTnOE
;
4100 /* switch output low */
4101 low_output
&= ~nTRST
;
4105 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4106 /* switch to input pin (high-Z + internal
4107 * and external pullup) */
4108 low_direction
&= ~nTRSTnOE
;
4110 /* switch output high */
4111 low_output
|= nTRST
;
4116 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4117 /* switch output low */
4118 low_output
&= ~nSRST
;
4120 /* switch to output pin (output is low) */
4121 low_direction
|= nSRSTnOE
;
4125 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4126 /* switch output high */
4127 low_output
|= nSRST
;
4129 /* switch to input pin (high-Z) */
4130 low_direction
&= ~nSRSTnOE
;
4133 /* command "set data bits low byte" */
4135 buffer_write(low_output
);
4136 buffer_write(low_direction
);
4137 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4138 "low_direction: 0x%2.2x",
4139 trst
, srst
, low_output
, low_direction
);
4141 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4142 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4143 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4144 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4145 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4149 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4150 high_output
&= ~nTRSTnOE
;
4152 high_output
&= ~nTRST
;
4156 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4157 high_output
|= nTRSTnOE
;
4159 high_output
|= nTRST
;
4164 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4165 high_output
&= ~nSRST
;
4167 high_output
&= ~nSRSTnOE
;
4171 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4172 high_output
|= nSRST
;
4174 high_output
|= nSRSTnOE
;
4177 /* command "set data bits high byte" */
4179 buffer_write(high_output
);
4180 buffer_write(high_direction
);
4181 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4182 "high_direction: 0x%2.2x",
4183 trst
, srst
, high_output
, high_direction
);
4185 else if (signalyzer_h_adapter_type
== 0x0000)
4189 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4190 /* switch to output pin (output is low) */
4191 low_direction
|= nTRSTnOE
;
4193 /* switch output low */
4194 low_output
&= ~nTRST
;
4198 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4199 /* switch to input pin (high-Z + internal
4200 * and external pullup) */
4201 low_direction
&= ~nTRSTnOE
;
4203 /* switch output high */
4204 low_output
|= nTRST
;
4209 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4210 /* switch output low */
4211 low_output
&= ~nSRST
;
4213 /* switch to output pin (output is low) */
4214 low_direction
|= nSRSTnOE
;
4218 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4219 /* switch output high */
4220 low_output
|= nSRST
;
4222 /* switch to input pin (high-Z) */
4223 low_direction
&= ~nSRSTnOE
;
4226 /* command "set data bits low byte" */
4228 buffer_write(low_output
);
4229 buffer_write(low_direction
);
4230 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4231 "low_direction: 0x%2.2x",
4232 trst
, srst
, low_output
, low_direction
);
4236 static void signalyzer_h_blink(void)
4238 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4241 /********************************************************************
4242 * Support for KT-LINK
4243 * JTAG adapter from KRISTECH
4244 * http://www.kristech.eu
4245 *******************************************************************/
4246 static int ktlink_init(void)
4249 uint32_t bytes_written
;
4250 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4252 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4253 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4255 // initialize low port
4256 buf
[0] = 0x80; // command "set data bits low byte"
4257 buf
[1] = low_output
;
4258 buf
[2] = low_direction
;
4259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4261 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4263 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4264 return ERROR_JTAG_INIT_FAILED
;
4272 high_output
= 0x80; // turn LED on
4273 high_direction
= 0xFF; // all outputs
4275 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4277 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4278 high_output
|= nTRSTnOE
;
4279 high_output
&= ~nTRST
;
4281 high_output
&= ~nTRSTnOE
;
4282 high_output
|= nTRST
;
4285 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4286 high_output
&= ~nSRSTnOE
;
4287 high_output
|= nSRST
;
4289 high_output
|= nSRSTnOE
;
4290 high_output
&= ~nSRST
;
4293 // initialize high port
4294 buf
[0] = 0x82; // command "set data bits high byte"
4295 buf
[1] = high_output
; // value
4296 buf
[2] = high_direction
;
4297 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4299 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4301 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4302 return ERROR_JTAG_INIT_FAILED
;
4308 static void ktlink_reset(int trst
, int srst
)
4310 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4313 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4314 high_output
&= ~nTRSTnOE
;
4316 high_output
&= ~nTRST
;
4317 } else if (trst
== 0) {
4318 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4319 high_output
|= nTRSTnOE
;
4321 high_output
|= nTRST
;
4325 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4326 high_output
&= ~nSRST
;
4328 high_output
&= ~nSRSTnOE
;
4329 } else if (srst
== 0) {
4330 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4331 high_output
|= nSRST
;
4333 high_output
|= nSRSTnOE
;
4336 buffer_write(0x82); // command "set data bits high byte"
4337 buffer_write(high_output
);
4338 buffer_write(high_direction
);
4339 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4342 static void ktlink_blink(void)
4344 /* LED connected to ACBUS7 */
4345 if (high_output
& 0x80)
4346 high_output
&= 0x7F;
4348 high_output
|= 0x80;
4350 buffer_write(0x82); // command "set data bits high byte"
4351 buffer_write(high_output
);
4352 buffer_write(high_direction
);
4355 static const struct command_registration ft2232_command_handlers
[] = {
4357 .name
= "ft2232_device_desc",
4358 .handler
= &ft2232_handle_device_desc_command
,
4359 .mode
= COMMAND_CONFIG
,
4360 .help
= "set the USB device description of the FTDI FT2232 device",
4361 .usage
= "description_string",
4364 .name
= "ft2232_serial",
4365 .handler
= &ft2232_handle_serial_command
,
4366 .mode
= COMMAND_CONFIG
,
4367 .help
= "set the serial number of the FTDI FT2232 device",
4368 .usage
= "serial_string",
4371 .name
= "ft2232_layout",
4372 .handler
= &ft2232_handle_layout_command
,
4373 .mode
= COMMAND_CONFIG
,
4374 .help
= "set the layout of the FT2232 GPIO signals used "
4375 "to control output-enables and reset signals",
4376 .usage
= "layout_name",
4379 .name
= "ft2232_vid_pid",
4380 .handler
= &ft2232_handle_vid_pid_command
,
4381 .mode
= COMMAND_CONFIG
,
4382 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4383 .usage
= "(vid pid)* ",
4386 .name
= "ft2232_latency",
4387 .handler
= &ft2232_handle_latency_command
,
4388 .mode
= COMMAND_CONFIG
,
4389 .help
= "set the FT2232 latency timer to a new value",
4392 COMMAND_REGISTRATION_DONE
4395 struct jtag_interface ft2232_interface
= {
4397 .supported
= DEBUG_CAP_TMS_SEQ
,
4398 .commands
= ft2232_command_handlers
,
4399 .transports
= jtag_only
,
4401 .init
= ft2232_init
,
4402 .quit
= ft2232_quit
,
4403 .speed
= ft2232_speed
,
4404 .speed_div
= ft2232_speed_div
,
4406 .execute_queue
= ft2232_execute_queue
,