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);
171 const char **transports;
174 /* init procedures for supported layouts */
175 static int usbjtag_init(void);
176 static int jtagkey_init(void);
177 static int lm3s811_jtag_init(void);
178 static int icdi_jtag_init(void);
179 static int olimex_jtag_init(void);
180 static int flyswatter_init(void);
181 static int turtle_init(void);
182 static int comstick_init(void);
183 static int stm32stick_init(void);
184 static int axm0432_jtag_init(void);
185 static int sheevaplug_init(void);
186 static int icebear_jtag_init(void);
187 static int cortino_jtag_init(void);
188 static int signalyzer_init(void);
189 static int signalyzer_h_init(void);
190 static int ktlink_init(void);
191 static int redbee_init(void);
193 /* reset procedures for supported layouts */
194 static void ftx23_reset(int trst, int srst);
195 static void jtagkey_reset(int trst, int srst);
196 static void olimex_jtag_reset(int trst, int srst);
197 static void flyswatter_reset(int trst, int srst);
198 static void turtle_reset(int trst, int srst);
199 static void comstick_reset(int trst, int srst);
200 static void stm32stick_reset(int trst, int srst);
201 static void axm0432_jtag_reset(int trst, int srst);
202 static void sheevaplug_reset(int trst, int srst);
203 static void icebear_jtag_reset(int trst, int srst);
204 static void signalyzer_h_reset(int trst, int srst);
205 static void ktlink_reset(int trst, int srst);
206 static void redbee_reset(int trst, int srst);
208 /* blink procedures for layouts that support a blinking led */
209 static void olimex_jtag_blink(void);
210 static void flyswatter_jtag_blink(void);
211 static void turtle_jtag_blink(void);
212 static void signalyzer_h_blink(void);
213 static void ktlink_blink(void);
215 /* common transport support options */
216 static const char *jtag_only[] = { "jtag", NULL };
219 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
220 #define jtag_and_swd NULL
222 static const struct ft2232_layout ft2232_layouts[] =
225 .init = usbjtag_init,
226 .reset = ftx23_reset,
229 .init = jtagkey_init,
230 .reset = jtagkey_reset,
232 { .name = "jtagkey_prototype_v1",
233 .init = jtagkey_init,
234 .reset = jtagkey_reset,
236 { .name = "oocdlink",
237 .init = jtagkey_init,
238 .reset = jtagkey_reset,
240 { .name = "signalyzer",
241 .init = signalyzer_init,
242 .reset = ftx23_reset,
244 { .name = "evb_lm3s811",
245 .init = lm3s811_jtag_init,
246 .reset = ftx23_reset,
247 .transports = jtag_and_swd,
249 { .name = "luminary_icdi",
250 .init = icdi_jtag_init,
251 .reset = ftx23_reset,
252 .transports = jtag_and_swd,
254 { .name = "olimex-jtag",
255 .init = olimex_jtag_init,
256 .reset = olimex_jtag_reset,
257 .blink = olimex_jtag_blink
259 { .name = "flyswatter",
260 .init = flyswatter_init,
261 .reset = flyswatter_reset,
262 .blink = flyswatter_jtag_blink
264 { .name = "turtelizer2",
266 .reset = turtle_reset,
267 .blink = turtle_jtag_blink
269 { .name = "comstick",
270 .init = comstick_init,
271 .reset = comstick_reset,
273 { .name = "stm32stick",
274 .init = stm32stick_init,
275 .reset = stm32stick_reset,
277 { .name = "axm0432_jtag",
278 .init = axm0432_jtag_init,
279 .reset = axm0432_jtag_reset,
281 { .name = "sheevaplug",
282 .init = sheevaplug_init,
283 .reset = sheevaplug_reset,
286 .init = icebear_jtag_init,
287 .reset = icebear_jtag_reset,
290 .init = cortino_jtag_init,
291 .reset = comstick_reset,
293 { .name = "signalyzer-h",
294 .init = signalyzer_h_init,
295 .reset = signalyzer_h_reset,
296 .blink = signalyzer_h_blink
300 .reset = ktlink_reset,
301 .blink = ktlink_blink
303 { .name = "redbee-econotag",
305 .reset = redbee_reset,
307 { .name = "redbee-usb",
309 .reset = redbee_reset,
310 .channel = INTERFACE_B,
312 { .name = NULL, /* END OF TABLE */ },
315 /* bitmask used to drive nTRST; usually a GPIOLx signal */
316 static uint8_t nTRST;
317 static uint8_t nTRSTnOE;
318 /* bitmask used to drive nSRST; usually a GPIOLx signal */
319 static uint8_t nSRST;
320 static uint8_t nSRSTnOE;
322 /** the layout being used with this debug session */
323 static const struct ft2232_layout *layout;
325 /** default bitmask values ddriven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
326 static uint8_t low_output = 0x0;
327 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
328 static uint8_t low_direction = 0x0;
329 /** default value bitmask for CBUS GPIOH(0..4) */
330 static uint8_t high_output = 0x0;
331 /** default direction bitmask for CBUS GPIOH(0..4) */
332 static uint8_t high_direction = 0x0;
334 #if BUILD_FT2232_FTD2XX == 1
335 static FT_HANDLE ftdih = NULL;
336 static FT_DEVICE ftdi_device = 0;
337 #elif BUILD_FT2232_LIBFTDI == 1
338 static struct ftdi_context ftdic;
339 static enum ftdi_chip_type ftdi_device;
342 static struct jtag_command* first_unsent; /* next command that has to be sent */
343 static int require_send;
345 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
347 "There is a significant difference between libftdi and libftd2xx. The latter
348 one allows to schedule up to 64*64 bytes of result data while libftdi fails
349 with more than 4*64. As a consequence, the FT2232 driver is forced to
350 perform around 16x more USB transactions for long command streams with TDO
351 capture when running with libftdi."
354 #define FT2232_BUFFER_SIZE 131072
355 a comment would have been nice.
358 #define FT2232_BUFFER_SIZE 131072
360 static uint8_t* ft2232_buffer = NULL;
361 static int ft2232_buffer_size = 0;
362 static int ft2232_read_pointer = 0;
363 static int ft2232_expect_read = 0;
366 * Function buffer_write
367 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
368 * @param val is the byte to send.
370 static inline void buffer_write(uint8_t val)
372 assert(ft2232_buffer);
373 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
374 ft2232_buffer[ft2232_buffer_size++] = val;
378 * Function buffer_read
379 * returns a byte from the byte buffer.
381 static inline uint8_t buffer_read(void)
383 assert(ft2232_buffer);
384 assert(ft2232_read_pointer < ft2232_buffer_size);
385 return ft2232_buffer[ft2232_read_pointer++];
389 * Clocks out \a bit_count bits on the TMS line, starting with the least
390 * significant bit of tms_bits and progressing to more significant bits.
391 * Rigorous state transition logging is done here via tap_set_state().
393 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
394 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
395 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
396 * is often used for this, 0x4b.
398 * @param tms_bits Holds the sequence of bits to send.
399 * @param tms_count Tells how many bits in the sequence.
400 * @param tdi_bit A single bit to pass on to TDI before the first TCK
401 * cycle and held static for the duration of TMS clocking.
403 * See the MPSSE spec referenced above.
405 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
409 int tms_ndx; /* bit index into tms_byte */
411 assert(tms_count > 0);
413 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
414 mpsse_cmd, tms_bits, tms_count);
416 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
418 bool bit = tms_bits & 1;
421 tms_byte |= (1 << tms_ndx);
423 /* always do state transitions in public view */
424 tap_set_state(tap_state_transition(tap_get_state(), bit));
426 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
431 if (tms_ndx == 7 || i == tms_count-1)
433 buffer_write(mpsse_cmd);
434 buffer_write(tms_ndx - 1);
436 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
437 TMS/CS and is held static for the duration of TMS/CS clocking.
439 buffer_write(tms_byte | (tdi_bit << 7));
445 * Function get_tms_buffer_requirements
446 * returns what clock_tms() will consume if called with
449 static inline int get_tms_buffer_requirements(int bit_count)
451 return ((bit_count + 6)/7) * 3;
455 * Function move_to_state
456 * moves the TAP controller from the current state to a
457 * \a goal_state through a path given by tap_get_tms_path(). State transition
458 * logging is performed by delegation to clock_tms().
460 * @param goal_state is the destination state for the move.
462 static void move_to_state(tap_state_t goal_state)
464 tap_state_t start_state = tap_get_state();
466 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
467 lookup of the required TMS pattern to move to this state from the
471 /* do the 2 lookups */
472 int tms_bits = tap_get_tms_path(start_state, goal_state);
473 int tms_count = tap_get_tms_path_len(start_state, goal_state);
475 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
477 clock_tms(0x4b, tms_bits, tms_count, 0);
480 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
482 #if BUILD_FT2232_FTD2XX == 1
484 DWORD dw_bytes_written;
485 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
487 *bytes_written = dw_bytes_written;
488 LOG_ERROR("FT_Write returned: %lu", status);
489 return ERROR_JTAG_DEVICE_ERROR;
493 *bytes_written = dw_bytes_written;
495 #elif BUILD_FT2232_LIBFTDI == 1
497 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
500 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
501 return ERROR_JTAG_DEVICE_ERROR;
505 *bytes_written = retval;
509 if (*bytes_written != (uint32_t)size)
511 return ERROR_JTAG_DEVICE_ERROR;
517 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
519 #if BUILD_FT2232_FTD2XX == 1
525 while ((*bytes_read < size) && timeout--)
527 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
528 *bytes_read, &dw_bytes_read)) != FT_OK)
531 LOG_ERROR("FT_Read returned: %lu", status);
532 return ERROR_JTAG_DEVICE_ERROR;
534 *bytes_read += dw_bytes_read;
537 #elif BUILD_FT2232_LIBFTDI == 1
539 int timeout = LIBFTDI_READ_RETRY_COUNT;
542 while ((*bytes_read < size) && timeout--)
544 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
547 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
548 return ERROR_JTAG_DEVICE_ERROR;
550 *bytes_read += retval;
555 if (*bytes_read < size)
557 LOG_ERROR("couldn't read enough bytes from "
558 "FT2232 device (%i < %i)",
559 (unsigned)*bytes_read,
561 return ERROR_JTAG_DEVICE_ERROR;
567 static bool ft2232_device_is_highspeed(void)
569 #if BUILD_FT2232_FTD2XX == 1
570 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
571 #elif BUILD_FT2232_LIBFTDI == 1
572 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
577 * Commands that only apply to the FT2232H and FT4232H devices.
578 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
579 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
582 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
584 uint8_t buf = enable ? 0x96 : 0x97;
585 LOG_DEBUG("%2.2x", buf);
587 uint32_t bytes_written;
590 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
592 LOG_ERROR("couldn't write command to %s adaptive clocking"
593 , enable ? "enable" : "disable");
601 * Enable/disable the clk divide by 5 of the 60MHz master clock.
602 * This result in a JTAG clock speed range of 91.553Hz-6MHz
603 * respective 457.763Hz-30MHz.
605 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
607 uint32_t bytes_written;
608 uint8_t buf = enable ? 0x8b : 0x8a;
610 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
612 LOG_ERROR("couldn't write command to %s clk divide by 5"
613 , enable ? "enable" : "disable");
614 return ERROR_JTAG_INIT_FAILED;
616 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
617 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
622 static int ft2232_speed(int speed)
626 uint32_t bytes_written;
629 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
630 if (ft2232_device_is_highspeed())
631 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
632 else if (enable_adaptive_clocking)
634 LOG_ERROR("ft2232 device %lu does not support RTCK"
635 , (long unsigned int)ftdi_device);
639 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
642 buf[0] = 0x86; /* command "set divisor" */
643 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
644 buf[2] = (speed >> 8) & 0xff; /* valueH */
646 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
647 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
649 LOG_ERROR("couldn't set FT2232 TCK speed");
656 static int ft2232_speed_div(int speed, int* khz)
658 /* Take a look in the FT2232 manual,
659 * AN2232C-01 Command Processor for
660 * MPSSE and MCU Host Bus. Chapter 3.8 */
662 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
667 static int ft2232_khz(int khz, int* jtag_speed)
671 if (ft2232_device_is_highspeed())
673 *jtag_speed = RTCK_SPEED;
678 LOG_DEBUG("RCLK not supported");
683 /* Take a look in the FT2232 manual,
684 * AN2232C-01 Command Processor for
685 * MPSSE and MCU Host Bus. Chapter 3.8
687 * We will calc here with a multiplier
688 * of 10 for better rounding later. */
690 /* Calc speed, (ft2232_max_tck / khz) - 1 */
691 /* Use 65000 for better rounding */
692 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
694 /* Add 0.9 for rounding */
697 /* Calc real speed */
698 *jtag_speed = *jtag_speed / 10;
700 /* Check if speed is greater than 0 */
706 /* Check max value */
707 if (*jtag_speed > 0xFFFF)
709 *jtag_speed = 0xFFFF;
715 static void ft2232_end_state(tap_state_t state)
717 if (tap_is_state_stable(state))
718 tap_set_end_state(state);
721 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
726 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
728 int num_bytes = (scan_size + 7) / 8;
729 int bits_left = scan_size;
732 while (num_bytes-- > 1)
734 buffer[cur_byte++] = buffer_read();
738 buffer[cur_byte] = 0x0;
740 /* There is one more partial byte left from the clock data in/out instructions */
743 buffer[cur_byte] = buffer_read() >> 1;
745 /* 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 */
746 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
749 static void ft2232_debug_dump_buffer(void)
755 for (i = 0; i < ft2232_buffer_size; i++)
757 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
760 LOG_DEBUG("%s", line);
766 LOG_DEBUG("%s", line);
769 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
771 struct jtag_command* cmd;
776 uint32_t bytes_written = 0;
777 uint32_t bytes_read = 0;
779 #ifdef _DEBUG_USB_IO_
780 struct timeval start, inter, inter2, end;
781 struct timeval d_inter, d_inter2, d_end;
784 #ifdef _DEBUG_USB_COMMS_
785 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
786 ft2232_debug_dump_buffer();
789 #ifdef _DEBUG_USB_IO_
790 gettimeofday(&start, NULL);
793 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
795 LOG_ERROR("couldn't write MPSSE commands to FT2232");
799 #ifdef _DEBUG_USB_IO_
800 gettimeofday(&inter, NULL);
803 if (ft2232_expect_read)
805 /* FIXME this "timeout" is never changed ... */
806 int timeout = LIBFTDI_READ_RETRY_COUNT;
807 ft2232_buffer_size = 0;
809 #ifdef _DEBUG_USB_IO_
810 gettimeofday(&inter2, NULL);
813 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
815 LOG_ERROR("couldn't read from FT2232");
819 #ifdef _DEBUG_USB_IO_
820 gettimeofday(&end, NULL);
822 timeval_subtract(&d_inter, &inter, &start);
823 timeval_subtract(&d_inter2, &inter2, &start);
824 timeval_subtract(&d_end, &end, &start);
826 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
827 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
828 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
829 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
832 ft2232_buffer_size = bytes_read;
834 if (ft2232_expect_read != ft2232_buffer_size)
836 LOG_ERROR("ft2232_expect_read (%i) != "
837 "ft2232_buffer_size (%i) "
841 LIBFTDI_READ_RETRY_COUNT - timeout);
842 ft2232_debug_dump_buffer();
847 #ifdef _DEBUG_USB_COMMS_
848 LOG_DEBUG("read buffer (%i retries): %i bytes",
849 LIBFTDI_READ_RETRY_COUNT - timeout,
851 ft2232_debug_dump_buffer();
855 ft2232_expect_read = 0;
856 ft2232_read_pointer = 0;
858 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
859 * that wasn't handled by a caller-provided error handler
869 type = jtag_scan_type(cmd->cmd.scan);
870 if (type != SCAN_OUT)
872 scan_size = jtag_scan_size(cmd->cmd.scan);
873 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
874 ft2232_read_scan(type, buffer, scan_size);
875 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
876 retval = ERROR_JTAG_QUEUE_FAILED;
888 ft2232_buffer_size = 0;
894 * Function ft2232_add_pathmove
895 * moves the TAP controller from the current state to a new state through the
896 * given path, where path is an array of tap_state_t's.
898 * @param path is an array of tap_stat_t which gives the states to traverse through
899 * ending with the last state at path[num_states-1]
900 * @param num_states is the count of state steps to move through
902 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
906 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
910 /* this loop verifies that the path is legal and logs each state in the path */
913 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
915 int num_states_batch = num_states > 7 ? 7 : num_states;
917 /* command "Clock Data to TMS/CS Pin (no Read)" */
920 /* number of states remaining */
921 buffer_write(num_states_batch - 1);
923 while (num_states_batch--) {
924 /* either TMS=0 or TMS=1 must work ... */
925 if (tap_state_transition(tap_get_state(), false)
926 == path[state_count])
927 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
928 else if (tap_state_transition(tap_get_state(), true)
929 == path[state_count])
930 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
932 /* ... or else the caller goofed BADLY */
934 LOG_ERROR("BUG: %s -> %s isn't a valid "
935 "TAP state transition",
936 tap_state_name(tap_get_state()),
937 tap_state_name(path[state_count]));
941 tap_set_state(path[state_count]);
946 buffer_write(tms_byte);
948 tap_set_end_state(tap_get_state());
951 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
953 int num_bytes = (scan_size + 7) / 8;
954 int bits_left = scan_size;
960 if (tap_get_state() != TAP_DRSHIFT)
962 move_to_state(TAP_DRSHIFT);
967 if (tap_get_state() != TAP_IRSHIFT)
969 move_to_state(TAP_IRSHIFT);
973 /* add command for complete bytes */
974 while (num_bytes > 1)
979 /* Clock Data Bytes In and Out LSB First */
981 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
983 else if (type == SCAN_OUT)
985 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
987 /* LOG_DEBUG("added TDI bytes (o)"); */
989 else if (type == SCAN_IN)
991 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
993 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
996 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
997 num_bytes -= thisrun_bytes;
999 buffer_write((uint8_t) (thisrun_bytes - 1));
1000 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1002 if (type != SCAN_IN)
1004 /* add complete bytes */
1005 while (thisrun_bytes-- > 0)
1007 buffer_write(buffer[cur_byte++]);
1011 else /* (type == SCAN_IN) */
1013 bits_left -= 8 * (thisrun_bytes);
1017 /* the most signifcant bit is scanned during TAP movement */
1018 if (type != SCAN_IN)
1019 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1023 /* process remaining bits but the last one */
1026 if (type == SCAN_IO)
1028 /* Clock Data Bits In and Out LSB First */
1030 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1032 else if (type == SCAN_OUT)
1034 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1036 /* LOG_DEBUG("added TDI bits (o)"); */
1038 else if (type == SCAN_IN)
1040 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1042 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1045 buffer_write(bits_left - 2);
1046 if (type != SCAN_IN)
1047 buffer_write(buffer[cur_byte]);
1050 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1051 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1053 if (type == SCAN_IO)
1055 /* Clock Data Bits In and Out LSB First */
1057 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1059 else if (type == SCAN_OUT)
1061 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1063 /* LOG_DEBUG("added TDI bits (o)"); */
1065 else if (type == SCAN_IN)
1067 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1069 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1072 buffer_write(last_bit);
1080 /* move from Shift-IR/DR to end state */
1081 if (type != SCAN_OUT)
1083 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1084 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1087 /* Clock Data to TMS/CS Pin with Read */
1092 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1093 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1094 /* Clock Data to TMS/CS Pin (no Read) */
1098 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1099 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1102 if (tap_get_state() != tap_get_end_state())
1104 move_to_state(tap_get_end_state());
1108 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1110 int num_bytes = (scan_size + 7) / 8;
1111 int bits_left = scan_size;
1114 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1115 uint8_t* receive_pointer = receive_buffer;
1116 uint32_t bytes_written;
1117 uint32_t bytes_read;
1119 int thisrun_read = 0;
1123 LOG_ERROR("BUG: large IR scans are not supported");
1127 if (tap_get_state() != TAP_DRSHIFT)
1129 move_to_state(TAP_DRSHIFT);
1132 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1134 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1137 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1138 ft2232_buffer_size, (int)bytes_written);
1139 ft2232_buffer_size = 0;
1141 /* add command for complete bytes */
1142 while (num_bytes > 1)
1146 if (type == SCAN_IO)
1148 /* Clock Data Bytes In and Out LSB First */
1150 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1152 else if (type == SCAN_OUT)
1154 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1156 /* LOG_DEBUG("added TDI bytes (o)"); */
1158 else if (type == SCAN_IN)
1160 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1162 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1165 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1166 thisrun_read = thisrun_bytes;
1167 num_bytes -= thisrun_bytes;
1168 buffer_write((uint8_t) (thisrun_bytes - 1));
1169 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1171 if (type != SCAN_IN)
1173 /* add complete bytes */
1174 while (thisrun_bytes-- > 0)
1176 buffer_write(buffer[cur_byte]);
1181 else /* (type == SCAN_IN) */
1183 bits_left -= 8 * (thisrun_bytes);
1186 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1188 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1191 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1193 (int)bytes_written);
1194 ft2232_buffer_size = 0;
1196 if (type != SCAN_OUT)
1198 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1200 LOG_ERROR("couldn't read from FT2232");
1203 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1206 receive_pointer += bytes_read;
1212 /* the most signifcant bit is scanned during TAP movement */
1213 if (type != SCAN_IN)
1214 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1218 /* process remaining bits but the last one */
1221 if (type == SCAN_IO)
1223 /* Clock Data Bits In and Out LSB First */
1225 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1227 else if (type == SCAN_OUT)
1229 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1231 /* LOG_DEBUG("added TDI bits (o)"); */
1233 else if (type == SCAN_IN)
1235 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1237 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1239 buffer_write(bits_left - 2);
1240 if (type != SCAN_IN)
1241 buffer_write(buffer[cur_byte]);
1243 if (type != SCAN_OUT)
1247 if (tap_get_end_state() == TAP_DRSHIFT)
1249 if (type == SCAN_IO)
1251 /* Clock Data Bits In and Out LSB First */
1253 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1255 else if (type == SCAN_OUT)
1257 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1259 /* LOG_DEBUG("added TDI bits (o)"); */
1261 else if (type == SCAN_IN)
1263 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1265 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1268 buffer_write(last_bit);
1272 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1273 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1276 /* move from Shift-IR/DR to end state */
1277 if (type != SCAN_OUT)
1279 /* Clock Data to TMS/CS Pin with Read */
1281 /* LOG_DEBUG("added TMS scan (read)"); */
1285 /* Clock Data to TMS/CS Pin (no Read) */
1287 /* LOG_DEBUG("added TMS scan (no read)"); */
1290 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1291 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1294 if (type != SCAN_OUT)
1297 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1299 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1302 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1304 (int)bytes_written);
1305 ft2232_buffer_size = 0;
1307 if (type != SCAN_OUT)
1309 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1311 LOG_ERROR("couldn't read from FT2232");
1314 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1317 receive_pointer += bytes_read;
1323 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1325 int predicted_size = 3;
1326 int num_bytes = (scan_size - 1) / 8;
1328 if (tap_get_state() != TAP_DRSHIFT)
1329 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1331 if (type == SCAN_IN) /* only from device to host */
1333 /* complete bytes */
1334 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1336 /* remaining bits - 1 (up to 7) */
1337 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1339 else /* host to device, or bidirectional */
1341 /* complete bytes */
1342 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1344 /* remaining bits -1 (up to 7) */
1345 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1348 return predicted_size;
1351 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1353 int predicted_size = 0;
1355 if (type != SCAN_OUT)
1357 /* complete bytes */
1358 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1360 /* remaining bits - 1 */
1361 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1363 /* last bit (from TMS scan) */
1364 predicted_size += 1;
1367 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1369 return predicted_size;
1372 /* semi-generic FT2232/FT4232 reset code */
1373 static void ftx23_reset(int trst, int srst)
1375 enum reset_types jtag_reset_config = jtag_get_reset_config();
1378 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1379 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1381 low_output &= ~nTRST; /* switch output low */
1385 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1386 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1388 low_output |= nTRST; /* switch output high */
1393 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1394 low_output &= ~nSRST; /* switch output low */
1396 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1400 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1401 low_output |= nSRST; /* switch output high */
1403 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1406 /* command "set data bits low byte" */
1408 buffer_write(low_output);
1409 buffer_write(low_direction);
1412 static void jtagkey_reset(int trst, int srst)
1414 enum reset_types jtag_reset_config = jtag_get_reset_config();
1417 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1418 high_output &= ~nTRSTnOE;
1420 high_output &= ~nTRST;
1424 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1425 high_output |= nTRSTnOE;
1427 high_output |= nTRST;
1432 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1433 high_output &= ~nSRST;
1435 high_output &= ~nSRSTnOE;
1439 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1440 high_output |= nSRST;
1442 high_output |= nSRSTnOE;
1445 /* command "set data bits high byte" */
1447 buffer_write(high_output);
1448 buffer_write(high_direction);
1449 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1453 static void olimex_jtag_reset(int trst, int srst)
1455 enum reset_types jtag_reset_config = jtag_get_reset_config();
1458 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1459 high_output &= ~nTRSTnOE;
1461 high_output &= ~nTRST;
1465 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1466 high_output |= nTRSTnOE;
1468 high_output |= nTRST;
1473 high_output |= nSRST;
1477 high_output &= ~nSRST;
1480 /* command "set data bits high byte" */
1482 buffer_write(high_output);
1483 buffer_write(high_direction);
1484 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1488 static void axm0432_jtag_reset(int trst, int srst)
1492 tap_set_state(TAP_RESET);
1493 high_output &= ~nTRST;
1497 high_output |= nTRST;
1502 high_output &= ~nSRST;
1506 high_output |= nSRST;
1509 /* command "set data bits low byte" */
1511 buffer_write(high_output);
1512 buffer_write(high_direction);
1513 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1517 static void flyswatter_reset(int trst, int srst)
1521 low_output &= ~nTRST;
1525 low_output |= nTRST;
1530 low_output |= nSRST;
1534 low_output &= ~nSRST;
1537 /* command "set data bits low byte" */
1539 buffer_write(low_output);
1540 buffer_write(low_direction);
1541 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1544 static void turtle_reset(int trst, int srst)
1550 low_output |= nSRST;
1554 low_output &= ~nSRST;
1557 /* command "set data bits low byte" */
1559 buffer_write(low_output);
1560 buffer_write(low_direction);
1561 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1564 static void comstick_reset(int trst, int srst)
1568 high_output &= ~nTRST;
1572 high_output |= nTRST;
1577 high_output &= ~nSRST;
1581 high_output |= nSRST;
1584 /* command "set data bits high byte" */
1586 buffer_write(high_output);
1587 buffer_write(high_direction);
1588 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1592 static void stm32stick_reset(int trst, int srst)
1596 high_output &= ~nTRST;
1600 high_output |= nTRST;
1605 low_output &= ~nSRST;
1609 low_output |= nSRST;
1612 /* command "set data bits low byte" */
1614 buffer_write(low_output);
1615 buffer_write(low_direction);
1617 /* command "set data bits high byte" */
1619 buffer_write(high_output);
1620 buffer_write(high_direction);
1621 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1625 static void sheevaplug_reset(int trst, int srst)
1628 high_output &= ~nTRST;
1630 high_output |= nTRST;
1633 high_output &= ~nSRSTnOE;
1635 high_output |= nSRSTnOE;
1637 /* command "set data bits high byte" */
1639 buffer_write(high_output);
1640 buffer_write(high_direction);
1641 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1644 static void redbee_reset(int trst, int srst)
1648 tap_set_state(TAP_RESET);
1649 high_output &= ~nTRST;
1653 high_output |= nTRST;
1658 high_output &= ~nSRST;
1662 high_output |= nSRST;
1665 /* command "set data bits low byte" */
1667 buffer_write(high_output);
1668 buffer_write(high_direction);
1669 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1670 "high_direction: 0x%2.2x", trst, srst, high_output,
1674 static int ft2232_execute_runtest(struct jtag_command *cmd)
1678 int predicted_size = 0;
1681 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1682 cmd->cmd.runtest->num_cycles,
1683 tap_state_name(cmd->cmd.runtest->end_state));
1685 /* only send the maximum buffer size that FT2232C can handle */
1687 if (tap_get_state() != TAP_IDLE)
1688 predicted_size += 3;
1689 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1690 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1691 predicted_size += 3;
1692 if (tap_get_end_state() != TAP_IDLE)
1693 predicted_size += 3;
1694 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1696 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1697 retval = ERROR_JTAG_QUEUE_FAILED;
1701 if (tap_get_state() != TAP_IDLE)
1703 move_to_state(TAP_IDLE);
1706 i = cmd->cmd.runtest->num_cycles;
1709 /* there are no state transitions in this code, so omit state tracking */
1711 /* command "Clock Data to TMS/CS Pin (no Read)" */
1715 buffer_write((i > 7) ? 6 : (i - 1));
1720 i -= (i > 7) ? 7 : i;
1721 /* LOG_DEBUG("added TMS scan (no read)"); */
1724 ft2232_end_state(cmd->cmd.runtest->end_state);
1726 if (tap_get_state() != tap_get_end_state())
1728 move_to_state(tap_get_end_state());
1732 DEBUG_JTAG_IO("runtest: %i, end in %s",
1733 cmd->cmd.runtest->num_cycles,
1734 tap_state_name(tap_get_end_state()));
1738 static int ft2232_execute_statemove(struct jtag_command *cmd)
1740 int predicted_size = 0;
1741 int retval = ERROR_OK;
1743 DEBUG_JTAG_IO("statemove end in %s",
1744 tap_state_name(cmd->cmd.statemove->end_state));
1746 /* only send the maximum buffer size that FT2232C can handle */
1748 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1750 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1751 retval = ERROR_JTAG_QUEUE_FAILED;
1755 ft2232_end_state(cmd->cmd.statemove->end_state);
1757 /* For TAP_RESET, ignore the current recorded state. It's often
1758 * wrong at server startup, and this transation is critical whenever
1761 if (tap_get_end_state() == TAP_RESET) {
1762 clock_tms(0x4b, 0xff, 5, 0);
1765 /* shortest-path move to desired end state */
1766 } else if (tap_get_state() != tap_get_end_state())
1768 move_to_state(tap_get_end_state());
1776 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1777 * (or SWD) state machine.
1779 static int ft2232_execute_tms(struct jtag_command *cmd)
1781 int retval = ERROR_OK;
1782 unsigned num_bits = cmd->cmd.tms->num_bits;
1783 const uint8_t *bits = cmd->cmd.tms->bits;
1786 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1788 /* only send the maximum buffer size that FT2232C can handle */
1789 count = 3 * DIV_ROUND_UP(num_bits, 4);
1790 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1791 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1792 retval = ERROR_JTAG_QUEUE_FAILED;
1798 /* Shift out in batches of at most 6 bits; there's a report of an
1799 * FT2232 bug in this area, where shifting exactly 7 bits can make
1800 * problems with TMS signaling for the last clock cycle:
1802 * http://developer.intra2net.com/mailarchive/html/
1803 * libftdi/2009/msg00292.html
1805 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1807 * Note that pathmoves in JTAG are not often seven bits, so that
1808 * isn't a particularly likely situation outside of "special"
1809 * signaling such as switching between JTAG and SWD modes.
1812 if (num_bits <= 6) {
1814 buffer_write(num_bits - 1);
1815 buffer_write(*bits & 0x3f);
1819 /* Yes, this is lazy ... we COULD shift out more data
1820 * bits per operation, but doing it in nybbles is easy
1824 buffer_write(*bits & 0xf);
1827 count = (num_bits > 4) ? 4 : num_bits;
1830 buffer_write(count - 1);
1831 buffer_write((*bits >> 4) & 0xf);
1841 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1843 int predicted_size = 0;
1844 int retval = ERROR_OK;
1846 tap_state_t* path = cmd->cmd.pathmove->path;
1847 int num_states = cmd->cmd.pathmove->num_states;
1849 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1850 tap_state_name(tap_get_state()),
1851 tap_state_name(path[num_states-1]));
1853 /* only send the maximum buffer size that FT2232C can handle */
1854 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1855 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1857 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1858 retval = ERROR_JTAG_QUEUE_FAILED;
1864 ft2232_add_pathmove(path, num_states);
1870 static int ft2232_execute_scan(struct jtag_command *cmd)
1873 int scan_size; /* size of IR or DR scan */
1874 int predicted_size = 0;
1875 int retval = ERROR_OK;
1877 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1879 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1881 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1883 predicted_size = ft2232_predict_scan_out(scan_size, type);
1884 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1886 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1887 /* unsent commands before this */
1888 if (first_unsent != cmd)
1889 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1890 retval = ERROR_JTAG_QUEUE_FAILED;
1892 /* current command */
1893 ft2232_end_state(cmd->cmd.scan->end_state);
1894 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1896 first_unsent = cmd->next;
1901 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1903 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1906 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1907 retval = ERROR_JTAG_QUEUE_FAILED;
1911 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1912 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1913 ft2232_end_state(cmd->cmd.scan->end_state);
1914 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1918 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1919 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1920 tap_state_name(tap_get_end_state()));
1925 static int ft2232_execute_reset(struct jtag_command *cmd)
1928 int predicted_size = 0;
1931 DEBUG_JTAG_IO("reset trst: %i srst %i",
1932 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1934 /* only send the maximum buffer size that FT2232C can handle */
1936 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1938 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1939 retval = ERROR_JTAG_QUEUE_FAILED;
1944 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1946 tap_set_state(TAP_RESET);
1949 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1952 DEBUG_JTAG_IO("trst: %i, srst: %i",
1953 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1957 static int ft2232_execute_sleep(struct jtag_command *cmd)
1962 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1964 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1965 retval = ERROR_JTAG_QUEUE_FAILED;
1966 first_unsent = cmd->next;
1967 jtag_sleep(cmd->cmd.sleep->us);
1968 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1970 tap_state_name(tap_get_state()));
1974 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1979 /* this is only allowed while in a stable state. A check for a stable
1980 * state was done in jtag_add_clocks()
1982 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1983 retval = ERROR_JTAG_QUEUE_FAILED;
1984 DEBUG_JTAG_IO("clocks %i while in %s",
1985 cmd->cmd.stableclocks->num_cycles,
1986 tap_state_name(tap_get_state()));
1990 static int ft2232_execute_command(struct jtag_command *cmd)
1996 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1997 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1998 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
1999 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2000 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2001 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2002 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2004 retval = ft2232_execute_tms(cmd);
2007 LOG_ERROR("BUG: unknown JTAG command type encountered");
2008 retval = ERROR_JTAG_QUEUE_FAILED;
2014 static int ft2232_execute_queue(void)
2016 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2019 first_unsent = cmd; /* next command that has to be sent */
2022 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2023 * that wasn't handled by a caller-provided error handler
2027 ft2232_buffer_size = 0;
2028 ft2232_expect_read = 0;
2030 /* blink, if the current layout has that feature */
2036 if (ft2232_execute_command(cmd) != ERROR_OK)
2037 retval = ERROR_JTAG_QUEUE_FAILED;
2038 /* Start reading input before FT2232 TX buffer fills up */
2040 if (ft2232_expect_read > 256)
2042 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2043 retval = ERROR_JTAG_QUEUE_FAILED;
2048 if (require_send > 0)
2049 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2050 retval = ERROR_JTAG_QUEUE_FAILED;
2055 #if BUILD_FT2232_FTD2XX == 1
2056 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2060 char SerialNumber[16];
2061 char Description[64];
2062 DWORD openex_flags = 0;
2063 char* openex_string = NULL;
2064 uint8_t latency_timer;
2066 if (layout == NULL) {
2067 LOG_WARNING("No ft2232 layout specified'");
2068 return ERROR_JTAG_INIT_FAILED;
2071 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2074 /* Add non-standard Vid/Pid to the linux driver */
2075 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2077 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2081 if (ft2232_device_desc && ft2232_serial)
2083 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2084 ft2232_device_desc = NULL;
2087 if (ft2232_device_desc)
2089 openex_string = ft2232_device_desc;
2090 openex_flags = FT_OPEN_BY_DESCRIPTION;
2092 else if (ft2232_serial)
2094 openex_string = ft2232_serial;
2095 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2099 LOG_ERROR("neither device description nor serial number specified");
2100 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2102 return ERROR_JTAG_INIT_FAILED;
2105 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2106 if (status != FT_OK) {
2107 /* under Win32, the FTD2XX driver appends an "A" to the end
2108 * of the description, if we tried by the desc, then
2109 * try by the alternate "A" description. */
2110 if (openex_string == ft2232_device_desc) {
2111 /* Try the alternate method. */
2112 openex_string = ft2232_device_desc_A;
2113 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2114 if (status == FT_OK) {
2115 /* yea, the "alternate" method worked! */
2117 /* drat, give the user a meaningfull message.
2118 * telling the use we tried *BOTH* methods. */
2119 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2121 ft2232_device_desc_A);
2126 if (status != FT_OK)
2132 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2134 return ERROR_JTAG_INIT_FAILED;
2136 LOG_ERROR("unable to open ftdi device: %lu", status);
2137 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2138 if (status == FT_OK)
2140 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2143 for (i = 0; i < num_devices; i++)
2144 desc_array[i] = malloc(64);
2146 desc_array[num_devices] = NULL;
2148 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2150 if (status == FT_OK)
2152 LOG_ERROR("ListDevices: %lu\n", num_devices);
2153 for (i = 0; i < num_devices; i++)
2154 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2157 for (i = 0; i < num_devices; i++)
2158 free(desc_array[i]);
2164 LOG_ERROR("ListDevices: NONE\n");
2166 return ERROR_JTAG_INIT_FAILED;
2169 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2171 LOG_ERROR("unable to set latency timer: %lu", status);
2172 return ERROR_JTAG_INIT_FAILED;
2175 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2177 LOG_ERROR("unable to get latency timer: %lu", status);
2178 return ERROR_JTAG_INIT_FAILED;
2182 LOG_DEBUG("current latency timer: %i", latency_timer);
2185 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2187 LOG_ERROR("unable to set timeouts: %lu", status);
2188 return ERROR_JTAG_INIT_FAILED;
2191 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2193 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2194 return ERROR_JTAG_INIT_FAILED;
2197 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2199 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2200 return ERROR_JTAG_INIT_FAILED;
2204 static const char* type_str[] =
2205 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2206 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2207 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2208 ? ftdi_device : FT_DEVICE_UNKNOWN;
2209 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2210 LOG_INFO("deviceID: %lu", deviceID);
2211 LOG_INFO("SerialNumber: %s", SerialNumber);
2212 LOG_INFO("Description: %s", Description);
2218 static int ft2232_purge_ftd2xx(void)
2222 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2224 LOG_ERROR("error purging ftd2xx device: %lu", status);
2225 return ERROR_JTAG_INIT_FAILED;
2231 #endif /* BUILD_FT2232_FTD2XX == 1 */
2233 #if BUILD_FT2232_LIBFTDI == 1
2234 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2236 uint8_t latency_timer;
2238 if (layout == NULL) {
2239 LOG_WARNING("No ft2232 layout specified'");
2240 return ERROR_JTAG_INIT_FAILED;
2243 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2244 layout->name, vid, pid);
2246 if (ftdi_init(&ftdic) < 0)
2247 return ERROR_JTAG_INIT_FAILED;
2249 /* default to INTERFACE_A */
2250 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2252 if (ftdi_set_interface(&ftdic, channel) < 0)
2254 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2255 return ERROR_JTAG_INIT_FAILED;
2258 /* context, vendor id, product id */
2259 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2263 LOG_WARNING("unable to open ftdi device (trying more): %s",
2266 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2268 return ERROR_JTAG_INIT_FAILED;
2271 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2272 if (ftdi_usb_reset(&ftdic) < 0)
2274 LOG_ERROR("unable to reset ftdi device");
2275 return ERROR_JTAG_INIT_FAILED;
2278 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2280 LOG_ERROR("unable to set latency timer");
2281 return ERROR_JTAG_INIT_FAILED;
2284 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2286 LOG_ERROR("unable to get latency timer");
2287 return ERROR_JTAG_INIT_FAILED;
2291 LOG_DEBUG("current latency timer: %i", latency_timer);
2294 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2296 ftdi_device = ftdic.type;
2297 static const char* type_str[] =
2298 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2299 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2300 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2301 ? ftdi_device : no_of_known_types;
2302 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2306 static int ft2232_purge_libftdi(void)
2308 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2310 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2311 return ERROR_JTAG_INIT_FAILED;
2317 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2319 static int ft2232_init(void)
2323 uint32_t bytes_written;
2325 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2327 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2331 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2334 if (layout == NULL) {
2335 LOG_WARNING("No ft2232 layout specified'");
2336 return ERROR_JTAG_INIT_FAILED;
2339 for (int i = 0; 1; i++)
2342 * "more indicates that there are more IDs to try, so we should
2343 * not print an error for an ID mismatch (but for anything
2346 * try_more indicates that the error code returned indicates an
2347 * ID mismatch (and nothing else) and that we should proceeed
2348 * with the next ID pair.
2350 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2353 #if BUILD_FT2232_FTD2XX == 1
2354 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2356 #elif BUILD_FT2232_LIBFTDI == 1
2357 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2358 more, &try_more, layout->channel);
2362 if (!more || !try_more)
2366 ft2232_buffer_size = 0;
2367 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2369 if (layout->init() != ERROR_OK)
2370 return ERROR_JTAG_INIT_FAILED;
2372 if (ft2232_device_is_highspeed())
2374 #ifndef BUILD_FT2232_HIGHSPEED
2375 #if BUILD_FT2232_FTD2XX == 1
2376 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2377 #elif BUILD_FT2232_LIBFTDI == 1
2378 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2381 /* make sure the legacy mode is disabled */
2382 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2383 return ERROR_JTAG_INIT_FAILED;
2386 ft2232_speed(jtag_get_speed());
2388 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2389 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2391 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2392 return ERROR_JTAG_INIT_FAILED;
2395 #if BUILD_FT2232_FTD2XX == 1
2396 return ft2232_purge_ftd2xx();
2397 #elif BUILD_FT2232_LIBFTDI == 1
2398 return ft2232_purge_libftdi();
2404 /** Updates defaults for DBUS signals: the four JTAG signals
2405 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2407 static inline void ftx232_dbus_init(void)
2410 low_direction = 0x0b;
2413 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2414 * the four GPIOL signals. Initialization covers value and direction,
2415 * as customized for each layout.
2417 static int ftx232_dbus_write(void)
2420 uint32_t bytes_written;
2422 enum reset_types jtag_reset_config = jtag_get_reset_config();
2423 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2425 low_direction &= ~nTRSTnOE; /* nTRST input */
2426 low_output &= ~nTRST; /* nTRST = 0 */
2430 low_direction |= nTRSTnOE; /* nTRST output */
2431 low_output |= nTRST; /* nTRST = 1 */
2434 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2436 low_direction |= nSRSTnOE; /* nSRST output */
2437 low_output |= nSRST; /* nSRST = 1 */
2441 low_direction &= ~nSRSTnOE; /* nSRST input */
2442 low_output &= ~nSRST; /* nSRST = 0 */
2445 /* initialize low byte for jtag */
2446 buf[0] = 0x80; /* command "set data bits low byte" */
2447 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2448 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2449 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2451 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2453 LOG_ERROR("couldn't initialize FT2232 DBUS");
2454 return ERROR_JTAG_INIT_FAILED;
2460 static int usbjtag_init(void)
2463 * NOTE: This is now _specific_ to the "usbjtag" layout.
2464 * Don't try cram any more layouts into this.
2473 return ftx232_dbus_write();
2476 static int lm3s811_jtag_init(void)
2480 /* There are multiple revisions of LM3S811 eval boards:
2481 * - Rev B (and older?) boards have no SWO trace support.
2482 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2483 * they should use the "luminary_icdi" layout instead.
2490 low_direction = 0x8b;
2492 return ftx232_dbus_write();
2495 static int icdi_jtag_init(void)
2499 /* Most Luminary eval boards support SWO trace output,
2500 * and should use this "luminary_icdi" layout.
2507 low_direction = 0xcb;
2509 return ftx232_dbus_write();
2512 static int signalyzer_init(void)
2520 return ftx232_dbus_write();
2523 static int axm0432_jtag_init(void)
2526 uint32_t bytes_written;
2529 low_direction = 0x2b;
2531 /* initialize low byte for jtag */
2532 buf[0] = 0x80; /* command "set data bits low byte" */
2533 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2534 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2535 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2537 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2539 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2540 return ERROR_JTAG_INIT_FAILED;
2543 if (strcmp(layout->name, "axm0432_jtag") == 0)
2546 nTRSTnOE = 0x0; /* No output enable for TRST*/
2548 nSRSTnOE = 0x0; /* No output enable for SRST*/
2552 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2557 high_direction = 0x0c;
2559 enum reset_types jtag_reset_config = jtag_get_reset_config();
2560 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2562 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2566 high_output |= nTRST;
2569 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2571 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2575 high_output |= nSRST;
2578 /* initialize high port */
2579 buf[0] = 0x82; /* command "set data bits high byte" */
2580 buf[1] = high_output; /* value */
2581 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2582 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2584 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2586 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2587 return ERROR_JTAG_INIT_FAILED;
2593 static int redbee_init(void)
2596 uint32_t bytes_written;
2599 low_direction = 0x2b;
2601 /* initialize low byte for jtag */
2602 /* command "set data bits low byte" */
2604 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2605 buf[2] = low_direction;
2606 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2607 buf[1] = low_output;
2608 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2610 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2612 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2613 return ERROR_JTAG_INIT_FAILED;
2617 nTRSTnOE = 0x0; /* No output enable for TRST*/
2619 nSRSTnOE = 0x0; /* No output enable for SRST*/
2622 high_direction = 0x0c;
2624 enum reset_types jtag_reset_config = jtag_get_reset_config();
2625 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2627 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2631 high_output |= nTRST;
2634 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2636 LOG_ERROR("can't set nSRST to push-pull on redbee");
2640 high_output |= nSRST;
2643 /* initialize high port */
2644 buf[0] = 0x82; /* command "set data bits high byte" */
2645 buf[1] = high_output; /* value */
2646 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2647 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2649 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2651 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2652 return ERROR_JTAG_INIT_FAILED;
2658 static int jtagkey_init(void)
2661 uint32_t bytes_written;
2664 low_direction = 0x1b;
2666 /* initialize low byte for jtag */
2667 buf[0] = 0x80; /* command "set data bits low byte" */
2668 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2669 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2670 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2672 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2674 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2675 return ERROR_JTAG_INIT_FAILED;
2678 if (strcmp(layout->name, "jtagkey") == 0)
2685 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2686 || (strcmp(layout->name, "oocdlink") == 0))
2695 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2700 high_direction = 0x0f;
2702 enum reset_types jtag_reset_config = jtag_get_reset_config();
2703 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2705 high_output |= nTRSTnOE;
2706 high_output &= ~nTRST;
2710 high_output &= ~nTRSTnOE;
2711 high_output |= nTRST;
2714 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2716 high_output &= ~nSRSTnOE;
2717 high_output |= nSRST;
2721 high_output |= nSRSTnOE;
2722 high_output &= ~nSRST;
2725 /* initialize high port */
2726 buf[0] = 0x82; /* command "set data bits high byte" */
2727 buf[1] = high_output; /* value */
2728 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2729 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2731 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2733 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2734 return ERROR_JTAG_INIT_FAILED;
2740 static int olimex_jtag_init(void)
2743 uint32_t bytes_written;
2746 low_direction = 0x1b;
2748 /* initialize low byte for jtag */
2749 buf[0] = 0x80; /* command "set data bits low byte" */
2750 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2751 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2752 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2754 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2756 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2757 return ERROR_JTAG_INIT_FAILED;
2763 nSRSTnOE = 0x00; /* no output enable for nSRST */
2766 high_direction = 0x0f;
2768 enum reset_types jtag_reset_config = jtag_get_reset_config();
2769 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2771 high_output |= nTRSTnOE;
2772 high_output &= ~nTRST;
2776 high_output &= ~nTRSTnOE;
2777 high_output |= nTRST;
2780 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2782 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2786 high_output &= ~nSRST;
2789 /* turn red LED on */
2790 high_output |= 0x08;
2792 /* initialize high port */
2793 buf[0] = 0x82; /* command "set data bits high byte" */
2794 buf[1] = high_output; /* value */
2795 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2796 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2798 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2800 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2801 return ERROR_JTAG_INIT_FAILED;
2807 static int flyswatter_init(void)
2810 uint32_t bytes_written;
2813 low_direction = 0xfb;
2815 /* initialize low byte for jtag */
2816 buf[0] = 0x80; /* command "set data bits low byte" */
2817 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2818 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2819 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2821 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2823 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2824 return ERROR_JTAG_INIT_FAILED;
2828 nTRSTnOE = 0x0; /* not output enable for nTRST */
2830 nSRSTnOE = 0x00; /* no output enable for nSRST */
2833 high_direction = 0x0c;
2835 /* turn red LED3 on, LED2 off */
2836 high_output |= 0x08;
2838 /* initialize high port */
2839 buf[0] = 0x82; /* command "set data bits high byte" */
2840 buf[1] = high_output; /* value */
2841 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2842 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2844 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2846 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2847 return ERROR_JTAG_INIT_FAILED;
2853 static int turtle_init(void)
2856 uint32_t bytes_written;
2859 low_direction = 0x5b;
2861 /* initialize low byte for jtag */
2862 buf[0] = 0x80; /* command "set data bits low byte" */
2863 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2864 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2865 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2867 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2869 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2870 return ERROR_JTAG_INIT_FAILED;
2876 high_direction = 0x0C;
2878 /* initialize high port */
2879 buf[0] = 0x82; /* command "set data bits high byte" */
2880 buf[1] = high_output;
2881 buf[2] = high_direction;
2882 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2884 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2886 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2887 return ERROR_JTAG_INIT_FAILED;
2893 static int comstick_init(void)
2896 uint32_t bytes_written;
2899 low_direction = 0x0b;
2901 /* initialize low byte for jtag */
2902 buf[0] = 0x80; /* command "set data bits low byte" */
2903 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2904 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2905 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2907 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2909 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2910 return ERROR_JTAG_INIT_FAILED;
2914 nTRSTnOE = 0x00; /* no output enable for nTRST */
2916 nSRSTnOE = 0x00; /* no output enable for nSRST */
2919 high_direction = 0x03;
2921 /* initialize high port */
2922 buf[0] = 0x82; /* command "set data bits high byte" */
2923 buf[1] = high_output;
2924 buf[2] = high_direction;
2925 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2927 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2929 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2930 return ERROR_JTAG_INIT_FAILED;
2936 static int stm32stick_init(void)
2939 uint32_t bytes_written;
2942 low_direction = 0x8b;
2944 /* initialize low byte for jtag */
2945 buf[0] = 0x80; /* command "set data bits low byte" */
2946 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2947 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2948 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2950 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2952 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2953 return ERROR_JTAG_INIT_FAILED;
2957 nTRSTnOE = 0x00; /* no output enable for nTRST */
2959 nSRSTnOE = 0x00; /* no output enable for nSRST */
2962 high_direction = 0x03;
2964 /* initialize high port */
2965 buf[0] = 0x82; /* command "set data bits high byte" */
2966 buf[1] = high_output;
2967 buf[2] = high_direction;
2968 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2970 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2972 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2973 return ERROR_JTAG_INIT_FAILED;
2979 static int sheevaplug_init(void)
2982 uint32_t bytes_written;
2985 low_direction = 0x1b;
2987 /* initialize low byte for jtag */
2988 buf[0] = 0x80; /* command "set data bits low byte" */
2989 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2990 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2991 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2993 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2995 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2996 return ERROR_JTAG_INIT_FAILED;
3005 high_direction = 0x0f;
3007 /* nTRST is always push-pull */
3008 high_output &= ~nTRSTnOE;
3009 high_output |= nTRST;
3011 /* nSRST is always open-drain */
3012 high_output |= nSRSTnOE;
3013 high_output &= ~nSRST;
3015 /* initialize high port */
3016 buf[0] = 0x82; /* command "set data bits high byte" */
3017 buf[1] = high_output; /* value */
3018 buf[2] = high_direction; /* all outputs - xRST */
3019 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3021 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3023 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3024 return ERROR_JTAG_INIT_FAILED;
3030 static int cortino_jtag_init(void)
3033 uint32_t bytes_written;
3036 low_direction = 0x1b;
3038 /* initialize low byte for jtag */
3039 buf[0] = 0x80; /* command "set data bits low byte" */
3040 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3041 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3042 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3044 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3046 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3047 return ERROR_JTAG_INIT_FAILED;
3051 nTRSTnOE = 0x00; /* no output enable for nTRST */
3053 nSRSTnOE = 0x00; /* no output enable for nSRST */
3056 high_direction = 0x03;
3058 /* initialize high port */
3059 buf[0] = 0x82; /* command "set data bits high byte" */
3060 buf[1] = high_output;
3061 buf[2] = high_direction;
3062 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3064 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3066 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3067 return ERROR_JTAG_INIT_FAILED;
3073 static void olimex_jtag_blink(void)
3075 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3076 * ACBUS3 is bit 3 of the GPIOH port
3078 if (high_output & 0x08)
3080 /* set port pin high */
3081 high_output &= 0x07;
3085 /* set port pin low */
3086 high_output |= 0x08;
3090 buffer_write(high_output);
3091 buffer_write(high_direction);
3094 static void flyswatter_jtag_blink(void)
3097 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3099 high_output ^= 0x0c;
3102 buffer_write(high_output);
3103 buffer_write(high_direction);
3106 static void turtle_jtag_blink(void)
3109 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3111 if (high_output & 0x08)
3121 buffer_write(high_output);
3122 buffer_write(high_direction);
3125 static int ft2232_quit(void)
3127 #if BUILD_FT2232_FTD2XX == 1
3130 status = FT_Close(ftdih);
3131 #elif BUILD_FT2232_LIBFTDI == 1
3132 ftdi_usb_close(&ftdic);
3134 ftdi_deinit(&ftdic);
3137 free(ft2232_buffer);
3138 ft2232_buffer = NULL;
3143 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3149 ft2232_device_desc = strdup(CMD_ARGV[0]);
3150 cp = strchr(ft2232_device_desc, 0);
3151 /* under Win32, the FTD2XX driver appends an "A" to the end
3152 * of the description, this examines the given desc
3153 * and creates the 'missing' _A or non_A variable. */
3154 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3155 /* it was, so make this the "A" version. */
3156 ft2232_device_desc_A = ft2232_device_desc;
3157 /* and *CREATE* the non-A version. */
3158 strcpy(buf, ft2232_device_desc);
3159 cp = strchr(buf, 0);
3161 ft2232_device_desc = strdup(buf);
3163 /* <space > A not defined
3165 sprintf(buf, "%s A", ft2232_device_desc);
3166 ft2232_device_desc_A = strdup(buf);
3171 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3177 COMMAND_HANDLER(ft2232_handle_serial_command)
3181 ft2232_serial = strdup(CMD_ARGV[0]);
3185 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3191 COMMAND_HANDLER(ft2232_handle_layout_command)
3193 if (CMD_ARGC != 1) {
3194 LOG_ERROR("Need exactly one argument to ft2232_layout");
3199 LOG_ERROR("already specified ft2232_layout %s",
3201 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3206 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3207 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3209 /* This may also select the transport
3210 * if we only suppport one of them.
3212 return allow_transports(CMD_CTX,
3213 l->transports ? : jtag_only);
3217 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3221 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3223 if (CMD_ARGC > MAX_USB_IDS * 2)
3225 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3226 "(maximum is %d pairs)", MAX_USB_IDS);
3227 CMD_ARGC = MAX_USB_IDS * 2;
3229 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3231 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3233 return ERROR_COMMAND_SYNTAX_ERROR;
3234 /* remove the incomplete trailing id */
3239 for (i = 0; i < CMD_ARGC; i += 2)
3241 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3242 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3246 * Explicitly terminate, in case there are multiples instances of
3249 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3254 COMMAND_HANDLER(ft2232_handle_latency_command)
3258 ft2232_latency = atoi(CMD_ARGV[0]);
3262 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3268 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3272 /* 7 bits of either ones or zeros. */
3273 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3275 while (num_cycles > 0)
3277 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3278 * at most 7 bits per invocation. Here we invoke it potentially
3281 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3283 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3285 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3286 retval = ERROR_JTAG_QUEUE_FAILED;
3291 /* there are no state transitions in this code, so omit state tracking */
3293 /* command "Clock Data to TMS/CS Pin (no Read)" */
3297 buffer_write(bitcount_per_command - 1);
3299 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3304 num_cycles -= bitcount_per_command;
3310 /* ---------------------------------------------------------------------
3311 * Support for IceBear JTAG adapter from Section5:
3312 * http://section5.ch/icebear
3314 * Author: Sten, debian@sansys-electronic.com
3317 /* Icebear pin layout
3319 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3320 * GND GND | 4 3| n.c.
3321 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3322 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3323 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3324 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3325 * ADBUS2 TDO |14 13| GND GND
3327 * ADBUS0 O L TCK ACBUS0 GND
3328 * ADBUS1 O L TDI ACBUS1 GND
3329 * ADBUS2 I TDO ACBUS2 n.c.
3330 * ADBUS3 O H TMS ACBUS3 n.c.
3336 static int icebear_jtag_init(void) {
3338 uint32_t bytes_written;
3340 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3341 low_output = 0x08; /* high: TMS; low: TCK TDI */
3345 enum reset_types jtag_reset_config = jtag_get_reset_config();
3346 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3347 low_direction &= ~nTRST; /* nTRST high impedance */
3350 low_direction |= nTRST;
3351 low_output |= nTRST;
3354 low_direction |= nSRST;
3355 low_output |= nSRST;
3357 /* initialize low byte for jtag */
3358 buf[0] = 0x80; /* command "set data bits low byte" */
3359 buf[1] = low_output;
3360 buf[2] = low_direction;
3361 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3363 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3364 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3365 return ERROR_JTAG_INIT_FAILED;
3369 high_direction = 0x00;
3372 /* initialize high port */
3373 buf[0] = 0x82; /* command "set data bits high byte" */
3374 buf[1] = high_output; /* value */
3375 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3376 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3378 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3379 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3380 return ERROR_JTAG_INIT_FAILED;
3386 static void icebear_jtag_reset(int trst, int srst) {
3389 low_direction |= nTRST;
3390 low_output &= ~nTRST;
3392 else if (trst == 0) {
3393 enum reset_types jtag_reset_config = jtag_get_reset_config();
3394 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3395 low_direction &= ~nTRST;
3397 low_output |= nTRST;
3401 low_output &= ~nSRST;
3403 else if (srst == 0) {
3404 low_output |= nSRST;
3407 /* command "set data bits low byte" */
3409 buffer_write(low_output);
3410 buffer_write(low_direction);
3412 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3415 /* ---------------------------------------------------------------------
3416 * Support for Signalyzer H2 and Signalyzer H4
3417 * JTAG adapter from Xverve Technologies Inc.
3418 * http://www.signalyzer.com or http://www.xverve.com
3420 * Author: Oleg Seiljus, oleg@signalyzer.com
3422 static unsigned char signalyzer_h_side;
3423 static unsigned int signalyzer_h_adapter_type;
3425 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3427 #if BUILD_FT2232_FTD2XX == 1
3428 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3431 #define SIGNALYZER_COMMAND_ADDR 128
3432 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3434 #define SIGNALYZER_COMMAND_VERSION 0x41
3435 #define SIGNALYZER_COMMAND_RESET 0x42
3436 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3437 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3438 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3439 #define SIGNALYZER_COMMAND_LED_SET 0x53
3440 #define SIGNALYZER_COMMAND_ADC 0x54
3441 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3442 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3443 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3444 #define SIGNALYZER_COMMAND_I2C 0x58
3446 #define SIGNALYZER_CHAN_A 1
3447 #define SIGNALYZER_CHAN_B 2
3448 /* LEDS use channel C */
3449 #define SIGNALYZER_CHAN_C 4
3451 #define SIGNALYZER_LED_GREEN 1
3452 #define SIGNALYZER_LED_RED 2
3454 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3455 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3456 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3457 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3458 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3461 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3463 #if BUILD_FT2232_FTD2XX == 1
3464 return FT_WriteEE(ftdih, address, value);
3465 #elif BUILD_FT2232_LIBFTDI == 1
3470 #if BUILD_FT2232_FTD2XX == 1
3471 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3473 return FT_ReadEE(ftdih, address, value);
3477 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3478 int on_time_ms, int off_time_ms, unsigned char cycles)
3480 unsigned char on_time;
3481 unsigned char off_time;
3483 if (on_time_ms < 0xFFFF)
3484 on_time = (unsigned char)(on_time_ms / 62);
3488 off_time = (unsigned char)(off_time_ms / 62);
3490 #if BUILD_FT2232_FTD2XX == 1
3493 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3494 ((uint32_t)(channel << 8) | led))) != FT_OK)
3496 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3497 return ERROR_JTAG_DEVICE_ERROR;
3500 if ((status = signalyzer_h_ctrl_write(
3501 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3502 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3504 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3505 return ERROR_JTAG_DEVICE_ERROR;
3508 if ((status = signalyzer_h_ctrl_write(
3509 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3510 ((uint32_t)cycles))) != FT_OK)
3512 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3513 return ERROR_JTAG_DEVICE_ERROR;
3516 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3517 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3519 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3520 return ERROR_JTAG_DEVICE_ERROR;
3524 #elif BUILD_FT2232_LIBFTDI == 1
3527 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3528 ((uint32_t)(channel << 8) | led))) < 0)
3530 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3531 ftdi_get_error_string(&ftdic));
3532 return ERROR_JTAG_DEVICE_ERROR;
3535 if ((retval = signalyzer_h_ctrl_write(
3536 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3537 ((uint32_t)(on_time << 8) | off_time))) < 0)
3539 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3540 ftdi_get_error_string(&ftdic));
3541 return ERROR_JTAG_DEVICE_ERROR;
3544 if ((retval = signalyzer_h_ctrl_write(
3545 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3546 (uint32_t)cycles)) < 0)
3548 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3549 ftdi_get_error_string(&ftdic));
3550 return ERROR_JTAG_DEVICE_ERROR;
3553 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3554 SIGNALYZER_COMMAND_LED_SET)) < 0)
3556 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3557 ftdi_get_error_string(&ftdic));
3558 return ERROR_JTAG_DEVICE_ERROR;
3565 static int signalyzer_h_init(void)
3567 #if BUILD_FT2232_FTD2XX == 1
3574 uint16_t read_buf[12] = { 0 };
3576 uint32_t bytes_written;
3578 /* turn on center green led */
3579 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3580 0xFFFF, 0x00, 0x00);
3582 /* determine what channel config wants to open
3583 * TODO: change me... current implementation is made to work
3584 * with openocd description parsing.
3586 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3590 signalyzer_h_side = *(end_of_desc - 1);
3591 if (signalyzer_h_side == 'B')
3592 signalyzer_h_side = SIGNALYZER_CHAN_B;
3594 signalyzer_h_side = SIGNALYZER_CHAN_A;
3598 LOG_ERROR("No Channel was specified");
3602 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3605 #if BUILD_FT2232_FTD2XX == 1
3606 /* read signalyzer versionining information */
3607 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3608 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3610 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3611 return ERROR_JTAG_DEVICE_ERROR;
3614 for (i = 0; i < 10; i++)
3616 if ((status = signalyzer_h_ctrl_read(
3617 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3618 &read_buf[i])) != FT_OK)
3620 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3622 return ERROR_JTAG_DEVICE_ERROR;
3626 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3627 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3628 read_buf[4], read_buf[5], read_buf[6]);
3630 /* set gpio register */
3631 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3632 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3635 return ERROR_JTAG_DEVICE_ERROR;
3638 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3641 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3642 return ERROR_JTAG_DEVICE_ERROR;
3645 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3646 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3648 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3649 return ERROR_JTAG_DEVICE_ERROR;
3652 /* read adapter type information */
3653 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3654 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3656 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3657 return ERROR_JTAG_DEVICE_ERROR;
3660 if ((status = signalyzer_h_ctrl_write(
3661 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3663 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3664 return ERROR_JTAG_DEVICE_ERROR;
3667 if ((status = signalyzer_h_ctrl_write(
3668 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != 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(SIGNALYZER_COMMAND_ADDR,
3675 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3677 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3678 return ERROR_JTAG_DEVICE_ERROR;
3683 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3684 &read_buf[0])) != FT_OK)
3686 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3687 return ERROR_JTAG_DEVICE_ERROR;
3690 if (read_buf[0] != 0x0498)
3691 signalyzer_h_adapter_type = 0x0000;
3694 for (i = 0; i < 4; i++)
3696 if ((status = signalyzer_h_ctrl_read(
3697 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3698 &read_buf[i])) != FT_OK)
3700 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3702 return ERROR_JTAG_DEVICE_ERROR;
3706 signalyzer_h_adapter_type = read_buf[0];
3709 #elif BUILD_FT2232_LIBFTDI == 1
3710 /* currently libftdi does not allow reading individual eeprom
3711 * locations, therefore adapter type cannot be detected.
3712 * override with most common type
3714 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3717 enum reset_types jtag_reset_config = jtag_get_reset_config();
3719 /* ADAPTOR: EM_LT16_A */
3720 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3722 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3723 "detected. (HW: %2x).", (read_buf[1] >> 8));
3731 low_direction = 0x1b;
3734 high_direction = 0x0;
3736 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3738 low_direction &= ~nTRSTnOE; /* nTRST input */
3739 low_output &= ~nTRST; /* nTRST = 0 */
3743 low_direction |= nTRSTnOE; /* nTRST output */
3744 low_output |= nTRST; /* nTRST = 1 */
3747 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3749 low_direction |= nSRSTnOE; /* nSRST output */
3750 low_output |= nSRST; /* nSRST = 1 */
3754 low_direction &= ~nSRSTnOE; /* nSRST input */
3755 low_output &= ~nSRST; /* nSRST = 0 */
3758 #if BUILD_FT2232_FTD2XX == 1
3759 /* enable power to the module */
3760 if ((status = signalyzer_h_ctrl_write(
3761 SIGNALYZER_DATA_BUFFER_ADDR,
3762 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3765 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3767 return ERROR_JTAG_DEVICE_ERROR;
3770 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3771 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3773 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3775 return ERROR_JTAG_DEVICE_ERROR;
3778 /* set gpio mode register */
3779 if ((status = signalyzer_h_ctrl_write(
3780 SIGNALYZER_DATA_BUFFER_ADDR,
3781 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3783 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3785 return ERROR_JTAG_DEVICE_ERROR;
3788 if ((status = signalyzer_h_ctrl_write(
3789 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3792 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3794 return ERROR_JTAG_DEVICE_ERROR;
3797 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3798 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3800 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3802 return ERROR_JTAG_DEVICE_ERROR;
3805 /* set gpio register */
3806 if ((status = signalyzer_h_ctrl_write(
3807 SIGNALYZER_DATA_BUFFER_ADDR,
3808 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3810 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3812 return ERROR_JTAG_DEVICE_ERROR;
3815 if ((status = signalyzer_h_ctrl_write(
3816 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3819 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3821 return ERROR_JTAG_DEVICE_ERROR;
3824 if ((status = signalyzer_h_ctrl_write(
3825 SIGNALYZER_COMMAND_ADDR,
3826 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3828 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3830 return ERROR_JTAG_DEVICE_ERROR;
3835 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3836 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3837 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3838 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3839 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3841 if (signalyzer_h_adapter_type
3842 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3843 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3844 "detected. (HW: %2x).", (read_buf[1] >> 8));
3845 else if (signalyzer_h_adapter_type
3846 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3847 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3848 "(ARM JTAG with PSU) detected. (HW: %2x).",
3849 (read_buf[1] >> 8));
3850 else if (signalyzer_h_adapter_type
3851 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3852 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3853 "detected. (HW: %2x).", (read_buf[1] >> 8));
3854 else if (signalyzer_h_adapter_type
3855 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3856 LOG_INFO("Signalyzer: EM-JTAG-P "
3857 "(Generic JTAG with PSU) detected. (HW: %2x).",
3858 (read_buf[1] >> 8));
3866 low_direction = 0x1b;
3869 high_direction = 0x1f;
3871 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3873 high_output |= nTRSTnOE;
3874 high_output &= ~nTRST;
3878 high_output &= ~nTRSTnOE;
3879 high_output |= nTRST;
3882 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3884 high_output &= ~nSRSTnOE;
3885 high_output |= nSRST;
3889 high_output |= nSRSTnOE;
3890 high_output &= ~nSRST;
3893 #if BUILD_FT2232_FTD2XX == 1
3894 /* enable power to the module */
3895 if ((status = signalyzer_h_ctrl_write(
3896 SIGNALYZER_DATA_BUFFER_ADDR,
3897 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3900 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3902 return ERROR_JTAG_DEVICE_ERROR;
3905 if ((status = signalyzer_h_ctrl_write(
3906 SIGNALYZER_COMMAND_ADDR,
3907 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3909 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3911 return ERROR_JTAG_DEVICE_ERROR;
3914 /* set gpio mode register (IO_16 and IO_17 set as analog
3915 * inputs, other is gpio)
3917 if ((status = signalyzer_h_ctrl_write(
3918 SIGNALYZER_DATA_BUFFER_ADDR,
3919 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3921 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3923 return ERROR_JTAG_DEVICE_ERROR;
3926 if ((status = signalyzer_h_ctrl_write(
3927 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3930 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3932 return ERROR_JTAG_DEVICE_ERROR;
3935 if ((status = signalyzer_h_ctrl_write(
3936 SIGNALYZER_COMMAND_ADDR,
3937 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3939 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3941 return ERROR_JTAG_DEVICE_ERROR;
3944 /* set gpio register (all inputs, for -P modules,
3945 * PSU will be turned off)
3947 if ((status = signalyzer_h_ctrl_write(
3948 SIGNALYZER_DATA_BUFFER_ADDR,
3949 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3951 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3953 return ERROR_JTAG_DEVICE_ERROR;
3956 if ((status = signalyzer_h_ctrl_write(
3957 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3960 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3962 return ERROR_JTAG_DEVICE_ERROR;
3965 if ((status = signalyzer_h_ctrl_write(
3966 SIGNALYZER_COMMAND_ADDR,
3967 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3969 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3971 return ERROR_JTAG_DEVICE_ERROR;
3976 else if (signalyzer_h_adapter_type == 0x0000)
3978 LOG_INFO("Signalyzer: No external modules were detected.");
3986 low_direction = 0x1b;
3989 high_direction = 0x0;
3991 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3993 low_direction &= ~nTRSTnOE; /* nTRST input */
3994 low_output &= ~nTRST; /* nTRST = 0 */
3998 low_direction |= nTRSTnOE; /* nTRST output */
3999 low_output |= nTRST; /* nTRST = 1 */
4002 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4004 low_direction |= nSRSTnOE; /* nSRST output */
4005 low_output |= nSRST; /* nSRST = 1 */
4009 low_direction &= ~nSRSTnOE; /* nSRST input */
4010 low_output &= ~nSRST; /* nSRST = 0 */
4015 LOG_ERROR("Unknown module type is detected: %.4x",
4016 signalyzer_h_adapter_type);
4017 return ERROR_JTAG_DEVICE_ERROR;
4020 /* initialize low byte of controller for jtag operation */
4022 buf[1] = low_output;
4023 buf[2] = low_direction;
4025 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4027 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4028 return ERROR_JTAG_INIT_FAILED;
4031 #if BUILD_FT2232_FTD2XX == 1
4032 if (ftdi_device == FT_DEVICE_2232H)
4034 /* initialize high byte of controller for jtag operation */
4036 buf[1] = high_output;
4037 buf[2] = high_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 #elif BUILD_FT2232_LIBFTDI == 1
4046 if (ftdi_device == TYPE_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;
4063 static void signalyzer_h_reset(int trst, int srst)
4065 enum reset_types jtag_reset_config = jtag_get_reset_config();
4067 /* ADAPTOR: EM_LT16_A */
4068 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4072 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4073 /* switch to output pin (output is low) */
4074 low_direction |= nTRSTnOE;
4076 /* switch output low */
4077 low_output &= ~nTRST;
4081 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4082 /* switch to input pin (high-Z + internal
4083 * and external pullup) */
4084 low_direction &= ~nTRSTnOE;
4086 /* switch output high */
4087 low_output |= nTRST;
4092 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4093 /* switch output low */
4094 low_output &= ~nSRST;
4096 /* switch to output pin (output is low) */
4097 low_direction |= nSRSTnOE;
4101 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4102 /* switch output high */
4103 low_output |= nSRST;
4105 /* switch to input pin (high-Z) */
4106 low_direction &= ~nSRSTnOE;
4109 /* command "set data bits low byte" */
4111 buffer_write(low_output);
4112 buffer_write(low_direction);
4113 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4114 "low_direction: 0x%2.2x",
4115 trst, srst, low_output, low_direction);
4117 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4118 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4119 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4120 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4121 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4125 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4126 high_output &= ~nTRSTnOE;
4128 high_output &= ~nTRST;
4132 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4133 high_output |= nTRSTnOE;
4135 high_output |= nTRST;
4140 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4141 high_output &= ~nSRST;
4143 high_output &= ~nSRSTnOE;
4147 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4148 high_output |= nSRST;
4150 high_output |= nSRSTnOE;
4153 /* command "set data bits high byte" */
4155 buffer_write(high_output);
4156 buffer_write(high_direction);
4157 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4158 "high_direction: 0x%2.2x",
4159 trst, srst, high_output, high_direction);
4161 else if (signalyzer_h_adapter_type == 0x0000)
4165 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4166 /* switch to output pin (output is low) */
4167 low_direction |= nTRSTnOE;
4169 /* switch output low */
4170 low_output &= ~nTRST;
4174 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4175 /* switch to input pin (high-Z + internal
4176 * and external pullup) */
4177 low_direction &= ~nTRSTnOE;
4179 /* switch output high */
4180 low_output |= nTRST;
4185 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4186 /* switch output low */
4187 low_output &= ~nSRST;
4189 /* switch to output pin (output is low) */
4190 low_direction |= nSRSTnOE;
4194 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4195 /* switch output high */
4196 low_output |= nSRST;
4198 /* switch to input pin (high-Z) */
4199 low_direction &= ~nSRSTnOE;
4202 /* command "set data bits low byte" */
4204 buffer_write(low_output);
4205 buffer_write(low_direction);
4206 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4207 "low_direction: 0x%2.2x",
4208 trst, srst, low_output, low_direction);
4212 static void signalyzer_h_blink(void)
4214 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4217 /********************************************************************
4218 * Support for KT-LINK
4219 * JTAG adapter from KRISTECH
4220 * http://www.kristech.eu
4221 *******************************************************************/
4222 static int ktlink_init(void)
4225 uint32_t bytes_written;
4226 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4228 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4229 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4231 // initialize low port
4232 buf[0] = 0x80; // command "set data bits low byte"
4233 buf[1] = low_output;
4234 buf[2] = low_direction;
4235 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4237 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4239 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4240 return ERROR_JTAG_INIT_FAILED;
4248 high_output = 0x80; // turn LED on
4249 high_direction = 0xFF; // all outputs
4251 enum reset_types jtag_reset_config = jtag_get_reset_config();
4253 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4254 high_output |= nTRSTnOE;
4255 high_output &= ~nTRST;
4257 high_output &= ~nTRSTnOE;
4258 high_output |= nTRST;
4261 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4262 high_output &= ~nSRSTnOE;
4263 high_output |= nSRST;
4265 high_output |= nSRSTnOE;
4266 high_output &= ~nSRST;
4269 // initialize high port
4270 buf[0] = 0x82; // command "set data bits high byte"
4271 buf[1] = high_output; // value
4272 buf[2] = high_direction;
4273 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4275 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4277 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4278 return ERROR_JTAG_INIT_FAILED;
4284 static void ktlink_reset(int trst, int srst)
4286 enum reset_types jtag_reset_config = jtag_get_reset_config();
4289 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4290 high_output &= ~nTRSTnOE;
4292 high_output &= ~nTRST;
4293 } else if (trst == 0) {
4294 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4295 high_output |= nTRSTnOE;
4297 high_output |= nTRST;
4301 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4302 high_output &= ~nSRST;
4304 high_output &= ~nSRSTnOE;
4305 } else if (srst == 0) {
4306 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4307 high_output |= nSRST;
4309 high_output |= nSRSTnOE;
4312 buffer_write(0x82); // command "set data bits high byte"
4313 buffer_write(high_output);
4314 buffer_write(high_direction);
4315 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4318 static void ktlink_blink(void)
4320 /* LED connected to ACBUS7 */
4321 if (high_output & 0x80)
4322 high_output &= 0x7F;
4324 high_output |= 0x80;
4326 buffer_write(0x82); // command "set data bits high byte"
4327 buffer_write(high_output);
4328 buffer_write(high_direction);
4331 static const struct command_registration ft2232_command_handlers[] = {
4333 .name = "ft2232_device_desc",
4334 .handler = &ft2232_handle_device_desc_command,
4335 .mode = COMMAND_CONFIG,
4336 .help = "set the USB device description of the FTDI FT2232 device",
4337 .usage = "description_string",
4340 .name = "ft2232_serial",
4341 .handler = &ft2232_handle_serial_command,
4342 .mode = COMMAND_CONFIG,
4343 .help = "set the serial number of the FTDI FT2232 device",
4344 .usage = "serial_string",
4347 .name = "ft2232_layout",
4348 .handler = &ft2232_handle_layout_command,
4349 .mode = COMMAND_CONFIG,
4350 .help = "set the layout of the FT2232 GPIO signals used "
4351 "to control output-enables and reset signals",
4352 .usage = "layout_name",
4355 .name = "ft2232_vid_pid",
4356 .handler = &ft2232_handle_vid_pid_command,
4357 .mode = COMMAND_CONFIG,
4358 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4359 .usage = "(vid pid)* ",
4362 .name = "ft2232_latency",
4363 .handler = &ft2232_handle_latency_command,
4364 .mode = COMMAND_CONFIG,
4365 .help = "set the FT2232 latency timer to a new value",
4368 COMMAND_REGISTRATION_DONE
4371 struct jtag_interface ft2232_interface = {
4373 .supported = DEBUG_CAP_TMS_SEQ,
4374 .commands = ft2232_command_handlers,
4376 .init = ft2232_init,
4377 .quit = ft2232_quit,
4378 .speed = ft2232_speed,
4379 .speed_div = ft2232_speed_div,
4381 .execute_queue = ft2232_execute_queue,