1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <transport/transport.h>
85 #include <helper/time_support.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
102 #include "ftd2xx_common.h"
104 enum ftdi_interface
{
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
*cmd
);
154 static char *ft2232_device_desc_A
;
155 static char *ft2232_device_desc
;
156 static char *ft2232_serial
;
157 static uint8_t ft2232_latency
= 2;
158 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
165 struct ft2232_layout
{
168 void (*reset
)(int trst
, int srst
);
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter1_init(void);
180 static int flyswatter2_init(void);
181 static int minimodule_init(void);
182 static int turtle_init(void);
183 static int comstick_init(void);
184 static int stm32stick_init(void);
185 static int axm0432_jtag_init(void);
186 static int sheevaplug_init(void);
187 static int icebear_jtag_init(void);
188 static int cortino_jtag_init(void);
189 static int signalyzer_init(void);
190 static int signalyzer_h_init(void);
191 static int ktlink_init(void);
192 static int redbee_init(void);
193 static int lisa_l_init(void);
194 static int flossjtag_init(void);
195 static int xds100v2_init(void);
197 /* reset procedures for supported layouts */
198 static void ftx23_reset(int trst
, int srst
);
199 static void jtagkey_reset(int trst
, int srst
);
200 static void olimex_jtag_reset(int trst
, int srst
);
201 static void flyswatter1_reset(int trst
, int srst
);
202 static void flyswatter2_reset(int trst
, int srst
);
203 static void minimodule_reset(int trst
, int srst
);
204 static void turtle_reset(int trst
, int srst
);
205 static void comstick_reset(int trst
, int srst
);
206 static void stm32stick_reset(int trst
, int srst
);
207 static void axm0432_jtag_reset(int trst
, int srst
);
208 static void sheevaplug_reset(int trst
, int srst
);
209 static void icebear_jtag_reset(int trst
, int srst
);
210 static void signalyzer_h_reset(int trst
, int srst
);
211 static void ktlink_reset(int trst
, int srst
);
212 static void redbee_reset(int trst
, int srst
);
213 static void xds100v2_reset(int trst
, int srst
);
215 /* blink procedures for layouts that support a blinking led */
216 static void olimex_jtag_blink(void);
217 static void flyswatter1_jtag_blink(void);
218 static void flyswatter2_jtag_blink(void);
219 static void turtle_jtag_blink(void);
220 static void signalyzer_h_blink(void);
221 static void ktlink_blink(void);
222 static void lisa_l_blink(void);
223 static void flossjtag_blink(void);
225 /* common transport support options */
227 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
229 static const struct ft2232_layout ft2232_layouts
[] = {
231 .init
= usbjtag_init
,
232 .reset
= ftx23_reset
,
235 .init
= jtagkey_init
,
236 .reset
= jtagkey_reset
,
238 { .name
= "jtagkey_prototype_v1",
239 .init
= jtagkey_init
,
240 .reset
= jtagkey_reset
,
242 { .name
= "oocdlink",
243 .init
= jtagkey_init
,
244 .reset
= jtagkey_reset
,
246 { .name
= "signalyzer",
247 .init
= signalyzer_init
,
248 .reset
= ftx23_reset
,
250 { .name
= "evb_lm3s811",
251 .init
= lm3s811_jtag_init
,
252 .reset
= ftx23_reset
,
254 { .name
= "luminary_icdi",
255 .init
= icdi_jtag_init
,
256 .reset
= ftx23_reset
,
258 { .name
= "olimex-jtag",
259 .init
= olimex_jtag_init
,
260 .reset
= olimex_jtag_reset
,
261 .blink
= olimex_jtag_blink
263 { .name
= "flyswatter",
264 .init
= flyswatter1_init
,
265 .reset
= flyswatter1_reset
,
266 .blink
= flyswatter1_jtag_blink
268 { .name
= "flyswatter2",
269 .init
= flyswatter2_init
,
270 .reset
= flyswatter2_reset
,
271 .blink
= flyswatter2_jtag_blink
273 { .name
= "minimodule",
274 .init
= minimodule_init
,
275 .reset
= minimodule_reset
,
277 { .name
= "turtelizer2",
279 .reset
= turtle_reset
,
280 .blink
= turtle_jtag_blink
282 { .name
= "comstick",
283 .init
= comstick_init
,
284 .reset
= comstick_reset
,
286 { .name
= "stm32stick",
287 .init
= stm32stick_init
,
288 .reset
= stm32stick_reset
,
290 { .name
= "axm0432_jtag",
291 .init
= axm0432_jtag_init
,
292 .reset
= axm0432_jtag_reset
,
294 { .name
= "sheevaplug",
295 .init
= sheevaplug_init
,
296 .reset
= sheevaplug_reset
,
299 .init
= icebear_jtag_init
,
300 .reset
= icebear_jtag_reset
,
303 .init
= cortino_jtag_init
,
304 .reset
= comstick_reset
,
306 { .name
= "signalyzer-h",
307 .init
= signalyzer_h_init
,
308 .reset
= signalyzer_h_reset
,
309 .blink
= signalyzer_h_blink
313 .reset
= ktlink_reset
,
314 .blink
= ktlink_blink
316 { .name
= "redbee-econotag",
318 .reset
= redbee_reset
,
320 { .name
= "redbee-usb",
322 .reset
= redbee_reset
,
323 .channel
= INTERFACE_B
,
327 .reset
= ftx23_reset
,
328 .blink
= lisa_l_blink
,
329 .channel
= INTERFACE_B
,
331 { .name
= "flossjtag",
332 .init
= flossjtag_init
,
333 .reset
= ftx23_reset
,
334 .blink
= flossjtag_blink
,
336 { .name
= "xds100v2",
337 .init
= xds100v2_init
,
338 .reset
= xds100v2_reset
,
340 { .name
= NULL
, /* END OF TABLE */ },
343 /* bitmask used to drive nTRST; usually a GPIOLx signal */
344 static uint8_t nTRST
;
345 static uint8_t nTRSTnOE
;
346 /* bitmask used to drive nSRST; usually a GPIOLx signal */
347 static uint8_t nSRST
;
348 static uint8_t nSRSTnOE
;
350 /** the layout being used with this debug session */
351 static const struct ft2232_layout
*layout
;
353 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
354 static uint8_t low_output
;
356 /* note that direction bit == 1 means that signal is an output */
358 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
359 static uint8_t low_direction
;
360 /** default value bitmask for CBUS GPIOH(0..4) */
361 static uint8_t high_output
;
362 /** default direction bitmask for CBUS GPIOH(0..4) */
363 static uint8_t high_direction
;
365 #if BUILD_FT2232_FTD2XX == 1
366 static FT_HANDLE ftdih
;
367 static FT_DEVICE ftdi_device
;
368 #elif BUILD_FT2232_LIBFTDI == 1
369 static struct ftdi_context ftdic
;
370 static enum ftdi_chip_type ftdi_device
;
373 static struct jtag_command
*first_unsent
; /* next command that has to be sent */
374 static int require_send
;
376 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
378 "There is a significant difference between libftdi and libftd2xx. The latter
379 one allows to schedule up to 64*64 bytes of result data while libftdi fails
380 with more than 4*64. As a consequence, the FT2232 driver is forced to
381 perform around 16x more USB transactions for long command streams with TDO
382 capture when running with libftdi."
385 #define FT2232_BUFFER_SIZE 131072
386 a comment would have been nice.
389 #if BUILD_FT2232_FTD2XX == 1
390 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
392 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
395 #define FT2232_BUFFER_SIZE 131072
397 static uint8_t *ft2232_buffer
;
398 static int ft2232_buffer_size
;
399 static int ft2232_read_pointer
;
400 static int ft2232_expect_read
;
403 * Function buffer_write
404 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
405 * @param val is the byte to send.
407 static inline void buffer_write(uint8_t val
)
409 assert(ft2232_buffer
);
410 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
411 ft2232_buffer
[ft2232_buffer_size
++] = val
;
415 * Function buffer_read
416 * returns a byte from the byte buffer.
418 static inline uint8_t buffer_read(void)
420 assert(ft2232_buffer
);
421 assert(ft2232_read_pointer
< ft2232_buffer_size
);
422 return ft2232_buffer
[ft2232_read_pointer
++];
426 * Clocks out \a bit_count bits on the TMS line, starting with the least
427 * significant bit of tms_bits and progressing to more significant bits.
428 * Rigorous state transition logging is done here via tap_set_state().
430 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
431 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
432 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
433 * is often used for this, 0x4b.
435 * @param tms_bits Holds the sequence of bits to send.
436 * @param tms_count Tells how many bits in the sequence.
437 * @param tdi_bit A single bit to pass on to TDI before the first TCK
438 * cycle and held static for the duration of TMS clocking.
440 * See the MPSSE spec referenced above.
442 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
446 int tms_ndx
; /* bit index into tms_byte */
448 assert(tms_count
> 0);
450 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
451 mpsse_cmd
, tms_bits
, tms_count
);
453 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>= 1) {
454 bool bit
= tms_bits
& 1;
457 tms_byte
|= (1 << tms_ndx
);
459 /* always do state transitions in public view */
460 tap_set_state(tap_state_transition(tap_get_state(), bit
));
462 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
467 if (tms_ndx
== 7 || i
== tms_count
-1) {
468 buffer_write(mpsse_cmd
);
469 buffer_write(tms_ndx
- 1);
471 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
472 * TMS/CS and is held static for the duration of TMS/CS clocking.
474 buffer_write(tms_byte
| (tdi_bit
<< 7));
480 * Function get_tms_buffer_requirements
481 * returns what clock_tms() will consume if called with
484 static inline int get_tms_buffer_requirements(int bit_count
)
486 return ((bit_count
+ 6)/7) * 3;
490 * Function move_to_state
491 * moves the TAP controller from the current state to a
492 * \a goal_state through a path given by tap_get_tms_path(). State transition
493 * logging is performed by delegation to clock_tms().
495 * @param goal_state is the destination state for the move.
497 static void move_to_state(tap_state_t goal_state
)
499 tap_state_t start_state
= tap_get_state();
501 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
502 * lookup of the required TMS pattern to move to this state from the start state.
505 /* do the 2 lookups */
506 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
507 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
509 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
511 clock_tms(0x4b, tms_bits
, tms_count
, 0);
514 static int ft2232_write(uint8_t *buf
, int size
, uint32_t *bytes_written
)
516 #if BUILD_FT2232_FTD2XX == 1
518 DWORD dw_bytes_written
= 0;
519 status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
);
520 if (status
!= FT_OK
) {
521 *bytes_written
= dw_bytes_written
;
522 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status
));
523 return ERROR_JTAG_DEVICE_ERROR
;
525 *bytes_written
= dw_bytes_written
;
527 #elif BUILD_FT2232_LIBFTDI == 1
528 int retval
= ftdi_write_data(&ftdic
, buf
, size
);
531 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
532 return ERROR_JTAG_DEVICE_ERROR
;
534 *bytes_written
= retval
;
538 if (*bytes_written
!= (uint32_t)size
)
539 return ERROR_JTAG_DEVICE_ERROR
;
544 static int ft2232_read(uint8_t *buf
, uint32_t size
, uint32_t *bytes_read
)
546 #if BUILD_FT2232_FTD2XX == 1
552 while ((*bytes_read
< size
) && timeout
--) {
553 status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
554 *bytes_read
, &dw_bytes_read
);
555 if (status
!= FT_OK
) {
557 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status
));
558 return ERROR_JTAG_DEVICE_ERROR
;
560 *bytes_read
+= dw_bytes_read
;
563 #elif BUILD_FT2232_LIBFTDI == 1
565 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
568 while ((*bytes_read
< size
) && timeout
--) {
569 retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
);
572 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
573 return ERROR_JTAG_DEVICE_ERROR
;
575 *bytes_read
+= retval
;
580 if (*bytes_read
< size
) {
581 LOG_ERROR("couldn't read enough bytes from "
582 "FT2232 device (%i < %i)",
583 (unsigned)*bytes_read
,
585 return ERROR_JTAG_DEVICE_ERROR
;
591 static bool ft2232_device_is_highspeed(void)
593 #if BUILD_FT2232_FTD2XX == 1
594 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
595 #elif BUILD_FT2232_LIBFTDI == 1
596 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
601 * Commands that only apply to the FT2232H and FT4232H devices.
602 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
603 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
606 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
608 uint8_t buf
= enable
? 0x96 : 0x97;
609 LOG_DEBUG("%2.2x", buf
);
611 uint32_t bytes_written
;
614 retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
);
615 if (retval
!= ERROR_OK
) {
616 LOG_ERROR("couldn't write command to %s adaptive clocking"
617 , enable
? "enable" : "disable");
625 * Enable/disable the clk divide by 5 of the 60MHz master clock.
626 * This result in a JTAG clock speed range of 91.553Hz-6MHz
627 * respective 457.763Hz-30MHz.
629 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
631 uint32_t bytes_written
;
632 uint8_t buf
= enable
? 0x8b : 0x8a;
634 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
635 LOG_ERROR("couldn't write command to %s clk divide by 5"
636 , enable
? "enable" : "disable");
637 return ERROR_JTAG_INIT_FAILED
;
639 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
640 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
645 static int ft2232_speed(int speed
)
649 uint32_t bytes_written
;
652 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
653 if (ft2232_device_is_highspeed())
654 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
655 else if (enable_adaptive_clocking
) {
656 LOG_ERROR("ft2232 device %lu does not support RTCK"
657 , (long unsigned int)ftdi_device
);
661 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
664 buf
[0] = 0x86; /* command "set divisor" */
665 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
666 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
668 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
669 retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
);
670 if (retval
!= ERROR_OK
) {
671 LOG_ERROR("couldn't set FT2232 TCK speed");
678 static int ft2232_speed_div(int speed
, int *khz
)
680 /* Take a look in the FT2232 manual,
681 * AN2232C-01 Command Processor for
682 * MPSSE and MCU Host Bus. Chapter 3.8 */
684 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
689 static int ft2232_khz(int khz
, int *jtag_speed
)
692 if (ft2232_device_is_highspeed()) {
693 *jtag_speed
= RTCK_SPEED
;
696 LOG_DEBUG("RCLK not supported");
701 /* Take a look in the FT2232 manual,
702 * AN2232C-01 Command Processor for
703 * MPSSE and MCU Host Bus. Chapter 3.8
705 * We will calc here with a multiplier
706 * of 10 for better rounding later. */
708 /* Calc speed, (ft2232_max_tck / khz) - 1
709 * Use 65000 for better rounding */
710 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
712 /* Add 0.9 for rounding */
715 /* Calc real speed */
716 *jtag_speed
= *jtag_speed
/ 10;
718 /* Check if speed is greater than 0 */
722 /* Check max value */
723 if (*jtag_speed
> 0xFFFF)
724 *jtag_speed
= 0xFFFF;
729 static void ft2232_end_state(tap_state_t state
)
731 if (tap_is_state_stable(state
))
732 tap_set_end_state(state
);
734 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
739 static void ft2232_read_scan(enum scan_type type
, uint8_t *buffer
, int scan_size
)
741 int num_bytes
= (scan_size
+ 7) / 8;
742 int bits_left
= scan_size
;
745 while (num_bytes
-- > 1) {
746 buffer
[cur_byte
++] = buffer_read();
750 buffer
[cur_byte
] = 0x0;
752 /* There is one more partial byte left from the clock data in/out instructions */
754 buffer
[cur_byte
] = buffer_read() >> 1;
755 /* This shift depends on the length of the
756 *clock data to tms instruction, insterted
757 *at end of the scan, now fixed to a two
758 *step transition in ft2232_add_scan */
759 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
762 static void ft2232_debug_dump_buffer(void)
768 for (i
= 0; i
< ft2232_buffer_size
; i
++) {
769 line_p
+= snprintf(line_p
,
770 sizeof(line
) - (line_p
- line
),
774 LOG_DEBUG("%s", line
);
780 LOG_DEBUG("%s", line
);
783 static int ft2232_send_and_recv(struct jtag_command
*first
, struct jtag_command
*last
)
785 struct jtag_command
*cmd
;
790 uint32_t bytes_written
= 0;
791 uint32_t bytes_read
= 0;
793 #ifdef _DEBUG_USB_IO_
794 struct timeval start
, inter
, inter2
, end
;
795 struct timeval d_inter
, d_inter2
, d_end
;
798 #ifdef _DEBUG_USB_COMMS_
799 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
800 ft2232_debug_dump_buffer();
803 #ifdef _DEBUG_USB_IO_
804 gettimeofday(&start
, NULL
);
807 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
808 if (retval
!= ERROR_OK
) {
809 LOG_ERROR("couldn't write MPSSE commands to FT2232");
813 #ifdef _DEBUG_USB_IO_
814 gettimeofday(&inter
, NULL
);
817 if (ft2232_expect_read
) {
818 /* FIXME this "timeout" is never changed ... */
819 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
820 ft2232_buffer_size
= 0;
822 #ifdef _DEBUG_USB_IO_
823 gettimeofday(&inter2
, NULL
);
826 retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
);
827 if (retval
!= ERROR_OK
) {
828 LOG_ERROR("couldn't read from FT2232");
832 #ifdef _DEBUG_USB_IO_
833 gettimeofday(&end
, NULL
);
835 timeval_subtract(&d_inter
, &inter
, &start
);
836 timeval_subtract(&d_inter2
, &inter2
, &start
);
837 timeval_subtract(&d_end
, &end
, &start
);
839 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
840 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
841 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
842 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
845 ft2232_buffer_size
= bytes_read
;
847 if (ft2232_expect_read
!= ft2232_buffer_size
) {
848 LOG_ERROR("ft2232_expect_read (%i) != "
849 "ft2232_buffer_size (%i) "
853 LIBFTDI_READ_RETRY_COUNT
- timeout
);
854 ft2232_debug_dump_buffer();
859 #ifdef _DEBUG_USB_COMMS_
860 LOG_DEBUG("read buffer (%i retries): %i bytes",
861 LIBFTDI_READ_RETRY_COUNT
- timeout
,
863 ft2232_debug_dump_buffer();
867 ft2232_expect_read
= 0;
868 ft2232_read_pointer
= 0;
870 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
871 * that wasn't handled by a caller-provided error handler
876 while (cmd
!= last
) {
879 type
= jtag_scan_type(cmd
->cmd
.scan
);
880 if (type
!= SCAN_OUT
) {
881 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
882 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
883 ft2232_read_scan(type
, buffer
, scan_size
);
884 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
885 retval
= ERROR_JTAG_QUEUE_FAILED
;
897 ft2232_buffer_size
= 0;
903 * Function ft2232_add_pathmove
904 * moves the TAP controller from the current state to a new state through the
905 * given path, where path is an array of tap_state_t's.
907 * @param path is an array of tap_stat_t which gives the states to traverse through
908 * ending with the last state at path[num_states-1]
909 * @param num_states is the count of state steps to move through
911 static void ft2232_add_pathmove(tap_state_t
*path
, int num_states
)
915 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
919 /* this loop verifies that the path is legal and logs each state in the path */
921 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
923 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
925 /* command "Clock Data to TMS/CS Pin (no Read)" */
928 /* number of states remaining */
929 buffer_write(num_states_batch
- 1);
931 while (num_states_batch
--) {
932 /* either TMS=0 or TMS=1 must work ... */
933 if (tap_state_transition(tap_get_state(), false) == path
[state_count
])
934 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
935 else if (tap_state_transition(tap_get_state(), true) == path
[state_count
])
936 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
938 /* ... or else the caller goofed BADLY */
940 LOG_ERROR("BUG: %s -> %s isn't a valid "
941 "TAP state transition",
942 tap_state_name(tap_get_state()),
943 tap_state_name(path
[state_count
]));
947 tap_set_state(path
[state_count
]);
952 buffer_write(tms_byte
);
954 tap_set_end_state(tap_get_state());
957 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
)
959 int num_bytes
= (scan_size
+ 7) / 8;
960 int bits_left
= scan_size
;
965 if (tap_get_state() != TAP_DRSHIFT
)
966 move_to_state(TAP_DRSHIFT
);
968 if (tap_get_state() != TAP_IRSHIFT
)
969 move_to_state(TAP_IRSHIFT
);
972 /* add command for complete bytes */
973 while (num_bytes
> 1) {
975 if (type
== SCAN_IO
) {
976 /* Clock Data Bytes In and Out LSB First */
978 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
979 } else if (type
== SCAN_OUT
) {
980 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
982 /* LOG_DEBUG("added TDI bytes (o)"); */
983 } else if (type
== SCAN_IN
) {
984 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
986 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
989 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
990 num_bytes
-= thisrun_bytes
;
992 buffer_write((uint8_t) (thisrun_bytes
- 1));
993 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
995 if (type
!= SCAN_IN
) {
996 /* add complete bytes */
997 while (thisrun_bytes
-- > 0) {
998 buffer_write(buffer
[cur_byte
++]);
1001 } else /* (type == SCAN_IN) */
1002 bits_left
-= 8 * (thisrun_bytes
);
1005 /* the most signifcant bit is scanned during TAP movement */
1006 if (type
!= SCAN_IN
)
1007 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1011 /* process remaining bits but the last one */
1012 if (bits_left
> 1) {
1013 if (type
== SCAN_IO
) {
1014 /* Clock Data Bits In and Out LSB First */
1016 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1017 } else if (type
== SCAN_OUT
) {
1018 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1020 /* LOG_DEBUG("added TDI bits (o)"); */
1021 } else if (type
== SCAN_IN
) {
1022 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1024 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1027 buffer_write(bits_left
- 2);
1028 if (type
!= SCAN_IN
)
1029 buffer_write(buffer
[cur_byte
]);
1032 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1033 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
))) {
1034 if (type
== SCAN_IO
) {
1035 /* Clock Data Bits In and Out LSB First */
1037 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1038 } else if (type
== SCAN_OUT
) {
1039 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1041 /* LOG_DEBUG("added TDI bits (o)"); */
1042 } else if (type
== SCAN_IN
) {
1043 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1045 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1048 buffer_write(last_bit
);
1054 /* move from Shift-IR/DR to end state */
1055 if (type
!= SCAN_OUT
) {
1056 /* We always go to the PAUSE state in two step at the end of an IN or IO
1058 * This must be coordinated with the bit shifts in ft2232_read_scan */
1061 /* Clock Data to TMS/CS Pin with Read */
1064 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1065 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1066 /* Clock Data to TMS/CS Pin (no Read) */
1070 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1071 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1074 if (tap_get_state() != tap_get_end_state())
1075 move_to_state(tap_get_end_state());
1078 static int ft2232_large_scan(struct scan_command
*cmd
,
1079 enum scan_type type
,
1083 int num_bytes
= (scan_size
+ 7) / 8;
1084 int bits_left
= scan_size
;
1087 uint8_t *receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1088 uint8_t *receive_pointer
= receive_buffer
;
1089 uint32_t bytes_written
;
1090 uint32_t bytes_read
;
1092 int thisrun_read
= 0;
1095 LOG_ERROR("BUG: large IR scans are not supported");
1099 if (tap_get_state() != TAP_DRSHIFT
)
1100 move_to_state(TAP_DRSHIFT
);
1102 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
1103 if (retval
!= ERROR_OK
) {
1104 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1107 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1108 ft2232_buffer_size
, (int)bytes_written
);
1109 ft2232_buffer_size
= 0;
1111 /* add command for complete bytes */
1112 while (num_bytes
> 1) {
1115 if (type
== SCAN_IO
) {
1116 /* Clock Data Bytes In and Out LSB First */
1118 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1119 } else if (type
== SCAN_OUT
) {
1120 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1122 /* LOG_DEBUG("added TDI bytes (o)"); */
1123 } else if (type
== SCAN_IN
) {
1124 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1126 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1129 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1130 thisrun_read
= thisrun_bytes
;
1131 num_bytes
-= thisrun_bytes
;
1132 buffer_write((uint8_t) (thisrun_bytes
- 1));
1133 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1135 if (type
!= SCAN_IN
) {
1136 /* add complete bytes */
1137 while (thisrun_bytes
-- > 0) {
1138 buffer_write(buffer
[cur_byte
]);
1142 } else /* (type == SCAN_IN) */
1143 bits_left
-= 8 * (thisrun_bytes
);
1145 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
1146 if (retval
!= ERROR_OK
) {
1147 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1150 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1152 (int)bytes_written
);
1153 ft2232_buffer_size
= 0;
1155 if (type
!= SCAN_OUT
) {
1156 retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
);
1157 if (retval
!= ERROR_OK
) {
1158 LOG_ERROR("couldn't read from FT2232");
1161 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1164 receive_pointer
+= bytes_read
;
1170 /* the most signifcant bit is scanned during TAP movement */
1171 if (type
!= SCAN_IN
)
1172 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1176 /* process remaining bits but the last one */
1177 if (bits_left
> 1) {
1178 if (type
== SCAN_IO
) {
1179 /* Clock Data Bits In and Out LSB First */
1181 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1182 } else if (type
== SCAN_OUT
) {
1183 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1185 /* LOG_DEBUG("added TDI bits (o)"); */
1186 } else if (type
== SCAN_IN
) {
1187 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1189 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1191 buffer_write(bits_left
- 2);
1192 if (type
!= SCAN_IN
)
1193 buffer_write(buffer
[cur_byte
]);
1195 if (type
!= SCAN_OUT
)
1199 if (tap_get_end_state() == TAP_DRSHIFT
) {
1200 if (type
== SCAN_IO
) {
1201 /* Clock Data Bits In and Out LSB First */
1203 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1204 } 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)"); */
1208 } else if (type
== SCAN_IN
) {
1209 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1211 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1214 buffer_write(last_bit
);
1216 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1217 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1220 /* move from Shift-IR/DR to end state */
1221 if (type
!= SCAN_OUT
) {
1222 /* Clock Data to TMS/CS Pin with Read */
1224 /* LOG_DEBUG("added TMS scan (read)"); */
1226 /* Clock Data to TMS/CS Pin (no Read) */
1228 /* LOG_DEBUG("added TMS scan (no read)"); */
1231 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1232 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1235 if (type
!= SCAN_OUT
)
1238 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
1239 if (retval
!= ERROR_OK
) {
1240 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1243 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1245 (int)bytes_written
);
1246 ft2232_buffer_size
= 0;
1248 if (type
!= SCAN_OUT
) {
1249 retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
);
1250 if (retval
!= ERROR_OK
) {
1251 LOG_ERROR("couldn't read from FT2232");
1254 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1262 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1264 int predicted_size
= 3;
1265 int num_bytes
= (scan_size
- 1) / 8;
1267 if (tap_get_state() != TAP_DRSHIFT
)
1268 predicted_size
+= get_tms_buffer_requirements(
1269 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1271 if (type
== SCAN_IN
) { /* only from device to host */
1272 /* complete bytes */
1273 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1275 /* remaining bits - 1 (up to 7) */
1276 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1277 } else {/* host to device, or bidirectional
1279 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1281 /* remaining bits -1 (up to 7) */
1282 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1285 return predicted_size
;
1288 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1290 int predicted_size
= 0;
1292 if (type
!= SCAN_OUT
) {
1293 /* complete bytes */
1295 (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1297 /* remaining bits - 1 */
1298 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1300 /* last bit (from TMS scan) */
1301 predicted_size
+= 1;
1304 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1306 return predicted_size
;
1309 /* semi-generic FT2232/FT4232 reset code */
1310 static void ftx23_reset(int trst
, int srst
)
1312 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1314 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1315 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1317 low_output
&= ~nTRST
; /* switch output low */
1318 } else if (trst
== 0) {
1319 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1320 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal
1321 *and external pullup) */
1323 low_output
|= nTRST
; /* switch output high */
1327 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1328 low_output
&= ~nSRST
; /* switch output low */
1330 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1331 } else if (srst
== 0) {
1332 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1333 low_output
|= nSRST
; /* switch output high */
1335 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1338 /* command "set data bits low byte" */
1340 buffer_write(low_output
);
1341 buffer_write(low_direction
);
1344 static void jtagkey_reset(int trst
, int srst
)
1346 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1348 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1349 high_output
&= ~nTRSTnOE
;
1351 high_output
&= ~nTRST
;
1352 } else if (trst
== 0) {
1353 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1354 high_output
|= nTRSTnOE
;
1356 high_output
|= nTRST
;
1360 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1361 high_output
&= ~nSRST
;
1363 high_output
&= ~nSRSTnOE
;
1364 } else if (srst
== 0) {
1365 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1366 high_output
|= nSRST
;
1368 high_output
|= nSRSTnOE
;
1371 /* command "set data bits high byte" */
1373 buffer_write(high_output
);
1374 buffer_write(high_direction
);
1375 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1382 static void olimex_jtag_reset(int trst
, int srst
)
1384 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1386 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1387 high_output
&= ~nTRSTnOE
;
1389 high_output
&= ~nTRST
;
1390 } else if (trst
== 0) {
1391 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1392 high_output
|= nTRSTnOE
;
1394 high_output
|= nTRST
;
1398 high_output
|= nSRST
;
1400 high_output
&= ~nSRST
;
1402 /* command "set data bits high byte" */
1404 buffer_write(high_output
);
1405 buffer_write(high_direction
);
1406 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1413 static void axm0432_jtag_reset(int trst
, int srst
)
1416 tap_set_state(TAP_RESET
);
1417 high_output
&= ~nTRST
;
1418 } else if (trst
== 0)
1419 high_output
|= nTRST
;
1422 high_output
&= ~nSRST
;
1424 high_output
|= nSRST
;
1426 /* command "set data bits low byte" */
1428 buffer_write(high_output
);
1429 buffer_write(high_direction
);
1430 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1437 static void flyswatter_reset(int trst
, int srst
)
1440 low_output
&= ~nTRST
;
1442 low_output
|= nTRST
;
1445 low_output
|= nSRST
;
1447 low_output
&= ~nSRST
;
1449 /* command "set data bits low byte" */
1451 buffer_write(low_output
);
1452 buffer_write(low_direction
);
1453 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1460 static void flyswatter1_reset(int trst
, int srst
)
1462 flyswatter_reset(trst
, srst
);
1465 static void flyswatter2_reset(int trst
, int srst
)
1467 flyswatter_reset(trst
, !srst
);
1470 static void minimodule_reset(int trst
, int srst
)
1473 low_output
&= ~nSRST
;
1475 low_output
|= nSRST
;
1477 /* command "set data bits low byte" */
1479 buffer_write(low_output
);
1480 buffer_write(low_direction
);
1481 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1488 static void turtle_reset(int trst
, int srst
)
1493 low_output
|= nSRST
;
1495 low_output
&= ~nSRST
;
1497 /* command "set data bits low byte" */
1499 buffer_write(low_output
);
1500 buffer_write(low_direction
);
1501 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1507 static void comstick_reset(int trst
, int srst
)
1510 high_output
&= ~nTRST
;
1512 high_output
|= nTRST
;
1515 high_output
&= ~nSRST
;
1517 high_output
|= nSRST
;
1519 /* command "set data bits high byte" */
1521 buffer_write(high_output
);
1522 buffer_write(high_direction
);
1523 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1530 static void stm32stick_reset(int trst
, int srst
)
1533 high_output
&= ~nTRST
;
1535 high_output
|= nTRST
;
1538 low_output
&= ~nSRST
;
1540 low_output
|= nSRST
;
1542 /* command "set data bits low byte" */
1544 buffer_write(low_output
);
1545 buffer_write(low_direction
);
1547 /* command "set data bits high byte" */
1549 buffer_write(high_output
);
1550 buffer_write(high_direction
);
1551 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1558 static void sheevaplug_reset(int trst
, int srst
)
1561 high_output
&= ~nTRST
;
1563 high_output
|= nTRST
;
1566 high_output
&= ~nSRSTnOE
;
1568 high_output
|= nSRSTnOE
;
1570 /* command "set data bits high byte" */
1572 buffer_write(high_output
);
1573 buffer_write(high_direction
);
1574 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1581 static void redbee_reset(int trst
, int srst
)
1584 tap_set_state(TAP_RESET
);
1585 high_output
&= ~nTRST
;
1586 } else if (trst
== 0)
1587 high_output
|= nTRST
;
1590 high_output
&= ~nSRST
;
1592 high_output
|= nSRST
;
1594 /* command "set data bits low byte" */
1596 buffer_write(high_output
);
1597 buffer_write(high_direction
);
1598 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1599 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1603 static void xds100v2_reset(int trst
, int srst
)
1606 tap_set_state(TAP_RESET
);
1607 high_output
&= ~nTRST
;
1608 } else if (trst
== 0)
1609 high_output
|= nTRST
;
1612 high_output
|= nSRST
;
1614 high_output
&= ~nSRST
;
1616 /* command "set data bits low byte" */
1618 buffer_write(high_output
);
1619 buffer_write(high_direction
);
1620 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1621 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1625 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1629 int predicted_size
= 0;
1632 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1633 cmd
->cmd
.runtest
->num_cycles
,
1634 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1636 /* only send the maximum buffer size that FT2232C can handle */
1638 if (tap_get_state() != TAP_IDLE
)
1639 predicted_size
+= 3;
1640 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1641 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1642 predicted_size
+= 3;
1643 if (tap_get_end_state() != TAP_IDLE
)
1644 predicted_size
+= 3;
1645 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1646 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1647 retval
= ERROR_JTAG_QUEUE_FAILED
;
1651 if (tap_get_state() != TAP_IDLE
) {
1652 move_to_state(TAP_IDLE
);
1655 i
= cmd
->cmd
.runtest
->num_cycles
;
1657 /* there are no state transitions in this code, so omit state tracking */
1659 /* command "Clock Data to TMS/CS Pin (no Read)" */
1663 buffer_write((i
> 7) ? 6 : (i
- 1));
1668 i
-= (i
> 7) ? 7 : i
;
1669 /* LOG_DEBUG("added TMS scan (no read)"); */
1672 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1674 if (tap_get_state() != tap_get_end_state())
1675 move_to_state(tap_get_end_state());
1678 DEBUG_JTAG_IO("runtest: %i, end in %s",
1679 cmd
->cmd
.runtest
->num_cycles
,
1680 tap_state_name(tap_get_end_state()));
1684 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1686 int predicted_size
= 0;
1687 int retval
= ERROR_OK
;
1689 DEBUG_JTAG_IO("statemove end in %s",
1690 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1692 /* only send the maximum buffer size that FT2232C can handle */
1694 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1695 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1696 retval
= ERROR_JTAG_QUEUE_FAILED
;
1700 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1702 /* For TAP_RESET, ignore the current recorded state. It's often
1703 * wrong at server startup, and this transation is critical whenever
1706 if (tap_get_end_state() == TAP_RESET
) {
1707 clock_tms(0x4b, 0xff, 5, 0);
1710 /* shortest-path move to desired end state */
1711 } else if (tap_get_state() != tap_get_end_state()) {
1712 move_to_state(tap_get_end_state());
1720 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1721 * (or SWD) state machine.
1723 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1725 int retval
= ERROR_OK
;
1726 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1727 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1730 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1732 /* only send the maximum buffer size that FT2232C can handle */
1733 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1734 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1735 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1736 retval
= ERROR_JTAG_QUEUE_FAILED
;
1742 /* Shift out in batches of at most 6 bits; there's a report of an
1743 * FT2232 bug in this area, where shifting exactly 7 bits can make
1744 * problems with TMS signaling for the last clock cycle:
1746 * http://developer.intra2net.com/mailarchive/html/
1747 * libftdi/2009/msg00292.html
1749 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1751 * Note that pathmoves in JTAG are not often seven bits, so that
1752 * isn't a particularly likely situation outside of "special"
1753 * signaling such as switching between JTAG and SWD modes.
1756 if (num_bits
<= 6) {
1758 buffer_write(num_bits
- 1);
1759 buffer_write(*bits
& 0x3f);
1763 /* Yes, this is lazy ... we COULD shift out more data
1764 * bits per operation, but doing it in nybbles is easy
1768 buffer_write(*bits
& 0xf);
1771 count
= (num_bits
> 4) ? 4 : num_bits
;
1774 buffer_write(count
- 1);
1775 buffer_write((*bits
>> 4) & 0xf);
1785 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1787 int predicted_size
= 0;
1788 int retval
= ERROR_OK
;
1790 tap_state_t
*path
= cmd
->cmd
.pathmove
->path
;
1791 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1793 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1794 tap_state_name(tap_get_state()),
1795 tap_state_name(path
[num_states
-1]));
1797 /* only send the maximum buffer size that FT2232C can handle */
1798 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1799 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1800 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1801 retval
= ERROR_JTAG_QUEUE_FAILED
;
1807 ft2232_add_pathmove(path
, num_states
);
1813 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1816 int scan_size
; /* size of IR or DR scan */
1817 int predicted_size
= 0;
1818 int retval
= ERROR_OK
;
1820 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1822 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1824 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1826 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1827 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
) {
1828 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1829 /* unsent commands before this */
1830 if (first_unsent
!= cmd
)
1831 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1832 retval
= ERROR_JTAG_QUEUE_FAILED
;
1834 /* current command */
1835 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1836 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1838 first_unsent
= cmd
->next
;
1842 } else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1844 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1847 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1848 retval
= ERROR_JTAG_QUEUE_FAILED
;
1852 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1853 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1854 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1855 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1859 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1860 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1861 tap_state_name(tap_get_end_state()));
1866 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1869 int predicted_size
= 0;
1872 DEBUG_JTAG_IO("reset trst: %i srst %i",
1873 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1875 /* only send the maximum buffer size that FT2232C can handle */
1877 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1878 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1879 retval
= ERROR_JTAG_QUEUE_FAILED
;
1884 if ((cmd
->cmd
.reset
->trst
== 1) ||
1885 (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1886 tap_set_state(TAP_RESET
);
1888 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1891 DEBUG_JTAG_IO("trst: %i, srst: %i",
1892 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1896 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1901 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1903 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1904 retval
= ERROR_JTAG_QUEUE_FAILED
;
1905 first_unsent
= cmd
->next
;
1906 jtag_sleep(cmd
->cmd
.sleep
->us
);
1907 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1909 tap_state_name(tap_get_state()));
1913 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1918 /* this is only allowed while in a stable state. A check for a stable
1919 * state was done in jtag_add_clocks()
1921 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1922 retval
= ERROR_JTAG_QUEUE_FAILED
;
1923 DEBUG_JTAG_IO("clocks %i while in %s",
1924 cmd
->cmd
.stableclocks
->num_cycles
,
1925 tap_state_name(tap_get_state()));
1929 static int ft2232_execute_command(struct jtag_command
*cmd
)
1933 switch (cmd
->type
) {
1935 retval
= ft2232_execute_reset(cmd
);
1938 retval
= ft2232_execute_runtest(cmd
);
1940 case JTAG_TLR_RESET
:
1941 retval
= ft2232_execute_statemove(cmd
);
1944 retval
= ft2232_execute_pathmove(cmd
);
1947 retval
= ft2232_execute_scan(cmd
);
1950 retval
= ft2232_execute_sleep(cmd
);
1952 case JTAG_STABLECLOCKS
:
1953 retval
= ft2232_execute_stableclocks(cmd
);
1956 retval
= ft2232_execute_tms(cmd
);
1959 LOG_ERROR("BUG: unknown JTAG command type encountered");
1960 retval
= ERROR_JTAG_QUEUE_FAILED
;
1966 static int ft2232_execute_queue(void)
1968 struct jtag_command
*cmd
= jtag_command_queue
; /* currently processed command */
1971 first_unsent
= cmd
; /* next command that has to be sent */
1974 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1975 * that wasn't handled by a caller-provided error handler
1979 ft2232_buffer_size
= 0;
1980 ft2232_expect_read
= 0;
1982 /* blink, if the current layout has that feature */
1987 /* fill the write buffer with the desired command */
1988 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1989 retval
= ERROR_JTAG_QUEUE_FAILED
;
1990 /* Start reading input before FT2232 TX buffer fills up.
1991 * Sometimes this happens because we don't know the
1992 * length of the last command before we execute it. So
1993 * we simple inform the user.
1997 if (ft2232_expect_read
>= FT2232_BUFFER_READ_QUEUE_SIZE
) {
1998 if (ft2232_expect_read
> (FT2232_BUFFER_READ_QUEUE_SIZE
+1))
1999 LOG_DEBUG("read buffer size looks too high %d/%d",
2001 (FT2232_BUFFER_READ_QUEUE_SIZE
+1));
2002 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2003 retval
= ERROR_JTAG_QUEUE_FAILED
;
2008 if (require_send
> 0)
2009 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2010 retval
= ERROR_JTAG_QUEUE_FAILED
;
2015 #if BUILD_FT2232_FTD2XX == 1
2016 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int *try_more
)
2020 char SerialNumber
[16];
2021 char Description
[64];
2022 DWORD openex_flags
= 0;
2023 char *openex_string
= NULL
;
2024 uint8_t latency_timer
;
2026 if (layout
== NULL
) {
2027 LOG_WARNING("No ft2232 layout specified'");
2028 return ERROR_JTAG_INIT_FAILED
;
2031 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2032 layout
->name
, vid
, pid
);
2035 /* Add non-standard Vid/Pid to the linux driver */
2036 status
= FT_SetVIDPID(vid
, pid
);
2037 if (status
!= FT_OK
)
2038 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2042 if (ft2232_device_desc
&& ft2232_serial
) {
2044 "can't open by device description and serial number, giving precedence to serial");
2045 ft2232_device_desc
= NULL
;
2048 if (ft2232_device_desc
) {
2049 openex_string
= ft2232_device_desc
;
2050 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2051 } else if (ft2232_serial
) {
2052 openex_string
= ft2232_serial
;
2053 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2055 LOG_ERROR("neither device description nor serial number specified");
2057 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2059 return ERROR_JTAG_INIT_FAILED
;
2062 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2063 if (status
!= FT_OK
) {
2064 /* under Win32, the FTD2XX driver appends an "A" to the end
2065 * of the description, if we tried by the desc, then
2066 * try by the alternate "A" description. */
2067 if (openex_string
== ft2232_device_desc
) {
2068 /* Try the alternate method. */
2069 openex_string
= ft2232_device_desc_A
;
2070 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2071 if (status
== FT_OK
) {
2072 /* yea, the "alternate" method worked! */
2074 /* drat, give the user a meaningfull message.
2075 * telling the use we tried *BOTH* methods. */
2076 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2078 ft2232_device_desc_A
);
2083 if (status
!= FT_OK
) {
2087 LOG_WARNING("unable to open ftdi device (trying more): %s",
2088 ftd2xx_status_string(status
));
2090 return ERROR_JTAG_INIT_FAILED
;
2092 LOG_ERROR("unable to open ftdi device: %s",
2093 ftd2xx_status_string(status
));
2094 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2095 if (status
== FT_OK
) {
2096 char **desc_array
= malloc(sizeof(char *) * (num_devices
+ 1));
2099 for (i
= 0; i
< num_devices
; i
++)
2100 desc_array
[i
] = malloc(64);
2102 desc_array
[num_devices
] = NULL
;
2104 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2106 if (status
== FT_OK
) {
2107 LOG_ERROR("ListDevices: %" PRIu32
, (uint32_t)num_devices
);
2108 for (i
= 0; i
< num_devices
; i
++)
2109 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2112 for (i
= 0; i
< num_devices
; i
++)
2113 free(desc_array
[i
]);
2117 LOG_ERROR("ListDevices: NONE");
2118 return ERROR_JTAG_INIT_FAILED
;
2121 status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
);
2122 if (status
!= FT_OK
) {
2123 LOG_ERROR("unable to set latency timer: %s",
2124 ftd2xx_status_string(status
));
2125 return ERROR_JTAG_INIT_FAILED
;
2128 status
= FT_GetLatencyTimer(ftdih
, &latency_timer
);
2129 if (status
!= FT_OK
) {
2130 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2131 * so ignore errors if using this driver version */
2134 status
= FT_GetDriverVersion(ftdih
, &dw_version
);
2135 LOG_ERROR("unable to get latency timer: %s",
2136 ftd2xx_status_string(status
));
2138 if ((status
== FT_OK
) && (dw_version
== 0x10004)) {
2139 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2140 "with FT_GetLatencyTimer, upgrade to a newer version");
2142 return ERROR_JTAG_INIT_FAILED
;
2144 LOG_DEBUG("current latency timer: %i", latency_timer
);
2146 status
= FT_SetTimeouts(ftdih
, 5000, 5000);
2147 if (status
!= FT_OK
) {
2148 LOG_ERROR("unable to set timeouts: %s",
2149 ftd2xx_status_string(status
));
2150 return ERROR_JTAG_INIT_FAILED
;
2153 status
= FT_SetBitMode(ftdih
, 0x0b, 2);
2154 if (status
!= FT_OK
) {
2155 LOG_ERROR("unable to enable bit i/o mode: %s",
2156 ftd2xx_status_string(status
));
2157 return ERROR_JTAG_INIT_FAILED
;
2160 status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
,
2161 SerialNumber
, Description
, NULL
);
2162 if (status
!= FT_OK
) {
2163 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2164 ftd2xx_status_string(status
));
2165 return ERROR_JTAG_INIT_FAILED
;
2167 static const char *type_str
[] = {
2168 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"
2170 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2171 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2172 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2173 LOG_INFO("device: %" PRIu32
" \"%s\"", (uint32_t)ftdi_device
, type_str
[type_index
]);
2174 LOG_INFO("deviceID: %" PRIu32
, (uint32_t)deviceID
);
2175 LOG_INFO("SerialNumber: %s", SerialNumber
);
2176 LOG_INFO("Description: %s", Description
);
2182 static int ft2232_purge_ftd2xx(void)
2186 status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
);
2187 if (status
!= FT_OK
) {
2188 LOG_ERROR("error purging ftd2xx device: %s",
2189 ftd2xx_status_string(status
));
2190 return ERROR_JTAG_INIT_FAILED
;
2196 #endif /* BUILD_FT2232_FTD2XX == 1 */
2198 #if BUILD_FT2232_LIBFTDI == 1
2199 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int *try_more
, int channel
)
2201 uint8_t latency_timer
;
2203 if (layout
== NULL
) {
2204 LOG_WARNING("No ft2232 layout specified'");
2205 return ERROR_JTAG_INIT_FAILED
;
2208 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2209 layout
->name
, vid
, pid
);
2211 if (ftdi_init(&ftdic
) < 0)
2212 return ERROR_JTAG_INIT_FAILED
;
2214 /* default to INTERFACE_A */
2215 if (channel
== INTERFACE_ANY
)
2216 channel
= INTERFACE_A
;
2217 if (ftdi_set_interface(&ftdic
, channel
) < 0) {
2218 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2219 return ERROR_JTAG_INIT_FAILED
;
2222 /* context, vendor id, product id */
2223 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
, ft2232_serial
) < 0) {
2225 LOG_WARNING("unable to open ftdi device (trying more): %s",
2228 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2230 return ERROR_JTAG_INIT_FAILED
;
2233 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2234 if (ftdi_usb_reset(&ftdic
) < 0) {
2235 LOG_ERROR("unable to reset ftdi device");
2236 return ERROR_JTAG_INIT_FAILED
;
2239 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0) {
2240 LOG_ERROR("unable to set latency timer");
2241 return ERROR_JTAG_INIT_FAILED
;
2244 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0) {
2245 LOG_ERROR("unable to get latency timer");
2246 return ERROR_JTAG_INIT_FAILED
;
2248 LOG_DEBUG("current latency timer: %i", latency_timer
);
2250 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2252 ftdi_device
= ftdic
.type
;
2253 static const char *type_str
[] = {
2254 "AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"
2256 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2257 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2258 ? ftdi_device
: no_of_known_types
;
2259 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2263 static int ft2232_purge_libftdi(void)
2265 if (ftdi_usb_purge_buffers(&ftdic
) < 0) {
2266 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2267 return ERROR_JTAG_INIT_FAILED
;
2273 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2275 static int ft2232_set_data_bits_low_byte(uint8_t value
, uint8_t direction
)
2278 uint32_t bytes_written
;
2280 buf
[0] = 0x80; /* command "set data bits low byte" */
2281 buf
[1] = value
; /* value */
2282 buf
[2] = direction
; /* direction */
2284 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2286 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
2287 LOG_ERROR("couldn't initialize data bits low byte");
2288 return ERROR_JTAG_INIT_FAILED
;
2294 static int ft2232_set_data_bits_high_byte(uint8_t value
, uint8_t direction
)
2297 uint32_t bytes_written
;
2299 buf
[0] = 0x82; /* command "set data bits high byte" */
2300 buf
[1] = value
; /* value */
2301 buf
[2] = direction
; /* direction */
2303 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2305 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
2306 LOG_ERROR("couldn't initialize data bits high byte");
2307 return ERROR_JTAG_INIT_FAILED
;
2313 static int ft2232_init(void)
2317 uint32_t bytes_written
;
2319 if (tap_get_tms_path_len(TAP_IRPAUSE
, TAP_IRPAUSE
) == 7)
2320 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2322 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2323 if (layout
== NULL
) {
2324 LOG_WARNING("No ft2232 layout specified'");
2325 return ERROR_JTAG_INIT_FAILED
;
2328 for (int i
= 0; 1; i
++) {
2330 * "more indicates that there are more IDs to try, so we should
2331 * not print an error for an ID mismatch (but for anything
2334 * try_more indicates that the error code returned indicates an
2335 * ID mismatch (and nothing else) and that we should proceeed
2336 * with the next ID pair.
2338 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2341 #if BUILD_FT2232_FTD2XX == 1
2342 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2344 #elif BUILD_FT2232_LIBFTDI == 1
2345 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2346 more
, &try_more
, layout
->channel
);
2350 if (!more
|| !try_more
)
2354 ft2232_buffer_size
= 0;
2355 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2357 if (layout
->init() != ERROR_OK
)
2358 return ERROR_JTAG_INIT_FAILED
;
2360 if (ft2232_device_is_highspeed()) {
2361 #ifndef BUILD_FT2232_HIGHSPEED
2362 #if BUILD_FT2232_FTD2XX == 1
2364 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2365 #elif BUILD_FT2232_LIBFTDI == 1
2367 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2370 /* make sure the legacy mode is disabled */
2371 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2372 return ERROR_JTAG_INIT_FAILED
;
2375 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2376 retval
= ft2232_write(buf
, 1, &bytes_written
);
2377 if (retval
!= ERROR_OK
) {
2378 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2379 return ERROR_JTAG_INIT_FAILED
;
2382 #if BUILD_FT2232_FTD2XX == 1
2383 return ft2232_purge_ftd2xx();
2384 #elif BUILD_FT2232_LIBFTDI == 1
2385 return ft2232_purge_libftdi();
2391 /** Updates defaults for DBUS signals: the four JTAG signals
2392 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2394 static inline void ftx232_dbus_init(void)
2397 low_direction
= 0x0b;
2400 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2401 * the four GPIOL signals. Initialization covers value and direction,
2402 * as customized for each layout.
2404 static int ftx232_dbus_write(void)
2406 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2407 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
2408 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2409 low_output
&= ~nTRST
; /* nTRST = 0 */
2411 low_direction
|= nTRSTnOE
; /* nTRST output */
2412 low_output
|= nTRST
; /* nTRST = 1 */
2415 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
2416 low_direction
|= nSRSTnOE
; /* nSRST output */
2417 low_output
|= nSRST
; /* nSRST = 1 */
2419 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2420 low_output
&= ~nSRST
; /* nSRST = 0 */
2423 /* initialize low byte for jtag */
2424 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2425 LOG_ERROR("couldn't initialize FT2232 DBUS");
2426 return ERROR_JTAG_INIT_FAILED
;
2432 static int usbjtag_init(void)
2435 * NOTE: This is now _specific_ to the "usbjtag" layout.
2436 * Don't try cram any more layouts into this.
2445 return ftx232_dbus_write();
2448 static int lm3s811_jtag_init(void)
2452 /* There are multiple revisions of LM3S811 eval boards:
2453 * - Rev B (and older?) boards have no SWO trace support.
2454 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2455 * they should use the "luminary_icdi" layout instead.
2462 low_direction
= 0x8b;
2464 return ftx232_dbus_write();
2467 static int icdi_jtag_init(void)
2471 /* Most Luminary eval boards support SWO trace output,
2472 * and should use this "luminary_icdi" layout.
2474 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2475 * to switch between JTAG and SWD, or switch the ft2232 UART
2476 * on the second MPSSE channel/interface (BDBUS)
2477 * between (i) the stellaris UART (on Luminary boards)
2478 * or (ii) SWO trace data (generic).
2480 * We come up in JTAG mode and may switch to SWD later (with
2481 * SWO/trace option if SWD is active).
2488 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2489 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2490 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2493 /* GPIOs on second channel/interface (UART) ... */
2494 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2495 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2496 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2501 nSRSTnOE
= ICDI_SRST
;
2503 low_direction
|= ICDI_JTAG_EN
| ICDI_DBG_ENn
;
2504 low_output
|= ICDI_JTAG_EN
;
2505 low_output
&= ~ICDI_DBG_ENn
;
2507 return ftx232_dbus_write();
2510 static int signalyzer_init(void)
2518 return ftx232_dbus_write();
2521 static int axm0432_jtag_init(void)
2524 low_direction
= 0x2b;
2526 /* initialize low byte for jtag */
2527 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2528 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2529 return ERROR_JTAG_INIT_FAILED
;
2532 if (strcmp(layout
->name
, "axm0432_jtag") == 0) {
2534 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2536 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2538 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2543 high_direction
= 0x0c;
2545 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2546 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2547 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2549 high_output
|= nTRST
;
2551 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2552 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2554 high_output
|= nSRST
;
2556 /* initialize high byte for jtag */
2557 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2558 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2559 return ERROR_JTAG_INIT_FAILED
;
2565 static int redbee_init(void)
2568 low_direction
= 0x2b;
2570 /* initialize low byte for jtag */
2571 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2572 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2573 return ERROR_JTAG_INIT_FAILED
;
2577 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2579 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2582 high_direction
= 0x0c;
2584 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2585 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2586 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2588 high_output
|= nTRST
;
2590 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2591 LOG_ERROR("can't set nSRST to push-pull on redbee");
2593 high_output
|= nSRST
;
2595 /* initialize high byte for jtag */
2596 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2597 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2598 return ERROR_JTAG_INIT_FAILED
;
2604 static int jtagkey_init(void)
2607 low_direction
= 0x1b;
2609 /* initialize low byte for jtag */
2610 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2611 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2612 return ERROR_JTAG_INIT_FAILED
;
2615 if (strcmp(layout
->name
, "jtagkey") == 0) {
2620 } else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2621 || (strcmp(layout
->name
, "oocdlink") == 0)) {
2627 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2632 high_direction
= 0x0f;
2634 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2635 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
2636 high_output
|= nTRSTnOE
;
2637 high_output
&= ~nTRST
;
2639 high_output
&= ~nTRSTnOE
;
2640 high_output
|= nTRST
;
2643 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
2644 high_output
&= ~nSRSTnOE
;
2645 high_output
|= nSRST
;
2647 high_output
|= nSRSTnOE
;
2648 high_output
&= ~nSRST
;
2651 /* initialize high byte for jtag */
2652 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2653 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2654 return ERROR_JTAG_INIT_FAILED
;
2660 static int olimex_jtag_init(void)
2663 low_direction
= 0x1b;
2665 /* initialize low byte for jtag */
2666 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2667 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2668 return ERROR_JTAG_INIT_FAILED
;
2674 nSRSTnOE
= 0x00;/* no output enable for nSRST */
2677 high_direction
= 0x0f;
2679 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2680 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
2681 high_output
|= nTRSTnOE
;
2682 high_output
&= ~nTRST
;
2684 high_output
&= ~nTRSTnOE
;
2685 high_output
|= nTRST
;
2688 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2689 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2691 high_output
&= ~nSRST
;
2693 /* turn red LED on */
2694 high_output
|= 0x08;
2696 /* initialize high byte for jtag */
2697 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2698 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2699 return ERROR_JTAG_INIT_FAILED
;
2705 static int flyswatter_init(int rev
)
2708 low_direction
= 0x7b;
2710 if ((rev
< 0) || (rev
> 3)) {
2711 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev
);
2712 return ERROR_JTAG_INIT_FAILED
;
2716 low_direction
|= 1 << 7;
2718 /* initialize low byte for jtag */
2719 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2720 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2721 return ERROR_JTAG_INIT_FAILED
;
2725 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2727 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2732 high_direction
= 0x0c;
2734 high_direction
= 0x01;
2736 /* turn red LED3 on, LED2 off */
2737 high_output
|= 0x08;
2739 /* initialize high byte for jtag */
2740 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2741 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2742 return ERROR_JTAG_INIT_FAILED
;
2748 static int flyswatter1_init(void)
2750 return flyswatter_init(1);
2753 static int flyswatter2_init(void)
2755 return flyswatter_init(2);
2758 static int minimodule_init(void)
2760 low_output
= 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2762 low_direction
= 0xfb; /* 0xfb; */
2764 /* initialize low byte for jtag */
2765 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2766 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2767 return ERROR_JTAG_INIT_FAILED
;
2774 high_direction
= 0x05;
2776 /* turn red LED3 on, LED2 off */
2777 /* high_output |= 0x08; */
2779 /* initialize high byte for jtag */
2780 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2781 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2782 return ERROR_JTAG_INIT_FAILED
;
2788 static int turtle_init(void)
2791 low_direction
= 0x5b;
2793 /* initialize low byte for jtag */
2794 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2795 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2796 return ERROR_JTAG_INIT_FAILED
;
2802 high_direction
= 0x0C;
2804 /* initialize high byte for jtag */
2805 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2806 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2807 return ERROR_JTAG_INIT_FAILED
;
2813 static int comstick_init(void)
2816 low_direction
= 0x0b;
2818 /* initialize low byte for jtag */
2819 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2820 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2821 return ERROR_JTAG_INIT_FAILED
;
2825 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2827 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2830 high_direction
= 0x03;
2832 /* initialize high byte for jtag */
2833 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2834 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2835 return ERROR_JTAG_INIT_FAILED
;
2841 static int stm32stick_init(void)
2844 low_direction
= 0x8b;
2846 /* initialize low byte for jtag */
2847 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2848 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2849 return ERROR_JTAG_INIT_FAILED
;
2853 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2855 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2858 high_direction
= 0x03;
2860 /* initialize high byte for jtag */
2861 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2862 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2863 return ERROR_JTAG_INIT_FAILED
;
2869 static int sheevaplug_init(void)
2872 low_direction
= 0x1b;
2874 /* initialize low byte for jtag */
2875 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2876 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2877 return ERROR_JTAG_INIT_FAILED
;
2886 high_direction
= 0x0f;
2888 /* nTRST is always push-pull */
2889 high_output
&= ~nTRSTnOE
;
2890 high_output
|= nTRST
;
2892 /* nSRST is always open-drain */
2893 high_output
|= nSRSTnOE
;
2894 high_output
&= ~nSRST
;
2896 /* initialize high byte for jtag */
2897 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2898 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2899 return ERROR_JTAG_INIT_FAILED
;
2905 static int cortino_jtag_init(void)
2908 low_direction
= 0x1b;
2910 /* initialize low byte for jtag */
2911 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2912 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2913 return ERROR_JTAG_INIT_FAILED
;
2917 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2919 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2922 high_direction
= 0x03;
2924 /* initialize high byte for jtag */
2925 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2926 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2927 return ERROR_JTAG_INIT_FAILED
;
2933 static int lisa_l_init(void)
2943 high_direction
= 0x18;
2945 /* initialize high byte for jtag */
2946 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2947 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2948 return ERROR_JTAG_INIT_FAILED
;
2951 return ftx232_dbus_write();
2954 static int flossjtag_init(void)
2964 high_direction
= 0x18;
2966 /* initialize high byte for jtag */
2967 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2968 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2969 return ERROR_JTAG_INIT_FAILED
;
2972 return ftx232_dbus_write();
2976 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
2977 * the door for a number of different configurations
2979 * Known Implementations:
2980 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
2982 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
2983 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
2984 * * ACBUS3 to transition 0->1 (OE rising edge)
2985 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
2986 * * ADBUS5/GPIOL1 = EMU_EN = 1
2987 * * ADBUS6/GPIOL2 = EMU0 = 0
2988 * * ACBUS4/SPARE0 = EMU1 = 0
2989 * * CPLD logic: Disable loopback
2990 * * ACBUS6/SPARE2 = LOOPBACK = 0
2992 #define XDS100_nEMU_EN (1<<5)
2993 #define XDS100_nEMU0 (1<<6)
2995 #define XDS100_PWR_RST (1<<3)
2996 #define XDS100_nEMU1 (1<<4)
2997 #define XDS100_LOOPBACK (1<<6)
2998 static int xds100v2_init(void)
3000 /* These are in the lower byte */
3004 /* These aren't actually used on 14 pin connectors
3005 * These are in the upper byte */
3009 low_output
= 0x08 | nTRST
| XDS100_nEMU_EN
;
3010 low_direction
= 0x0b | nTRSTnOE
| XDS100_nEMU_EN
| XDS100_nEMU0
;
3012 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
3013 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3014 return ERROR_JTAG_INIT_FAILED
;
3018 high_direction
= nSRSTnOE
| XDS100_LOOPBACK
| XDS100_PWR_RST
| XDS100_nEMU1
;
3020 /* initialize high byte for jtag */
3021 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3022 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3023 return ERROR_JTAG_INIT_FAILED
;
3026 high_output
|= XDS100_PWR_RST
;
3028 /* initialize high byte for jtag */
3029 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3030 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3031 return ERROR_JTAG_INIT_FAILED
;
3037 static void olimex_jtag_blink(void)
3039 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3040 * ACBUS3 is bit 3 of the GPIOH port
3042 high_output
^= 0x08;
3045 buffer_write(high_output
);
3046 buffer_write(high_direction
);
3049 static void flyswatter_jtag_blink(unsigned char led
)
3052 buffer_write(high_output
^ led
);
3053 buffer_write(high_direction
);
3056 static void flyswatter1_jtag_blink(void)
3059 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3061 flyswatter_jtag_blink(0xc);
3064 static void flyswatter2_jtag_blink(void)
3067 * Flyswatter2 only has one LED connected to ACBUS2
3069 flyswatter_jtag_blink(0x4);
3072 static void turtle_jtag_blink(void)
3075 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3077 if (high_output
& 0x08)
3083 buffer_write(high_output
);
3084 buffer_write(high_direction
);
3087 static void lisa_l_blink(void)
3090 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3092 if (high_output
& 0x10)
3098 buffer_write(high_output
);
3099 buffer_write(high_direction
);
3102 static void flossjtag_blink(void)
3105 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3107 if (high_output
& 0x10)
3113 buffer_write(high_output
);
3114 buffer_write(high_direction
);
3117 static int ft2232_quit(void)
3119 #if BUILD_FT2232_FTD2XX == 1
3122 status
= FT_Close(ftdih
);
3123 #elif BUILD_FT2232_LIBFTDI == 1
3124 ftdi_usb_close(&ftdic
);
3126 ftdi_deinit(&ftdic
);
3129 free(ft2232_buffer
);
3130 ft2232_buffer
= NULL
;
3135 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3139 if (CMD_ARGC
== 1) {
3140 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3141 cp
= strchr(ft2232_device_desc
, 0);
3142 /* under Win32, the FTD2XX driver appends an "A" to the end
3143 * of the description, this examines the given desc
3144 * and creates the 'missing' _A or non_A variable. */
3145 if ((cp
[-1] == 'A') && (cp
[-2] == ' ')) {
3146 /* it was, so make this the "A" version. */
3147 ft2232_device_desc_A
= ft2232_device_desc
;
3148 /* and *CREATE* the non-A version. */
3149 strcpy(buf
, ft2232_device_desc
);
3150 cp
= strchr(buf
, 0);
3152 ft2232_device_desc
= strdup(buf
);
3154 /* <space > A not defined
3156 sprintf(buf
, "%s A", ft2232_device_desc
);
3157 ft2232_device_desc_A
= strdup(buf
);
3160 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3165 COMMAND_HANDLER(ft2232_handle_serial_command
)
3168 ft2232_serial
= strdup(CMD_ARGV
[0]);
3170 return ERROR_COMMAND_SYNTAX_ERROR
;
3175 COMMAND_HANDLER(ft2232_handle_layout_command
)
3178 return ERROR_COMMAND_SYNTAX_ERROR
;
3181 LOG_ERROR("already specified ft2232_layout %s",
3183 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3188 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3189 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3195 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3199 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3201 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
3202 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3203 "(maximum is %d pairs)", MAX_USB_IDS
);
3204 CMD_ARGC
= MAX_USB_IDS
* 2;
3206 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3207 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3209 return ERROR_COMMAND_SYNTAX_ERROR
;
3210 /* remove the incomplete trailing id */
3215 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3216 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3217 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3221 * Explicitly terminate, in case there are multiples instances of
3224 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3229 COMMAND_HANDLER(ft2232_handle_latency_command
)
3232 ft2232_latency
= atoi(CMD_ARGV
[0]);
3234 return ERROR_COMMAND_SYNTAX_ERROR
;
3239 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
*cmd
)
3243 /* 7 bits of either ones or zeros. */
3244 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3246 while (num_cycles
> 0) {
3247 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3248 * at most 7 bits per invocation. Here we invoke it potentially
3251 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3253 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
) {
3254 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3255 retval
= ERROR_JTAG_QUEUE_FAILED
;
3260 /* there are no state transitions in this code, so omit state tracking */
3262 /* command "Clock Data to TMS/CS Pin (no Read)" */
3266 buffer_write(bitcount_per_command
- 1);
3268 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3273 num_cycles
-= bitcount_per_command
;
3279 /* ---------------------------------------------------------------------
3280 * Support for IceBear JTAG adapter from Section5:
3281 * http://section5.ch/icebear
3283 * Author: Sten, debian@sansys-electronic.com
3286 /* Icebear pin layout
3288 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3289 * GND GND | 4 3| n.c.
3290 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3291 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3292 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3293 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3294 * ADBUS2 TDO |14 13| GND GND
3296 * ADBUS0 O L TCK ACBUS0 GND
3297 * ADBUS1 O L TDI ACBUS1 GND
3298 * ADBUS2 I TDO ACBUS2 n.c.
3299 * ADBUS3 O H TMS ACBUS3 n.c.
3305 static int icebear_jtag_init(void)
3307 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3308 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3312 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3313 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3314 low_direction
&= ~nTRST
; /* nTRST high impedance */
3316 low_direction
|= nTRST
;
3317 low_output
|= nTRST
;
3320 low_direction
|= nSRST
;
3321 low_output
|= nSRST
;
3323 /* initialize low byte for jtag */
3324 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
3325 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3326 return ERROR_JTAG_INIT_FAILED
;
3330 high_direction
= 0x00;
3332 /* initialize high byte for jtag */
3333 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3334 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3335 return ERROR_JTAG_INIT_FAILED
;
3341 static void icebear_jtag_reset(int trst
, int srst
)
3344 low_direction
|= nTRST
;
3345 low_output
&= ~nTRST
;
3346 } else if (trst
== 0) {
3347 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3348 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3349 low_direction
&= ~nTRST
;
3351 low_output
|= nTRST
;
3355 low_output
&= ~nSRST
;
3357 low_output
|= nSRST
;
3359 /* command "set data bits low byte" */
3361 buffer_write(low_output
);
3362 buffer_write(low_direction
);
3364 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3371 /* ---------------------------------------------------------------------
3372 * Support for Signalyzer H2 and Signalyzer H4
3373 * JTAG adapter from Xverve Technologies Inc.
3374 * http://www.signalyzer.com or http://www.xverve.com
3376 * Author: Oleg Seiljus, oleg@signalyzer.com
3378 static unsigned char signalyzer_h_side
;
3379 static unsigned int signalyzer_h_adapter_type
;
3381 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3383 #if BUILD_FT2232_FTD2XX == 1
3384 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3387 #define SIGNALYZER_COMMAND_ADDR 128
3388 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3390 #define SIGNALYZER_COMMAND_VERSION 0x41
3391 #define SIGNALYZER_COMMAND_RESET 0x42
3392 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3393 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3394 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3395 #define SIGNALYZER_COMMAND_LED_SET 0x53
3396 #define SIGNALYZER_COMMAND_ADC 0x54
3397 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3398 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3399 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3400 #define SIGNALYZER_COMMAND_I2C 0x58
3402 #define SIGNALYZER_CHAN_A 1
3403 #define SIGNALYZER_CHAN_B 2
3404 /* LEDS use channel C */
3405 #define SIGNALYZER_CHAN_C 4
3407 #define SIGNALYZER_LED_GREEN 1
3408 #define SIGNALYZER_LED_RED 2
3410 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3411 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3412 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3413 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3414 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3417 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3419 #if BUILD_FT2232_FTD2XX == 1
3420 return FT_WriteEE(ftdih
, address
, value
);
3421 #elif BUILD_FT2232_LIBFTDI == 1
3426 #if BUILD_FT2232_FTD2XX == 1
3427 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3429 return FT_ReadEE(ftdih
, address
, value
);
3433 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3434 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3436 unsigned char on_time
;
3437 unsigned char off_time
;
3439 if (on_time_ms
< 0xFFFF)
3440 on_time
= (unsigned char)(on_time_ms
/ 62);
3444 off_time
= (unsigned char)(off_time_ms
/ 62);
3446 #if BUILD_FT2232_FTD2XX == 1
3449 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3450 ((uint32_t)(channel
<< 8) | led
));
3451 if (status
!= FT_OK
) {
3452 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3453 ftd2xx_status_string(status
));
3454 return ERROR_JTAG_DEVICE_ERROR
;
3457 status
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3458 ((uint32_t)(on_time
<< 8) | off_time
));
3459 if (status
!= FT_OK
) {
3460 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3461 ftd2xx_status_string(status
));
3462 return ERROR_JTAG_DEVICE_ERROR
;
3465 status
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3466 ((uint32_t)cycles
));
3467 if (status
!= FT_OK
) {
3468 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3469 ftd2xx_status_string(status
));
3470 return ERROR_JTAG_DEVICE_ERROR
;
3473 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3474 SIGNALYZER_COMMAND_LED_SET
);
3475 if (status
!= FT_OK
) {
3476 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3477 ftd2xx_status_string(status
));
3478 return ERROR_JTAG_DEVICE_ERROR
;
3482 #elif BUILD_FT2232_LIBFTDI == 1
3485 retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3486 ((uint32_t)(channel
<< 8) | led
));
3488 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3489 ftdi_get_error_string(&ftdic
));
3490 return ERROR_JTAG_DEVICE_ERROR
;
3493 retval
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3494 ((uint32_t)(on_time
<< 8) | off_time
));
3496 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3497 ftdi_get_error_string(&ftdic
));
3498 return ERROR_JTAG_DEVICE_ERROR
;
3501 retval
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3504 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3505 ftdi_get_error_string(&ftdic
));
3506 return ERROR_JTAG_DEVICE_ERROR
;
3509 retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3510 SIGNALYZER_COMMAND_LED_SET
);
3512 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3513 ftdi_get_error_string(&ftdic
));
3514 return ERROR_JTAG_DEVICE_ERROR
;
3521 static int signalyzer_h_init(void)
3523 #if BUILD_FT2232_FTD2XX == 1
3530 uint16_t read_buf
[12] = { 0 };
3532 /* turn on center green led */
3533 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3534 0xFFFF, 0x00, 0x00);
3536 /* determine what channel config wants to open
3537 * TODO: change me... current implementation is made to work
3538 * with openocd description parsing.
3540 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3543 signalyzer_h_side
= *(end_of_desc
- 1);
3544 if (signalyzer_h_side
== 'B')
3545 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3547 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3549 LOG_ERROR("No Channel was specified");
3553 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3556 #if BUILD_FT2232_FTD2XX == 1
3557 /* read signalyzer versionining information */
3558 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3559 SIGNALYZER_COMMAND_VERSION
);
3560 if (status
!= FT_OK
) {
3561 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3562 ftd2xx_status_string(status
));
3563 return ERROR_JTAG_DEVICE_ERROR
;
3566 for (i
= 0; i
< 10; i
++) {
3567 status
= signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3569 if (status
!= FT_OK
) {
3570 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3571 ftd2xx_status_string(status
));
3572 return ERROR_JTAG_DEVICE_ERROR
;
3576 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3577 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3578 read_buf
[4], read_buf
[5], read_buf
[6]);
3580 /* set gpio register */
3581 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3582 (uint32_t)(signalyzer_h_side
<< 8));
3583 if (status
!= FT_OK
) {
3584 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3585 ftd2xx_status_string(status
));
3586 return ERROR_JTAG_DEVICE_ERROR
;
3589 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0404);
3590 if (status
!= FT_OK
) {
3591 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3592 ftd2xx_status_string(status
));
3593 return ERROR_JTAG_DEVICE_ERROR
;
3596 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3597 SIGNALYZER_COMMAND_GPIO_STATE
);
3598 if (status
!= FT_OK
) {
3599 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3600 ftd2xx_status_string(status
));
3601 return ERROR_JTAG_DEVICE_ERROR
;
3604 /* read adapter type information */
3605 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3606 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01));
3607 if (status
!= FT_OK
) {
3608 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3609 ftd2xx_status_string(status
));
3610 return ERROR_JTAG_DEVICE_ERROR
;
3613 status
= signalyzer_h_ctrl_write(
3614 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000);
3615 if (status
!= FT_OK
) {
3616 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3617 ftd2xx_status_string(status
));
3618 return ERROR_JTAG_DEVICE_ERROR
;
3621 status
= signalyzer_h_ctrl_write(
3622 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008);
3623 if (status
!= FT_OK
) {
3624 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3625 ftd2xx_status_string(status
));
3626 return ERROR_JTAG_DEVICE_ERROR
;
3629 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3630 SIGNALYZER_COMMAND_I2C
);
3631 if (status
!= FT_OK
) {
3632 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3633 ftd2xx_status_string(status
));
3634 return ERROR_JTAG_DEVICE_ERROR
;
3639 status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
, &read_buf
[0]);
3640 if (status
!= FT_OK
) {
3641 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3642 ftd2xx_status_string(status
));
3643 return ERROR_JTAG_DEVICE_ERROR
;
3646 if (read_buf
[0] != 0x0498)
3647 signalyzer_h_adapter_type
= 0x0000;
3649 for (i
= 0; i
< 4; i
++) {
3650 status
= signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR
+ i
), &read_buf
[i
]);
3651 if (status
!= FT_OK
) {
3652 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3653 ftd2xx_status_string(status
));
3654 return ERROR_JTAG_DEVICE_ERROR
;
3658 signalyzer_h_adapter_type
= read_buf
[0];
3661 #elif BUILD_FT2232_LIBFTDI == 1
3662 /* currently libftdi does not allow reading individual eeprom
3663 * locations, therefore adapter type cannot be detected.
3664 * override with most common type
3666 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3669 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3671 /* ADAPTOR: EM_LT16_A */
3672 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
) {
3673 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3674 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3682 low_direction
= 0x1b;
3685 high_direction
= 0x0;
3687 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3688 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3689 low_output
&= ~nTRST
; /* nTRST = 0 */
3691 low_direction
|= nTRSTnOE
; /* nTRST output */
3692 low_output
|= nTRST
; /* nTRST = 1 */
3695 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3696 low_direction
|= nSRSTnOE
; /* nSRST output */
3697 low_output
|= nSRST
; /* nSRST = 1 */
3699 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3700 low_output
&= ~nSRST
; /* nSRST = 0 */
3703 #if BUILD_FT2232_FTD2XX == 1
3704 /* enable power to the module */
3705 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3706 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01));
3707 if (status
!= FT_OK
) {
3708 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3709 ftd2xx_status_string(status
));
3710 return ERROR_JTAG_DEVICE_ERROR
;
3713 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3714 SIGNALYZER_COMMAND_POWERCONTROL_SET
);
3715 if (status
!= FT_OK
) {
3716 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3717 ftd2xx_status_string(status
));
3718 return ERROR_JTAG_DEVICE_ERROR
;
3721 /* set gpio mode register */
3722 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3723 (uint32_t)(signalyzer_h_side
<< 8));
3724 if (status
!= FT_OK
) {
3725 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3726 ftd2xx_status_string(status
));
3727 return ERROR_JTAG_DEVICE_ERROR
;
3730 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000);
3731 if (status
!= FT_OK
) {
3732 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3733 ftd2xx_status_string(status
));
3734 return ERROR_JTAG_DEVICE_ERROR
;
3737 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
, SIGNALYZER_COMMAND_GPIO_MODE
);
3738 if (status
!= FT_OK
) {
3739 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3740 ftd2xx_status_string(status
));
3741 return ERROR_JTAG_DEVICE_ERROR
;
3744 /* set gpio register */
3745 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3746 (uint32_t)(signalyzer_h_side
<< 8));
3747 if (status
!= FT_OK
) {
3748 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3749 ftd2xx_status_string(status
));
3750 return ERROR_JTAG_DEVICE_ERROR
;
3753 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040);
3754 if (status
!= FT_OK
) {
3755 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3756 ftd2xx_status_string(status
));
3757 return ERROR_JTAG_DEVICE_ERROR
;
3760 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3761 SIGNALYZER_COMMAND_GPIO_STATE
);
3762 if (status
!= FT_OK
) {
3763 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3764 ftd2xx_status_string(status
));
3765 return ERROR_JTAG_DEVICE_ERROR
;
3769 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3770 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3771 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3772 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3773 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)) {
3774 if (signalyzer_h_adapter_type
3775 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3776 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3777 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3778 else if (signalyzer_h_adapter_type
3779 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3780 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3781 "(ARM JTAG with PSU) detected. (HW: %2x).",
3782 (read_buf
[1] >> 8));
3783 else if (signalyzer_h_adapter_type
3784 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3785 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3786 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3787 else if (signalyzer_h_adapter_type
3788 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3789 LOG_INFO("Signalyzer: EM-JTAG-P "
3790 "(Generic JTAG with PSU) detected. (HW: %2x).",
3791 (read_buf
[1] >> 8));
3799 low_direction
= 0x1b;
3802 high_direction
= 0x1f;
3804 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3805 high_output
|= nTRSTnOE
;
3806 high_output
&= ~nTRST
;
3808 high_output
&= ~nTRSTnOE
;
3809 high_output
|= nTRST
;
3812 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3813 high_output
&= ~nSRSTnOE
;
3814 high_output
|= nSRST
;
3816 high_output
|= nSRSTnOE
;
3817 high_output
&= ~nSRST
;
3820 #if BUILD_FT2232_FTD2XX == 1
3821 /* enable power to the module */
3822 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3823 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01));
3824 if (status
!= FT_OK
) {
3825 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3826 ftd2xx_status_string(status
));
3827 return ERROR_JTAG_DEVICE_ERROR
;
3830 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3831 SIGNALYZER_COMMAND_POWERCONTROL_SET
);
3832 if (status
!= FT_OK
) {
3833 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3834 ftd2xx_status_string(status
));
3835 return ERROR_JTAG_DEVICE_ERROR
;
3838 /* set gpio mode register (IO_16 and IO_17 set as analog
3839 * inputs, other is gpio)
3841 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3842 (uint32_t)(signalyzer_h_side
<< 8));
3843 if (status
!= FT_OK
) {
3844 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3845 ftd2xx_status_string(status
));
3846 return ERROR_JTAG_DEVICE_ERROR
;
3849 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060);
3850 if (status
!= FT_OK
) {
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3852 ftd2xx_status_string(status
));
3853 return ERROR_JTAG_DEVICE_ERROR
;
3856 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
, SIGNALYZER_COMMAND_GPIO_MODE
);
3857 if (status
!= FT_OK
) {
3858 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3859 ftd2xx_status_string(status
));
3860 return ERROR_JTAG_DEVICE_ERROR
;
3863 /* set gpio register (all inputs, for -P modules,
3864 * PSU will be turned off)
3866 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3867 (uint32_t)(signalyzer_h_side
<< 8));
3868 if (status
!= FT_OK
) {
3869 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3870 ftd2xx_status_string(status
));
3871 return ERROR_JTAG_DEVICE_ERROR
;
3874 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000);
3875 if (status
!= FT_OK
) {
3876 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3877 ftd2xx_status_string(status
));
3878 return ERROR_JTAG_DEVICE_ERROR
;
3881 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
, SIGNALYZER_COMMAND_GPIO_STATE
);
3882 if (status
!= FT_OK
) {
3883 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3884 ftd2xx_status_string(status
));
3885 return ERROR_JTAG_DEVICE_ERROR
;
3888 } else if (signalyzer_h_adapter_type
== 0x0000) {
3889 LOG_INFO("Signalyzer: No external modules were detected.");
3897 low_direction
= 0x1b;
3900 high_direction
= 0x0;
3902 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3903 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3904 low_output
&= ~nTRST
; /* nTRST = 0 */
3906 low_direction
|= nTRSTnOE
; /* nTRST output */
3907 low_output
|= nTRST
; /* nTRST = 1 */
3910 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3911 low_direction
|= nSRSTnOE
; /* nSRST output */
3912 low_output
|= nSRST
; /* nSRST = 1 */
3914 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3915 low_output
&= ~nSRST
; /* nSRST = 0 */
3918 LOG_ERROR("Unknown module type is detected: %.4x",
3919 signalyzer_h_adapter_type
);
3920 return ERROR_JTAG_DEVICE_ERROR
;
3923 /* initialize low byte of controller for jtag operation */
3924 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
3925 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3926 return ERROR_JTAG_INIT_FAILED
;
3929 #if BUILD_FT2232_FTD2XX == 1
3930 if (ftdi_device
== FT_DEVICE_2232H
) {
3931 /* initialize high byte of controller for jtag operation */
3932 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3933 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3934 return ERROR_JTAG_INIT_FAILED
;
3937 #elif BUILD_FT2232_LIBFTDI == 1
3938 if (ftdi_device
== TYPE_2232H
) {
3939 /* initialize high byte of controller for jtag operation */
3940 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3941 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3942 return ERROR_JTAG_INIT_FAILED
;
3949 static void signalyzer_h_reset(int trst
, int srst
)
3951 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3953 /* ADAPTOR: EM_LT16_A */
3954 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
) {
3956 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3957 /* switch to output pin (output is low) */
3958 low_direction
|= nTRSTnOE
;
3960 /* switch output low */
3961 low_output
&= ~nTRST
;
3962 } else if (trst
== 0) {
3963 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3964 /* switch to input pin (high-Z + internal
3965 * and external pullup) */
3966 low_direction
&= ~nTRSTnOE
;
3968 /* switch output high */
3969 low_output
|= nTRST
;
3973 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3974 /* switch output low */
3975 low_output
&= ~nSRST
;
3977 /* switch to output pin (output is low) */
3978 low_direction
|= nSRSTnOE
;
3979 } else if (srst
== 0) {
3980 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3981 /* switch output high */
3982 low_output
|= nSRST
;
3984 /* switch to input pin (high-Z) */
3985 low_direction
&= ~nSRSTnOE
;
3988 /* command "set data bits low byte" */
3990 buffer_write(low_output
);
3991 buffer_write(low_direction
);
3992 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3993 "low_direction: 0x%2.2x",
3994 trst
, srst
, low_output
, low_direction
);
3996 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3997 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3998 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3999 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4000 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)) {
4002 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4003 high_output
&= ~nTRSTnOE
;
4005 high_output
&= ~nTRST
;
4006 } else if (trst
== 0) {
4007 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4008 high_output
|= nTRSTnOE
;
4010 high_output
|= nTRST
;
4014 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4015 high_output
&= ~nSRST
;
4017 high_output
&= ~nSRSTnOE
;
4018 } else if (srst
== 0) {
4019 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4020 high_output
|= nSRST
;
4022 high_output
|= nSRSTnOE
;
4025 /* command "set data bits high byte" */
4027 buffer_write(high_output
);
4028 buffer_write(high_direction
);
4029 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4030 "high_direction: 0x%2.2x",
4031 trst
, srst
, high_output
, high_direction
);
4032 } else if (signalyzer_h_adapter_type
== 0x0000) {
4034 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4035 /* switch to output pin (output is low) */
4036 low_direction
|= nTRSTnOE
;
4038 /* switch output low */
4039 low_output
&= ~nTRST
;
4040 } else if (trst
== 0) {
4041 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4042 /* switch to input pin (high-Z + internal
4043 * and external pullup) */
4044 low_direction
&= ~nTRSTnOE
;
4046 /* switch output high */
4047 low_output
|= nTRST
;
4051 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4052 /* switch output low */
4053 low_output
&= ~nSRST
;
4055 /* switch to output pin (output is low) */
4056 low_direction
|= nSRSTnOE
;
4057 } else if (srst
== 0) {
4058 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4059 /* switch output high */
4060 low_output
|= nSRST
;
4062 /* switch to input pin (high-Z) */
4063 low_direction
&= ~nSRSTnOE
;
4066 /* command "set data bits low byte" */
4068 buffer_write(low_output
);
4069 buffer_write(low_direction
);
4070 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4071 "low_direction: 0x%2.2x",
4072 trst
, srst
, low_output
, low_direction
);
4076 static void signalyzer_h_blink(void)
4078 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4081 /********************************************************************
4082 * Support for KT-LINK
4083 * JTAG adapter from KRISTECH
4084 * http://www.kristech.eu
4085 *******************************************************************/
4086 static int ktlink_init(void)
4088 uint8_t swd_en
= 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4090 low_output
= 0x08 | swd_en
; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4091 low_direction
= 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4093 /* initialize low byte for jtag */
4094 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
4095 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4096 return ERROR_JTAG_INIT_FAILED
;
4104 high_output
= 0x80; /* turn LED on */
4105 high_direction
= 0xFF; /* all outputs */
4107 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4109 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4110 high_output
|= nTRSTnOE
;
4111 high_output
&= ~nTRST
;
4113 high_output
&= ~nTRSTnOE
;
4114 high_output
|= nTRST
;
4117 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4118 high_output
&= ~nSRSTnOE
;
4119 high_output
|= nSRST
;
4121 high_output
|= nSRSTnOE
;
4122 high_output
&= ~nSRST
;
4125 /* initialize high byte for jtag */
4126 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
4127 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4128 return ERROR_JTAG_INIT_FAILED
;
4134 static void ktlink_reset(int trst
, int srst
)
4136 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4139 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4140 high_output
&= ~nTRSTnOE
;
4142 high_output
&= ~nTRST
;
4143 } else if (trst
== 0) {
4144 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4145 high_output
|= nTRSTnOE
;
4147 high_output
|= nTRST
;
4151 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4152 high_output
&= ~nSRST
;
4154 high_output
&= ~nSRSTnOE
;
4155 } else if (srst
== 0) {
4156 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4157 high_output
|= nSRST
;
4159 high_output
|= nSRSTnOE
;
4162 buffer_write(0x82); /* command "set data bits high byte" */
4163 buffer_write(high_output
);
4164 buffer_write(high_direction
);
4165 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4172 static void ktlink_blink(void)
4174 /* LED connected to ACBUS7 */
4175 high_output
^= 0x80;
4177 buffer_write(0x82); /* command "set data bits high byte" */
4178 buffer_write(high_output
);
4179 buffer_write(high_direction
);
4182 static const struct command_registration ft2232_command_handlers
[] = {
4184 .name
= "ft2232_device_desc",
4185 .handler
= &ft2232_handle_device_desc_command
,
4186 .mode
= COMMAND_CONFIG
,
4187 .help
= "set the USB device description of the FTDI FT2232 device",
4188 .usage
= "description_string",
4191 .name
= "ft2232_serial",
4192 .handler
= &ft2232_handle_serial_command
,
4193 .mode
= COMMAND_CONFIG
,
4194 .help
= "set the serial number of the FTDI FT2232 device",
4195 .usage
= "serial_string",
4198 .name
= "ft2232_layout",
4199 .handler
= &ft2232_handle_layout_command
,
4200 .mode
= COMMAND_CONFIG
,
4201 .help
= "set the layout of the FT2232 GPIO signals used "
4202 "to control output-enables and reset signals",
4203 .usage
= "layout_name",
4206 .name
= "ft2232_vid_pid",
4207 .handler
= &ft2232_handle_vid_pid_command
,
4208 .mode
= COMMAND_CONFIG
,
4209 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4210 .usage
= "(vid pid)* ",
4213 .name
= "ft2232_latency",
4214 .handler
= &ft2232_handle_latency_command
,
4215 .mode
= COMMAND_CONFIG
,
4216 .help
= "set the FT2232 latency timer to a new value",
4219 COMMAND_REGISTRATION_DONE
4222 struct jtag_interface ft2232_interface
= {
4224 .supported
= DEBUG_CAP_TMS_SEQ
,
4225 .commands
= ft2232_command_handlers
,
4226 .transports
= jtag_only
,
4228 .init
= ft2232_init
,
4229 .quit
= ft2232_quit
,
4230 .speed
= ft2232_speed
,
4231 .speed_div
= ft2232_speed_div
,
4233 .execute_queue
= ft2232_execute_queue
,