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);
192 /* reset procedures for supported layouts */
193 static void ftx23_reset(int trst, int srst);
194 static void jtagkey_reset(int trst, int srst);
195 static void olimex_jtag_reset(int trst, int srst);
196 static void flyswatter_reset(int trst, int srst);
197 static void turtle_reset(int trst, int srst);
198 static void comstick_reset(int trst, int srst);
199 static void stm32stick_reset(int trst, int srst);
200 static void axm0432_jtag_reset(int trst, int srst);
201 static void sheevaplug_reset(int trst, int srst);
202 static void icebear_jtag_reset(int trst, int srst);
203 static void signalyzer_h_reset(int trst, int srst);
204 static void ktlink_reset(int trst, int srst);
205 static void redbee_reset(int trst, int srst);
207 /* blink procedures for layouts that support a blinking led */
208 static void olimex_jtag_blink(void);
209 static void flyswatter_jtag_blink(void);
210 static void turtle_jtag_blink(void);
211 static void signalyzer_h_blink(void);
212 static void ktlink_blink(void);
214 /* common transport support options */
216 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
218 static const struct ft2232_layout ft2232_layouts[] =
221 .init = usbjtag_init,
222 .reset = ftx23_reset,
225 .init = jtagkey_init,
226 .reset = jtagkey_reset,
228 { .name = "jtagkey_prototype_v1",
229 .init = jtagkey_init,
230 .reset = jtagkey_reset,
232 { .name = "oocdlink",
233 .init = jtagkey_init,
234 .reset = jtagkey_reset,
236 { .name = "signalyzer",
237 .init = signalyzer_init,
238 .reset = ftx23_reset,
240 { .name = "evb_lm3s811",
241 .init = lm3s811_jtag_init,
242 .reset = ftx23_reset,
244 { .name = "luminary_icdi",
245 .init = icdi_jtag_init,
246 .reset = ftx23_reset,
248 { .name = "olimex-jtag",
249 .init = olimex_jtag_init,
250 .reset = olimex_jtag_reset,
251 .blink = olimex_jtag_blink
253 { .name = "flyswatter",
254 .init = flyswatter_init,
255 .reset = flyswatter_reset,
256 .blink = flyswatter_jtag_blink
258 { .name = "turtelizer2",
260 .reset = turtle_reset,
261 .blink = turtle_jtag_blink
263 { .name = "comstick",
264 .init = comstick_init,
265 .reset = comstick_reset,
267 { .name = "stm32stick",
268 .init = stm32stick_init,
269 .reset = stm32stick_reset,
271 { .name = "axm0432_jtag",
272 .init = axm0432_jtag_init,
273 .reset = axm0432_jtag_reset,
275 { .name = "sheevaplug",
276 .init = sheevaplug_init,
277 .reset = sheevaplug_reset,
280 .init = icebear_jtag_init,
281 .reset = icebear_jtag_reset,
284 .init = cortino_jtag_init,
285 .reset = comstick_reset,
287 { .name = "signalyzer-h",
288 .init = signalyzer_h_init,
289 .reset = signalyzer_h_reset,
290 .blink = signalyzer_h_blink
294 .reset = ktlink_reset,
295 .blink = ktlink_blink
297 { .name = "redbee-econotag",
299 .reset = redbee_reset,
301 { .name = "redbee-usb",
303 .reset = redbee_reset,
304 .channel = INTERFACE_B,
306 { .name = NULL, /* END OF TABLE */ },
309 /* bitmask used to drive nTRST; usually a GPIOLx signal */
310 static uint8_t nTRST;
311 static uint8_t nTRSTnOE;
312 /* bitmask used to drive nSRST; usually a GPIOLx signal */
313 static uint8_t nSRST;
314 static uint8_t nSRSTnOE;
316 /** the layout being used with this debug session */
317 static const struct ft2232_layout *layout;
319 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
320 static uint8_t low_output = 0x0;
322 /* note that direction bit == 1 means that signal is an output */
324 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
325 static uint8_t low_direction = 0x0;
326 /** default value bitmask for CBUS GPIOH(0..4) */
327 static uint8_t high_output = 0x0;
328 /** default direction bitmask for CBUS GPIOH(0..4) */
329 static uint8_t high_direction = 0x0;
331 #if BUILD_FT2232_FTD2XX == 1
332 static FT_HANDLE ftdih = NULL;
333 static FT_DEVICE ftdi_device = 0;
334 #elif BUILD_FT2232_LIBFTDI == 1
335 static struct ftdi_context ftdic;
336 static enum ftdi_chip_type ftdi_device;
339 static struct jtag_command* first_unsent; /* next command that has to be sent */
340 static int require_send;
342 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
344 "There is a significant difference between libftdi and libftd2xx. The latter
345 one allows to schedule up to 64*64 bytes of result data while libftdi fails
346 with more than 4*64. As a consequence, the FT2232 driver is forced to
347 perform around 16x more USB transactions for long command streams with TDO
348 capture when running with libftdi."
351 #define FT2232_BUFFER_SIZE 131072
352 a comment would have been nice.
355 #define FT2232_BUFFER_SIZE 131072
357 static uint8_t* ft2232_buffer = NULL;
358 static int ft2232_buffer_size = 0;
359 static int ft2232_read_pointer = 0;
360 static int ft2232_expect_read = 0;
363 * Function buffer_write
364 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
365 * @param val is the byte to send.
367 static inline void buffer_write(uint8_t val)
369 assert(ft2232_buffer);
370 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
371 ft2232_buffer[ft2232_buffer_size++] = val;
375 * Function buffer_read
376 * returns a byte from the byte buffer.
378 static inline uint8_t buffer_read(void)
380 assert(ft2232_buffer);
381 assert(ft2232_read_pointer < ft2232_buffer_size);
382 return ft2232_buffer[ft2232_read_pointer++];
386 * Clocks out \a bit_count bits on the TMS line, starting with the least
387 * significant bit of tms_bits and progressing to more significant bits.
388 * Rigorous state transition logging is done here via tap_set_state().
390 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
391 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
392 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
393 * is often used for this, 0x4b.
395 * @param tms_bits Holds the sequence of bits to send.
396 * @param tms_count Tells how many bits in the sequence.
397 * @param tdi_bit A single bit to pass on to TDI before the first TCK
398 * cycle and held static for the duration of TMS clocking.
400 * See the MPSSE spec referenced above.
402 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
406 int tms_ndx; /* bit index into tms_byte */
408 assert(tms_count > 0);
410 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
411 mpsse_cmd, tms_bits, tms_count);
413 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
415 bool bit = tms_bits & 1;
418 tms_byte |= (1 << tms_ndx);
420 /* always do state transitions in public view */
421 tap_set_state(tap_state_transition(tap_get_state(), bit));
423 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
428 if (tms_ndx == 7 || i == tms_count-1)
430 buffer_write(mpsse_cmd);
431 buffer_write(tms_ndx - 1);
433 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
434 TMS/CS and is held static for the duration of TMS/CS clocking.
436 buffer_write(tms_byte | (tdi_bit << 7));
442 * Function get_tms_buffer_requirements
443 * returns what clock_tms() will consume if called with
446 static inline int get_tms_buffer_requirements(int bit_count)
448 return ((bit_count + 6)/7) * 3;
452 * Function move_to_state
453 * moves the TAP controller from the current state to a
454 * \a goal_state through a path given by tap_get_tms_path(). State transition
455 * logging is performed by delegation to clock_tms().
457 * @param goal_state is the destination state for the move.
459 static void move_to_state(tap_state_t goal_state)
461 tap_state_t start_state = tap_get_state();
463 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
464 lookup of the required TMS pattern to move to this state from the
468 /* do the 2 lookups */
469 int tms_bits = tap_get_tms_path(start_state, goal_state);
470 int tms_count = tap_get_tms_path_len(start_state, goal_state);
472 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
474 clock_tms(0x4b, tms_bits, tms_count, 0);
477 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
479 #if BUILD_FT2232_FTD2XX == 1
481 DWORD dw_bytes_written;
482 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
484 *bytes_written = dw_bytes_written;
485 LOG_ERROR("FT_Write returned: %lu", status);
486 return ERROR_JTAG_DEVICE_ERROR;
490 *bytes_written = dw_bytes_written;
492 #elif BUILD_FT2232_LIBFTDI == 1
494 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
497 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
498 return ERROR_JTAG_DEVICE_ERROR;
502 *bytes_written = retval;
506 if (*bytes_written != (uint32_t)size)
508 return ERROR_JTAG_DEVICE_ERROR;
514 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
516 #if BUILD_FT2232_FTD2XX == 1
522 while ((*bytes_read < size) && timeout--)
524 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
525 *bytes_read, &dw_bytes_read)) != FT_OK)
528 LOG_ERROR("FT_Read returned: %lu", status);
529 return ERROR_JTAG_DEVICE_ERROR;
531 *bytes_read += dw_bytes_read;
534 #elif BUILD_FT2232_LIBFTDI == 1
536 int timeout = LIBFTDI_READ_RETRY_COUNT;
539 while ((*bytes_read < size) && timeout--)
541 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
544 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
545 return ERROR_JTAG_DEVICE_ERROR;
547 *bytes_read += retval;
552 if (*bytes_read < size)
554 LOG_ERROR("couldn't read enough bytes from "
555 "FT2232 device (%i < %i)",
556 (unsigned)*bytes_read,
558 return ERROR_JTAG_DEVICE_ERROR;
564 static bool ft2232_device_is_highspeed(void)
566 #if BUILD_FT2232_FTD2XX == 1
567 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
568 #elif BUILD_FT2232_LIBFTDI == 1
569 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
574 * Commands that only apply to the FT2232H and FT4232H devices.
575 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
576 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
579 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
581 uint8_t buf = enable ? 0x96 : 0x97;
582 LOG_DEBUG("%2.2x", buf);
584 uint32_t bytes_written;
587 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
589 LOG_ERROR("couldn't write command to %s adaptive clocking"
590 , enable ? "enable" : "disable");
598 * Enable/disable the clk divide by 5 of the 60MHz master clock.
599 * This result in a JTAG clock speed range of 91.553Hz-6MHz
600 * respective 457.763Hz-30MHz.
602 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
604 uint32_t bytes_written;
605 uint8_t buf = enable ? 0x8b : 0x8a;
607 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
609 LOG_ERROR("couldn't write command to %s clk divide by 5"
610 , enable ? "enable" : "disable");
611 return ERROR_JTAG_INIT_FAILED;
613 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
614 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
619 static int ft2232_speed(int speed)
623 uint32_t bytes_written;
626 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
627 if (ft2232_device_is_highspeed())
628 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
629 else if (enable_adaptive_clocking)
631 LOG_ERROR("ft2232 device %lu does not support RTCK"
632 , (long unsigned int)ftdi_device);
636 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
639 buf[0] = 0x86; /* command "set divisor" */
640 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
641 buf[2] = (speed >> 8) & 0xff; /* valueH */
643 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
644 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
646 LOG_ERROR("couldn't set FT2232 TCK speed");
653 static int ft2232_speed_div(int speed, int* khz)
655 /* Take a look in the FT2232 manual,
656 * AN2232C-01 Command Processor for
657 * MPSSE and MCU Host Bus. Chapter 3.8 */
659 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
664 static int ft2232_khz(int khz, int* jtag_speed)
668 if (ft2232_device_is_highspeed())
670 *jtag_speed = RTCK_SPEED;
675 LOG_DEBUG("RCLK not supported");
680 /* Take a look in the FT2232 manual,
681 * AN2232C-01 Command Processor for
682 * MPSSE and MCU Host Bus. Chapter 3.8
684 * We will calc here with a multiplier
685 * of 10 for better rounding later. */
687 /* Calc speed, (ft2232_max_tck / khz) - 1 */
688 /* Use 65000 for better rounding */
689 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
691 /* Add 0.9 for rounding */
694 /* Calc real speed */
695 *jtag_speed = *jtag_speed / 10;
697 /* Check if speed is greater than 0 */
703 /* Check max value */
704 if (*jtag_speed > 0xFFFF)
706 *jtag_speed = 0xFFFF;
712 static void ft2232_end_state(tap_state_t state)
714 if (tap_is_state_stable(state))
715 tap_set_end_state(state);
718 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
723 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
725 int num_bytes = (scan_size + 7) / 8;
726 int bits_left = scan_size;
729 while (num_bytes-- > 1)
731 buffer[cur_byte++] = buffer_read();
735 buffer[cur_byte] = 0x0;
737 /* There is one more partial byte left from the clock data in/out instructions */
740 buffer[cur_byte] = buffer_read() >> 1;
742 /* 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 */
743 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
746 static void ft2232_debug_dump_buffer(void)
752 for (i = 0; i < ft2232_buffer_size; i++)
754 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
757 LOG_DEBUG("%s", line);
763 LOG_DEBUG("%s", line);
766 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
768 struct jtag_command* cmd;
773 uint32_t bytes_written = 0;
774 uint32_t bytes_read = 0;
776 #ifdef _DEBUG_USB_IO_
777 struct timeval start, inter, inter2, end;
778 struct timeval d_inter, d_inter2, d_end;
781 #ifdef _DEBUG_USB_COMMS_
782 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
783 ft2232_debug_dump_buffer();
786 #ifdef _DEBUG_USB_IO_
787 gettimeofday(&start, NULL);
790 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
792 LOG_ERROR("couldn't write MPSSE commands to FT2232");
796 #ifdef _DEBUG_USB_IO_
797 gettimeofday(&inter, NULL);
800 if (ft2232_expect_read)
802 /* FIXME this "timeout" is never changed ... */
803 int timeout = LIBFTDI_READ_RETRY_COUNT;
804 ft2232_buffer_size = 0;
806 #ifdef _DEBUG_USB_IO_
807 gettimeofday(&inter2, NULL);
810 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
812 LOG_ERROR("couldn't read from FT2232");
816 #ifdef _DEBUG_USB_IO_
817 gettimeofday(&end, NULL);
819 timeval_subtract(&d_inter, &inter, &start);
820 timeval_subtract(&d_inter2, &inter2, &start);
821 timeval_subtract(&d_end, &end, &start);
823 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
824 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
825 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
826 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
829 ft2232_buffer_size = bytes_read;
831 if (ft2232_expect_read != ft2232_buffer_size)
833 LOG_ERROR("ft2232_expect_read (%i) != "
834 "ft2232_buffer_size (%i) "
838 LIBFTDI_READ_RETRY_COUNT - timeout);
839 ft2232_debug_dump_buffer();
844 #ifdef _DEBUG_USB_COMMS_
845 LOG_DEBUG("read buffer (%i retries): %i bytes",
846 LIBFTDI_READ_RETRY_COUNT - timeout,
848 ft2232_debug_dump_buffer();
852 ft2232_expect_read = 0;
853 ft2232_read_pointer = 0;
855 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
856 * that wasn't handled by a caller-provided error handler
866 type = jtag_scan_type(cmd->cmd.scan);
867 if (type != SCAN_OUT)
869 scan_size = jtag_scan_size(cmd->cmd.scan);
870 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
871 ft2232_read_scan(type, buffer, scan_size);
872 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
873 retval = ERROR_JTAG_QUEUE_FAILED;
885 ft2232_buffer_size = 0;
891 * Function ft2232_add_pathmove
892 * moves the TAP controller from the current state to a new state through the
893 * given path, where path is an array of tap_state_t's.
895 * @param path is an array of tap_stat_t which gives the states to traverse through
896 * ending with the last state at path[num_states-1]
897 * @param num_states is the count of state steps to move through
899 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
903 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
907 /* this loop verifies that the path is legal and logs each state in the path */
910 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
912 int num_states_batch = num_states > 7 ? 7 : num_states;
914 /* command "Clock Data to TMS/CS Pin (no Read)" */
917 /* number of states remaining */
918 buffer_write(num_states_batch - 1);
920 while (num_states_batch--) {
921 /* either TMS=0 or TMS=1 must work ... */
922 if (tap_state_transition(tap_get_state(), false)
923 == path[state_count])
924 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
925 else if (tap_state_transition(tap_get_state(), true)
926 == path[state_count])
927 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
929 /* ... or else the caller goofed BADLY */
931 LOG_ERROR("BUG: %s -> %s isn't a valid "
932 "TAP state transition",
933 tap_state_name(tap_get_state()),
934 tap_state_name(path[state_count]));
938 tap_set_state(path[state_count]);
943 buffer_write(tms_byte);
945 tap_set_end_state(tap_get_state());
948 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
950 int num_bytes = (scan_size + 7) / 8;
951 int bits_left = scan_size;
957 if (tap_get_state() != TAP_DRSHIFT)
959 move_to_state(TAP_DRSHIFT);
964 if (tap_get_state() != TAP_IRSHIFT)
966 move_to_state(TAP_IRSHIFT);
970 /* add command for complete bytes */
971 while (num_bytes > 1)
976 /* Clock Data Bytes In and Out LSB First */
978 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
980 else if (type == SCAN_OUT)
982 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
984 /* LOG_DEBUG("added TDI bytes (o)"); */
986 else if (type == SCAN_IN)
988 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
990 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
993 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
994 num_bytes -= thisrun_bytes;
996 buffer_write((uint8_t) (thisrun_bytes - 1));
997 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1001 /* add complete bytes */
1002 while (thisrun_bytes-- > 0)
1004 buffer_write(buffer[cur_byte++]);
1008 else /* (type == SCAN_IN) */
1010 bits_left -= 8 * (thisrun_bytes);
1014 /* the most signifcant bit is scanned during TAP movement */
1015 if (type != SCAN_IN)
1016 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1020 /* process remaining bits but the last one */
1023 if (type == SCAN_IO)
1025 /* Clock Data Bits In and Out LSB First */
1027 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1029 else if (type == SCAN_OUT)
1031 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1033 /* LOG_DEBUG("added TDI bits (o)"); */
1035 else if (type == SCAN_IN)
1037 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1039 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1042 buffer_write(bits_left - 2);
1043 if (type != SCAN_IN)
1044 buffer_write(buffer[cur_byte]);
1047 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1048 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1050 if (type == SCAN_IO)
1052 /* Clock Data Bits In and Out LSB First */
1054 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1056 else if (type == SCAN_OUT)
1058 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1060 /* LOG_DEBUG("added TDI bits (o)"); */
1062 else if (type == SCAN_IN)
1064 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1066 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1069 buffer_write(last_bit);
1077 /* move from Shift-IR/DR to end state */
1078 if (type != SCAN_OUT)
1080 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1081 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1084 /* Clock Data to TMS/CS Pin with Read */
1089 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1090 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1091 /* Clock Data to TMS/CS Pin (no Read) */
1095 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1096 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1099 if (tap_get_state() != tap_get_end_state())
1101 move_to_state(tap_get_end_state());
1105 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1107 int num_bytes = (scan_size + 7) / 8;
1108 int bits_left = scan_size;
1111 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1112 uint8_t* receive_pointer = receive_buffer;
1113 uint32_t bytes_written;
1114 uint32_t bytes_read;
1116 int thisrun_read = 0;
1120 LOG_ERROR("BUG: large IR scans are not supported");
1124 if (tap_get_state() != TAP_DRSHIFT)
1126 move_to_state(TAP_DRSHIFT);
1129 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1131 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1134 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1135 ft2232_buffer_size, (int)bytes_written);
1136 ft2232_buffer_size = 0;
1138 /* add command for complete bytes */
1139 while (num_bytes > 1)
1143 if (type == SCAN_IO)
1145 /* Clock Data Bytes In and Out LSB First */
1147 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1149 else if (type == SCAN_OUT)
1151 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1153 /* LOG_DEBUG("added TDI bytes (o)"); */
1155 else if (type == SCAN_IN)
1157 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1159 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1162 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1163 thisrun_read = thisrun_bytes;
1164 num_bytes -= thisrun_bytes;
1165 buffer_write((uint8_t) (thisrun_bytes - 1));
1166 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1168 if (type != SCAN_IN)
1170 /* add complete bytes */
1171 while (thisrun_bytes-- > 0)
1173 buffer_write(buffer[cur_byte]);
1178 else /* (type == SCAN_IN) */
1180 bits_left -= 8 * (thisrun_bytes);
1183 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1185 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1188 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1190 (int)bytes_written);
1191 ft2232_buffer_size = 0;
1193 if (type != SCAN_OUT)
1195 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1197 LOG_ERROR("couldn't read from FT2232");
1200 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1203 receive_pointer += bytes_read;
1209 /* the most signifcant bit is scanned during TAP movement */
1210 if (type != SCAN_IN)
1211 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1215 /* process remaining bits but the last one */
1218 if (type == SCAN_IO)
1220 /* Clock Data Bits In and Out LSB First */
1222 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1224 else if (type == SCAN_OUT)
1226 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1228 /* LOG_DEBUG("added TDI bits (o)"); */
1230 else if (type == SCAN_IN)
1232 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1234 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1236 buffer_write(bits_left - 2);
1237 if (type != SCAN_IN)
1238 buffer_write(buffer[cur_byte]);
1240 if (type != SCAN_OUT)
1244 if (tap_get_end_state() == TAP_DRSHIFT)
1246 if (type == SCAN_IO)
1248 /* Clock Data Bits In and Out LSB First */
1250 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1252 else if (type == SCAN_OUT)
1254 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1256 /* LOG_DEBUG("added TDI bits (o)"); */
1258 else if (type == SCAN_IN)
1260 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1262 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1265 buffer_write(last_bit);
1269 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1270 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1273 /* move from Shift-IR/DR to end state */
1274 if (type != SCAN_OUT)
1276 /* Clock Data to TMS/CS Pin with Read */
1278 /* LOG_DEBUG("added TMS scan (read)"); */
1282 /* Clock Data to TMS/CS Pin (no Read) */
1284 /* LOG_DEBUG("added TMS scan (no read)"); */
1287 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1288 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1291 if (type != SCAN_OUT)
1294 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1296 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1299 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1301 (int)bytes_written);
1302 ft2232_buffer_size = 0;
1304 if (type != SCAN_OUT)
1306 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1308 LOG_ERROR("couldn't read from FT2232");
1311 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1314 receive_pointer += bytes_read;
1320 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1322 int predicted_size = 3;
1323 int num_bytes = (scan_size - 1) / 8;
1325 if (tap_get_state() != TAP_DRSHIFT)
1326 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1328 if (type == SCAN_IN) /* only from device to host */
1330 /* complete bytes */
1331 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1333 /* remaining bits - 1 (up to 7) */
1334 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1336 else /* host to device, or bidirectional */
1338 /* complete bytes */
1339 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1341 /* remaining bits -1 (up to 7) */
1342 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1345 return predicted_size;
1348 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1350 int predicted_size = 0;
1352 if (type != SCAN_OUT)
1354 /* complete bytes */
1355 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1357 /* remaining bits - 1 */
1358 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1360 /* last bit (from TMS scan) */
1361 predicted_size += 1;
1364 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1366 return predicted_size;
1369 /* semi-generic FT2232/FT4232 reset code */
1370 static void ftx23_reset(int trst, int srst)
1372 enum reset_types jtag_reset_config = jtag_get_reset_config();
1375 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1376 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1378 low_output &= ~nTRST; /* switch output low */
1382 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1383 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1385 low_output |= nTRST; /* switch output high */
1390 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1391 low_output &= ~nSRST; /* switch output low */
1393 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1397 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1398 low_output |= nSRST; /* switch output high */
1400 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1403 /* command "set data bits low byte" */
1405 buffer_write(low_output);
1406 buffer_write(low_direction);
1409 static void jtagkey_reset(int trst, int srst)
1411 enum reset_types jtag_reset_config = jtag_get_reset_config();
1414 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1415 high_output &= ~nTRSTnOE;
1417 high_output &= ~nTRST;
1421 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1422 high_output |= nTRSTnOE;
1424 high_output |= nTRST;
1429 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1430 high_output &= ~nSRST;
1432 high_output &= ~nSRSTnOE;
1436 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1437 high_output |= nSRST;
1439 high_output |= nSRSTnOE;
1442 /* command "set data bits high byte" */
1444 buffer_write(high_output);
1445 buffer_write(high_direction);
1446 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1450 static void olimex_jtag_reset(int trst, int srst)
1452 enum reset_types jtag_reset_config = jtag_get_reset_config();
1455 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1456 high_output &= ~nTRSTnOE;
1458 high_output &= ~nTRST;
1462 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1463 high_output |= nTRSTnOE;
1465 high_output |= nTRST;
1470 high_output |= nSRST;
1474 high_output &= ~nSRST;
1477 /* command "set data bits high byte" */
1479 buffer_write(high_output);
1480 buffer_write(high_direction);
1481 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1485 static void axm0432_jtag_reset(int trst, int srst)
1489 tap_set_state(TAP_RESET);
1490 high_output &= ~nTRST;
1494 high_output |= nTRST;
1499 high_output &= ~nSRST;
1503 high_output |= nSRST;
1506 /* command "set data bits low byte" */
1508 buffer_write(high_output);
1509 buffer_write(high_direction);
1510 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1514 static void flyswatter_reset(int trst, int srst)
1518 low_output &= ~nTRST;
1522 low_output |= nTRST;
1527 low_output |= nSRST;
1531 low_output &= ~nSRST;
1534 /* command "set data bits low byte" */
1536 buffer_write(low_output);
1537 buffer_write(low_direction);
1538 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1541 static void turtle_reset(int trst, int srst)
1547 low_output |= nSRST;
1551 low_output &= ~nSRST;
1554 /* command "set data bits low byte" */
1556 buffer_write(low_output);
1557 buffer_write(low_direction);
1558 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1561 static void comstick_reset(int trst, int srst)
1565 high_output &= ~nTRST;
1569 high_output |= nTRST;
1574 high_output &= ~nSRST;
1578 high_output |= nSRST;
1581 /* command "set data bits high byte" */
1583 buffer_write(high_output);
1584 buffer_write(high_direction);
1585 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1589 static void stm32stick_reset(int trst, int srst)
1593 high_output &= ~nTRST;
1597 high_output |= nTRST;
1602 low_output &= ~nSRST;
1606 low_output |= nSRST;
1609 /* command "set data bits low byte" */
1611 buffer_write(low_output);
1612 buffer_write(low_direction);
1614 /* command "set data bits high byte" */
1616 buffer_write(high_output);
1617 buffer_write(high_direction);
1618 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1622 static void sheevaplug_reset(int trst, int srst)
1625 high_output &= ~nTRST;
1627 high_output |= nTRST;
1630 high_output &= ~nSRSTnOE;
1632 high_output |= nSRSTnOE;
1634 /* command "set data bits high byte" */
1636 buffer_write(high_output);
1637 buffer_write(high_direction);
1638 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1641 static void redbee_reset(int trst, int srst)
1645 tap_set_state(TAP_RESET);
1646 high_output &= ~nTRST;
1650 high_output |= nTRST;
1655 high_output &= ~nSRST;
1659 high_output |= nSRST;
1662 /* command "set data bits low byte" */
1664 buffer_write(high_output);
1665 buffer_write(high_direction);
1666 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1667 "high_direction: 0x%2.2x", trst, srst, high_output,
1671 static int ft2232_execute_runtest(struct jtag_command *cmd)
1675 int predicted_size = 0;
1678 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1679 cmd->cmd.runtest->num_cycles,
1680 tap_state_name(cmd->cmd.runtest->end_state));
1682 /* only send the maximum buffer size that FT2232C can handle */
1684 if (tap_get_state() != TAP_IDLE)
1685 predicted_size += 3;
1686 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1687 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1688 predicted_size += 3;
1689 if (tap_get_end_state() != TAP_IDLE)
1690 predicted_size += 3;
1691 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1693 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1694 retval = ERROR_JTAG_QUEUE_FAILED;
1698 if (tap_get_state() != TAP_IDLE)
1700 move_to_state(TAP_IDLE);
1703 i = cmd->cmd.runtest->num_cycles;
1706 /* there are no state transitions in this code, so omit state tracking */
1708 /* command "Clock Data to TMS/CS Pin (no Read)" */
1712 buffer_write((i > 7) ? 6 : (i - 1));
1717 i -= (i > 7) ? 7 : i;
1718 /* LOG_DEBUG("added TMS scan (no read)"); */
1721 ft2232_end_state(cmd->cmd.runtest->end_state);
1723 if (tap_get_state() != tap_get_end_state())
1725 move_to_state(tap_get_end_state());
1729 DEBUG_JTAG_IO("runtest: %i, end in %s",
1730 cmd->cmd.runtest->num_cycles,
1731 tap_state_name(tap_get_end_state()));
1735 static int ft2232_execute_statemove(struct jtag_command *cmd)
1737 int predicted_size = 0;
1738 int retval = ERROR_OK;
1740 DEBUG_JTAG_IO("statemove end in %s",
1741 tap_state_name(cmd->cmd.statemove->end_state));
1743 /* only send the maximum buffer size that FT2232C can handle */
1745 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1747 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1748 retval = ERROR_JTAG_QUEUE_FAILED;
1752 ft2232_end_state(cmd->cmd.statemove->end_state);
1754 /* For TAP_RESET, ignore the current recorded state. It's often
1755 * wrong at server startup, and this transation is critical whenever
1758 if (tap_get_end_state() == TAP_RESET) {
1759 clock_tms(0x4b, 0xff, 5, 0);
1762 /* shortest-path move to desired end state */
1763 } else if (tap_get_state() != tap_get_end_state())
1765 move_to_state(tap_get_end_state());
1773 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1774 * (or SWD) state machine.
1776 static int ft2232_execute_tms(struct jtag_command *cmd)
1778 int retval = ERROR_OK;
1779 unsigned num_bits = cmd->cmd.tms->num_bits;
1780 const uint8_t *bits = cmd->cmd.tms->bits;
1783 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1785 /* only send the maximum buffer size that FT2232C can handle */
1786 count = 3 * DIV_ROUND_UP(num_bits, 4);
1787 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1788 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1789 retval = ERROR_JTAG_QUEUE_FAILED;
1795 /* Shift out in batches of at most 6 bits; there's a report of an
1796 * FT2232 bug in this area, where shifting exactly 7 bits can make
1797 * problems with TMS signaling for the last clock cycle:
1799 * http://developer.intra2net.com/mailarchive/html/
1800 * libftdi/2009/msg00292.html
1802 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1804 * Note that pathmoves in JTAG are not often seven bits, so that
1805 * isn't a particularly likely situation outside of "special"
1806 * signaling such as switching between JTAG and SWD modes.
1809 if (num_bits <= 6) {
1811 buffer_write(num_bits - 1);
1812 buffer_write(*bits & 0x3f);
1816 /* Yes, this is lazy ... we COULD shift out more data
1817 * bits per operation, but doing it in nybbles is easy
1821 buffer_write(*bits & 0xf);
1824 count = (num_bits > 4) ? 4 : num_bits;
1827 buffer_write(count - 1);
1828 buffer_write((*bits >> 4) & 0xf);
1838 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1840 int predicted_size = 0;
1841 int retval = ERROR_OK;
1843 tap_state_t* path = cmd->cmd.pathmove->path;
1844 int num_states = cmd->cmd.pathmove->num_states;
1846 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1847 tap_state_name(tap_get_state()),
1848 tap_state_name(path[num_states-1]));
1850 /* only send the maximum buffer size that FT2232C can handle */
1851 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1852 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1854 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1855 retval = ERROR_JTAG_QUEUE_FAILED;
1861 ft2232_add_pathmove(path, num_states);
1867 static int ft2232_execute_scan(struct jtag_command *cmd)
1870 int scan_size; /* size of IR or DR scan */
1871 int predicted_size = 0;
1872 int retval = ERROR_OK;
1874 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1876 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1878 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1880 predicted_size = ft2232_predict_scan_out(scan_size, type);
1881 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1883 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1884 /* unsent commands before this */
1885 if (first_unsent != cmd)
1886 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1887 retval = ERROR_JTAG_QUEUE_FAILED;
1889 /* current command */
1890 ft2232_end_state(cmd->cmd.scan->end_state);
1891 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1893 first_unsent = cmd->next;
1898 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1900 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1903 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1904 retval = ERROR_JTAG_QUEUE_FAILED;
1908 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1909 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1910 ft2232_end_state(cmd->cmd.scan->end_state);
1911 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1915 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1916 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1917 tap_state_name(tap_get_end_state()));
1922 static int ft2232_execute_reset(struct jtag_command *cmd)
1925 int predicted_size = 0;
1928 DEBUG_JTAG_IO("reset trst: %i srst %i",
1929 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1931 /* only send the maximum buffer size that FT2232C can handle */
1933 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1935 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1936 retval = ERROR_JTAG_QUEUE_FAILED;
1941 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1943 tap_set_state(TAP_RESET);
1946 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1949 DEBUG_JTAG_IO("trst: %i, srst: %i",
1950 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1954 static int ft2232_execute_sleep(struct jtag_command *cmd)
1959 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1961 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1962 retval = ERROR_JTAG_QUEUE_FAILED;
1963 first_unsent = cmd->next;
1964 jtag_sleep(cmd->cmd.sleep->us);
1965 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1967 tap_state_name(tap_get_state()));
1971 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1976 /* this is only allowed while in a stable state. A check for a stable
1977 * state was done in jtag_add_clocks()
1979 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1980 retval = ERROR_JTAG_QUEUE_FAILED;
1981 DEBUG_JTAG_IO("clocks %i while in %s",
1982 cmd->cmd.stableclocks->num_cycles,
1983 tap_state_name(tap_get_state()));
1987 static int ft2232_execute_command(struct jtag_command *cmd)
1993 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1994 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1995 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
1996 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1997 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1998 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1999 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2001 retval = ft2232_execute_tms(cmd);
2004 LOG_ERROR("BUG: unknown JTAG command type encountered");
2005 retval = ERROR_JTAG_QUEUE_FAILED;
2011 static int ft2232_execute_queue(void)
2013 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2016 first_unsent = cmd; /* next command that has to be sent */
2019 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2020 * that wasn't handled by a caller-provided error handler
2024 ft2232_buffer_size = 0;
2025 ft2232_expect_read = 0;
2027 /* blink, if the current layout has that feature */
2033 if (ft2232_execute_command(cmd) != ERROR_OK)
2034 retval = ERROR_JTAG_QUEUE_FAILED;
2035 /* Start reading input before FT2232 TX buffer fills up */
2037 if (ft2232_expect_read > 256)
2039 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2040 retval = ERROR_JTAG_QUEUE_FAILED;
2045 if (require_send > 0)
2046 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2047 retval = ERROR_JTAG_QUEUE_FAILED;
2052 #if BUILD_FT2232_FTD2XX == 1
2053 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2057 char SerialNumber[16];
2058 char Description[64];
2059 DWORD openex_flags = 0;
2060 char* openex_string = NULL;
2061 uint8_t latency_timer;
2063 if (layout == NULL) {
2064 LOG_WARNING("No ft2232 layout specified'");
2065 return ERROR_JTAG_INIT_FAILED;
2068 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2071 /* Add non-standard Vid/Pid to the linux driver */
2072 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2074 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2078 if (ft2232_device_desc && ft2232_serial)
2080 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2081 ft2232_device_desc = NULL;
2084 if (ft2232_device_desc)
2086 openex_string = ft2232_device_desc;
2087 openex_flags = FT_OPEN_BY_DESCRIPTION;
2089 else if (ft2232_serial)
2091 openex_string = ft2232_serial;
2092 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2096 LOG_ERROR("neither device description nor serial number specified");
2097 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2099 return ERROR_JTAG_INIT_FAILED;
2102 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2103 if (status != FT_OK) {
2104 /* under Win32, the FTD2XX driver appends an "A" to the end
2105 * of the description, if we tried by the desc, then
2106 * try by the alternate "A" description. */
2107 if (openex_string == ft2232_device_desc) {
2108 /* Try the alternate method. */
2109 openex_string = ft2232_device_desc_A;
2110 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2111 if (status == FT_OK) {
2112 /* yea, the "alternate" method worked! */
2114 /* drat, give the user a meaningfull message.
2115 * telling the use we tried *BOTH* methods. */
2116 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2118 ft2232_device_desc_A);
2123 if (status != FT_OK)
2129 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2131 return ERROR_JTAG_INIT_FAILED;
2133 LOG_ERROR("unable to open ftdi device: %lu", status);
2134 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2135 if (status == FT_OK)
2137 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2140 for (i = 0; i < num_devices; i++)
2141 desc_array[i] = malloc(64);
2143 desc_array[num_devices] = NULL;
2145 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2147 if (status == FT_OK)
2149 LOG_ERROR("ListDevices: %lu\n", num_devices);
2150 for (i = 0; i < num_devices; i++)
2151 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2154 for (i = 0; i < num_devices; i++)
2155 free(desc_array[i]);
2161 LOG_ERROR("ListDevices: NONE\n");
2163 return ERROR_JTAG_INIT_FAILED;
2166 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2168 LOG_ERROR("unable to set latency timer: %lu", status);
2169 return ERROR_JTAG_INIT_FAILED;
2172 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2174 LOG_ERROR("unable to get latency timer: %lu", status);
2175 return ERROR_JTAG_INIT_FAILED;
2179 LOG_DEBUG("current latency timer: %i", latency_timer);
2182 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2184 LOG_ERROR("unable to set timeouts: %lu", status);
2185 return ERROR_JTAG_INIT_FAILED;
2188 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2190 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2191 return ERROR_JTAG_INIT_FAILED;
2194 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2196 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2197 return ERROR_JTAG_INIT_FAILED;
2201 static const char* type_str[] =
2202 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2203 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2204 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2205 ? ftdi_device : FT_DEVICE_UNKNOWN;
2206 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2207 LOG_INFO("deviceID: %lu", deviceID);
2208 LOG_INFO("SerialNumber: %s", SerialNumber);
2209 LOG_INFO("Description: %s", Description);
2215 static int ft2232_purge_ftd2xx(void)
2219 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2221 LOG_ERROR("error purging ftd2xx device: %lu", status);
2222 return ERROR_JTAG_INIT_FAILED;
2228 #endif /* BUILD_FT2232_FTD2XX == 1 */
2230 #if BUILD_FT2232_LIBFTDI == 1
2231 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2233 uint8_t latency_timer;
2235 if (layout == NULL) {
2236 LOG_WARNING("No ft2232 layout specified'");
2237 return ERROR_JTAG_INIT_FAILED;
2240 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2241 layout->name, vid, pid);
2243 if (ftdi_init(&ftdic) < 0)
2244 return ERROR_JTAG_INIT_FAILED;
2246 /* default to INTERFACE_A */
2247 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2249 if (ftdi_set_interface(&ftdic, channel) < 0)
2251 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2252 return ERROR_JTAG_INIT_FAILED;
2255 /* context, vendor id, product id */
2256 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2260 LOG_WARNING("unable to open ftdi device (trying more): %s",
2263 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2265 return ERROR_JTAG_INIT_FAILED;
2268 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2269 if (ftdi_usb_reset(&ftdic) < 0)
2271 LOG_ERROR("unable to reset ftdi device");
2272 return ERROR_JTAG_INIT_FAILED;
2275 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2277 LOG_ERROR("unable to set latency timer");
2278 return ERROR_JTAG_INIT_FAILED;
2281 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2283 LOG_ERROR("unable to get latency timer");
2284 return ERROR_JTAG_INIT_FAILED;
2288 LOG_DEBUG("current latency timer: %i", latency_timer);
2291 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2293 ftdi_device = ftdic.type;
2294 static const char* type_str[] =
2295 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2296 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2297 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2298 ? ftdi_device : no_of_known_types;
2299 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2303 static int ft2232_purge_libftdi(void)
2305 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2307 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2308 return ERROR_JTAG_INIT_FAILED;
2314 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2316 static int ft2232_init(void)
2320 uint32_t bytes_written;
2322 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2324 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2328 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2331 if (layout == NULL) {
2332 LOG_WARNING("No ft2232 layout specified'");
2333 return ERROR_JTAG_INIT_FAILED;
2336 for (int i = 0; 1; i++)
2339 * "more indicates that there are more IDs to try, so we should
2340 * not print an error for an ID mismatch (but for anything
2343 * try_more indicates that the error code returned indicates an
2344 * ID mismatch (and nothing else) and that we should proceeed
2345 * with the next ID pair.
2347 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2350 #if BUILD_FT2232_FTD2XX == 1
2351 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2353 #elif BUILD_FT2232_LIBFTDI == 1
2354 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2355 more, &try_more, layout->channel);
2359 if (!more || !try_more)
2363 ft2232_buffer_size = 0;
2364 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2366 if (layout->init() != ERROR_OK)
2367 return ERROR_JTAG_INIT_FAILED;
2369 if (ft2232_device_is_highspeed())
2371 #ifndef BUILD_FT2232_HIGHSPEED
2372 #if BUILD_FT2232_FTD2XX == 1
2373 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2374 #elif BUILD_FT2232_LIBFTDI == 1
2375 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2378 /* make sure the legacy mode is disabled */
2379 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2380 return ERROR_JTAG_INIT_FAILED;
2383 ft2232_speed(jtag_get_speed());
2385 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2386 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2388 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2389 return ERROR_JTAG_INIT_FAILED;
2392 #if BUILD_FT2232_FTD2XX == 1
2393 return ft2232_purge_ftd2xx();
2394 #elif BUILD_FT2232_LIBFTDI == 1
2395 return ft2232_purge_libftdi();
2401 /** Updates defaults for DBUS signals: the four JTAG signals
2402 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2404 static inline void ftx232_dbus_init(void)
2407 low_direction = 0x0b;
2410 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2411 * the four GPIOL signals. Initialization covers value and direction,
2412 * as customized for each layout.
2414 static int ftx232_dbus_write(void)
2417 uint32_t bytes_written;
2419 enum reset_types jtag_reset_config = jtag_get_reset_config();
2420 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2422 low_direction &= ~nTRSTnOE; /* nTRST input */
2423 low_output &= ~nTRST; /* nTRST = 0 */
2427 low_direction |= nTRSTnOE; /* nTRST output */
2428 low_output |= nTRST; /* nTRST = 1 */
2431 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2433 low_direction |= nSRSTnOE; /* nSRST output */
2434 low_output |= nSRST; /* nSRST = 1 */
2438 low_direction &= ~nSRSTnOE; /* nSRST input */
2439 low_output &= ~nSRST; /* nSRST = 0 */
2442 /* initialize low byte for jtag */
2443 buf[0] = 0x80; /* command "set data bits low byte" */
2444 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2445 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2446 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2448 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2450 LOG_ERROR("couldn't initialize FT2232 DBUS");
2451 return ERROR_JTAG_INIT_FAILED;
2457 static int usbjtag_init(void)
2460 * NOTE: This is now _specific_ to the "usbjtag" layout.
2461 * Don't try cram any more layouts into this.
2470 return ftx232_dbus_write();
2473 static int lm3s811_jtag_init(void)
2477 /* There are multiple revisions of LM3S811 eval boards:
2478 * - Rev B (and older?) boards have no SWO trace support.
2479 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2480 * they should use the "luminary_icdi" layout instead.
2487 low_direction = 0x8b;
2489 return ftx232_dbus_write();
2492 static int icdi_jtag_init(void)
2496 /* Most Luminary eval boards support SWO trace output,
2497 * and should use this "luminary_icdi" layout.
2499 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2500 * to switch between JTAG and SWD, or switch the ft2232 UART
2501 * on the second MPSSE channel/interface (BDBUS)
2502 * between (i) the stellaris UART (on Luminary boards)
2503 * or (ii) SWO trace data (generic).
2505 * We come up in JTAG mode and may switch to SWD later (with
2506 * SWO/trace option if SWD is active).
2513 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2514 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2515 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2518 /* GPIOs on second channel/interface (UART) ... */
2519 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2520 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2521 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2526 nSRSTnOE = ICDI_SRST;
2528 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2529 low_output |= ICDI_JTAG_EN;
2530 low_output &= ~ICDI_DBG_ENn;
2532 return ftx232_dbus_write();
2535 static int signalyzer_init(void)
2543 return ftx232_dbus_write();
2546 static int axm0432_jtag_init(void)
2549 uint32_t bytes_written;
2552 low_direction = 0x2b;
2554 /* initialize low byte for jtag */
2555 buf[0] = 0x80; /* command "set data bits low byte" */
2556 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2557 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2558 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2560 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2562 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2563 return ERROR_JTAG_INIT_FAILED;
2566 if (strcmp(layout->name, "axm0432_jtag") == 0)
2569 nTRSTnOE = 0x0; /* No output enable for TRST*/
2571 nSRSTnOE = 0x0; /* No output enable for SRST*/
2575 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2580 high_direction = 0x0c;
2582 enum reset_types jtag_reset_config = jtag_get_reset_config();
2583 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2585 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2589 high_output |= nTRST;
2592 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2594 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2598 high_output |= nSRST;
2601 /* initialize high port */
2602 buf[0] = 0x82; /* command "set data bits high byte" */
2603 buf[1] = high_output; /* value */
2604 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2605 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2607 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2609 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2610 return ERROR_JTAG_INIT_FAILED;
2616 static int redbee_init(void)
2619 uint32_t bytes_written;
2622 low_direction = 0x2b;
2624 /* initialize low byte for jtag */
2625 /* command "set data bits low byte" */
2627 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2628 buf[2] = low_direction;
2629 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2630 buf[1] = low_output;
2631 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2633 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2635 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2636 return ERROR_JTAG_INIT_FAILED;
2640 nTRSTnOE = 0x0; /* No output enable for TRST*/
2642 nSRSTnOE = 0x0; /* No output enable for SRST*/
2645 high_direction = 0x0c;
2647 enum reset_types jtag_reset_config = jtag_get_reset_config();
2648 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2650 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2654 high_output |= nTRST;
2657 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2659 LOG_ERROR("can't set nSRST to push-pull on redbee");
2663 high_output |= nSRST;
2666 /* initialize high port */
2667 buf[0] = 0x82; /* command "set data bits high byte" */
2668 buf[1] = high_output; /* value */
2669 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2670 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2672 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2674 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2675 return ERROR_JTAG_INIT_FAILED;
2681 static int jtagkey_init(void)
2684 uint32_t bytes_written;
2687 low_direction = 0x1b;
2689 /* initialize low byte for jtag */
2690 buf[0] = 0x80; /* command "set data bits low byte" */
2691 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2692 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2693 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2695 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2697 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2698 return ERROR_JTAG_INIT_FAILED;
2701 if (strcmp(layout->name, "jtagkey") == 0)
2708 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2709 || (strcmp(layout->name, "oocdlink") == 0))
2718 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2723 high_direction = 0x0f;
2725 enum reset_types jtag_reset_config = jtag_get_reset_config();
2726 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2728 high_output |= nTRSTnOE;
2729 high_output &= ~nTRST;
2733 high_output &= ~nTRSTnOE;
2734 high_output |= nTRST;
2737 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2739 high_output &= ~nSRSTnOE;
2740 high_output |= nSRST;
2744 high_output |= nSRSTnOE;
2745 high_output &= ~nSRST;
2748 /* initialize high port */
2749 buf[0] = 0x82; /* command "set data bits high byte" */
2750 buf[1] = high_output; /* value */
2751 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2752 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2754 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2756 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2757 return ERROR_JTAG_INIT_FAILED;
2763 static int olimex_jtag_init(void)
2766 uint32_t bytes_written;
2769 low_direction = 0x1b;
2771 /* initialize low byte for jtag */
2772 buf[0] = 0x80; /* command "set data bits low byte" */
2773 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2774 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2775 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2777 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2779 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2780 return ERROR_JTAG_INIT_FAILED;
2786 nSRSTnOE = 0x00; /* no output enable for nSRST */
2789 high_direction = 0x0f;
2791 enum reset_types jtag_reset_config = jtag_get_reset_config();
2792 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2794 high_output |= nTRSTnOE;
2795 high_output &= ~nTRST;
2799 high_output &= ~nTRSTnOE;
2800 high_output |= nTRST;
2803 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2805 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2809 high_output &= ~nSRST;
2812 /* turn red LED on */
2813 high_output |= 0x08;
2815 /* initialize high port */
2816 buf[0] = 0x82; /* command "set data bits high byte" */
2817 buf[1] = high_output; /* value */
2818 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2819 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2821 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2823 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2824 return ERROR_JTAG_INIT_FAILED;
2830 static int flyswatter_init(void)
2833 uint32_t bytes_written;
2836 low_direction = 0xfb;
2838 /* initialize low byte for jtag */
2839 buf[0] = 0x80; /* command "set data bits low byte" */
2840 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2841 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2842 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2844 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2846 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2847 return ERROR_JTAG_INIT_FAILED;
2851 nTRSTnOE = 0x0; /* not output enable for nTRST */
2853 nSRSTnOE = 0x00; /* no output enable for nSRST */
2856 high_direction = 0x0c;
2858 /* turn red LED3 on, LED2 off */
2859 high_output |= 0x08;
2861 /* initialize high port */
2862 buf[0] = 0x82; /* command "set data bits high byte" */
2863 buf[1] = high_output; /* value */
2864 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2865 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2867 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2869 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2870 return ERROR_JTAG_INIT_FAILED;
2876 static int turtle_init(void)
2879 uint32_t bytes_written;
2882 low_direction = 0x5b;
2884 /* initialize low byte for jtag */
2885 buf[0] = 0x80; /* command "set data bits low byte" */
2886 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2887 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2888 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2890 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2892 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2893 return ERROR_JTAG_INIT_FAILED;
2899 high_direction = 0x0C;
2901 /* initialize high port */
2902 buf[0] = 0x82; /* command "set data bits high byte" */
2903 buf[1] = high_output;
2904 buf[2] = high_direction;
2905 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2907 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2909 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2910 return ERROR_JTAG_INIT_FAILED;
2916 static int comstick_init(void)
2919 uint32_t bytes_written;
2922 low_direction = 0x0b;
2924 /* initialize low byte for jtag */
2925 buf[0] = 0x80; /* command "set data bits low byte" */
2926 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2927 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2928 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2930 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2932 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2933 return ERROR_JTAG_INIT_FAILED;
2937 nTRSTnOE = 0x00; /* no output enable for nTRST */
2939 nSRSTnOE = 0x00; /* no output enable for nSRST */
2942 high_direction = 0x03;
2944 /* initialize high port */
2945 buf[0] = 0x82; /* command "set data bits high byte" */
2946 buf[1] = high_output;
2947 buf[2] = high_direction;
2948 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2950 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2952 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2953 return ERROR_JTAG_INIT_FAILED;
2959 static int stm32stick_init(void)
2962 uint32_t bytes_written;
2965 low_direction = 0x8b;
2967 /* initialize low byte for jtag */
2968 buf[0] = 0x80; /* command "set data bits low byte" */
2969 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2970 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2971 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2973 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2975 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2976 return ERROR_JTAG_INIT_FAILED;
2980 nTRSTnOE = 0x00; /* no output enable for nTRST */
2982 nSRSTnOE = 0x00; /* no output enable for nSRST */
2985 high_direction = 0x03;
2987 /* initialize high port */
2988 buf[0] = 0x82; /* command "set data bits high byte" */
2989 buf[1] = high_output;
2990 buf[2] = high_direction;
2991 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2993 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2995 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2996 return ERROR_JTAG_INIT_FAILED;
3002 static int sheevaplug_init(void)
3005 uint32_t bytes_written;
3008 low_direction = 0x1b;
3010 /* initialize low byte for jtag */
3011 buf[0] = 0x80; /* command "set data bits low byte" */
3012 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3013 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3014 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3016 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3018 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3019 return ERROR_JTAG_INIT_FAILED;
3028 high_direction = 0x0f;
3030 /* nTRST is always push-pull */
3031 high_output &= ~nTRSTnOE;
3032 high_output |= nTRST;
3034 /* nSRST is always open-drain */
3035 high_output |= nSRSTnOE;
3036 high_output &= ~nSRST;
3038 /* initialize high port */
3039 buf[0] = 0x82; /* command "set data bits high byte" */
3040 buf[1] = high_output; /* value */
3041 buf[2] = high_direction; /* all outputs - xRST */
3042 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3044 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3046 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3047 return ERROR_JTAG_INIT_FAILED;
3053 static int cortino_jtag_init(void)
3056 uint32_t bytes_written;
3059 low_direction = 0x1b;
3061 /* initialize low byte for jtag */
3062 buf[0] = 0x80; /* command "set data bits low byte" */
3063 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3064 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3065 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3067 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3069 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3070 return ERROR_JTAG_INIT_FAILED;
3074 nTRSTnOE = 0x00; /* no output enable for nTRST */
3076 nSRSTnOE = 0x00; /* no output enable for nSRST */
3079 high_direction = 0x03;
3081 /* initialize high port */
3082 buf[0] = 0x82; /* command "set data bits high byte" */
3083 buf[1] = high_output;
3084 buf[2] = high_direction;
3085 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3087 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3089 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3090 return ERROR_JTAG_INIT_FAILED;
3096 static void olimex_jtag_blink(void)
3098 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3099 * ACBUS3 is bit 3 of the GPIOH port
3101 if (high_output & 0x08)
3103 /* set port pin high */
3104 high_output &= 0x07;
3108 /* set port pin low */
3109 high_output |= 0x08;
3113 buffer_write(high_output);
3114 buffer_write(high_direction);
3117 static void flyswatter_jtag_blink(void)
3120 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3122 high_output ^= 0x0c;
3125 buffer_write(high_output);
3126 buffer_write(high_direction);
3129 static void turtle_jtag_blink(void)
3132 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3134 if (high_output & 0x08)
3144 buffer_write(high_output);
3145 buffer_write(high_direction);
3148 static int ft2232_quit(void)
3150 #if BUILD_FT2232_FTD2XX == 1
3153 status = FT_Close(ftdih);
3154 #elif BUILD_FT2232_LIBFTDI == 1
3155 ftdi_usb_close(&ftdic);
3157 ftdi_deinit(&ftdic);
3160 free(ft2232_buffer);
3161 ft2232_buffer = NULL;
3166 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3172 ft2232_device_desc = strdup(CMD_ARGV[0]);
3173 cp = strchr(ft2232_device_desc, 0);
3174 /* under Win32, the FTD2XX driver appends an "A" to the end
3175 * of the description, this examines the given desc
3176 * and creates the 'missing' _A or non_A variable. */
3177 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3178 /* it was, so make this the "A" version. */
3179 ft2232_device_desc_A = ft2232_device_desc;
3180 /* and *CREATE* the non-A version. */
3181 strcpy(buf, ft2232_device_desc);
3182 cp = strchr(buf, 0);
3184 ft2232_device_desc = strdup(buf);
3186 /* <space > A not defined
3188 sprintf(buf, "%s A", ft2232_device_desc);
3189 ft2232_device_desc_A = strdup(buf);
3194 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3200 COMMAND_HANDLER(ft2232_handle_serial_command)
3204 ft2232_serial = strdup(CMD_ARGV[0]);
3208 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3214 COMMAND_HANDLER(ft2232_handle_layout_command)
3216 if (CMD_ARGC != 1) {
3217 LOG_ERROR("Need exactly one argument to ft2232_layout");
3222 LOG_ERROR("already specified ft2232_layout %s",
3224 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3229 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3230 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3236 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3240 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3242 if (CMD_ARGC > MAX_USB_IDS * 2)
3244 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3245 "(maximum is %d pairs)", MAX_USB_IDS);
3246 CMD_ARGC = MAX_USB_IDS * 2;
3248 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3250 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3252 return ERROR_COMMAND_SYNTAX_ERROR;
3253 /* remove the incomplete trailing id */
3258 for (i = 0; i < CMD_ARGC; i += 2)
3260 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3261 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3265 * Explicitly terminate, in case there are multiples instances of
3268 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3273 COMMAND_HANDLER(ft2232_handle_latency_command)
3277 ft2232_latency = atoi(CMD_ARGV[0]);
3281 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3287 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3291 /* 7 bits of either ones or zeros. */
3292 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3294 while (num_cycles > 0)
3296 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3297 * at most 7 bits per invocation. Here we invoke it potentially
3300 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3302 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3304 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3305 retval = ERROR_JTAG_QUEUE_FAILED;
3310 /* there are no state transitions in this code, so omit state tracking */
3312 /* command "Clock Data to TMS/CS Pin (no Read)" */
3316 buffer_write(bitcount_per_command - 1);
3318 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3323 num_cycles -= bitcount_per_command;
3329 /* ---------------------------------------------------------------------
3330 * Support for IceBear JTAG adapter from Section5:
3331 * http://section5.ch/icebear
3333 * Author: Sten, debian@sansys-electronic.com
3336 /* Icebear pin layout
3338 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3339 * GND GND | 4 3| n.c.
3340 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3341 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3342 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3343 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3344 * ADBUS2 TDO |14 13| GND GND
3346 * ADBUS0 O L TCK ACBUS0 GND
3347 * ADBUS1 O L TDI ACBUS1 GND
3348 * ADBUS2 I TDO ACBUS2 n.c.
3349 * ADBUS3 O H TMS ACBUS3 n.c.
3355 static int icebear_jtag_init(void) {
3357 uint32_t bytes_written;
3359 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3360 low_output = 0x08; /* high: TMS; low: TCK TDI */
3364 enum reset_types jtag_reset_config = jtag_get_reset_config();
3365 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3366 low_direction &= ~nTRST; /* nTRST high impedance */
3369 low_direction |= nTRST;
3370 low_output |= nTRST;
3373 low_direction |= nSRST;
3374 low_output |= nSRST;
3376 /* initialize low byte for jtag */
3377 buf[0] = 0x80; /* command "set data bits low byte" */
3378 buf[1] = low_output;
3379 buf[2] = low_direction;
3380 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3382 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3383 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3384 return ERROR_JTAG_INIT_FAILED;
3388 high_direction = 0x00;
3391 /* initialize high port */
3392 buf[0] = 0x82; /* command "set data bits high byte" */
3393 buf[1] = high_output; /* value */
3394 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3395 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3397 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3398 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3399 return ERROR_JTAG_INIT_FAILED;
3405 static void icebear_jtag_reset(int trst, int srst) {
3408 low_direction |= nTRST;
3409 low_output &= ~nTRST;
3411 else if (trst == 0) {
3412 enum reset_types jtag_reset_config = jtag_get_reset_config();
3413 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3414 low_direction &= ~nTRST;
3416 low_output |= nTRST;
3420 low_output &= ~nSRST;
3422 else if (srst == 0) {
3423 low_output |= nSRST;
3426 /* command "set data bits low byte" */
3428 buffer_write(low_output);
3429 buffer_write(low_direction);
3431 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3434 /* ---------------------------------------------------------------------
3435 * Support for Signalyzer H2 and Signalyzer H4
3436 * JTAG adapter from Xverve Technologies Inc.
3437 * http://www.signalyzer.com or http://www.xverve.com
3439 * Author: Oleg Seiljus, oleg@signalyzer.com
3441 static unsigned char signalyzer_h_side;
3442 static unsigned int signalyzer_h_adapter_type;
3444 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3446 #if BUILD_FT2232_FTD2XX == 1
3447 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3450 #define SIGNALYZER_COMMAND_ADDR 128
3451 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3453 #define SIGNALYZER_COMMAND_VERSION 0x41
3454 #define SIGNALYZER_COMMAND_RESET 0x42
3455 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3456 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3457 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3458 #define SIGNALYZER_COMMAND_LED_SET 0x53
3459 #define SIGNALYZER_COMMAND_ADC 0x54
3460 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3461 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3462 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3463 #define SIGNALYZER_COMMAND_I2C 0x58
3465 #define SIGNALYZER_CHAN_A 1
3466 #define SIGNALYZER_CHAN_B 2
3467 /* LEDS use channel C */
3468 #define SIGNALYZER_CHAN_C 4
3470 #define SIGNALYZER_LED_GREEN 1
3471 #define SIGNALYZER_LED_RED 2
3473 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3474 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3475 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3476 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3477 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3480 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3482 #if BUILD_FT2232_FTD2XX == 1
3483 return FT_WriteEE(ftdih, address, value);
3484 #elif BUILD_FT2232_LIBFTDI == 1
3489 #if BUILD_FT2232_FTD2XX == 1
3490 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3492 return FT_ReadEE(ftdih, address, value);
3496 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3497 int on_time_ms, int off_time_ms, unsigned char cycles)
3499 unsigned char on_time;
3500 unsigned char off_time;
3502 if (on_time_ms < 0xFFFF)
3503 on_time = (unsigned char)(on_time_ms / 62);
3507 off_time = (unsigned char)(off_time_ms / 62);
3509 #if BUILD_FT2232_FTD2XX == 1
3512 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3513 ((uint32_t)(channel << 8) | led))) != FT_OK)
3515 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3516 return ERROR_JTAG_DEVICE_ERROR;
3519 if ((status = signalyzer_h_ctrl_write(
3520 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3521 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3523 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3524 return ERROR_JTAG_DEVICE_ERROR;
3527 if ((status = signalyzer_h_ctrl_write(
3528 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3529 ((uint32_t)cycles))) != FT_OK)
3531 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3532 return ERROR_JTAG_DEVICE_ERROR;
3535 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3536 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3538 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3539 return ERROR_JTAG_DEVICE_ERROR;
3543 #elif BUILD_FT2232_LIBFTDI == 1
3546 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3547 ((uint32_t)(channel << 8) | led))) < 0)
3549 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3550 ftdi_get_error_string(&ftdic));
3551 return ERROR_JTAG_DEVICE_ERROR;
3554 if ((retval = signalyzer_h_ctrl_write(
3555 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3556 ((uint32_t)(on_time << 8) | off_time))) < 0)
3558 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3559 ftdi_get_error_string(&ftdic));
3560 return ERROR_JTAG_DEVICE_ERROR;
3563 if ((retval = signalyzer_h_ctrl_write(
3564 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3565 (uint32_t)cycles)) < 0)
3567 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3568 ftdi_get_error_string(&ftdic));
3569 return ERROR_JTAG_DEVICE_ERROR;
3572 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3573 SIGNALYZER_COMMAND_LED_SET)) < 0)
3575 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3576 ftdi_get_error_string(&ftdic));
3577 return ERROR_JTAG_DEVICE_ERROR;
3584 static int signalyzer_h_init(void)
3586 #if BUILD_FT2232_FTD2XX == 1
3593 uint16_t read_buf[12] = { 0 };
3595 uint32_t bytes_written;
3597 /* turn on center green led */
3598 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3599 0xFFFF, 0x00, 0x00);
3601 /* determine what channel config wants to open
3602 * TODO: change me... current implementation is made to work
3603 * with openocd description parsing.
3605 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3609 signalyzer_h_side = *(end_of_desc - 1);
3610 if (signalyzer_h_side == 'B')
3611 signalyzer_h_side = SIGNALYZER_CHAN_B;
3613 signalyzer_h_side = SIGNALYZER_CHAN_A;
3617 LOG_ERROR("No Channel was specified");
3621 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3624 #if BUILD_FT2232_FTD2XX == 1
3625 /* read signalyzer versionining information */
3626 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3627 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3629 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3630 return ERROR_JTAG_DEVICE_ERROR;
3633 for (i = 0; i < 10; i++)
3635 if ((status = signalyzer_h_ctrl_read(
3636 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3637 &read_buf[i])) != FT_OK)
3639 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3641 return ERROR_JTAG_DEVICE_ERROR;
3645 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3646 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3647 read_buf[4], read_buf[5], read_buf[6]);
3649 /* set gpio register */
3650 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3651 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3653 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3654 return ERROR_JTAG_DEVICE_ERROR;
3657 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3660 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3661 return ERROR_JTAG_DEVICE_ERROR;
3664 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3665 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3667 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3668 return ERROR_JTAG_DEVICE_ERROR;
3671 /* read adapter type information */
3672 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3673 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3675 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3676 return ERROR_JTAG_DEVICE_ERROR;
3679 if ((status = signalyzer_h_ctrl_write(
3680 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3682 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3683 return ERROR_JTAG_DEVICE_ERROR;
3686 if ((status = signalyzer_h_ctrl_write(
3687 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3689 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3690 return ERROR_JTAG_DEVICE_ERROR;
3693 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3694 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3696 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3697 return ERROR_JTAG_DEVICE_ERROR;
3702 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3703 &read_buf[0])) != FT_OK)
3705 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3706 return ERROR_JTAG_DEVICE_ERROR;
3709 if (read_buf[0] != 0x0498)
3710 signalyzer_h_adapter_type = 0x0000;
3713 for (i = 0; i < 4; i++)
3715 if ((status = signalyzer_h_ctrl_read(
3716 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3717 &read_buf[i])) != FT_OK)
3719 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3721 return ERROR_JTAG_DEVICE_ERROR;
3725 signalyzer_h_adapter_type = read_buf[0];
3728 #elif BUILD_FT2232_LIBFTDI == 1
3729 /* currently libftdi does not allow reading individual eeprom
3730 * locations, therefore adapter type cannot be detected.
3731 * override with most common type
3733 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3736 enum reset_types jtag_reset_config = jtag_get_reset_config();
3738 /* ADAPTOR: EM_LT16_A */
3739 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3741 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3742 "detected. (HW: %2x).", (read_buf[1] >> 8));
3750 low_direction = 0x1b;
3753 high_direction = 0x0;
3755 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3757 low_direction &= ~nTRSTnOE; /* nTRST input */
3758 low_output &= ~nTRST; /* nTRST = 0 */
3762 low_direction |= nTRSTnOE; /* nTRST output */
3763 low_output |= nTRST; /* nTRST = 1 */
3766 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3768 low_direction |= nSRSTnOE; /* nSRST output */
3769 low_output |= nSRST; /* nSRST = 1 */
3773 low_direction &= ~nSRSTnOE; /* nSRST input */
3774 low_output &= ~nSRST; /* nSRST = 0 */
3777 #if BUILD_FT2232_FTD2XX == 1
3778 /* enable power to the module */
3779 if ((status = signalyzer_h_ctrl_write(
3780 SIGNALYZER_DATA_BUFFER_ADDR,
3781 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3784 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3786 return ERROR_JTAG_DEVICE_ERROR;
3789 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3790 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3792 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3794 return ERROR_JTAG_DEVICE_ERROR;
3797 /* set gpio mode register */
3798 if ((status = signalyzer_h_ctrl_write(
3799 SIGNALYZER_DATA_BUFFER_ADDR,
3800 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3802 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3804 return ERROR_JTAG_DEVICE_ERROR;
3807 if ((status = signalyzer_h_ctrl_write(
3808 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3811 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3813 return ERROR_JTAG_DEVICE_ERROR;
3816 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3817 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3819 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3821 return ERROR_JTAG_DEVICE_ERROR;
3824 /* set gpio register */
3825 if ((status = signalyzer_h_ctrl_write(
3826 SIGNALYZER_DATA_BUFFER_ADDR,
3827 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3829 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3831 return ERROR_JTAG_DEVICE_ERROR;
3834 if ((status = signalyzer_h_ctrl_write(
3835 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3838 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3840 return ERROR_JTAG_DEVICE_ERROR;
3843 if ((status = signalyzer_h_ctrl_write(
3844 SIGNALYZER_COMMAND_ADDR,
3845 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3847 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3849 return ERROR_JTAG_DEVICE_ERROR;
3854 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3855 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3856 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3857 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3858 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3860 if (signalyzer_h_adapter_type
3861 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3862 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3863 "detected. (HW: %2x).", (read_buf[1] >> 8));
3864 else if (signalyzer_h_adapter_type
3865 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3866 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3867 "(ARM JTAG with PSU) detected. (HW: %2x).",
3868 (read_buf[1] >> 8));
3869 else if (signalyzer_h_adapter_type
3870 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3871 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3872 "detected. (HW: %2x).", (read_buf[1] >> 8));
3873 else if (signalyzer_h_adapter_type
3874 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3875 LOG_INFO("Signalyzer: EM-JTAG-P "
3876 "(Generic JTAG with PSU) detected. (HW: %2x).",
3877 (read_buf[1] >> 8));
3885 low_direction = 0x1b;
3888 high_direction = 0x1f;
3890 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3892 high_output |= nTRSTnOE;
3893 high_output &= ~nTRST;
3897 high_output &= ~nTRSTnOE;
3898 high_output |= nTRST;
3901 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3903 high_output &= ~nSRSTnOE;
3904 high_output |= nSRST;
3908 high_output |= nSRSTnOE;
3909 high_output &= ~nSRST;
3912 #if BUILD_FT2232_FTD2XX == 1
3913 /* enable power to the module */
3914 if ((status = signalyzer_h_ctrl_write(
3915 SIGNALYZER_DATA_BUFFER_ADDR,
3916 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3919 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3921 return ERROR_JTAG_DEVICE_ERROR;
3924 if ((status = signalyzer_h_ctrl_write(
3925 SIGNALYZER_COMMAND_ADDR,
3926 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3928 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3930 return ERROR_JTAG_DEVICE_ERROR;
3933 /* set gpio mode register (IO_16 and IO_17 set as analog
3934 * inputs, other is gpio)
3936 if ((status = signalyzer_h_ctrl_write(
3937 SIGNALYZER_DATA_BUFFER_ADDR,
3938 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3940 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3942 return ERROR_JTAG_DEVICE_ERROR;
3945 if ((status = signalyzer_h_ctrl_write(
3946 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3949 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3951 return ERROR_JTAG_DEVICE_ERROR;
3954 if ((status = signalyzer_h_ctrl_write(
3955 SIGNALYZER_COMMAND_ADDR,
3956 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3958 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3960 return ERROR_JTAG_DEVICE_ERROR;
3963 /* set gpio register (all inputs, for -P modules,
3964 * PSU will be turned off)
3966 if ((status = signalyzer_h_ctrl_write(
3967 SIGNALYZER_DATA_BUFFER_ADDR,
3968 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3970 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3972 return ERROR_JTAG_DEVICE_ERROR;
3975 if ((status = signalyzer_h_ctrl_write(
3976 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3979 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3981 return ERROR_JTAG_DEVICE_ERROR;
3984 if ((status = signalyzer_h_ctrl_write(
3985 SIGNALYZER_COMMAND_ADDR,
3986 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3988 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3990 return ERROR_JTAG_DEVICE_ERROR;
3995 else if (signalyzer_h_adapter_type == 0x0000)
3997 LOG_INFO("Signalyzer: No external modules were detected.");
4005 low_direction = 0x1b;
4008 high_direction = 0x0;
4010 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4012 low_direction &= ~nTRSTnOE; /* nTRST input */
4013 low_output &= ~nTRST; /* nTRST = 0 */
4017 low_direction |= nTRSTnOE; /* nTRST output */
4018 low_output |= nTRST; /* nTRST = 1 */
4021 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4023 low_direction |= nSRSTnOE; /* nSRST output */
4024 low_output |= nSRST; /* nSRST = 1 */
4028 low_direction &= ~nSRSTnOE; /* nSRST input */
4029 low_output &= ~nSRST; /* nSRST = 0 */
4034 LOG_ERROR("Unknown module type is detected: %.4x",
4035 signalyzer_h_adapter_type);
4036 return ERROR_JTAG_DEVICE_ERROR;
4039 /* initialize low byte of controller for jtag operation */
4041 buf[1] = low_output;
4042 buf[2] = low_direction;
4044 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4046 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4047 return ERROR_JTAG_INIT_FAILED;
4050 #if BUILD_FT2232_FTD2XX == 1
4051 if (ftdi_device == FT_DEVICE_2232H)
4053 /* initialize high byte of controller for jtag operation */
4055 buf[1] = high_output;
4056 buf[2] = high_direction;
4058 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4060 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4061 return ERROR_JTAG_INIT_FAILED;
4064 #elif BUILD_FT2232_LIBFTDI == 1
4065 if (ftdi_device == TYPE_2232H)
4067 /* initialize high byte of controller for jtag operation */
4069 buf[1] = high_output;
4070 buf[2] = high_direction;
4072 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4074 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4075 return ERROR_JTAG_INIT_FAILED;
4082 static void signalyzer_h_reset(int trst, int srst)
4084 enum reset_types jtag_reset_config = jtag_get_reset_config();
4086 /* ADAPTOR: EM_LT16_A */
4087 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4091 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4092 /* switch to output pin (output is low) */
4093 low_direction |= nTRSTnOE;
4095 /* switch output low */
4096 low_output &= ~nTRST;
4100 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4101 /* switch to input pin (high-Z + internal
4102 * and external pullup) */
4103 low_direction &= ~nTRSTnOE;
4105 /* switch output high */
4106 low_output |= nTRST;
4111 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4112 /* switch output low */
4113 low_output &= ~nSRST;
4115 /* switch to output pin (output is low) */
4116 low_direction |= nSRSTnOE;
4120 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4121 /* switch output high */
4122 low_output |= nSRST;
4124 /* switch to input pin (high-Z) */
4125 low_direction &= ~nSRSTnOE;
4128 /* command "set data bits low byte" */
4130 buffer_write(low_output);
4131 buffer_write(low_direction);
4132 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4133 "low_direction: 0x%2.2x",
4134 trst, srst, low_output, low_direction);
4136 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4137 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4138 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4139 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4140 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4144 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4145 high_output &= ~nTRSTnOE;
4147 high_output &= ~nTRST;
4151 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4152 high_output |= nTRSTnOE;
4154 high_output |= nTRST;
4159 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4160 high_output &= ~nSRST;
4162 high_output &= ~nSRSTnOE;
4166 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4167 high_output |= nSRST;
4169 high_output |= nSRSTnOE;
4172 /* command "set data bits high byte" */
4174 buffer_write(high_output);
4175 buffer_write(high_direction);
4176 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4177 "high_direction: 0x%2.2x",
4178 trst, srst, high_output, high_direction);
4180 else if (signalyzer_h_adapter_type == 0x0000)
4184 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4185 /* switch to output pin (output is low) */
4186 low_direction |= nTRSTnOE;
4188 /* switch output low */
4189 low_output &= ~nTRST;
4193 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4194 /* switch to input pin (high-Z + internal
4195 * and external pullup) */
4196 low_direction &= ~nTRSTnOE;
4198 /* switch output high */
4199 low_output |= nTRST;
4204 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4205 /* switch output low */
4206 low_output &= ~nSRST;
4208 /* switch to output pin (output is low) */
4209 low_direction |= nSRSTnOE;
4213 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4214 /* switch output high */
4215 low_output |= nSRST;
4217 /* switch to input pin (high-Z) */
4218 low_direction &= ~nSRSTnOE;
4221 /* command "set data bits low byte" */
4223 buffer_write(low_output);
4224 buffer_write(low_direction);
4225 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4226 "low_direction: 0x%2.2x",
4227 trst, srst, low_output, low_direction);
4231 static void signalyzer_h_blink(void)
4233 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4236 /********************************************************************
4237 * Support for KT-LINK
4238 * JTAG adapter from KRISTECH
4239 * http://www.kristech.eu
4240 *******************************************************************/
4241 static int ktlink_init(void)
4244 uint32_t bytes_written;
4245 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4247 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4248 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4250 // initialize low port
4251 buf[0] = 0x80; // command "set data bits low byte"
4252 buf[1] = low_output;
4253 buf[2] = low_direction;
4254 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4256 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4258 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4259 return ERROR_JTAG_INIT_FAILED;
4267 high_output = 0x80; // turn LED on
4268 high_direction = 0xFF; // all outputs
4270 enum reset_types jtag_reset_config = jtag_get_reset_config();
4272 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4273 high_output |= nTRSTnOE;
4274 high_output &= ~nTRST;
4276 high_output &= ~nTRSTnOE;
4277 high_output |= nTRST;
4280 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4281 high_output &= ~nSRSTnOE;
4282 high_output |= nSRST;
4284 high_output |= nSRSTnOE;
4285 high_output &= ~nSRST;
4288 // initialize high port
4289 buf[0] = 0x82; // command "set data bits high byte"
4290 buf[1] = high_output; // value
4291 buf[2] = high_direction;
4292 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4294 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4296 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4297 return ERROR_JTAG_INIT_FAILED;
4303 static void ktlink_reset(int trst, int srst)
4305 enum reset_types jtag_reset_config = jtag_get_reset_config();
4308 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4309 high_output &= ~nTRSTnOE;
4311 high_output &= ~nTRST;
4312 } else if (trst == 0) {
4313 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4314 high_output |= nTRSTnOE;
4316 high_output |= nTRST;
4320 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4321 high_output &= ~nSRST;
4323 high_output &= ~nSRSTnOE;
4324 } else if (srst == 0) {
4325 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4326 high_output |= nSRST;
4328 high_output |= nSRSTnOE;
4331 buffer_write(0x82); // command "set data bits high byte"
4332 buffer_write(high_output);
4333 buffer_write(high_direction);
4334 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4337 static void ktlink_blink(void)
4339 /* LED connected to ACBUS7 */
4340 if (high_output & 0x80)
4341 high_output &= 0x7F;
4343 high_output |= 0x80;
4345 buffer_write(0x82); // command "set data bits high byte"
4346 buffer_write(high_output);
4347 buffer_write(high_direction);
4350 static const struct command_registration ft2232_command_handlers[] = {
4352 .name = "ft2232_device_desc",
4353 .handler = &ft2232_handle_device_desc_command,
4354 .mode = COMMAND_CONFIG,
4355 .help = "set the USB device description of the FTDI FT2232 device",
4356 .usage = "description_string",
4359 .name = "ft2232_serial",
4360 .handler = &ft2232_handle_serial_command,
4361 .mode = COMMAND_CONFIG,
4362 .help = "set the serial number of the FTDI FT2232 device",
4363 .usage = "serial_string",
4366 .name = "ft2232_layout",
4367 .handler = &ft2232_handle_layout_command,
4368 .mode = COMMAND_CONFIG,
4369 .help = "set the layout of the FT2232 GPIO signals used "
4370 "to control output-enables and reset signals",
4371 .usage = "layout_name",
4374 .name = "ft2232_vid_pid",
4375 .handler = &ft2232_handle_vid_pid_command,
4376 .mode = COMMAND_CONFIG,
4377 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4378 .usage = "(vid pid)* ",
4381 .name = "ft2232_latency",
4382 .handler = &ft2232_handle_latency_command,
4383 .mode = COMMAND_CONFIG,
4384 .help = "set the FT2232 latency timer to a new value",
4387 COMMAND_REGISTRATION_DONE
4390 struct jtag_interface ft2232_interface = {
4392 .supported = DEBUG_CAP_TMS_SEQ,
4393 .commands = ft2232_command_handlers,
4394 .transports = jtag_only,
4396 .init = ft2232_init,
4397 .quit = ft2232_quit,
4398 .speed = ft2232_speed,
4399 .speed_div = ft2232_speed_div,
4401 .execute_queue = ft2232_execute_queue,