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 #define FT2232_BUFFER_SIZE 131072
378 static uint8_t* ft2232_buffer = NULL;
379 static int ft2232_buffer_size = 0;
380 static int ft2232_read_pointer = 0;
381 static int ft2232_expect_read = 0;
384 * Function buffer_write
385 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
386 * @param val is the byte to send.
388 static inline void buffer_write(uint8_t val)
390 assert(ft2232_buffer);
391 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
392 ft2232_buffer[ft2232_buffer_size++] = val;
396 * Function buffer_read
397 * returns a byte from the byte buffer.
399 static inline uint8_t buffer_read(void)
401 assert(ft2232_buffer);
402 assert(ft2232_read_pointer < ft2232_buffer_size);
403 return ft2232_buffer[ft2232_read_pointer++];
407 * Clocks out \a bit_count bits on the TMS line, starting with the least
408 * significant bit of tms_bits and progressing to more significant bits.
409 * Rigorous state transition logging is done here via tap_set_state().
411 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
412 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
413 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
414 * is often used for this, 0x4b.
416 * @param tms_bits Holds the sequence of bits to send.
417 * @param tms_count Tells how many bits in the sequence.
418 * @param tdi_bit A single bit to pass on to TDI before the first TCK
419 * cycle and held static for the duration of TMS clocking.
421 * See the MPSSE spec referenced above.
423 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
427 int tms_ndx; /* bit index into tms_byte */
429 assert(tms_count > 0);
431 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
432 mpsse_cmd, tms_bits, tms_count);
434 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
436 bool bit = tms_bits & 1;
439 tms_byte |= (1 << tms_ndx);
441 /* always do state transitions in public view */
442 tap_set_state(tap_state_transition(tap_get_state(), bit));
444 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
449 if (tms_ndx == 7 || i == tms_count-1)
451 buffer_write(mpsse_cmd);
452 buffer_write(tms_ndx - 1);
454 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
455 TMS/CS and is held static for the duration of TMS/CS clocking.
457 buffer_write(tms_byte | (tdi_bit << 7));
463 * Function get_tms_buffer_requirements
464 * returns what clock_tms() will consume if called with
467 static inline int get_tms_buffer_requirements(int bit_count)
469 return ((bit_count + 6)/7) * 3;
473 * Function move_to_state
474 * moves the TAP controller from the current state to a
475 * \a goal_state through a path given by tap_get_tms_path(). State transition
476 * logging is performed by delegation to clock_tms().
478 * @param goal_state is the destination state for the move.
480 static void move_to_state(tap_state_t goal_state)
482 tap_state_t start_state = tap_get_state();
484 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
485 lookup of the required TMS pattern to move to this state from the
489 /* do the 2 lookups */
490 int tms_bits = tap_get_tms_path(start_state, goal_state);
491 int tms_count = tap_get_tms_path_len(start_state, goal_state);
493 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
495 clock_tms(0x4b, tms_bits, tms_count, 0);
498 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
500 #if BUILD_FT2232_FTD2XX == 1
502 DWORD dw_bytes_written;
503 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
505 *bytes_written = dw_bytes_written;
506 LOG_ERROR("FT_Write returned: %lu", status);
507 return ERROR_JTAG_DEVICE_ERROR;
511 *bytes_written = dw_bytes_written;
513 #elif BUILD_FT2232_LIBFTDI == 1
515 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
518 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
519 return ERROR_JTAG_DEVICE_ERROR;
523 *bytes_written = retval;
527 if (*bytes_written != (uint32_t)size)
529 return ERROR_JTAG_DEVICE_ERROR;
535 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
537 #if BUILD_FT2232_FTD2XX == 1
543 while ((*bytes_read < size) && timeout--)
545 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
546 *bytes_read, &dw_bytes_read)) != FT_OK)
549 LOG_ERROR("FT_Read returned: %lu", status);
550 return ERROR_JTAG_DEVICE_ERROR;
552 *bytes_read += dw_bytes_read;
555 #elif BUILD_FT2232_LIBFTDI == 1
557 int timeout = LIBFTDI_READ_RETRY_COUNT;
560 while ((*bytes_read < size) && timeout--)
562 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
565 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
566 return ERROR_JTAG_DEVICE_ERROR;
568 *bytes_read += retval;
573 if (*bytes_read < size)
575 LOG_ERROR("couldn't read enough bytes from "
576 "FT2232 device (%i < %i)",
577 (unsigned)*bytes_read,
579 return ERROR_JTAG_DEVICE_ERROR;
585 static bool ft2232_device_is_highspeed(void)
587 #if BUILD_FT2232_FTD2XX == 1
588 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
589 #elif BUILD_FT2232_LIBFTDI == 1
590 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
595 * Commands that only apply to the FT2232H and FT4232H devices.
596 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
597 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
600 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
602 uint8_t buf = enable ? 0x96 : 0x97;
603 LOG_DEBUG("%2.2x", buf);
605 uint32_t bytes_written;
608 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
610 LOG_ERROR("couldn't write command to %s adaptive clocking"
611 , enable ? "enable" : "disable");
619 * Enable/disable the clk divide by 5 of the 60MHz master clock.
620 * This result in a JTAG clock speed range of 91.553Hz-6MHz
621 * respective 457.763Hz-30MHz.
623 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
625 uint32_t bytes_written;
626 uint8_t buf = enable ? 0x8b : 0x8a;
628 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
630 LOG_ERROR("couldn't write command to %s clk divide by 5"
631 , enable ? "enable" : "disable");
632 return ERROR_JTAG_INIT_FAILED;
634 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
635 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
640 static int ft2232_speed(int speed)
644 uint32_t bytes_written;
647 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
648 if (ft2232_device_is_highspeed())
649 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
650 else if (enable_adaptive_clocking)
652 LOG_ERROR("ft2232 device %lu does not support RTCK"
653 , (long unsigned int)ftdi_device);
657 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
660 buf[0] = 0x86; /* command "set divisor" */
661 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
662 buf[2] = (speed >> 8) & 0xff; /* valueH */
664 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
665 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
667 LOG_ERROR("couldn't set FT2232 TCK speed");
674 static int ft2232_speed_div(int speed, int* khz)
676 /* Take a look in the FT2232 manual,
677 * AN2232C-01 Command Processor for
678 * MPSSE and MCU Host Bus. Chapter 3.8 */
680 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
685 static int ft2232_khz(int khz, int* jtag_speed)
689 if (ft2232_device_is_highspeed())
691 *jtag_speed = RTCK_SPEED;
696 LOG_DEBUG("RCLK not supported");
701 /* Take a look in the FT2232 manual,
702 * AN2232C-01 Command Processor for
703 * MPSSE and MCU Host Bus. Chapter 3.8
705 * We will calc here with a multiplier
706 * of 10 for better rounding later. */
708 /* Calc speed, (ft2232_max_tck / khz) - 1 */
709 /* Use 65000 for better rounding */
710 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
712 /* Add 0.9 for rounding */
715 /* Calc real speed */
716 *jtag_speed = *jtag_speed / 10;
718 /* Check if speed is greater than 0 */
724 /* Check max value */
725 if (*jtag_speed > 0xFFFF)
727 *jtag_speed = 0xFFFF;
733 static void ft2232_end_state(tap_state_t state)
735 if (tap_is_state_stable(state))
736 tap_set_end_state(state);
739 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
744 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
746 int num_bytes = (scan_size + 7) / 8;
747 int bits_left = scan_size;
750 while (num_bytes-- > 1)
752 buffer[cur_byte++] = buffer_read();
756 buffer[cur_byte] = 0x0;
758 /* There is one more partial byte left from the clock data in/out instructions */
761 buffer[cur_byte] = buffer_read() >> 1;
763 /* 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 */
764 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
767 static void ft2232_debug_dump_buffer(void)
773 for (i = 0; i < ft2232_buffer_size; i++)
775 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
778 LOG_DEBUG("%s", line);
784 LOG_DEBUG("%s", line);
787 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
789 struct jtag_command* cmd;
794 uint32_t bytes_written = 0;
795 uint32_t bytes_read = 0;
797 #ifdef _DEBUG_USB_IO_
798 struct timeval start, inter, inter2, end;
799 struct timeval d_inter, d_inter2, d_end;
802 #ifdef _DEBUG_USB_COMMS_
803 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
804 ft2232_debug_dump_buffer();
807 #ifdef _DEBUG_USB_IO_
808 gettimeofday(&start, NULL);
811 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
813 LOG_ERROR("couldn't write MPSSE commands to FT2232");
817 #ifdef _DEBUG_USB_IO_
818 gettimeofday(&inter, NULL);
821 if (ft2232_expect_read)
823 /* FIXME this "timeout" is never changed ... */
824 int timeout = LIBFTDI_READ_RETRY_COUNT;
825 ft2232_buffer_size = 0;
827 #ifdef _DEBUG_USB_IO_
828 gettimeofday(&inter2, NULL);
831 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
833 LOG_ERROR("couldn't read from FT2232");
837 #ifdef _DEBUG_USB_IO_
838 gettimeofday(&end, NULL);
840 timeval_subtract(&d_inter, &inter, &start);
841 timeval_subtract(&d_inter2, &inter2, &start);
842 timeval_subtract(&d_end, &end, &start);
844 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
845 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
846 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
847 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
850 ft2232_buffer_size = bytes_read;
852 if (ft2232_expect_read != ft2232_buffer_size)
854 LOG_ERROR("ft2232_expect_read (%i) != "
855 "ft2232_buffer_size (%i) "
859 LIBFTDI_READ_RETRY_COUNT - timeout);
860 ft2232_debug_dump_buffer();
865 #ifdef _DEBUG_USB_COMMS_
866 LOG_DEBUG("read buffer (%i retries): %i bytes",
867 LIBFTDI_READ_RETRY_COUNT - timeout,
869 ft2232_debug_dump_buffer();
873 ft2232_expect_read = 0;
874 ft2232_read_pointer = 0;
876 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
877 * that wasn't handled by a caller-provided error handler
887 type = jtag_scan_type(cmd->cmd.scan);
888 if (type != SCAN_OUT)
890 scan_size = jtag_scan_size(cmd->cmd.scan);
891 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
892 ft2232_read_scan(type, buffer, scan_size);
893 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
894 retval = ERROR_JTAG_QUEUE_FAILED;
906 ft2232_buffer_size = 0;
912 * Function ft2232_add_pathmove
913 * moves the TAP controller from the current state to a new state through the
914 * given path, where path is an array of tap_state_t's.
916 * @param path is an array of tap_stat_t which gives the states to traverse through
917 * ending with the last state at path[num_states-1]
918 * @param num_states is the count of state steps to move through
920 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
924 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
928 /* this loop verifies that the path is legal and logs each state in the path */
931 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
933 int num_states_batch = num_states > 7 ? 7 : num_states;
935 /* command "Clock Data to TMS/CS Pin (no Read)" */
938 /* number of states remaining */
939 buffer_write(num_states_batch - 1);
941 while (num_states_batch--) {
942 /* either TMS=0 or TMS=1 must work ... */
943 if (tap_state_transition(tap_get_state(), false)
944 == path[state_count])
945 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
946 else if (tap_state_transition(tap_get_state(), true)
947 == path[state_count])
948 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
950 /* ... or else the caller goofed BADLY */
952 LOG_ERROR("BUG: %s -> %s isn't a valid "
953 "TAP state transition",
954 tap_state_name(tap_get_state()),
955 tap_state_name(path[state_count]));
959 tap_set_state(path[state_count]);
964 buffer_write(tms_byte);
966 tap_set_end_state(tap_get_state());
969 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
971 int num_bytes = (scan_size + 7) / 8;
972 int bits_left = scan_size;
978 if (tap_get_state() != TAP_DRSHIFT)
980 move_to_state(TAP_DRSHIFT);
985 if (tap_get_state() != TAP_IRSHIFT)
987 move_to_state(TAP_IRSHIFT);
991 /* add command for complete bytes */
992 while (num_bytes > 1)
997 /* Clock Data Bytes In and Out LSB First */
999 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1001 else if (type == SCAN_OUT)
1003 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1005 /* LOG_DEBUG("added TDI bytes (o)"); */
1007 else if (type == SCAN_IN)
1009 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1011 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1014 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1015 num_bytes -= thisrun_bytes;
1017 buffer_write((uint8_t) (thisrun_bytes - 1));
1018 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1020 if (type != SCAN_IN)
1022 /* add complete bytes */
1023 while (thisrun_bytes-- > 0)
1025 buffer_write(buffer[cur_byte++]);
1029 else /* (type == SCAN_IN) */
1031 bits_left -= 8 * (thisrun_bytes);
1035 /* the most signifcant bit is scanned during TAP movement */
1036 if (type != SCAN_IN)
1037 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1041 /* process remaining bits but the last one */
1044 if (type == SCAN_IO)
1046 /* Clock Data Bits In and Out LSB First */
1048 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1050 else if (type == SCAN_OUT)
1052 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1054 /* LOG_DEBUG("added TDI bits (o)"); */
1056 else if (type == SCAN_IN)
1058 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1060 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1063 buffer_write(bits_left - 2);
1064 if (type != SCAN_IN)
1065 buffer_write(buffer[cur_byte]);
1068 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1069 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1071 if (type == SCAN_IO)
1073 /* Clock Data Bits In and Out LSB First */
1075 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1077 else if (type == SCAN_OUT)
1079 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1081 /* LOG_DEBUG("added TDI bits (o)"); */
1083 else if (type == SCAN_IN)
1085 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1087 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1090 buffer_write(last_bit);
1098 /* move from Shift-IR/DR to end state */
1099 if (type != SCAN_OUT)
1101 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1102 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1105 /* Clock Data to TMS/CS Pin with Read */
1110 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1111 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1112 /* Clock Data to TMS/CS Pin (no Read) */
1116 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1117 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1120 if (tap_get_state() != tap_get_end_state())
1122 move_to_state(tap_get_end_state());
1126 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1128 int num_bytes = (scan_size + 7) / 8;
1129 int bits_left = scan_size;
1132 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1133 uint8_t* receive_pointer = receive_buffer;
1134 uint32_t bytes_written;
1135 uint32_t bytes_read;
1137 int thisrun_read = 0;
1141 LOG_ERROR("BUG: large IR scans are not supported");
1145 if (tap_get_state() != TAP_DRSHIFT)
1147 move_to_state(TAP_DRSHIFT);
1150 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1152 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1155 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1156 ft2232_buffer_size, (int)bytes_written);
1157 ft2232_buffer_size = 0;
1159 /* add command for complete bytes */
1160 while (num_bytes > 1)
1164 if (type == SCAN_IO)
1166 /* Clock Data Bytes In and Out LSB First */
1168 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1170 else if (type == SCAN_OUT)
1172 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1174 /* LOG_DEBUG("added TDI bytes (o)"); */
1176 else if (type == SCAN_IN)
1178 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1180 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1183 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1184 thisrun_read = thisrun_bytes;
1185 num_bytes -= thisrun_bytes;
1186 buffer_write((uint8_t) (thisrun_bytes - 1));
1187 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1189 if (type != SCAN_IN)
1191 /* add complete bytes */
1192 while (thisrun_bytes-- > 0)
1194 buffer_write(buffer[cur_byte]);
1199 else /* (type == SCAN_IN) */
1201 bits_left -= 8 * (thisrun_bytes);
1204 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1206 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1209 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1211 (int)bytes_written);
1212 ft2232_buffer_size = 0;
1214 if (type != SCAN_OUT)
1216 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1218 LOG_ERROR("couldn't read from FT2232");
1221 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1224 receive_pointer += bytes_read;
1230 /* the most signifcant bit is scanned during TAP movement */
1231 if (type != SCAN_IN)
1232 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1236 /* process remaining bits but the last one */
1239 if (type == SCAN_IO)
1241 /* Clock Data Bits In and Out LSB First */
1243 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1245 else if (type == SCAN_OUT)
1247 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1249 /* LOG_DEBUG("added TDI bits (o)"); */
1251 else if (type == SCAN_IN)
1253 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1255 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1257 buffer_write(bits_left - 2);
1258 if (type != SCAN_IN)
1259 buffer_write(buffer[cur_byte]);
1261 if (type != SCAN_OUT)
1265 if (tap_get_end_state() == TAP_DRSHIFT)
1267 if (type == SCAN_IO)
1269 /* Clock Data Bits In and Out LSB First */
1271 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1273 else if (type == SCAN_OUT)
1275 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1277 /* LOG_DEBUG("added TDI bits (o)"); */
1279 else if (type == SCAN_IN)
1281 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1283 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1286 buffer_write(last_bit);
1290 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1291 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1294 /* move from Shift-IR/DR to end state */
1295 if (type != SCAN_OUT)
1297 /* Clock Data to TMS/CS Pin with Read */
1299 /* LOG_DEBUG("added TMS scan (read)"); */
1303 /* Clock Data to TMS/CS Pin (no Read) */
1305 /* LOG_DEBUG("added TMS scan (no read)"); */
1308 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1309 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1312 if (type != SCAN_OUT)
1315 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1317 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1320 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1322 (int)bytes_written);
1323 ft2232_buffer_size = 0;
1325 if (type != SCAN_OUT)
1327 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1329 LOG_ERROR("couldn't read from FT2232");
1332 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1335 receive_pointer += bytes_read;
1341 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1343 int predicted_size = 3;
1344 int num_bytes = (scan_size - 1) / 8;
1346 if (tap_get_state() != TAP_DRSHIFT)
1347 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1349 if (type == SCAN_IN) /* only from device to host */
1351 /* complete bytes */
1352 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1354 /* remaining bits - 1 (up to 7) */
1355 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1357 else /* host to device, or bidirectional */
1359 /* complete bytes */
1360 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1362 /* remaining bits -1 (up to 7) */
1363 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1366 return predicted_size;
1369 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1371 int predicted_size = 0;
1373 if (type != SCAN_OUT)
1375 /* complete bytes */
1376 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1378 /* remaining bits - 1 */
1379 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1381 /* last bit (from TMS scan) */
1382 predicted_size += 1;
1385 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1387 return predicted_size;
1390 /* semi-generic FT2232/FT4232 reset code */
1391 static void ftx23_reset(int trst, int srst)
1393 enum reset_types jtag_reset_config = jtag_get_reset_config();
1396 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1397 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1399 low_output &= ~nTRST; /* switch output low */
1403 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1404 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1406 low_output |= nTRST; /* switch output high */
1411 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1412 low_output &= ~nSRST; /* switch output low */
1414 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1418 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1419 low_output |= nSRST; /* switch output high */
1421 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1424 /* command "set data bits low byte" */
1426 buffer_write(low_output);
1427 buffer_write(low_direction);
1430 static void jtagkey_reset(int trst, int srst)
1432 enum reset_types jtag_reset_config = jtag_get_reset_config();
1435 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1436 high_output &= ~nTRSTnOE;
1438 high_output &= ~nTRST;
1442 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1443 high_output |= nTRSTnOE;
1445 high_output |= nTRST;
1450 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1451 high_output &= ~nSRST;
1453 high_output &= ~nSRSTnOE;
1457 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1458 high_output |= nSRST;
1460 high_output |= nSRSTnOE;
1463 /* command "set data bits high byte" */
1465 buffer_write(high_output);
1466 buffer_write(high_direction);
1467 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1471 static void olimex_jtag_reset(int trst, int srst)
1473 enum reset_types jtag_reset_config = jtag_get_reset_config();
1476 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1477 high_output &= ~nTRSTnOE;
1479 high_output &= ~nTRST;
1483 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1484 high_output |= nTRSTnOE;
1486 high_output |= nTRST;
1491 high_output |= nSRST;
1495 high_output &= ~nSRST;
1498 /* command "set data bits high byte" */
1500 buffer_write(high_output);
1501 buffer_write(high_direction);
1502 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1506 static void axm0432_jtag_reset(int trst, int srst)
1510 tap_set_state(TAP_RESET);
1511 high_output &= ~nTRST;
1515 high_output |= nTRST;
1520 high_output &= ~nSRST;
1524 high_output |= nSRST;
1527 /* command "set data bits low byte" */
1529 buffer_write(high_output);
1530 buffer_write(high_direction);
1531 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1535 static void flyswatter_reset(int trst, int srst)
1539 low_output &= ~nTRST;
1543 low_output |= nTRST;
1548 low_output |= nSRST;
1552 low_output &= ~nSRST;
1555 /* command "set data bits low byte" */
1557 buffer_write(low_output);
1558 buffer_write(low_direction);
1559 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1562 static void turtle_reset(int trst, int srst)
1568 low_output |= nSRST;
1572 low_output &= ~nSRST;
1575 /* command "set data bits low byte" */
1577 buffer_write(low_output);
1578 buffer_write(low_direction);
1579 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1582 static void comstick_reset(int trst, int srst)
1586 high_output &= ~nTRST;
1590 high_output |= nTRST;
1595 high_output &= ~nSRST;
1599 high_output |= nSRST;
1602 /* command "set data bits high byte" */
1604 buffer_write(high_output);
1605 buffer_write(high_direction);
1606 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1610 static void stm32stick_reset(int trst, int srst)
1614 high_output &= ~nTRST;
1618 high_output |= nTRST;
1623 low_output &= ~nSRST;
1627 low_output |= nSRST;
1630 /* command "set data bits low byte" */
1632 buffer_write(low_output);
1633 buffer_write(low_direction);
1635 /* command "set data bits high byte" */
1637 buffer_write(high_output);
1638 buffer_write(high_direction);
1639 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1643 static void sheevaplug_reset(int trst, int srst)
1646 high_output &= ~nTRST;
1648 high_output |= nTRST;
1651 high_output &= ~nSRSTnOE;
1653 high_output |= nSRSTnOE;
1655 /* command "set data bits high byte" */
1657 buffer_write(high_output);
1658 buffer_write(high_direction);
1659 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1662 static void redbee_reset(int trst, int srst)
1666 tap_set_state(TAP_RESET);
1667 high_output &= ~nTRST;
1671 high_output |= nTRST;
1676 high_output &= ~nSRST;
1680 high_output |= nSRST;
1683 /* command "set data bits low byte" */
1685 buffer_write(high_output);
1686 buffer_write(high_direction);
1687 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1688 "high_direction: 0x%2.2x", trst, srst, high_output,
1692 static void xds100v2_reset(int trst, int srst)
1696 tap_set_state(TAP_RESET);
1697 high_output &= ~nTRST;
1701 high_output |= nTRST;
1706 high_output |= nSRST;
1710 high_output &= ~nSRST;
1713 /* command "set data bits low byte" */
1715 buffer_write(high_output);
1716 buffer_write(high_direction);
1717 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1718 "high_direction: 0x%2.2x", trst, srst, high_output,
1722 static int ft2232_execute_runtest(struct jtag_command *cmd)
1726 int predicted_size = 0;
1729 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1730 cmd->cmd.runtest->num_cycles,
1731 tap_state_name(cmd->cmd.runtest->end_state));
1733 /* only send the maximum buffer size that FT2232C can handle */
1735 if (tap_get_state() != TAP_IDLE)
1736 predicted_size += 3;
1737 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1738 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1739 predicted_size += 3;
1740 if (tap_get_end_state() != TAP_IDLE)
1741 predicted_size += 3;
1742 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1744 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1745 retval = ERROR_JTAG_QUEUE_FAILED;
1749 if (tap_get_state() != TAP_IDLE)
1751 move_to_state(TAP_IDLE);
1754 i = cmd->cmd.runtest->num_cycles;
1757 /* there are no state transitions in this code, so omit state tracking */
1759 /* command "Clock Data to TMS/CS Pin (no Read)" */
1763 buffer_write((i > 7) ? 6 : (i - 1));
1768 i -= (i > 7) ? 7 : i;
1769 /* LOG_DEBUG("added TMS scan (no read)"); */
1772 ft2232_end_state(cmd->cmd.runtest->end_state);
1774 if (tap_get_state() != tap_get_end_state())
1776 move_to_state(tap_get_end_state());
1780 DEBUG_JTAG_IO("runtest: %i, end in %s",
1781 cmd->cmd.runtest->num_cycles,
1782 tap_state_name(tap_get_end_state()));
1786 static int ft2232_execute_statemove(struct jtag_command *cmd)
1788 int predicted_size = 0;
1789 int retval = ERROR_OK;
1791 DEBUG_JTAG_IO("statemove end in %s",
1792 tap_state_name(cmd->cmd.statemove->end_state));
1794 /* only send the maximum buffer size that FT2232C can handle */
1796 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1798 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1799 retval = ERROR_JTAG_QUEUE_FAILED;
1803 ft2232_end_state(cmd->cmd.statemove->end_state);
1805 /* For TAP_RESET, ignore the current recorded state. It's often
1806 * wrong at server startup, and this transation is critical whenever
1809 if (tap_get_end_state() == TAP_RESET) {
1810 clock_tms(0x4b, 0xff, 5, 0);
1813 /* shortest-path move to desired end state */
1814 } else if (tap_get_state() != tap_get_end_state())
1816 move_to_state(tap_get_end_state());
1824 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1825 * (or SWD) state machine.
1827 static int ft2232_execute_tms(struct jtag_command *cmd)
1829 int retval = ERROR_OK;
1830 unsigned num_bits = cmd->cmd.tms->num_bits;
1831 const uint8_t *bits = cmd->cmd.tms->bits;
1834 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1836 /* only send the maximum buffer size that FT2232C can handle */
1837 count = 3 * DIV_ROUND_UP(num_bits, 4);
1838 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1839 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1840 retval = ERROR_JTAG_QUEUE_FAILED;
1846 /* Shift out in batches of at most 6 bits; there's a report of an
1847 * FT2232 bug in this area, where shifting exactly 7 bits can make
1848 * problems with TMS signaling for the last clock cycle:
1850 * http://developer.intra2net.com/mailarchive/html/
1851 * libftdi/2009/msg00292.html
1853 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1855 * Note that pathmoves in JTAG are not often seven bits, so that
1856 * isn't a particularly likely situation outside of "special"
1857 * signaling such as switching between JTAG and SWD modes.
1860 if (num_bits <= 6) {
1862 buffer_write(num_bits - 1);
1863 buffer_write(*bits & 0x3f);
1867 /* Yes, this is lazy ... we COULD shift out more data
1868 * bits per operation, but doing it in nybbles is easy
1872 buffer_write(*bits & 0xf);
1875 count = (num_bits > 4) ? 4 : num_bits;
1878 buffer_write(count - 1);
1879 buffer_write((*bits >> 4) & 0xf);
1889 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1891 int predicted_size = 0;
1892 int retval = ERROR_OK;
1894 tap_state_t* path = cmd->cmd.pathmove->path;
1895 int num_states = cmd->cmd.pathmove->num_states;
1897 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1898 tap_state_name(tap_get_state()),
1899 tap_state_name(path[num_states-1]));
1901 /* only send the maximum buffer size that FT2232C can handle */
1902 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1903 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1905 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1906 retval = ERROR_JTAG_QUEUE_FAILED;
1912 ft2232_add_pathmove(path, num_states);
1918 static int ft2232_execute_scan(struct jtag_command *cmd)
1921 int scan_size; /* size of IR or DR scan */
1922 int predicted_size = 0;
1923 int retval = ERROR_OK;
1925 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1927 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1929 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1931 predicted_size = ft2232_predict_scan_out(scan_size, type);
1932 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1934 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1935 /* unsent commands before this */
1936 if (first_unsent != cmd)
1937 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1938 retval = ERROR_JTAG_QUEUE_FAILED;
1940 /* current command */
1941 ft2232_end_state(cmd->cmd.scan->end_state);
1942 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1944 first_unsent = cmd->next;
1949 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1951 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1954 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1955 retval = ERROR_JTAG_QUEUE_FAILED;
1959 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1960 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1961 ft2232_end_state(cmd->cmd.scan->end_state);
1962 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1966 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1967 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1968 tap_state_name(tap_get_end_state()));
1973 static int ft2232_execute_reset(struct jtag_command *cmd)
1976 int predicted_size = 0;
1979 DEBUG_JTAG_IO("reset trst: %i srst %i",
1980 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1982 /* only send the maximum buffer size that FT2232C can handle */
1984 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1986 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1987 retval = ERROR_JTAG_QUEUE_FAILED;
1992 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1994 tap_set_state(TAP_RESET);
1997 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2000 DEBUG_JTAG_IO("trst: %i, srst: %i",
2001 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2005 static int ft2232_execute_sleep(struct jtag_command *cmd)
2010 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2012 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2013 retval = ERROR_JTAG_QUEUE_FAILED;
2014 first_unsent = cmd->next;
2015 jtag_sleep(cmd->cmd.sleep->us);
2016 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2018 tap_state_name(tap_get_state()));
2022 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2027 /* this is only allowed while in a stable state. A check for a stable
2028 * state was done in jtag_add_clocks()
2030 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2031 retval = ERROR_JTAG_QUEUE_FAILED;
2032 DEBUG_JTAG_IO("clocks %i while in %s",
2033 cmd->cmd.stableclocks->num_cycles,
2034 tap_state_name(tap_get_state()));
2038 static int ft2232_execute_command(struct jtag_command *cmd)
2044 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2045 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2046 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2047 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2048 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2049 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2050 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2052 retval = ft2232_execute_tms(cmd);
2055 LOG_ERROR("BUG: unknown JTAG command type encountered");
2056 retval = ERROR_JTAG_QUEUE_FAILED;
2062 static int ft2232_execute_queue(void)
2064 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2067 first_unsent = cmd; /* next command that has to be sent */
2070 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2071 * that wasn't handled by a caller-provided error handler
2075 ft2232_buffer_size = 0;
2076 ft2232_expect_read = 0;
2078 /* blink, if the current layout has that feature */
2084 if (ft2232_execute_command(cmd) != ERROR_OK)
2085 retval = ERROR_JTAG_QUEUE_FAILED;
2086 /* Start reading input before FT2232 TX buffer fills up */
2088 if (ft2232_expect_read > 256)
2090 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2091 retval = ERROR_JTAG_QUEUE_FAILED;
2096 if (require_send > 0)
2097 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2098 retval = ERROR_JTAG_QUEUE_FAILED;
2103 #if BUILD_FT2232_FTD2XX == 1
2104 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2108 char SerialNumber[16];
2109 char Description[64];
2110 DWORD openex_flags = 0;
2111 char* openex_string = NULL;
2112 uint8_t latency_timer;
2114 if (layout == NULL) {
2115 LOG_WARNING("No ft2232 layout specified'");
2116 return ERROR_JTAG_INIT_FAILED;
2119 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2122 /* Add non-standard Vid/Pid to the linux driver */
2123 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2125 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2129 if (ft2232_device_desc && ft2232_serial)
2131 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2132 ft2232_device_desc = NULL;
2135 if (ft2232_device_desc)
2137 openex_string = ft2232_device_desc;
2138 openex_flags = FT_OPEN_BY_DESCRIPTION;
2140 else if (ft2232_serial)
2142 openex_string = ft2232_serial;
2143 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2147 LOG_ERROR("neither device description nor serial number specified");
2148 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2150 return ERROR_JTAG_INIT_FAILED;
2153 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2154 if (status != FT_OK) {
2155 /* under Win32, the FTD2XX driver appends an "A" to the end
2156 * of the description, if we tried by the desc, then
2157 * try by the alternate "A" description. */
2158 if (openex_string == ft2232_device_desc) {
2159 /* Try the alternate method. */
2160 openex_string = ft2232_device_desc_A;
2161 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2162 if (status == FT_OK) {
2163 /* yea, the "alternate" method worked! */
2165 /* drat, give the user a meaningfull message.
2166 * telling the use we tried *BOTH* methods. */
2167 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2169 ft2232_device_desc_A);
2174 if (status != FT_OK)
2180 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2182 return ERROR_JTAG_INIT_FAILED;
2184 LOG_ERROR("unable to open ftdi device: %lu", status);
2185 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2186 if (status == FT_OK)
2188 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2191 for (i = 0; i < num_devices; i++)
2192 desc_array[i] = malloc(64);
2194 desc_array[num_devices] = NULL;
2196 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2198 if (status == FT_OK)
2200 LOG_ERROR("ListDevices: %lu", num_devices);
2201 for (i = 0; i < num_devices; i++)
2202 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2205 for (i = 0; i < num_devices; i++)
2206 free(desc_array[i]);
2212 LOG_ERROR("ListDevices: NONE");
2214 return ERROR_JTAG_INIT_FAILED;
2217 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2219 LOG_ERROR("unable to set latency timer: %lu", status);
2220 return ERROR_JTAG_INIT_FAILED;
2223 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2225 LOG_ERROR("unable to get latency timer: %lu", status);
2226 return ERROR_JTAG_INIT_FAILED;
2230 LOG_DEBUG("current latency timer: %i", latency_timer);
2233 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2235 LOG_ERROR("unable to set timeouts: %lu", status);
2236 return ERROR_JTAG_INIT_FAILED;
2239 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2241 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2242 return ERROR_JTAG_INIT_FAILED;
2245 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2247 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2248 return ERROR_JTAG_INIT_FAILED;
2252 static const char* type_str[] =
2253 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2254 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2255 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2256 ? ftdi_device : FT_DEVICE_UNKNOWN;
2257 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2258 LOG_INFO("deviceID: %lu", deviceID);
2259 LOG_INFO("SerialNumber: %s", SerialNumber);
2260 LOG_INFO("Description: %s", Description);
2266 static int ft2232_purge_ftd2xx(void)
2270 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2272 LOG_ERROR("error purging ftd2xx device: %lu", status);
2273 return ERROR_JTAG_INIT_FAILED;
2279 #endif /* BUILD_FT2232_FTD2XX == 1 */
2281 #if BUILD_FT2232_LIBFTDI == 1
2282 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2284 uint8_t latency_timer;
2286 if (layout == NULL) {
2287 LOG_WARNING("No ft2232 layout specified'");
2288 return ERROR_JTAG_INIT_FAILED;
2291 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2292 layout->name, vid, pid);
2294 if (ftdi_init(&ftdic) < 0)
2295 return ERROR_JTAG_INIT_FAILED;
2297 /* default to INTERFACE_A */
2298 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2300 if (ftdi_set_interface(&ftdic, channel) < 0)
2302 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2303 return ERROR_JTAG_INIT_FAILED;
2306 /* context, vendor id, product id */
2307 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2311 LOG_WARNING("unable to open ftdi device (trying more): %s",
2314 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2316 return ERROR_JTAG_INIT_FAILED;
2319 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2320 if (ftdi_usb_reset(&ftdic) < 0)
2322 LOG_ERROR("unable to reset ftdi device");
2323 return ERROR_JTAG_INIT_FAILED;
2326 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2328 LOG_ERROR("unable to set latency timer");
2329 return ERROR_JTAG_INIT_FAILED;
2332 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2334 LOG_ERROR("unable to get latency timer");
2335 return ERROR_JTAG_INIT_FAILED;
2339 LOG_DEBUG("current latency timer: %i", latency_timer);
2342 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2344 ftdi_device = ftdic.type;
2345 static const char* type_str[] =
2346 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2347 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2348 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2349 ? ftdi_device : no_of_known_types;
2350 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2354 static int ft2232_purge_libftdi(void)
2356 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2358 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2359 return ERROR_JTAG_INIT_FAILED;
2365 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2367 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2370 uint32_t bytes_written;
2372 buf[0] = 0x80; /* command "set data bits low byte" */
2373 buf[1] = value; /* value */
2374 buf[2] = direction; /* direction */
2376 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2378 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2380 LOG_ERROR("couldn't initialize data bits low byte");
2381 return ERROR_JTAG_INIT_FAILED;
2387 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2390 uint32_t bytes_written;
2392 buf[0] = 0x82; /* command "set data bits high byte" */
2393 buf[1] = value; /* value */
2394 buf[2] = direction; /* direction */
2396 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2398 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2400 LOG_ERROR("couldn't initialize data bits high byte");
2401 return ERROR_JTAG_INIT_FAILED;
2407 static int ft2232_init(void)
2411 uint32_t bytes_written;
2413 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2415 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2419 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2422 if (layout == NULL) {
2423 LOG_WARNING("No ft2232 layout specified'");
2424 return ERROR_JTAG_INIT_FAILED;
2427 for (int i = 0; 1; i++)
2430 * "more indicates that there are more IDs to try, so we should
2431 * not print an error for an ID mismatch (but for anything
2434 * try_more indicates that the error code returned indicates an
2435 * ID mismatch (and nothing else) and that we should proceeed
2436 * with the next ID pair.
2438 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2441 #if BUILD_FT2232_FTD2XX == 1
2442 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2444 #elif BUILD_FT2232_LIBFTDI == 1
2445 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2446 more, &try_more, layout->channel);
2450 if (!more || !try_more)
2454 ft2232_buffer_size = 0;
2455 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2457 if (layout->init() != ERROR_OK)
2458 return ERROR_JTAG_INIT_FAILED;
2460 if (ft2232_device_is_highspeed())
2462 #ifndef BUILD_FT2232_HIGHSPEED
2463 #if BUILD_FT2232_FTD2XX == 1
2464 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2465 #elif BUILD_FT2232_LIBFTDI == 1
2466 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2469 /* make sure the legacy mode is disabled */
2470 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2471 return ERROR_JTAG_INIT_FAILED;
2475 retval = jtag_get_speed(&jtag_speed_var);
2476 if (retval != ERROR_OK)
2478 ft2232_speed(jtag_speed_var);
2480 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2481 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2483 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2484 return ERROR_JTAG_INIT_FAILED;
2487 #if BUILD_FT2232_FTD2XX == 1
2488 return ft2232_purge_ftd2xx();
2489 #elif BUILD_FT2232_LIBFTDI == 1
2490 return ft2232_purge_libftdi();
2496 /** Updates defaults for DBUS signals: the four JTAG signals
2497 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2499 static inline void ftx232_dbus_init(void)
2502 low_direction = 0x0b;
2505 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2506 * the four GPIOL signals. Initialization covers value and direction,
2507 * as customized for each layout.
2509 static int ftx232_dbus_write(void)
2511 enum reset_types jtag_reset_config = jtag_get_reset_config();
2512 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2514 low_direction &= ~nTRSTnOE; /* nTRST input */
2515 low_output &= ~nTRST; /* nTRST = 0 */
2519 low_direction |= nTRSTnOE; /* nTRST output */
2520 low_output |= nTRST; /* nTRST = 1 */
2523 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2525 low_direction |= nSRSTnOE; /* nSRST output */
2526 low_output |= nSRST; /* nSRST = 1 */
2530 low_direction &= ~nSRSTnOE; /* nSRST input */
2531 low_output &= ~nSRST; /* nSRST = 0 */
2534 /* initialize low byte for jtag */
2535 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2537 LOG_ERROR("couldn't initialize FT2232 DBUS");
2538 return ERROR_JTAG_INIT_FAILED;
2544 static int usbjtag_init(void)
2547 * NOTE: This is now _specific_ to the "usbjtag" layout.
2548 * Don't try cram any more layouts into this.
2557 return ftx232_dbus_write();
2560 static int lm3s811_jtag_init(void)
2564 /* There are multiple revisions of LM3S811 eval boards:
2565 * - Rev B (and older?) boards have no SWO trace support.
2566 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2567 * they should use the "luminary_icdi" layout instead.
2574 low_direction = 0x8b;
2576 return ftx232_dbus_write();
2579 static int icdi_jtag_init(void)
2583 /* Most Luminary eval boards support SWO trace output,
2584 * and should use this "luminary_icdi" layout.
2586 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2587 * to switch between JTAG and SWD, or switch the ft2232 UART
2588 * on the second MPSSE channel/interface (BDBUS)
2589 * between (i) the stellaris UART (on Luminary boards)
2590 * or (ii) SWO trace data (generic).
2592 * We come up in JTAG mode and may switch to SWD later (with
2593 * SWO/trace option if SWD is active).
2600 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2601 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2602 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2605 /* GPIOs on second channel/interface (UART) ... */
2606 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2607 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2608 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2613 nSRSTnOE = ICDI_SRST;
2615 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2616 low_output |= ICDI_JTAG_EN;
2617 low_output &= ~ICDI_DBG_ENn;
2619 return ftx232_dbus_write();
2622 static int signalyzer_init(void)
2630 return ftx232_dbus_write();
2633 static int axm0432_jtag_init(void)
2636 low_direction = 0x2b;
2638 /* initialize low byte for jtag */
2639 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2641 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2642 return ERROR_JTAG_INIT_FAILED;
2645 if (strcmp(layout->name, "axm0432_jtag") == 0)
2648 nTRSTnOE = 0x0; /* No output enable for TRST*/
2650 nSRSTnOE = 0x0; /* No output enable for SRST*/
2654 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2659 high_direction = 0x0c;
2661 enum reset_types jtag_reset_config = jtag_get_reset_config();
2662 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2664 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2668 high_output |= nTRST;
2671 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2673 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2677 high_output |= nSRST;
2680 /* initialize high byte for jtag */
2681 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2683 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2684 return ERROR_JTAG_INIT_FAILED;
2690 static int redbee_init(void)
2693 low_direction = 0x2b;
2695 /* initialize low byte for jtag */
2696 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2698 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2699 return ERROR_JTAG_INIT_FAILED;
2703 nTRSTnOE = 0x0; /* No output enable for TRST*/
2705 nSRSTnOE = 0x0; /* No output enable for SRST*/
2708 high_direction = 0x0c;
2710 enum reset_types jtag_reset_config = jtag_get_reset_config();
2711 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2713 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2717 high_output |= nTRST;
2720 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2722 LOG_ERROR("can't set nSRST to push-pull on redbee");
2726 high_output |= nSRST;
2729 /* initialize high byte for jtag */
2730 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2732 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2733 return ERROR_JTAG_INIT_FAILED;
2739 static int jtagkey_init(void)
2742 low_direction = 0x1b;
2744 /* initialize low byte for jtag */
2745 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2747 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2748 return ERROR_JTAG_INIT_FAILED;
2751 if (strcmp(layout->name, "jtagkey") == 0)
2758 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2759 || (strcmp(layout->name, "oocdlink") == 0))
2768 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2773 high_direction = 0x0f;
2775 enum reset_types jtag_reset_config = jtag_get_reset_config();
2776 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2778 high_output |= nTRSTnOE;
2779 high_output &= ~nTRST;
2783 high_output &= ~nTRSTnOE;
2784 high_output |= nTRST;
2787 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2789 high_output &= ~nSRSTnOE;
2790 high_output |= nSRST;
2794 high_output |= nSRSTnOE;
2795 high_output &= ~nSRST;
2798 /* initialize high byte for jtag */
2799 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2801 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2802 return ERROR_JTAG_INIT_FAILED;
2808 static int olimex_jtag_init(void)
2811 low_direction = 0x1b;
2813 /* initialize low byte for jtag */
2814 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2816 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2817 return ERROR_JTAG_INIT_FAILED;
2823 nSRSTnOE = 0x00; /* no output enable for nSRST */
2826 high_direction = 0x0f;
2828 enum reset_types jtag_reset_config = jtag_get_reset_config();
2829 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2831 high_output |= nTRSTnOE;
2832 high_output &= ~nTRST;
2836 high_output &= ~nTRSTnOE;
2837 high_output |= nTRST;
2840 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2842 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2846 high_output &= ~nSRST;
2849 /* turn red LED on */
2850 high_output |= 0x08;
2852 /* initialize high byte for jtag */
2853 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2855 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2856 return ERROR_JTAG_INIT_FAILED;
2862 static int flyswatter_init(void)
2865 low_direction = 0xfb;
2867 /* initialize low byte for jtag */
2868 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2870 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2871 return ERROR_JTAG_INIT_FAILED;
2875 nTRSTnOE = 0x0; /* not output enable for nTRST */
2877 nSRSTnOE = 0x00; /* no output enable for nSRST */
2880 high_direction = 0x0c;
2882 /* turn red LED3 on, LED2 off */
2883 high_output |= 0x08;
2885 /* initialize high byte for jtag */
2886 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2888 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2889 return ERROR_JTAG_INIT_FAILED;
2895 static int turtle_init(void)
2898 low_direction = 0x5b;
2900 /* initialize low byte for jtag */
2901 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2903 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2904 return ERROR_JTAG_INIT_FAILED;
2910 high_direction = 0x0C;
2912 /* initialize high byte for jtag */
2913 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2915 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2916 return ERROR_JTAG_INIT_FAILED;
2922 static int comstick_init(void)
2925 low_direction = 0x0b;
2927 /* initialize low byte for jtag */
2928 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2930 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2931 return ERROR_JTAG_INIT_FAILED;
2935 nTRSTnOE = 0x00; /* no output enable for nTRST */
2937 nSRSTnOE = 0x00; /* no output enable for nSRST */
2940 high_direction = 0x03;
2942 /* initialize high byte for jtag */
2943 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2945 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2946 return ERROR_JTAG_INIT_FAILED;
2952 static int stm32stick_init(void)
2955 low_direction = 0x8b;
2957 /* initialize low byte for jtag */
2958 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2960 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2961 return ERROR_JTAG_INIT_FAILED;
2965 nTRSTnOE = 0x00; /* no output enable for nTRST */
2967 nSRSTnOE = 0x00; /* no output enable for nSRST */
2970 high_direction = 0x03;
2972 /* initialize high byte for jtag */
2973 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2975 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2976 return ERROR_JTAG_INIT_FAILED;
2982 static int sheevaplug_init(void)
2985 low_direction = 0x1b;
2987 /* initialize low byte for jtag */
2988 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2990 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2991 return ERROR_JTAG_INIT_FAILED;
3000 high_direction = 0x0f;
3002 /* nTRST is always push-pull */
3003 high_output &= ~nTRSTnOE;
3004 high_output |= nTRST;
3006 /* nSRST is always open-drain */
3007 high_output |= nSRSTnOE;
3008 high_output &= ~nSRST;
3010 /* initialize high byte for jtag */
3011 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3013 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3014 return ERROR_JTAG_INIT_FAILED;
3020 static int cortino_jtag_init(void)
3023 low_direction = 0x1b;
3025 /* initialize low byte for jtag */
3026 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3028 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3029 return ERROR_JTAG_INIT_FAILED;
3033 nTRSTnOE = 0x00; /* no output enable for nTRST */
3035 nSRSTnOE = 0x00; /* no output enable for nSRST */
3038 high_direction = 0x03;
3040 /* initialize high byte for jtag */
3041 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3043 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3044 return ERROR_JTAG_INIT_FAILED;
3050 static int lisa_l_init(void)
3060 high_direction = 0x18;
3062 /* initialize high byte for jtag */
3063 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3065 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3066 return ERROR_JTAG_INIT_FAILED;
3069 return ftx232_dbus_write();
3072 static int flossjtag_init(void)
3082 high_direction = 0x18;
3084 /* initialize high byte for jtag */
3085 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3087 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3088 return ERROR_JTAG_INIT_FAILED;
3091 return ftx232_dbus_write();
3094 static int xds100v2_init(void)
3097 low_direction = 0x7B;
3099 /* initialize low byte for jtag */
3100 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3102 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3103 return ERROR_JTAG_INIT_FAILED;
3107 nTRSTnOE = 0x0; /* not output enable for nTRST */
3108 nSRST = 0x00; /* TODO: SRST is not supported yet */
3109 nSRSTnOE = 0x00; /* no output enable for nSRST */
3112 high_direction = 0x59;
3114 /* initialize high byte for jtag */
3115 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3117 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3118 return ERROR_JTAG_INIT_FAILED;
3122 high_direction = 0x59;
3124 /* initialize high byte for jtag */
3125 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3127 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3128 return ERROR_JTAG_INIT_FAILED;
3134 static void olimex_jtag_blink(void)
3136 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3137 * ACBUS3 is bit 3 of the GPIOH port
3139 high_output ^= 0x08;
3142 buffer_write(high_output);
3143 buffer_write(high_direction);
3146 static void flyswatter_jtag_blink(void)
3149 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3151 high_output ^= 0x0c;
3154 buffer_write(high_output);
3155 buffer_write(high_direction);
3158 static void turtle_jtag_blink(void)
3161 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3163 if (high_output & 0x08)
3173 buffer_write(high_output);
3174 buffer_write(high_direction);
3177 static void lisa_l_blink(void)
3180 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3182 if (high_output & 0x10)
3192 buffer_write(high_output);
3193 buffer_write(high_direction);
3196 static void flossjtag_blink(void)
3199 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3201 if (high_output & 0x10)
3211 buffer_write(high_output);
3212 buffer_write(high_direction);
3215 static int ft2232_quit(void)
3217 #if BUILD_FT2232_FTD2XX == 1
3220 status = FT_Close(ftdih);
3221 #elif BUILD_FT2232_LIBFTDI == 1
3222 ftdi_usb_close(&ftdic);
3224 ftdi_deinit(&ftdic);
3227 free(ft2232_buffer);
3228 ft2232_buffer = NULL;
3233 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3239 ft2232_device_desc = strdup(CMD_ARGV[0]);
3240 cp = strchr(ft2232_device_desc, 0);
3241 /* under Win32, the FTD2XX driver appends an "A" to the end
3242 * of the description, this examines the given desc
3243 * and creates the 'missing' _A or non_A variable. */
3244 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3245 /* it was, so make this the "A" version. */
3246 ft2232_device_desc_A = ft2232_device_desc;
3247 /* and *CREATE* the non-A version. */
3248 strcpy(buf, ft2232_device_desc);
3249 cp = strchr(buf, 0);
3251 ft2232_device_desc = strdup(buf);
3253 /* <space > A not defined
3255 sprintf(buf, "%s A", ft2232_device_desc);
3256 ft2232_device_desc_A = strdup(buf);
3261 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3267 COMMAND_HANDLER(ft2232_handle_serial_command)
3271 ft2232_serial = strdup(CMD_ARGV[0]);
3275 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3281 COMMAND_HANDLER(ft2232_handle_layout_command)
3283 if (CMD_ARGC != 1) {
3284 LOG_ERROR("Need exactly one argument to ft2232_layout");
3289 LOG_ERROR("already specified ft2232_layout %s",
3291 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3296 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3297 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3303 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3307 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3309 if (CMD_ARGC > MAX_USB_IDS * 2)
3311 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3312 "(maximum is %d pairs)", MAX_USB_IDS);
3313 CMD_ARGC = MAX_USB_IDS * 2;
3315 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3317 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3319 return ERROR_COMMAND_SYNTAX_ERROR;
3320 /* remove the incomplete trailing id */
3325 for (i = 0; i < CMD_ARGC; i += 2)
3327 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3328 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3332 * Explicitly terminate, in case there are multiples instances of
3335 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3340 COMMAND_HANDLER(ft2232_handle_latency_command)
3344 ft2232_latency = atoi(CMD_ARGV[0]);
3348 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3354 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3358 /* 7 bits of either ones or zeros. */
3359 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3361 while (num_cycles > 0)
3363 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3364 * at most 7 bits per invocation. Here we invoke it potentially
3367 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3369 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3371 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3372 retval = ERROR_JTAG_QUEUE_FAILED;
3377 /* there are no state transitions in this code, so omit state tracking */
3379 /* command "Clock Data to TMS/CS Pin (no Read)" */
3383 buffer_write(bitcount_per_command - 1);
3385 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3390 num_cycles -= bitcount_per_command;
3396 /* ---------------------------------------------------------------------
3397 * Support for IceBear JTAG adapter from Section5:
3398 * http://section5.ch/icebear
3400 * Author: Sten, debian@sansys-electronic.com
3403 /* Icebear pin layout
3405 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3406 * GND GND | 4 3| n.c.
3407 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3408 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3409 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3410 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3411 * ADBUS2 TDO |14 13| GND GND
3413 * ADBUS0 O L TCK ACBUS0 GND
3414 * ADBUS1 O L TDI ACBUS1 GND
3415 * ADBUS2 I TDO ACBUS2 n.c.
3416 * ADBUS3 O H TMS ACBUS3 n.c.
3422 static int icebear_jtag_init(void) {
3423 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3424 low_output = 0x08; /* high: TMS; low: TCK TDI */
3428 enum reset_types jtag_reset_config = jtag_get_reset_config();
3429 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3430 low_direction &= ~nTRST; /* nTRST high impedance */
3433 low_direction |= nTRST;
3434 low_output |= nTRST;
3437 low_direction |= nSRST;
3438 low_output |= nSRST;
3440 /* initialize low byte for jtag */
3441 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3442 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3443 return ERROR_JTAG_INIT_FAILED;
3447 high_direction = 0x00;
3449 /* initialize high byte for jtag */
3450 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3451 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3452 return ERROR_JTAG_INIT_FAILED;
3458 static void icebear_jtag_reset(int trst, int srst) {
3461 low_direction |= nTRST;
3462 low_output &= ~nTRST;
3464 else if (trst == 0) {
3465 enum reset_types jtag_reset_config = jtag_get_reset_config();
3466 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3467 low_direction &= ~nTRST;
3469 low_output |= nTRST;
3473 low_output &= ~nSRST;
3475 else if (srst == 0) {
3476 low_output |= nSRST;
3479 /* command "set data bits low byte" */
3481 buffer_write(low_output);
3482 buffer_write(low_direction);
3484 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3487 /* ---------------------------------------------------------------------
3488 * Support for Signalyzer H2 and Signalyzer H4
3489 * JTAG adapter from Xverve Technologies Inc.
3490 * http://www.signalyzer.com or http://www.xverve.com
3492 * Author: Oleg Seiljus, oleg@signalyzer.com
3494 static unsigned char signalyzer_h_side;
3495 static unsigned int signalyzer_h_adapter_type;
3497 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3499 #if BUILD_FT2232_FTD2XX == 1
3500 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3503 #define SIGNALYZER_COMMAND_ADDR 128
3504 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3506 #define SIGNALYZER_COMMAND_VERSION 0x41
3507 #define SIGNALYZER_COMMAND_RESET 0x42
3508 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3509 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3510 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3511 #define SIGNALYZER_COMMAND_LED_SET 0x53
3512 #define SIGNALYZER_COMMAND_ADC 0x54
3513 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3514 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3515 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3516 #define SIGNALYZER_COMMAND_I2C 0x58
3518 #define SIGNALYZER_CHAN_A 1
3519 #define SIGNALYZER_CHAN_B 2
3520 /* LEDS use channel C */
3521 #define SIGNALYZER_CHAN_C 4
3523 #define SIGNALYZER_LED_GREEN 1
3524 #define SIGNALYZER_LED_RED 2
3526 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3527 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3528 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3529 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3530 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3533 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3535 #if BUILD_FT2232_FTD2XX == 1
3536 return FT_WriteEE(ftdih, address, value);
3537 #elif BUILD_FT2232_LIBFTDI == 1
3542 #if BUILD_FT2232_FTD2XX == 1
3543 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3545 return FT_ReadEE(ftdih, address, value);
3549 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3550 int on_time_ms, int off_time_ms, unsigned char cycles)
3552 unsigned char on_time;
3553 unsigned char off_time;
3555 if (on_time_ms < 0xFFFF)
3556 on_time = (unsigned char)(on_time_ms / 62);
3560 off_time = (unsigned char)(off_time_ms / 62);
3562 #if BUILD_FT2232_FTD2XX == 1
3565 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3566 ((uint32_t)(channel << 8) | led))) != FT_OK)
3568 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3569 return ERROR_JTAG_DEVICE_ERROR;
3572 if ((status = signalyzer_h_ctrl_write(
3573 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3574 ((uint32_t)(on_time << 8) | off_time))) != 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 + 2),
3582 ((uint32_t)cycles))) != 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(SIGNALYZER_COMMAND_ADDR,
3589 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3591 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3592 return ERROR_JTAG_DEVICE_ERROR;
3596 #elif BUILD_FT2232_LIBFTDI == 1
3599 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3600 ((uint32_t)(channel << 8) | led))) < 0)
3602 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3603 ftdi_get_error_string(&ftdic));
3604 return ERROR_JTAG_DEVICE_ERROR;
3607 if ((retval = signalyzer_h_ctrl_write(
3608 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3609 ((uint32_t)(on_time << 8) | off_time))) < 0)
3611 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3612 ftdi_get_error_string(&ftdic));
3613 return ERROR_JTAG_DEVICE_ERROR;
3616 if ((retval = signalyzer_h_ctrl_write(
3617 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3618 (uint32_t)cycles)) < 0)
3620 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3621 ftdi_get_error_string(&ftdic));
3622 return ERROR_JTAG_DEVICE_ERROR;
3625 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3626 SIGNALYZER_COMMAND_LED_SET)) < 0)
3628 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3629 ftdi_get_error_string(&ftdic));
3630 return ERROR_JTAG_DEVICE_ERROR;
3637 static int signalyzer_h_init(void)
3639 #if BUILD_FT2232_FTD2XX == 1
3646 uint16_t read_buf[12] = { 0 };
3648 /* turn on center green led */
3649 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3650 0xFFFF, 0x00, 0x00);
3652 /* determine what channel config wants to open
3653 * TODO: change me... current implementation is made to work
3654 * with openocd description parsing.
3656 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3660 signalyzer_h_side = *(end_of_desc - 1);
3661 if (signalyzer_h_side == 'B')
3662 signalyzer_h_side = SIGNALYZER_CHAN_B;
3664 signalyzer_h_side = SIGNALYZER_CHAN_A;
3668 LOG_ERROR("No Channel was specified");
3672 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3675 #if BUILD_FT2232_FTD2XX == 1
3676 /* read signalyzer versionining information */
3677 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3678 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3680 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3681 return ERROR_JTAG_DEVICE_ERROR;
3684 for (i = 0; i < 10; i++)
3686 if ((status = signalyzer_h_ctrl_read(
3687 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3688 &read_buf[i])) != FT_OK)
3690 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3692 return ERROR_JTAG_DEVICE_ERROR;
3696 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3697 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3698 read_buf[4], read_buf[5], read_buf[6]);
3700 /* set gpio register */
3701 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3702 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3704 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3705 return ERROR_JTAG_DEVICE_ERROR;
3708 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3711 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3712 return ERROR_JTAG_DEVICE_ERROR;
3715 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3716 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3718 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3719 return ERROR_JTAG_DEVICE_ERROR;
3722 /* read adapter type information */
3723 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3724 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3726 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3727 return ERROR_JTAG_DEVICE_ERROR;
3730 if ((status = signalyzer_h_ctrl_write(
3731 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3733 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3734 return ERROR_JTAG_DEVICE_ERROR;
3737 if ((status = signalyzer_h_ctrl_write(
3738 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3740 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3741 return ERROR_JTAG_DEVICE_ERROR;
3744 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3745 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3747 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3748 return ERROR_JTAG_DEVICE_ERROR;
3753 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3754 &read_buf[0])) != FT_OK)
3756 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3757 return ERROR_JTAG_DEVICE_ERROR;
3760 if (read_buf[0] != 0x0498)
3761 signalyzer_h_adapter_type = 0x0000;
3764 for (i = 0; i < 4; i++)
3766 if ((status = signalyzer_h_ctrl_read(
3767 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3768 &read_buf[i])) != FT_OK)
3770 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3772 return ERROR_JTAG_DEVICE_ERROR;
3776 signalyzer_h_adapter_type = read_buf[0];
3779 #elif BUILD_FT2232_LIBFTDI == 1
3780 /* currently libftdi does not allow reading individual eeprom
3781 * locations, therefore adapter type cannot be detected.
3782 * override with most common type
3784 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3787 enum reset_types jtag_reset_config = jtag_get_reset_config();
3789 /* ADAPTOR: EM_LT16_A */
3790 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3792 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3793 "detected. (HW: %2x).", (read_buf[1] >> 8));
3801 low_direction = 0x1b;
3804 high_direction = 0x0;
3806 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3808 low_direction &= ~nTRSTnOE; /* nTRST input */
3809 low_output &= ~nTRST; /* nTRST = 0 */
3813 low_direction |= nTRSTnOE; /* nTRST output */
3814 low_output |= nTRST; /* nTRST = 1 */
3817 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3819 low_direction |= nSRSTnOE; /* nSRST output */
3820 low_output |= nSRST; /* nSRST = 1 */
3824 low_direction &= ~nSRSTnOE; /* nSRST input */
3825 low_output &= ~nSRST; /* nSRST = 0 */
3828 #if BUILD_FT2232_FTD2XX == 1
3829 /* enable power to the module */
3830 if ((status = signalyzer_h_ctrl_write(
3831 SIGNALYZER_DATA_BUFFER_ADDR,
3832 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3835 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3837 return ERROR_JTAG_DEVICE_ERROR;
3840 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3841 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3843 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3845 return ERROR_JTAG_DEVICE_ERROR;
3848 /* set gpio mode register */
3849 if ((status = signalyzer_h_ctrl_write(
3850 SIGNALYZER_DATA_BUFFER_ADDR,
3851 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3853 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3855 return ERROR_JTAG_DEVICE_ERROR;
3858 if ((status = signalyzer_h_ctrl_write(
3859 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3862 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3864 return ERROR_JTAG_DEVICE_ERROR;
3867 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3868 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3870 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3872 return ERROR_JTAG_DEVICE_ERROR;
3875 /* set gpio register */
3876 if ((status = signalyzer_h_ctrl_write(
3877 SIGNALYZER_DATA_BUFFER_ADDR,
3878 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3880 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3882 return ERROR_JTAG_DEVICE_ERROR;
3885 if ((status = signalyzer_h_ctrl_write(
3886 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3889 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3891 return ERROR_JTAG_DEVICE_ERROR;
3894 if ((status = signalyzer_h_ctrl_write(
3895 SIGNALYZER_COMMAND_ADDR,
3896 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3898 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3900 return ERROR_JTAG_DEVICE_ERROR;
3905 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3906 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3907 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3908 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3909 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3911 if (signalyzer_h_adapter_type
3912 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3913 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3914 "detected. (HW: %2x).", (read_buf[1] >> 8));
3915 else if (signalyzer_h_adapter_type
3916 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3917 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3918 "(ARM JTAG with PSU) detected. (HW: %2x).",
3919 (read_buf[1] >> 8));
3920 else if (signalyzer_h_adapter_type
3921 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3922 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3923 "detected. (HW: %2x).", (read_buf[1] >> 8));
3924 else if (signalyzer_h_adapter_type
3925 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3926 LOG_INFO("Signalyzer: EM-JTAG-P "
3927 "(Generic JTAG with PSU) detected. (HW: %2x).",
3928 (read_buf[1] >> 8));
3936 low_direction = 0x1b;
3939 high_direction = 0x1f;
3941 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3943 high_output |= nTRSTnOE;
3944 high_output &= ~nTRST;
3948 high_output &= ~nTRSTnOE;
3949 high_output |= nTRST;
3952 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3954 high_output &= ~nSRSTnOE;
3955 high_output |= nSRST;
3959 high_output |= nSRSTnOE;
3960 high_output &= ~nSRST;
3963 #if BUILD_FT2232_FTD2XX == 1
3964 /* enable power to the module */
3965 if ((status = signalyzer_h_ctrl_write(
3966 SIGNALYZER_DATA_BUFFER_ADDR,
3967 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3970 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3972 return ERROR_JTAG_DEVICE_ERROR;
3975 if ((status = signalyzer_h_ctrl_write(
3976 SIGNALYZER_COMMAND_ADDR,
3977 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3979 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3981 return ERROR_JTAG_DEVICE_ERROR;
3984 /* set gpio mode register (IO_16 and IO_17 set as analog
3985 * inputs, other is gpio)
3987 if ((status = signalyzer_h_ctrl_write(
3988 SIGNALYZER_DATA_BUFFER_ADDR,
3989 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3991 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3993 return ERROR_JTAG_DEVICE_ERROR;
3996 if ((status = signalyzer_h_ctrl_write(
3997 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4000 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4002 return ERROR_JTAG_DEVICE_ERROR;
4005 if ((status = signalyzer_h_ctrl_write(
4006 SIGNALYZER_COMMAND_ADDR,
4007 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4009 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4011 return ERROR_JTAG_DEVICE_ERROR;
4014 /* set gpio register (all inputs, for -P modules,
4015 * PSU will be turned off)
4017 if ((status = signalyzer_h_ctrl_write(
4018 SIGNALYZER_DATA_BUFFER_ADDR,
4019 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4021 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4023 return ERROR_JTAG_DEVICE_ERROR;
4026 if ((status = signalyzer_h_ctrl_write(
4027 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4030 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4032 return ERROR_JTAG_DEVICE_ERROR;
4035 if ((status = signalyzer_h_ctrl_write(
4036 SIGNALYZER_COMMAND_ADDR,
4037 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4039 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4041 return ERROR_JTAG_DEVICE_ERROR;
4046 else if (signalyzer_h_adapter_type == 0x0000)
4048 LOG_INFO("Signalyzer: No external modules were detected.");
4056 low_direction = 0x1b;
4059 high_direction = 0x0;
4061 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4063 low_direction &= ~nTRSTnOE; /* nTRST input */
4064 low_output &= ~nTRST; /* nTRST = 0 */
4068 low_direction |= nTRSTnOE; /* nTRST output */
4069 low_output |= nTRST; /* nTRST = 1 */
4072 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4074 low_direction |= nSRSTnOE; /* nSRST output */
4075 low_output |= nSRST; /* nSRST = 1 */
4079 low_direction &= ~nSRSTnOE; /* nSRST input */
4080 low_output &= ~nSRST; /* nSRST = 0 */
4085 LOG_ERROR("Unknown module type is detected: %.4x",
4086 signalyzer_h_adapter_type);
4087 return ERROR_JTAG_DEVICE_ERROR;
4090 /* initialize low byte of controller for jtag operation */
4091 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4093 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4094 return ERROR_JTAG_INIT_FAILED;
4097 #if BUILD_FT2232_FTD2XX == 1
4098 if (ftdi_device == FT_DEVICE_2232H)
4100 /* initialize high byte of controller for jtag operation */
4101 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4103 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4104 return ERROR_JTAG_INIT_FAILED;
4107 #elif BUILD_FT2232_LIBFTDI == 1
4108 if (ftdi_device == TYPE_2232H)
4110 /* initialize high byte of controller for jtag operation */
4111 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4113 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4114 return ERROR_JTAG_INIT_FAILED;
4121 static void signalyzer_h_reset(int trst, int srst)
4123 enum reset_types jtag_reset_config = jtag_get_reset_config();
4125 /* ADAPTOR: EM_LT16_A */
4126 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4130 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4131 /* switch to output pin (output is low) */
4132 low_direction |= nTRSTnOE;
4134 /* switch output low */
4135 low_output &= ~nTRST;
4139 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4140 /* switch to input pin (high-Z + internal
4141 * and external pullup) */
4142 low_direction &= ~nTRSTnOE;
4144 /* switch output high */
4145 low_output |= nTRST;
4150 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4151 /* switch output low */
4152 low_output &= ~nSRST;
4154 /* switch to output pin (output is low) */
4155 low_direction |= nSRSTnOE;
4159 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4160 /* switch output high */
4161 low_output |= nSRST;
4163 /* switch to input pin (high-Z) */
4164 low_direction &= ~nSRSTnOE;
4167 /* command "set data bits low byte" */
4169 buffer_write(low_output);
4170 buffer_write(low_direction);
4171 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4172 "low_direction: 0x%2.2x",
4173 trst, srst, low_output, low_direction);
4175 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4176 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4177 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4178 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4179 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4183 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4184 high_output &= ~nTRSTnOE;
4186 high_output &= ~nTRST;
4190 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4191 high_output |= nTRSTnOE;
4193 high_output |= nTRST;
4198 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4199 high_output &= ~nSRST;
4201 high_output &= ~nSRSTnOE;
4205 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4206 high_output |= nSRST;
4208 high_output |= nSRSTnOE;
4211 /* command "set data bits high byte" */
4213 buffer_write(high_output);
4214 buffer_write(high_direction);
4215 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4216 "high_direction: 0x%2.2x",
4217 trst, srst, high_output, high_direction);
4219 else if (signalyzer_h_adapter_type == 0x0000)
4223 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4224 /* switch to output pin (output is low) */
4225 low_direction |= nTRSTnOE;
4227 /* switch output low */
4228 low_output &= ~nTRST;
4232 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4233 /* switch to input pin (high-Z + internal
4234 * and external pullup) */
4235 low_direction &= ~nTRSTnOE;
4237 /* switch output high */
4238 low_output |= nTRST;
4243 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4244 /* switch output low */
4245 low_output &= ~nSRST;
4247 /* switch to output pin (output is low) */
4248 low_direction |= nSRSTnOE;
4252 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4253 /* switch output high */
4254 low_output |= nSRST;
4256 /* switch to input pin (high-Z) */
4257 low_direction &= ~nSRSTnOE;
4260 /* command "set data bits low byte" */
4262 buffer_write(low_output);
4263 buffer_write(low_direction);
4264 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4265 "low_direction: 0x%2.2x",
4266 trst, srst, low_output, low_direction);
4270 static void signalyzer_h_blink(void)
4272 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4275 /********************************************************************
4276 * Support for KT-LINK
4277 * JTAG adapter from KRISTECH
4278 * http://www.kristech.eu
4279 *******************************************************************/
4280 static int ktlink_init(void)
4282 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4284 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4285 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4287 /* initialize low byte for jtag */
4288 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4290 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4291 return ERROR_JTAG_INIT_FAILED;
4299 high_output = 0x80; // turn LED on
4300 high_direction = 0xFF; // all outputs
4302 enum reset_types jtag_reset_config = jtag_get_reset_config();
4304 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4305 high_output |= nTRSTnOE;
4306 high_output &= ~nTRST;
4308 high_output &= ~nTRSTnOE;
4309 high_output |= nTRST;
4312 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4313 high_output &= ~nSRSTnOE;
4314 high_output |= nSRST;
4316 high_output |= nSRSTnOE;
4317 high_output &= ~nSRST;
4320 /* initialize high byte for jtag */
4321 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4323 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4324 return ERROR_JTAG_INIT_FAILED;
4330 static void ktlink_reset(int trst, int srst)
4332 enum reset_types jtag_reset_config = jtag_get_reset_config();
4335 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4336 high_output &= ~nTRSTnOE;
4338 high_output &= ~nTRST;
4339 } else if (trst == 0) {
4340 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4341 high_output |= nTRSTnOE;
4343 high_output |= nTRST;
4347 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4348 high_output &= ~nSRST;
4350 high_output &= ~nSRSTnOE;
4351 } else if (srst == 0) {
4352 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4353 high_output |= nSRST;
4355 high_output |= nSRSTnOE;
4358 buffer_write(0x82); // command "set data bits high byte"
4359 buffer_write(high_output);
4360 buffer_write(high_direction);
4361 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4364 static void ktlink_blink(void)
4366 /* LED connected to ACBUS7 */
4367 high_output ^= 0x80;
4369 buffer_write(0x82); // command "set data bits high byte"
4370 buffer_write(high_output);
4371 buffer_write(high_direction);
4374 static const struct command_registration ft2232_command_handlers[] = {
4376 .name = "ft2232_device_desc",
4377 .handler = &ft2232_handle_device_desc_command,
4378 .mode = COMMAND_CONFIG,
4379 .help = "set the USB device description of the FTDI FT2232 device",
4380 .usage = "description_string",
4383 .name = "ft2232_serial",
4384 .handler = &ft2232_handle_serial_command,
4385 .mode = COMMAND_CONFIG,
4386 .help = "set the serial number of the FTDI FT2232 device",
4387 .usage = "serial_string",
4390 .name = "ft2232_layout",
4391 .handler = &ft2232_handle_layout_command,
4392 .mode = COMMAND_CONFIG,
4393 .help = "set the layout of the FT2232 GPIO signals used "
4394 "to control output-enables and reset signals",
4395 .usage = "layout_name",
4398 .name = "ft2232_vid_pid",
4399 .handler = &ft2232_handle_vid_pid_command,
4400 .mode = COMMAND_CONFIG,
4401 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4402 .usage = "(vid pid)* ",
4405 .name = "ft2232_latency",
4406 .handler = &ft2232_handle_latency_command,
4407 .mode = COMMAND_CONFIG,
4408 .help = "set the FT2232 latency timer to a new value",
4411 COMMAND_REGISTRATION_DONE
4414 struct jtag_interface ft2232_interface = {
4416 .supported = DEBUG_CAP_TMS_SEQ,
4417 .commands = ft2232_command_handlers,
4418 .transports = jtag_only,
4420 .init = ft2232_init,
4421 .quit = ft2232_quit,
4422 .speed = ft2232_speed,
4423 .speed_div = ft2232_speed_div,
4425 .execute_queue = ft2232_execute_queue,