1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <transport/transport.h>
85 #include <helper/time_support.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
102 #include "ftd2xx_common.h"
104 enum ftdi_interface {
112 #elif BUILD_FT2232_LIBFTDI == 1
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_x232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H, FT_DEVICE_232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H = 4, TYPE_4232H = 5, TYPE_232H = 6 };
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
154 static char *ft2232_device_desc_A;
155 static char *ft2232_device_desc;
156 static char *ft2232_serial;
157 static uint8_t ft2232_latency = 2;
158 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
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 #ifdef HAS_ENUM_FT232H
603 || (ftdi_device == FT_DEVICE_232H)
606 #elif BUILD_FT2232_LIBFTDI == 1
607 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H
608 #ifdef HAS_ENUM_FT232H
609 || ftdi_device == TYPE_232H
616 * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
617 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
618 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
621 static int ftx232h_adaptive_clocking(bool enable)
623 uint8_t buf = enable ? 0x96 : 0x97;
624 LOG_DEBUG("%2.2x", buf);
626 uint32_t bytes_written;
629 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
630 if (retval != ERROR_OK) {
631 LOG_ERROR("couldn't write command to %s adaptive clocking"
632 , enable ? "enable" : "disable");
640 * Enable/disable the clk divide by 5 of the 60MHz master clock.
641 * This result in a JTAG clock speed range of 91.553Hz-6MHz
642 * respective 457.763Hz-30MHz.
644 static int ftx232h_clk_divide_by_5(bool enable)
646 uint32_t bytes_written;
647 uint8_t buf = enable ? 0x8b : 0x8a;
649 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
650 LOG_ERROR("couldn't write command to %s clk divide by 5"
651 , enable ? "enable" : "disable");
652 return ERROR_JTAG_INIT_FAILED;
654 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_x232H_MAX_TCK;
655 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
660 static int ft2232_speed(int speed)
664 uint32_t bytes_written;
667 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
668 if (ft2232_device_is_highspeed())
669 retval = ftx232h_adaptive_clocking(enable_adaptive_clocking);
670 else if (enable_adaptive_clocking) {
671 LOG_ERROR("ft2232 device %lu does not support RTCK"
672 , (long unsigned int)ftdi_device);
676 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
679 buf[0] = 0x86; /* command "set divisor" */
680 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
681 buf[2] = (speed >> 8) & 0xff; /* valueH */
683 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
684 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
685 if (retval != ERROR_OK) {
686 LOG_ERROR("couldn't set FT2232 TCK speed");
693 static int ft2232_speed_div(int speed, int *khz)
695 /* Take a look in the FT2232 manual,
696 * AN2232C-01 Command Processor for
697 * MPSSE and MCU Host Bus. Chapter 3.8 */
699 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
704 static int ft2232_khz(int khz, int *jtag_speed)
707 if (ft2232_device_is_highspeed()) {
708 *jtag_speed = RTCK_SPEED;
711 LOG_DEBUG("RCLK not supported");
716 /* Take a look in the FT2232 manual,
717 * AN2232C-01 Command Processor for
718 * MPSSE and MCU Host Bus. Chapter 3.8
720 * We will calc here with a multiplier
721 * of 10 for better rounding later. */
723 /* Calc speed, (ft2232_max_tck / khz) - 1
724 * Use 65000 for better rounding */
725 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
727 /* Add 0.9 for rounding */
730 /* Calc real speed */
731 *jtag_speed = *jtag_speed / 10;
733 /* Check if speed is greater than 0 */
737 /* Check max value */
738 if (*jtag_speed > 0xFFFF)
739 *jtag_speed = 0xFFFF;
744 static void ft2232_end_state(tap_state_t state)
746 if (tap_is_state_stable(state))
747 tap_set_end_state(state);
749 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
754 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
756 int num_bytes = (scan_size + 7) / 8;
757 int bits_left = scan_size;
760 while (num_bytes-- > 1) {
761 buffer[cur_byte++] = buffer_read();
765 buffer[cur_byte] = 0x0;
767 /* There is one more partial byte left from the clock data in/out instructions */
769 buffer[cur_byte] = buffer_read() >> 1;
770 /* This shift depends on the length of the
771 *clock data to tms instruction, insterted
772 *at end of the scan, now fixed to a two
773 *step transition in ft2232_add_scan */
774 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
777 static void ft2232_debug_dump_buffer(void)
783 for (i = 0; i < ft2232_buffer_size; i++) {
784 line_p += snprintf(line_p,
785 sizeof(line) - (line_p - line),
789 LOG_DEBUG("%s", line);
795 LOG_DEBUG("%s", line);
798 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
800 struct jtag_command *cmd;
805 uint32_t bytes_written = 0;
806 uint32_t bytes_read = 0;
808 #ifdef _DEBUG_USB_IO_
809 struct timeval start, inter, inter2, end;
810 struct timeval d_inter, d_inter2, d_end;
813 #ifdef _DEBUG_USB_COMMS_
814 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
815 ft2232_debug_dump_buffer();
818 #ifdef _DEBUG_USB_IO_
819 gettimeofday(&start, NULL);
822 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
823 if (retval != ERROR_OK) {
824 LOG_ERROR("couldn't write MPSSE commands to FT2232");
828 #ifdef _DEBUG_USB_IO_
829 gettimeofday(&inter, NULL);
832 if (ft2232_expect_read) {
833 /* FIXME this "timeout" is never changed ... */
834 int timeout = LIBFTDI_READ_RETRY_COUNT;
835 ft2232_buffer_size = 0;
837 #ifdef _DEBUG_USB_IO_
838 gettimeofday(&inter2, NULL);
841 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
842 if (retval != ERROR_OK) {
843 LOG_ERROR("couldn't read from FT2232");
847 #ifdef _DEBUG_USB_IO_
848 gettimeofday(&end, NULL);
850 timeval_subtract(&d_inter, &inter, &start);
851 timeval_subtract(&d_inter2, &inter2, &start);
852 timeval_subtract(&d_end, &end, &start);
854 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
855 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
856 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
857 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
860 ft2232_buffer_size = bytes_read;
862 if (ft2232_expect_read != ft2232_buffer_size) {
863 LOG_ERROR("ft2232_expect_read (%i) != "
864 "ft2232_buffer_size (%i) "
868 LIBFTDI_READ_RETRY_COUNT - timeout);
869 ft2232_debug_dump_buffer();
874 #ifdef _DEBUG_USB_COMMS_
875 LOG_DEBUG("read buffer (%i retries): %i bytes",
876 LIBFTDI_READ_RETRY_COUNT - timeout,
878 ft2232_debug_dump_buffer();
882 ft2232_expect_read = 0;
883 ft2232_read_pointer = 0;
885 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
886 * that wasn't handled by a caller-provided error handler
891 while (cmd != last) {
894 type = jtag_scan_type(cmd->cmd.scan);
895 if (type != SCAN_OUT) {
896 scan_size = jtag_scan_size(cmd->cmd.scan);
897 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
898 ft2232_read_scan(type, buffer, scan_size);
899 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
900 retval = ERROR_JTAG_QUEUE_FAILED;
912 ft2232_buffer_size = 0;
918 * Function ft2232_add_pathmove
919 * moves the TAP controller from the current state to a new state through the
920 * given path, where path is an array of tap_state_t's.
922 * @param path is an array of tap_stat_t which gives the states to traverse through
923 * ending with the last state at path[num_states-1]
924 * @param num_states is the count of state steps to move through
926 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
930 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
934 /* this loop verifies that the path is legal and logs each state in the path */
936 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
938 int num_states_batch = num_states > 7 ? 7 : num_states;
940 /* command "Clock Data to TMS/CS Pin (no Read)" */
943 /* number of states remaining */
944 buffer_write(num_states_batch - 1);
946 while (num_states_batch--) {
947 /* either TMS=0 or TMS=1 must work ... */
948 if (tap_state_transition(tap_get_state(), false) == path[state_count])
949 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
950 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
951 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
953 /* ... or else the caller goofed BADLY */
955 LOG_ERROR("BUG: %s -> %s isn't a valid "
956 "TAP state transition",
957 tap_state_name(tap_get_state()),
958 tap_state_name(path[state_count]));
962 tap_set_state(path[state_count]);
967 buffer_write(tms_byte);
969 tap_set_end_state(tap_get_state());
972 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
974 int num_bytes = (scan_size + 7) / 8;
975 int bits_left = scan_size;
980 if (tap_get_state() != TAP_DRSHIFT)
981 move_to_state(TAP_DRSHIFT);
983 if (tap_get_state() != TAP_IRSHIFT)
984 move_to_state(TAP_IRSHIFT);
987 /* add command for complete bytes */
988 while (num_bytes > 1) {
990 if (type == SCAN_IO) {
991 /* Clock Data Bytes In and Out LSB First */
993 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
994 } else if (type == SCAN_OUT) {
995 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
997 /* LOG_DEBUG("added TDI bytes (o)"); */
998 } else if (type == SCAN_IN) {
999 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1001 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1004 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1005 num_bytes -= thisrun_bytes;
1007 buffer_write((uint8_t) (thisrun_bytes - 1));
1008 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1010 if (type != SCAN_IN) {
1011 /* add complete bytes */
1012 while (thisrun_bytes-- > 0) {
1013 buffer_write(buffer[cur_byte++]);
1016 } else /* (type == SCAN_IN) */
1017 bits_left -= 8 * (thisrun_bytes);
1020 /* the most signifcant bit is scanned during TAP movement */
1021 if (type != SCAN_IN)
1022 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1026 /* process remaining bits but the last one */
1027 if (bits_left > 1) {
1028 if (type == SCAN_IO) {
1029 /* Clock Data Bits In and Out LSB First */
1031 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1032 } else if (type == SCAN_OUT) {
1033 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1035 /* LOG_DEBUG("added TDI bits (o)"); */
1036 } else if (type == SCAN_IN) {
1037 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1039 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1042 buffer_write(bits_left - 2);
1043 if (type != SCAN_IN)
1044 buffer_write(buffer[cur_byte]);
1047 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1048 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1049 if (type == SCAN_IO) {
1050 /* Clock Data Bits In and Out LSB First */
1052 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1053 } else if (type == SCAN_OUT) {
1054 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1056 /* LOG_DEBUG("added TDI bits (o)"); */
1057 } else if (type == SCAN_IN) {
1058 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1060 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1063 buffer_write(last_bit);
1069 /* move from Shift-IR/DR to end state */
1070 if (type != SCAN_OUT) {
1071 /* We always go to the PAUSE state in two step at the end of an IN or IO
1073 * This must be coordinated with the bit shifts in ft2232_read_scan */
1076 /* Clock Data to TMS/CS Pin with Read */
1079 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1080 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1081 /* Clock Data to TMS/CS Pin (no Read) */
1085 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1086 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1089 if (tap_get_state() != tap_get_end_state())
1090 move_to_state(tap_get_end_state());
1093 static int ft2232_large_scan(struct scan_command *cmd,
1094 enum scan_type type,
1098 int num_bytes = (scan_size + 7) / 8;
1099 int bits_left = scan_size;
1102 uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1103 uint8_t *receive_pointer = receive_buffer;
1104 uint32_t bytes_written;
1105 uint32_t bytes_read;
1107 int thisrun_read = 0;
1110 LOG_ERROR("BUG: large IR scans are not supported");
1114 if (tap_get_state() != TAP_DRSHIFT)
1115 move_to_state(TAP_DRSHIFT);
1117 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1118 if (retval != ERROR_OK) {
1119 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1122 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1123 ft2232_buffer_size, (int)bytes_written);
1124 ft2232_buffer_size = 0;
1126 /* add command for complete bytes */
1127 while (num_bytes > 1) {
1130 if (type == SCAN_IO) {
1131 /* Clock Data Bytes In and Out LSB First */
1133 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1134 } else if (type == SCAN_OUT) {
1135 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1137 /* LOG_DEBUG("added TDI bytes (o)"); */
1138 } else if (type == SCAN_IN) {
1139 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1141 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1144 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1145 thisrun_read = thisrun_bytes;
1146 num_bytes -= thisrun_bytes;
1147 buffer_write((uint8_t) (thisrun_bytes - 1));
1148 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1150 if (type != SCAN_IN) {
1151 /* add complete bytes */
1152 while (thisrun_bytes-- > 0) {
1153 buffer_write(buffer[cur_byte]);
1157 } else /* (type == SCAN_IN) */
1158 bits_left -= 8 * (thisrun_bytes);
1160 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1161 if (retval != ERROR_OK) {
1162 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1165 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1167 (int)bytes_written);
1168 ft2232_buffer_size = 0;
1170 if (type != SCAN_OUT) {
1171 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1172 if (retval != ERROR_OK) {
1173 LOG_ERROR("couldn't read from FT2232");
1176 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1179 receive_pointer += bytes_read;
1185 /* the most signifcant bit is scanned during TAP movement */
1186 if (type != SCAN_IN)
1187 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1191 /* process remaining bits but the last one */
1192 if (bits_left > 1) {
1193 if (type == SCAN_IO) {
1194 /* Clock Data Bits In and Out LSB First */
1196 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1197 } else if (type == SCAN_OUT) {
1198 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1200 /* LOG_DEBUG("added TDI bits (o)"); */
1201 } else if (type == SCAN_IN) {
1202 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1204 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1206 buffer_write(bits_left - 2);
1207 if (type != SCAN_IN)
1208 buffer_write(buffer[cur_byte]);
1210 if (type != SCAN_OUT)
1214 if (tap_get_end_state() == TAP_DRSHIFT) {
1215 if (type == SCAN_IO) {
1216 /* Clock Data Bits In and Out LSB First */
1218 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1219 } else if (type == SCAN_OUT) {
1220 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1222 /* LOG_DEBUG("added TDI bits (o)"); */
1223 } else if (type == SCAN_IN) {
1224 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1226 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1229 buffer_write(last_bit);
1231 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1232 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1235 /* move from Shift-IR/DR to end state */
1236 if (type != SCAN_OUT) {
1237 /* Clock Data to TMS/CS Pin with Read */
1239 /* LOG_DEBUG("added TMS scan (read)"); */
1241 /* Clock Data to TMS/CS Pin (no Read) */
1243 /* LOG_DEBUG("added TMS scan (no read)"); */
1246 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1247 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1250 if (type != SCAN_OUT)
1253 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1254 if (retval != ERROR_OK) {
1255 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1258 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1260 (int)bytes_written);
1261 ft2232_buffer_size = 0;
1263 if (type != SCAN_OUT) {
1264 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1265 if (retval != ERROR_OK) {
1266 LOG_ERROR("couldn't read from FT2232");
1269 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1277 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1279 int predicted_size = 3;
1280 int num_bytes = (scan_size - 1) / 8;
1282 if (tap_get_state() != TAP_DRSHIFT)
1283 predicted_size += get_tms_buffer_requirements(
1284 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1286 if (type == SCAN_IN) { /* only from device to host */
1287 /* complete bytes */
1288 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1290 /* remaining bits - 1 (up to 7) */
1291 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1292 } else {/* host to device, or bidirectional
1294 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1296 /* remaining bits -1 (up to 7) */
1297 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1300 return predicted_size;
1303 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1305 int predicted_size = 0;
1307 if (type != SCAN_OUT) {
1308 /* complete bytes */
1310 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1312 /* remaining bits - 1 */
1313 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1315 /* last bit (from TMS scan) */
1316 predicted_size += 1;
1319 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1321 return predicted_size;
1324 /* semi-generic FT2232/FT4232 reset code */
1325 static void ftx23_reset(int trst, int srst)
1327 enum reset_types jtag_reset_config = jtag_get_reset_config();
1329 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1330 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1332 low_output &= ~nTRST; /* switch output low */
1333 } else if (trst == 0) {
1334 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1335 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1336 *and external pullup) */
1338 low_output |= nTRST; /* switch output high */
1342 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1343 low_output &= ~nSRST; /* switch output low */
1345 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1346 } else if (srst == 0) {
1347 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1348 low_output |= nSRST; /* switch output high */
1350 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1353 /* command "set data bits low byte" */
1355 buffer_write(low_output);
1356 buffer_write(low_direction);
1359 static void jtagkey_reset(int trst, int srst)
1361 enum reset_types jtag_reset_config = jtag_get_reset_config();
1363 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1364 high_output &= ~nTRSTnOE;
1366 high_output &= ~nTRST;
1367 } else if (trst == 0) {
1368 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1369 high_output |= nTRSTnOE;
1371 high_output |= nTRST;
1375 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1376 high_output &= ~nSRST;
1378 high_output &= ~nSRSTnOE;
1379 } else if (srst == 0) {
1380 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1381 high_output |= nSRST;
1383 high_output |= nSRSTnOE;
1386 /* command "set data bits high byte" */
1388 buffer_write(high_output);
1389 buffer_write(high_direction);
1390 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1397 static void olimex_jtag_reset(int trst, int srst)
1399 enum reset_types jtag_reset_config = jtag_get_reset_config();
1401 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1402 high_output &= ~nTRSTnOE;
1404 high_output &= ~nTRST;
1405 } else if (trst == 0) {
1406 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1407 high_output |= nTRSTnOE;
1409 high_output |= nTRST;
1413 high_output |= nSRST;
1415 high_output &= ~nSRST;
1417 /* command "set data bits high byte" */
1419 buffer_write(high_output);
1420 buffer_write(high_direction);
1421 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1428 static void axm0432_jtag_reset(int trst, int srst)
1431 tap_set_state(TAP_RESET);
1432 high_output &= ~nTRST;
1433 } else if (trst == 0)
1434 high_output |= nTRST;
1437 high_output &= ~nSRST;
1439 high_output |= nSRST;
1441 /* command "set data bits low byte" */
1443 buffer_write(high_output);
1444 buffer_write(high_direction);
1445 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1452 static void flyswatter_reset(int trst, int srst)
1455 low_output &= ~nTRST;
1457 low_output |= nTRST;
1460 low_output |= nSRST;
1462 low_output &= ~nSRST;
1464 /* command "set data bits low byte" */
1466 buffer_write(low_output);
1467 buffer_write(low_direction);
1468 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1475 static void flyswatter1_reset(int trst, int srst)
1477 flyswatter_reset(trst, srst);
1480 static void flyswatter2_reset(int trst, int srst)
1482 flyswatter_reset(trst, !srst);
1485 static void minimodule_reset(int trst, int srst)
1488 low_output &= ~nSRST;
1490 low_output |= nSRST;
1492 /* command "set data bits low byte" */
1494 buffer_write(low_output);
1495 buffer_write(low_direction);
1496 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1503 static void turtle_reset(int trst, int srst)
1508 low_output |= nSRST;
1510 low_output &= ~nSRST;
1512 /* command "set data bits low byte" */
1514 buffer_write(low_output);
1515 buffer_write(low_direction);
1516 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1522 static void comstick_reset(int trst, int srst)
1525 high_output &= ~nTRST;
1527 high_output |= nTRST;
1530 high_output &= ~nSRST;
1532 high_output |= nSRST;
1534 /* command "set data bits high byte" */
1536 buffer_write(high_output);
1537 buffer_write(high_direction);
1538 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1545 static void stm32stick_reset(int trst, int srst)
1548 high_output &= ~nTRST;
1550 high_output |= nTRST;
1553 low_output &= ~nSRST;
1555 low_output |= nSRST;
1557 /* command "set data bits low byte" */
1559 buffer_write(low_output);
1560 buffer_write(low_direction);
1562 /* command "set data bits high byte" */
1564 buffer_write(high_output);
1565 buffer_write(high_direction);
1566 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1573 static void sheevaplug_reset(int trst, int srst)
1576 high_output &= ~nTRST;
1578 high_output |= nTRST;
1581 high_output &= ~nSRSTnOE;
1583 high_output |= nSRSTnOE;
1585 /* command "set data bits high byte" */
1587 buffer_write(high_output);
1588 buffer_write(high_direction);
1589 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1596 static void redbee_reset(int trst, int srst)
1599 tap_set_state(TAP_RESET);
1600 high_output &= ~nTRST;
1601 } else if (trst == 0)
1602 high_output |= nTRST;
1605 high_output &= ~nSRST;
1607 high_output |= nSRST;
1609 /* command "set data bits low byte" */
1611 buffer_write(high_output);
1612 buffer_write(high_direction);
1613 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1614 "high_direction: 0x%2.2x", trst, srst, high_output,
1618 static void xds100v2_reset(int trst, int srst)
1621 tap_set_state(TAP_RESET);
1622 high_output &= ~nTRST;
1623 } else if (trst == 0)
1624 high_output |= nTRST;
1627 high_output |= nSRST;
1629 high_output &= ~nSRST;
1631 /* command "set data bits low byte" */
1633 buffer_write(high_output);
1634 buffer_write(high_direction);
1635 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1636 "high_direction: 0x%2.2x", trst, srst, high_output,
1640 static int ft2232_execute_runtest(struct jtag_command *cmd)
1644 int predicted_size = 0;
1647 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1648 cmd->cmd.runtest->num_cycles,
1649 tap_state_name(cmd->cmd.runtest->end_state));
1651 /* only send the maximum buffer size that FT2232C can handle */
1653 if (tap_get_state() != TAP_IDLE)
1654 predicted_size += 3;
1655 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1656 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1657 predicted_size += 3;
1658 if (tap_get_end_state() != TAP_IDLE)
1659 predicted_size += 3;
1660 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1661 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1662 retval = ERROR_JTAG_QUEUE_FAILED;
1666 if (tap_get_state() != TAP_IDLE) {
1667 move_to_state(TAP_IDLE);
1670 i = cmd->cmd.runtest->num_cycles;
1672 /* there are no state transitions in this code, so omit state tracking */
1674 /* command "Clock Data to TMS/CS Pin (no Read)" */
1678 buffer_write((i > 7) ? 6 : (i - 1));
1683 i -= (i > 7) ? 7 : i;
1684 /* LOG_DEBUG("added TMS scan (no read)"); */
1687 ft2232_end_state(cmd->cmd.runtest->end_state);
1689 if (tap_get_state() != tap_get_end_state())
1690 move_to_state(tap_get_end_state());
1693 DEBUG_JTAG_IO("runtest: %i, end in %s",
1694 cmd->cmd.runtest->num_cycles,
1695 tap_state_name(tap_get_end_state()));
1699 static int ft2232_execute_statemove(struct jtag_command *cmd)
1701 int predicted_size = 0;
1702 int retval = ERROR_OK;
1704 DEBUG_JTAG_IO("statemove end in %s",
1705 tap_state_name(cmd->cmd.statemove->end_state));
1707 /* only send the maximum buffer size that FT2232C can handle */
1709 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1710 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1711 retval = ERROR_JTAG_QUEUE_FAILED;
1715 ft2232_end_state(cmd->cmd.statemove->end_state);
1717 /* For TAP_RESET, ignore the current recorded state. It's often
1718 * wrong at server startup, and this transation is critical whenever
1721 if (tap_get_end_state() == TAP_RESET) {
1722 clock_tms(0x4b, 0xff, 5, 0);
1725 /* shortest-path move to desired end state */
1726 } else if (tap_get_state() != tap_get_end_state()) {
1727 move_to_state(tap_get_end_state());
1735 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1736 * (or SWD) state machine.
1738 static int ft2232_execute_tms(struct jtag_command *cmd)
1740 int retval = ERROR_OK;
1741 unsigned num_bits = cmd->cmd.tms->num_bits;
1742 const uint8_t *bits = cmd->cmd.tms->bits;
1745 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1747 /* only send the maximum buffer size that FT2232C can handle */
1748 count = 3 * DIV_ROUND_UP(num_bits, 4);
1749 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1750 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1751 retval = ERROR_JTAG_QUEUE_FAILED;
1757 /* Shift out in batches of at most 6 bits; there's a report of an
1758 * FT2232 bug in this area, where shifting exactly 7 bits can make
1759 * problems with TMS signaling for the last clock cycle:
1761 * http://developer.intra2net.com/mailarchive/html/
1762 * libftdi/2009/msg00292.html
1764 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1766 * Note that pathmoves in JTAG are not often seven bits, so that
1767 * isn't a particularly likely situation outside of "special"
1768 * signaling such as switching between JTAG and SWD modes.
1771 if (num_bits <= 6) {
1773 buffer_write(num_bits - 1);
1774 buffer_write(*bits & 0x3f);
1778 /* Yes, this is lazy ... we COULD shift out more data
1779 * bits per operation, but doing it in nybbles is easy
1783 buffer_write(*bits & 0xf);
1786 count = (num_bits > 4) ? 4 : num_bits;
1789 buffer_write(count - 1);
1790 buffer_write((*bits >> 4) & 0xf);
1800 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1802 int predicted_size = 0;
1803 int retval = ERROR_OK;
1805 tap_state_t *path = cmd->cmd.pathmove->path;
1806 int num_states = cmd->cmd.pathmove->num_states;
1808 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1809 tap_state_name(tap_get_state()),
1810 tap_state_name(path[num_states-1]));
1812 /* only send the maximum buffer size that FT2232C can handle */
1813 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1814 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1815 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1816 retval = ERROR_JTAG_QUEUE_FAILED;
1822 ft2232_add_pathmove(path, num_states);
1828 static int ft2232_execute_scan(struct jtag_command *cmd)
1831 int scan_size; /* size of IR or DR scan */
1832 int predicted_size = 0;
1833 int retval = ERROR_OK;
1835 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1837 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1839 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1841 predicted_size = ft2232_predict_scan_out(scan_size, type);
1842 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1843 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1844 /* unsent commands before this */
1845 if (first_unsent != cmd)
1846 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1847 retval = ERROR_JTAG_QUEUE_FAILED;
1849 /* current command */
1850 ft2232_end_state(cmd->cmd.scan->end_state);
1851 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1853 first_unsent = cmd->next;
1857 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1859 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1862 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1863 retval = ERROR_JTAG_QUEUE_FAILED;
1867 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1868 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1869 ft2232_end_state(cmd->cmd.scan->end_state);
1870 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1874 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1875 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1876 tap_state_name(tap_get_end_state()));
1881 static int ft2232_execute_reset(struct jtag_command *cmd)
1884 int predicted_size = 0;
1887 DEBUG_JTAG_IO("reset trst: %i srst %i",
1888 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1890 /* only send the maximum buffer size that FT2232C can handle */
1892 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1893 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1894 retval = ERROR_JTAG_QUEUE_FAILED;
1899 if ((cmd->cmd.reset->trst == 1) ||
1900 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1901 tap_set_state(TAP_RESET);
1903 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1906 DEBUG_JTAG_IO("trst: %i, srst: %i",
1907 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1911 static int ft2232_execute_sleep(struct jtag_command *cmd)
1916 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1918 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1919 retval = ERROR_JTAG_QUEUE_FAILED;
1920 first_unsent = cmd->next;
1921 jtag_sleep(cmd->cmd.sleep->us);
1922 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1924 tap_state_name(tap_get_state()));
1928 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1933 /* this is only allowed while in a stable state. A check for a stable
1934 * state was done in jtag_add_clocks()
1936 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1937 retval = ERROR_JTAG_QUEUE_FAILED;
1938 DEBUG_JTAG_IO("clocks %i while in %s",
1939 cmd->cmd.stableclocks->num_cycles,
1940 tap_state_name(tap_get_state()));
1944 static int ft2232_execute_command(struct jtag_command *cmd)
1948 switch (cmd->type) {
1950 retval = ft2232_execute_reset(cmd);
1953 retval = ft2232_execute_runtest(cmd);
1955 case JTAG_TLR_RESET:
1956 retval = ft2232_execute_statemove(cmd);
1959 retval = ft2232_execute_pathmove(cmd);
1962 retval = ft2232_execute_scan(cmd);
1965 retval = ft2232_execute_sleep(cmd);
1967 case JTAG_STABLECLOCKS:
1968 retval = ft2232_execute_stableclocks(cmd);
1971 retval = ft2232_execute_tms(cmd);
1974 LOG_ERROR("BUG: unknown JTAG command type encountered");
1975 retval = ERROR_JTAG_QUEUE_FAILED;
1981 static int ft2232_execute_queue(void)
1983 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1986 first_unsent = cmd; /* next command that has to be sent */
1989 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1990 * that wasn't handled by a caller-provided error handler
1994 ft2232_buffer_size = 0;
1995 ft2232_expect_read = 0;
1997 /* blink, if the current layout has that feature */
2002 /* fill the write buffer with the desired command */
2003 if (ft2232_execute_command(cmd) != ERROR_OK)
2004 retval = ERROR_JTAG_QUEUE_FAILED;
2005 /* Start reading input before FT2232 TX buffer fills up.
2006 * Sometimes this happens because we don't know the
2007 * length of the last command before we execute it. So
2008 * we simple inform the user.
2012 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2013 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2014 LOG_DEBUG("read buffer size looks too high %d/%d",
2016 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2017 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2018 retval = ERROR_JTAG_QUEUE_FAILED;
2023 if (require_send > 0)
2024 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2025 retval = ERROR_JTAG_QUEUE_FAILED;
2030 #if BUILD_FT2232_FTD2XX == 1
2031 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2035 char SerialNumber[16];
2036 char Description[64];
2037 DWORD openex_flags = 0;
2038 char *openex_string = NULL;
2039 uint8_t latency_timer;
2041 if (layout == NULL) {
2042 LOG_WARNING("No ft2232 layout specified'");
2043 return ERROR_JTAG_INIT_FAILED;
2046 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2047 layout->name, vid, pid);
2050 /* Add non-standard Vid/Pid to the linux driver */
2051 status = FT_SetVIDPID(vid, pid);
2052 if (status != FT_OK)
2053 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2057 if (ft2232_device_desc && ft2232_serial) {
2059 "can't open by device description and serial number, giving precedence to serial");
2060 ft2232_device_desc = NULL;
2063 if (ft2232_device_desc) {
2064 openex_string = ft2232_device_desc;
2065 openex_flags = FT_OPEN_BY_DESCRIPTION;
2066 } else if (ft2232_serial) {
2067 openex_string = ft2232_serial;
2068 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2070 LOG_ERROR("neither device description nor serial number specified");
2072 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2074 return ERROR_JTAG_INIT_FAILED;
2077 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2078 if (status != FT_OK) {
2079 /* under Win32, the FTD2XX driver appends an "A" to the end
2080 * of the description, if we tried by the desc, then
2081 * try by the alternate "A" description. */
2082 if (openex_string == ft2232_device_desc) {
2083 /* Try the alternate method. */
2084 openex_string = ft2232_device_desc_A;
2085 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2086 if (status == FT_OK) {
2087 /* yea, the "alternate" method worked! */
2089 /* drat, give the user a meaningfull message.
2090 * telling the use we tried *BOTH* methods. */
2091 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2093 ft2232_device_desc_A);
2098 if (status != FT_OK) {
2102 LOG_WARNING("unable to open ftdi device (trying more): %s",
2103 ftd2xx_status_string(status));
2105 return ERROR_JTAG_INIT_FAILED;
2107 LOG_ERROR("unable to open ftdi device: %s",
2108 ftd2xx_status_string(status));
2109 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2110 if (status == FT_OK) {
2111 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2114 for (i = 0; i < num_devices; i++)
2115 desc_array[i] = malloc(64);
2117 desc_array[num_devices] = NULL;
2119 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2121 if (status == FT_OK) {
2122 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2123 for (i = 0; i < num_devices; i++)
2124 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2127 for (i = 0; i < num_devices; i++)
2128 free(desc_array[i]);
2132 LOG_ERROR("ListDevices: NONE");
2133 return ERROR_JTAG_INIT_FAILED;
2136 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2137 if (status != FT_OK) {
2138 LOG_ERROR("unable to set latency timer: %s",
2139 ftd2xx_status_string(status));
2140 return ERROR_JTAG_INIT_FAILED;
2143 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2144 if (status != FT_OK) {
2145 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2146 * so ignore errors if using this driver version */
2149 status = FT_GetDriverVersion(ftdih, &dw_version);
2150 LOG_ERROR("unable to get latency timer: %s",
2151 ftd2xx_status_string(status));
2153 if ((status == FT_OK) && (dw_version == 0x10004)) {
2154 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2155 "with FT_GetLatencyTimer, upgrade to a newer version");
2157 return ERROR_JTAG_INIT_FAILED;
2159 LOG_DEBUG("current latency timer: %i", latency_timer);
2161 status = FT_SetTimeouts(ftdih, 5000, 5000);
2162 if (status != FT_OK) {
2163 LOG_ERROR("unable to set timeouts: %s",
2164 ftd2xx_status_string(status));
2165 return ERROR_JTAG_INIT_FAILED;
2168 status = FT_SetBitMode(ftdih, 0x0b, 2);
2169 if (status != FT_OK) {
2170 LOG_ERROR("unable to enable bit i/o mode: %s",
2171 ftd2xx_status_string(status));
2172 return ERROR_JTAG_INIT_FAILED;
2175 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2176 SerialNumber, Description, NULL);
2177 if (status != FT_OK) {
2178 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2179 ftd2xx_status_string(status));
2180 return ERROR_JTAG_INIT_FAILED;
2182 static const char *type_str[] = {
2183 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2185 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2186 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2187 ? ftdi_device : FT_DEVICE_UNKNOWN;
2188 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2189 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2190 LOG_INFO("SerialNumber: %s", SerialNumber);
2191 LOG_INFO("Description: %s", Description);
2197 static int ft2232_purge_ftd2xx(void)
2201 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2202 if (status != FT_OK) {
2203 LOG_ERROR("error purging ftd2xx device: %s",
2204 ftd2xx_status_string(status));
2205 return ERROR_JTAG_INIT_FAILED;
2211 #endif /* BUILD_FT2232_FTD2XX == 1 */
2213 #if BUILD_FT2232_LIBFTDI == 1
2214 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2216 uint8_t latency_timer;
2218 if (layout == NULL) {
2219 LOG_WARNING("No ft2232 layout specified'");
2220 return ERROR_JTAG_INIT_FAILED;
2223 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2224 layout->name, vid, pid);
2226 if (ftdi_init(&ftdic) < 0)
2227 return ERROR_JTAG_INIT_FAILED;
2229 /* default to INTERFACE_A */
2230 if (channel == INTERFACE_ANY)
2231 channel = INTERFACE_A;
2232 if (ftdi_set_interface(&ftdic, channel) < 0) {
2233 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2234 return ERROR_JTAG_INIT_FAILED;
2237 /* context, vendor id, product id */
2238 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2240 LOG_WARNING("unable to open ftdi device (trying more): %s",
2243 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2245 return ERROR_JTAG_INIT_FAILED;
2248 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2249 if (ftdi_usb_reset(&ftdic) < 0) {
2250 LOG_ERROR("unable to reset ftdi device");
2251 return ERROR_JTAG_INIT_FAILED;
2254 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2255 LOG_ERROR("unable to set latency timer");
2256 return ERROR_JTAG_INIT_FAILED;
2259 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2260 LOG_ERROR("unable to get latency timer");
2261 return ERROR_JTAG_INIT_FAILED;
2263 LOG_DEBUG("current latency timer: %i", latency_timer);
2265 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2267 ftdi_device = ftdic.type;
2268 static const char *type_str[] = {
2269 "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "Unknown"
2271 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2272 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2273 ? ftdi_device : no_of_known_types;
2274 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2278 static int ft2232_purge_libftdi(void)
2280 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2281 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2282 return ERROR_JTAG_INIT_FAILED;
2288 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2290 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2293 uint32_t bytes_written;
2295 buf[0] = 0x80; /* command "set data bits low byte" */
2296 buf[1] = value; /* value */
2297 buf[2] = direction; /* direction */
2299 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2301 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2302 LOG_ERROR("couldn't initialize data bits low byte");
2303 return ERROR_JTAG_INIT_FAILED;
2309 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2312 uint32_t bytes_written;
2314 buf[0] = 0x82; /* command "set data bits high byte" */
2315 buf[1] = value; /* value */
2316 buf[2] = direction; /* direction */
2318 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2320 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2321 LOG_ERROR("couldn't initialize data bits high byte");
2322 return ERROR_JTAG_INIT_FAILED;
2328 static int ft2232_init(void)
2332 uint32_t bytes_written;
2334 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2335 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2337 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2338 if (layout == NULL) {
2339 LOG_WARNING("No ft2232 layout specified'");
2340 return ERROR_JTAG_INIT_FAILED;
2343 for (int i = 0; 1; i++) {
2345 * "more indicates that there are more IDs to try, so we should
2346 * not print an error for an ID mismatch (but for anything
2349 * try_more indicates that the error code returned indicates an
2350 * ID mismatch (and nothing else) and that we should proceeed
2351 * with the next ID pair.
2353 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2356 #if BUILD_FT2232_FTD2XX == 1
2357 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2359 #elif BUILD_FT2232_LIBFTDI == 1
2360 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2361 more, &try_more, layout->channel);
2365 if (!more || !try_more)
2369 ft2232_buffer_size = 0;
2370 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2372 if (layout->init() != ERROR_OK)
2373 return ERROR_JTAG_INIT_FAILED;
2375 if (ft2232_device_is_highspeed()) {
2376 #ifndef BUILD_FT2232_HIGHSPEED
2377 #if BUILD_FT2232_FTD2XX == 1
2379 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2380 #elif BUILD_FT2232_LIBFTDI == 1
2382 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2385 /* make sure the legacy mode is disabled */
2386 if (ftx232h_clk_divide_by_5(false) != ERROR_OK)
2387 return ERROR_JTAG_INIT_FAILED;
2390 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2391 retval = ft2232_write(buf, 1, &bytes_written);
2392 if (retval != ERROR_OK) {
2393 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2394 return ERROR_JTAG_INIT_FAILED;
2397 #if BUILD_FT2232_FTD2XX == 1
2398 return ft2232_purge_ftd2xx();
2399 #elif BUILD_FT2232_LIBFTDI == 1
2400 return ft2232_purge_libftdi();
2406 /** Updates defaults for DBUS signals: the four JTAG signals
2407 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2409 static inline void ftx232_dbus_init(void)
2412 low_direction = 0x0b;
2415 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2416 * the four GPIOL signals. Initialization covers value and direction,
2417 * as customized for each layout.
2419 static int ftx232_dbus_write(void)
2421 enum reset_types jtag_reset_config = jtag_get_reset_config();
2422 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2423 low_direction &= ~nTRSTnOE; /* nTRST input */
2424 low_output &= ~nTRST; /* nTRST = 0 */
2426 low_direction |= nTRSTnOE; /* nTRST output */
2427 low_output |= nTRST; /* nTRST = 1 */
2430 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2431 low_direction |= nSRSTnOE; /* nSRST output */
2432 low_output |= nSRST; /* nSRST = 1 */
2434 low_direction &= ~nSRSTnOE; /* nSRST input */
2435 low_output &= ~nSRST; /* nSRST = 0 */
2438 /* initialize low byte for jtag */
2439 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2440 LOG_ERROR("couldn't initialize FT2232 DBUS");
2441 return ERROR_JTAG_INIT_FAILED;
2447 static int usbjtag_init(void)
2450 * NOTE: This is now _specific_ to the "usbjtag" layout.
2451 * Don't try cram any more layouts into this.
2460 return ftx232_dbus_write();
2463 static int lm3s811_jtag_init(void)
2467 /* There are multiple revisions of LM3S811 eval boards:
2468 * - Rev B (and older?) boards have no SWO trace support.
2469 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2470 * they should use the "luminary_icdi" layout instead.
2477 low_direction = 0x8b;
2479 return ftx232_dbus_write();
2482 static int icdi_jtag_init(void)
2486 /* Most Luminary eval boards support SWO trace output,
2487 * and should use this "luminary_icdi" layout.
2489 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2490 * to switch between JTAG and SWD, or switch the ft2232 UART
2491 * on the second MPSSE channel/interface (BDBUS)
2492 * between (i) the stellaris UART (on Luminary boards)
2493 * or (ii) SWO trace data (generic).
2495 * We come up in JTAG mode and may switch to SWD later (with
2496 * SWO/trace option if SWD is active).
2503 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2504 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2505 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2508 /* GPIOs on second channel/interface (UART) ... */
2509 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2510 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2511 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2516 nSRSTnOE = ICDI_SRST;
2518 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2519 low_output |= ICDI_JTAG_EN;
2520 low_output &= ~ICDI_DBG_ENn;
2522 return ftx232_dbus_write();
2525 static int signalyzer_init(void)
2533 return ftx232_dbus_write();
2536 static int axm0432_jtag_init(void)
2539 low_direction = 0x2b;
2541 /* initialize low byte for jtag */
2542 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2543 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2544 return ERROR_JTAG_INIT_FAILED;
2547 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2549 nTRSTnOE = 0x0; /* No output enable for TRST*/
2551 nSRSTnOE = 0x0; /* No output enable for SRST*/
2553 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2558 high_direction = 0x0c;
2560 enum reset_types jtag_reset_config = jtag_get_reset_config();
2561 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2562 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2564 high_output |= nTRST;
2566 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2567 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2569 high_output |= nSRST;
2571 /* initialize high byte for jtag */
2572 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2573 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2574 return ERROR_JTAG_INIT_FAILED;
2580 static int redbee_init(void)
2583 low_direction = 0x2b;
2585 /* initialize low byte for jtag */
2586 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2587 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2588 return ERROR_JTAG_INIT_FAILED;
2592 nTRSTnOE = 0x0; /* No output enable for TRST*/
2594 nSRSTnOE = 0x0; /* No output enable for SRST*/
2597 high_direction = 0x0c;
2599 enum reset_types jtag_reset_config = jtag_get_reset_config();
2600 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2601 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2603 high_output |= nTRST;
2605 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2606 LOG_ERROR("can't set nSRST to push-pull on redbee");
2608 high_output |= nSRST;
2610 /* initialize high byte for jtag */
2611 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2612 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2613 return ERROR_JTAG_INIT_FAILED;
2619 static int jtagkey_init(void)
2622 low_direction = 0x1b;
2624 /* initialize low byte for jtag */
2625 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2626 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2627 return ERROR_JTAG_INIT_FAILED;
2630 if (strcmp(layout->name, "jtagkey") == 0) {
2635 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2636 || (strcmp(layout->name, "oocdlink") == 0)) {
2642 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2647 high_direction = 0x0f;
2649 enum reset_types jtag_reset_config = jtag_get_reset_config();
2650 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2651 high_output |= nTRSTnOE;
2652 high_output &= ~nTRST;
2654 high_output &= ~nTRSTnOE;
2655 high_output |= nTRST;
2658 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2659 high_output &= ~nSRSTnOE;
2660 high_output |= nSRST;
2662 high_output |= nSRSTnOE;
2663 high_output &= ~nSRST;
2666 /* initialize high byte for jtag */
2667 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2668 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2669 return ERROR_JTAG_INIT_FAILED;
2675 static int olimex_jtag_init(void)
2678 low_direction = 0x1b;
2680 /* initialize low byte for jtag */
2681 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2682 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2683 return ERROR_JTAG_INIT_FAILED;
2689 nSRSTnOE = 0x00;/* no output enable for nSRST */
2692 high_direction = 0x0f;
2694 enum reset_types jtag_reset_config = jtag_get_reset_config();
2695 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2696 high_output |= nTRSTnOE;
2697 high_output &= ~nTRST;
2699 high_output &= ~nTRSTnOE;
2700 high_output |= nTRST;
2703 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2704 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2706 high_output &= ~nSRST;
2708 /* turn red LED on */
2709 high_output |= 0x08;
2711 /* initialize high byte for jtag */
2712 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2713 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2714 return ERROR_JTAG_INIT_FAILED;
2720 static int flyswatter_init(int rev)
2723 low_direction = 0x7b;
2725 if ((rev < 0) || (rev > 3)) {
2726 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2727 return ERROR_JTAG_INIT_FAILED;
2731 low_direction |= 1 << 7;
2733 /* initialize low byte for jtag */
2734 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2735 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2736 return ERROR_JTAG_INIT_FAILED;
2740 nTRSTnOE = 0x0; /* not output enable for nTRST */
2742 nSRSTnOE = 0x00; /* no output enable for nSRST */
2747 high_direction = 0x0c;
2749 high_direction = 0x01;
2751 /* turn red LED3 on, LED2 off */
2752 high_output |= 0x08;
2754 /* initialize high byte for jtag */
2755 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2756 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2757 return ERROR_JTAG_INIT_FAILED;
2763 static int flyswatter1_init(void)
2765 return flyswatter_init(1);
2768 static int flyswatter2_init(void)
2770 return flyswatter_init(2);
2773 static int minimodule_init(void)
2775 low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2777 low_direction = 0xfb; /* 0xfb; */
2779 /* initialize low byte for jtag */
2780 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2781 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2782 return ERROR_JTAG_INIT_FAILED;
2789 high_direction = 0x05;
2791 /* turn red LED3 on, LED2 off */
2792 /* high_output |= 0x08; */
2794 /* initialize high byte for jtag */
2795 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2796 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2797 return ERROR_JTAG_INIT_FAILED;
2803 static int turtle_init(void)
2806 low_direction = 0x5b;
2808 /* initialize low byte for jtag */
2809 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2810 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2811 return ERROR_JTAG_INIT_FAILED;
2817 high_direction = 0x0C;
2819 /* initialize high byte for jtag */
2820 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2821 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2822 return ERROR_JTAG_INIT_FAILED;
2828 static int comstick_init(void)
2831 low_direction = 0x0b;
2833 /* initialize low byte for jtag */
2834 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2835 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2836 return ERROR_JTAG_INIT_FAILED;
2840 nTRSTnOE = 0x00; /* no output enable for nTRST */
2842 nSRSTnOE = 0x00; /* no output enable for nSRST */
2845 high_direction = 0x03;
2847 /* initialize high byte for jtag */
2848 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2849 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2850 return ERROR_JTAG_INIT_FAILED;
2856 static int stm32stick_init(void)
2859 low_direction = 0x8b;
2861 /* initialize low byte for jtag */
2862 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2863 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2864 return ERROR_JTAG_INIT_FAILED;
2868 nTRSTnOE = 0x00; /* no output enable for nTRST */
2870 nSRSTnOE = 0x00; /* no output enable for nSRST */
2873 high_direction = 0x03;
2875 /* initialize high byte for jtag */
2876 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2877 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2878 return ERROR_JTAG_INIT_FAILED;
2884 static int sheevaplug_init(void)
2887 low_direction = 0x1b;
2889 /* initialize low byte for jtag */
2890 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2891 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2892 return ERROR_JTAG_INIT_FAILED;
2901 high_direction = 0x0f;
2903 /* nTRST is always push-pull */
2904 high_output &= ~nTRSTnOE;
2905 high_output |= nTRST;
2907 /* nSRST is always open-drain */
2908 high_output |= nSRSTnOE;
2909 high_output &= ~nSRST;
2911 /* initialize high byte for jtag */
2912 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2913 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2914 return ERROR_JTAG_INIT_FAILED;
2920 static int cortino_jtag_init(void)
2923 low_direction = 0x1b;
2925 /* initialize low byte for jtag */
2926 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2927 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2928 return ERROR_JTAG_INIT_FAILED;
2932 nTRSTnOE = 0x00; /* no output enable for nTRST */
2934 nSRSTnOE = 0x00; /* no output enable for nSRST */
2937 high_direction = 0x03;
2939 /* initialize high byte for jtag */
2940 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2941 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2942 return ERROR_JTAG_INIT_FAILED;
2948 static int lisa_l_init(void)
2958 high_direction = 0x18;
2960 /* initialize high byte for jtag */
2961 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2962 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2963 return ERROR_JTAG_INIT_FAILED;
2966 return ftx232_dbus_write();
2969 static int flossjtag_init(void)
2979 high_direction = 0x18;
2981 /* initialize high byte for jtag */
2982 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2983 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2984 return ERROR_JTAG_INIT_FAILED;
2987 return ftx232_dbus_write();
2991 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
2992 * the door for a number of different configurations
2994 * Known Implementations:
2995 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
2997 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
2998 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
2999 * * ACBUS3 to transition 0->1 (OE rising edge)
3000 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3001 * * ADBUS5/GPIOL1 = EMU_EN = 1
3002 * * ADBUS6/GPIOL2 = EMU0 = 0
3003 * * ACBUS4/SPARE0 = EMU1 = 0
3004 * * CPLD logic: Disable loopback
3005 * * ACBUS6/SPARE2 = LOOPBACK = 0
3007 #define XDS100_nEMU_EN (1<<5)
3008 #define XDS100_nEMU0 (1<<6)
3010 #define XDS100_PWR_RST (1<<3)
3011 #define XDS100_nEMU1 (1<<4)
3012 #define XDS100_LOOPBACK (1<<6)
3013 static int xds100v2_init(void)
3015 /* These are in the lower byte */
3019 /* These aren't actually used on 14 pin connectors
3020 * These are in the upper byte */
3024 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3025 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3027 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3028 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3029 return ERROR_JTAG_INIT_FAILED;
3033 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
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 put CPLD in to reset with 'xds100v2' layout");
3038 return ERROR_JTAG_INIT_FAILED;
3041 high_output |= XDS100_PWR_RST;
3043 /* initialize high byte for jtag */
3044 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3045 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3046 return ERROR_JTAG_INIT_FAILED;
3052 static void olimex_jtag_blink(void)
3054 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3055 * ACBUS3 is bit 3 of the GPIOH port
3057 high_output ^= 0x08;
3060 buffer_write(high_output);
3061 buffer_write(high_direction);
3064 static void flyswatter_jtag_blink(unsigned char led)
3067 buffer_write(high_output ^ led);
3068 buffer_write(high_direction);
3071 static void flyswatter1_jtag_blink(void)
3074 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3076 flyswatter_jtag_blink(0xc);
3079 static void flyswatter2_jtag_blink(void)
3082 * Flyswatter2 only has one LED connected to ACBUS2
3084 flyswatter_jtag_blink(0x4);
3087 static void turtle_jtag_blink(void)
3090 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3092 if (high_output & 0x08)
3098 buffer_write(high_output);
3099 buffer_write(high_direction);
3102 static void lisa_l_blink(void)
3105 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3107 if (high_output & 0x10)
3113 buffer_write(high_output);
3114 buffer_write(high_direction);
3117 static void flossjtag_blink(void)
3120 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3122 if (high_output & 0x10)
3128 buffer_write(high_output);
3129 buffer_write(high_direction);
3132 static int ft2232_quit(void)
3134 #if BUILD_FT2232_FTD2XX == 1
3137 status = FT_Close(ftdih);
3138 #elif BUILD_FT2232_LIBFTDI == 1
3139 ftdi_usb_close(&ftdic);
3141 ftdi_deinit(&ftdic);
3144 free(ft2232_buffer);
3145 ft2232_buffer = NULL;
3150 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3154 if (CMD_ARGC == 1) {
3155 ft2232_device_desc = strdup(CMD_ARGV[0]);
3156 cp = strchr(ft2232_device_desc, 0);
3157 /* under Win32, the FTD2XX driver appends an "A" to the end
3158 * of the description, this examines the given desc
3159 * and creates the 'missing' _A or non_A variable. */
3160 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3161 /* it was, so make this the "A" version. */
3162 ft2232_device_desc_A = ft2232_device_desc;
3163 /* and *CREATE* the non-A version. */
3164 strcpy(buf, ft2232_device_desc);
3165 cp = strchr(buf, 0);
3167 ft2232_device_desc = strdup(buf);
3169 /* <space > A not defined
3171 sprintf(buf, "%s A", ft2232_device_desc);
3172 ft2232_device_desc_A = strdup(buf);
3175 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3180 COMMAND_HANDLER(ft2232_handle_serial_command)
3183 ft2232_serial = strdup(CMD_ARGV[0]);
3185 return ERROR_COMMAND_SYNTAX_ERROR;
3190 COMMAND_HANDLER(ft2232_handle_layout_command)
3193 return ERROR_COMMAND_SYNTAX_ERROR;
3196 LOG_ERROR("already specified ft2232_layout %s",
3198 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3203 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3204 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3210 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3214 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3216 if (CMD_ARGC > MAX_USB_IDS * 2) {
3217 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3218 "(maximum is %d pairs)", MAX_USB_IDS);
3219 CMD_ARGC = MAX_USB_IDS * 2;
3221 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3222 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3224 return ERROR_COMMAND_SYNTAX_ERROR;
3225 /* remove the incomplete trailing id */
3230 for (i = 0; i < CMD_ARGC; i += 2) {
3231 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3232 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3236 * Explicitly terminate, in case there are multiples instances of
3239 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3244 COMMAND_HANDLER(ft2232_handle_latency_command)
3247 ft2232_latency = atoi(CMD_ARGV[0]);
3249 return ERROR_COMMAND_SYNTAX_ERROR;
3254 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3258 /* 7 bits of either ones or zeros. */
3259 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3261 while (num_cycles > 0) {
3262 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3263 * at most 7 bits per invocation. Here we invoke it potentially
3266 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3268 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3269 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3270 retval = ERROR_JTAG_QUEUE_FAILED;
3275 /* there are no state transitions in this code, so omit state tracking */
3277 /* command "Clock Data to TMS/CS Pin (no Read)" */
3281 buffer_write(bitcount_per_command - 1);
3283 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3288 num_cycles -= bitcount_per_command;
3294 /* ---------------------------------------------------------------------
3295 * Support for IceBear JTAG adapter from Section5:
3296 * http://section5.ch/icebear
3298 * Author: Sten, debian@sansys-electronic.com
3301 /* Icebear pin layout
3303 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3304 * GND GND | 4 3| n.c.
3305 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3306 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3307 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3308 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3309 * ADBUS2 TDO |14 13| GND GND
3311 * ADBUS0 O L TCK ACBUS0 GND
3312 * ADBUS1 O L TDI ACBUS1 GND
3313 * ADBUS2 I TDO ACBUS2 n.c.
3314 * ADBUS3 O H TMS ACBUS3 n.c.
3320 static int icebear_jtag_init(void)
3322 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3323 low_output = 0x08; /* high: TMS; low: TCK TDI */
3327 enum reset_types jtag_reset_config = jtag_get_reset_config();
3328 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3329 low_direction &= ~nTRST; /* nTRST high impedance */
3331 low_direction |= nTRST;
3332 low_output |= nTRST;
3335 low_direction |= nSRST;
3336 low_output |= nSRST;
3338 /* initialize low byte for jtag */
3339 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3340 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3341 return ERROR_JTAG_INIT_FAILED;
3345 high_direction = 0x00;
3347 /* initialize high byte for jtag */
3348 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3349 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3350 return ERROR_JTAG_INIT_FAILED;
3356 static void icebear_jtag_reset(int trst, int srst)
3359 low_direction |= nTRST;
3360 low_output &= ~nTRST;
3361 } else if (trst == 0) {
3362 enum reset_types jtag_reset_config = jtag_get_reset_config();
3363 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3364 low_direction &= ~nTRST;
3366 low_output |= nTRST;
3370 low_output &= ~nSRST;
3372 low_output |= nSRST;
3374 /* command "set data bits low byte" */
3376 buffer_write(low_output);
3377 buffer_write(low_direction);
3379 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3386 /* ---------------------------------------------------------------------
3387 * Support for Signalyzer H2 and Signalyzer H4
3388 * JTAG adapter from Xverve Technologies Inc.
3389 * http://www.signalyzer.com or http://www.xverve.com
3391 * Author: Oleg Seiljus, oleg@signalyzer.com
3393 static unsigned char signalyzer_h_side;
3394 static unsigned int signalyzer_h_adapter_type;
3396 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3398 #if BUILD_FT2232_FTD2XX == 1
3399 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3402 #define SIGNALYZER_COMMAND_ADDR 128
3403 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3405 #define SIGNALYZER_COMMAND_VERSION 0x41
3406 #define SIGNALYZER_COMMAND_RESET 0x42
3407 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3408 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3409 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3410 #define SIGNALYZER_COMMAND_LED_SET 0x53
3411 #define SIGNALYZER_COMMAND_ADC 0x54
3412 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3413 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3414 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3415 #define SIGNALYZER_COMMAND_I2C 0x58
3417 #define SIGNALYZER_CHAN_A 1
3418 #define SIGNALYZER_CHAN_B 2
3419 /* LEDS use channel C */
3420 #define SIGNALYZER_CHAN_C 4
3422 #define SIGNALYZER_LED_GREEN 1
3423 #define SIGNALYZER_LED_RED 2
3425 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3426 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3427 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3428 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3429 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3432 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3434 #if BUILD_FT2232_FTD2XX == 1
3435 return FT_WriteEE(ftdih, address, value);
3436 #elif BUILD_FT2232_LIBFTDI == 1
3441 #if BUILD_FT2232_FTD2XX == 1
3442 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3444 return FT_ReadEE(ftdih, address, value);
3448 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3449 int on_time_ms, int off_time_ms, unsigned char cycles)
3451 unsigned char on_time;
3452 unsigned char off_time;
3454 if (on_time_ms < 0xFFFF)
3455 on_time = (unsigned char)(on_time_ms / 62);
3459 off_time = (unsigned char)(off_time_ms / 62);
3461 #if BUILD_FT2232_FTD2XX == 1
3464 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3465 ((uint32_t)(channel << 8) | led));
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 + 1),
3473 ((uint32_t)(on_time << 8) | off_time));
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_DATA_BUFFER_ADDR + 2),
3481 ((uint32_t)cycles));
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;
3488 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3489 SIGNALYZER_COMMAND_LED_SET);
3490 if (status != FT_OK) {
3491 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3492 ftd2xx_status_string(status));
3493 return ERROR_JTAG_DEVICE_ERROR;
3497 #elif BUILD_FT2232_LIBFTDI == 1
3500 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3501 ((uint32_t)(channel << 8) | led));
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 + 1),
3509 ((uint32_t)(on_time << 8) | off_time));
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_DATA_BUFFER_ADDR + 2),
3519 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3520 ftdi_get_error_string(&ftdic));
3521 return ERROR_JTAG_DEVICE_ERROR;
3524 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3525 SIGNALYZER_COMMAND_LED_SET);
3527 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3528 ftdi_get_error_string(&ftdic));
3529 return ERROR_JTAG_DEVICE_ERROR;
3536 static int signalyzer_h_init(void)
3538 #if BUILD_FT2232_FTD2XX == 1
3545 uint16_t read_buf[12] = { 0 };
3547 /* turn on center green led */
3548 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3549 0xFFFF, 0x00, 0x00);
3551 /* determine what channel config wants to open
3552 * TODO: change me... current implementation is made to work
3553 * with openocd description parsing.
3555 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3558 signalyzer_h_side = *(end_of_desc - 1);
3559 if (signalyzer_h_side == 'B')
3560 signalyzer_h_side = SIGNALYZER_CHAN_B;
3562 signalyzer_h_side = SIGNALYZER_CHAN_A;
3564 LOG_ERROR("No Channel was specified");
3568 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3571 #if BUILD_FT2232_FTD2XX == 1
3572 /* read signalyzer versionining information */
3573 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3574 SIGNALYZER_COMMAND_VERSION);
3575 if (status != FT_OK) {
3576 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3577 ftd2xx_status_string(status));
3578 return ERROR_JTAG_DEVICE_ERROR;
3581 for (i = 0; i < 10; i++) {
3582 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3584 if (status != FT_OK) {
3585 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3586 ftd2xx_status_string(status));
3587 return ERROR_JTAG_DEVICE_ERROR;
3591 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3592 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3593 read_buf[4], read_buf[5], read_buf[6]);
3595 /* set gpio register */
3596 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3597 (uint32_t)(signalyzer_h_side << 8));
3598 if (status != FT_OK) {
3599 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3600 ftd2xx_status_string(status));
3601 return ERROR_JTAG_DEVICE_ERROR;
3604 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
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 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3612 SIGNALYZER_COMMAND_GPIO_STATE);
3613 if (status != FT_OK) {
3614 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3615 ftd2xx_status_string(status));
3616 return ERROR_JTAG_DEVICE_ERROR;
3619 /* read adapter type information */
3620 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3621 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
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 + 1), 0xA000);
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(
3637 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
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;
3644 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3645 SIGNALYZER_COMMAND_I2C);
3646 if (status != FT_OK) {
3647 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3648 ftd2xx_status_string(status));
3649 return ERROR_JTAG_DEVICE_ERROR;
3654 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3655 if (status != FT_OK) {
3656 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3657 ftd2xx_status_string(status));
3658 return ERROR_JTAG_DEVICE_ERROR;
3661 if (read_buf[0] != 0x0498)
3662 signalyzer_h_adapter_type = 0x0000;
3664 for (i = 0; i < 4; i++) {
3665 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3666 if (status != FT_OK) {
3667 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3668 ftd2xx_status_string(status));
3669 return ERROR_JTAG_DEVICE_ERROR;
3673 signalyzer_h_adapter_type = read_buf[0];
3676 #elif BUILD_FT2232_LIBFTDI == 1
3677 /* currently libftdi does not allow reading individual eeprom
3678 * locations, therefore adapter type cannot be detected.
3679 * override with most common type
3681 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3684 enum reset_types jtag_reset_config = jtag_get_reset_config();
3686 /* ADAPTOR: EM_LT16_A */
3687 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3688 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3689 "detected. (HW: %2x).", (read_buf[1] >> 8));
3697 low_direction = 0x1b;
3700 high_direction = 0x0;
3702 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3703 low_direction &= ~nTRSTnOE; /* nTRST input */
3704 low_output &= ~nTRST; /* nTRST = 0 */
3706 low_direction |= nTRSTnOE; /* nTRST output */
3707 low_output |= nTRST; /* nTRST = 1 */
3710 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3711 low_direction |= nSRSTnOE; /* nSRST output */
3712 low_output |= nSRST; /* nSRST = 1 */
3714 low_direction &= ~nSRSTnOE; /* nSRST input */
3715 low_output &= ~nSRST; /* nSRST = 0 */
3718 #if BUILD_FT2232_FTD2XX == 1
3719 /* enable power to the module */
3720 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3721 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
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 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3729 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3730 if (status != FT_OK) {
3731 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3732 ftd2xx_status_string(status));
3733 return ERROR_JTAG_DEVICE_ERROR;
3736 /* set gpio mode register */
3737 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3738 (uint32_t)(signalyzer_h_side << 8));
3739 if (status != FT_OK) {
3740 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3741 ftd2xx_status_string(status));
3742 return ERROR_JTAG_DEVICE_ERROR;
3745 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3746 if (status != FT_OK) {
3747 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3748 ftd2xx_status_string(status));
3749 return ERROR_JTAG_DEVICE_ERROR;
3752 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3753 if (status != FT_OK) {
3754 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3755 ftd2xx_status_string(status));
3756 return ERROR_JTAG_DEVICE_ERROR;
3759 /* set gpio register */
3760 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3761 (uint32_t)(signalyzer_h_side << 8));
3762 if (status != FT_OK) {
3763 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3764 ftd2xx_status_string(status));
3765 return ERROR_JTAG_DEVICE_ERROR;
3768 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040);
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;
3775 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3776 SIGNALYZER_COMMAND_GPIO_STATE);
3777 if (status != FT_OK) {
3778 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3779 ftd2xx_status_string(status));
3780 return ERROR_JTAG_DEVICE_ERROR;
3784 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3785 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3786 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3787 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3788 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3789 if (signalyzer_h_adapter_type
3790 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3791 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3792 "detected. (HW: %2x).", (read_buf[1] >> 8));
3793 else if (signalyzer_h_adapter_type
3794 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3795 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3796 "(ARM JTAG with PSU) detected. (HW: %2x).",
3797 (read_buf[1] >> 8));
3798 else if (signalyzer_h_adapter_type
3799 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3800 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3801 "detected. (HW: %2x).", (read_buf[1] >> 8));
3802 else if (signalyzer_h_adapter_type
3803 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3804 LOG_INFO("Signalyzer: EM-JTAG-P "
3805 "(Generic JTAG with PSU) detected. (HW: %2x).",
3806 (read_buf[1] >> 8));
3814 low_direction = 0x1b;
3817 high_direction = 0x1f;
3819 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3820 high_output |= nTRSTnOE;
3821 high_output &= ~nTRST;
3823 high_output &= ~nTRSTnOE;
3824 high_output |= nTRST;
3827 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3828 high_output &= ~nSRSTnOE;
3829 high_output |= nSRST;
3831 high_output |= nSRSTnOE;
3832 high_output &= ~nSRST;
3835 #if BUILD_FT2232_FTD2XX == 1
3836 /* enable power to the module */
3837 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3838 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
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 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3846 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3847 if (status != FT_OK) {
3848 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3849 ftd2xx_status_string(status));
3850 return ERROR_JTAG_DEVICE_ERROR;
3853 /* set gpio mode register (IO_16 and IO_17 set as analog
3854 * inputs, other is gpio)
3856 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3857 (uint32_t)(signalyzer_h_side << 8));
3858 if (status != FT_OK) {
3859 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3860 ftd2xx_status_string(status));
3861 return ERROR_JTAG_DEVICE_ERROR;
3864 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060);
3865 if (status != FT_OK) {
3866 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3867 ftd2xx_status_string(status));
3868 return ERROR_JTAG_DEVICE_ERROR;
3871 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3872 if (status != FT_OK) {
3873 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3874 ftd2xx_status_string(status));
3875 return ERROR_JTAG_DEVICE_ERROR;
3878 /* set gpio register (all inputs, for -P modules,
3879 * PSU will be turned off)
3881 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3882 (uint32_t)(signalyzer_h_side << 8));
3883 if (status != FT_OK) {
3884 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3885 ftd2xx_status_string(status));
3886 return ERROR_JTAG_DEVICE_ERROR;
3889 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3890 if (status != FT_OK) {
3891 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3892 ftd2xx_status_string(status));
3893 return ERROR_JTAG_DEVICE_ERROR;
3896 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
3897 if (status != FT_OK) {
3898 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3899 ftd2xx_status_string(status));
3900 return ERROR_JTAG_DEVICE_ERROR;
3903 } else if (signalyzer_h_adapter_type == 0x0000) {
3904 LOG_INFO("Signalyzer: No external modules were detected.");
3912 low_direction = 0x1b;
3915 high_direction = 0x0;
3917 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3918 low_direction &= ~nTRSTnOE; /* nTRST input */
3919 low_output &= ~nTRST; /* nTRST = 0 */
3921 low_direction |= nTRSTnOE; /* nTRST output */
3922 low_output |= nTRST; /* nTRST = 1 */
3925 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3926 low_direction |= nSRSTnOE; /* nSRST output */
3927 low_output |= nSRST; /* nSRST = 1 */
3929 low_direction &= ~nSRSTnOE; /* nSRST input */
3930 low_output &= ~nSRST; /* nSRST = 0 */
3933 LOG_ERROR("Unknown module type is detected: %.4x",
3934 signalyzer_h_adapter_type);
3935 return ERROR_JTAG_DEVICE_ERROR;
3938 /* initialize low byte of controller for jtag operation */
3939 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3940 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3941 return ERROR_JTAG_INIT_FAILED;
3944 #if BUILD_FT2232_FTD2XX == 1
3945 if (ftdi_device == FT_DEVICE_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;
3952 #elif BUILD_FT2232_LIBFTDI == 1
3953 if (ftdi_device == TYPE_2232H) {
3954 /* initialize high byte of controller for jtag operation */
3955 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3956 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3957 return ERROR_JTAG_INIT_FAILED;
3964 static void signalyzer_h_reset(int trst, int srst)
3966 enum reset_types jtag_reset_config = jtag_get_reset_config();
3968 /* ADAPTOR: EM_LT16_A */
3969 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3971 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3972 /* switch to output pin (output is low) */
3973 low_direction |= nTRSTnOE;
3975 /* switch output low */
3976 low_output &= ~nTRST;
3977 } else if (trst == 0) {
3978 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3979 /* switch to input pin (high-Z + internal
3980 * and external pullup) */
3981 low_direction &= ~nTRSTnOE;
3983 /* switch output high */
3984 low_output |= nTRST;
3988 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3989 /* switch output low */
3990 low_output &= ~nSRST;
3992 /* switch to output pin (output is low) */
3993 low_direction |= nSRSTnOE;
3994 } else if (srst == 0) {
3995 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3996 /* switch output high */
3997 low_output |= nSRST;
3999 /* switch to input pin (high-Z) */
4000 low_direction &= ~nSRSTnOE;
4003 /* command "set data bits low byte" */
4005 buffer_write(low_output);
4006 buffer_write(low_direction);
4007 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4008 "low_direction: 0x%2.2x",
4009 trst, srst, low_output, low_direction);
4011 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4012 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4013 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4014 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4015 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4017 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4018 high_output &= ~nTRSTnOE;
4020 high_output &= ~nTRST;
4021 } else if (trst == 0) {
4022 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4023 high_output |= nTRSTnOE;
4025 high_output |= nTRST;
4029 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4030 high_output &= ~nSRST;
4032 high_output &= ~nSRSTnOE;
4033 } else if (srst == 0) {
4034 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4035 high_output |= nSRST;
4037 high_output |= nSRSTnOE;
4040 /* command "set data bits high byte" */
4042 buffer_write(high_output);
4043 buffer_write(high_direction);
4044 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4045 "high_direction: 0x%2.2x",
4046 trst, srst, high_output, high_direction);
4047 } else if (signalyzer_h_adapter_type == 0x0000) {
4049 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4050 /* switch to output pin (output is low) */
4051 low_direction |= nTRSTnOE;
4053 /* switch output low */
4054 low_output &= ~nTRST;
4055 } else if (trst == 0) {
4056 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4057 /* switch to input pin (high-Z + internal
4058 * and external pullup) */
4059 low_direction &= ~nTRSTnOE;
4061 /* switch output high */
4062 low_output |= nTRST;
4066 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4067 /* switch output low */
4068 low_output &= ~nSRST;
4070 /* switch to output pin (output is low) */
4071 low_direction |= nSRSTnOE;
4072 } else if (srst == 0) {
4073 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4074 /* switch output high */
4075 low_output |= nSRST;
4077 /* switch to input pin (high-Z) */
4078 low_direction &= ~nSRSTnOE;
4081 /* command "set data bits low byte" */
4083 buffer_write(low_output);
4084 buffer_write(low_direction);
4085 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4086 "low_direction: 0x%2.2x",
4087 trst, srst, low_output, low_direction);
4091 static void signalyzer_h_blink(void)
4093 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4096 /********************************************************************
4097 * Support for KT-LINK
4098 * JTAG adapter from KRISTECH
4099 * http://www.kristech.eu
4100 *******************************************************************/
4101 static int ktlink_init(void)
4103 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4105 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4106 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4108 /* initialize low byte for jtag */
4109 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4110 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4111 return ERROR_JTAG_INIT_FAILED;
4119 high_output = 0x80; /* turn LED on */
4120 high_direction = 0xFF; /* all outputs */
4122 enum reset_types jtag_reset_config = jtag_get_reset_config();
4124 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4125 high_output |= nTRSTnOE;
4126 high_output &= ~nTRST;
4128 high_output &= ~nTRSTnOE;
4129 high_output |= nTRST;
4132 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4133 high_output &= ~nSRSTnOE;
4134 high_output |= nSRST;
4136 high_output |= nSRSTnOE;
4137 high_output &= ~nSRST;
4140 /* initialize high byte for jtag */
4141 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4142 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4143 return ERROR_JTAG_INIT_FAILED;
4149 static void ktlink_reset(int trst, int srst)
4151 enum reset_types jtag_reset_config = jtag_get_reset_config();
4154 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4155 high_output &= ~nTRSTnOE;
4157 high_output &= ~nTRST;
4158 } else if (trst == 0) {
4159 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4160 high_output |= nTRSTnOE;
4162 high_output |= nTRST;
4166 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4167 high_output &= ~nSRST;
4169 high_output &= ~nSRSTnOE;
4170 } else if (srst == 0) {
4171 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4172 high_output |= nSRST;
4174 high_output |= nSRSTnOE;
4177 buffer_write(0x82); /* command "set data bits high byte" */
4178 buffer_write(high_output);
4179 buffer_write(high_direction);
4180 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4187 static void ktlink_blink(void)
4189 /* LED connected to ACBUS7 */
4190 high_output ^= 0x80;
4192 buffer_write(0x82); /* command "set data bits high byte" */
4193 buffer_write(high_output);
4194 buffer_write(high_direction);
4197 /********************************************************************
4198 * Support for Digilent HS-1
4199 * JTAG adapter from Digilent
4200 * http://www.digilent.com
4201 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4202 *******************************************************************/
4204 static int digilent_hs1_init(void)
4206 /* the adapter only supports the base JTAG signals, no nTRST
4209 low_direction = 0x8b;
4211 /* initialize low byte for jtag */
4212 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4213 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4214 return ERROR_JTAG_INIT_FAILED;
4219 static void digilent_hs1_reset(int trst, int srst)
4221 /* Dummy function, no reset signals supported. */
4224 static const struct command_registration ft2232_command_handlers[] = {
4226 .name = "ft2232_device_desc",
4227 .handler = &ft2232_handle_device_desc_command,
4228 .mode = COMMAND_CONFIG,
4229 .help = "set the USB device description of the FTDI FT2232 device",
4230 .usage = "description_string",
4233 .name = "ft2232_serial",
4234 .handler = &ft2232_handle_serial_command,
4235 .mode = COMMAND_CONFIG,
4236 .help = "set the serial number of the FTDI FT2232 device",
4237 .usage = "serial_string",
4240 .name = "ft2232_layout",
4241 .handler = &ft2232_handle_layout_command,
4242 .mode = COMMAND_CONFIG,
4243 .help = "set the layout of the FT2232 GPIO signals used "
4244 "to control output-enables and reset signals",
4245 .usage = "layout_name",
4248 .name = "ft2232_vid_pid",
4249 .handler = &ft2232_handle_vid_pid_command,
4250 .mode = COMMAND_CONFIG,
4251 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4252 .usage = "(vid pid)* ",
4255 .name = "ft2232_latency",
4256 .handler = &ft2232_handle_latency_command,
4257 .mode = COMMAND_CONFIG,
4258 .help = "set the FT2232 latency timer to a new value",
4261 COMMAND_REGISTRATION_DONE
4264 struct jtag_interface ft2232_interface = {
4266 .supported = DEBUG_CAP_TMS_SEQ,
4267 .commands = ft2232_command_handlers,
4268 .transports = jtag_only,
4270 .init = ft2232_init,
4271 .quit = ft2232_quit,
4272 .speed = ft2232_speed,
4273 .speed_div = ft2232_speed_div,
4275 .execute_queue = ft2232_execute_queue,