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_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read,(FT2232_BUFFER_READ_QUEUE_SIZE+1));
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;
2488 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2489 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2491 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2492 return ERROR_JTAG_INIT_FAILED;
2495 #if BUILD_FT2232_FTD2XX == 1
2496 return ft2232_purge_ftd2xx();
2497 #elif BUILD_FT2232_LIBFTDI == 1
2498 return ft2232_purge_libftdi();
2504 /** Updates defaults for DBUS signals: the four JTAG signals
2505 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2507 static inline void ftx232_dbus_init(void)
2510 low_direction = 0x0b;
2513 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2514 * the four GPIOL signals. Initialization covers value and direction,
2515 * as customized for each layout.
2517 static int ftx232_dbus_write(void)
2519 enum reset_types jtag_reset_config = jtag_get_reset_config();
2520 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2522 low_direction &= ~nTRSTnOE; /* nTRST input */
2523 low_output &= ~nTRST; /* nTRST = 0 */
2527 low_direction |= nTRSTnOE; /* nTRST output */
2528 low_output |= nTRST; /* nTRST = 1 */
2531 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2533 low_direction |= nSRSTnOE; /* nSRST output */
2534 low_output |= nSRST; /* nSRST = 1 */
2538 low_direction &= ~nSRSTnOE; /* nSRST input */
2539 low_output &= ~nSRST; /* nSRST = 0 */
2542 /* initialize low byte for jtag */
2543 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2545 LOG_ERROR("couldn't initialize FT2232 DBUS");
2546 return ERROR_JTAG_INIT_FAILED;
2552 static int usbjtag_init(void)
2555 * NOTE: This is now _specific_ to the "usbjtag" layout.
2556 * Don't try cram any more layouts into this.
2565 return ftx232_dbus_write();
2568 static int lm3s811_jtag_init(void)
2572 /* There are multiple revisions of LM3S811 eval boards:
2573 * - Rev B (and older?) boards have no SWO trace support.
2574 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2575 * they should use the "luminary_icdi" layout instead.
2582 low_direction = 0x8b;
2584 return ftx232_dbus_write();
2587 static int icdi_jtag_init(void)
2591 /* Most Luminary eval boards support SWO trace output,
2592 * and should use this "luminary_icdi" layout.
2594 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2595 * to switch between JTAG and SWD, or switch the ft2232 UART
2596 * on the second MPSSE channel/interface (BDBUS)
2597 * between (i) the stellaris UART (on Luminary boards)
2598 * or (ii) SWO trace data (generic).
2600 * We come up in JTAG mode and may switch to SWD later (with
2601 * SWO/trace option if SWD is active).
2608 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2609 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2610 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2613 /* GPIOs on second channel/interface (UART) ... */
2614 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2615 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2616 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2621 nSRSTnOE = ICDI_SRST;
2623 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2624 low_output |= ICDI_JTAG_EN;
2625 low_output &= ~ICDI_DBG_ENn;
2627 return ftx232_dbus_write();
2630 static int signalyzer_init(void)
2638 return ftx232_dbus_write();
2641 static int axm0432_jtag_init(void)
2644 low_direction = 0x2b;
2646 /* initialize low byte for jtag */
2647 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2649 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2650 return ERROR_JTAG_INIT_FAILED;
2653 if (strcmp(layout->name, "axm0432_jtag") == 0)
2656 nTRSTnOE = 0x0; /* No output enable for TRST*/
2658 nSRSTnOE = 0x0; /* No output enable for SRST*/
2662 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2667 high_direction = 0x0c;
2669 enum reset_types jtag_reset_config = jtag_get_reset_config();
2670 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2672 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2676 high_output |= nTRST;
2679 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2681 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2685 high_output |= nSRST;
2688 /* initialize high byte for jtag */
2689 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2691 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2692 return ERROR_JTAG_INIT_FAILED;
2698 static int redbee_init(void)
2701 low_direction = 0x2b;
2703 /* initialize low byte for jtag */
2704 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2706 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2707 return ERROR_JTAG_INIT_FAILED;
2711 nTRSTnOE = 0x0; /* No output enable for TRST*/
2713 nSRSTnOE = 0x0; /* No output enable for SRST*/
2716 high_direction = 0x0c;
2718 enum reset_types jtag_reset_config = jtag_get_reset_config();
2719 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2721 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2725 high_output |= nTRST;
2728 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2730 LOG_ERROR("can't set nSRST to push-pull on redbee");
2734 high_output |= nSRST;
2737 /* initialize high byte for jtag */
2738 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2740 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2741 return ERROR_JTAG_INIT_FAILED;
2747 static int jtagkey_init(void)
2750 low_direction = 0x1b;
2752 /* initialize low byte for jtag */
2753 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2755 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2756 return ERROR_JTAG_INIT_FAILED;
2759 if (strcmp(layout->name, "jtagkey") == 0)
2766 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2767 || (strcmp(layout->name, "oocdlink") == 0))
2776 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2781 high_direction = 0x0f;
2783 enum reset_types jtag_reset_config = jtag_get_reset_config();
2784 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2786 high_output |= nTRSTnOE;
2787 high_output &= ~nTRST;
2791 high_output &= ~nTRSTnOE;
2792 high_output |= nTRST;
2795 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2797 high_output &= ~nSRSTnOE;
2798 high_output |= nSRST;
2802 high_output |= nSRSTnOE;
2803 high_output &= ~nSRST;
2806 /* initialize high byte for jtag */
2807 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2809 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2810 return ERROR_JTAG_INIT_FAILED;
2816 static int olimex_jtag_init(void)
2819 low_direction = 0x1b;
2821 /* initialize low byte for jtag */
2822 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2824 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2825 return ERROR_JTAG_INIT_FAILED;
2831 nSRSTnOE = 0x00; /* no output enable for nSRST */
2834 high_direction = 0x0f;
2836 enum reset_types jtag_reset_config = jtag_get_reset_config();
2837 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2839 high_output |= nTRSTnOE;
2840 high_output &= ~nTRST;
2844 high_output &= ~nTRSTnOE;
2845 high_output |= nTRST;
2848 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2850 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2854 high_output &= ~nSRST;
2857 /* turn red LED on */
2858 high_output |= 0x08;
2860 /* initialize high byte for jtag */
2861 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2863 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2864 return ERROR_JTAG_INIT_FAILED;
2870 static int flyswatter_init(void)
2873 low_direction = 0xfb;
2875 /* initialize low byte for jtag */
2876 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2878 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2879 return ERROR_JTAG_INIT_FAILED;
2883 nTRSTnOE = 0x0; /* not output enable for nTRST */
2885 nSRSTnOE = 0x00; /* no output enable for nSRST */
2888 high_direction = 0x0c;
2890 /* turn red LED3 on, LED2 off */
2891 high_output |= 0x08;
2893 /* initialize high byte for jtag */
2894 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2896 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2897 return ERROR_JTAG_INIT_FAILED;
2903 static int turtle_init(void)
2906 low_direction = 0x5b;
2908 /* initialize low byte for jtag */
2909 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2911 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2912 return ERROR_JTAG_INIT_FAILED;
2918 high_direction = 0x0C;
2920 /* initialize high byte for jtag */
2921 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2923 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2924 return ERROR_JTAG_INIT_FAILED;
2930 static int comstick_init(void)
2933 low_direction = 0x0b;
2935 /* initialize low byte for jtag */
2936 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2938 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2939 return ERROR_JTAG_INIT_FAILED;
2943 nTRSTnOE = 0x00; /* no output enable for nTRST */
2945 nSRSTnOE = 0x00; /* no output enable for nSRST */
2948 high_direction = 0x03;
2950 /* initialize high byte for jtag */
2951 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2953 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2954 return ERROR_JTAG_INIT_FAILED;
2960 static int stm32stick_init(void)
2963 low_direction = 0x8b;
2965 /* initialize low byte for jtag */
2966 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2968 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2969 return ERROR_JTAG_INIT_FAILED;
2973 nTRSTnOE = 0x00; /* no output enable for nTRST */
2975 nSRSTnOE = 0x00; /* no output enable for nSRST */
2978 high_direction = 0x03;
2980 /* initialize high byte for jtag */
2981 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2983 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2984 return ERROR_JTAG_INIT_FAILED;
2990 static int sheevaplug_init(void)
2993 low_direction = 0x1b;
2995 /* initialize low byte for jtag */
2996 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2998 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2999 return ERROR_JTAG_INIT_FAILED;
3008 high_direction = 0x0f;
3010 /* nTRST is always push-pull */
3011 high_output &= ~nTRSTnOE;
3012 high_output |= nTRST;
3014 /* nSRST is always open-drain */
3015 high_output |= nSRSTnOE;
3016 high_output &= ~nSRST;
3018 /* initialize high byte for jtag */
3019 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3021 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3022 return ERROR_JTAG_INIT_FAILED;
3028 static int cortino_jtag_init(void)
3031 low_direction = 0x1b;
3033 /* initialize low byte for jtag */
3034 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3036 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3037 return ERROR_JTAG_INIT_FAILED;
3041 nTRSTnOE = 0x00; /* no output enable for nTRST */
3043 nSRSTnOE = 0x00; /* no output enable for nSRST */
3046 high_direction = 0x03;
3048 /* initialize high byte for jtag */
3049 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3051 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3052 return ERROR_JTAG_INIT_FAILED;
3058 static int lisa_l_init(void)
3068 high_direction = 0x18;
3070 /* initialize high byte for jtag */
3071 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3073 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3074 return ERROR_JTAG_INIT_FAILED;
3077 return ftx232_dbus_write();
3080 static int flossjtag_init(void)
3090 high_direction = 0x18;
3092 /* initialize high byte for jtag */
3093 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3095 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3096 return ERROR_JTAG_INIT_FAILED;
3099 return ftx232_dbus_write();
3102 static int xds100v2_init(void)
3105 low_direction = 0x7B;
3107 /* initialize low byte for jtag */
3108 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3110 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3111 return ERROR_JTAG_INIT_FAILED;
3115 nTRSTnOE = 0x0; /* not output enable for nTRST */
3116 nSRST = 0x00; /* TODO: SRST is not supported yet */
3117 nSRSTnOE = 0x00; /* no output enable for nSRST */
3120 high_direction = 0x59;
3122 /* initialize high byte for jtag */
3123 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3125 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3126 return ERROR_JTAG_INIT_FAILED;
3130 high_direction = 0x59;
3132 /* initialize high byte for jtag */
3133 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3135 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3136 return ERROR_JTAG_INIT_FAILED;
3142 static void olimex_jtag_blink(void)
3144 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3145 * ACBUS3 is bit 3 of the GPIOH port
3147 high_output ^= 0x08;
3150 buffer_write(high_output);
3151 buffer_write(high_direction);
3154 static void flyswatter_jtag_blink(void)
3157 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3159 high_output ^= 0x0c;
3162 buffer_write(high_output);
3163 buffer_write(high_direction);
3166 static void turtle_jtag_blink(void)
3169 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3171 if (high_output & 0x08)
3181 buffer_write(high_output);
3182 buffer_write(high_direction);
3185 static void lisa_l_blink(void)
3188 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3190 if (high_output & 0x10)
3200 buffer_write(high_output);
3201 buffer_write(high_direction);
3204 static void flossjtag_blink(void)
3207 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3209 if (high_output & 0x10)
3219 buffer_write(high_output);
3220 buffer_write(high_direction);
3223 static int ft2232_quit(void)
3225 #if BUILD_FT2232_FTD2XX == 1
3228 status = FT_Close(ftdih);
3229 #elif BUILD_FT2232_LIBFTDI == 1
3230 ftdi_usb_close(&ftdic);
3232 ftdi_deinit(&ftdic);
3235 free(ft2232_buffer);
3236 ft2232_buffer = NULL;
3241 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3247 ft2232_device_desc = strdup(CMD_ARGV[0]);
3248 cp = strchr(ft2232_device_desc, 0);
3249 /* under Win32, the FTD2XX driver appends an "A" to the end
3250 * of the description, this examines the given desc
3251 * and creates the 'missing' _A or non_A variable. */
3252 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3253 /* it was, so make this the "A" version. */
3254 ft2232_device_desc_A = ft2232_device_desc;
3255 /* and *CREATE* the non-A version. */
3256 strcpy(buf, ft2232_device_desc);
3257 cp = strchr(buf, 0);
3259 ft2232_device_desc = strdup(buf);
3261 /* <space > A not defined
3263 sprintf(buf, "%s A", ft2232_device_desc);
3264 ft2232_device_desc_A = strdup(buf);
3269 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3275 COMMAND_HANDLER(ft2232_handle_serial_command)
3279 ft2232_serial = strdup(CMD_ARGV[0]);
3283 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3289 COMMAND_HANDLER(ft2232_handle_layout_command)
3291 if (CMD_ARGC != 1) {
3292 LOG_ERROR("Need exactly one argument to ft2232_layout");
3297 LOG_ERROR("already specified ft2232_layout %s",
3299 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3304 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3305 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3311 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3315 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3317 if (CMD_ARGC > MAX_USB_IDS * 2)
3319 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3320 "(maximum is %d pairs)", MAX_USB_IDS);
3321 CMD_ARGC = MAX_USB_IDS * 2;
3323 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3325 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3327 return ERROR_COMMAND_SYNTAX_ERROR;
3328 /* remove the incomplete trailing id */
3333 for (i = 0; i < CMD_ARGC; i += 2)
3335 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3336 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3340 * Explicitly terminate, in case there are multiples instances of
3343 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3348 COMMAND_HANDLER(ft2232_handle_latency_command)
3352 ft2232_latency = atoi(CMD_ARGV[0]);
3356 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3362 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3366 /* 7 bits of either ones or zeros. */
3367 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3369 while (num_cycles > 0)
3371 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3372 * at most 7 bits per invocation. Here we invoke it potentially
3375 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3377 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3379 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3380 retval = ERROR_JTAG_QUEUE_FAILED;
3385 /* there are no state transitions in this code, so omit state tracking */
3387 /* command "Clock Data to TMS/CS Pin (no Read)" */
3391 buffer_write(bitcount_per_command - 1);
3393 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3398 num_cycles -= bitcount_per_command;
3404 /* ---------------------------------------------------------------------
3405 * Support for IceBear JTAG adapter from Section5:
3406 * http://section5.ch/icebear
3408 * Author: Sten, debian@sansys-electronic.com
3411 /* Icebear pin layout
3413 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3414 * GND GND | 4 3| n.c.
3415 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3416 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3417 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3418 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3419 * ADBUS2 TDO |14 13| GND GND
3421 * ADBUS0 O L TCK ACBUS0 GND
3422 * ADBUS1 O L TDI ACBUS1 GND
3423 * ADBUS2 I TDO ACBUS2 n.c.
3424 * ADBUS3 O H TMS ACBUS3 n.c.
3430 static int icebear_jtag_init(void) {
3431 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3432 low_output = 0x08; /* high: TMS; low: TCK TDI */
3436 enum reset_types jtag_reset_config = jtag_get_reset_config();
3437 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3438 low_direction &= ~nTRST; /* nTRST high impedance */
3441 low_direction |= nTRST;
3442 low_output |= nTRST;
3445 low_direction |= nSRST;
3446 low_output |= nSRST;
3448 /* initialize low byte for jtag */
3449 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3450 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3451 return ERROR_JTAG_INIT_FAILED;
3455 high_direction = 0x00;
3457 /* initialize high byte for jtag */
3458 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3459 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3460 return ERROR_JTAG_INIT_FAILED;
3466 static void icebear_jtag_reset(int trst, int srst) {
3469 low_direction |= nTRST;
3470 low_output &= ~nTRST;
3472 else if (trst == 0) {
3473 enum reset_types jtag_reset_config = jtag_get_reset_config();
3474 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3475 low_direction &= ~nTRST;
3477 low_output |= nTRST;
3481 low_output &= ~nSRST;
3483 else if (srst == 0) {
3484 low_output |= nSRST;
3487 /* command "set data bits low byte" */
3489 buffer_write(low_output);
3490 buffer_write(low_direction);
3492 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3495 /* ---------------------------------------------------------------------
3496 * Support for Signalyzer H2 and Signalyzer H4
3497 * JTAG adapter from Xverve Technologies Inc.
3498 * http://www.signalyzer.com or http://www.xverve.com
3500 * Author: Oleg Seiljus, oleg@signalyzer.com
3502 static unsigned char signalyzer_h_side;
3503 static unsigned int signalyzer_h_adapter_type;
3505 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3507 #if BUILD_FT2232_FTD2XX == 1
3508 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3511 #define SIGNALYZER_COMMAND_ADDR 128
3512 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3514 #define SIGNALYZER_COMMAND_VERSION 0x41
3515 #define SIGNALYZER_COMMAND_RESET 0x42
3516 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3517 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3518 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3519 #define SIGNALYZER_COMMAND_LED_SET 0x53
3520 #define SIGNALYZER_COMMAND_ADC 0x54
3521 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3522 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3523 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3524 #define SIGNALYZER_COMMAND_I2C 0x58
3526 #define SIGNALYZER_CHAN_A 1
3527 #define SIGNALYZER_CHAN_B 2
3528 /* LEDS use channel C */
3529 #define SIGNALYZER_CHAN_C 4
3531 #define SIGNALYZER_LED_GREEN 1
3532 #define SIGNALYZER_LED_RED 2
3534 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3535 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3536 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3537 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3538 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3541 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3543 #if BUILD_FT2232_FTD2XX == 1
3544 return FT_WriteEE(ftdih, address, value);
3545 #elif BUILD_FT2232_LIBFTDI == 1
3550 #if BUILD_FT2232_FTD2XX == 1
3551 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3553 return FT_ReadEE(ftdih, address, value);
3557 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3558 int on_time_ms, int off_time_ms, unsigned char cycles)
3560 unsigned char on_time;
3561 unsigned char off_time;
3563 if (on_time_ms < 0xFFFF)
3564 on_time = (unsigned char)(on_time_ms / 62);
3568 off_time = (unsigned char)(off_time_ms / 62);
3570 #if BUILD_FT2232_FTD2XX == 1
3573 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3574 ((uint32_t)(channel << 8) | led))) != FT_OK)
3576 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3577 return ERROR_JTAG_DEVICE_ERROR;
3580 if ((status = signalyzer_h_ctrl_write(
3581 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3582 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3584 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3585 return ERROR_JTAG_DEVICE_ERROR;
3588 if ((status = signalyzer_h_ctrl_write(
3589 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3590 ((uint32_t)cycles))) != FT_OK)
3592 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3593 return ERROR_JTAG_DEVICE_ERROR;
3596 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3597 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3599 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3600 return ERROR_JTAG_DEVICE_ERROR;
3604 #elif BUILD_FT2232_LIBFTDI == 1
3607 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3608 ((uint32_t)(channel << 8) | led))) < 0)
3610 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3611 ftdi_get_error_string(&ftdic));
3612 return ERROR_JTAG_DEVICE_ERROR;
3615 if ((retval = signalyzer_h_ctrl_write(
3616 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3617 ((uint32_t)(on_time << 8) | off_time))) < 0)
3619 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3620 ftdi_get_error_string(&ftdic));
3621 return ERROR_JTAG_DEVICE_ERROR;
3624 if ((retval = signalyzer_h_ctrl_write(
3625 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3626 (uint32_t)cycles)) < 0)
3628 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3629 ftdi_get_error_string(&ftdic));
3630 return ERROR_JTAG_DEVICE_ERROR;
3633 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3634 SIGNALYZER_COMMAND_LED_SET)) < 0)
3636 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3637 ftdi_get_error_string(&ftdic));
3638 return ERROR_JTAG_DEVICE_ERROR;
3645 static int signalyzer_h_init(void)
3647 #if BUILD_FT2232_FTD2XX == 1
3654 uint16_t read_buf[12] = { 0 };
3656 /* turn on center green led */
3657 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3658 0xFFFF, 0x00, 0x00);
3660 /* determine what channel config wants to open
3661 * TODO: change me... current implementation is made to work
3662 * with openocd description parsing.
3664 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3668 signalyzer_h_side = *(end_of_desc - 1);
3669 if (signalyzer_h_side == 'B')
3670 signalyzer_h_side = SIGNALYZER_CHAN_B;
3672 signalyzer_h_side = SIGNALYZER_CHAN_A;
3676 LOG_ERROR("No Channel was specified");
3680 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3683 #if BUILD_FT2232_FTD2XX == 1
3684 /* read signalyzer versionining information */
3685 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3686 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3688 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3689 return ERROR_JTAG_DEVICE_ERROR;
3692 for (i = 0; i < 10; i++)
3694 if ((status = signalyzer_h_ctrl_read(
3695 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3696 &read_buf[i])) != FT_OK)
3698 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3700 return ERROR_JTAG_DEVICE_ERROR;
3704 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3705 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3706 read_buf[4], read_buf[5], read_buf[6]);
3708 /* set gpio register */
3709 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3710 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3712 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3713 return ERROR_JTAG_DEVICE_ERROR;
3716 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3719 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3720 return ERROR_JTAG_DEVICE_ERROR;
3723 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3724 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3726 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3727 return ERROR_JTAG_DEVICE_ERROR;
3730 /* read adapter type information */
3731 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3732 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3734 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3735 return ERROR_JTAG_DEVICE_ERROR;
3738 if ((status = signalyzer_h_ctrl_write(
3739 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3741 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3742 return ERROR_JTAG_DEVICE_ERROR;
3745 if ((status = signalyzer_h_ctrl_write(
3746 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3748 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3749 return ERROR_JTAG_DEVICE_ERROR;
3752 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3753 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3755 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3756 return ERROR_JTAG_DEVICE_ERROR;
3761 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3762 &read_buf[0])) != FT_OK)
3764 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3765 return ERROR_JTAG_DEVICE_ERROR;
3768 if (read_buf[0] != 0x0498)
3769 signalyzer_h_adapter_type = 0x0000;
3772 for (i = 0; i < 4; i++)
3774 if ((status = signalyzer_h_ctrl_read(
3775 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3776 &read_buf[i])) != FT_OK)
3778 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3780 return ERROR_JTAG_DEVICE_ERROR;
3784 signalyzer_h_adapter_type = read_buf[0];
3787 #elif BUILD_FT2232_LIBFTDI == 1
3788 /* currently libftdi does not allow reading individual eeprom
3789 * locations, therefore adapter type cannot be detected.
3790 * override with most common type
3792 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3795 enum reset_types jtag_reset_config = jtag_get_reset_config();
3797 /* ADAPTOR: EM_LT16_A */
3798 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3800 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3801 "detected. (HW: %2x).", (read_buf[1] >> 8));
3809 low_direction = 0x1b;
3812 high_direction = 0x0;
3814 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3816 low_direction &= ~nTRSTnOE; /* nTRST input */
3817 low_output &= ~nTRST; /* nTRST = 0 */
3821 low_direction |= nTRSTnOE; /* nTRST output */
3822 low_output |= nTRST; /* nTRST = 1 */
3825 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3827 low_direction |= nSRSTnOE; /* nSRST output */
3828 low_output |= nSRST; /* nSRST = 1 */
3832 low_direction &= ~nSRSTnOE; /* nSRST input */
3833 low_output &= ~nSRST; /* nSRST = 0 */
3836 #if BUILD_FT2232_FTD2XX == 1
3837 /* enable power to the module */
3838 if ((status = signalyzer_h_ctrl_write(
3839 SIGNALYZER_DATA_BUFFER_ADDR,
3840 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3843 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3845 return ERROR_JTAG_DEVICE_ERROR;
3848 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3849 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3853 return ERROR_JTAG_DEVICE_ERROR;
3856 /* set gpio mode register */
3857 if ((status = signalyzer_h_ctrl_write(
3858 SIGNALYZER_DATA_BUFFER_ADDR,
3859 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3861 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3863 return ERROR_JTAG_DEVICE_ERROR;
3866 if ((status = signalyzer_h_ctrl_write(
3867 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3870 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3872 return ERROR_JTAG_DEVICE_ERROR;
3875 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3876 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3878 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3880 return ERROR_JTAG_DEVICE_ERROR;
3883 /* set gpio register */
3884 if ((status = signalyzer_h_ctrl_write(
3885 SIGNALYZER_DATA_BUFFER_ADDR,
3886 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3888 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3890 return ERROR_JTAG_DEVICE_ERROR;
3893 if ((status = signalyzer_h_ctrl_write(
3894 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3897 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3899 return ERROR_JTAG_DEVICE_ERROR;
3902 if ((status = signalyzer_h_ctrl_write(
3903 SIGNALYZER_COMMAND_ADDR,
3904 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3906 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3908 return ERROR_JTAG_DEVICE_ERROR;
3913 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3914 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3915 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3916 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3917 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3919 if (signalyzer_h_adapter_type
3920 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3921 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3922 "detected. (HW: %2x).", (read_buf[1] >> 8));
3923 else if (signalyzer_h_adapter_type
3924 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3925 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3926 "(ARM JTAG with PSU) detected. (HW: %2x).",
3927 (read_buf[1] >> 8));
3928 else if (signalyzer_h_adapter_type
3929 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3930 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3931 "detected. (HW: %2x).", (read_buf[1] >> 8));
3932 else if (signalyzer_h_adapter_type
3933 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3934 LOG_INFO("Signalyzer: EM-JTAG-P "
3935 "(Generic JTAG with PSU) detected. (HW: %2x).",
3936 (read_buf[1] >> 8));
3944 low_direction = 0x1b;
3947 high_direction = 0x1f;
3949 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3951 high_output |= nTRSTnOE;
3952 high_output &= ~nTRST;
3956 high_output &= ~nTRSTnOE;
3957 high_output |= nTRST;
3960 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3962 high_output &= ~nSRSTnOE;
3963 high_output |= nSRST;
3967 high_output |= nSRSTnOE;
3968 high_output &= ~nSRST;
3971 #if BUILD_FT2232_FTD2XX == 1
3972 /* enable power to the module */
3973 if ((status = signalyzer_h_ctrl_write(
3974 SIGNALYZER_DATA_BUFFER_ADDR,
3975 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3978 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3980 return ERROR_JTAG_DEVICE_ERROR;
3983 if ((status = signalyzer_h_ctrl_write(
3984 SIGNALYZER_COMMAND_ADDR,
3985 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3987 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3989 return ERROR_JTAG_DEVICE_ERROR;
3992 /* set gpio mode register (IO_16 and IO_17 set as analog
3993 * inputs, other is gpio)
3995 if ((status = signalyzer_h_ctrl_write(
3996 SIGNALYZER_DATA_BUFFER_ADDR,
3997 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3999 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4001 return ERROR_JTAG_DEVICE_ERROR;
4004 if ((status = signalyzer_h_ctrl_write(
4005 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4008 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4010 return ERROR_JTAG_DEVICE_ERROR;
4013 if ((status = signalyzer_h_ctrl_write(
4014 SIGNALYZER_COMMAND_ADDR,
4015 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4017 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4019 return ERROR_JTAG_DEVICE_ERROR;
4022 /* set gpio register (all inputs, for -P modules,
4023 * PSU will be turned off)
4025 if ((status = signalyzer_h_ctrl_write(
4026 SIGNALYZER_DATA_BUFFER_ADDR,
4027 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4029 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4031 return ERROR_JTAG_DEVICE_ERROR;
4034 if ((status = signalyzer_h_ctrl_write(
4035 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4038 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4040 return ERROR_JTAG_DEVICE_ERROR;
4043 if ((status = signalyzer_h_ctrl_write(
4044 SIGNALYZER_COMMAND_ADDR,
4045 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4047 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4049 return ERROR_JTAG_DEVICE_ERROR;
4054 else if (signalyzer_h_adapter_type == 0x0000)
4056 LOG_INFO("Signalyzer: No external modules were detected.");
4064 low_direction = 0x1b;
4067 high_direction = 0x0;
4069 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4071 low_direction &= ~nTRSTnOE; /* nTRST input */
4072 low_output &= ~nTRST; /* nTRST = 0 */
4076 low_direction |= nTRSTnOE; /* nTRST output */
4077 low_output |= nTRST; /* nTRST = 1 */
4080 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4082 low_direction |= nSRSTnOE; /* nSRST output */
4083 low_output |= nSRST; /* nSRST = 1 */
4087 low_direction &= ~nSRSTnOE; /* nSRST input */
4088 low_output &= ~nSRST; /* nSRST = 0 */
4093 LOG_ERROR("Unknown module type is detected: %.4x",
4094 signalyzer_h_adapter_type);
4095 return ERROR_JTAG_DEVICE_ERROR;
4098 /* initialize low byte of controller for jtag operation */
4099 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4101 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4102 return ERROR_JTAG_INIT_FAILED;
4105 #if BUILD_FT2232_FTD2XX == 1
4106 if (ftdi_device == FT_DEVICE_2232H)
4108 /* initialize high byte of controller for jtag operation */
4109 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4111 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4112 return ERROR_JTAG_INIT_FAILED;
4115 #elif BUILD_FT2232_LIBFTDI == 1
4116 if (ftdi_device == TYPE_2232H)
4118 /* initialize high byte of controller for jtag operation */
4119 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4121 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4122 return ERROR_JTAG_INIT_FAILED;
4129 static void signalyzer_h_reset(int trst, int srst)
4131 enum reset_types jtag_reset_config = jtag_get_reset_config();
4133 /* ADAPTOR: EM_LT16_A */
4134 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4138 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4139 /* switch to output pin (output is low) */
4140 low_direction |= nTRSTnOE;
4142 /* switch output low */
4143 low_output &= ~nTRST;
4147 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4148 /* switch to input pin (high-Z + internal
4149 * and external pullup) */
4150 low_direction &= ~nTRSTnOE;
4152 /* switch output high */
4153 low_output |= nTRST;
4158 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4159 /* switch output low */
4160 low_output &= ~nSRST;
4162 /* switch to output pin (output is low) */
4163 low_direction |= nSRSTnOE;
4167 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4168 /* switch output high */
4169 low_output |= nSRST;
4171 /* switch to input pin (high-Z) */
4172 low_direction &= ~nSRSTnOE;
4175 /* command "set data bits low byte" */
4177 buffer_write(low_output);
4178 buffer_write(low_direction);
4179 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4180 "low_direction: 0x%2.2x",
4181 trst, srst, low_output, low_direction);
4183 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4184 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4185 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4186 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4187 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4191 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4192 high_output &= ~nTRSTnOE;
4194 high_output &= ~nTRST;
4198 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4199 high_output |= nTRSTnOE;
4201 high_output |= nTRST;
4206 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4207 high_output &= ~nSRST;
4209 high_output &= ~nSRSTnOE;
4213 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4214 high_output |= nSRST;
4216 high_output |= nSRSTnOE;
4219 /* command "set data bits high byte" */
4221 buffer_write(high_output);
4222 buffer_write(high_direction);
4223 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4224 "high_direction: 0x%2.2x",
4225 trst, srst, high_output, high_direction);
4227 else if (signalyzer_h_adapter_type == 0x0000)
4231 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4232 /* switch to output pin (output is low) */
4233 low_direction |= nTRSTnOE;
4235 /* switch output low */
4236 low_output &= ~nTRST;
4240 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4241 /* switch to input pin (high-Z + internal
4242 * and external pullup) */
4243 low_direction &= ~nTRSTnOE;
4245 /* switch output high */
4246 low_output |= nTRST;
4251 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4252 /* switch output low */
4253 low_output &= ~nSRST;
4255 /* switch to output pin (output is low) */
4256 low_direction |= nSRSTnOE;
4260 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4261 /* switch output high */
4262 low_output |= nSRST;
4264 /* switch to input pin (high-Z) */
4265 low_direction &= ~nSRSTnOE;
4268 /* command "set data bits low byte" */
4270 buffer_write(low_output);
4271 buffer_write(low_direction);
4272 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4273 "low_direction: 0x%2.2x",
4274 trst, srst, low_output, low_direction);
4278 static void signalyzer_h_blink(void)
4280 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4283 /********************************************************************
4284 * Support for KT-LINK
4285 * JTAG adapter from KRISTECH
4286 * http://www.kristech.eu
4287 *******************************************************************/
4288 static int ktlink_init(void)
4290 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4292 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4293 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4295 /* initialize low byte for jtag */
4296 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4298 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4299 return ERROR_JTAG_INIT_FAILED;
4307 high_output = 0x80; // turn LED on
4308 high_direction = 0xFF; // all outputs
4310 enum reset_types jtag_reset_config = jtag_get_reset_config();
4312 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4313 high_output |= nTRSTnOE;
4314 high_output &= ~nTRST;
4316 high_output &= ~nTRSTnOE;
4317 high_output |= nTRST;
4320 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4321 high_output &= ~nSRSTnOE;
4322 high_output |= nSRST;
4324 high_output |= nSRSTnOE;
4325 high_output &= ~nSRST;
4328 /* initialize high byte for jtag */
4329 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4331 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4332 return ERROR_JTAG_INIT_FAILED;
4338 static void ktlink_reset(int trst, int srst)
4340 enum reset_types jtag_reset_config = jtag_get_reset_config();
4343 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4344 high_output &= ~nTRSTnOE;
4346 high_output &= ~nTRST;
4347 } else if (trst == 0) {
4348 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4349 high_output |= nTRSTnOE;
4351 high_output |= nTRST;
4355 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4356 high_output &= ~nSRST;
4358 high_output &= ~nSRSTnOE;
4359 } else if (srst == 0) {
4360 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4361 high_output |= nSRST;
4363 high_output |= nSRSTnOE;
4366 buffer_write(0x82); // command "set data bits high byte"
4367 buffer_write(high_output);
4368 buffer_write(high_direction);
4369 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4372 static void ktlink_blink(void)
4374 /* LED connected to ACBUS7 */
4375 high_output ^= 0x80;
4377 buffer_write(0x82); // command "set data bits high byte"
4378 buffer_write(high_output);
4379 buffer_write(high_direction);
4382 static const struct command_registration ft2232_command_handlers[] = {
4384 .name = "ft2232_device_desc",
4385 .handler = &ft2232_handle_device_desc_command,
4386 .mode = COMMAND_CONFIG,
4387 .help = "set the USB device description of the FTDI FT2232 device",
4388 .usage = "description_string",
4391 .name = "ft2232_serial",
4392 .handler = &ft2232_handle_serial_command,
4393 .mode = COMMAND_CONFIG,
4394 .help = "set the serial number of the FTDI FT2232 device",
4395 .usage = "serial_string",
4398 .name = "ft2232_layout",
4399 .handler = &ft2232_handle_layout_command,
4400 .mode = COMMAND_CONFIG,
4401 .help = "set the layout of the FT2232 GPIO signals used "
4402 "to control output-enables and reset signals",
4403 .usage = "layout_name",
4406 .name = "ft2232_vid_pid",
4407 .handler = &ft2232_handle_vid_pid_command,
4408 .mode = COMMAND_CONFIG,
4409 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4410 .usage = "(vid pid)* ",
4413 .name = "ft2232_latency",
4414 .handler = &ft2232_handle_latency_command,
4415 .mode = COMMAND_CONFIG,
4416 .help = "set the FT2232 latency timer to a new value",
4419 COMMAND_REGISTRATION_DONE
4422 struct jtag_interface ft2232_interface = {
4424 .supported = DEBUG_CAP_TMS_SEQ,
4425 .commands = ft2232_command_handlers,
4426 .transports = jtag_only,
4428 .init = ft2232_init,
4429 .quit = ft2232_quit,
4430 .speed = ft2232_speed,
4431 .speed_div = ft2232_speed_div,
4433 .execute_queue = ft2232_execute_queue,