1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <jtag/transport.h>
85 #include <helper/time_support.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
154 static char * ft2232_device_desc_A = NULL;
155 static char* ft2232_device_desc = NULL;
156 static char* ft2232_serial = NULL;
157 static uint8_t ft2232_latency = 2;
158 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165 struct ft2232_layout {
168 void (*reset)(int trst, int srst);
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter_init(void);
180 static int turtle_init(void);
181 static int comstick_init(void);
182 static int stm32stick_init(void);
183 static int axm0432_jtag_init(void);
184 static int sheevaplug_init(void);
185 static int icebear_jtag_init(void);
186 static int cortino_jtag_init(void);
187 static int signalyzer_init(void);
188 static int signalyzer_h_init(void);
189 static int ktlink_init(void);
190 static int redbee_init(void);
191 static int lisa_l_init(void);
192 static int flossjtag_init(void);
193 static int xds100v2_init(void);
195 /* reset procedures for supported layouts */
196 static void ftx23_reset(int trst, int srst);
197 static void jtagkey_reset(int trst, int srst);
198 static void olimex_jtag_reset(int trst, int srst);
199 static void flyswatter_reset(int trst, int srst);
200 static void turtle_reset(int trst, int srst);
201 static void comstick_reset(int trst, int srst);
202 static void stm32stick_reset(int trst, int srst);
203 static void axm0432_jtag_reset(int trst, int srst);
204 static void sheevaplug_reset(int trst, int srst);
205 static void icebear_jtag_reset(int trst, int srst);
206 static void signalyzer_h_reset(int trst, int srst);
207 static void ktlink_reset(int trst, int srst);
208 static void redbee_reset(int trst, int srst);
209 static void xds100v2_reset(int trst, int srst);
211 /* blink procedures for layouts that support a blinking led */
212 static void olimex_jtag_blink(void);
213 static void flyswatter_jtag_blink(void);
214 static void turtle_jtag_blink(void);
215 static void signalyzer_h_blink(void);
216 static void ktlink_blink(void);
217 static void lisa_l_blink(void);
218 static void flossjtag_blink(void);
220 /* common transport support options */
222 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
224 static const struct ft2232_layout ft2232_layouts[] =
227 .init = usbjtag_init,
228 .reset = ftx23_reset,
231 .init = jtagkey_init,
232 .reset = jtagkey_reset,
234 { .name = "jtagkey_prototype_v1",
235 .init = jtagkey_init,
236 .reset = jtagkey_reset,
238 { .name = "oocdlink",
239 .init = jtagkey_init,
240 .reset = jtagkey_reset,
242 { .name = "signalyzer",
243 .init = signalyzer_init,
244 .reset = ftx23_reset,
246 { .name = "evb_lm3s811",
247 .init = lm3s811_jtag_init,
248 .reset = ftx23_reset,
250 { .name = "luminary_icdi",
251 .init = icdi_jtag_init,
252 .reset = ftx23_reset,
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,
314 .reset = ftx23_reset,
315 .blink = lisa_l_blink,
316 .channel = INTERFACE_B,
318 { .name = "flossjtag",
319 .init = flossjtag_init,
320 .reset = ftx23_reset,
321 .blink = flossjtag_blink,
323 { .name = "xds100v2",
324 .init = xds100v2_init,
325 .reset = xds100v2_reset,
327 { .name = NULL, /* END OF TABLE */ },
330 /* bitmask used to drive nTRST; usually a GPIOLx signal */
331 static uint8_t nTRST;
332 static uint8_t nTRSTnOE;
333 /* bitmask used to drive nSRST; usually a GPIOLx signal */
334 static uint8_t nSRST;
335 static uint8_t nSRSTnOE;
337 /** the layout being used with this debug session */
338 static const struct ft2232_layout *layout;
340 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
341 static uint8_t low_output = 0x0;
343 /* note that direction bit == 1 means that signal is an output */
345 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
346 static uint8_t low_direction = 0x0;
347 /** default value bitmask for CBUS GPIOH(0..4) */
348 static uint8_t high_output = 0x0;
349 /** default direction bitmask for CBUS GPIOH(0..4) */
350 static uint8_t high_direction = 0x0;
352 #if BUILD_FT2232_FTD2XX == 1
353 static FT_HANDLE ftdih = NULL;
354 static FT_DEVICE ftdi_device = 0;
355 #elif BUILD_FT2232_LIBFTDI == 1
356 static struct ftdi_context ftdic;
357 static enum ftdi_chip_type ftdi_device;
360 static struct jtag_command* first_unsent; /* next command that has to be sent */
361 static int require_send;
363 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
365 "There is a significant difference between libftdi and libftd2xx. The latter
366 one allows to schedule up to 64*64 bytes of result data while libftdi fails
367 with more than 4*64. As a consequence, the FT2232 driver is forced to
368 perform around 16x more USB transactions for long command streams with TDO
369 capture when running with libftdi."
372 #define FT2232_BUFFER_SIZE 131072
373 a comment would have been nice.
376 #if BUILD_FT2232_FTD2XX == 1
377 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
379 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
382 #define FT2232_BUFFER_SIZE 131072
384 static uint8_t* ft2232_buffer = NULL;
385 static int ft2232_buffer_size = 0;
386 static int ft2232_read_pointer = 0;
387 static int ft2232_expect_read = 0;
390 * Function buffer_write
391 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
392 * @param val is the byte to send.
394 static inline void buffer_write(uint8_t val)
396 assert(ft2232_buffer);
397 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
398 ft2232_buffer[ft2232_buffer_size++] = val;
402 * Function buffer_read
403 * returns a byte from the byte buffer.
405 static inline uint8_t buffer_read(void)
407 assert(ft2232_buffer);
408 assert(ft2232_read_pointer < ft2232_buffer_size);
409 return ft2232_buffer[ft2232_read_pointer++];
413 * Clocks out \a bit_count bits on the TMS line, starting with the least
414 * significant bit of tms_bits and progressing to more significant bits.
415 * Rigorous state transition logging is done here via tap_set_state().
417 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
418 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
419 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
420 * is often used for this, 0x4b.
422 * @param tms_bits Holds the sequence of bits to send.
423 * @param tms_count Tells how many bits in the sequence.
424 * @param tdi_bit A single bit to pass on to TDI before the first TCK
425 * cycle and held static for the duration of TMS clocking.
427 * See the MPSSE spec referenced above.
429 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
433 int tms_ndx; /* bit index into tms_byte */
435 assert(tms_count > 0);
437 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
438 mpsse_cmd, tms_bits, tms_count);
440 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
442 bool bit = tms_bits & 1;
445 tms_byte |= (1 << tms_ndx);
447 /* always do state transitions in public view */
448 tap_set_state(tap_state_transition(tap_get_state(), bit));
450 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
455 if (tms_ndx == 7 || i == tms_count-1)
457 buffer_write(mpsse_cmd);
458 buffer_write(tms_ndx - 1);
460 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
461 TMS/CS and is held static for the duration of TMS/CS clocking.
463 buffer_write(tms_byte | (tdi_bit << 7));
469 * Function get_tms_buffer_requirements
470 * returns what clock_tms() will consume if called with
473 static inline int get_tms_buffer_requirements(int bit_count)
475 return ((bit_count + 6)/7) * 3;
479 * Function move_to_state
480 * moves the TAP controller from the current state to a
481 * \a goal_state through a path given by tap_get_tms_path(). State transition
482 * logging is performed by delegation to clock_tms().
484 * @param goal_state is the destination state for the move.
486 static void move_to_state(tap_state_t goal_state)
488 tap_state_t start_state = tap_get_state();
490 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
491 lookup of the required TMS pattern to move to this state from the
495 /* do the 2 lookups */
496 int tms_bits = tap_get_tms_path(start_state, goal_state);
497 int tms_count = tap_get_tms_path_len(start_state, goal_state);
499 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
501 clock_tms(0x4b, tms_bits, tms_count, 0);
504 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
506 #if BUILD_FT2232_FTD2XX == 1
508 DWORD dw_bytes_written = 0;
509 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
511 *bytes_written = dw_bytes_written;
512 LOG_ERROR("FT_Write returned: %lu", status);
513 return ERROR_JTAG_DEVICE_ERROR;
517 *bytes_written = dw_bytes_written;
519 #elif BUILD_FT2232_LIBFTDI == 1
521 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
524 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
525 return ERROR_JTAG_DEVICE_ERROR;
529 *bytes_written = retval;
533 if (*bytes_written != (uint32_t)size)
535 return ERROR_JTAG_DEVICE_ERROR;
541 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
543 #if BUILD_FT2232_FTD2XX == 1
549 while ((*bytes_read < size) && timeout--)
551 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
552 *bytes_read, &dw_bytes_read)) != FT_OK)
555 LOG_ERROR("FT_Read returned: %lu", status);
556 return ERROR_JTAG_DEVICE_ERROR;
558 *bytes_read += dw_bytes_read;
561 #elif BUILD_FT2232_LIBFTDI == 1
563 int timeout = LIBFTDI_READ_RETRY_COUNT;
566 while ((*bytes_read < size) && timeout--)
568 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
571 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
572 return ERROR_JTAG_DEVICE_ERROR;
574 *bytes_read += retval;
579 if (*bytes_read < size)
581 LOG_ERROR("couldn't read enough bytes from "
582 "FT2232 device (%i < %i)",
583 (unsigned)*bytes_read,
585 return ERROR_JTAG_DEVICE_ERROR;
591 static bool ft2232_device_is_highspeed(void)
593 #if BUILD_FT2232_FTD2XX == 1
594 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
595 #elif BUILD_FT2232_LIBFTDI == 1
596 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
601 * Commands that only apply to the FT2232H and FT4232H devices.
602 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
603 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
606 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
608 uint8_t buf = enable ? 0x96 : 0x97;
609 LOG_DEBUG("%2.2x", buf);
611 uint32_t bytes_written;
614 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
616 LOG_ERROR("couldn't write command to %s adaptive clocking"
617 , enable ? "enable" : "disable");
625 * Enable/disable the clk divide by 5 of the 60MHz master clock.
626 * This result in a JTAG clock speed range of 91.553Hz-6MHz
627 * respective 457.763Hz-30MHz.
629 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
631 uint32_t bytes_written;
632 uint8_t buf = enable ? 0x8b : 0x8a;
634 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
636 LOG_ERROR("couldn't write command to %s clk divide by 5"
637 , enable ? "enable" : "disable");
638 return ERROR_JTAG_INIT_FAILED;
640 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
641 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
646 static int ft2232_speed(int speed)
650 uint32_t bytes_written;
653 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
654 if (ft2232_device_is_highspeed())
655 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
656 else if (enable_adaptive_clocking)
658 LOG_ERROR("ft2232 device %lu does not support RTCK"
659 , (long unsigned int)ftdi_device);
663 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
666 buf[0] = 0x86; /* command "set divisor" */
667 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
668 buf[2] = (speed >> 8) & 0xff; /* valueH */
670 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
671 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
673 LOG_ERROR("couldn't set FT2232 TCK speed");
680 static int ft2232_speed_div(int speed, int* khz)
682 /* Take a look in the FT2232 manual,
683 * AN2232C-01 Command Processor for
684 * MPSSE and MCU Host Bus. Chapter 3.8 */
686 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
691 static int ft2232_khz(int khz, int* jtag_speed)
695 if (ft2232_device_is_highspeed())
697 *jtag_speed = RTCK_SPEED;
702 LOG_DEBUG("RCLK not supported");
707 /* Take a look in the FT2232 manual,
708 * AN2232C-01 Command Processor for
709 * MPSSE and MCU Host Bus. Chapter 3.8
711 * We will calc here with a multiplier
712 * of 10 for better rounding later. */
714 /* Calc speed, (ft2232_max_tck / khz) - 1 */
715 /* Use 65000 for better rounding */
716 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
718 /* Add 0.9 for rounding */
721 /* Calc real speed */
722 *jtag_speed = *jtag_speed / 10;
724 /* Check if speed is greater than 0 */
730 /* Check max value */
731 if (*jtag_speed > 0xFFFF)
733 *jtag_speed = 0xFFFF;
739 static void ft2232_end_state(tap_state_t state)
741 if (tap_is_state_stable(state))
742 tap_set_end_state(state);
745 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
750 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
752 int num_bytes = (scan_size + 7) / 8;
753 int bits_left = scan_size;
756 while (num_bytes-- > 1)
758 buffer[cur_byte++] = buffer_read();
762 buffer[cur_byte] = 0x0;
764 /* There is one more partial byte left from the clock data in/out instructions */
767 buffer[cur_byte] = buffer_read() >> 1;
769 /* 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 */
770 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
773 static void ft2232_debug_dump_buffer(void)
779 for (i = 0; i < ft2232_buffer_size; i++)
781 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
784 LOG_DEBUG("%s", line);
790 LOG_DEBUG("%s", line);
793 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
795 struct jtag_command* cmd;
800 uint32_t bytes_written = 0;
801 uint32_t bytes_read = 0;
803 #ifdef _DEBUG_USB_IO_
804 struct timeval start, inter, inter2, end;
805 struct timeval d_inter, d_inter2, d_end;
808 #ifdef _DEBUG_USB_COMMS_
809 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
810 ft2232_debug_dump_buffer();
813 #ifdef _DEBUG_USB_IO_
814 gettimeofday(&start, NULL);
817 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
819 LOG_ERROR("couldn't write MPSSE commands to FT2232");
823 #ifdef _DEBUG_USB_IO_
824 gettimeofday(&inter, NULL);
827 if (ft2232_expect_read)
829 /* FIXME this "timeout" is never changed ... */
830 int timeout = LIBFTDI_READ_RETRY_COUNT;
831 ft2232_buffer_size = 0;
833 #ifdef _DEBUG_USB_IO_
834 gettimeofday(&inter2, NULL);
837 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
839 LOG_ERROR("couldn't read from FT2232");
843 #ifdef _DEBUG_USB_IO_
844 gettimeofday(&end, NULL);
846 timeval_subtract(&d_inter, &inter, &start);
847 timeval_subtract(&d_inter2, &inter2, &start);
848 timeval_subtract(&d_end, &end, &start);
850 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
851 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
852 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
853 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
856 ft2232_buffer_size = bytes_read;
858 if (ft2232_expect_read != ft2232_buffer_size)
860 LOG_ERROR("ft2232_expect_read (%i) != "
861 "ft2232_buffer_size (%i) "
865 LIBFTDI_READ_RETRY_COUNT - timeout);
866 ft2232_debug_dump_buffer();
871 #ifdef _DEBUG_USB_COMMS_
872 LOG_DEBUG("read buffer (%i retries): %i bytes",
873 LIBFTDI_READ_RETRY_COUNT - timeout,
875 ft2232_debug_dump_buffer();
879 ft2232_expect_read = 0;
880 ft2232_read_pointer = 0;
882 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
883 * that wasn't handled by a caller-provided error handler
893 type = jtag_scan_type(cmd->cmd.scan);
894 if (type != SCAN_OUT)
896 scan_size = jtag_scan_size(cmd->cmd.scan);
897 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
898 ft2232_read_scan(type, buffer, scan_size);
899 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
900 retval = ERROR_JTAG_QUEUE_FAILED;
912 ft2232_buffer_size = 0;
918 * Function ft2232_add_pathmove
919 * moves the TAP controller from the current state to a new state through the
920 * given path, where path is an array of tap_state_t's.
922 * @param path is an array of tap_stat_t which gives the states to traverse through
923 * ending with the last state at path[num_states-1]
924 * @param num_states is the count of state steps to move through
926 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
930 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
934 /* this loop verifies that the path is legal and logs each state in the path */
937 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
939 int num_states_batch = num_states > 7 ? 7 : num_states;
941 /* command "Clock Data to TMS/CS Pin (no Read)" */
944 /* number of states remaining */
945 buffer_write(num_states_batch - 1);
947 while (num_states_batch--) {
948 /* either TMS=0 or TMS=1 must work ... */
949 if (tap_state_transition(tap_get_state(), false)
950 == path[state_count])
951 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
952 else if (tap_state_transition(tap_get_state(), true)
953 == path[state_count])
954 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
956 /* ... or else the caller goofed BADLY */
958 LOG_ERROR("BUG: %s -> %s isn't a valid "
959 "TAP state transition",
960 tap_state_name(tap_get_state()),
961 tap_state_name(path[state_count]));
965 tap_set_state(path[state_count]);
970 buffer_write(tms_byte);
972 tap_set_end_state(tap_get_state());
975 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
977 int num_bytes = (scan_size + 7) / 8;
978 int bits_left = scan_size;
984 if (tap_get_state() != TAP_DRSHIFT)
986 move_to_state(TAP_DRSHIFT);
991 if (tap_get_state() != TAP_IRSHIFT)
993 move_to_state(TAP_IRSHIFT);
997 /* add command for complete bytes */
998 while (num_bytes > 1)
1001 if (type == SCAN_IO)
1003 /* Clock Data Bytes In and Out LSB First */
1005 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1007 else if (type == SCAN_OUT)
1009 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1011 /* LOG_DEBUG("added TDI bytes (o)"); */
1013 else if (type == SCAN_IN)
1015 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1017 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1020 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1021 num_bytes -= thisrun_bytes;
1023 buffer_write((uint8_t) (thisrun_bytes - 1));
1024 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1026 if (type != SCAN_IN)
1028 /* add complete bytes */
1029 while (thisrun_bytes-- > 0)
1031 buffer_write(buffer[cur_byte++]);
1035 else /* (type == SCAN_IN) */
1037 bits_left -= 8 * (thisrun_bytes);
1041 /* the most signifcant bit is scanned during TAP movement */
1042 if (type != SCAN_IN)
1043 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1047 /* process remaining bits but the last one */
1050 if (type == SCAN_IO)
1052 /* Clock Data Bits In and Out LSB First */
1054 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1056 else if (type == SCAN_OUT)
1058 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1060 /* LOG_DEBUG("added TDI bits (o)"); */
1062 else if (type == SCAN_IN)
1064 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1066 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1069 buffer_write(bits_left - 2);
1070 if (type != SCAN_IN)
1071 buffer_write(buffer[cur_byte]);
1074 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1075 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1077 if (type == SCAN_IO)
1079 /* Clock Data Bits In and Out LSB First */
1081 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1083 else if (type == SCAN_OUT)
1085 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1087 /* LOG_DEBUG("added TDI bits (o)"); */
1089 else if (type == SCAN_IN)
1091 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1093 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1096 buffer_write(last_bit);
1104 /* move from Shift-IR/DR to end state */
1105 if (type != SCAN_OUT)
1107 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1108 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1111 /* Clock Data to TMS/CS Pin with Read */
1116 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1117 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1118 /* Clock Data to TMS/CS Pin (no Read) */
1122 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1123 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1126 if (tap_get_state() != tap_get_end_state())
1128 move_to_state(tap_get_end_state());
1132 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1134 int num_bytes = (scan_size + 7) / 8;
1135 int bits_left = scan_size;
1138 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1139 uint8_t* receive_pointer = receive_buffer;
1140 uint32_t bytes_written;
1141 uint32_t bytes_read;
1143 int thisrun_read = 0;
1147 LOG_ERROR("BUG: large IR scans are not supported");
1151 if (tap_get_state() != TAP_DRSHIFT)
1153 move_to_state(TAP_DRSHIFT);
1156 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1158 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1161 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1162 ft2232_buffer_size, (int)bytes_written);
1163 ft2232_buffer_size = 0;
1165 /* add command for complete bytes */
1166 while (num_bytes > 1)
1170 if (type == SCAN_IO)
1172 /* Clock Data Bytes In and Out LSB First */
1174 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1176 else if (type == SCAN_OUT)
1178 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1180 /* LOG_DEBUG("added TDI bytes (o)"); */
1182 else if (type == SCAN_IN)
1184 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1186 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1189 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1190 thisrun_read = thisrun_bytes;
1191 num_bytes -= thisrun_bytes;
1192 buffer_write((uint8_t) (thisrun_bytes - 1));
1193 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1195 if (type != SCAN_IN)
1197 /* add complete bytes */
1198 while (thisrun_bytes-- > 0)
1200 buffer_write(buffer[cur_byte]);
1205 else /* (type == SCAN_IN) */
1207 bits_left -= 8 * (thisrun_bytes);
1210 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1212 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1215 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1217 (int)bytes_written);
1218 ft2232_buffer_size = 0;
1220 if (type != SCAN_OUT)
1222 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1224 LOG_ERROR("couldn't read from FT2232");
1227 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1230 receive_pointer += bytes_read;
1236 /* the most signifcant bit is scanned during TAP movement */
1237 if (type != SCAN_IN)
1238 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1242 /* process remaining bits but the last one */
1245 if (type == SCAN_IO)
1247 /* Clock Data Bits In and Out LSB First */
1249 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1251 else if (type == SCAN_OUT)
1253 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1255 /* LOG_DEBUG("added TDI bits (o)"); */
1257 else if (type == SCAN_IN)
1259 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1261 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1263 buffer_write(bits_left - 2);
1264 if (type != SCAN_IN)
1265 buffer_write(buffer[cur_byte]);
1267 if (type != SCAN_OUT)
1271 if (tap_get_end_state() == TAP_DRSHIFT)
1273 if (type == SCAN_IO)
1275 /* Clock Data Bits In and Out LSB First */
1277 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1279 else if (type == SCAN_OUT)
1281 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1283 /* LOG_DEBUG("added TDI bits (o)"); */
1285 else if (type == SCAN_IN)
1287 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1289 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1292 buffer_write(last_bit);
1296 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1297 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1300 /* move from Shift-IR/DR to end state */
1301 if (type != SCAN_OUT)
1303 /* Clock Data to TMS/CS Pin with Read */
1305 /* LOG_DEBUG("added TMS scan (read)"); */
1309 /* Clock Data to TMS/CS Pin (no Read) */
1311 /* LOG_DEBUG("added TMS scan (no read)"); */
1314 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1315 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1318 if (type != SCAN_OUT)
1321 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1323 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1326 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1328 (int)bytes_written);
1329 ft2232_buffer_size = 0;
1331 if (type != SCAN_OUT)
1333 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1335 LOG_ERROR("couldn't read from FT2232");
1338 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1341 receive_pointer += bytes_read;
1347 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1349 int predicted_size = 3;
1350 int num_bytes = (scan_size - 1) / 8;
1352 if (tap_get_state() != TAP_DRSHIFT)
1353 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1355 if (type == SCAN_IN) /* only from device to host */
1357 /* complete bytes */
1358 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1360 /* remaining bits - 1 (up to 7) */
1361 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1363 else /* host to device, or bidirectional */
1365 /* complete bytes */
1366 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1368 /* remaining bits -1 (up to 7) */
1369 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1372 return predicted_size;
1375 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1377 int predicted_size = 0;
1379 if (type != SCAN_OUT)
1381 /* complete bytes */
1382 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1384 /* remaining bits - 1 */
1385 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1387 /* last bit (from TMS scan) */
1388 predicted_size += 1;
1391 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1393 return predicted_size;
1396 /* semi-generic FT2232/FT4232 reset code */
1397 static void ftx23_reset(int trst, int srst)
1399 enum reset_types jtag_reset_config = jtag_get_reset_config();
1402 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1403 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1405 low_output &= ~nTRST; /* switch output low */
1409 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1410 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1412 low_output |= nTRST; /* switch output high */
1417 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1418 low_output &= ~nSRST; /* switch output low */
1420 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1424 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1425 low_output |= nSRST; /* switch output high */
1427 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1430 /* command "set data bits low byte" */
1432 buffer_write(low_output);
1433 buffer_write(low_direction);
1436 static void jtagkey_reset(int trst, int srst)
1438 enum reset_types jtag_reset_config = jtag_get_reset_config();
1441 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1442 high_output &= ~nTRSTnOE;
1444 high_output &= ~nTRST;
1448 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1449 high_output |= nTRSTnOE;
1451 high_output |= nTRST;
1456 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1457 high_output &= ~nSRST;
1459 high_output &= ~nSRSTnOE;
1463 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1464 high_output |= nSRST;
1466 high_output |= nSRSTnOE;
1469 /* command "set data bits high byte" */
1471 buffer_write(high_output);
1472 buffer_write(high_direction);
1473 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1477 static void olimex_jtag_reset(int trst, int srst)
1479 enum reset_types jtag_reset_config = jtag_get_reset_config();
1482 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1483 high_output &= ~nTRSTnOE;
1485 high_output &= ~nTRST;
1489 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1490 high_output |= nTRSTnOE;
1492 high_output |= nTRST;
1497 high_output |= nSRST;
1501 high_output &= ~nSRST;
1504 /* command "set data bits high byte" */
1506 buffer_write(high_output);
1507 buffer_write(high_direction);
1508 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1512 static void axm0432_jtag_reset(int trst, int srst)
1516 tap_set_state(TAP_RESET);
1517 high_output &= ~nTRST;
1521 high_output |= nTRST;
1526 high_output &= ~nSRST;
1530 high_output |= nSRST;
1533 /* command "set data bits low byte" */
1535 buffer_write(high_output);
1536 buffer_write(high_direction);
1537 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1541 static void flyswatter_reset(int trst, int srst)
1545 low_output &= ~nTRST;
1549 low_output |= nTRST;
1554 low_output |= nSRST;
1558 low_output &= ~nSRST;
1561 /* command "set data bits low byte" */
1563 buffer_write(low_output);
1564 buffer_write(low_direction);
1565 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1568 static void turtle_reset(int trst, int srst)
1574 low_output |= nSRST;
1578 low_output &= ~nSRST;
1581 /* command "set data bits low byte" */
1583 buffer_write(low_output);
1584 buffer_write(low_direction);
1585 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1588 static void comstick_reset(int trst, int srst)
1592 high_output &= ~nTRST;
1596 high_output |= nTRST;
1601 high_output &= ~nSRST;
1605 high_output |= nSRST;
1608 /* command "set data bits high byte" */
1610 buffer_write(high_output);
1611 buffer_write(high_direction);
1612 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1616 static void stm32stick_reset(int trst, int srst)
1620 high_output &= ~nTRST;
1624 high_output |= nTRST;
1629 low_output &= ~nSRST;
1633 low_output |= nSRST;
1636 /* command "set data bits low byte" */
1638 buffer_write(low_output);
1639 buffer_write(low_direction);
1641 /* command "set data bits high byte" */
1643 buffer_write(high_output);
1644 buffer_write(high_direction);
1645 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1649 static void sheevaplug_reset(int trst, int srst)
1652 high_output &= ~nTRST;
1654 high_output |= nTRST;
1657 high_output &= ~nSRSTnOE;
1659 high_output |= nSRSTnOE;
1661 /* command "set data bits high byte" */
1663 buffer_write(high_output);
1664 buffer_write(high_direction);
1665 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1668 static void redbee_reset(int trst, int srst)
1672 tap_set_state(TAP_RESET);
1673 high_output &= ~nTRST;
1677 high_output |= nTRST;
1682 high_output &= ~nSRST;
1686 high_output |= nSRST;
1689 /* command "set data bits low byte" */
1691 buffer_write(high_output);
1692 buffer_write(high_direction);
1693 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1694 "high_direction: 0x%2.2x", trst, srst, high_output,
1698 static void xds100v2_reset(int trst, int srst)
1702 tap_set_state(TAP_RESET);
1703 high_output &= ~nTRST;
1707 high_output |= nTRST;
1712 high_output |= nSRST;
1716 high_output &= ~nSRST;
1719 /* command "set data bits low byte" */
1721 buffer_write(high_output);
1722 buffer_write(high_direction);
1723 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1724 "high_direction: 0x%2.2x", trst, srst, high_output,
1728 static int ft2232_execute_runtest(struct jtag_command *cmd)
1732 int predicted_size = 0;
1735 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1736 cmd->cmd.runtest->num_cycles,
1737 tap_state_name(cmd->cmd.runtest->end_state));
1739 /* only send the maximum buffer size that FT2232C can handle */
1741 if (tap_get_state() != TAP_IDLE)
1742 predicted_size += 3;
1743 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1744 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1745 predicted_size += 3;
1746 if (tap_get_end_state() != TAP_IDLE)
1747 predicted_size += 3;
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 if (tap_get_state() != TAP_IDLE)
1757 move_to_state(TAP_IDLE);
1760 i = cmd->cmd.runtest->num_cycles;
1763 /* there are no state transitions in this code, so omit state tracking */
1765 /* command "Clock Data to TMS/CS Pin (no Read)" */
1769 buffer_write((i > 7) ? 6 : (i - 1));
1774 i -= (i > 7) ? 7 : i;
1775 /* LOG_DEBUG("added TMS scan (no read)"); */
1778 ft2232_end_state(cmd->cmd.runtest->end_state);
1780 if (tap_get_state() != tap_get_end_state())
1782 move_to_state(tap_get_end_state());
1786 DEBUG_JTAG_IO("runtest: %i, end in %s",
1787 cmd->cmd.runtest->num_cycles,
1788 tap_state_name(tap_get_end_state()));
1792 static int ft2232_execute_statemove(struct jtag_command *cmd)
1794 int predicted_size = 0;
1795 int retval = ERROR_OK;
1797 DEBUG_JTAG_IO("statemove end in %s",
1798 tap_state_name(cmd->cmd.statemove->end_state));
1800 /* only send the maximum buffer size that FT2232C can handle */
1802 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1804 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1805 retval = ERROR_JTAG_QUEUE_FAILED;
1809 ft2232_end_state(cmd->cmd.statemove->end_state);
1811 /* For TAP_RESET, ignore the current recorded state. It's often
1812 * wrong at server startup, and this transation is critical whenever
1815 if (tap_get_end_state() == TAP_RESET) {
1816 clock_tms(0x4b, 0xff, 5, 0);
1819 /* shortest-path move to desired end state */
1820 } else if (tap_get_state() != tap_get_end_state())
1822 move_to_state(tap_get_end_state());
1830 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1831 * (or SWD) state machine.
1833 static int ft2232_execute_tms(struct jtag_command *cmd)
1835 int retval = ERROR_OK;
1836 unsigned num_bits = cmd->cmd.tms->num_bits;
1837 const uint8_t *bits = cmd->cmd.tms->bits;
1840 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1842 /* only send the maximum buffer size that FT2232C can handle */
1843 count = 3 * DIV_ROUND_UP(num_bits, 4);
1844 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1845 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1846 retval = ERROR_JTAG_QUEUE_FAILED;
1852 /* Shift out in batches of at most 6 bits; there's a report of an
1853 * FT2232 bug in this area, where shifting exactly 7 bits can make
1854 * problems with TMS signaling for the last clock cycle:
1856 * http://developer.intra2net.com/mailarchive/html/
1857 * libftdi/2009/msg00292.html
1859 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1861 * Note that pathmoves in JTAG are not often seven bits, so that
1862 * isn't a particularly likely situation outside of "special"
1863 * signaling such as switching between JTAG and SWD modes.
1866 if (num_bits <= 6) {
1868 buffer_write(num_bits - 1);
1869 buffer_write(*bits & 0x3f);
1873 /* Yes, this is lazy ... we COULD shift out more data
1874 * bits per operation, but doing it in nybbles is easy
1878 buffer_write(*bits & 0xf);
1881 count = (num_bits > 4) ? 4 : num_bits;
1884 buffer_write(count - 1);
1885 buffer_write((*bits >> 4) & 0xf);
1895 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1897 int predicted_size = 0;
1898 int retval = ERROR_OK;
1900 tap_state_t* path = cmd->cmd.pathmove->path;
1901 int num_states = cmd->cmd.pathmove->num_states;
1903 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1904 tap_state_name(tap_get_state()),
1905 tap_state_name(path[num_states-1]));
1907 /* only send the maximum buffer size that FT2232C can handle */
1908 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1909 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1911 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1912 retval = ERROR_JTAG_QUEUE_FAILED;
1918 ft2232_add_pathmove(path, num_states);
1924 static int ft2232_execute_scan(struct jtag_command *cmd)
1927 int scan_size; /* size of IR or DR scan */
1928 int predicted_size = 0;
1929 int retval = ERROR_OK;
1931 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1933 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1935 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1937 predicted_size = ft2232_predict_scan_out(scan_size, type);
1938 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1940 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1941 /* unsent commands before this */
1942 if (first_unsent != cmd)
1943 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1944 retval = ERROR_JTAG_QUEUE_FAILED;
1946 /* current command */
1947 ft2232_end_state(cmd->cmd.scan->end_state);
1948 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1950 first_unsent = cmd->next;
1955 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1957 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1960 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1961 retval = ERROR_JTAG_QUEUE_FAILED;
1965 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1966 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1967 ft2232_end_state(cmd->cmd.scan->end_state);
1968 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1972 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1973 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1974 tap_state_name(tap_get_end_state()));
1979 static int ft2232_execute_reset(struct jtag_command *cmd)
1982 int predicted_size = 0;
1985 DEBUG_JTAG_IO("reset trst: %i srst %i",
1986 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1988 /* only send the maximum buffer size that FT2232C can handle */
1990 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1992 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1993 retval = ERROR_JTAG_QUEUE_FAILED;
1998 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
2000 tap_set_state(TAP_RESET);
2003 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2006 DEBUG_JTAG_IO("trst: %i, srst: %i",
2007 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2011 static int ft2232_execute_sleep(struct jtag_command *cmd)
2016 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2018 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2019 retval = ERROR_JTAG_QUEUE_FAILED;
2020 first_unsent = cmd->next;
2021 jtag_sleep(cmd->cmd.sleep->us);
2022 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2024 tap_state_name(tap_get_state()));
2028 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2033 /* this is only allowed while in a stable state. A check for a stable
2034 * state was done in jtag_add_clocks()
2036 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2037 retval = ERROR_JTAG_QUEUE_FAILED;
2038 DEBUG_JTAG_IO("clocks %i while in %s",
2039 cmd->cmd.stableclocks->num_cycles,
2040 tap_state_name(tap_get_state()));
2044 static int ft2232_execute_command(struct jtag_command *cmd)
2050 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2051 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2052 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2053 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2054 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2055 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2056 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2058 retval = ft2232_execute_tms(cmd);
2061 LOG_ERROR("BUG: unknown JTAG command type encountered");
2062 retval = ERROR_JTAG_QUEUE_FAILED;
2068 static int ft2232_execute_queue(void)
2070 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2073 first_unsent = cmd; /* next command that has to be sent */
2076 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2077 * that wasn't handled by a caller-provided error handler
2081 ft2232_buffer_size = 0;
2082 ft2232_expect_read = 0;
2084 /* blink, if the current layout has that feature */
2090 /* fill the write buffer with the desired command */
2091 if (ft2232_execute_command(cmd) != ERROR_OK)
2092 retval = ERROR_JTAG_QUEUE_FAILED;
2093 /* Start reading input before FT2232 TX buffer fills up.
2094 * Sometimes this happens because we don't know the
2095 * length of the last command before we execute it. So
2096 * we simple inform the user.
2100 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE )
2102 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1) )
2103 LOG_WARNING("read buffer size looks to high");
2104 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2105 retval = ERROR_JTAG_QUEUE_FAILED;
2110 if (require_send > 0)
2111 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2112 retval = ERROR_JTAG_QUEUE_FAILED;
2117 #if BUILD_FT2232_FTD2XX == 1
2118 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2122 char SerialNumber[16];
2123 char Description[64];
2124 DWORD openex_flags = 0;
2125 char* openex_string = NULL;
2126 uint8_t latency_timer;
2128 if (layout == NULL) {
2129 LOG_WARNING("No ft2232 layout specified'");
2130 return ERROR_JTAG_INIT_FAILED;
2133 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2136 /* Add non-standard Vid/Pid to the linux driver */
2137 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2139 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2143 if (ft2232_device_desc && ft2232_serial)
2145 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2146 ft2232_device_desc = NULL;
2149 if (ft2232_device_desc)
2151 openex_string = ft2232_device_desc;
2152 openex_flags = FT_OPEN_BY_DESCRIPTION;
2154 else if (ft2232_serial)
2156 openex_string = ft2232_serial;
2157 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2161 LOG_ERROR("neither device description nor serial number specified");
2162 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2164 return ERROR_JTAG_INIT_FAILED;
2167 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2168 if (status != FT_OK) {
2169 /* under Win32, the FTD2XX driver appends an "A" to the end
2170 * of the description, if we tried by the desc, then
2171 * try by the alternate "A" description. */
2172 if (openex_string == ft2232_device_desc) {
2173 /* Try the alternate method. */
2174 openex_string = ft2232_device_desc_A;
2175 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2176 if (status == FT_OK) {
2177 /* yea, the "alternate" method worked! */
2179 /* drat, give the user a meaningfull message.
2180 * telling the use we tried *BOTH* methods. */
2181 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2183 ft2232_device_desc_A);
2188 if (status != FT_OK)
2194 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2196 return ERROR_JTAG_INIT_FAILED;
2198 LOG_ERROR("unable to open ftdi device: %lu", status);
2199 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2200 if (status == FT_OK)
2202 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2205 for (i = 0; i < num_devices; i++)
2206 desc_array[i] = malloc(64);
2208 desc_array[num_devices] = NULL;
2210 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2212 if (status == FT_OK)
2214 LOG_ERROR("ListDevices: %lu", num_devices);
2215 for (i = 0; i < num_devices; i++)
2216 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2219 for (i = 0; i < num_devices; i++)
2220 free(desc_array[i]);
2226 LOG_ERROR("ListDevices: NONE");
2228 return ERROR_JTAG_INIT_FAILED;
2231 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2233 LOG_ERROR("unable to set latency timer: %lu", status);
2234 return ERROR_JTAG_INIT_FAILED;
2237 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2239 LOG_ERROR("unable to get latency timer: %lu", status);
2240 return ERROR_JTAG_INIT_FAILED;
2244 LOG_DEBUG("current latency timer: %i", latency_timer);
2247 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2249 LOG_ERROR("unable to set timeouts: %lu", status);
2250 return ERROR_JTAG_INIT_FAILED;
2253 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2255 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2256 return ERROR_JTAG_INIT_FAILED;
2259 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2261 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2262 return ERROR_JTAG_INIT_FAILED;
2266 static const char* type_str[] =
2267 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2268 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2269 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2270 ? ftdi_device : FT_DEVICE_UNKNOWN;
2271 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2272 LOG_INFO("deviceID: %lu", deviceID);
2273 LOG_INFO("SerialNumber: %s", SerialNumber);
2274 LOG_INFO("Description: %s", Description);
2280 static int ft2232_purge_ftd2xx(void)
2284 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2286 LOG_ERROR("error purging ftd2xx device: %lu", status);
2287 return ERROR_JTAG_INIT_FAILED;
2293 #endif /* BUILD_FT2232_FTD2XX == 1 */
2295 #if BUILD_FT2232_LIBFTDI == 1
2296 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2298 uint8_t latency_timer;
2300 if (layout == NULL) {
2301 LOG_WARNING("No ft2232 layout specified'");
2302 return ERROR_JTAG_INIT_FAILED;
2305 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2306 layout->name, vid, pid);
2308 if (ftdi_init(&ftdic) < 0)
2309 return ERROR_JTAG_INIT_FAILED;
2311 /* default to INTERFACE_A */
2312 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2314 if (ftdi_set_interface(&ftdic, channel) < 0)
2316 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2317 return ERROR_JTAG_INIT_FAILED;
2320 /* context, vendor id, product id */
2321 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2325 LOG_WARNING("unable to open ftdi device (trying more): %s",
2328 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2330 return ERROR_JTAG_INIT_FAILED;
2333 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2334 if (ftdi_usb_reset(&ftdic) < 0)
2336 LOG_ERROR("unable to reset ftdi device");
2337 return ERROR_JTAG_INIT_FAILED;
2340 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2342 LOG_ERROR("unable to set latency timer");
2343 return ERROR_JTAG_INIT_FAILED;
2346 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2348 LOG_ERROR("unable to get latency timer");
2349 return ERROR_JTAG_INIT_FAILED;
2353 LOG_DEBUG("current latency timer: %i", latency_timer);
2356 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2358 ftdi_device = ftdic.type;
2359 static const char* type_str[] =
2360 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2361 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2362 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2363 ? ftdi_device : no_of_known_types;
2364 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2368 static int ft2232_purge_libftdi(void)
2370 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2372 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2373 return ERROR_JTAG_INIT_FAILED;
2379 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2381 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2384 uint32_t bytes_written;
2386 buf[0] = 0x80; /* command "set data bits low byte" */
2387 buf[1] = value; /* value */
2388 buf[2] = direction; /* direction */
2390 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2392 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2394 LOG_ERROR("couldn't initialize data bits low byte");
2395 return ERROR_JTAG_INIT_FAILED;
2401 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2404 uint32_t bytes_written;
2406 buf[0] = 0x82; /* command "set data bits high byte" */
2407 buf[1] = value; /* value */
2408 buf[2] = direction; /* direction */
2410 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2412 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2414 LOG_ERROR("couldn't initialize data bits high byte");
2415 return ERROR_JTAG_INIT_FAILED;
2421 static int ft2232_init(void)
2425 uint32_t bytes_written;
2427 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2429 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2433 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2436 if (layout == NULL) {
2437 LOG_WARNING("No ft2232 layout specified'");
2438 return ERROR_JTAG_INIT_FAILED;
2441 for (int i = 0; 1; i++)
2444 * "more indicates that there are more IDs to try, so we should
2445 * not print an error for an ID mismatch (but for anything
2448 * try_more indicates that the error code returned indicates an
2449 * ID mismatch (and nothing else) and that we should proceeed
2450 * with the next ID pair.
2452 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2455 #if BUILD_FT2232_FTD2XX == 1
2456 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2458 #elif BUILD_FT2232_LIBFTDI == 1
2459 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2460 more, &try_more, layout->channel);
2464 if (!more || !try_more)
2468 ft2232_buffer_size = 0;
2469 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2471 if (layout->init() != ERROR_OK)
2472 return ERROR_JTAG_INIT_FAILED;
2474 if (ft2232_device_is_highspeed())
2476 #ifndef BUILD_FT2232_HIGHSPEED
2477 #if BUILD_FT2232_FTD2XX == 1
2478 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2479 #elif BUILD_FT2232_LIBFTDI == 1
2480 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2483 /* make sure the legacy mode is disabled */
2484 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2485 return ERROR_JTAG_INIT_FAILED;
2489 retval = jtag_get_speed(&jtag_speed_var);
2490 if (retval != ERROR_OK)
2492 ft2232_speed(jtag_speed_var);
2494 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2495 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2497 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2498 return ERROR_JTAG_INIT_FAILED;
2501 #if BUILD_FT2232_FTD2XX == 1
2502 return ft2232_purge_ftd2xx();
2503 #elif BUILD_FT2232_LIBFTDI == 1
2504 return ft2232_purge_libftdi();
2510 /** Updates defaults for DBUS signals: the four JTAG signals
2511 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2513 static inline void ftx232_dbus_init(void)
2516 low_direction = 0x0b;
2519 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2520 * the four GPIOL signals. Initialization covers value and direction,
2521 * as customized for each layout.
2523 static int ftx232_dbus_write(void)
2525 enum reset_types jtag_reset_config = jtag_get_reset_config();
2526 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2528 low_direction &= ~nTRSTnOE; /* nTRST input */
2529 low_output &= ~nTRST; /* nTRST = 0 */
2533 low_direction |= nTRSTnOE; /* nTRST output */
2534 low_output |= nTRST; /* nTRST = 1 */
2537 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2539 low_direction |= nSRSTnOE; /* nSRST output */
2540 low_output |= nSRST; /* nSRST = 1 */
2544 low_direction &= ~nSRSTnOE; /* nSRST input */
2545 low_output &= ~nSRST; /* nSRST = 0 */
2548 /* initialize low byte for jtag */
2549 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2551 LOG_ERROR("couldn't initialize FT2232 DBUS");
2552 return ERROR_JTAG_INIT_FAILED;
2558 static int usbjtag_init(void)
2561 * NOTE: This is now _specific_ to the "usbjtag" layout.
2562 * Don't try cram any more layouts into this.
2571 return ftx232_dbus_write();
2574 static int lm3s811_jtag_init(void)
2578 /* There are multiple revisions of LM3S811 eval boards:
2579 * - Rev B (and older?) boards have no SWO trace support.
2580 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2581 * they should use the "luminary_icdi" layout instead.
2588 low_direction = 0x8b;
2590 return ftx232_dbus_write();
2593 static int icdi_jtag_init(void)
2597 /* Most Luminary eval boards support SWO trace output,
2598 * and should use this "luminary_icdi" layout.
2600 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2601 * to switch between JTAG and SWD, or switch the ft2232 UART
2602 * on the second MPSSE channel/interface (BDBUS)
2603 * between (i) the stellaris UART (on Luminary boards)
2604 * or (ii) SWO trace data (generic).
2606 * We come up in JTAG mode and may switch to SWD later (with
2607 * SWO/trace option if SWD is active).
2614 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2615 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2616 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2619 /* GPIOs on second channel/interface (UART) ... */
2620 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2621 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2622 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2627 nSRSTnOE = ICDI_SRST;
2629 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2630 low_output |= ICDI_JTAG_EN;
2631 low_output &= ~ICDI_DBG_ENn;
2633 return ftx232_dbus_write();
2636 static int signalyzer_init(void)
2644 return ftx232_dbus_write();
2647 static int axm0432_jtag_init(void)
2650 low_direction = 0x2b;
2652 /* initialize low byte for jtag */
2653 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2655 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2656 return ERROR_JTAG_INIT_FAILED;
2659 if (strcmp(layout->name, "axm0432_jtag") == 0)
2662 nTRSTnOE = 0x0; /* No output enable for TRST*/
2664 nSRSTnOE = 0x0; /* No output enable for SRST*/
2668 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2673 high_direction = 0x0c;
2675 enum reset_types jtag_reset_config = jtag_get_reset_config();
2676 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2678 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2682 high_output |= nTRST;
2685 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2687 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2691 high_output |= nSRST;
2694 /* initialize high byte for jtag */
2695 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2697 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2698 return ERROR_JTAG_INIT_FAILED;
2704 static int redbee_init(void)
2707 low_direction = 0x2b;
2709 /* initialize low byte for jtag */
2710 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2712 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2713 return ERROR_JTAG_INIT_FAILED;
2717 nTRSTnOE = 0x0; /* No output enable for TRST*/
2719 nSRSTnOE = 0x0; /* No output enable for SRST*/
2722 high_direction = 0x0c;
2724 enum reset_types jtag_reset_config = jtag_get_reset_config();
2725 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2727 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2731 high_output |= nTRST;
2734 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2736 LOG_ERROR("can't set nSRST to push-pull on redbee");
2740 high_output |= nSRST;
2743 /* initialize high byte for jtag */
2744 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2746 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2747 return ERROR_JTAG_INIT_FAILED;
2753 static int jtagkey_init(void)
2756 low_direction = 0x1b;
2758 /* initialize low byte for jtag */
2759 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2761 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2762 return ERROR_JTAG_INIT_FAILED;
2765 if (strcmp(layout->name, "jtagkey") == 0)
2772 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2773 || (strcmp(layout->name, "oocdlink") == 0))
2782 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2787 high_direction = 0x0f;
2789 enum reset_types jtag_reset_config = jtag_get_reset_config();
2790 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2792 high_output |= nTRSTnOE;
2793 high_output &= ~nTRST;
2797 high_output &= ~nTRSTnOE;
2798 high_output |= nTRST;
2801 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2803 high_output &= ~nSRSTnOE;
2804 high_output |= nSRST;
2808 high_output |= nSRSTnOE;
2809 high_output &= ~nSRST;
2812 /* initialize high byte for jtag */
2813 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2815 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2816 return ERROR_JTAG_INIT_FAILED;
2822 static int olimex_jtag_init(void)
2825 low_direction = 0x1b;
2827 /* initialize low byte for jtag */
2828 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2830 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2831 return ERROR_JTAG_INIT_FAILED;
2837 nSRSTnOE = 0x00; /* no output enable for nSRST */
2840 high_direction = 0x0f;
2842 enum reset_types jtag_reset_config = jtag_get_reset_config();
2843 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2845 high_output |= nTRSTnOE;
2846 high_output &= ~nTRST;
2850 high_output &= ~nTRSTnOE;
2851 high_output |= nTRST;
2854 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2856 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2860 high_output &= ~nSRST;
2863 /* turn red LED on */
2864 high_output |= 0x08;
2866 /* initialize high byte for jtag */
2867 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2869 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2870 return ERROR_JTAG_INIT_FAILED;
2876 static int flyswatter_init(void)
2879 low_direction = 0xfb;
2881 /* initialize low byte for jtag */
2882 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2884 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2885 return ERROR_JTAG_INIT_FAILED;
2889 nTRSTnOE = 0x0; /* not output enable for nTRST */
2891 nSRSTnOE = 0x00; /* no output enable for nSRST */
2894 high_direction = 0x0c;
2896 /* turn red LED3 on, LED2 off */
2897 high_output |= 0x08;
2899 /* initialize high byte for jtag */
2900 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2902 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2903 return ERROR_JTAG_INIT_FAILED;
2909 static int turtle_init(void)
2912 low_direction = 0x5b;
2914 /* initialize low byte for jtag */
2915 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2917 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2918 return ERROR_JTAG_INIT_FAILED;
2924 high_direction = 0x0C;
2926 /* initialize high byte for jtag */
2927 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2929 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2930 return ERROR_JTAG_INIT_FAILED;
2936 static int comstick_init(void)
2939 low_direction = 0x0b;
2941 /* initialize low byte for jtag */
2942 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2944 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2945 return ERROR_JTAG_INIT_FAILED;
2949 nTRSTnOE = 0x00; /* no output enable for nTRST */
2951 nSRSTnOE = 0x00; /* no output enable for nSRST */
2954 high_direction = 0x03;
2956 /* initialize high byte for jtag */
2957 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2959 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2960 return ERROR_JTAG_INIT_FAILED;
2966 static int stm32stick_init(void)
2969 low_direction = 0x8b;
2971 /* initialize low byte for jtag */
2972 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2974 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2975 return ERROR_JTAG_INIT_FAILED;
2979 nTRSTnOE = 0x00; /* no output enable for nTRST */
2981 nSRSTnOE = 0x00; /* no output enable for nSRST */
2984 high_direction = 0x03;
2986 /* initialize high byte for jtag */
2987 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2989 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2990 return ERROR_JTAG_INIT_FAILED;
2996 static int sheevaplug_init(void)
2999 low_direction = 0x1b;
3001 /* initialize low byte for jtag */
3002 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3004 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3005 return ERROR_JTAG_INIT_FAILED;
3014 high_direction = 0x0f;
3016 /* nTRST is always push-pull */
3017 high_output &= ~nTRSTnOE;
3018 high_output |= nTRST;
3020 /* nSRST is always open-drain */
3021 high_output |= nSRSTnOE;
3022 high_output &= ~nSRST;
3024 /* initialize high byte for jtag */
3025 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3027 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3028 return ERROR_JTAG_INIT_FAILED;
3034 static int cortino_jtag_init(void)
3037 low_direction = 0x1b;
3039 /* initialize low byte for jtag */
3040 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3042 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3043 return ERROR_JTAG_INIT_FAILED;
3047 nTRSTnOE = 0x00; /* no output enable for nTRST */
3049 nSRSTnOE = 0x00; /* no output enable for nSRST */
3052 high_direction = 0x03;
3054 /* initialize high byte for jtag */
3055 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3057 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3058 return ERROR_JTAG_INIT_FAILED;
3064 static int lisa_l_init(void)
3074 high_direction = 0x18;
3076 /* initialize high byte for jtag */
3077 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3079 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3080 return ERROR_JTAG_INIT_FAILED;
3083 return ftx232_dbus_write();
3086 static int flossjtag_init(void)
3096 high_direction = 0x18;
3098 /* initialize high byte for jtag */
3099 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3101 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3102 return ERROR_JTAG_INIT_FAILED;
3105 return ftx232_dbus_write();
3108 static int xds100v2_init(void)
3111 low_direction = 0x7B;
3113 /* initialize low byte for jtag */
3114 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3116 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3117 return ERROR_JTAG_INIT_FAILED;
3121 nTRSTnOE = 0x0; /* not output enable for nTRST */
3122 nSRST = 0x00; /* TODO: SRST is not supported yet */
3123 nSRSTnOE = 0x00; /* no output enable for nSRST */
3126 high_direction = 0x59;
3128 /* initialize high byte for jtag */
3129 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3131 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3132 return ERROR_JTAG_INIT_FAILED;
3136 high_direction = 0x59;
3138 /* initialize high byte for jtag */
3139 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3141 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3142 return ERROR_JTAG_INIT_FAILED;
3148 static void olimex_jtag_blink(void)
3150 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3151 * ACBUS3 is bit 3 of the GPIOH port
3153 high_output ^= 0x08;
3156 buffer_write(high_output);
3157 buffer_write(high_direction);
3160 static void flyswatter_jtag_blink(void)
3163 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3165 high_output ^= 0x0c;
3168 buffer_write(high_output);
3169 buffer_write(high_direction);
3172 static void turtle_jtag_blink(void)
3175 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3177 if (high_output & 0x08)
3187 buffer_write(high_output);
3188 buffer_write(high_direction);
3191 static void lisa_l_blink(void)
3194 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3196 if (high_output & 0x10)
3206 buffer_write(high_output);
3207 buffer_write(high_direction);
3210 static void flossjtag_blink(void)
3213 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3215 if (high_output & 0x10)
3225 buffer_write(high_output);
3226 buffer_write(high_direction);
3229 static int ft2232_quit(void)
3231 #if BUILD_FT2232_FTD2XX == 1
3234 status = FT_Close(ftdih);
3235 #elif BUILD_FT2232_LIBFTDI == 1
3236 ftdi_usb_close(&ftdic);
3238 ftdi_deinit(&ftdic);
3241 free(ft2232_buffer);
3242 ft2232_buffer = NULL;
3247 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3253 ft2232_device_desc = strdup(CMD_ARGV[0]);
3254 cp = strchr(ft2232_device_desc, 0);
3255 /* under Win32, the FTD2XX driver appends an "A" to the end
3256 * of the description, this examines the given desc
3257 * and creates the 'missing' _A or non_A variable. */
3258 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3259 /* it was, so make this the "A" version. */
3260 ft2232_device_desc_A = ft2232_device_desc;
3261 /* and *CREATE* the non-A version. */
3262 strcpy(buf, ft2232_device_desc);
3263 cp = strchr(buf, 0);
3265 ft2232_device_desc = strdup(buf);
3267 /* <space > A not defined
3269 sprintf(buf, "%s A", ft2232_device_desc);
3270 ft2232_device_desc_A = strdup(buf);
3275 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3281 COMMAND_HANDLER(ft2232_handle_serial_command)
3285 ft2232_serial = strdup(CMD_ARGV[0]);
3289 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3295 COMMAND_HANDLER(ft2232_handle_layout_command)
3297 if (CMD_ARGC != 1) {
3298 LOG_ERROR("Need exactly one argument to ft2232_layout");
3303 LOG_ERROR("already specified ft2232_layout %s",
3305 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3310 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3311 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3317 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3321 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3323 if (CMD_ARGC > MAX_USB_IDS * 2)
3325 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3326 "(maximum is %d pairs)", MAX_USB_IDS);
3327 CMD_ARGC = MAX_USB_IDS * 2;
3329 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3331 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3333 return ERROR_COMMAND_SYNTAX_ERROR;
3334 /* remove the incomplete trailing id */
3339 for (i = 0; i < CMD_ARGC; i += 2)
3341 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3342 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3346 * Explicitly terminate, in case there are multiples instances of
3349 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3354 COMMAND_HANDLER(ft2232_handle_latency_command)
3358 ft2232_latency = atoi(CMD_ARGV[0]);
3362 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3368 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3372 /* 7 bits of either ones or zeros. */
3373 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3375 while (num_cycles > 0)
3377 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3378 * at most 7 bits per invocation. Here we invoke it potentially
3381 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3383 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3385 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3386 retval = ERROR_JTAG_QUEUE_FAILED;
3391 /* there are no state transitions in this code, so omit state tracking */
3393 /* command "Clock Data to TMS/CS Pin (no Read)" */
3397 buffer_write(bitcount_per_command - 1);
3399 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3404 num_cycles -= bitcount_per_command;
3410 /* ---------------------------------------------------------------------
3411 * Support for IceBear JTAG adapter from Section5:
3412 * http://section5.ch/icebear
3414 * Author: Sten, debian@sansys-electronic.com
3417 /* Icebear pin layout
3419 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3420 * GND GND | 4 3| n.c.
3421 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3422 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3423 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3424 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3425 * ADBUS2 TDO |14 13| GND GND
3427 * ADBUS0 O L TCK ACBUS0 GND
3428 * ADBUS1 O L TDI ACBUS1 GND
3429 * ADBUS2 I TDO ACBUS2 n.c.
3430 * ADBUS3 O H TMS ACBUS3 n.c.
3436 static int icebear_jtag_init(void) {
3437 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3438 low_output = 0x08; /* high: TMS; low: TCK TDI */
3442 enum reset_types jtag_reset_config = jtag_get_reset_config();
3443 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3444 low_direction &= ~nTRST; /* nTRST high impedance */
3447 low_direction |= nTRST;
3448 low_output |= nTRST;
3451 low_direction |= nSRST;
3452 low_output |= nSRST;
3454 /* initialize low byte for jtag */
3455 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3456 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3457 return ERROR_JTAG_INIT_FAILED;
3461 high_direction = 0x00;
3463 /* initialize high byte for jtag */
3464 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3465 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3466 return ERROR_JTAG_INIT_FAILED;
3472 static void icebear_jtag_reset(int trst, int srst) {
3475 low_direction |= nTRST;
3476 low_output &= ~nTRST;
3478 else if (trst == 0) {
3479 enum reset_types jtag_reset_config = jtag_get_reset_config();
3480 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3481 low_direction &= ~nTRST;
3483 low_output |= nTRST;
3487 low_output &= ~nSRST;
3489 else if (srst == 0) {
3490 low_output |= nSRST;
3493 /* command "set data bits low byte" */
3495 buffer_write(low_output);
3496 buffer_write(low_direction);
3498 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3501 /* ---------------------------------------------------------------------
3502 * Support for Signalyzer H2 and Signalyzer H4
3503 * JTAG adapter from Xverve Technologies Inc.
3504 * http://www.signalyzer.com or http://www.xverve.com
3506 * Author: Oleg Seiljus, oleg@signalyzer.com
3508 static unsigned char signalyzer_h_side;
3509 static unsigned int signalyzer_h_adapter_type;
3511 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3513 #if BUILD_FT2232_FTD2XX == 1
3514 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3517 #define SIGNALYZER_COMMAND_ADDR 128
3518 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3520 #define SIGNALYZER_COMMAND_VERSION 0x41
3521 #define SIGNALYZER_COMMAND_RESET 0x42
3522 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3523 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3524 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3525 #define SIGNALYZER_COMMAND_LED_SET 0x53
3526 #define SIGNALYZER_COMMAND_ADC 0x54
3527 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3528 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3529 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3530 #define SIGNALYZER_COMMAND_I2C 0x58
3532 #define SIGNALYZER_CHAN_A 1
3533 #define SIGNALYZER_CHAN_B 2
3534 /* LEDS use channel C */
3535 #define SIGNALYZER_CHAN_C 4
3537 #define SIGNALYZER_LED_GREEN 1
3538 #define SIGNALYZER_LED_RED 2
3540 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3541 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3542 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3543 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3544 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3547 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3549 #if BUILD_FT2232_FTD2XX == 1
3550 return FT_WriteEE(ftdih, address, value);
3551 #elif BUILD_FT2232_LIBFTDI == 1
3556 #if BUILD_FT2232_FTD2XX == 1
3557 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3559 return FT_ReadEE(ftdih, address, value);
3563 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3564 int on_time_ms, int off_time_ms, unsigned char cycles)
3566 unsigned char on_time;
3567 unsigned char off_time;
3569 if (on_time_ms < 0xFFFF)
3570 on_time = (unsigned char)(on_time_ms / 62);
3574 off_time = (unsigned char)(off_time_ms / 62);
3576 #if BUILD_FT2232_FTD2XX == 1
3579 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3580 ((uint32_t)(channel << 8) | led))) != FT_OK)
3582 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3583 return ERROR_JTAG_DEVICE_ERROR;
3586 if ((status = signalyzer_h_ctrl_write(
3587 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3588 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3590 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3591 return ERROR_JTAG_DEVICE_ERROR;
3594 if ((status = signalyzer_h_ctrl_write(
3595 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3596 ((uint32_t)cycles))) != FT_OK)
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3599 return ERROR_JTAG_DEVICE_ERROR;
3602 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3603 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3605 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3606 return ERROR_JTAG_DEVICE_ERROR;
3610 #elif BUILD_FT2232_LIBFTDI == 1
3613 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3614 ((uint32_t)(channel << 8) | led))) < 0)
3616 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3617 ftdi_get_error_string(&ftdic));
3618 return ERROR_JTAG_DEVICE_ERROR;
3621 if ((retval = signalyzer_h_ctrl_write(
3622 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3623 ((uint32_t)(on_time << 8) | off_time))) < 0)
3625 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3626 ftdi_get_error_string(&ftdic));
3627 return ERROR_JTAG_DEVICE_ERROR;
3630 if ((retval = signalyzer_h_ctrl_write(
3631 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3632 (uint32_t)cycles)) < 0)
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3635 ftdi_get_error_string(&ftdic));
3636 return ERROR_JTAG_DEVICE_ERROR;
3639 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3640 SIGNALYZER_COMMAND_LED_SET)) < 0)
3642 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3643 ftdi_get_error_string(&ftdic));
3644 return ERROR_JTAG_DEVICE_ERROR;
3651 static int signalyzer_h_init(void)
3653 #if BUILD_FT2232_FTD2XX == 1
3660 uint16_t read_buf[12] = { 0 };
3662 /* turn on center green led */
3663 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3664 0xFFFF, 0x00, 0x00);
3666 /* determine what channel config wants to open
3667 * TODO: change me... current implementation is made to work
3668 * with openocd description parsing.
3670 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3674 signalyzer_h_side = *(end_of_desc - 1);
3675 if (signalyzer_h_side == 'B')
3676 signalyzer_h_side = SIGNALYZER_CHAN_B;
3678 signalyzer_h_side = SIGNALYZER_CHAN_A;
3682 LOG_ERROR("No Channel was specified");
3686 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3689 #if BUILD_FT2232_FTD2XX == 1
3690 /* read signalyzer versionining information */
3691 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3692 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3694 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3695 return ERROR_JTAG_DEVICE_ERROR;
3698 for (i = 0; i < 10; i++)
3700 if ((status = signalyzer_h_ctrl_read(
3701 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3702 &read_buf[i])) != FT_OK)
3704 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3706 return ERROR_JTAG_DEVICE_ERROR;
3710 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3711 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3712 read_buf[4], read_buf[5], read_buf[6]);
3714 /* set gpio register */
3715 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3716 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3718 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3719 return ERROR_JTAG_DEVICE_ERROR;
3722 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3725 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3726 return ERROR_JTAG_DEVICE_ERROR;
3729 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3730 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3732 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3733 return ERROR_JTAG_DEVICE_ERROR;
3736 /* read adapter type information */
3737 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3738 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3740 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3741 return ERROR_JTAG_DEVICE_ERROR;
3744 if ((status = signalyzer_h_ctrl_write(
3745 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3747 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3748 return ERROR_JTAG_DEVICE_ERROR;
3751 if ((status = signalyzer_h_ctrl_write(
3752 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3754 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3755 return ERROR_JTAG_DEVICE_ERROR;
3758 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3759 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3761 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3762 return ERROR_JTAG_DEVICE_ERROR;
3767 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3768 &read_buf[0])) != FT_OK)
3770 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3771 return ERROR_JTAG_DEVICE_ERROR;
3774 if (read_buf[0] != 0x0498)
3775 signalyzer_h_adapter_type = 0x0000;
3778 for (i = 0; i < 4; i++)
3780 if ((status = signalyzer_h_ctrl_read(
3781 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3782 &read_buf[i])) != FT_OK)
3784 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3786 return ERROR_JTAG_DEVICE_ERROR;
3790 signalyzer_h_adapter_type = read_buf[0];
3793 #elif BUILD_FT2232_LIBFTDI == 1
3794 /* currently libftdi does not allow reading individual eeprom
3795 * locations, therefore adapter type cannot be detected.
3796 * override with most common type
3798 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3801 enum reset_types jtag_reset_config = jtag_get_reset_config();
3803 /* ADAPTOR: EM_LT16_A */
3804 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3806 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3807 "detected. (HW: %2x).", (read_buf[1] >> 8));
3815 low_direction = 0x1b;
3818 high_direction = 0x0;
3820 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3822 low_direction &= ~nTRSTnOE; /* nTRST input */
3823 low_output &= ~nTRST; /* nTRST = 0 */
3827 low_direction |= nTRSTnOE; /* nTRST output */
3828 low_output |= nTRST; /* nTRST = 1 */
3831 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3833 low_direction |= nSRSTnOE; /* nSRST output */
3834 low_output |= nSRST; /* nSRST = 1 */
3838 low_direction &= ~nSRSTnOE; /* nSRST input */
3839 low_output &= ~nSRST; /* nSRST = 0 */
3842 #if BUILD_FT2232_FTD2XX == 1
3843 /* enable power to the module */
3844 if ((status = signalyzer_h_ctrl_write(
3845 SIGNALYZER_DATA_BUFFER_ADDR,
3846 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3849 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3851 return ERROR_JTAG_DEVICE_ERROR;
3854 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3855 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3857 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3859 return ERROR_JTAG_DEVICE_ERROR;
3862 /* set gpio mode register */
3863 if ((status = signalyzer_h_ctrl_write(
3864 SIGNALYZER_DATA_BUFFER_ADDR,
3865 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3867 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3869 return ERROR_JTAG_DEVICE_ERROR;
3872 if ((status = signalyzer_h_ctrl_write(
3873 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3876 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3878 return ERROR_JTAG_DEVICE_ERROR;
3881 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3882 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3884 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3886 return ERROR_JTAG_DEVICE_ERROR;
3889 /* set gpio register */
3890 if ((status = signalyzer_h_ctrl_write(
3891 SIGNALYZER_DATA_BUFFER_ADDR,
3892 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3894 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3896 return ERROR_JTAG_DEVICE_ERROR;
3899 if ((status = signalyzer_h_ctrl_write(
3900 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3903 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3905 return ERROR_JTAG_DEVICE_ERROR;
3908 if ((status = signalyzer_h_ctrl_write(
3909 SIGNALYZER_COMMAND_ADDR,
3910 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3912 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3914 return ERROR_JTAG_DEVICE_ERROR;
3919 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3920 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3921 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3922 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3923 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3925 if (signalyzer_h_adapter_type
3926 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3927 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3928 "detected. (HW: %2x).", (read_buf[1] >> 8));
3929 else if (signalyzer_h_adapter_type
3930 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3931 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3932 "(ARM JTAG with PSU) detected. (HW: %2x).",
3933 (read_buf[1] >> 8));
3934 else if (signalyzer_h_adapter_type
3935 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3936 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3937 "detected. (HW: %2x).", (read_buf[1] >> 8));
3938 else if (signalyzer_h_adapter_type
3939 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3940 LOG_INFO("Signalyzer: EM-JTAG-P "
3941 "(Generic JTAG with PSU) detected. (HW: %2x).",
3942 (read_buf[1] >> 8));
3950 low_direction = 0x1b;
3953 high_direction = 0x1f;
3955 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3957 high_output |= nTRSTnOE;
3958 high_output &= ~nTRST;
3962 high_output &= ~nTRSTnOE;
3963 high_output |= nTRST;
3966 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3968 high_output &= ~nSRSTnOE;
3969 high_output |= nSRST;
3973 high_output |= nSRSTnOE;
3974 high_output &= ~nSRST;
3977 #if BUILD_FT2232_FTD2XX == 1
3978 /* enable power to the module */
3979 if ((status = signalyzer_h_ctrl_write(
3980 SIGNALYZER_DATA_BUFFER_ADDR,
3981 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3984 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3986 return ERROR_JTAG_DEVICE_ERROR;
3989 if ((status = signalyzer_h_ctrl_write(
3990 SIGNALYZER_COMMAND_ADDR,
3991 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3993 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3995 return ERROR_JTAG_DEVICE_ERROR;
3998 /* set gpio mode register (IO_16 and IO_17 set as analog
3999 * inputs, other is gpio)
4001 if ((status = signalyzer_h_ctrl_write(
4002 SIGNALYZER_DATA_BUFFER_ADDR,
4003 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4005 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4007 return ERROR_JTAG_DEVICE_ERROR;
4010 if ((status = signalyzer_h_ctrl_write(
4011 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4014 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4016 return ERROR_JTAG_DEVICE_ERROR;
4019 if ((status = signalyzer_h_ctrl_write(
4020 SIGNALYZER_COMMAND_ADDR,
4021 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4023 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4025 return ERROR_JTAG_DEVICE_ERROR;
4028 /* set gpio register (all inputs, for -P modules,
4029 * PSU will be turned off)
4031 if ((status = signalyzer_h_ctrl_write(
4032 SIGNALYZER_DATA_BUFFER_ADDR,
4033 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4035 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4037 return ERROR_JTAG_DEVICE_ERROR;
4040 if ((status = signalyzer_h_ctrl_write(
4041 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4044 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4046 return ERROR_JTAG_DEVICE_ERROR;
4049 if ((status = signalyzer_h_ctrl_write(
4050 SIGNALYZER_COMMAND_ADDR,
4051 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4053 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4055 return ERROR_JTAG_DEVICE_ERROR;
4060 else if (signalyzer_h_adapter_type == 0x0000)
4062 LOG_INFO("Signalyzer: No external modules were detected.");
4070 low_direction = 0x1b;
4073 high_direction = 0x0;
4075 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4077 low_direction &= ~nTRSTnOE; /* nTRST input */
4078 low_output &= ~nTRST; /* nTRST = 0 */
4082 low_direction |= nTRSTnOE; /* nTRST output */
4083 low_output |= nTRST; /* nTRST = 1 */
4086 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4088 low_direction |= nSRSTnOE; /* nSRST output */
4089 low_output |= nSRST; /* nSRST = 1 */
4093 low_direction &= ~nSRSTnOE; /* nSRST input */
4094 low_output &= ~nSRST; /* nSRST = 0 */
4099 LOG_ERROR("Unknown module type is detected: %.4x",
4100 signalyzer_h_adapter_type);
4101 return ERROR_JTAG_DEVICE_ERROR;
4104 /* initialize low byte of controller for jtag operation */
4105 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4107 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4108 return ERROR_JTAG_INIT_FAILED;
4111 #if BUILD_FT2232_FTD2XX == 1
4112 if (ftdi_device == FT_DEVICE_2232H)
4114 /* initialize high byte of controller for jtag operation */
4115 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4117 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4118 return ERROR_JTAG_INIT_FAILED;
4121 #elif BUILD_FT2232_LIBFTDI == 1
4122 if (ftdi_device == TYPE_2232H)
4124 /* initialize high byte of controller for jtag operation */
4125 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4127 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4128 return ERROR_JTAG_INIT_FAILED;
4135 static void signalyzer_h_reset(int trst, int srst)
4137 enum reset_types jtag_reset_config = jtag_get_reset_config();
4139 /* ADAPTOR: EM_LT16_A */
4140 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4144 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4145 /* switch to output pin (output is low) */
4146 low_direction |= nTRSTnOE;
4148 /* switch output low */
4149 low_output &= ~nTRST;
4153 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4154 /* switch to input pin (high-Z + internal
4155 * and external pullup) */
4156 low_direction &= ~nTRSTnOE;
4158 /* switch output high */
4159 low_output |= nTRST;
4164 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4165 /* switch output low */
4166 low_output &= ~nSRST;
4168 /* switch to output pin (output is low) */
4169 low_direction |= nSRSTnOE;
4173 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4174 /* switch output high */
4175 low_output |= nSRST;
4177 /* switch to input pin (high-Z) */
4178 low_direction &= ~nSRSTnOE;
4181 /* command "set data bits low byte" */
4183 buffer_write(low_output);
4184 buffer_write(low_direction);
4185 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4186 "low_direction: 0x%2.2x",
4187 trst, srst, low_output, low_direction);
4189 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4190 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4191 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4192 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4193 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4197 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4198 high_output &= ~nTRSTnOE;
4200 high_output &= ~nTRST;
4204 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4205 high_output |= nTRSTnOE;
4207 high_output |= nTRST;
4212 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4213 high_output &= ~nSRST;
4215 high_output &= ~nSRSTnOE;
4219 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4220 high_output |= nSRST;
4222 high_output |= nSRSTnOE;
4225 /* command "set data bits high byte" */
4227 buffer_write(high_output);
4228 buffer_write(high_direction);
4229 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4230 "high_direction: 0x%2.2x",
4231 trst, srst, high_output, high_direction);
4233 else if (signalyzer_h_adapter_type == 0x0000)
4237 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4238 /* switch to output pin (output is low) */
4239 low_direction |= nTRSTnOE;
4241 /* switch output low */
4242 low_output &= ~nTRST;
4246 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4247 /* switch to input pin (high-Z + internal
4248 * and external pullup) */
4249 low_direction &= ~nTRSTnOE;
4251 /* switch output high */
4252 low_output |= nTRST;
4257 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4258 /* switch output low */
4259 low_output &= ~nSRST;
4261 /* switch to output pin (output is low) */
4262 low_direction |= nSRSTnOE;
4266 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4267 /* switch output high */
4268 low_output |= nSRST;
4270 /* switch to input pin (high-Z) */
4271 low_direction &= ~nSRSTnOE;
4274 /* command "set data bits low byte" */
4276 buffer_write(low_output);
4277 buffer_write(low_direction);
4278 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4279 "low_direction: 0x%2.2x",
4280 trst, srst, low_output, low_direction);
4284 static void signalyzer_h_blink(void)
4286 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4289 /********************************************************************
4290 * Support for KT-LINK
4291 * JTAG adapter from KRISTECH
4292 * http://www.kristech.eu
4293 *******************************************************************/
4294 static int ktlink_init(void)
4296 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4298 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4299 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4301 /* initialize low byte for jtag */
4302 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4304 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4305 return ERROR_JTAG_INIT_FAILED;
4313 high_output = 0x80; // turn LED on
4314 high_direction = 0xFF; // all outputs
4316 enum reset_types jtag_reset_config = jtag_get_reset_config();
4318 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4319 high_output |= nTRSTnOE;
4320 high_output &= ~nTRST;
4322 high_output &= ~nTRSTnOE;
4323 high_output |= nTRST;
4326 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4327 high_output &= ~nSRSTnOE;
4328 high_output |= nSRST;
4330 high_output |= nSRSTnOE;
4331 high_output &= ~nSRST;
4334 /* initialize high byte for jtag */
4335 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4337 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4338 return ERROR_JTAG_INIT_FAILED;
4344 static void ktlink_reset(int trst, int srst)
4346 enum reset_types jtag_reset_config = jtag_get_reset_config();
4349 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4350 high_output &= ~nTRSTnOE;
4352 high_output &= ~nTRST;
4353 } else if (trst == 0) {
4354 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4355 high_output |= nTRSTnOE;
4357 high_output |= nTRST;
4361 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4362 high_output &= ~nSRST;
4364 high_output &= ~nSRSTnOE;
4365 } else if (srst == 0) {
4366 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4367 high_output |= nSRST;
4369 high_output |= nSRSTnOE;
4372 buffer_write(0x82); // command "set data bits high byte"
4373 buffer_write(high_output);
4374 buffer_write(high_direction);
4375 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4378 static void ktlink_blink(void)
4380 /* LED connected to ACBUS7 */
4381 high_output ^= 0x80;
4383 buffer_write(0x82); // command "set data bits high byte"
4384 buffer_write(high_output);
4385 buffer_write(high_direction);
4388 static const struct command_registration ft2232_command_handlers[] = {
4390 .name = "ft2232_device_desc",
4391 .handler = &ft2232_handle_device_desc_command,
4392 .mode = COMMAND_CONFIG,
4393 .help = "set the USB device description of the FTDI FT2232 device",
4394 .usage = "description_string",
4397 .name = "ft2232_serial",
4398 .handler = &ft2232_handle_serial_command,
4399 .mode = COMMAND_CONFIG,
4400 .help = "set the serial number of the FTDI FT2232 device",
4401 .usage = "serial_string",
4404 .name = "ft2232_layout",
4405 .handler = &ft2232_handle_layout_command,
4406 .mode = COMMAND_CONFIG,
4407 .help = "set the layout of the FT2232 GPIO signals used "
4408 "to control output-enables and reset signals",
4409 .usage = "layout_name",
4412 .name = "ft2232_vid_pid",
4413 .handler = &ft2232_handle_vid_pid_command,
4414 .mode = COMMAND_CONFIG,
4415 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4416 .usage = "(vid pid)* ",
4419 .name = "ft2232_latency",
4420 .handler = &ft2232_handle_latency_command,
4421 .mode = COMMAND_CONFIG,
4422 .help = "set the FT2232 latency timer to a new value",
4425 COMMAND_REGISTRATION_DONE
4428 struct jtag_interface ft2232_interface = {
4430 .supported = DEBUG_CAP_TMS_SEQ,
4431 .commands = ft2232_command_handlers,
4432 .transports = jtag_only,
4434 .init = ft2232_init,
4435 .quit = ft2232_quit,
4436 .speed = ft2232_speed,
4437 .speed_div = ft2232_speed_div,
4439 .execute_queue = ft2232_execute_queue,