1 /***************************************************************************
2 * Copyright (C) 2009 by Oyvind Harboe *
3 * Oyvind 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 * Copyright (C) 2011-2012 Tomasz Boleslaw CEDRO *
15 * cederom@tlen.pl, http://www.tomek.cedro.info *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
35 * JTAG adapters based on the FT2232 full and high speed USB parts are
36 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
37 * are discrete, but development boards may integrate them as alternatives
38 * to more capable (and expensive) third party JTAG pods.
40 * JTAG uses only one of the two communications channels ("MPSSE engines")
41 * on these devices. Adapters based on FT4232 parts have four ports/channels
42 * (A/B/C/D), instead of just two (A/B).
44 * Especially on development boards integrating one of these chips (as
45 * opposed to discrete pods/dongles), the additional channels can be used
46 * for a variety of purposes, but OpenOCD only uses one channel at a time.
48 * - As a USB-to-serial adapter for the target's console UART ...
49 * which may be able to support ROM boot loaders that load initial
50 * firmware images to flash (or SRAM).
52 * - On systems which support ARM's SWD in addition to JTAG, or instead
53 * of it, that second port can be used for reading SWV/SWO trace data.
55 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
57 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
58 * request/response interactions involve round trips over the USB link.
59 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
60 * can for example poll quickly for a status change (usually taking on the
61 * order of microseconds not milliseconds) before beginning a queued
62 * transaction which require the previous one to have completed.
64 * There are dozens of adapters of this type, differing in details which
65 * this driver needs to understand. Those "layout" details are required
66 * as part of FT2232 driver configuration.
68 * This code uses information contained in the MPSSE specification which was
70 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
71 * Hereafter this is called the "MPSSE Spec".
73 * The datasheet for the ftdichip.com's FT2232D part is here:
74 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
76 * Also note the issue with code 0x4b (clock data to TMS) noted in
77 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
78 * which can affect longer JTAG state paths.
85 /* project specific includes */
86 #include <jtag/interface.h>
87 #include <transport/transport.h>
88 #include <helper/time_support.h>
96 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
97 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
98 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
99 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
102 /* FT2232 access library includes */
103 #if BUILD_FT2232_FTD2XX == 1
105 #include "ftd2xx_common.h"
107 enum ftdi_interface
{
115 #elif BUILD_FT2232_LIBFTDI == 1
119 /* max TCK for the high speed devices 30000 kHz */
120 #define FTDI_x232H_MAX_TCK 30000
121 /* max TCK for the full speed devices 6000 kHz */
122 #define FTDI_2232C_MAX_TCK 6000
123 /* this speed value tells that RTCK is requested */
124 #define RTCK_SPEED -1
127 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
128 * errors with a retry count of 100. Increasing it solves the problem for me.
131 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
132 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
135 #define LIBFTDI_READ_RETRY_COUNT 2000
137 #ifndef BUILD_FT2232_HIGHSPEED
138 #if BUILD_FT2232_FTD2XX == 1
139 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
, FT_DEVICE_232H
};
140 #elif BUILD_FT2232_LIBFTDI == 1
141 enum ftdi_chip_type
{ TYPE_2232H
= 4, TYPE_4232H
= 5, TYPE_232H
= 6 };
146 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
147 * stable state. Calling code must ensure that current state is stable,
148 * that verification is not done in here.
150 * @param num_cycles The number of clocks cycles to send.
151 * @param cmd The command to send.
153 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
155 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
*cmd
);
157 static char *ft2232_device_desc_A
;
158 static char *ft2232_device_desc
;
159 static char *ft2232_serial
;
160 static uint8_t ft2232_latency
= 2;
161 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
163 #define MAX_USB_IDS 8
164 /* vid = pid = 0 marks the end of the list */
165 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
166 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
168 /** This structure describes different layout of FT2232 based devices. */
169 struct ft2232_layout
{
172 /** Layout specific initialization routine. */
174 /** Layout specific reset routine. */
175 void (*reset
)(int trst
, int srst
);
176 /** Layout specific LED blink routine. */
178 /** Which FTDI channel does this layout use. */
180 /** This will forbid bitbanging selected port pins. */
184 /* init procedures for supported layouts */
185 static int usbjtag_init(void);
186 static int jtagkey_init(void);
187 static int lm3s811_jtag_init(void);
188 static int icdi_jtag_init(void);
189 static int olimex_jtag_init(void);
190 static int flyswatter1_init(void);
191 static int flyswatter2_init(void);
192 static int minimodule_init(void);
193 static int turtle_init(void);
194 static int comstick_init(void);
195 static int stm32stick_init(void);
196 static int axm0432_jtag_init(void);
197 static int sheevaplug_init(void);
198 static int icebear_jtag_init(void);
199 static int cortino_jtag_init(void);
200 static int signalyzer_init(void);
201 static int signalyzer_h_init(void);
202 static int ktlink_init(void);
203 static int redbee_init(void);
204 static int lisa_l_init(void);
205 static int flossjtag_init(void);
206 static int xds100v2_init(void);
207 static int digilent_hs1_init(void);
209 /* reset procedures for supported layouts */
210 static void ftx23_reset(int trst
, int srst
);
211 static void jtagkey_reset(int trst
, int srst
);
212 static void olimex_jtag_reset(int trst
, int srst
);
213 static void flyswatter1_reset(int trst
, int srst
);
214 static void flyswatter2_reset(int trst
, int srst
);
215 static void minimodule_reset(int trst
, int srst
);
216 static void turtle_reset(int trst
, int srst
);
217 static void comstick_reset(int trst
, int srst
);
218 static void stm32stick_reset(int trst
, int srst
);
219 static void axm0432_jtag_reset(int trst
, int srst
);
220 static void sheevaplug_reset(int trst
, int srst
);
221 static void icebear_jtag_reset(int trst
, int srst
);
222 static void signalyzer_h_reset(int trst
, int srst
);
223 static void ktlink_reset(int trst
, int srst
);
224 static void redbee_reset(int trst
, int srst
);
225 static void xds100v2_reset(int trst
, int srst
);
226 static void digilent_hs1_reset(int trst
, int srst
);
228 /* blink procedures for layouts that support a blinking led */
229 static void olimex_jtag_blink(void);
230 static void flyswatter1_jtag_blink(void);
231 static void flyswatter2_jtag_blink(void);
232 static void turtle_jtag_blink(void);
233 static void signalyzer_h_blink(void);
234 static void ktlink_blink(void);
235 static void lisa_l_blink(void);
236 static void flossjtag_blink(void);
238 /* common transport support options */
240 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
242 static const struct ft2232_layout ft2232_layouts
[] = {
244 .init
= usbjtag_init
,
245 .reset
= ftx23_reset
,
248 .init
= jtagkey_init
,
249 .reset
= jtagkey_reset
,
251 { .name
= "jtagkey_prototype_v1",
252 .init
= jtagkey_init
,
253 .reset
= jtagkey_reset
,
255 { .name
= "oocdlink",
256 .init
= jtagkey_init
,
257 .reset
= jtagkey_reset
,
259 { .name
= "signalyzer",
260 .init
= signalyzer_init
,
261 .reset
= ftx23_reset
,
263 { .name
= "evb_lm3s811",
264 .init
= lm3s811_jtag_init
,
265 .reset
= ftx23_reset
,
267 { .name
= "luminary_icdi",
268 .init
= icdi_jtag_init
,
269 .reset
= ftx23_reset
,
271 { .name
= "olimex-jtag",
272 .init
= olimex_jtag_init
,
273 .reset
= olimex_jtag_reset
,
274 .blink
= olimex_jtag_blink
276 { .name
= "flyswatter",
277 .init
= flyswatter1_init
,
278 .reset
= flyswatter1_reset
,
279 .blink
= flyswatter1_jtag_blink
281 { .name
= "flyswatter2",
282 .init
= flyswatter2_init
,
283 .reset
= flyswatter2_reset
,
284 .blink
= flyswatter2_jtag_blink
286 { .name
= "minimodule",
287 .init
= minimodule_init
,
288 .reset
= minimodule_reset
,
290 { .name
= "turtelizer2",
292 .reset
= turtle_reset
,
293 .blink
= turtle_jtag_blink
295 { .name
= "comstick",
296 .init
= comstick_init
,
297 .reset
= comstick_reset
,
299 { .name
= "stm32stick",
300 .init
= stm32stick_init
,
301 .reset
= stm32stick_reset
,
303 { .name
= "axm0432_jtag",
304 .init
= axm0432_jtag_init
,
305 .reset
= axm0432_jtag_reset
,
307 { .name
= "sheevaplug",
308 .init
= sheevaplug_init
,
309 .reset
= sheevaplug_reset
,
312 .init
= icebear_jtag_init
,
313 .reset
= icebear_jtag_reset
,
316 .init
= cortino_jtag_init
,
317 .reset
= comstick_reset
,
319 { .name
= "signalyzer-h",
320 .init
= signalyzer_h_init
,
321 .reset
= signalyzer_h_reset
,
322 .blink
= signalyzer_h_blink
326 .reset
= ktlink_reset
,
327 .blink
= ktlink_blink
329 { .name
= "redbee-econotag",
331 .reset
= redbee_reset
,
333 { .name
= "redbee-usb",
335 .reset
= redbee_reset
,
336 .channel
= INTERFACE_B
,
340 .reset
= ftx23_reset
,
341 .blink
= lisa_l_blink
,
342 .channel
= INTERFACE_B
,
344 { .name
= "flossjtag",
345 .init
= flossjtag_init
,
346 .reset
= ftx23_reset
,
347 .blink
= flossjtag_blink
,
349 { .name
= "xds100v2",
350 .init
= xds100v2_init
,
351 .reset
= xds100v2_reset
,
353 { .name
= "digilent-hs1",
354 .init
= digilent_hs1_init
,
355 .reset
= digilent_hs1_reset
,
356 .channel
= INTERFACE_A
,
358 { .name
= NULL
, /* END OF TABLE */ },
361 /* bitmask used to drive nTRST; usually a GPIOLx signal */
362 static uint8_t nTRST
;
363 static uint8_t nTRSTnOE
;
364 /* bitmask used to drive nSRST; usually a GPIOLx signal */
365 static uint8_t nSRST
;
366 static uint8_t nSRSTnOE
;
368 /** the layout being used with this debug session */
369 static const struct ft2232_layout
*layout
;
371 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
372 static uint8_t low_output
;
374 /* note that direction bit == 1 means that signal is an output */
376 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
377 static uint8_t low_direction
;
378 /** default value bitmask for CBUS GPIOH(0..4) */
379 static uint8_t high_output
;
380 /** default direction bitmask for CBUS GPIOH(0..4) */
381 static uint8_t high_direction
;
383 #if BUILD_FT2232_FTD2XX == 1
384 static FT_HANDLE ftdih
;
385 static FT_DEVICE ftdi_device
;
386 #elif BUILD_FT2232_LIBFTDI == 1
387 static struct ftdi_context ftdic
;
388 static enum ftdi_chip_type ftdi_device
;
391 static struct jtag_command
*first_unsent
; /* next command that has to be sent */
392 static int require_send
;
394 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
396 "There is a significant difference between libftdi and libftd2xx. The latter
397 one allows to schedule up to 64*64 bytes of result data while libftdi fails
398 with more than 4*64. As a consequence, the FT2232 driver is forced to
399 perform around 16x more USB transactions for long command streams with TDO
400 capture when running with libftdi."
403 #define FT2232_BUFFER_SIZE 131072
404 a comment would have been nice.
407 #if BUILD_FT2232_FTD2XX == 1
408 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
410 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
413 #define FT2232_BUFFER_SIZE 131072
415 static uint8_t *ft2232_buffer
;
416 static int ft2232_buffer_size
;
417 static int ft2232_read_pointer
;
418 static int ft2232_expect_read
;
421 * Function buffer_write
422 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
423 * @param val is the byte to send.
425 static inline void buffer_write(uint8_t val
)
427 assert(ft2232_buffer
);
428 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
429 ft2232_buffer
[ft2232_buffer_size
++] = val
;
433 * Function buffer_read
434 * returns a byte from the byte buffer.
436 static inline uint8_t buffer_read(void)
438 assert(ft2232_buffer
);
439 assert(ft2232_read_pointer
< ft2232_buffer_size
);
440 return ft2232_buffer
[ft2232_read_pointer
++];
444 * Clocks out \a bit_count bits on the TMS line, starting with the least
445 * significant bit of tms_bits and progressing to more significant bits.
446 * Rigorous state transition logging is done here via tap_set_state().
448 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
449 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
450 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
451 * is often used for this, 0x4b.
453 * @param tms_bits Holds the sequence of bits to send.
454 * @param tms_count Tells how many bits in the sequence.
455 * @param tdi_bit A single bit to pass on to TDI before the first TCK
456 * cycle and held static for the duration of TMS clocking.
458 * See the MPSSE spec referenced above.
460 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
464 int tms_ndx
; /* bit index into tms_byte */
466 assert(tms_count
> 0);
468 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
469 mpsse_cmd
, tms_bits
, tms_count
);
471 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>= 1) {
472 bool bit
= tms_bits
& 1;
475 tms_byte
|= (1 << tms_ndx
);
477 /* always do state transitions in public view */
478 tap_set_state(tap_state_transition(tap_get_state(), bit
));
480 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
485 if (tms_ndx
== 7 || i
== tms_count
-1) {
486 buffer_write(mpsse_cmd
);
487 buffer_write(tms_ndx
- 1);
489 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
490 * TMS/CS and is held static for the duration of TMS/CS clocking.
492 buffer_write(tms_byte
| (tdi_bit
<< 7));
498 * Function get_tms_buffer_requirements
499 * returns what clock_tms() will consume if called with
502 static inline int get_tms_buffer_requirements(int bit_count
)
504 return ((bit_count
+ 6)/7) * 3;
508 * Function move_to_state
509 * moves the TAP controller from the current state to a
510 * \a goal_state through a path given by tap_get_tms_path(). State transition
511 * logging is performed by delegation to clock_tms().
513 * @param goal_state is the destination state for the move.
515 static void move_to_state(tap_state_t goal_state
)
517 tap_state_t start_state
= tap_get_state();
519 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
520 * lookup of the required TMS pattern to move to this state from the start state.
523 /* do the 2 lookups */
524 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
525 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
527 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
529 clock_tms(0x4b, tms_bits
, tms_count
, 0);
532 static int ft2232_write(uint8_t *buf
, int size
, uint32_t *bytes_written
)
534 #if BUILD_FT2232_FTD2XX == 1
536 DWORD dw_bytes_written
= 0;
537 status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
);
538 if (status
!= FT_OK
) {
539 *bytes_written
= dw_bytes_written
;
540 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status
));
541 return ERROR_JTAG_DEVICE_ERROR
;
543 *bytes_written
= dw_bytes_written
;
545 #elif BUILD_FT2232_LIBFTDI == 1
546 int retval
= ftdi_write_data(&ftdic
, buf
, size
);
549 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
550 return ERROR_JTAG_DEVICE_ERROR
;
552 *bytes_written
= retval
;
556 if (*bytes_written
!= (uint32_t)size
)
557 return ERROR_JTAG_DEVICE_ERROR
;
562 static int ft2232_read(uint8_t *buf
, uint32_t size
, uint32_t *bytes_read
)
564 #if BUILD_FT2232_FTD2XX == 1
570 while ((*bytes_read
< size
) && timeout
--) {
571 status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
572 *bytes_read
, &dw_bytes_read
);
573 if (status
!= FT_OK
) {
575 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status
));
576 return ERROR_JTAG_DEVICE_ERROR
;
578 *bytes_read
+= dw_bytes_read
;
581 #elif BUILD_FT2232_LIBFTDI == 1
583 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
586 while ((*bytes_read
< size
) && timeout
--) {
587 retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
);
590 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
591 return ERROR_JTAG_DEVICE_ERROR
;
593 *bytes_read
+= retval
;
598 if (*bytes_read
< size
) {
599 LOG_ERROR("couldn't read enough bytes from "
600 "FT2232 device (%i < %i)",
601 (unsigned)*bytes_read
,
603 return ERROR_JTAG_DEVICE_ERROR
;
609 static bool ft2232_device_is_highspeed(void)
611 #if BUILD_FT2232_FTD2XX == 1
612 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
)
613 #ifdef HAS_ENUM_FT232H
614 || (ftdi_device
== FT_DEVICE_232H
)
617 #elif BUILD_FT2232_LIBFTDI == 1
618 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
619 #ifdef HAS_ENUM_FT232H
620 || ftdi_device
== TYPE_232H
627 * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
628 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
629 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
632 static int ftx232h_adaptive_clocking(bool enable
)
634 uint8_t buf
= enable
? 0x96 : 0x97;
635 LOG_DEBUG("%2.2x", buf
);
637 uint32_t bytes_written
;
640 retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
);
641 if (retval
!= ERROR_OK
) {
642 LOG_ERROR("couldn't write command to %s adaptive clocking"
643 , enable
? "enable" : "disable");
651 * Enable/disable the clk divide by 5 of the 60MHz master clock.
652 * This result in a JTAG clock speed range of 91.553Hz-6MHz
653 * respective 457.763Hz-30MHz.
655 static int ftx232h_clk_divide_by_5(bool enable
)
657 uint32_t bytes_written
;
658 uint8_t buf
= enable
? 0x8b : 0x8a;
660 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
661 LOG_ERROR("couldn't write command to %s clk divide by 5"
662 , enable
? "enable" : "disable");
663 return ERROR_JTAG_INIT_FAILED
;
665 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_x232H_MAX_TCK
;
666 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
671 static int ft2232_speed(int speed
)
675 uint32_t bytes_written
;
678 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
679 if (ft2232_device_is_highspeed())
680 retval
= ftx232h_adaptive_clocking(enable_adaptive_clocking
);
681 else if (enable_adaptive_clocking
) {
682 LOG_ERROR("ft2232 device %lu does not support RTCK"
683 , (long unsigned int)ftdi_device
);
687 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
690 buf
[0] = 0x86; /* command "set divisor" */
691 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
692 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
694 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
695 retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
);
696 if (retval
!= ERROR_OK
) {
697 LOG_ERROR("couldn't set FT2232 TCK speed");
704 static int ft2232_speed_div(int speed
, int *khz
)
706 /* Take a look in the FT2232 manual,
707 * AN2232C-01 Command Processor for
708 * MPSSE and MCU Host Bus. Chapter 3.8 */
710 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
715 static int ft2232_khz(int khz
, int *jtag_speed
)
718 if (ft2232_device_is_highspeed()) {
719 *jtag_speed
= RTCK_SPEED
;
722 LOG_DEBUG("RCLK not supported");
727 /* Take a look in the FT2232 manual,
728 * AN2232C-01 Command Processor for
729 * MPSSE and MCU Host Bus. Chapter 3.8
731 * We will calc here with a multiplier
732 * of 10 for better rounding later. */
734 /* Calc speed, (ft2232_max_tck / khz) - 1
735 * Use 65000 for better rounding */
736 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
738 /* Add 0.9 for rounding */
741 /* Calc real speed */
742 *jtag_speed
= *jtag_speed
/ 10;
744 /* Check if speed is greater than 0 */
748 /* Check max value */
749 if (*jtag_speed
> 0xFFFF)
750 *jtag_speed
= 0xFFFF;
755 static void ft2232_end_state(tap_state_t state
)
757 if (tap_is_state_stable(state
))
758 tap_set_end_state(state
);
760 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
765 static void ft2232_read_scan(enum scan_type type
, uint8_t *buffer
, int scan_size
)
767 int num_bytes
= (scan_size
+ 7) / 8;
768 int bits_left
= scan_size
;
771 while (num_bytes
-- > 1) {
772 buffer
[cur_byte
++] = buffer_read();
776 buffer
[cur_byte
] = 0x0;
778 /* There is one more partial byte left from the clock data in/out instructions */
780 buffer
[cur_byte
] = buffer_read() >> 1;
781 /* This shift depends on the length of the
782 *clock data to tms instruction, insterted
783 *at end of the scan, now fixed to a two
784 *step transition in ft2232_add_scan */
785 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
788 static void ft2232_debug_dump_buffer(void)
794 for (i
= 0; i
< ft2232_buffer_size
; i
++) {
795 line_p
+= snprintf(line_p
,
796 sizeof(line
) - (line_p
- line
),
800 LOG_DEBUG("%s", line
);
806 LOG_DEBUG("%s", line
);
809 static int ft2232_send_and_recv(struct jtag_command
*first
, struct jtag_command
*last
)
811 struct jtag_command
*cmd
;
816 uint32_t bytes_written
= 0;
817 uint32_t bytes_read
= 0;
819 #ifdef _DEBUG_USB_IO_
820 struct timeval start
, inter
, inter2
, end
;
821 struct timeval d_inter
, d_inter2
, d_end
;
824 #ifdef _DEBUG_USB_COMMS_
825 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
826 ft2232_debug_dump_buffer();
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&start
, NULL
);
833 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
834 if (retval
!= ERROR_OK
) {
835 LOG_ERROR("couldn't write MPSSE commands to FT2232");
839 #ifdef _DEBUG_USB_IO_
840 gettimeofday(&inter
, NULL
);
843 if (ft2232_expect_read
) {
844 /* FIXME this "timeout" is never changed ... */
845 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
846 ft2232_buffer_size
= 0;
848 #ifdef _DEBUG_USB_IO_
849 gettimeofday(&inter2
, NULL
);
852 retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
);
853 if (retval
!= ERROR_OK
) {
854 LOG_ERROR("couldn't read from FT2232");
858 #ifdef _DEBUG_USB_IO_
859 gettimeofday(&end
, NULL
);
861 timeval_subtract(&d_inter
, &inter
, &start
);
862 timeval_subtract(&d_inter2
, &inter2
, &start
);
863 timeval_subtract(&d_end
, &end
, &start
);
865 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
866 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
867 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
868 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
871 ft2232_buffer_size
= bytes_read
;
873 if (ft2232_expect_read
!= ft2232_buffer_size
) {
874 LOG_ERROR("ft2232_expect_read (%i) != "
875 "ft2232_buffer_size (%i) "
879 LIBFTDI_READ_RETRY_COUNT
- timeout
);
880 ft2232_debug_dump_buffer();
885 #ifdef _DEBUG_USB_COMMS_
886 LOG_DEBUG("read buffer (%i retries): %i bytes",
887 LIBFTDI_READ_RETRY_COUNT
- timeout
,
889 ft2232_debug_dump_buffer();
893 ft2232_expect_read
= 0;
894 ft2232_read_pointer
= 0;
896 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
897 * that wasn't handled by a caller-provided error handler
902 while (cmd
!= last
) {
905 type
= jtag_scan_type(cmd
->cmd
.scan
);
906 if (type
!= SCAN_OUT
) {
907 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
908 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
909 ft2232_read_scan(type
, buffer
, scan_size
);
910 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
911 retval
= ERROR_JTAG_QUEUE_FAILED
;
923 ft2232_buffer_size
= 0;
929 * Function ft2232_add_pathmove
930 * moves the TAP controller from the current state to a new state through the
931 * given path, where path is an array of tap_state_t's.
933 * @param path is an array of tap_stat_t which gives the states to traverse through
934 * ending with the last state at path[num_states-1]
935 * @param num_states is the count of state steps to move through
937 static void ft2232_add_pathmove(tap_state_t
*path
, int num_states
)
941 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
945 /* this loop verifies that the path is legal and logs each state in the path */
947 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
949 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
951 /* command "Clock Data to TMS/CS Pin (no Read)" */
954 /* number of states remaining */
955 buffer_write(num_states_batch
- 1);
957 while (num_states_batch
--) {
958 /* either TMS=0 or TMS=1 must work ... */
959 if (tap_state_transition(tap_get_state(), false) == path
[state_count
])
960 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
961 else if (tap_state_transition(tap_get_state(), true) == path
[state_count
])
962 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
964 /* ... or else the caller goofed BADLY */
966 LOG_ERROR("BUG: %s -> %s isn't a valid "
967 "TAP state transition",
968 tap_state_name(tap_get_state()),
969 tap_state_name(path
[state_count
]));
973 tap_set_state(path
[state_count
]);
978 buffer_write(tms_byte
);
980 tap_set_end_state(tap_get_state());
983 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
)
985 int num_bytes
= (scan_size
+ 7) / 8;
986 int bits_left
= scan_size
;
991 if (tap_get_state() != TAP_DRSHIFT
)
992 move_to_state(TAP_DRSHIFT
);
994 if (tap_get_state() != TAP_IRSHIFT
)
995 move_to_state(TAP_IRSHIFT
);
998 /* add command for complete bytes */
999 while (num_bytes
> 1) {
1001 if (type
== SCAN_IO
) {
1002 /* Clock Data Bytes In and Out LSB First */
1004 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1005 } else if (type
== SCAN_OUT
) {
1006 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1008 /* LOG_DEBUG("added TDI bytes (o)"); */
1009 } else if (type
== SCAN_IN
) {
1010 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1012 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1015 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1016 num_bytes
-= thisrun_bytes
;
1018 buffer_write((uint8_t) (thisrun_bytes
- 1));
1019 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1021 if (type
!= SCAN_IN
) {
1022 /* add complete bytes */
1023 while (thisrun_bytes
-- > 0) {
1024 buffer_write(buffer
[cur_byte
++]);
1027 } else /* (type == SCAN_IN) */
1028 bits_left
-= 8 * (thisrun_bytes
);
1031 /* the most signifcant bit is scanned during TAP movement */
1032 if (type
!= SCAN_IN
)
1033 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1037 /* process remaining bits but the last one */
1038 if (bits_left
> 1) {
1039 if (type
== SCAN_IO
) {
1040 /* Clock Data Bits In and Out LSB First */
1042 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1043 } else if (type
== SCAN_OUT
) {
1044 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1046 /* LOG_DEBUG("added TDI bits (o)"); */
1047 } else if (type
== SCAN_IN
) {
1048 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1050 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1053 buffer_write(bits_left
- 2);
1054 if (type
!= SCAN_IN
)
1055 buffer_write(buffer
[cur_byte
]);
1058 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1059 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
))) {
1060 if (type
== SCAN_IO
) {
1061 /* Clock Data Bits In and Out LSB First */
1063 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1064 } else if (type
== SCAN_OUT
) {
1065 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1067 /* LOG_DEBUG("added TDI bits (o)"); */
1068 } else if (type
== SCAN_IN
) {
1069 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1071 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1074 buffer_write(last_bit
);
1080 /* move from Shift-IR/DR to end state */
1081 if (type
!= SCAN_OUT
) {
1082 /* We always go to the PAUSE state in two step at the end of an IN or IO
1084 * This must be coordinated with the bit shifts in ft2232_read_scan */
1087 /* Clock Data to TMS/CS Pin with Read */
1090 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1091 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1092 /* Clock Data to TMS/CS Pin (no Read) */
1096 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1097 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1100 if (tap_get_state() != tap_get_end_state())
1101 move_to_state(tap_get_end_state());
1104 static int ft2232_large_scan(struct scan_command
*cmd
,
1105 enum scan_type type
,
1109 int num_bytes
= (scan_size
+ 7) / 8;
1110 int bits_left
= scan_size
;
1113 uint8_t *receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1114 uint8_t *receive_pointer
= receive_buffer
;
1115 uint32_t bytes_written
;
1116 uint32_t bytes_read
;
1118 int thisrun_read
= 0;
1121 LOG_ERROR("BUG: large IR scans are not supported");
1125 if (tap_get_state() != TAP_DRSHIFT
)
1126 move_to_state(TAP_DRSHIFT
);
1128 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
1129 if (retval
!= ERROR_OK
) {
1130 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1133 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1134 ft2232_buffer_size
, (int)bytes_written
);
1135 ft2232_buffer_size
= 0;
1137 /* add command for complete bytes */
1138 while (num_bytes
> 1) {
1141 if (type
== SCAN_IO
) {
1142 /* Clock Data Bytes In and Out LSB First */
1144 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1145 } else if (type
== SCAN_OUT
) {
1146 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1148 /* LOG_DEBUG("added TDI bytes (o)"); */
1149 } else if (type
== SCAN_IN
) {
1150 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1152 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1155 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1156 thisrun_read
= thisrun_bytes
;
1157 num_bytes
-= thisrun_bytes
;
1158 buffer_write((uint8_t) (thisrun_bytes
- 1));
1159 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1161 if (type
!= SCAN_IN
) {
1162 /* add complete bytes */
1163 while (thisrun_bytes
-- > 0) {
1164 buffer_write(buffer
[cur_byte
]);
1168 } else /* (type == SCAN_IN) */
1169 bits_left
-= 8 * (thisrun_bytes
);
1171 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
1172 if (retval
!= ERROR_OK
) {
1173 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1176 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1178 (int)bytes_written
);
1179 ft2232_buffer_size
= 0;
1181 if (type
!= SCAN_OUT
) {
1182 retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
);
1183 if (retval
!= ERROR_OK
) {
1184 LOG_ERROR("couldn't read from FT2232");
1187 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1190 receive_pointer
+= bytes_read
;
1196 /* the most signifcant bit is scanned during TAP movement */
1197 if (type
!= SCAN_IN
)
1198 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1202 /* process remaining bits but the last one */
1203 if (bits_left
> 1) {
1204 if (type
== SCAN_IO
) {
1205 /* Clock Data Bits In and Out LSB First */
1207 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1208 } else if (type
== SCAN_OUT
) {
1209 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1211 /* LOG_DEBUG("added TDI bits (o)"); */
1212 } else if (type
== SCAN_IN
) {
1213 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1215 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1217 buffer_write(bits_left
- 2);
1218 if (type
!= SCAN_IN
)
1219 buffer_write(buffer
[cur_byte
]);
1221 if (type
!= SCAN_OUT
)
1225 if (tap_get_end_state() == TAP_DRSHIFT
) {
1226 if (type
== SCAN_IO
) {
1227 /* Clock Data Bits In and Out LSB First */
1229 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1230 } else if (type
== SCAN_OUT
) {
1231 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1233 /* LOG_DEBUG("added TDI bits (o)"); */
1234 } else if (type
== SCAN_IN
) {
1235 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1237 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1240 buffer_write(last_bit
);
1242 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1243 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1246 /* move from Shift-IR/DR to end state */
1247 if (type
!= SCAN_OUT
) {
1248 /* Clock Data to TMS/CS Pin with Read */
1250 /* LOG_DEBUG("added TMS scan (read)"); */
1252 /* Clock Data to TMS/CS Pin (no Read) */
1254 /* LOG_DEBUG("added TMS scan (no read)"); */
1257 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1258 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1261 if (type
!= SCAN_OUT
)
1264 retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
);
1265 if (retval
!= ERROR_OK
) {
1266 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1269 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1271 (int)bytes_written
);
1272 ft2232_buffer_size
= 0;
1274 if (type
!= SCAN_OUT
) {
1275 retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
);
1276 if (retval
!= ERROR_OK
) {
1277 LOG_ERROR("couldn't read from FT2232");
1280 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1288 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1290 int predicted_size
= 3;
1291 int num_bytes
= (scan_size
- 1) / 8;
1293 if (tap_get_state() != TAP_DRSHIFT
)
1294 predicted_size
+= get_tms_buffer_requirements(
1295 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1297 if (type
== SCAN_IN
) { /* only from device to host */
1298 /* complete bytes */
1299 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1301 /* remaining bits - 1 (up to 7) */
1302 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1303 } else {/* host to device, or bidirectional
1305 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1307 /* remaining bits -1 (up to 7) */
1308 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1311 return predicted_size
;
1314 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1316 int predicted_size
= 0;
1318 if (type
!= SCAN_OUT
) {
1319 /* complete bytes */
1321 (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1323 /* remaining bits - 1 */
1324 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1326 /* last bit (from TMS scan) */
1327 predicted_size
+= 1;
1330 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1332 return predicted_size
;
1335 /* semi-generic FT2232/FT4232 reset code */
1336 static void ftx23_reset(int trst
, int srst
)
1338 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1340 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1341 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1343 low_output
&= ~nTRST
; /* switch output low */
1344 } else if (trst
== 0) {
1345 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1346 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal
1347 *and external pullup) */
1349 low_output
|= nTRST
; /* switch output high */
1353 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1354 low_output
&= ~nSRST
; /* switch output low */
1356 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1357 } else if (srst
== 0) {
1358 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1359 low_output
|= nSRST
; /* switch output high */
1361 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1364 /* command "set data bits low byte" */
1366 buffer_write(low_output
);
1367 buffer_write(low_direction
);
1370 static void jtagkey_reset(int trst
, int srst
)
1372 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1374 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1375 high_output
&= ~nTRSTnOE
;
1377 high_output
&= ~nTRST
;
1378 } else if (trst
== 0) {
1379 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1380 high_output
|= nTRSTnOE
;
1382 high_output
|= nTRST
;
1386 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1387 high_output
&= ~nSRST
;
1389 high_output
&= ~nSRSTnOE
;
1390 } else if (srst
== 0) {
1391 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1392 high_output
|= nSRST
;
1394 high_output
|= nSRSTnOE
;
1397 /* command "set data bits high byte" */
1399 buffer_write(high_output
);
1400 buffer_write(high_direction
);
1401 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1408 static void olimex_jtag_reset(int trst
, int srst
)
1410 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1412 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1413 high_output
&= ~nTRSTnOE
;
1415 high_output
&= ~nTRST
;
1416 } else if (trst
== 0) {
1417 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1418 high_output
|= nTRSTnOE
;
1420 high_output
|= nTRST
;
1424 high_output
|= nSRST
;
1426 high_output
&= ~nSRST
;
1428 /* command "set data bits high byte" */
1430 buffer_write(high_output
);
1431 buffer_write(high_direction
);
1432 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1439 static void axm0432_jtag_reset(int trst
, int srst
)
1442 tap_set_state(TAP_RESET
);
1443 high_output
&= ~nTRST
;
1444 } else if (trst
== 0)
1445 high_output
|= nTRST
;
1448 high_output
&= ~nSRST
;
1450 high_output
|= nSRST
;
1452 /* command "set data bits low byte" */
1454 buffer_write(high_output
);
1455 buffer_write(high_direction
);
1456 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1463 static void flyswatter_reset(int trst
, int srst
)
1466 low_output
&= ~nTRST
;
1468 low_output
|= nTRST
;
1471 low_output
|= nSRST
;
1473 low_output
&= ~nSRST
;
1475 /* command "set data bits low byte" */
1477 buffer_write(low_output
);
1478 buffer_write(low_direction
);
1479 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1486 static void flyswatter1_reset(int trst
, int srst
)
1488 flyswatter_reset(trst
, srst
);
1491 static void flyswatter2_reset(int trst
, int srst
)
1493 flyswatter_reset(trst
, !srst
);
1496 static void minimodule_reset(int trst
, int srst
)
1499 low_output
&= ~nSRST
;
1501 low_output
|= nSRST
;
1503 /* command "set data bits low byte" */
1505 buffer_write(low_output
);
1506 buffer_write(low_direction
);
1507 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1514 static void turtle_reset(int trst
, int srst
)
1519 low_output
|= nSRST
;
1521 low_output
&= ~nSRST
;
1523 /* command "set data bits low byte" */
1525 buffer_write(low_output
);
1526 buffer_write(low_direction
);
1527 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1533 static void comstick_reset(int trst
, int srst
)
1536 high_output
&= ~nTRST
;
1538 high_output
|= nTRST
;
1541 high_output
&= ~nSRST
;
1543 high_output
|= nSRST
;
1545 /* command "set data bits high byte" */
1547 buffer_write(high_output
);
1548 buffer_write(high_direction
);
1549 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1556 static void stm32stick_reset(int trst
, int srst
)
1559 high_output
&= ~nTRST
;
1561 high_output
|= nTRST
;
1564 low_output
&= ~nSRST
;
1566 low_output
|= nSRST
;
1568 /* command "set data bits low byte" */
1570 buffer_write(low_output
);
1571 buffer_write(low_direction
);
1573 /* command "set data bits high byte" */
1575 buffer_write(high_output
);
1576 buffer_write(high_direction
);
1577 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1584 static void sheevaplug_reset(int trst
, int srst
)
1587 high_output
&= ~nTRST
;
1589 high_output
|= nTRST
;
1592 high_output
&= ~nSRSTnOE
;
1594 high_output
|= nSRSTnOE
;
1596 /* command "set data bits high byte" */
1598 buffer_write(high_output
);
1599 buffer_write(high_direction
);
1600 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1607 static void redbee_reset(int trst
, int srst
)
1610 tap_set_state(TAP_RESET
);
1611 high_output
&= ~nTRST
;
1612 } else if (trst
== 0)
1613 high_output
|= nTRST
;
1616 high_output
&= ~nSRST
;
1618 high_output
|= nSRST
;
1620 /* command "set data bits low byte" */
1622 buffer_write(high_output
);
1623 buffer_write(high_direction
);
1624 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1625 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1629 static void xds100v2_reset(int trst
, int srst
)
1632 tap_set_state(TAP_RESET
);
1633 high_output
&= ~nTRST
;
1634 } else if (trst
== 0)
1635 high_output
|= nTRST
;
1638 high_output
|= nSRST
;
1640 high_output
&= ~nSRST
;
1642 /* command "set data bits low byte" */
1644 buffer_write(high_output
);
1645 buffer_write(high_direction
);
1646 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1647 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1651 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1655 int predicted_size
= 0;
1658 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1659 cmd
->cmd
.runtest
->num_cycles
,
1660 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1662 /* only send the maximum buffer size that FT2232C can handle */
1664 if (tap_get_state() != TAP_IDLE
)
1665 predicted_size
+= 3;
1666 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1667 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1668 predicted_size
+= 3;
1669 if (tap_get_end_state() != TAP_IDLE
)
1670 predicted_size
+= 3;
1671 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1672 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1673 retval
= ERROR_JTAG_QUEUE_FAILED
;
1677 if (tap_get_state() != TAP_IDLE
) {
1678 move_to_state(TAP_IDLE
);
1681 i
= cmd
->cmd
.runtest
->num_cycles
;
1683 /* there are no state transitions in this code, so omit state tracking */
1685 /* command "Clock Data to TMS/CS Pin (no Read)" */
1689 buffer_write((i
> 7) ? 6 : (i
- 1));
1694 i
-= (i
> 7) ? 7 : i
;
1695 /* LOG_DEBUG("added TMS scan (no read)"); */
1698 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1700 if (tap_get_state() != tap_get_end_state())
1701 move_to_state(tap_get_end_state());
1704 DEBUG_JTAG_IO("runtest: %i, end in %s",
1705 cmd
->cmd
.runtest
->num_cycles
,
1706 tap_state_name(tap_get_end_state()));
1710 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1712 int predicted_size
= 0;
1713 int retval
= ERROR_OK
;
1715 DEBUG_JTAG_IO("statemove end in %s",
1716 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1718 /* only send the maximum buffer size that FT2232C can handle */
1720 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1721 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1722 retval
= ERROR_JTAG_QUEUE_FAILED
;
1726 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1728 /* For TAP_RESET, ignore the current recorded state. It's often
1729 * wrong at server startup, and this transation is critical whenever
1732 if (tap_get_end_state() == TAP_RESET
) {
1733 clock_tms(0x4b, 0xff, 5, 0);
1736 /* shortest-path move to desired end state */
1737 } else if (tap_get_state() != tap_get_end_state()) {
1738 move_to_state(tap_get_end_state());
1746 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1747 * (or SWD) state machine.
1749 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1751 int retval
= ERROR_OK
;
1752 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1753 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1756 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1758 /* only send the maximum buffer size that FT2232C can handle */
1759 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1760 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1761 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1762 retval
= ERROR_JTAG_QUEUE_FAILED
;
1768 /* Shift out in batches of at most 6 bits; there's a report of an
1769 * FT2232 bug in this area, where shifting exactly 7 bits can make
1770 * problems with TMS signaling for the last clock cycle:
1772 * http://developer.intra2net.com/mailarchive/html/
1773 * libftdi/2009/msg00292.html
1775 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1777 * Note that pathmoves in JTAG are not often seven bits, so that
1778 * isn't a particularly likely situation outside of "special"
1779 * signaling such as switching between JTAG and SWD modes.
1782 if (num_bits
<= 6) {
1784 buffer_write(num_bits
- 1);
1785 buffer_write(*bits
& 0x3f);
1789 /* Yes, this is lazy ... we COULD shift out more data
1790 * bits per operation, but doing it in nybbles is easy
1794 buffer_write(*bits
& 0xf);
1797 count
= (num_bits
> 4) ? 4 : num_bits
;
1800 buffer_write(count
- 1);
1801 buffer_write((*bits
>> 4) & 0xf);
1811 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1813 int predicted_size
= 0;
1814 int retval
= ERROR_OK
;
1816 tap_state_t
*path
= cmd
->cmd
.pathmove
->path
;
1817 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1819 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1820 tap_state_name(tap_get_state()),
1821 tap_state_name(path
[num_states
-1]));
1823 /* only send the maximum buffer size that FT2232C can handle */
1824 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1825 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1826 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1827 retval
= ERROR_JTAG_QUEUE_FAILED
;
1833 ft2232_add_pathmove(path
, num_states
);
1839 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1842 int scan_size
; /* size of IR or DR scan */
1843 int predicted_size
= 0;
1844 int retval
= ERROR_OK
;
1846 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1848 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1850 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1852 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1853 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
) {
1854 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1855 /* unsent commands before this */
1856 if (first_unsent
!= cmd
)
1857 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1858 retval
= ERROR_JTAG_QUEUE_FAILED
;
1860 /* current command */
1861 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1862 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1864 first_unsent
= cmd
->next
;
1868 } else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1870 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1873 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1874 retval
= ERROR_JTAG_QUEUE_FAILED
;
1878 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1879 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1880 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1881 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1885 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1886 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1887 tap_state_name(tap_get_end_state()));
1892 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1895 int predicted_size
= 0;
1898 DEBUG_JTAG_IO("reset trst: %i srst %i",
1899 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1901 /* only send the maximum buffer size that FT2232C can handle */
1903 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
) {
1904 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1905 retval
= ERROR_JTAG_QUEUE_FAILED
;
1910 if ((cmd
->cmd
.reset
->trst
== 1) ||
1911 (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1912 tap_set_state(TAP_RESET
);
1914 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1917 DEBUG_JTAG_IO("trst: %i, srst: %i",
1918 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1922 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1927 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1929 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1930 retval
= ERROR_JTAG_QUEUE_FAILED
;
1931 first_unsent
= cmd
->next
;
1932 jtag_sleep(cmd
->cmd
.sleep
->us
);
1933 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1935 tap_state_name(tap_get_state()));
1939 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1944 /* this is only allowed while in a stable state. A check for a stable
1945 * state was done in jtag_add_clocks()
1947 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1948 retval
= ERROR_JTAG_QUEUE_FAILED
;
1949 DEBUG_JTAG_IO("clocks %i while in %s",
1950 cmd
->cmd
.stableclocks
->num_cycles
,
1951 tap_state_name(tap_get_state()));
1955 static int ft2232_execute_command(struct jtag_command
*cmd
)
1959 switch (cmd
->type
) {
1961 retval
= ft2232_execute_reset(cmd
);
1964 retval
= ft2232_execute_runtest(cmd
);
1966 case JTAG_TLR_RESET
:
1967 retval
= ft2232_execute_statemove(cmd
);
1970 retval
= ft2232_execute_pathmove(cmd
);
1973 retval
= ft2232_execute_scan(cmd
);
1976 retval
= ft2232_execute_sleep(cmd
);
1978 case JTAG_STABLECLOCKS
:
1979 retval
= ft2232_execute_stableclocks(cmd
);
1982 retval
= ft2232_execute_tms(cmd
);
1985 LOG_ERROR("BUG: unknown JTAG command type encountered");
1986 retval
= ERROR_JTAG_QUEUE_FAILED
;
1992 static int ft2232_execute_queue(void)
1994 struct jtag_command
*cmd
= jtag_command_queue
; /* currently processed command */
1997 first_unsent
= cmd
; /* next command that has to be sent */
2000 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2001 * that wasn't handled by a caller-provided error handler
2005 ft2232_buffer_size
= 0;
2006 ft2232_expect_read
= 0;
2008 /* blink, if the current layout has that feature */
2013 /* fill the write buffer with the desired command */
2014 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2015 retval
= ERROR_JTAG_QUEUE_FAILED
;
2016 /* Start reading input before FT2232 TX buffer fills up.
2017 * Sometimes this happens because we don't know the
2018 * length of the last command before we execute it. So
2019 * we simple inform the user.
2023 if (ft2232_expect_read
>= FT2232_BUFFER_READ_QUEUE_SIZE
) {
2024 if (ft2232_expect_read
> (FT2232_BUFFER_READ_QUEUE_SIZE
+1))
2025 LOG_DEBUG("read buffer size looks too high %d/%d",
2027 (FT2232_BUFFER_READ_QUEUE_SIZE
+1));
2028 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2029 retval
= ERROR_JTAG_QUEUE_FAILED
;
2034 if (require_send
> 0)
2035 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2036 retval
= ERROR_JTAG_QUEUE_FAILED
;
2041 #if BUILD_FT2232_FTD2XX == 1
2042 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int *try_more
)
2046 char SerialNumber
[16];
2047 char Description
[64];
2048 DWORD openex_flags
= 0;
2049 char *openex_string
= NULL
;
2050 uint8_t latency_timer
;
2052 if (layout
== NULL
) {
2053 LOG_WARNING("No ft2232 layout specified'");
2054 return ERROR_JTAG_INIT_FAILED
;
2057 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2058 layout
->name
, vid
, pid
);
2061 /* Add non-standard Vid/Pid to the linux driver */
2062 status
= FT_SetVIDPID(vid
, pid
);
2063 if (status
!= FT_OK
)
2064 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2068 if (ft2232_device_desc
&& ft2232_serial
) {
2070 "can't open by device description and serial number, giving precedence to serial");
2071 ft2232_device_desc
= NULL
;
2074 if (ft2232_device_desc
) {
2075 openex_string
= ft2232_device_desc
;
2076 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2077 } else if (ft2232_serial
) {
2078 openex_string
= ft2232_serial
;
2079 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2081 LOG_ERROR("neither device description nor serial number specified");
2083 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2085 return ERROR_JTAG_INIT_FAILED
;
2088 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2089 if (status
!= FT_OK
) {
2090 /* under Win32, the FTD2XX driver appends an "A" to the end
2091 * of the description, if we tried by the desc, then
2092 * try by the alternate "A" description. */
2093 if (openex_string
== ft2232_device_desc
) {
2094 /* Try the alternate method. */
2095 openex_string
= ft2232_device_desc_A
;
2096 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2097 if (status
== FT_OK
) {
2098 /* yea, the "alternate" method worked! */
2100 /* drat, give the user a meaningfull message.
2101 * telling the use we tried *BOTH* methods. */
2102 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2104 ft2232_device_desc_A
);
2109 if (status
!= FT_OK
) {
2113 LOG_WARNING("unable to open ftdi device (trying more): %s",
2114 ftd2xx_status_string(status
));
2116 return ERROR_JTAG_INIT_FAILED
;
2118 LOG_ERROR("unable to open ftdi device: %s",
2119 ftd2xx_status_string(status
));
2120 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2121 if (status
== FT_OK
) {
2122 char **desc_array
= malloc(sizeof(char *) * (num_devices
+ 1));
2125 for (i
= 0; i
< num_devices
; i
++)
2126 desc_array
[i
] = malloc(64);
2128 desc_array
[num_devices
] = NULL
;
2130 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2132 if (status
== FT_OK
) {
2133 LOG_ERROR("ListDevices: %" PRIu32
, (uint32_t)num_devices
);
2134 for (i
= 0; i
< num_devices
; i
++)
2135 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2138 for (i
= 0; i
< num_devices
; i
++)
2139 free(desc_array
[i
]);
2143 LOG_ERROR("ListDevices: NONE");
2144 return ERROR_JTAG_INIT_FAILED
;
2147 status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
);
2148 if (status
!= FT_OK
) {
2149 LOG_ERROR("unable to set latency timer: %s",
2150 ftd2xx_status_string(status
));
2151 return ERROR_JTAG_INIT_FAILED
;
2154 status
= FT_GetLatencyTimer(ftdih
, &latency_timer
);
2155 if (status
!= FT_OK
) {
2156 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2157 * so ignore errors if using this driver version */
2160 status
= FT_GetDriverVersion(ftdih
, &dw_version
);
2161 LOG_ERROR("unable to get latency timer: %s",
2162 ftd2xx_status_string(status
));
2164 if ((status
== FT_OK
) && (dw_version
== 0x10004)) {
2165 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2166 "with FT_GetLatencyTimer, upgrade to a newer version");
2168 return ERROR_JTAG_INIT_FAILED
;
2170 LOG_DEBUG("current latency timer: %i", latency_timer
);
2172 status
= FT_SetTimeouts(ftdih
, 5000, 5000);
2173 if (status
!= FT_OK
) {
2174 LOG_ERROR("unable to set timeouts: %s",
2175 ftd2xx_status_string(status
));
2176 return ERROR_JTAG_INIT_FAILED
;
2179 status
= FT_SetBitMode(ftdih
, 0x0b, 2);
2180 if (status
!= FT_OK
) {
2181 LOG_ERROR("unable to enable bit i/o mode: %s",
2182 ftd2xx_status_string(status
));
2183 return ERROR_JTAG_INIT_FAILED
;
2186 status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
,
2187 SerialNumber
, Description
, NULL
);
2188 if (status
!= FT_OK
) {
2189 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2190 ftd2xx_status_string(status
));
2191 return ERROR_JTAG_INIT_FAILED
;
2193 static const char *type_str
[] = {
2194 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2196 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2197 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2198 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2199 LOG_INFO("device: %" PRIu32
" \"%s\"", (uint32_t)ftdi_device
, type_str
[type_index
]);
2200 LOG_INFO("deviceID: %" PRIu32
, (uint32_t)deviceID
);
2201 LOG_INFO("SerialNumber: %s", SerialNumber
);
2202 LOG_INFO("Description: %s", Description
);
2208 static int ft2232_purge_ftd2xx(void)
2212 status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
);
2213 if (status
!= FT_OK
) {
2214 LOG_ERROR("error purging ftd2xx device: %s",
2215 ftd2xx_status_string(status
));
2216 return ERROR_JTAG_INIT_FAILED
;
2222 #endif /* BUILD_FT2232_FTD2XX == 1 */
2224 #if BUILD_FT2232_LIBFTDI == 1
2225 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int *try_more
, int channel
)
2227 uint8_t latency_timer
;
2229 if (layout
== NULL
) {
2230 LOG_WARNING("No ft2232 layout specified'");
2231 return ERROR_JTAG_INIT_FAILED
;
2234 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2235 layout
->name
, vid
, pid
);
2237 if (ftdi_init(&ftdic
) < 0)
2238 return ERROR_JTAG_INIT_FAILED
;
2240 /* default to INTERFACE_A */
2241 if (channel
== INTERFACE_ANY
)
2242 channel
= INTERFACE_A
;
2243 if (ftdi_set_interface(&ftdic
, channel
) < 0) {
2244 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2245 return ERROR_JTAG_INIT_FAILED
;
2248 /* context, vendor id, product id */
2249 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
, ft2232_serial
) < 0) {
2251 LOG_WARNING("unable to open ftdi device (trying more): %s",
2254 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2256 return ERROR_JTAG_INIT_FAILED
;
2259 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2260 if (ftdi_usb_reset(&ftdic
) < 0) {
2261 LOG_ERROR("unable to reset ftdi device");
2262 return ERROR_JTAG_INIT_FAILED
;
2265 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0) {
2266 LOG_ERROR("unable to set latency timer");
2267 return ERROR_JTAG_INIT_FAILED
;
2270 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0) {
2271 LOG_ERROR("unable to get latency timer");
2272 return ERROR_JTAG_INIT_FAILED
;
2274 LOG_DEBUG("current latency timer: %i", latency_timer
);
2276 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2278 ftdi_device
= ftdic
.type
;
2279 static const char *type_str
[] = {
2280 "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "Unknown"
2282 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2283 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2284 ? ftdi_device
: no_of_known_types
;
2285 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2289 static int ft2232_purge_libftdi(void)
2291 if (ftdi_usb_purge_buffers(&ftdic
) < 0) {
2292 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2293 return ERROR_JTAG_INIT_FAILED
;
2299 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2301 static int ft2232_set_data_bits_low_byte(uint8_t value
, uint8_t direction
)
2304 uint32_t bytes_written
;
2306 buf
[0] = 0x80; /* command "set data bits low byte" */
2307 buf
[1] = value
; /* value */
2308 buf
[2] = direction
; /* direction */
2310 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2312 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
2313 LOG_ERROR("couldn't initialize data bits low byte");
2314 return ERROR_JTAG_INIT_FAILED
;
2320 static int ft2232_set_data_bits_high_byte(uint8_t value
, uint8_t direction
)
2323 uint32_t bytes_written
;
2325 buf
[0] = 0x82; /* command "set data bits high byte" */
2326 buf
[1] = value
; /* value */
2327 buf
[2] = direction
; /* direction */
2329 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2331 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
2332 LOG_ERROR("couldn't initialize data bits high byte");
2333 return ERROR_JTAG_INIT_FAILED
;
2339 static int ft2232_init(void)
2343 uint32_t bytes_written
;
2345 if (tap_get_tms_path_len(TAP_IRPAUSE
, TAP_IRPAUSE
) == 7)
2346 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2348 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2349 if (layout
== NULL
) {
2350 LOG_WARNING("No ft2232 layout specified'");
2351 return ERROR_JTAG_INIT_FAILED
;
2354 for (int i
= 0; 1; i
++) {
2356 * "more indicates that there are more IDs to try, so we should
2357 * not print an error for an ID mismatch (but for anything
2360 * try_more indicates that the error code returned indicates an
2361 * ID mismatch (and nothing else) and that we should proceeed
2362 * with the next ID pair.
2364 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2367 #if BUILD_FT2232_FTD2XX == 1
2368 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2370 #elif BUILD_FT2232_LIBFTDI == 1
2371 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2372 more
, &try_more
, layout
->channel
);
2376 if (!more
|| !try_more
)
2380 ft2232_buffer_size
= 0;
2381 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2383 if (layout
->init() != ERROR_OK
)
2384 return ERROR_JTAG_INIT_FAILED
;
2386 if (ft2232_device_is_highspeed()) {
2387 #ifndef BUILD_FT2232_HIGHSPEED
2388 #if BUILD_FT2232_FTD2XX == 1
2390 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2391 #elif BUILD_FT2232_LIBFTDI == 1
2393 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2396 /* make sure the legacy mode is disabled */
2397 if (ftx232h_clk_divide_by_5(false) != ERROR_OK
)
2398 return ERROR_JTAG_INIT_FAILED
;
2401 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2402 retval
= ft2232_write(buf
, 1, &bytes_written
);
2403 if (retval
!= ERROR_OK
) {
2404 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2405 return ERROR_JTAG_INIT_FAILED
;
2408 #if BUILD_FT2232_FTD2XX == 1
2409 return ft2232_purge_ftd2xx();
2410 #elif BUILD_FT2232_LIBFTDI == 1
2411 return ft2232_purge_libftdi();
2417 /** Updates defaults for DBUS signals: the four JTAG signals
2418 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2420 static inline void ftx232_dbus_init(void)
2423 low_direction
= 0x0b;
2426 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2427 * the four GPIOL signals. Initialization covers value and direction,
2428 * as customized for each layout.
2430 static int ftx232_dbus_write(void)
2432 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2433 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
2434 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2435 low_output
&= ~nTRST
; /* nTRST = 0 */
2437 low_direction
|= nTRSTnOE
; /* nTRST output */
2438 low_output
|= nTRST
; /* nTRST = 1 */
2441 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
2442 low_direction
|= nSRSTnOE
; /* nSRST output */
2443 low_output
|= nSRST
; /* nSRST = 1 */
2445 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2446 low_output
&= ~nSRST
; /* nSRST = 0 */
2449 /* initialize low byte for jtag */
2450 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2451 LOG_ERROR("couldn't initialize FT2232 DBUS");
2452 return ERROR_JTAG_INIT_FAILED
;
2458 static int usbjtag_init(void)
2461 * NOTE: This is now _specific_ to the "usbjtag" layout.
2462 * Don't try cram any more layouts into this.
2471 return ftx232_dbus_write();
2474 static int lm3s811_jtag_init(void)
2478 /* There are multiple revisions of LM3S811 eval boards:
2479 * - Rev B (and older?) boards have no SWO trace support.
2480 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2481 * they should use the "luminary_icdi" layout instead.
2488 low_direction
= 0x8b;
2490 return ftx232_dbus_write();
2493 static int icdi_jtag_init(void)
2497 /* Most Luminary eval boards support SWO trace output,
2498 * and should use this "luminary_icdi" layout.
2500 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2501 * to switch between JTAG and SWD, or switch the ft2232 UART
2502 * on the second MPSSE channel/interface (BDBUS)
2503 * between (i) the stellaris UART (on Luminary boards)
2504 * or (ii) SWO trace data (generic).
2506 * We come up in JTAG mode and may switch to SWD later (with
2507 * SWO/trace option if SWD is active).
2514 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2515 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2516 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2519 /* GPIOs on second channel/interface (UART) ... */
2520 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2521 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2522 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2527 nSRSTnOE
= ICDI_SRST
;
2529 low_direction
|= ICDI_JTAG_EN
| ICDI_DBG_ENn
;
2530 low_output
|= ICDI_JTAG_EN
;
2531 low_output
&= ~ICDI_DBG_ENn
;
2533 return ftx232_dbus_write();
2536 static int signalyzer_init(void)
2544 return ftx232_dbus_write();
2547 static int axm0432_jtag_init(void)
2550 low_direction
= 0x2b;
2552 /* initialize low byte for jtag */
2553 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2554 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2555 return ERROR_JTAG_INIT_FAILED
;
2558 if (strcmp(layout
->name
, "axm0432_jtag") == 0) {
2560 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2562 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2564 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2569 high_direction
= 0x0c;
2571 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2572 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2573 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2575 high_output
|= nTRST
;
2577 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2578 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2580 high_output
|= nSRST
;
2582 /* initialize high byte for jtag */
2583 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2584 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2585 return ERROR_JTAG_INIT_FAILED
;
2591 static int redbee_init(void)
2594 low_direction
= 0x2b;
2596 /* initialize low byte for jtag */
2597 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2598 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2599 return ERROR_JTAG_INIT_FAILED
;
2603 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2605 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2608 high_direction
= 0x0c;
2610 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2611 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2612 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2614 high_output
|= nTRST
;
2616 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2617 LOG_ERROR("can't set nSRST to push-pull on redbee");
2619 high_output
|= nSRST
;
2621 /* initialize high byte for jtag */
2622 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2623 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2624 return ERROR_JTAG_INIT_FAILED
;
2630 static int jtagkey_init(void)
2633 low_direction
= 0x1b;
2635 /* initialize low byte for jtag */
2636 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2637 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2638 return ERROR_JTAG_INIT_FAILED
;
2641 if (strcmp(layout
->name
, "jtagkey") == 0) {
2646 } else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2647 || (strcmp(layout
->name
, "oocdlink") == 0)) {
2653 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2658 high_direction
= 0x0f;
2660 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2661 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
2662 high_output
|= nTRSTnOE
;
2663 high_output
&= ~nTRST
;
2665 high_output
&= ~nTRSTnOE
;
2666 high_output
|= nTRST
;
2669 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
2670 high_output
&= ~nSRSTnOE
;
2671 high_output
|= nSRST
;
2673 high_output
|= nSRSTnOE
;
2674 high_output
&= ~nSRST
;
2677 /* initialize high byte for jtag */
2678 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2679 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2680 return ERROR_JTAG_INIT_FAILED
;
2686 static int olimex_jtag_init(void)
2689 low_direction
= 0x1b;
2691 /* initialize low byte for jtag */
2692 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2693 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2694 return ERROR_JTAG_INIT_FAILED
;
2700 nSRSTnOE
= 0x00;/* no output enable for nSRST */
2703 high_direction
= 0x0f;
2705 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2706 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
2707 high_output
|= nTRSTnOE
;
2708 high_output
&= ~nTRST
;
2710 high_output
&= ~nTRSTnOE
;
2711 high_output
|= nTRST
;
2714 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2715 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2717 high_output
&= ~nSRST
;
2719 /* turn red LED on */
2720 high_output
|= 0x08;
2722 /* initialize high byte for jtag */
2723 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2724 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2725 return ERROR_JTAG_INIT_FAILED
;
2731 static int flyswatter_init(int rev
)
2734 low_direction
= 0x7b;
2736 if ((rev
< 0) || (rev
> 3)) {
2737 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev
);
2738 return ERROR_JTAG_INIT_FAILED
;
2742 low_direction
|= 1 << 7;
2744 /* initialize low byte for jtag */
2745 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2746 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2747 return ERROR_JTAG_INIT_FAILED
;
2751 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2753 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2758 high_direction
= 0x0c;
2760 high_direction
= 0x01;
2762 /* turn red LED3 on, LED2 off */
2763 high_output
|= 0x08;
2765 /* initialize high byte for jtag */
2766 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2767 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2768 return ERROR_JTAG_INIT_FAILED
;
2774 static int flyswatter1_init(void)
2776 return flyswatter_init(1);
2779 static int flyswatter2_init(void)
2781 return flyswatter_init(2);
2784 static int minimodule_init(void)
2786 low_output
= 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2788 low_direction
= 0xfb; /* 0xfb; */
2790 /* initialize low byte for jtag */
2791 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2792 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2793 return ERROR_JTAG_INIT_FAILED
;
2800 high_direction
= 0x05;
2802 /* turn red LED3 on, LED2 off */
2803 /* high_output |= 0x08; */
2805 /* initialize high byte for jtag */
2806 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2807 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2808 return ERROR_JTAG_INIT_FAILED
;
2814 static int turtle_init(void)
2817 low_direction
= 0x5b;
2819 /* initialize low byte for jtag */
2820 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2821 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2822 return ERROR_JTAG_INIT_FAILED
;
2828 high_direction
= 0x0C;
2830 /* initialize high byte for jtag */
2831 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2832 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2833 return ERROR_JTAG_INIT_FAILED
;
2839 static int comstick_init(void)
2842 low_direction
= 0x0b;
2844 /* initialize low byte for jtag */
2845 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2846 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2847 return ERROR_JTAG_INIT_FAILED
;
2851 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2853 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2856 high_direction
= 0x03;
2858 /* initialize high byte for jtag */
2859 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2860 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2861 return ERROR_JTAG_INIT_FAILED
;
2867 static int stm32stick_init(void)
2870 low_direction
= 0x8b;
2872 /* initialize low byte for jtag */
2873 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2874 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2875 return ERROR_JTAG_INIT_FAILED
;
2879 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2881 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2884 high_direction
= 0x03;
2886 /* initialize high byte for jtag */
2887 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2888 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2889 return ERROR_JTAG_INIT_FAILED
;
2895 static int sheevaplug_init(void)
2898 low_direction
= 0x1b;
2900 /* initialize low byte for jtag */
2901 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2902 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2903 return ERROR_JTAG_INIT_FAILED
;
2912 high_direction
= 0x0f;
2914 /* nTRST is always push-pull */
2915 high_output
&= ~nTRSTnOE
;
2916 high_output
|= nTRST
;
2918 /* nSRST is always open-drain */
2919 high_output
|= nSRSTnOE
;
2920 high_output
&= ~nSRST
;
2922 /* initialize high byte for jtag */
2923 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2924 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2925 return ERROR_JTAG_INIT_FAILED
;
2931 static int cortino_jtag_init(void)
2934 low_direction
= 0x1b;
2936 /* initialize low byte for jtag */
2937 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
2938 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2939 return ERROR_JTAG_INIT_FAILED
;
2943 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2945 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2948 high_direction
= 0x03;
2950 /* initialize high byte for jtag */
2951 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2952 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2953 return ERROR_JTAG_INIT_FAILED
;
2959 static int lisa_l_init(void)
2969 high_direction
= 0x18;
2971 /* initialize high byte for jtag */
2972 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2973 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2974 return ERROR_JTAG_INIT_FAILED
;
2977 return ftx232_dbus_write();
2980 static int flossjtag_init(void)
2990 high_direction
= 0x18;
2992 /* initialize high byte for jtag */
2993 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
2994 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2995 return ERROR_JTAG_INIT_FAILED
;
2998 return ftx232_dbus_write();
3002 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3003 * the door for a number of different configurations
3005 * Known Implementations:
3006 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3008 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3009 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3010 * * ACBUS3 to transition 0->1 (OE rising edge)
3011 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3012 * * ADBUS5/GPIOL1 = EMU_EN = 1
3013 * * ADBUS6/GPIOL2 = EMU0 = 0
3014 * * ACBUS4/SPARE0 = EMU1 = 0
3015 * * CPLD logic: Disable loopback
3016 * * ACBUS6/SPARE2 = LOOPBACK = 0
3018 #define XDS100_nEMU_EN (1<<5)
3019 #define XDS100_nEMU0 (1<<6)
3021 #define XDS100_PWR_RST (1<<3)
3022 #define XDS100_nEMU1 (1<<4)
3023 #define XDS100_LOOPBACK (1<<6)
3024 static int xds100v2_init(void)
3026 /* These are in the lower byte */
3030 /* These aren't actually used on 14 pin connectors
3031 * These are in the upper byte */
3035 low_output
= 0x08 | nTRST
| XDS100_nEMU_EN
;
3036 low_direction
= 0x0b | nTRSTnOE
| XDS100_nEMU_EN
| XDS100_nEMU0
;
3038 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
3039 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3040 return ERROR_JTAG_INIT_FAILED
;
3044 high_direction
= nSRSTnOE
| XDS100_LOOPBACK
| XDS100_PWR_RST
| XDS100_nEMU1
;
3046 /* initialize high byte for jtag */
3047 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3048 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3049 return ERROR_JTAG_INIT_FAILED
;
3052 high_output
|= XDS100_PWR_RST
;
3054 /* initialize high byte for jtag */
3055 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3056 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3057 return ERROR_JTAG_INIT_FAILED
;
3063 static void olimex_jtag_blink(void)
3065 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3066 * ACBUS3 is bit 3 of the GPIOH port
3068 high_output
^= 0x08;
3071 buffer_write(high_output
);
3072 buffer_write(high_direction
);
3075 static void flyswatter_jtag_blink(unsigned char led
)
3078 buffer_write(high_output
^ led
);
3079 buffer_write(high_direction
);
3082 static void flyswatter1_jtag_blink(void)
3085 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3087 flyswatter_jtag_blink(0xc);
3090 static void flyswatter2_jtag_blink(void)
3093 * Flyswatter2 only has one LED connected to ACBUS2
3095 flyswatter_jtag_blink(0x4);
3098 static void turtle_jtag_blink(void)
3101 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3103 if (high_output
& 0x08)
3109 buffer_write(high_output
);
3110 buffer_write(high_direction
);
3113 static void lisa_l_blink(void)
3116 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3118 if (high_output
& 0x10)
3124 buffer_write(high_output
);
3125 buffer_write(high_direction
);
3128 static void flossjtag_blink(void)
3131 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3133 if (high_output
& 0x10)
3139 buffer_write(high_output
);
3140 buffer_write(high_direction
);
3143 static int ft2232_quit(void)
3145 #if BUILD_FT2232_FTD2XX == 1
3148 status
= FT_Close(ftdih
);
3149 #elif BUILD_FT2232_LIBFTDI == 1
3150 ftdi_usb_close(&ftdic
);
3152 ftdi_deinit(&ftdic
);
3155 free(ft2232_buffer
);
3156 ft2232_buffer
= NULL
;
3161 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3165 if (CMD_ARGC
== 1) {
3166 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3167 cp
= strchr(ft2232_device_desc
, 0);
3168 /* under Win32, the FTD2XX driver appends an "A" to the end
3169 * of the description, this examines the given desc
3170 * and creates the 'missing' _A or non_A variable. */
3171 if ((cp
[-1] == 'A') && (cp
[-2] == ' ')) {
3172 /* it was, so make this the "A" version. */
3173 ft2232_device_desc_A
= ft2232_device_desc
;
3174 /* and *CREATE* the non-A version. */
3175 strcpy(buf
, ft2232_device_desc
);
3176 cp
= strchr(buf
, 0);
3178 ft2232_device_desc
= strdup(buf
);
3180 /* <space > A not defined
3182 sprintf(buf
, "%s A", ft2232_device_desc
);
3183 ft2232_device_desc_A
= strdup(buf
);
3186 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3191 COMMAND_HANDLER(ft2232_handle_serial_command
)
3194 ft2232_serial
= strdup(CMD_ARGV
[0]);
3196 return ERROR_COMMAND_SYNTAX_ERROR
;
3201 COMMAND_HANDLER(ft2232_handle_layout_command
)
3204 return ERROR_COMMAND_SYNTAX_ERROR
;
3207 LOG_ERROR("already specified ft2232_layout %s",
3209 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3214 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3215 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3221 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3225 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3227 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
3228 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3229 "(maximum is %d pairs)", MAX_USB_IDS
);
3230 CMD_ARGC
= MAX_USB_IDS
* 2;
3232 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3233 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3235 return ERROR_COMMAND_SYNTAX_ERROR
;
3236 /* remove the incomplete trailing id */
3241 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3242 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3243 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3247 * Explicitly terminate, in case there are multiples instances of
3250 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3255 COMMAND_HANDLER(ft2232_handle_latency_command
)
3258 ft2232_latency
= atoi(CMD_ARGV
[0]);
3260 return ERROR_COMMAND_SYNTAX_ERROR
;
3265 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
*cmd
)
3269 /* 7 bits of either ones or zeros. */
3270 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3272 while (num_cycles
> 0) {
3273 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3274 * at most 7 bits per invocation. Here we invoke it potentially
3277 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3279 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
) {
3280 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3281 retval
= ERROR_JTAG_QUEUE_FAILED
;
3286 /* there are no state transitions in this code, so omit state tracking */
3288 /* command "Clock Data to TMS/CS Pin (no Read)" */
3292 buffer_write(bitcount_per_command
- 1);
3294 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3299 num_cycles
-= bitcount_per_command
;
3305 /* ---------------------------------------------------------------------
3306 * Support for IceBear JTAG adapter from Section5:
3307 * http://section5.ch/icebear
3309 * Author: Sten, debian@sansys-electronic.com
3312 /* Icebear pin layout
3314 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3315 * GND GND | 4 3| n.c.
3316 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3317 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3318 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3319 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3320 * ADBUS2 TDO |14 13| GND GND
3322 * ADBUS0 O L TCK ACBUS0 GND
3323 * ADBUS1 O L TDI ACBUS1 GND
3324 * ADBUS2 I TDO ACBUS2 n.c.
3325 * ADBUS3 O H TMS ACBUS3 n.c.
3331 static int icebear_jtag_init(void)
3333 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3334 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3338 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3339 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3340 low_direction
&= ~nTRST
; /* nTRST high impedance */
3342 low_direction
|= nTRST
;
3343 low_output
|= nTRST
;
3346 low_direction
|= nSRST
;
3347 low_output
|= nSRST
;
3349 /* initialize low byte for jtag */
3350 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
3351 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3352 return ERROR_JTAG_INIT_FAILED
;
3356 high_direction
= 0x00;
3358 /* initialize high byte for jtag */
3359 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3360 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3361 return ERROR_JTAG_INIT_FAILED
;
3367 static void icebear_jtag_reset(int trst
, int srst
)
3370 low_direction
|= nTRST
;
3371 low_output
&= ~nTRST
;
3372 } else if (trst
== 0) {
3373 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3374 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3375 low_direction
&= ~nTRST
;
3377 low_output
|= nTRST
;
3381 low_output
&= ~nSRST
;
3383 low_output
|= nSRST
;
3385 /* command "set data bits low byte" */
3387 buffer_write(low_output
);
3388 buffer_write(low_direction
);
3390 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3397 /* ---------------------------------------------------------------------
3398 * Support for Signalyzer H2 and Signalyzer H4
3399 * JTAG adapter from Xverve Technologies Inc.
3400 * http://www.signalyzer.com or http://www.xverve.com
3402 * Author: Oleg Seiljus, oleg@signalyzer.com
3404 static unsigned char signalyzer_h_side
;
3405 static unsigned int signalyzer_h_adapter_type
;
3407 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3409 #if BUILD_FT2232_FTD2XX == 1
3410 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3413 #define SIGNALYZER_COMMAND_ADDR 128
3414 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3416 #define SIGNALYZER_COMMAND_VERSION 0x41
3417 #define SIGNALYZER_COMMAND_RESET 0x42
3418 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3419 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3420 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3421 #define SIGNALYZER_COMMAND_LED_SET 0x53
3422 #define SIGNALYZER_COMMAND_ADC 0x54
3423 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3424 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3425 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3426 #define SIGNALYZER_COMMAND_I2C 0x58
3428 #define SIGNALYZER_CHAN_A 1
3429 #define SIGNALYZER_CHAN_B 2
3430 /* LEDS use channel C */
3431 #define SIGNALYZER_CHAN_C 4
3433 #define SIGNALYZER_LED_GREEN 1
3434 #define SIGNALYZER_LED_RED 2
3436 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3437 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3438 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3439 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3440 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3443 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3445 #if BUILD_FT2232_FTD2XX == 1
3446 return FT_WriteEE(ftdih
, address
, value
);
3447 #elif BUILD_FT2232_LIBFTDI == 1
3452 #if BUILD_FT2232_FTD2XX == 1
3453 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3455 return FT_ReadEE(ftdih
, address
, value
);
3459 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3460 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3462 unsigned char on_time
;
3463 unsigned char off_time
;
3465 if (on_time_ms
< 0xFFFF)
3466 on_time
= (unsigned char)(on_time_ms
/ 62);
3470 off_time
= (unsigned char)(off_time_ms
/ 62);
3472 #if BUILD_FT2232_FTD2XX == 1
3475 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3476 ((uint32_t)(channel
<< 8) | led
));
3477 if (status
!= FT_OK
) {
3478 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3479 ftd2xx_status_string(status
));
3480 return ERROR_JTAG_DEVICE_ERROR
;
3483 status
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3484 ((uint32_t)(on_time
<< 8) | off_time
));
3485 if (status
!= FT_OK
) {
3486 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3487 ftd2xx_status_string(status
));
3488 return ERROR_JTAG_DEVICE_ERROR
;
3491 status
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3492 ((uint32_t)cycles
));
3493 if (status
!= FT_OK
) {
3494 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3495 ftd2xx_status_string(status
));
3496 return ERROR_JTAG_DEVICE_ERROR
;
3499 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3500 SIGNALYZER_COMMAND_LED_SET
);
3501 if (status
!= FT_OK
) {
3502 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3503 ftd2xx_status_string(status
));
3504 return ERROR_JTAG_DEVICE_ERROR
;
3508 #elif BUILD_FT2232_LIBFTDI == 1
3511 retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3512 ((uint32_t)(channel
<< 8) | led
));
3514 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3515 ftdi_get_error_string(&ftdic
));
3516 return ERROR_JTAG_DEVICE_ERROR
;
3519 retval
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3520 ((uint32_t)(on_time
<< 8) | off_time
));
3522 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3523 ftdi_get_error_string(&ftdic
));
3524 return ERROR_JTAG_DEVICE_ERROR
;
3527 retval
= signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3530 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3531 ftdi_get_error_string(&ftdic
));
3532 return ERROR_JTAG_DEVICE_ERROR
;
3535 retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3536 SIGNALYZER_COMMAND_LED_SET
);
3538 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3539 ftdi_get_error_string(&ftdic
));
3540 return ERROR_JTAG_DEVICE_ERROR
;
3547 static int signalyzer_h_init(void)
3549 #if BUILD_FT2232_FTD2XX == 1
3556 uint16_t read_buf
[12] = { 0 };
3558 /* turn on center green led */
3559 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3560 0xFFFF, 0x00, 0x00);
3562 /* determine what channel config wants to open
3563 * TODO: change me... current implementation is made to work
3564 * with openocd description parsing.
3566 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3569 signalyzer_h_side
= *(end_of_desc
- 1);
3570 if (signalyzer_h_side
== 'B')
3571 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3573 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3575 LOG_ERROR("No Channel was specified");
3579 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3582 #if BUILD_FT2232_FTD2XX == 1
3583 /* read signalyzer versionining information */
3584 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3585 SIGNALYZER_COMMAND_VERSION
);
3586 if (status
!= FT_OK
) {
3587 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3588 ftd2xx_status_string(status
));
3589 return ERROR_JTAG_DEVICE_ERROR
;
3592 for (i
= 0; i
< 10; i
++) {
3593 status
= signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3595 if (status
!= FT_OK
) {
3596 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3597 ftd2xx_status_string(status
));
3598 return ERROR_JTAG_DEVICE_ERROR
;
3602 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3603 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3604 read_buf
[4], read_buf
[5], read_buf
[6]);
3606 /* set gpio register */
3607 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3608 (uint32_t)(signalyzer_h_side
<< 8));
3609 if (status
!= FT_OK
) {
3610 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3611 ftd2xx_status_string(status
));
3612 return ERROR_JTAG_DEVICE_ERROR
;
3615 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0404);
3616 if (status
!= FT_OK
) {
3617 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3618 ftd2xx_status_string(status
));
3619 return ERROR_JTAG_DEVICE_ERROR
;
3622 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3623 SIGNALYZER_COMMAND_GPIO_STATE
);
3624 if (status
!= FT_OK
) {
3625 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3626 ftd2xx_status_string(status
));
3627 return ERROR_JTAG_DEVICE_ERROR
;
3630 /* read adapter type information */
3631 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3632 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01));
3633 if (status
!= FT_OK
) {
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3635 ftd2xx_status_string(status
));
3636 return ERROR_JTAG_DEVICE_ERROR
;
3639 status
= signalyzer_h_ctrl_write(
3640 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000);
3641 if (status
!= FT_OK
) {
3642 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3643 ftd2xx_status_string(status
));
3644 return ERROR_JTAG_DEVICE_ERROR
;
3647 status
= signalyzer_h_ctrl_write(
3648 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008);
3649 if (status
!= FT_OK
) {
3650 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3651 ftd2xx_status_string(status
));
3652 return ERROR_JTAG_DEVICE_ERROR
;
3655 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3656 SIGNALYZER_COMMAND_I2C
);
3657 if (status
!= FT_OK
) {
3658 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3659 ftd2xx_status_string(status
));
3660 return ERROR_JTAG_DEVICE_ERROR
;
3665 status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
, &read_buf
[0]);
3666 if (status
!= FT_OK
) {
3667 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3668 ftd2xx_status_string(status
));
3669 return ERROR_JTAG_DEVICE_ERROR
;
3672 if (read_buf
[0] != 0x0498)
3673 signalyzer_h_adapter_type
= 0x0000;
3675 for (i
= 0; i
< 4; i
++) {
3676 status
= signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR
+ i
), &read_buf
[i
]);
3677 if (status
!= FT_OK
) {
3678 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3679 ftd2xx_status_string(status
));
3680 return ERROR_JTAG_DEVICE_ERROR
;
3684 signalyzer_h_adapter_type
= read_buf
[0];
3687 #elif BUILD_FT2232_LIBFTDI == 1
3688 /* currently libftdi does not allow reading individual eeprom
3689 * locations, therefore adapter type cannot be detected.
3690 * override with most common type
3692 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3695 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3697 /* ADAPTOR: EM_LT16_A */
3698 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
) {
3699 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3700 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3708 low_direction
= 0x1b;
3711 high_direction
= 0x0;
3713 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3714 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3715 low_output
&= ~nTRST
; /* nTRST = 0 */
3717 low_direction
|= nTRSTnOE
; /* nTRST output */
3718 low_output
|= nTRST
; /* nTRST = 1 */
3721 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3722 low_direction
|= nSRSTnOE
; /* nSRST output */
3723 low_output
|= nSRST
; /* nSRST = 1 */
3725 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3726 low_output
&= ~nSRST
; /* nSRST = 0 */
3729 #if BUILD_FT2232_FTD2XX == 1
3730 /* enable power to the module */
3731 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3732 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01));
3733 if (status
!= FT_OK
) {
3734 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3735 ftd2xx_status_string(status
));
3736 return ERROR_JTAG_DEVICE_ERROR
;
3739 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3740 SIGNALYZER_COMMAND_POWERCONTROL_SET
);
3741 if (status
!= FT_OK
) {
3742 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3743 ftd2xx_status_string(status
));
3744 return ERROR_JTAG_DEVICE_ERROR
;
3747 /* set gpio mode register */
3748 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3749 (uint32_t)(signalyzer_h_side
<< 8));
3750 if (status
!= FT_OK
) {
3751 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3752 ftd2xx_status_string(status
));
3753 return ERROR_JTAG_DEVICE_ERROR
;
3756 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000);
3757 if (status
!= FT_OK
) {
3758 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3759 ftd2xx_status_string(status
));
3760 return ERROR_JTAG_DEVICE_ERROR
;
3763 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
, SIGNALYZER_COMMAND_GPIO_MODE
);
3764 if (status
!= FT_OK
) {
3765 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3766 ftd2xx_status_string(status
));
3767 return ERROR_JTAG_DEVICE_ERROR
;
3770 /* set gpio register */
3771 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3772 (uint32_t)(signalyzer_h_side
<< 8));
3773 if (status
!= FT_OK
) {
3774 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3775 ftd2xx_status_string(status
));
3776 return ERROR_JTAG_DEVICE_ERROR
;
3779 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040);
3780 if (status
!= FT_OK
) {
3781 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3782 ftd2xx_status_string(status
));
3783 return ERROR_JTAG_DEVICE_ERROR
;
3786 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3787 SIGNALYZER_COMMAND_GPIO_STATE
);
3788 if (status
!= FT_OK
) {
3789 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3790 ftd2xx_status_string(status
));
3791 return ERROR_JTAG_DEVICE_ERROR
;
3795 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3796 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3797 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3798 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3799 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)) {
3800 if (signalyzer_h_adapter_type
3801 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3802 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3803 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3804 else if (signalyzer_h_adapter_type
3805 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3806 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3807 "(ARM JTAG with PSU) detected. (HW: %2x).",
3808 (read_buf
[1] >> 8));
3809 else if (signalyzer_h_adapter_type
3810 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3811 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3812 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3813 else if (signalyzer_h_adapter_type
3814 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3815 LOG_INFO("Signalyzer: EM-JTAG-P "
3816 "(Generic JTAG with PSU) detected. (HW: %2x).",
3817 (read_buf
[1] >> 8));
3825 low_direction
= 0x1b;
3828 high_direction
= 0x1f;
3830 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3831 high_output
|= nTRSTnOE
;
3832 high_output
&= ~nTRST
;
3834 high_output
&= ~nTRSTnOE
;
3835 high_output
|= nTRST
;
3838 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3839 high_output
&= ~nSRSTnOE
;
3840 high_output
|= nSRST
;
3842 high_output
|= nSRSTnOE
;
3843 high_output
&= ~nSRST
;
3846 #if BUILD_FT2232_FTD2XX == 1
3847 /* enable power to the module */
3848 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3849 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01));
3850 if (status
!= FT_OK
) {
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3852 ftd2xx_status_string(status
));
3853 return ERROR_JTAG_DEVICE_ERROR
;
3856 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3857 SIGNALYZER_COMMAND_POWERCONTROL_SET
);
3858 if (status
!= FT_OK
) {
3859 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3860 ftd2xx_status_string(status
));
3861 return ERROR_JTAG_DEVICE_ERROR
;
3864 /* set gpio mode register (IO_16 and IO_17 set as analog
3865 * inputs, other is gpio)
3867 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3868 (uint32_t)(signalyzer_h_side
<< 8));
3869 if (status
!= FT_OK
) {
3870 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3871 ftd2xx_status_string(status
));
3872 return ERROR_JTAG_DEVICE_ERROR
;
3875 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060);
3876 if (status
!= FT_OK
) {
3877 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3878 ftd2xx_status_string(status
));
3879 return ERROR_JTAG_DEVICE_ERROR
;
3882 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
, SIGNALYZER_COMMAND_GPIO_MODE
);
3883 if (status
!= FT_OK
) {
3884 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3885 ftd2xx_status_string(status
));
3886 return ERROR_JTAG_DEVICE_ERROR
;
3889 /* set gpio register (all inputs, for -P modules,
3890 * PSU will be turned off)
3892 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3893 (uint32_t)(signalyzer_h_side
<< 8));
3894 if (status
!= FT_OK
) {
3895 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3896 ftd2xx_status_string(status
));
3897 return ERROR_JTAG_DEVICE_ERROR
;
3900 status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000);
3901 if (status
!= FT_OK
) {
3902 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3903 ftd2xx_status_string(status
));
3904 return ERROR_JTAG_DEVICE_ERROR
;
3907 status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
, SIGNALYZER_COMMAND_GPIO_STATE
);
3908 if (status
!= FT_OK
) {
3909 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3910 ftd2xx_status_string(status
));
3911 return ERROR_JTAG_DEVICE_ERROR
;
3914 } else if (signalyzer_h_adapter_type
== 0x0000) {
3915 LOG_INFO("Signalyzer: No external modules were detected.");
3923 low_direction
= 0x1b;
3926 high_direction
= 0x0;
3928 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3929 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3930 low_output
&= ~nTRST
; /* nTRST = 0 */
3932 low_direction
|= nTRSTnOE
; /* nTRST output */
3933 low_output
|= nTRST
; /* nTRST = 1 */
3936 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3937 low_direction
|= nSRSTnOE
; /* nSRST output */
3938 low_output
|= nSRST
; /* nSRST = 1 */
3940 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3941 low_output
&= ~nSRST
; /* nSRST = 0 */
3944 LOG_ERROR("Unknown module type is detected: %.4x",
3945 signalyzer_h_adapter_type
);
3946 return ERROR_JTAG_DEVICE_ERROR
;
3949 /* initialize low byte of controller for jtag operation */
3950 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
3951 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3952 return ERROR_JTAG_INIT_FAILED
;
3955 #if BUILD_FT2232_FTD2XX == 1
3956 if (ftdi_device
== FT_DEVICE_2232H
) {
3957 /* initialize high byte of controller for jtag operation */
3958 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3959 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3960 return ERROR_JTAG_INIT_FAILED
;
3963 #elif BUILD_FT2232_LIBFTDI == 1
3964 if (ftdi_device
== TYPE_2232H
) {
3965 /* initialize high byte of controller for jtag operation */
3966 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
3967 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3968 return ERROR_JTAG_INIT_FAILED
;
3975 static void signalyzer_h_reset(int trst
, int srst
)
3977 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3979 /* ADAPTOR: EM_LT16_A */
3980 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
;
3988 } else if (trst
== 0) {
3989 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3990 /* switch to input pin (high-Z + internal
3991 * and external pullup) */
3992 low_direction
&= ~nTRSTnOE
;
3994 /* switch output high */
3995 low_output
|= nTRST
;
3999 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4000 /* switch output low */
4001 low_output
&= ~nSRST
;
4003 /* switch to output pin (output is low) */
4004 low_direction
|= nSRSTnOE
;
4005 } else if (srst
== 0) {
4006 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4007 /* switch output high */
4008 low_output
|= nSRST
;
4010 /* switch to input pin (high-Z) */
4011 low_direction
&= ~nSRSTnOE
;
4014 /* command "set data bits low byte" */
4016 buffer_write(low_output
);
4017 buffer_write(low_direction
);
4018 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4019 "low_direction: 0x%2.2x",
4020 trst
, srst
, low_output
, low_direction
);
4022 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4023 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4024 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4025 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4026 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)) {
4028 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4029 high_output
&= ~nTRSTnOE
;
4031 high_output
&= ~nTRST
;
4032 } else if (trst
== 0) {
4033 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4034 high_output
|= nTRSTnOE
;
4036 high_output
|= nTRST
;
4040 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4041 high_output
&= ~nSRST
;
4043 high_output
&= ~nSRSTnOE
;
4044 } else if (srst
== 0) {
4045 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4046 high_output
|= nSRST
;
4048 high_output
|= nSRSTnOE
;
4051 /* command "set data bits high byte" */
4053 buffer_write(high_output
);
4054 buffer_write(high_direction
);
4055 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4056 "high_direction: 0x%2.2x",
4057 trst
, srst
, high_output
, high_direction
);
4058 } else if (signalyzer_h_adapter_type
== 0x0000) {
4060 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4061 /* switch to output pin (output is low) */
4062 low_direction
|= nTRSTnOE
;
4064 /* switch output low */
4065 low_output
&= ~nTRST
;
4066 } else if (trst
== 0) {
4067 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4068 /* switch to input pin (high-Z + internal
4069 * and external pullup) */
4070 low_direction
&= ~nTRSTnOE
;
4072 /* switch output high */
4073 low_output
|= nTRST
;
4077 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4078 /* switch output low */
4079 low_output
&= ~nSRST
;
4081 /* switch to output pin (output is low) */
4082 low_direction
|= nSRSTnOE
;
4083 } else if (srst
== 0) {
4084 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4085 /* switch output high */
4086 low_output
|= nSRST
;
4088 /* switch to input pin (high-Z) */
4089 low_direction
&= ~nSRSTnOE
;
4092 /* command "set data bits low byte" */
4094 buffer_write(low_output
);
4095 buffer_write(low_direction
);
4096 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4097 "low_direction: 0x%2.2x",
4098 trst
, srst
, low_output
, low_direction
);
4102 static void signalyzer_h_blink(void)
4104 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4107 /********************************************************************
4108 * Support for KT-LINK
4109 * JTAG adapter from KRISTECH
4110 * http://www.kristech.eu
4111 *******************************************************************/
4112 static int ktlink_init(void)
4114 uint8_t swd_en
= 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4116 low_output
= 0x08 | swd_en
; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4117 low_direction
= 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4119 /* initialize low byte for jtag */
4120 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
4121 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4122 return ERROR_JTAG_INIT_FAILED
;
4130 high_output
= 0x80; /* turn LED on */
4131 high_direction
= 0xFF; /* all outputs */
4133 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4135 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4136 high_output
|= nTRSTnOE
;
4137 high_output
&= ~nTRST
;
4139 high_output
&= ~nTRSTnOE
;
4140 high_output
|= nTRST
;
4143 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4144 high_output
&= ~nSRSTnOE
;
4145 high_output
|= nSRST
;
4147 high_output
|= nSRSTnOE
;
4148 high_output
&= ~nSRST
;
4151 /* initialize high byte for jtag */
4152 if (ft2232_set_data_bits_high_byte(high_output
, high_direction
) != ERROR_OK
) {
4153 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4154 return ERROR_JTAG_INIT_FAILED
;
4160 static void ktlink_reset(int trst
, int srst
)
4162 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4165 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4166 high_output
&= ~nTRSTnOE
;
4168 high_output
&= ~nTRST
;
4169 } else if (trst
== 0) {
4170 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4171 high_output
|= nTRSTnOE
;
4173 high_output
|= nTRST
;
4177 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4178 high_output
&= ~nSRST
;
4180 high_output
&= ~nSRSTnOE
;
4181 } else if (srst
== 0) {
4182 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4183 high_output
|= nSRST
;
4185 high_output
|= nSRSTnOE
;
4188 buffer_write(0x82); /* command "set data bits high byte" */
4189 buffer_write(high_output
);
4190 buffer_write(high_direction
);
4191 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4198 static void ktlink_blink(void)
4200 /* LED connected to ACBUS7 */
4201 high_output
^= 0x80;
4203 buffer_write(0x82); /* command "set data bits high byte" */
4204 buffer_write(high_output
);
4205 buffer_write(high_direction
);
4208 /********************************************************************
4209 * Support for Digilent HS-1
4210 * JTAG adapter from Digilent
4211 * http://www.digilent.com
4212 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4213 *******************************************************************/
4215 static int digilent_hs1_init(void)
4217 /* the adapter only supports the base JTAG signals, no nTRST
4220 low_direction
= 0x8b;
4222 /* initialize low byte for jtag */
4223 if (ft2232_set_data_bits_low_byte(low_output
, low_direction
) != ERROR_OK
) {
4224 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4225 return ERROR_JTAG_INIT_FAILED
;
4230 static void digilent_hs1_reset(int trst
, int srst
)
4232 /* Dummy function, no reset signals supported. */
4235 static const struct command_registration ft2232_command_handlers
[] = {
4237 .name
= "ft2232_device_desc",
4238 .handler
= &ft2232_handle_device_desc_command
,
4239 .mode
= COMMAND_CONFIG
,
4240 .help
= "set the USB device description of the FTDI FT2232 device",
4241 .usage
= "description_string",
4244 .name
= "ft2232_serial",
4245 .handler
= &ft2232_handle_serial_command
,
4246 .mode
= COMMAND_CONFIG
,
4247 .help
= "set the serial number of the FTDI FT2232 device",
4248 .usage
= "serial_string",
4251 .name
= "ft2232_layout",
4252 .handler
= &ft2232_handle_layout_command
,
4253 .mode
= COMMAND_CONFIG
,
4254 .help
= "set the layout of the FT2232 GPIO signals used "
4255 "to control output-enables and reset signals",
4256 .usage
= "layout_name",
4259 .name
= "ft2232_vid_pid",
4260 .handler
= &ft2232_handle_vid_pid_command
,
4261 .mode
= COMMAND_CONFIG
,
4262 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4263 .usage
= "(vid pid)* ",
4266 .name
= "ft2232_latency",
4267 .handler
= &ft2232_handle_latency_command
,
4268 .mode
= COMMAND_CONFIG
,
4269 .help
= "set the FT2232 latency timer to a new value",
4272 COMMAND_REGISTRATION_DONE
4275 struct jtag_interface ft2232_interface
= {
4277 .supported
= DEBUG_CAP_TMS_SEQ
,
4278 .commands
= ft2232_command_handlers
,
4279 .transports
= jtag_only
,
4281 .init
= ft2232_init
,
4282 .quit
= ft2232_quit
,
4283 .speed
= ft2232_speed
,
4284 .speed_div
= ft2232_speed_div
,
4286 .execute_queue
= ft2232_execute_queue
,