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 minimodule_init(void);
181 static int turtle_init(void);
182 static int comstick_init(void);
183 static int stm32stick_init(void);
184 static int axm0432_jtag_init(void);
185 static int sheevaplug_init(void);
186 static int icebear_jtag_init(void);
187 static int cortino_jtag_init(void);
188 static int signalyzer_init(void);
189 static int signalyzer_h_init(void);
190 static int ktlink_init(void);
191 static int redbee_init(void);
192 static int lisa_l_init(void);
193 static int flossjtag_init(void);
194 static int xds100v2_init(void);
196 /* reset procedures for supported layouts */
197 static void ftx23_reset(int trst, int srst);
198 static void jtagkey_reset(int trst, int srst);
199 static void olimex_jtag_reset(int trst, int srst);
200 static void flyswatter_reset(int trst, int srst);
201 static void minimodule_reset(int trst, int srst);
202 static void turtle_reset(int trst, int srst);
203 static void comstick_reset(int trst, int srst);
204 static void stm32stick_reset(int trst, int srst);
205 static void axm0432_jtag_reset(int trst, int srst);
206 static void sheevaplug_reset(int trst, int srst);
207 static void icebear_jtag_reset(int trst, int srst);
208 static void signalyzer_h_reset(int trst, int srst);
209 static void ktlink_reset(int trst, int srst);
210 static void redbee_reset(int trst, int srst);
211 static void xds100v2_reset(int trst, int srst);
213 /* blink procedures for layouts that support a blinking led */
214 static void olimex_jtag_blink(void);
215 static void flyswatter_jtag_blink(void);
216 static void turtle_jtag_blink(void);
217 static void signalyzer_h_blink(void);
218 static void ktlink_blink(void);
219 static void lisa_l_blink(void);
220 static void flossjtag_blink(void);
222 /* common transport support options */
224 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
226 static const struct ft2232_layout ft2232_layouts[] =
229 .init = usbjtag_init,
230 .reset = ftx23_reset,
233 .init = jtagkey_init,
234 .reset = jtagkey_reset,
236 { .name = "jtagkey_prototype_v1",
237 .init = jtagkey_init,
238 .reset = jtagkey_reset,
240 { .name = "oocdlink",
241 .init = jtagkey_init,
242 .reset = jtagkey_reset,
244 { .name = "signalyzer",
245 .init = signalyzer_init,
246 .reset = ftx23_reset,
248 { .name = "evb_lm3s811",
249 .init = lm3s811_jtag_init,
250 .reset = ftx23_reset,
252 { .name = "luminary_icdi",
253 .init = icdi_jtag_init,
254 .reset = ftx23_reset,
256 { .name = "olimex-jtag",
257 .init = olimex_jtag_init,
258 .reset = olimex_jtag_reset,
259 .blink = olimex_jtag_blink
261 { .name = "flyswatter",
262 .init = flyswatter_init,
263 .reset = flyswatter_reset,
264 .blink = flyswatter_jtag_blink
266 { .name = "minimodule",
267 .init = minimodule_init,
268 .reset = minimodule_reset,
270 { .name = "turtelizer2",
272 .reset = turtle_reset,
273 .blink = turtle_jtag_blink
275 { .name = "comstick",
276 .init = comstick_init,
277 .reset = comstick_reset,
279 { .name = "stm32stick",
280 .init = stm32stick_init,
281 .reset = stm32stick_reset,
283 { .name = "axm0432_jtag",
284 .init = axm0432_jtag_init,
285 .reset = axm0432_jtag_reset,
287 { .name = "sheevaplug",
288 .init = sheevaplug_init,
289 .reset = sheevaplug_reset,
292 .init = icebear_jtag_init,
293 .reset = icebear_jtag_reset,
296 .init = cortino_jtag_init,
297 .reset = comstick_reset,
299 { .name = "signalyzer-h",
300 .init = signalyzer_h_init,
301 .reset = signalyzer_h_reset,
302 .blink = signalyzer_h_blink
306 .reset = ktlink_reset,
307 .blink = ktlink_blink
309 { .name = "redbee-econotag",
311 .reset = redbee_reset,
313 { .name = "redbee-usb",
315 .reset = redbee_reset,
316 .channel = INTERFACE_B,
320 .reset = ftx23_reset,
321 .blink = lisa_l_blink,
322 .channel = INTERFACE_B,
324 { .name = "flossjtag",
325 .init = flossjtag_init,
326 .reset = ftx23_reset,
327 .blink = flossjtag_blink,
329 { .name = "xds100v2",
330 .init = xds100v2_init,
331 .reset = xds100v2_reset,
333 { .name = NULL, /* END OF TABLE */ },
336 /* bitmask used to drive nTRST; usually a GPIOLx signal */
337 static uint8_t nTRST;
338 static uint8_t nTRSTnOE;
339 /* bitmask used to drive nSRST; usually a GPIOLx signal */
340 static uint8_t nSRST;
341 static uint8_t nSRSTnOE;
343 /** the layout being used with this debug session */
344 static const struct ft2232_layout *layout;
346 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
347 static uint8_t low_output = 0x0;
349 /* note that direction bit == 1 means that signal is an output */
351 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
352 static uint8_t low_direction = 0x0;
353 /** default value bitmask for CBUS GPIOH(0..4) */
354 static uint8_t high_output = 0x0;
355 /** default direction bitmask for CBUS GPIOH(0..4) */
356 static uint8_t high_direction = 0x0;
358 #if BUILD_FT2232_FTD2XX == 1
359 static FT_HANDLE ftdih = NULL;
360 static FT_DEVICE ftdi_device = 0;
361 #elif BUILD_FT2232_LIBFTDI == 1
362 static struct ftdi_context ftdic;
363 static enum ftdi_chip_type ftdi_device;
366 static struct jtag_command* first_unsent; /* next command that has to be sent */
367 static int require_send;
369 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
371 "There is a significant difference between libftdi and libftd2xx. The latter
372 one allows to schedule up to 64*64 bytes of result data while libftdi fails
373 with more than 4*64. As a consequence, the FT2232 driver is forced to
374 perform around 16x more USB transactions for long command streams with TDO
375 capture when running with libftdi."
378 #define FT2232_BUFFER_SIZE 131072
379 a comment would have been nice.
382 #if BUILD_FT2232_FTD2XX == 1
383 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
385 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
388 #define FT2232_BUFFER_SIZE 131072
390 static uint8_t* ft2232_buffer = NULL;
391 static int ft2232_buffer_size = 0;
392 static int ft2232_read_pointer = 0;
393 static int ft2232_expect_read = 0;
396 * Function buffer_write
397 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
398 * @param val is the byte to send.
400 static inline void buffer_write(uint8_t val)
402 assert(ft2232_buffer);
403 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
404 ft2232_buffer[ft2232_buffer_size++] = val;
408 * Function buffer_read
409 * returns a byte from the byte buffer.
411 static inline uint8_t buffer_read(void)
413 assert(ft2232_buffer);
414 assert(ft2232_read_pointer < ft2232_buffer_size);
415 return ft2232_buffer[ft2232_read_pointer++];
419 * Clocks out \a bit_count bits on the TMS line, starting with the least
420 * significant bit of tms_bits and progressing to more significant bits.
421 * Rigorous state transition logging is done here via tap_set_state().
423 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
424 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
425 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
426 * is often used for this, 0x4b.
428 * @param tms_bits Holds the sequence of bits to send.
429 * @param tms_count Tells how many bits in the sequence.
430 * @param tdi_bit A single bit to pass on to TDI before the first TCK
431 * cycle and held static for the duration of TMS clocking.
433 * See the MPSSE spec referenced above.
435 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
439 int tms_ndx; /* bit index into tms_byte */
441 assert(tms_count > 0);
443 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
444 mpsse_cmd, tms_bits, tms_count);
446 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
448 bool bit = tms_bits & 1;
451 tms_byte |= (1 << tms_ndx);
453 /* always do state transitions in public view */
454 tap_set_state(tap_state_transition(tap_get_state(), bit));
456 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
461 if (tms_ndx == 7 || i == tms_count-1)
463 buffer_write(mpsse_cmd);
464 buffer_write(tms_ndx - 1);
466 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
467 TMS/CS and is held static for the duration of TMS/CS clocking.
469 buffer_write(tms_byte | (tdi_bit << 7));
475 * Function get_tms_buffer_requirements
476 * returns what clock_tms() will consume if called with
479 static inline int get_tms_buffer_requirements(int bit_count)
481 return ((bit_count + 6)/7) * 3;
485 * Function move_to_state
486 * moves the TAP controller from the current state to a
487 * \a goal_state through a path given by tap_get_tms_path(). State transition
488 * logging is performed by delegation to clock_tms().
490 * @param goal_state is the destination state for the move.
492 static void move_to_state(tap_state_t goal_state)
494 tap_state_t start_state = tap_get_state();
496 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
497 lookup of the required TMS pattern to move to this state from the
501 /* do the 2 lookups */
502 int tms_bits = tap_get_tms_path(start_state, goal_state);
503 int tms_count = tap_get_tms_path_len(start_state, goal_state);
505 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
507 clock_tms(0x4b, tms_bits, tms_count, 0);
510 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
512 #if BUILD_FT2232_FTD2XX == 1
514 DWORD dw_bytes_written = 0;
515 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
517 *bytes_written = dw_bytes_written;
518 LOG_ERROR("FT_Write returned: %lu", status);
519 return ERROR_JTAG_DEVICE_ERROR;
523 *bytes_written = dw_bytes_written;
525 #elif BUILD_FT2232_LIBFTDI == 1
527 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
530 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
531 return ERROR_JTAG_DEVICE_ERROR;
535 *bytes_written = retval;
539 if (*bytes_written != (uint32_t)size)
541 return ERROR_JTAG_DEVICE_ERROR;
547 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
549 #if BUILD_FT2232_FTD2XX == 1
555 while ((*bytes_read < size) && timeout--)
557 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
558 *bytes_read, &dw_bytes_read)) != FT_OK)
561 LOG_ERROR("FT_Read returned: %lu", status);
562 return ERROR_JTAG_DEVICE_ERROR;
564 *bytes_read += dw_bytes_read;
567 #elif BUILD_FT2232_LIBFTDI == 1
569 int timeout = LIBFTDI_READ_RETRY_COUNT;
572 while ((*bytes_read < size) && timeout--)
574 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
577 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
578 return ERROR_JTAG_DEVICE_ERROR;
580 *bytes_read += retval;
585 if (*bytes_read < size)
587 LOG_ERROR("couldn't read enough bytes from "
588 "FT2232 device (%i < %i)",
589 (unsigned)*bytes_read,
591 return ERROR_JTAG_DEVICE_ERROR;
597 static bool ft2232_device_is_highspeed(void)
599 #if BUILD_FT2232_FTD2XX == 1
600 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
601 #elif BUILD_FT2232_LIBFTDI == 1
602 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
607 * Commands that only apply to the FT2232H and FT4232H devices.
608 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
609 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
612 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
614 uint8_t buf = enable ? 0x96 : 0x97;
615 LOG_DEBUG("%2.2x", buf);
617 uint32_t bytes_written;
620 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
622 LOG_ERROR("couldn't write command to %s adaptive clocking"
623 , enable ? "enable" : "disable");
631 * Enable/disable the clk divide by 5 of the 60MHz master clock.
632 * This result in a JTAG clock speed range of 91.553Hz-6MHz
633 * respective 457.763Hz-30MHz.
635 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
637 uint32_t bytes_written;
638 uint8_t buf = enable ? 0x8b : 0x8a;
640 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
642 LOG_ERROR("couldn't write command to %s clk divide by 5"
643 , enable ? "enable" : "disable");
644 return ERROR_JTAG_INIT_FAILED;
646 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
647 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
652 static int ft2232_speed(int speed)
656 uint32_t bytes_written;
659 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
660 if (ft2232_device_is_highspeed())
661 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
662 else if (enable_adaptive_clocking)
664 LOG_ERROR("ft2232 device %lu does not support RTCK"
665 , (long unsigned int)ftdi_device);
669 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
672 buf[0] = 0x86; /* command "set divisor" */
673 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
674 buf[2] = (speed >> 8) & 0xff; /* valueH */
676 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
677 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
679 LOG_ERROR("couldn't set FT2232 TCK speed");
686 static int ft2232_speed_div(int speed, int* khz)
688 /* Take a look in the FT2232 manual,
689 * AN2232C-01 Command Processor for
690 * MPSSE and MCU Host Bus. Chapter 3.8 */
692 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
697 static int ft2232_khz(int khz, int* jtag_speed)
701 if (ft2232_device_is_highspeed())
703 *jtag_speed = RTCK_SPEED;
708 LOG_DEBUG("RCLK not supported");
713 /* Take a look in the FT2232 manual,
714 * AN2232C-01 Command Processor for
715 * MPSSE and MCU Host Bus. Chapter 3.8
717 * We will calc here with a multiplier
718 * of 10 for better rounding later. */
720 /* Calc speed, (ft2232_max_tck / khz) - 1 */
721 /* Use 65000 for better rounding */
722 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
724 /* Add 0.9 for rounding */
727 /* Calc real speed */
728 *jtag_speed = *jtag_speed / 10;
730 /* Check if speed is greater than 0 */
736 /* Check max value */
737 if (*jtag_speed > 0xFFFF)
739 *jtag_speed = 0xFFFF;
745 static void ft2232_end_state(tap_state_t state)
747 if (tap_is_state_stable(state))
748 tap_set_end_state(state);
751 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
756 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
758 int num_bytes = (scan_size + 7) / 8;
759 int bits_left = scan_size;
762 while (num_bytes-- > 1)
764 buffer[cur_byte++] = buffer_read();
768 buffer[cur_byte] = 0x0;
770 /* There is one more partial byte left from the clock data in/out instructions */
773 buffer[cur_byte] = buffer_read() >> 1;
775 /* 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 */
776 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
779 static void ft2232_debug_dump_buffer(void)
785 for (i = 0; i < ft2232_buffer_size; i++)
787 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
790 LOG_DEBUG("%s", line);
796 LOG_DEBUG("%s", line);
799 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
801 struct jtag_command* cmd;
806 uint32_t bytes_written = 0;
807 uint32_t bytes_read = 0;
809 #ifdef _DEBUG_USB_IO_
810 struct timeval start, inter, inter2, end;
811 struct timeval d_inter, d_inter2, d_end;
814 #ifdef _DEBUG_USB_COMMS_
815 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
816 ft2232_debug_dump_buffer();
819 #ifdef _DEBUG_USB_IO_
820 gettimeofday(&start, NULL);
823 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
825 LOG_ERROR("couldn't write MPSSE commands to FT2232");
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&inter, NULL);
833 if (ft2232_expect_read)
835 /* FIXME this "timeout" is never changed ... */
836 int timeout = LIBFTDI_READ_RETRY_COUNT;
837 ft2232_buffer_size = 0;
839 #ifdef _DEBUG_USB_IO_
840 gettimeofday(&inter2, NULL);
843 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
845 LOG_ERROR("couldn't read from FT2232");
849 #ifdef _DEBUG_USB_IO_
850 gettimeofday(&end, NULL);
852 timeval_subtract(&d_inter, &inter, &start);
853 timeval_subtract(&d_inter2, &inter2, &start);
854 timeval_subtract(&d_end, &end, &start);
856 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
857 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
858 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
859 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
862 ft2232_buffer_size = bytes_read;
864 if (ft2232_expect_read != ft2232_buffer_size)
866 LOG_ERROR("ft2232_expect_read (%i) != "
867 "ft2232_buffer_size (%i) "
871 LIBFTDI_READ_RETRY_COUNT - timeout);
872 ft2232_debug_dump_buffer();
877 #ifdef _DEBUG_USB_COMMS_
878 LOG_DEBUG("read buffer (%i retries): %i bytes",
879 LIBFTDI_READ_RETRY_COUNT - timeout,
881 ft2232_debug_dump_buffer();
885 ft2232_expect_read = 0;
886 ft2232_read_pointer = 0;
888 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
889 * that wasn't handled by a caller-provided error handler
899 type = jtag_scan_type(cmd->cmd.scan);
900 if (type != SCAN_OUT)
902 scan_size = jtag_scan_size(cmd->cmd.scan);
903 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
904 ft2232_read_scan(type, buffer, scan_size);
905 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
906 retval = ERROR_JTAG_QUEUE_FAILED;
918 ft2232_buffer_size = 0;
924 * Function ft2232_add_pathmove
925 * moves the TAP controller from the current state to a new state through the
926 * given path, where path is an array of tap_state_t's.
928 * @param path is an array of tap_stat_t which gives the states to traverse through
929 * ending with the last state at path[num_states-1]
930 * @param num_states is the count of state steps to move through
932 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
936 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
940 /* this loop verifies that the path is legal and logs each state in the path */
943 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
945 int num_states_batch = num_states > 7 ? 7 : num_states;
947 /* command "Clock Data to TMS/CS Pin (no Read)" */
950 /* number of states remaining */
951 buffer_write(num_states_batch - 1);
953 while (num_states_batch--) {
954 /* either TMS=0 or TMS=1 must work ... */
955 if (tap_state_transition(tap_get_state(), false)
956 == path[state_count])
957 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
958 else if (tap_state_transition(tap_get_state(), true)
959 == path[state_count])
960 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
962 /* ... or else the caller goofed BADLY */
964 LOG_ERROR("BUG: %s -> %s isn't a valid "
965 "TAP state transition",
966 tap_state_name(tap_get_state()),
967 tap_state_name(path[state_count]));
971 tap_set_state(path[state_count]);
976 buffer_write(tms_byte);
978 tap_set_end_state(tap_get_state());
981 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
983 int num_bytes = (scan_size + 7) / 8;
984 int bits_left = scan_size;
990 if (tap_get_state() != TAP_DRSHIFT)
992 move_to_state(TAP_DRSHIFT);
997 if (tap_get_state() != TAP_IRSHIFT)
999 move_to_state(TAP_IRSHIFT);
1003 /* add command for complete bytes */
1004 while (num_bytes > 1)
1007 if (type == SCAN_IO)
1009 /* Clock Data Bytes In and Out LSB First */
1011 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1013 else if (type == SCAN_OUT)
1015 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1017 /* LOG_DEBUG("added TDI bytes (o)"); */
1019 else if (type == SCAN_IN)
1021 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1023 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1026 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1027 num_bytes -= thisrun_bytes;
1029 buffer_write((uint8_t) (thisrun_bytes - 1));
1030 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1032 if (type != SCAN_IN)
1034 /* add complete bytes */
1035 while (thisrun_bytes-- > 0)
1037 buffer_write(buffer[cur_byte++]);
1041 else /* (type == SCAN_IN) */
1043 bits_left -= 8 * (thisrun_bytes);
1047 /* the most signifcant bit is scanned during TAP movement */
1048 if (type != SCAN_IN)
1049 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1053 /* process remaining bits but the last one */
1056 if (type == SCAN_IO)
1058 /* Clock Data Bits In and Out LSB First */
1060 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1062 else if (type == SCAN_OUT)
1064 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1066 /* LOG_DEBUG("added TDI bits (o)"); */
1068 else if (type == SCAN_IN)
1070 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1072 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1075 buffer_write(bits_left - 2);
1076 if (type != SCAN_IN)
1077 buffer_write(buffer[cur_byte]);
1080 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1081 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1083 if (type == SCAN_IO)
1085 /* Clock Data Bits In and Out LSB First */
1087 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1089 else if (type == SCAN_OUT)
1091 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1093 /* LOG_DEBUG("added TDI bits (o)"); */
1095 else if (type == SCAN_IN)
1097 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1099 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1102 buffer_write(last_bit);
1110 /* move from Shift-IR/DR to end state */
1111 if (type != SCAN_OUT)
1113 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1114 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1117 /* Clock Data to TMS/CS Pin with Read */
1122 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1123 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1124 /* Clock Data to TMS/CS Pin (no Read) */
1128 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1129 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1132 if (tap_get_state() != tap_get_end_state())
1134 move_to_state(tap_get_end_state());
1138 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1140 int num_bytes = (scan_size + 7) / 8;
1141 int bits_left = scan_size;
1144 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1145 uint8_t* receive_pointer = receive_buffer;
1146 uint32_t bytes_written;
1147 uint32_t bytes_read;
1149 int thisrun_read = 0;
1153 LOG_ERROR("BUG: large IR scans are not supported");
1157 if (tap_get_state() != TAP_DRSHIFT)
1159 move_to_state(TAP_DRSHIFT);
1162 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1164 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1167 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1168 ft2232_buffer_size, (int)bytes_written);
1169 ft2232_buffer_size = 0;
1171 /* add command for complete bytes */
1172 while (num_bytes > 1)
1176 if (type == SCAN_IO)
1178 /* Clock Data Bytes In and Out LSB First */
1180 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1182 else if (type == SCAN_OUT)
1184 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1186 /* LOG_DEBUG("added TDI bytes (o)"); */
1188 else if (type == SCAN_IN)
1190 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1192 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1195 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1196 thisrun_read = thisrun_bytes;
1197 num_bytes -= thisrun_bytes;
1198 buffer_write((uint8_t) (thisrun_bytes - 1));
1199 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1201 if (type != SCAN_IN)
1203 /* add complete bytes */
1204 while (thisrun_bytes-- > 0)
1206 buffer_write(buffer[cur_byte]);
1211 else /* (type == SCAN_IN) */
1213 bits_left -= 8 * (thisrun_bytes);
1216 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1218 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1221 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1223 (int)bytes_written);
1224 ft2232_buffer_size = 0;
1226 if (type != SCAN_OUT)
1228 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1230 LOG_ERROR("couldn't read from FT2232");
1233 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1236 receive_pointer += bytes_read;
1242 /* the most signifcant bit is scanned during TAP movement */
1243 if (type != SCAN_IN)
1244 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1248 /* process remaining bits but the last one */
1251 if (type == SCAN_IO)
1253 /* Clock Data Bits In and Out LSB First */
1255 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1257 else if (type == SCAN_OUT)
1259 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1261 /* LOG_DEBUG("added TDI bits (o)"); */
1263 else if (type == SCAN_IN)
1265 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1267 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1269 buffer_write(bits_left - 2);
1270 if (type != SCAN_IN)
1271 buffer_write(buffer[cur_byte]);
1273 if (type != SCAN_OUT)
1277 if (tap_get_end_state() == TAP_DRSHIFT)
1279 if (type == SCAN_IO)
1281 /* Clock Data Bits In and Out LSB First */
1283 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1285 else if (type == SCAN_OUT)
1287 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1289 /* LOG_DEBUG("added TDI bits (o)"); */
1291 else if (type == SCAN_IN)
1293 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1295 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1298 buffer_write(last_bit);
1302 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1303 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1306 /* move from Shift-IR/DR to end state */
1307 if (type != SCAN_OUT)
1309 /* Clock Data to TMS/CS Pin with Read */
1311 /* LOG_DEBUG("added TMS scan (read)"); */
1315 /* Clock Data to TMS/CS Pin (no Read) */
1317 /* LOG_DEBUG("added TMS scan (no read)"); */
1320 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1321 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1324 if (type != SCAN_OUT)
1327 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1329 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1332 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1334 (int)bytes_written);
1335 ft2232_buffer_size = 0;
1337 if (type != SCAN_OUT)
1339 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1341 LOG_ERROR("couldn't read from FT2232");
1344 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1347 receive_pointer += bytes_read;
1353 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1355 int predicted_size = 3;
1356 int num_bytes = (scan_size - 1) / 8;
1358 if (tap_get_state() != TAP_DRSHIFT)
1359 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1361 if (type == SCAN_IN) /* only from device to host */
1363 /* complete bytes */
1364 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1366 /* remaining bits - 1 (up to 7) */
1367 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1369 else /* host to device, or bidirectional */
1371 /* complete bytes */
1372 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1374 /* remaining bits -1 (up to 7) */
1375 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1378 return predicted_size;
1381 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1383 int predicted_size = 0;
1385 if (type != SCAN_OUT)
1387 /* complete bytes */
1388 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1390 /* remaining bits - 1 */
1391 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1393 /* last bit (from TMS scan) */
1394 predicted_size += 1;
1397 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1399 return predicted_size;
1402 /* semi-generic FT2232/FT4232 reset code */
1403 static void ftx23_reset(int trst, int srst)
1405 enum reset_types jtag_reset_config = jtag_get_reset_config();
1408 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1409 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1411 low_output &= ~nTRST; /* switch output low */
1415 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1416 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1418 low_output |= nTRST; /* switch output high */
1423 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1424 low_output &= ~nSRST; /* switch output low */
1426 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1430 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1431 low_output |= nSRST; /* switch output high */
1433 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1436 /* command "set data bits low byte" */
1438 buffer_write(low_output);
1439 buffer_write(low_direction);
1442 static void jtagkey_reset(int trst, int srst)
1444 enum reset_types jtag_reset_config = jtag_get_reset_config();
1447 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1448 high_output &= ~nTRSTnOE;
1450 high_output &= ~nTRST;
1454 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1455 high_output |= nTRSTnOE;
1457 high_output |= nTRST;
1462 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1463 high_output &= ~nSRST;
1465 high_output &= ~nSRSTnOE;
1469 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1470 high_output |= nSRST;
1472 high_output |= nSRSTnOE;
1475 /* command "set data bits high byte" */
1477 buffer_write(high_output);
1478 buffer_write(high_direction);
1479 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1483 static void olimex_jtag_reset(int trst, int srst)
1485 enum reset_types jtag_reset_config = jtag_get_reset_config();
1488 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1489 high_output &= ~nTRSTnOE;
1491 high_output &= ~nTRST;
1495 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1496 high_output |= nTRSTnOE;
1498 high_output |= nTRST;
1503 high_output |= nSRST;
1507 high_output &= ~nSRST;
1510 /* command "set data bits high byte" */
1512 buffer_write(high_output);
1513 buffer_write(high_direction);
1514 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1518 static void axm0432_jtag_reset(int trst, int srst)
1522 tap_set_state(TAP_RESET);
1523 high_output &= ~nTRST;
1527 high_output |= nTRST;
1532 high_output &= ~nSRST;
1536 high_output |= nSRST;
1539 /* command "set data bits low byte" */
1541 buffer_write(high_output);
1542 buffer_write(high_direction);
1543 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1547 static void flyswatter_reset(int trst, int srst)
1551 low_output &= ~nTRST;
1555 low_output |= nTRST;
1560 low_output |= nSRST;
1564 low_output &= ~nSRST;
1567 /* command "set data bits low byte" */
1569 buffer_write(low_output);
1570 buffer_write(low_direction);
1571 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1574 static void minimodule_reset(int trst, int srst)
1578 low_output &= ~nSRST;
1582 low_output |= nSRST;
1585 /* command "set data bits low byte" */
1587 buffer_write(low_output);
1588 buffer_write(low_direction);
1589 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1592 static void turtle_reset(int trst, int srst)
1598 low_output |= nSRST;
1602 low_output &= ~nSRST;
1605 /* command "set data bits low byte" */
1607 buffer_write(low_output);
1608 buffer_write(low_direction);
1609 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1612 static void comstick_reset(int trst, int srst)
1616 high_output &= ~nTRST;
1620 high_output |= nTRST;
1625 high_output &= ~nSRST;
1629 high_output |= nSRST;
1632 /* command "set data bits high byte" */
1634 buffer_write(high_output);
1635 buffer_write(high_direction);
1636 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1640 static void stm32stick_reset(int trst, int srst)
1644 high_output &= ~nTRST;
1648 high_output |= nTRST;
1653 low_output &= ~nSRST;
1657 low_output |= nSRST;
1660 /* command "set data bits low byte" */
1662 buffer_write(low_output);
1663 buffer_write(low_direction);
1665 /* command "set data bits high byte" */
1667 buffer_write(high_output);
1668 buffer_write(high_direction);
1669 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1673 static void sheevaplug_reset(int trst, int srst)
1676 high_output &= ~nTRST;
1678 high_output |= nTRST;
1681 high_output &= ~nSRSTnOE;
1683 high_output |= nSRSTnOE;
1685 /* command "set data bits high byte" */
1687 buffer_write(high_output);
1688 buffer_write(high_direction);
1689 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1692 static void redbee_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 void xds100v2_reset(int trst, int srst)
1726 tap_set_state(TAP_RESET);
1727 high_output &= ~nTRST;
1731 high_output |= nTRST;
1736 high_output |= nSRST;
1740 high_output &= ~nSRST;
1743 /* command "set data bits low byte" */
1745 buffer_write(high_output);
1746 buffer_write(high_direction);
1747 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1748 "high_direction: 0x%2.2x", trst, srst, high_output,
1752 static int ft2232_execute_runtest(struct jtag_command *cmd)
1756 int predicted_size = 0;
1759 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1760 cmd->cmd.runtest->num_cycles,
1761 tap_state_name(cmd->cmd.runtest->end_state));
1763 /* only send the maximum buffer size that FT2232C can handle */
1765 if (tap_get_state() != TAP_IDLE)
1766 predicted_size += 3;
1767 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1768 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1769 predicted_size += 3;
1770 if (tap_get_end_state() != TAP_IDLE)
1771 predicted_size += 3;
1772 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1774 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1775 retval = ERROR_JTAG_QUEUE_FAILED;
1779 if (tap_get_state() != TAP_IDLE)
1781 move_to_state(TAP_IDLE);
1784 i = cmd->cmd.runtest->num_cycles;
1787 /* there are no state transitions in this code, so omit state tracking */
1789 /* command "Clock Data to TMS/CS Pin (no Read)" */
1793 buffer_write((i > 7) ? 6 : (i - 1));
1798 i -= (i > 7) ? 7 : i;
1799 /* LOG_DEBUG("added TMS scan (no read)"); */
1802 ft2232_end_state(cmd->cmd.runtest->end_state);
1804 if (tap_get_state() != tap_get_end_state())
1806 move_to_state(tap_get_end_state());
1810 DEBUG_JTAG_IO("runtest: %i, end in %s",
1811 cmd->cmd.runtest->num_cycles,
1812 tap_state_name(tap_get_end_state()));
1816 static int ft2232_execute_statemove(struct jtag_command *cmd)
1818 int predicted_size = 0;
1819 int retval = ERROR_OK;
1821 DEBUG_JTAG_IO("statemove end in %s",
1822 tap_state_name(cmd->cmd.statemove->end_state));
1824 /* only send the maximum buffer size that FT2232C can handle */
1826 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1828 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1829 retval = ERROR_JTAG_QUEUE_FAILED;
1833 ft2232_end_state(cmd->cmd.statemove->end_state);
1835 /* For TAP_RESET, ignore the current recorded state. It's often
1836 * wrong at server startup, and this transation is critical whenever
1839 if (tap_get_end_state() == TAP_RESET) {
1840 clock_tms(0x4b, 0xff, 5, 0);
1843 /* shortest-path move to desired end state */
1844 } else if (tap_get_state() != tap_get_end_state())
1846 move_to_state(tap_get_end_state());
1854 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1855 * (or SWD) state machine.
1857 static int ft2232_execute_tms(struct jtag_command *cmd)
1859 int retval = ERROR_OK;
1860 unsigned num_bits = cmd->cmd.tms->num_bits;
1861 const uint8_t *bits = cmd->cmd.tms->bits;
1864 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1866 /* only send the maximum buffer size that FT2232C can handle */
1867 count = 3 * DIV_ROUND_UP(num_bits, 4);
1868 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1869 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1870 retval = ERROR_JTAG_QUEUE_FAILED;
1876 /* Shift out in batches of at most 6 bits; there's a report of an
1877 * FT2232 bug in this area, where shifting exactly 7 bits can make
1878 * problems with TMS signaling for the last clock cycle:
1880 * http://developer.intra2net.com/mailarchive/html/
1881 * libftdi/2009/msg00292.html
1883 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1885 * Note that pathmoves in JTAG are not often seven bits, so that
1886 * isn't a particularly likely situation outside of "special"
1887 * signaling such as switching between JTAG and SWD modes.
1890 if (num_bits <= 6) {
1892 buffer_write(num_bits - 1);
1893 buffer_write(*bits & 0x3f);
1897 /* Yes, this is lazy ... we COULD shift out more data
1898 * bits per operation, but doing it in nybbles is easy
1902 buffer_write(*bits & 0xf);
1905 count = (num_bits > 4) ? 4 : num_bits;
1908 buffer_write(count - 1);
1909 buffer_write((*bits >> 4) & 0xf);
1919 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1921 int predicted_size = 0;
1922 int retval = ERROR_OK;
1924 tap_state_t* path = cmd->cmd.pathmove->path;
1925 int num_states = cmd->cmd.pathmove->num_states;
1927 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1928 tap_state_name(tap_get_state()),
1929 tap_state_name(path[num_states-1]));
1931 /* only send the maximum buffer size that FT2232C can handle */
1932 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1933 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1935 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1936 retval = ERROR_JTAG_QUEUE_FAILED;
1942 ft2232_add_pathmove(path, num_states);
1948 static int ft2232_execute_scan(struct jtag_command *cmd)
1951 int scan_size; /* size of IR or DR scan */
1952 int predicted_size = 0;
1953 int retval = ERROR_OK;
1955 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1957 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1959 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1961 predicted_size = ft2232_predict_scan_out(scan_size, type);
1962 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1964 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1965 /* unsent commands before this */
1966 if (first_unsent != cmd)
1967 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1968 retval = ERROR_JTAG_QUEUE_FAILED;
1970 /* current command */
1971 ft2232_end_state(cmd->cmd.scan->end_state);
1972 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1974 first_unsent = cmd->next;
1979 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1981 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1984 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1985 retval = ERROR_JTAG_QUEUE_FAILED;
1989 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1990 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1991 ft2232_end_state(cmd->cmd.scan->end_state);
1992 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1996 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1997 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1998 tap_state_name(tap_get_end_state()));
2003 static int ft2232_execute_reset(struct jtag_command *cmd)
2006 int predicted_size = 0;
2009 DEBUG_JTAG_IO("reset trst: %i srst %i",
2010 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2012 /* only send the maximum buffer size that FT2232C can handle */
2014 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
2016 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2017 retval = ERROR_JTAG_QUEUE_FAILED;
2022 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
2024 tap_set_state(TAP_RESET);
2027 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2030 DEBUG_JTAG_IO("trst: %i, srst: %i",
2031 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2035 static int ft2232_execute_sleep(struct jtag_command *cmd)
2040 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2042 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2043 retval = ERROR_JTAG_QUEUE_FAILED;
2044 first_unsent = cmd->next;
2045 jtag_sleep(cmd->cmd.sleep->us);
2046 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2048 tap_state_name(tap_get_state()));
2052 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2057 /* this is only allowed while in a stable state. A check for a stable
2058 * state was done in jtag_add_clocks()
2060 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2061 retval = ERROR_JTAG_QUEUE_FAILED;
2062 DEBUG_JTAG_IO("clocks %i while in %s",
2063 cmd->cmd.stableclocks->num_cycles,
2064 tap_state_name(tap_get_state()));
2068 static int ft2232_execute_command(struct jtag_command *cmd)
2074 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2075 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2076 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2077 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2078 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2079 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2080 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2082 retval = ft2232_execute_tms(cmd);
2085 LOG_ERROR("BUG: unknown JTAG command type encountered");
2086 retval = ERROR_JTAG_QUEUE_FAILED;
2092 static int ft2232_execute_queue(void)
2094 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2097 first_unsent = cmd; /* next command that has to be sent */
2100 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2101 * that wasn't handled by a caller-provided error handler
2105 ft2232_buffer_size = 0;
2106 ft2232_expect_read = 0;
2108 /* blink, if the current layout has that feature */
2114 /* fill the write buffer with the desired command */
2115 if (ft2232_execute_command(cmd) != ERROR_OK)
2116 retval = ERROR_JTAG_QUEUE_FAILED;
2117 /* Start reading input before FT2232 TX buffer fills up.
2118 * Sometimes this happens because we don't know the
2119 * length of the last command before we execute it. So
2120 * we simple inform the user.
2124 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE )
2126 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1) )
2127 LOG_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read,(FT2232_BUFFER_READ_QUEUE_SIZE+1));
2128 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2129 retval = ERROR_JTAG_QUEUE_FAILED;
2134 if (require_send > 0)
2135 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2136 retval = ERROR_JTAG_QUEUE_FAILED;
2141 #if BUILD_FT2232_FTD2XX == 1
2142 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2146 char SerialNumber[16];
2147 char Description[64];
2148 DWORD openex_flags = 0;
2149 char* openex_string = NULL;
2150 uint8_t latency_timer;
2152 if (layout == NULL) {
2153 LOG_WARNING("No ft2232 layout specified'");
2154 return ERROR_JTAG_INIT_FAILED;
2157 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2160 /* Add non-standard Vid/Pid to the linux driver */
2161 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2163 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2167 if (ft2232_device_desc && ft2232_serial)
2169 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2170 ft2232_device_desc = NULL;
2173 if (ft2232_device_desc)
2175 openex_string = ft2232_device_desc;
2176 openex_flags = FT_OPEN_BY_DESCRIPTION;
2178 else if (ft2232_serial)
2180 openex_string = ft2232_serial;
2181 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2185 LOG_ERROR("neither device description nor serial number specified");
2186 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2188 return ERROR_JTAG_INIT_FAILED;
2191 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2192 if (status != FT_OK) {
2193 /* under Win32, the FTD2XX driver appends an "A" to the end
2194 * of the description, if we tried by the desc, then
2195 * try by the alternate "A" description. */
2196 if (openex_string == ft2232_device_desc) {
2197 /* Try the alternate method. */
2198 openex_string = ft2232_device_desc_A;
2199 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2200 if (status == FT_OK) {
2201 /* yea, the "alternate" method worked! */
2203 /* drat, give the user a meaningfull message.
2204 * telling the use we tried *BOTH* methods. */
2205 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2207 ft2232_device_desc_A);
2212 if (status != FT_OK)
2218 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2220 return ERROR_JTAG_INIT_FAILED;
2222 LOG_ERROR("unable to open ftdi device: %lu", status);
2223 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2224 if (status == FT_OK)
2226 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2229 for (i = 0; i < num_devices; i++)
2230 desc_array[i] = malloc(64);
2232 desc_array[num_devices] = NULL;
2234 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2236 if (status == FT_OK)
2238 LOG_ERROR("ListDevices: %lu", num_devices);
2239 for (i = 0; i < num_devices; i++)
2240 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2243 for (i = 0; i < num_devices; i++)
2244 free(desc_array[i]);
2250 LOG_ERROR("ListDevices: NONE");
2252 return ERROR_JTAG_INIT_FAILED;
2255 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2257 LOG_ERROR("unable to set latency timer: %lu", status);
2258 return ERROR_JTAG_INIT_FAILED;
2261 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2263 LOG_ERROR("unable to get latency timer: %lu", status);
2264 return ERROR_JTAG_INIT_FAILED;
2268 LOG_DEBUG("current latency timer: %i", latency_timer);
2271 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2273 LOG_ERROR("unable to set timeouts: %lu", status);
2274 return ERROR_JTAG_INIT_FAILED;
2277 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2279 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2280 return ERROR_JTAG_INIT_FAILED;
2283 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2285 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2286 return ERROR_JTAG_INIT_FAILED;
2290 static const char* type_str[] =
2291 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2292 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2293 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2294 ? ftdi_device : FT_DEVICE_UNKNOWN;
2295 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2296 LOG_INFO("deviceID: %lu", deviceID);
2297 LOG_INFO("SerialNumber: %s", SerialNumber);
2298 LOG_INFO("Description: %s", Description);
2304 static int ft2232_purge_ftd2xx(void)
2308 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2310 LOG_ERROR("error purging ftd2xx device: %lu", status);
2311 return ERROR_JTAG_INIT_FAILED;
2317 #endif /* BUILD_FT2232_FTD2XX == 1 */
2319 #if BUILD_FT2232_LIBFTDI == 1
2320 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2322 uint8_t latency_timer;
2324 if (layout == NULL) {
2325 LOG_WARNING("No ft2232 layout specified'");
2326 return ERROR_JTAG_INIT_FAILED;
2329 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2330 layout->name, vid, pid);
2332 if (ftdi_init(&ftdic) < 0)
2333 return ERROR_JTAG_INIT_FAILED;
2335 /* default to INTERFACE_A */
2336 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2338 if (ftdi_set_interface(&ftdic, channel) < 0)
2340 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2341 return ERROR_JTAG_INIT_FAILED;
2344 /* context, vendor id, product id */
2345 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2349 LOG_WARNING("unable to open ftdi device (trying more): %s",
2352 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2354 return ERROR_JTAG_INIT_FAILED;
2357 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2358 if (ftdi_usb_reset(&ftdic) < 0)
2360 LOG_ERROR("unable to reset ftdi device");
2361 return ERROR_JTAG_INIT_FAILED;
2364 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2366 LOG_ERROR("unable to set latency timer");
2367 return ERROR_JTAG_INIT_FAILED;
2370 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2372 LOG_ERROR("unable to get latency timer");
2373 return ERROR_JTAG_INIT_FAILED;
2377 LOG_DEBUG("current latency timer: %i", latency_timer);
2380 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2382 ftdi_device = ftdic.type;
2383 static const char* type_str[] =
2384 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2385 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2386 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2387 ? ftdi_device : no_of_known_types;
2388 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2392 static int ft2232_purge_libftdi(void)
2394 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2396 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2397 return ERROR_JTAG_INIT_FAILED;
2403 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2405 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2408 uint32_t bytes_written;
2410 buf[0] = 0x80; /* command "set data bits low byte" */
2411 buf[1] = value; /* value */
2412 buf[2] = direction; /* direction */
2414 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2416 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2418 LOG_ERROR("couldn't initialize data bits low byte");
2419 return ERROR_JTAG_INIT_FAILED;
2425 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2428 uint32_t bytes_written;
2430 buf[0] = 0x82; /* command "set data bits high byte" */
2431 buf[1] = value; /* value */
2432 buf[2] = direction; /* direction */
2434 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2436 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2438 LOG_ERROR("couldn't initialize data bits high byte");
2439 return ERROR_JTAG_INIT_FAILED;
2445 static int ft2232_init(void)
2449 uint32_t bytes_written;
2451 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2453 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2457 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2460 if (layout == NULL) {
2461 LOG_WARNING("No ft2232 layout specified'");
2462 return ERROR_JTAG_INIT_FAILED;
2465 for (int i = 0; 1; i++)
2468 * "more indicates that there are more IDs to try, so we should
2469 * not print an error for an ID mismatch (but for anything
2472 * try_more indicates that the error code returned indicates an
2473 * ID mismatch (and nothing else) and that we should proceeed
2474 * with the next ID pair.
2476 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2479 #if BUILD_FT2232_FTD2XX == 1
2480 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2482 #elif BUILD_FT2232_LIBFTDI == 1
2483 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2484 more, &try_more, layout->channel);
2488 if (!more || !try_more)
2492 ft2232_buffer_size = 0;
2493 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2495 if (layout->init() != ERROR_OK)
2496 return ERROR_JTAG_INIT_FAILED;
2498 if (ft2232_device_is_highspeed())
2500 #ifndef BUILD_FT2232_HIGHSPEED
2501 #if BUILD_FT2232_FTD2XX == 1
2502 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2503 #elif BUILD_FT2232_LIBFTDI == 1
2504 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2507 /* make sure the legacy mode is disabled */
2508 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2509 return ERROR_JTAG_INIT_FAILED;
2512 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2513 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2515 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2516 return ERROR_JTAG_INIT_FAILED;
2519 #if BUILD_FT2232_FTD2XX == 1
2520 return ft2232_purge_ftd2xx();
2521 #elif BUILD_FT2232_LIBFTDI == 1
2522 return ft2232_purge_libftdi();
2528 /** Updates defaults for DBUS signals: the four JTAG signals
2529 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2531 static inline void ftx232_dbus_init(void)
2534 low_direction = 0x0b;
2537 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2538 * the four GPIOL signals. Initialization covers value and direction,
2539 * as customized for each layout.
2541 static int ftx232_dbus_write(void)
2543 enum reset_types jtag_reset_config = jtag_get_reset_config();
2544 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2546 low_direction &= ~nTRSTnOE; /* nTRST input */
2547 low_output &= ~nTRST; /* nTRST = 0 */
2551 low_direction |= nTRSTnOE; /* nTRST output */
2552 low_output |= nTRST; /* nTRST = 1 */
2555 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2557 low_direction |= nSRSTnOE; /* nSRST output */
2558 low_output |= nSRST; /* nSRST = 1 */
2562 low_direction &= ~nSRSTnOE; /* nSRST input */
2563 low_output &= ~nSRST; /* nSRST = 0 */
2566 /* initialize low byte for jtag */
2567 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2569 LOG_ERROR("couldn't initialize FT2232 DBUS");
2570 return ERROR_JTAG_INIT_FAILED;
2576 static int usbjtag_init(void)
2579 * NOTE: This is now _specific_ to the "usbjtag" layout.
2580 * Don't try cram any more layouts into this.
2589 return ftx232_dbus_write();
2592 static int lm3s811_jtag_init(void)
2596 /* There are multiple revisions of LM3S811 eval boards:
2597 * - Rev B (and older?) boards have no SWO trace support.
2598 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2599 * they should use the "luminary_icdi" layout instead.
2606 low_direction = 0x8b;
2608 return ftx232_dbus_write();
2611 static int icdi_jtag_init(void)
2615 /* Most Luminary eval boards support SWO trace output,
2616 * and should use this "luminary_icdi" layout.
2618 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2619 * to switch between JTAG and SWD, or switch the ft2232 UART
2620 * on the second MPSSE channel/interface (BDBUS)
2621 * between (i) the stellaris UART (on Luminary boards)
2622 * or (ii) SWO trace data (generic).
2624 * We come up in JTAG mode and may switch to SWD later (with
2625 * SWO/trace option if SWD is active).
2632 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2633 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2634 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2637 /* GPIOs on second channel/interface (UART) ... */
2638 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2639 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2640 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2645 nSRSTnOE = ICDI_SRST;
2647 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2648 low_output |= ICDI_JTAG_EN;
2649 low_output &= ~ICDI_DBG_ENn;
2651 return ftx232_dbus_write();
2654 static int signalyzer_init(void)
2662 return ftx232_dbus_write();
2665 static int axm0432_jtag_init(void)
2668 low_direction = 0x2b;
2670 /* initialize low byte for jtag */
2671 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2673 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2674 return ERROR_JTAG_INIT_FAILED;
2677 if (strcmp(layout->name, "axm0432_jtag") == 0)
2680 nTRSTnOE = 0x0; /* No output enable for TRST*/
2682 nSRSTnOE = 0x0; /* No output enable for SRST*/
2686 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2691 high_direction = 0x0c;
2693 enum reset_types jtag_reset_config = jtag_get_reset_config();
2694 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2696 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2700 high_output |= nTRST;
2703 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2705 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2709 high_output |= nSRST;
2712 /* initialize high byte for jtag */
2713 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2715 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2716 return ERROR_JTAG_INIT_FAILED;
2722 static int redbee_init(void)
2725 low_direction = 0x2b;
2727 /* initialize low byte for jtag */
2728 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2730 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2731 return ERROR_JTAG_INIT_FAILED;
2735 nTRSTnOE = 0x0; /* No output enable for TRST*/
2737 nSRSTnOE = 0x0; /* No output enable for SRST*/
2740 high_direction = 0x0c;
2742 enum reset_types jtag_reset_config = jtag_get_reset_config();
2743 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2745 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2749 high_output |= nTRST;
2752 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2754 LOG_ERROR("can't set nSRST to push-pull on redbee");
2758 high_output |= nSRST;
2761 /* initialize high byte for jtag */
2762 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2764 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2765 return ERROR_JTAG_INIT_FAILED;
2771 static int jtagkey_init(void)
2774 low_direction = 0x1b;
2776 /* initialize low byte for jtag */
2777 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2779 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2780 return ERROR_JTAG_INIT_FAILED;
2783 if (strcmp(layout->name, "jtagkey") == 0)
2790 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2791 || (strcmp(layout->name, "oocdlink") == 0))
2800 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2805 high_direction = 0x0f;
2807 enum reset_types jtag_reset_config = jtag_get_reset_config();
2808 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2810 high_output |= nTRSTnOE;
2811 high_output &= ~nTRST;
2815 high_output &= ~nTRSTnOE;
2816 high_output |= nTRST;
2819 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2821 high_output &= ~nSRSTnOE;
2822 high_output |= nSRST;
2826 high_output |= nSRSTnOE;
2827 high_output &= ~nSRST;
2830 /* initialize high byte for jtag */
2831 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2833 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2834 return ERROR_JTAG_INIT_FAILED;
2840 static int olimex_jtag_init(void)
2843 low_direction = 0x1b;
2845 /* initialize low byte for jtag */
2846 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2848 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2849 return ERROR_JTAG_INIT_FAILED;
2855 nSRSTnOE = 0x00; /* no output enable for nSRST */
2858 high_direction = 0x0f;
2860 enum reset_types jtag_reset_config = jtag_get_reset_config();
2861 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2863 high_output |= nTRSTnOE;
2864 high_output &= ~nTRST;
2868 high_output &= ~nTRSTnOE;
2869 high_output |= nTRST;
2872 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2874 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2878 high_output &= ~nSRST;
2881 /* turn red LED on */
2882 high_output |= 0x08;
2884 /* initialize high byte for jtag */
2885 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2887 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2888 return ERROR_JTAG_INIT_FAILED;
2894 static int flyswatter_init(void)
2897 low_direction = 0xfb;
2899 /* initialize low byte for jtag */
2900 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2902 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2903 return ERROR_JTAG_INIT_FAILED;
2907 nTRSTnOE = 0x0; /* not output enable for nTRST */
2909 nSRSTnOE = 0x00; /* no output enable for nSRST */
2912 high_direction = 0x0c;
2914 /* turn red LED3 on, LED2 off */
2915 high_output |= 0x08;
2917 /* initialize high byte for jtag */
2918 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2920 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2921 return ERROR_JTAG_INIT_FAILED;
2927 static int minimodule_init(void)
2929 low_output = 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18)
2930 low_direction = 0xfb;//0xfb;
2932 /* initialize low byte for jtag */
2933 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2935 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2936 return ERROR_JTAG_INIT_FAILED;
2943 high_direction = 0x05;
2945 /* turn red LED3 on, LED2 off */
2946 //high_output |= 0x08;
2948 /* initialize high byte for jtag */
2949 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2951 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2952 return ERROR_JTAG_INIT_FAILED;
2958 static int turtle_init(void)
2961 low_direction = 0x5b;
2963 /* initialize low byte for jtag */
2964 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2966 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2967 return ERROR_JTAG_INIT_FAILED;
2973 high_direction = 0x0C;
2975 /* initialize high byte for jtag */
2976 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2978 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2979 return ERROR_JTAG_INIT_FAILED;
2985 static int comstick_init(void)
2988 low_direction = 0x0b;
2990 /* initialize low byte for jtag */
2991 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2993 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2994 return ERROR_JTAG_INIT_FAILED;
2998 nTRSTnOE = 0x00; /* no output enable for nTRST */
3000 nSRSTnOE = 0x00; /* no output enable for nSRST */
3003 high_direction = 0x03;
3005 /* initialize high byte for jtag */
3006 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3008 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3009 return ERROR_JTAG_INIT_FAILED;
3015 static int stm32stick_init(void)
3018 low_direction = 0x8b;
3020 /* initialize low byte for jtag */
3021 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3023 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3024 return ERROR_JTAG_INIT_FAILED;
3028 nTRSTnOE = 0x00; /* no output enable for nTRST */
3030 nSRSTnOE = 0x00; /* no output enable for nSRST */
3033 high_direction = 0x03;
3035 /* initialize high byte for jtag */
3036 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3038 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3039 return ERROR_JTAG_INIT_FAILED;
3045 static int sheevaplug_init(void)
3048 low_direction = 0x1b;
3050 /* initialize low byte for jtag */
3051 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3053 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3054 return ERROR_JTAG_INIT_FAILED;
3063 high_direction = 0x0f;
3065 /* nTRST is always push-pull */
3066 high_output &= ~nTRSTnOE;
3067 high_output |= nTRST;
3069 /* nSRST is always open-drain */
3070 high_output |= nSRSTnOE;
3071 high_output &= ~nSRST;
3073 /* initialize high byte for jtag */
3074 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3076 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3077 return ERROR_JTAG_INIT_FAILED;
3083 static int cortino_jtag_init(void)
3086 low_direction = 0x1b;
3088 /* initialize low byte for jtag */
3089 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3091 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3092 return ERROR_JTAG_INIT_FAILED;
3096 nTRSTnOE = 0x00; /* no output enable for nTRST */
3098 nSRSTnOE = 0x00; /* no output enable for nSRST */
3101 high_direction = 0x03;
3103 /* initialize high byte for jtag */
3104 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3106 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3107 return ERROR_JTAG_INIT_FAILED;
3113 static int lisa_l_init(void)
3123 high_direction = 0x18;
3125 /* initialize high byte for jtag */
3126 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3128 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3129 return ERROR_JTAG_INIT_FAILED;
3132 return ftx232_dbus_write();
3135 static int flossjtag_init(void)
3145 high_direction = 0x18;
3147 /* initialize high byte for jtag */
3148 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3150 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3151 return ERROR_JTAG_INIT_FAILED;
3154 return ftx232_dbus_write();
3157 static int xds100v2_init(void)
3160 low_direction = 0x7B;
3162 /* initialize low byte for jtag */
3163 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3165 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3166 return ERROR_JTAG_INIT_FAILED;
3170 nTRSTnOE = 0x0; /* not output enable for nTRST */
3171 nSRST = 0x00; /* TODO: SRST is not supported yet */
3172 nSRSTnOE = 0x00; /* no output enable for nSRST */
3175 high_direction = 0x59;
3177 /* initialize high byte for jtag */
3178 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3180 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3181 return ERROR_JTAG_INIT_FAILED;
3185 high_direction = 0x59;
3187 /* initialize high byte for jtag */
3188 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3190 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3191 return ERROR_JTAG_INIT_FAILED;
3197 static void olimex_jtag_blink(void)
3199 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3200 * ACBUS3 is bit 3 of the GPIOH port
3202 high_output ^= 0x08;
3205 buffer_write(high_output);
3206 buffer_write(high_direction);
3209 static void flyswatter_jtag_blink(void)
3212 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3214 high_output ^= 0x0c;
3217 buffer_write(high_output);
3218 buffer_write(high_direction);
3221 static void turtle_jtag_blink(void)
3224 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3226 if (high_output & 0x08)
3236 buffer_write(high_output);
3237 buffer_write(high_direction);
3240 static void lisa_l_blink(void)
3243 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3245 if (high_output & 0x10)
3255 buffer_write(high_output);
3256 buffer_write(high_direction);
3259 static void flossjtag_blink(void)
3262 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3264 if (high_output & 0x10)
3274 buffer_write(high_output);
3275 buffer_write(high_direction);
3278 static int ft2232_quit(void)
3280 #if BUILD_FT2232_FTD2XX == 1
3283 status = FT_Close(ftdih);
3284 #elif BUILD_FT2232_LIBFTDI == 1
3285 ftdi_usb_close(&ftdic);
3287 ftdi_deinit(&ftdic);
3290 free(ft2232_buffer);
3291 ft2232_buffer = NULL;
3296 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3302 ft2232_device_desc = strdup(CMD_ARGV[0]);
3303 cp = strchr(ft2232_device_desc, 0);
3304 /* under Win32, the FTD2XX driver appends an "A" to the end
3305 * of the description, this examines the given desc
3306 * and creates the 'missing' _A or non_A variable. */
3307 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3308 /* it was, so make this the "A" version. */
3309 ft2232_device_desc_A = ft2232_device_desc;
3310 /* and *CREATE* the non-A version. */
3311 strcpy(buf, ft2232_device_desc);
3312 cp = strchr(buf, 0);
3314 ft2232_device_desc = strdup(buf);
3316 /* <space > A not defined
3318 sprintf(buf, "%s A", ft2232_device_desc);
3319 ft2232_device_desc_A = strdup(buf);
3324 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3330 COMMAND_HANDLER(ft2232_handle_serial_command)
3334 ft2232_serial = strdup(CMD_ARGV[0]);
3338 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3344 COMMAND_HANDLER(ft2232_handle_layout_command)
3346 if (CMD_ARGC != 1) {
3347 LOG_ERROR("Need exactly one argument to ft2232_layout");
3352 LOG_ERROR("already specified ft2232_layout %s",
3354 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3359 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3360 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3366 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3370 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3372 if (CMD_ARGC > MAX_USB_IDS * 2)
3374 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3375 "(maximum is %d pairs)", MAX_USB_IDS);
3376 CMD_ARGC = MAX_USB_IDS * 2;
3378 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3380 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3382 return ERROR_COMMAND_SYNTAX_ERROR;
3383 /* remove the incomplete trailing id */
3388 for (i = 0; i < CMD_ARGC; i += 2)
3390 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3391 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3395 * Explicitly terminate, in case there are multiples instances of
3398 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3403 COMMAND_HANDLER(ft2232_handle_latency_command)
3407 ft2232_latency = atoi(CMD_ARGV[0]);
3411 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3417 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3421 /* 7 bits of either ones or zeros. */
3422 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3424 while (num_cycles > 0)
3426 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3427 * at most 7 bits per invocation. Here we invoke it potentially
3430 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3432 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3434 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3435 retval = ERROR_JTAG_QUEUE_FAILED;
3440 /* there are no state transitions in this code, so omit state tracking */
3442 /* command "Clock Data to TMS/CS Pin (no Read)" */
3446 buffer_write(bitcount_per_command - 1);
3448 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3453 num_cycles -= bitcount_per_command;
3459 /* ---------------------------------------------------------------------
3460 * Support for IceBear JTAG adapter from Section5:
3461 * http://section5.ch/icebear
3463 * Author: Sten, debian@sansys-electronic.com
3466 /* Icebear pin layout
3468 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3469 * GND GND | 4 3| n.c.
3470 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3471 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3472 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3473 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3474 * ADBUS2 TDO |14 13| GND GND
3476 * ADBUS0 O L TCK ACBUS0 GND
3477 * ADBUS1 O L TDI ACBUS1 GND
3478 * ADBUS2 I TDO ACBUS2 n.c.
3479 * ADBUS3 O H TMS ACBUS3 n.c.
3485 static int icebear_jtag_init(void) {
3486 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3487 low_output = 0x08; /* high: TMS; low: TCK TDI */
3491 enum reset_types jtag_reset_config = jtag_get_reset_config();
3492 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3493 low_direction &= ~nTRST; /* nTRST high impedance */
3496 low_direction |= nTRST;
3497 low_output |= nTRST;
3500 low_direction |= nSRST;
3501 low_output |= nSRST;
3503 /* initialize low byte for jtag */
3504 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3505 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3506 return ERROR_JTAG_INIT_FAILED;
3510 high_direction = 0x00;
3512 /* initialize high byte for jtag */
3513 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3514 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3515 return ERROR_JTAG_INIT_FAILED;
3521 static void icebear_jtag_reset(int trst, int srst) {
3524 low_direction |= nTRST;
3525 low_output &= ~nTRST;
3527 else if (trst == 0) {
3528 enum reset_types jtag_reset_config = jtag_get_reset_config();
3529 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3530 low_direction &= ~nTRST;
3532 low_output |= nTRST;
3536 low_output &= ~nSRST;
3538 else if (srst == 0) {
3539 low_output |= nSRST;
3542 /* command "set data bits low byte" */
3544 buffer_write(low_output);
3545 buffer_write(low_direction);
3547 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3550 /* ---------------------------------------------------------------------
3551 * Support for Signalyzer H2 and Signalyzer H4
3552 * JTAG adapter from Xverve Technologies Inc.
3553 * http://www.signalyzer.com or http://www.xverve.com
3555 * Author: Oleg Seiljus, oleg@signalyzer.com
3557 static unsigned char signalyzer_h_side;
3558 static unsigned int signalyzer_h_adapter_type;
3560 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3562 #if BUILD_FT2232_FTD2XX == 1
3563 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3566 #define SIGNALYZER_COMMAND_ADDR 128
3567 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3569 #define SIGNALYZER_COMMAND_VERSION 0x41
3570 #define SIGNALYZER_COMMAND_RESET 0x42
3571 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3572 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3573 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3574 #define SIGNALYZER_COMMAND_LED_SET 0x53
3575 #define SIGNALYZER_COMMAND_ADC 0x54
3576 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3577 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3578 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3579 #define SIGNALYZER_COMMAND_I2C 0x58
3581 #define SIGNALYZER_CHAN_A 1
3582 #define SIGNALYZER_CHAN_B 2
3583 /* LEDS use channel C */
3584 #define SIGNALYZER_CHAN_C 4
3586 #define SIGNALYZER_LED_GREEN 1
3587 #define SIGNALYZER_LED_RED 2
3589 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3590 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3591 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3592 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3593 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3596 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3598 #if BUILD_FT2232_FTD2XX == 1
3599 return FT_WriteEE(ftdih, address, value);
3600 #elif BUILD_FT2232_LIBFTDI == 1
3605 #if BUILD_FT2232_FTD2XX == 1
3606 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3608 return FT_ReadEE(ftdih, address, value);
3612 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3613 int on_time_ms, int off_time_ms, unsigned char cycles)
3615 unsigned char on_time;
3616 unsigned char off_time;
3618 if (on_time_ms < 0xFFFF)
3619 on_time = (unsigned char)(on_time_ms / 62);
3623 off_time = (unsigned char)(off_time_ms / 62);
3625 #if BUILD_FT2232_FTD2XX == 1
3628 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3629 ((uint32_t)(channel << 8) | led))) != FT_OK)
3631 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3632 return ERROR_JTAG_DEVICE_ERROR;
3635 if ((status = signalyzer_h_ctrl_write(
3636 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3637 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3639 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3640 return ERROR_JTAG_DEVICE_ERROR;
3643 if ((status = signalyzer_h_ctrl_write(
3644 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3645 ((uint32_t)cycles))) != FT_OK)
3647 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3648 return ERROR_JTAG_DEVICE_ERROR;
3651 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3652 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3654 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3655 return ERROR_JTAG_DEVICE_ERROR;
3659 #elif BUILD_FT2232_LIBFTDI == 1
3662 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3663 ((uint32_t)(channel << 8) | led))) < 0)
3665 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3666 ftdi_get_error_string(&ftdic));
3667 return ERROR_JTAG_DEVICE_ERROR;
3670 if ((retval = signalyzer_h_ctrl_write(
3671 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3672 ((uint32_t)(on_time << 8) | off_time))) < 0)
3674 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3675 ftdi_get_error_string(&ftdic));
3676 return ERROR_JTAG_DEVICE_ERROR;
3679 if ((retval = signalyzer_h_ctrl_write(
3680 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3681 (uint32_t)cycles)) < 0)
3683 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3684 ftdi_get_error_string(&ftdic));
3685 return ERROR_JTAG_DEVICE_ERROR;
3688 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3689 SIGNALYZER_COMMAND_LED_SET)) < 0)
3691 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3692 ftdi_get_error_string(&ftdic));
3693 return ERROR_JTAG_DEVICE_ERROR;
3700 static int signalyzer_h_init(void)
3702 #if BUILD_FT2232_FTD2XX == 1
3709 uint16_t read_buf[12] = { 0 };
3711 /* turn on center green led */
3712 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3713 0xFFFF, 0x00, 0x00);
3715 /* determine what channel config wants to open
3716 * TODO: change me... current implementation is made to work
3717 * with openocd description parsing.
3719 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3723 signalyzer_h_side = *(end_of_desc - 1);
3724 if (signalyzer_h_side == 'B')
3725 signalyzer_h_side = SIGNALYZER_CHAN_B;
3727 signalyzer_h_side = SIGNALYZER_CHAN_A;
3731 LOG_ERROR("No Channel was specified");
3735 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3738 #if BUILD_FT2232_FTD2XX == 1
3739 /* read signalyzer versionining information */
3740 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3741 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3743 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3744 return ERROR_JTAG_DEVICE_ERROR;
3747 for (i = 0; i < 10; i++)
3749 if ((status = signalyzer_h_ctrl_read(
3750 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3751 &read_buf[i])) != FT_OK)
3753 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3755 return ERROR_JTAG_DEVICE_ERROR;
3759 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3760 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3761 read_buf[4], read_buf[5], read_buf[6]);
3763 /* set gpio register */
3764 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3765 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3767 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3768 return ERROR_JTAG_DEVICE_ERROR;
3771 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3774 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3775 return ERROR_JTAG_DEVICE_ERROR;
3778 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3779 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3781 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3782 return ERROR_JTAG_DEVICE_ERROR;
3785 /* read adapter type information */
3786 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3787 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3789 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3790 return ERROR_JTAG_DEVICE_ERROR;
3793 if ((status = signalyzer_h_ctrl_write(
3794 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3796 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3797 return ERROR_JTAG_DEVICE_ERROR;
3800 if ((status = signalyzer_h_ctrl_write(
3801 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3803 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3804 return ERROR_JTAG_DEVICE_ERROR;
3807 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3808 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3810 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3811 return ERROR_JTAG_DEVICE_ERROR;
3816 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3817 &read_buf[0])) != FT_OK)
3819 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3820 return ERROR_JTAG_DEVICE_ERROR;
3823 if (read_buf[0] != 0x0498)
3824 signalyzer_h_adapter_type = 0x0000;
3827 for (i = 0; i < 4; i++)
3829 if ((status = signalyzer_h_ctrl_read(
3830 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3831 &read_buf[i])) != FT_OK)
3833 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3835 return ERROR_JTAG_DEVICE_ERROR;
3839 signalyzer_h_adapter_type = read_buf[0];
3842 #elif BUILD_FT2232_LIBFTDI == 1
3843 /* currently libftdi does not allow reading individual eeprom
3844 * locations, therefore adapter type cannot be detected.
3845 * override with most common type
3847 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3850 enum reset_types jtag_reset_config = jtag_get_reset_config();
3852 /* ADAPTOR: EM_LT16_A */
3853 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3855 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3856 "detected. (HW: %2x).", (read_buf[1] >> 8));
3864 low_direction = 0x1b;
3867 high_direction = 0x0;
3869 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3871 low_direction &= ~nTRSTnOE; /* nTRST input */
3872 low_output &= ~nTRST; /* nTRST = 0 */
3876 low_direction |= nTRSTnOE; /* nTRST output */
3877 low_output |= nTRST; /* nTRST = 1 */
3880 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3882 low_direction |= nSRSTnOE; /* nSRST output */
3883 low_output |= nSRST; /* nSRST = 1 */
3887 low_direction &= ~nSRSTnOE; /* nSRST input */
3888 low_output &= ~nSRST; /* nSRST = 0 */
3891 #if BUILD_FT2232_FTD2XX == 1
3892 /* enable power to the module */
3893 if ((status = signalyzer_h_ctrl_write(
3894 SIGNALYZER_DATA_BUFFER_ADDR,
3895 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3898 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3900 return ERROR_JTAG_DEVICE_ERROR;
3903 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3904 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3906 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3908 return ERROR_JTAG_DEVICE_ERROR;
3911 /* set gpio mode register */
3912 if ((status = signalyzer_h_ctrl_write(
3913 SIGNALYZER_DATA_BUFFER_ADDR,
3914 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3916 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3918 return ERROR_JTAG_DEVICE_ERROR;
3921 if ((status = signalyzer_h_ctrl_write(
3922 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3925 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3927 return ERROR_JTAG_DEVICE_ERROR;
3930 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3931 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3933 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3935 return ERROR_JTAG_DEVICE_ERROR;
3938 /* set gpio register */
3939 if ((status = signalyzer_h_ctrl_write(
3940 SIGNALYZER_DATA_BUFFER_ADDR,
3941 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3943 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3945 return ERROR_JTAG_DEVICE_ERROR;
3948 if ((status = signalyzer_h_ctrl_write(
3949 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3952 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3954 return ERROR_JTAG_DEVICE_ERROR;
3957 if ((status = signalyzer_h_ctrl_write(
3958 SIGNALYZER_COMMAND_ADDR,
3959 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3961 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3963 return ERROR_JTAG_DEVICE_ERROR;
3968 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3969 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3970 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3971 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3972 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3974 if (signalyzer_h_adapter_type
3975 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3976 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3977 "detected. (HW: %2x).", (read_buf[1] >> 8));
3978 else if (signalyzer_h_adapter_type
3979 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3980 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3981 "(ARM JTAG with PSU) detected. (HW: %2x).",
3982 (read_buf[1] >> 8));
3983 else if (signalyzer_h_adapter_type
3984 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3985 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3986 "detected. (HW: %2x).", (read_buf[1] >> 8));
3987 else if (signalyzer_h_adapter_type
3988 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3989 LOG_INFO("Signalyzer: EM-JTAG-P "
3990 "(Generic JTAG with PSU) detected. (HW: %2x).",
3991 (read_buf[1] >> 8));
3999 low_direction = 0x1b;
4002 high_direction = 0x1f;
4004 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4006 high_output |= nTRSTnOE;
4007 high_output &= ~nTRST;
4011 high_output &= ~nTRSTnOE;
4012 high_output |= nTRST;
4015 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4017 high_output &= ~nSRSTnOE;
4018 high_output |= nSRST;
4022 high_output |= nSRSTnOE;
4023 high_output &= ~nSRST;
4026 #if BUILD_FT2232_FTD2XX == 1
4027 /* enable power to the module */
4028 if ((status = signalyzer_h_ctrl_write(
4029 SIGNALYZER_DATA_BUFFER_ADDR,
4030 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4033 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4035 return ERROR_JTAG_DEVICE_ERROR;
4038 if ((status = signalyzer_h_ctrl_write(
4039 SIGNALYZER_COMMAND_ADDR,
4040 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4042 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4044 return ERROR_JTAG_DEVICE_ERROR;
4047 /* set gpio mode register (IO_16 and IO_17 set as analog
4048 * inputs, other is gpio)
4050 if ((status = signalyzer_h_ctrl_write(
4051 SIGNALYZER_DATA_BUFFER_ADDR,
4052 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4054 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4056 return ERROR_JTAG_DEVICE_ERROR;
4059 if ((status = signalyzer_h_ctrl_write(
4060 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4063 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4065 return ERROR_JTAG_DEVICE_ERROR;
4068 if ((status = signalyzer_h_ctrl_write(
4069 SIGNALYZER_COMMAND_ADDR,
4070 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4072 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4074 return ERROR_JTAG_DEVICE_ERROR;
4077 /* set gpio register (all inputs, for -P modules,
4078 * PSU will be turned off)
4080 if ((status = signalyzer_h_ctrl_write(
4081 SIGNALYZER_DATA_BUFFER_ADDR,
4082 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4084 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4086 return ERROR_JTAG_DEVICE_ERROR;
4089 if ((status = signalyzer_h_ctrl_write(
4090 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4093 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4095 return ERROR_JTAG_DEVICE_ERROR;
4098 if ((status = signalyzer_h_ctrl_write(
4099 SIGNALYZER_COMMAND_ADDR,
4100 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4102 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4104 return ERROR_JTAG_DEVICE_ERROR;
4109 else if (signalyzer_h_adapter_type == 0x0000)
4111 LOG_INFO("Signalyzer: No external modules were detected.");
4119 low_direction = 0x1b;
4122 high_direction = 0x0;
4124 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4126 low_direction &= ~nTRSTnOE; /* nTRST input */
4127 low_output &= ~nTRST; /* nTRST = 0 */
4131 low_direction |= nTRSTnOE; /* nTRST output */
4132 low_output |= nTRST; /* nTRST = 1 */
4135 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4137 low_direction |= nSRSTnOE; /* nSRST output */
4138 low_output |= nSRST; /* nSRST = 1 */
4142 low_direction &= ~nSRSTnOE; /* nSRST input */
4143 low_output &= ~nSRST; /* nSRST = 0 */
4148 LOG_ERROR("Unknown module type is detected: %.4x",
4149 signalyzer_h_adapter_type);
4150 return ERROR_JTAG_DEVICE_ERROR;
4153 /* initialize low byte of controller for jtag operation */
4154 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4156 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4157 return ERROR_JTAG_INIT_FAILED;
4160 #if BUILD_FT2232_FTD2XX == 1
4161 if (ftdi_device == FT_DEVICE_2232H)
4163 /* initialize high byte of controller for jtag operation */
4164 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4166 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4167 return ERROR_JTAG_INIT_FAILED;
4170 #elif BUILD_FT2232_LIBFTDI == 1
4171 if (ftdi_device == TYPE_2232H)
4173 /* initialize high byte of controller for jtag operation */
4174 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4176 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4177 return ERROR_JTAG_INIT_FAILED;
4184 static void signalyzer_h_reset(int trst, int srst)
4186 enum reset_types jtag_reset_config = jtag_get_reset_config();
4188 /* ADAPTOR: EM_LT16_A */
4189 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4193 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4194 /* switch to output pin (output is low) */
4195 low_direction |= nTRSTnOE;
4197 /* switch output low */
4198 low_output &= ~nTRST;
4202 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4203 /* switch to input pin (high-Z + internal
4204 * and external pullup) */
4205 low_direction &= ~nTRSTnOE;
4207 /* switch output high */
4208 low_output |= nTRST;
4213 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4214 /* switch output low */
4215 low_output &= ~nSRST;
4217 /* switch to output pin (output is low) */
4218 low_direction |= nSRSTnOE;
4222 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4223 /* switch output high */
4224 low_output |= nSRST;
4226 /* switch to input pin (high-Z) */
4227 low_direction &= ~nSRSTnOE;
4230 /* command "set data bits low byte" */
4232 buffer_write(low_output);
4233 buffer_write(low_direction);
4234 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4235 "low_direction: 0x%2.2x",
4236 trst, srst, low_output, low_direction);
4238 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4239 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4240 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4241 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4242 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4246 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4247 high_output &= ~nTRSTnOE;
4249 high_output &= ~nTRST;
4253 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4254 high_output |= nTRSTnOE;
4256 high_output |= nTRST;
4261 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4262 high_output &= ~nSRST;
4264 high_output &= ~nSRSTnOE;
4268 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4269 high_output |= nSRST;
4271 high_output |= nSRSTnOE;
4274 /* command "set data bits high byte" */
4276 buffer_write(high_output);
4277 buffer_write(high_direction);
4278 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4279 "high_direction: 0x%2.2x",
4280 trst, srst, high_output, high_direction);
4282 else if (signalyzer_h_adapter_type == 0x0000)
4286 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4287 /* switch to output pin (output is low) */
4288 low_direction |= nTRSTnOE;
4290 /* switch output low */
4291 low_output &= ~nTRST;
4295 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4296 /* switch to input pin (high-Z + internal
4297 * and external pullup) */
4298 low_direction &= ~nTRSTnOE;
4300 /* switch output high */
4301 low_output |= nTRST;
4306 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4307 /* switch output low */
4308 low_output &= ~nSRST;
4310 /* switch to output pin (output is low) */
4311 low_direction |= nSRSTnOE;
4315 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4316 /* switch output high */
4317 low_output |= nSRST;
4319 /* switch to input pin (high-Z) */
4320 low_direction &= ~nSRSTnOE;
4323 /* command "set data bits low byte" */
4325 buffer_write(low_output);
4326 buffer_write(low_direction);
4327 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4328 "low_direction: 0x%2.2x",
4329 trst, srst, low_output, low_direction);
4333 static void signalyzer_h_blink(void)
4335 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4338 /********************************************************************
4339 * Support for KT-LINK
4340 * JTAG adapter from KRISTECH
4341 * http://www.kristech.eu
4342 *******************************************************************/
4343 static int ktlink_init(void)
4345 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4347 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4348 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4350 /* initialize low byte for jtag */
4351 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4353 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4354 return ERROR_JTAG_INIT_FAILED;
4362 high_output = 0x80; // turn LED on
4363 high_direction = 0xFF; // all outputs
4365 enum reset_types jtag_reset_config = jtag_get_reset_config();
4367 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4368 high_output |= nTRSTnOE;
4369 high_output &= ~nTRST;
4371 high_output &= ~nTRSTnOE;
4372 high_output |= nTRST;
4375 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4376 high_output &= ~nSRSTnOE;
4377 high_output |= nSRST;
4379 high_output |= nSRSTnOE;
4380 high_output &= ~nSRST;
4383 /* initialize high byte for jtag */
4384 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4386 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4387 return ERROR_JTAG_INIT_FAILED;
4393 static void ktlink_reset(int trst, int srst)
4395 enum reset_types jtag_reset_config = jtag_get_reset_config();
4398 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4399 high_output &= ~nTRSTnOE;
4401 high_output &= ~nTRST;
4402 } else if (trst == 0) {
4403 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4404 high_output |= nTRSTnOE;
4406 high_output |= nTRST;
4410 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4411 high_output &= ~nSRST;
4413 high_output &= ~nSRSTnOE;
4414 } else if (srst == 0) {
4415 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4416 high_output |= nSRST;
4418 high_output |= nSRSTnOE;
4421 buffer_write(0x82); // command "set data bits high byte"
4422 buffer_write(high_output);
4423 buffer_write(high_direction);
4424 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4427 static void ktlink_blink(void)
4429 /* LED connected to ACBUS7 */
4430 high_output ^= 0x80;
4432 buffer_write(0x82); // command "set data bits high byte"
4433 buffer_write(high_output);
4434 buffer_write(high_direction);
4437 static const struct command_registration ft2232_command_handlers[] = {
4439 .name = "ft2232_device_desc",
4440 .handler = &ft2232_handle_device_desc_command,
4441 .mode = COMMAND_CONFIG,
4442 .help = "set the USB device description of the FTDI FT2232 device",
4443 .usage = "description_string",
4446 .name = "ft2232_serial",
4447 .handler = &ft2232_handle_serial_command,
4448 .mode = COMMAND_CONFIG,
4449 .help = "set the serial number of the FTDI FT2232 device",
4450 .usage = "serial_string",
4453 .name = "ft2232_layout",
4454 .handler = &ft2232_handle_layout_command,
4455 .mode = COMMAND_CONFIG,
4456 .help = "set the layout of the FT2232 GPIO signals used "
4457 "to control output-enables and reset signals",
4458 .usage = "layout_name",
4461 .name = "ft2232_vid_pid",
4462 .handler = &ft2232_handle_vid_pid_command,
4463 .mode = COMMAND_CONFIG,
4464 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4465 .usage = "(vid pid)* ",
4468 .name = "ft2232_latency",
4469 .handler = &ft2232_handle_latency_command,
4470 .mode = COMMAND_CONFIG,
4471 .help = "set the FT2232 latency timer to a new value",
4474 COMMAND_REGISTRATION_DONE
4477 struct jtag_interface ft2232_interface = {
4479 .supported = DEBUG_CAP_TMS_SEQ,
4480 .commands = ft2232_command_handlers,
4481 .transports = jtag_only,
4483 .init = ft2232_init,
4484 .quit = ft2232_quit,
4485 .speed = ft2232_speed,
4486 .speed_div = ft2232_speed_div,
4488 .execute_queue = ft2232_execute_queue,