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_init(void)
2371 uint32_t bytes_written;
2373 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2375 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2379 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2382 if (layout == NULL) {
2383 LOG_WARNING("No ft2232 layout specified'");
2384 return ERROR_JTAG_INIT_FAILED;
2387 for (int i = 0; 1; i++)
2390 * "more indicates that there are more IDs to try, so we should
2391 * not print an error for an ID mismatch (but for anything
2394 * try_more indicates that the error code returned indicates an
2395 * ID mismatch (and nothing else) and that we should proceeed
2396 * with the next ID pair.
2398 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2401 #if BUILD_FT2232_FTD2XX == 1
2402 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2404 #elif BUILD_FT2232_LIBFTDI == 1
2405 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2406 more, &try_more, layout->channel);
2410 if (!more || !try_more)
2414 ft2232_buffer_size = 0;
2415 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2417 if (layout->init() != ERROR_OK)
2418 return ERROR_JTAG_INIT_FAILED;
2420 if (ft2232_device_is_highspeed())
2422 #ifndef BUILD_FT2232_HIGHSPEED
2423 #if BUILD_FT2232_FTD2XX == 1
2424 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2425 #elif BUILD_FT2232_LIBFTDI == 1
2426 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2429 /* make sure the legacy mode is disabled */
2430 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2431 return ERROR_JTAG_INIT_FAILED;
2435 retval = jtag_get_speed(&jtag_speed_var);
2436 if (retval != ERROR_OK)
2438 ft2232_speed(jtag_speed_var);
2440 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2441 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2443 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2444 return ERROR_JTAG_INIT_FAILED;
2447 #if BUILD_FT2232_FTD2XX == 1
2448 return ft2232_purge_ftd2xx();
2449 #elif BUILD_FT2232_LIBFTDI == 1
2450 return ft2232_purge_libftdi();
2456 /** Updates defaults for DBUS signals: the four JTAG signals
2457 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2459 static inline void ftx232_dbus_init(void)
2462 low_direction = 0x0b;
2465 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2466 * the four GPIOL signals. Initialization covers value and direction,
2467 * as customized for each layout.
2469 static int ftx232_dbus_write(void)
2472 uint32_t bytes_written;
2474 enum reset_types jtag_reset_config = jtag_get_reset_config();
2475 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2477 low_direction &= ~nTRSTnOE; /* nTRST input */
2478 low_output &= ~nTRST; /* nTRST = 0 */
2482 low_direction |= nTRSTnOE; /* nTRST output */
2483 low_output |= nTRST; /* nTRST = 1 */
2486 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2488 low_direction |= nSRSTnOE; /* nSRST output */
2489 low_output |= nSRST; /* nSRST = 1 */
2493 low_direction &= ~nSRSTnOE; /* nSRST input */
2494 low_output &= ~nSRST; /* nSRST = 0 */
2497 /* initialize low byte for jtag */
2498 buf[0] = 0x80; /* command "set data bits low byte" */
2499 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2500 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2501 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2503 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2505 LOG_ERROR("couldn't initialize FT2232 DBUS");
2506 return ERROR_JTAG_INIT_FAILED;
2512 static int usbjtag_init(void)
2515 * NOTE: This is now _specific_ to the "usbjtag" layout.
2516 * Don't try cram any more layouts into this.
2525 return ftx232_dbus_write();
2528 static int lm3s811_jtag_init(void)
2532 /* There are multiple revisions of LM3S811 eval boards:
2533 * - Rev B (and older?) boards have no SWO trace support.
2534 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2535 * they should use the "luminary_icdi" layout instead.
2542 low_direction = 0x8b;
2544 return ftx232_dbus_write();
2547 static int icdi_jtag_init(void)
2551 /* Most Luminary eval boards support SWO trace output,
2552 * and should use this "luminary_icdi" layout.
2554 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2555 * to switch between JTAG and SWD, or switch the ft2232 UART
2556 * on the second MPSSE channel/interface (BDBUS)
2557 * between (i) the stellaris UART (on Luminary boards)
2558 * or (ii) SWO trace data (generic).
2560 * We come up in JTAG mode and may switch to SWD later (with
2561 * SWO/trace option if SWD is active).
2568 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2569 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2570 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2573 /* GPIOs on second channel/interface (UART) ... */
2574 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2575 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2576 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2581 nSRSTnOE = ICDI_SRST;
2583 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2584 low_output |= ICDI_JTAG_EN;
2585 low_output &= ~ICDI_DBG_ENn;
2587 return ftx232_dbus_write();
2590 static int signalyzer_init(void)
2598 return ftx232_dbus_write();
2601 static int axm0432_jtag_init(void)
2604 uint32_t bytes_written;
2607 low_direction = 0x2b;
2609 /* initialize low byte for jtag */
2610 buf[0] = 0x80; /* command "set data bits low byte" */
2611 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2612 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2613 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2615 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2617 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2618 return ERROR_JTAG_INIT_FAILED;
2621 if (strcmp(layout->name, "axm0432_jtag") == 0)
2624 nTRSTnOE = 0x0; /* No output enable for TRST*/
2626 nSRSTnOE = 0x0; /* No output enable for SRST*/
2630 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2635 high_direction = 0x0c;
2637 enum reset_types jtag_reset_config = jtag_get_reset_config();
2638 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2640 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2644 high_output |= nTRST;
2647 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2649 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2653 high_output |= nSRST;
2656 /* initialize high port */
2657 buf[0] = 0x82; /* command "set data bits high byte" */
2658 buf[1] = high_output; /* value */
2659 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2660 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2662 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2664 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2665 return ERROR_JTAG_INIT_FAILED;
2671 static int redbee_init(void)
2674 uint32_t bytes_written;
2677 low_direction = 0x2b;
2679 /* initialize low byte for jtag */
2680 /* command "set data bits low byte" */
2682 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2683 buf[2] = low_direction;
2684 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2685 buf[1] = low_output;
2686 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2688 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2690 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2691 return ERROR_JTAG_INIT_FAILED;
2695 nTRSTnOE = 0x0; /* No output enable for TRST*/
2697 nSRSTnOE = 0x0; /* No output enable for SRST*/
2700 high_direction = 0x0c;
2702 enum reset_types jtag_reset_config = jtag_get_reset_config();
2703 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2705 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2709 high_output |= nTRST;
2712 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2714 LOG_ERROR("can't set nSRST to push-pull on redbee");
2718 high_output |= nSRST;
2721 /* initialize high port */
2722 buf[0] = 0x82; /* command "set data bits high byte" */
2723 buf[1] = high_output; /* value */
2724 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2725 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2727 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2729 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2730 return ERROR_JTAG_INIT_FAILED;
2736 static int jtagkey_init(void)
2739 uint32_t bytes_written;
2742 low_direction = 0x1b;
2744 /* initialize low byte for jtag */
2745 buf[0] = 0x80; /* command "set data bits low byte" */
2746 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2747 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2748 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2750 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2752 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2753 return ERROR_JTAG_INIT_FAILED;
2756 if (strcmp(layout->name, "jtagkey") == 0)
2763 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2764 || (strcmp(layout->name, "oocdlink") == 0))
2773 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2778 high_direction = 0x0f;
2780 enum reset_types jtag_reset_config = jtag_get_reset_config();
2781 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2783 high_output |= nTRSTnOE;
2784 high_output &= ~nTRST;
2788 high_output &= ~nTRSTnOE;
2789 high_output |= nTRST;
2792 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2794 high_output &= ~nSRSTnOE;
2795 high_output |= nSRST;
2799 high_output |= nSRSTnOE;
2800 high_output &= ~nSRST;
2803 /* initialize high port */
2804 buf[0] = 0x82; /* command "set data bits high byte" */
2805 buf[1] = high_output; /* value */
2806 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2807 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2809 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2811 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2812 return ERROR_JTAG_INIT_FAILED;
2818 static int olimex_jtag_init(void)
2821 uint32_t bytes_written;
2824 low_direction = 0x1b;
2826 /* initialize low byte for jtag */
2827 buf[0] = 0x80; /* command "set data bits low byte" */
2828 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2829 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2830 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2832 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2834 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2835 return ERROR_JTAG_INIT_FAILED;
2841 nSRSTnOE = 0x00; /* no output enable for nSRST */
2844 high_direction = 0x0f;
2846 enum reset_types jtag_reset_config = jtag_get_reset_config();
2847 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2849 high_output |= nTRSTnOE;
2850 high_output &= ~nTRST;
2854 high_output &= ~nTRSTnOE;
2855 high_output |= nTRST;
2858 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2860 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2864 high_output &= ~nSRST;
2867 /* turn red LED on */
2868 high_output |= 0x08;
2870 /* initialize high port */
2871 buf[0] = 0x82; /* command "set data bits high byte" */
2872 buf[1] = high_output; /* value */
2873 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2874 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2876 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2878 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2879 return ERROR_JTAG_INIT_FAILED;
2885 static int flyswatter_init(void)
2888 uint32_t bytes_written;
2891 low_direction = 0xfb;
2893 /* initialize low byte for jtag */
2894 buf[0] = 0x80; /* command "set data bits low byte" */
2895 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2896 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2897 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2899 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2901 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2902 return ERROR_JTAG_INIT_FAILED;
2906 nTRSTnOE = 0x0; /* not output enable for nTRST */
2908 nSRSTnOE = 0x00; /* no output enable for nSRST */
2911 high_direction = 0x0c;
2913 /* turn red LED3 on, LED2 off */
2914 high_output |= 0x08;
2916 /* initialize high port */
2917 buf[0] = 0x82; /* command "set data bits high byte" */
2918 buf[1] = high_output; /* value */
2919 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2920 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2922 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2924 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2925 return ERROR_JTAG_INIT_FAILED;
2931 static int turtle_init(void)
2934 uint32_t bytes_written;
2937 low_direction = 0x5b;
2939 /* initialize low byte for jtag */
2940 buf[0] = 0x80; /* command "set data bits low byte" */
2941 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2942 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2943 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2945 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2947 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2948 return ERROR_JTAG_INIT_FAILED;
2954 high_direction = 0x0C;
2956 /* initialize high port */
2957 buf[0] = 0x82; /* command "set data bits high byte" */
2958 buf[1] = high_output;
2959 buf[2] = high_direction;
2960 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2962 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2964 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2965 return ERROR_JTAG_INIT_FAILED;
2971 static int comstick_init(void)
2974 uint32_t bytes_written;
2977 low_direction = 0x0b;
2979 /* initialize low byte for jtag */
2980 buf[0] = 0x80; /* command "set data bits low byte" */
2981 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2982 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2983 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2985 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2987 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2988 return ERROR_JTAG_INIT_FAILED;
2992 nTRSTnOE = 0x00; /* no output enable for nTRST */
2994 nSRSTnOE = 0x00; /* no output enable for nSRST */
2997 high_direction = 0x03;
2999 /* initialize high port */
3000 buf[0] = 0x82; /* command "set data bits high byte" */
3001 buf[1] = high_output;
3002 buf[2] = high_direction;
3003 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3005 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3007 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3008 return ERROR_JTAG_INIT_FAILED;
3014 static int stm32stick_init(void)
3017 uint32_t bytes_written;
3020 low_direction = 0x8b;
3022 /* initialize low byte for jtag */
3023 buf[0] = 0x80; /* command "set data bits low byte" */
3024 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3025 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3026 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3028 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3030 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3031 return ERROR_JTAG_INIT_FAILED;
3035 nTRSTnOE = 0x00; /* no output enable for nTRST */
3037 nSRSTnOE = 0x00; /* no output enable for nSRST */
3040 high_direction = 0x03;
3042 /* initialize high port */
3043 buf[0] = 0x82; /* command "set data bits high byte" */
3044 buf[1] = high_output;
3045 buf[2] = high_direction;
3046 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3048 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3050 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3051 return ERROR_JTAG_INIT_FAILED;
3057 static int sheevaplug_init(void)
3060 uint32_t bytes_written;
3063 low_direction = 0x1b;
3065 /* initialize low byte for jtag */
3066 buf[0] = 0x80; /* command "set data bits low byte" */
3067 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3068 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3069 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3071 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3073 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3074 return ERROR_JTAG_INIT_FAILED;
3083 high_direction = 0x0f;
3085 /* nTRST is always push-pull */
3086 high_output &= ~nTRSTnOE;
3087 high_output |= nTRST;
3089 /* nSRST is always open-drain */
3090 high_output |= nSRSTnOE;
3091 high_output &= ~nSRST;
3093 /* initialize high port */
3094 buf[0] = 0x82; /* command "set data bits high byte" */
3095 buf[1] = high_output; /* value */
3096 buf[2] = high_direction; /* all outputs - xRST */
3097 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3099 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3101 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3102 return ERROR_JTAG_INIT_FAILED;
3108 static int cortino_jtag_init(void)
3111 uint32_t bytes_written;
3114 low_direction = 0x1b;
3116 /* initialize low byte for jtag */
3117 buf[0] = 0x80; /* command "set data bits low byte" */
3118 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3119 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3120 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3122 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3124 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3125 return ERROR_JTAG_INIT_FAILED;
3129 nTRSTnOE = 0x00; /* no output enable for nTRST */
3131 nSRSTnOE = 0x00; /* no output enable for nSRST */
3134 high_direction = 0x03;
3136 /* initialize high port */
3137 buf[0] = 0x82; /* command "set data bits high byte" */
3138 buf[1] = high_output;
3139 buf[2] = high_direction;
3140 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3142 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3144 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3145 return ERROR_JTAG_INIT_FAILED;
3151 static int lisa_l_init(void)
3154 uint32_t bytes_written;
3164 high_direction = 0x18;
3166 /* initialize high port */
3167 buf[0] = 0x82; /* command "set data bits high byte" */
3168 buf[1] = high_output;
3169 buf[2] = high_direction;
3170 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3172 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3174 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3175 return ERROR_JTAG_INIT_FAILED;
3178 return ftx232_dbus_write();
3181 static int flossjtag_init(void)
3184 uint32_t bytes_written;
3194 high_direction = 0x18;
3196 /* initialize high port */
3197 buf[0] = 0x82; /* command "set data bits high byte" */
3198 buf[1] = high_output;
3199 buf[2] = high_direction;
3200 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3202 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3204 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3205 return ERROR_JTAG_INIT_FAILED;
3208 return ftx232_dbus_write();
3211 static int xds100v2_init(void)
3214 uint32_t bytes_written;
3217 low_direction = 0x7B;
3219 /* initialize low byte for jtag */
3220 buf[0] = 0x80; /* command "set data bits low byte" */
3221 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3222 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
3223 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3225 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3227 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3228 return ERROR_JTAG_INIT_FAILED;
3232 nTRSTnOE = 0x0; /* not output enable for nTRST */
3233 nSRST = 0x00; /* TODO: SRST is not supported yet */
3234 nSRSTnOE = 0x00; /* no output enable for nSRST */
3237 high_direction = 0x59;
3239 /* initialize high port */
3240 buf[0] = 0x82; /* command "set data bits high byte" */
3241 buf[1] = high_output; /* value */
3242 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3243 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3245 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3247 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3248 return ERROR_JTAG_INIT_FAILED;
3252 high_direction = 0x59;
3254 /* initialize high port */
3255 buf[0] = 0x82; /* command "set data bits high byte" */
3256 buf[1] = high_output; /* value */
3257 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3258 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3260 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3262 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3263 return ERROR_JTAG_INIT_FAILED;
3269 static void olimex_jtag_blink(void)
3271 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3272 * ACBUS3 is bit 3 of the GPIOH port
3274 if (high_output & 0x08)
3276 /* set port pin high */
3277 high_output &= 0x07;
3281 /* set port pin low */
3282 high_output |= 0x08;
3286 buffer_write(high_output);
3287 buffer_write(high_direction);
3290 static void flyswatter_jtag_blink(void)
3293 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3295 high_output ^= 0x0c;
3298 buffer_write(high_output);
3299 buffer_write(high_direction);
3302 static void turtle_jtag_blink(void)
3305 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3307 if (high_output & 0x08)
3317 buffer_write(high_output);
3318 buffer_write(high_direction);
3321 static void lisa_l_blink(void)
3324 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3326 if (high_output & 0x10)
3336 buffer_write(high_output);
3337 buffer_write(high_direction);
3340 static void flossjtag_blink(void)
3343 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3345 if (high_output & 0x10)
3355 buffer_write(high_output);
3356 buffer_write(high_direction);
3359 static int ft2232_quit(void)
3361 #if BUILD_FT2232_FTD2XX == 1
3364 status = FT_Close(ftdih);
3365 #elif BUILD_FT2232_LIBFTDI == 1
3366 ftdi_usb_close(&ftdic);
3368 ftdi_deinit(&ftdic);
3371 free(ft2232_buffer);
3372 ft2232_buffer = NULL;
3377 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3383 ft2232_device_desc = strdup(CMD_ARGV[0]);
3384 cp = strchr(ft2232_device_desc, 0);
3385 /* under Win32, the FTD2XX driver appends an "A" to the end
3386 * of the description, this examines the given desc
3387 * and creates the 'missing' _A or non_A variable. */
3388 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3389 /* it was, so make this the "A" version. */
3390 ft2232_device_desc_A = ft2232_device_desc;
3391 /* and *CREATE* the non-A version. */
3392 strcpy(buf, ft2232_device_desc);
3393 cp = strchr(buf, 0);
3395 ft2232_device_desc = strdup(buf);
3397 /* <space > A not defined
3399 sprintf(buf, "%s A", ft2232_device_desc);
3400 ft2232_device_desc_A = strdup(buf);
3405 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3411 COMMAND_HANDLER(ft2232_handle_serial_command)
3415 ft2232_serial = strdup(CMD_ARGV[0]);
3419 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3425 COMMAND_HANDLER(ft2232_handle_layout_command)
3427 if (CMD_ARGC != 1) {
3428 LOG_ERROR("Need exactly one argument to ft2232_layout");
3433 LOG_ERROR("already specified ft2232_layout %s",
3435 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3440 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3441 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3447 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3451 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3453 if (CMD_ARGC > MAX_USB_IDS * 2)
3455 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3456 "(maximum is %d pairs)", MAX_USB_IDS);
3457 CMD_ARGC = MAX_USB_IDS * 2;
3459 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3461 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3463 return ERROR_COMMAND_SYNTAX_ERROR;
3464 /* remove the incomplete trailing id */
3469 for (i = 0; i < CMD_ARGC; i += 2)
3471 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3472 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3476 * Explicitly terminate, in case there are multiples instances of
3479 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3484 COMMAND_HANDLER(ft2232_handle_latency_command)
3488 ft2232_latency = atoi(CMD_ARGV[0]);
3492 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3498 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3502 /* 7 bits of either ones or zeros. */
3503 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3505 while (num_cycles > 0)
3507 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3508 * at most 7 bits per invocation. Here we invoke it potentially
3511 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3513 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3515 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3516 retval = ERROR_JTAG_QUEUE_FAILED;
3521 /* there are no state transitions in this code, so omit state tracking */
3523 /* command "Clock Data to TMS/CS Pin (no Read)" */
3527 buffer_write(bitcount_per_command - 1);
3529 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3534 num_cycles -= bitcount_per_command;
3540 /* ---------------------------------------------------------------------
3541 * Support for IceBear JTAG adapter from Section5:
3542 * http://section5.ch/icebear
3544 * Author: Sten, debian@sansys-electronic.com
3547 /* Icebear pin layout
3549 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3550 * GND GND | 4 3| n.c.
3551 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3552 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3553 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3554 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3555 * ADBUS2 TDO |14 13| GND GND
3557 * ADBUS0 O L TCK ACBUS0 GND
3558 * ADBUS1 O L TDI ACBUS1 GND
3559 * ADBUS2 I TDO ACBUS2 n.c.
3560 * ADBUS3 O H TMS ACBUS3 n.c.
3566 static int icebear_jtag_init(void) {
3568 uint32_t bytes_written;
3570 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3571 low_output = 0x08; /* high: TMS; low: TCK TDI */
3575 enum reset_types jtag_reset_config = jtag_get_reset_config();
3576 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3577 low_direction &= ~nTRST; /* nTRST high impedance */
3580 low_direction |= nTRST;
3581 low_output |= nTRST;
3584 low_direction |= nSRST;
3585 low_output |= nSRST;
3587 /* initialize low byte for jtag */
3588 buf[0] = 0x80; /* command "set data bits low byte" */
3589 buf[1] = low_output;
3590 buf[2] = low_direction;
3591 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3593 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3594 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3595 return ERROR_JTAG_INIT_FAILED;
3599 high_direction = 0x00;
3602 /* initialize high port */
3603 buf[0] = 0x82; /* command "set data bits high byte" */
3604 buf[1] = high_output; /* value */
3605 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3606 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3608 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3609 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3610 return ERROR_JTAG_INIT_FAILED;
3616 static void icebear_jtag_reset(int trst, int srst) {
3619 low_direction |= nTRST;
3620 low_output &= ~nTRST;
3622 else if (trst == 0) {
3623 enum reset_types jtag_reset_config = jtag_get_reset_config();
3624 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3625 low_direction &= ~nTRST;
3627 low_output |= nTRST;
3631 low_output &= ~nSRST;
3633 else if (srst == 0) {
3634 low_output |= nSRST;
3637 /* command "set data bits low byte" */
3639 buffer_write(low_output);
3640 buffer_write(low_direction);
3642 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3645 /* ---------------------------------------------------------------------
3646 * Support for Signalyzer H2 and Signalyzer H4
3647 * JTAG adapter from Xverve Technologies Inc.
3648 * http://www.signalyzer.com or http://www.xverve.com
3650 * Author: Oleg Seiljus, oleg@signalyzer.com
3652 static unsigned char signalyzer_h_side;
3653 static unsigned int signalyzer_h_adapter_type;
3655 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3657 #if BUILD_FT2232_FTD2XX == 1
3658 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3661 #define SIGNALYZER_COMMAND_ADDR 128
3662 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3664 #define SIGNALYZER_COMMAND_VERSION 0x41
3665 #define SIGNALYZER_COMMAND_RESET 0x42
3666 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3667 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3668 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3669 #define SIGNALYZER_COMMAND_LED_SET 0x53
3670 #define SIGNALYZER_COMMAND_ADC 0x54
3671 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3672 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3673 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3674 #define SIGNALYZER_COMMAND_I2C 0x58
3676 #define SIGNALYZER_CHAN_A 1
3677 #define SIGNALYZER_CHAN_B 2
3678 /* LEDS use channel C */
3679 #define SIGNALYZER_CHAN_C 4
3681 #define SIGNALYZER_LED_GREEN 1
3682 #define SIGNALYZER_LED_RED 2
3684 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3685 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3686 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3687 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3688 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3691 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3693 #if BUILD_FT2232_FTD2XX == 1
3694 return FT_WriteEE(ftdih, address, value);
3695 #elif BUILD_FT2232_LIBFTDI == 1
3700 #if BUILD_FT2232_FTD2XX == 1
3701 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3703 return FT_ReadEE(ftdih, address, value);
3707 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3708 int on_time_ms, int off_time_ms, unsigned char cycles)
3710 unsigned char on_time;
3711 unsigned char off_time;
3713 if (on_time_ms < 0xFFFF)
3714 on_time = (unsigned char)(on_time_ms / 62);
3718 off_time = (unsigned char)(off_time_ms / 62);
3720 #if BUILD_FT2232_FTD2XX == 1
3723 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3724 ((uint32_t)(channel << 8) | led))) != 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),
3732 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3734 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3735 return ERROR_JTAG_DEVICE_ERROR;
3738 if ((status = signalyzer_h_ctrl_write(
3739 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3740 ((uint32_t)cycles))) != FT_OK)
3742 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3743 return ERROR_JTAG_DEVICE_ERROR;
3746 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3747 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3749 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3750 return ERROR_JTAG_DEVICE_ERROR;
3754 #elif BUILD_FT2232_LIBFTDI == 1
3757 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3758 ((uint32_t)(channel << 8) | led))) < 0)
3760 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3761 ftdi_get_error_string(&ftdic));
3762 return ERROR_JTAG_DEVICE_ERROR;
3765 if ((retval = signalyzer_h_ctrl_write(
3766 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3767 ((uint32_t)(on_time << 8) | off_time))) < 0)
3769 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3770 ftdi_get_error_string(&ftdic));
3771 return ERROR_JTAG_DEVICE_ERROR;
3774 if ((retval = signalyzer_h_ctrl_write(
3775 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3776 (uint32_t)cycles)) < 0)
3778 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3779 ftdi_get_error_string(&ftdic));
3780 return ERROR_JTAG_DEVICE_ERROR;
3783 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3784 SIGNALYZER_COMMAND_LED_SET)) < 0)
3786 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3787 ftdi_get_error_string(&ftdic));
3788 return ERROR_JTAG_DEVICE_ERROR;
3795 static int signalyzer_h_init(void)
3797 #if BUILD_FT2232_FTD2XX == 1
3804 uint16_t read_buf[12] = { 0 };
3806 uint32_t bytes_written;
3808 /* turn on center green led */
3809 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3810 0xFFFF, 0x00, 0x00);
3812 /* determine what channel config wants to open
3813 * TODO: change me... current implementation is made to work
3814 * with openocd description parsing.
3816 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3820 signalyzer_h_side = *(end_of_desc - 1);
3821 if (signalyzer_h_side == 'B')
3822 signalyzer_h_side = SIGNALYZER_CHAN_B;
3824 signalyzer_h_side = SIGNALYZER_CHAN_A;
3828 LOG_ERROR("No Channel was specified");
3832 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3835 #if BUILD_FT2232_FTD2XX == 1
3836 /* read signalyzer versionining information */
3837 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3838 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3840 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3841 return ERROR_JTAG_DEVICE_ERROR;
3844 for (i = 0; i < 10; i++)
3846 if ((status = signalyzer_h_ctrl_read(
3847 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3848 &read_buf[i])) != FT_OK)
3850 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3852 return ERROR_JTAG_DEVICE_ERROR;
3856 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3857 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3858 read_buf[4], read_buf[5], read_buf[6]);
3860 /* set gpio register */
3861 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3862 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3864 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3865 return ERROR_JTAG_DEVICE_ERROR;
3868 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3871 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3872 return ERROR_JTAG_DEVICE_ERROR;
3875 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3876 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3878 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3879 return ERROR_JTAG_DEVICE_ERROR;
3882 /* read adapter type information */
3883 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3884 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3886 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3887 return ERROR_JTAG_DEVICE_ERROR;
3890 if ((status = signalyzer_h_ctrl_write(
3891 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3893 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3894 return ERROR_JTAG_DEVICE_ERROR;
3897 if ((status = signalyzer_h_ctrl_write(
3898 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3900 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3901 return ERROR_JTAG_DEVICE_ERROR;
3904 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3905 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3907 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3908 return ERROR_JTAG_DEVICE_ERROR;
3913 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3914 &read_buf[0])) != FT_OK)
3916 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3917 return ERROR_JTAG_DEVICE_ERROR;
3920 if (read_buf[0] != 0x0498)
3921 signalyzer_h_adapter_type = 0x0000;
3924 for (i = 0; i < 4; i++)
3926 if ((status = signalyzer_h_ctrl_read(
3927 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3928 &read_buf[i])) != FT_OK)
3930 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3932 return ERROR_JTAG_DEVICE_ERROR;
3936 signalyzer_h_adapter_type = read_buf[0];
3939 #elif BUILD_FT2232_LIBFTDI == 1
3940 /* currently libftdi does not allow reading individual eeprom
3941 * locations, therefore adapter type cannot be detected.
3942 * override with most common type
3944 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3947 enum reset_types jtag_reset_config = jtag_get_reset_config();
3949 /* ADAPTOR: EM_LT16_A */
3950 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3952 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3953 "detected. (HW: %2x).", (read_buf[1] >> 8));
3961 low_direction = 0x1b;
3964 high_direction = 0x0;
3966 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3968 low_direction &= ~nTRSTnOE; /* nTRST input */
3969 low_output &= ~nTRST; /* nTRST = 0 */
3973 low_direction |= nTRSTnOE; /* nTRST output */
3974 low_output |= nTRST; /* nTRST = 1 */
3977 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3979 low_direction |= nSRSTnOE; /* nSRST output */
3980 low_output |= nSRST; /* nSRST = 1 */
3984 low_direction &= ~nSRSTnOE; /* nSRST input */
3985 low_output &= ~nSRST; /* nSRST = 0 */
3988 #if BUILD_FT2232_FTD2XX == 1
3989 /* enable power to the module */
3990 if ((status = signalyzer_h_ctrl_write(
3991 SIGNALYZER_DATA_BUFFER_ADDR,
3992 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3995 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3997 return ERROR_JTAG_DEVICE_ERROR;
4000 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4001 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4003 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4005 return ERROR_JTAG_DEVICE_ERROR;
4008 /* set gpio mode register */
4009 if ((status = signalyzer_h_ctrl_write(
4010 SIGNALYZER_DATA_BUFFER_ADDR,
4011 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4013 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4015 return ERROR_JTAG_DEVICE_ERROR;
4018 if ((status = signalyzer_h_ctrl_write(
4019 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4022 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4024 return ERROR_JTAG_DEVICE_ERROR;
4027 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4028 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4030 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4032 return ERROR_JTAG_DEVICE_ERROR;
4035 /* set gpio register */
4036 if ((status = signalyzer_h_ctrl_write(
4037 SIGNALYZER_DATA_BUFFER_ADDR,
4038 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4040 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4042 return ERROR_JTAG_DEVICE_ERROR;
4045 if ((status = signalyzer_h_ctrl_write(
4046 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
4049 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4051 return ERROR_JTAG_DEVICE_ERROR;
4054 if ((status = signalyzer_h_ctrl_write(
4055 SIGNALYZER_COMMAND_ADDR,
4056 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4058 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4060 return ERROR_JTAG_DEVICE_ERROR;
4065 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4066 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4067 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4068 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4069 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4071 if (signalyzer_h_adapter_type
4072 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
4073 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
4074 "detected. (HW: %2x).", (read_buf[1] >> 8));
4075 else if (signalyzer_h_adapter_type
4076 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
4077 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
4078 "(ARM JTAG with PSU) detected. (HW: %2x).",
4079 (read_buf[1] >> 8));
4080 else if (signalyzer_h_adapter_type
4081 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
4082 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
4083 "detected. (HW: %2x).", (read_buf[1] >> 8));
4084 else if (signalyzer_h_adapter_type
4085 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
4086 LOG_INFO("Signalyzer: EM-JTAG-P "
4087 "(Generic JTAG with PSU) detected. (HW: %2x).",
4088 (read_buf[1] >> 8));
4096 low_direction = 0x1b;
4099 high_direction = 0x1f;
4101 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4103 high_output |= nTRSTnOE;
4104 high_output &= ~nTRST;
4108 high_output &= ~nTRSTnOE;
4109 high_output |= nTRST;
4112 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4114 high_output &= ~nSRSTnOE;
4115 high_output |= nSRST;
4119 high_output |= nSRSTnOE;
4120 high_output &= ~nSRST;
4123 #if BUILD_FT2232_FTD2XX == 1
4124 /* enable power to the module */
4125 if ((status = signalyzer_h_ctrl_write(
4126 SIGNALYZER_DATA_BUFFER_ADDR,
4127 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4130 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4132 return ERROR_JTAG_DEVICE_ERROR;
4135 if ((status = signalyzer_h_ctrl_write(
4136 SIGNALYZER_COMMAND_ADDR,
4137 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4139 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4141 return ERROR_JTAG_DEVICE_ERROR;
4144 /* set gpio mode register (IO_16 and IO_17 set as analog
4145 * inputs, other is gpio)
4147 if ((status = signalyzer_h_ctrl_write(
4148 SIGNALYZER_DATA_BUFFER_ADDR,
4149 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4151 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4153 return ERROR_JTAG_DEVICE_ERROR;
4156 if ((status = signalyzer_h_ctrl_write(
4157 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4160 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4162 return ERROR_JTAG_DEVICE_ERROR;
4165 if ((status = signalyzer_h_ctrl_write(
4166 SIGNALYZER_COMMAND_ADDR,
4167 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4169 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4171 return ERROR_JTAG_DEVICE_ERROR;
4174 /* set gpio register (all inputs, for -P modules,
4175 * PSU will be turned off)
4177 if ((status = signalyzer_h_ctrl_write(
4178 SIGNALYZER_DATA_BUFFER_ADDR,
4179 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4181 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4183 return ERROR_JTAG_DEVICE_ERROR;
4186 if ((status = signalyzer_h_ctrl_write(
4187 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4190 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4192 return ERROR_JTAG_DEVICE_ERROR;
4195 if ((status = signalyzer_h_ctrl_write(
4196 SIGNALYZER_COMMAND_ADDR,
4197 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4199 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4201 return ERROR_JTAG_DEVICE_ERROR;
4206 else if (signalyzer_h_adapter_type == 0x0000)
4208 LOG_INFO("Signalyzer: No external modules were detected.");
4216 low_direction = 0x1b;
4219 high_direction = 0x0;
4221 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4223 low_direction &= ~nTRSTnOE; /* nTRST input */
4224 low_output &= ~nTRST; /* nTRST = 0 */
4228 low_direction |= nTRSTnOE; /* nTRST output */
4229 low_output |= nTRST; /* nTRST = 1 */
4232 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4234 low_direction |= nSRSTnOE; /* nSRST output */
4235 low_output |= nSRST; /* nSRST = 1 */
4239 low_direction &= ~nSRSTnOE; /* nSRST input */
4240 low_output &= ~nSRST; /* nSRST = 0 */
4245 LOG_ERROR("Unknown module type is detected: %.4x",
4246 signalyzer_h_adapter_type);
4247 return ERROR_JTAG_DEVICE_ERROR;
4250 /* initialize low byte of controller for jtag operation */
4252 buf[1] = low_output;
4253 buf[2] = low_direction;
4255 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4257 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4258 return ERROR_JTAG_INIT_FAILED;
4261 #if BUILD_FT2232_FTD2XX == 1
4262 if (ftdi_device == FT_DEVICE_2232H)
4264 /* initialize high byte of controller for jtag operation */
4266 buf[1] = high_output;
4267 buf[2] = high_direction;
4269 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4271 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4272 return ERROR_JTAG_INIT_FAILED;
4275 #elif BUILD_FT2232_LIBFTDI == 1
4276 if (ftdi_device == TYPE_2232H)
4278 /* initialize high byte of controller for jtag operation */
4280 buf[1] = high_output;
4281 buf[2] = high_direction;
4283 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4285 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4286 return ERROR_JTAG_INIT_FAILED;
4293 static void signalyzer_h_reset(int trst, int srst)
4295 enum reset_types jtag_reset_config = jtag_get_reset_config();
4297 /* ADAPTOR: EM_LT16_A */
4298 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4302 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4303 /* switch to output pin (output is low) */
4304 low_direction |= nTRSTnOE;
4306 /* switch output low */
4307 low_output &= ~nTRST;
4311 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4312 /* switch to input pin (high-Z + internal
4313 * and external pullup) */
4314 low_direction &= ~nTRSTnOE;
4316 /* switch output high */
4317 low_output |= nTRST;
4322 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4323 /* switch output low */
4324 low_output &= ~nSRST;
4326 /* switch to output pin (output is low) */
4327 low_direction |= nSRSTnOE;
4331 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4332 /* switch output high */
4333 low_output |= nSRST;
4335 /* switch to input pin (high-Z) */
4336 low_direction &= ~nSRSTnOE;
4339 /* command "set data bits low byte" */
4341 buffer_write(low_output);
4342 buffer_write(low_direction);
4343 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4344 "low_direction: 0x%2.2x",
4345 trst, srst, low_output, low_direction);
4347 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4348 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4349 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4350 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4351 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4355 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4356 high_output &= ~nTRSTnOE;
4358 high_output &= ~nTRST;
4362 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4363 high_output |= nTRSTnOE;
4365 high_output |= nTRST;
4370 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4371 high_output &= ~nSRST;
4373 high_output &= ~nSRSTnOE;
4377 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4378 high_output |= nSRST;
4380 high_output |= nSRSTnOE;
4383 /* command "set data bits high byte" */
4385 buffer_write(high_output);
4386 buffer_write(high_direction);
4387 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4388 "high_direction: 0x%2.2x",
4389 trst, srst, high_output, high_direction);
4391 else if (signalyzer_h_adapter_type == 0x0000)
4395 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4396 /* switch to output pin (output is low) */
4397 low_direction |= nTRSTnOE;
4399 /* switch output low */
4400 low_output &= ~nTRST;
4404 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4405 /* switch to input pin (high-Z + internal
4406 * and external pullup) */
4407 low_direction &= ~nTRSTnOE;
4409 /* switch output high */
4410 low_output |= nTRST;
4415 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4416 /* switch output low */
4417 low_output &= ~nSRST;
4419 /* switch to output pin (output is low) */
4420 low_direction |= nSRSTnOE;
4424 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4425 /* switch output high */
4426 low_output |= nSRST;
4428 /* switch to input pin (high-Z) */
4429 low_direction &= ~nSRSTnOE;
4432 /* command "set data bits low byte" */
4434 buffer_write(low_output);
4435 buffer_write(low_direction);
4436 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4437 "low_direction: 0x%2.2x",
4438 trst, srst, low_output, low_direction);
4442 static void signalyzer_h_blink(void)
4444 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4447 /********************************************************************
4448 * Support for KT-LINK
4449 * JTAG adapter from KRISTECH
4450 * http://www.kristech.eu
4451 *******************************************************************/
4452 static int ktlink_init(void)
4455 uint32_t bytes_written;
4456 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4458 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4459 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4461 // initialize low port
4462 buf[0] = 0x80; // command "set data bits low byte"
4463 buf[1] = low_output;
4464 buf[2] = low_direction;
4465 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4467 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4469 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4470 return ERROR_JTAG_INIT_FAILED;
4478 high_output = 0x80; // turn LED on
4479 high_direction = 0xFF; // all outputs
4481 enum reset_types jtag_reset_config = jtag_get_reset_config();
4483 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4484 high_output |= nTRSTnOE;
4485 high_output &= ~nTRST;
4487 high_output &= ~nTRSTnOE;
4488 high_output |= nTRST;
4491 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4492 high_output &= ~nSRSTnOE;
4493 high_output |= nSRST;
4495 high_output |= nSRSTnOE;
4496 high_output &= ~nSRST;
4499 // initialize high port
4500 buf[0] = 0x82; // command "set data bits high byte"
4501 buf[1] = high_output; // value
4502 buf[2] = high_direction;
4503 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4505 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4507 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4508 return ERROR_JTAG_INIT_FAILED;
4514 static void ktlink_reset(int trst, int srst)
4516 enum reset_types jtag_reset_config = jtag_get_reset_config();
4519 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4520 high_output &= ~nTRSTnOE;
4522 high_output &= ~nTRST;
4523 } else if (trst == 0) {
4524 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4525 high_output |= nTRSTnOE;
4527 high_output |= nTRST;
4531 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4532 high_output &= ~nSRST;
4534 high_output &= ~nSRSTnOE;
4535 } else if (srst == 0) {
4536 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4537 high_output |= nSRST;
4539 high_output |= nSRSTnOE;
4542 buffer_write(0x82); // command "set data bits high byte"
4543 buffer_write(high_output);
4544 buffer_write(high_direction);
4545 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4548 static void ktlink_blink(void)
4550 /* LED connected to ACBUS7 */
4551 if (high_output & 0x80)
4552 high_output &= 0x7F;
4554 high_output |= 0x80;
4556 buffer_write(0x82); // command "set data bits high byte"
4557 buffer_write(high_output);
4558 buffer_write(high_direction);
4561 static const struct command_registration ft2232_command_handlers[] = {
4563 .name = "ft2232_device_desc",
4564 .handler = &ft2232_handle_device_desc_command,
4565 .mode = COMMAND_CONFIG,
4566 .help = "set the USB device description of the FTDI FT2232 device",
4567 .usage = "description_string",
4570 .name = "ft2232_serial",
4571 .handler = &ft2232_handle_serial_command,
4572 .mode = COMMAND_CONFIG,
4573 .help = "set the serial number of the FTDI FT2232 device",
4574 .usage = "serial_string",
4577 .name = "ft2232_layout",
4578 .handler = &ft2232_handle_layout_command,
4579 .mode = COMMAND_CONFIG,
4580 .help = "set the layout of the FT2232 GPIO signals used "
4581 "to control output-enables and reset signals",
4582 .usage = "layout_name",
4585 .name = "ft2232_vid_pid",
4586 .handler = &ft2232_handle_vid_pid_command,
4587 .mode = COMMAND_CONFIG,
4588 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4589 .usage = "(vid pid)* ",
4592 .name = "ft2232_latency",
4593 .handler = &ft2232_handle_latency_command,
4594 .mode = COMMAND_CONFIG,
4595 .help = "set the FT2232 latency timer to a new value",
4598 COMMAND_REGISTRATION_DONE
4601 struct jtag_interface ft2232_interface = {
4603 .supported = DEBUG_CAP_TMS_SEQ,
4604 .commands = ft2232_command_handlers,
4605 .transports = jtag_only,
4607 .init = ft2232_init,
4608 .quit = ft2232_quit,
4609 .speed = ft2232_speed,
4610 .speed_div = ft2232_speed_div,
4612 .execute_queue = ft2232_execute_queue,