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\n", 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\n");
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;
2398 ft2232_speed(jtag_get_speed());
2400 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2401 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2403 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2404 return ERROR_JTAG_INIT_FAILED;
2407 #if BUILD_FT2232_FTD2XX == 1
2408 return ft2232_purge_ftd2xx();
2409 #elif BUILD_FT2232_LIBFTDI == 1
2410 return ft2232_purge_libftdi();
2416 /** Updates defaults for DBUS signals: the four JTAG signals
2417 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2419 static inline void ftx232_dbus_init(void)
2422 low_direction = 0x0b;
2425 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2426 * the four GPIOL signals. Initialization covers value and direction,
2427 * as customized for each layout.
2429 static int ftx232_dbus_write(void)
2432 uint32_t bytes_written;
2434 enum reset_types jtag_reset_config = jtag_get_reset_config();
2435 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2437 low_direction &= ~nTRSTnOE; /* nTRST input */
2438 low_output &= ~nTRST; /* nTRST = 0 */
2442 low_direction |= nTRSTnOE; /* nTRST output */
2443 low_output |= nTRST; /* nTRST = 1 */
2446 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2448 low_direction |= nSRSTnOE; /* nSRST output */
2449 low_output |= nSRST; /* nSRST = 1 */
2453 low_direction &= ~nSRSTnOE; /* nSRST input */
2454 low_output &= ~nSRST; /* nSRST = 0 */
2457 /* initialize low byte for jtag */
2458 buf[0] = 0x80; /* command "set data bits low byte" */
2459 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2460 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2461 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2463 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2465 LOG_ERROR("couldn't initialize FT2232 DBUS");
2466 return ERROR_JTAG_INIT_FAILED;
2472 static int usbjtag_init(void)
2475 * NOTE: This is now _specific_ to the "usbjtag" layout.
2476 * Don't try cram any more layouts into this.
2485 return ftx232_dbus_write();
2488 static int lm3s811_jtag_init(void)
2492 /* There are multiple revisions of LM3S811 eval boards:
2493 * - Rev B (and older?) boards have no SWO trace support.
2494 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2495 * they should use the "luminary_icdi" layout instead.
2502 low_direction = 0x8b;
2504 return ftx232_dbus_write();
2507 static int icdi_jtag_init(void)
2511 /* Most Luminary eval boards support SWO trace output,
2512 * and should use this "luminary_icdi" layout.
2514 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2515 * to switch between JTAG and SWD, or switch the ft2232 UART
2516 * on the second MPSSE channel/interface (BDBUS)
2517 * between (i) the stellaris UART (on Luminary boards)
2518 * or (ii) SWO trace data (generic).
2520 * We come up in JTAG mode and may switch to SWD later (with
2521 * SWO/trace option if SWD is active).
2528 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2529 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2530 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2533 /* GPIOs on second channel/interface (UART) ... */
2534 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2535 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2536 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2541 nSRSTnOE = ICDI_SRST;
2543 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2544 low_output |= ICDI_JTAG_EN;
2545 low_output &= ~ICDI_DBG_ENn;
2547 return ftx232_dbus_write();
2550 static int signalyzer_init(void)
2558 return ftx232_dbus_write();
2561 static int axm0432_jtag_init(void)
2564 uint32_t bytes_written;
2567 low_direction = 0x2b;
2569 /* initialize low byte for jtag */
2570 buf[0] = 0x80; /* command "set data bits low byte" */
2571 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2572 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2573 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2575 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2577 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2578 return ERROR_JTAG_INIT_FAILED;
2581 if (strcmp(layout->name, "axm0432_jtag") == 0)
2584 nTRSTnOE = 0x0; /* No output enable for TRST*/
2586 nSRSTnOE = 0x0; /* No output enable for SRST*/
2590 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2595 high_direction = 0x0c;
2597 enum reset_types jtag_reset_config = jtag_get_reset_config();
2598 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2600 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2604 high_output |= nTRST;
2607 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2609 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2613 high_output |= nSRST;
2616 /* initialize high port */
2617 buf[0] = 0x82; /* command "set data bits high byte" */
2618 buf[1] = high_output; /* value */
2619 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2620 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2622 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2624 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2625 return ERROR_JTAG_INIT_FAILED;
2631 static int redbee_init(void)
2634 uint32_t bytes_written;
2637 low_direction = 0x2b;
2639 /* initialize low byte for jtag */
2640 /* command "set data bits low byte" */
2642 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2643 buf[2] = low_direction;
2644 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2645 buf[1] = low_output;
2646 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2648 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2650 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2651 return ERROR_JTAG_INIT_FAILED;
2655 nTRSTnOE = 0x0; /* No output enable for TRST*/
2657 nSRSTnOE = 0x0; /* No output enable for SRST*/
2660 high_direction = 0x0c;
2662 enum reset_types jtag_reset_config = jtag_get_reset_config();
2663 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2665 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2669 high_output |= nTRST;
2672 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2674 LOG_ERROR("can't set nSRST to push-pull on redbee");
2678 high_output |= nSRST;
2681 /* initialize high port */
2682 buf[0] = 0x82; /* command "set data bits high byte" */
2683 buf[1] = high_output; /* value */
2684 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2685 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2687 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2689 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2690 return ERROR_JTAG_INIT_FAILED;
2696 static int jtagkey_init(void)
2699 uint32_t bytes_written;
2702 low_direction = 0x1b;
2704 /* initialize low byte for jtag */
2705 buf[0] = 0x80; /* command "set data bits low byte" */
2706 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2707 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2708 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2710 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2712 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2713 return ERROR_JTAG_INIT_FAILED;
2716 if (strcmp(layout->name, "jtagkey") == 0)
2723 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2724 || (strcmp(layout->name, "oocdlink") == 0))
2733 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2738 high_direction = 0x0f;
2740 enum reset_types jtag_reset_config = jtag_get_reset_config();
2741 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2743 high_output |= nTRSTnOE;
2744 high_output &= ~nTRST;
2748 high_output &= ~nTRSTnOE;
2749 high_output |= nTRST;
2752 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2754 high_output &= ~nSRSTnOE;
2755 high_output |= nSRST;
2759 high_output |= nSRSTnOE;
2760 high_output &= ~nSRST;
2763 /* initialize high port */
2764 buf[0] = 0x82; /* command "set data bits high byte" */
2765 buf[1] = high_output; /* value */
2766 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2767 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2769 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2771 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2772 return ERROR_JTAG_INIT_FAILED;
2778 static int olimex_jtag_init(void)
2781 uint32_t bytes_written;
2784 low_direction = 0x1b;
2786 /* initialize low byte for jtag */
2787 buf[0] = 0x80; /* command "set data bits low byte" */
2788 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2789 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2790 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2792 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2794 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2795 return ERROR_JTAG_INIT_FAILED;
2801 nSRSTnOE = 0x00; /* no output enable for nSRST */
2804 high_direction = 0x0f;
2806 enum reset_types jtag_reset_config = jtag_get_reset_config();
2807 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2809 high_output |= nTRSTnOE;
2810 high_output &= ~nTRST;
2814 high_output &= ~nTRSTnOE;
2815 high_output |= nTRST;
2818 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2820 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2824 high_output &= ~nSRST;
2827 /* turn red LED on */
2828 high_output |= 0x08;
2830 /* initialize high port */
2831 buf[0] = 0x82; /* command "set data bits high byte" */
2832 buf[1] = high_output; /* value */
2833 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2834 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2836 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2838 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2839 return ERROR_JTAG_INIT_FAILED;
2845 static int flyswatter_init(void)
2848 uint32_t bytes_written;
2851 low_direction = 0xfb;
2853 /* initialize low byte for jtag */
2854 buf[0] = 0x80; /* command "set data bits low byte" */
2855 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2856 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2857 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2859 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2861 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2862 return ERROR_JTAG_INIT_FAILED;
2866 nTRSTnOE = 0x0; /* not output enable for nTRST */
2868 nSRSTnOE = 0x00; /* no output enable for nSRST */
2871 high_direction = 0x0c;
2873 /* turn red LED3 on, LED2 off */
2874 high_output |= 0x08;
2876 /* initialize high port */
2877 buf[0] = 0x82; /* command "set data bits high byte" */
2878 buf[1] = high_output; /* value */
2879 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2880 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2882 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2884 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2885 return ERROR_JTAG_INIT_FAILED;
2891 static int turtle_init(void)
2894 uint32_t bytes_written;
2897 low_direction = 0x5b;
2899 /* initialize low byte for jtag */
2900 buf[0] = 0x80; /* command "set data bits low byte" */
2901 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2902 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2903 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2905 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2907 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2908 return ERROR_JTAG_INIT_FAILED;
2914 high_direction = 0x0C;
2916 /* initialize high port */
2917 buf[0] = 0x82; /* command "set data bits high byte" */
2918 buf[1] = high_output;
2919 buf[2] = high_direction;
2920 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2922 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2924 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2925 return ERROR_JTAG_INIT_FAILED;
2931 static int comstick_init(void)
2934 uint32_t bytes_written;
2937 low_direction = 0x0b;
2939 /* initialize low byte for jtag */
2940 buf[0] = 0x80; /* command "set data bits low byte" */
2941 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2942 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2943 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2945 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2947 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2948 return ERROR_JTAG_INIT_FAILED;
2952 nTRSTnOE = 0x00; /* no output enable for nTRST */
2954 nSRSTnOE = 0x00; /* no output enable for nSRST */
2957 high_direction = 0x03;
2959 /* initialize high port */
2960 buf[0] = 0x82; /* command "set data bits high byte" */
2961 buf[1] = high_output;
2962 buf[2] = high_direction;
2963 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2965 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2967 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2968 return ERROR_JTAG_INIT_FAILED;
2974 static int stm32stick_init(void)
2977 uint32_t bytes_written;
2980 low_direction = 0x8b;
2982 /* initialize low byte for jtag */
2983 buf[0] = 0x80; /* command "set data bits low byte" */
2984 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2985 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2986 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2988 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2990 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2991 return ERROR_JTAG_INIT_FAILED;
2995 nTRSTnOE = 0x00; /* no output enable for nTRST */
2997 nSRSTnOE = 0x00; /* no output enable for nSRST */
3000 high_direction = 0x03;
3002 /* initialize high port */
3003 buf[0] = 0x82; /* command "set data bits high byte" */
3004 buf[1] = high_output;
3005 buf[2] = high_direction;
3006 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3008 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3010 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3011 return ERROR_JTAG_INIT_FAILED;
3017 static int sheevaplug_init(void)
3020 uint32_t bytes_written;
3023 low_direction = 0x1b;
3025 /* initialize low byte for jtag */
3026 buf[0] = 0x80; /* command "set data bits low byte" */
3027 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3028 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3029 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3031 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3033 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3034 return ERROR_JTAG_INIT_FAILED;
3043 high_direction = 0x0f;
3045 /* nTRST is always push-pull */
3046 high_output &= ~nTRSTnOE;
3047 high_output |= nTRST;
3049 /* nSRST is always open-drain */
3050 high_output |= nSRSTnOE;
3051 high_output &= ~nSRST;
3053 /* initialize high port */
3054 buf[0] = 0x82; /* command "set data bits high byte" */
3055 buf[1] = high_output; /* value */
3056 buf[2] = high_direction; /* all outputs - xRST */
3057 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3059 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3061 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3062 return ERROR_JTAG_INIT_FAILED;
3068 static int cortino_jtag_init(void)
3071 uint32_t bytes_written;
3074 low_direction = 0x1b;
3076 /* initialize low byte for jtag */
3077 buf[0] = 0x80; /* command "set data bits low byte" */
3078 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3079 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3080 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3082 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3084 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3085 return ERROR_JTAG_INIT_FAILED;
3089 nTRSTnOE = 0x00; /* no output enable for nTRST */
3091 nSRSTnOE = 0x00; /* no output enable for nSRST */
3094 high_direction = 0x03;
3096 /* initialize high port */
3097 buf[0] = 0x82; /* command "set data bits high byte" */
3098 buf[1] = high_output;
3099 buf[2] = high_direction;
3100 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3102 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3104 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3105 return ERROR_JTAG_INIT_FAILED;
3111 static int lisa_l_init(void)
3114 uint32_t bytes_written;
3124 high_direction = 0x18;
3126 /* initialize high port */
3127 buf[0] = 0x82; /* command "set data bits high byte" */
3128 buf[1] = high_output;
3129 buf[2] = high_direction;
3130 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3132 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3134 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3135 return ERROR_JTAG_INIT_FAILED;
3138 return ftx232_dbus_write();
3141 static int flossjtag_init(void)
3144 uint32_t bytes_written;
3154 high_direction = 0x18;
3156 /* initialize high port */
3157 buf[0] = 0x82; /* command "set data bits high byte" */
3158 buf[1] = high_output;
3159 buf[2] = high_direction;
3160 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3162 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3164 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3165 return ERROR_JTAG_INIT_FAILED;
3168 return ftx232_dbus_write();
3171 static void olimex_jtag_blink(void)
3173 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3174 * ACBUS3 is bit 3 of the GPIOH port
3176 if (high_output & 0x08)
3178 /* set port pin high */
3179 high_output &= 0x07;
3183 /* set port pin low */
3184 high_output |= 0x08;
3188 buffer_write(high_output);
3189 buffer_write(high_direction);
3192 static void flyswatter_jtag_blink(void)
3195 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3197 high_output ^= 0x0c;
3200 buffer_write(high_output);
3201 buffer_write(high_direction);
3204 static void turtle_jtag_blink(void)
3207 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3209 if (high_output & 0x08)
3219 buffer_write(high_output);
3220 buffer_write(high_direction);
3223 static void lisa_l_blink(void)
3226 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3228 if (high_output & 0x10)
3238 buffer_write(high_output);
3239 buffer_write(high_direction);
3242 static void flossjtag_blink(void)
3245 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3247 if (high_output & 0x10)
3257 buffer_write(high_output);
3258 buffer_write(high_direction);
3261 static int ft2232_quit(void)
3263 #if BUILD_FT2232_FTD2XX == 1
3266 status = FT_Close(ftdih);
3267 #elif BUILD_FT2232_LIBFTDI == 1
3268 ftdi_usb_close(&ftdic);
3270 ftdi_deinit(&ftdic);
3273 free(ft2232_buffer);
3274 ft2232_buffer = NULL;
3279 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3285 ft2232_device_desc = strdup(CMD_ARGV[0]);
3286 cp = strchr(ft2232_device_desc, 0);
3287 /* under Win32, the FTD2XX driver appends an "A" to the end
3288 * of the description, this examines the given desc
3289 * and creates the 'missing' _A or non_A variable. */
3290 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3291 /* it was, so make this the "A" version. */
3292 ft2232_device_desc_A = ft2232_device_desc;
3293 /* and *CREATE* the non-A version. */
3294 strcpy(buf, ft2232_device_desc);
3295 cp = strchr(buf, 0);
3297 ft2232_device_desc = strdup(buf);
3299 /* <space > A not defined
3301 sprintf(buf, "%s A", ft2232_device_desc);
3302 ft2232_device_desc_A = strdup(buf);
3307 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3313 COMMAND_HANDLER(ft2232_handle_serial_command)
3317 ft2232_serial = strdup(CMD_ARGV[0]);
3321 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3327 COMMAND_HANDLER(ft2232_handle_layout_command)
3329 if (CMD_ARGC != 1) {
3330 LOG_ERROR("Need exactly one argument to ft2232_layout");
3335 LOG_ERROR("already specified ft2232_layout %s",
3337 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3342 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3343 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3349 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3353 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3355 if (CMD_ARGC > MAX_USB_IDS * 2)
3357 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3358 "(maximum is %d pairs)", MAX_USB_IDS);
3359 CMD_ARGC = MAX_USB_IDS * 2;
3361 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3363 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3365 return ERROR_COMMAND_SYNTAX_ERROR;
3366 /* remove the incomplete trailing id */
3371 for (i = 0; i < CMD_ARGC; i += 2)
3373 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3374 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3378 * Explicitly terminate, in case there are multiples instances of
3381 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3386 COMMAND_HANDLER(ft2232_handle_latency_command)
3390 ft2232_latency = atoi(CMD_ARGV[0]);
3394 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3400 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3404 /* 7 bits of either ones or zeros. */
3405 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3407 while (num_cycles > 0)
3409 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3410 * at most 7 bits per invocation. Here we invoke it potentially
3413 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3415 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3417 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3418 retval = ERROR_JTAG_QUEUE_FAILED;
3423 /* there are no state transitions in this code, so omit state tracking */
3425 /* command "Clock Data to TMS/CS Pin (no Read)" */
3429 buffer_write(bitcount_per_command - 1);
3431 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3436 num_cycles -= bitcount_per_command;
3442 /* ---------------------------------------------------------------------
3443 * Support for IceBear JTAG adapter from Section5:
3444 * http://section5.ch/icebear
3446 * Author: Sten, debian@sansys-electronic.com
3449 /* Icebear pin layout
3451 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3452 * GND GND | 4 3| n.c.
3453 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3454 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3455 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3456 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3457 * ADBUS2 TDO |14 13| GND GND
3459 * ADBUS0 O L TCK ACBUS0 GND
3460 * ADBUS1 O L TDI ACBUS1 GND
3461 * ADBUS2 I TDO ACBUS2 n.c.
3462 * ADBUS3 O H TMS ACBUS3 n.c.
3468 static int icebear_jtag_init(void) {
3470 uint32_t bytes_written;
3472 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3473 low_output = 0x08; /* high: TMS; low: TCK TDI */
3477 enum reset_types jtag_reset_config = jtag_get_reset_config();
3478 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3479 low_direction &= ~nTRST; /* nTRST high impedance */
3482 low_direction |= nTRST;
3483 low_output |= nTRST;
3486 low_direction |= nSRST;
3487 low_output |= nSRST;
3489 /* initialize low byte for jtag */
3490 buf[0] = 0x80; /* command "set data bits low byte" */
3491 buf[1] = low_output;
3492 buf[2] = low_direction;
3493 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3495 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3496 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3497 return ERROR_JTAG_INIT_FAILED;
3501 high_direction = 0x00;
3504 /* initialize high port */
3505 buf[0] = 0x82; /* command "set data bits high byte" */
3506 buf[1] = high_output; /* value */
3507 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3508 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3510 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3511 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3512 return ERROR_JTAG_INIT_FAILED;
3518 static void icebear_jtag_reset(int trst, int srst) {
3521 low_direction |= nTRST;
3522 low_output &= ~nTRST;
3524 else if (trst == 0) {
3525 enum reset_types jtag_reset_config = jtag_get_reset_config();
3526 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3527 low_direction &= ~nTRST;
3529 low_output |= nTRST;
3533 low_output &= ~nSRST;
3535 else if (srst == 0) {
3536 low_output |= nSRST;
3539 /* command "set data bits low byte" */
3541 buffer_write(low_output);
3542 buffer_write(low_direction);
3544 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3547 /* ---------------------------------------------------------------------
3548 * Support for Signalyzer H2 and Signalyzer H4
3549 * JTAG adapter from Xverve Technologies Inc.
3550 * http://www.signalyzer.com or http://www.xverve.com
3552 * Author: Oleg Seiljus, oleg@signalyzer.com
3554 static unsigned char signalyzer_h_side;
3555 static unsigned int signalyzer_h_adapter_type;
3557 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3559 #if BUILD_FT2232_FTD2XX == 1
3560 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3563 #define SIGNALYZER_COMMAND_ADDR 128
3564 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3566 #define SIGNALYZER_COMMAND_VERSION 0x41
3567 #define SIGNALYZER_COMMAND_RESET 0x42
3568 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3569 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3570 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3571 #define SIGNALYZER_COMMAND_LED_SET 0x53
3572 #define SIGNALYZER_COMMAND_ADC 0x54
3573 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3574 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3575 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3576 #define SIGNALYZER_COMMAND_I2C 0x58
3578 #define SIGNALYZER_CHAN_A 1
3579 #define SIGNALYZER_CHAN_B 2
3580 /* LEDS use channel C */
3581 #define SIGNALYZER_CHAN_C 4
3583 #define SIGNALYZER_LED_GREEN 1
3584 #define SIGNALYZER_LED_RED 2
3586 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3587 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3588 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3589 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3590 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3593 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3595 #if BUILD_FT2232_FTD2XX == 1
3596 return FT_WriteEE(ftdih, address, value);
3597 #elif BUILD_FT2232_LIBFTDI == 1
3602 #if BUILD_FT2232_FTD2XX == 1
3603 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3605 return FT_ReadEE(ftdih, address, value);
3609 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3610 int on_time_ms, int off_time_ms, unsigned char cycles)
3612 unsigned char on_time;
3613 unsigned char off_time;
3615 if (on_time_ms < 0xFFFF)
3616 on_time = (unsigned char)(on_time_ms / 62);
3620 off_time = (unsigned char)(off_time_ms / 62);
3622 #if BUILD_FT2232_FTD2XX == 1
3625 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3626 ((uint32_t)(channel << 8) | led))) != FT_OK)
3628 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3629 return ERROR_JTAG_DEVICE_ERROR;
3632 if ((status = signalyzer_h_ctrl_write(
3633 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3634 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3636 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3637 return ERROR_JTAG_DEVICE_ERROR;
3640 if ((status = signalyzer_h_ctrl_write(
3641 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3642 ((uint32_t)cycles))) != FT_OK)
3644 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3645 return ERROR_JTAG_DEVICE_ERROR;
3648 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3649 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3651 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3652 return ERROR_JTAG_DEVICE_ERROR;
3656 #elif BUILD_FT2232_LIBFTDI == 1
3659 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3660 ((uint32_t)(channel << 8) | led))) < 0)
3662 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3663 ftdi_get_error_string(&ftdic));
3664 return ERROR_JTAG_DEVICE_ERROR;
3667 if ((retval = signalyzer_h_ctrl_write(
3668 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3669 ((uint32_t)(on_time << 8) | off_time))) < 0)
3671 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3672 ftdi_get_error_string(&ftdic));
3673 return ERROR_JTAG_DEVICE_ERROR;
3676 if ((retval = signalyzer_h_ctrl_write(
3677 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3678 (uint32_t)cycles)) < 0)
3680 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3681 ftdi_get_error_string(&ftdic));
3682 return ERROR_JTAG_DEVICE_ERROR;
3685 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3686 SIGNALYZER_COMMAND_LED_SET)) < 0)
3688 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3689 ftdi_get_error_string(&ftdic));
3690 return ERROR_JTAG_DEVICE_ERROR;
3697 static int signalyzer_h_init(void)
3699 #if BUILD_FT2232_FTD2XX == 1
3706 uint16_t read_buf[12] = { 0 };
3708 uint32_t bytes_written;
3710 /* turn on center green led */
3711 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3712 0xFFFF, 0x00, 0x00);
3714 /* determine what channel config wants to open
3715 * TODO: change me... current implementation is made to work
3716 * with openocd description parsing.
3718 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3722 signalyzer_h_side = *(end_of_desc - 1);
3723 if (signalyzer_h_side == 'B')
3724 signalyzer_h_side = SIGNALYZER_CHAN_B;
3726 signalyzer_h_side = SIGNALYZER_CHAN_A;
3730 LOG_ERROR("No Channel was specified");
3734 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3737 #if BUILD_FT2232_FTD2XX == 1
3738 /* read signalyzer versionining information */
3739 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3740 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3742 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3743 return ERROR_JTAG_DEVICE_ERROR;
3746 for (i = 0; i < 10; i++)
3748 if ((status = signalyzer_h_ctrl_read(
3749 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3750 &read_buf[i])) != FT_OK)
3752 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3754 return ERROR_JTAG_DEVICE_ERROR;
3758 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3759 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3760 read_buf[4], read_buf[5], read_buf[6]);
3762 /* set gpio register */
3763 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3764 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3766 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3767 return ERROR_JTAG_DEVICE_ERROR;
3770 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3773 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3774 return ERROR_JTAG_DEVICE_ERROR;
3777 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3778 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3780 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3781 return ERROR_JTAG_DEVICE_ERROR;
3784 /* read adapter type information */
3785 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3786 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3788 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3789 return ERROR_JTAG_DEVICE_ERROR;
3792 if ((status = signalyzer_h_ctrl_write(
3793 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3795 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3796 return ERROR_JTAG_DEVICE_ERROR;
3799 if ((status = signalyzer_h_ctrl_write(
3800 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3802 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3803 return ERROR_JTAG_DEVICE_ERROR;
3806 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3807 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3809 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3810 return ERROR_JTAG_DEVICE_ERROR;
3815 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3816 &read_buf[0])) != FT_OK)
3818 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3819 return ERROR_JTAG_DEVICE_ERROR;
3822 if (read_buf[0] != 0x0498)
3823 signalyzer_h_adapter_type = 0x0000;
3826 for (i = 0; i < 4; i++)
3828 if ((status = signalyzer_h_ctrl_read(
3829 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3830 &read_buf[i])) != FT_OK)
3832 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3834 return ERROR_JTAG_DEVICE_ERROR;
3838 signalyzer_h_adapter_type = read_buf[0];
3841 #elif BUILD_FT2232_LIBFTDI == 1
3842 /* currently libftdi does not allow reading individual eeprom
3843 * locations, therefore adapter type cannot be detected.
3844 * override with most common type
3846 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3849 enum reset_types jtag_reset_config = jtag_get_reset_config();
3851 /* ADAPTOR: EM_LT16_A */
3852 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3854 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3855 "detected. (HW: %2x).", (read_buf[1] >> 8));
3863 low_direction = 0x1b;
3866 high_direction = 0x0;
3868 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3870 low_direction &= ~nTRSTnOE; /* nTRST input */
3871 low_output &= ~nTRST; /* nTRST = 0 */
3875 low_direction |= nTRSTnOE; /* nTRST output */
3876 low_output |= nTRST; /* nTRST = 1 */
3879 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3881 low_direction |= nSRSTnOE; /* nSRST output */
3882 low_output |= nSRST; /* nSRST = 1 */
3886 low_direction &= ~nSRSTnOE; /* nSRST input */
3887 low_output &= ~nSRST; /* nSRST = 0 */
3890 #if BUILD_FT2232_FTD2XX == 1
3891 /* enable power to the module */
3892 if ((status = signalyzer_h_ctrl_write(
3893 SIGNALYZER_DATA_BUFFER_ADDR,
3894 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3897 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3899 return ERROR_JTAG_DEVICE_ERROR;
3902 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3903 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3905 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3907 return ERROR_JTAG_DEVICE_ERROR;
3910 /* set gpio mode register */
3911 if ((status = signalyzer_h_ctrl_write(
3912 SIGNALYZER_DATA_BUFFER_ADDR,
3913 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3915 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3917 return ERROR_JTAG_DEVICE_ERROR;
3920 if ((status = signalyzer_h_ctrl_write(
3921 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3924 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3926 return ERROR_JTAG_DEVICE_ERROR;
3929 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3930 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3932 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3934 return ERROR_JTAG_DEVICE_ERROR;
3937 /* set gpio register */
3938 if ((status = signalyzer_h_ctrl_write(
3939 SIGNALYZER_DATA_BUFFER_ADDR,
3940 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3942 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3944 return ERROR_JTAG_DEVICE_ERROR;
3947 if ((status = signalyzer_h_ctrl_write(
3948 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3951 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3953 return ERROR_JTAG_DEVICE_ERROR;
3956 if ((status = signalyzer_h_ctrl_write(
3957 SIGNALYZER_COMMAND_ADDR,
3958 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3960 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3962 return ERROR_JTAG_DEVICE_ERROR;
3967 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3968 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3969 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3970 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3971 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3973 if (signalyzer_h_adapter_type
3974 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3975 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3976 "detected. (HW: %2x).", (read_buf[1] >> 8));
3977 else if (signalyzer_h_adapter_type
3978 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3979 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3980 "(ARM JTAG with PSU) detected. (HW: %2x).",
3981 (read_buf[1] >> 8));
3982 else if (signalyzer_h_adapter_type
3983 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3984 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3985 "detected. (HW: %2x).", (read_buf[1] >> 8));
3986 else if (signalyzer_h_adapter_type
3987 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3988 LOG_INFO("Signalyzer: EM-JTAG-P "
3989 "(Generic JTAG with PSU) detected. (HW: %2x).",
3990 (read_buf[1] >> 8));
3998 low_direction = 0x1b;
4001 high_direction = 0x1f;
4003 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4005 high_output |= nTRSTnOE;
4006 high_output &= ~nTRST;
4010 high_output &= ~nTRSTnOE;
4011 high_output |= nTRST;
4014 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4016 high_output &= ~nSRSTnOE;
4017 high_output |= nSRST;
4021 high_output |= nSRSTnOE;
4022 high_output &= ~nSRST;
4025 #if BUILD_FT2232_FTD2XX == 1
4026 /* enable power to the module */
4027 if ((status = signalyzer_h_ctrl_write(
4028 SIGNALYZER_DATA_BUFFER_ADDR,
4029 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4032 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4034 return ERROR_JTAG_DEVICE_ERROR;
4037 if ((status = signalyzer_h_ctrl_write(
4038 SIGNALYZER_COMMAND_ADDR,
4039 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4041 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4043 return ERROR_JTAG_DEVICE_ERROR;
4046 /* set gpio mode register (IO_16 and IO_17 set as analog
4047 * inputs, other is gpio)
4049 if ((status = signalyzer_h_ctrl_write(
4050 SIGNALYZER_DATA_BUFFER_ADDR,
4051 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4053 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4055 return ERROR_JTAG_DEVICE_ERROR;
4058 if ((status = signalyzer_h_ctrl_write(
4059 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4062 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4064 return ERROR_JTAG_DEVICE_ERROR;
4067 if ((status = signalyzer_h_ctrl_write(
4068 SIGNALYZER_COMMAND_ADDR,
4069 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4071 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4073 return ERROR_JTAG_DEVICE_ERROR;
4076 /* set gpio register (all inputs, for -P modules,
4077 * PSU will be turned off)
4079 if ((status = signalyzer_h_ctrl_write(
4080 SIGNALYZER_DATA_BUFFER_ADDR,
4081 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4083 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4085 return ERROR_JTAG_DEVICE_ERROR;
4088 if ((status = signalyzer_h_ctrl_write(
4089 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4092 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4094 return ERROR_JTAG_DEVICE_ERROR;
4097 if ((status = signalyzer_h_ctrl_write(
4098 SIGNALYZER_COMMAND_ADDR,
4099 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4101 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4103 return ERROR_JTAG_DEVICE_ERROR;
4108 else if (signalyzer_h_adapter_type == 0x0000)
4110 LOG_INFO("Signalyzer: No external modules were detected.");
4118 low_direction = 0x1b;
4121 high_direction = 0x0;
4123 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4125 low_direction &= ~nTRSTnOE; /* nTRST input */
4126 low_output &= ~nTRST; /* nTRST = 0 */
4130 low_direction |= nTRSTnOE; /* nTRST output */
4131 low_output |= nTRST; /* nTRST = 1 */
4134 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4136 low_direction |= nSRSTnOE; /* nSRST output */
4137 low_output |= nSRST; /* nSRST = 1 */
4141 low_direction &= ~nSRSTnOE; /* nSRST input */
4142 low_output &= ~nSRST; /* nSRST = 0 */
4147 LOG_ERROR("Unknown module type is detected: %.4x",
4148 signalyzer_h_adapter_type);
4149 return ERROR_JTAG_DEVICE_ERROR;
4152 /* initialize low byte of controller for jtag operation */
4154 buf[1] = low_output;
4155 buf[2] = low_direction;
4157 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4159 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4160 return ERROR_JTAG_INIT_FAILED;
4163 #if BUILD_FT2232_FTD2XX == 1
4164 if (ftdi_device == FT_DEVICE_2232H)
4166 /* initialize high byte of controller for jtag operation */
4168 buf[1] = high_output;
4169 buf[2] = high_direction;
4171 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4173 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4174 return ERROR_JTAG_INIT_FAILED;
4177 #elif BUILD_FT2232_LIBFTDI == 1
4178 if (ftdi_device == TYPE_2232H)
4180 /* initialize high byte of controller for jtag operation */
4182 buf[1] = high_output;
4183 buf[2] = high_direction;
4185 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4187 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4188 return ERROR_JTAG_INIT_FAILED;
4195 static void signalyzer_h_reset(int trst, int srst)
4197 enum reset_types jtag_reset_config = jtag_get_reset_config();
4199 /* ADAPTOR: EM_LT16_A */
4200 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4204 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4205 /* switch to output pin (output is low) */
4206 low_direction |= nTRSTnOE;
4208 /* switch output low */
4209 low_output &= ~nTRST;
4213 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4214 /* switch to input pin (high-Z + internal
4215 * and external pullup) */
4216 low_direction &= ~nTRSTnOE;
4218 /* switch output high */
4219 low_output |= nTRST;
4224 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4225 /* switch output low */
4226 low_output &= ~nSRST;
4228 /* switch to output pin (output is low) */
4229 low_direction |= nSRSTnOE;
4233 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4234 /* switch output high */
4235 low_output |= nSRST;
4237 /* switch to input pin (high-Z) */
4238 low_direction &= ~nSRSTnOE;
4241 /* command "set data bits low byte" */
4243 buffer_write(low_output);
4244 buffer_write(low_direction);
4245 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4246 "low_direction: 0x%2.2x",
4247 trst, srst, low_output, low_direction);
4249 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4250 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4251 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4252 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4253 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4257 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4258 high_output &= ~nTRSTnOE;
4260 high_output &= ~nTRST;
4264 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4265 high_output |= nTRSTnOE;
4267 high_output |= nTRST;
4272 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4273 high_output &= ~nSRST;
4275 high_output &= ~nSRSTnOE;
4279 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4280 high_output |= nSRST;
4282 high_output |= nSRSTnOE;
4285 /* command "set data bits high byte" */
4287 buffer_write(high_output);
4288 buffer_write(high_direction);
4289 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4290 "high_direction: 0x%2.2x",
4291 trst, srst, high_output, high_direction);
4293 else if (signalyzer_h_adapter_type == 0x0000)
4297 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4298 /* switch to output pin (output is low) */
4299 low_direction |= nTRSTnOE;
4301 /* switch output low */
4302 low_output &= ~nTRST;
4306 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4307 /* switch to input pin (high-Z + internal
4308 * and external pullup) */
4309 low_direction &= ~nTRSTnOE;
4311 /* switch output high */
4312 low_output |= nTRST;
4317 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4318 /* switch output low */
4319 low_output &= ~nSRST;
4321 /* switch to output pin (output is low) */
4322 low_direction |= nSRSTnOE;
4326 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4327 /* switch output high */
4328 low_output |= nSRST;
4330 /* switch to input pin (high-Z) */
4331 low_direction &= ~nSRSTnOE;
4334 /* command "set data bits low byte" */
4336 buffer_write(low_output);
4337 buffer_write(low_direction);
4338 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4339 "low_direction: 0x%2.2x",
4340 trst, srst, low_output, low_direction);
4344 static void signalyzer_h_blink(void)
4346 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4349 /********************************************************************
4350 * Support for KT-LINK
4351 * JTAG adapter from KRISTECH
4352 * http://www.kristech.eu
4353 *******************************************************************/
4354 static int ktlink_init(void)
4357 uint32_t bytes_written;
4358 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4360 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4361 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4363 // initialize low port
4364 buf[0] = 0x80; // command "set data bits low byte"
4365 buf[1] = low_output;
4366 buf[2] = low_direction;
4367 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4369 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4371 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4372 return ERROR_JTAG_INIT_FAILED;
4380 high_output = 0x80; // turn LED on
4381 high_direction = 0xFF; // all outputs
4383 enum reset_types jtag_reset_config = jtag_get_reset_config();
4385 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4386 high_output |= nTRSTnOE;
4387 high_output &= ~nTRST;
4389 high_output &= ~nTRSTnOE;
4390 high_output |= nTRST;
4393 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4394 high_output &= ~nSRSTnOE;
4395 high_output |= nSRST;
4397 high_output |= nSRSTnOE;
4398 high_output &= ~nSRST;
4401 // initialize high port
4402 buf[0] = 0x82; // command "set data bits high byte"
4403 buf[1] = high_output; // value
4404 buf[2] = high_direction;
4405 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4407 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4409 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4410 return ERROR_JTAG_INIT_FAILED;
4416 static void ktlink_reset(int trst, int srst)
4418 enum reset_types jtag_reset_config = jtag_get_reset_config();
4421 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4422 high_output &= ~nTRSTnOE;
4424 high_output &= ~nTRST;
4425 } else if (trst == 0) {
4426 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4427 high_output |= nTRSTnOE;
4429 high_output |= nTRST;
4433 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4434 high_output &= ~nSRST;
4436 high_output &= ~nSRSTnOE;
4437 } else if (srst == 0) {
4438 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4439 high_output |= nSRST;
4441 high_output |= nSRSTnOE;
4444 buffer_write(0x82); // command "set data bits high byte"
4445 buffer_write(high_output);
4446 buffer_write(high_direction);
4447 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4450 static void ktlink_blink(void)
4452 /* LED connected to ACBUS7 */
4453 if (high_output & 0x80)
4454 high_output &= 0x7F;
4456 high_output |= 0x80;
4458 buffer_write(0x82); // command "set data bits high byte"
4459 buffer_write(high_output);
4460 buffer_write(high_direction);
4463 static const struct command_registration ft2232_command_handlers[] = {
4465 .name = "ft2232_device_desc",
4466 .handler = &ft2232_handle_device_desc_command,
4467 .mode = COMMAND_CONFIG,
4468 .help = "set the USB device description of the FTDI FT2232 device",
4469 .usage = "description_string",
4472 .name = "ft2232_serial",
4473 .handler = &ft2232_handle_serial_command,
4474 .mode = COMMAND_CONFIG,
4475 .help = "set the serial number of the FTDI FT2232 device",
4476 .usage = "serial_string",
4479 .name = "ft2232_layout",
4480 .handler = &ft2232_handle_layout_command,
4481 .mode = COMMAND_CONFIG,
4482 .help = "set the layout of the FT2232 GPIO signals used "
4483 "to control output-enables and reset signals",
4484 .usage = "layout_name",
4487 .name = "ft2232_vid_pid",
4488 .handler = &ft2232_handle_vid_pid_command,
4489 .mode = COMMAND_CONFIG,
4490 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4491 .usage = "(vid pid)* ",
4494 .name = "ft2232_latency",
4495 .handler = &ft2232_handle_latency_command,
4496 .mode = COMMAND_CONFIG,
4497 .help = "set the FT2232 latency timer to a new value",
4500 COMMAND_REGISTRATION_DONE
4503 struct jtag_interface ft2232_interface = {
4505 .supported = DEBUG_CAP_TMS_SEQ,
4506 .commands = ft2232_command_handlers,
4507 .transports = jtag_only,
4509 .init = ft2232_init,
4510 .quit = ft2232_quit,
4511 .speed = ft2232_speed,
4512 .speed_div = ft2232_speed_div,
4514 .execute_queue = ft2232_execute_queue,