1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <jtag/transport.h>
85 #include <helper/time_support.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
154 static char * ft2232_device_desc_A
= NULL
;
155 static char* ft2232_device_desc
= NULL
;
156 static char* ft2232_serial
= NULL
;
157 static uint8_t ft2232_latency
= 2;
158 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
165 struct ft2232_layout
{
168 void (*reset
)(int trst
, int srst
);
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter_init(void);
180 static int turtle_init(void);
181 static int comstick_init(void);
182 static int stm32stick_init(void);
183 static int axm0432_jtag_init(void);
184 static int sheevaplug_init(void);
185 static int icebear_jtag_init(void);
186 static int cortino_jtag_init(void);
187 static int signalyzer_init(void);
188 static int signalyzer_h_init(void);
189 static int ktlink_init(void);
190 static int redbee_init(void);
192 /* reset procedures for supported layouts */
193 static void ftx23_reset(int trst
, int srst
);
194 static void jtagkey_reset(int trst
, int srst
);
195 static void olimex_jtag_reset(int trst
, int srst
);
196 static void flyswatter_reset(int trst
, int srst
);
197 static void turtle_reset(int trst
, int srst
);
198 static void comstick_reset(int trst
, int srst
);
199 static void stm32stick_reset(int trst
, int srst
);
200 static void axm0432_jtag_reset(int trst
, int srst
);
201 static void sheevaplug_reset(int trst
, int srst
);
202 static void icebear_jtag_reset(int trst
, int srst
);
203 static void signalyzer_h_reset(int trst
, int srst
);
204 static void ktlink_reset(int trst
, int srst
);
205 static void redbee_reset(int trst
, int srst
);
207 /* blink procedures for layouts that support a blinking led */
208 static void olimex_jtag_blink(void);
209 static void flyswatter_jtag_blink(void);
210 static void turtle_jtag_blink(void);
211 static void signalyzer_h_blink(void);
212 static void ktlink_blink(void);
214 /* common transport support options */
216 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
218 static const struct ft2232_layout ft2232_layouts
[] =
221 .init
= usbjtag_init
,
222 .reset
= ftx23_reset
,
225 .init
= jtagkey_init
,
226 .reset
= jtagkey_reset
,
228 { .name
= "jtagkey_prototype_v1",
229 .init
= jtagkey_init
,
230 .reset
= jtagkey_reset
,
232 { .name
= "oocdlink",
233 .init
= jtagkey_init
,
234 .reset
= jtagkey_reset
,
236 { .name
= "signalyzer",
237 .init
= signalyzer_init
,
238 .reset
= ftx23_reset
,
240 { .name
= "evb_lm3s811",
241 .init
= lm3s811_jtag_init
,
242 .reset
= ftx23_reset
,
244 { .name
= "luminary_icdi",
245 .init
= icdi_jtag_init
,
246 .reset
= ftx23_reset
,
248 { .name
= "olimex-jtag",
249 .init
= olimex_jtag_init
,
250 .reset
= olimex_jtag_reset
,
251 .blink
= olimex_jtag_blink
253 { .name
= "flyswatter",
254 .init
= flyswatter_init
,
255 .reset
= flyswatter_reset
,
256 .blink
= flyswatter_jtag_blink
258 { .name
= "turtelizer2",
260 .reset
= turtle_reset
,
261 .blink
= turtle_jtag_blink
263 { .name
= "comstick",
264 .init
= comstick_init
,
265 .reset
= comstick_reset
,
267 { .name
= "stm32stick",
268 .init
= stm32stick_init
,
269 .reset
= stm32stick_reset
,
271 { .name
= "axm0432_jtag",
272 .init
= axm0432_jtag_init
,
273 .reset
= axm0432_jtag_reset
,
275 { .name
= "sheevaplug",
276 .init
= sheevaplug_init
,
277 .reset
= sheevaplug_reset
,
280 .init
= icebear_jtag_init
,
281 .reset
= icebear_jtag_reset
,
284 .init
= cortino_jtag_init
,
285 .reset
= comstick_reset
,
287 { .name
= "signalyzer-h",
288 .init
= signalyzer_h_init
,
289 .reset
= signalyzer_h_reset
,
290 .blink
= signalyzer_h_blink
294 .reset
= ktlink_reset
,
295 .blink
= ktlink_blink
297 { .name
= "redbee-econotag",
299 .reset
= redbee_reset
,
301 { .name
= "redbee-usb",
303 .reset
= redbee_reset
,
304 .channel
= INTERFACE_B
,
306 { .name
= NULL
, /* END OF TABLE */ },
309 /* bitmask used to drive nTRST; usually a GPIOLx signal */
310 static uint8_t nTRST
;
311 static uint8_t nTRSTnOE
;
312 /* bitmask used to drive nSRST; usually a GPIOLx signal */
313 static uint8_t nSRST
;
314 static uint8_t nSRSTnOE
;
316 /** the layout being used with this debug session */
317 static const struct ft2232_layout
*layout
;
319 /** default bitmask values ddriven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
320 static uint8_t low_output
= 0x0;
322 /* note that direction bit == 1 means that signal is an output */
324 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
325 static uint8_t low_direction
= 0x0;
326 /** default value bitmask for CBUS GPIOH(0..4) */
327 static uint8_t high_output
= 0x0;
328 /** default direction bitmask for CBUS GPIOH(0..4) */
329 static uint8_t high_direction
= 0x0;
331 #if BUILD_FT2232_FTD2XX == 1
332 static FT_HANDLE ftdih
= NULL
;
333 static FT_DEVICE ftdi_device
= 0;
334 #elif BUILD_FT2232_LIBFTDI == 1
335 static struct ftdi_context ftdic
;
336 static enum ftdi_chip_type ftdi_device
;
339 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
340 static int require_send
;
342 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
344 "There is a significant difference between libftdi and libftd2xx. The latter
345 one allows to schedule up to 64*64 bytes of result data while libftdi fails
346 with more than 4*64. As a consequence, the FT2232 driver is forced to
347 perform around 16x more USB transactions for long command streams with TDO
348 capture when running with libftdi."
351 #define FT2232_BUFFER_SIZE 131072
352 a comment would have been nice.
355 #define FT2232_BUFFER_SIZE 131072
357 static uint8_t* ft2232_buffer
= NULL
;
358 static int ft2232_buffer_size
= 0;
359 static int ft2232_read_pointer
= 0;
360 static int ft2232_expect_read
= 0;
363 * Function buffer_write
364 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
365 * @param val is the byte to send.
367 static inline void buffer_write(uint8_t val
)
369 assert(ft2232_buffer
);
370 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
371 ft2232_buffer
[ft2232_buffer_size
++] = val
;
375 * Function buffer_read
376 * returns a byte from the byte buffer.
378 static inline uint8_t buffer_read(void)
380 assert(ft2232_buffer
);
381 assert(ft2232_read_pointer
< ft2232_buffer_size
);
382 return ft2232_buffer
[ft2232_read_pointer
++];
386 * Clocks out \a bit_count bits on the TMS line, starting with the least
387 * significant bit of tms_bits and progressing to more significant bits.
388 * Rigorous state transition logging is done here via tap_set_state().
390 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
391 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
392 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
393 * is often used for this, 0x4b.
395 * @param tms_bits Holds the sequence of bits to send.
396 * @param tms_count Tells how many bits in the sequence.
397 * @param tdi_bit A single bit to pass on to TDI before the first TCK
398 * cycle and held static for the duration of TMS clocking.
400 * See the MPSSE spec referenced above.
402 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
406 int tms_ndx
; /* bit index into tms_byte */
408 assert(tms_count
> 0);
410 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
411 mpsse_cmd
, tms_bits
, tms_count
);
413 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
415 bool bit
= tms_bits
& 1;
418 tms_byte
|= (1 << tms_ndx
);
420 /* always do state transitions in public view */
421 tap_set_state(tap_state_transition(tap_get_state(), bit
));
423 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
428 if (tms_ndx
== 7 || i
== tms_count
-1)
430 buffer_write(mpsse_cmd
);
431 buffer_write(tms_ndx
- 1);
433 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
434 TMS/CS and is held static for the duration of TMS/CS clocking.
436 buffer_write(tms_byte
| (tdi_bit
<< 7));
442 * Function get_tms_buffer_requirements
443 * returns what clock_tms() will consume if called with
446 static inline int get_tms_buffer_requirements(int bit_count
)
448 return ((bit_count
+ 6)/7) * 3;
452 * Function move_to_state
453 * moves the TAP controller from the current state to a
454 * \a goal_state through a path given by tap_get_tms_path(). State transition
455 * logging is performed by delegation to clock_tms().
457 * @param goal_state is the destination state for the move.
459 static void move_to_state(tap_state_t goal_state
)
461 tap_state_t start_state
= tap_get_state();
463 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
464 lookup of the required TMS pattern to move to this state from the
468 /* do the 2 lookups */
469 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
470 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
472 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
474 clock_tms(0x4b, tms_bits
, tms_count
, 0);
477 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
479 #if BUILD_FT2232_FTD2XX == 1
481 DWORD dw_bytes_written
;
482 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
484 *bytes_written
= dw_bytes_written
;
485 LOG_ERROR("FT_Write returned: %lu", status
);
486 return ERROR_JTAG_DEVICE_ERROR
;
490 *bytes_written
= dw_bytes_written
;
492 #elif BUILD_FT2232_LIBFTDI == 1
494 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
497 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
498 return ERROR_JTAG_DEVICE_ERROR
;
502 *bytes_written
= retval
;
506 if (*bytes_written
!= (uint32_t)size
)
508 return ERROR_JTAG_DEVICE_ERROR
;
514 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
516 #if BUILD_FT2232_FTD2XX == 1
522 while ((*bytes_read
< size
) && timeout
--)
524 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
525 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
528 LOG_ERROR("FT_Read returned: %lu", status
);
529 return ERROR_JTAG_DEVICE_ERROR
;
531 *bytes_read
+= dw_bytes_read
;
534 #elif BUILD_FT2232_LIBFTDI == 1
536 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
539 while ((*bytes_read
< size
) && timeout
--)
541 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
544 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
545 return ERROR_JTAG_DEVICE_ERROR
;
547 *bytes_read
+= retval
;
552 if (*bytes_read
< size
)
554 LOG_ERROR("couldn't read enough bytes from "
555 "FT2232 device (%i < %i)",
556 (unsigned)*bytes_read
,
558 return ERROR_JTAG_DEVICE_ERROR
;
564 static bool ft2232_device_is_highspeed(void)
566 #if BUILD_FT2232_FTD2XX == 1
567 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
568 #elif BUILD_FT2232_LIBFTDI == 1
569 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
574 * Commands that only apply to the FT2232H and FT4232H devices.
575 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
576 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
579 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
581 uint8_t buf
= enable
? 0x96 : 0x97;
582 LOG_DEBUG("%2.2x", buf
);
584 uint32_t bytes_written
;
587 if ((retval
= ft2232_write(&buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
589 LOG_ERROR("couldn't write command to %s adaptive clocking"
590 , enable
? "enable" : "disable");
598 * Enable/disable the clk divide by 5 of the 60MHz master clock.
599 * This result in a JTAG clock speed range of 91.553Hz-6MHz
600 * respective 457.763Hz-30MHz.
602 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
604 uint32_t bytes_written
;
605 uint8_t buf
= enable
? 0x8b : 0x8a;
607 if (ft2232_write(&buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
609 LOG_ERROR("couldn't write command to %s clk divide by 5"
610 , enable
? "enable" : "disable");
611 return ERROR_JTAG_INIT_FAILED
;
613 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
614 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
619 static int ft2232_speed(int speed
)
623 uint32_t bytes_written
;
626 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
627 if (ft2232_device_is_highspeed())
628 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
629 else if (enable_adaptive_clocking
)
631 LOG_ERROR("ft2232 device %lu does not support RTCK"
632 , (long unsigned int)ftdi_device
);
636 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
639 buf
[0] = 0x86; /* command "set divisor" */
640 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
641 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
643 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
644 if ((retval
= ft2232_write(buf
, sizeof(buf
), &bytes_written
)) != ERROR_OK
)
646 LOG_ERROR("couldn't set FT2232 TCK speed");
653 static int ft2232_speed_div(int speed
, int* khz
)
655 /* Take a look in the FT2232 manual,
656 * AN2232C-01 Command Processor for
657 * MPSSE and MCU Host Bus. Chapter 3.8 */
659 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
664 static int ft2232_khz(int khz
, int* jtag_speed
)
668 if (ft2232_device_is_highspeed())
670 *jtag_speed
= RTCK_SPEED
;
675 LOG_DEBUG("RCLK not supported");
680 /* Take a look in the FT2232 manual,
681 * AN2232C-01 Command Processor for
682 * MPSSE and MCU Host Bus. Chapter 3.8
684 * We will calc here with a multiplier
685 * of 10 for better rounding later. */
687 /* Calc speed, (ft2232_max_tck / khz) - 1 */
688 /* Use 65000 for better rounding */
689 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
691 /* Add 0.9 for rounding */
694 /* Calc real speed */
695 *jtag_speed
= *jtag_speed
/ 10;
697 /* Check if speed is greater than 0 */
703 /* Check max value */
704 if (*jtag_speed
> 0xFFFF)
706 *jtag_speed
= 0xFFFF;
712 static void ft2232_end_state(tap_state_t state
)
714 if (tap_is_state_stable(state
))
715 tap_set_end_state(state
);
718 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
723 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
725 int num_bytes
= (scan_size
+ 7) / 8;
726 int bits_left
= scan_size
;
729 while (num_bytes
-- > 1)
731 buffer
[cur_byte
++] = buffer_read();
735 buffer
[cur_byte
] = 0x0;
737 /* There is one more partial byte left from the clock data in/out instructions */
740 buffer
[cur_byte
] = buffer_read() >> 1;
742 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
743 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
746 static void ft2232_debug_dump_buffer(void)
752 for (i
= 0; i
< ft2232_buffer_size
; i
++)
754 line_p
+= snprintf(line_p
, sizeof(line
) - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
757 LOG_DEBUG("%s", line
);
763 LOG_DEBUG("%s", line
);
766 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
768 struct jtag_command
* cmd
;
773 uint32_t bytes_written
= 0;
774 uint32_t bytes_read
= 0;
776 #ifdef _DEBUG_USB_IO_
777 struct timeval start
, inter
, inter2
, end
;
778 struct timeval d_inter
, d_inter2
, d_end
;
781 #ifdef _DEBUG_USB_COMMS_
782 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
783 ft2232_debug_dump_buffer();
786 #ifdef _DEBUG_USB_IO_
787 gettimeofday(&start
, NULL
);
790 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
792 LOG_ERROR("couldn't write MPSSE commands to FT2232");
796 #ifdef _DEBUG_USB_IO_
797 gettimeofday(&inter
, NULL
);
800 if (ft2232_expect_read
)
802 /* FIXME this "timeout" is never changed ... */
803 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
804 ft2232_buffer_size
= 0;
806 #ifdef _DEBUG_USB_IO_
807 gettimeofday(&inter2
, NULL
);
810 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
812 LOG_ERROR("couldn't read from FT2232");
816 #ifdef _DEBUG_USB_IO_
817 gettimeofday(&end
, NULL
);
819 timeval_subtract(&d_inter
, &inter
, &start
);
820 timeval_subtract(&d_inter2
, &inter2
, &start
);
821 timeval_subtract(&d_end
, &end
, &start
);
823 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
824 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
825 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
826 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
829 ft2232_buffer_size
= bytes_read
;
831 if (ft2232_expect_read
!= ft2232_buffer_size
)
833 LOG_ERROR("ft2232_expect_read (%i) != "
834 "ft2232_buffer_size (%i) "
838 LIBFTDI_READ_RETRY_COUNT
- timeout
);
839 ft2232_debug_dump_buffer();
844 #ifdef _DEBUG_USB_COMMS_
845 LOG_DEBUG("read buffer (%i retries): %i bytes",
846 LIBFTDI_READ_RETRY_COUNT
- timeout
,
848 ft2232_debug_dump_buffer();
852 ft2232_expect_read
= 0;
853 ft2232_read_pointer
= 0;
855 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
856 * that wasn't handled by a caller-provided error handler
866 type
= jtag_scan_type(cmd
->cmd
.scan
);
867 if (type
!= SCAN_OUT
)
869 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
870 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
871 ft2232_read_scan(type
, buffer
, scan_size
);
872 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
873 retval
= ERROR_JTAG_QUEUE_FAILED
;
885 ft2232_buffer_size
= 0;
891 * Function ft2232_add_pathmove
892 * moves the TAP controller from the current state to a new state through the
893 * given path, where path is an array of tap_state_t's.
895 * @param path is an array of tap_stat_t which gives the states to traverse through
896 * ending with the last state at path[num_states-1]
897 * @param num_states is the count of state steps to move through
899 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
903 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
907 /* this loop verifies that the path is legal and logs each state in the path */
910 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
912 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
914 /* command "Clock Data to TMS/CS Pin (no Read)" */
917 /* number of states remaining */
918 buffer_write(num_states_batch
- 1);
920 while (num_states_batch
--) {
921 /* either TMS=0 or TMS=1 must work ... */
922 if (tap_state_transition(tap_get_state(), false)
923 == path
[state_count
])
924 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
925 else if (tap_state_transition(tap_get_state(), true)
926 == path
[state_count
])
927 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
929 /* ... or else the caller goofed BADLY */
931 LOG_ERROR("BUG: %s -> %s isn't a valid "
932 "TAP state transition",
933 tap_state_name(tap_get_state()),
934 tap_state_name(path
[state_count
]));
938 tap_set_state(path
[state_count
]);
943 buffer_write(tms_byte
);
945 tap_set_end_state(tap_get_state());
948 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
950 int num_bytes
= (scan_size
+ 7) / 8;
951 int bits_left
= scan_size
;
957 if (tap_get_state() != TAP_DRSHIFT
)
959 move_to_state(TAP_DRSHIFT
);
964 if (tap_get_state() != TAP_IRSHIFT
)
966 move_to_state(TAP_IRSHIFT
);
970 /* add command for complete bytes */
971 while (num_bytes
> 1)
976 /* Clock Data Bytes In and Out LSB First */
978 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
980 else if (type
== SCAN_OUT
)
982 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
984 /* LOG_DEBUG("added TDI bytes (o)"); */
986 else if (type
== SCAN_IN
)
988 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
990 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
993 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
994 num_bytes
-= thisrun_bytes
;
996 buffer_write((uint8_t) (thisrun_bytes
- 1));
997 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1001 /* add complete bytes */
1002 while (thisrun_bytes
-- > 0)
1004 buffer_write(buffer
[cur_byte
++]);
1008 else /* (type == SCAN_IN) */
1010 bits_left
-= 8 * (thisrun_bytes
);
1014 /* the most signifcant bit is scanned during TAP movement */
1015 if (type
!= SCAN_IN
)
1016 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1020 /* process remaining bits but the last one */
1023 if (type
== SCAN_IO
)
1025 /* Clock Data Bits In and Out LSB First */
1027 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1029 else if (type
== SCAN_OUT
)
1031 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1033 /* LOG_DEBUG("added TDI bits (o)"); */
1035 else if (type
== SCAN_IN
)
1037 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1039 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1042 buffer_write(bits_left
- 2);
1043 if (type
!= SCAN_IN
)
1044 buffer_write(buffer
[cur_byte
]);
1047 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1048 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1050 if (type
== SCAN_IO
)
1052 /* Clock Data Bits In and Out LSB First */
1054 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1056 else if (type
== SCAN_OUT
)
1058 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1060 /* LOG_DEBUG("added TDI bits (o)"); */
1062 else if (type
== SCAN_IN
)
1064 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1066 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1069 buffer_write(last_bit
);
1077 /* move from Shift-IR/DR to end state */
1078 if (type
!= SCAN_OUT
)
1080 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1081 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1084 /* Clock Data to TMS/CS Pin with Read */
1089 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1090 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1091 /* Clock Data to TMS/CS Pin (no Read) */
1095 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1096 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1099 if (tap_get_state() != tap_get_end_state())
1101 move_to_state(tap_get_end_state());
1105 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1107 int num_bytes
= (scan_size
+ 7) / 8;
1108 int bits_left
= scan_size
;
1111 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1112 uint8_t* receive_pointer
= receive_buffer
;
1113 uint32_t bytes_written
;
1114 uint32_t bytes_read
;
1116 int thisrun_read
= 0;
1120 LOG_ERROR("BUG: large IR scans are not supported");
1124 if (tap_get_state() != TAP_DRSHIFT
)
1126 move_to_state(TAP_DRSHIFT
);
1129 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1131 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1134 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1135 ft2232_buffer_size
, (int)bytes_written
);
1136 ft2232_buffer_size
= 0;
1138 /* add command for complete bytes */
1139 while (num_bytes
> 1)
1143 if (type
== SCAN_IO
)
1145 /* Clock Data Bytes In and Out LSB First */
1147 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1149 else if (type
== SCAN_OUT
)
1151 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1153 /* LOG_DEBUG("added TDI bytes (o)"); */
1155 else if (type
== SCAN_IN
)
1157 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1159 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1162 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1163 thisrun_read
= thisrun_bytes
;
1164 num_bytes
-= thisrun_bytes
;
1165 buffer_write((uint8_t) (thisrun_bytes
- 1));
1166 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1168 if (type
!= SCAN_IN
)
1170 /* add complete bytes */
1171 while (thisrun_bytes
-- > 0)
1173 buffer_write(buffer
[cur_byte
]);
1178 else /* (type == SCAN_IN) */
1180 bits_left
-= 8 * (thisrun_bytes
);
1183 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1185 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1188 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1190 (int)bytes_written
);
1191 ft2232_buffer_size
= 0;
1193 if (type
!= SCAN_OUT
)
1195 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1197 LOG_ERROR("couldn't read from FT2232");
1200 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1203 receive_pointer
+= bytes_read
;
1209 /* the most signifcant bit is scanned during TAP movement */
1210 if (type
!= SCAN_IN
)
1211 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1215 /* process remaining bits but the last one */
1218 if (type
== SCAN_IO
)
1220 /* Clock Data Bits In and Out LSB First */
1222 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1224 else if (type
== SCAN_OUT
)
1226 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1228 /* LOG_DEBUG("added TDI bits (o)"); */
1230 else if (type
== SCAN_IN
)
1232 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1234 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1236 buffer_write(bits_left
- 2);
1237 if (type
!= SCAN_IN
)
1238 buffer_write(buffer
[cur_byte
]);
1240 if (type
!= SCAN_OUT
)
1244 if (tap_get_end_state() == TAP_DRSHIFT
)
1246 if (type
== SCAN_IO
)
1248 /* Clock Data Bits In and Out LSB First */
1250 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1252 else if (type
== SCAN_OUT
)
1254 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1256 /* LOG_DEBUG("added TDI bits (o)"); */
1258 else if (type
== SCAN_IN
)
1260 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1262 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1265 buffer_write(last_bit
);
1269 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1270 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1273 /* move from Shift-IR/DR to end state */
1274 if (type
!= SCAN_OUT
)
1276 /* Clock Data to TMS/CS Pin with Read */
1278 /* LOG_DEBUG("added TMS scan (read)"); */
1282 /* Clock Data to TMS/CS Pin (no Read) */
1284 /* LOG_DEBUG("added TMS scan (no read)"); */
1287 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1288 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1291 if (type
!= SCAN_OUT
)
1294 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1296 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1299 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1301 (int)bytes_written
);
1302 ft2232_buffer_size
= 0;
1304 if (type
!= SCAN_OUT
)
1306 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1308 LOG_ERROR("couldn't read from FT2232");
1311 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1314 receive_pointer
+= bytes_read
;
1320 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1322 int predicted_size
= 3;
1323 int num_bytes
= (scan_size
- 1) / 8;
1325 if (tap_get_state() != TAP_DRSHIFT
)
1326 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1328 if (type
== SCAN_IN
) /* only from device to host */
1330 /* complete bytes */
1331 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1333 /* remaining bits - 1 (up to 7) */
1334 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1336 else /* host to device, or bidirectional */
1338 /* complete bytes */
1339 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1341 /* remaining bits -1 (up to 7) */
1342 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1345 return predicted_size
;
1348 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1350 int predicted_size
= 0;
1352 if (type
!= SCAN_OUT
)
1354 /* complete bytes */
1355 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1357 /* remaining bits - 1 */
1358 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1360 /* last bit (from TMS scan) */
1361 predicted_size
+= 1;
1364 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1366 return predicted_size
;
1369 /* semi-generic FT2232/FT4232 reset code */
1370 static void ftx23_reset(int trst
, int srst
)
1372 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1375 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1376 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1378 low_output
&= ~nTRST
; /* switch output low */
1382 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1383 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1385 low_output
|= nTRST
; /* switch output high */
1390 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1391 low_output
&= ~nSRST
; /* switch output low */
1393 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1397 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1398 low_output
|= nSRST
; /* switch output high */
1400 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1403 /* command "set data bits low byte" */
1405 buffer_write(low_output
);
1406 buffer_write(low_direction
);
1409 static void jtagkey_reset(int trst
, int srst
)
1411 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1414 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1415 high_output
&= ~nTRSTnOE
;
1417 high_output
&= ~nTRST
;
1421 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1422 high_output
|= nTRSTnOE
;
1424 high_output
|= nTRST
;
1429 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1430 high_output
&= ~nSRST
;
1432 high_output
&= ~nSRSTnOE
;
1436 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1437 high_output
|= nSRST
;
1439 high_output
|= nSRSTnOE
;
1442 /* command "set data bits high byte" */
1444 buffer_write(high_output
);
1445 buffer_write(high_direction
);
1446 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1450 static void olimex_jtag_reset(int trst
, int srst
)
1452 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1455 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1456 high_output
&= ~nTRSTnOE
;
1458 high_output
&= ~nTRST
;
1462 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1463 high_output
|= nTRSTnOE
;
1465 high_output
|= nTRST
;
1470 high_output
|= nSRST
;
1474 high_output
&= ~nSRST
;
1477 /* command "set data bits high byte" */
1479 buffer_write(high_output
);
1480 buffer_write(high_direction
);
1481 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1485 static void axm0432_jtag_reset(int trst
, int srst
)
1489 tap_set_state(TAP_RESET
);
1490 high_output
&= ~nTRST
;
1494 high_output
|= nTRST
;
1499 high_output
&= ~nSRST
;
1503 high_output
|= nSRST
;
1506 /* command "set data bits low byte" */
1508 buffer_write(high_output
);
1509 buffer_write(high_direction
);
1510 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1514 static void flyswatter_reset(int trst
, int srst
)
1518 low_output
&= ~nTRST
;
1522 low_output
|= nTRST
;
1527 low_output
|= nSRST
;
1531 low_output
&= ~nSRST
;
1534 /* command "set data bits low byte" */
1536 buffer_write(low_output
);
1537 buffer_write(low_direction
);
1538 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1541 static void turtle_reset(int trst
, int srst
)
1547 low_output
|= nSRST
;
1551 low_output
&= ~nSRST
;
1554 /* command "set data bits low byte" */
1556 buffer_write(low_output
);
1557 buffer_write(low_direction
);
1558 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1561 static void comstick_reset(int trst
, int srst
)
1565 high_output
&= ~nTRST
;
1569 high_output
|= nTRST
;
1574 high_output
&= ~nSRST
;
1578 high_output
|= nSRST
;
1581 /* command "set data bits high byte" */
1583 buffer_write(high_output
);
1584 buffer_write(high_direction
);
1585 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1589 static void stm32stick_reset(int trst
, int srst
)
1593 high_output
&= ~nTRST
;
1597 high_output
|= nTRST
;
1602 low_output
&= ~nSRST
;
1606 low_output
|= nSRST
;
1609 /* command "set data bits low byte" */
1611 buffer_write(low_output
);
1612 buffer_write(low_direction
);
1614 /* command "set data bits high byte" */
1616 buffer_write(high_output
);
1617 buffer_write(high_direction
);
1618 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1622 static void sheevaplug_reset(int trst
, int srst
)
1625 high_output
&= ~nTRST
;
1627 high_output
|= nTRST
;
1630 high_output
&= ~nSRSTnOE
;
1632 high_output
|= nSRSTnOE
;
1634 /* command "set data bits high byte" */
1636 buffer_write(high_output
);
1637 buffer_write(high_direction
);
1638 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1641 static void redbee_reset(int trst
, int srst
)
1645 tap_set_state(TAP_RESET
);
1646 high_output
&= ~nTRST
;
1650 high_output
|= nTRST
;
1655 high_output
&= ~nSRST
;
1659 high_output
|= nSRST
;
1662 /* command "set data bits low byte" */
1664 buffer_write(high_output
);
1665 buffer_write(high_direction
);
1666 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1667 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1671 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1675 int predicted_size
= 0;
1678 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1679 cmd
->cmd
.runtest
->num_cycles
,
1680 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1682 /* only send the maximum buffer size that FT2232C can handle */
1684 if (tap_get_state() != TAP_IDLE
)
1685 predicted_size
+= 3;
1686 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1687 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1688 predicted_size
+= 3;
1689 if (tap_get_end_state() != TAP_IDLE
)
1690 predicted_size
+= 3;
1691 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1693 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1694 retval
= ERROR_JTAG_QUEUE_FAILED
;
1698 if (tap_get_state() != TAP_IDLE
)
1700 move_to_state(TAP_IDLE
);
1703 i
= cmd
->cmd
.runtest
->num_cycles
;
1706 /* there are no state transitions in this code, so omit state tracking */
1708 /* command "Clock Data to TMS/CS Pin (no Read)" */
1712 buffer_write((i
> 7) ? 6 : (i
- 1));
1717 i
-= (i
> 7) ? 7 : i
;
1718 /* LOG_DEBUG("added TMS scan (no read)"); */
1721 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1723 if (tap_get_state() != tap_get_end_state())
1725 move_to_state(tap_get_end_state());
1729 DEBUG_JTAG_IO("runtest: %i, end in %s",
1730 cmd
->cmd
.runtest
->num_cycles
,
1731 tap_state_name(tap_get_end_state()));
1735 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1737 int predicted_size
= 0;
1738 int retval
= ERROR_OK
;
1740 DEBUG_JTAG_IO("statemove end in %s",
1741 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1743 /* only send the maximum buffer size that FT2232C can handle */
1745 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1747 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1748 retval
= ERROR_JTAG_QUEUE_FAILED
;
1752 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1754 /* For TAP_RESET, ignore the current recorded state. It's often
1755 * wrong at server startup, and this transation is critical whenever
1758 if (tap_get_end_state() == TAP_RESET
) {
1759 clock_tms(0x4b, 0xff, 5, 0);
1762 /* shortest-path move to desired end state */
1763 } else if (tap_get_state() != tap_get_end_state())
1765 move_to_state(tap_get_end_state());
1773 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1774 * (or SWD) state machine.
1776 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1778 int retval
= ERROR_OK
;
1779 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1780 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1783 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1785 /* only send the maximum buffer size that FT2232C can handle */
1786 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1787 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1788 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1789 retval
= ERROR_JTAG_QUEUE_FAILED
;
1795 /* Shift out in batches of at most 6 bits; there's a report of an
1796 * FT2232 bug in this area, where shifting exactly 7 bits can make
1797 * problems with TMS signaling for the last clock cycle:
1799 * http://developer.intra2net.com/mailarchive/html/
1800 * libftdi/2009/msg00292.html
1802 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1804 * Note that pathmoves in JTAG are not often seven bits, so that
1805 * isn't a particularly likely situation outside of "special"
1806 * signaling such as switching between JTAG and SWD modes.
1809 if (num_bits
<= 6) {
1811 buffer_write(num_bits
- 1);
1812 buffer_write(*bits
& 0x3f);
1816 /* Yes, this is lazy ... we COULD shift out more data
1817 * bits per operation, but doing it in nybbles is easy
1821 buffer_write(*bits
& 0xf);
1824 count
= (num_bits
> 4) ? 4 : num_bits
;
1827 buffer_write(count
- 1);
1828 buffer_write((*bits
>> 4) & 0xf);
1838 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1840 int predicted_size
= 0;
1841 int retval
= ERROR_OK
;
1843 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1844 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1846 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1847 tap_state_name(tap_get_state()),
1848 tap_state_name(path
[num_states
-1]));
1850 /* only send the maximum buffer size that FT2232C can handle */
1851 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1852 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1854 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1855 retval
= ERROR_JTAG_QUEUE_FAILED
;
1861 ft2232_add_pathmove(path
, num_states
);
1867 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1870 int scan_size
; /* size of IR or DR scan */
1871 int predicted_size
= 0;
1872 int retval
= ERROR_OK
;
1874 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1876 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1878 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1880 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1881 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1883 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1884 /* unsent commands before this */
1885 if (first_unsent
!= cmd
)
1886 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1887 retval
= ERROR_JTAG_QUEUE_FAILED
;
1889 /* current command */
1890 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1891 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1893 first_unsent
= cmd
->next
;
1898 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1900 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1903 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1904 retval
= ERROR_JTAG_QUEUE_FAILED
;
1908 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1909 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1910 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1911 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1915 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1916 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1917 tap_state_name(tap_get_end_state()));
1922 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1925 int predicted_size
= 0;
1928 DEBUG_JTAG_IO("reset trst: %i srst %i",
1929 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1931 /* only send the maximum buffer size that FT2232C can handle */
1933 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1935 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1936 retval
= ERROR_JTAG_QUEUE_FAILED
;
1941 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1943 tap_set_state(TAP_RESET
);
1946 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1949 DEBUG_JTAG_IO("trst: %i, srst: %i",
1950 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1954 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1959 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1961 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1962 retval
= ERROR_JTAG_QUEUE_FAILED
;
1963 first_unsent
= cmd
->next
;
1964 jtag_sleep(cmd
->cmd
.sleep
->us
);
1965 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1967 tap_state_name(tap_get_state()));
1971 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1976 /* this is only allowed while in a stable state. A check for a stable
1977 * state was done in jtag_add_clocks()
1979 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1980 retval
= ERROR_JTAG_QUEUE_FAILED
;
1981 DEBUG_JTAG_IO("clocks %i while in %s",
1982 cmd
->cmd
.stableclocks
->num_cycles
,
1983 tap_state_name(tap_get_state()));
1987 static int ft2232_execute_command(struct jtag_command
*cmd
)
1993 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1994 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1995 case JTAG_TLR_RESET
: retval
= ft2232_execute_statemove(cmd
); break;
1996 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1997 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1998 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1999 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
2001 retval
= ft2232_execute_tms(cmd
);
2004 LOG_ERROR("BUG: unknown JTAG command type encountered");
2005 retval
= ERROR_JTAG_QUEUE_FAILED
;
2011 static int ft2232_execute_queue(void)
2013 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
2016 first_unsent
= cmd
; /* next command that has to be sent */
2019 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2020 * that wasn't handled by a caller-provided error handler
2024 ft2232_buffer_size
= 0;
2025 ft2232_expect_read
= 0;
2027 /* blink, if the current layout has that feature */
2033 if (ft2232_execute_command(cmd
) != ERROR_OK
)
2034 retval
= ERROR_JTAG_QUEUE_FAILED
;
2035 /* Start reading input before FT2232 TX buffer fills up */
2037 if (ft2232_expect_read
> 256)
2039 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2040 retval
= ERROR_JTAG_QUEUE_FAILED
;
2045 if (require_send
> 0)
2046 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2047 retval
= ERROR_JTAG_QUEUE_FAILED
;
2052 #if BUILD_FT2232_FTD2XX == 1
2053 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2057 char SerialNumber
[16];
2058 char Description
[64];
2059 DWORD openex_flags
= 0;
2060 char* openex_string
= NULL
;
2061 uint8_t latency_timer
;
2063 if (layout
== NULL
) {
2064 LOG_WARNING("No ft2232 layout specified'");
2065 return ERROR_JTAG_INIT_FAILED
;
2068 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2071 /* Add non-standard Vid/Pid to the linux driver */
2072 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2074 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2078 if (ft2232_device_desc
&& ft2232_serial
)
2080 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2081 ft2232_device_desc
= NULL
;
2084 if (ft2232_device_desc
)
2086 openex_string
= ft2232_device_desc
;
2087 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2089 else if (ft2232_serial
)
2091 openex_string
= ft2232_serial
;
2092 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2096 LOG_ERROR("neither device description nor serial number specified");
2097 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2099 return ERROR_JTAG_INIT_FAILED
;
2102 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2103 if (status
!= FT_OK
) {
2104 /* under Win32, the FTD2XX driver appends an "A" to the end
2105 * of the description, if we tried by the desc, then
2106 * try by the alternate "A" description. */
2107 if (openex_string
== ft2232_device_desc
) {
2108 /* Try the alternate method. */
2109 openex_string
= ft2232_device_desc_A
;
2110 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2111 if (status
== FT_OK
) {
2112 /* yea, the "alternate" method worked! */
2114 /* drat, give the user a meaningfull message.
2115 * telling the use we tried *BOTH* methods. */
2116 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2118 ft2232_device_desc_A
);
2123 if (status
!= FT_OK
)
2129 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2131 return ERROR_JTAG_INIT_FAILED
;
2133 LOG_ERROR("unable to open ftdi device: %lu", status
);
2134 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2135 if (status
== FT_OK
)
2137 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2140 for (i
= 0; i
< num_devices
; i
++)
2141 desc_array
[i
] = malloc(64);
2143 desc_array
[num_devices
] = NULL
;
2145 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2147 if (status
== FT_OK
)
2149 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2150 for (i
= 0; i
< num_devices
; i
++)
2151 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2154 for (i
= 0; i
< num_devices
; i
++)
2155 free(desc_array
[i
]);
2161 LOG_ERROR("ListDevices: NONE\n");
2163 return ERROR_JTAG_INIT_FAILED
;
2166 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2168 LOG_ERROR("unable to set latency timer: %lu", status
);
2169 return ERROR_JTAG_INIT_FAILED
;
2172 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2174 LOG_ERROR("unable to get latency timer: %lu", status
);
2175 return ERROR_JTAG_INIT_FAILED
;
2179 LOG_DEBUG("current latency timer: %i", latency_timer
);
2182 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2184 LOG_ERROR("unable to set timeouts: %lu", status
);
2185 return ERROR_JTAG_INIT_FAILED
;
2188 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2190 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2191 return ERROR_JTAG_INIT_FAILED
;
2194 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2196 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2197 return ERROR_JTAG_INIT_FAILED
;
2201 static const char* type_str
[] =
2202 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2203 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2204 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2205 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2206 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2207 LOG_INFO("deviceID: %lu", deviceID
);
2208 LOG_INFO("SerialNumber: %s", SerialNumber
);
2209 LOG_INFO("Description: %s", Description
);
2215 static int ft2232_purge_ftd2xx(void)
2219 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2221 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2222 return ERROR_JTAG_INIT_FAILED
;
2228 #endif /* BUILD_FT2232_FTD2XX == 1 */
2230 #if BUILD_FT2232_LIBFTDI == 1
2231 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2233 uint8_t latency_timer
;
2235 if (layout
== NULL
) {
2236 LOG_WARNING("No ft2232 layout specified'");
2237 return ERROR_JTAG_INIT_FAILED
;
2240 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2241 layout
->name
, vid
, pid
);
2243 if (ftdi_init(&ftdic
) < 0)
2244 return ERROR_JTAG_INIT_FAILED
;
2246 /* default to INTERFACE_A */
2247 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2249 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2251 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2252 return ERROR_JTAG_INIT_FAILED
;
2255 /* context, vendor id, product id */
2256 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2260 LOG_WARNING("unable to open ftdi device (trying more): %s",
2263 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2265 return ERROR_JTAG_INIT_FAILED
;
2268 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2269 if (ftdi_usb_reset(&ftdic
) < 0)
2271 LOG_ERROR("unable to reset ftdi device");
2272 return ERROR_JTAG_INIT_FAILED
;
2275 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2277 LOG_ERROR("unable to set latency timer");
2278 return ERROR_JTAG_INIT_FAILED
;
2281 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2283 LOG_ERROR("unable to get latency timer");
2284 return ERROR_JTAG_INIT_FAILED
;
2288 LOG_DEBUG("current latency timer: %i", latency_timer
);
2291 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2293 ftdi_device
= ftdic
.type
;
2294 static const char* type_str
[] =
2295 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2296 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2297 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2298 ? ftdi_device
: no_of_known_types
;
2299 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2303 static int ft2232_purge_libftdi(void)
2305 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2307 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2308 return ERROR_JTAG_INIT_FAILED
;
2314 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2316 static int ft2232_init(void)
2320 uint32_t bytes_written
;
2322 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2324 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2328 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2331 if (layout
== NULL
) {
2332 LOG_WARNING("No ft2232 layout specified'");
2333 return ERROR_JTAG_INIT_FAILED
;
2336 for (int i
= 0; 1; i
++)
2339 * "more indicates that there are more IDs to try, so we should
2340 * not print an error for an ID mismatch (but for anything
2343 * try_more indicates that the error code returned indicates an
2344 * ID mismatch (and nothing else) and that we should proceeed
2345 * with the next ID pair.
2347 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2350 #if BUILD_FT2232_FTD2XX == 1
2351 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2353 #elif BUILD_FT2232_LIBFTDI == 1
2354 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2355 more
, &try_more
, layout
->channel
);
2359 if (!more
|| !try_more
)
2363 ft2232_buffer_size
= 0;
2364 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2366 if (layout
->init() != ERROR_OK
)
2367 return ERROR_JTAG_INIT_FAILED
;
2369 if (ft2232_device_is_highspeed())
2371 #ifndef BUILD_FT2232_HIGHSPEED
2372 #if BUILD_FT2232_FTD2XX == 1
2373 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2374 #elif BUILD_FT2232_LIBFTDI == 1
2375 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2378 /* make sure the legacy mode is disabled */
2379 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2380 return ERROR_JTAG_INIT_FAILED
;
2383 ft2232_speed(jtag_get_speed());
2385 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2386 if ((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
)
2388 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2389 return ERROR_JTAG_INIT_FAILED
;
2392 #if BUILD_FT2232_FTD2XX == 1
2393 return ft2232_purge_ftd2xx();
2394 #elif BUILD_FT2232_LIBFTDI == 1
2395 return ft2232_purge_libftdi();
2401 /** Updates defaults for DBUS signals: the four JTAG signals
2402 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2404 static inline void ftx232_dbus_init(void)
2407 low_direction
= 0x0b;
2410 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2411 * the four GPIOL signals. Initialization covers value and direction,
2412 * as customized for each layout.
2414 static int ftx232_dbus_write(void)
2417 uint32_t bytes_written
;
2419 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2420 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2422 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2423 low_output
&= ~nTRST
; /* nTRST = 0 */
2427 low_direction
|= nTRSTnOE
; /* nTRST output */
2428 low_output
|= nTRST
; /* nTRST = 1 */
2431 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2433 low_direction
|= nSRSTnOE
; /* nSRST output */
2434 low_output
|= nSRST
; /* nSRST = 1 */
2438 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2439 low_output
&= ~nSRST
; /* nSRST = 0 */
2442 /* initialize low byte for jtag */
2443 buf
[0] = 0x80; /* command "set data bits low byte" */
2444 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2445 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2446 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2448 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2450 LOG_ERROR("couldn't initialize FT2232 DBUS");
2451 return ERROR_JTAG_INIT_FAILED
;
2457 static int usbjtag_init(void)
2460 * NOTE: This is now _specific_ to the "usbjtag" layout.
2461 * Don't try cram any more layouts into this.
2470 return ftx232_dbus_write();
2473 static int lm3s811_jtag_init(void)
2477 /* There are multiple revisions of LM3S811 eval boards:
2478 * - Rev B (and older?) boards have no SWO trace support.
2479 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2480 * they should use the "luminary_icdi" layout instead.
2487 low_direction
= 0x8b;
2489 return ftx232_dbus_write();
2492 static int icdi_jtag_init(void)
2496 /* Most Luminary eval boards support SWO trace output,
2497 * and should use this "luminary_icdi" layout.
2499 * DBUS 0..3 are used for JTAG as usual. GPIOs are used
2500 * to switch between JTAG and SWD, or switch the ft2232 UART
2501 * between (i) the target UART or (ii) SWO trace data.
2503 * We come up in JTAG mode and may switch to SWD later (with
2504 * SWO/trace option if SWD is active).
2511 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2512 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2513 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2514 #define ICDI_TDI (1 << 2) /* ADBUS 2 (INPUT) */
2516 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2517 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2524 low_output
= 0x08 | ICDI_JTAG_EN
;
2525 low_direction
= 0xcb | ICDI_JTAG_EN
;
2527 return ftx232_dbus_write();
2530 static int signalyzer_init(void)
2538 return ftx232_dbus_write();
2541 static int axm0432_jtag_init(void)
2544 uint32_t bytes_written
;
2547 low_direction
= 0x2b;
2549 /* initialize low byte for jtag */
2550 buf
[0] = 0x80; /* command "set data bits low byte" */
2551 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2552 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2553 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2555 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2557 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2558 return ERROR_JTAG_INIT_FAILED
;
2561 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2564 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2566 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2570 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2575 high_direction
= 0x0c;
2577 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2578 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2580 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2584 high_output
|= nTRST
;
2587 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2589 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2593 high_output
|= nSRST
;
2596 /* initialize high port */
2597 buf
[0] = 0x82; /* command "set data bits high byte" */
2598 buf
[1] = high_output
; /* value */
2599 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2600 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2602 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2604 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2605 return ERROR_JTAG_INIT_FAILED
;
2611 static int redbee_init(void)
2614 uint32_t bytes_written
;
2617 low_direction
= 0x2b;
2619 /* initialize low byte for jtag */
2620 /* command "set data bits low byte" */
2622 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2623 buf
[2] = low_direction
;
2624 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2625 buf
[1] = low_output
;
2626 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2628 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2630 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2631 return ERROR_JTAG_INIT_FAILED
;
2635 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2637 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2640 high_direction
= 0x0c;
2642 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2643 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2645 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2649 high_output
|= nTRST
;
2652 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2654 LOG_ERROR("can't set nSRST to push-pull on redbee");
2658 high_output
|= nSRST
;
2661 /* initialize high port */
2662 buf
[0] = 0x82; /* command "set data bits high byte" */
2663 buf
[1] = high_output
; /* value */
2664 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2665 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2667 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2669 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2670 return ERROR_JTAG_INIT_FAILED
;
2676 static int jtagkey_init(void)
2679 uint32_t bytes_written
;
2682 low_direction
= 0x1b;
2684 /* initialize low byte for jtag */
2685 buf
[0] = 0x80; /* command "set data bits low byte" */
2686 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2687 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2688 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2690 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2692 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2693 return ERROR_JTAG_INIT_FAILED
;
2696 if (strcmp(layout
->name
, "jtagkey") == 0)
2703 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2704 || (strcmp(layout
->name
, "oocdlink") == 0))
2713 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2718 high_direction
= 0x0f;
2720 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2721 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2723 high_output
|= nTRSTnOE
;
2724 high_output
&= ~nTRST
;
2728 high_output
&= ~nTRSTnOE
;
2729 high_output
|= nTRST
;
2732 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2734 high_output
&= ~nSRSTnOE
;
2735 high_output
|= nSRST
;
2739 high_output
|= nSRSTnOE
;
2740 high_output
&= ~nSRST
;
2743 /* initialize high port */
2744 buf
[0] = 0x82; /* command "set data bits high byte" */
2745 buf
[1] = high_output
; /* value */
2746 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2747 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2749 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2751 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2752 return ERROR_JTAG_INIT_FAILED
;
2758 static int olimex_jtag_init(void)
2761 uint32_t bytes_written
;
2764 low_direction
= 0x1b;
2766 /* initialize low byte for jtag */
2767 buf
[0] = 0x80; /* command "set data bits low byte" */
2768 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2769 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2770 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2772 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2774 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2775 return ERROR_JTAG_INIT_FAILED
;
2781 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2784 high_direction
= 0x0f;
2786 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2787 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2789 high_output
|= nTRSTnOE
;
2790 high_output
&= ~nTRST
;
2794 high_output
&= ~nTRSTnOE
;
2795 high_output
|= nTRST
;
2798 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2800 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2804 high_output
&= ~nSRST
;
2807 /* turn red LED on */
2808 high_output
|= 0x08;
2810 /* initialize high port */
2811 buf
[0] = 0x82; /* command "set data bits high byte" */
2812 buf
[1] = high_output
; /* value */
2813 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2814 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2816 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2818 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2819 return ERROR_JTAG_INIT_FAILED
;
2825 static int flyswatter_init(void)
2828 uint32_t bytes_written
;
2831 low_direction
= 0xfb;
2833 /* initialize low byte for jtag */
2834 buf
[0] = 0x80; /* command "set data bits low byte" */
2835 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2836 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2837 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2839 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2841 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2842 return ERROR_JTAG_INIT_FAILED
;
2846 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2848 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2851 high_direction
= 0x0c;
2853 /* turn red LED3 on, LED2 off */
2854 high_output
|= 0x08;
2856 /* initialize high port */
2857 buf
[0] = 0x82; /* command "set data bits high byte" */
2858 buf
[1] = high_output
; /* value */
2859 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2860 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2862 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2864 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2865 return ERROR_JTAG_INIT_FAILED
;
2871 static int turtle_init(void)
2874 uint32_t bytes_written
;
2877 low_direction
= 0x5b;
2879 /* initialize low byte for jtag */
2880 buf
[0] = 0x80; /* command "set data bits low byte" */
2881 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2882 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2883 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2885 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2887 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2888 return ERROR_JTAG_INIT_FAILED
;
2894 high_direction
= 0x0C;
2896 /* initialize high port */
2897 buf
[0] = 0x82; /* command "set data bits high byte" */
2898 buf
[1] = high_output
;
2899 buf
[2] = high_direction
;
2900 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2902 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2904 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2905 return ERROR_JTAG_INIT_FAILED
;
2911 static int comstick_init(void)
2914 uint32_t bytes_written
;
2917 low_direction
= 0x0b;
2919 /* initialize low byte for jtag */
2920 buf
[0] = 0x80; /* command "set data bits low byte" */
2921 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2922 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2923 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2925 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2927 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2928 return ERROR_JTAG_INIT_FAILED
;
2932 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2934 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2937 high_direction
= 0x03;
2939 /* initialize high port */
2940 buf
[0] = 0x82; /* command "set data bits high byte" */
2941 buf
[1] = high_output
;
2942 buf
[2] = high_direction
;
2943 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2945 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2947 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2948 return ERROR_JTAG_INIT_FAILED
;
2954 static int stm32stick_init(void)
2957 uint32_t bytes_written
;
2960 low_direction
= 0x8b;
2962 /* initialize low byte for jtag */
2963 buf
[0] = 0x80; /* command "set data bits low byte" */
2964 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2965 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2966 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2968 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2970 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2971 return ERROR_JTAG_INIT_FAILED
;
2975 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2977 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2980 high_direction
= 0x03;
2982 /* initialize high port */
2983 buf
[0] = 0x82; /* command "set data bits high byte" */
2984 buf
[1] = high_output
;
2985 buf
[2] = high_direction
;
2986 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2988 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
2990 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2991 return ERROR_JTAG_INIT_FAILED
;
2997 static int sheevaplug_init(void)
3000 uint32_t bytes_written
;
3003 low_direction
= 0x1b;
3005 /* initialize low byte for jtag */
3006 buf
[0] = 0x80; /* command "set data bits low byte" */
3007 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3008 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3009 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3011 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3013 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3014 return ERROR_JTAG_INIT_FAILED
;
3023 high_direction
= 0x0f;
3025 /* nTRST is always push-pull */
3026 high_output
&= ~nTRSTnOE
;
3027 high_output
|= nTRST
;
3029 /* nSRST is always open-drain */
3030 high_output
|= nSRSTnOE
;
3031 high_output
&= ~nSRST
;
3033 /* initialize high port */
3034 buf
[0] = 0x82; /* command "set data bits high byte" */
3035 buf
[1] = high_output
; /* value */
3036 buf
[2] = high_direction
; /* all outputs - xRST */
3037 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3039 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3041 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3042 return ERROR_JTAG_INIT_FAILED
;
3048 static int cortino_jtag_init(void)
3051 uint32_t bytes_written
;
3054 low_direction
= 0x1b;
3056 /* initialize low byte for jtag */
3057 buf
[0] = 0x80; /* command "set data bits low byte" */
3058 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3059 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3060 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3062 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3064 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3065 return ERROR_JTAG_INIT_FAILED
;
3069 nTRSTnOE
= 0x00; /* no output enable for nTRST */
3071 nSRSTnOE
= 0x00; /* no output enable for nSRST */
3074 high_direction
= 0x03;
3076 /* initialize high port */
3077 buf
[0] = 0x82; /* command "set data bits high byte" */
3078 buf
[1] = high_output
;
3079 buf
[2] = high_direction
;
3080 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3082 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
3084 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3085 return ERROR_JTAG_INIT_FAILED
;
3091 static void olimex_jtag_blink(void)
3093 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3094 * ACBUS3 is bit 3 of the GPIOH port
3096 if (high_output
& 0x08)
3098 /* set port pin high */
3099 high_output
&= 0x07;
3103 /* set port pin low */
3104 high_output
|= 0x08;
3108 buffer_write(high_output
);
3109 buffer_write(high_direction
);
3112 static void flyswatter_jtag_blink(void)
3115 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3117 high_output
^= 0x0c;
3120 buffer_write(high_output
);
3121 buffer_write(high_direction
);
3124 static void turtle_jtag_blink(void)
3127 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3129 if (high_output
& 0x08)
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
)
3167 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3168 cp
= strchr(ft2232_device_desc
, 0);
3169 /* under Win32, the FTD2XX driver appends an "A" to the end
3170 * of the description, this examines the given desc
3171 * and creates the 'missing' _A or non_A variable. */
3172 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3173 /* it was, so make this the "A" version. */
3174 ft2232_device_desc_A
= ft2232_device_desc
;
3175 /* and *CREATE* the non-A version. */
3176 strcpy(buf
, ft2232_device_desc
);
3177 cp
= strchr(buf
, 0);
3179 ft2232_device_desc
= strdup(buf
);
3181 /* <space > A not defined
3183 sprintf(buf
, "%s A", ft2232_device_desc
);
3184 ft2232_device_desc_A
= strdup(buf
);
3189 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3195 COMMAND_HANDLER(ft2232_handle_serial_command
)
3199 ft2232_serial
= strdup(CMD_ARGV
[0]);
3203 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3209 COMMAND_HANDLER(ft2232_handle_layout_command
)
3211 if (CMD_ARGC
!= 1) {
3212 LOG_ERROR("Need exactly one argument to ft2232_layout");
3217 LOG_ERROR("already specified ft2232_layout %s",
3219 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3224 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3225 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3231 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3235 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3237 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3239 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3240 "(maximum is %d pairs)", MAX_USB_IDS
);
3241 CMD_ARGC
= MAX_USB_IDS
* 2;
3243 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3245 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3247 return ERROR_COMMAND_SYNTAX_ERROR
;
3248 /* remove the incomplete trailing id */
3253 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3255 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3256 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3260 * Explicitly terminate, in case there are multiples instances of
3263 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3268 COMMAND_HANDLER(ft2232_handle_latency_command
)
3272 ft2232_latency
= atoi(CMD_ARGV
[0]);
3276 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3282 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3286 /* 7 bits of either ones or zeros. */
3287 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3289 while (num_cycles
> 0)
3291 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3292 * at most 7 bits per invocation. Here we invoke it potentially
3295 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3297 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3299 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3300 retval
= ERROR_JTAG_QUEUE_FAILED
;
3305 /* there are no state transitions in this code, so omit state tracking */
3307 /* command "Clock Data to TMS/CS Pin (no Read)" */
3311 buffer_write(bitcount_per_command
- 1);
3313 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3318 num_cycles
-= bitcount_per_command
;
3324 /* ---------------------------------------------------------------------
3325 * Support for IceBear JTAG adapter from Section5:
3326 * http://section5.ch/icebear
3328 * Author: Sten, debian@sansys-electronic.com
3331 /* Icebear pin layout
3333 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3334 * GND GND | 4 3| n.c.
3335 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3336 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3337 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3338 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3339 * ADBUS2 TDO |14 13| GND GND
3341 * ADBUS0 O L TCK ACBUS0 GND
3342 * ADBUS1 O L TDI ACBUS1 GND
3343 * ADBUS2 I TDO ACBUS2 n.c.
3344 * ADBUS3 O H TMS ACBUS3 n.c.
3350 static int icebear_jtag_init(void) {
3352 uint32_t bytes_written
;
3354 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3355 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3359 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3360 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3361 low_direction
&= ~nTRST
; /* nTRST high impedance */
3364 low_direction
|= nTRST
;
3365 low_output
|= nTRST
;
3368 low_direction
|= nSRST
;
3369 low_output
|= nSRST
;
3371 /* initialize low byte for jtag */
3372 buf
[0] = 0x80; /* command "set data bits low byte" */
3373 buf
[1] = low_output
;
3374 buf
[2] = low_direction
;
3375 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3377 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3378 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3379 return ERROR_JTAG_INIT_FAILED
;
3383 high_direction
= 0x00;
3386 /* initialize high port */
3387 buf
[0] = 0x82; /* command "set data bits high byte" */
3388 buf
[1] = high_output
; /* value */
3389 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3390 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3392 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
) {
3393 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3394 return ERROR_JTAG_INIT_FAILED
;
3400 static void icebear_jtag_reset(int trst
, int srst
) {
3403 low_direction
|= nTRST
;
3404 low_output
&= ~nTRST
;
3406 else if (trst
== 0) {
3407 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3408 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3409 low_direction
&= ~nTRST
;
3411 low_output
|= nTRST
;
3415 low_output
&= ~nSRST
;
3417 else if (srst
== 0) {
3418 low_output
|= nSRST
;
3421 /* command "set data bits low byte" */
3423 buffer_write(low_output
);
3424 buffer_write(low_direction
);
3426 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3429 /* ---------------------------------------------------------------------
3430 * Support for Signalyzer H2 and Signalyzer H4
3431 * JTAG adapter from Xverve Technologies Inc.
3432 * http://www.signalyzer.com or http://www.xverve.com
3434 * Author: Oleg Seiljus, oleg@signalyzer.com
3436 static unsigned char signalyzer_h_side
;
3437 static unsigned int signalyzer_h_adapter_type
;
3439 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3441 #if BUILD_FT2232_FTD2XX == 1
3442 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3445 #define SIGNALYZER_COMMAND_ADDR 128
3446 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3448 #define SIGNALYZER_COMMAND_VERSION 0x41
3449 #define SIGNALYZER_COMMAND_RESET 0x42
3450 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3451 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3452 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3453 #define SIGNALYZER_COMMAND_LED_SET 0x53
3454 #define SIGNALYZER_COMMAND_ADC 0x54
3455 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3456 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3457 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3458 #define SIGNALYZER_COMMAND_I2C 0x58
3460 #define SIGNALYZER_CHAN_A 1
3461 #define SIGNALYZER_CHAN_B 2
3462 /* LEDS use channel C */
3463 #define SIGNALYZER_CHAN_C 4
3465 #define SIGNALYZER_LED_GREEN 1
3466 #define SIGNALYZER_LED_RED 2
3468 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3469 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3470 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3471 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3472 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3475 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3477 #if BUILD_FT2232_FTD2XX == 1
3478 return FT_WriteEE(ftdih
, address
, value
);
3479 #elif BUILD_FT2232_LIBFTDI == 1
3484 #if BUILD_FT2232_FTD2XX == 1
3485 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3487 return FT_ReadEE(ftdih
, address
, value
);
3491 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3492 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3494 unsigned char on_time
;
3495 unsigned char off_time
;
3497 if (on_time_ms
< 0xFFFF)
3498 on_time
= (unsigned char)(on_time_ms
/ 62);
3502 off_time
= (unsigned char)(off_time_ms
/ 62);
3504 #if BUILD_FT2232_FTD2XX == 1
3507 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3508 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3510 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3511 return ERROR_JTAG_DEVICE_ERROR
;
3514 if ((status
= signalyzer_h_ctrl_write(
3515 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3516 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3518 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3519 return ERROR_JTAG_DEVICE_ERROR
;
3522 if ((status
= signalyzer_h_ctrl_write(
3523 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3524 ((uint32_t)cycles
))) != FT_OK
)
3526 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3527 return ERROR_JTAG_DEVICE_ERROR
;
3530 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3531 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3533 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3534 return ERROR_JTAG_DEVICE_ERROR
;
3538 #elif BUILD_FT2232_LIBFTDI == 1
3541 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3542 ((uint32_t)(channel
<< 8) | led
))) < 0)
3544 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3545 ftdi_get_error_string(&ftdic
));
3546 return ERROR_JTAG_DEVICE_ERROR
;
3549 if ((retval
= signalyzer_h_ctrl_write(
3550 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3551 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3553 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3554 ftdi_get_error_string(&ftdic
));
3555 return ERROR_JTAG_DEVICE_ERROR
;
3558 if ((retval
= signalyzer_h_ctrl_write(
3559 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3560 (uint32_t)cycles
)) < 0)
3562 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3563 ftdi_get_error_string(&ftdic
));
3564 return ERROR_JTAG_DEVICE_ERROR
;
3567 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3568 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3570 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3571 ftdi_get_error_string(&ftdic
));
3572 return ERROR_JTAG_DEVICE_ERROR
;
3579 static int signalyzer_h_init(void)
3581 #if BUILD_FT2232_FTD2XX == 1
3588 uint16_t read_buf
[12] = { 0 };
3590 uint32_t bytes_written
;
3592 /* turn on center green led */
3593 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3594 0xFFFF, 0x00, 0x00);
3596 /* determine what channel config wants to open
3597 * TODO: change me... current implementation is made to work
3598 * with openocd description parsing.
3600 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3604 signalyzer_h_side
= *(end_of_desc
- 1);
3605 if (signalyzer_h_side
== 'B')
3606 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3608 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3612 LOG_ERROR("No Channel was specified");
3616 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3619 #if BUILD_FT2232_FTD2XX == 1
3620 /* read signalyzer versionining information */
3621 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3622 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3624 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3625 return ERROR_JTAG_DEVICE_ERROR
;
3628 for (i
= 0; i
< 10; i
++)
3630 if ((status
= signalyzer_h_ctrl_read(
3631 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3632 &read_buf
[i
])) != FT_OK
)
3634 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3636 return ERROR_JTAG_DEVICE_ERROR
;
3640 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3641 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3642 read_buf
[4], read_buf
[5], read_buf
[6]);
3644 /* set gpio register */
3645 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3646 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3648 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3649 return ERROR_JTAG_DEVICE_ERROR
;
3652 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3655 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3656 return ERROR_JTAG_DEVICE_ERROR
;
3659 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3660 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3662 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3663 return ERROR_JTAG_DEVICE_ERROR
;
3666 /* read adapter type information */
3667 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3668 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3670 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3671 return ERROR_JTAG_DEVICE_ERROR
;
3674 if ((status
= signalyzer_h_ctrl_write(
3675 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3677 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3678 return ERROR_JTAG_DEVICE_ERROR
;
3681 if ((status
= signalyzer_h_ctrl_write(
3682 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3684 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3685 return ERROR_JTAG_DEVICE_ERROR
;
3688 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3689 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3691 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3692 return ERROR_JTAG_DEVICE_ERROR
;
3697 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3698 &read_buf
[0])) != FT_OK
)
3700 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3701 return ERROR_JTAG_DEVICE_ERROR
;
3704 if (read_buf
[0] != 0x0498)
3705 signalyzer_h_adapter_type
= 0x0000;
3708 for (i
= 0; i
< 4; i
++)
3710 if ((status
= signalyzer_h_ctrl_read(
3711 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3712 &read_buf
[i
])) != FT_OK
)
3714 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3716 return ERROR_JTAG_DEVICE_ERROR
;
3720 signalyzer_h_adapter_type
= read_buf
[0];
3723 #elif BUILD_FT2232_LIBFTDI == 1
3724 /* currently libftdi does not allow reading individual eeprom
3725 * locations, therefore adapter type cannot be detected.
3726 * override with most common type
3728 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3731 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3733 /* ADAPTOR: EM_LT16_A */
3734 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3736 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3737 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3745 low_direction
= 0x1b;
3748 high_direction
= 0x0;
3750 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3752 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3753 low_output
&= ~nTRST
; /* nTRST = 0 */
3757 low_direction
|= nTRSTnOE
; /* nTRST output */
3758 low_output
|= nTRST
; /* nTRST = 1 */
3761 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3763 low_direction
|= nSRSTnOE
; /* nSRST output */
3764 low_output
|= nSRST
; /* nSRST = 1 */
3768 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3769 low_output
&= ~nSRST
; /* nSRST = 0 */
3772 #if BUILD_FT2232_FTD2XX == 1
3773 /* enable power to the module */
3774 if ((status
= signalyzer_h_ctrl_write(
3775 SIGNALYZER_DATA_BUFFER_ADDR
,
3776 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3779 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3781 return ERROR_JTAG_DEVICE_ERROR
;
3784 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3785 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3787 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3789 return ERROR_JTAG_DEVICE_ERROR
;
3792 /* set gpio mode register */
3793 if ((status
= signalyzer_h_ctrl_write(
3794 SIGNALYZER_DATA_BUFFER_ADDR
,
3795 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3797 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3799 return ERROR_JTAG_DEVICE_ERROR
;
3802 if ((status
= signalyzer_h_ctrl_write(
3803 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3806 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3808 return ERROR_JTAG_DEVICE_ERROR
;
3811 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3812 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3814 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3816 return ERROR_JTAG_DEVICE_ERROR
;
3819 /* set gpio register */
3820 if ((status
= signalyzer_h_ctrl_write(
3821 SIGNALYZER_DATA_BUFFER_ADDR
,
3822 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3824 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3826 return ERROR_JTAG_DEVICE_ERROR
;
3829 if ((status
= signalyzer_h_ctrl_write(
3830 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3833 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3835 return ERROR_JTAG_DEVICE_ERROR
;
3838 if ((status
= signalyzer_h_ctrl_write(
3839 SIGNALYZER_COMMAND_ADDR
,
3840 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3842 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3844 return ERROR_JTAG_DEVICE_ERROR
;
3849 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3850 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3851 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3852 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3853 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3855 if (signalyzer_h_adapter_type
3856 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3857 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3858 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3859 else if (signalyzer_h_adapter_type
3860 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3861 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3862 "(ARM JTAG with PSU) detected. (HW: %2x).",
3863 (read_buf
[1] >> 8));
3864 else if (signalyzer_h_adapter_type
3865 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3866 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3867 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3868 else if (signalyzer_h_adapter_type
3869 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3870 LOG_INFO("Signalyzer: EM-JTAG-P "
3871 "(Generic JTAG with PSU) detected. (HW: %2x).",
3872 (read_buf
[1] >> 8));
3880 low_direction
= 0x1b;
3883 high_direction
= 0x1f;
3885 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3887 high_output
|= nTRSTnOE
;
3888 high_output
&= ~nTRST
;
3892 high_output
&= ~nTRSTnOE
;
3893 high_output
|= nTRST
;
3896 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3898 high_output
&= ~nSRSTnOE
;
3899 high_output
|= nSRST
;
3903 high_output
|= nSRSTnOE
;
3904 high_output
&= ~nSRST
;
3907 #if BUILD_FT2232_FTD2XX == 1
3908 /* enable power to the module */
3909 if ((status
= signalyzer_h_ctrl_write(
3910 SIGNALYZER_DATA_BUFFER_ADDR
,
3911 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3914 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3916 return ERROR_JTAG_DEVICE_ERROR
;
3919 if ((status
= signalyzer_h_ctrl_write(
3920 SIGNALYZER_COMMAND_ADDR
,
3921 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3923 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3925 return ERROR_JTAG_DEVICE_ERROR
;
3928 /* set gpio mode register (IO_16 and IO_17 set as analog
3929 * inputs, other is gpio)
3931 if ((status
= signalyzer_h_ctrl_write(
3932 SIGNALYZER_DATA_BUFFER_ADDR
,
3933 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3935 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3937 return ERROR_JTAG_DEVICE_ERROR
;
3940 if ((status
= signalyzer_h_ctrl_write(
3941 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3944 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3946 return ERROR_JTAG_DEVICE_ERROR
;
3949 if ((status
= signalyzer_h_ctrl_write(
3950 SIGNALYZER_COMMAND_ADDR
,
3951 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3953 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3955 return ERROR_JTAG_DEVICE_ERROR
;
3958 /* set gpio register (all inputs, for -P modules,
3959 * PSU will be turned off)
3961 if ((status
= signalyzer_h_ctrl_write(
3962 SIGNALYZER_DATA_BUFFER_ADDR
,
3963 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3965 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3967 return ERROR_JTAG_DEVICE_ERROR
;
3970 if ((status
= signalyzer_h_ctrl_write(
3971 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3974 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3976 return ERROR_JTAG_DEVICE_ERROR
;
3979 if ((status
= signalyzer_h_ctrl_write(
3980 SIGNALYZER_COMMAND_ADDR
,
3981 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3983 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3985 return ERROR_JTAG_DEVICE_ERROR
;
3990 else if (signalyzer_h_adapter_type
== 0x0000)
3992 LOG_INFO("Signalyzer: No external modules were detected.");
4000 low_direction
= 0x1b;
4003 high_direction
= 0x0;
4005 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4007 low_direction
&= ~nTRSTnOE
; /* nTRST input */
4008 low_output
&= ~nTRST
; /* nTRST = 0 */
4012 low_direction
|= nTRSTnOE
; /* nTRST output */
4013 low_output
|= nTRST
; /* nTRST = 1 */
4016 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4018 low_direction
|= nSRSTnOE
; /* nSRST output */
4019 low_output
|= nSRST
; /* nSRST = 1 */
4023 low_direction
&= ~nSRSTnOE
; /* nSRST input */
4024 low_output
&= ~nSRST
; /* nSRST = 0 */
4029 LOG_ERROR("Unknown module type is detected: %.4x",
4030 signalyzer_h_adapter_type
);
4031 return ERROR_JTAG_DEVICE_ERROR
;
4034 /* initialize low byte of controller for jtag operation */
4036 buf
[1] = low_output
;
4037 buf
[2] = low_direction
;
4039 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4041 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4042 return ERROR_JTAG_INIT_FAILED
;
4045 #if BUILD_FT2232_FTD2XX == 1
4046 if (ftdi_device
== FT_DEVICE_2232H
)
4048 /* initialize high byte of controller for jtag operation */
4050 buf
[1] = high_output
;
4051 buf
[2] = high_direction
;
4053 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4055 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4056 return ERROR_JTAG_INIT_FAILED
;
4059 #elif BUILD_FT2232_LIBFTDI == 1
4060 if (ftdi_device
== TYPE_2232H
)
4062 /* initialize high byte of controller for jtag operation */
4064 buf
[1] = high_output
;
4065 buf
[2] = high_direction
;
4067 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4069 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4070 return ERROR_JTAG_INIT_FAILED
;
4077 static void signalyzer_h_reset(int trst
, int srst
)
4079 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4081 /* ADAPTOR: EM_LT16_A */
4082 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4086 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4087 /* switch to output pin (output is low) */
4088 low_direction
|= nTRSTnOE
;
4090 /* switch output low */
4091 low_output
&= ~nTRST
;
4095 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4096 /* switch to input pin (high-Z + internal
4097 * and external pullup) */
4098 low_direction
&= ~nTRSTnOE
;
4100 /* switch output high */
4101 low_output
|= nTRST
;
4106 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4107 /* switch output low */
4108 low_output
&= ~nSRST
;
4110 /* switch to output pin (output is low) */
4111 low_direction
|= nSRSTnOE
;
4115 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4116 /* switch output high */
4117 low_output
|= nSRST
;
4119 /* switch to input pin (high-Z) */
4120 low_direction
&= ~nSRSTnOE
;
4123 /* command "set data bits low byte" */
4125 buffer_write(low_output
);
4126 buffer_write(low_direction
);
4127 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4128 "low_direction: 0x%2.2x",
4129 trst
, srst
, low_output
, low_direction
);
4131 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4132 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4133 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4134 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4135 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4139 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4140 high_output
&= ~nTRSTnOE
;
4142 high_output
&= ~nTRST
;
4146 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4147 high_output
|= nTRSTnOE
;
4149 high_output
|= nTRST
;
4154 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4155 high_output
&= ~nSRST
;
4157 high_output
&= ~nSRSTnOE
;
4161 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4162 high_output
|= nSRST
;
4164 high_output
|= nSRSTnOE
;
4167 /* command "set data bits high byte" */
4169 buffer_write(high_output
);
4170 buffer_write(high_direction
);
4171 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4172 "high_direction: 0x%2.2x",
4173 trst
, srst
, high_output
, high_direction
);
4175 else if (signalyzer_h_adapter_type
== 0x0000)
4179 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4180 /* switch to output pin (output is low) */
4181 low_direction
|= nTRSTnOE
;
4183 /* switch output low */
4184 low_output
&= ~nTRST
;
4188 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4189 /* switch to input pin (high-Z + internal
4190 * and external pullup) */
4191 low_direction
&= ~nTRSTnOE
;
4193 /* switch output high */
4194 low_output
|= nTRST
;
4199 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4200 /* switch output low */
4201 low_output
&= ~nSRST
;
4203 /* switch to output pin (output is low) */
4204 low_direction
|= nSRSTnOE
;
4208 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4209 /* switch output high */
4210 low_output
|= nSRST
;
4212 /* switch to input pin (high-Z) */
4213 low_direction
&= ~nSRSTnOE
;
4216 /* command "set data bits low byte" */
4218 buffer_write(low_output
);
4219 buffer_write(low_direction
);
4220 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4221 "low_direction: 0x%2.2x",
4222 trst
, srst
, low_output
, low_direction
);
4226 static void signalyzer_h_blink(void)
4228 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4231 /********************************************************************
4232 * Support for KT-LINK
4233 * JTAG adapter from KRISTECH
4234 * http://www.kristech.eu
4235 *******************************************************************/
4236 static int ktlink_init(void)
4239 uint32_t bytes_written
;
4240 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4242 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4243 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4245 // initialize low port
4246 buf
[0] = 0x80; // command "set data bits low byte"
4247 buf
[1] = low_output
;
4248 buf
[2] = low_direction
;
4249 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4251 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4253 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4254 return ERROR_JTAG_INIT_FAILED
;
4262 high_output
= 0x80; // turn LED on
4263 high_direction
= 0xFF; // all outputs
4265 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4267 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4268 high_output
|= nTRSTnOE
;
4269 high_output
&= ~nTRST
;
4271 high_output
&= ~nTRSTnOE
;
4272 high_output
|= nTRST
;
4275 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4276 high_output
&= ~nSRSTnOE
;
4277 high_output
|= nSRST
;
4279 high_output
|= nSRSTnOE
;
4280 high_output
&= ~nSRST
;
4283 // initialize high port
4284 buf
[0] = 0x82; // command "set data bits high byte"
4285 buf
[1] = high_output
; // value
4286 buf
[2] = high_direction
;
4287 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4289 if (ft2232_write(buf
, sizeof(buf
), &bytes_written
) != ERROR_OK
)
4291 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4292 return ERROR_JTAG_INIT_FAILED
;
4298 static void ktlink_reset(int trst
, int srst
)
4300 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4303 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4304 high_output
&= ~nTRSTnOE
;
4306 high_output
&= ~nTRST
;
4307 } else if (trst
== 0) {
4308 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4309 high_output
|= nTRSTnOE
;
4311 high_output
|= nTRST
;
4315 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4316 high_output
&= ~nSRST
;
4318 high_output
&= ~nSRSTnOE
;
4319 } else if (srst
== 0) {
4320 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4321 high_output
|= nSRST
;
4323 high_output
|= nSRSTnOE
;
4326 buffer_write(0x82); // command "set data bits high byte"
4327 buffer_write(high_output
);
4328 buffer_write(high_direction
);
4329 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4332 static void ktlink_blink(void)
4334 /* LED connected to ACBUS7 */
4335 if (high_output
& 0x80)
4336 high_output
&= 0x7F;
4338 high_output
|= 0x80;
4340 buffer_write(0x82); // command "set data bits high byte"
4341 buffer_write(high_output
);
4342 buffer_write(high_direction
);
4345 static const struct command_registration ft2232_command_handlers
[] = {
4347 .name
= "ft2232_device_desc",
4348 .handler
= &ft2232_handle_device_desc_command
,
4349 .mode
= COMMAND_CONFIG
,
4350 .help
= "set the USB device description of the FTDI FT2232 device",
4351 .usage
= "description_string",
4354 .name
= "ft2232_serial",
4355 .handler
= &ft2232_handle_serial_command
,
4356 .mode
= COMMAND_CONFIG
,
4357 .help
= "set the serial number of the FTDI FT2232 device",
4358 .usage
= "serial_string",
4361 .name
= "ft2232_layout",
4362 .handler
= &ft2232_handle_layout_command
,
4363 .mode
= COMMAND_CONFIG
,
4364 .help
= "set the layout of the FT2232 GPIO signals used "
4365 "to control output-enables and reset signals",
4366 .usage
= "layout_name",
4369 .name
= "ft2232_vid_pid",
4370 .handler
= &ft2232_handle_vid_pid_command
,
4371 .mode
= COMMAND_CONFIG
,
4372 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4373 .usage
= "(vid pid)* ",
4376 .name
= "ft2232_latency",
4377 .handler
= &ft2232_handle_latency_command
,
4378 .mode
= COMMAND_CONFIG
,
4379 .help
= "set the FT2232 latency timer to a new value",
4382 COMMAND_REGISTRATION_DONE
4385 struct jtag_interface ft2232_interface
= {
4387 .supported
= DEBUG_CAP_TMS_SEQ
,
4388 .commands
= ft2232_command_handlers
,
4389 .transports
= jtag_only
,
4391 .init
= ft2232_init
,
4392 .quit
= ft2232_quit
,
4393 .speed
= ft2232_speed
,
4394 .speed_div
= ft2232_speed_div
,
4396 .execute_queue
= ft2232_execute_queue
,