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. Since JTAG uses
36 * only one of the two ports on these devices, on integrated boards the
37 * second port often serves as a USB-to-serial adapter for the target's
38 * console UART even when the JTAG port is not in use. (Systems which
39 * support ARM's SWD in addition to JTAG, or instead of it, may use that
40 * second port for reading SWV trace data.)
42 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
43 * request/response interactions involve round trips over the USB link.
44 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
45 * can for example poll quickly for a status change (usually taking on the
46 * order of microseconds not milliseconds) before beginning a queued
47 * transaction which require the previous one to have completed.
49 * There are dozens of adapters of this type, differing in details which
50 * this driver needs to understand. Those "layout" details are required
51 * as part of FT2232 driver configuration.
53 * This code uses information contained in the MPSSE specification which was
55 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
56 * Hereafter this is called the "MPSSE Spec".
58 * The datasheet for the ftdichip.com's FT2232D part is here:
59 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
61 * Also note the issue with code 0x4b (clock data to TMS) noted in
62 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
63 * which can affect longer JTAG state paths.
70 /* project specific includes */
71 #include <jtag/interface.h>
72 #include <helper/time_support.h>
80 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
81 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
82 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
83 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
86 /* FT2232 access library includes */
87 #if BUILD_FT2232_FTD2XX == 1
89 #elif BUILD_FT2232_LIBFTDI == 1
93 /* max TCK for the high speed devices 30000 kHz */
94 #define FTDI_2232H_4232H_MAX_TCK 30000
95 /* max TCK for the full speed devices 6000 kHz */
96 #define FTDI_2232C_MAX_TCK 6000
97 /* this speed value tells that RTCK is requested */
101 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
102 * errors with a retry count of 100. Increasing it solves the problem for me.
105 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
106 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
109 #define LIBFTDI_READ_RETRY_COUNT 2000
111 #ifndef BUILD_FT2232_HIGHSPEED
112 #if BUILD_FT2232_FTD2XX == 1
113 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
114 #elif BUILD_FT2232_LIBFTDI == 1
115 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
120 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
121 * stable state. Calling code must ensure that current state is stable,
122 * that verification is not done in here.
124 * @param num_cycles The number of clocks cycles to send.
125 * @param cmd The command to send.
127 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
129 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
131 static char * ft2232_device_desc_A
= NULL
;
132 static char* ft2232_device_desc
= NULL
;
133 static char* ft2232_serial
= NULL
;
134 static char* ft2232_layout
= NULL
;
135 static uint8_t ft2232_latency
= 2;
136 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
138 #define MAX_USB_IDS 8
139 /* vid = pid = 0 marks the end of the list */
140 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
141 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
143 struct ft2232_layout
{
146 void (*reset
)(int trst
, int srst
);
151 /* init procedures for supported layouts */
152 static int usbjtag_init(void);
153 static int jtagkey_init(void);
154 static int olimex_jtag_init(void);
155 static int flyswatter_init(void);
156 static int turtle_init(void);
157 static int comstick_init(void);
158 static int stm32stick_init(void);
159 static int axm0432_jtag_init(void);
160 static int sheevaplug_init(void);
161 static int icebear_jtag_init(void);
162 static int cortino_jtag_init(void);
163 static int signalyzer_h_init(void);
164 static int ktlink_init(void);
165 static int redbee_init(void);
167 /* reset procedures for supported layouts */
168 static void usbjtag_reset(int trst
, int srst
);
169 static void jtagkey_reset(int trst
, int srst
);
170 static void olimex_jtag_reset(int trst
, int srst
);
171 static void flyswatter_reset(int trst
, int srst
);
172 static void turtle_reset(int trst
, int srst
);
173 static void comstick_reset(int trst
, int srst
);
174 static void stm32stick_reset(int trst
, int srst
);
175 static void axm0432_jtag_reset(int trst
, int srst
);
176 static void sheevaplug_reset(int trst
, int srst
);
177 static void icebear_jtag_reset(int trst
, int srst
);
178 static void signalyzer_h_reset(int trst
, int srst
);
179 static void ktlink_reset(int trst
, int srst
);
180 static void redbee_reset(int trst
, int srst
);
182 /* blink procedures for layouts that support a blinking led */
183 static void olimex_jtag_blink(void);
184 static void flyswatter_jtag_blink(void);
185 static void turtle_jtag_blink(void);
186 static void signalyzer_h_blink(void);
187 static void ktlink_blink(void);
189 static const struct ft2232_layout ft2232_layouts
[] =
192 .init
= usbjtag_init
,
193 .reset
= usbjtag_reset
,
196 .init
= jtagkey_init
,
197 .reset
= jtagkey_reset
,
199 { .name
= "jtagkey_prototype_v1",
200 .init
= jtagkey_init
,
201 .reset
= jtagkey_reset
,
203 { .name
= "oocdlink",
204 .init
= jtagkey_init
,
205 .reset
= jtagkey_reset
,
207 { .name
= "signalyzer",
208 .init
= usbjtag_init
,
209 .reset
= usbjtag_reset
,
211 { .name
= "evb_lm3s811",
212 .init
= usbjtag_init
,
213 .reset
= usbjtag_reset
,
215 { .name
= "luminary_icdi",
216 .init
= usbjtag_init
,
217 .reset
= usbjtag_reset
,
219 { .name
= "olimex-jtag",
220 .init
= olimex_jtag_init
,
221 .reset
= olimex_jtag_reset
,
222 .blink
= olimex_jtag_blink
224 { .name
= "flyswatter",
225 .init
= flyswatter_init
,
226 .reset
= flyswatter_reset
,
227 .blink
= flyswatter_jtag_blink
229 { .name
= "turtelizer2",
231 .reset
= turtle_reset
,
232 .blink
= turtle_jtag_blink
234 { .name
= "comstick",
235 .init
= comstick_init
,
236 .reset
= comstick_reset
,
238 { .name
= "stm32stick",
239 .init
= stm32stick_init
,
240 .reset
= stm32stick_reset
,
242 { .name
= "axm0432_jtag",
243 .init
= axm0432_jtag_init
,
244 .reset
= axm0432_jtag_reset
,
246 { .name
= "sheevaplug",
247 .init
= sheevaplug_init
,
248 .reset
= sheevaplug_reset
,
251 .init
= icebear_jtag_init
,
252 .reset
= icebear_jtag_reset
,
255 .init
= cortino_jtag_init
,
256 .reset
= comstick_reset
,
258 { .name
= "signalyzer-h",
259 .init
= signalyzer_h_init
,
260 .reset
= signalyzer_h_reset
,
261 .blink
= signalyzer_h_blink
265 .reset
= ktlink_reset
,
266 .blink
= ktlink_blink
268 { .name
= "redbee-econotag",
270 .reset
= redbee_reset
,
272 { .name
= "redbee-usb",
274 .reset
= redbee_reset
,
275 .channel
= INTERFACE_B
,
277 { .name
= NULL
, /* END OF TABLE */ },
280 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
282 static const struct ft2232_layout
*layout
;
283 static uint8_t low_output
= 0x0;
284 static uint8_t low_direction
= 0x0;
285 static uint8_t high_output
= 0x0;
286 static uint8_t high_direction
= 0x0;
288 #if BUILD_FT2232_FTD2XX == 1
289 static FT_HANDLE ftdih
= NULL
;
290 static FT_DEVICE ftdi_device
= 0;
291 #elif BUILD_FT2232_LIBFTDI == 1
292 static struct ftdi_context ftdic
;
293 static enum ftdi_chip_type ftdi_device
;
296 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
297 static int require_send
;
299 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
301 "There is a significant difference between libftdi and libftd2xx. The latter
302 one allows to schedule up to 64*64 bytes of result data while libftdi fails
303 with more than 4*64. As a consequence, the FT2232 driver is forced to
304 perform around 16x more USB transactions for long command streams with TDO
305 capture when running with libftdi."
308 #define FT2232_BUFFER_SIZE 131072
309 a comment would have been nice.
312 #define FT2232_BUFFER_SIZE 131072
314 static uint8_t* ft2232_buffer
= NULL
;
315 static int ft2232_buffer_size
= 0;
316 static int ft2232_read_pointer
= 0;
317 static int ft2232_expect_read
= 0;
320 * Function buffer_write
321 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
322 * @param val is the byte to send.
324 static inline void buffer_write(uint8_t val
)
326 assert(ft2232_buffer
);
327 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
328 ft2232_buffer
[ft2232_buffer_size
++] = val
;
332 * Function buffer_read
333 * returns a byte from the byte buffer.
335 static inline uint8_t buffer_read(void)
337 assert(ft2232_buffer
);
338 assert(ft2232_read_pointer
< ft2232_buffer_size
);
339 return ft2232_buffer
[ft2232_read_pointer
++];
343 * Clocks out \a bit_count bits on the TMS line, starting with the least
344 * significant bit of tms_bits and progressing to more significant bits.
345 * Rigorous state transition logging is done here via tap_set_state().
347 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
348 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
349 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
350 * is often used for this, 0x4b.
352 * @param tms_bits Holds the sequence of bits to send.
353 * @param tms_count Tells how many bits in the sequence.
354 * @param tdi_bit A single bit to pass on to TDI before the first TCK
355 * cycle and held static for the duration of TMS clocking.
357 * See the MPSSE spec referenced above.
359 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
363 int tms_ndx
; /* bit index into tms_byte */
365 assert(tms_count
> 0);
367 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
368 mpsse_cmd
, tms_bits
, tms_count
);
370 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
372 bool bit
= tms_bits
& 1;
375 tms_byte
|= (1 << tms_ndx
);
377 /* always do state transitions in public view */
378 tap_set_state(tap_state_transition(tap_get_state(), bit
));
380 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
385 if (tms_ndx
== 7 || i
== tms_count
-1)
387 buffer_write(mpsse_cmd
);
388 buffer_write(tms_ndx
- 1);
390 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
391 TMS/CS and is held static for the duration of TMS/CS clocking.
393 buffer_write(tms_byte
| (tdi_bit
<< 7));
399 * Function get_tms_buffer_requirements
400 * returns what clock_tms() will consume if called with
403 static inline int get_tms_buffer_requirements(int bit_count
)
405 return ((bit_count
+ 6)/7) * 3;
409 * Function move_to_state
410 * moves the TAP controller from the current state to a
411 * \a goal_state through a path given by tap_get_tms_path(). State transition
412 * logging is performed by delegation to clock_tms().
414 * @param goal_state is the destination state for the move.
416 static void move_to_state(tap_state_t goal_state
)
418 tap_state_t start_state
= tap_get_state();
420 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
421 lookup of the required TMS pattern to move to this state from the
425 /* do the 2 lookups */
426 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
427 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
429 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
431 clock_tms(0x4b, tms_bits
, tms_count
, 0);
434 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
436 #if BUILD_FT2232_FTD2XX == 1
438 DWORD dw_bytes_written
;
439 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
441 *bytes_written
= dw_bytes_written
;
442 LOG_ERROR("FT_Write returned: %lu", status
);
443 return ERROR_JTAG_DEVICE_ERROR
;
447 *bytes_written
= dw_bytes_written
;
450 #elif BUILD_FT2232_LIBFTDI == 1
452 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
455 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
456 return ERROR_JTAG_DEVICE_ERROR
;
460 *bytes_written
= retval
;
466 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
468 #if BUILD_FT2232_FTD2XX == 1
474 while ((*bytes_read
< size
) && timeout
--)
476 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
477 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
480 LOG_ERROR("FT_Read returned: %lu", status
);
481 return ERROR_JTAG_DEVICE_ERROR
;
483 *bytes_read
+= dw_bytes_read
;
486 #elif BUILD_FT2232_LIBFTDI == 1
488 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
491 while ((*bytes_read
< size
) && timeout
--)
493 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
496 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
497 return ERROR_JTAG_DEVICE_ERROR
;
499 *bytes_read
+= retval
;
504 if (*bytes_read
< size
)
506 LOG_ERROR("couldn't read enough bytes from "
507 "FT2232 device (%i < %i)",
508 (unsigned)*bytes_read
,
510 return ERROR_JTAG_DEVICE_ERROR
;
516 static bool ft2232_device_is_highspeed(void)
518 #if BUILD_FT2232_FTD2XX == 1
519 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
520 #elif BUILD_FT2232_LIBFTDI == 1
521 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
526 * Commands that only apply to the FT2232H and FT4232H devices.
527 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
528 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
531 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
533 uint8_t buf
= enable
? 0x96 : 0x97;
534 LOG_DEBUG("%2.2x", buf
);
536 uint32_t bytes_written
;
537 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
538 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
540 LOG_ERROR("couldn't write command to %s adaptive clocking"
541 , enable
? "enable" : "disable");
549 * Enable/disable the clk divide by 5 of the 60MHz master clock.
550 * This result in a JTAG clock speed range of 91.553Hz-6MHz
551 * respective 457.763Hz-30MHz.
553 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
555 uint32_t bytes_written
;
556 uint8_t buf
= enable
? 0x8b : 0x8a;
557 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
558 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
560 LOG_ERROR("couldn't write command to %s clk divide by 5"
561 , enable
? "enable" : "disable");
562 return ERROR_JTAG_INIT_FAILED
;
564 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
565 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
570 static int ft2232_speed(int speed
)
574 uint32_t bytes_written
;
577 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
578 if (ft2232_device_is_highspeed())
579 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
580 else if (enable_adaptive_clocking
)
582 LOG_ERROR("ft2232 device %lu does not support RTCK"
583 , (long unsigned int)ftdi_device
);
587 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
590 buf
[0] = 0x86; /* command "set divisor" */
591 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
592 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
594 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
595 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
597 LOG_ERROR("couldn't set FT2232 TCK speed");
604 static int ft2232_speed_div(int speed
, int* khz
)
606 /* Take a look in the FT2232 manual,
607 * AN2232C-01 Command Processor for
608 * MPSSE and MCU Host Bus. Chapter 3.8 */
610 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
615 static int ft2232_khz(int khz
, int* jtag_speed
)
619 if (ft2232_device_is_highspeed())
621 *jtag_speed
= RTCK_SPEED
;
626 LOG_DEBUG("RCLK not supported");
631 /* Take a look in the FT2232 manual,
632 * AN2232C-01 Command Processor for
633 * MPSSE and MCU Host Bus. Chapter 3.8
635 * We will calc here with a multiplier
636 * of 10 for better rounding later. */
638 /* Calc speed, (ft2232_max_tck / khz) - 1 */
639 /* Use 65000 for better rounding */
640 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
642 /* Add 0.9 for rounding */
645 /* Calc real speed */
646 *jtag_speed
= *jtag_speed
/ 10;
648 /* Check if speed is greater than 0 */
654 /* Check max value */
655 if (*jtag_speed
> 0xFFFF)
657 *jtag_speed
= 0xFFFF;
663 static void ft2232_end_state(tap_state_t state
)
665 if (tap_is_state_stable(state
))
666 tap_set_end_state(state
);
669 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
674 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
676 int num_bytes
= (scan_size
+ 7) / 8;
677 int bits_left
= scan_size
;
680 while (num_bytes
-- > 1)
682 buffer
[cur_byte
++] = buffer_read();
686 buffer
[cur_byte
] = 0x0;
688 /* There is one more partial byte left from the clock data in/out instructions */
691 buffer
[cur_byte
] = buffer_read() >> 1;
693 /* 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 */
694 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
697 static void ft2232_debug_dump_buffer(void)
703 for (i
= 0; i
< ft2232_buffer_size
; i
++)
705 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
708 LOG_DEBUG("%s", line
);
714 LOG_DEBUG("%s", line
);
717 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
719 struct jtag_command
* cmd
;
724 uint32_t bytes_written
= 0;
725 uint32_t bytes_read
= 0;
727 #ifdef _DEBUG_USB_IO_
728 struct timeval start
, inter
, inter2
, end
;
729 struct timeval d_inter
, d_inter2
, d_end
;
732 #ifdef _DEBUG_USB_COMMS_
733 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
734 ft2232_debug_dump_buffer();
737 #ifdef _DEBUG_USB_IO_
738 gettimeofday(&start
, NULL
);
741 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
743 LOG_ERROR("couldn't write MPSSE commands to FT2232");
747 #ifdef _DEBUG_USB_IO_
748 gettimeofday(&inter
, NULL
);
751 if (ft2232_expect_read
)
753 /* FIXME this "timeout" is never changed ... */
754 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
755 ft2232_buffer_size
= 0;
757 #ifdef _DEBUG_USB_IO_
758 gettimeofday(&inter2
, NULL
);
761 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
763 LOG_ERROR("couldn't read from FT2232");
767 #ifdef _DEBUG_USB_IO_
768 gettimeofday(&end
, NULL
);
770 timeval_subtract(&d_inter
, &inter
, &start
);
771 timeval_subtract(&d_inter2
, &inter2
, &start
);
772 timeval_subtract(&d_end
, &end
, &start
);
774 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
775 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
776 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
777 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
780 ft2232_buffer_size
= bytes_read
;
782 if (ft2232_expect_read
!= ft2232_buffer_size
)
784 LOG_ERROR("ft2232_expect_read (%i) != "
785 "ft2232_buffer_size (%i) "
789 LIBFTDI_READ_RETRY_COUNT
- timeout
);
790 ft2232_debug_dump_buffer();
795 #ifdef _DEBUG_USB_COMMS_
796 LOG_DEBUG("read buffer (%i retries): %i bytes",
797 LIBFTDI_READ_RETRY_COUNT
- timeout
,
799 ft2232_debug_dump_buffer();
803 ft2232_expect_read
= 0;
804 ft2232_read_pointer
= 0;
806 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
807 * that wasn't handled by a caller-provided error handler
817 type
= jtag_scan_type(cmd
->cmd
.scan
);
818 if (type
!= SCAN_OUT
)
820 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
821 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
822 ft2232_read_scan(type
, buffer
, scan_size
);
823 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
824 retval
= ERROR_JTAG_QUEUE_FAILED
;
836 ft2232_buffer_size
= 0;
842 * Function ft2232_add_pathmove
843 * moves the TAP controller from the current state to a new state through the
844 * given path, where path is an array of tap_state_t's.
846 * @param path is an array of tap_stat_t which gives the states to traverse through
847 * ending with the last state at path[num_states-1]
848 * @param num_states is the count of state steps to move through
850 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
854 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
858 /* this loop verifies that the path is legal and logs each state in the path */
861 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
863 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
865 /* command "Clock Data to TMS/CS Pin (no Read)" */
868 /* number of states remaining */
869 buffer_write(num_states_batch
- 1);
871 while (num_states_batch
--) {
872 /* either TMS=0 or TMS=1 must work ... */
873 if (tap_state_transition(tap_get_state(), false)
874 == path
[state_count
])
875 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
876 else if (tap_state_transition(tap_get_state(), true)
877 == path
[state_count
])
878 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
880 /* ... or else the caller goofed BADLY */
882 LOG_ERROR("BUG: %s -> %s isn't a valid "
883 "TAP state transition",
884 tap_state_name(tap_get_state()),
885 tap_state_name(path
[state_count
]));
889 tap_set_state(path
[state_count
]);
894 buffer_write(tms_byte
);
896 tap_set_end_state(tap_get_state());
899 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
901 int num_bytes
= (scan_size
+ 7) / 8;
902 int bits_left
= scan_size
;
908 if (tap_get_state() != TAP_DRSHIFT
)
910 move_to_state(TAP_DRSHIFT
);
915 if (tap_get_state() != TAP_IRSHIFT
)
917 move_to_state(TAP_IRSHIFT
);
921 /* add command for complete bytes */
922 while (num_bytes
> 1)
927 /* Clock Data Bytes In and Out LSB First */
929 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
931 else if (type
== SCAN_OUT
)
933 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
935 /* LOG_DEBUG("added TDI bytes (o)"); */
937 else if (type
== SCAN_IN
)
939 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
941 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
944 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
945 num_bytes
-= thisrun_bytes
;
947 buffer_write((uint8_t) (thisrun_bytes
- 1));
948 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
952 /* add complete bytes */
953 while (thisrun_bytes
-- > 0)
955 buffer_write(buffer
[cur_byte
++]);
959 else /* (type == SCAN_IN) */
961 bits_left
-= 8 * (thisrun_bytes
);
965 /* the most signifcant bit is scanned during TAP movement */
967 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
971 /* process remaining bits but the last one */
976 /* Clock Data Bits In and Out LSB First */
978 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
980 else if (type
== SCAN_OUT
)
982 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
984 /* LOG_DEBUG("added TDI bits (o)"); */
986 else if (type
== SCAN_IN
)
988 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
990 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
993 buffer_write(bits_left
- 2);
995 buffer_write(buffer
[cur_byte
]);
998 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
999 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1001 if (type
== SCAN_IO
)
1003 /* Clock Data Bits In and Out LSB First */
1005 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1007 else if (type
== SCAN_OUT
)
1009 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1011 /* LOG_DEBUG("added TDI bits (o)"); */
1013 else if (type
== SCAN_IN
)
1015 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1017 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1020 buffer_write(last_bit
);
1028 /* move from Shift-IR/DR to end state */
1029 if (type
!= SCAN_OUT
)
1031 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1032 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1035 /* Clock Data to TMS/CS Pin with Read */
1040 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1041 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1042 /* Clock Data to TMS/CS Pin (no Read) */
1046 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1047 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1050 if (tap_get_state() != tap_get_end_state())
1052 move_to_state(tap_get_end_state());
1056 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1058 int num_bytes
= (scan_size
+ 7) / 8;
1059 int bits_left
= scan_size
;
1062 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1063 uint8_t* receive_pointer
= receive_buffer
;
1064 uint32_t bytes_written
;
1065 uint32_t bytes_read
;
1067 int thisrun_read
= 0;
1071 LOG_ERROR("BUG: large IR scans are not supported");
1075 if (tap_get_state() != TAP_DRSHIFT
)
1077 move_to_state(TAP_DRSHIFT
);
1080 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1082 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1085 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1086 ft2232_buffer_size
, (int)bytes_written
);
1087 ft2232_buffer_size
= 0;
1089 /* add command for complete bytes */
1090 while (num_bytes
> 1)
1094 if (type
== SCAN_IO
)
1096 /* Clock Data Bytes In and Out LSB First */
1098 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1100 else if (type
== SCAN_OUT
)
1102 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1104 /* LOG_DEBUG("added TDI bytes (o)"); */
1106 else if (type
== SCAN_IN
)
1108 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1110 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1113 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1114 thisrun_read
= thisrun_bytes
;
1115 num_bytes
-= thisrun_bytes
;
1116 buffer_write((uint8_t) (thisrun_bytes
- 1));
1117 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1119 if (type
!= SCAN_IN
)
1121 /* add complete bytes */
1122 while (thisrun_bytes
-- > 0)
1124 buffer_write(buffer
[cur_byte
]);
1129 else /* (type == SCAN_IN) */
1131 bits_left
-= 8 * (thisrun_bytes
);
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",
1141 (int)bytes_written
);
1142 ft2232_buffer_size
= 0;
1144 if (type
!= SCAN_OUT
)
1146 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1148 LOG_ERROR("couldn't read from FT2232");
1151 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1154 receive_pointer
+= bytes_read
;
1160 /* the most signifcant bit is scanned during TAP movement */
1161 if (type
!= SCAN_IN
)
1162 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1166 /* process remaining bits but the last one */
1169 if (type
== SCAN_IO
)
1171 /* Clock Data Bits In and Out LSB First */
1173 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1175 else if (type
== SCAN_OUT
)
1177 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1179 /* LOG_DEBUG("added TDI bits (o)"); */
1181 else if (type
== SCAN_IN
)
1183 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1185 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1187 buffer_write(bits_left
- 2);
1188 if (type
!= SCAN_IN
)
1189 buffer_write(buffer
[cur_byte
]);
1191 if (type
!= SCAN_OUT
)
1195 if (tap_get_end_state() == TAP_DRSHIFT
)
1197 if (type
== SCAN_IO
)
1199 /* Clock Data Bits In and Out LSB First */
1201 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1203 else if (type
== SCAN_OUT
)
1205 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1207 /* LOG_DEBUG("added TDI bits (o)"); */
1209 else if (type
== SCAN_IN
)
1211 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1213 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1216 buffer_write(last_bit
);
1220 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1221 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1224 /* move from Shift-IR/DR to end state */
1225 if (type
!= SCAN_OUT
)
1227 /* Clock Data to TMS/CS Pin with Read */
1229 /* LOG_DEBUG("added TMS scan (read)"); */
1233 /* Clock Data to TMS/CS Pin (no Read) */
1235 /* LOG_DEBUG("added TMS scan (no read)"); */
1238 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1239 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1242 if (type
!= SCAN_OUT
)
1245 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1247 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1250 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1252 (int)bytes_written
);
1253 ft2232_buffer_size
= 0;
1255 if (type
!= SCAN_OUT
)
1257 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1259 LOG_ERROR("couldn't read from FT2232");
1262 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1265 receive_pointer
+= bytes_read
;
1271 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1273 int predicted_size
= 3;
1274 int num_bytes
= (scan_size
- 1) / 8;
1276 if (tap_get_state() != TAP_DRSHIFT
)
1277 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1279 if (type
== SCAN_IN
) /* only from device to host */
1281 /* complete bytes */
1282 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1284 /* remaining bits - 1 (up to 7) */
1285 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1287 else /* host to device, or bidirectional */
1289 /* complete bytes */
1290 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1292 /* remaining bits -1 (up to 7) */
1293 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1296 return predicted_size
;
1299 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1301 int predicted_size
= 0;
1303 if (type
!= SCAN_OUT
)
1305 /* complete bytes */
1306 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1308 /* remaining bits - 1 */
1309 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1311 /* last bit (from TMS scan) */
1312 predicted_size
+= 1;
1315 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1317 return predicted_size
;
1320 static void usbjtag_reset(int trst
, int srst
)
1322 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1325 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1326 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1328 low_output
&= ~nTRST
; /* switch output low */
1332 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1333 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1335 low_output
|= nTRST
; /* switch output high */
1340 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1341 low_output
&= ~nSRST
; /* switch output low */
1343 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1347 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1348 low_output
|= nSRST
; /* switch output high */
1350 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1353 /* command "set data bits low byte" */
1355 buffer_write(low_output
);
1356 buffer_write(low_direction
);
1359 static void jtagkey_reset(int trst
, int srst
)
1361 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1364 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1365 high_output
&= ~nTRSTnOE
;
1367 high_output
&= ~nTRST
;
1371 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1372 high_output
|= nTRSTnOE
;
1374 high_output
|= nTRST
;
1379 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1380 high_output
&= ~nSRST
;
1382 high_output
&= ~nSRSTnOE
;
1386 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1387 high_output
|= nSRST
;
1389 high_output
|= nSRSTnOE
;
1392 /* command "set data bits high byte" */
1394 buffer_write(high_output
);
1395 buffer_write(high_direction
);
1396 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1400 static void olimex_jtag_reset(int trst
, int srst
)
1402 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1405 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1406 high_output
&= ~nTRSTnOE
;
1408 high_output
&= ~nTRST
;
1412 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1413 high_output
|= nTRSTnOE
;
1415 high_output
|= nTRST
;
1420 high_output
|= nSRST
;
1424 high_output
&= ~nSRST
;
1427 /* command "set data bits high byte" */
1429 buffer_write(high_output
);
1430 buffer_write(high_direction
);
1431 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1435 static void axm0432_jtag_reset(int trst
, int srst
)
1439 tap_set_state(TAP_RESET
);
1440 high_output
&= ~nTRST
;
1444 high_output
|= nTRST
;
1449 high_output
&= ~nSRST
;
1453 high_output
|= nSRST
;
1456 /* command "set data bits low byte" */
1458 buffer_write(high_output
);
1459 buffer_write(high_direction
);
1460 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1464 static void flyswatter_reset(int trst
, int srst
)
1468 low_output
&= ~nTRST
;
1472 low_output
|= nTRST
;
1477 low_output
|= nSRST
;
1481 low_output
&= ~nSRST
;
1484 /* command "set data bits low byte" */
1486 buffer_write(low_output
);
1487 buffer_write(low_direction
);
1488 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1491 static void turtle_reset(int trst
, int srst
)
1497 low_output
|= nSRST
;
1501 low_output
&= ~nSRST
;
1504 /* command "set data bits low byte" */
1506 buffer_write(low_output
);
1507 buffer_write(low_direction
);
1508 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1511 static void comstick_reset(int trst
, int srst
)
1515 high_output
&= ~nTRST
;
1519 high_output
|= nTRST
;
1524 high_output
&= ~nSRST
;
1528 high_output
|= nSRST
;
1531 /* command "set data bits high byte" */
1533 buffer_write(high_output
);
1534 buffer_write(high_direction
);
1535 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1539 static void stm32stick_reset(int trst
, int srst
)
1543 high_output
&= ~nTRST
;
1547 high_output
|= nTRST
;
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
);
1564 /* command "set data bits high byte" */
1566 buffer_write(high_output
);
1567 buffer_write(high_direction
);
1568 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1572 static void sheevaplug_reset(int trst
, int srst
)
1575 high_output
&= ~nTRST
;
1577 high_output
|= nTRST
;
1580 high_output
&= ~nSRSTnOE
;
1582 high_output
|= nSRSTnOE
;
1584 /* command "set data bits high byte" */
1586 buffer_write(high_output
);
1587 buffer_write(high_direction
);
1588 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1591 static void redbee_reset(int trst
, int srst
)
1595 tap_set_state(TAP_RESET
);
1596 high_output
&= ~nTRST
;
1600 high_output
|= nTRST
;
1605 high_output
&= ~nSRST
;
1609 high_output
|= nSRST
;
1612 /* command "set data bits low byte" */
1614 buffer_write(high_output
);
1615 buffer_write(high_direction
);
1616 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1617 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1621 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1625 int predicted_size
= 0;
1628 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1629 cmd
->cmd
.runtest
->num_cycles
,
1630 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1632 /* only send the maximum buffer size that FT2232C can handle */
1634 if (tap_get_state() != TAP_IDLE
)
1635 predicted_size
+= 3;
1636 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1637 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1638 predicted_size
+= 3;
1639 if (tap_get_end_state() != TAP_IDLE
)
1640 predicted_size
+= 3;
1641 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1643 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1644 retval
= ERROR_JTAG_QUEUE_FAILED
;
1648 if (tap_get_state() != TAP_IDLE
)
1650 move_to_state(TAP_IDLE
);
1653 i
= cmd
->cmd
.runtest
->num_cycles
;
1656 /* there are no state transitions in this code, so omit state tracking */
1658 /* command "Clock Data to TMS/CS Pin (no Read)" */
1662 buffer_write((i
> 7) ? 6 : (i
- 1));
1667 i
-= (i
> 7) ? 7 : i
;
1668 /* LOG_DEBUG("added TMS scan (no read)"); */
1671 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1673 if (tap_get_state() != tap_get_end_state())
1675 move_to_state(tap_get_end_state());
1679 DEBUG_JTAG_IO("runtest: %i, end in %s",
1680 cmd
->cmd
.runtest
->num_cycles
,
1681 tap_state_name(tap_get_end_state()));
1685 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1687 int predicted_size
= 0;
1688 int retval
= ERROR_OK
;
1690 DEBUG_JTAG_IO("statemove end in %s",
1691 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1693 /* only send the maximum buffer size that FT2232C can handle */
1695 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1697 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1698 retval
= ERROR_JTAG_QUEUE_FAILED
;
1702 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1704 /* For TAP_RESET, ignore the current recorded state. It's often
1705 * wrong at server startup, and this transation is critical whenever
1708 if (tap_get_end_state() == TAP_RESET
) {
1709 clock_tms(0x4b, 0xff, 5, 0);
1712 /* shortest-path move to desired end state */
1713 } else if (tap_get_state() != tap_get_end_state())
1715 move_to_state(tap_get_end_state());
1723 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1724 * (or SWD) state machine.
1726 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1728 int retval
= ERROR_OK
;
1729 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1730 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1733 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1735 /* only send the maximum buffer size that FT2232C can handle */
1736 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1737 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1738 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1739 retval
= ERROR_JTAG_QUEUE_FAILED
;
1745 /* Shift out in batches of at most 6 bits; there's a report of an
1746 * FT2232 bug in this area, where shifting exactly 7 bits can make
1747 * problems with TMS signaling for the last clock cycle:
1749 * http://developer.intra2net.com/mailarchive/html/
1750 * libftdi/2009/msg00292.html
1752 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1754 * Note that pathmoves in JTAG are not often seven bits, so that
1755 * isn't a particularly likely situation outside of "special"
1756 * signaling such as switching between JTAG and SWD modes.
1759 if (num_bits
<= 6) {
1761 buffer_write(num_bits
- 1);
1762 buffer_write(*bits
& 0x3f);
1766 /* Yes, this is lazy ... we COULD shift out more data
1767 * bits per operation, but doing it in nybbles is easy
1771 buffer_write(*bits
& 0xf);
1774 count
= (num_bits
> 4) ? 4 : num_bits
;
1777 buffer_write(count
- 1);
1778 buffer_write((*bits
>> 4) & 0xf);
1788 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1790 int predicted_size
= 0;
1791 int retval
= ERROR_OK
;
1793 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1794 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1796 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1797 tap_state_name(tap_get_state()),
1798 tap_state_name(path
[num_states
-1]));
1800 /* only send the maximum buffer size that FT2232C can handle */
1801 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1802 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1804 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1805 retval
= ERROR_JTAG_QUEUE_FAILED
;
1811 ft2232_add_pathmove(path
, num_states
);
1817 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1820 int scan_size
; /* size of IR or DR scan */
1821 int predicted_size
= 0;
1822 int retval
= ERROR_OK
;
1824 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1826 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1828 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1830 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1831 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1833 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1834 /* unsent commands before this */
1835 if (first_unsent
!= cmd
)
1836 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1837 retval
= ERROR_JTAG_QUEUE_FAILED
;
1839 /* current command */
1840 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1841 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1843 first_unsent
= cmd
->next
;
1848 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1850 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1853 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1854 retval
= ERROR_JTAG_QUEUE_FAILED
;
1858 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1859 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1860 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1861 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1865 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1866 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1867 tap_state_name(tap_get_end_state()));
1872 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1875 int predicted_size
= 0;
1878 DEBUG_JTAG_IO("reset trst: %i srst %i",
1879 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1881 /* only send the maximum buffer size that FT2232C can handle */
1883 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1885 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1886 retval
= ERROR_JTAG_QUEUE_FAILED
;
1891 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1893 tap_set_state(TAP_RESET
);
1896 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1899 DEBUG_JTAG_IO("trst: %i, srst: %i",
1900 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1904 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1909 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1911 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1912 retval
= ERROR_JTAG_QUEUE_FAILED
;
1913 first_unsent
= cmd
->next
;
1914 jtag_sleep(cmd
->cmd
.sleep
->us
);
1915 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1917 tap_state_name(tap_get_state()));
1921 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1926 /* this is only allowed while in a stable state. A check for a stable
1927 * state was done in jtag_add_clocks()
1929 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1930 retval
= ERROR_JTAG_QUEUE_FAILED
;
1931 DEBUG_JTAG_IO("clocks %i while in %s",
1932 cmd
->cmd
.stableclocks
->num_cycles
,
1933 tap_state_name(tap_get_state()));
1937 static int ft2232_execute_command(struct jtag_command
*cmd
)
1943 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1944 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1945 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1946 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1947 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1948 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1949 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1951 retval
= ft2232_execute_tms(cmd
);
1954 LOG_ERROR("BUG: unknown JTAG command type encountered");
1955 retval
= ERROR_JTAG_QUEUE_FAILED
;
1961 static int ft2232_execute_queue(void)
1963 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
1966 first_unsent
= cmd
; /* next command that has to be sent */
1969 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1970 * that wasn't handled by a caller-provided error handler
1974 ft2232_buffer_size
= 0;
1975 ft2232_expect_read
= 0;
1977 /* blink, if the current layout has that feature */
1983 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1984 retval
= ERROR_JTAG_QUEUE_FAILED
;
1985 /* Start reading input before FT2232 TX buffer fills up */
1987 if (ft2232_expect_read
> 256)
1989 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1990 retval
= ERROR_JTAG_QUEUE_FAILED
;
1995 if (require_send
> 0)
1996 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1997 retval
= ERROR_JTAG_QUEUE_FAILED
;
2002 #if BUILD_FT2232_FTD2XX == 1
2003 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2007 char SerialNumber
[16];
2008 char Description
[64];
2009 DWORD openex_flags
= 0;
2010 char* openex_string
= NULL
;
2011 uint8_t latency_timer
;
2013 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
2016 /* Add non-standard Vid/Pid to the linux driver */
2017 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2019 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2023 if (ft2232_device_desc
&& ft2232_serial
)
2025 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2026 ft2232_device_desc
= NULL
;
2029 if (ft2232_device_desc
)
2031 openex_string
= ft2232_device_desc
;
2032 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2034 else if (ft2232_serial
)
2036 openex_string
= ft2232_serial
;
2037 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2041 LOG_ERROR("neither device description nor serial number specified");
2042 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2044 return ERROR_JTAG_INIT_FAILED
;
2047 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2048 if (status
!= FT_OK
) {
2049 /* under Win32, the FTD2XX driver appends an "A" to the end
2050 * of the description, if we tried by the desc, then
2051 * try by the alternate "A" description. */
2052 if (openex_string
== ft2232_device_desc
) {
2053 /* Try the alternate method. */
2054 openex_string
= ft2232_device_desc_A
;
2055 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2056 if (status
== FT_OK
) {
2057 /* yea, the "alternate" method worked! */
2059 /* drat, give the user a meaningfull message.
2060 * telling the use we tried *BOTH* methods. */
2061 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2063 ft2232_device_desc_A
);
2068 if (status
!= FT_OK
)
2074 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2076 return ERROR_JTAG_INIT_FAILED
;
2078 LOG_ERROR("unable to open ftdi device: %lu", status
);
2079 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2080 if (status
== FT_OK
)
2082 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2085 for (i
= 0; i
< num_devices
; i
++)
2086 desc_array
[i
] = malloc(64);
2088 desc_array
[num_devices
] = NULL
;
2090 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2092 if (status
== FT_OK
)
2094 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2095 for (i
= 0; i
< num_devices
; i
++)
2096 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2099 for (i
= 0; i
< num_devices
; i
++)
2100 free(desc_array
[i
]);
2106 LOG_ERROR("ListDevices: NONE\n");
2108 return ERROR_JTAG_INIT_FAILED
;
2111 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2113 LOG_ERROR("unable to set latency timer: %lu", status
);
2114 return ERROR_JTAG_INIT_FAILED
;
2117 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2119 LOG_ERROR("unable to get latency timer: %lu", status
);
2120 return ERROR_JTAG_INIT_FAILED
;
2124 LOG_DEBUG("current latency timer: %i", latency_timer
);
2127 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2129 LOG_ERROR("unable to set timeouts: %lu", status
);
2130 return ERROR_JTAG_INIT_FAILED
;
2133 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2135 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2136 return ERROR_JTAG_INIT_FAILED
;
2139 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2141 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2142 return ERROR_JTAG_INIT_FAILED
;
2146 static const char* type_str
[] =
2147 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2148 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2149 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2150 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2151 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2152 LOG_INFO("deviceID: %lu", deviceID
);
2153 LOG_INFO("SerialNumber: %s", SerialNumber
);
2154 LOG_INFO("Description: %s", Description
);
2160 static int ft2232_purge_ftd2xx(void)
2164 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2166 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2167 return ERROR_JTAG_INIT_FAILED
;
2173 #endif /* BUILD_FT2232_FTD2XX == 1 */
2175 #if BUILD_FT2232_LIBFTDI == 1
2176 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2178 uint8_t latency_timer
;
2180 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2181 ft2232_layout
, vid
, pid
);
2183 if (ftdi_init(&ftdic
) < 0)
2184 return ERROR_JTAG_INIT_FAILED
;
2186 /* default to INTERFACE_A */
2187 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2189 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2191 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2192 return ERROR_JTAG_INIT_FAILED
;
2195 /* context, vendor id, product id */
2196 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2200 LOG_WARNING("unable to open ftdi device (trying more): %s",
2203 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2205 return ERROR_JTAG_INIT_FAILED
;
2208 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2209 if (ftdi_usb_reset(&ftdic
) < 0)
2211 LOG_ERROR("unable to reset ftdi device");
2212 return ERROR_JTAG_INIT_FAILED
;
2215 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2217 LOG_ERROR("unable to set latency timer");
2218 return ERROR_JTAG_INIT_FAILED
;
2221 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2223 LOG_ERROR("unable to get latency timer");
2224 return ERROR_JTAG_INIT_FAILED
;
2228 LOG_DEBUG("current latency timer: %i", latency_timer
);
2231 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2233 ftdi_device
= ftdic
.type
;
2234 static const char* type_str
[] =
2235 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2236 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2237 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2238 ? ftdi_device
: no_of_known_types
;
2239 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2243 static int ft2232_purge_libftdi(void)
2245 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2247 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2248 return ERROR_JTAG_INIT_FAILED
;
2254 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2256 static int ft2232_init(void)
2260 uint32_t bytes_written
;
2261 const struct ft2232_layout
* cur_layout
= ft2232_layouts
;
2264 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2266 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2270 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2273 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2275 ft2232_layout
= "usbjtag";
2276 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2279 while (cur_layout
->name
)
2281 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2283 layout
= cur_layout
;
2291 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2292 return ERROR_JTAG_INIT_FAILED
;
2298 * "more indicates that there are more IDs to try, so we should
2299 * not print an error for an ID mismatch (but for anything
2302 * try_more indicates that the error code returned indicates an
2303 * ID mismatch (and nothing else) and that we should proceeed
2304 * with the next ID pair.
2306 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2309 #if BUILD_FT2232_FTD2XX == 1
2310 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2312 #elif BUILD_FT2232_LIBFTDI == 1
2313 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2314 more
, &try_more
, cur_layout
->channel
);
2318 if (!more
|| !try_more
)
2322 ft2232_buffer_size
= 0;
2323 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2325 if (layout
->init() != ERROR_OK
)
2326 return ERROR_JTAG_INIT_FAILED
;
2328 if (ft2232_device_is_highspeed())
2330 #ifndef BUILD_FT2232_HIGHSPEED
2331 #if BUILD_FT2232_FTD2XX == 1
2332 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2333 #elif BUILD_FT2232_LIBFTDI == 1
2334 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2337 /* make sure the legacy mode is disabled */
2338 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2339 return ERROR_JTAG_INIT_FAILED
;
2342 ft2232_speed(jtag_get_speed());
2344 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2345 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2347 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2348 return ERROR_JTAG_INIT_FAILED
;
2351 #if BUILD_FT2232_FTD2XX == 1
2352 return ft2232_purge_ftd2xx();
2353 #elif BUILD_FT2232_LIBFTDI == 1
2354 return ft2232_purge_libftdi();
2360 static int usbjtag_init(void)
2363 uint32_t bytes_written
;
2366 low_direction
= 0x0b;
2368 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2375 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2382 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2384 /* There are multiple revisions of LM3S811 eval boards:
2385 * - Rev B (and older?) boards have no SWO trace support.
2386 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2387 * they should use the "luminary_icdi" layout instead.
2394 low_direction
= 0x8b;
2396 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2398 /* Most Luminary eval boards support SWO trace output,
2399 * and should use this "luminary_icdi" layout.
2406 low_direction
= 0xcb;
2410 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2411 return ERROR_JTAG_INIT_FAILED
;
2414 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2415 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2417 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2418 low_output
&= ~nTRST
; /* nTRST = 0 */
2422 low_direction
|= nTRSTnOE
; /* nTRST output */
2423 low_output
|= nTRST
; /* nTRST = 1 */
2426 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2428 low_direction
|= nSRSTnOE
; /* nSRST output */
2429 low_output
|= nSRST
; /* nSRST = 1 */
2433 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2434 low_output
&= ~nSRST
; /* nSRST = 0 */
2437 /* initialize low byte for jtag */
2438 buf
[0] = 0x80; /* command "set data bits low byte" */
2439 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2440 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2441 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2443 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2445 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2446 return ERROR_JTAG_INIT_FAILED
;
2452 static int axm0432_jtag_init(void)
2455 uint32_t bytes_written
;
2458 low_direction
= 0x2b;
2460 /* initialize low byte for jtag */
2461 buf
[0] = 0x80; /* command "set data bits low byte" */
2462 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2463 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2464 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2466 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2468 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2469 return ERROR_JTAG_INIT_FAILED
;
2472 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2475 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2477 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2481 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2486 high_direction
= 0x0c;
2488 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2489 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2491 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2495 high_output
|= nTRST
;
2498 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2500 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2504 high_output
|= nSRST
;
2507 /* initialize high port */
2508 buf
[0] = 0x82; /* command "set data bits high byte" */
2509 buf
[1] = high_output
; /* value */
2510 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2511 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2513 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2515 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2516 return ERROR_JTAG_INIT_FAILED
;
2522 static int redbee_init(void)
2525 uint32_t bytes_written
;
2528 low_direction
= 0x2b;
2530 /* initialize low byte for jtag */
2531 /* command "set data bits low byte" */
2533 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2534 buf
[2] = low_direction
;
2535 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2536 buf
[1] = low_output
;
2537 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2539 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2540 || (bytes_written
!= 3))
2542 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2543 return ERROR_JTAG_INIT_FAILED
;
2547 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2549 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2552 high_direction
= 0x0c;
2554 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2555 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2557 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2561 high_output
|= nTRST
;
2564 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2566 LOG_ERROR("can't set nSRST to push-pull on redbee");
2570 high_output
|= nSRST
;
2573 /* initialize high port */
2574 buf
[0] = 0x82; /* command "set data bits high byte" */
2575 buf
[1] = high_output
; /* value */
2576 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2577 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2579 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2580 || (bytes_written
!= 3))
2582 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2583 return ERROR_JTAG_INIT_FAILED
;
2589 static int jtagkey_init(void)
2592 uint32_t bytes_written
;
2595 low_direction
= 0x1b;
2597 /* initialize low byte for jtag */
2598 buf
[0] = 0x80; /* command "set data bits low byte" */
2599 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2600 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2601 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2603 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2605 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2606 return ERROR_JTAG_INIT_FAILED
;
2609 if (strcmp(layout
->name
, "jtagkey") == 0)
2616 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2617 || (strcmp(layout
->name
, "oocdlink") == 0))
2626 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2631 high_direction
= 0x0f;
2633 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2634 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2636 high_output
|= nTRSTnOE
;
2637 high_output
&= ~nTRST
;
2641 high_output
&= ~nTRSTnOE
;
2642 high_output
|= nTRST
;
2645 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2647 high_output
&= ~nSRSTnOE
;
2648 high_output
|= nSRST
;
2652 high_output
|= nSRSTnOE
;
2653 high_output
&= ~nSRST
;
2656 /* initialize high port */
2657 buf
[0] = 0x82; /* command "set data bits high byte" */
2658 buf
[1] = high_output
; /* value */
2659 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2660 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2662 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2664 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2665 return ERROR_JTAG_INIT_FAILED
;
2671 static int olimex_jtag_init(void)
2674 uint32_t bytes_written
;
2677 low_direction
= 0x1b;
2679 /* initialize low byte for jtag */
2680 buf
[0] = 0x80; /* command "set data bits low byte" */
2681 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2682 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2683 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2685 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2687 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2688 return ERROR_JTAG_INIT_FAILED
;
2694 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2697 high_direction
= 0x0f;
2699 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2700 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2702 high_output
|= nTRSTnOE
;
2703 high_output
&= ~nTRST
;
2707 high_output
&= ~nTRSTnOE
;
2708 high_output
|= nTRST
;
2711 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2713 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2717 high_output
&= ~nSRST
;
2720 /* turn red LED on */
2721 high_output
|= 0x08;
2723 /* initialize high port */
2724 buf
[0] = 0x82; /* command "set data bits high byte" */
2725 buf
[1] = high_output
; /* value */
2726 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2727 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2729 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2731 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2732 return ERROR_JTAG_INIT_FAILED
;
2738 static int flyswatter_init(void)
2741 uint32_t bytes_written
;
2744 low_direction
= 0xfb;
2746 /* initialize low byte for jtag */
2747 buf
[0] = 0x80; /* command "set data bits low byte" */
2748 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2749 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2750 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2752 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2754 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2755 return ERROR_JTAG_INIT_FAILED
;
2759 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2761 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2764 high_direction
= 0x0c;
2766 /* turn red LED3 on, LED2 off */
2767 high_output
|= 0x08;
2769 /* initialize high port */
2770 buf
[0] = 0x82; /* command "set data bits high byte" */
2771 buf
[1] = high_output
; /* value */
2772 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2773 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2775 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2777 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2778 return ERROR_JTAG_INIT_FAILED
;
2784 static int turtle_init(void)
2787 uint32_t bytes_written
;
2790 low_direction
= 0x5b;
2792 /* initialize low byte for jtag */
2793 buf
[0] = 0x80; /* command "set data bits low byte" */
2794 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2795 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2796 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2798 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2800 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2801 return ERROR_JTAG_INIT_FAILED
;
2807 high_direction
= 0x0C;
2809 /* initialize high port */
2810 buf
[0] = 0x82; /* command "set data bits high byte" */
2811 buf
[1] = high_output
;
2812 buf
[2] = high_direction
;
2813 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2815 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2817 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2818 return ERROR_JTAG_INIT_FAILED
;
2824 static int comstick_init(void)
2827 uint32_t bytes_written
;
2830 low_direction
= 0x0b;
2832 /* initialize low byte for jtag */
2833 buf
[0] = 0x80; /* command "set data bits low byte" */
2834 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2835 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2836 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2838 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2840 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2841 return ERROR_JTAG_INIT_FAILED
;
2845 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2847 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2850 high_direction
= 0x03;
2852 /* initialize high port */
2853 buf
[0] = 0x82; /* command "set data bits high byte" */
2854 buf
[1] = high_output
;
2855 buf
[2] = high_direction
;
2856 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2858 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2860 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2861 return ERROR_JTAG_INIT_FAILED
;
2867 static int stm32stick_init(void)
2870 uint32_t bytes_written
;
2873 low_direction
= 0x8b;
2875 /* initialize low byte for jtag */
2876 buf
[0] = 0x80; /* command "set data bits low byte" */
2877 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2878 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2879 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2881 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2883 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2884 return ERROR_JTAG_INIT_FAILED
;
2888 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2890 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2893 high_direction
= 0x03;
2895 /* initialize high port */
2896 buf
[0] = 0x82; /* command "set data bits high byte" */
2897 buf
[1] = high_output
;
2898 buf
[2] = high_direction
;
2899 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2901 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2903 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2904 return ERROR_JTAG_INIT_FAILED
;
2910 static int sheevaplug_init(void)
2913 uint32_t bytes_written
;
2916 low_direction
= 0x1b;
2918 /* initialize low byte for jtag */
2919 buf
[0] = 0x80; /* command "set data bits low byte" */
2920 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2921 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2922 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2924 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2926 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2927 return ERROR_JTAG_INIT_FAILED
;
2936 high_direction
= 0x0f;
2938 /* nTRST is always push-pull */
2939 high_output
&= ~nTRSTnOE
;
2940 high_output
|= nTRST
;
2942 /* nSRST is always open-drain */
2943 high_output
|= nSRSTnOE
;
2944 high_output
&= ~nSRST
;
2946 /* initialize high port */
2947 buf
[0] = 0x82; /* command "set data bits high byte" */
2948 buf
[1] = high_output
; /* value */
2949 buf
[2] = high_direction
; /* all outputs - xRST */
2950 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2952 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2954 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2955 return ERROR_JTAG_INIT_FAILED
;
2961 static int cortino_jtag_init(void)
2964 uint32_t bytes_written
;
2967 low_direction
= 0x1b;
2969 /* initialize low byte for jtag */
2970 buf
[0] = 0x80; /* command "set data bits low byte" */
2971 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2972 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2973 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2975 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2977 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2978 return ERROR_JTAG_INIT_FAILED
;
2982 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2984 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2987 high_direction
= 0x03;
2989 /* initialize high port */
2990 buf
[0] = 0x82; /* command "set data bits high byte" */
2991 buf
[1] = high_output
;
2992 buf
[2] = high_direction
;
2993 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2995 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2997 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2998 return ERROR_JTAG_INIT_FAILED
;
3004 static void olimex_jtag_blink(void)
3006 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3007 * ACBUS3 is bit 3 of the GPIOH port
3009 if (high_output
& 0x08)
3011 /* set port pin high */
3012 high_output
&= 0x07;
3016 /* set port pin low */
3017 high_output
|= 0x08;
3021 buffer_write(high_output
);
3022 buffer_write(high_direction
);
3025 static void flyswatter_jtag_blink(void)
3028 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3030 high_output
^= 0x0c;
3033 buffer_write(high_output
);
3034 buffer_write(high_direction
);
3037 static void turtle_jtag_blink(void)
3040 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3042 if (high_output
& 0x08)
3052 buffer_write(high_output
);
3053 buffer_write(high_direction
);
3056 static int ft2232_quit(void)
3058 #if BUILD_FT2232_FTD2XX == 1
3061 status
= FT_Close(ftdih
);
3062 #elif BUILD_FT2232_LIBFTDI == 1
3063 ftdi_usb_close(&ftdic
);
3065 ftdi_deinit(&ftdic
);
3068 free(ft2232_buffer
);
3069 ft2232_buffer
= NULL
;
3074 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3080 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3081 cp
= strchr(ft2232_device_desc
, 0);
3082 /* under Win32, the FTD2XX driver appends an "A" to the end
3083 * of the description, this examines the given desc
3084 * and creates the 'missing' _A or non_A variable. */
3085 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3086 /* it was, so make this the "A" version. */
3087 ft2232_device_desc_A
= ft2232_device_desc
;
3088 /* and *CREATE* the non-A version. */
3089 strcpy(buf
, ft2232_device_desc
);
3090 cp
= strchr(buf
, 0);
3092 ft2232_device_desc
= strdup(buf
);
3094 /* <space > A not defined
3096 sprintf(buf
, "%s A", ft2232_device_desc
);
3097 ft2232_device_desc_A
= strdup(buf
);
3102 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3108 COMMAND_HANDLER(ft2232_handle_serial_command
)
3112 ft2232_serial
= strdup(CMD_ARGV
[0]);
3116 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3122 COMMAND_HANDLER(ft2232_handle_layout_command
)
3127 ft2232_layout
= malloc(strlen(CMD_ARGV
[0]) + 1);
3128 strcpy(ft2232_layout
, CMD_ARGV
[0]);
3133 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3135 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3137 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3138 "(maximum is %d pairs)", MAX_USB_IDS
);
3139 CMD_ARGC
= MAX_USB_IDS
* 2;
3141 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3143 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3145 return ERROR_COMMAND_SYNTAX_ERROR
;
3146 /* remove the incomplete trailing id */
3151 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3153 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3154 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3158 * Explicitly terminate, in case there are multiples instances of
3161 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3166 COMMAND_HANDLER(ft2232_handle_latency_command
)
3170 ft2232_latency
= atoi(CMD_ARGV
[0]);
3174 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3180 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3184 /* 7 bits of either ones or zeros. */
3185 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3187 while (num_cycles
> 0)
3189 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3190 * at most 7 bits per invocation. Here we invoke it potentially
3193 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3195 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3197 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3198 retval
= ERROR_JTAG_QUEUE_FAILED
;
3203 /* there are no state transitions in this code, so omit state tracking */
3205 /* command "Clock Data to TMS/CS Pin (no Read)" */
3209 buffer_write(bitcount_per_command
- 1);
3211 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3216 num_cycles
-= bitcount_per_command
;
3222 /* ---------------------------------------------------------------------
3223 * Support for IceBear JTAG adapter from Section5:
3224 * http://section5.ch/icebear
3226 * Author: Sten, debian@sansys-electronic.com
3229 /* Icebear pin layout
3231 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3232 * GND GND | 4 3| n.c.
3233 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3234 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3235 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3236 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3237 * ADBUS2 TDO |14 13| GND GND
3239 * ADBUS0 O L TCK ACBUS0 GND
3240 * ADBUS1 O L TDI ACBUS1 GND
3241 * ADBUS2 I TDO ACBUS2 n.c.
3242 * ADBUS3 O H TMS ACBUS3 n.c.
3248 static int icebear_jtag_init(void) {
3250 uint32_t bytes_written
;
3252 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3253 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3257 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3258 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3259 low_direction
&= ~nTRST
; /* nTRST high impedance */
3262 low_direction
|= nTRST
;
3263 low_output
|= nTRST
;
3266 low_direction
|= nSRST
;
3267 low_output
|= nSRST
;
3269 /* initialize low byte for jtag */
3270 buf
[0] = 0x80; /* command "set data bits low byte" */
3271 buf
[1] = low_output
;
3272 buf
[2] = low_direction
;
3273 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3275 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3276 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3277 return ERROR_JTAG_INIT_FAILED
;
3281 high_direction
= 0x00;
3284 /* initialize high port */
3285 buf
[0] = 0x82; /* command "set data bits high byte" */
3286 buf
[1] = high_output
; /* value */
3287 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3288 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3290 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3291 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3292 return ERROR_JTAG_INIT_FAILED
;
3298 static void icebear_jtag_reset(int trst
, int srst
) {
3301 low_direction
|= nTRST
;
3302 low_output
&= ~nTRST
;
3304 else if (trst
== 0) {
3305 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3306 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3307 low_direction
&= ~nTRST
;
3309 low_output
|= nTRST
;
3313 low_output
&= ~nSRST
;
3315 else if (srst
== 0) {
3316 low_output
|= nSRST
;
3319 /* command "set data bits low byte" */
3321 buffer_write(low_output
);
3322 buffer_write(low_direction
);
3324 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3327 /* ---------------------------------------------------------------------
3328 * Support for Signalyzer H2 and Signalyzer H4
3329 * JTAG adapter from Xverve Technologies Inc.
3330 * http://www.signalyzer.com or http://www.xverve.com
3332 * Author: Oleg Seiljus, oleg@signalyzer.com
3334 static unsigned char signalyzer_h_side
;
3335 static unsigned int signalyzer_h_adapter_type
;
3337 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3339 #if BUILD_FT2232_FTD2XX == 1
3340 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3343 #define SIGNALYZER_COMMAND_ADDR 128
3344 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3346 #define SIGNALYZER_COMMAND_VERSION 0x41
3347 #define SIGNALYZER_COMMAND_RESET 0x42
3348 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3349 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3350 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3351 #define SIGNALYZER_COMMAND_LED_SET 0x53
3352 #define SIGNALYZER_COMMAND_ADC 0x54
3353 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3354 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3355 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3356 #define SIGNALYZER_COMMAND_I2C 0x58
3358 #define SIGNALYZER_CHAN_A 1
3359 #define SIGNALYZER_CHAN_B 2
3360 /* LEDS use channel C */
3361 #define SIGNALYZER_CHAN_C 4
3363 #define SIGNALYZER_LED_GREEN 1
3364 #define SIGNALYZER_LED_RED 2
3366 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3367 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3368 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3369 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3370 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3373 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3375 #if BUILD_FT2232_FTD2XX == 1
3376 return FT_WriteEE(ftdih
, address
, value
);
3377 #elif BUILD_FT2232_LIBFTDI == 1
3382 #if BUILD_FT2232_FTD2XX == 1
3383 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3385 return FT_ReadEE(ftdih
, address
, value
);
3389 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3390 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3392 unsigned char on_time
;
3393 unsigned char off_time
;
3395 if (on_time_ms
< 0xFFFF)
3396 on_time
= (unsigned char)(on_time_ms
/ 62);
3400 off_time
= (unsigned char)(off_time_ms
/ 62);
3402 #if BUILD_FT2232_FTD2XX == 1
3405 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3406 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3408 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3409 return ERROR_JTAG_DEVICE_ERROR
;
3412 if ((status
= signalyzer_h_ctrl_write(
3413 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3414 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3416 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3417 return ERROR_JTAG_DEVICE_ERROR
;
3420 if ((status
= signalyzer_h_ctrl_write(
3421 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3422 ((uint32_t)cycles
))) != FT_OK
)
3424 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3425 return ERROR_JTAG_DEVICE_ERROR
;
3428 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3429 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3431 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3432 return ERROR_JTAG_DEVICE_ERROR
;
3436 #elif BUILD_FT2232_LIBFTDI == 1
3439 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3440 ((uint32_t)(channel
<< 8) | led
))) < 0)
3442 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3443 ftdi_get_error_string(&ftdic
));
3444 return ERROR_JTAG_DEVICE_ERROR
;
3447 if ((retval
= signalyzer_h_ctrl_write(
3448 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3449 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3451 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3452 ftdi_get_error_string(&ftdic
));
3453 return ERROR_JTAG_DEVICE_ERROR
;
3456 if ((retval
= signalyzer_h_ctrl_write(
3457 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3458 (uint32_t)cycles
)) < 0)
3460 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3461 ftdi_get_error_string(&ftdic
));
3462 return ERROR_JTAG_DEVICE_ERROR
;
3465 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3466 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3468 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3469 ftdi_get_error_string(&ftdic
));
3470 return ERROR_JTAG_DEVICE_ERROR
;
3477 static int signalyzer_h_init(void)
3479 #if BUILD_FT2232_FTD2XX == 1
3486 uint16_t read_buf
[12] = { 0 };
3488 uint32_t bytes_written
;
3490 /* turn on center green led */
3491 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3492 0xFFFF, 0x00, 0x00);
3494 /* determine what channel config wants to open
3495 * TODO: change me... current implementation is made to work
3496 * with openocd description parsing.
3498 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3502 signalyzer_h_side
= *(end_of_desc
- 1);
3503 if (signalyzer_h_side
== 'B')
3504 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3506 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3510 LOG_ERROR("No Channel was specified");
3514 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3517 #if BUILD_FT2232_FTD2XX == 1
3518 /* read signalyzer versionining information */
3519 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3520 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3522 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3523 return ERROR_JTAG_DEVICE_ERROR
;
3526 for (i
= 0; i
< 10; i
++)
3528 if ((status
= signalyzer_h_ctrl_read(
3529 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3530 &read_buf
[i
])) != FT_OK
)
3532 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3534 return ERROR_JTAG_DEVICE_ERROR
;
3538 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3539 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3540 read_buf
[4], read_buf
[5], read_buf
[6]);
3542 /* set gpio register */
3543 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3544 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3546 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3547 return ERROR_JTAG_DEVICE_ERROR
;
3550 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3553 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3554 return ERROR_JTAG_DEVICE_ERROR
;
3557 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3558 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3560 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3561 return ERROR_JTAG_DEVICE_ERROR
;
3564 /* read adapter type information */
3565 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3566 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3568 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3569 return ERROR_JTAG_DEVICE_ERROR
;
3572 if ((status
= signalyzer_h_ctrl_write(
3573 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3575 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3576 return ERROR_JTAG_DEVICE_ERROR
;
3579 if ((status
= signalyzer_h_ctrl_write(
3580 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3582 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3583 return ERROR_JTAG_DEVICE_ERROR
;
3586 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3587 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3589 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3590 return ERROR_JTAG_DEVICE_ERROR
;
3595 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3596 &read_buf
[0])) != FT_OK
)
3598 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3599 return ERROR_JTAG_DEVICE_ERROR
;
3602 if (read_buf
[0] != 0x0498)
3603 signalyzer_h_adapter_type
= 0x0000;
3606 for (i
= 0; i
< 4; i
++)
3608 if ((status
= signalyzer_h_ctrl_read(
3609 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3610 &read_buf
[i
])) != FT_OK
)
3612 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3614 return ERROR_JTAG_DEVICE_ERROR
;
3618 signalyzer_h_adapter_type
= read_buf
[0];
3621 #elif BUILD_FT2232_LIBFTDI == 1
3622 /* currently libftdi does not allow reading individual eeprom
3623 * locations, therefore adapter type cannot be detected.
3624 * override with most common type
3626 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3629 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3631 /* ADAPTOR: EM_LT16_A */
3632 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3634 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3635 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3643 low_direction
= 0x1b;
3646 high_direction
= 0x0;
3648 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3650 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3651 low_output
&= ~nTRST
; /* nTRST = 0 */
3655 low_direction
|= nTRSTnOE
; /* nTRST output */
3656 low_output
|= nTRST
; /* nTRST = 1 */
3659 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3661 low_direction
|= nSRSTnOE
; /* nSRST output */
3662 low_output
|= nSRST
; /* nSRST = 1 */
3666 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3667 low_output
&= ~nSRST
; /* nSRST = 0 */
3670 #if BUILD_FT2232_FTD2XX == 1
3671 /* enable power to the module */
3672 if ((status
= signalyzer_h_ctrl_write(
3673 SIGNALYZER_DATA_BUFFER_ADDR
,
3674 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3677 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3679 return ERROR_JTAG_DEVICE_ERROR
;
3682 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3683 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3685 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3687 return ERROR_JTAG_DEVICE_ERROR
;
3690 /* set gpio mode register */
3691 if ((status
= signalyzer_h_ctrl_write(
3692 SIGNALYZER_DATA_BUFFER_ADDR
,
3693 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3695 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3697 return ERROR_JTAG_DEVICE_ERROR
;
3700 if ((status
= signalyzer_h_ctrl_write(
3701 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3704 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3706 return ERROR_JTAG_DEVICE_ERROR
;
3709 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3710 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3712 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3714 return ERROR_JTAG_DEVICE_ERROR
;
3717 /* set gpio register */
3718 if ((status
= signalyzer_h_ctrl_write(
3719 SIGNALYZER_DATA_BUFFER_ADDR
,
3720 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3722 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3724 return ERROR_JTAG_DEVICE_ERROR
;
3727 if ((status
= signalyzer_h_ctrl_write(
3728 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3731 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3733 return ERROR_JTAG_DEVICE_ERROR
;
3736 if ((status
= signalyzer_h_ctrl_write(
3737 SIGNALYZER_COMMAND_ADDR
,
3738 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3740 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3742 return ERROR_JTAG_DEVICE_ERROR
;
3747 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3748 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3749 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3750 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3751 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3753 if (signalyzer_h_adapter_type
3754 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3755 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3756 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3757 else if (signalyzer_h_adapter_type
3758 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3759 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3760 "(ARM JTAG with PSU) detected. (HW: %2x).",
3761 (read_buf
[1] >> 8));
3762 else if (signalyzer_h_adapter_type
3763 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3764 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3765 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3766 else if (signalyzer_h_adapter_type
3767 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3768 LOG_INFO("Signalyzer: EM-JTAG-P "
3769 "(Generic JTAG with PSU) detected. (HW: %2x).",
3770 (read_buf
[1] >> 8));
3778 low_direction
= 0x1b;
3781 high_direction
= 0x1f;
3783 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3785 high_output
|= nTRSTnOE
;
3786 high_output
&= ~nTRST
;
3790 high_output
&= ~nTRSTnOE
;
3791 high_output
|= nTRST
;
3794 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3796 high_output
&= ~nSRSTnOE
;
3797 high_output
|= nSRST
;
3801 high_output
|= nSRSTnOE
;
3802 high_output
&= ~nSRST
;
3805 #if BUILD_FT2232_FTD2XX == 1
3806 /* enable power to the module */
3807 if ((status
= signalyzer_h_ctrl_write(
3808 SIGNALYZER_DATA_BUFFER_ADDR
,
3809 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3812 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3814 return ERROR_JTAG_DEVICE_ERROR
;
3817 if ((status
= signalyzer_h_ctrl_write(
3818 SIGNALYZER_COMMAND_ADDR
,
3819 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3821 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3823 return ERROR_JTAG_DEVICE_ERROR
;
3826 /* set gpio mode register (IO_16 and IO_17 set as analog
3827 * inputs, other is gpio)
3829 if ((status
= signalyzer_h_ctrl_write(
3830 SIGNALYZER_DATA_BUFFER_ADDR
,
3831 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3833 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3835 return ERROR_JTAG_DEVICE_ERROR
;
3838 if ((status
= signalyzer_h_ctrl_write(
3839 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3842 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3844 return ERROR_JTAG_DEVICE_ERROR
;
3847 if ((status
= signalyzer_h_ctrl_write(
3848 SIGNALYZER_COMMAND_ADDR
,
3849 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3853 return ERROR_JTAG_DEVICE_ERROR
;
3856 /* set gpio register (all inputs, for -P modules,
3857 * PSU will be turned off)
3859 if ((status
= signalyzer_h_ctrl_write(
3860 SIGNALYZER_DATA_BUFFER_ADDR
,
3861 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3863 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3865 return ERROR_JTAG_DEVICE_ERROR
;
3868 if ((status
= signalyzer_h_ctrl_write(
3869 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3872 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3874 return ERROR_JTAG_DEVICE_ERROR
;
3877 if ((status
= signalyzer_h_ctrl_write(
3878 SIGNALYZER_COMMAND_ADDR
,
3879 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3881 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3883 return ERROR_JTAG_DEVICE_ERROR
;
3888 else if (signalyzer_h_adapter_type
== 0x0000)
3890 LOG_INFO("Signalyzer: No external modules were detected.");
3898 low_direction
= 0x1b;
3901 high_direction
= 0x0;
3903 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3905 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3906 low_output
&= ~nTRST
; /* nTRST = 0 */
3910 low_direction
|= nTRSTnOE
; /* nTRST output */
3911 low_output
|= nTRST
; /* nTRST = 1 */
3914 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3916 low_direction
|= nSRSTnOE
; /* nSRST output */
3917 low_output
|= nSRST
; /* nSRST = 1 */
3921 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3922 low_output
&= ~nSRST
; /* nSRST = 0 */
3927 LOG_ERROR("Unknown module type is detected: %.4x",
3928 signalyzer_h_adapter_type
);
3929 return ERROR_JTAG_DEVICE_ERROR
;
3932 /* initialize low byte of controller for jtag operation */
3934 buf
[1] = low_output
;
3935 buf
[2] = low_direction
;
3937 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3938 || (bytes_written
!= 3))
3940 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3941 return ERROR_JTAG_INIT_FAILED
;
3944 #if BUILD_FT2232_FTD2XX == 1
3945 if (ftdi_device
== FT_DEVICE_2232H
)
3947 /* initialize high byte of controller for jtag operation */
3949 buf
[1] = high_output
;
3950 buf
[2] = high_direction
;
3952 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3953 || (bytes_written
!= 3))
3955 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3956 return ERROR_JTAG_INIT_FAILED
;
3959 #elif BUILD_FT2232_LIBFTDI == 1
3960 if (ftdi_device
== TYPE_2232H
)
3962 /* initialize high byte of controller for jtag operation */
3964 buf
[1] = high_output
;
3965 buf
[2] = high_direction
;
3967 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3968 || (bytes_written
!= 3))
3970 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3971 return ERROR_JTAG_INIT_FAILED
;
3978 static void signalyzer_h_reset(int trst
, int srst
)
3980 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3982 /* ADAPTOR: EM_LT16_A */
3983 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3987 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3988 /* switch to output pin (output is low) */
3989 low_direction
|= nTRSTnOE
;
3991 /* switch output low */
3992 low_output
&= ~nTRST
;
3996 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3997 /* switch to input pin (high-Z + internal
3998 * and external pullup) */
3999 low_direction
&= ~nTRSTnOE
;
4001 /* switch output high */
4002 low_output
|= nTRST
;
4007 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4008 /* switch output low */
4009 low_output
&= ~nSRST
;
4011 /* switch to output pin (output is low) */
4012 low_direction
|= nSRSTnOE
;
4016 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4017 /* switch output high */
4018 low_output
|= nSRST
;
4020 /* switch to input pin (high-Z) */
4021 low_direction
&= ~nSRSTnOE
;
4024 /* command "set data bits low byte" */
4026 buffer_write(low_output
);
4027 buffer_write(low_direction
);
4028 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4029 "low_direction: 0x%2.2x",
4030 trst
, srst
, low_output
, low_direction
);
4032 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4033 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4034 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4035 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4036 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4040 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4041 high_output
&= ~nTRSTnOE
;
4043 high_output
&= ~nTRST
;
4047 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4048 high_output
|= nTRSTnOE
;
4050 high_output
|= nTRST
;
4055 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4056 high_output
&= ~nSRST
;
4058 high_output
&= ~nSRSTnOE
;
4062 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4063 high_output
|= nSRST
;
4065 high_output
|= nSRSTnOE
;
4068 /* command "set data bits high byte" */
4070 buffer_write(high_output
);
4071 buffer_write(high_direction
);
4072 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4073 "high_direction: 0x%2.2x",
4074 trst
, srst
, high_output
, high_direction
);
4076 else if (signalyzer_h_adapter_type
== 0x0000)
4080 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4081 /* switch to output pin (output is low) */
4082 low_direction
|= nTRSTnOE
;
4084 /* switch output low */
4085 low_output
&= ~nTRST
;
4089 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4090 /* switch to input pin (high-Z + internal
4091 * and external pullup) */
4092 low_direction
&= ~nTRSTnOE
;
4094 /* switch output high */
4095 low_output
|= nTRST
;
4100 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4101 /* switch output low */
4102 low_output
&= ~nSRST
;
4104 /* switch to output pin (output is low) */
4105 low_direction
|= nSRSTnOE
;
4109 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4110 /* switch output high */
4111 low_output
|= nSRST
;
4113 /* switch to input pin (high-Z) */
4114 low_direction
&= ~nSRSTnOE
;
4117 /* command "set data bits low byte" */
4119 buffer_write(low_output
);
4120 buffer_write(low_direction
);
4121 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4122 "low_direction: 0x%2.2x",
4123 trst
, srst
, low_output
, low_direction
);
4127 static void signalyzer_h_blink(void)
4129 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4132 /********************************************************************
4133 * Support for KT-LINK
4134 * JTAG adapter from KRISTECH
4135 * http://www.kristech.eu
4136 *******************************************************************/
4137 static int ktlink_init(void)
4140 uint32_t bytes_written
;
4141 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4143 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4144 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4146 // initialize low port
4147 buf
[0] = 0x80; // command "set data bits low byte"
4148 buf
[1] = low_output
;
4149 buf
[2] = low_direction
;
4150 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4152 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4154 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4155 return ERROR_JTAG_INIT_FAILED
;
4163 high_output
= 0x80; // turn LED on
4164 high_direction
= 0xFF; // all outputs
4166 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4168 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4169 high_output
|= nTRSTnOE
;
4170 high_output
&= ~nTRST
;
4172 high_output
&= ~nTRSTnOE
;
4173 high_output
|= nTRST
;
4176 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4177 high_output
&= ~nSRSTnOE
;
4178 high_output
|= nSRST
;
4180 high_output
|= nSRSTnOE
;
4181 high_output
&= ~nSRST
;
4184 // initialize high port
4185 buf
[0] = 0x82; // command "set data bits high byte"
4186 buf
[1] = high_output
; // value
4187 buf
[2] = high_direction
;
4188 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4190 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4192 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4193 return ERROR_JTAG_INIT_FAILED
;
4199 static void ktlink_reset(int trst
, int srst
)
4201 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4204 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4205 high_output
&= ~nTRSTnOE
;
4207 high_output
&= ~nTRST
;
4208 } else if (trst
== 0) {
4209 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4210 high_output
|= nTRSTnOE
;
4212 high_output
|= nTRST
;
4216 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4217 high_output
&= ~nSRST
;
4219 high_output
&= ~nSRSTnOE
;
4220 } else if (srst
== 0) {
4221 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4222 high_output
|= nSRST
;
4224 high_output
|= nSRSTnOE
;
4227 buffer_write(0x82); // command "set data bits high byte"
4228 buffer_write(high_output
);
4229 buffer_write(high_direction
);
4230 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4233 static void ktlink_blink(void)
4235 /* LED connected to ACBUS7 */
4236 if (high_output
& 0x80)
4237 high_output
&= 0x7F;
4239 high_output
|= 0x80;
4241 buffer_write(0x82); // command "set data bits high byte"
4242 buffer_write(high_output
);
4243 buffer_write(high_direction
);
4246 static const struct command_registration ft2232_command_handlers
[] = {
4248 .name
= "ft2232_device_desc",
4249 .handler
= &ft2232_handle_device_desc_command
,
4250 .mode
= COMMAND_CONFIG
,
4251 .help
= "set the USB device description of the FTDI FT2232 device",
4252 .usage
= "description_string",
4255 .name
= "ft2232_serial",
4256 .handler
= &ft2232_handle_serial_command
,
4257 .mode
= COMMAND_CONFIG
,
4258 .help
= "set the serial number of the FTDI FT2232 device",
4259 .usage
= "serial_string",
4262 .name
= "ft2232_layout",
4263 .handler
= &ft2232_handle_layout_command
,
4264 .mode
= COMMAND_CONFIG
,
4265 .help
= "set the layout of the FT2232 GPIO signals used "
4266 "to control output-enables and reset signals",
4267 .usage
= "layout_name",
4270 .name
= "ft2232_vid_pid",
4271 .handler
= &ft2232_handle_vid_pid_command
,
4272 .mode
= COMMAND_CONFIG
,
4273 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4274 .usage
= "(vid pid)* ",
4277 .name
= "ft2232_latency",
4278 .handler
= &ft2232_handle_latency_command
,
4279 .mode
= COMMAND_CONFIG
,
4280 .help
= "set the FT2232 latency timer to a new value",
4283 COMMAND_REGISTRATION_DONE
4286 struct jtag_interface ft2232_interface
= {
4288 .supported
= DEBUG_CAP_TMS_SEQ
,
4289 .commands
= ft2232_command_handlers
,
4291 .init
= ft2232_init
,
4292 .quit
= ft2232_quit
,
4293 .speed
= ft2232_speed
,
4294 .speed_div
= ft2232_speed_div
,
4296 .execute_queue
= ft2232_execute_queue
,