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_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
154 static char *ft2232_device_desc_A;
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;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165 struct ft2232_layout {
168 void (*reset)(int trst, int srst);
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter1_init(void);
180 static int flyswatter2_init(void);
181 static int minimodule_init(void);
182 static int turtle_init(void);
183 static int comstick_init(void);
184 static int stm32stick_init(void);
185 static int axm0432_jtag_init(void);
186 static int sheevaplug_init(void);
187 static int icebear_jtag_init(void);
188 static int cortino_jtag_init(void);
189 static int signalyzer_init(void);
190 static int signalyzer_h_init(void);
191 static int ktlink_init(void);
192 static int redbee_init(void);
193 static int lisa_l_init(void);
194 static int flossjtag_init(void);
195 static int xds100v2_init(void);
196 static int digilent_hs1_init(void);
198 /* reset procedures for supported layouts */
199 static void ftx23_reset(int trst, int srst);
200 static void jtagkey_reset(int trst, int srst);
201 static void olimex_jtag_reset(int trst, int srst);
202 static void flyswatter1_reset(int trst, int srst);
203 static void flyswatter2_reset(int trst, int srst);
204 static void minimodule_reset(int trst, int srst);
205 static void turtle_reset(int trst, int srst);
206 static void comstick_reset(int trst, int srst);
207 static void stm32stick_reset(int trst, int srst);
208 static void axm0432_jtag_reset(int trst, int srst);
209 static void sheevaplug_reset(int trst, int srst);
210 static void icebear_jtag_reset(int trst, int srst);
211 static void signalyzer_h_reset(int trst, int srst);
212 static void ktlink_reset(int trst, int srst);
213 static void redbee_reset(int trst, int srst);
214 static void xds100v2_reset(int trst, int srst);
215 static void digilent_hs1_reset(int trst, int srst);
217 /* blink procedures for layouts that support a blinking led */
218 static void olimex_jtag_blink(void);
219 static void flyswatter1_jtag_blink(void);
220 static void flyswatter2_jtag_blink(void);
221 static void turtle_jtag_blink(void);
222 static void signalyzer_h_blink(void);
223 static void ktlink_blink(void);
224 static void lisa_l_blink(void);
225 static void flossjtag_blink(void);
227 /* common transport support options */
229 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
231 static const struct ft2232_layout ft2232_layouts[] = {
233 .init = usbjtag_init,
234 .reset = ftx23_reset,
237 .init = jtagkey_init,
238 .reset = jtagkey_reset,
240 { .name = "jtagkey_prototype_v1",
241 .init = jtagkey_init,
242 .reset = jtagkey_reset,
244 { .name = "oocdlink",
245 .init = jtagkey_init,
246 .reset = jtagkey_reset,
248 { .name = "signalyzer",
249 .init = signalyzer_init,
250 .reset = ftx23_reset,
252 { .name = "evb_lm3s811",
253 .init = lm3s811_jtag_init,
254 .reset = ftx23_reset,
256 { .name = "luminary_icdi",
257 .init = icdi_jtag_init,
258 .reset = ftx23_reset,
260 { .name = "olimex-jtag",
261 .init = olimex_jtag_init,
262 .reset = olimex_jtag_reset,
263 .blink = olimex_jtag_blink
265 { .name = "flyswatter",
266 .init = flyswatter1_init,
267 .reset = flyswatter1_reset,
268 .blink = flyswatter1_jtag_blink
270 { .name = "flyswatter2",
271 .init = flyswatter2_init,
272 .reset = flyswatter2_reset,
273 .blink = flyswatter2_jtag_blink
275 { .name = "minimodule",
276 .init = minimodule_init,
277 .reset = minimodule_reset,
279 { .name = "turtelizer2",
281 .reset = turtle_reset,
282 .blink = turtle_jtag_blink
284 { .name = "comstick",
285 .init = comstick_init,
286 .reset = comstick_reset,
288 { .name = "stm32stick",
289 .init = stm32stick_init,
290 .reset = stm32stick_reset,
292 { .name = "axm0432_jtag",
293 .init = axm0432_jtag_init,
294 .reset = axm0432_jtag_reset,
296 { .name = "sheevaplug",
297 .init = sheevaplug_init,
298 .reset = sheevaplug_reset,
301 .init = icebear_jtag_init,
302 .reset = icebear_jtag_reset,
305 .init = cortino_jtag_init,
306 .reset = comstick_reset,
308 { .name = "signalyzer-h",
309 .init = signalyzer_h_init,
310 .reset = signalyzer_h_reset,
311 .blink = signalyzer_h_blink
315 .reset = ktlink_reset,
316 .blink = ktlink_blink
318 { .name = "redbee-econotag",
320 .reset = redbee_reset,
322 { .name = "redbee-usb",
324 .reset = redbee_reset,
325 .channel = INTERFACE_B,
329 .reset = ftx23_reset,
330 .blink = lisa_l_blink,
331 .channel = INTERFACE_B,
333 { .name = "flossjtag",
334 .init = flossjtag_init,
335 .reset = ftx23_reset,
336 .blink = flossjtag_blink,
338 { .name = "xds100v2",
339 .init = xds100v2_init,
340 .reset = xds100v2_reset,
342 { .name = "digilent-hs1",
343 .init = digilent_hs1_init,
344 .reset = digilent_hs1_reset,
345 .channel = INTERFACE_A,
347 { .name = NULL, /* END OF TABLE */ },
350 /* bitmask used to drive nTRST; usually a GPIOLx signal */
351 static uint8_t nTRST;
352 static uint8_t nTRSTnOE;
353 /* bitmask used to drive nSRST; usually a GPIOLx signal */
354 static uint8_t nSRST;
355 static uint8_t nSRSTnOE;
357 /** the layout being used with this debug session */
358 static const struct ft2232_layout *layout;
360 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
361 static uint8_t low_output;
363 /* note that direction bit == 1 means that signal is an output */
365 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
366 static uint8_t low_direction;
367 /** default value bitmask for CBUS GPIOH(0..4) */
368 static uint8_t high_output;
369 /** default direction bitmask for CBUS GPIOH(0..4) */
370 static uint8_t high_direction;
372 #if BUILD_FT2232_FTD2XX == 1
373 static FT_HANDLE ftdih;
374 static FT_DEVICE ftdi_device;
375 #elif BUILD_FT2232_LIBFTDI == 1
376 static struct ftdi_context ftdic;
377 static enum ftdi_chip_type ftdi_device;
380 static struct jtag_command *first_unsent; /* next command that has to be sent */
381 static int require_send;
383 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
385 "There is a significant difference between libftdi and libftd2xx. The latter
386 one allows to schedule up to 64*64 bytes of result data while libftdi fails
387 with more than 4*64. As a consequence, the FT2232 driver is forced to
388 perform around 16x more USB transactions for long command streams with TDO
389 capture when running with libftdi."
392 #define FT2232_BUFFER_SIZE 131072
393 a comment would have been nice.
396 #if BUILD_FT2232_FTD2XX == 1
397 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
399 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
402 #define FT2232_BUFFER_SIZE 131072
404 static uint8_t *ft2232_buffer;
405 static int ft2232_buffer_size;
406 static int ft2232_read_pointer;
407 static int ft2232_expect_read;
410 * Function buffer_write
411 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
412 * @param val is the byte to send.
414 static inline void buffer_write(uint8_t val)
416 assert(ft2232_buffer);
417 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
418 ft2232_buffer[ft2232_buffer_size++] = val;
422 * Function buffer_read
423 * returns a byte from the byte buffer.
425 static inline uint8_t buffer_read(void)
427 assert(ft2232_buffer);
428 assert(ft2232_read_pointer < ft2232_buffer_size);
429 return ft2232_buffer[ft2232_read_pointer++];
433 * Clocks out \a bit_count bits on the TMS line, starting with the least
434 * significant bit of tms_bits and progressing to more significant bits.
435 * Rigorous state transition logging is done here via tap_set_state().
437 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
438 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
439 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
440 * is often used for this, 0x4b.
442 * @param tms_bits Holds the sequence of bits to send.
443 * @param tms_count Tells how many bits in the sequence.
444 * @param tdi_bit A single bit to pass on to TDI before the first TCK
445 * cycle and held static for the duration of TMS clocking.
447 * See the MPSSE spec referenced above.
449 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
453 int tms_ndx; /* bit index into tms_byte */
455 assert(tms_count > 0);
457 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
458 mpsse_cmd, tms_bits, tms_count);
460 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
461 bool bit = tms_bits & 1;
464 tms_byte |= (1 << tms_ndx);
466 /* always do state transitions in public view */
467 tap_set_state(tap_state_transition(tap_get_state(), bit));
469 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
474 if (tms_ndx == 7 || i == tms_count-1) {
475 buffer_write(mpsse_cmd);
476 buffer_write(tms_ndx - 1);
478 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
479 * TMS/CS and is held static for the duration of TMS/CS clocking.
481 buffer_write(tms_byte | (tdi_bit << 7));
487 * Function get_tms_buffer_requirements
488 * returns what clock_tms() will consume if called with
491 static inline int get_tms_buffer_requirements(int bit_count)
493 return ((bit_count + 6)/7) * 3;
497 * Function move_to_state
498 * moves the TAP controller from the current state to a
499 * \a goal_state through a path given by tap_get_tms_path(). State transition
500 * logging is performed by delegation to clock_tms().
502 * @param goal_state is the destination state for the move.
504 static void move_to_state(tap_state_t goal_state)
506 tap_state_t start_state = tap_get_state();
508 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
509 * lookup of the required TMS pattern to move to this state from the start state.
512 /* do the 2 lookups */
513 int tms_bits = tap_get_tms_path(start_state, goal_state);
514 int tms_count = tap_get_tms_path_len(start_state, goal_state);
516 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
518 clock_tms(0x4b, tms_bits, tms_count, 0);
521 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
523 #if BUILD_FT2232_FTD2XX == 1
525 DWORD dw_bytes_written = 0;
526 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
527 if (status != FT_OK) {
528 *bytes_written = dw_bytes_written;
529 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
530 return ERROR_JTAG_DEVICE_ERROR;
532 *bytes_written = dw_bytes_written;
534 #elif BUILD_FT2232_LIBFTDI == 1
535 int retval = ftdi_write_data(&ftdic, buf, size);
538 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
539 return ERROR_JTAG_DEVICE_ERROR;
541 *bytes_written = retval;
545 if (*bytes_written != (uint32_t)size)
546 return ERROR_JTAG_DEVICE_ERROR;
551 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
553 #if BUILD_FT2232_FTD2XX == 1
559 while ((*bytes_read < size) && timeout--) {
560 status = FT_Read(ftdih, buf + *bytes_read, size -
561 *bytes_read, &dw_bytes_read);
562 if (status != FT_OK) {
564 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
565 return ERROR_JTAG_DEVICE_ERROR;
567 *bytes_read += dw_bytes_read;
570 #elif BUILD_FT2232_LIBFTDI == 1
572 int timeout = LIBFTDI_READ_RETRY_COUNT;
575 while ((*bytes_read < size) && timeout--) {
576 retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
579 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
580 return ERROR_JTAG_DEVICE_ERROR;
582 *bytes_read += retval;
587 if (*bytes_read < size) {
588 LOG_ERROR("couldn't read enough bytes from "
589 "FT2232 device (%i < %i)",
590 (unsigned)*bytes_read,
592 return ERROR_JTAG_DEVICE_ERROR;
598 static bool ft2232_device_is_highspeed(void)
600 #if BUILD_FT2232_FTD2XX == 1
601 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
602 #elif BUILD_FT2232_LIBFTDI == 1
603 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
608 * Commands that only apply to the FT2232H and FT4232H devices.
609 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
610 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
613 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
615 uint8_t buf = enable ? 0x96 : 0x97;
616 LOG_DEBUG("%2.2x", buf);
618 uint32_t bytes_written;
621 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
622 if (retval != ERROR_OK) {
623 LOG_ERROR("couldn't write command to %s adaptive clocking"
624 , enable ? "enable" : "disable");
632 * Enable/disable the clk divide by 5 of the 60MHz master clock.
633 * This result in a JTAG clock speed range of 91.553Hz-6MHz
634 * respective 457.763Hz-30MHz.
636 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
638 uint32_t bytes_written;
639 uint8_t buf = enable ? 0x8b : 0x8a;
641 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
642 LOG_ERROR("couldn't write command to %s clk divide by 5"
643 , enable ? "enable" : "disable");
644 return ERROR_JTAG_INIT_FAILED;
646 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
647 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
652 static int ft2232_speed(int speed)
656 uint32_t bytes_written;
659 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
660 if (ft2232_device_is_highspeed())
661 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
662 else if (enable_adaptive_clocking) {
663 LOG_ERROR("ft2232 device %lu does not support RTCK"
664 , (long unsigned int)ftdi_device);
668 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
671 buf[0] = 0x86; /* command "set divisor" */
672 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
673 buf[2] = (speed >> 8) & 0xff; /* valueH */
675 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
676 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
677 if (retval != ERROR_OK) {
678 LOG_ERROR("couldn't set FT2232 TCK speed");
685 static int ft2232_speed_div(int speed, int *khz)
687 /* Take a look in the FT2232 manual,
688 * AN2232C-01 Command Processor for
689 * MPSSE and MCU Host Bus. Chapter 3.8 */
691 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
696 static int ft2232_khz(int khz, int *jtag_speed)
699 if (ft2232_device_is_highspeed()) {
700 *jtag_speed = RTCK_SPEED;
703 LOG_DEBUG("RCLK not supported");
708 /* Take a look in the FT2232 manual,
709 * AN2232C-01 Command Processor for
710 * MPSSE and MCU Host Bus. Chapter 3.8
712 * We will calc here with a multiplier
713 * of 10 for better rounding later. */
715 /* Calc speed, (ft2232_max_tck / khz) - 1
716 * Use 65000 for better rounding */
717 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
719 /* Add 0.9 for rounding */
722 /* Calc real speed */
723 *jtag_speed = *jtag_speed / 10;
725 /* Check if speed is greater than 0 */
729 /* Check max value */
730 if (*jtag_speed > 0xFFFF)
731 *jtag_speed = 0xFFFF;
736 static void ft2232_end_state(tap_state_t state)
738 if (tap_is_state_stable(state))
739 tap_set_end_state(state);
741 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
746 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
748 int num_bytes = (scan_size + 7) / 8;
749 int bits_left = scan_size;
752 while (num_bytes-- > 1) {
753 buffer[cur_byte++] = buffer_read();
757 buffer[cur_byte] = 0x0;
759 /* There is one more partial byte left from the clock data in/out instructions */
761 buffer[cur_byte] = buffer_read() >> 1;
762 /* This shift depends on the length of the
763 *clock data to tms instruction, insterted
764 *at end of the scan, now fixed to a two
765 *step transition in ft2232_add_scan */
766 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
769 static void ft2232_debug_dump_buffer(void)
775 for (i = 0; i < ft2232_buffer_size; i++) {
776 line_p += snprintf(line_p,
777 sizeof(line) - (line_p - line),
781 LOG_DEBUG("%s", line);
787 LOG_DEBUG("%s", line);
790 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
792 struct jtag_command *cmd;
797 uint32_t bytes_written = 0;
798 uint32_t bytes_read = 0;
800 #ifdef _DEBUG_USB_IO_
801 struct timeval start, inter, inter2, end;
802 struct timeval d_inter, d_inter2, d_end;
805 #ifdef _DEBUG_USB_COMMS_
806 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
807 ft2232_debug_dump_buffer();
810 #ifdef _DEBUG_USB_IO_
811 gettimeofday(&start, NULL);
814 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
815 if (retval != ERROR_OK) {
816 LOG_ERROR("couldn't write MPSSE commands to FT2232");
820 #ifdef _DEBUG_USB_IO_
821 gettimeofday(&inter, NULL);
824 if (ft2232_expect_read) {
825 /* FIXME this "timeout" is never changed ... */
826 int timeout = LIBFTDI_READ_RETRY_COUNT;
827 ft2232_buffer_size = 0;
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&inter2, NULL);
833 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
834 if (retval != ERROR_OK) {
835 LOG_ERROR("couldn't read from FT2232");
839 #ifdef _DEBUG_USB_IO_
840 gettimeofday(&end, NULL);
842 timeval_subtract(&d_inter, &inter, &start);
843 timeval_subtract(&d_inter2, &inter2, &start);
844 timeval_subtract(&d_end, &end, &start);
846 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
847 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
848 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
849 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
852 ft2232_buffer_size = bytes_read;
854 if (ft2232_expect_read != ft2232_buffer_size) {
855 LOG_ERROR("ft2232_expect_read (%i) != "
856 "ft2232_buffer_size (%i) "
860 LIBFTDI_READ_RETRY_COUNT - timeout);
861 ft2232_debug_dump_buffer();
866 #ifdef _DEBUG_USB_COMMS_
867 LOG_DEBUG("read buffer (%i retries): %i bytes",
868 LIBFTDI_READ_RETRY_COUNT - timeout,
870 ft2232_debug_dump_buffer();
874 ft2232_expect_read = 0;
875 ft2232_read_pointer = 0;
877 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
878 * that wasn't handled by a caller-provided error handler
883 while (cmd != last) {
886 type = jtag_scan_type(cmd->cmd.scan);
887 if (type != SCAN_OUT) {
888 scan_size = jtag_scan_size(cmd->cmd.scan);
889 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
890 ft2232_read_scan(type, buffer, scan_size);
891 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
892 retval = ERROR_JTAG_QUEUE_FAILED;
904 ft2232_buffer_size = 0;
910 * Function ft2232_add_pathmove
911 * moves the TAP controller from the current state to a new state through the
912 * given path, where path is an array of tap_state_t's.
914 * @param path is an array of tap_stat_t which gives the states to traverse through
915 * ending with the last state at path[num_states-1]
916 * @param num_states is the count of state steps to move through
918 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
922 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
926 /* this loop verifies that the path is legal and logs each state in the path */
928 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
930 int num_states_batch = num_states > 7 ? 7 : num_states;
932 /* command "Clock Data to TMS/CS Pin (no Read)" */
935 /* number of states remaining */
936 buffer_write(num_states_batch - 1);
938 while (num_states_batch--) {
939 /* either TMS=0 or TMS=1 must work ... */
940 if (tap_state_transition(tap_get_state(), false) == path[state_count])
941 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
942 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
943 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
945 /* ... or else the caller goofed BADLY */
947 LOG_ERROR("BUG: %s -> %s isn't a valid "
948 "TAP state transition",
949 tap_state_name(tap_get_state()),
950 tap_state_name(path[state_count]));
954 tap_set_state(path[state_count]);
959 buffer_write(tms_byte);
961 tap_set_end_state(tap_get_state());
964 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
966 int num_bytes = (scan_size + 7) / 8;
967 int bits_left = scan_size;
972 if (tap_get_state() != TAP_DRSHIFT)
973 move_to_state(TAP_DRSHIFT);
975 if (tap_get_state() != TAP_IRSHIFT)
976 move_to_state(TAP_IRSHIFT);
979 /* add command for complete bytes */
980 while (num_bytes > 1) {
982 if (type == SCAN_IO) {
983 /* Clock Data Bytes In and Out LSB First */
985 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
986 } else if (type == SCAN_OUT) {
987 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
989 /* LOG_DEBUG("added TDI bytes (o)"); */
990 } else if (type == SCAN_IN) {
991 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
993 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
996 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
997 num_bytes -= thisrun_bytes;
999 buffer_write((uint8_t) (thisrun_bytes - 1));
1000 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1002 if (type != SCAN_IN) {
1003 /* add complete bytes */
1004 while (thisrun_bytes-- > 0) {
1005 buffer_write(buffer[cur_byte++]);
1008 } else /* (type == SCAN_IN) */
1009 bits_left -= 8 * (thisrun_bytes);
1012 /* the most signifcant bit is scanned during TAP movement */
1013 if (type != SCAN_IN)
1014 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1018 /* process remaining bits but the last one */
1019 if (bits_left > 1) {
1020 if (type == SCAN_IO) {
1021 /* Clock Data Bits In and Out LSB First */
1023 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1024 } else if (type == SCAN_OUT) {
1025 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1027 /* LOG_DEBUG("added TDI bits (o)"); */
1028 } else if (type == SCAN_IN) {
1029 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1031 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1034 buffer_write(bits_left - 2);
1035 if (type != SCAN_IN)
1036 buffer_write(buffer[cur_byte]);
1039 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1040 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1041 if (type == SCAN_IO) {
1042 /* Clock Data Bits In and Out LSB First */
1044 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1045 } else if (type == SCAN_OUT) {
1046 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1048 /* LOG_DEBUG("added TDI bits (o)"); */
1049 } else if (type == SCAN_IN) {
1050 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1052 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1055 buffer_write(last_bit);
1061 /* move from Shift-IR/DR to end state */
1062 if (type != SCAN_OUT) {
1063 /* We always go to the PAUSE state in two step at the end of an IN or IO
1065 * This must be coordinated with the bit shifts in ft2232_read_scan */
1068 /* Clock Data to TMS/CS Pin with Read */
1071 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1072 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1073 /* Clock Data to TMS/CS Pin (no Read) */
1077 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1078 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1081 if (tap_get_state() != tap_get_end_state())
1082 move_to_state(tap_get_end_state());
1085 static int ft2232_large_scan(struct scan_command *cmd,
1086 enum scan_type type,
1090 int num_bytes = (scan_size + 7) / 8;
1091 int bits_left = scan_size;
1094 uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1095 uint8_t *receive_pointer = receive_buffer;
1096 uint32_t bytes_written;
1097 uint32_t bytes_read;
1099 int thisrun_read = 0;
1102 LOG_ERROR("BUG: large IR scans are not supported");
1106 if (tap_get_state() != TAP_DRSHIFT)
1107 move_to_state(TAP_DRSHIFT);
1109 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1110 if (retval != ERROR_OK) {
1111 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1114 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1115 ft2232_buffer_size, (int)bytes_written);
1116 ft2232_buffer_size = 0;
1118 /* add command for complete bytes */
1119 while (num_bytes > 1) {
1122 if (type == SCAN_IO) {
1123 /* Clock Data Bytes In and Out LSB First */
1125 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1126 } else if (type == SCAN_OUT) {
1127 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1129 /* LOG_DEBUG("added TDI bytes (o)"); */
1130 } else if (type == SCAN_IN) {
1131 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1133 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1136 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1137 thisrun_read = thisrun_bytes;
1138 num_bytes -= thisrun_bytes;
1139 buffer_write((uint8_t) (thisrun_bytes - 1));
1140 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1142 if (type != SCAN_IN) {
1143 /* add complete bytes */
1144 while (thisrun_bytes-- > 0) {
1145 buffer_write(buffer[cur_byte]);
1149 } else /* (type == SCAN_IN) */
1150 bits_left -= 8 * (thisrun_bytes);
1152 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1153 if (retval != ERROR_OK) {
1154 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1157 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1159 (int)bytes_written);
1160 ft2232_buffer_size = 0;
1162 if (type != SCAN_OUT) {
1163 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1164 if (retval != ERROR_OK) {
1165 LOG_ERROR("couldn't read from FT2232");
1168 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1171 receive_pointer += bytes_read;
1177 /* the most signifcant bit is scanned during TAP movement */
1178 if (type != SCAN_IN)
1179 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1183 /* process remaining bits but the last one */
1184 if (bits_left > 1) {
1185 if (type == SCAN_IO) {
1186 /* Clock Data Bits In and Out LSB First */
1188 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1189 } else if (type == SCAN_OUT) {
1190 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1192 /* LOG_DEBUG("added TDI bits (o)"); */
1193 } else if (type == SCAN_IN) {
1194 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1196 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1198 buffer_write(bits_left - 2);
1199 if (type != SCAN_IN)
1200 buffer_write(buffer[cur_byte]);
1202 if (type != SCAN_OUT)
1206 if (tap_get_end_state() == TAP_DRSHIFT) {
1207 if (type == SCAN_IO) {
1208 /* Clock Data Bits In and Out LSB First */
1210 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1211 } else if (type == SCAN_OUT) {
1212 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1214 /* LOG_DEBUG("added TDI bits (o)"); */
1215 } else if (type == SCAN_IN) {
1216 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1218 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1221 buffer_write(last_bit);
1223 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1224 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1227 /* move from Shift-IR/DR to end state */
1228 if (type != SCAN_OUT) {
1229 /* Clock Data to TMS/CS Pin with Read */
1231 /* LOG_DEBUG("added TMS scan (read)"); */
1233 /* Clock Data to TMS/CS Pin (no Read) */
1235 /* LOG_DEBUG("added TMS scan (no read)"); */
1238 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1239 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1242 if (type != SCAN_OUT)
1245 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1246 if (retval != ERROR_OK) {
1247 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1250 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1252 (int)bytes_written);
1253 ft2232_buffer_size = 0;
1255 if (type != SCAN_OUT) {
1256 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1257 if (retval != ERROR_OK) {
1258 LOG_ERROR("couldn't read from FT2232");
1261 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1269 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1271 int predicted_size = 3;
1272 int num_bytes = (scan_size - 1) / 8;
1274 if (tap_get_state() != TAP_DRSHIFT)
1275 predicted_size += get_tms_buffer_requirements(
1276 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1278 if (type == SCAN_IN) { /* only from device to host */
1279 /* complete bytes */
1280 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1282 /* remaining bits - 1 (up to 7) */
1283 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1284 } else {/* host to device, or bidirectional
1286 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1288 /* remaining bits -1 (up to 7) */
1289 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1292 return predicted_size;
1295 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1297 int predicted_size = 0;
1299 if (type != SCAN_OUT) {
1300 /* complete bytes */
1302 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1304 /* remaining bits - 1 */
1305 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1307 /* last bit (from TMS scan) */
1308 predicted_size += 1;
1311 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1313 return predicted_size;
1316 /* semi-generic FT2232/FT4232 reset code */
1317 static void ftx23_reset(int trst, int srst)
1319 enum reset_types jtag_reset_config = jtag_get_reset_config();
1321 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1322 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1324 low_output &= ~nTRST; /* switch output low */
1325 } else if (trst == 0) {
1326 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1327 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1328 *and external pullup) */
1330 low_output |= nTRST; /* switch output high */
1334 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1335 low_output &= ~nSRST; /* switch output low */
1337 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1338 } else if (srst == 0) {
1339 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1340 low_output |= nSRST; /* switch output high */
1342 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1345 /* command "set data bits low byte" */
1347 buffer_write(low_output);
1348 buffer_write(low_direction);
1351 static void jtagkey_reset(int trst, int srst)
1353 enum reset_types jtag_reset_config = jtag_get_reset_config();
1355 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1356 high_output &= ~nTRSTnOE;
1358 high_output &= ~nTRST;
1359 } else if (trst == 0) {
1360 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1361 high_output |= nTRSTnOE;
1363 high_output |= nTRST;
1367 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1368 high_output &= ~nSRST;
1370 high_output &= ~nSRSTnOE;
1371 } else if (srst == 0) {
1372 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1373 high_output |= nSRST;
1375 high_output |= nSRSTnOE;
1378 /* command "set data bits high byte" */
1380 buffer_write(high_output);
1381 buffer_write(high_direction);
1382 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1389 static void olimex_jtag_reset(int trst, int srst)
1391 enum reset_types jtag_reset_config = jtag_get_reset_config();
1393 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1394 high_output &= ~nTRSTnOE;
1396 high_output &= ~nTRST;
1397 } else if (trst == 0) {
1398 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1399 high_output |= nTRSTnOE;
1401 high_output |= nTRST;
1405 high_output |= nSRST;
1407 high_output &= ~nSRST;
1409 /* command "set data bits high byte" */
1411 buffer_write(high_output);
1412 buffer_write(high_direction);
1413 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1420 static void axm0432_jtag_reset(int trst, int srst)
1423 tap_set_state(TAP_RESET);
1424 high_output &= ~nTRST;
1425 } else if (trst == 0)
1426 high_output |= nTRST;
1429 high_output &= ~nSRST;
1431 high_output |= nSRST;
1433 /* command "set data bits low byte" */
1435 buffer_write(high_output);
1436 buffer_write(high_direction);
1437 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1444 static void flyswatter_reset(int trst, int srst)
1447 low_output &= ~nTRST;
1449 low_output |= nTRST;
1452 low_output |= nSRST;
1454 low_output &= ~nSRST;
1456 /* command "set data bits low byte" */
1458 buffer_write(low_output);
1459 buffer_write(low_direction);
1460 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1467 static void flyswatter1_reset(int trst, int srst)
1469 flyswatter_reset(trst, srst);
1472 static void flyswatter2_reset(int trst, int srst)
1474 flyswatter_reset(trst, !srst);
1477 static void minimodule_reset(int trst, int srst)
1480 low_output &= ~nSRST;
1482 low_output |= nSRST;
1484 /* command "set data bits low byte" */
1486 buffer_write(low_output);
1487 buffer_write(low_direction);
1488 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1495 static void turtle_reset(int trst, int srst)
1500 low_output |= nSRST;
1502 low_output &= ~nSRST;
1504 /* command "set data bits low byte" */
1506 buffer_write(low_output);
1507 buffer_write(low_direction);
1508 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1514 static void comstick_reset(int trst, int srst)
1517 high_output &= ~nTRST;
1519 high_output |= nTRST;
1522 high_output &= ~nSRST;
1524 high_output |= nSRST;
1526 /* command "set data bits high byte" */
1528 buffer_write(high_output);
1529 buffer_write(high_direction);
1530 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1537 static void stm32stick_reset(int trst, int srst)
1540 high_output &= ~nTRST;
1542 high_output |= nTRST;
1545 low_output &= ~nSRST;
1547 low_output |= nSRST;
1549 /* command "set data bits low byte" */
1551 buffer_write(low_output);
1552 buffer_write(low_direction);
1554 /* command "set data bits high byte" */
1556 buffer_write(high_output);
1557 buffer_write(high_direction);
1558 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1565 static void sheevaplug_reset(int trst, int srst)
1568 high_output &= ~nTRST;
1570 high_output |= nTRST;
1573 high_output &= ~nSRSTnOE;
1575 high_output |= nSRSTnOE;
1577 /* command "set data bits high byte" */
1579 buffer_write(high_output);
1580 buffer_write(high_direction);
1581 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1588 static void redbee_reset(int trst, int srst)
1591 tap_set_state(TAP_RESET);
1592 high_output &= ~nTRST;
1593 } else if (trst == 0)
1594 high_output |= nTRST;
1597 high_output &= ~nSRST;
1599 high_output |= nSRST;
1601 /* command "set data bits low byte" */
1603 buffer_write(high_output);
1604 buffer_write(high_direction);
1605 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1606 "high_direction: 0x%2.2x", trst, srst, high_output,
1610 static void xds100v2_reset(int trst, int srst)
1613 tap_set_state(TAP_RESET);
1614 high_output &= ~nTRST;
1615 } else if (trst == 0)
1616 high_output |= nTRST;
1619 high_output |= nSRST;
1621 high_output &= ~nSRST;
1623 /* command "set data bits low byte" */
1625 buffer_write(high_output);
1626 buffer_write(high_direction);
1627 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1628 "high_direction: 0x%2.2x", trst, srst, high_output,
1632 static int ft2232_execute_runtest(struct jtag_command *cmd)
1636 int predicted_size = 0;
1639 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1640 cmd->cmd.runtest->num_cycles,
1641 tap_state_name(cmd->cmd.runtest->end_state));
1643 /* only send the maximum buffer size that FT2232C can handle */
1645 if (tap_get_state() != TAP_IDLE)
1646 predicted_size += 3;
1647 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1648 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1649 predicted_size += 3;
1650 if (tap_get_end_state() != TAP_IDLE)
1651 predicted_size += 3;
1652 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1653 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1654 retval = ERROR_JTAG_QUEUE_FAILED;
1658 if (tap_get_state() != TAP_IDLE) {
1659 move_to_state(TAP_IDLE);
1662 i = cmd->cmd.runtest->num_cycles;
1664 /* there are no state transitions in this code, so omit state tracking */
1666 /* command "Clock Data to TMS/CS Pin (no Read)" */
1670 buffer_write((i > 7) ? 6 : (i - 1));
1675 i -= (i > 7) ? 7 : i;
1676 /* LOG_DEBUG("added TMS scan (no read)"); */
1679 ft2232_end_state(cmd->cmd.runtest->end_state);
1681 if (tap_get_state() != tap_get_end_state())
1682 move_to_state(tap_get_end_state());
1685 DEBUG_JTAG_IO("runtest: %i, end in %s",
1686 cmd->cmd.runtest->num_cycles,
1687 tap_state_name(tap_get_end_state()));
1691 static int ft2232_execute_statemove(struct jtag_command *cmd)
1693 int predicted_size = 0;
1694 int retval = ERROR_OK;
1696 DEBUG_JTAG_IO("statemove end in %s",
1697 tap_state_name(cmd->cmd.statemove->end_state));
1699 /* only send the maximum buffer size that FT2232C can handle */
1701 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1702 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1703 retval = ERROR_JTAG_QUEUE_FAILED;
1707 ft2232_end_state(cmd->cmd.statemove->end_state);
1709 /* For TAP_RESET, ignore the current recorded state. It's often
1710 * wrong at server startup, and this transation is critical whenever
1713 if (tap_get_end_state() == TAP_RESET) {
1714 clock_tms(0x4b, 0xff, 5, 0);
1717 /* shortest-path move to desired end state */
1718 } else if (tap_get_state() != tap_get_end_state()) {
1719 move_to_state(tap_get_end_state());
1727 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1728 * (or SWD) state machine.
1730 static int ft2232_execute_tms(struct jtag_command *cmd)
1732 int retval = ERROR_OK;
1733 unsigned num_bits = cmd->cmd.tms->num_bits;
1734 const uint8_t *bits = cmd->cmd.tms->bits;
1737 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1739 /* only send the maximum buffer size that FT2232C can handle */
1740 count = 3 * DIV_ROUND_UP(num_bits, 4);
1741 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1742 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1743 retval = ERROR_JTAG_QUEUE_FAILED;
1749 /* Shift out in batches of at most 6 bits; there's a report of an
1750 * FT2232 bug in this area, where shifting exactly 7 bits can make
1751 * problems with TMS signaling for the last clock cycle:
1753 * http://developer.intra2net.com/mailarchive/html/
1754 * libftdi/2009/msg00292.html
1756 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1758 * Note that pathmoves in JTAG are not often seven bits, so that
1759 * isn't a particularly likely situation outside of "special"
1760 * signaling such as switching between JTAG and SWD modes.
1763 if (num_bits <= 6) {
1765 buffer_write(num_bits - 1);
1766 buffer_write(*bits & 0x3f);
1770 /* Yes, this is lazy ... we COULD shift out more data
1771 * bits per operation, but doing it in nybbles is easy
1775 buffer_write(*bits & 0xf);
1778 count = (num_bits > 4) ? 4 : num_bits;
1781 buffer_write(count - 1);
1782 buffer_write((*bits >> 4) & 0xf);
1792 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1794 int predicted_size = 0;
1795 int retval = ERROR_OK;
1797 tap_state_t *path = cmd->cmd.pathmove->path;
1798 int num_states = cmd->cmd.pathmove->num_states;
1800 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1801 tap_state_name(tap_get_state()),
1802 tap_state_name(path[num_states-1]));
1804 /* only send the maximum buffer size that FT2232C can handle */
1805 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1806 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1807 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1808 retval = ERROR_JTAG_QUEUE_FAILED;
1814 ft2232_add_pathmove(path, num_states);
1820 static int ft2232_execute_scan(struct jtag_command *cmd)
1823 int scan_size; /* size of IR or DR scan */
1824 int predicted_size = 0;
1825 int retval = ERROR_OK;
1827 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1829 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1831 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1833 predicted_size = ft2232_predict_scan_out(scan_size, type);
1834 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1835 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1836 /* unsent commands before this */
1837 if (first_unsent != cmd)
1838 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1839 retval = ERROR_JTAG_QUEUE_FAILED;
1841 /* current command */
1842 ft2232_end_state(cmd->cmd.scan->end_state);
1843 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1845 first_unsent = cmd->next;
1849 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1851 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1854 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1855 retval = ERROR_JTAG_QUEUE_FAILED;
1859 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1860 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1861 ft2232_end_state(cmd->cmd.scan->end_state);
1862 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1866 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1867 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1868 tap_state_name(tap_get_end_state()));
1873 static int ft2232_execute_reset(struct jtag_command *cmd)
1876 int predicted_size = 0;
1879 DEBUG_JTAG_IO("reset trst: %i srst %i",
1880 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1882 /* only send the maximum buffer size that FT2232C can handle */
1884 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1885 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1886 retval = ERROR_JTAG_QUEUE_FAILED;
1891 if ((cmd->cmd.reset->trst == 1) ||
1892 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1893 tap_set_state(TAP_RESET);
1895 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1898 DEBUG_JTAG_IO("trst: %i, srst: %i",
1899 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1903 static int ft2232_execute_sleep(struct jtag_command *cmd)
1908 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1910 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1911 retval = ERROR_JTAG_QUEUE_FAILED;
1912 first_unsent = cmd->next;
1913 jtag_sleep(cmd->cmd.sleep->us);
1914 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1916 tap_state_name(tap_get_state()));
1920 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1925 /* this is only allowed while in a stable state. A check for a stable
1926 * state was done in jtag_add_clocks()
1928 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1929 retval = ERROR_JTAG_QUEUE_FAILED;
1930 DEBUG_JTAG_IO("clocks %i while in %s",
1931 cmd->cmd.stableclocks->num_cycles,
1932 tap_state_name(tap_get_state()));
1936 static int ft2232_execute_command(struct jtag_command *cmd)
1940 switch (cmd->type) {
1942 retval = ft2232_execute_reset(cmd);
1945 retval = ft2232_execute_runtest(cmd);
1947 case JTAG_TLR_RESET:
1948 retval = ft2232_execute_statemove(cmd);
1951 retval = ft2232_execute_pathmove(cmd);
1954 retval = ft2232_execute_scan(cmd);
1957 retval = ft2232_execute_sleep(cmd);
1959 case JTAG_STABLECLOCKS:
1960 retval = ft2232_execute_stableclocks(cmd);
1963 retval = ft2232_execute_tms(cmd);
1966 LOG_ERROR("BUG: unknown JTAG command type encountered");
1967 retval = ERROR_JTAG_QUEUE_FAILED;
1973 static int ft2232_execute_queue(void)
1975 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1978 first_unsent = cmd; /* next command that has to be sent */
1981 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1982 * that wasn't handled by a caller-provided error handler
1986 ft2232_buffer_size = 0;
1987 ft2232_expect_read = 0;
1989 /* blink, if the current layout has that feature */
1994 /* fill the write buffer with the desired command */
1995 if (ft2232_execute_command(cmd) != ERROR_OK)
1996 retval = ERROR_JTAG_QUEUE_FAILED;
1997 /* Start reading input before FT2232 TX buffer fills up.
1998 * Sometimes this happens because we don't know the
1999 * length of the last command before we execute it. So
2000 * we simple inform the user.
2004 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2005 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2006 LOG_DEBUG("read buffer size looks too high %d/%d",
2008 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2009 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2010 retval = ERROR_JTAG_QUEUE_FAILED;
2015 if (require_send > 0)
2016 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2017 retval = ERROR_JTAG_QUEUE_FAILED;
2022 #if BUILD_FT2232_FTD2XX == 1
2023 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2027 char SerialNumber[16];
2028 char Description[64];
2029 DWORD openex_flags = 0;
2030 char *openex_string = NULL;
2031 uint8_t latency_timer;
2033 if (layout == NULL) {
2034 LOG_WARNING("No ft2232 layout specified'");
2035 return ERROR_JTAG_INIT_FAILED;
2038 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2039 layout->name, vid, pid);
2042 /* Add non-standard Vid/Pid to the linux driver */
2043 status = FT_SetVIDPID(vid, pid);
2044 if (status != FT_OK)
2045 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2049 if (ft2232_device_desc && ft2232_serial) {
2051 "can't open by device description and serial number, giving precedence to serial");
2052 ft2232_device_desc = NULL;
2055 if (ft2232_device_desc) {
2056 openex_string = ft2232_device_desc;
2057 openex_flags = FT_OPEN_BY_DESCRIPTION;
2058 } else if (ft2232_serial) {
2059 openex_string = ft2232_serial;
2060 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2062 LOG_ERROR("neither device description nor serial number specified");
2064 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2066 return ERROR_JTAG_INIT_FAILED;
2069 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2070 if (status != FT_OK) {
2071 /* under Win32, the FTD2XX driver appends an "A" to the end
2072 * of the description, if we tried by the desc, then
2073 * try by the alternate "A" description. */
2074 if (openex_string == ft2232_device_desc) {
2075 /* Try the alternate method. */
2076 openex_string = ft2232_device_desc_A;
2077 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2078 if (status == FT_OK) {
2079 /* yea, the "alternate" method worked! */
2081 /* drat, give the user a meaningfull message.
2082 * telling the use we tried *BOTH* methods. */
2083 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2085 ft2232_device_desc_A);
2090 if (status != FT_OK) {
2094 LOG_WARNING("unable to open ftdi device (trying more): %s",
2095 ftd2xx_status_string(status));
2097 return ERROR_JTAG_INIT_FAILED;
2099 LOG_ERROR("unable to open ftdi device: %s",
2100 ftd2xx_status_string(status));
2101 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2102 if (status == FT_OK) {
2103 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2106 for (i = 0; i < num_devices; i++)
2107 desc_array[i] = malloc(64);
2109 desc_array[num_devices] = NULL;
2111 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2113 if (status == FT_OK) {
2114 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2115 for (i = 0; i < num_devices; i++)
2116 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2119 for (i = 0; i < num_devices; i++)
2120 free(desc_array[i]);
2124 LOG_ERROR("ListDevices: NONE");
2125 return ERROR_JTAG_INIT_FAILED;
2128 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2129 if (status != FT_OK) {
2130 LOG_ERROR("unable to set latency timer: %s",
2131 ftd2xx_status_string(status));
2132 return ERROR_JTAG_INIT_FAILED;
2135 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2136 if (status != FT_OK) {
2137 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2138 * so ignore errors if using this driver version */
2141 status = FT_GetDriverVersion(ftdih, &dw_version);
2142 LOG_ERROR("unable to get latency timer: %s",
2143 ftd2xx_status_string(status));
2145 if ((status == FT_OK) && (dw_version == 0x10004)) {
2146 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2147 "with FT_GetLatencyTimer, upgrade to a newer version");
2149 return ERROR_JTAG_INIT_FAILED;
2151 LOG_DEBUG("current latency timer: %i", latency_timer);
2153 status = FT_SetTimeouts(ftdih, 5000, 5000);
2154 if (status != FT_OK) {
2155 LOG_ERROR("unable to set timeouts: %s",
2156 ftd2xx_status_string(status));
2157 return ERROR_JTAG_INIT_FAILED;
2160 status = FT_SetBitMode(ftdih, 0x0b, 2);
2161 if (status != FT_OK) {
2162 LOG_ERROR("unable to enable bit i/o mode: %s",
2163 ftd2xx_status_string(status));
2164 return ERROR_JTAG_INIT_FAILED;
2167 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2168 SerialNumber, Description, NULL);
2169 if (status != FT_OK) {
2170 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2171 ftd2xx_status_string(status));
2172 return ERROR_JTAG_INIT_FAILED;
2174 static const char *type_str[] = {
2175 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"
2177 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2178 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2179 ? ftdi_device : FT_DEVICE_UNKNOWN;
2180 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2181 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2182 LOG_INFO("SerialNumber: %s", SerialNumber);
2183 LOG_INFO("Description: %s", Description);
2189 static int ft2232_purge_ftd2xx(void)
2193 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2194 if (status != FT_OK) {
2195 LOG_ERROR("error purging ftd2xx device: %s",
2196 ftd2xx_status_string(status));
2197 return ERROR_JTAG_INIT_FAILED;
2203 #endif /* BUILD_FT2232_FTD2XX == 1 */
2205 #if BUILD_FT2232_LIBFTDI == 1
2206 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2208 uint8_t latency_timer;
2210 if (layout == NULL) {
2211 LOG_WARNING("No ft2232 layout specified'");
2212 return ERROR_JTAG_INIT_FAILED;
2215 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2216 layout->name, vid, pid);
2218 if (ftdi_init(&ftdic) < 0)
2219 return ERROR_JTAG_INIT_FAILED;
2221 /* default to INTERFACE_A */
2222 if (channel == INTERFACE_ANY)
2223 channel = INTERFACE_A;
2224 if (ftdi_set_interface(&ftdic, channel) < 0) {
2225 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2226 return ERROR_JTAG_INIT_FAILED;
2229 /* context, vendor id, product id */
2230 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2232 LOG_WARNING("unable to open ftdi device (trying more): %s",
2235 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2237 return ERROR_JTAG_INIT_FAILED;
2240 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2241 if (ftdi_usb_reset(&ftdic) < 0) {
2242 LOG_ERROR("unable to reset ftdi device");
2243 return ERROR_JTAG_INIT_FAILED;
2246 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2247 LOG_ERROR("unable to set latency timer");
2248 return ERROR_JTAG_INIT_FAILED;
2251 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2252 LOG_ERROR("unable to get latency timer");
2253 return ERROR_JTAG_INIT_FAILED;
2255 LOG_DEBUG("current latency timer: %i", latency_timer);
2257 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2259 ftdi_device = ftdic.type;
2260 static const char *type_str[] = {
2261 "AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"
2263 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2264 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2265 ? ftdi_device : no_of_known_types;
2266 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2270 static int ft2232_purge_libftdi(void)
2272 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2273 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2274 return ERROR_JTAG_INIT_FAILED;
2280 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2282 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2285 uint32_t bytes_written;
2287 buf[0] = 0x80; /* command "set data bits low byte" */
2288 buf[1] = value; /* value */
2289 buf[2] = direction; /* direction */
2291 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2293 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2294 LOG_ERROR("couldn't initialize data bits low byte");
2295 return ERROR_JTAG_INIT_FAILED;
2301 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2304 uint32_t bytes_written;
2306 buf[0] = 0x82; /* command "set data bits high byte" */
2307 buf[1] = value; /* value */
2308 buf[2] = direction; /* direction */
2310 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2312 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2313 LOG_ERROR("couldn't initialize data bits high byte");
2314 return ERROR_JTAG_INIT_FAILED;
2320 static int ft2232_init(void)
2324 uint32_t bytes_written;
2326 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2327 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2329 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2330 if (layout == NULL) {
2331 LOG_WARNING("No ft2232 layout specified'");
2332 return ERROR_JTAG_INIT_FAILED;
2335 for (int i = 0; 1; i++) {
2337 * "more indicates that there are more IDs to try, so we should
2338 * not print an error for an ID mismatch (but for anything
2341 * try_more indicates that the error code returned indicates an
2342 * ID mismatch (and nothing else) and that we should proceeed
2343 * with the next ID pair.
2345 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2348 #if BUILD_FT2232_FTD2XX == 1
2349 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2351 #elif BUILD_FT2232_LIBFTDI == 1
2352 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2353 more, &try_more, layout->channel);
2357 if (!more || !try_more)
2361 ft2232_buffer_size = 0;
2362 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2364 if (layout->init() != ERROR_OK)
2365 return ERROR_JTAG_INIT_FAILED;
2367 if (ft2232_device_is_highspeed()) {
2368 #ifndef BUILD_FT2232_HIGHSPEED
2369 #if BUILD_FT2232_FTD2XX == 1
2371 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2372 #elif BUILD_FT2232_LIBFTDI == 1
2374 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2377 /* make sure the legacy mode is disabled */
2378 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2379 return ERROR_JTAG_INIT_FAILED;
2382 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2383 retval = ft2232_write(buf, 1, &bytes_written);
2384 if (retval != ERROR_OK) {
2385 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2386 return ERROR_JTAG_INIT_FAILED;
2389 #if BUILD_FT2232_FTD2XX == 1
2390 return ft2232_purge_ftd2xx();
2391 #elif BUILD_FT2232_LIBFTDI == 1
2392 return ft2232_purge_libftdi();
2398 /** Updates defaults for DBUS signals: the four JTAG signals
2399 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2401 static inline void ftx232_dbus_init(void)
2404 low_direction = 0x0b;
2407 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2408 * the four GPIOL signals. Initialization covers value and direction,
2409 * as customized for each layout.
2411 static int ftx232_dbus_write(void)
2413 enum reset_types jtag_reset_config = jtag_get_reset_config();
2414 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2415 low_direction &= ~nTRSTnOE; /* nTRST input */
2416 low_output &= ~nTRST; /* nTRST = 0 */
2418 low_direction |= nTRSTnOE; /* nTRST output */
2419 low_output |= nTRST; /* nTRST = 1 */
2422 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2423 low_direction |= nSRSTnOE; /* nSRST output */
2424 low_output |= nSRST; /* nSRST = 1 */
2426 low_direction &= ~nSRSTnOE; /* nSRST input */
2427 low_output &= ~nSRST; /* nSRST = 0 */
2430 /* initialize low byte for jtag */
2431 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2432 LOG_ERROR("couldn't initialize FT2232 DBUS");
2433 return ERROR_JTAG_INIT_FAILED;
2439 static int usbjtag_init(void)
2442 * NOTE: This is now _specific_ to the "usbjtag" layout.
2443 * Don't try cram any more layouts into this.
2452 return ftx232_dbus_write();
2455 static int lm3s811_jtag_init(void)
2459 /* There are multiple revisions of LM3S811 eval boards:
2460 * - Rev B (and older?) boards have no SWO trace support.
2461 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2462 * they should use the "luminary_icdi" layout instead.
2469 low_direction = 0x8b;
2471 return ftx232_dbus_write();
2474 static int icdi_jtag_init(void)
2478 /* Most Luminary eval boards support SWO trace output,
2479 * and should use this "luminary_icdi" layout.
2481 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2482 * to switch between JTAG and SWD, or switch the ft2232 UART
2483 * on the second MPSSE channel/interface (BDBUS)
2484 * between (i) the stellaris UART (on Luminary boards)
2485 * or (ii) SWO trace data (generic).
2487 * We come up in JTAG mode and may switch to SWD later (with
2488 * SWO/trace option if SWD is active).
2495 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2496 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2497 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2500 /* GPIOs on second channel/interface (UART) ... */
2501 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2502 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2503 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2508 nSRSTnOE = ICDI_SRST;
2510 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2511 low_output |= ICDI_JTAG_EN;
2512 low_output &= ~ICDI_DBG_ENn;
2514 return ftx232_dbus_write();
2517 static int signalyzer_init(void)
2525 return ftx232_dbus_write();
2528 static int axm0432_jtag_init(void)
2531 low_direction = 0x2b;
2533 /* initialize low byte for jtag */
2534 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2535 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2536 return ERROR_JTAG_INIT_FAILED;
2539 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2541 nTRSTnOE = 0x0; /* No output enable for TRST*/
2543 nSRSTnOE = 0x0; /* No output enable for SRST*/
2545 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2550 high_direction = 0x0c;
2552 enum reset_types jtag_reset_config = jtag_get_reset_config();
2553 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2554 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2556 high_output |= nTRST;
2558 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2559 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2561 high_output |= nSRST;
2563 /* initialize high byte for jtag */
2564 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2565 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2566 return ERROR_JTAG_INIT_FAILED;
2572 static int redbee_init(void)
2575 low_direction = 0x2b;
2577 /* initialize low byte for jtag */
2578 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2579 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2580 return ERROR_JTAG_INIT_FAILED;
2584 nTRSTnOE = 0x0; /* No output enable for TRST*/
2586 nSRSTnOE = 0x0; /* No output enable for SRST*/
2589 high_direction = 0x0c;
2591 enum reset_types jtag_reset_config = jtag_get_reset_config();
2592 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2593 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2595 high_output |= nTRST;
2597 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2598 LOG_ERROR("can't set nSRST to push-pull on redbee");
2600 high_output |= nSRST;
2602 /* initialize high byte for jtag */
2603 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2604 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2605 return ERROR_JTAG_INIT_FAILED;
2611 static int jtagkey_init(void)
2614 low_direction = 0x1b;
2616 /* initialize low byte for jtag */
2617 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2618 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2619 return ERROR_JTAG_INIT_FAILED;
2622 if (strcmp(layout->name, "jtagkey") == 0) {
2627 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2628 || (strcmp(layout->name, "oocdlink") == 0)) {
2634 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2639 high_direction = 0x0f;
2641 enum reset_types jtag_reset_config = jtag_get_reset_config();
2642 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2643 high_output |= nTRSTnOE;
2644 high_output &= ~nTRST;
2646 high_output &= ~nTRSTnOE;
2647 high_output |= nTRST;
2650 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2651 high_output &= ~nSRSTnOE;
2652 high_output |= nSRST;
2654 high_output |= nSRSTnOE;
2655 high_output &= ~nSRST;
2658 /* initialize high byte for jtag */
2659 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2660 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2661 return ERROR_JTAG_INIT_FAILED;
2667 static int olimex_jtag_init(void)
2670 low_direction = 0x1b;
2672 /* initialize low byte for jtag */
2673 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2674 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2675 return ERROR_JTAG_INIT_FAILED;
2681 nSRSTnOE = 0x00;/* no output enable for nSRST */
2684 high_direction = 0x0f;
2686 enum reset_types jtag_reset_config = jtag_get_reset_config();
2687 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2688 high_output |= nTRSTnOE;
2689 high_output &= ~nTRST;
2691 high_output &= ~nTRSTnOE;
2692 high_output |= nTRST;
2695 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2696 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2698 high_output &= ~nSRST;
2700 /* turn red LED on */
2701 high_output |= 0x08;
2703 /* initialize high byte for jtag */
2704 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2705 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2706 return ERROR_JTAG_INIT_FAILED;
2712 static int flyswatter_init(int rev)
2715 low_direction = 0x7b;
2717 if ((rev < 0) || (rev > 3)) {
2718 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2719 return ERROR_JTAG_INIT_FAILED;
2723 low_direction |= 1 << 7;
2725 /* initialize low byte for jtag */
2726 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2727 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2728 return ERROR_JTAG_INIT_FAILED;
2732 nTRSTnOE = 0x0; /* not output enable for nTRST */
2734 nSRSTnOE = 0x00; /* no output enable for nSRST */
2739 high_direction = 0x0c;
2741 high_direction = 0x01;
2743 /* turn red LED3 on, LED2 off */
2744 high_output |= 0x08;
2746 /* initialize high byte for jtag */
2747 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2748 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2749 return ERROR_JTAG_INIT_FAILED;
2755 static int flyswatter1_init(void)
2757 return flyswatter_init(1);
2760 static int flyswatter2_init(void)
2762 return flyswatter_init(2);
2765 static int minimodule_init(void)
2767 low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2769 low_direction = 0xfb; /* 0xfb; */
2771 /* initialize low byte for jtag */
2772 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2773 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2774 return ERROR_JTAG_INIT_FAILED;
2781 high_direction = 0x05;
2783 /* turn red LED3 on, LED2 off */
2784 /* high_output |= 0x08; */
2786 /* initialize high byte for jtag */
2787 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2788 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2789 return ERROR_JTAG_INIT_FAILED;
2795 static int turtle_init(void)
2798 low_direction = 0x5b;
2800 /* initialize low byte for jtag */
2801 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2802 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2803 return ERROR_JTAG_INIT_FAILED;
2809 high_direction = 0x0C;
2811 /* initialize high byte for jtag */
2812 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2813 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2814 return ERROR_JTAG_INIT_FAILED;
2820 static int comstick_init(void)
2823 low_direction = 0x0b;
2825 /* initialize low byte for jtag */
2826 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2827 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2828 return ERROR_JTAG_INIT_FAILED;
2832 nTRSTnOE = 0x00; /* no output enable for nTRST */
2834 nSRSTnOE = 0x00; /* no output enable for nSRST */
2837 high_direction = 0x03;
2839 /* initialize high byte for jtag */
2840 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2841 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2842 return ERROR_JTAG_INIT_FAILED;
2848 static int stm32stick_init(void)
2851 low_direction = 0x8b;
2853 /* initialize low byte for jtag */
2854 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2855 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2856 return ERROR_JTAG_INIT_FAILED;
2860 nTRSTnOE = 0x00; /* no output enable for nTRST */
2862 nSRSTnOE = 0x00; /* no output enable for nSRST */
2865 high_direction = 0x03;
2867 /* initialize high byte for jtag */
2868 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2869 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2870 return ERROR_JTAG_INIT_FAILED;
2876 static int sheevaplug_init(void)
2879 low_direction = 0x1b;
2881 /* initialize low byte for jtag */
2882 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2883 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2884 return ERROR_JTAG_INIT_FAILED;
2893 high_direction = 0x0f;
2895 /* nTRST is always push-pull */
2896 high_output &= ~nTRSTnOE;
2897 high_output |= nTRST;
2899 /* nSRST is always open-drain */
2900 high_output |= nSRSTnOE;
2901 high_output &= ~nSRST;
2903 /* initialize high byte for jtag */
2904 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2905 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2906 return ERROR_JTAG_INIT_FAILED;
2912 static int cortino_jtag_init(void)
2915 low_direction = 0x1b;
2917 /* initialize low byte for jtag */
2918 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2919 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2920 return ERROR_JTAG_INIT_FAILED;
2924 nTRSTnOE = 0x00; /* no output enable for nTRST */
2926 nSRSTnOE = 0x00; /* no output enable for nSRST */
2929 high_direction = 0x03;
2931 /* initialize high byte for jtag */
2932 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2933 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2934 return ERROR_JTAG_INIT_FAILED;
2940 static int lisa_l_init(void)
2950 high_direction = 0x18;
2952 /* initialize high byte for jtag */
2953 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2954 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2955 return ERROR_JTAG_INIT_FAILED;
2958 return ftx232_dbus_write();
2961 static int flossjtag_init(void)
2971 high_direction = 0x18;
2973 /* initialize high byte for jtag */
2974 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2975 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2976 return ERROR_JTAG_INIT_FAILED;
2979 return ftx232_dbus_write();
2983 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
2984 * the door for a number of different configurations
2986 * Known Implementations:
2987 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
2989 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
2990 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
2991 * * ACBUS3 to transition 0->1 (OE rising edge)
2992 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
2993 * * ADBUS5/GPIOL1 = EMU_EN = 1
2994 * * ADBUS6/GPIOL2 = EMU0 = 0
2995 * * ACBUS4/SPARE0 = EMU1 = 0
2996 * * CPLD logic: Disable loopback
2997 * * ACBUS6/SPARE2 = LOOPBACK = 0
2999 #define XDS100_nEMU_EN (1<<5)
3000 #define XDS100_nEMU0 (1<<6)
3002 #define XDS100_PWR_RST (1<<3)
3003 #define XDS100_nEMU1 (1<<4)
3004 #define XDS100_LOOPBACK (1<<6)
3005 static int xds100v2_init(void)
3007 /* These are in the lower byte */
3011 /* These aren't actually used on 14 pin connectors
3012 * These are in the upper byte */
3016 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3017 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3019 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3020 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3021 return ERROR_JTAG_INIT_FAILED;
3025 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3027 /* initialize high byte for jtag */
3028 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3029 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3030 return ERROR_JTAG_INIT_FAILED;
3033 high_output |= XDS100_PWR_RST;
3035 /* initialize high byte for jtag */
3036 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3037 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3038 return ERROR_JTAG_INIT_FAILED;
3044 static void olimex_jtag_blink(void)
3046 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3047 * ACBUS3 is bit 3 of the GPIOH port
3049 high_output ^= 0x08;
3052 buffer_write(high_output);
3053 buffer_write(high_direction);
3056 static void flyswatter_jtag_blink(unsigned char led)
3059 buffer_write(high_output ^ led);
3060 buffer_write(high_direction);
3063 static void flyswatter1_jtag_blink(void)
3066 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3068 flyswatter_jtag_blink(0xc);
3071 static void flyswatter2_jtag_blink(void)
3074 * Flyswatter2 only has one LED connected to ACBUS2
3076 flyswatter_jtag_blink(0x4);
3079 static void turtle_jtag_blink(void)
3082 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3084 if (high_output & 0x08)
3090 buffer_write(high_output);
3091 buffer_write(high_direction);
3094 static void lisa_l_blink(void)
3097 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3099 if (high_output & 0x10)
3105 buffer_write(high_output);
3106 buffer_write(high_direction);
3109 static void flossjtag_blink(void)
3112 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3114 if (high_output & 0x10)
3120 buffer_write(high_output);
3121 buffer_write(high_direction);
3124 static int ft2232_quit(void)
3126 #if BUILD_FT2232_FTD2XX == 1
3129 status = FT_Close(ftdih);
3130 #elif BUILD_FT2232_LIBFTDI == 1
3131 ftdi_usb_close(&ftdic);
3133 ftdi_deinit(&ftdic);
3136 free(ft2232_buffer);
3137 ft2232_buffer = NULL;
3142 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3146 if (CMD_ARGC == 1) {
3147 ft2232_device_desc = strdup(CMD_ARGV[0]);
3148 cp = strchr(ft2232_device_desc, 0);
3149 /* under Win32, the FTD2XX driver appends an "A" to the end
3150 * of the description, this examines the given desc
3151 * and creates the 'missing' _A or non_A variable. */
3152 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3153 /* it was, so make this the "A" version. */
3154 ft2232_device_desc_A = ft2232_device_desc;
3155 /* and *CREATE* the non-A version. */
3156 strcpy(buf, ft2232_device_desc);
3157 cp = strchr(buf, 0);
3159 ft2232_device_desc = strdup(buf);
3161 /* <space > A not defined
3163 sprintf(buf, "%s A", ft2232_device_desc);
3164 ft2232_device_desc_A = strdup(buf);
3167 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3172 COMMAND_HANDLER(ft2232_handle_serial_command)
3175 ft2232_serial = strdup(CMD_ARGV[0]);
3177 return ERROR_COMMAND_SYNTAX_ERROR;
3182 COMMAND_HANDLER(ft2232_handle_layout_command)
3185 return ERROR_COMMAND_SYNTAX_ERROR;
3188 LOG_ERROR("already specified ft2232_layout %s",
3190 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3195 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3196 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3202 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3206 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3208 if (CMD_ARGC > MAX_USB_IDS * 2) {
3209 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3210 "(maximum is %d pairs)", MAX_USB_IDS);
3211 CMD_ARGC = MAX_USB_IDS * 2;
3213 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3214 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3216 return ERROR_COMMAND_SYNTAX_ERROR;
3217 /* remove the incomplete trailing id */
3222 for (i = 0; i < CMD_ARGC; i += 2) {
3223 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3224 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3228 * Explicitly terminate, in case there are multiples instances of
3231 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3236 COMMAND_HANDLER(ft2232_handle_latency_command)
3239 ft2232_latency = atoi(CMD_ARGV[0]);
3241 return ERROR_COMMAND_SYNTAX_ERROR;
3246 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3250 /* 7 bits of either ones or zeros. */
3251 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3253 while (num_cycles > 0) {
3254 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3255 * at most 7 bits per invocation. Here we invoke it potentially
3258 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3260 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3261 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3262 retval = ERROR_JTAG_QUEUE_FAILED;
3267 /* there are no state transitions in this code, so omit state tracking */
3269 /* command "Clock Data to TMS/CS Pin (no Read)" */
3273 buffer_write(bitcount_per_command - 1);
3275 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3280 num_cycles -= bitcount_per_command;
3286 /* ---------------------------------------------------------------------
3287 * Support for IceBear JTAG adapter from Section5:
3288 * http://section5.ch/icebear
3290 * Author: Sten, debian@sansys-electronic.com
3293 /* Icebear pin layout
3295 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3296 * GND GND | 4 3| n.c.
3297 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3298 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3299 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3300 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3301 * ADBUS2 TDO |14 13| GND GND
3303 * ADBUS0 O L TCK ACBUS0 GND
3304 * ADBUS1 O L TDI ACBUS1 GND
3305 * ADBUS2 I TDO ACBUS2 n.c.
3306 * ADBUS3 O H TMS ACBUS3 n.c.
3312 static int icebear_jtag_init(void)
3314 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3315 low_output = 0x08; /* high: TMS; low: TCK TDI */
3319 enum reset_types jtag_reset_config = jtag_get_reset_config();
3320 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3321 low_direction &= ~nTRST; /* nTRST high impedance */
3323 low_direction |= nTRST;
3324 low_output |= nTRST;
3327 low_direction |= nSRST;
3328 low_output |= nSRST;
3330 /* initialize low byte for jtag */
3331 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3332 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3333 return ERROR_JTAG_INIT_FAILED;
3337 high_direction = 0x00;
3339 /* initialize high byte for jtag */
3340 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3341 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3342 return ERROR_JTAG_INIT_FAILED;
3348 static void icebear_jtag_reset(int trst, int srst)
3351 low_direction |= nTRST;
3352 low_output &= ~nTRST;
3353 } else if (trst == 0) {
3354 enum reset_types jtag_reset_config = jtag_get_reset_config();
3355 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3356 low_direction &= ~nTRST;
3358 low_output |= nTRST;
3362 low_output &= ~nSRST;
3364 low_output |= nSRST;
3366 /* command "set data bits low byte" */
3368 buffer_write(low_output);
3369 buffer_write(low_direction);
3371 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3378 /* ---------------------------------------------------------------------
3379 * Support for Signalyzer H2 and Signalyzer H4
3380 * JTAG adapter from Xverve Technologies Inc.
3381 * http://www.signalyzer.com or http://www.xverve.com
3383 * Author: Oleg Seiljus, oleg@signalyzer.com
3385 static unsigned char signalyzer_h_side;
3386 static unsigned int signalyzer_h_adapter_type;
3388 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3390 #if BUILD_FT2232_FTD2XX == 1
3391 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3394 #define SIGNALYZER_COMMAND_ADDR 128
3395 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3397 #define SIGNALYZER_COMMAND_VERSION 0x41
3398 #define SIGNALYZER_COMMAND_RESET 0x42
3399 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3400 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3401 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3402 #define SIGNALYZER_COMMAND_LED_SET 0x53
3403 #define SIGNALYZER_COMMAND_ADC 0x54
3404 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3405 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3406 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3407 #define SIGNALYZER_COMMAND_I2C 0x58
3409 #define SIGNALYZER_CHAN_A 1
3410 #define SIGNALYZER_CHAN_B 2
3411 /* LEDS use channel C */
3412 #define SIGNALYZER_CHAN_C 4
3414 #define SIGNALYZER_LED_GREEN 1
3415 #define SIGNALYZER_LED_RED 2
3417 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3418 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3419 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3420 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3421 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3424 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3426 #if BUILD_FT2232_FTD2XX == 1
3427 return FT_WriteEE(ftdih, address, value);
3428 #elif BUILD_FT2232_LIBFTDI == 1
3433 #if BUILD_FT2232_FTD2XX == 1
3434 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3436 return FT_ReadEE(ftdih, address, value);
3440 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3441 int on_time_ms, int off_time_ms, unsigned char cycles)
3443 unsigned char on_time;
3444 unsigned char off_time;
3446 if (on_time_ms < 0xFFFF)
3447 on_time = (unsigned char)(on_time_ms / 62);
3451 off_time = (unsigned char)(off_time_ms / 62);
3453 #if BUILD_FT2232_FTD2XX == 1
3456 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3457 ((uint32_t)(channel << 8) | led));
3458 if (status != FT_OK) {
3459 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3460 ftd2xx_status_string(status));
3461 return ERROR_JTAG_DEVICE_ERROR;
3464 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3465 ((uint32_t)(on_time << 8) | off_time));
3466 if (status != FT_OK) {
3467 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3468 ftd2xx_status_string(status));
3469 return ERROR_JTAG_DEVICE_ERROR;
3472 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3473 ((uint32_t)cycles));
3474 if (status != FT_OK) {
3475 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3476 ftd2xx_status_string(status));
3477 return ERROR_JTAG_DEVICE_ERROR;
3480 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3481 SIGNALYZER_COMMAND_LED_SET);
3482 if (status != FT_OK) {
3483 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3484 ftd2xx_status_string(status));
3485 return ERROR_JTAG_DEVICE_ERROR;
3489 #elif BUILD_FT2232_LIBFTDI == 1
3492 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3493 ((uint32_t)(channel << 8) | led));
3495 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3496 ftdi_get_error_string(&ftdic));
3497 return ERROR_JTAG_DEVICE_ERROR;
3500 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3501 ((uint32_t)(on_time << 8) | off_time));
3503 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3504 ftdi_get_error_string(&ftdic));
3505 return ERROR_JTAG_DEVICE_ERROR;
3508 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3511 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3512 ftdi_get_error_string(&ftdic));
3513 return ERROR_JTAG_DEVICE_ERROR;
3516 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3517 SIGNALYZER_COMMAND_LED_SET);
3519 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3520 ftdi_get_error_string(&ftdic));
3521 return ERROR_JTAG_DEVICE_ERROR;
3528 static int signalyzer_h_init(void)
3530 #if BUILD_FT2232_FTD2XX == 1
3537 uint16_t read_buf[12] = { 0 };
3539 /* turn on center green led */
3540 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3541 0xFFFF, 0x00, 0x00);
3543 /* determine what channel config wants to open
3544 * TODO: change me... current implementation is made to work
3545 * with openocd description parsing.
3547 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3550 signalyzer_h_side = *(end_of_desc - 1);
3551 if (signalyzer_h_side == 'B')
3552 signalyzer_h_side = SIGNALYZER_CHAN_B;
3554 signalyzer_h_side = SIGNALYZER_CHAN_A;
3556 LOG_ERROR("No Channel was specified");
3560 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3563 #if BUILD_FT2232_FTD2XX == 1
3564 /* read signalyzer versionining information */
3565 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3566 SIGNALYZER_COMMAND_VERSION);
3567 if (status != FT_OK) {
3568 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3569 ftd2xx_status_string(status));
3570 return ERROR_JTAG_DEVICE_ERROR;
3573 for (i = 0; i < 10; i++) {
3574 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3576 if (status != FT_OK) {
3577 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3578 ftd2xx_status_string(status));
3579 return ERROR_JTAG_DEVICE_ERROR;
3583 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3584 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3585 read_buf[4], read_buf[5], read_buf[6]);
3587 /* set gpio register */
3588 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3589 (uint32_t)(signalyzer_h_side << 8));
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 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
3597 if (status != FT_OK) {
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3599 ftd2xx_status_string(status));
3600 return ERROR_JTAG_DEVICE_ERROR;
3603 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3604 SIGNALYZER_COMMAND_GPIO_STATE);
3605 if (status != FT_OK) {
3606 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3607 ftd2xx_status_string(status));
3608 return ERROR_JTAG_DEVICE_ERROR;
3611 /* read adapter type information */
3612 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3613 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3614 if (status != FT_OK) {
3615 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3616 ftd2xx_status_string(status));
3617 return ERROR_JTAG_DEVICE_ERROR;
3620 status = signalyzer_h_ctrl_write(
3621 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3622 if (status != FT_OK) {
3623 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3624 ftd2xx_status_string(status));
3625 return ERROR_JTAG_DEVICE_ERROR;
3628 status = signalyzer_h_ctrl_write(
3629 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3630 if (status != FT_OK) {
3631 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3632 ftd2xx_status_string(status));
3633 return ERROR_JTAG_DEVICE_ERROR;
3636 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3637 SIGNALYZER_COMMAND_I2C);
3638 if (status != FT_OK) {
3639 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3640 ftd2xx_status_string(status));
3641 return ERROR_JTAG_DEVICE_ERROR;
3646 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3647 if (status != FT_OK) {
3648 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3649 ftd2xx_status_string(status));
3650 return ERROR_JTAG_DEVICE_ERROR;
3653 if (read_buf[0] != 0x0498)
3654 signalyzer_h_adapter_type = 0x0000;
3656 for (i = 0; i < 4; i++) {
3657 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3658 if (status != FT_OK) {
3659 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3660 ftd2xx_status_string(status));
3661 return ERROR_JTAG_DEVICE_ERROR;
3665 signalyzer_h_adapter_type = read_buf[0];
3668 #elif BUILD_FT2232_LIBFTDI == 1
3669 /* currently libftdi does not allow reading individual eeprom
3670 * locations, therefore adapter type cannot be detected.
3671 * override with most common type
3673 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3676 enum reset_types jtag_reset_config = jtag_get_reset_config();
3678 /* ADAPTOR: EM_LT16_A */
3679 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3680 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3681 "detected. (HW: %2x).", (read_buf[1] >> 8));
3689 low_direction = 0x1b;
3692 high_direction = 0x0;
3694 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3695 low_direction &= ~nTRSTnOE; /* nTRST input */
3696 low_output &= ~nTRST; /* nTRST = 0 */
3698 low_direction |= nTRSTnOE; /* nTRST output */
3699 low_output |= nTRST; /* nTRST = 1 */
3702 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3703 low_direction |= nSRSTnOE; /* nSRST output */
3704 low_output |= nSRST; /* nSRST = 1 */
3706 low_direction &= ~nSRSTnOE; /* nSRST input */
3707 low_output &= ~nSRST; /* nSRST = 0 */
3710 #if BUILD_FT2232_FTD2XX == 1
3711 /* enable power to the module */
3712 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3713 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3714 if (status != FT_OK) {
3715 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3716 ftd2xx_status_string(status));
3717 return ERROR_JTAG_DEVICE_ERROR;
3720 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3721 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3722 if (status != FT_OK) {
3723 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3724 ftd2xx_status_string(status));
3725 return ERROR_JTAG_DEVICE_ERROR;
3728 /* set gpio mode register */
3729 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3730 (uint32_t)(signalyzer_h_side << 8));
3731 if (status != FT_OK) {
3732 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3733 ftd2xx_status_string(status));
3734 return ERROR_JTAG_DEVICE_ERROR;
3737 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3738 if (status != FT_OK) {
3739 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3740 ftd2xx_status_string(status));
3741 return ERROR_JTAG_DEVICE_ERROR;
3744 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
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 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, 0x4040);
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,
3768 SIGNALYZER_COMMAND_GPIO_STATE);
3769 if (status != FT_OK) {
3770 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3771 ftd2xx_status_string(status));
3772 return ERROR_JTAG_DEVICE_ERROR;
3776 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3777 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3778 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3779 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3780 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3781 if (signalyzer_h_adapter_type
3782 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3783 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3784 "detected. (HW: %2x).", (read_buf[1] >> 8));
3785 else if (signalyzer_h_adapter_type
3786 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3787 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3788 "(ARM JTAG with PSU) detected. (HW: %2x).",
3789 (read_buf[1] >> 8));
3790 else if (signalyzer_h_adapter_type
3791 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3792 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3793 "detected. (HW: %2x).", (read_buf[1] >> 8));
3794 else if (signalyzer_h_adapter_type
3795 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3796 LOG_INFO("Signalyzer: EM-JTAG-P "
3797 "(Generic JTAG with PSU) detected. (HW: %2x).",
3798 (read_buf[1] >> 8));
3806 low_direction = 0x1b;
3809 high_direction = 0x1f;
3811 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3812 high_output |= nTRSTnOE;
3813 high_output &= ~nTRST;
3815 high_output &= ~nTRSTnOE;
3816 high_output |= nTRST;
3819 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3820 high_output &= ~nSRSTnOE;
3821 high_output |= nSRST;
3823 high_output |= nSRSTnOE;
3824 high_output &= ~nSRST;
3827 #if BUILD_FT2232_FTD2XX == 1
3828 /* enable power to the module */
3829 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3830 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3831 if (status != FT_OK) {
3832 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3833 ftd2xx_status_string(status));
3834 return ERROR_JTAG_DEVICE_ERROR;
3837 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3838 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3839 if (status != FT_OK) {
3840 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3841 ftd2xx_status_string(status));
3842 return ERROR_JTAG_DEVICE_ERROR;
3845 /* set gpio mode register (IO_16 and IO_17 set as analog
3846 * inputs, other is gpio)
3848 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3849 (uint32_t)(signalyzer_h_side << 8));
3850 if (status != FT_OK) {
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3852 ftd2xx_status_string(status));
3853 return ERROR_JTAG_DEVICE_ERROR;
3856 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060);
3857 if (status != FT_OK) {
3858 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3859 ftd2xx_status_string(status));
3860 return ERROR_JTAG_DEVICE_ERROR;
3863 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3864 if (status != FT_OK) {
3865 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3866 ftd2xx_status_string(status));
3867 return ERROR_JTAG_DEVICE_ERROR;
3870 /* set gpio register (all inputs, for -P modules,
3871 * PSU will be turned off)
3873 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3874 (uint32_t)(signalyzer_h_side << 8));
3875 if (status != FT_OK) {
3876 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3877 ftd2xx_status_string(status));
3878 return ERROR_JTAG_DEVICE_ERROR;
3881 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3882 if (status != FT_OK) {
3883 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3884 ftd2xx_status_string(status));
3885 return ERROR_JTAG_DEVICE_ERROR;
3888 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
3889 if (status != FT_OK) {
3890 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3891 ftd2xx_status_string(status));
3892 return ERROR_JTAG_DEVICE_ERROR;
3895 } else if (signalyzer_h_adapter_type == 0x0000) {
3896 LOG_INFO("Signalyzer: No external modules were detected.");
3904 low_direction = 0x1b;
3907 high_direction = 0x0;
3909 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3910 low_direction &= ~nTRSTnOE; /* nTRST input */
3911 low_output &= ~nTRST; /* nTRST = 0 */
3913 low_direction |= nTRSTnOE; /* nTRST output */
3914 low_output |= nTRST; /* nTRST = 1 */
3917 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3918 low_direction |= nSRSTnOE; /* nSRST output */
3919 low_output |= nSRST; /* nSRST = 1 */
3921 low_direction &= ~nSRSTnOE; /* nSRST input */
3922 low_output &= ~nSRST; /* nSRST = 0 */
3925 LOG_ERROR("Unknown module type is detected: %.4x",
3926 signalyzer_h_adapter_type);
3927 return ERROR_JTAG_DEVICE_ERROR;
3930 /* initialize low byte of controller for jtag operation */
3931 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3932 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3933 return ERROR_JTAG_INIT_FAILED;
3936 #if BUILD_FT2232_FTD2XX == 1
3937 if (ftdi_device == FT_DEVICE_2232H) {
3938 /* initialize high byte of controller for jtag operation */
3939 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3940 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3941 return ERROR_JTAG_INIT_FAILED;
3944 #elif BUILD_FT2232_LIBFTDI == 1
3945 if (ftdi_device == TYPE_2232H) {
3946 /* initialize high byte of controller for jtag operation */
3947 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3948 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3949 return ERROR_JTAG_INIT_FAILED;
3956 static void signalyzer_h_reset(int trst, int srst)
3958 enum reset_types jtag_reset_config = jtag_get_reset_config();
3960 /* ADAPTOR: EM_LT16_A */
3961 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3963 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3964 /* switch to output pin (output is low) */
3965 low_direction |= nTRSTnOE;
3967 /* switch output low */
3968 low_output &= ~nTRST;
3969 } else if (trst == 0) {
3970 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3971 /* switch to input pin (high-Z + internal
3972 * and external pullup) */
3973 low_direction &= ~nTRSTnOE;
3975 /* switch output high */
3976 low_output |= nTRST;
3980 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3981 /* switch output low */
3982 low_output &= ~nSRST;
3984 /* switch to output pin (output is low) */
3985 low_direction |= nSRSTnOE;
3986 } else if (srst == 0) {
3987 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3988 /* switch output high */
3989 low_output |= nSRST;
3991 /* switch to input pin (high-Z) */
3992 low_direction &= ~nSRSTnOE;
3995 /* command "set data bits low byte" */
3997 buffer_write(low_output);
3998 buffer_write(low_direction);
3999 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4000 "low_direction: 0x%2.2x",
4001 trst, srst, low_output, low_direction);
4003 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4004 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4005 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4006 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4007 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4009 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4010 high_output &= ~nTRSTnOE;
4012 high_output &= ~nTRST;
4013 } else if (trst == 0) {
4014 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4015 high_output |= nTRSTnOE;
4017 high_output |= nTRST;
4021 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4022 high_output &= ~nSRST;
4024 high_output &= ~nSRSTnOE;
4025 } else if (srst == 0) {
4026 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4027 high_output |= nSRST;
4029 high_output |= nSRSTnOE;
4032 /* command "set data bits high byte" */
4034 buffer_write(high_output);
4035 buffer_write(high_direction);
4036 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4037 "high_direction: 0x%2.2x",
4038 trst, srst, high_output, high_direction);
4039 } else if (signalyzer_h_adapter_type == 0x0000) {
4041 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4042 /* switch to output pin (output is low) */
4043 low_direction |= nTRSTnOE;
4045 /* switch output low */
4046 low_output &= ~nTRST;
4047 } else if (trst == 0) {
4048 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4049 /* switch to input pin (high-Z + internal
4050 * and external pullup) */
4051 low_direction &= ~nTRSTnOE;
4053 /* switch output high */
4054 low_output |= nTRST;
4058 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4059 /* switch output low */
4060 low_output &= ~nSRST;
4062 /* switch to output pin (output is low) */
4063 low_direction |= nSRSTnOE;
4064 } else if (srst == 0) {
4065 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4066 /* switch output high */
4067 low_output |= nSRST;
4069 /* switch to input pin (high-Z) */
4070 low_direction &= ~nSRSTnOE;
4073 /* command "set data bits low byte" */
4075 buffer_write(low_output);
4076 buffer_write(low_direction);
4077 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4078 "low_direction: 0x%2.2x",
4079 trst, srst, low_output, low_direction);
4083 static void signalyzer_h_blink(void)
4085 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4088 /********************************************************************
4089 * Support for KT-LINK
4090 * JTAG adapter from KRISTECH
4091 * http://www.kristech.eu
4092 *******************************************************************/
4093 static int ktlink_init(void)
4095 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4097 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4098 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4100 /* initialize low byte for jtag */
4101 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4102 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4103 return ERROR_JTAG_INIT_FAILED;
4111 high_output = 0x80; /* turn LED on */
4112 high_direction = 0xFF; /* all outputs */
4114 enum reset_types jtag_reset_config = jtag_get_reset_config();
4116 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4117 high_output |= nTRSTnOE;
4118 high_output &= ~nTRST;
4120 high_output &= ~nTRSTnOE;
4121 high_output |= nTRST;
4124 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4125 high_output &= ~nSRSTnOE;
4126 high_output |= nSRST;
4128 high_output |= nSRSTnOE;
4129 high_output &= ~nSRST;
4132 /* initialize high byte for jtag */
4133 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4134 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4135 return ERROR_JTAG_INIT_FAILED;
4141 static void ktlink_reset(int trst, int srst)
4143 enum reset_types jtag_reset_config = jtag_get_reset_config();
4146 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4147 high_output &= ~nTRSTnOE;
4149 high_output &= ~nTRST;
4150 } else if (trst == 0) {
4151 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4152 high_output |= nTRSTnOE;
4154 high_output |= nTRST;
4158 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4159 high_output &= ~nSRST;
4161 high_output &= ~nSRSTnOE;
4162 } else if (srst == 0) {
4163 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4164 high_output |= nSRST;
4166 high_output |= nSRSTnOE;
4169 buffer_write(0x82); /* command "set data bits high byte" */
4170 buffer_write(high_output);
4171 buffer_write(high_direction);
4172 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4179 static void ktlink_blink(void)
4181 /* LED connected to ACBUS7 */
4182 high_output ^= 0x80;
4184 buffer_write(0x82); /* command "set data bits high byte" */
4185 buffer_write(high_output);
4186 buffer_write(high_direction);
4189 /********************************************************************
4190 * Support for Digilent HS-1
4191 * JTAG adapter from Digilent
4192 * http://www.digilent.com
4193 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4194 *******************************************************************/
4196 static int digilent_hs1_init(void)
4198 /* the adapter only supports the base JTAG signals, no nTRST
4201 low_direction = 0x8b;
4203 /* initialize low byte for jtag */
4204 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4205 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4206 return ERROR_JTAG_INIT_FAILED;
4211 static void digilent_hs1_reset(int trst, int srst)
4213 /* Dummy function, no reset signals supported. */
4216 static const struct command_registration ft2232_command_handlers[] = {
4218 .name = "ft2232_device_desc",
4219 .handler = &ft2232_handle_device_desc_command,
4220 .mode = COMMAND_CONFIG,
4221 .help = "set the USB device description of the FTDI FT2232 device",
4222 .usage = "description_string",
4225 .name = "ft2232_serial",
4226 .handler = &ft2232_handle_serial_command,
4227 .mode = COMMAND_CONFIG,
4228 .help = "set the serial number of the FTDI FT2232 device",
4229 .usage = "serial_string",
4232 .name = "ft2232_layout",
4233 .handler = &ft2232_handle_layout_command,
4234 .mode = COMMAND_CONFIG,
4235 .help = "set the layout of the FT2232 GPIO signals used "
4236 "to control output-enables and reset signals",
4237 .usage = "layout_name",
4240 .name = "ft2232_vid_pid",
4241 .handler = &ft2232_handle_vid_pid_command,
4242 .mode = COMMAND_CONFIG,
4243 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4244 .usage = "(vid pid)* ",
4247 .name = "ft2232_latency",
4248 .handler = &ft2232_handle_latency_command,
4249 .mode = COMMAND_CONFIG,
4250 .help = "set the FT2232 latency timer to a new value",
4253 COMMAND_REGISTRATION_DONE
4256 struct jtag_interface ft2232_interface = {
4258 .supported = DEBUG_CAP_TMS_SEQ,
4259 .commands = ft2232_command_handlers,
4260 .transports = jtag_only,
4262 .init = ft2232_init,
4263 .quit = ft2232_quit,
4264 .speed = ft2232_speed,
4265 .speed_div = ft2232_speed_div,
4267 .execute_queue = ft2232_execute_queue,