1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods. Since JTAG uses
36 * only one of the two ports on these devices, on integrated boards the
37 * second port often serves as a USB-to-serial adapter for the target's
38 * console UART even when the JTAG port is not in use. (Systems which
39 * support ARM's SWD in addition to JTAG, or instead of it, may use that
40 * second port for reading SWV trace data.)
42 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
43 * request/response interactions involve round trips over the USB link.
44 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
45 * can for example poll quickly for a status change (usually taking on the
46 * order of microseconds not milliseconds) before beginning a queued
47 * transaction which require the previous one to have completed.
49 * There are dozens of adapters of this type, differing in details which
50 * this driver needs to understand. Those "layout" details are required
51 * as part of FT2232 driver configuration.
53 * This code uses information contained in the MPSSE specification which was
55 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
56 * Hereafter this is called the "MPSSE Spec".
58 * The datasheet for the ftdichip.com's FT2232D part is here:
59 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
61 * Also note the issue with code 0x4b (clock data to TMS) noted in
62 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
63 * which can affect longer JTAG state paths.
70 /* project specific includes */
71 #include <jtag/interface.h>
72 #include <helper/time_support.h>
80 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
81 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
82 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
83 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
86 /* FT2232 access library includes */
87 #if BUILD_FT2232_FTD2XX == 1
89 #elif BUILD_FT2232_LIBFTDI == 1
93 /* max TCK for the high speed devices 30000 kHz */
94 #define FTDI_2232H_4232H_MAX_TCK 30000
95 /* max TCK for the full speed devices 6000 kHz */
96 #define FTDI_2232C_MAX_TCK 6000
97 /* this speed value tells that RTCK is requested */
101 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
102 * errors with a retry count of 100. Increasing it solves the problem for me.
105 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
106 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
109 #define LIBFTDI_READ_RETRY_COUNT 2000
111 #ifndef BUILD_FT2232_HIGHSPEED
112 #if BUILD_FT2232_FTD2XX == 1
113 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
114 #elif BUILD_FT2232_LIBFTDI == 1
115 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
120 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
121 * stable state. Calling code must ensure that current state is stable,
122 * that verification is not done in here.
124 * @param num_cycles The number of clocks cycles to send.
125 * @param cmd The command to send.
127 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
129 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
131 static char * ft2232_device_desc_A
= NULL
;
132 static char* ft2232_device_desc
= NULL
;
133 static char* ft2232_serial
= NULL
;
134 static char* ft2232_layout
= NULL
;
135 static uint8_t ft2232_latency
= 2;
136 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
138 #define MAX_USB_IDS 8
139 /* vid = pid = 0 marks the end of the list */
140 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
141 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
143 struct ft2232_layout
{
146 void (*reset
)(int trst
, int srst
);
151 /* init procedures for supported layouts */
152 static int usbjtag_init(void);
153 static int jtagkey_init(void);
154 static int olimex_jtag_init(void);
155 static int flyswatter_init(void);
156 static int turtle_init(void);
157 static int comstick_init(void);
158 static int stm32stick_init(void);
159 static int axm0432_jtag_init(void);
160 static int sheevaplug_init(void);
161 static int icebear_jtag_init(void);
162 static int cortino_jtag_init(void);
163 static int signalyzer_h_init(void);
164 static int ktlink_init(void);
165 static int redbee_init(void);
167 /* reset procedures for supported layouts */
168 static void usbjtag_reset(int trst
, int srst
);
169 static void jtagkey_reset(int trst
, int srst
);
170 static void olimex_jtag_reset(int trst
, int srst
);
171 static void flyswatter_reset(int trst
, int srst
);
172 static void turtle_reset(int trst
, int srst
);
173 static void comstick_reset(int trst
, int srst
);
174 static void stm32stick_reset(int trst
, int srst
);
175 static void axm0432_jtag_reset(int trst
, int srst
);
176 static void sheevaplug_reset(int trst
, int srst
);
177 static void icebear_jtag_reset(int trst
, int srst
);
178 static void signalyzer_h_reset(int trst
, int srst
);
179 static void ktlink_reset(int trst
, int srst
);
180 static void redbee_reset(int trst
, int srst
);
182 /* blink procedures for layouts that support a blinking led */
183 static void olimex_jtag_blink(void);
184 static void flyswatter_jtag_blink(void);
185 static void turtle_jtag_blink(void);
186 static void signalyzer_h_blink(void);
187 static void ktlink_blink(void);
189 static const struct ft2232_layout ft2232_layouts
[] =
192 .init
= usbjtag_init
,
193 .reset
= usbjtag_reset
,
196 .init
= jtagkey_init
,
197 .reset
= jtagkey_reset
,
199 { .name
= "jtagkey_prototype_v1",
200 .init
= jtagkey_init
,
201 .reset
= jtagkey_reset
,
203 { .name
= "oocdlink",
204 .init
= jtagkey_init
,
205 .reset
= jtagkey_reset
,
207 { .name
= "signalyzer",
208 .init
= usbjtag_init
,
209 .reset
= usbjtag_reset
,
211 { .name
= "evb_lm3s811",
212 .init
= usbjtag_init
,
213 .reset
= usbjtag_reset
,
215 { .name
= "luminary_icdi",
216 .init
= usbjtag_init
,
217 .reset
= usbjtag_reset
,
219 { .name
= "olimex-jtag",
220 .init
= olimex_jtag_init
,
221 .reset
= olimex_jtag_reset
,
222 .blink
= olimex_jtag_blink
224 { .name
= "flyswatter",
225 .init
= flyswatter_init
,
226 .reset
= flyswatter_reset
,
227 .blink
= flyswatter_jtag_blink
229 { .name
= "turtelizer2",
231 .reset
= turtle_reset
,
232 .blink
= turtle_jtag_blink
234 { .name
= "comstick",
235 .init
= comstick_init
,
236 .reset
= comstick_reset
,
238 { .name
= "stm32stick",
239 .init
= stm32stick_init
,
240 .reset
= stm32stick_reset
,
242 { .name
= "axm0432_jtag",
243 .init
= axm0432_jtag_init
,
244 .reset
= axm0432_jtag_reset
,
246 { .name
= "sheevaplug",
247 .init
= sheevaplug_init
,
248 .reset
= sheevaplug_reset
,
251 .init
= icebear_jtag_init
,
252 .reset
= icebear_jtag_reset
,
255 .init
= cortino_jtag_init
,
256 .reset
= comstick_reset
,
258 { .name
= "signalyzer-h",
259 .init
= signalyzer_h_init
,
260 .reset
= signalyzer_h_reset
,
261 .blink
= signalyzer_h_blink
265 .reset
= ktlink_reset
,
266 .blink
= ktlink_blink
268 { .name
= "redbee-econotag",
270 .reset
= redbee_reset
,
272 { .name
= NULL
, /* END OF TABLE */ },
275 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
277 static const struct ft2232_layout
*layout
;
278 static uint8_t low_output
= 0x0;
279 static uint8_t low_direction
= 0x0;
280 static uint8_t high_output
= 0x0;
281 static uint8_t high_direction
= 0x0;
283 #if BUILD_FT2232_FTD2XX == 1
284 static FT_HANDLE ftdih
= NULL
;
285 static FT_DEVICE ftdi_device
= 0;
286 #elif BUILD_FT2232_LIBFTDI == 1
287 static struct ftdi_context ftdic
;
288 static enum ftdi_chip_type ftdi_device
;
291 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
292 static int require_send
;
294 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
296 "There is a significant difference between libftdi and libftd2xx. The latter
297 one allows to schedule up to 64*64 bytes of result data while libftdi fails
298 with more than 4*64. As a consequence, the FT2232 driver is forced to
299 perform around 16x more USB transactions for long command streams with TDO
300 capture when running with libftdi."
303 #define FT2232_BUFFER_SIZE 131072
304 a comment would have been nice.
307 #define FT2232_BUFFER_SIZE 131072
309 static uint8_t* ft2232_buffer
= NULL
;
310 static int ft2232_buffer_size
= 0;
311 static int ft2232_read_pointer
= 0;
312 static int ft2232_expect_read
= 0;
315 * Function buffer_write
316 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
317 * @param val is the byte to send.
319 static inline void buffer_write(uint8_t val
)
321 assert(ft2232_buffer
);
322 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
323 ft2232_buffer
[ft2232_buffer_size
++] = val
;
327 * Function buffer_read
328 * returns a byte from the byte buffer.
330 static inline uint8_t buffer_read(void)
332 assert(ft2232_buffer
);
333 assert(ft2232_read_pointer
< ft2232_buffer_size
);
334 return ft2232_buffer
[ft2232_read_pointer
++];
338 * Clocks out \a bit_count bits on the TMS line, starting with the least
339 * significant bit of tms_bits and progressing to more significant bits.
340 * Rigorous state transition logging is done here via tap_set_state().
342 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
343 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
344 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
345 * is often used for this, 0x4b.
347 * @param tms_bits Holds the sequence of bits to send.
348 * @param tms_count Tells how many bits in the sequence.
349 * @param tdi_bit A single bit to pass on to TDI before the first TCK
350 * cycle and held static for the duration of TMS clocking.
352 * See the MPSSE spec referenced above.
354 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
358 int tms_ndx
; /* bit index into tms_byte */
360 assert(tms_count
> 0);
362 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
363 mpsse_cmd
, tms_bits
, tms_count
);
365 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
367 bool bit
= tms_bits
& 1;
370 tms_byte
|= (1 << tms_ndx
);
372 /* always do state transitions in public view */
373 tap_set_state(tap_state_transition(tap_get_state(), bit
));
375 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
380 if (tms_ndx
== 7 || i
== tms_count
-1)
382 buffer_write(mpsse_cmd
);
383 buffer_write(tms_ndx
- 1);
385 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
386 TMS/CS and is held static for the duration of TMS/CS clocking.
388 buffer_write(tms_byte
| (tdi_bit
<< 7));
394 * Function get_tms_buffer_requirements
395 * returns what clock_tms() will consume if called with
398 static inline int get_tms_buffer_requirements(int bit_count
)
400 return ((bit_count
+ 6)/7) * 3;
404 * Function move_to_state
405 * moves the TAP controller from the current state to a
406 * \a goal_state through a path given by tap_get_tms_path(). State transition
407 * logging is performed by delegation to clock_tms().
409 * @param goal_state is the destination state for the move.
411 static void move_to_state(tap_state_t goal_state
)
413 tap_state_t start_state
= tap_get_state();
415 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
416 lookup of the required TMS pattern to move to this state from the
420 /* do the 2 lookups */
421 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
422 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
424 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
426 clock_tms(0x4b, tms_bits
, tms_count
, 0);
429 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
431 #if BUILD_FT2232_FTD2XX == 1
433 DWORD dw_bytes_written
;
434 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
436 *bytes_written
= dw_bytes_written
;
437 LOG_ERROR("FT_Write returned: %lu", status
);
438 return ERROR_JTAG_DEVICE_ERROR
;
442 *bytes_written
= dw_bytes_written
;
445 #elif BUILD_FT2232_LIBFTDI == 1
447 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
450 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
451 return ERROR_JTAG_DEVICE_ERROR
;
455 *bytes_written
= retval
;
461 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
463 #if BUILD_FT2232_FTD2XX == 1
469 while ((*bytes_read
< size
) && timeout
--)
471 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
472 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
475 LOG_ERROR("FT_Read returned: %lu", status
);
476 return ERROR_JTAG_DEVICE_ERROR
;
478 *bytes_read
+= dw_bytes_read
;
481 #elif BUILD_FT2232_LIBFTDI == 1
483 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
486 while ((*bytes_read
< size
) && timeout
--)
488 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
491 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
492 return ERROR_JTAG_DEVICE_ERROR
;
494 *bytes_read
+= retval
;
499 if (*bytes_read
< size
)
501 LOG_ERROR("couldn't read enough bytes from "
502 "FT2232 device (%i < %i)",
503 (unsigned)*bytes_read
,
505 return ERROR_JTAG_DEVICE_ERROR
;
511 static bool ft2232_device_is_highspeed(void)
513 #if BUILD_FT2232_FTD2XX == 1
514 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
515 #elif BUILD_FT2232_LIBFTDI == 1
516 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
521 * Commands that only apply to the FT2232H and FT4232H devices.
522 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
523 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
526 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
528 uint8_t buf
= enable
? 0x96 : 0x97;
529 LOG_DEBUG("%2.2x", buf
);
531 uint32_t bytes_written
;
532 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
533 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
535 LOG_ERROR("couldn't write command to %s adaptive clocking"
536 , enable
? "enable" : "disable");
544 * Enable/disable the clk divide by 5 of the 60MHz master clock.
545 * This result in a JTAG clock speed range of 91.553Hz-6MHz
546 * respective 457.763Hz-30MHz.
548 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
550 uint32_t bytes_written
;
551 uint8_t buf
= enable
? 0x8b : 0x8a;
552 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
553 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
555 LOG_ERROR("couldn't write command to %s clk divide by 5"
556 , enable
? "enable" : "disable");
557 return ERROR_JTAG_INIT_FAILED
;
559 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
560 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
565 static int ft2232_speed(int speed
)
569 uint32_t bytes_written
;
572 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
573 if (ft2232_device_is_highspeed())
574 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
575 else if (enable_adaptive_clocking
)
577 LOG_ERROR("ft2232 device %lu does not support RTCK"
578 , (long unsigned int)ftdi_device
);
582 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
585 buf
[0] = 0x86; /* command "set divisor" */
586 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
587 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
589 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
590 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
592 LOG_ERROR("couldn't set FT2232 TCK speed");
599 static int ft2232_speed_div(int speed
, int* khz
)
601 /* Take a look in the FT2232 manual,
602 * AN2232C-01 Command Processor for
603 * MPSSE and MCU Host Bus. Chapter 3.8 */
605 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
610 static int ft2232_khz(int khz
, int* jtag_speed
)
614 if (ft2232_device_is_highspeed())
616 *jtag_speed
= RTCK_SPEED
;
621 LOG_DEBUG("RCLK not supported");
626 /* Take a look in the FT2232 manual,
627 * AN2232C-01 Command Processor for
628 * MPSSE and MCU Host Bus. Chapter 3.8
630 * We will calc here with a multiplier
631 * of 10 for better rounding later. */
633 /* Calc speed, (ft2232_max_tck / khz) - 1 */
634 /* Use 65000 for better rounding */
635 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
637 /* Add 0.9 for rounding */
640 /* Calc real speed */
641 *jtag_speed
= *jtag_speed
/ 10;
643 /* Check if speed is greater than 0 */
649 /* Check max value */
650 if (*jtag_speed
> 0xFFFF)
652 *jtag_speed
= 0xFFFF;
658 static void ft2232_end_state(tap_state_t state
)
660 if (tap_is_state_stable(state
))
661 tap_set_end_state(state
);
664 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
669 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
671 int num_bytes
= (scan_size
+ 7) / 8;
672 int bits_left
= scan_size
;
675 while (num_bytes
-- > 1)
677 buffer
[cur_byte
++] = buffer_read();
681 buffer
[cur_byte
] = 0x0;
683 /* There is one more partial byte left from the clock data in/out instructions */
686 buffer
[cur_byte
] = buffer_read() >> 1;
688 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
689 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
692 static void ft2232_debug_dump_buffer(void)
698 for (i
= 0; i
< ft2232_buffer_size
; i
++)
700 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
703 LOG_DEBUG("%s", line
);
709 LOG_DEBUG("%s", line
);
712 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
714 struct jtag_command
* cmd
;
719 uint32_t bytes_written
= 0;
720 uint32_t bytes_read
= 0;
722 #ifdef _DEBUG_USB_IO_
723 struct timeval start
, inter
, inter2
, end
;
724 struct timeval d_inter
, d_inter2
, d_end
;
727 #ifdef _DEBUG_USB_COMMS_
728 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
729 ft2232_debug_dump_buffer();
732 #ifdef _DEBUG_USB_IO_
733 gettimeofday(&start
, NULL
);
736 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
738 LOG_ERROR("couldn't write MPSSE commands to FT2232");
742 #ifdef _DEBUG_USB_IO_
743 gettimeofday(&inter
, NULL
);
746 if (ft2232_expect_read
)
748 /* FIXME this "timeout" is never changed ... */
749 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
750 ft2232_buffer_size
= 0;
752 #ifdef _DEBUG_USB_IO_
753 gettimeofday(&inter2
, NULL
);
756 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
758 LOG_ERROR("couldn't read from FT2232");
762 #ifdef _DEBUG_USB_IO_
763 gettimeofday(&end
, NULL
);
765 timeval_subtract(&d_inter
, &inter
, &start
);
766 timeval_subtract(&d_inter2
, &inter2
, &start
);
767 timeval_subtract(&d_end
, &end
, &start
);
769 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
770 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
771 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
772 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
775 ft2232_buffer_size
= bytes_read
;
777 if (ft2232_expect_read
!= ft2232_buffer_size
)
779 LOG_ERROR("ft2232_expect_read (%i) != "
780 "ft2232_buffer_size (%i) "
784 LIBFTDI_READ_RETRY_COUNT
- timeout
);
785 ft2232_debug_dump_buffer();
790 #ifdef _DEBUG_USB_COMMS_
791 LOG_DEBUG("read buffer (%i retries): %i bytes",
792 LIBFTDI_READ_RETRY_COUNT
- timeout
,
794 ft2232_debug_dump_buffer();
798 ft2232_expect_read
= 0;
799 ft2232_read_pointer
= 0;
801 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
802 * that wasn't handled by a caller-provided error handler
812 type
= jtag_scan_type(cmd
->cmd
.scan
);
813 if (type
!= SCAN_OUT
)
815 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
816 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
817 ft2232_read_scan(type
, buffer
, scan_size
);
818 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
819 retval
= ERROR_JTAG_QUEUE_FAILED
;
831 ft2232_buffer_size
= 0;
837 * Function ft2232_add_pathmove
838 * moves the TAP controller from the current state to a new state through the
839 * given path, where path is an array of tap_state_t's.
841 * @param path is an array of tap_stat_t which gives the states to traverse through
842 * ending with the last state at path[num_states-1]
843 * @param num_states is the count of state steps to move through
845 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
849 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
853 /* this loop verifies that the path is legal and logs each state in the path */
856 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
858 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
860 /* command "Clock Data to TMS/CS Pin (no Read)" */
863 /* number of states remaining */
864 buffer_write(num_states_batch
- 1);
866 while (num_states_batch
--) {
867 /* either TMS=0 or TMS=1 must work ... */
868 if (tap_state_transition(tap_get_state(), false)
869 == path
[state_count
])
870 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
871 else if (tap_state_transition(tap_get_state(), true)
872 == path
[state_count
])
873 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
875 /* ... or else the caller goofed BADLY */
877 LOG_ERROR("BUG: %s -> %s isn't a valid "
878 "TAP state transition",
879 tap_state_name(tap_get_state()),
880 tap_state_name(path
[state_count
]));
884 tap_set_state(path
[state_count
]);
889 buffer_write(tms_byte
);
891 tap_set_end_state(tap_get_state());
894 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
896 int num_bytes
= (scan_size
+ 7) / 8;
897 int bits_left
= scan_size
;
903 if (tap_get_state() != TAP_DRSHIFT
)
905 move_to_state(TAP_DRSHIFT
);
910 if (tap_get_state() != TAP_IRSHIFT
)
912 move_to_state(TAP_IRSHIFT
);
916 /* add command for complete bytes */
917 while (num_bytes
> 1)
922 /* Clock Data Bytes In and Out LSB First */
924 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
926 else if (type
== SCAN_OUT
)
928 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
930 /* LOG_DEBUG("added TDI bytes (o)"); */
932 else if (type
== SCAN_IN
)
934 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
936 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
939 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
940 num_bytes
-= thisrun_bytes
;
942 buffer_write((uint8_t) (thisrun_bytes
- 1));
943 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
947 /* add complete bytes */
948 while (thisrun_bytes
-- > 0)
950 buffer_write(buffer
[cur_byte
++]);
954 else /* (type == SCAN_IN) */
956 bits_left
-= 8 * (thisrun_bytes
);
960 /* the most signifcant bit is scanned during TAP movement */
962 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
966 /* process remaining bits but the last one */
971 /* Clock Data Bits In and Out LSB First */
973 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
975 else if (type
== SCAN_OUT
)
977 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
979 /* LOG_DEBUG("added TDI bits (o)"); */
981 else if (type
== SCAN_IN
)
983 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
985 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
988 buffer_write(bits_left
- 2);
990 buffer_write(buffer
[cur_byte
]);
993 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
994 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
998 /* Clock Data Bits In and Out LSB First */
1000 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1002 else if (type
== SCAN_OUT
)
1004 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1006 /* LOG_DEBUG("added TDI bits (o)"); */
1008 else if (type
== SCAN_IN
)
1010 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1012 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1015 buffer_write(last_bit
);
1023 /* move from Shift-IR/DR to end state */
1024 if (type
!= SCAN_OUT
)
1026 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1027 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1030 /* Clock Data to TMS/CS Pin with Read */
1035 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1036 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1037 /* Clock Data to TMS/CS Pin (no Read) */
1041 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1042 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1045 if (tap_get_state() != tap_get_end_state())
1047 move_to_state(tap_get_end_state());
1051 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1053 int num_bytes
= (scan_size
+ 7) / 8;
1054 int bits_left
= scan_size
;
1057 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1058 uint8_t* receive_pointer
= receive_buffer
;
1059 uint32_t bytes_written
;
1060 uint32_t bytes_read
;
1062 int thisrun_read
= 0;
1066 LOG_ERROR("BUG: large IR scans are not supported");
1070 if (tap_get_state() != TAP_DRSHIFT
)
1072 move_to_state(TAP_DRSHIFT
);
1075 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1077 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1080 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1081 ft2232_buffer_size
, (int)bytes_written
);
1082 ft2232_buffer_size
= 0;
1084 /* add command for complete bytes */
1085 while (num_bytes
> 1)
1089 if (type
== SCAN_IO
)
1091 /* Clock Data Bytes In and Out LSB First */
1093 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1095 else if (type
== SCAN_OUT
)
1097 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1099 /* LOG_DEBUG("added TDI bytes (o)"); */
1101 else if (type
== SCAN_IN
)
1103 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1105 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1108 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1109 thisrun_read
= thisrun_bytes
;
1110 num_bytes
-= thisrun_bytes
;
1111 buffer_write((uint8_t) (thisrun_bytes
- 1));
1112 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1114 if (type
!= SCAN_IN
)
1116 /* add complete bytes */
1117 while (thisrun_bytes
-- > 0)
1119 buffer_write(buffer
[cur_byte
]);
1124 else /* (type == SCAN_IN) */
1126 bits_left
-= 8 * (thisrun_bytes
);
1129 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1131 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1134 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1136 (int)bytes_written
);
1137 ft2232_buffer_size
= 0;
1139 if (type
!= SCAN_OUT
)
1141 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1143 LOG_ERROR("couldn't read from FT2232");
1146 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1149 receive_pointer
+= bytes_read
;
1155 /* the most signifcant bit is scanned during TAP movement */
1156 if (type
!= SCAN_IN
)
1157 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1161 /* process remaining bits but the last one */
1164 if (type
== SCAN_IO
)
1166 /* Clock Data Bits In and Out LSB First */
1168 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1170 else if (type
== SCAN_OUT
)
1172 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1174 /* LOG_DEBUG("added TDI bits (o)"); */
1176 else if (type
== SCAN_IN
)
1178 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1180 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1182 buffer_write(bits_left
- 2);
1183 if (type
!= SCAN_IN
)
1184 buffer_write(buffer
[cur_byte
]);
1186 if (type
!= SCAN_OUT
)
1190 if (tap_get_end_state() == TAP_DRSHIFT
)
1192 if (type
== SCAN_IO
)
1194 /* Clock Data Bits In and Out LSB First */
1196 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1198 else if (type
== SCAN_OUT
)
1200 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1202 /* LOG_DEBUG("added TDI bits (o)"); */
1204 else if (type
== SCAN_IN
)
1206 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1208 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1211 buffer_write(last_bit
);
1215 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1216 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1219 /* move from Shift-IR/DR to end state */
1220 if (type
!= SCAN_OUT
)
1222 /* Clock Data to TMS/CS Pin with Read */
1224 /* LOG_DEBUG("added TMS scan (read)"); */
1228 /* Clock Data to TMS/CS Pin (no Read) */
1230 /* LOG_DEBUG("added TMS scan (no read)"); */
1233 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1234 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1237 if (type
!= SCAN_OUT
)
1240 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1242 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1245 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1247 (int)bytes_written
);
1248 ft2232_buffer_size
= 0;
1250 if (type
!= SCAN_OUT
)
1252 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1254 LOG_ERROR("couldn't read from FT2232");
1257 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1260 receive_pointer
+= bytes_read
;
1266 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1268 int predicted_size
= 3;
1269 int num_bytes
= (scan_size
- 1) / 8;
1271 if (tap_get_state() != TAP_DRSHIFT
)
1272 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1274 if (type
== SCAN_IN
) /* only from device to host */
1276 /* complete bytes */
1277 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1279 /* remaining bits - 1 (up to 7) */
1280 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1282 else /* host to device, or bidirectional */
1284 /* complete bytes */
1285 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1287 /* remaining bits -1 (up to 7) */
1288 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1291 return predicted_size
;
1294 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1296 int predicted_size
= 0;
1298 if (type
!= SCAN_OUT
)
1300 /* complete bytes */
1301 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1303 /* remaining bits - 1 */
1304 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1306 /* last bit (from TMS scan) */
1307 predicted_size
+= 1;
1310 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1312 return predicted_size
;
1315 static void usbjtag_reset(int trst
, int srst
)
1317 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1320 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1321 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1323 low_output
&= ~nTRST
; /* switch output low */
1327 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1328 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1330 low_output
|= nTRST
; /* switch output high */
1335 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1336 low_output
&= ~nSRST
; /* switch output low */
1338 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1342 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1343 low_output
|= nSRST
; /* switch output high */
1345 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1348 /* command "set data bits low byte" */
1350 buffer_write(low_output
);
1351 buffer_write(low_direction
);
1354 static void jtagkey_reset(int trst
, int srst
)
1356 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1359 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1360 high_output
&= ~nTRSTnOE
;
1362 high_output
&= ~nTRST
;
1366 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1367 high_output
|= nTRSTnOE
;
1369 high_output
|= nTRST
;
1374 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1375 high_output
&= ~nSRST
;
1377 high_output
&= ~nSRSTnOE
;
1381 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1382 high_output
|= nSRST
;
1384 high_output
|= nSRSTnOE
;
1387 /* command "set data bits high byte" */
1389 buffer_write(high_output
);
1390 buffer_write(high_direction
);
1391 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1395 static void olimex_jtag_reset(int trst
, int srst
)
1397 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1400 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1401 high_output
&= ~nTRSTnOE
;
1403 high_output
&= ~nTRST
;
1407 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1408 high_output
|= nTRSTnOE
;
1410 high_output
|= nTRST
;
1415 high_output
|= nSRST
;
1419 high_output
&= ~nSRST
;
1422 /* command "set data bits high byte" */
1424 buffer_write(high_output
);
1425 buffer_write(high_direction
);
1426 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1430 static void axm0432_jtag_reset(int trst
, int srst
)
1434 tap_set_state(TAP_RESET
);
1435 high_output
&= ~nTRST
;
1439 high_output
|= nTRST
;
1444 high_output
&= ~nSRST
;
1448 high_output
|= nSRST
;
1451 /* command "set data bits low byte" */
1453 buffer_write(high_output
);
1454 buffer_write(high_direction
);
1455 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1459 static void flyswatter_reset(int trst
, int srst
)
1463 low_output
&= ~nTRST
;
1467 low_output
|= nTRST
;
1472 low_output
|= nSRST
;
1476 low_output
&= ~nSRST
;
1479 /* command "set data bits low byte" */
1481 buffer_write(low_output
);
1482 buffer_write(low_direction
);
1483 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1486 static void turtle_reset(int trst
, int srst
)
1492 low_output
|= nSRST
;
1496 low_output
&= ~nSRST
;
1499 /* command "set data bits low byte" */
1501 buffer_write(low_output
);
1502 buffer_write(low_direction
);
1503 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1506 static void comstick_reset(int trst
, int srst
)
1510 high_output
&= ~nTRST
;
1514 high_output
|= nTRST
;
1519 high_output
&= ~nSRST
;
1523 high_output
|= nSRST
;
1526 /* command "set data bits high byte" */
1528 buffer_write(high_output
);
1529 buffer_write(high_direction
);
1530 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1534 static void stm32stick_reset(int trst
, int srst
)
1538 high_output
&= ~nTRST
;
1542 high_output
|= nTRST
;
1547 low_output
&= ~nSRST
;
1551 low_output
|= nSRST
;
1554 /* command "set data bits low byte" */
1556 buffer_write(low_output
);
1557 buffer_write(low_direction
);
1559 /* command "set data bits high byte" */
1561 buffer_write(high_output
);
1562 buffer_write(high_direction
);
1563 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1567 static void sheevaplug_reset(int trst
, int srst
)
1570 high_output
&= ~nTRST
;
1572 high_output
|= nTRST
;
1575 high_output
&= ~nSRSTnOE
;
1577 high_output
|= nSRSTnOE
;
1579 /* command "set data bits high byte" */
1581 buffer_write(high_output
);
1582 buffer_write(high_direction
);
1583 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1586 static void redbee_reset(int trst
, int srst
)
1590 tap_set_state(TAP_RESET
);
1591 high_output
&= ~nTRST
;
1595 high_output
|= nTRST
;
1600 high_output
&= ~nSRST
;
1604 high_output
|= nSRST
;
1607 /* command "set data bits low byte" */
1609 buffer_write(high_output
);
1610 buffer_write(high_direction
);
1611 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1612 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1616 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1620 int predicted_size
= 0;
1623 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1624 cmd
->cmd
.runtest
->num_cycles
,
1625 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1627 /* only send the maximum buffer size that FT2232C can handle */
1629 if (tap_get_state() != TAP_IDLE
)
1630 predicted_size
+= 3;
1631 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1632 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1633 predicted_size
+= 3;
1634 if (tap_get_end_state() != TAP_IDLE
)
1635 predicted_size
+= 3;
1636 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1638 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1639 retval
= ERROR_JTAG_QUEUE_FAILED
;
1643 if (tap_get_state() != TAP_IDLE
)
1645 move_to_state(TAP_IDLE
);
1648 i
= cmd
->cmd
.runtest
->num_cycles
;
1651 /* there are no state transitions in this code, so omit state tracking */
1653 /* command "Clock Data to TMS/CS Pin (no Read)" */
1657 buffer_write((i
> 7) ? 6 : (i
- 1));
1662 i
-= (i
> 7) ? 7 : i
;
1663 /* LOG_DEBUG("added TMS scan (no read)"); */
1666 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1668 if (tap_get_state() != tap_get_end_state())
1670 move_to_state(tap_get_end_state());
1674 DEBUG_JTAG_IO("runtest: %i, end in %s",
1675 cmd
->cmd
.runtest
->num_cycles
,
1676 tap_state_name(tap_get_end_state()));
1680 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1682 int predicted_size
= 0;
1683 int retval
= ERROR_OK
;
1685 DEBUG_JTAG_IO("statemove end in %s",
1686 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1688 /* only send the maximum buffer size that FT2232C can handle */
1690 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1692 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1693 retval
= ERROR_JTAG_QUEUE_FAILED
;
1697 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1699 /* For TAP_RESET, ignore the current recorded state. It's often
1700 * wrong at server startup, and this transation is critical whenever
1703 if (tap_get_end_state() == TAP_RESET
) {
1704 clock_tms(0x4b, 0xff, 5, 0);
1707 /* shortest-path move to desired end state */
1708 } else if (tap_get_state() != tap_get_end_state())
1710 move_to_state(tap_get_end_state());
1718 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1719 * (or SWD) state machine.
1721 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1723 int retval
= ERROR_OK
;
1724 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1725 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1728 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1730 /* only send the maximum buffer size that FT2232C can handle */
1731 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1732 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1733 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1734 retval
= ERROR_JTAG_QUEUE_FAILED
;
1740 /* Shift out in batches of at most 6 bits; there's a report of an
1741 * FT2232 bug in this area, where shifting exactly 7 bits can make
1742 * problems with TMS signaling for the last clock cycle:
1744 * http://developer.intra2net.com/mailarchive/html/
1745 * libftdi/2009/msg00292.html
1747 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1749 * Note that pathmoves in JTAG are not often seven bits, so that
1750 * isn't a particularly likely situation outside of "special"
1751 * signaling such as switching between JTAG and SWD modes.
1754 if (num_bits
<= 6) {
1756 buffer_write(num_bits
- 1);
1757 buffer_write(*bits
& 0x3f);
1761 /* Yes, this is lazy ... we COULD shift out more data
1762 * bits per operation, but doing it in nybbles is easy
1766 buffer_write(*bits
& 0xf);
1769 count
= (num_bits
> 4) ? 4 : num_bits
;
1772 buffer_write(count
- 1);
1773 buffer_write((*bits
>> 4) & 0xf);
1783 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1785 int predicted_size
= 0;
1786 int retval
= ERROR_OK
;
1788 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1789 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1791 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1792 tap_state_name(tap_get_state()),
1793 tap_state_name(path
[num_states
-1]));
1795 /* only send the maximum buffer size that FT2232C can handle */
1796 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1797 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1799 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1800 retval
= ERROR_JTAG_QUEUE_FAILED
;
1806 ft2232_add_pathmove(path
, num_states
);
1812 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1815 int scan_size
; /* size of IR or DR scan */
1816 int predicted_size
= 0;
1817 int retval
= ERROR_OK
;
1819 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1821 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1823 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1825 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1826 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
;
1843 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1845 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1848 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1849 retval
= ERROR_JTAG_QUEUE_FAILED
;
1853 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1854 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1855 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1856 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1860 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1861 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1862 tap_state_name(tap_get_end_state()));
1867 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1870 int predicted_size
= 0;
1873 DEBUG_JTAG_IO("reset trst: %i srst %i",
1874 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1876 /* only send the maximum buffer size that FT2232C can handle */
1878 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1880 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1881 retval
= ERROR_JTAG_QUEUE_FAILED
;
1886 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1888 tap_set_state(TAP_RESET
);
1891 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1894 DEBUG_JTAG_IO("trst: %i, srst: %i",
1895 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1899 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1904 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1906 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1907 retval
= ERROR_JTAG_QUEUE_FAILED
;
1908 first_unsent
= cmd
->next
;
1909 jtag_sleep(cmd
->cmd
.sleep
->us
);
1910 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1912 tap_state_name(tap_get_state()));
1916 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1921 /* this is only allowed while in a stable state. A check for a stable
1922 * state was done in jtag_add_clocks()
1924 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1925 retval
= ERROR_JTAG_QUEUE_FAILED
;
1926 DEBUG_JTAG_IO("clocks %i while in %s",
1927 cmd
->cmd
.stableclocks
->num_cycles
,
1928 tap_state_name(tap_get_state()));
1932 static int ft2232_execute_command(struct jtag_command
*cmd
)
1938 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1939 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1940 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1941 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1942 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1943 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1944 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1946 retval
= ft2232_execute_tms(cmd
);
1949 LOG_ERROR("BUG: unknown JTAG command type encountered");
1950 retval
= ERROR_JTAG_QUEUE_FAILED
;
1956 static int ft2232_execute_queue(void)
1958 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
1961 first_unsent
= cmd
; /* next command that has to be sent */
1964 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1965 * that wasn't handled by a caller-provided error handler
1969 ft2232_buffer_size
= 0;
1970 ft2232_expect_read
= 0;
1972 /* blink, if the current layout has that feature */
1978 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1979 retval
= ERROR_JTAG_QUEUE_FAILED
;
1980 /* Start reading input before FT2232 TX buffer fills up */
1982 if (ft2232_expect_read
> 256)
1984 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1985 retval
= ERROR_JTAG_QUEUE_FAILED
;
1990 if (require_send
> 0)
1991 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1992 retval
= ERROR_JTAG_QUEUE_FAILED
;
1997 #if BUILD_FT2232_FTD2XX == 1
1998 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2002 char SerialNumber
[16];
2003 char Description
[64];
2004 DWORD openex_flags
= 0;
2005 char* openex_string
= NULL
;
2006 uint8_t latency_timer
;
2008 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
2011 /* Add non-standard Vid/Pid to the linux driver */
2012 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2014 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2018 if (ft2232_device_desc
&& ft2232_serial
)
2020 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2021 ft2232_device_desc
= NULL
;
2024 if (ft2232_device_desc
)
2026 openex_string
= ft2232_device_desc
;
2027 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2029 else if (ft2232_serial
)
2031 openex_string
= ft2232_serial
;
2032 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2036 LOG_ERROR("neither device description nor serial number specified");
2037 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2039 return ERROR_JTAG_INIT_FAILED
;
2042 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2043 if (status
!= FT_OK
) {
2044 /* under Win32, the FTD2XX driver appends an "A" to the end
2045 * of the description, if we tried by the desc, then
2046 * try by the alternate "A" description. */
2047 if (openex_string
== ft2232_device_desc
) {
2048 /* Try the alternate method. */
2049 openex_string
= ft2232_device_desc_A
;
2050 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2051 if (status
== FT_OK
) {
2052 /* yea, the "alternate" method worked! */
2054 /* drat, give the user a meaningfull message.
2055 * telling the use we tried *BOTH* methods. */
2056 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2058 ft2232_device_desc_A
);
2063 if (status
!= FT_OK
)
2069 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2071 return ERROR_JTAG_INIT_FAILED
;
2073 LOG_ERROR("unable to open ftdi device: %lu", status
);
2074 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2075 if (status
== FT_OK
)
2077 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2080 for (i
= 0; i
< num_devices
; i
++)
2081 desc_array
[i
] = malloc(64);
2083 desc_array
[num_devices
] = NULL
;
2085 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2087 if (status
== FT_OK
)
2089 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2090 for (i
= 0; i
< num_devices
; i
++)
2091 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2094 for (i
= 0; i
< num_devices
; i
++)
2095 free(desc_array
[i
]);
2101 LOG_ERROR("ListDevices: NONE\n");
2103 return ERROR_JTAG_INIT_FAILED
;
2106 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2108 LOG_ERROR("unable to set latency timer: %lu", status
);
2109 return ERROR_JTAG_INIT_FAILED
;
2112 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2114 LOG_ERROR("unable to get latency timer: %lu", status
);
2115 return ERROR_JTAG_INIT_FAILED
;
2119 LOG_DEBUG("current latency timer: %i", latency_timer
);
2122 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2124 LOG_ERROR("unable to set timeouts: %lu", status
);
2125 return ERROR_JTAG_INIT_FAILED
;
2128 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2130 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2131 return ERROR_JTAG_INIT_FAILED
;
2134 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2136 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2137 return ERROR_JTAG_INIT_FAILED
;
2141 static const char* type_str
[] =
2142 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2143 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2144 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2145 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2146 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2147 LOG_INFO("deviceID: %lu", deviceID
);
2148 LOG_INFO("SerialNumber: %s", SerialNumber
);
2149 LOG_INFO("Description: %s", Description
);
2155 static int ft2232_purge_ftd2xx(void)
2159 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2161 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2162 return ERROR_JTAG_INIT_FAILED
;
2168 #endif /* BUILD_FT2232_FTD2XX == 1 */
2170 #if BUILD_FT2232_LIBFTDI == 1
2171 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2173 uint8_t latency_timer
;
2175 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2176 ft2232_layout
, vid
, pid
);
2178 if (ftdi_init(&ftdic
) < 0)
2179 return ERROR_JTAG_INIT_FAILED
;
2181 /* default to INTERFACE_A */
2182 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2184 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2186 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2187 return ERROR_JTAG_INIT_FAILED
;
2190 /* context, vendor id, product id */
2191 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2195 LOG_WARNING("unable to open ftdi device (trying more): %s",
2198 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2200 return ERROR_JTAG_INIT_FAILED
;
2203 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2204 if (ftdi_usb_reset(&ftdic
) < 0)
2206 LOG_ERROR("unable to reset ftdi device");
2207 return ERROR_JTAG_INIT_FAILED
;
2210 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2212 LOG_ERROR("unable to set latency timer");
2213 return ERROR_JTAG_INIT_FAILED
;
2216 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2218 LOG_ERROR("unable to get latency timer");
2219 return ERROR_JTAG_INIT_FAILED
;
2223 LOG_DEBUG("current latency timer: %i", latency_timer
);
2226 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2228 ftdi_device
= ftdic
.type
;
2229 static const char* type_str
[] =
2230 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2231 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2232 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2233 ? ftdi_device
: no_of_known_types
;
2234 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2238 static int ft2232_purge_libftdi(void)
2240 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2242 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2243 return ERROR_JTAG_INIT_FAILED
;
2249 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2251 static int ft2232_init(void)
2255 uint32_t bytes_written
;
2256 const struct ft2232_layout
* cur_layout
= ft2232_layouts
;
2259 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2261 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2265 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2268 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2270 ft2232_layout
= "usbjtag";
2271 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2274 while (cur_layout
->name
)
2276 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2278 layout
= cur_layout
;
2286 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2287 return ERROR_JTAG_INIT_FAILED
;
2293 * "more indicates that there are more IDs to try, so we should
2294 * not print an error for an ID mismatch (but for anything
2297 * try_more indicates that the error code returned indicates an
2298 * ID mismatch (and nothing else) and that we should proceeed
2299 * with the next ID pair.
2301 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2304 #if BUILD_FT2232_FTD2XX == 1
2305 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2307 #elif BUILD_FT2232_LIBFTDI == 1
2308 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2309 more
, &try_more
, cur_layout
->channel
);
2313 if (!more
|| !try_more
)
2317 ft2232_buffer_size
= 0;
2318 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2320 if (layout
->init() != ERROR_OK
)
2321 return ERROR_JTAG_INIT_FAILED
;
2323 if (ft2232_device_is_highspeed())
2325 #ifndef BUILD_FT2232_HIGHSPEED
2326 #if BUILD_FT2232_FTD2XX == 1
2327 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2328 #elif BUILD_FT2232_LIBFTDI == 1
2329 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2332 /* make sure the legacy mode is disabled */
2333 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2334 return ERROR_JTAG_INIT_FAILED
;
2337 ft2232_speed(jtag_get_speed());
2339 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2340 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2342 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2343 return ERROR_JTAG_INIT_FAILED
;
2346 #if BUILD_FT2232_FTD2XX == 1
2347 return ft2232_purge_ftd2xx();
2348 #elif BUILD_FT2232_LIBFTDI == 1
2349 return ft2232_purge_libftdi();
2355 static int usbjtag_init(void)
2358 uint32_t bytes_written
;
2361 low_direction
= 0x0b;
2363 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2370 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2377 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2379 /* There are multiple revisions of LM3S811 eval boards:
2380 * - Rev B (and older?) boards have no SWO trace support.
2381 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2382 * they should use the "luminary_icdi" layout instead.
2389 low_direction
= 0x8b;
2391 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2393 /* Most Luminary eval boards support SWO trace output,
2394 * and should use this "luminary_icdi" layout.
2401 low_direction
= 0xcb;
2405 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2406 return ERROR_JTAG_INIT_FAILED
;
2409 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2410 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2412 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2413 low_output
&= ~nTRST
; /* nTRST = 0 */
2417 low_direction
|= nTRSTnOE
; /* nTRST output */
2418 low_output
|= nTRST
; /* nTRST = 1 */
2421 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2423 low_direction
|= nSRSTnOE
; /* nSRST output */
2424 low_output
|= nSRST
; /* nSRST = 1 */
2428 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2429 low_output
&= ~nSRST
; /* nSRST = 0 */
2432 /* initialize low byte for jtag */
2433 buf
[0] = 0x80; /* command "set data bits low byte" */
2434 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2435 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2436 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2438 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2440 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2441 return ERROR_JTAG_INIT_FAILED
;
2447 static int axm0432_jtag_init(void)
2450 uint32_t bytes_written
;
2453 low_direction
= 0x2b;
2455 /* initialize low byte for jtag */
2456 buf
[0] = 0x80; /* command "set data bits low byte" */
2457 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2458 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2459 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2461 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2463 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2464 return ERROR_JTAG_INIT_FAILED
;
2467 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2470 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2472 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2476 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2481 high_direction
= 0x0c;
2483 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2484 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2486 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2490 high_output
|= nTRST
;
2493 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2495 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2499 high_output
|= nSRST
;
2502 /* initialize high port */
2503 buf
[0] = 0x82; /* command "set data bits high byte" */
2504 buf
[1] = high_output
; /* value */
2505 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2506 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2508 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2510 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2511 return ERROR_JTAG_INIT_FAILED
;
2517 static int redbee_init(void)
2520 uint32_t bytes_written
;
2523 low_direction
= 0x2b;
2525 /* initialize low byte for jtag */
2526 /* command "set data bits low byte" */
2528 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2529 buf
[2] = low_direction
;
2530 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2531 buf
[1] = low_output
;
2532 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2534 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2535 || (bytes_written
!= 3))
2537 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2538 return ERROR_JTAG_INIT_FAILED
;
2542 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2544 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2547 high_direction
= 0x0c;
2549 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2550 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2552 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2556 high_output
|= nTRST
;
2559 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2561 LOG_ERROR("can't set nSRST to push-pull on redbee");
2565 high_output
|= nSRST
;
2568 /* initialize high port */
2569 buf
[0] = 0x82; /* command "set data bits high byte" */
2570 buf
[1] = high_output
; /* value */
2571 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2572 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2574 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2575 || (bytes_written
!= 3))
2577 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2578 return ERROR_JTAG_INIT_FAILED
;
2584 static int jtagkey_init(void)
2587 uint32_t bytes_written
;
2590 low_direction
= 0x1b;
2592 /* initialize low byte for jtag */
2593 buf
[0] = 0x80; /* command "set data bits low byte" */
2594 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2595 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2596 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2598 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2600 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2601 return ERROR_JTAG_INIT_FAILED
;
2604 if (strcmp(layout
->name
, "jtagkey") == 0)
2611 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2612 || (strcmp(layout
->name
, "oocdlink") == 0))
2621 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2626 high_direction
= 0x0f;
2628 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2629 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2631 high_output
|= nTRSTnOE
;
2632 high_output
&= ~nTRST
;
2636 high_output
&= ~nTRSTnOE
;
2637 high_output
|= nTRST
;
2640 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2642 high_output
&= ~nSRSTnOE
;
2643 high_output
|= nSRST
;
2647 high_output
|= nSRSTnOE
;
2648 high_output
&= ~nSRST
;
2651 /* initialize high port */
2652 buf
[0] = 0x82; /* command "set data bits high byte" */
2653 buf
[1] = high_output
; /* value */
2654 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2655 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2657 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2659 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2660 return ERROR_JTAG_INIT_FAILED
;
2666 static int olimex_jtag_init(void)
2669 uint32_t bytes_written
;
2672 low_direction
= 0x1b;
2674 /* initialize low byte for jtag */
2675 buf
[0] = 0x80; /* command "set data bits low byte" */
2676 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2677 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2678 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2680 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2682 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2683 return ERROR_JTAG_INIT_FAILED
;
2689 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2692 high_direction
= 0x0f;
2694 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2695 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2697 high_output
|= nTRSTnOE
;
2698 high_output
&= ~nTRST
;
2702 high_output
&= ~nTRSTnOE
;
2703 high_output
|= nTRST
;
2706 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2708 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2712 high_output
&= ~nSRST
;
2715 /* turn red LED on */
2716 high_output
|= 0x08;
2718 /* initialize high port */
2719 buf
[0] = 0x82; /* command "set data bits high byte" */
2720 buf
[1] = high_output
; /* value */
2721 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2722 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2724 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2726 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2727 return ERROR_JTAG_INIT_FAILED
;
2733 static int flyswatter_init(void)
2736 uint32_t bytes_written
;
2739 low_direction
= 0xfb;
2741 /* initialize low byte for jtag */
2742 buf
[0] = 0x80; /* command "set data bits low byte" */
2743 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2744 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2745 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2747 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2749 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2750 return ERROR_JTAG_INIT_FAILED
;
2754 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2756 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2759 high_direction
= 0x0c;
2761 /* turn red LED3 on, LED2 off */
2762 high_output
|= 0x08;
2764 /* initialize high port */
2765 buf
[0] = 0x82; /* command "set data bits high byte" */
2766 buf
[1] = high_output
; /* value */
2767 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2768 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2770 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2772 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2773 return ERROR_JTAG_INIT_FAILED
;
2779 static int turtle_init(void)
2782 uint32_t bytes_written
;
2785 low_direction
= 0x5b;
2787 /* initialize low byte for jtag */
2788 buf
[0] = 0x80; /* command "set data bits low byte" */
2789 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2790 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2791 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2793 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2795 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2796 return ERROR_JTAG_INIT_FAILED
;
2802 high_direction
= 0x0C;
2804 /* initialize high port */
2805 buf
[0] = 0x82; /* command "set data bits high byte" */
2806 buf
[1] = high_output
;
2807 buf
[2] = high_direction
;
2808 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2810 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2812 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2813 return ERROR_JTAG_INIT_FAILED
;
2819 static int comstick_init(void)
2822 uint32_t bytes_written
;
2825 low_direction
= 0x0b;
2827 /* initialize low byte for jtag */
2828 buf
[0] = 0x80; /* command "set data bits low byte" */
2829 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2830 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2831 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2833 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2835 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2836 return ERROR_JTAG_INIT_FAILED
;
2840 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2842 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2845 high_direction
= 0x03;
2847 /* initialize high port */
2848 buf
[0] = 0x82; /* command "set data bits high byte" */
2849 buf
[1] = high_output
;
2850 buf
[2] = high_direction
;
2851 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2853 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2855 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2856 return ERROR_JTAG_INIT_FAILED
;
2862 static int stm32stick_init(void)
2865 uint32_t bytes_written
;
2868 low_direction
= 0x8b;
2870 /* initialize low byte for jtag */
2871 buf
[0] = 0x80; /* command "set data bits low byte" */
2872 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2873 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2874 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2876 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2878 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2879 return ERROR_JTAG_INIT_FAILED
;
2883 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2885 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2888 high_direction
= 0x03;
2890 /* initialize high port */
2891 buf
[0] = 0x82; /* command "set data bits high byte" */
2892 buf
[1] = high_output
;
2893 buf
[2] = high_direction
;
2894 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2896 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2898 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2899 return ERROR_JTAG_INIT_FAILED
;
2905 static int sheevaplug_init(void)
2908 uint32_t bytes_written
;
2911 low_direction
= 0x1b;
2913 /* initialize low byte for jtag */
2914 buf
[0] = 0x80; /* command "set data bits low byte" */
2915 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2916 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2917 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2919 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2921 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2922 return ERROR_JTAG_INIT_FAILED
;
2931 high_direction
= 0x0f;
2933 /* nTRST is always push-pull */
2934 high_output
&= ~nTRSTnOE
;
2935 high_output
|= nTRST
;
2937 /* nSRST is always open-drain */
2938 high_output
|= nSRSTnOE
;
2939 high_output
&= ~nSRST
;
2941 /* initialize high port */
2942 buf
[0] = 0x82; /* command "set data bits high byte" */
2943 buf
[1] = high_output
; /* value */
2944 buf
[2] = high_direction
; /* all outputs - xRST */
2945 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2947 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2949 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2950 return ERROR_JTAG_INIT_FAILED
;
2956 static int cortino_jtag_init(void)
2959 uint32_t bytes_written
;
2962 low_direction
= 0x1b;
2964 /* initialize low byte for jtag */
2965 buf
[0] = 0x80; /* command "set data bits low byte" */
2966 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2967 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2968 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2970 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2972 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2973 return ERROR_JTAG_INIT_FAILED
;
2977 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2979 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2982 high_direction
= 0x03;
2984 /* initialize high port */
2985 buf
[0] = 0x82; /* command "set data bits high byte" */
2986 buf
[1] = high_output
;
2987 buf
[2] = high_direction
;
2988 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2990 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2992 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2993 return ERROR_JTAG_INIT_FAILED
;
2999 static void olimex_jtag_blink(void)
3001 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3002 * ACBUS3 is bit 3 of the GPIOH port
3004 if (high_output
& 0x08)
3006 /* set port pin high */
3007 high_output
&= 0x07;
3011 /* set port pin low */
3012 high_output
|= 0x08;
3016 buffer_write(high_output
);
3017 buffer_write(high_direction
);
3020 static void flyswatter_jtag_blink(void)
3023 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3025 high_output
^= 0x0c;
3028 buffer_write(high_output
);
3029 buffer_write(high_direction
);
3032 static void turtle_jtag_blink(void)
3035 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3037 if (high_output
& 0x08)
3047 buffer_write(high_output
);
3048 buffer_write(high_direction
);
3051 static int ft2232_quit(void)
3053 #if BUILD_FT2232_FTD2XX == 1
3056 status
= FT_Close(ftdih
);
3057 #elif BUILD_FT2232_LIBFTDI == 1
3058 ftdi_usb_close(&ftdic
);
3060 ftdi_deinit(&ftdic
);
3063 free(ft2232_buffer
);
3064 ft2232_buffer
= NULL
;
3069 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3075 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3076 cp
= strchr(ft2232_device_desc
, 0);
3077 /* under Win32, the FTD2XX driver appends an "A" to the end
3078 * of the description, this examines the given desc
3079 * and creates the 'missing' _A or non_A variable. */
3080 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3081 /* it was, so make this the "A" version. */
3082 ft2232_device_desc_A
= ft2232_device_desc
;
3083 /* and *CREATE* the non-A version. */
3084 strcpy(buf
, ft2232_device_desc
);
3085 cp
= strchr(buf
, 0);
3087 ft2232_device_desc
= strdup(buf
);
3089 /* <space > A not defined
3091 sprintf(buf
, "%s A", ft2232_device_desc
);
3092 ft2232_device_desc_A
= strdup(buf
);
3097 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3103 COMMAND_HANDLER(ft2232_handle_serial_command
)
3107 ft2232_serial
= strdup(CMD_ARGV
[0]);
3111 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3117 COMMAND_HANDLER(ft2232_handle_layout_command
)
3122 ft2232_layout
= malloc(strlen(CMD_ARGV
[0]) + 1);
3123 strcpy(ft2232_layout
, CMD_ARGV
[0]);
3128 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3130 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3132 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3133 "(maximum is %d pairs)", MAX_USB_IDS
);
3134 CMD_ARGC
= MAX_USB_IDS
* 2;
3136 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3138 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3140 return ERROR_COMMAND_SYNTAX_ERROR
;
3141 /* remove the incomplete trailing id */
3146 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3148 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3149 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3153 * Explicitly terminate, in case there are multiples instances of
3156 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3161 COMMAND_HANDLER(ft2232_handle_latency_command
)
3165 ft2232_latency
= atoi(CMD_ARGV
[0]);
3169 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3175 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3179 /* 7 bits of either ones or zeros. */
3180 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3182 while (num_cycles
> 0)
3184 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3185 * at most 7 bits per invocation. Here we invoke it potentially
3188 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3190 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3192 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3193 retval
= ERROR_JTAG_QUEUE_FAILED
;
3198 /* there are no state transitions in this code, so omit state tracking */
3200 /* command "Clock Data to TMS/CS Pin (no Read)" */
3204 buffer_write(bitcount_per_command
- 1);
3206 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3211 num_cycles
-= bitcount_per_command
;
3217 /* ---------------------------------------------------------------------
3218 * Support for IceBear JTAG adapter from Section5:
3219 * http://section5.ch/icebear
3221 * Author: Sten, debian@sansys-electronic.com
3224 /* Icebear pin layout
3226 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3227 * GND GND | 4 3| n.c.
3228 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3229 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3230 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3231 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3232 * ADBUS2 TDO |14 13| GND GND
3234 * ADBUS0 O L TCK ACBUS0 GND
3235 * ADBUS1 O L TDI ACBUS1 GND
3236 * ADBUS2 I TDO ACBUS2 n.c.
3237 * ADBUS3 O H TMS ACBUS3 n.c.
3243 static int icebear_jtag_init(void) {
3245 uint32_t bytes_written
;
3247 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3248 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3252 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3253 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3254 low_direction
&= ~nTRST
; /* nTRST high impedance */
3257 low_direction
|= nTRST
;
3258 low_output
|= nTRST
;
3261 low_direction
|= nSRST
;
3262 low_output
|= nSRST
;
3264 /* initialize low byte for jtag */
3265 buf
[0] = 0x80; /* command "set data bits low byte" */
3266 buf
[1] = low_output
;
3267 buf
[2] = low_direction
;
3268 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3270 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3271 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3272 return ERROR_JTAG_INIT_FAILED
;
3276 high_direction
= 0x00;
3279 /* initialize high port */
3280 buf
[0] = 0x82; /* command "set data bits high byte" */
3281 buf
[1] = high_output
; /* value */
3282 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3283 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3285 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3286 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3287 return ERROR_JTAG_INIT_FAILED
;
3293 static void icebear_jtag_reset(int trst
, int srst
) {
3296 low_direction
|= nTRST
;
3297 low_output
&= ~nTRST
;
3299 else if (trst
== 0) {
3300 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3301 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3302 low_direction
&= ~nTRST
;
3304 low_output
|= nTRST
;
3308 low_output
&= ~nSRST
;
3310 else if (srst
== 0) {
3311 low_output
|= nSRST
;
3314 /* command "set data bits low byte" */
3316 buffer_write(low_output
);
3317 buffer_write(low_direction
);
3319 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3322 /* ---------------------------------------------------------------------
3323 * Support for Signalyzer H2 and Signalyzer H4
3324 * JTAG adapter from Xverve Technologies Inc.
3325 * http://www.signalyzer.com or http://www.xverve.com
3327 * Author: Oleg Seiljus, oleg@signalyzer.com
3329 static unsigned char signalyzer_h_side
;
3330 static unsigned int signalyzer_h_adapter_type
;
3332 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3334 #if BUILD_FT2232_FTD2XX == 1
3335 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3338 #define SIGNALYZER_COMMAND_ADDR 128
3339 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3341 #define SIGNALYZER_COMMAND_VERSION 0x41
3342 #define SIGNALYZER_COMMAND_RESET 0x42
3343 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3344 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3345 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3346 #define SIGNALYZER_COMMAND_LED_SET 0x53
3347 #define SIGNALYZER_COMMAND_ADC 0x54
3348 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3349 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3350 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3351 #define SIGNALYZER_COMMAND_I2C 0x58
3353 #define SIGNALYZER_CHAN_A 1
3354 #define SIGNALYZER_CHAN_B 2
3355 /* LEDS use channel C */
3356 #define SIGNALYZER_CHAN_C 4
3358 #define SIGNALYZER_LED_GREEN 1
3359 #define SIGNALYZER_LED_RED 2
3361 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3362 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3363 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3364 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3365 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3368 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3370 #if BUILD_FT2232_FTD2XX == 1
3371 return FT_WriteEE(ftdih
, address
, value
);
3372 #elif BUILD_FT2232_LIBFTDI == 1
3377 #if BUILD_FT2232_FTD2XX == 1
3378 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3380 return FT_ReadEE(ftdih
, address
, value
);
3384 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3385 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3387 unsigned char on_time
;
3388 unsigned char off_time
;
3390 if (on_time_ms
< 0xFFFF)
3391 on_time
= (unsigned char)(on_time_ms
/ 62);
3395 off_time
= (unsigned char)(off_time_ms
/ 62);
3397 #if BUILD_FT2232_FTD2XX == 1
3400 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3401 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3403 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3404 return ERROR_JTAG_DEVICE_ERROR
;
3407 if ((status
= signalyzer_h_ctrl_write(
3408 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3409 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3411 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3412 return ERROR_JTAG_DEVICE_ERROR
;
3415 if ((status
= signalyzer_h_ctrl_write(
3416 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3417 ((uint32_t)cycles
))) != FT_OK
)
3419 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3420 return ERROR_JTAG_DEVICE_ERROR
;
3423 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3424 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3426 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3427 return ERROR_JTAG_DEVICE_ERROR
;
3431 #elif BUILD_FT2232_LIBFTDI == 1
3434 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3435 ((uint32_t)(channel
<< 8) | led
))) < 0)
3437 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3438 ftdi_get_error_string(&ftdic
));
3439 return ERROR_JTAG_DEVICE_ERROR
;
3442 if ((retval
= signalyzer_h_ctrl_write(
3443 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3444 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3446 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3447 ftdi_get_error_string(&ftdic
));
3448 return ERROR_JTAG_DEVICE_ERROR
;
3451 if ((retval
= signalyzer_h_ctrl_write(
3452 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3453 (uint32_t)cycles
)) < 0)
3455 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3456 ftdi_get_error_string(&ftdic
));
3457 return ERROR_JTAG_DEVICE_ERROR
;
3460 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3461 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3463 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3464 ftdi_get_error_string(&ftdic
));
3465 return ERROR_JTAG_DEVICE_ERROR
;
3472 static int signalyzer_h_init(void)
3474 #if BUILD_FT2232_FTD2XX == 1
3481 uint16_t read_buf
[12] = { 0 };
3483 uint32_t bytes_written
;
3485 /* turn on center green led */
3486 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3487 0xFFFF, 0x00, 0x00);
3489 /* determine what channel config wants to open
3490 * TODO: change me... current implementation is made to work
3491 * with openocd description parsing.
3493 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3497 signalyzer_h_side
= *(end_of_desc
- 1);
3498 if (signalyzer_h_side
== 'B')
3499 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3501 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3505 LOG_ERROR("No Channel was specified");
3509 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3512 #if BUILD_FT2232_FTD2XX == 1
3513 /* read signalyzer versionining information */
3514 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3515 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3517 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3518 return ERROR_JTAG_DEVICE_ERROR
;
3521 for (i
= 0; i
< 10; i
++)
3523 if ((status
= signalyzer_h_ctrl_read(
3524 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3525 &read_buf
[i
])) != FT_OK
)
3527 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3529 return ERROR_JTAG_DEVICE_ERROR
;
3533 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3534 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3535 read_buf
[4], read_buf
[5], read_buf
[6]);
3537 /* set gpio register */
3538 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3539 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3541 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3542 return ERROR_JTAG_DEVICE_ERROR
;
3545 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3548 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3549 return ERROR_JTAG_DEVICE_ERROR
;
3552 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3553 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3555 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3556 return ERROR_JTAG_DEVICE_ERROR
;
3559 /* read adapter type information */
3560 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3561 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3563 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3564 return ERROR_JTAG_DEVICE_ERROR
;
3567 if ((status
= signalyzer_h_ctrl_write(
3568 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3570 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3571 return ERROR_JTAG_DEVICE_ERROR
;
3574 if ((status
= signalyzer_h_ctrl_write(
3575 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3577 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3578 return ERROR_JTAG_DEVICE_ERROR
;
3581 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3582 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3584 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3585 return ERROR_JTAG_DEVICE_ERROR
;
3590 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3591 &read_buf
[0])) != FT_OK
)
3593 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3594 return ERROR_JTAG_DEVICE_ERROR
;
3597 if (read_buf
[0] != 0x0498)
3598 signalyzer_h_adapter_type
= 0x0000;
3601 for (i
= 0; i
< 4; i
++)
3603 if ((status
= signalyzer_h_ctrl_read(
3604 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3605 &read_buf
[i
])) != FT_OK
)
3607 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3609 return ERROR_JTAG_DEVICE_ERROR
;
3613 signalyzer_h_adapter_type
= read_buf
[0];
3616 #elif BUILD_FT2232_LIBFTDI == 1
3617 /* currently libftdi does not allow reading individual eeprom
3618 * locations, therefore adapter type cannot be detected.
3619 * override with most common type
3621 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3624 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3626 /* ADAPTOR: EM_LT16_A */
3627 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3629 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3630 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3638 low_direction
= 0x1b;
3641 high_direction
= 0x0;
3643 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3645 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3646 low_output
&= ~nTRST
; /* nTRST = 0 */
3650 low_direction
|= nTRSTnOE
; /* nTRST output */
3651 low_output
|= nTRST
; /* nTRST = 1 */
3654 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3656 low_direction
|= nSRSTnOE
; /* nSRST output */
3657 low_output
|= nSRST
; /* nSRST = 1 */
3661 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3662 low_output
&= ~nSRST
; /* nSRST = 0 */
3665 #if BUILD_FT2232_FTD2XX == 1
3666 /* enable power to the module */
3667 if ((status
= signalyzer_h_ctrl_write(
3668 SIGNALYZER_DATA_BUFFER_ADDR
,
3669 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3672 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3674 return ERROR_JTAG_DEVICE_ERROR
;
3677 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3678 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3680 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3682 return ERROR_JTAG_DEVICE_ERROR
;
3685 /* set gpio mode register */
3686 if ((status
= signalyzer_h_ctrl_write(
3687 SIGNALYZER_DATA_BUFFER_ADDR
,
3688 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3690 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3692 return ERROR_JTAG_DEVICE_ERROR
;
3695 if ((status
= signalyzer_h_ctrl_write(
3696 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3699 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3701 return ERROR_JTAG_DEVICE_ERROR
;
3704 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3705 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3707 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3709 return ERROR_JTAG_DEVICE_ERROR
;
3712 /* set gpio register */
3713 if ((status
= signalyzer_h_ctrl_write(
3714 SIGNALYZER_DATA_BUFFER_ADDR
,
3715 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3717 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3719 return ERROR_JTAG_DEVICE_ERROR
;
3722 if ((status
= signalyzer_h_ctrl_write(
3723 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3726 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3728 return ERROR_JTAG_DEVICE_ERROR
;
3731 if ((status
= signalyzer_h_ctrl_write(
3732 SIGNALYZER_COMMAND_ADDR
,
3733 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3735 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3737 return ERROR_JTAG_DEVICE_ERROR
;
3742 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3743 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3744 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3745 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3746 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3748 if (signalyzer_h_adapter_type
3749 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3750 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3751 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3752 else if (signalyzer_h_adapter_type
3753 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3754 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3755 "(ARM JTAG with PSU) detected. (HW: %2x).",
3756 (read_buf
[1] >> 8));
3757 else if (signalyzer_h_adapter_type
3758 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3759 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3760 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3761 else if (signalyzer_h_adapter_type
3762 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3763 LOG_INFO("Signalyzer: EM-JTAG-P "
3764 "(Generic JTAG with PSU) detected. (HW: %2x).",
3765 (read_buf
[1] >> 8));
3773 low_direction
= 0x1b;
3776 high_direction
= 0x1f;
3778 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3780 high_output
|= nTRSTnOE
;
3781 high_output
&= ~nTRST
;
3785 high_output
&= ~nTRSTnOE
;
3786 high_output
|= nTRST
;
3789 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3791 high_output
&= ~nSRSTnOE
;
3792 high_output
|= nSRST
;
3796 high_output
|= nSRSTnOE
;
3797 high_output
&= ~nSRST
;
3800 #if BUILD_FT2232_FTD2XX == 1
3801 /* enable power to the module */
3802 if ((status
= signalyzer_h_ctrl_write(
3803 SIGNALYZER_DATA_BUFFER_ADDR
,
3804 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3807 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3809 return ERROR_JTAG_DEVICE_ERROR
;
3812 if ((status
= signalyzer_h_ctrl_write(
3813 SIGNALYZER_COMMAND_ADDR
,
3814 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3816 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3818 return ERROR_JTAG_DEVICE_ERROR
;
3821 /* set gpio mode register (IO_16 and IO_17 set as analog
3822 * inputs, other is gpio)
3824 if ((status
= signalyzer_h_ctrl_write(
3825 SIGNALYZER_DATA_BUFFER_ADDR
,
3826 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3828 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3830 return ERROR_JTAG_DEVICE_ERROR
;
3833 if ((status
= signalyzer_h_ctrl_write(
3834 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3837 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3839 return ERROR_JTAG_DEVICE_ERROR
;
3842 if ((status
= signalyzer_h_ctrl_write(
3843 SIGNALYZER_COMMAND_ADDR
,
3844 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3846 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3848 return ERROR_JTAG_DEVICE_ERROR
;
3851 /* set gpio register (all inputs, for -P modules,
3852 * PSU will be turned off)
3854 if ((status
= signalyzer_h_ctrl_write(
3855 SIGNALYZER_DATA_BUFFER_ADDR
,
3856 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3858 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3860 return ERROR_JTAG_DEVICE_ERROR
;
3863 if ((status
= signalyzer_h_ctrl_write(
3864 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3867 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3869 return ERROR_JTAG_DEVICE_ERROR
;
3872 if ((status
= signalyzer_h_ctrl_write(
3873 SIGNALYZER_COMMAND_ADDR
,
3874 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3876 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3878 return ERROR_JTAG_DEVICE_ERROR
;
3883 else if (signalyzer_h_adapter_type
== 0x0000)
3885 LOG_INFO("Signalyzer: No external modules were detected.");
3893 low_direction
= 0x1b;
3896 high_direction
= 0x0;
3898 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3900 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3901 low_output
&= ~nTRST
; /* nTRST = 0 */
3905 low_direction
|= nTRSTnOE
; /* nTRST output */
3906 low_output
|= nTRST
; /* nTRST = 1 */
3909 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3911 low_direction
|= nSRSTnOE
; /* nSRST output */
3912 low_output
|= nSRST
; /* nSRST = 1 */
3916 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3917 low_output
&= ~nSRST
; /* nSRST = 0 */
3922 LOG_ERROR("Unknown module type is detected: %.4x",
3923 signalyzer_h_adapter_type
);
3924 return ERROR_JTAG_DEVICE_ERROR
;
3927 /* initialize low byte of controller for jtag operation */
3929 buf
[1] = low_output
;
3930 buf
[2] = low_direction
;
3932 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3933 || (bytes_written
!= 3))
3935 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3936 return ERROR_JTAG_INIT_FAILED
;
3939 #if BUILD_FT2232_FTD2XX == 1
3940 if (ftdi_device
== FT_DEVICE_2232H
)
3942 /* initialize high byte of controller for jtag operation */
3944 buf
[1] = high_output
;
3945 buf
[2] = high_direction
;
3947 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3948 || (bytes_written
!= 3))
3950 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3951 return ERROR_JTAG_INIT_FAILED
;
3954 #elif BUILD_FT2232_LIBFTDI == 1
3955 if (ftdi_device
== TYPE_2232H
)
3957 /* initialize high byte of controller for jtag operation */
3959 buf
[1] = high_output
;
3960 buf
[2] = high_direction
;
3962 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3963 || (bytes_written
!= 3))
3965 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3966 return ERROR_JTAG_INIT_FAILED
;
3973 static void signalyzer_h_reset(int trst
, int srst
)
3975 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3977 /* ADAPTOR: EM_LT16_A */
3978 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3982 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3983 /* switch to output pin (output is low) */
3984 low_direction
|= nTRSTnOE
;
3986 /* switch output low */
3987 low_output
&= ~nTRST
;
3991 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3992 /* switch to input pin (high-Z + internal
3993 * and external pullup) */
3994 low_direction
&= ~nTRSTnOE
;
3996 /* switch output high */
3997 low_output
|= nTRST
;
4002 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4003 /* switch output low */
4004 low_output
&= ~nSRST
;
4006 /* switch to output pin (output is low) */
4007 low_direction
|= nSRSTnOE
;
4011 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4012 /* switch output high */
4013 low_output
|= nSRST
;
4015 /* switch to input pin (high-Z) */
4016 low_direction
&= ~nSRSTnOE
;
4019 /* command "set data bits low byte" */
4021 buffer_write(low_output
);
4022 buffer_write(low_direction
);
4023 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4024 "low_direction: 0x%2.2x",
4025 trst
, srst
, low_output
, low_direction
);
4027 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4028 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4029 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4030 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4031 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4035 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4036 high_output
&= ~nTRSTnOE
;
4038 high_output
&= ~nTRST
;
4042 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4043 high_output
|= nTRSTnOE
;
4045 high_output
|= nTRST
;
4050 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4051 high_output
&= ~nSRST
;
4053 high_output
&= ~nSRSTnOE
;
4057 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4058 high_output
|= nSRST
;
4060 high_output
|= nSRSTnOE
;
4063 /* command "set data bits high byte" */
4065 buffer_write(high_output
);
4066 buffer_write(high_direction
);
4067 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4068 "high_direction: 0x%2.2x",
4069 trst
, srst
, high_output
, high_direction
);
4071 else if (signalyzer_h_adapter_type
== 0x0000)
4075 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4076 /* switch to output pin (output is low) */
4077 low_direction
|= nTRSTnOE
;
4079 /* switch output low */
4080 low_output
&= ~nTRST
;
4084 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4085 /* switch to input pin (high-Z + internal
4086 * and external pullup) */
4087 low_direction
&= ~nTRSTnOE
;
4089 /* switch output high */
4090 low_output
|= nTRST
;
4095 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4096 /* switch output low */
4097 low_output
&= ~nSRST
;
4099 /* switch to output pin (output is low) */
4100 low_direction
|= nSRSTnOE
;
4104 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4105 /* switch output high */
4106 low_output
|= nSRST
;
4108 /* switch to input pin (high-Z) */
4109 low_direction
&= ~nSRSTnOE
;
4112 /* command "set data bits low byte" */
4114 buffer_write(low_output
);
4115 buffer_write(low_direction
);
4116 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4117 "low_direction: 0x%2.2x",
4118 trst
, srst
, low_output
, low_direction
);
4122 static void signalyzer_h_blink(void)
4124 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4127 /********************************************************************
4128 * Support for KT-LINK
4129 * JTAG adapter from KRISTECH
4130 * http://www.kristech.eu
4131 *******************************************************************/
4132 static int ktlink_init(void)
4135 uint32_t bytes_written
;
4136 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4138 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4139 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4141 // initialize low port
4142 buf
[0] = 0x80; // command "set data bits low byte"
4143 buf
[1] = low_output
;
4144 buf
[2] = low_direction
;
4145 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4147 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4149 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4150 return ERROR_JTAG_INIT_FAILED
;
4158 high_output
= 0x80; // turn LED on
4159 high_direction
= 0xFF; // all outputs
4161 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4163 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4164 high_output
|= nTRSTnOE
;
4165 high_output
&= ~nTRST
;
4167 high_output
&= ~nTRSTnOE
;
4168 high_output
|= nTRST
;
4171 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4172 high_output
&= ~nSRSTnOE
;
4173 high_output
|= nSRST
;
4175 high_output
|= nSRSTnOE
;
4176 high_output
&= ~nSRST
;
4179 // initialize high port
4180 buf
[0] = 0x82; // command "set data bits high byte"
4181 buf
[1] = high_output
; // value
4182 buf
[2] = high_direction
;
4183 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4185 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4187 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4188 return ERROR_JTAG_INIT_FAILED
;
4194 static void ktlink_reset(int trst
, int srst
)
4196 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4199 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4200 high_output
&= ~nTRSTnOE
;
4202 high_output
&= ~nTRST
;
4203 } else if (trst
== 0) {
4204 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4205 high_output
|= nTRSTnOE
;
4207 high_output
|= nTRST
;
4211 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4212 high_output
&= ~nSRST
;
4214 high_output
&= ~nSRSTnOE
;
4215 } else if (srst
== 0) {
4216 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4217 high_output
|= nSRST
;
4219 high_output
|= nSRSTnOE
;
4222 buffer_write(0x82); // command "set data bits high byte"
4223 buffer_write(high_output
);
4224 buffer_write(high_direction
);
4225 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4228 static void ktlink_blink(void)
4230 /* LED connected to ACBUS7 */
4231 if (high_output
& 0x80)
4232 high_output
&= 0x7F;
4234 high_output
|= 0x80;
4236 buffer_write(0x82); // command "set data bits high byte"
4237 buffer_write(high_output
);
4238 buffer_write(high_direction
);
4241 static const struct command_registration ft2232_command_handlers
[] = {
4243 .name
= "ft2232_device_desc",
4244 .handler
= &ft2232_handle_device_desc_command
,
4245 .mode
= COMMAND_CONFIG
,
4246 .help
= "set the USB device description of the FTDI FT2232 device",
4247 .usage
= "description_string",
4250 .name
= "ft2232_serial",
4251 .handler
= &ft2232_handle_serial_command
,
4252 .mode
= COMMAND_CONFIG
,
4253 .help
= "set the serial number of the FTDI FT2232 device",
4254 .usage
= "serial_string",
4257 .name
= "ft2232_layout",
4258 .handler
= &ft2232_handle_layout_command
,
4259 .mode
= COMMAND_CONFIG
,
4260 .help
= "set the layout of the FT2232 GPIO signals used "
4261 "to control output-enables and reset signals",
4262 .usage
= "layout_name",
4265 .name
= "ft2232_vid_pid",
4266 .handler
= &ft2232_handle_vid_pid_command
,
4267 .mode
= COMMAND_CONFIG
,
4268 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4269 .usage
= "(vid pid)* ",
4272 .name
= "ft2232_latency",
4273 .handler
= &ft2232_handle_latency_command
,
4274 .mode
= COMMAND_CONFIG
,
4275 .help
= "set the FT2232 latency timer to a new value",
4278 COMMAND_REGISTRATION_DONE
4281 struct jtag_interface ft2232_interface
= {
4283 .supported
= DEBUG_CAP_TMS_SEQ
,
4284 .commands
= ft2232_command_handlers
,
4286 .init
= ft2232_init
,
4287 .quit
= ft2232_quit
,
4288 .speed
= ft2232_speed
,
4289 .speed_div
= ft2232_speed_div
,
4291 .execute_queue
= ft2232_execute_queue
,