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 <transport/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
102 #include "ftd2xx_common.h"
104 enum ftdi_interface {
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_x232H_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, FT_DEVICE_232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum ftdi_chip_type { TYPE_2232H = 4, TYPE_4232H = 5, TYPE_232H = 6 };
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;
155 static char *ft2232_device_desc;
156 static char *ft2232_serial;
157 static uint8_t ft2232_latency = 2;
158 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
159 static int ft2232_channel = INTERFACE_ANY;
161 #define MAX_USB_IDS 8
162 /* vid = pid = 0 marks the end of the list */
163 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
164 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
166 struct ft2232_layout {
169 void (*reset)(int trst, int srst);
174 /* init procedures for supported layouts */
175 static int usbjtag_init(void);
176 static int jtagkey_init(void);
177 static int lm3s811_jtag_init(void);
178 static int icdi_jtag_init(void);
179 static int olimex_jtag_init(void);
180 static int flyswatter1_init(void);
181 static int flyswatter2_init(void);
182 static int minimodule_init(void);
183 static int turtle_init(void);
184 static int comstick_init(void);
185 static int stm32stick_init(void);
186 static int axm0432_jtag_init(void);
187 static int sheevaplug_init(void);
188 static int icebear_jtag_init(void);
189 static int cortino_jtag_init(void);
190 static int signalyzer_init(void);
191 static int signalyzer_h_init(void);
192 static int ktlink_init(void);
193 static int redbee_init(void);
194 static int lisa_l_init(void);
195 static int flossjtag_init(void);
196 static int xds100v2_init(void);
197 static int digilent_hs1_init(void);
199 /* reset procedures for supported layouts */
200 static void ftx23_reset(int trst, int srst);
201 static void jtagkey_reset(int trst, int srst);
202 static void olimex_jtag_reset(int trst, int srst);
203 static void flyswatter1_reset(int trst, int srst);
204 static void flyswatter2_reset(int trst, int srst);
205 static void minimodule_reset(int trst, int srst);
206 static void turtle_reset(int trst, int srst);
207 static void comstick_reset(int trst, int srst);
208 static void stm32stick_reset(int trst, int srst);
209 static void axm0432_jtag_reset(int trst, int srst);
210 static void sheevaplug_reset(int trst, int srst);
211 static void icebear_jtag_reset(int trst, int srst);
212 static void signalyzer_h_reset(int trst, int srst);
213 static void ktlink_reset(int trst, int srst);
214 static void redbee_reset(int trst, int srst);
215 static void xds100v2_reset(int trst, int srst);
216 static void digilent_hs1_reset(int trst, int srst);
218 /* blink procedures for layouts that support a blinking led */
219 static void olimex_jtag_blink(void);
220 static void flyswatter1_jtag_blink(void);
221 static void flyswatter2_jtag_blink(void);
222 static void turtle_jtag_blink(void);
223 static void signalyzer_h_blink(void);
224 static void ktlink_blink(void);
225 static void lisa_l_blink(void);
226 static void flossjtag_blink(void);
228 /* common transport support options */
230 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
232 static const struct ft2232_layout ft2232_layouts[] = {
234 .init = usbjtag_init,
235 .reset = ftx23_reset,
238 .init = jtagkey_init,
239 .reset = jtagkey_reset,
241 { .name = "jtagkey_prototype_v1",
242 .init = jtagkey_init,
243 .reset = jtagkey_reset,
245 { .name = "oocdlink",
246 .init = jtagkey_init,
247 .reset = jtagkey_reset,
249 { .name = "signalyzer",
250 .init = signalyzer_init,
251 .reset = ftx23_reset,
253 { .name = "evb_lm3s811",
254 .init = lm3s811_jtag_init,
255 .reset = ftx23_reset,
257 { .name = "luminary_icdi",
258 .init = icdi_jtag_init,
259 .reset = ftx23_reset,
261 { .name = "olimex-jtag",
262 .init = olimex_jtag_init,
263 .reset = olimex_jtag_reset,
264 .blink = olimex_jtag_blink
266 { .name = "flyswatter",
267 .init = flyswatter1_init,
268 .reset = flyswatter1_reset,
269 .blink = flyswatter1_jtag_blink
271 { .name = "flyswatter2",
272 .init = flyswatter2_init,
273 .reset = flyswatter2_reset,
274 .blink = flyswatter2_jtag_blink
276 { .name = "minimodule",
277 .init = minimodule_init,
278 .reset = minimodule_reset,
280 { .name = "turtelizer2",
282 .reset = turtle_reset,
283 .blink = turtle_jtag_blink
285 { .name = "comstick",
286 .init = comstick_init,
287 .reset = comstick_reset,
289 { .name = "stm32stick",
290 .init = stm32stick_init,
291 .reset = stm32stick_reset,
293 { .name = "axm0432_jtag",
294 .init = axm0432_jtag_init,
295 .reset = axm0432_jtag_reset,
297 { .name = "sheevaplug",
298 .init = sheevaplug_init,
299 .reset = sheevaplug_reset,
302 .init = icebear_jtag_init,
303 .reset = icebear_jtag_reset,
306 .init = cortino_jtag_init,
307 .reset = comstick_reset,
309 { .name = "signalyzer-h",
310 .init = signalyzer_h_init,
311 .reset = signalyzer_h_reset,
312 .blink = signalyzer_h_blink
316 .reset = ktlink_reset,
317 .blink = ktlink_blink
319 { .name = "redbee-econotag",
321 .reset = redbee_reset,
323 { .name = "redbee-usb",
325 .reset = redbee_reset,
326 .channel = INTERFACE_B,
330 .reset = ftx23_reset,
331 .blink = lisa_l_blink,
332 .channel = INTERFACE_B,
334 { .name = "flossjtag",
335 .init = flossjtag_init,
336 .reset = ftx23_reset,
337 .blink = flossjtag_blink,
339 { .name = "xds100v2",
340 .init = xds100v2_init,
341 .reset = xds100v2_reset,
343 { .name = "digilent-hs1",
344 .init = digilent_hs1_init,
345 .reset = digilent_hs1_reset,
346 .channel = INTERFACE_A,
348 { .name = NULL, /* END OF TABLE */ },
351 /* bitmask used to drive nTRST; usually a GPIOLx signal */
352 static uint8_t nTRST;
353 static uint8_t nTRSTnOE;
354 /* bitmask used to drive nSRST; usually a GPIOLx signal */
355 static uint8_t nSRST;
356 static uint8_t nSRSTnOE;
358 /** the layout being used with this debug session */
359 static const struct ft2232_layout *layout;
361 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
362 static uint8_t low_output;
364 /* note that direction bit == 1 means that signal is an output */
366 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
367 static uint8_t low_direction;
368 /** default value bitmask for CBUS GPIOH(0..4) */
369 static uint8_t high_output;
370 /** default direction bitmask for CBUS GPIOH(0..4) */
371 static uint8_t high_direction;
373 #if BUILD_FT2232_FTD2XX == 1
374 static FT_HANDLE ftdih;
375 static FT_DEVICE ftdi_device;
376 #elif BUILD_FT2232_LIBFTDI == 1
377 static struct ftdi_context ftdic;
378 static enum ftdi_chip_type ftdi_device;
381 static struct jtag_command *first_unsent; /* next command that has to be sent */
382 static int require_send;
384 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
386 "There is a significant difference between libftdi and libftd2xx. The latter
387 one allows to schedule up to 64*64 bytes of result data while libftdi fails
388 with more than 4*64. As a consequence, the FT2232 driver is forced to
389 perform around 16x more USB transactions for long command streams with TDO
390 capture when running with libftdi."
393 #define FT2232_BUFFER_SIZE 131072
394 a comment would have been nice.
397 #if BUILD_FT2232_FTD2XX == 1
398 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
400 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
403 #define FT2232_BUFFER_SIZE 131072
405 static uint8_t *ft2232_buffer;
406 static int ft2232_buffer_size;
407 static int ft2232_read_pointer;
408 static int ft2232_expect_read;
411 * Function buffer_write
412 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
413 * @param val is the byte to send.
415 static inline void buffer_write(uint8_t val)
417 assert(ft2232_buffer);
418 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
419 ft2232_buffer[ft2232_buffer_size++] = val;
423 * Function buffer_read
424 * returns a byte from the byte buffer.
426 static inline uint8_t buffer_read(void)
428 assert(ft2232_buffer);
429 assert(ft2232_read_pointer < ft2232_buffer_size);
430 return ft2232_buffer[ft2232_read_pointer++];
434 * Clocks out \a bit_count bits on the TMS line, starting with the least
435 * significant bit of tms_bits and progressing to more significant bits.
436 * Rigorous state transition logging is done here via tap_set_state().
438 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
439 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
440 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
441 * is often used for this, 0x4b.
443 * @param tms_bits Holds the sequence of bits to send.
444 * @param tms_count Tells how many bits in the sequence.
445 * @param tdi_bit A single bit to pass on to TDI before the first TCK
446 * cycle and held static for the duration of TMS clocking.
448 * See the MPSSE spec referenced above.
450 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
454 int tms_ndx; /* bit index into tms_byte */
456 assert(tms_count > 0);
458 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
459 mpsse_cmd, tms_bits, tms_count);
461 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
462 bool bit = tms_bits & 1;
465 tms_byte |= (1 << tms_ndx);
467 /* always do state transitions in public view */
468 tap_set_state(tap_state_transition(tap_get_state(), bit));
470 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
475 if (tms_ndx == 7 || i == tms_count-1) {
476 buffer_write(mpsse_cmd);
477 buffer_write(tms_ndx - 1);
479 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
480 * TMS/CS and is held static for the duration of TMS/CS clocking.
482 buffer_write(tms_byte | (tdi_bit << 7));
488 * Function get_tms_buffer_requirements
489 * returns what clock_tms() will consume if called with
492 static inline int get_tms_buffer_requirements(int bit_count)
494 return ((bit_count + 6)/7) * 3;
498 * Function move_to_state
499 * moves the TAP controller from the current state to a
500 * \a goal_state through a path given by tap_get_tms_path(). State transition
501 * logging is performed by delegation to clock_tms().
503 * @param goal_state is the destination state for the move.
505 static void move_to_state(tap_state_t goal_state)
507 tap_state_t start_state = tap_get_state();
509 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
510 * lookup of the required TMS pattern to move to this state from the start state.
513 /* do the 2 lookups */
514 int tms_bits = tap_get_tms_path(start_state, goal_state);
515 int tms_count = tap_get_tms_path_len(start_state, goal_state);
517 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
519 clock_tms(0x4b, tms_bits, tms_count, 0);
522 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
524 #if BUILD_FT2232_FTD2XX == 1
526 DWORD dw_bytes_written = 0;
527 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
528 if (status != FT_OK) {
529 *bytes_written = dw_bytes_written;
530 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
531 return ERROR_JTAG_DEVICE_ERROR;
533 *bytes_written = dw_bytes_written;
535 #elif BUILD_FT2232_LIBFTDI == 1
536 int retval = ftdi_write_data(&ftdic, buf, size);
539 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
540 return ERROR_JTAG_DEVICE_ERROR;
542 *bytes_written = retval;
546 if (*bytes_written != (uint32_t)size)
547 return ERROR_JTAG_DEVICE_ERROR;
552 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
554 #if BUILD_FT2232_FTD2XX == 1
560 while ((*bytes_read < size) && timeout--) {
561 status = FT_Read(ftdih, buf + *bytes_read, size -
562 *bytes_read, &dw_bytes_read);
563 if (status != FT_OK) {
565 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
566 return ERROR_JTAG_DEVICE_ERROR;
568 *bytes_read += dw_bytes_read;
571 #elif BUILD_FT2232_LIBFTDI == 1
573 int timeout = LIBFTDI_READ_RETRY_COUNT;
576 while ((*bytes_read < size) && timeout--) {
577 retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
580 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
581 return ERROR_JTAG_DEVICE_ERROR;
583 *bytes_read += retval;
588 if (*bytes_read < size) {
589 LOG_ERROR("couldn't read enough bytes from "
590 "FT2232 device (%i < %i)",
591 (unsigned)*bytes_read,
593 return ERROR_JTAG_DEVICE_ERROR;
599 static bool ft2232_device_is_highspeed(void)
601 #if BUILD_FT2232_FTD2XX == 1
602 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H)
603 #ifdef HAS_ENUM_FT232H
604 || (ftdi_device == FT_DEVICE_232H)
607 #elif BUILD_FT2232_LIBFTDI == 1
608 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H
609 #ifdef HAS_ENUM_FT232H
610 || ftdi_device == TYPE_232H
617 * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
618 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
619 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
622 static int ftx232h_adaptive_clocking(bool enable)
624 uint8_t buf = enable ? 0x96 : 0x97;
625 LOG_DEBUG("%2.2x", buf);
627 uint32_t bytes_written;
630 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
631 if (retval != ERROR_OK) {
632 LOG_ERROR("couldn't write command to %s adaptive clocking"
633 , enable ? "enable" : "disable");
641 * Enable/disable the clk divide by 5 of the 60MHz master clock.
642 * This result in a JTAG clock speed range of 91.553Hz-6MHz
643 * respective 457.763Hz-30MHz.
645 static int ftx232h_clk_divide_by_5(bool enable)
647 uint32_t bytes_written;
648 uint8_t buf = enable ? 0x8b : 0x8a;
650 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
651 LOG_ERROR("couldn't write command to %s clk divide by 5"
652 , enable ? "enable" : "disable");
653 return ERROR_JTAG_INIT_FAILED;
655 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_x232H_MAX_TCK;
656 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
661 static int ft2232_speed(int speed)
665 uint32_t bytes_written;
668 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
669 if (ft2232_device_is_highspeed())
670 retval = ftx232h_adaptive_clocking(enable_adaptive_clocking);
671 else if (enable_adaptive_clocking) {
672 LOG_ERROR("ft2232 device %lu does not support RTCK"
673 , (long unsigned int)ftdi_device);
677 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
680 buf[0] = 0x86; /* command "set divisor" */
681 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
682 buf[2] = (speed >> 8) & 0xff; /* valueH */
684 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
685 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
686 if (retval != ERROR_OK) {
687 LOG_ERROR("couldn't set FT2232 TCK speed");
694 static int ft2232_speed_div(int speed, int *khz)
696 /* Take a look in the FT2232 manual,
697 * AN2232C-01 Command Processor for
698 * MPSSE and MCU Host Bus. Chapter 3.8 */
700 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
705 static int ft2232_khz(int khz, int *jtag_speed)
708 if (ft2232_device_is_highspeed()) {
709 *jtag_speed = RTCK_SPEED;
712 LOG_DEBUG("RCLK not supported");
717 /* Take a look in the FT2232 manual,
718 * AN2232C-01 Command Processor for
719 * MPSSE and MCU Host Bus. Chapter 3.8
721 * We will calc here with a multiplier
722 * of 10 for better rounding later. */
724 /* Calc speed, (ft2232_max_tck / khz) - 1
725 * Use 65000 for better rounding */
726 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
728 /* Add 0.9 for rounding */
731 /* Calc real speed */
732 *jtag_speed = *jtag_speed / 10;
734 /* Check if speed is greater than 0 */
738 /* Check max value */
739 if (*jtag_speed > 0xFFFF)
740 *jtag_speed = 0xFFFF;
745 static void ft2232_end_state(tap_state_t state)
747 if (tap_is_state_stable(state))
748 tap_set_end_state(state);
750 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
755 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
757 int num_bytes = (scan_size + 7) / 8;
758 int bits_left = scan_size;
761 while (num_bytes-- > 1) {
762 buffer[cur_byte++] = buffer_read();
766 buffer[cur_byte] = 0x0;
768 /* There is one more partial byte left from the clock data in/out instructions */
770 buffer[cur_byte] = buffer_read() >> 1;
771 /* This shift depends on the length of the
772 *clock data to tms instruction, insterted
773 *at end of the scan, now fixed to a two
774 *step transition in ft2232_add_scan */
775 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
778 static void ft2232_debug_dump_buffer(void)
784 for (i = 0; i < ft2232_buffer_size; i++) {
785 line_p += snprintf(line_p,
786 sizeof(line) - (line_p - line),
790 LOG_DEBUG("%s", line);
796 LOG_DEBUG("%s", line);
799 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
801 struct jtag_command *cmd;
806 uint32_t bytes_written = 0;
807 uint32_t bytes_read = 0;
809 #ifdef _DEBUG_USB_IO_
810 struct timeval start, inter, inter2, end;
811 struct timeval d_inter, d_inter2, d_end;
814 #ifdef _DEBUG_USB_COMMS_
815 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
816 ft2232_debug_dump_buffer();
819 #ifdef _DEBUG_USB_IO_
820 gettimeofday(&start, NULL);
823 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
824 if (retval != ERROR_OK) {
825 LOG_ERROR("couldn't write MPSSE commands to FT2232");
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&inter, NULL);
833 if (ft2232_expect_read) {
834 /* FIXME this "timeout" is never changed ... */
835 int timeout = LIBFTDI_READ_RETRY_COUNT;
836 ft2232_buffer_size = 0;
838 #ifdef _DEBUG_USB_IO_
839 gettimeofday(&inter2, NULL);
842 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
843 if (retval != ERROR_OK) {
844 LOG_ERROR("couldn't read from FT2232");
848 #ifdef _DEBUG_USB_IO_
849 gettimeofday(&end, NULL);
851 timeval_subtract(&d_inter, &inter, &start);
852 timeval_subtract(&d_inter2, &inter2, &start);
853 timeval_subtract(&d_end, &end, &start);
855 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
856 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
857 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
858 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
861 ft2232_buffer_size = bytes_read;
863 if (ft2232_expect_read != ft2232_buffer_size) {
864 LOG_ERROR("ft2232_expect_read (%i) != "
865 "ft2232_buffer_size (%i) "
869 LIBFTDI_READ_RETRY_COUNT - timeout);
870 ft2232_debug_dump_buffer();
875 #ifdef _DEBUG_USB_COMMS_
876 LOG_DEBUG("read buffer (%i retries): %i bytes",
877 LIBFTDI_READ_RETRY_COUNT - timeout,
879 ft2232_debug_dump_buffer();
883 ft2232_expect_read = 0;
884 ft2232_read_pointer = 0;
886 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
887 * that wasn't handled by a caller-provided error handler
892 while (cmd != last) {
895 type = jtag_scan_type(cmd->cmd.scan);
896 if (type != SCAN_OUT) {
897 scan_size = jtag_scan_size(cmd->cmd.scan);
898 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
899 ft2232_read_scan(type, buffer, scan_size);
900 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
901 retval = ERROR_JTAG_QUEUE_FAILED;
913 ft2232_buffer_size = 0;
919 * Function ft2232_add_pathmove
920 * moves the TAP controller from the current state to a new state through the
921 * given path, where path is an array of tap_state_t's.
923 * @param path is an array of tap_stat_t which gives the states to traverse through
924 * ending with the last state at path[num_states-1]
925 * @param num_states is the count of state steps to move through
927 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
931 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
935 /* this loop verifies that the path is legal and logs each state in the path */
937 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
939 int num_states_batch = num_states > 7 ? 7 : num_states;
941 /* command "Clock Data to TMS/CS Pin (no Read)" */
944 /* number of states remaining */
945 buffer_write(num_states_batch - 1);
947 while (num_states_batch--) {
948 /* either TMS=0 or TMS=1 must work ... */
949 if (tap_state_transition(tap_get_state(), false) == path[state_count])
950 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
951 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
952 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
954 /* ... or else the caller goofed BADLY */
956 LOG_ERROR("BUG: %s -> %s isn't a valid "
957 "TAP state transition",
958 tap_state_name(tap_get_state()),
959 tap_state_name(path[state_count]));
963 tap_set_state(path[state_count]);
968 buffer_write(tms_byte);
970 tap_set_end_state(tap_get_state());
973 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
975 int num_bytes = (scan_size + 7) / 8;
976 int bits_left = scan_size;
981 if (tap_get_state() != TAP_DRSHIFT)
982 move_to_state(TAP_DRSHIFT);
984 if (tap_get_state() != TAP_IRSHIFT)
985 move_to_state(TAP_IRSHIFT);
988 /* add command for complete bytes */
989 while (num_bytes > 1) {
991 if (type == SCAN_IO) {
992 /* Clock Data Bytes In and Out LSB First */
994 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
995 } else if (type == SCAN_OUT) {
996 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
998 /* LOG_DEBUG("added TDI bytes (o)"); */
999 } else if (type == SCAN_IN) {
1000 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1002 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1005 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1006 num_bytes -= thisrun_bytes;
1008 buffer_write((uint8_t) (thisrun_bytes - 1));
1009 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1011 if (type != SCAN_IN) {
1012 /* add complete bytes */
1013 while (thisrun_bytes-- > 0) {
1014 buffer_write(buffer[cur_byte++]);
1017 } else /* (type == SCAN_IN) */
1018 bits_left -= 8 * (thisrun_bytes);
1021 /* the most signifcant bit is scanned during TAP movement */
1022 if (type != SCAN_IN)
1023 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1027 /* process remaining bits but the last one */
1028 if (bits_left > 1) {
1029 if (type == SCAN_IO) {
1030 /* Clock Data Bits In and Out LSB First */
1032 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1033 } else if (type == SCAN_OUT) {
1034 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1036 /* LOG_DEBUG("added TDI bits (o)"); */
1037 } else if (type == SCAN_IN) {
1038 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1040 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1043 buffer_write(bits_left - 2);
1044 if (type != SCAN_IN)
1045 buffer_write(buffer[cur_byte]);
1048 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1049 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1050 if (type == SCAN_IO) {
1051 /* Clock Data Bits In and Out LSB First */
1053 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1054 } else if (type == SCAN_OUT) {
1055 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1057 /* LOG_DEBUG("added TDI bits (o)"); */
1058 } else if (type == SCAN_IN) {
1059 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1061 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1064 if (type != SCAN_IN)
1065 buffer_write(last_bit);
1071 /* move from Shift-IR/DR to end state */
1072 if (type != SCAN_OUT) {
1073 /* We always go to the PAUSE state in two step at the end of an IN or IO
1075 * This must be coordinated with the bit shifts in ft2232_read_scan */
1078 /* Clock Data to TMS/CS Pin with Read */
1081 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1082 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1083 /* Clock Data to TMS/CS Pin (no Read) */
1087 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1088 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1091 if (tap_get_state() != tap_get_end_state())
1092 move_to_state(tap_get_end_state());
1095 static int ft2232_large_scan(struct scan_command *cmd,
1096 enum scan_type type,
1100 int num_bytes = (scan_size + 7) / 8;
1101 int bits_left = scan_size;
1104 uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1105 uint8_t *receive_pointer = receive_buffer;
1106 uint32_t bytes_written;
1107 uint32_t bytes_read;
1109 int thisrun_read = 0;
1112 LOG_ERROR("BUG: large IR scans are not supported");
1116 if (tap_get_state() != TAP_DRSHIFT)
1117 move_to_state(TAP_DRSHIFT);
1119 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1120 if (retval != ERROR_OK) {
1121 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1124 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1125 ft2232_buffer_size, (int)bytes_written);
1126 ft2232_buffer_size = 0;
1128 /* add command for complete bytes */
1129 while (num_bytes > 1) {
1132 if (type == SCAN_IO) {
1133 /* Clock Data Bytes In and Out LSB First */
1135 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1136 } else if (type == SCAN_OUT) {
1137 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1139 /* LOG_DEBUG("added TDI bytes (o)"); */
1140 } else if (type == SCAN_IN) {
1141 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1143 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1146 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1147 thisrun_read = thisrun_bytes;
1148 num_bytes -= thisrun_bytes;
1149 buffer_write((uint8_t) (thisrun_bytes - 1));
1150 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1152 if (type != SCAN_IN) {
1153 /* add complete bytes */
1154 while (thisrun_bytes-- > 0) {
1155 buffer_write(buffer[cur_byte]);
1159 } else /* (type == SCAN_IN) */
1160 bits_left -= 8 * (thisrun_bytes);
1162 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1163 if (retval != ERROR_OK) {
1164 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1167 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1169 (int)bytes_written);
1170 ft2232_buffer_size = 0;
1172 if (type != SCAN_OUT) {
1173 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1174 if (retval != ERROR_OK) {
1175 LOG_ERROR("couldn't read from FT2232");
1178 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1181 receive_pointer += bytes_read;
1187 /* the most signifcant bit is scanned during TAP movement */
1188 if (type != SCAN_IN)
1189 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1193 /* process remaining bits but the last one */
1194 if (bits_left > 1) {
1195 if (type == SCAN_IO) {
1196 /* Clock Data Bits In and Out LSB First */
1198 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1199 } else if (type == SCAN_OUT) {
1200 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1202 /* LOG_DEBUG("added TDI bits (o)"); */
1203 } else if (type == SCAN_IN) {
1204 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1206 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1208 buffer_write(bits_left - 2);
1209 if (type != SCAN_IN)
1210 buffer_write(buffer[cur_byte]);
1212 if (type != SCAN_OUT)
1216 if (tap_get_end_state() == TAP_DRSHIFT) {
1217 if (type == SCAN_IO) {
1218 /* Clock Data Bits In and Out LSB First */
1220 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1221 } else if (type == SCAN_OUT) {
1222 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1224 /* LOG_DEBUG("added TDI bits (o)"); */
1225 } else if (type == SCAN_IN) {
1226 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1228 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1231 buffer_write(last_bit);
1233 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1234 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1237 /* move from Shift-IR/DR to end state */
1238 if (type != SCAN_OUT) {
1239 /* Clock Data to TMS/CS Pin with Read */
1241 /* LOG_DEBUG("added TMS scan (read)"); */
1243 /* Clock Data to TMS/CS Pin (no Read) */
1245 /* LOG_DEBUG("added TMS scan (no read)"); */
1248 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1249 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1252 if (type != SCAN_OUT)
1255 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1256 if (retval != ERROR_OK) {
1257 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1260 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1262 (int)bytes_written);
1263 ft2232_buffer_size = 0;
1265 if (type != SCAN_OUT) {
1266 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1267 if (retval != ERROR_OK) {
1268 LOG_ERROR("couldn't read from FT2232");
1271 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1279 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1281 int predicted_size = 3;
1282 int num_bytes = (scan_size - 1) / 8;
1284 if (tap_get_state() != TAP_DRSHIFT)
1285 predicted_size += get_tms_buffer_requirements(
1286 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1288 if (type == SCAN_IN) { /* only from device to host */
1289 /* complete bytes */
1290 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1292 /* remaining bits - 1 (up to 7) */
1293 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1294 } else {/* host to device, or bidirectional
1296 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1298 /* remaining bits -1 (up to 7) */
1299 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1302 return predicted_size;
1305 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1307 int predicted_size = 0;
1309 if (type != SCAN_OUT) {
1310 /* complete bytes */
1312 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1314 /* remaining bits - 1 */
1315 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1317 /* last bit (from TMS scan) */
1318 predicted_size += 1;
1321 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1323 return predicted_size;
1326 /* semi-generic FT2232/FT4232 reset code */
1327 static void ftx23_reset(int trst, int srst)
1329 enum reset_types jtag_reset_config = jtag_get_reset_config();
1331 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1332 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1334 low_output &= ~nTRST; /* switch output low */
1335 } else if (trst == 0) {
1336 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1337 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1338 *and external pullup) */
1340 low_output |= nTRST; /* switch output high */
1344 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1345 low_output &= ~nSRST; /* switch output low */
1347 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1348 } else if (srst == 0) {
1349 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1350 low_output |= nSRST; /* switch output high */
1352 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1355 /* command "set data bits low byte" */
1357 buffer_write(low_output);
1358 buffer_write(low_direction);
1361 static void jtagkey_reset(int trst, int srst)
1363 enum reset_types jtag_reset_config = jtag_get_reset_config();
1365 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1366 high_output &= ~nTRSTnOE;
1368 high_output &= ~nTRST;
1369 } else if (trst == 0) {
1370 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1371 high_output |= nTRSTnOE;
1373 high_output |= nTRST;
1377 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1378 high_output &= ~nSRST;
1380 high_output &= ~nSRSTnOE;
1381 } else if (srst == 0) {
1382 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1383 high_output |= nSRST;
1385 high_output |= nSRSTnOE;
1388 /* command "set data bits high byte" */
1390 buffer_write(high_output);
1391 buffer_write(high_direction);
1392 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1399 static void olimex_jtag_reset(int trst, int srst)
1401 enum reset_types jtag_reset_config = jtag_get_reset_config();
1403 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1404 high_output &= ~nTRSTnOE;
1406 high_output &= ~nTRST;
1407 } else if (trst == 0) {
1408 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1409 high_output |= nTRSTnOE;
1411 high_output |= nTRST;
1415 high_output |= nSRST;
1417 high_output &= ~nSRST;
1419 /* command "set data bits high byte" */
1421 buffer_write(high_output);
1422 buffer_write(high_direction);
1423 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1430 static void axm0432_jtag_reset(int trst, int srst)
1433 tap_set_state(TAP_RESET);
1434 high_output &= ~nTRST;
1435 } else if (trst == 0)
1436 high_output |= nTRST;
1439 high_output &= ~nSRST;
1441 high_output |= nSRST;
1443 /* command "set data bits low byte" */
1445 buffer_write(high_output);
1446 buffer_write(high_direction);
1447 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1454 static void flyswatter_reset(int trst, int srst)
1457 low_output &= ~nTRST;
1459 low_output |= nTRST;
1462 low_output |= nSRST;
1464 low_output &= ~nSRST;
1466 /* command "set data bits low byte" */
1468 buffer_write(low_output);
1469 buffer_write(low_direction);
1470 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1477 static void flyswatter1_reset(int trst, int srst)
1479 flyswatter_reset(trst, srst);
1482 static void flyswatter2_reset(int trst, int srst)
1484 flyswatter_reset(trst, !srst);
1487 static void minimodule_reset(int trst, int srst)
1490 low_output &= ~nSRST;
1492 low_output |= nSRST;
1494 /* command "set data bits low byte" */
1496 buffer_write(low_output);
1497 buffer_write(low_direction);
1498 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1505 static void turtle_reset(int trst, int srst)
1510 low_output |= nSRST;
1512 low_output &= ~nSRST;
1514 /* command "set data bits low byte" */
1516 buffer_write(low_output);
1517 buffer_write(low_direction);
1518 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1524 static void comstick_reset(int trst, int srst)
1527 high_output &= ~nTRST;
1529 high_output |= nTRST;
1532 high_output &= ~nSRST;
1534 high_output |= nSRST;
1536 /* command "set data bits high byte" */
1538 buffer_write(high_output);
1539 buffer_write(high_direction);
1540 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1547 static void stm32stick_reset(int trst, int srst)
1550 high_output &= ~nTRST;
1552 high_output |= nTRST;
1555 low_output &= ~nSRST;
1557 low_output |= nSRST;
1559 /* command "set data bits low byte" */
1561 buffer_write(low_output);
1562 buffer_write(low_direction);
1564 /* command "set data bits high byte" */
1566 buffer_write(high_output);
1567 buffer_write(high_direction);
1568 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1575 static void sheevaplug_reset(int trst, int srst)
1578 high_output &= ~nTRST;
1580 high_output |= nTRST;
1583 high_output &= ~nSRSTnOE;
1585 high_output |= nSRSTnOE;
1587 /* command "set data bits high byte" */
1589 buffer_write(high_output);
1590 buffer_write(high_direction);
1591 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1598 static void redbee_reset(int trst, int srst)
1601 tap_set_state(TAP_RESET);
1602 high_output &= ~nTRST;
1603 } else if (trst == 0)
1604 high_output |= nTRST;
1607 high_output &= ~nSRST;
1609 high_output |= nSRST;
1611 /* command "set data bits low byte" */
1613 buffer_write(high_output);
1614 buffer_write(high_direction);
1615 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1616 "high_direction: 0x%2.2x", trst, srst, high_output,
1620 static void xds100v2_reset(int trst, int srst)
1623 tap_set_state(TAP_RESET);
1624 high_output &= ~nTRST;
1625 } else if (trst == 0)
1626 high_output |= nTRST;
1629 high_output |= nSRST;
1631 high_output &= ~nSRST;
1633 /* command "set data bits low byte" */
1635 buffer_write(high_output);
1636 buffer_write(high_direction);
1637 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1638 "high_direction: 0x%2.2x", trst, srst, high_output,
1642 static int ft2232_execute_runtest(struct jtag_command *cmd)
1646 int predicted_size = 0;
1649 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1650 cmd->cmd.runtest->num_cycles,
1651 tap_state_name(cmd->cmd.runtest->end_state));
1653 /* only send the maximum buffer size that FT2232C can handle */
1655 if (tap_get_state() != TAP_IDLE)
1656 predicted_size += 3;
1657 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1658 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1659 predicted_size += 3;
1660 if (tap_get_end_state() != TAP_IDLE)
1661 predicted_size += 3;
1662 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1663 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1664 retval = ERROR_JTAG_QUEUE_FAILED;
1668 if (tap_get_state() != TAP_IDLE) {
1669 move_to_state(TAP_IDLE);
1672 i = cmd->cmd.runtest->num_cycles;
1674 /* there are no state transitions in this code, so omit state tracking */
1676 /* command "Clock Data to TMS/CS Pin (no Read)" */
1680 buffer_write((i > 7) ? 6 : (i - 1));
1685 i -= (i > 7) ? 7 : i;
1686 /* LOG_DEBUG("added TMS scan (no read)"); */
1689 ft2232_end_state(cmd->cmd.runtest->end_state);
1691 if (tap_get_state() != tap_get_end_state())
1692 move_to_state(tap_get_end_state());
1695 DEBUG_JTAG_IO("runtest: %i, end in %s",
1696 cmd->cmd.runtest->num_cycles,
1697 tap_state_name(tap_get_end_state()));
1701 static int ft2232_execute_statemove(struct jtag_command *cmd)
1703 int predicted_size = 0;
1704 int retval = ERROR_OK;
1706 DEBUG_JTAG_IO("statemove end in %s",
1707 tap_state_name(cmd->cmd.statemove->end_state));
1709 /* only send the maximum buffer size that FT2232C can handle */
1711 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1712 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1713 retval = ERROR_JTAG_QUEUE_FAILED;
1717 ft2232_end_state(cmd->cmd.statemove->end_state);
1719 /* For TAP_RESET, ignore the current recorded state. It's often
1720 * wrong at server startup, and this transation is critical whenever
1723 if (tap_get_end_state() == TAP_RESET) {
1724 clock_tms(0x4b, 0xff, 5, 0);
1727 /* shortest-path move to desired end state */
1728 } else if (tap_get_state() != tap_get_end_state()) {
1729 move_to_state(tap_get_end_state());
1737 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1738 * (or SWD) state machine.
1740 static int ft2232_execute_tms(struct jtag_command *cmd)
1742 int retval = ERROR_OK;
1743 unsigned num_bits = cmd->cmd.tms->num_bits;
1744 const uint8_t *bits = cmd->cmd.tms->bits;
1747 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1749 /* only send the maximum buffer size that FT2232C can handle */
1750 count = 3 * DIV_ROUND_UP(num_bits, 4);
1751 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1752 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1753 retval = ERROR_JTAG_QUEUE_FAILED;
1759 /* Shift out in batches of at most 6 bits; there's a report of an
1760 * FT2232 bug in this area, where shifting exactly 7 bits can make
1761 * problems with TMS signaling for the last clock cycle:
1763 * http://developer.intra2net.com/mailarchive/html/
1764 * libftdi/2009/msg00292.html
1766 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1768 * Note that pathmoves in JTAG are not often seven bits, so that
1769 * isn't a particularly likely situation outside of "special"
1770 * signaling such as switching between JTAG and SWD modes.
1773 if (num_bits <= 6) {
1775 buffer_write(num_bits - 1);
1776 buffer_write(*bits & 0x3f);
1780 /* Yes, this is lazy ... we COULD shift out more data
1781 * bits per operation, but doing it in nybbles is easy
1785 buffer_write(*bits & 0xf);
1788 count = (num_bits > 4) ? 4 : num_bits;
1791 buffer_write(count - 1);
1792 buffer_write((*bits >> 4) & 0xf);
1802 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1804 int predicted_size = 0;
1805 int retval = ERROR_OK;
1807 tap_state_t *path = cmd->cmd.pathmove->path;
1808 int num_states = cmd->cmd.pathmove->num_states;
1810 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1811 tap_state_name(tap_get_state()),
1812 tap_state_name(path[num_states-1]));
1814 /* only send the maximum buffer size that FT2232C can handle */
1815 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1816 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1817 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1818 retval = ERROR_JTAG_QUEUE_FAILED;
1824 ft2232_add_pathmove(path, num_states);
1830 static int ft2232_execute_scan(struct jtag_command *cmd)
1833 int scan_size; /* size of IR or DR scan */
1834 int predicted_size = 0;
1835 int retval = ERROR_OK;
1837 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1839 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1841 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1843 predicted_size = ft2232_predict_scan_out(scan_size, type);
1844 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1845 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1846 /* unsent commands before this */
1847 if (first_unsent != cmd)
1848 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1849 retval = ERROR_JTAG_QUEUE_FAILED;
1851 /* current command */
1852 ft2232_end_state(cmd->cmd.scan->end_state);
1853 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1855 first_unsent = cmd->next;
1859 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1861 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1864 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1865 retval = ERROR_JTAG_QUEUE_FAILED;
1869 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1870 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1871 ft2232_end_state(cmd->cmd.scan->end_state);
1872 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1876 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1877 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1878 tap_state_name(tap_get_end_state()));
1883 static int ft2232_execute_reset(struct jtag_command *cmd)
1886 int predicted_size = 0;
1889 DEBUG_JTAG_IO("reset trst: %i srst %i",
1890 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1892 /* only send the maximum buffer size that FT2232C can handle */
1894 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1895 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1896 retval = ERROR_JTAG_QUEUE_FAILED;
1901 if ((cmd->cmd.reset->trst == 1) ||
1902 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1903 tap_set_state(TAP_RESET);
1905 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1908 DEBUG_JTAG_IO("trst: %i, srst: %i",
1909 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1913 static int ft2232_execute_sleep(struct jtag_command *cmd)
1918 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1920 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1921 retval = ERROR_JTAG_QUEUE_FAILED;
1922 first_unsent = cmd->next;
1923 jtag_sleep(cmd->cmd.sleep->us);
1924 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1926 tap_state_name(tap_get_state()));
1930 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1935 /* this is only allowed while in a stable state. A check for a stable
1936 * state was done in jtag_add_clocks()
1938 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1939 retval = ERROR_JTAG_QUEUE_FAILED;
1940 DEBUG_JTAG_IO("clocks %i while in %s",
1941 cmd->cmd.stableclocks->num_cycles,
1942 tap_state_name(tap_get_state()));
1946 static int ft2232_execute_command(struct jtag_command *cmd)
1950 switch (cmd->type) {
1952 retval = ft2232_execute_reset(cmd);
1955 retval = ft2232_execute_runtest(cmd);
1957 case JTAG_TLR_RESET:
1958 retval = ft2232_execute_statemove(cmd);
1961 retval = ft2232_execute_pathmove(cmd);
1964 retval = ft2232_execute_scan(cmd);
1967 retval = ft2232_execute_sleep(cmd);
1969 case JTAG_STABLECLOCKS:
1970 retval = ft2232_execute_stableclocks(cmd);
1973 retval = ft2232_execute_tms(cmd);
1976 LOG_ERROR("BUG: unknown JTAG command type encountered");
1977 retval = ERROR_JTAG_QUEUE_FAILED;
1983 static int ft2232_execute_queue(void)
1985 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1988 first_unsent = cmd; /* next command that has to be sent */
1991 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1992 * that wasn't handled by a caller-provided error handler
1996 ft2232_buffer_size = 0;
1997 ft2232_expect_read = 0;
1999 /* blink, if the current layout has that feature */
2004 /* fill the write buffer with the desired command */
2005 if (ft2232_execute_command(cmd) != ERROR_OK)
2006 retval = ERROR_JTAG_QUEUE_FAILED;
2007 /* Start reading input before FT2232 TX buffer fills up.
2008 * Sometimes this happens because we don't know the
2009 * length of the last command before we execute it. So
2010 * we simple inform the user.
2014 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2015 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2016 LOG_DEBUG("read buffer size looks too high %d/%d",
2018 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2019 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2020 retval = ERROR_JTAG_QUEUE_FAILED;
2025 if (require_send > 0)
2026 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2027 retval = ERROR_JTAG_QUEUE_FAILED;
2032 #if BUILD_FT2232_FTD2XX == 1
2033 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2037 char SerialNumber[16];
2038 char Description[64];
2039 DWORD openex_flags = 0;
2040 char *openex_string = NULL;
2041 uint8_t latency_timer;
2043 if (layout == NULL) {
2044 LOG_WARNING("No ft2232 layout specified'");
2045 return ERROR_JTAG_INIT_FAILED;
2048 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2049 layout->name, vid, pid);
2052 /* Add non-standard Vid/Pid to the linux driver */
2053 status = FT_SetVIDPID(vid, pid);
2054 if (status != FT_OK)
2055 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2059 if (ft2232_device_desc && ft2232_serial) {
2061 "can't open by device description and serial number, giving precedence to serial");
2062 ft2232_device_desc = NULL;
2065 if (ft2232_device_desc) {
2066 openex_string = ft2232_device_desc;
2067 openex_flags = FT_OPEN_BY_DESCRIPTION;
2068 } else if (ft2232_serial) {
2069 openex_string = ft2232_serial;
2070 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2072 LOG_ERROR("neither device description nor serial number specified");
2074 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2076 return ERROR_JTAG_INIT_FAILED;
2079 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2080 if (status != FT_OK) {
2081 /* under Win32, the FTD2XX driver appends an "A" to the end
2082 * of the description, if we tried by the desc, then
2083 * try by the alternate "A" description. */
2084 if (openex_string == ft2232_device_desc) {
2085 /* Try the alternate method. */
2086 openex_string = ft2232_device_desc_A;
2087 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2088 if (status == FT_OK) {
2089 /* yea, the "alternate" method worked! */
2091 /* drat, give the user a meaningfull message.
2092 * telling the use we tried *BOTH* methods. */
2093 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2095 ft2232_device_desc_A);
2100 if (status != FT_OK) {
2104 LOG_WARNING("unable to open ftdi device (trying more): %s",
2105 ftd2xx_status_string(status));
2107 return ERROR_JTAG_INIT_FAILED;
2109 LOG_ERROR("unable to open ftdi device: %s",
2110 ftd2xx_status_string(status));
2111 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2112 if (status == FT_OK) {
2113 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2116 for (i = 0; i < num_devices; i++)
2117 desc_array[i] = malloc(64);
2119 desc_array[num_devices] = NULL;
2121 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2123 if (status == FT_OK) {
2124 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2125 for (i = 0; i < num_devices; i++)
2126 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2129 for (i = 0; i < num_devices; i++)
2130 free(desc_array[i]);
2134 LOG_ERROR("ListDevices: NONE");
2135 return ERROR_JTAG_INIT_FAILED;
2138 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2139 if (status != FT_OK) {
2140 LOG_ERROR("unable to set latency timer: %s",
2141 ftd2xx_status_string(status));
2142 return ERROR_JTAG_INIT_FAILED;
2145 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2146 if (status != FT_OK) {
2147 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2148 * so ignore errors if using this driver version */
2151 status = FT_GetDriverVersion(ftdih, &dw_version);
2152 LOG_ERROR("unable to get latency timer: %s",
2153 ftd2xx_status_string(status));
2155 if ((status == FT_OK) && (dw_version == 0x10004)) {
2156 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2157 "with FT_GetLatencyTimer, upgrade to a newer version");
2159 return ERROR_JTAG_INIT_FAILED;
2161 LOG_DEBUG("current latency timer: %i", latency_timer);
2163 status = FT_SetTimeouts(ftdih, 5000, 5000);
2164 if (status != FT_OK) {
2165 LOG_ERROR("unable to set timeouts: %s",
2166 ftd2xx_status_string(status));
2167 return ERROR_JTAG_INIT_FAILED;
2170 status = FT_SetBitMode(ftdih, 0x0b, 2);
2171 if (status != FT_OK) {
2172 LOG_ERROR("unable to enable bit i/o mode: %s",
2173 ftd2xx_status_string(status));
2174 return ERROR_JTAG_INIT_FAILED;
2177 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2178 SerialNumber, Description, NULL);
2179 if (status != FT_OK) {
2180 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2181 ftd2xx_status_string(status));
2182 return ERROR_JTAG_INIT_FAILED;
2184 static const char *type_str[] = {
2185 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2187 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2188 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2189 ? ftdi_device : FT_DEVICE_UNKNOWN;
2190 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2191 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2192 LOG_INFO("SerialNumber: %s", SerialNumber);
2193 LOG_INFO("Description: %s", Description);
2199 static int ft2232_purge_ftd2xx(void)
2203 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2204 if (status != FT_OK) {
2205 LOG_ERROR("error purging ftd2xx device: %s",
2206 ftd2xx_status_string(status));
2207 return ERROR_JTAG_INIT_FAILED;
2213 #endif /* BUILD_FT2232_FTD2XX == 1 */
2215 #if BUILD_FT2232_LIBFTDI == 1
2216 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2218 uint8_t latency_timer;
2220 if (layout == NULL) {
2221 LOG_WARNING("No ft2232 layout specified'");
2222 return ERROR_JTAG_INIT_FAILED;
2225 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2226 layout->name, vid, pid);
2228 if (ftdi_init(&ftdic) < 0)
2229 return ERROR_JTAG_INIT_FAILED;
2231 /* default to INTERFACE_A */
2232 if (channel == INTERFACE_ANY)
2233 channel = INTERFACE_A;
2234 if (ftdi_set_interface(&ftdic, channel) < 0) {
2235 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2236 return ERROR_JTAG_INIT_FAILED;
2239 /* context, vendor id, product id */
2240 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2242 LOG_WARNING("unable to open ftdi device (trying more): %s",
2245 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2247 return ERROR_JTAG_INIT_FAILED;
2250 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2251 if (ftdi_usb_reset(&ftdic) < 0) {
2252 LOG_ERROR("unable to reset ftdi device");
2253 return ERROR_JTAG_INIT_FAILED;
2256 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2257 LOG_ERROR("unable to set latency timer");
2258 return ERROR_JTAG_INIT_FAILED;
2261 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2262 LOG_ERROR("unable to get latency timer");
2263 return ERROR_JTAG_INIT_FAILED;
2265 LOG_DEBUG("current latency timer: %i", latency_timer);
2267 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2269 ftdi_device = ftdic.type;
2270 static const char *type_str[] = {
2271 "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "Unknown"
2273 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2274 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2275 ? ftdi_device : no_of_known_types;
2276 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2280 static int ft2232_purge_libftdi(void)
2282 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2283 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2284 return ERROR_JTAG_INIT_FAILED;
2290 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2292 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2295 uint32_t bytes_written;
2297 buf[0] = 0x80; /* command "set data bits low byte" */
2298 buf[1] = value; /* value */
2299 buf[2] = direction; /* direction */
2301 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2303 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2304 LOG_ERROR("couldn't initialize data bits low byte");
2305 return ERROR_JTAG_INIT_FAILED;
2311 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2314 uint32_t bytes_written;
2316 buf[0] = 0x82; /* command "set data bits high byte" */
2317 buf[1] = value; /* value */
2318 buf[2] = direction; /* direction */
2320 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2322 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2323 LOG_ERROR("couldn't initialize data bits high byte");
2324 return ERROR_JTAG_INIT_FAILED;
2330 static int ft2232_init(void)
2334 uint32_t bytes_written;
2336 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2337 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2339 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2340 if (layout == NULL) {
2341 LOG_WARNING("No ft2232 layout specified'");
2342 return ERROR_JTAG_INIT_FAILED;
2345 for (int i = 0; 1; i++) {
2347 * "more indicates that there are more IDs to try, so we should
2348 * not print an error for an ID mismatch (but for anything
2351 * try_more indicates that the error code returned indicates an
2352 * ID mismatch (and nothing else) and that we should proceeed
2353 * with the next ID pair.
2355 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2358 #if BUILD_FT2232_FTD2XX == 1
2359 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2361 #elif BUILD_FT2232_LIBFTDI == 1
2362 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2363 more, &try_more, ft2232_channel);
2367 if (!more || !try_more)
2371 ft2232_buffer_size = 0;
2372 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2374 if (layout->init() != ERROR_OK)
2375 return ERROR_JTAG_INIT_FAILED;
2377 if (ft2232_device_is_highspeed()) {
2378 #ifndef BUILD_FT2232_HIGHSPEED
2379 #if BUILD_FT2232_FTD2XX == 1
2381 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2382 #elif BUILD_FT2232_LIBFTDI == 1
2384 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2387 /* make sure the legacy mode is disabled */
2388 if (ftx232h_clk_divide_by_5(false) != ERROR_OK)
2389 return ERROR_JTAG_INIT_FAILED;
2392 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2393 retval = ft2232_write(buf, 1, &bytes_written);
2394 if (retval != ERROR_OK) {
2395 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2396 return ERROR_JTAG_INIT_FAILED;
2399 #if BUILD_FT2232_FTD2XX == 1
2400 return ft2232_purge_ftd2xx();
2401 #elif BUILD_FT2232_LIBFTDI == 1
2402 return ft2232_purge_libftdi();
2408 /** Updates defaults for DBUS signals: the four JTAG signals
2409 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2411 static inline void ftx232_dbus_init(void)
2414 low_direction = 0x0b;
2417 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2418 * the four GPIOL signals. Initialization covers value and direction,
2419 * as customized for each layout.
2421 static int ftx232_dbus_write(void)
2423 enum reset_types jtag_reset_config = jtag_get_reset_config();
2424 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2425 low_direction &= ~nTRSTnOE; /* nTRST input */
2426 low_output &= ~nTRST; /* nTRST = 0 */
2428 low_direction |= nTRSTnOE; /* nTRST output */
2429 low_output |= nTRST; /* nTRST = 1 */
2432 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2433 low_direction |= nSRSTnOE; /* nSRST output */
2434 low_output |= nSRST; /* nSRST = 1 */
2436 low_direction &= ~nSRSTnOE; /* nSRST input */
2437 low_output &= ~nSRST; /* nSRST = 0 */
2440 /* initialize low byte for jtag */
2441 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2442 LOG_ERROR("couldn't initialize FT2232 DBUS");
2443 return ERROR_JTAG_INIT_FAILED;
2449 static int usbjtag_init(void)
2452 * NOTE: This is now _specific_ to the "usbjtag" layout.
2453 * Don't try cram any more layouts into this.
2462 return ftx232_dbus_write();
2465 static int lm3s811_jtag_init(void)
2469 /* There are multiple revisions of LM3S811 eval boards:
2470 * - Rev B (and older?) boards have no SWO trace support.
2471 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2472 * they should use the "luminary_icdi" layout instead.
2479 low_direction = 0x8b;
2481 return ftx232_dbus_write();
2484 static int icdi_jtag_init(void)
2488 /* Most Luminary eval boards support SWO trace output,
2489 * and should use this "luminary_icdi" layout.
2491 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2492 * to switch between JTAG and SWD, or switch the ft2232 UART
2493 * on the second MPSSE channel/interface (BDBUS)
2494 * between (i) the stellaris UART (on Luminary boards)
2495 * or (ii) SWO trace data (generic).
2497 * We come up in JTAG mode and may switch to SWD later (with
2498 * SWO/trace option if SWD is active).
2505 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2506 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2507 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2510 /* GPIOs on second channel/interface (UART) ... */
2511 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2512 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2513 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2518 nSRSTnOE = ICDI_SRST;
2520 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2521 low_output |= ICDI_JTAG_EN;
2522 low_output &= ~ICDI_DBG_ENn;
2524 return ftx232_dbus_write();
2527 static int signalyzer_init(void)
2535 return ftx232_dbus_write();
2538 static int axm0432_jtag_init(void)
2541 low_direction = 0x2b;
2543 /* initialize low byte for jtag */
2544 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2545 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2546 return ERROR_JTAG_INIT_FAILED;
2549 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2551 nTRSTnOE = 0x0; /* No output enable for TRST*/
2553 nSRSTnOE = 0x0; /* No output enable for SRST*/
2555 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2560 high_direction = 0x0c;
2562 enum reset_types jtag_reset_config = jtag_get_reset_config();
2563 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2564 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2566 high_output |= nTRST;
2568 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2569 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2571 high_output |= nSRST;
2573 /* initialize high byte for jtag */
2574 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2575 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2576 return ERROR_JTAG_INIT_FAILED;
2582 static int redbee_init(void)
2585 low_direction = 0x2b;
2587 /* initialize low byte for jtag */
2588 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2589 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2590 return ERROR_JTAG_INIT_FAILED;
2594 nTRSTnOE = 0x0; /* No output enable for TRST*/
2596 nSRSTnOE = 0x0; /* No output enable for SRST*/
2599 high_direction = 0x0c;
2601 enum reset_types jtag_reset_config = jtag_get_reset_config();
2602 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2603 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2605 high_output |= nTRST;
2607 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2608 LOG_ERROR("can't set nSRST to push-pull on redbee");
2610 high_output |= nSRST;
2612 /* initialize high byte for jtag */
2613 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2614 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2615 return ERROR_JTAG_INIT_FAILED;
2621 static int jtagkey_init(void)
2624 low_direction = 0x1b;
2626 /* initialize low byte for jtag */
2627 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2628 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2629 return ERROR_JTAG_INIT_FAILED;
2632 if (strcmp(layout->name, "jtagkey") == 0) {
2637 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2638 || (strcmp(layout->name, "oocdlink") == 0)) {
2644 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2649 high_direction = 0x0f;
2651 enum reset_types jtag_reset_config = jtag_get_reset_config();
2652 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2653 high_output |= nTRSTnOE;
2654 high_output &= ~nTRST;
2656 high_output &= ~nTRSTnOE;
2657 high_output |= nTRST;
2660 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2661 high_output &= ~nSRSTnOE;
2662 high_output |= nSRST;
2664 high_output |= nSRSTnOE;
2665 high_output &= ~nSRST;
2668 /* initialize high byte for jtag */
2669 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2670 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2671 return ERROR_JTAG_INIT_FAILED;
2677 static int olimex_jtag_init(void)
2680 low_direction = 0x1b;
2682 /* initialize low byte for jtag */
2683 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2684 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2685 return ERROR_JTAG_INIT_FAILED;
2691 nSRSTnOE = 0x00;/* no output enable for nSRST */
2694 high_direction = 0x0f;
2696 enum reset_types jtag_reset_config = jtag_get_reset_config();
2697 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2698 high_output |= nTRSTnOE;
2699 high_output &= ~nTRST;
2701 high_output &= ~nTRSTnOE;
2702 high_output |= nTRST;
2705 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2706 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2708 high_output &= ~nSRST;
2710 /* turn red LED on */
2711 high_output |= 0x08;
2713 /* initialize high byte for jtag */
2714 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2715 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2716 return ERROR_JTAG_INIT_FAILED;
2722 static int flyswatter_init(int rev)
2725 low_direction = 0x7b;
2727 if ((rev < 0) || (rev > 3)) {
2728 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2729 return ERROR_JTAG_INIT_FAILED;
2733 low_direction |= 1 << 7;
2735 /* initialize low byte for jtag */
2736 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2737 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2738 return ERROR_JTAG_INIT_FAILED;
2742 nTRSTnOE = 0x0; /* not output enable for nTRST */
2744 nSRSTnOE = 0x00; /* no output enable for nSRST */
2749 high_direction = 0x0c;
2751 high_direction = 0x01;
2753 /* turn red LED3 on, LED2 off */
2754 high_output |= 0x08;
2756 /* initialize high byte for jtag */
2757 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2758 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2759 return ERROR_JTAG_INIT_FAILED;
2765 static int flyswatter1_init(void)
2767 return flyswatter_init(1);
2770 static int flyswatter2_init(void)
2772 return flyswatter_init(2);
2775 static int minimodule_init(void)
2777 low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2779 low_direction = 0xfb; /* 0xfb; */
2781 /* initialize low byte for jtag */
2782 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2783 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2784 return ERROR_JTAG_INIT_FAILED;
2791 high_direction = 0x05;
2793 /* turn red LED3 on, LED2 off */
2794 /* high_output |= 0x08; */
2796 /* initialize high byte for jtag */
2797 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2798 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2799 return ERROR_JTAG_INIT_FAILED;
2805 static int turtle_init(void)
2808 low_direction = 0x5b;
2810 /* initialize low byte for jtag */
2811 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2812 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2813 return ERROR_JTAG_INIT_FAILED;
2819 high_direction = 0x0C;
2821 /* initialize high byte for jtag */
2822 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2823 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2824 return ERROR_JTAG_INIT_FAILED;
2830 static int comstick_init(void)
2833 low_direction = 0x0b;
2835 /* initialize low byte for jtag */
2836 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2837 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2838 return ERROR_JTAG_INIT_FAILED;
2842 nTRSTnOE = 0x00; /* no output enable for nTRST */
2844 nSRSTnOE = 0x00; /* no output enable for nSRST */
2847 high_direction = 0x03;
2849 /* initialize high byte for jtag */
2850 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2851 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2852 return ERROR_JTAG_INIT_FAILED;
2858 static int stm32stick_init(void)
2861 low_direction = 0x8b;
2863 /* initialize low byte for jtag */
2864 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2865 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2866 return ERROR_JTAG_INIT_FAILED;
2870 nTRSTnOE = 0x00; /* no output enable for nTRST */
2872 nSRSTnOE = 0x00; /* no output enable for nSRST */
2875 high_direction = 0x03;
2877 /* initialize high byte for jtag */
2878 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2879 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2880 return ERROR_JTAG_INIT_FAILED;
2886 static int sheevaplug_init(void)
2889 low_direction = 0x1b;
2891 /* initialize low byte for jtag */
2892 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2893 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2894 return ERROR_JTAG_INIT_FAILED;
2903 high_direction = 0x0f;
2905 /* nTRST is always push-pull */
2906 high_output &= ~nTRSTnOE;
2907 high_output |= nTRST;
2909 /* nSRST is always open-drain */
2910 high_output |= nSRSTnOE;
2911 high_output &= ~nSRST;
2913 /* initialize high byte for jtag */
2914 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2915 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2916 return ERROR_JTAG_INIT_FAILED;
2922 static int cortino_jtag_init(void)
2925 low_direction = 0x1b;
2927 /* initialize low byte for jtag */
2928 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2929 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2930 return ERROR_JTAG_INIT_FAILED;
2934 nTRSTnOE = 0x00; /* no output enable for nTRST */
2936 nSRSTnOE = 0x00; /* no output enable for nSRST */
2939 high_direction = 0x03;
2941 /* initialize high byte for jtag */
2942 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2943 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2944 return ERROR_JTAG_INIT_FAILED;
2950 static int lisa_l_init(void)
2960 high_direction = 0x18;
2962 /* initialize high byte for jtag */
2963 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2964 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2965 return ERROR_JTAG_INIT_FAILED;
2968 return ftx232_dbus_write();
2971 static int flossjtag_init(void)
2981 high_direction = 0x18;
2983 /* initialize high byte for jtag */
2984 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2985 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2986 return ERROR_JTAG_INIT_FAILED;
2989 return ftx232_dbus_write();
2993 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
2994 * the door for a number of different configurations
2996 * Known Implementations:
2997 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
2999 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3000 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3001 * * ACBUS3 to transition 0->1 (OE rising edge)
3002 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3003 * * ADBUS5/GPIOL1 = EMU_EN = 1
3004 * * ADBUS6/GPIOL2 = EMU0 = 0
3005 * * ACBUS4/SPARE0 = EMU1 = 0
3006 * * CPLD logic: Disable loopback
3007 * * ACBUS6/SPARE2 = LOOPBACK = 0
3009 #define XDS100_nEMU_EN (1<<5)
3010 #define XDS100_nEMU0 (1<<6)
3012 #define XDS100_PWR_RST (1<<3)
3013 #define XDS100_nEMU1 (1<<4)
3014 #define XDS100_LOOPBACK (1<<6)
3015 static int xds100v2_init(void)
3017 /* These are in the lower byte */
3021 /* These aren't actually used on 14 pin connectors
3022 * These are in the upper byte */
3026 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3027 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3029 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3030 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3031 return ERROR_JTAG_INIT_FAILED;
3035 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3037 /* initialize high byte for jtag */
3038 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3039 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3040 return ERROR_JTAG_INIT_FAILED;
3043 high_output |= XDS100_PWR_RST;
3045 /* initialize high byte for jtag */
3046 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3047 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3048 return ERROR_JTAG_INIT_FAILED;
3054 static void olimex_jtag_blink(void)
3056 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3057 * ACBUS3 is bit 3 of the GPIOH port
3059 high_output ^= 0x08;
3062 buffer_write(high_output);
3063 buffer_write(high_direction);
3066 static void flyswatter_jtag_blink(unsigned char led)
3069 buffer_write(high_output ^ led);
3070 buffer_write(high_direction);
3073 static void flyswatter1_jtag_blink(void)
3076 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3078 flyswatter_jtag_blink(0xc);
3081 static void flyswatter2_jtag_blink(void)
3084 * Flyswatter2 only has one LED connected to ACBUS2
3086 flyswatter_jtag_blink(0x4);
3089 static void turtle_jtag_blink(void)
3092 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3094 if (high_output & 0x08)
3100 buffer_write(high_output);
3101 buffer_write(high_direction);
3104 static void lisa_l_blink(void)
3107 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3109 if (high_output & 0x10)
3115 buffer_write(high_output);
3116 buffer_write(high_direction);
3119 static void flossjtag_blink(void)
3122 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3124 if (high_output & 0x10)
3130 buffer_write(high_output);
3131 buffer_write(high_direction);
3134 static int ft2232_quit(void)
3136 #if BUILD_FT2232_FTD2XX == 1
3139 status = FT_Close(ftdih);
3140 #elif BUILD_FT2232_LIBFTDI == 1
3141 ftdi_usb_close(&ftdic);
3143 ftdi_deinit(&ftdic);
3146 free(ft2232_buffer);
3147 ft2232_buffer = NULL;
3152 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3156 if (CMD_ARGC == 1) {
3157 ft2232_device_desc = strdup(CMD_ARGV[0]);
3158 cp = strchr(ft2232_device_desc, 0);
3159 /* under Win32, the FTD2XX driver appends an "A" to the end
3160 * of the description, this examines the given desc
3161 * and creates the 'missing' _A or non_A variable. */
3162 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3163 /* it was, so make this the "A" version. */
3164 ft2232_device_desc_A = ft2232_device_desc;
3165 /* and *CREATE* the non-A version. */
3166 strcpy(buf, ft2232_device_desc);
3167 cp = strchr(buf, 0);
3169 ft2232_device_desc = strdup(buf);
3171 /* <space > A not defined
3173 sprintf(buf, "%s A", ft2232_device_desc);
3174 ft2232_device_desc_A = strdup(buf);
3177 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3182 COMMAND_HANDLER(ft2232_handle_serial_command)
3185 ft2232_serial = strdup(CMD_ARGV[0]);
3187 return ERROR_COMMAND_SYNTAX_ERROR;
3192 COMMAND_HANDLER(ft2232_handle_layout_command)
3195 return ERROR_COMMAND_SYNTAX_ERROR;
3198 LOG_ERROR("already specified ft2232_layout %s",
3200 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3205 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3206 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3208 ft2232_channel = l->channel;
3213 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3217 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3219 if (CMD_ARGC > MAX_USB_IDS * 2) {
3220 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3221 "(maximum is %d pairs)", MAX_USB_IDS);
3222 CMD_ARGC = MAX_USB_IDS * 2;
3224 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3225 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3227 return ERROR_COMMAND_SYNTAX_ERROR;
3228 /* remove the incomplete trailing id */
3233 for (i = 0; i < CMD_ARGC; i += 2) {
3234 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3235 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3239 * Explicitly terminate, in case there are multiples instances of
3242 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3247 COMMAND_HANDLER(ft2232_handle_latency_command)
3250 ft2232_latency = atoi(CMD_ARGV[0]);
3252 return ERROR_COMMAND_SYNTAX_ERROR;
3257 COMMAND_HANDLER(ft2232_handle_channel_command)
3259 if (CMD_ARGC == 1) {
3260 ft2232_channel = atoi(CMD_ARGV[0]);
3261 if (ft2232_channel < 0 || ft2232_channel > 4)
3262 LOG_ERROR("ft2232_channel must be in the 0 to 4 range");
3264 LOG_ERROR("expected exactly one argument to ft2232_channel <ch>");
3269 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3273 /* 7 bits of either ones or zeros. */
3274 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3276 while (num_cycles > 0) {
3277 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3278 * at most 7 bits per invocation. Here we invoke it potentially
3281 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3283 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3284 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3285 retval = ERROR_JTAG_QUEUE_FAILED;
3290 /* there are no state transitions in this code, so omit state tracking */
3292 /* command "Clock Data to TMS/CS Pin (no Read)" */
3296 buffer_write(bitcount_per_command - 1);
3298 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3303 num_cycles -= bitcount_per_command;
3309 /* ---------------------------------------------------------------------
3310 * Support for IceBear JTAG adapter from Section5:
3311 * http://section5.ch/icebear
3313 * Author: Sten, debian@sansys-electronic.com
3316 /* Icebear pin layout
3318 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3319 * GND GND | 4 3| n.c.
3320 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3321 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3322 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3323 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3324 * ADBUS2 TDO |14 13| GND GND
3326 * ADBUS0 O L TCK ACBUS0 GND
3327 * ADBUS1 O L TDI ACBUS1 GND
3328 * ADBUS2 I TDO ACBUS2 n.c.
3329 * ADBUS3 O H TMS ACBUS3 n.c.
3335 static int icebear_jtag_init(void)
3337 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3338 low_output = 0x08; /* high: TMS; low: TCK TDI */
3342 enum reset_types jtag_reset_config = jtag_get_reset_config();
3343 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3344 low_direction &= ~nTRST; /* nTRST high impedance */
3346 low_direction |= nTRST;
3347 low_output |= nTRST;
3350 low_direction |= nSRST;
3351 low_output |= nSRST;
3353 /* initialize low byte for jtag */
3354 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3355 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3356 return ERROR_JTAG_INIT_FAILED;
3360 high_direction = 0x00;
3362 /* initialize high byte for jtag */
3363 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3364 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3365 return ERROR_JTAG_INIT_FAILED;
3371 static void icebear_jtag_reset(int trst, int srst)
3374 low_direction |= nTRST;
3375 low_output &= ~nTRST;
3376 } else if (trst == 0) {
3377 enum reset_types jtag_reset_config = jtag_get_reset_config();
3378 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3379 low_direction &= ~nTRST;
3381 low_output |= nTRST;
3385 low_output &= ~nSRST;
3387 low_output |= nSRST;
3389 /* command "set data bits low byte" */
3391 buffer_write(low_output);
3392 buffer_write(low_direction);
3394 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3401 /* ---------------------------------------------------------------------
3402 * Support for Signalyzer H2 and Signalyzer H4
3403 * JTAG adapter from Xverve Technologies Inc.
3404 * http://www.signalyzer.com or http://www.xverve.com
3406 * Author: Oleg Seiljus, oleg@signalyzer.com
3408 static unsigned char signalyzer_h_side;
3409 static unsigned int signalyzer_h_adapter_type;
3411 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3413 #if BUILD_FT2232_FTD2XX == 1
3414 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3417 #define SIGNALYZER_COMMAND_ADDR 128
3418 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3420 #define SIGNALYZER_COMMAND_VERSION 0x41
3421 #define SIGNALYZER_COMMAND_RESET 0x42
3422 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3423 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3424 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3425 #define SIGNALYZER_COMMAND_LED_SET 0x53
3426 #define SIGNALYZER_COMMAND_ADC 0x54
3427 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3428 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3429 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3430 #define SIGNALYZER_COMMAND_I2C 0x58
3432 #define SIGNALYZER_CHAN_A 1
3433 #define SIGNALYZER_CHAN_B 2
3434 /* LEDS use channel C */
3435 #define SIGNALYZER_CHAN_C 4
3437 #define SIGNALYZER_LED_GREEN 1
3438 #define SIGNALYZER_LED_RED 2
3440 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3441 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3442 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3443 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3444 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3447 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3449 #if BUILD_FT2232_FTD2XX == 1
3450 return FT_WriteEE(ftdih, address, value);
3451 #elif BUILD_FT2232_LIBFTDI == 1
3456 #if BUILD_FT2232_FTD2XX == 1
3457 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3459 return FT_ReadEE(ftdih, address, value);
3463 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3464 int on_time_ms, int off_time_ms, unsigned char cycles)
3466 unsigned char on_time;
3467 unsigned char off_time;
3469 if (on_time_ms < 0xFFFF)
3470 on_time = (unsigned char)(on_time_ms / 62);
3474 off_time = (unsigned char)(off_time_ms / 62);
3476 #if BUILD_FT2232_FTD2XX == 1
3479 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3480 ((uint32_t)(channel << 8) | led));
3481 if (status != FT_OK) {
3482 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3483 ftd2xx_status_string(status));
3484 return ERROR_JTAG_DEVICE_ERROR;
3487 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3488 ((uint32_t)(on_time << 8) | off_time));
3489 if (status != FT_OK) {
3490 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3491 ftd2xx_status_string(status));
3492 return ERROR_JTAG_DEVICE_ERROR;
3495 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3496 ((uint32_t)cycles));
3497 if (status != FT_OK) {
3498 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3499 ftd2xx_status_string(status));
3500 return ERROR_JTAG_DEVICE_ERROR;
3503 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3504 SIGNALYZER_COMMAND_LED_SET);
3505 if (status != FT_OK) {
3506 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3507 ftd2xx_status_string(status));
3508 return ERROR_JTAG_DEVICE_ERROR;
3512 #elif BUILD_FT2232_LIBFTDI == 1
3515 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3516 ((uint32_t)(channel << 8) | led));
3518 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3519 ftdi_get_error_string(&ftdic));
3520 return ERROR_JTAG_DEVICE_ERROR;
3523 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3524 ((uint32_t)(on_time << 8) | off_time));
3526 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3527 ftdi_get_error_string(&ftdic));
3528 return ERROR_JTAG_DEVICE_ERROR;
3531 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3534 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3535 ftdi_get_error_string(&ftdic));
3536 return ERROR_JTAG_DEVICE_ERROR;
3539 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3540 SIGNALYZER_COMMAND_LED_SET);
3542 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3543 ftdi_get_error_string(&ftdic));
3544 return ERROR_JTAG_DEVICE_ERROR;
3551 static int signalyzer_h_init(void)
3553 #if BUILD_FT2232_FTD2XX == 1
3560 uint16_t read_buf[12] = { 0 };
3562 /* turn on center green led */
3563 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3564 0xFFFF, 0x00, 0x00);
3566 /* determine what channel config wants to open
3567 * TODO: change me... current implementation is made to work
3568 * with openocd description parsing.
3570 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3573 signalyzer_h_side = *(end_of_desc - 1);
3574 if (signalyzer_h_side == 'B')
3575 signalyzer_h_side = SIGNALYZER_CHAN_B;
3577 signalyzer_h_side = SIGNALYZER_CHAN_A;
3579 LOG_ERROR("No Channel was specified");
3583 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3586 #if BUILD_FT2232_FTD2XX == 1
3587 /* read signalyzer versionining information */
3588 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3589 SIGNALYZER_COMMAND_VERSION);
3590 if (status != FT_OK) {
3591 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3592 ftd2xx_status_string(status));
3593 return ERROR_JTAG_DEVICE_ERROR;
3596 for (i = 0; i < 10; i++) {
3597 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3599 if (status != FT_OK) {
3600 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3601 ftd2xx_status_string(status));
3602 return ERROR_JTAG_DEVICE_ERROR;
3606 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3607 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3608 read_buf[4], read_buf[5], read_buf[6]);
3610 /* set gpio register */
3611 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3612 (uint32_t)(signalyzer_h_side << 8));
3613 if (status != FT_OK) {
3614 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3615 ftd2xx_status_string(status));
3616 return ERROR_JTAG_DEVICE_ERROR;
3619 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
3620 if (status != FT_OK) {
3621 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3622 ftd2xx_status_string(status));
3623 return ERROR_JTAG_DEVICE_ERROR;
3626 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3627 SIGNALYZER_COMMAND_GPIO_STATE);
3628 if (status != FT_OK) {
3629 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3630 ftd2xx_status_string(status));
3631 return ERROR_JTAG_DEVICE_ERROR;
3634 /* read adapter type information */
3635 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3636 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3637 if (status != FT_OK) {
3638 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3639 ftd2xx_status_string(status));
3640 return ERROR_JTAG_DEVICE_ERROR;
3643 status = signalyzer_h_ctrl_write(
3644 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3645 if (status != FT_OK) {
3646 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3647 ftd2xx_status_string(status));
3648 return ERROR_JTAG_DEVICE_ERROR;
3651 status = signalyzer_h_ctrl_write(
3652 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3653 if (status != FT_OK) {
3654 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3655 ftd2xx_status_string(status));
3656 return ERROR_JTAG_DEVICE_ERROR;
3659 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3660 SIGNALYZER_COMMAND_I2C);
3661 if (status != FT_OK) {
3662 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3663 ftd2xx_status_string(status));
3664 return ERROR_JTAG_DEVICE_ERROR;
3669 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3670 if (status != FT_OK) {
3671 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3672 ftd2xx_status_string(status));
3673 return ERROR_JTAG_DEVICE_ERROR;
3676 if (read_buf[0] != 0x0498)
3677 signalyzer_h_adapter_type = 0x0000;
3679 for (i = 0; i < 4; i++) {
3680 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3681 if (status != FT_OK) {
3682 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3683 ftd2xx_status_string(status));
3684 return ERROR_JTAG_DEVICE_ERROR;
3688 signalyzer_h_adapter_type = read_buf[0];
3691 #elif BUILD_FT2232_LIBFTDI == 1
3692 /* currently libftdi does not allow reading individual eeprom
3693 * locations, therefore adapter type cannot be detected.
3694 * override with most common type
3696 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3699 enum reset_types jtag_reset_config = jtag_get_reset_config();
3701 /* ADAPTOR: EM_LT16_A */
3702 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3703 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3704 "detected. (HW: %2x).", (read_buf[1] >> 8));
3712 low_direction = 0x1b;
3715 high_direction = 0x0;
3717 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3718 low_direction &= ~nTRSTnOE; /* nTRST input */
3719 low_output &= ~nTRST; /* nTRST = 0 */
3721 low_direction |= nTRSTnOE; /* nTRST output */
3722 low_output |= nTRST; /* nTRST = 1 */
3725 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3726 low_direction |= nSRSTnOE; /* nSRST output */
3727 low_output |= nSRST; /* nSRST = 1 */
3729 low_direction &= ~nSRSTnOE; /* nSRST input */
3730 low_output &= ~nSRST; /* nSRST = 0 */
3733 #if BUILD_FT2232_FTD2XX == 1
3734 /* enable power to the module */
3735 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3736 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3737 if (status != FT_OK) {
3738 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3739 ftd2xx_status_string(status));
3740 return ERROR_JTAG_DEVICE_ERROR;
3743 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3744 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3745 if (status != FT_OK) {
3746 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3747 ftd2xx_status_string(status));
3748 return ERROR_JTAG_DEVICE_ERROR;
3751 /* set gpio mode register */
3752 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3753 (uint32_t)(signalyzer_h_side << 8));
3754 if (status != FT_OK) {
3755 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3756 ftd2xx_status_string(status));
3757 return ERROR_JTAG_DEVICE_ERROR;
3760 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3761 if (status != FT_OK) {
3762 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3763 ftd2xx_status_string(status));
3764 return ERROR_JTAG_DEVICE_ERROR;
3767 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3768 if (status != FT_OK) {
3769 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3770 ftd2xx_status_string(status));
3771 return ERROR_JTAG_DEVICE_ERROR;
3774 /* set gpio register */
3775 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3776 (uint32_t)(signalyzer_h_side << 8));
3777 if (status != FT_OK) {
3778 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3779 ftd2xx_status_string(status));
3780 return ERROR_JTAG_DEVICE_ERROR;
3783 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040);
3784 if (status != FT_OK) {
3785 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3786 ftd2xx_status_string(status));
3787 return ERROR_JTAG_DEVICE_ERROR;
3790 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3791 SIGNALYZER_COMMAND_GPIO_STATE);
3792 if (status != FT_OK) {
3793 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3794 ftd2xx_status_string(status));
3795 return ERROR_JTAG_DEVICE_ERROR;
3799 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3800 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3801 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3802 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3803 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3804 if (signalyzer_h_adapter_type
3805 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3806 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3807 "detected. (HW: %2x).", (read_buf[1] >> 8));
3808 else if (signalyzer_h_adapter_type
3809 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3810 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3811 "(ARM JTAG with PSU) detected. (HW: %2x).",
3812 (read_buf[1] >> 8));
3813 else if (signalyzer_h_adapter_type
3814 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3815 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3816 "detected. (HW: %2x).", (read_buf[1] >> 8));
3817 else if (signalyzer_h_adapter_type
3818 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3819 LOG_INFO("Signalyzer: EM-JTAG-P "
3820 "(Generic JTAG with PSU) detected. (HW: %2x).",
3821 (read_buf[1] >> 8));
3829 low_direction = 0x1b;
3832 high_direction = 0x1f;
3834 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3835 high_output |= nTRSTnOE;
3836 high_output &= ~nTRST;
3838 high_output &= ~nTRSTnOE;
3839 high_output |= nTRST;
3842 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3843 high_output &= ~nSRSTnOE;
3844 high_output |= nSRST;
3846 high_output |= nSRSTnOE;
3847 high_output &= ~nSRST;
3850 #if BUILD_FT2232_FTD2XX == 1
3851 /* enable power to the module */
3852 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3853 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3854 if (status != FT_OK) {
3855 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3856 ftd2xx_status_string(status));
3857 return ERROR_JTAG_DEVICE_ERROR;
3860 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3861 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3862 if (status != FT_OK) {
3863 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3864 ftd2xx_status_string(status));
3865 return ERROR_JTAG_DEVICE_ERROR;
3868 /* set gpio mode register (IO_16 and IO_17 set as analog
3869 * inputs, other is gpio)
3871 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3872 (uint32_t)(signalyzer_h_side << 8));
3873 if (status != FT_OK) {
3874 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3875 ftd2xx_status_string(status));
3876 return ERROR_JTAG_DEVICE_ERROR;
3879 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060);
3880 if (status != FT_OK) {
3881 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3882 ftd2xx_status_string(status));
3883 return ERROR_JTAG_DEVICE_ERROR;
3886 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3887 if (status != FT_OK) {
3888 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3889 ftd2xx_status_string(status));
3890 return ERROR_JTAG_DEVICE_ERROR;
3893 /* set gpio register (all inputs, for -P modules,
3894 * PSU will be turned off)
3896 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3897 (uint32_t)(signalyzer_h_side << 8));
3898 if (status != FT_OK) {
3899 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3900 ftd2xx_status_string(status));
3901 return ERROR_JTAG_DEVICE_ERROR;
3904 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3905 if (status != FT_OK) {
3906 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3907 ftd2xx_status_string(status));
3908 return ERROR_JTAG_DEVICE_ERROR;
3911 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
3912 if (status != FT_OK) {
3913 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3914 ftd2xx_status_string(status));
3915 return ERROR_JTAG_DEVICE_ERROR;
3918 } else if (signalyzer_h_adapter_type == 0x0000) {
3919 LOG_INFO("Signalyzer: No external modules were detected.");
3927 low_direction = 0x1b;
3930 high_direction = 0x0;
3932 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3933 low_direction &= ~nTRSTnOE; /* nTRST input */
3934 low_output &= ~nTRST; /* nTRST = 0 */
3936 low_direction |= nTRSTnOE; /* nTRST output */
3937 low_output |= nTRST; /* nTRST = 1 */
3940 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3941 low_direction |= nSRSTnOE; /* nSRST output */
3942 low_output |= nSRST; /* nSRST = 1 */
3944 low_direction &= ~nSRSTnOE; /* nSRST input */
3945 low_output &= ~nSRST; /* nSRST = 0 */
3948 LOG_ERROR("Unknown module type is detected: %.4x",
3949 signalyzer_h_adapter_type);
3950 return ERROR_JTAG_DEVICE_ERROR;
3953 /* initialize low byte of controller for jtag operation */
3954 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3955 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3956 return ERROR_JTAG_INIT_FAILED;
3959 #if BUILD_FT2232_FTD2XX == 1
3960 if (ftdi_device == FT_DEVICE_2232H) {
3961 /* initialize high byte of controller for jtag operation */
3962 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3963 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3964 return ERROR_JTAG_INIT_FAILED;
3967 #elif BUILD_FT2232_LIBFTDI == 1
3968 if (ftdi_device == TYPE_2232H) {
3969 /* initialize high byte of controller for jtag operation */
3970 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3971 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3972 return ERROR_JTAG_INIT_FAILED;
3979 static void signalyzer_h_reset(int trst, int srst)
3981 enum reset_types jtag_reset_config = jtag_get_reset_config();
3983 /* ADAPTOR: EM_LT16_A */
3984 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3986 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3987 /* switch to output pin (output is low) */
3988 low_direction |= nTRSTnOE;
3990 /* switch output low */
3991 low_output &= ~nTRST;
3992 } else if (trst == 0) {
3993 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3994 /* switch to input pin (high-Z + internal
3995 * and external pullup) */
3996 low_direction &= ~nTRSTnOE;
3998 /* switch output high */
3999 low_output |= nTRST;
4003 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4004 /* switch output low */
4005 low_output &= ~nSRST;
4007 /* switch to output pin (output is low) */
4008 low_direction |= nSRSTnOE;
4009 } else if (srst == 0) {
4010 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4011 /* switch output high */
4012 low_output |= nSRST;
4014 /* switch to input pin (high-Z) */
4015 low_direction &= ~nSRSTnOE;
4018 /* command "set data bits low byte" */
4020 buffer_write(low_output);
4021 buffer_write(low_direction);
4022 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4023 "low_direction: 0x%2.2x",
4024 trst, srst, low_output, low_direction);
4026 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4027 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4028 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4029 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4030 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4032 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4033 high_output &= ~nTRSTnOE;
4035 high_output &= ~nTRST;
4036 } else if (trst == 0) {
4037 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4038 high_output |= nTRSTnOE;
4040 high_output |= nTRST;
4044 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4045 high_output &= ~nSRST;
4047 high_output &= ~nSRSTnOE;
4048 } else if (srst == 0) {
4049 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4050 high_output |= nSRST;
4052 high_output |= nSRSTnOE;
4055 /* command "set data bits high byte" */
4057 buffer_write(high_output);
4058 buffer_write(high_direction);
4059 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4060 "high_direction: 0x%2.2x",
4061 trst, srst, high_output, high_direction);
4062 } else if (signalyzer_h_adapter_type == 0x0000) {
4064 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4065 /* switch to output pin (output is low) */
4066 low_direction |= nTRSTnOE;
4068 /* switch output low */
4069 low_output &= ~nTRST;
4070 } else if (trst == 0) {
4071 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4072 /* switch to input pin (high-Z + internal
4073 * and external pullup) */
4074 low_direction &= ~nTRSTnOE;
4076 /* switch output high */
4077 low_output |= nTRST;
4081 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4082 /* switch output low */
4083 low_output &= ~nSRST;
4085 /* switch to output pin (output is low) */
4086 low_direction |= nSRSTnOE;
4087 } else if (srst == 0) {
4088 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4089 /* switch output high */
4090 low_output |= nSRST;
4092 /* switch to input pin (high-Z) */
4093 low_direction &= ~nSRSTnOE;
4096 /* command "set data bits low byte" */
4098 buffer_write(low_output);
4099 buffer_write(low_direction);
4100 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4101 "low_direction: 0x%2.2x",
4102 trst, srst, low_output, low_direction);
4106 static void signalyzer_h_blink(void)
4108 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4111 /********************************************************************
4112 * Support for KT-LINK
4113 * JTAG adapter from KRISTECH
4114 * http://www.kristech.eu
4115 *******************************************************************/
4116 static int ktlink_init(void)
4118 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4120 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4121 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4123 /* initialize low byte for jtag */
4124 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4125 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4126 return ERROR_JTAG_INIT_FAILED;
4134 high_output = 0x80; /* turn LED on */
4135 high_direction = 0xFF; /* all outputs */
4137 enum reset_types jtag_reset_config = jtag_get_reset_config();
4139 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4140 high_output |= nTRSTnOE;
4141 high_output &= ~nTRST;
4143 high_output &= ~nTRSTnOE;
4144 high_output |= nTRST;
4147 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4148 high_output &= ~nSRSTnOE;
4149 high_output |= nSRST;
4151 high_output |= nSRSTnOE;
4152 high_output &= ~nSRST;
4155 /* initialize high byte for jtag */
4156 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4157 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4158 return ERROR_JTAG_INIT_FAILED;
4164 static void ktlink_reset(int trst, int srst)
4166 enum reset_types jtag_reset_config = jtag_get_reset_config();
4169 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4170 high_output &= ~nTRSTnOE;
4172 high_output &= ~nTRST;
4173 } else if (trst == 0) {
4174 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4175 high_output |= nTRSTnOE;
4177 high_output |= nTRST;
4181 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4182 high_output &= ~nSRST;
4184 high_output &= ~nSRSTnOE;
4185 } else if (srst == 0) {
4186 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4187 high_output |= nSRST;
4189 high_output |= nSRSTnOE;
4192 buffer_write(0x82); /* command "set data bits high byte" */
4193 buffer_write(high_output);
4194 buffer_write(high_direction);
4195 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4202 static void ktlink_blink(void)
4204 /* LED connected to ACBUS7 */
4205 high_output ^= 0x80;
4207 buffer_write(0x82); /* command "set data bits high byte" */
4208 buffer_write(high_output);
4209 buffer_write(high_direction);
4212 /********************************************************************
4213 * Support for Digilent HS-1
4214 * JTAG adapter from Digilent
4215 * http://www.digilent.com
4216 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4217 *******************************************************************/
4219 static int digilent_hs1_init(void)
4221 /* the adapter only supports the base JTAG signals, no nTRST
4224 low_direction = 0x8b;
4226 /* initialize low byte for jtag */
4227 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4228 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4229 return ERROR_JTAG_INIT_FAILED;
4234 static void digilent_hs1_reset(int trst, int srst)
4236 /* Dummy function, no reset signals supported. */
4239 static const struct command_registration ft2232_command_handlers[] = {
4241 .name = "ft2232_device_desc",
4242 .handler = &ft2232_handle_device_desc_command,
4243 .mode = COMMAND_CONFIG,
4244 .help = "set the USB device description of the FTDI FT2232 device",
4245 .usage = "description_string",
4248 .name = "ft2232_serial",
4249 .handler = &ft2232_handle_serial_command,
4250 .mode = COMMAND_CONFIG,
4251 .help = "set the serial number of the FTDI FT2232 device",
4252 .usage = "serial_string",
4255 .name = "ft2232_layout",
4256 .handler = &ft2232_handle_layout_command,
4257 .mode = COMMAND_CONFIG,
4258 .help = "set the layout of the FT2232 GPIO signals used "
4259 "to control output-enables and reset signals",
4260 .usage = "layout_name",
4263 .name = "ft2232_vid_pid",
4264 .handler = &ft2232_handle_vid_pid_command,
4265 .mode = COMMAND_CONFIG,
4266 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4267 .usage = "(vid pid)* ",
4270 .name = "ft2232_latency",
4271 .handler = &ft2232_handle_latency_command,
4272 .mode = COMMAND_CONFIG,
4273 .help = "set the FT2232 latency timer to a new value",
4277 .name = "ft2232_channel",
4278 .handler = &ft2232_handle_channel_command,
4279 .mode = COMMAND_CONFIG,
4280 .help = "set the FT2232 channel to a new value",
4283 COMMAND_REGISTRATION_DONE
4286 struct jtag_interface ft2232_interface = {
4288 .supported = DEBUG_CAP_TMS_SEQ,
4289 .commands = ft2232_command_handlers,
4290 .transports = jtag_only,
4292 .init = ft2232_init,
4293 .quit = ft2232_quit,
4294 .speed = ft2232_speed,
4295 .speed_div = ft2232_speed_div,
4297 .execute_queue = ft2232_execute_queue,