1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <jtag/transport.h>
85 #include <helper/time_support.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
154 static char * ft2232_device_desc_A = NULL;
155 static char* ft2232_device_desc = NULL;
156 static char* ft2232_serial = NULL;
157 static uint8_t ft2232_latency = 2;
158 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165 struct ft2232_layout {
168 void (*reset)(int trst, int srst);
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter_init(void);
180 static int turtle_init(void);
181 static int comstick_init(void);
182 static int stm32stick_init(void);
183 static int axm0432_jtag_init(void);
184 static int sheevaplug_init(void);
185 static int icebear_jtag_init(void);
186 static int cortino_jtag_init(void);
187 static int signalyzer_init(void);
188 static int signalyzer_h_init(void);
189 static int ktlink_init(void);
190 static int redbee_init(void);
191 static int lisa_l_init(void);
192 static int flossjtag_init(void);
194 /* reset procedures for supported layouts */
195 static void ftx23_reset(int trst, int srst);
196 static void jtagkey_reset(int trst, int srst);
197 static void olimex_jtag_reset(int trst, int srst);
198 static void flyswatter_reset(int trst, int srst);
199 static void turtle_reset(int trst, int srst);
200 static void comstick_reset(int trst, int srst);
201 static void stm32stick_reset(int trst, int srst);
202 static void axm0432_jtag_reset(int trst, int srst);
203 static void sheevaplug_reset(int trst, int srst);
204 static void icebear_jtag_reset(int trst, int srst);
205 static void signalyzer_h_reset(int trst, int srst);
206 static void ktlink_reset(int trst, int srst);
207 static void redbee_reset(int trst, int srst);
209 /* blink procedures for layouts that support a blinking led */
210 static void olimex_jtag_blink(void);
211 static void flyswatter_jtag_blink(void);
212 static void turtle_jtag_blink(void);
213 static void signalyzer_h_blink(void);
214 static void ktlink_blink(void);
215 static void lisa_l_blink(void);
216 static void flossjtag_blink(void);
218 /* common transport support options */
220 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
222 static const struct ft2232_layout ft2232_layouts[] =
225 .init = usbjtag_init,
226 .reset = ftx23_reset,
229 .init = jtagkey_init,
230 .reset = jtagkey_reset,
232 { .name = "jtagkey_prototype_v1",
233 .init = jtagkey_init,
234 .reset = jtagkey_reset,
236 { .name = "oocdlink",
237 .init = jtagkey_init,
238 .reset = jtagkey_reset,
240 { .name = "signalyzer",
241 .init = signalyzer_init,
242 .reset = ftx23_reset,
244 { .name = "evb_lm3s811",
245 .init = lm3s811_jtag_init,
246 .reset = ftx23_reset,
248 { .name = "luminary_icdi",
249 .init = icdi_jtag_init,
250 .reset = ftx23_reset,
252 { .name = "olimex-jtag",
253 .init = olimex_jtag_init,
254 .reset = olimex_jtag_reset,
255 .blink = olimex_jtag_blink
257 { .name = "flyswatter",
258 .init = flyswatter_init,
259 .reset = flyswatter_reset,
260 .blink = flyswatter_jtag_blink
262 { .name = "turtelizer2",
264 .reset = turtle_reset,
265 .blink = turtle_jtag_blink
267 { .name = "comstick",
268 .init = comstick_init,
269 .reset = comstick_reset,
271 { .name = "stm32stick",
272 .init = stm32stick_init,
273 .reset = stm32stick_reset,
275 { .name = "axm0432_jtag",
276 .init = axm0432_jtag_init,
277 .reset = axm0432_jtag_reset,
279 { .name = "sheevaplug",
280 .init = sheevaplug_init,
281 .reset = sheevaplug_reset,
284 .init = icebear_jtag_init,
285 .reset = icebear_jtag_reset,
288 .init = cortino_jtag_init,
289 .reset = comstick_reset,
291 { .name = "signalyzer-h",
292 .init = signalyzer_h_init,
293 .reset = signalyzer_h_reset,
294 .blink = signalyzer_h_blink
298 .reset = ktlink_reset,
299 .blink = ktlink_blink
301 { .name = "redbee-econotag",
303 .reset = redbee_reset,
305 { .name = "redbee-usb",
307 .reset = redbee_reset,
308 .channel = INTERFACE_B,
312 .reset = ftx23_reset,
313 .blink = lisa_l_blink,
314 .channel = INTERFACE_B,
316 { .name = "flossjtag",
317 .init = flossjtag_init,
318 .reset = ftx23_reset,
319 .blink = flossjtag_blink,
321 { .name = NULL, /* END OF TABLE */ },
324 /* bitmask used to drive nTRST; usually a GPIOLx signal */
325 static uint8_t nTRST;
326 static uint8_t nTRSTnOE;
327 /* bitmask used to drive nSRST; usually a GPIOLx signal */
328 static uint8_t nSRST;
329 static uint8_t nSRSTnOE;
331 /** the layout being used with this debug session */
332 static const struct ft2232_layout *layout;
334 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
335 static uint8_t low_output = 0x0;
337 /* note that direction bit == 1 means that signal is an output */
339 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
340 static uint8_t low_direction = 0x0;
341 /** default value bitmask for CBUS GPIOH(0..4) */
342 static uint8_t high_output = 0x0;
343 /** default direction bitmask for CBUS GPIOH(0..4) */
344 static uint8_t high_direction = 0x0;
346 #if BUILD_FT2232_FTD2XX == 1
347 static FT_HANDLE ftdih = NULL;
348 static FT_DEVICE ftdi_device = 0;
349 #elif BUILD_FT2232_LIBFTDI == 1
350 static struct ftdi_context ftdic;
351 static enum ftdi_chip_type ftdi_device;
354 static struct jtag_command* first_unsent; /* next command that has to be sent */
355 static int require_send;
357 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
359 "There is a significant difference between libftdi and libftd2xx. The latter
360 one allows to schedule up to 64*64 bytes of result data while libftdi fails
361 with more than 4*64. As a consequence, the FT2232 driver is forced to
362 perform around 16x more USB transactions for long command streams with TDO
363 capture when running with libftdi."
366 #define FT2232_BUFFER_SIZE 131072
367 a comment would have been nice.
370 #define FT2232_BUFFER_SIZE 131072
372 static uint8_t* ft2232_buffer = NULL;
373 static int ft2232_buffer_size = 0;
374 static int ft2232_read_pointer = 0;
375 static int ft2232_expect_read = 0;
378 * Function buffer_write
379 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
380 * @param val is the byte to send.
382 static inline void buffer_write(uint8_t val)
384 assert(ft2232_buffer);
385 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
386 ft2232_buffer[ft2232_buffer_size++] = val;
390 * Function buffer_read
391 * returns a byte from the byte buffer.
393 static inline uint8_t buffer_read(void)
395 assert(ft2232_buffer);
396 assert(ft2232_read_pointer < ft2232_buffer_size);
397 return ft2232_buffer[ft2232_read_pointer++];
401 * Clocks out \a bit_count bits on the TMS line, starting with the least
402 * significant bit of tms_bits and progressing to more significant bits.
403 * Rigorous state transition logging is done here via tap_set_state().
405 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
406 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
407 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
408 * is often used for this, 0x4b.
410 * @param tms_bits Holds the sequence of bits to send.
411 * @param tms_count Tells how many bits in the sequence.
412 * @param tdi_bit A single bit to pass on to TDI before the first TCK
413 * cycle and held static for the duration of TMS clocking.
415 * See the MPSSE spec referenced above.
417 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
421 int tms_ndx; /* bit index into tms_byte */
423 assert(tms_count > 0);
425 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
426 mpsse_cmd, tms_bits, tms_count);
428 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
430 bool bit = tms_bits & 1;
433 tms_byte |= (1 << tms_ndx);
435 /* always do state transitions in public view */
436 tap_set_state(tap_state_transition(tap_get_state(), bit));
438 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
443 if (tms_ndx == 7 || i == tms_count-1)
445 buffer_write(mpsse_cmd);
446 buffer_write(tms_ndx - 1);
448 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
449 TMS/CS and is held static for the duration of TMS/CS clocking.
451 buffer_write(tms_byte | (tdi_bit << 7));
457 * Function get_tms_buffer_requirements
458 * returns what clock_tms() will consume if called with
461 static inline int get_tms_buffer_requirements(int bit_count)
463 return ((bit_count + 6)/7) * 3;
467 * Function move_to_state
468 * moves the TAP controller from the current state to a
469 * \a goal_state through a path given by tap_get_tms_path(). State transition
470 * logging is performed by delegation to clock_tms().
472 * @param goal_state is the destination state for the move.
474 static void move_to_state(tap_state_t goal_state)
476 tap_state_t start_state = tap_get_state();
478 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
479 lookup of the required TMS pattern to move to this state from the
483 /* do the 2 lookups */
484 int tms_bits = tap_get_tms_path(start_state, goal_state);
485 int tms_count = tap_get_tms_path_len(start_state, goal_state);
487 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
489 clock_tms(0x4b, tms_bits, tms_count, 0);
492 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
494 #if BUILD_FT2232_FTD2XX == 1
496 DWORD dw_bytes_written;
497 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
499 *bytes_written = dw_bytes_written;
500 LOG_ERROR("FT_Write returned: %lu", status);
501 return ERROR_JTAG_DEVICE_ERROR;
505 *bytes_written = dw_bytes_written;
507 #elif BUILD_FT2232_LIBFTDI == 1
509 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
512 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
513 return ERROR_JTAG_DEVICE_ERROR;
517 *bytes_written = retval;
521 if (*bytes_written != (uint32_t)size)
523 return ERROR_JTAG_DEVICE_ERROR;
529 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
531 #if BUILD_FT2232_FTD2XX == 1
537 while ((*bytes_read < size) && timeout--)
539 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
540 *bytes_read, &dw_bytes_read)) != FT_OK)
543 LOG_ERROR("FT_Read returned: %lu", status);
544 return ERROR_JTAG_DEVICE_ERROR;
546 *bytes_read += dw_bytes_read;
549 #elif BUILD_FT2232_LIBFTDI == 1
551 int timeout = LIBFTDI_READ_RETRY_COUNT;
554 while ((*bytes_read < size) && timeout--)
556 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
559 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
560 return ERROR_JTAG_DEVICE_ERROR;
562 *bytes_read += retval;
567 if (*bytes_read < size)
569 LOG_ERROR("couldn't read enough bytes from "
570 "FT2232 device (%i < %i)",
571 (unsigned)*bytes_read,
573 return ERROR_JTAG_DEVICE_ERROR;
579 static bool ft2232_device_is_highspeed(void)
581 #if BUILD_FT2232_FTD2XX == 1
582 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
583 #elif BUILD_FT2232_LIBFTDI == 1
584 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
589 * Commands that only apply to the FT2232H and FT4232H devices.
590 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
591 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
594 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
596 uint8_t buf = enable ? 0x96 : 0x97;
597 LOG_DEBUG("%2.2x", buf);
599 uint32_t bytes_written;
602 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
604 LOG_ERROR("couldn't write command to %s adaptive clocking"
605 , enable ? "enable" : "disable");
613 * Enable/disable the clk divide by 5 of the 60MHz master clock.
614 * This result in a JTAG clock speed range of 91.553Hz-6MHz
615 * respective 457.763Hz-30MHz.
617 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
619 uint32_t bytes_written;
620 uint8_t buf = enable ? 0x8b : 0x8a;
622 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
624 LOG_ERROR("couldn't write command to %s clk divide by 5"
625 , enable ? "enable" : "disable");
626 return ERROR_JTAG_INIT_FAILED;
628 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
629 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
634 static int ft2232_speed(int speed)
638 uint32_t bytes_written;
641 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
642 if (ft2232_device_is_highspeed())
643 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
644 else if (enable_adaptive_clocking)
646 LOG_ERROR("ft2232 device %lu does not support RTCK"
647 , (long unsigned int)ftdi_device);
651 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
654 buf[0] = 0x86; /* command "set divisor" */
655 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
656 buf[2] = (speed >> 8) & 0xff; /* valueH */
658 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
659 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
661 LOG_ERROR("couldn't set FT2232 TCK speed");
668 static int ft2232_speed_div(int speed, int* khz)
670 /* Take a look in the FT2232 manual,
671 * AN2232C-01 Command Processor for
672 * MPSSE and MCU Host Bus. Chapter 3.8 */
674 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
679 static int ft2232_khz(int khz, int* jtag_speed)
683 if (ft2232_device_is_highspeed())
685 *jtag_speed = RTCK_SPEED;
690 LOG_DEBUG("RCLK not supported");
695 /* Take a look in the FT2232 manual,
696 * AN2232C-01 Command Processor for
697 * MPSSE and MCU Host Bus. Chapter 3.8
699 * We will calc here with a multiplier
700 * of 10 for better rounding later. */
702 /* Calc speed, (ft2232_max_tck / khz) - 1 */
703 /* Use 65000 for better rounding */
704 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
706 /* Add 0.9 for rounding */
709 /* Calc real speed */
710 *jtag_speed = *jtag_speed / 10;
712 /* Check if speed is greater than 0 */
718 /* Check max value */
719 if (*jtag_speed > 0xFFFF)
721 *jtag_speed = 0xFFFF;
727 static void ft2232_end_state(tap_state_t state)
729 if (tap_is_state_stable(state))
730 tap_set_end_state(state);
733 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
738 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
740 int num_bytes = (scan_size + 7) / 8;
741 int bits_left = scan_size;
744 while (num_bytes-- > 1)
746 buffer[cur_byte++] = buffer_read();
750 buffer[cur_byte] = 0x0;
752 /* There is one more partial byte left from the clock data in/out instructions */
755 buffer[cur_byte] = buffer_read() >> 1;
757 /* 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 */
758 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
761 static void ft2232_debug_dump_buffer(void)
767 for (i = 0; i < ft2232_buffer_size; i++)
769 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
772 LOG_DEBUG("%s", line);
778 LOG_DEBUG("%s", line);
781 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
783 struct jtag_command* cmd;
788 uint32_t bytes_written = 0;
789 uint32_t bytes_read = 0;
791 #ifdef _DEBUG_USB_IO_
792 struct timeval start, inter, inter2, end;
793 struct timeval d_inter, d_inter2, d_end;
796 #ifdef _DEBUG_USB_COMMS_
797 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
798 ft2232_debug_dump_buffer();
801 #ifdef _DEBUG_USB_IO_
802 gettimeofday(&start, NULL);
805 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
807 LOG_ERROR("couldn't write MPSSE commands to FT2232");
811 #ifdef _DEBUG_USB_IO_
812 gettimeofday(&inter, NULL);
815 if (ft2232_expect_read)
817 /* FIXME this "timeout" is never changed ... */
818 int timeout = LIBFTDI_READ_RETRY_COUNT;
819 ft2232_buffer_size = 0;
821 #ifdef _DEBUG_USB_IO_
822 gettimeofday(&inter2, NULL);
825 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
827 LOG_ERROR("couldn't read from FT2232");
831 #ifdef _DEBUG_USB_IO_
832 gettimeofday(&end, NULL);
834 timeval_subtract(&d_inter, &inter, &start);
835 timeval_subtract(&d_inter2, &inter2, &start);
836 timeval_subtract(&d_end, &end, &start);
838 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
839 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
840 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
841 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
844 ft2232_buffer_size = bytes_read;
846 if (ft2232_expect_read != ft2232_buffer_size)
848 LOG_ERROR("ft2232_expect_read (%i) != "
849 "ft2232_buffer_size (%i) "
853 LIBFTDI_READ_RETRY_COUNT - timeout);
854 ft2232_debug_dump_buffer();
859 #ifdef _DEBUG_USB_COMMS_
860 LOG_DEBUG("read buffer (%i retries): %i bytes",
861 LIBFTDI_READ_RETRY_COUNT - timeout,
863 ft2232_debug_dump_buffer();
867 ft2232_expect_read = 0;
868 ft2232_read_pointer = 0;
870 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
871 * that wasn't handled by a caller-provided error handler
881 type = jtag_scan_type(cmd->cmd.scan);
882 if (type != SCAN_OUT)
884 scan_size = jtag_scan_size(cmd->cmd.scan);
885 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
886 ft2232_read_scan(type, buffer, scan_size);
887 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
888 retval = ERROR_JTAG_QUEUE_FAILED;
900 ft2232_buffer_size = 0;
906 * Function ft2232_add_pathmove
907 * moves the TAP controller from the current state to a new state through the
908 * given path, where path is an array of tap_state_t's.
910 * @param path is an array of tap_stat_t which gives the states to traverse through
911 * ending with the last state at path[num_states-1]
912 * @param num_states is the count of state steps to move through
914 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
918 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
922 /* this loop verifies that the path is legal and logs each state in the path */
925 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
927 int num_states_batch = num_states > 7 ? 7 : num_states;
929 /* command "Clock Data to TMS/CS Pin (no Read)" */
932 /* number of states remaining */
933 buffer_write(num_states_batch - 1);
935 while (num_states_batch--) {
936 /* either TMS=0 or TMS=1 must work ... */
937 if (tap_state_transition(tap_get_state(), false)
938 == path[state_count])
939 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
940 else if (tap_state_transition(tap_get_state(), true)
941 == path[state_count])
942 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
944 /* ... or else the caller goofed BADLY */
946 LOG_ERROR("BUG: %s -> %s isn't a valid "
947 "TAP state transition",
948 tap_state_name(tap_get_state()),
949 tap_state_name(path[state_count]));
953 tap_set_state(path[state_count]);
958 buffer_write(tms_byte);
960 tap_set_end_state(tap_get_state());
963 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
965 int num_bytes = (scan_size + 7) / 8;
966 int bits_left = scan_size;
972 if (tap_get_state() != TAP_DRSHIFT)
974 move_to_state(TAP_DRSHIFT);
979 if (tap_get_state() != TAP_IRSHIFT)
981 move_to_state(TAP_IRSHIFT);
985 /* add command for complete bytes */
986 while (num_bytes > 1)
991 /* Clock Data Bytes In and Out LSB First */
993 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
995 else if (type == SCAN_OUT)
997 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
999 /* LOG_DEBUG("added TDI bytes (o)"); */
1001 else if (type == SCAN_IN)
1003 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1005 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1008 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1009 num_bytes -= thisrun_bytes;
1011 buffer_write((uint8_t) (thisrun_bytes - 1));
1012 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1014 if (type != SCAN_IN)
1016 /* add complete bytes */
1017 while (thisrun_bytes-- > 0)
1019 buffer_write(buffer[cur_byte++]);
1023 else /* (type == SCAN_IN) */
1025 bits_left -= 8 * (thisrun_bytes);
1029 /* the most signifcant bit is scanned during TAP movement */
1030 if (type != SCAN_IN)
1031 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1035 /* process remaining bits but the last one */
1038 if (type == SCAN_IO)
1040 /* Clock Data Bits In and Out LSB First */
1042 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1044 else if (type == SCAN_OUT)
1046 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1048 /* LOG_DEBUG("added TDI bits (o)"); */
1050 else if (type == SCAN_IN)
1052 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1054 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1057 buffer_write(bits_left - 2);
1058 if (type != SCAN_IN)
1059 buffer_write(buffer[cur_byte]);
1062 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1063 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1065 if (type == SCAN_IO)
1067 /* Clock Data Bits In and Out LSB First */
1069 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1071 else if (type == SCAN_OUT)
1073 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1075 /* LOG_DEBUG("added TDI bits (o)"); */
1077 else if (type == SCAN_IN)
1079 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1081 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1084 buffer_write(last_bit);
1092 /* move from Shift-IR/DR to end state */
1093 if (type != SCAN_OUT)
1095 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1096 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1099 /* Clock Data to TMS/CS Pin with Read */
1104 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1105 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1106 /* Clock Data to TMS/CS Pin (no Read) */
1110 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1111 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1114 if (tap_get_state() != tap_get_end_state())
1116 move_to_state(tap_get_end_state());
1120 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1122 int num_bytes = (scan_size + 7) / 8;
1123 int bits_left = scan_size;
1126 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1127 uint8_t* receive_pointer = receive_buffer;
1128 uint32_t bytes_written;
1129 uint32_t bytes_read;
1131 int thisrun_read = 0;
1135 LOG_ERROR("BUG: large IR scans are not supported");
1139 if (tap_get_state() != TAP_DRSHIFT)
1141 move_to_state(TAP_DRSHIFT);
1144 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1146 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1149 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1150 ft2232_buffer_size, (int)bytes_written);
1151 ft2232_buffer_size = 0;
1153 /* add command for complete bytes */
1154 while (num_bytes > 1)
1158 if (type == SCAN_IO)
1160 /* Clock Data Bytes In and Out LSB First */
1162 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1164 else if (type == SCAN_OUT)
1166 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1168 /* LOG_DEBUG("added TDI bytes (o)"); */
1170 else if (type == SCAN_IN)
1172 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1174 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1177 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1178 thisrun_read = thisrun_bytes;
1179 num_bytes -= thisrun_bytes;
1180 buffer_write((uint8_t) (thisrun_bytes - 1));
1181 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1183 if (type != SCAN_IN)
1185 /* add complete bytes */
1186 while (thisrun_bytes-- > 0)
1188 buffer_write(buffer[cur_byte]);
1193 else /* (type == SCAN_IN) */
1195 bits_left -= 8 * (thisrun_bytes);
1198 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1200 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1203 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1205 (int)bytes_written);
1206 ft2232_buffer_size = 0;
1208 if (type != SCAN_OUT)
1210 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1212 LOG_ERROR("couldn't read from FT2232");
1215 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1218 receive_pointer += bytes_read;
1224 /* the most signifcant bit is scanned during TAP movement */
1225 if (type != SCAN_IN)
1226 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1230 /* process remaining bits but the last one */
1233 if (type == SCAN_IO)
1235 /* Clock Data Bits In and Out LSB First */
1237 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1239 else if (type == SCAN_OUT)
1241 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1243 /* LOG_DEBUG("added TDI bits (o)"); */
1245 else if (type == SCAN_IN)
1247 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1249 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1251 buffer_write(bits_left - 2);
1252 if (type != SCAN_IN)
1253 buffer_write(buffer[cur_byte]);
1255 if (type != SCAN_OUT)
1259 if (tap_get_end_state() == TAP_DRSHIFT)
1261 if (type == SCAN_IO)
1263 /* Clock Data Bits In and Out LSB First */
1265 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1267 else if (type == SCAN_OUT)
1269 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1271 /* LOG_DEBUG("added TDI bits (o)"); */
1273 else if (type == SCAN_IN)
1275 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1277 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1280 buffer_write(last_bit);
1284 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1285 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1288 /* move from Shift-IR/DR to end state */
1289 if (type != SCAN_OUT)
1291 /* Clock Data to TMS/CS Pin with Read */
1293 /* LOG_DEBUG("added TMS scan (read)"); */
1297 /* Clock Data to TMS/CS Pin (no Read) */
1299 /* LOG_DEBUG("added TMS scan (no read)"); */
1302 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1303 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1306 if (type != SCAN_OUT)
1309 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1311 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1314 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1316 (int)bytes_written);
1317 ft2232_buffer_size = 0;
1319 if (type != SCAN_OUT)
1321 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1323 LOG_ERROR("couldn't read from FT2232");
1326 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1329 receive_pointer += bytes_read;
1335 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1337 int predicted_size = 3;
1338 int num_bytes = (scan_size - 1) / 8;
1340 if (tap_get_state() != TAP_DRSHIFT)
1341 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1343 if (type == SCAN_IN) /* only from device to host */
1345 /* complete bytes */
1346 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1348 /* remaining bits - 1 (up to 7) */
1349 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1351 else /* host to device, or bidirectional */
1353 /* complete bytes */
1354 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1356 /* remaining bits -1 (up to 7) */
1357 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1360 return predicted_size;
1363 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1365 int predicted_size = 0;
1367 if (type != SCAN_OUT)
1369 /* complete bytes */
1370 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1372 /* remaining bits - 1 */
1373 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1375 /* last bit (from TMS scan) */
1376 predicted_size += 1;
1379 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1381 return predicted_size;
1384 /* semi-generic FT2232/FT4232 reset code */
1385 static void ftx23_reset(int trst, int srst)
1387 enum reset_types jtag_reset_config = jtag_get_reset_config();
1390 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1391 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1393 low_output &= ~nTRST; /* switch output low */
1397 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1398 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1400 low_output |= nTRST; /* switch output high */
1405 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1406 low_output &= ~nSRST; /* switch output low */
1408 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1412 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1413 low_output |= nSRST; /* switch output high */
1415 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1418 /* command "set data bits low byte" */
1420 buffer_write(low_output);
1421 buffer_write(low_direction);
1424 static void jtagkey_reset(int trst, int srst)
1426 enum reset_types jtag_reset_config = jtag_get_reset_config();
1429 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1430 high_output &= ~nTRSTnOE;
1432 high_output &= ~nTRST;
1436 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1437 high_output |= nTRSTnOE;
1439 high_output |= nTRST;
1444 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1445 high_output &= ~nSRST;
1447 high_output &= ~nSRSTnOE;
1451 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1452 high_output |= nSRST;
1454 high_output |= nSRSTnOE;
1457 /* command "set data bits high byte" */
1459 buffer_write(high_output);
1460 buffer_write(high_direction);
1461 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1465 static void olimex_jtag_reset(int trst, int srst)
1467 enum reset_types jtag_reset_config = jtag_get_reset_config();
1470 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1471 high_output &= ~nTRSTnOE;
1473 high_output &= ~nTRST;
1477 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1478 high_output |= nTRSTnOE;
1480 high_output |= nTRST;
1485 high_output |= nSRST;
1489 high_output &= ~nSRST;
1492 /* command "set data bits high byte" */
1494 buffer_write(high_output);
1495 buffer_write(high_direction);
1496 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1500 static void axm0432_jtag_reset(int trst, int srst)
1504 tap_set_state(TAP_RESET);
1505 high_output &= ~nTRST;
1509 high_output |= nTRST;
1514 high_output &= ~nSRST;
1518 high_output |= nSRST;
1521 /* command "set data bits low byte" */
1523 buffer_write(high_output);
1524 buffer_write(high_direction);
1525 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1529 static void flyswatter_reset(int trst, int srst)
1533 low_output &= ~nTRST;
1537 low_output |= nTRST;
1542 low_output |= nSRST;
1546 low_output &= ~nSRST;
1549 /* command "set data bits low byte" */
1551 buffer_write(low_output);
1552 buffer_write(low_direction);
1553 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1556 static void turtle_reset(int trst, int srst)
1562 low_output |= nSRST;
1566 low_output &= ~nSRST;
1569 /* command "set data bits low byte" */
1571 buffer_write(low_output);
1572 buffer_write(low_direction);
1573 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1576 static void comstick_reset(int trst, int srst)
1580 high_output &= ~nTRST;
1584 high_output |= nTRST;
1589 high_output &= ~nSRST;
1593 high_output |= nSRST;
1596 /* command "set data bits high byte" */
1598 buffer_write(high_output);
1599 buffer_write(high_direction);
1600 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1604 static void stm32stick_reset(int trst, int srst)
1608 high_output &= ~nTRST;
1612 high_output |= nTRST;
1617 low_output &= ~nSRST;
1621 low_output |= nSRST;
1624 /* command "set data bits low byte" */
1626 buffer_write(low_output);
1627 buffer_write(low_direction);
1629 /* command "set data bits high byte" */
1631 buffer_write(high_output);
1632 buffer_write(high_direction);
1633 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1637 static void sheevaplug_reset(int trst, int srst)
1640 high_output &= ~nTRST;
1642 high_output |= nTRST;
1645 high_output &= ~nSRSTnOE;
1647 high_output |= nSRSTnOE;
1649 /* command "set data bits high byte" */
1651 buffer_write(high_output);
1652 buffer_write(high_direction);
1653 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1656 static void redbee_reset(int trst, int srst)
1660 tap_set_state(TAP_RESET);
1661 high_output &= ~nTRST;
1665 high_output |= nTRST;
1670 high_output &= ~nSRST;
1674 high_output |= nSRST;
1677 /* command "set data bits low byte" */
1679 buffer_write(high_output);
1680 buffer_write(high_direction);
1681 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1682 "high_direction: 0x%2.2x", trst, srst, high_output,
1686 static int ft2232_execute_runtest(struct jtag_command *cmd)
1690 int predicted_size = 0;
1693 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1694 cmd->cmd.runtest->num_cycles,
1695 tap_state_name(cmd->cmd.runtest->end_state));
1697 /* only send the maximum buffer size that FT2232C can handle */
1699 if (tap_get_state() != TAP_IDLE)
1700 predicted_size += 3;
1701 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1702 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1703 predicted_size += 3;
1704 if (tap_get_end_state() != TAP_IDLE)
1705 predicted_size += 3;
1706 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1708 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1709 retval = ERROR_JTAG_QUEUE_FAILED;
1713 if (tap_get_state() != TAP_IDLE)
1715 move_to_state(TAP_IDLE);
1718 i = cmd->cmd.runtest->num_cycles;
1721 /* there are no state transitions in this code, so omit state tracking */
1723 /* command "Clock Data to TMS/CS Pin (no Read)" */
1727 buffer_write((i > 7) ? 6 : (i - 1));
1732 i -= (i > 7) ? 7 : i;
1733 /* LOG_DEBUG("added TMS scan (no read)"); */
1736 ft2232_end_state(cmd->cmd.runtest->end_state);
1738 if (tap_get_state() != tap_get_end_state())
1740 move_to_state(tap_get_end_state());
1744 DEBUG_JTAG_IO("runtest: %i, end in %s",
1745 cmd->cmd.runtest->num_cycles,
1746 tap_state_name(tap_get_end_state()));
1750 static int ft2232_execute_statemove(struct jtag_command *cmd)
1752 int predicted_size = 0;
1753 int retval = ERROR_OK;
1755 DEBUG_JTAG_IO("statemove end in %s",
1756 tap_state_name(cmd->cmd.statemove->end_state));
1758 /* only send the maximum buffer size that FT2232C can handle */
1760 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1762 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1763 retval = ERROR_JTAG_QUEUE_FAILED;
1767 ft2232_end_state(cmd->cmd.statemove->end_state);
1769 /* For TAP_RESET, ignore the current recorded state. It's often
1770 * wrong at server startup, and this transation is critical whenever
1773 if (tap_get_end_state() == TAP_RESET) {
1774 clock_tms(0x4b, 0xff, 5, 0);
1777 /* shortest-path move to desired end state */
1778 } else if (tap_get_state() != tap_get_end_state())
1780 move_to_state(tap_get_end_state());
1788 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1789 * (or SWD) state machine.
1791 static int ft2232_execute_tms(struct jtag_command *cmd)
1793 int retval = ERROR_OK;
1794 unsigned num_bits = cmd->cmd.tms->num_bits;
1795 const uint8_t *bits = cmd->cmd.tms->bits;
1798 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1800 /* only send the maximum buffer size that FT2232C can handle */
1801 count = 3 * DIV_ROUND_UP(num_bits, 4);
1802 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1803 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1804 retval = ERROR_JTAG_QUEUE_FAILED;
1810 /* Shift out in batches of at most 6 bits; there's a report of an
1811 * FT2232 bug in this area, where shifting exactly 7 bits can make
1812 * problems with TMS signaling for the last clock cycle:
1814 * http://developer.intra2net.com/mailarchive/html/
1815 * libftdi/2009/msg00292.html
1817 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1819 * Note that pathmoves in JTAG are not often seven bits, so that
1820 * isn't a particularly likely situation outside of "special"
1821 * signaling such as switching between JTAG and SWD modes.
1824 if (num_bits <= 6) {
1826 buffer_write(num_bits - 1);
1827 buffer_write(*bits & 0x3f);
1831 /* Yes, this is lazy ... we COULD shift out more data
1832 * bits per operation, but doing it in nybbles is easy
1836 buffer_write(*bits & 0xf);
1839 count = (num_bits > 4) ? 4 : num_bits;
1842 buffer_write(count - 1);
1843 buffer_write((*bits >> 4) & 0xf);
1853 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1855 int predicted_size = 0;
1856 int retval = ERROR_OK;
1858 tap_state_t* path = cmd->cmd.pathmove->path;
1859 int num_states = cmd->cmd.pathmove->num_states;
1861 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1862 tap_state_name(tap_get_state()),
1863 tap_state_name(path[num_states-1]));
1865 /* only send the maximum buffer size that FT2232C can handle */
1866 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1867 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1869 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1870 retval = ERROR_JTAG_QUEUE_FAILED;
1876 ft2232_add_pathmove(path, num_states);
1882 static int ft2232_execute_scan(struct jtag_command *cmd)
1885 int scan_size; /* size of IR or DR scan */
1886 int predicted_size = 0;
1887 int retval = ERROR_OK;
1889 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1891 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1893 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1895 predicted_size = ft2232_predict_scan_out(scan_size, type);
1896 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1898 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1899 /* unsent commands before this */
1900 if (first_unsent != cmd)
1901 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1902 retval = ERROR_JTAG_QUEUE_FAILED;
1904 /* current command */
1905 ft2232_end_state(cmd->cmd.scan->end_state);
1906 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1908 first_unsent = cmd->next;
1913 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1915 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1918 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1919 retval = ERROR_JTAG_QUEUE_FAILED;
1923 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1924 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1925 ft2232_end_state(cmd->cmd.scan->end_state);
1926 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1930 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1931 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1932 tap_state_name(tap_get_end_state()));
1937 static int ft2232_execute_reset(struct jtag_command *cmd)
1940 int predicted_size = 0;
1943 DEBUG_JTAG_IO("reset trst: %i srst %i",
1944 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1946 /* only send the maximum buffer size that FT2232C can handle */
1948 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1950 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1951 retval = ERROR_JTAG_QUEUE_FAILED;
1956 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1958 tap_set_state(TAP_RESET);
1961 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1964 DEBUG_JTAG_IO("trst: %i, srst: %i",
1965 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1969 static int ft2232_execute_sleep(struct jtag_command *cmd)
1974 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1976 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1977 retval = ERROR_JTAG_QUEUE_FAILED;
1978 first_unsent = cmd->next;
1979 jtag_sleep(cmd->cmd.sleep->us);
1980 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1982 tap_state_name(tap_get_state()));
1986 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1991 /* this is only allowed while in a stable state. A check for a stable
1992 * state was done in jtag_add_clocks()
1994 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1995 retval = ERROR_JTAG_QUEUE_FAILED;
1996 DEBUG_JTAG_IO("clocks %i while in %s",
1997 cmd->cmd.stableclocks->num_cycles,
1998 tap_state_name(tap_get_state()));
2002 static int ft2232_execute_command(struct jtag_command *cmd)
2008 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2009 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2010 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2011 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2012 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2013 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2014 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2016 retval = ft2232_execute_tms(cmd);
2019 LOG_ERROR("BUG: unknown JTAG command type encountered");
2020 retval = ERROR_JTAG_QUEUE_FAILED;
2026 static int ft2232_execute_queue(void)
2028 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2031 first_unsent = cmd; /* next command that has to be sent */
2034 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2035 * that wasn't handled by a caller-provided error handler
2039 ft2232_buffer_size = 0;
2040 ft2232_expect_read = 0;
2042 /* blink, if the current layout has that feature */
2048 if (ft2232_execute_command(cmd) != ERROR_OK)
2049 retval = ERROR_JTAG_QUEUE_FAILED;
2050 /* Start reading input before FT2232 TX buffer fills up */
2052 if (ft2232_expect_read > 256)
2054 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2055 retval = ERROR_JTAG_QUEUE_FAILED;
2060 if (require_send > 0)
2061 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2062 retval = ERROR_JTAG_QUEUE_FAILED;
2067 #if BUILD_FT2232_FTD2XX == 1
2068 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2072 char SerialNumber[16];
2073 char Description[64];
2074 DWORD openex_flags = 0;
2075 char* openex_string = NULL;
2076 uint8_t latency_timer;
2078 if (layout == NULL) {
2079 LOG_WARNING("No ft2232 layout specified'");
2080 return ERROR_JTAG_INIT_FAILED;
2083 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2086 /* Add non-standard Vid/Pid to the linux driver */
2087 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2089 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2093 if (ft2232_device_desc && ft2232_serial)
2095 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2096 ft2232_device_desc = NULL;
2099 if (ft2232_device_desc)
2101 openex_string = ft2232_device_desc;
2102 openex_flags = FT_OPEN_BY_DESCRIPTION;
2104 else if (ft2232_serial)
2106 openex_string = ft2232_serial;
2107 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2111 LOG_ERROR("neither device description nor serial number specified");
2112 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2114 return ERROR_JTAG_INIT_FAILED;
2117 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2118 if (status != FT_OK) {
2119 /* under Win32, the FTD2XX driver appends an "A" to the end
2120 * of the description, if we tried by the desc, then
2121 * try by the alternate "A" description. */
2122 if (openex_string == ft2232_device_desc) {
2123 /* Try the alternate method. */
2124 openex_string = ft2232_device_desc_A;
2125 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2126 if (status == FT_OK) {
2127 /* yea, the "alternate" method worked! */
2129 /* drat, give the user a meaningfull message.
2130 * telling the use we tried *BOTH* methods. */
2131 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2133 ft2232_device_desc_A);
2138 if (status != FT_OK)
2144 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2146 return ERROR_JTAG_INIT_FAILED;
2148 LOG_ERROR("unable to open ftdi device: %lu", status);
2149 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2150 if (status == FT_OK)
2152 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2155 for (i = 0; i < num_devices; i++)
2156 desc_array[i] = malloc(64);
2158 desc_array[num_devices] = NULL;
2160 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2162 if (status == FT_OK)
2164 LOG_ERROR("ListDevices: %lu", num_devices);
2165 for (i = 0; i < num_devices; i++)
2166 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2169 for (i = 0; i < num_devices; i++)
2170 free(desc_array[i]);
2176 LOG_ERROR("ListDevices: NONE");
2178 return ERROR_JTAG_INIT_FAILED;
2181 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2183 LOG_ERROR("unable to set latency timer: %lu", status);
2184 return ERROR_JTAG_INIT_FAILED;
2187 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2189 LOG_ERROR("unable to get latency timer: %lu", status);
2190 return ERROR_JTAG_INIT_FAILED;
2194 LOG_DEBUG("current latency timer: %i", latency_timer);
2197 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2199 LOG_ERROR("unable to set timeouts: %lu", status);
2200 return ERROR_JTAG_INIT_FAILED;
2203 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2205 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2206 return ERROR_JTAG_INIT_FAILED;
2209 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2211 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2212 return ERROR_JTAG_INIT_FAILED;
2216 static const char* type_str[] =
2217 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2218 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2219 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2220 ? ftdi_device : FT_DEVICE_UNKNOWN;
2221 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2222 LOG_INFO("deviceID: %lu", deviceID);
2223 LOG_INFO("SerialNumber: %s", SerialNumber);
2224 LOG_INFO("Description: %s", Description);
2230 static int ft2232_purge_ftd2xx(void)
2234 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2236 LOG_ERROR("error purging ftd2xx device: %lu", status);
2237 return ERROR_JTAG_INIT_FAILED;
2243 #endif /* BUILD_FT2232_FTD2XX == 1 */
2245 #if BUILD_FT2232_LIBFTDI == 1
2246 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2248 uint8_t latency_timer;
2250 if (layout == NULL) {
2251 LOG_WARNING("No ft2232 layout specified'");
2252 return ERROR_JTAG_INIT_FAILED;
2255 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2256 layout->name, vid, pid);
2258 if (ftdi_init(&ftdic) < 0)
2259 return ERROR_JTAG_INIT_FAILED;
2261 /* default to INTERFACE_A */
2262 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2264 if (ftdi_set_interface(&ftdic, channel) < 0)
2266 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2267 return ERROR_JTAG_INIT_FAILED;
2270 /* context, vendor id, product id */
2271 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2275 LOG_WARNING("unable to open ftdi device (trying more): %s",
2278 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2280 return ERROR_JTAG_INIT_FAILED;
2283 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2284 if (ftdi_usb_reset(&ftdic) < 0)
2286 LOG_ERROR("unable to reset ftdi device");
2287 return ERROR_JTAG_INIT_FAILED;
2290 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2292 LOG_ERROR("unable to set latency timer");
2293 return ERROR_JTAG_INIT_FAILED;
2296 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2298 LOG_ERROR("unable to get latency timer");
2299 return ERROR_JTAG_INIT_FAILED;
2303 LOG_DEBUG("current latency timer: %i", latency_timer);
2306 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2308 ftdi_device = ftdic.type;
2309 static const char* type_str[] =
2310 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2311 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2312 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2313 ? ftdi_device : no_of_known_types;
2314 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2318 static int ft2232_purge_libftdi(void)
2320 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2322 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2323 return ERROR_JTAG_INIT_FAILED;
2329 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2331 static int ft2232_init(void)
2335 uint32_t bytes_written;
2337 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2339 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2343 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2346 if (layout == NULL) {
2347 LOG_WARNING("No ft2232 layout specified'");
2348 return ERROR_JTAG_INIT_FAILED;
2351 for (int i = 0; 1; i++)
2354 * "more indicates that there are more IDs to try, so we should
2355 * not print an error for an ID mismatch (but for anything
2358 * try_more indicates that the error code returned indicates an
2359 * ID mismatch (and nothing else) and that we should proceeed
2360 * with the next ID pair.
2362 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2365 #if BUILD_FT2232_FTD2XX == 1
2366 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2368 #elif BUILD_FT2232_LIBFTDI == 1
2369 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2370 more, &try_more, layout->channel);
2374 if (!more || !try_more)
2378 ft2232_buffer_size = 0;
2379 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2381 if (layout->init() != ERROR_OK)
2382 return ERROR_JTAG_INIT_FAILED;
2384 if (ft2232_device_is_highspeed())
2386 #ifndef BUILD_FT2232_HIGHSPEED
2387 #if BUILD_FT2232_FTD2XX == 1
2388 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2389 #elif BUILD_FT2232_LIBFTDI == 1
2390 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2393 /* make sure the legacy mode is disabled */
2394 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2395 return ERROR_JTAG_INIT_FAILED;
2399 retval = jtag_get_speed(&jtag_speed_var);
2400 if (retval != ERROR_OK)
2402 ft2232_speed(jtag_speed_var);
2404 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2405 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2407 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2408 return ERROR_JTAG_INIT_FAILED;
2411 #if BUILD_FT2232_FTD2XX == 1
2412 return ft2232_purge_ftd2xx();
2413 #elif BUILD_FT2232_LIBFTDI == 1
2414 return ft2232_purge_libftdi();
2420 /** Updates defaults for DBUS signals: the four JTAG signals
2421 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2423 static inline void ftx232_dbus_init(void)
2426 low_direction = 0x0b;
2429 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2430 * the four GPIOL signals. Initialization covers value and direction,
2431 * as customized for each layout.
2433 static int ftx232_dbus_write(void)
2436 uint32_t bytes_written;
2438 enum reset_types jtag_reset_config = jtag_get_reset_config();
2439 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2441 low_direction &= ~nTRSTnOE; /* nTRST input */
2442 low_output &= ~nTRST; /* nTRST = 0 */
2446 low_direction |= nTRSTnOE; /* nTRST output */
2447 low_output |= nTRST; /* nTRST = 1 */
2450 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2452 low_direction |= nSRSTnOE; /* nSRST output */
2453 low_output |= nSRST; /* nSRST = 1 */
2457 low_direction &= ~nSRSTnOE; /* nSRST input */
2458 low_output &= ~nSRST; /* nSRST = 0 */
2461 /* initialize low byte for jtag */
2462 buf[0] = 0x80; /* command "set data bits low byte" */
2463 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2464 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2465 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2467 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2469 LOG_ERROR("couldn't initialize FT2232 DBUS");
2470 return ERROR_JTAG_INIT_FAILED;
2476 static int usbjtag_init(void)
2479 * NOTE: This is now _specific_ to the "usbjtag" layout.
2480 * Don't try cram any more layouts into this.
2489 return ftx232_dbus_write();
2492 static int lm3s811_jtag_init(void)
2496 /* There are multiple revisions of LM3S811 eval boards:
2497 * - Rev B (and older?) boards have no SWO trace support.
2498 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2499 * they should use the "luminary_icdi" layout instead.
2506 low_direction = 0x8b;
2508 return ftx232_dbus_write();
2511 static int icdi_jtag_init(void)
2515 /* Most Luminary eval boards support SWO trace output,
2516 * and should use this "luminary_icdi" layout.
2518 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2519 * to switch between JTAG and SWD, or switch the ft2232 UART
2520 * on the second MPSSE channel/interface (BDBUS)
2521 * between (i) the stellaris UART (on Luminary boards)
2522 * or (ii) SWO trace data (generic).
2524 * We come up in JTAG mode and may switch to SWD later (with
2525 * SWO/trace option if SWD is active).
2532 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2533 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2534 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2537 /* GPIOs on second channel/interface (UART) ... */
2538 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2539 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2540 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2545 nSRSTnOE = ICDI_SRST;
2547 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2548 low_output |= ICDI_JTAG_EN;
2549 low_output &= ~ICDI_DBG_ENn;
2551 return ftx232_dbus_write();
2554 static int signalyzer_init(void)
2562 return ftx232_dbus_write();
2565 static int axm0432_jtag_init(void)
2568 uint32_t bytes_written;
2571 low_direction = 0x2b;
2573 /* initialize low byte for jtag */
2574 buf[0] = 0x80; /* command "set data bits low byte" */
2575 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2576 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2577 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2579 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2581 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2582 return ERROR_JTAG_INIT_FAILED;
2585 if (strcmp(layout->name, "axm0432_jtag") == 0)
2588 nTRSTnOE = 0x0; /* No output enable for TRST*/
2590 nSRSTnOE = 0x0; /* No output enable for SRST*/
2594 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2599 high_direction = 0x0c;
2601 enum reset_types jtag_reset_config = jtag_get_reset_config();
2602 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2604 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2608 high_output |= nTRST;
2611 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2613 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2617 high_output |= nSRST;
2620 /* initialize high port */
2621 buf[0] = 0x82; /* command "set data bits high byte" */
2622 buf[1] = high_output; /* value */
2623 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2624 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2626 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2628 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2629 return ERROR_JTAG_INIT_FAILED;
2635 static int redbee_init(void)
2638 uint32_t bytes_written;
2641 low_direction = 0x2b;
2643 /* initialize low byte for jtag */
2644 /* command "set data bits low byte" */
2646 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2647 buf[2] = low_direction;
2648 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2649 buf[1] = low_output;
2650 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2652 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2654 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2655 return ERROR_JTAG_INIT_FAILED;
2659 nTRSTnOE = 0x0; /* No output enable for TRST*/
2661 nSRSTnOE = 0x0; /* No output enable for SRST*/
2664 high_direction = 0x0c;
2666 enum reset_types jtag_reset_config = jtag_get_reset_config();
2667 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2669 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2673 high_output |= nTRST;
2676 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2678 LOG_ERROR("can't set nSRST to push-pull on redbee");
2682 high_output |= nSRST;
2685 /* initialize high port */
2686 buf[0] = 0x82; /* command "set data bits high byte" */
2687 buf[1] = high_output; /* value */
2688 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2689 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2691 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2693 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2694 return ERROR_JTAG_INIT_FAILED;
2700 static int jtagkey_init(void)
2703 uint32_t bytes_written;
2706 low_direction = 0x1b;
2708 /* initialize low byte for jtag */
2709 buf[0] = 0x80; /* command "set data bits low byte" */
2710 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2711 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2712 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2714 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2716 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2717 return ERROR_JTAG_INIT_FAILED;
2720 if (strcmp(layout->name, "jtagkey") == 0)
2727 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2728 || (strcmp(layout->name, "oocdlink") == 0))
2737 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2742 high_direction = 0x0f;
2744 enum reset_types jtag_reset_config = jtag_get_reset_config();
2745 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2747 high_output |= nTRSTnOE;
2748 high_output &= ~nTRST;
2752 high_output &= ~nTRSTnOE;
2753 high_output |= nTRST;
2756 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2758 high_output &= ~nSRSTnOE;
2759 high_output |= nSRST;
2763 high_output |= nSRSTnOE;
2764 high_output &= ~nSRST;
2767 /* initialize high port */
2768 buf[0] = 0x82; /* command "set data bits high byte" */
2769 buf[1] = high_output; /* value */
2770 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2771 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2773 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2775 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2776 return ERROR_JTAG_INIT_FAILED;
2782 static int olimex_jtag_init(void)
2785 uint32_t bytes_written;
2788 low_direction = 0x1b;
2790 /* initialize low byte for jtag */
2791 buf[0] = 0x80; /* command "set data bits low byte" */
2792 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2793 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2794 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2796 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2798 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2799 return ERROR_JTAG_INIT_FAILED;
2805 nSRSTnOE = 0x00; /* no output enable for nSRST */
2808 high_direction = 0x0f;
2810 enum reset_types jtag_reset_config = jtag_get_reset_config();
2811 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2813 high_output |= nTRSTnOE;
2814 high_output &= ~nTRST;
2818 high_output &= ~nTRSTnOE;
2819 high_output |= nTRST;
2822 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2824 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2828 high_output &= ~nSRST;
2831 /* turn red LED on */
2832 high_output |= 0x08;
2834 /* initialize high port */
2835 buf[0] = 0x82; /* command "set data bits high byte" */
2836 buf[1] = high_output; /* value */
2837 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2838 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2840 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2842 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2843 return ERROR_JTAG_INIT_FAILED;
2849 static int flyswatter_init(void)
2852 uint32_t bytes_written;
2855 low_direction = 0xfb;
2857 /* initialize low byte for jtag */
2858 buf[0] = 0x80; /* command "set data bits low byte" */
2859 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2860 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2861 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2863 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2865 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2866 return ERROR_JTAG_INIT_FAILED;
2870 nTRSTnOE = 0x0; /* not output enable for nTRST */
2872 nSRSTnOE = 0x00; /* no output enable for nSRST */
2875 high_direction = 0x0c;
2877 /* turn red LED3 on, LED2 off */
2878 high_output |= 0x08;
2880 /* initialize high port */
2881 buf[0] = 0x82; /* command "set data bits high byte" */
2882 buf[1] = high_output; /* value */
2883 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2884 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2886 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2888 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2889 return ERROR_JTAG_INIT_FAILED;
2895 static int turtle_init(void)
2898 uint32_t bytes_written;
2901 low_direction = 0x5b;
2903 /* initialize low byte for jtag */
2904 buf[0] = 0x80; /* command "set data bits low byte" */
2905 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2906 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2907 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2909 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2911 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2912 return ERROR_JTAG_INIT_FAILED;
2918 high_direction = 0x0C;
2920 /* initialize high port */
2921 buf[0] = 0x82; /* command "set data bits high byte" */
2922 buf[1] = high_output;
2923 buf[2] = high_direction;
2924 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2926 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2928 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2929 return ERROR_JTAG_INIT_FAILED;
2935 static int comstick_init(void)
2938 uint32_t bytes_written;
2941 low_direction = 0x0b;
2943 /* initialize low byte for jtag */
2944 buf[0] = 0x80; /* command "set data bits low byte" */
2945 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2946 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2947 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2949 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2951 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2952 return ERROR_JTAG_INIT_FAILED;
2956 nTRSTnOE = 0x00; /* no output enable for nTRST */
2958 nSRSTnOE = 0x00; /* no output enable for nSRST */
2961 high_direction = 0x03;
2963 /* initialize high port */
2964 buf[0] = 0x82; /* command "set data bits high byte" */
2965 buf[1] = high_output;
2966 buf[2] = high_direction;
2967 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2969 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2971 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2972 return ERROR_JTAG_INIT_FAILED;
2978 static int stm32stick_init(void)
2981 uint32_t bytes_written;
2984 low_direction = 0x8b;
2986 /* initialize low byte for jtag */
2987 buf[0] = 0x80; /* command "set data bits low byte" */
2988 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2989 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2990 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2992 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2994 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2995 return ERROR_JTAG_INIT_FAILED;
2999 nTRSTnOE = 0x00; /* no output enable for nTRST */
3001 nSRSTnOE = 0x00; /* no output enable for nSRST */
3004 high_direction = 0x03;
3006 /* initialize high port */
3007 buf[0] = 0x82; /* command "set data bits high byte" */
3008 buf[1] = high_output;
3009 buf[2] = high_direction;
3010 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3012 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3014 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3015 return ERROR_JTAG_INIT_FAILED;
3021 static int sheevaplug_init(void)
3024 uint32_t bytes_written;
3027 low_direction = 0x1b;
3029 /* initialize low byte for jtag */
3030 buf[0] = 0x80; /* command "set data bits low byte" */
3031 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3032 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3033 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3035 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3037 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3038 return ERROR_JTAG_INIT_FAILED;
3047 high_direction = 0x0f;
3049 /* nTRST is always push-pull */
3050 high_output &= ~nTRSTnOE;
3051 high_output |= nTRST;
3053 /* nSRST is always open-drain */
3054 high_output |= nSRSTnOE;
3055 high_output &= ~nSRST;
3057 /* initialize high port */
3058 buf[0] = 0x82; /* command "set data bits high byte" */
3059 buf[1] = high_output; /* value */
3060 buf[2] = high_direction; /* all outputs - xRST */
3061 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3063 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3065 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3066 return ERROR_JTAG_INIT_FAILED;
3072 static int cortino_jtag_init(void)
3075 uint32_t bytes_written;
3078 low_direction = 0x1b;
3080 /* initialize low byte for jtag */
3081 buf[0] = 0x80; /* command "set data bits low byte" */
3082 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3083 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3084 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3086 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3088 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3089 return ERROR_JTAG_INIT_FAILED;
3093 nTRSTnOE = 0x00; /* no output enable for nTRST */
3095 nSRSTnOE = 0x00; /* no output enable for nSRST */
3098 high_direction = 0x03;
3100 /* initialize high port */
3101 buf[0] = 0x82; /* command "set data bits high byte" */
3102 buf[1] = high_output;
3103 buf[2] = high_direction;
3104 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3106 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3108 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3109 return ERROR_JTAG_INIT_FAILED;
3115 static int lisa_l_init(void)
3118 uint32_t bytes_written;
3128 high_direction = 0x18;
3130 /* initialize high port */
3131 buf[0] = 0x82; /* command "set data bits high byte" */
3132 buf[1] = high_output;
3133 buf[2] = high_direction;
3134 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3136 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3138 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3139 return ERROR_JTAG_INIT_FAILED;
3142 return ftx232_dbus_write();
3145 static int flossjtag_init(void)
3148 uint32_t bytes_written;
3158 high_direction = 0x18;
3160 /* initialize high port */
3161 buf[0] = 0x82; /* command "set data bits high byte" */
3162 buf[1] = high_output;
3163 buf[2] = high_direction;
3164 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3166 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3168 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3169 return ERROR_JTAG_INIT_FAILED;
3172 return ftx232_dbus_write();
3175 static void olimex_jtag_blink(void)
3177 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3178 * ACBUS3 is bit 3 of the GPIOH port
3180 if (high_output & 0x08)
3182 /* set port pin high */
3183 high_output &= 0x07;
3187 /* set port pin low */
3188 high_output |= 0x08;
3192 buffer_write(high_output);
3193 buffer_write(high_direction);
3196 static void flyswatter_jtag_blink(void)
3199 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3201 high_output ^= 0x0c;
3204 buffer_write(high_output);
3205 buffer_write(high_direction);
3208 static void turtle_jtag_blink(void)
3211 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3213 if (high_output & 0x08)
3223 buffer_write(high_output);
3224 buffer_write(high_direction);
3227 static void lisa_l_blink(void)
3230 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3232 if (high_output & 0x10)
3242 buffer_write(high_output);
3243 buffer_write(high_direction);
3246 static void flossjtag_blink(void)
3249 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3251 if (high_output & 0x10)
3261 buffer_write(high_output);
3262 buffer_write(high_direction);
3265 static int ft2232_quit(void)
3267 #if BUILD_FT2232_FTD2XX == 1
3270 status = FT_Close(ftdih);
3271 #elif BUILD_FT2232_LIBFTDI == 1
3272 ftdi_usb_close(&ftdic);
3274 ftdi_deinit(&ftdic);
3277 free(ft2232_buffer);
3278 ft2232_buffer = NULL;
3283 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3289 ft2232_device_desc = strdup(CMD_ARGV[0]);
3290 cp = strchr(ft2232_device_desc, 0);
3291 /* under Win32, the FTD2XX driver appends an "A" to the end
3292 * of the description, this examines the given desc
3293 * and creates the 'missing' _A or non_A variable. */
3294 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3295 /* it was, so make this the "A" version. */
3296 ft2232_device_desc_A = ft2232_device_desc;
3297 /* and *CREATE* the non-A version. */
3298 strcpy(buf, ft2232_device_desc);
3299 cp = strchr(buf, 0);
3301 ft2232_device_desc = strdup(buf);
3303 /* <space > A not defined
3305 sprintf(buf, "%s A", ft2232_device_desc);
3306 ft2232_device_desc_A = strdup(buf);
3311 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3317 COMMAND_HANDLER(ft2232_handle_serial_command)
3321 ft2232_serial = strdup(CMD_ARGV[0]);
3325 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3331 COMMAND_HANDLER(ft2232_handle_layout_command)
3333 if (CMD_ARGC != 1) {
3334 LOG_ERROR("Need exactly one argument to ft2232_layout");
3339 LOG_ERROR("already specified ft2232_layout %s",
3341 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3346 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3347 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3353 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3357 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3359 if (CMD_ARGC > MAX_USB_IDS * 2)
3361 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3362 "(maximum is %d pairs)", MAX_USB_IDS);
3363 CMD_ARGC = MAX_USB_IDS * 2;
3365 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3367 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3369 return ERROR_COMMAND_SYNTAX_ERROR;
3370 /* remove the incomplete trailing id */
3375 for (i = 0; i < CMD_ARGC; i += 2)
3377 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3378 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3382 * Explicitly terminate, in case there are multiples instances of
3385 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3390 COMMAND_HANDLER(ft2232_handle_latency_command)
3394 ft2232_latency = atoi(CMD_ARGV[0]);
3398 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3404 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3408 /* 7 bits of either ones or zeros. */
3409 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3411 while (num_cycles > 0)
3413 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3414 * at most 7 bits per invocation. Here we invoke it potentially
3417 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3419 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3421 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3422 retval = ERROR_JTAG_QUEUE_FAILED;
3427 /* there are no state transitions in this code, so omit state tracking */
3429 /* command "Clock Data to TMS/CS Pin (no Read)" */
3433 buffer_write(bitcount_per_command - 1);
3435 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3440 num_cycles -= bitcount_per_command;
3446 /* ---------------------------------------------------------------------
3447 * Support for IceBear JTAG adapter from Section5:
3448 * http://section5.ch/icebear
3450 * Author: Sten, debian@sansys-electronic.com
3453 /* Icebear pin layout
3455 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3456 * GND GND | 4 3| n.c.
3457 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3458 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3459 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3460 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3461 * ADBUS2 TDO |14 13| GND GND
3463 * ADBUS0 O L TCK ACBUS0 GND
3464 * ADBUS1 O L TDI ACBUS1 GND
3465 * ADBUS2 I TDO ACBUS2 n.c.
3466 * ADBUS3 O H TMS ACBUS3 n.c.
3472 static int icebear_jtag_init(void) {
3474 uint32_t bytes_written;
3476 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3477 low_output = 0x08; /* high: TMS; low: TCK TDI */
3481 enum reset_types jtag_reset_config = jtag_get_reset_config();
3482 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3483 low_direction &= ~nTRST; /* nTRST high impedance */
3486 low_direction |= nTRST;
3487 low_output |= nTRST;
3490 low_direction |= nSRST;
3491 low_output |= nSRST;
3493 /* initialize low byte for jtag */
3494 buf[0] = 0x80; /* command "set data bits low byte" */
3495 buf[1] = low_output;
3496 buf[2] = low_direction;
3497 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3499 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3500 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3501 return ERROR_JTAG_INIT_FAILED;
3505 high_direction = 0x00;
3508 /* initialize high port */
3509 buf[0] = 0x82; /* command "set data bits high byte" */
3510 buf[1] = high_output; /* value */
3511 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3512 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3514 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3515 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3516 return ERROR_JTAG_INIT_FAILED;
3522 static void icebear_jtag_reset(int trst, int srst) {
3525 low_direction |= nTRST;
3526 low_output &= ~nTRST;
3528 else if (trst == 0) {
3529 enum reset_types jtag_reset_config = jtag_get_reset_config();
3530 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3531 low_direction &= ~nTRST;
3533 low_output |= nTRST;
3537 low_output &= ~nSRST;
3539 else if (srst == 0) {
3540 low_output |= nSRST;
3543 /* command "set data bits low byte" */
3545 buffer_write(low_output);
3546 buffer_write(low_direction);
3548 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3551 /* ---------------------------------------------------------------------
3552 * Support for Signalyzer H2 and Signalyzer H4
3553 * JTAG adapter from Xverve Technologies Inc.
3554 * http://www.signalyzer.com or http://www.xverve.com
3556 * Author: Oleg Seiljus, oleg@signalyzer.com
3558 static unsigned char signalyzer_h_side;
3559 static unsigned int signalyzer_h_adapter_type;
3561 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3563 #if BUILD_FT2232_FTD2XX == 1
3564 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3567 #define SIGNALYZER_COMMAND_ADDR 128
3568 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3570 #define SIGNALYZER_COMMAND_VERSION 0x41
3571 #define SIGNALYZER_COMMAND_RESET 0x42
3572 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3573 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3574 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3575 #define SIGNALYZER_COMMAND_LED_SET 0x53
3576 #define SIGNALYZER_COMMAND_ADC 0x54
3577 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3578 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3579 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3580 #define SIGNALYZER_COMMAND_I2C 0x58
3582 #define SIGNALYZER_CHAN_A 1
3583 #define SIGNALYZER_CHAN_B 2
3584 /* LEDS use channel C */
3585 #define SIGNALYZER_CHAN_C 4
3587 #define SIGNALYZER_LED_GREEN 1
3588 #define SIGNALYZER_LED_RED 2
3590 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3591 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3592 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3593 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3594 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3597 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3599 #if BUILD_FT2232_FTD2XX == 1
3600 return FT_WriteEE(ftdih, address, value);
3601 #elif BUILD_FT2232_LIBFTDI == 1
3606 #if BUILD_FT2232_FTD2XX == 1
3607 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3609 return FT_ReadEE(ftdih, address, value);
3613 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3614 int on_time_ms, int off_time_ms, unsigned char cycles)
3616 unsigned char on_time;
3617 unsigned char off_time;
3619 if (on_time_ms < 0xFFFF)
3620 on_time = (unsigned char)(on_time_ms / 62);
3624 off_time = (unsigned char)(off_time_ms / 62);
3626 #if BUILD_FT2232_FTD2XX == 1
3629 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3630 ((uint32_t)(channel << 8) | led))) != FT_OK)
3632 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3633 return ERROR_JTAG_DEVICE_ERROR;
3636 if ((status = signalyzer_h_ctrl_write(
3637 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3638 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3640 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3641 return ERROR_JTAG_DEVICE_ERROR;
3644 if ((status = signalyzer_h_ctrl_write(
3645 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3646 ((uint32_t)cycles))) != FT_OK)
3648 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3649 return ERROR_JTAG_DEVICE_ERROR;
3652 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3653 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3655 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3656 return ERROR_JTAG_DEVICE_ERROR;
3660 #elif BUILD_FT2232_LIBFTDI == 1
3663 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3664 ((uint32_t)(channel << 8) | led))) < 0)
3666 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3667 ftdi_get_error_string(&ftdic));
3668 return ERROR_JTAG_DEVICE_ERROR;
3671 if ((retval = signalyzer_h_ctrl_write(
3672 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3673 ((uint32_t)(on_time << 8) | off_time))) < 0)
3675 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3676 ftdi_get_error_string(&ftdic));
3677 return ERROR_JTAG_DEVICE_ERROR;
3680 if ((retval = signalyzer_h_ctrl_write(
3681 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3682 (uint32_t)cycles)) < 0)
3684 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3685 ftdi_get_error_string(&ftdic));
3686 return ERROR_JTAG_DEVICE_ERROR;
3689 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3690 SIGNALYZER_COMMAND_LED_SET)) < 0)
3692 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3693 ftdi_get_error_string(&ftdic));
3694 return ERROR_JTAG_DEVICE_ERROR;
3701 static int signalyzer_h_init(void)
3703 #if BUILD_FT2232_FTD2XX == 1
3710 uint16_t read_buf[12] = { 0 };
3712 uint32_t bytes_written;
3714 /* turn on center green led */
3715 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3716 0xFFFF, 0x00, 0x00);
3718 /* determine what channel config wants to open
3719 * TODO: change me... current implementation is made to work
3720 * with openocd description parsing.
3722 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3726 signalyzer_h_side = *(end_of_desc - 1);
3727 if (signalyzer_h_side == 'B')
3728 signalyzer_h_side = SIGNALYZER_CHAN_B;
3730 signalyzer_h_side = SIGNALYZER_CHAN_A;
3734 LOG_ERROR("No Channel was specified");
3738 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3741 #if BUILD_FT2232_FTD2XX == 1
3742 /* read signalyzer versionining information */
3743 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3744 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3746 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3747 return ERROR_JTAG_DEVICE_ERROR;
3750 for (i = 0; i < 10; i++)
3752 if ((status = signalyzer_h_ctrl_read(
3753 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3754 &read_buf[i])) != FT_OK)
3756 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3758 return ERROR_JTAG_DEVICE_ERROR;
3762 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3763 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3764 read_buf[4], read_buf[5], read_buf[6]);
3766 /* set gpio register */
3767 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3768 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3770 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3771 return ERROR_JTAG_DEVICE_ERROR;
3774 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3777 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3778 return ERROR_JTAG_DEVICE_ERROR;
3781 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3782 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3784 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3785 return ERROR_JTAG_DEVICE_ERROR;
3788 /* read adapter type information */
3789 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3790 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3792 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3793 return ERROR_JTAG_DEVICE_ERROR;
3796 if ((status = signalyzer_h_ctrl_write(
3797 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3799 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3800 return ERROR_JTAG_DEVICE_ERROR;
3803 if ((status = signalyzer_h_ctrl_write(
3804 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3806 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3807 return ERROR_JTAG_DEVICE_ERROR;
3810 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3811 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3813 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3814 return ERROR_JTAG_DEVICE_ERROR;
3819 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3820 &read_buf[0])) != FT_OK)
3822 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3823 return ERROR_JTAG_DEVICE_ERROR;
3826 if (read_buf[0] != 0x0498)
3827 signalyzer_h_adapter_type = 0x0000;
3830 for (i = 0; i < 4; i++)
3832 if ((status = signalyzer_h_ctrl_read(
3833 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3834 &read_buf[i])) != FT_OK)
3836 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3838 return ERROR_JTAG_DEVICE_ERROR;
3842 signalyzer_h_adapter_type = read_buf[0];
3845 #elif BUILD_FT2232_LIBFTDI == 1
3846 /* currently libftdi does not allow reading individual eeprom
3847 * locations, therefore adapter type cannot be detected.
3848 * override with most common type
3850 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3853 enum reset_types jtag_reset_config = jtag_get_reset_config();
3855 /* ADAPTOR: EM_LT16_A */
3856 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3858 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3859 "detected. (HW: %2x).", (read_buf[1] >> 8));
3867 low_direction = 0x1b;
3870 high_direction = 0x0;
3872 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3874 low_direction &= ~nTRSTnOE; /* nTRST input */
3875 low_output &= ~nTRST; /* nTRST = 0 */
3879 low_direction |= nTRSTnOE; /* nTRST output */
3880 low_output |= nTRST; /* nTRST = 1 */
3883 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3885 low_direction |= nSRSTnOE; /* nSRST output */
3886 low_output |= nSRST; /* nSRST = 1 */
3890 low_direction &= ~nSRSTnOE; /* nSRST input */
3891 low_output &= ~nSRST; /* nSRST = 0 */
3894 #if BUILD_FT2232_FTD2XX == 1
3895 /* enable power to the module */
3896 if ((status = signalyzer_h_ctrl_write(
3897 SIGNALYZER_DATA_BUFFER_ADDR,
3898 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3901 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3903 return ERROR_JTAG_DEVICE_ERROR;
3906 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3907 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3909 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3911 return ERROR_JTAG_DEVICE_ERROR;
3914 /* set gpio mode register */
3915 if ((status = signalyzer_h_ctrl_write(
3916 SIGNALYZER_DATA_BUFFER_ADDR,
3917 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3919 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3921 return ERROR_JTAG_DEVICE_ERROR;
3924 if ((status = signalyzer_h_ctrl_write(
3925 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3928 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3930 return ERROR_JTAG_DEVICE_ERROR;
3933 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3934 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3936 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3938 return ERROR_JTAG_DEVICE_ERROR;
3941 /* set gpio register */
3942 if ((status = signalyzer_h_ctrl_write(
3943 SIGNALYZER_DATA_BUFFER_ADDR,
3944 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3946 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3948 return ERROR_JTAG_DEVICE_ERROR;
3951 if ((status = signalyzer_h_ctrl_write(
3952 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3955 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3957 return ERROR_JTAG_DEVICE_ERROR;
3960 if ((status = signalyzer_h_ctrl_write(
3961 SIGNALYZER_COMMAND_ADDR,
3962 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3964 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3966 return ERROR_JTAG_DEVICE_ERROR;
3971 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3972 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3973 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3974 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3975 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3977 if (signalyzer_h_adapter_type
3978 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3979 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3980 "detected. (HW: %2x).", (read_buf[1] >> 8));
3981 else if (signalyzer_h_adapter_type
3982 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3983 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3984 "(ARM JTAG with PSU) detected. (HW: %2x).",
3985 (read_buf[1] >> 8));
3986 else if (signalyzer_h_adapter_type
3987 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3988 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3989 "detected. (HW: %2x).", (read_buf[1] >> 8));
3990 else if (signalyzer_h_adapter_type
3991 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3992 LOG_INFO("Signalyzer: EM-JTAG-P "
3993 "(Generic JTAG with PSU) detected. (HW: %2x).",
3994 (read_buf[1] >> 8));
4002 low_direction = 0x1b;
4005 high_direction = 0x1f;
4007 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4009 high_output |= nTRSTnOE;
4010 high_output &= ~nTRST;
4014 high_output &= ~nTRSTnOE;
4015 high_output |= nTRST;
4018 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4020 high_output &= ~nSRSTnOE;
4021 high_output |= nSRST;
4025 high_output |= nSRSTnOE;
4026 high_output &= ~nSRST;
4029 #if BUILD_FT2232_FTD2XX == 1
4030 /* enable power to the module */
4031 if ((status = signalyzer_h_ctrl_write(
4032 SIGNALYZER_DATA_BUFFER_ADDR,
4033 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4036 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4038 return ERROR_JTAG_DEVICE_ERROR;
4041 if ((status = signalyzer_h_ctrl_write(
4042 SIGNALYZER_COMMAND_ADDR,
4043 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4045 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4047 return ERROR_JTAG_DEVICE_ERROR;
4050 /* set gpio mode register (IO_16 and IO_17 set as analog
4051 * inputs, other is gpio)
4053 if ((status = signalyzer_h_ctrl_write(
4054 SIGNALYZER_DATA_BUFFER_ADDR,
4055 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4057 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4059 return ERROR_JTAG_DEVICE_ERROR;
4062 if ((status = signalyzer_h_ctrl_write(
4063 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4066 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4068 return ERROR_JTAG_DEVICE_ERROR;
4071 if ((status = signalyzer_h_ctrl_write(
4072 SIGNALYZER_COMMAND_ADDR,
4073 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4075 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4077 return ERROR_JTAG_DEVICE_ERROR;
4080 /* set gpio register (all inputs, for -P modules,
4081 * PSU will be turned off)
4083 if ((status = signalyzer_h_ctrl_write(
4084 SIGNALYZER_DATA_BUFFER_ADDR,
4085 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4087 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4089 return ERROR_JTAG_DEVICE_ERROR;
4092 if ((status = signalyzer_h_ctrl_write(
4093 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4096 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4098 return ERROR_JTAG_DEVICE_ERROR;
4101 if ((status = signalyzer_h_ctrl_write(
4102 SIGNALYZER_COMMAND_ADDR,
4103 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4105 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4107 return ERROR_JTAG_DEVICE_ERROR;
4112 else if (signalyzer_h_adapter_type == 0x0000)
4114 LOG_INFO("Signalyzer: No external modules were detected.");
4122 low_direction = 0x1b;
4125 high_direction = 0x0;
4127 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4129 low_direction &= ~nTRSTnOE; /* nTRST input */
4130 low_output &= ~nTRST; /* nTRST = 0 */
4134 low_direction |= nTRSTnOE; /* nTRST output */
4135 low_output |= nTRST; /* nTRST = 1 */
4138 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4140 low_direction |= nSRSTnOE; /* nSRST output */
4141 low_output |= nSRST; /* nSRST = 1 */
4145 low_direction &= ~nSRSTnOE; /* nSRST input */
4146 low_output &= ~nSRST; /* nSRST = 0 */
4151 LOG_ERROR("Unknown module type is detected: %.4x",
4152 signalyzer_h_adapter_type);
4153 return ERROR_JTAG_DEVICE_ERROR;
4156 /* initialize low byte of controller for jtag operation */
4158 buf[1] = low_output;
4159 buf[2] = low_direction;
4161 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4163 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4164 return ERROR_JTAG_INIT_FAILED;
4167 #if BUILD_FT2232_FTD2XX == 1
4168 if (ftdi_device == FT_DEVICE_2232H)
4170 /* initialize high byte of controller for jtag operation */
4172 buf[1] = high_output;
4173 buf[2] = high_direction;
4175 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4177 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4178 return ERROR_JTAG_INIT_FAILED;
4181 #elif BUILD_FT2232_LIBFTDI == 1
4182 if (ftdi_device == TYPE_2232H)
4184 /* initialize high byte of controller for jtag operation */
4186 buf[1] = high_output;
4187 buf[2] = high_direction;
4189 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4191 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4192 return ERROR_JTAG_INIT_FAILED;
4199 static void signalyzer_h_reset(int trst, int srst)
4201 enum reset_types jtag_reset_config = jtag_get_reset_config();
4203 /* ADAPTOR: EM_LT16_A */
4204 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4208 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4209 /* switch to output pin (output is low) */
4210 low_direction |= nTRSTnOE;
4212 /* switch output low */
4213 low_output &= ~nTRST;
4217 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4218 /* switch to input pin (high-Z + internal
4219 * and external pullup) */
4220 low_direction &= ~nTRSTnOE;
4222 /* switch output high */
4223 low_output |= nTRST;
4228 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4229 /* switch output low */
4230 low_output &= ~nSRST;
4232 /* switch to output pin (output is low) */
4233 low_direction |= nSRSTnOE;
4237 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4238 /* switch output high */
4239 low_output |= nSRST;
4241 /* switch to input pin (high-Z) */
4242 low_direction &= ~nSRSTnOE;
4245 /* command "set data bits low byte" */
4247 buffer_write(low_output);
4248 buffer_write(low_direction);
4249 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4250 "low_direction: 0x%2.2x",
4251 trst, srst, low_output, low_direction);
4253 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4254 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4255 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4256 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4257 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4261 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4262 high_output &= ~nTRSTnOE;
4264 high_output &= ~nTRST;
4268 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4269 high_output |= nTRSTnOE;
4271 high_output |= nTRST;
4276 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4277 high_output &= ~nSRST;
4279 high_output &= ~nSRSTnOE;
4283 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4284 high_output |= nSRST;
4286 high_output |= nSRSTnOE;
4289 /* command "set data bits high byte" */
4291 buffer_write(high_output);
4292 buffer_write(high_direction);
4293 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4294 "high_direction: 0x%2.2x",
4295 trst, srst, high_output, high_direction);
4297 else if (signalyzer_h_adapter_type == 0x0000)
4301 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4302 /* switch to output pin (output is low) */
4303 low_direction |= nTRSTnOE;
4305 /* switch output low */
4306 low_output &= ~nTRST;
4310 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4311 /* switch to input pin (high-Z + internal
4312 * and external pullup) */
4313 low_direction &= ~nTRSTnOE;
4315 /* switch output high */
4316 low_output |= nTRST;
4321 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4322 /* switch output low */
4323 low_output &= ~nSRST;
4325 /* switch to output pin (output is low) */
4326 low_direction |= nSRSTnOE;
4330 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4331 /* switch output high */
4332 low_output |= nSRST;
4334 /* switch to input pin (high-Z) */
4335 low_direction &= ~nSRSTnOE;
4338 /* command "set data bits low byte" */
4340 buffer_write(low_output);
4341 buffer_write(low_direction);
4342 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4343 "low_direction: 0x%2.2x",
4344 trst, srst, low_output, low_direction);
4348 static void signalyzer_h_blink(void)
4350 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4353 /********************************************************************
4354 * Support for KT-LINK
4355 * JTAG adapter from KRISTECH
4356 * http://www.kristech.eu
4357 *******************************************************************/
4358 static int ktlink_init(void)
4361 uint32_t bytes_written;
4362 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4364 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4365 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4367 // initialize low port
4368 buf[0] = 0x80; // command "set data bits low byte"
4369 buf[1] = low_output;
4370 buf[2] = low_direction;
4371 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4373 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4375 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4376 return ERROR_JTAG_INIT_FAILED;
4384 high_output = 0x80; // turn LED on
4385 high_direction = 0xFF; // all outputs
4387 enum reset_types jtag_reset_config = jtag_get_reset_config();
4389 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4390 high_output |= nTRSTnOE;
4391 high_output &= ~nTRST;
4393 high_output &= ~nTRSTnOE;
4394 high_output |= nTRST;
4397 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4398 high_output &= ~nSRSTnOE;
4399 high_output |= nSRST;
4401 high_output |= nSRSTnOE;
4402 high_output &= ~nSRST;
4405 // initialize high port
4406 buf[0] = 0x82; // command "set data bits high byte"
4407 buf[1] = high_output; // value
4408 buf[2] = high_direction;
4409 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4411 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4413 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4414 return ERROR_JTAG_INIT_FAILED;
4420 static void ktlink_reset(int trst, int srst)
4422 enum reset_types jtag_reset_config = jtag_get_reset_config();
4425 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4426 high_output &= ~nTRSTnOE;
4428 high_output &= ~nTRST;
4429 } else if (trst == 0) {
4430 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4431 high_output |= nTRSTnOE;
4433 high_output |= nTRST;
4437 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4438 high_output &= ~nSRST;
4440 high_output &= ~nSRSTnOE;
4441 } else if (srst == 0) {
4442 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4443 high_output |= nSRST;
4445 high_output |= nSRSTnOE;
4448 buffer_write(0x82); // command "set data bits high byte"
4449 buffer_write(high_output);
4450 buffer_write(high_direction);
4451 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4454 static void ktlink_blink(void)
4456 /* LED connected to ACBUS7 */
4457 if (high_output & 0x80)
4458 high_output &= 0x7F;
4460 high_output |= 0x80;
4462 buffer_write(0x82); // command "set data bits high byte"
4463 buffer_write(high_output);
4464 buffer_write(high_direction);
4467 static const struct command_registration ft2232_command_handlers[] = {
4469 .name = "ft2232_device_desc",
4470 .handler = &ft2232_handle_device_desc_command,
4471 .mode = COMMAND_CONFIG,
4472 .help = "set the USB device description of the FTDI FT2232 device",
4473 .usage = "description_string",
4476 .name = "ft2232_serial",
4477 .handler = &ft2232_handle_serial_command,
4478 .mode = COMMAND_CONFIG,
4479 .help = "set the serial number of the FTDI FT2232 device",
4480 .usage = "serial_string",
4483 .name = "ft2232_layout",
4484 .handler = &ft2232_handle_layout_command,
4485 .mode = COMMAND_CONFIG,
4486 .help = "set the layout of the FT2232 GPIO signals used "
4487 "to control output-enables and reset signals",
4488 .usage = "layout_name",
4491 .name = "ft2232_vid_pid",
4492 .handler = &ft2232_handle_vid_pid_command,
4493 .mode = COMMAND_CONFIG,
4494 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4495 .usage = "(vid pid)* ",
4498 .name = "ft2232_latency",
4499 .handler = &ft2232_handle_latency_command,
4500 .mode = COMMAND_CONFIG,
4501 .help = "set the FT2232 latency timer to a new value",
4504 COMMAND_REGISTRATION_DONE
4507 struct jtag_interface ft2232_interface = {
4509 .supported = DEBUG_CAP_TMS_SEQ,
4510 .commands = ft2232_command_handlers,
4511 .transports = jtag_only,
4513 .init = ft2232_init,
4514 .quit = ft2232_quit,
4515 .speed = ft2232_speed,
4516 .speed_div = ft2232_speed_div,
4518 .execute_queue = ft2232_execute_queue,