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, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
30 * JTAG adapters based on the FT2232 full and high speed USB parts are
31 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
32 * are discrete, but development boards may integrate them as alternatives
33 * to more capable (and expensive) third party JTAG pods.
35 * JTAG uses only one of the two communications channels ("MPSSE engines")
36 * on these devices. Adapters based on FT4232 parts have four ports/channels
37 * (A/B/C/D), instead of just two (A/B).
39 * Especially on development boards integrating one of these chips (as
40 * opposed to discrete pods/dongles), the additional channels can be used
41 * for a variety of purposes, but OpenOCD only uses one channel at a time.
43 * - As a USB-to-serial adapter for the target's console UART ...
44 * which may be able to support ROM boot loaders that load initial
45 * firmware images to flash (or SRAM).
47 * - On systems which support ARM's SWD in addition to JTAG, or instead
48 * of it, that second port can be used for reading SWV/SWO trace data.
50 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
52 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
53 * request/response interactions involve round trips over the USB link.
54 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
55 * can for example poll quickly for a status change (usually taking on the
56 * order of microseconds not milliseconds) before beginning a queued
57 * transaction which require the previous one to have completed.
59 * There are dozens of adapters of this type, differing in details which
60 * this driver needs to understand. Those "layout" details are required
61 * as part of FT2232 driver configuration.
63 * This code uses information contained in the MPSSE specification which was
65 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
66 * Hereafter this is called the "MPSSE Spec".
68 * The datasheet for the ftdichip.com's FT2232D part is here:
69 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
71 * Also note the issue with code 0x4b (clock data to TMS) noted in
72 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
73 * which can affect longer JTAG state paths.
80 /* project specific includes */
81 #include <jtag/interface.h>
82 #include <transport/transport.h>
83 #include <helper/time_support.h>
91 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
92 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
93 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
94 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
97 /* FT2232 access library includes */
98 #if BUILD_FT2232_FTD2XX == 1
100 #include "ftd2xx_common.h"
102 enum ftdi_interface {
110 #elif BUILD_FT2232_LIBFTDI == 1
114 /* max TCK for the high speed devices 30000 kHz */
115 #define FTDI_x232H_MAX_TCK 30000
116 /* max TCK for the full speed devices 6000 kHz */
117 #define FTDI_2232C_MAX_TCK 6000
118 /* this speed value tells that RTCK is requested */
119 #define RTCK_SPEED -1
122 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
123 * errors with a retry count of 100. Increasing it solves the problem for me.
126 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
127 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
130 #define LIBFTDI_READ_RETRY_COUNT 2000
132 #ifndef BUILD_FT2232_HIGHSPEED
133 #if BUILD_FT2232_FTD2XX == 1
134 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H, FT_DEVICE_232H };
135 #elif BUILD_FT2232_LIBFTDI == 1
136 enum ftdi_chip_type { TYPE_2232H = 4, TYPE_4232H = 5, TYPE_232H = 6 };
141 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
142 * stable state. Calling code must ensure that current state is stable,
143 * that verification is not done in here.
145 * @param num_cycles The number of clocks cycles to send.
146 * @param cmd The command to send.
148 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
150 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
152 static char *ft2232_device_desc_A;
153 static char *ft2232_device_desc;
154 static char *ft2232_serial;
155 static uint8_t ft2232_latency = 2;
156 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
157 static int ft2232_channel = INTERFACE_ANY;
159 #define MAX_USB_IDS 8
160 /* vid = pid = 0 marks the end of the list */
161 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
162 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
164 struct ft2232_layout {
167 void (*reset)(int trst, int srst);
172 /* init procedures for supported layouts */
173 static int usbjtag_init(void);
174 static int jtagkey_init(void);
175 static int lm3s811_jtag_init(void);
176 static int icdi_jtag_init(void);
177 static int olimex_jtag_init(void);
178 static int flyswatter1_init(void);
179 static int flyswatter2_init(void);
180 static int minimodule_init(void);
181 static int turtle_init(void);
182 static int comstick_init(void);
183 static int stm32stick_init(void);
184 static int axm0432_jtag_init(void);
185 static int sheevaplug_init(void);
186 static int icebear_jtag_init(void);
187 static int cortino_jtag_init(void);
188 static int signalyzer_init(void);
189 static int signalyzer_h_init(void);
190 static int ktlink_init(void);
191 static int redbee_init(void);
192 static int lisa_l_init(void);
193 static int flossjtag_init(void);
194 static int xds100v2_init(void);
195 static int digilent_hs1_init(void);
197 /* reset procedures for supported layouts */
198 static void ftx23_reset(int trst, int srst);
199 static void jtagkey_reset(int trst, int srst);
200 static void olimex_jtag_reset(int trst, int srst);
201 static void flyswatter1_reset(int trst, int srst);
202 static void flyswatter2_reset(int trst, int srst);
203 static void minimodule_reset(int trst, int srst);
204 static void turtle_reset(int trst, int srst);
205 static void comstick_reset(int trst, int srst);
206 static void stm32stick_reset(int trst, int srst);
207 static void axm0432_jtag_reset(int trst, int srst);
208 static void sheevaplug_reset(int trst, int srst);
209 static void icebear_jtag_reset(int trst, int srst);
210 static void signalyzer_h_reset(int trst, int srst);
211 static void ktlink_reset(int trst, int srst);
212 static void redbee_reset(int trst, int srst);
213 static void xds100v2_reset(int trst, int srst);
214 static void digilent_hs1_reset(int trst, int srst);
216 /* blink procedures for layouts that support a blinking led */
217 static void olimex_jtag_blink(void);
218 static void flyswatter1_jtag_blink(void);
219 static void flyswatter2_jtag_blink(void);
220 static void turtle_jtag_blink(void);
221 static void signalyzer_h_blink(void);
222 static void ktlink_blink(void);
223 static void lisa_l_blink(void);
224 static void flossjtag_blink(void);
226 /* common transport support options */
228 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
230 static const struct ft2232_layout ft2232_layouts[] = {
232 .init = usbjtag_init,
233 .reset = ftx23_reset,
236 .init = jtagkey_init,
237 .reset = jtagkey_reset,
239 { .name = "jtagkey_prototype_v1",
240 .init = jtagkey_init,
241 .reset = jtagkey_reset,
243 { .name = "oocdlink",
244 .init = jtagkey_init,
245 .reset = jtagkey_reset,
247 { .name = "signalyzer",
248 .init = signalyzer_init,
249 .reset = ftx23_reset,
251 { .name = "evb_lm3s811",
252 .init = lm3s811_jtag_init,
253 .reset = ftx23_reset,
255 { .name = "luminary_icdi",
256 .init = icdi_jtag_init,
257 .reset = ftx23_reset,
259 { .name = "olimex-jtag",
260 .init = olimex_jtag_init,
261 .reset = olimex_jtag_reset,
262 .blink = olimex_jtag_blink
264 { .name = "flyswatter",
265 .init = flyswatter1_init,
266 .reset = flyswatter1_reset,
267 .blink = flyswatter1_jtag_blink
269 { .name = "flyswatter2",
270 .init = flyswatter2_init,
271 .reset = flyswatter2_reset,
272 .blink = flyswatter2_jtag_blink
274 { .name = "minimodule",
275 .init = minimodule_init,
276 .reset = minimodule_reset,
278 { .name = "turtelizer2",
280 .reset = turtle_reset,
281 .blink = turtle_jtag_blink
283 { .name = "comstick",
284 .init = comstick_init,
285 .reset = comstick_reset,
287 { .name = "stm32stick",
288 .init = stm32stick_init,
289 .reset = stm32stick_reset,
291 { .name = "axm0432_jtag",
292 .init = axm0432_jtag_init,
293 .reset = axm0432_jtag_reset,
295 { .name = "sheevaplug",
296 .init = sheevaplug_init,
297 .reset = sheevaplug_reset,
300 .init = icebear_jtag_init,
301 .reset = icebear_jtag_reset,
304 .init = cortino_jtag_init,
305 .reset = comstick_reset,
307 { .name = "signalyzer-h",
308 .init = signalyzer_h_init,
309 .reset = signalyzer_h_reset,
310 .blink = signalyzer_h_blink
314 .reset = ktlink_reset,
315 .blink = ktlink_blink
317 { .name = "redbee-econotag",
319 .reset = redbee_reset,
321 { .name = "redbee-usb",
323 .reset = redbee_reset,
324 .channel = INTERFACE_B,
328 .reset = ftx23_reset,
329 .blink = lisa_l_blink,
330 .channel = INTERFACE_B,
332 { .name = "flossjtag",
333 .init = flossjtag_init,
334 .reset = ftx23_reset,
335 .blink = flossjtag_blink,
337 { .name = "xds100v2",
338 .init = xds100v2_init,
339 .reset = xds100v2_reset,
341 { .name = "digilent-hs1",
342 .init = digilent_hs1_init,
343 .reset = digilent_hs1_reset,
344 .channel = INTERFACE_A,
346 { .name = NULL, /* END OF TABLE */ },
349 /* bitmask used to drive nTRST; usually a GPIOLx signal */
350 static uint8_t nTRST;
351 static uint8_t nTRSTnOE;
352 /* bitmask used to drive nSRST; usually a GPIOLx signal */
353 static uint8_t nSRST;
354 static uint8_t nSRSTnOE;
356 /** the layout being used with this debug session */
357 static const struct ft2232_layout *layout;
359 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
360 static uint8_t low_output;
362 /* note that direction bit == 1 means that signal is an output */
364 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
365 static uint8_t low_direction;
366 /** default value bitmask for CBUS GPIOH(0..4) */
367 static uint8_t high_output;
368 /** default direction bitmask for CBUS GPIOH(0..4) */
369 static uint8_t high_direction;
371 #if BUILD_FT2232_FTD2XX == 1
372 static FT_HANDLE ftdih;
373 static FT_DEVICE ftdi_device;
374 #elif BUILD_FT2232_LIBFTDI == 1
375 static struct ftdi_context ftdic;
376 static enum ftdi_chip_type ftdi_device;
379 static struct jtag_command *first_unsent; /* next command that has to be sent */
380 static int require_send;
382 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
384 "There is a significant difference between libftdi and libftd2xx. The latter
385 one allows to schedule up to 64*64 bytes of result data while libftdi fails
386 with more than 4*64. As a consequence, the FT2232 driver is forced to
387 perform around 16x more USB transactions for long command streams with TDO
388 capture when running with libftdi."
391 #define FT2232_BUFFER_SIZE 131072
392 a comment would have been nice.
395 #if BUILD_FT2232_FTD2XX == 1
396 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
398 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
401 #define FT2232_BUFFER_SIZE 131072
403 static uint8_t *ft2232_buffer;
404 static int ft2232_buffer_size;
405 static int ft2232_read_pointer;
406 static int ft2232_expect_read;
409 * Function buffer_write
410 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
411 * @param val is the byte to send.
413 static inline void buffer_write(uint8_t val)
415 assert(ft2232_buffer);
416 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
417 ft2232_buffer[ft2232_buffer_size++] = val;
421 * Function buffer_read
422 * returns a byte from the byte buffer.
424 static inline uint8_t buffer_read(void)
426 assert(ft2232_buffer);
427 assert(ft2232_read_pointer < ft2232_buffer_size);
428 return ft2232_buffer[ft2232_read_pointer++];
432 * Clocks out \a bit_count bits on the TMS line, starting with the least
433 * significant bit of tms_bits and progressing to more significant bits.
434 * Rigorous state transition logging is done here via tap_set_state().
436 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
437 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
438 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
439 * is often used for this, 0x4b.
441 * @param tms_bits Holds the sequence of bits to send.
442 * @param tms_count Tells how many bits in the sequence.
443 * @param tdi_bit A single bit to pass on to TDI before the first TCK
444 * cycle and held static for the duration of TMS clocking.
446 * See the MPSSE spec referenced above.
448 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
452 int tms_ndx; /* bit index into tms_byte */
454 assert(tms_count > 0);
456 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
457 mpsse_cmd, tms_bits, tms_count);
459 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
460 bool bit = tms_bits & 1;
463 tms_byte |= (1 << tms_ndx);
465 /* always do state transitions in public view */
466 tap_set_state(tap_state_transition(tap_get_state(), bit));
468 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
473 if (tms_ndx == 7 || i == tms_count-1) {
474 buffer_write(mpsse_cmd);
475 buffer_write(tms_ndx - 1);
477 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
478 * TMS/CS and is held static for the duration of TMS/CS clocking.
480 buffer_write(tms_byte | (tdi_bit << 7));
486 * Function get_tms_buffer_requirements
487 * returns what clock_tms() will consume if called with
490 static inline int get_tms_buffer_requirements(int bit_count)
492 return ((bit_count + 6)/7) * 3;
496 * Function move_to_state
497 * moves the TAP controller from the current state to a
498 * \a goal_state through a path given by tap_get_tms_path(). State transition
499 * logging is performed by delegation to clock_tms().
501 * @param goal_state is the destination state for the move.
503 static void move_to_state(tap_state_t goal_state)
505 tap_state_t start_state = tap_get_state();
507 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
508 * lookup of the required TMS pattern to move to this state from the start state.
511 /* do the 2 lookups */
512 int tms_bits = tap_get_tms_path(start_state, goal_state);
513 int tms_count = tap_get_tms_path_len(start_state, goal_state);
515 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
517 clock_tms(0x4b, tms_bits, tms_count, 0);
520 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
522 #if BUILD_FT2232_FTD2XX == 1
524 DWORD dw_bytes_written = 0;
525 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
526 if (status != FT_OK) {
527 *bytes_written = dw_bytes_written;
528 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
529 return ERROR_JTAG_DEVICE_ERROR;
531 *bytes_written = dw_bytes_written;
533 #elif BUILD_FT2232_LIBFTDI == 1
534 int retval = ftdi_write_data(&ftdic, buf, size);
537 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
538 return ERROR_JTAG_DEVICE_ERROR;
540 *bytes_written = retval;
544 if (*bytes_written != (uint32_t)size)
545 return ERROR_JTAG_DEVICE_ERROR;
550 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
552 #if BUILD_FT2232_FTD2XX == 1
558 while ((*bytes_read < size) && timeout--) {
559 status = FT_Read(ftdih, buf + *bytes_read, size -
560 *bytes_read, &dw_bytes_read);
561 if (status != FT_OK) {
563 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
564 return ERROR_JTAG_DEVICE_ERROR;
566 *bytes_read += dw_bytes_read;
569 #elif BUILD_FT2232_LIBFTDI == 1
571 int timeout = LIBFTDI_READ_RETRY_COUNT;
574 while ((*bytes_read < size) && timeout--) {
575 retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
578 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
579 return ERROR_JTAG_DEVICE_ERROR;
581 *bytes_read += retval;
586 if (*bytes_read < size) {
587 LOG_ERROR("couldn't read enough bytes from "
588 "FT2232 device (%i < %i)",
589 (unsigned)*bytes_read,
591 return ERROR_JTAG_DEVICE_ERROR;
597 static bool ft2232_device_is_highspeed(void)
599 #if BUILD_FT2232_FTD2XX == 1
600 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H)
601 #ifdef HAS_ENUM_FT232H
602 || (ftdi_device == FT_DEVICE_232H)
605 #elif BUILD_FT2232_LIBFTDI == 1
606 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H
607 #ifdef HAS_ENUM_FT232H
608 || ftdi_device == TYPE_232H
615 * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
616 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
617 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
620 static int ftx232h_adaptive_clocking(bool enable)
622 uint8_t buf = enable ? 0x96 : 0x97;
623 LOG_DEBUG("%2.2x", buf);
625 uint32_t bytes_written;
628 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
629 if (retval != ERROR_OK) {
630 LOG_ERROR("couldn't write command to %s adaptive clocking"
631 , enable ? "enable" : "disable");
639 * Enable/disable the clk divide by 5 of the 60MHz master clock.
640 * This result in a JTAG clock speed range of 91.553Hz-6MHz
641 * respective 457.763Hz-30MHz.
643 static int ftx232h_clk_divide_by_5(bool enable)
645 uint32_t bytes_written;
646 uint8_t buf = enable ? 0x8b : 0x8a;
648 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
649 LOG_ERROR("couldn't write command to %s clk divide by 5"
650 , enable ? "enable" : "disable");
651 return ERROR_JTAG_INIT_FAILED;
653 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_x232H_MAX_TCK;
654 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
659 static int ft2232_speed(int speed)
663 uint32_t bytes_written;
666 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
667 if (ft2232_device_is_highspeed())
668 retval = ftx232h_adaptive_clocking(enable_adaptive_clocking);
669 else if (enable_adaptive_clocking) {
670 LOG_ERROR("ft2232 device %lu does not support RTCK"
671 , (long unsigned int)ftdi_device);
675 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
678 buf[0] = 0x86; /* command "set divisor" */
679 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
680 buf[2] = (speed >> 8) & 0xff; /* valueH */
682 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
683 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
684 if (retval != ERROR_OK) {
685 LOG_ERROR("couldn't set FT2232 TCK speed");
692 static int ft2232_speed_div(int speed, int *khz)
694 /* Take a look in the FT2232 manual,
695 * AN2232C-01 Command Processor for
696 * MPSSE and MCU Host Bus. Chapter 3.8 */
698 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
703 static int ft2232_khz(int khz, int *jtag_speed)
706 if (ft2232_device_is_highspeed()) {
707 *jtag_speed = RTCK_SPEED;
710 LOG_DEBUG("RCLK not supported");
715 /* Take a look in the FT2232 manual,
716 * AN2232C-01 Command Processor for
717 * MPSSE and MCU Host Bus. Chapter 3.8
719 * We will calc here with a multiplier
720 * of 10 for better rounding later. */
722 /* Calc speed, (ft2232_max_tck / khz) - 1
723 * Use 65000 for better rounding */
724 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
726 /* Add 0.9 for rounding */
729 /* Calc real speed */
730 *jtag_speed = *jtag_speed / 10;
732 /* Check if speed is greater than 0 */
736 /* Check max value */
737 if (*jtag_speed > 0xFFFF)
738 *jtag_speed = 0xFFFF;
743 static void ft2232_end_state(tap_state_t state)
745 if (tap_is_state_stable(state))
746 tap_set_end_state(state);
748 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
753 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
755 int num_bytes = (scan_size + 7) / 8;
756 int bits_left = scan_size;
759 while (num_bytes-- > 1) {
760 buffer[cur_byte++] = buffer_read();
764 buffer[cur_byte] = 0x0;
766 /* There is one more partial byte left from the clock data in/out instructions */
768 buffer[cur_byte] = buffer_read() >> 1;
769 /* This shift depends on the length of the
770 *clock data to tms instruction, insterted
771 *at end of the scan, now fixed to a two
772 *step transition in ft2232_add_scan */
773 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
776 static void ft2232_debug_dump_buffer(void)
782 for (i = 0; i < ft2232_buffer_size; i++) {
783 line_p += snprintf(line_p,
784 sizeof(line) - (line_p - line),
788 LOG_DEBUG("%s", line);
794 LOG_DEBUG("%s", line);
797 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
799 struct jtag_command *cmd;
804 uint32_t bytes_written = 0;
805 uint32_t bytes_read = 0;
807 #ifdef _DEBUG_USB_IO_
808 struct timeval start, inter, inter2, end;
809 struct timeval d_inter, d_inter2, d_end;
812 #ifdef _DEBUG_USB_COMMS_
813 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
814 ft2232_debug_dump_buffer();
817 #ifdef _DEBUG_USB_IO_
818 gettimeofday(&start, NULL);
821 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
822 if (retval != ERROR_OK) {
823 LOG_ERROR("couldn't write MPSSE commands to FT2232");
827 #ifdef _DEBUG_USB_IO_
828 gettimeofday(&inter, NULL);
831 if (ft2232_expect_read) {
832 /* FIXME this "timeout" is never changed ... */
833 int timeout = LIBFTDI_READ_RETRY_COUNT;
834 ft2232_buffer_size = 0;
836 #ifdef _DEBUG_USB_IO_
837 gettimeofday(&inter2, NULL);
840 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
841 if (retval != ERROR_OK) {
842 LOG_ERROR("couldn't read from FT2232");
846 #ifdef _DEBUG_USB_IO_
847 gettimeofday(&end, NULL);
849 timeval_subtract(&d_inter, &inter, &start);
850 timeval_subtract(&d_inter2, &inter2, &start);
851 timeval_subtract(&d_end, &end, &start);
853 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
854 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
855 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
856 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
859 ft2232_buffer_size = bytes_read;
861 if (ft2232_expect_read != ft2232_buffer_size) {
862 LOG_ERROR("ft2232_expect_read (%i) != "
863 "ft2232_buffer_size (%i) "
867 LIBFTDI_READ_RETRY_COUNT - timeout);
868 ft2232_debug_dump_buffer();
873 #ifdef _DEBUG_USB_COMMS_
874 LOG_DEBUG("read buffer (%i retries): %i bytes",
875 LIBFTDI_READ_RETRY_COUNT - timeout,
877 ft2232_debug_dump_buffer();
881 ft2232_expect_read = 0;
882 ft2232_read_pointer = 0;
884 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
885 * that wasn't handled by a caller-provided error handler
890 while (cmd != last) {
893 type = jtag_scan_type(cmd->cmd.scan);
894 if (type != SCAN_OUT) {
895 scan_size = jtag_scan_size(cmd->cmd.scan);
896 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
897 ft2232_read_scan(type, buffer, scan_size);
898 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
899 retval = ERROR_JTAG_QUEUE_FAILED;
911 ft2232_buffer_size = 0;
917 * Function ft2232_add_pathmove
918 * moves the TAP controller from the current state to a new state through the
919 * given path, where path is an array of tap_state_t's.
921 * @param path is an array of tap_stat_t which gives the states to traverse through
922 * ending with the last state at path[num_states-1]
923 * @param num_states is the count of state steps to move through
925 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
929 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
933 /* this loop verifies that the path is legal and logs each state in the path */
935 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
937 int num_states_batch = num_states > 7 ? 7 : num_states;
939 /* command "Clock Data to TMS/CS Pin (no Read)" */
942 /* number of states remaining */
943 buffer_write(num_states_batch - 1);
945 while (num_states_batch--) {
946 /* either TMS=0 or TMS=1 must work ... */
947 if (tap_state_transition(tap_get_state(), false) == path[state_count])
948 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
949 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
950 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
952 /* ... or else the caller goofed BADLY */
954 LOG_ERROR("BUG: %s -> %s isn't a valid "
955 "TAP state transition",
956 tap_state_name(tap_get_state()),
957 tap_state_name(path[state_count]));
961 tap_set_state(path[state_count]);
966 buffer_write(tms_byte);
968 tap_set_end_state(tap_get_state());
971 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
973 int num_bytes = (scan_size + 7) / 8;
974 int bits_left = scan_size;
979 if (tap_get_state() != TAP_DRSHIFT)
980 move_to_state(TAP_DRSHIFT);
982 if (tap_get_state() != TAP_IRSHIFT)
983 move_to_state(TAP_IRSHIFT);
986 /* add command for complete bytes */
987 while (num_bytes > 1) {
989 if (type == SCAN_IO) {
990 /* Clock Data Bytes In and Out LSB First */
992 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
993 } else if (type == SCAN_OUT) {
994 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
996 /* LOG_DEBUG("added TDI bytes (o)"); */
997 } else if (type == SCAN_IN) {
998 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1000 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1003 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1004 num_bytes -= thisrun_bytes;
1006 buffer_write((uint8_t) (thisrun_bytes - 1));
1007 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1009 if (type != SCAN_IN) {
1010 /* add complete bytes */
1011 while (thisrun_bytes-- > 0) {
1012 buffer_write(buffer[cur_byte++]);
1015 } else /* (type == SCAN_IN) */
1016 bits_left -= 8 * (thisrun_bytes);
1019 /* the most signifcant bit is scanned during TAP movement */
1020 if (type != SCAN_IN)
1021 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1025 /* process remaining bits but the last one */
1026 if (bits_left > 1) {
1027 if (type == SCAN_IO) {
1028 /* Clock Data Bits In and Out LSB First */
1030 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1031 } else if (type == SCAN_OUT) {
1032 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1034 /* LOG_DEBUG("added TDI bits (o)"); */
1035 } else if (type == SCAN_IN) {
1036 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1038 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1041 buffer_write(bits_left - 2);
1042 if (type != SCAN_IN)
1043 buffer_write(buffer[cur_byte]);
1046 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1047 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1048 if (type == SCAN_IO) {
1049 /* Clock Data Bits In and Out LSB First */
1051 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1052 } else if (type == SCAN_OUT) {
1053 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1055 /* LOG_DEBUG("added TDI bits (o)"); */
1056 } else if (type == SCAN_IN) {
1057 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1059 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1062 if (type != SCAN_IN)
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;
1109 if (!receive_buffer) {
1110 LOG_ERROR("failed to allocate memory");
1115 LOG_ERROR("BUG: large IR scans are not supported");
1119 if (tap_get_state() != TAP_DRSHIFT)
1120 move_to_state(TAP_DRSHIFT);
1122 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1123 if (retval != ERROR_OK) {
1124 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1127 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1128 ft2232_buffer_size, (int)bytes_written);
1129 ft2232_buffer_size = 0;
1131 /* add command for complete bytes */
1132 while (num_bytes > 1) {
1135 if (type == SCAN_IO) {
1136 /* Clock Data Bytes In and Out LSB First */
1138 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1139 } else if (type == SCAN_OUT) {
1140 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1142 /* LOG_DEBUG("added TDI bytes (o)"); */
1143 } else if (type == SCAN_IN) {
1144 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1146 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1149 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1150 thisrun_read = thisrun_bytes;
1151 num_bytes -= thisrun_bytes;
1152 buffer_write((uint8_t) (thisrun_bytes - 1));
1153 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1155 if (type != SCAN_IN) {
1156 /* add complete bytes */
1157 while (thisrun_bytes-- > 0) {
1158 buffer_write(buffer[cur_byte]);
1162 } else /* (type == SCAN_IN) */
1163 bits_left -= 8 * (thisrun_bytes);
1165 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1166 if (retval != ERROR_OK) {
1167 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1170 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1172 (int)bytes_written);
1173 ft2232_buffer_size = 0;
1175 if (type != SCAN_OUT) {
1176 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1177 if (retval != ERROR_OK) {
1178 LOG_ERROR("couldn't read from FT2232");
1181 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1184 receive_pointer += bytes_read;
1190 /* the most signifcant bit is scanned during TAP movement */
1191 if (type != SCAN_IN)
1192 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1196 /* process remaining bits but the last one */
1197 if (bits_left > 1) {
1198 if (type == SCAN_IO) {
1199 /* Clock Data Bits In and Out LSB First */
1201 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1202 } else if (type == SCAN_OUT) {
1203 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1205 /* LOG_DEBUG("added TDI bits (o)"); */
1206 } else if (type == SCAN_IN) {
1207 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1209 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1211 buffer_write(bits_left - 2);
1212 if (type != SCAN_IN)
1213 buffer_write(buffer[cur_byte]);
1215 if (type != SCAN_OUT)
1219 if (tap_get_end_state() == TAP_DRSHIFT) {
1220 if (type == SCAN_IO) {
1221 /* Clock Data Bits In and Out LSB First */
1223 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1224 } else if (type == SCAN_OUT) {
1225 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1227 /* LOG_DEBUG("added TDI bits (o)"); */
1228 } else if (type == SCAN_IN) {
1229 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1231 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1234 buffer_write(last_bit);
1236 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1237 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1240 /* move from Shift-IR/DR to end state */
1241 if (type != SCAN_OUT) {
1242 /* Clock Data to TMS/CS Pin with Read */
1244 /* LOG_DEBUG("added TMS scan (read)"); */
1246 /* Clock Data to TMS/CS Pin (no Read) */
1248 /* LOG_DEBUG("added TMS scan (no read)"); */
1251 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1252 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1255 if (type != SCAN_OUT)
1258 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1259 if (retval != ERROR_OK) {
1260 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1263 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1265 (int)bytes_written);
1266 ft2232_buffer_size = 0;
1268 if (type != SCAN_OUT) {
1269 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1270 if (retval != ERROR_OK) {
1271 LOG_ERROR("couldn't read from FT2232");
1274 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1279 free(receive_buffer);
1284 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1286 int predicted_size = 3;
1287 int num_bytes = (scan_size - 1) / 8;
1289 if (tap_get_state() != TAP_DRSHIFT)
1290 predicted_size += get_tms_buffer_requirements(
1291 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1293 if (type == SCAN_IN) { /* only from device to host */
1294 /* complete bytes */
1295 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1297 /* remaining bits - 1 (up to 7) */
1298 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1299 } else {/* host to device, or bidirectional
1301 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1303 /* remaining bits -1 (up to 7) */
1304 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1307 return predicted_size;
1310 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1312 int predicted_size = 0;
1314 if (type != SCAN_OUT) {
1315 /* complete bytes */
1317 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1319 /* remaining bits - 1 */
1320 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1322 /* last bit (from TMS scan) */
1323 predicted_size += 1;
1326 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1328 return predicted_size;
1331 /* semi-generic FT2232/FT4232 reset code */
1332 static void ftx23_reset(int trst, int srst)
1334 enum reset_types jtag_reset_config = jtag_get_reset_config();
1336 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1337 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1339 low_output &= ~nTRST; /* switch output low */
1340 } else if (trst == 0) {
1341 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1342 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1343 *and external pullup) */
1345 low_output |= nTRST; /* switch output high */
1349 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1350 low_output &= ~nSRST; /* switch output low */
1352 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1353 } else if (srst == 0) {
1354 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1355 low_output |= nSRST; /* switch output high */
1357 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1360 /* command "set data bits low byte" */
1362 buffer_write(low_output);
1363 buffer_write(low_direction);
1366 static void jtagkey_reset(int trst, int srst)
1368 enum reset_types jtag_reset_config = jtag_get_reset_config();
1370 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1371 high_output &= ~nTRSTnOE;
1373 high_output &= ~nTRST;
1374 } else if (trst == 0) {
1375 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1376 high_output |= nTRSTnOE;
1378 high_output |= nTRST;
1382 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1383 high_output &= ~nSRST;
1385 high_output &= ~nSRSTnOE;
1386 } else if (srst == 0) {
1387 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1388 high_output |= nSRST;
1390 high_output |= nSRSTnOE;
1393 /* command "set data bits high byte" */
1395 buffer_write(high_output);
1396 buffer_write(high_direction);
1397 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1404 static void olimex_jtag_reset(int trst, int srst)
1406 enum reset_types jtag_reset_config = jtag_get_reset_config();
1408 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1409 high_output &= ~nTRSTnOE;
1411 high_output &= ~nTRST;
1412 } else if (trst == 0) {
1413 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1414 high_output |= nTRSTnOE;
1416 high_output |= nTRST;
1420 high_output |= nSRST;
1422 high_output &= ~nSRST;
1424 /* command "set data bits high byte" */
1426 buffer_write(high_output);
1427 buffer_write(high_direction);
1428 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1435 static void axm0432_jtag_reset(int trst, int srst)
1438 tap_set_state(TAP_RESET);
1439 high_output &= ~nTRST;
1440 } else if (trst == 0)
1441 high_output |= nTRST;
1444 high_output &= ~nSRST;
1446 high_output |= nSRST;
1448 /* command "set data bits low byte" */
1450 buffer_write(high_output);
1451 buffer_write(high_direction);
1452 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1459 static void flyswatter_reset(int trst, int srst)
1462 low_output &= ~nTRST;
1464 low_output |= nTRST;
1467 low_output |= nSRST;
1469 low_output &= ~nSRST;
1471 /* command "set data bits low byte" */
1473 buffer_write(low_output);
1474 buffer_write(low_direction);
1475 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1482 static void flyswatter1_reset(int trst, int srst)
1484 flyswatter_reset(trst, srst);
1487 static void flyswatter2_reset(int trst, int srst)
1489 flyswatter_reset(trst, !srst);
1492 static void minimodule_reset(int trst, int srst)
1495 low_output &= ~nSRST;
1497 low_output |= nSRST;
1499 /* command "set data bits low byte" */
1501 buffer_write(low_output);
1502 buffer_write(low_direction);
1503 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1510 static void turtle_reset(int trst, int srst)
1513 LOG_ERROR("Can't assert TRST: the adapter lacks this signal");
1516 low_output |= nSRST;
1518 low_output &= ~nSRST;
1520 /* command "set data bits low byte" */
1522 buffer_write(low_output);
1523 buffer_write(low_direction);
1524 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1530 static void comstick_reset(int trst, int srst)
1533 high_output &= ~nTRST;
1535 high_output |= nTRST;
1538 high_output &= ~nSRST;
1540 high_output |= nSRST;
1542 /* command "set data bits high byte" */
1544 buffer_write(high_output);
1545 buffer_write(high_direction);
1546 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1553 static void stm32stick_reset(int trst, int srst)
1556 high_output &= ~nTRST;
1558 high_output |= nTRST;
1561 low_output &= ~nSRST;
1563 low_output |= nSRST;
1565 /* command "set data bits low byte" */
1567 buffer_write(low_output);
1568 buffer_write(low_direction);
1570 /* command "set data bits high byte" */
1572 buffer_write(high_output);
1573 buffer_write(high_direction);
1574 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1581 static void sheevaplug_reset(int trst, int srst)
1584 high_output &= ~nTRST;
1586 high_output |= nTRST;
1589 high_output &= ~nSRSTnOE;
1591 high_output |= nSRSTnOE;
1593 /* command "set data bits high byte" */
1595 buffer_write(high_output);
1596 buffer_write(high_direction);
1597 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1604 static void redbee_reset(int trst, int srst)
1607 tap_set_state(TAP_RESET);
1608 high_output &= ~nTRST;
1609 } else if (trst == 0)
1610 high_output |= nTRST;
1613 high_output &= ~nSRST;
1615 high_output |= nSRST;
1617 /* command "set data bits low byte" */
1619 buffer_write(high_output);
1620 buffer_write(high_direction);
1621 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1622 "high_direction: 0x%2.2x", trst, srst, high_output,
1626 static void xds100v2_reset(int trst, int srst)
1629 tap_set_state(TAP_RESET);
1630 high_output &= ~nTRST;
1631 } else if (trst == 0)
1632 high_output |= nTRST;
1635 high_output |= nSRST;
1637 high_output &= ~nSRST;
1639 /* command "set data bits low byte" */
1641 buffer_write(high_output);
1642 buffer_write(high_direction);
1643 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1644 "high_direction: 0x%2.2x", trst, srst, high_output,
1648 static int ft2232_execute_runtest(struct jtag_command *cmd)
1652 int predicted_size = 0;
1655 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1656 cmd->cmd.runtest->num_cycles,
1657 tap_state_name(cmd->cmd.runtest->end_state));
1659 /* only send the maximum buffer size that FT2232C can handle */
1661 if (tap_get_state() != TAP_IDLE)
1662 predicted_size += 3;
1663 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1664 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1665 predicted_size += 3;
1666 if (tap_get_end_state() != TAP_IDLE)
1667 predicted_size += 3;
1668 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1669 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1670 retval = ERROR_JTAG_QUEUE_FAILED;
1674 if (tap_get_state() != TAP_IDLE) {
1675 move_to_state(TAP_IDLE);
1678 i = cmd->cmd.runtest->num_cycles;
1680 /* there are no state transitions in this code, so omit state tracking */
1682 /* command "Clock Data to TMS/CS Pin (no Read)" */
1686 buffer_write((i > 7) ? 6 : (i - 1));
1691 i -= (i > 7) ? 7 : i;
1692 /* LOG_DEBUG("added TMS scan (no read)"); */
1695 ft2232_end_state(cmd->cmd.runtest->end_state);
1697 if (tap_get_state() != tap_get_end_state())
1698 move_to_state(tap_get_end_state());
1701 DEBUG_JTAG_IO("runtest: %i, end in %s",
1702 cmd->cmd.runtest->num_cycles,
1703 tap_state_name(tap_get_end_state()));
1707 static int ft2232_execute_statemove(struct jtag_command *cmd)
1709 int predicted_size = 0;
1710 int retval = ERROR_OK;
1712 DEBUG_JTAG_IO("statemove end in %s",
1713 tap_state_name(cmd->cmd.statemove->end_state));
1715 /* only send the maximum buffer size that FT2232C can handle */
1717 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1718 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1719 retval = ERROR_JTAG_QUEUE_FAILED;
1723 ft2232_end_state(cmd->cmd.statemove->end_state);
1725 /* For TAP_RESET, ignore the current recorded state. It's often
1726 * wrong at server startup, and this transation is critical whenever
1729 if (tap_get_end_state() == TAP_RESET) {
1730 clock_tms(0x4b, 0xff, 5, 0);
1733 /* shortest-path move to desired end state */
1734 } else if (tap_get_state() != tap_get_end_state()) {
1735 move_to_state(tap_get_end_state());
1743 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1744 * (or SWD) state machine.
1746 static int ft2232_execute_tms(struct jtag_command *cmd)
1748 int retval = ERROR_OK;
1749 unsigned num_bits = cmd->cmd.tms->num_bits;
1750 const uint8_t *bits = cmd->cmd.tms->bits;
1753 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1755 /* only send the maximum buffer size that FT2232C can handle */
1756 count = 3 * DIV_ROUND_UP(num_bits, 4);
1757 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1758 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1759 retval = ERROR_JTAG_QUEUE_FAILED;
1765 /* Shift out in batches of at most 6 bits; there's a report of an
1766 * FT2232 bug in this area, where shifting exactly 7 bits can make
1767 * problems with TMS signaling for the last clock cycle:
1769 * http://developer.intra2net.com/mailarchive/html/
1770 * libftdi/2009/msg00292.html
1772 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1774 * Note that pathmoves in JTAG are not often seven bits, so that
1775 * isn't a particularly likely situation outside of "special"
1776 * signaling such as switching between JTAG and SWD modes.
1779 if (num_bits <= 6) {
1781 buffer_write(num_bits - 1);
1782 buffer_write(*bits & 0x3f);
1786 /* Yes, this is lazy ... we COULD shift out more data
1787 * bits per operation, but doing it in nybbles is easy
1791 buffer_write(*bits & 0xf);
1794 count = (num_bits > 4) ? 4 : num_bits;
1797 buffer_write(count - 1);
1798 buffer_write((*bits >> 4) & 0xf);
1808 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1810 int predicted_size = 0;
1811 int retval = ERROR_OK;
1813 tap_state_t *path = cmd->cmd.pathmove->path;
1814 int num_states = cmd->cmd.pathmove->num_states;
1816 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1817 tap_state_name(tap_get_state()),
1818 tap_state_name(path[num_states-1]));
1820 /* only send the maximum buffer size that FT2232C can handle */
1821 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1822 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1823 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1824 retval = ERROR_JTAG_QUEUE_FAILED;
1830 ft2232_add_pathmove(path, num_states);
1836 static int ft2232_execute_scan(struct jtag_command *cmd)
1839 int scan_size; /* size of IR or DR scan */
1840 int predicted_size = 0;
1841 int retval = ERROR_OK;
1843 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1845 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1847 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1849 predicted_size = ft2232_predict_scan_out(scan_size, type);
1850 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1851 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1852 /* unsent commands before this */
1853 if (first_unsent != cmd)
1854 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1855 retval = ERROR_JTAG_QUEUE_FAILED;
1857 /* current command */
1858 ft2232_end_state(cmd->cmd.scan->end_state);
1859 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1861 first_unsent = cmd->next;
1865 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1867 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1870 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1871 retval = ERROR_JTAG_QUEUE_FAILED;
1875 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1876 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1877 ft2232_end_state(cmd->cmd.scan->end_state);
1878 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1882 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1883 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1884 tap_state_name(tap_get_end_state()));
1889 static int ft2232_execute_reset(struct jtag_command *cmd)
1892 int predicted_size = 0;
1895 DEBUG_JTAG_IO("reset trst: %i srst %i",
1896 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1898 /* only send the maximum buffer size that FT2232C can handle */
1900 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1901 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1902 retval = ERROR_JTAG_QUEUE_FAILED;
1907 if ((cmd->cmd.reset->trst == 1) ||
1908 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1909 tap_set_state(TAP_RESET);
1911 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1914 DEBUG_JTAG_IO("trst: %i, srst: %i",
1915 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1919 static int ft2232_execute_sleep(struct jtag_command *cmd)
1924 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1926 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1927 retval = ERROR_JTAG_QUEUE_FAILED;
1928 first_unsent = cmd->next;
1929 jtag_sleep(cmd->cmd.sleep->us);
1930 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1932 tap_state_name(tap_get_state()));
1936 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1941 /* this is only allowed while in a stable state. A check for a stable
1942 * state was done in jtag_add_clocks()
1944 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1945 retval = ERROR_JTAG_QUEUE_FAILED;
1946 DEBUG_JTAG_IO("clocks %i while in %s",
1947 cmd->cmd.stableclocks->num_cycles,
1948 tap_state_name(tap_get_state()));
1952 static int ft2232_execute_command(struct jtag_command *cmd)
1956 switch (cmd->type) {
1958 retval = ft2232_execute_reset(cmd);
1961 retval = ft2232_execute_runtest(cmd);
1963 case JTAG_TLR_RESET:
1964 retval = ft2232_execute_statemove(cmd);
1967 retval = ft2232_execute_pathmove(cmd);
1970 retval = ft2232_execute_scan(cmd);
1973 retval = ft2232_execute_sleep(cmd);
1975 case JTAG_STABLECLOCKS:
1976 retval = ft2232_execute_stableclocks(cmd);
1979 retval = ft2232_execute_tms(cmd);
1982 LOG_ERROR("BUG: unknown JTAG command type encountered");
1983 retval = ERROR_JTAG_QUEUE_FAILED;
1989 static int ft2232_execute_queue(void)
1991 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1994 first_unsent = cmd; /* next command that has to be sent */
1997 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1998 * that wasn't handled by a caller-provided error handler
2002 ft2232_buffer_size = 0;
2003 ft2232_expect_read = 0;
2005 /* blink, if the current layout has that feature */
2010 /* fill the write buffer with the desired command */
2011 if (ft2232_execute_command(cmd) != ERROR_OK)
2012 retval = ERROR_JTAG_QUEUE_FAILED;
2013 /* Start reading input before FT2232 TX buffer fills up.
2014 * Sometimes this happens because we don't know the
2015 * length of the last command before we execute it. So
2016 * we simple inform the user.
2020 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2021 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2022 LOG_DEBUG("read buffer size looks too high %d/%d",
2024 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2025 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2026 retval = ERROR_JTAG_QUEUE_FAILED;
2031 if (require_send > 0)
2032 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2033 retval = ERROR_JTAG_QUEUE_FAILED;
2038 #if BUILD_FT2232_FTD2XX == 1
2039 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2043 char SerialNumber[16];
2044 char Description[64];
2045 DWORD openex_flags = 0;
2046 char *openex_string = NULL;
2047 uint8_t latency_timer;
2049 if (layout == NULL) {
2050 LOG_WARNING("No ft2232 layout specified'");
2051 return ERROR_JTAG_INIT_FAILED;
2054 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2055 layout->name, vid, pid);
2058 /* Add non-standard Vid/Pid to the linux driver */
2059 status = FT_SetVIDPID(vid, pid);
2060 if (status != FT_OK)
2061 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2065 if (ft2232_device_desc && ft2232_serial) {
2067 "can't open by device description and serial number, giving precedence to serial");
2068 ft2232_device_desc = NULL;
2071 if (ft2232_device_desc) {
2072 openex_string = ft2232_device_desc;
2073 openex_flags = FT_OPEN_BY_DESCRIPTION;
2074 } else if (ft2232_serial) {
2075 openex_string = ft2232_serial;
2076 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2078 LOG_ERROR("neither device description nor serial number specified");
2080 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2082 return ERROR_JTAG_INIT_FAILED;
2085 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2086 if (status != FT_OK) {
2087 /* under Win32, the FTD2XX driver appends an "A" to the end
2088 * of the description, if we tried by the desc, then
2089 * try by the alternate "A" description. */
2090 if (openex_string == ft2232_device_desc) {
2091 /* Try the alternate method. */
2092 openex_string = ft2232_device_desc_A;
2093 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2094 if (status == FT_OK) {
2095 /* yea, the "alternate" method worked! */
2097 /* drat, give the user a meaningfull message.
2098 * telling the use we tried *BOTH* methods. */
2099 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2101 ft2232_device_desc_A);
2106 if (status != FT_OK) {
2110 LOG_WARNING("unable to open ftdi device (trying more): %s",
2111 ftd2xx_status_string(status));
2113 return ERROR_JTAG_INIT_FAILED;
2115 LOG_ERROR("unable to open ftdi device: %s",
2116 ftd2xx_status_string(status));
2117 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2118 if (status == FT_OK) {
2119 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2122 for (i = 0; i < num_devices; i++)
2123 desc_array[i] = malloc(64);
2125 desc_array[num_devices] = NULL;
2127 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2129 if (status == FT_OK) {
2130 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2131 for (i = 0; i < num_devices; i++)
2132 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2135 for (i = 0; i < num_devices; i++)
2136 free(desc_array[i]);
2140 LOG_ERROR("ListDevices: NONE");
2141 return ERROR_JTAG_INIT_FAILED;
2144 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2145 if (status != FT_OK) {
2146 LOG_ERROR("unable to set latency timer: %s",
2147 ftd2xx_status_string(status));
2148 return ERROR_JTAG_INIT_FAILED;
2151 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2152 if (status != FT_OK) {
2153 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2154 * so ignore errors if using this driver version */
2157 status = FT_GetDriverVersion(ftdih, &dw_version);
2158 LOG_ERROR("unable to get latency timer: %s",
2159 ftd2xx_status_string(status));
2161 if ((status == FT_OK) && (dw_version == 0x10004)) {
2162 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2163 "with FT_GetLatencyTimer, upgrade to a newer version");
2165 return ERROR_JTAG_INIT_FAILED;
2167 LOG_DEBUG("current latency timer: %i", latency_timer);
2169 status = FT_SetTimeouts(ftdih, 5000, 5000);
2170 if (status != FT_OK) {
2171 LOG_ERROR("unable to set timeouts: %s",
2172 ftd2xx_status_string(status));
2173 return ERROR_JTAG_INIT_FAILED;
2176 status = FT_SetBitMode(ftdih, 0x0b, 2);
2177 if (status != FT_OK) {
2178 LOG_ERROR("unable to enable bit i/o mode: %s",
2179 ftd2xx_status_string(status));
2180 return ERROR_JTAG_INIT_FAILED;
2183 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2184 SerialNumber, Description, NULL);
2185 if (status != FT_OK) {
2186 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2187 ftd2xx_status_string(status));
2188 return ERROR_JTAG_INIT_FAILED;
2190 static const char *type_str[] = {
2191 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2193 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2194 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2195 ? ftdi_device : FT_DEVICE_UNKNOWN;
2196 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2197 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2198 LOG_INFO("SerialNumber: %s", SerialNumber);
2199 LOG_INFO("Description: %s", Description);
2205 static int ft2232_purge_ftd2xx(void)
2209 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2210 if (status != FT_OK) {
2211 LOG_ERROR("error purging ftd2xx device: %s",
2212 ftd2xx_status_string(status));
2213 return ERROR_JTAG_INIT_FAILED;
2219 #endif /* BUILD_FT2232_FTD2XX == 1 */
2221 #if BUILD_FT2232_LIBFTDI == 1
2222 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2224 uint8_t latency_timer;
2226 if (layout == NULL) {
2227 LOG_WARNING("No ft2232 layout specified'");
2228 return ERROR_JTAG_INIT_FAILED;
2231 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2232 layout->name, vid, pid);
2234 if (ftdi_init(&ftdic) < 0)
2235 return ERROR_JTAG_INIT_FAILED;
2237 /* default to INTERFACE_A */
2238 if (channel == INTERFACE_ANY)
2239 channel = INTERFACE_A;
2240 if (ftdi_set_interface(&ftdic, channel) < 0) {
2241 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2242 return ERROR_JTAG_INIT_FAILED;
2245 /* context, vendor id, product id */
2246 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2248 LOG_WARNING("unable to open ftdi device (trying more): %s",
2251 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2253 return ERROR_JTAG_INIT_FAILED;
2256 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2257 if (ftdi_usb_reset(&ftdic) < 0) {
2258 LOG_ERROR("unable to reset ftdi device");
2259 return ERROR_JTAG_INIT_FAILED;
2262 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2263 LOG_ERROR("unable to set latency timer");
2264 return ERROR_JTAG_INIT_FAILED;
2267 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2268 LOG_ERROR("unable to get latency timer");
2269 return ERROR_JTAG_INIT_FAILED;
2271 LOG_DEBUG("current latency timer: %i", latency_timer);
2273 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2275 ftdi_device = ftdic.type;
2276 static const char *type_str[] = {
2277 "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "Unknown"
2279 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2280 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2281 ? ftdi_device : no_of_known_types;
2282 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2286 static int ft2232_purge_libftdi(void)
2288 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2289 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2290 return ERROR_JTAG_INIT_FAILED;
2296 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2298 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2301 uint32_t bytes_written;
2303 buf[0] = 0x80; /* command "set data bits low byte" */
2304 buf[1] = value; /* value */
2305 buf[2] = direction; /* direction */
2307 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2309 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2310 LOG_ERROR("couldn't initialize data bits low byte");
2311 return ERROR_JTAG_INIT_FAILED;
2317 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2320 uint32_t bytes_written;
2322 buf[0] = 0x82; /* command "set data bits high byte" */
2323 buf[1] = value; /* value */
2324 buf[2] = direction; /* direction */
2326 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2328 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2329 LOG_ERROR("couldn't initialize data bits high byte");
2330 return ERROR_JTAG_INIT_FAILED;
2336 static int ft2232_init(void)
2340 uint32_t bytes_written;
2342 LOG_WARNING("Using DEPRECATED interface driver 'ft2232'");
2344 LOG_INFO("Consider using the 'ftdi' interface driver, with configuration files in interface/ftdi/...");
2347 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2348 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2350 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2351 if (layout == NULL) {
2352 LOG_WARNING("No ft2232 layout specified'");
2353 return ERROR_JTAG_INIT_FAILED;
2356 for (int i = 0; 1; i++) {
2358 * "more indicates that there are more IDs to try, so we should
2359 * not print an error for an ID mismatch (but for anything
2362 * try_more indicates that the error code returned indicates an
2363 * ID mismatch (and nothing else) and that we should proceeed
2364 * with the next ID pair.
2366 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2369 #if BUILD_FT2232_FTD2XX == 1
2370 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2372 #elif BUILD_FT2232_LIBFTDI == 1
2373 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2374 more, &try_more, ft2232_channel);
2378 if (!more || !try_more)
2382 ft2232_buffer_size = 0;
2383 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2385 if (layout->init() != ERROR_OK)
2386 return ERROR_JTAG_INIT_FAILED;
2388 if (ft2232_device_is_highspeed()) {
2389 #ifndef BUILD_FT2232_HIGHSPEED
2390 #if BUILD_FT2232_FTD2XX == 1
2392 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2393 #elif BUILD_FT2232_LIBFTDI == 1
2395 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2398 /* make sure the legacy mode is disabled */
2399 if (ftx232h_clk_divide_by_5(false) != ERROR_OK)
2400 return ERROR_JTAG_INIT_FAILED;
2403 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2404 retval = ft2232_write(buf, 1, &bytes_written);
2405 if (retval != ERROR_OK) {
2406 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2407 return ERROR_JTAG_INIT_FAILED;
2410 #if BUILD_FT2232_FTD2XX == 1
2411 return ft2232_purge_ftd2xx();
2412 #elif BUILD_FT2232_LIBFTDI == 1
2413 return ft2232_purge_libftdi();
2419 /** Updates defaults for DBUS signals: the four JTAG signals
2420 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2422 static inline void ftx232_dbus_init(void)
2425 low_direction = 0x0b;
2428 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2429 * the four GPIOL signals. Initialization covers value and direction,
2430 * as customized for each layout.
2432 static int ftx232_dbus_write(void)
2434 enum reset_types jtag_reset_config = jtag_get_reset_config();
2435 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2436 low_direction &= ~nTRSTnOE; /* nTRST input */
2437 low_output &= ~nTRST; /* nTRST = 0 */
2439 low_direction |= nTRSTnOE; /* nTRST output */
2440 low_output |= nTRST; /* nTRST = 1 */
2443 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2444 low_direction |= nSRSTnOE; /* nSRST output */
2445 low_output |= nSRST; /* nSRST = 1 */
2447 low_direction &= ~nSRSTnOE; /* nSRST input */
2448 low_output &= ~nSRST; /* nSRST = 0 */
2451 /* initialize low byte for jtag */
2452 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2453 LOG_ERROR("couldn't initialize FT2232 DBUS");
2454 return ERROR_JTAG_INIT_FAILED;
2460 static int usbjtag_init(void)
2463 * NOTE: This is now _specific_ to the "usbjtag" layout.
2464 * Don't try cram any more layouts into this.
2473 return ftx232_dbus_write();
2476 static int lm3s811_jtag_init(void)
2480 /* There are multiple revisions of LM3S811 eval boards:
2481 * - Rev B (and older?) boards have no SWO trace support.
2482 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2483 * they should use the "luminary_icdi" layout instead.
2490 low_direction = 0x8b;
2492 return ftx232_dbus_write();
2495 static int icdi_jtag_init(void)
2499 /* Most Luminary eval boards support SWO trace output,
2500 * and should use this "luminary_icdi" layout.
2502 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2503 * to switch between JTAG and SWD, or switch the ft2232 UART
2504 * on the second MPSSE channel/interface (BDBUS)
2505 * between (i) the stellaris UART (on Luminary boards)
2506 * or (ii) SWO trace data (generic).
2508 * We come up in JTAG mode and may switch to SWD later (with
2509 * SWO/trace option if SWD is active).
2516 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2517 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2518 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2521 /* GPIOs on second channel/interface (UART) ... */
2522 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2523 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2524 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2529 nSRSTnOE = ICDI_SRST;
2531 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2532 low_output |= ICDI_JTAG_EN;
2533 low_output &= ~ICDI_DBG_ENn;
2535 return ftx232_dbus_write();
2538 static int signalyzer_init(void)
2546 return ftx232_dbus_write();
2549 static int axm0432_jtag_init(void)
2552 low_direction = 0x2b;
2554 /* initialize low byte for jtag */
2555 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2556 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2557 return ERROR_JTAG_INIT_FAILED;
2560 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2562 nTRSTnOE = 0x0; /* No output enable for TRST*/
2564 nSRSTnOE = 0x0; /* No output enable for SRST*/
2566 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2571 high_direction = 0x0c;
2573 enum reset_types jtag_reset_config = jtag_get_reset_config();
2574 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2575 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2577 high_output |= nTRST;
2579 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2580 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2582 high_output |= nSRST;
2584 /* initialize high byte for jtag */
2585 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2586 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2587 return ERROR_JTAG_INIT_FAILED;
2593 static int redbee_init(void)
2596 low_direction = 0x2b;
2598 /* initialize low byte for jtag */
2599 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2600 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2601 return ERROR_JTAG_INIT_FAILED;
2605 nTRSTnOE = 0x0; /* No output enable for TRST*/
2607 nSRSTnOE = 0x0; /* No output enable for SRST*/
2610 high_direction = 0x0c;
2612 enum reset_types jtag_reset_config = jtag_get_reset_config();
2613 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2614 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2616 high_output |= nTRST;
2618 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2619 LOG_ERROR("can't set nSRST to push-pull on redbee");
2621 high_output |= nSRST;
2623 /* initialize high byte for jtag */
2624 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2625 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2626 return ERROR_JTAG_INIT_FAILED;
2632 static int jtagkey_init(void)
2635 low_direction = 0x1b;
2637 /* initialize low byte for jtag */
2638 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2639 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2640 return ERROR_JTAG_INIT_FAILED;
2643 if (strcmp(layout->name, "jtagkey") == 0) {
2648 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2649 || (strcmp(layout->name, "oocdlink") == 0)) {
2655 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2660 high_direction = 0x0f;
2662 enum reset_types jtag_reset_config = jtag_get_reset_config();
2663 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2664 high_output |= nTRSTnOE;
2665 high_output &= ~nTRST;
2667 high_output &= ~nTRSTnOE;
2668 high_output |= nTRST;
2671 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2672 high_output &= ~nSRSTnOE;
2673 high_output |= nSRST;
2675 high_output |= nSRSTnOE;
2676 high_output &= ~nSRST;
2679 /* initialize high byte for jtag */
2680 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2681 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2682 return ERROR_JTAG_INIT_FAILED;
2688 static int olimex_jtag_init(void)
2691 low_direction = 0x1b;
2693 /* initialize low byte for jtag */
2694 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2695 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2696 return ERROR_JTAG_INIT_FAILED;
2702 nSRSTnOE = 0x00;/* no output enable for nSRST */
2705 high_direction = 0x0f;
2707 enum reset_types jtag_reset_config = jtag_get_reset_config();
2708 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2709 high_output |= nTRSTnOE;
2710 high_output &= ~nTRST;
2712 high_output &= ~nTRSTnOE;
2713 high_output |= nTRST;
2716 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2717 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2719 high_output &= ~nSRST;
2721 /* turn red LED on */
2722 high_output |= 0x08;
2724 /* initialize high byte for jtag */
2725 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2726 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2727 return ERROR_JTAG_INIT_FAILED;
2733 static int flyswatter_init(int rev)
2736 low_direction = 0x7b;
2738 if ((rev < 0) || (rev > 3)) {
2739 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2740 return ERROR_JTAG_INIT_FAILED;
2744 low_direction |= 1 << 7;
2746 /* initialize low byte for jtag */
2747 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2748 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2749 return ERROR_JTAG_INIT_FAILED;
2753 nTRSTnOE = 0x0; /* not output enable for nTRST */
2755 nSRSTnOE = 0x00; /* no output enable for nSRST */
2760 high_direction = 0x0c;
2762 high_direction = 0x01;
2764 /* turn red LED3 on, LED2 off */
2765 high_output |= 0x08;
2767 /* initialize high byte for jtag */
2768 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2769 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2770 return ERROR_JTAG_INIT_FAILED;
2776 static int flyswatter1_init(void)
2778 return flyswatter_init(1);
2781 static int flyswatter2_init(void)
2783 return flyswatter_init(2);
2786 static int minimodule_init(void)
2788 low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2790 low_direction = 0xfb; /* 0xfb; */
2792 /* initialize low byte for jtag */
2793 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2794 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2795 return ERROR_JTAG_INIT_FAILED;
2802 high_direction = 0x05;
2804 /* turn red LED3 on, LED2 off */
2805 /* high_output |= 0x08; */
2807 /* initialize high byte for jtag */
2808 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2809 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2810 return ERROR_JTAG_INIT_FAILED;
2816 static int turtle_init(void)
2819 low_direction = 0x5b;
2821 /* initialize low byte for jtag */
2822 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2823 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2824 return ERROR_JTAG_INIT_FAILED;
2830 high_direction = 0x0C;
2832 /* initialize high byte for jtag */
2833 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2834 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2835 return ERROR_JTAG_INIT_FAILED;
2841 static int comstick_init(void)
2844 low_direction = 0x0b;
2846 /* initialize low byte for jtag */
2847 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2848 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2849 return ERROR_JTAG_INIT_FAILED;
2853 nTRSTnOE = 0x00; /* no output enable for nTRST */
2855 nSRSTnOE = 0x00; /* no output enable for nSRST */
2858 high_direction = 0x03;
2860 /* initialize high byte for jtag */
2861 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2862 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2863 return ERROR_JTAG_INIT_FAILED;
2869 static int stm32stick_init(void)
2872 low_direction = 0x8b;
2874 /* initialize low byte for jtag */
2875 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2876 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2877 return ERROR_JTAG_INIT_FAILED;
2881 nTRSTnOE = 0x00; /* no output enable for nTRST */
2883 nSRSTnOE = 0x00; /* no output enable for nSRST */
2886 high_direction = 0x03;
2888 /* initialize high byte for jtag */
2889 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2890 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2891 return ERROR_JTAG_INIT_FAILED;
2897 static int sheevaplug_init(void)
2900 low_direction = 0x1b;
2902 /* initialize low byte for jtag */
2903 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2904 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2905 return ERROR_JTAG_INIT_FAILED;
2914 high_direction = 0x0f;
2916 /* nTRST is always push-pull */
2917 high_output &= ~nTRSTnOE;
2918 high_output |= nTRST;
2920 /* nSRST is always open-drain */
2921 high_output |= nSRSTnOE;
2922 high_output &= ~nSRST;
2924 /* initialize high byte for jtag */
2925 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2926 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2927 return ERROR_JTAG_INIT_FAILED;
2933 static int cortino_jtag_init(void)
2936 low_direction = 0x1b;
2938 /* initialize low byte for jtag */
2939 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2940 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2941 return ERROR_JTAG_INIT_FAILED;
2945 nTRSTnOE = 0x00; /* no output enable for nTRST */
2947 nSRSTnOE = 0x00; /* no output enable for nSRST */
2950 high_direction = 0x03;
2952 /* initialize high byte for jtag */
2953 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2954 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2955 return ERROR_JTAG_INIT_FAILED;
2961 static int lisa_l_init(void)
2971 high_direction = 0x18;
2973 /* initialize high byte for jtag */
2974 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2975 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2976 return ERROR_JTAG_INIT_FAILED;
2979 return ftx232_dbus_write();
2982 static int flossjtag_init(void)
2992 high_direction = 0x18;
2994 /* initialize high byte for jtag */
2995 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2996 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2997 return ERROR_JTAG_INIT_FAILED;
3000 return ftx232_dbus_write();
3004 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3005 * the door for a number of different configurations
3007 * Known Implementations:
3008 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3010 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3011 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3012 * * ACBUS3 to transition 0->1 (OE rising edge)
3013 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3014 * * ADBUS5/GPIOL1 = EMU_EN = 1
3015 * * ADBUS6/GPIOL2 = EMU0 = 0
3016 * * ACBUS4/SPARE0 = EMU1 = 0
3017 * * CPLD logic: Disable loopback
3018 * * ACBUS6/SPARE2 = LOOPBACK = 0
3020 #define XDS100_nEMU_EN (1<<5)
3021 #define XDS100_nEMU0 (1<<6)
3023 #define XDS100_PWR_RST (1<<3)
3024 #define XDS100_nEMU1 (1<<4)
3025 #define XDS100_LOOPBACK (1<<6)
3026 static int xds100v2_init(void)
3028 /* These are in the lower byte */
3032 /* These aren't actually used on 14 pin connectors
3033 * These are in the upper byte */
3037 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3038 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3040 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3041 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3042 return ERROR_JTAG_INIT_FAILED;
3046 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3048 /* initialize high byte for jtag */
3049 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3050 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3051 return ERROR_JTAG_INIT_FAILED;
3054 high_output |= XDS100_PWR_RST;
3056 /* initialize high byte for jtag */
3057 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3058 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3059 return ERROR_JTAG_INIT_FAILED;
3065 static void olimex_jtag_blink(void)
3067 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3068 * ACBUS3 is bit 3 of the GPIOH port
3070 high_output ^= 0x08;
3073 buffer_write(high_output);
3074 buffer_write(high_direction);
3077 static void flyswatter_jtag_blink(unsigned char led)
3080 buffer_write(high_output ^ led);
3081 buffer_write(high_direction);
3084 static void flyswatter1_jtag_blink(void)
3087 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3089 flyswatter_jtag_blink(0xc);
3092 static void flyswatter2_jtag_blink(void)
3095 * Flyswatter2 only has one LED connected to ACBUS2
3097 flyswatter_jtag_blink(0x4);
3100 static void turtle_jtag_blink(void)
3103 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3105 if (high_output & 0x08)
3111 buffer_write(high_output);
3112 buffer_write(high_direction);
3115 static void lisa_l_blink(void)
3118 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3120 if (high_output & 0x10)
3126 buffer_write(high_output);
3127 buffer_write(high_direction);
3130 static void flossjtag_blink(void)
3133 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3135 if (high_output & 0x10)
3141 buffer_write(high_output);
3142 buffer_write(high_direction);
3145 static int ft2232_quit(void)
3147 #if BUILD_FT2232_FTD2XX == 1
3150 #elif BUILD_FT2232_LIBFTDI == 1
3151 ftdi_usb_close(&ftdic);
3153 ftdi_deinit(&ftdic);
3156 free(ft2232_buffer);
3157 ft2232_buffer = NULL;
3162 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3166 if (CMD_ARGC == 1) {
3167 ft2232_device_desc = strdup(CMD_ARGV[0]);
3168 cp = strchr(ft2232_device_desc, 0);
3169 /* under Win32, the FTD2XX driver appends an "A" to the end
3170 * of the description, this examines the given desc
3171 * and creates the 'missing' _A or non_A variable. */
3172 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3173 /* it was, so make this the "A" version. */
3174 ft2232_device_desc_A = ft2232_device_desc;
3175 /* and *CREATE* the non-A version. */
3176 strcpy(buf, ft2232_device_desc);
3177 cp = strchr(buf, 0);
3179 ft2232_device_desc = strdup(buf);
3181 /* <space > A not defined
3183 sprintf(buf, "%s A", ft2232_device_desc);
3184 ft2232_device_desc_A = strdup(buf);
3187 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3192 COMMAND_HANDLER(ft2232_handle_serial_command)
3195 ft2232_serial = strdup(CMD_ARGV[0]);
3197 return ERROR_COMMAND_SYNTAX_ERROR;
3202 COMMAND_HANDLER(ft2232_handle_layout_command)
3205 return ERROR_COMMAND_SYNTAX_ERROR;
3208 LOG_ERROR("already specified ft2232_layout %s",
3210 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3215 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3216 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3218 ft2232_channel = l->channel;
3223 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3227 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3229 if (CMD_ARGC > MAX_USB_IDS * 2) {
3230 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3231 "(maximum is %d pairs)", MAX_USB_IDS);
3232 CMD_ARGC = MAX_USB_IDS * 2;
3234 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3235 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3237 return ERROR_COMMAND_SYNTAX_ERROR;
3238 /* remove the incomplete trailing id */
3243 for (i = 0; i < CMD_ARGC; i += 2) {
3244 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3245 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3249 * Explicitly terminate, in case there are multiples instances of
3252 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3257 COMMAND_HANDLER(ft2232_handle_latency_command)
3260 ft2232_latency = atoi(CMD_ARGV[0]);
3262 return ERROR_COMMAND_SYNTAX_ERROR;
3267 COMMAND_HANDLER(ft2232_handle_channel_command)
3269 if (CMD_ARGC == 1) {
3270 ft2232_channel = atoi(CMD_ARGV[0]);
3271 if (ft2232_channel < 0 || ft2232_channel > 4)
3272 LOG_ERROR("ft2232_channel must be in the 0 to 4 range");
3274 LOG_ERROR("expected exactly one argument to ft2232_channel <ch>");
3279 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3283 /* 7 bits of either ones or zeros. */
3284 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3286 while (num_cycles > 0) {
3287 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3288 * at most 7 bits per invocation. Here we invoke it potentially
3291 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3293 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3294 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3295 retval = ERROR_JTAG_QUEUE_FAILED;
3300 /* there are no state transitions in this code, so omit state tracking */
3302 /* command "Clock Data to TMS/CS Pin (no Read)" */
3306 buffer_write(bitcount_per_command - 1);
3308 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3313 num_cycles -= bitcount_per_command;
3319 /* ---------------------------------------------------------------------
3320 * Support for IceBear JTAG adapter from Section5:
3321 * http://section5.ch/icebear
3323 * Author: Sten, debian@sansys-electronic.com
3326 /* Icebear pin layout
3328 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3329 * GND GND | 4 3| n.c.
3330 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3331 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3332 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3333 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3334 * ADBUS2 TDO |14 13| GND GND
3336 * ADBUS0 O L TCK ACBUS0 GND
3337 * ADBUS1 O L TDI ACBUS1 GND
3338 * ADBUS2 I TDO ACBUS2 n.c.
3339 * ADBUS3 O H TMS ACBUS3 n.c.
3345 static int icebear_jtag_init(void)
3347 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3348 low_output = 0x08; /* high: TMS; low: TCK TDI */
3352 enum reset_types jtag_reset_config = jtag_get_reset_config();
3353 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3354 low_direction &= ~nTRST; /* nTRST high impedance */
3356 low_direction |= nTRST;
3357 low_output |= nTRST;
3360 low_direction |= nSRST;
3361 low_output |= nSRST;
3363 /* initialize low byte for jtag */
3364 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3365 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3366 return ERROR_JTAG_INIT_FAILED;
3370 high_direction = 0x00;
3372 /* initialize high byte for jtag */
3373 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3374 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3375 return ERROR_JTAG_INIT_FAILED;
3381 static void icebear_jtag_reset(int trst, int srst)
3384 low_direction |= nTRST;
3385 low_output &= ~nTRST;
3386 } else if (trst == 0) {
3387 enum reset_types jtag_reset_config = jtag_get_reset_config();
3388 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3389 low_direction &= ~nTRST;
3391 low_output |= nTRST;
3395 low_output &= ~nSRST;
3397 low_output |= nSRST;
3399 /* command "set data bits low byte" */
3401 buffer_write(low_output);
3402 buffer_write(low_direction);
3404 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3411 /* ---------------------------------------------------------------------
3412 * Support for Signalyzer H2 and Signalyzer H4
3413 * JTAG adapter from Xverve Technologies Inc.
3414 * http://www.signalyzer.com or http://www.xverve.com
3416 * Author: Oleg Seiljus, oleg@signalyzer.com
3418 static unsigned char signalyzer_h_side;
3419 static unsigned int signalyzer_h_adapter_type;
3421 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3423 #if BUILD_FT2232_FTD2XX == 1
3424 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3427 #define SIGNALYZER_COMMAND_ADDR 128
3428 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3430 #define SIGNALYZER_COMMAND_VERSION 0x41
3431 #define SIGNALYZER_COMMAND_RESET 0x42
3432 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3433 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3434 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3435 #define SIGNALYZER_COMMAND_LED_SET 0x53
3436 #define SIGNALYZER_COMMAND_ADC 0x54
3437 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3438 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3439 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3440 #define SIGNALYZER_COMMAND_I2C 0x58
3442 #define SIGNALYZER_CHAN_A 1
3443 #define SIGNALYZER_CHAN_B 2
3444 /* LEDS use channel C */
3445 #define SIGNALYZER_CHAN_C 4
3447 #define SIGNALYZER_LED_GREEN 1
3448 #define SIGNALYZER_LED_RED 2
3450 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3451 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3452 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3453 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3454 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3457 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3459 #if BUILD_FT2232_FTD2XX == 1
3460 return FT_WriteEE(ftdih, address, value);
3461 #elif BUILD_FT2232_LIBFTDI == 1
3466 #if BUILD_FT2232_FTD2XX == 1
3467 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3469 return FT_ReadEE(ftdih, address, value);
3473 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3474 int on_time_ms, int off_time_ms, unsigned char cycles)
3476 unsigned char on_time;
3477 unsigned char off_time;
3479 if (on_time_ms < 0xFFFF)
3480 on_time = (unsigned char)(on_time_ms / 62);
3484 off_time = (unsigned char)(off_time_ms / 62);
3486 #if BUILD_FT2232_FTD2XX == 1
3489 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3490 ((uint32_t)(channel << 8) | led));
3491 if (status != FT_OK) {
3492 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3493 ftd2xx_status_string(status));
3494 return ERROR_JTAG_DEVICE_ERROR;
3497 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3498 ((uint32_t)(on_time << 8) | off_time));
3499 if (status != FT_OK) {
3500 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3501 ftd2xx_status_string(status));
3502 return ERROR_JTAG_DEVICE_ERROR;
3505 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3506 ((uint32_t)cycles));
3507 if (status != FT_OK) {
3508 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3509 ftd2xx_status_string(status));
3510 return ERROR_JTAG_DEVICE_ERROR;
3513 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3514 SIGNALYZER_COMMAND_LED_SET);
3515 if (status != FT_OK) {
3516 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3517 ftd2xx_status_string(status));
3518 return ERROR_JTAG_DEVICE_ERROR;
3522 #elif BUILD_FT2232_LIBFTDI == 1
3525 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3526 ((uint32_t)(channel << 8) | led));
3528 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3529 ftdi_get_error_string(&ftdic));
3530 return ERROR_JTAG_DEVICE_ERROR;
3533 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3534 ((uint32_t)(on_time << 8) | off_time));
3536 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3537 ftdi_get_error_string(&ftdic));
3538 return ERROR_JTAG_DEVICE_ERROR;
3541 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3544 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3545 ftdi_get_error_string(&ftdic));
3546 return ERROR_JTAG_DEVICE_ERROR;
3549 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3550 SIGNALYZER_COMMAND_LED_SET);
3552 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3553 ftdi_get_error_string(&ftdic));
3554 return ERROR_JTAG_DEVICE_ERROR;
3561 static int signalyzer_h_init(void)
3563 #if BUILD_FT2232_FTD2XX == 1
3570 uint16_t read_buf[12] = { 0 };
3572 /* turn on center green led */
3573 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3574 0xFFFF, 0x00, 0x00);
3576 /* determine what channel config wants to open
3577 * TODO: change me... current implementation is made to work
3578 * with openocd description parsing.
3580 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3583 signalyzer_h_side = *(end_of_desc - 1);
3584 if (signalyzer_h_side == 'B')
3585 signalyzer_h_side = SIGNALYZER_CHAN_B;
3587 signalyzer_h_side = SIGNALYZER_CHAN_A;
3589 LOG_ERROR("No Channel was specified");
3593 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3596 #if BUILD_FT2232_FTD2XX == 1
3597 /* read signalyzer versionining information */
3598 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3599 SIGNALYZER_COMMAND_VERSION);
3600 if (status != FT_OK) {
3601 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3602 ftd2xx_status_string(status));
3603 return ERROR_JTAG_DEVICE_ERROR;
3606 for (i = 0; i < 10; i++) {
3607 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3609 if (status != FT_OK) {
3610 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3611 ftd2xx_status_string(status));
3612 return ERROR_JTAG_DEVICE_ERROR;
3616 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3617 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3618 read_buf[4], read_buf[5], read_buf[6]);
3620 /* set gpio register */
3621 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3622 (uint32_t)(signalyzer_h_side << 8));
3623 if (status != FT_OK) {
3624 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3625 ftd2xx_status_string(status));
3626 return ERROR_JTAG_DEVICE_ERROR;
3629 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
3630 if (status != FT_OK) {
3631 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3632 ftd2xx_status_string(status));
3633 return ERROR_JTAG_DEVICE_ERROR;
3636 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3637 SIGNALYZER_COMMAND_GPIO_STATE);
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 /* read adapter type information */
3645 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3646 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3647 if (status != FT_OK) {
3648 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3649 ftd2xx_status_string(status));
3650 return ERROR_JTAG_DEVICE_ERROR;
3653 status = signalyzer_h_ctrl_write(
3654 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3655 if (status != FT_OK) {
3656 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3657 ftd2xx_status_string(status));
3658 return ERROR_JTAG_DEVICE_ERROR;
3661 status = signalyzer_h_ctrl_write(
3662 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3663 if (status != FT_OK) {
3664 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3665 ftd2xx_status_string(status));
3666 return ERROR_JTAG_DEVICE_ERROR;
3669 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3670 SIGNALYZER_COMMAND_I2C);
3671 if (status != FT_OK) {
3672 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3673 ftd2xx_status_string(status));
3674 return ERROR_JTAG_DEVICE_ERROR;
3679 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3680 if (status != FT_OK) {
3681 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3682 ftd2xx_status_string(status));
3683 return ERROR_JTAG_DEVICE_ERROR;
3686 if (read_buf[0] != 0x0498)
3687 signalyzer_h_adapter_type = 0x0000;
3689 for (i = 0; i < 4; i++) {
3690 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3691 if (status != FT_OK) {
3692 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3693 ftd2xx_status_string(status));
3694 return ERROR_JTAG_DEVICE_ERROR;
3698 signalyzer_h_adapter_type = read_buf[0];
3701 #elif BUILD_FT2232_LIBFTDI == 1
3702 /* currently libftdi does not allow reading individual eeprom
3703 * locations, therefore adapter type cannot be detected.
3704 * override with most common type
3706 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3709 enum reset_types jtag_reset_config = jtag_get_reset_config();
3711 /* ADAPTOR: EM_LT16_A */
3712 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3713 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3714 "detected. (HW: %2x).", (read_buf[1] >> 8));
3722 low_direction = 0x1b;
3725 high_direction = 0x0;
3727 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3728 low_direction &= ~nTRSTnOE; /* nTRST input */
3729 low_output &= ~nTRST; /* nTRST = 0 */
3731 low_direction |= nTRSTnOE; /* nTRST output */
3732 low_output |= nTRST; /* nTRST = 1 */
3735 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3736 low_direction |= nSRSTnOE; /* nSRST output */
3737 low_output |= nSRST; /* nSRST = 1 */
3739 low_direction &= ~nSRSTnOE; /* nSRST input */
3740 low_output &= ~nSRST; /* nSRST = 0 */
3743 #if BUILD_FT2232_FTD2XX == 1
3744 /* enable power to the module */
3745 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3746 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3747 if (status != FT_OK) {
3748 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3749 ftd2xx_status_string(status));
3750 return ERROR_JTAG_DEVICE_ERROR;
3753 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3754 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3755 if (status != FT_OK) {
3756 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3757 ftd2xx_status_string(status));
3758 return ERROR_JTAG_DEVICE_ERROR;
3761 /* set gpio mode register */
3762 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3763 (uint32_t)(signalyzer_h_side << 8));
3764 if (status != FT_OK) {
3765 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3766 ftd2xx_status_string(status));
3767 return ERROR_JTAG_DEVICE_ERROR;
3770 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3771 if (status != FT_OK) {
3772 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3773 ftd2xx_status_string(status));
3774 return ERROR_JTAG_DEVICE_ERROR;
3777 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3778 if (status != FT_OK) {
3779 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3780 ftd2xx_status_string(status));
3781 return ERROR_JTAG_DEVICE_ERROR;
3784 /* set gpio register */
3785 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3786 (uint32_t)(signalyzer_h_side << 8));
3787 if (status != FT_OK) {
3788 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3789 ftd2xx_status_string(status));
3790 return ERROR_JTAG_DEVICE_ERROR;
3793 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040);
3794 if (status != FT_OK) {
3795 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3796 ftd2xx_status_string(status));
3797 return ERROR_JTAG_DEVICE_ERROR;
3800 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3801 SIGNALYZER_COMMAND_GPIO_STATE);
3802 if (status != FT_OK) {
3803 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3804 ftd2xx_status_string(status));
3805 return ERROR_JTAG_DEVICE_ERROR;
3809 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3810 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3811 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3812 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3813 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3814 if (signalyzer_h_adapter_type
3815 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3816 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3817 "detected. (HW: %2x).", (read_buf[1] >> 8));
3818 else if (signalyzer_h_adapter_type
3819 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3820 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3821 "(ARM JTAG with PSU) detected. (HW: %2x).",
3822 (read_buf[1] >> 8));
3823 else if (signalyzer_h_adapter_type
3824 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3825 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3826 "detected. (HW: %2x).", (read_buf[1] >> 8));
3827 else if (signalyzer_h_adapter_type
3828 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3829 LOG_INFO("Signalyzer: EM-JTAG-P "
3830 "(Generic JTAG with PSU) detected. (HW: %2x).",
3831 (read_buf[1] >> 8));
3839 low_direction = 0x1b;
3842 high_direction = 0x1f;
3844 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3845 high_output |= nTRSTnOE;
3846 high_output &= ~nTRST;
3848 high_output &= ~nTRSTnOE;
3849 high_output |= nTRST;
3852 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3853 high_output &= ~nSRSTnOE;
3854 high_output |= nSRST;
3856 high_output |= nSRSTnOE;
3857 high_output &= ~nSRST;
3860 #if BUILD_FT2232_FTD2XX == 1
3861 /* enable power to the module */
3862 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3863 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3864 if (status != FT_OK) {
3865 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3866 ftd2xx_status_string(status));
3867 return ERROR_JTAG_DEVICE_ERROR;
3870 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3871 SIGNALYZER_COMMAND_POWERCONTROL_SET);
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 mode register (IO_16 and IO_17 set as analog
3879 * inputs, other is gpio)
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, 0x0060);
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_MODE);
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 /* set gpio register (all inputs, for -P modules,
3904 * PSU will be turned off)
3906 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3907 (uint32_t)(signalyzer_h_side << 8));
3908 if (status != FT_OK) {
3909 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3910 ftd2xx_status_string(status));
3911 return ERROR_JTAG_DEVICE_ERROR;
3914 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3915 if (status != FT_OK) {
3916 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3917 ftd2xx_status_string(status));
3918 return ERROR_JTAG_DEVICE_ERROR;
3921 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
3922 if (status != FT_OK) {
3923 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3924 ftd2xx_status_string(status));
3925 return ERROR_JTAG_DEVICE_ERROR;
3928 } else if (signalyzer_h_adapter_type == 0x0000) {
3929 LOG_INFO("Signalyzer: No external modules were detected.");
3937 low_direction = 0x1b;
3940 high_direction = 0x0;
3942 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3943 low_direction &= ~nTRSTnOE; /* nTRST input */
3944 low_output &= ~nTRST; /* nTRST = 0 */
3946 low_direction |= nTRSTnOE; /* nTRST output */
3947 low_output |= nTRST; /* nTRST = 1 */
3950 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3951 low_direction |= nSRSTnOE; /* nSRST output */
3952 low_output |= nSRST; /* nSRST = 1 */
3954 low_direction &= ~nSRSTnOE; /* nSRST input */
3955 low_output &= ~nSRST; /* nSRST = 0 */
3958 LOG_ERROR("Unknown module type is detected: %.4x",
3959 signalyzer_h_adapter_type);
3960 return ERROR_JTAG_DEVICE_ERROR;
3963 /* initialize low byte of controller for jtag operation */
3964 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3965 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3966 return ERROR_JTAG_INIT_FAILED;
3969 #if BUILD_FT2232_FTD2XX == 1
3970 if (ftdi_device == FT_DEVICE_2232H) {
3971 /* initialize high byte of controller for jtag operation */
3972 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3973 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3974 return ERROR_JTAG_INIT_FAILED;
3977 #elif BUILD_FT2232_LIBFTDI == 1
3978 if (ftdi_device == TYPE_2232H) {
3979 /* initialize high byte of controller for jtag operation */
3980 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3981 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3982 return ERROR_JTAG_INIT_FAILED;
3989 static void signalyzer_h_reset(int trst, int srst)
3991 enum reset_types jtag_reset_config = jtag_get_reset_config();
3993 /* ADAPTOR: EM_LT16_A */
3994 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3996 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3997 /* switch to output pin (output is low) */
3998 low_direction |= nTRSTnOE;
4000 /* switch output low */
4001 low_output &= ~nTRST;
4002 } else if (trst == 0) {
4003 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4004 /* switch to input pin (high-Z + internal
4005 * and external pullup) */
4006 low_direction &= ~nTRSTnOE;
4008 /* switch output high */
4009 low_output |= nTRST;
4013 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4014 /* switch output low */
4015 low_output &= ~nSRST;
4017 /* switch to output pin (output is low) */
4018 low_direction |= nSRSTnOE;
4019 } else if (srst == 0) {
4020 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4021 /* switch output high */
4022 low_output |= nSRST;
4024 /* switch to input pin (high-Z) */
4025 low_direction &= ~nSRSTnOE;
4028 /* command "set data bits low byte" */
4030 buffer_write(low_output);
4031 buffer_write(low_direction);
4032 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4033 "low_direction: 0x%2.2x",
4034 trst, srst, low_output, low_direction);
4036 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4037 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4038 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4039 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4040 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4042 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4043 high_output &= ~nTRSTnOE;
4045 high_output &= ~nTRST;
4046 } else if (trst == 0) {
4047 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4048 high_output |= nTRSTnOE;
4050 high_output |= nTRST;
4054 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4055 high_output &= ~nSRST;
4057 high_output &= ~nSRSTnOE;
4058 } else if (srst == 0) {
4059 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4060 high_output |= nSRST;
4062 high_output |= nSRSTnOE;
4065 /* command "set data bits high byte" */
4067 buffer_write(high_output);
4068 buffer_write(high_direction);
4069 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4070 "high_direction: 0x%2.2x",
4071 trst, srst, high_output, high_direction);
4072 } else if (signalyzer_h_adapter_type == 0x0000) {
4074 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4075 /* switch to output pin (output is low) */
4076 low_direction |= nTRSTnOE;
4078 /* switch output low */
4079 low_output &= ~nTRST;
4080 } else if (trst == 0) {
4081 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4082 /* switch to input pin (high-Z + internal
4083 * and external pullup) */
4084 low_direction &= ~nTRSTnOE;
4086 /* switch output high */
4087 low_output |= nTRST;
4091 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4092 /* switch output low */
4093 low_output &= ~nSRST;
4095 /* switch to output pin (output is low) */
4096 low_direction |= nSRSTnOE;
4097 } else if (srst == 0) {
4098 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4099 /* switch output high */
4100 low_output |= nSRST;
4102 /* switch to input pin (high-Z) */
4103 low_direction &= ~nSRSTnOE;
4106 /* command "set data bits low byte" */
4108 buffer_write(low_output);
4109 buffer_write(low_direction);
4110 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4111 "low_direction: 0x%2.2x",
4112 trst, srst, low_output, low_direction);
4116 static void signalyzer_h_blink(void)
4118 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4121 /********************************************************************
4122 * Support for KT-LINK
4123 * JTAG adapter from KRISTECH
4124 * http://www.kristech.eu
4125 *******************************************************************/
4126 static int ktlink_init(void)
4128 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4130 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4131 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4133 /* initialize low byte for jtag */
4134 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4135 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4136 return ERROR_JTAG_INIT_FAILED;
4144 high_output = 0x80; /* turn LED on */
4145 high_direction = 0xFF; /* all outputs */
4147 enum reset_types jtag_reset_config = jtag_get_reset_config();
4149 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4150 high_output |= nTRSTnOE;
4151 high_output &= ~nTRST;
4153 high_output &= ~nTRSTnOE;
4154 high_output |= nTRST;
4157 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4158 high_output &= ~nSRSTnOE;
4159 high_output |= nSRST;
4161 high_output |= nSRSTnOE;
4162 high_output &= ~nSRST;
4165 /* initialize high byte for jtag */
4166 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4167 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4168 return ERROR_JTAG_INIT_FAILED;
4174 static void ktlink_reset(int trst, int srst)
4176 enum reset_types jtag_reset_config = jtag_get_reset_config();
4179 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4180 high_output &= ~nTRSTnOE;
4182 high_output &= ~nTRST;
4183 } else if (trst == 0) {
4184 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4185 high_output |= nTRSTnOE;
4187 high_output |= nTRST;
4191 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4192 high_output &= ~nSRST;
4194 high_output &= ~nSRSTnOE;
4195 } else if (srst == 0) {
4196 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4197 high_output |= nSRST;
4199 high_output |= nSRSTnOE;
4202 buffer_write(0x82); /* command "set data bits high byte" */
4203 buffer_write(high_output);
4204 buffer_write(high_direction);
4205 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4212 static void ktlink_blink(void)
4214 /* LED connected to ACBUS7 */
4215 high_output ^= 0x80;
4217 buffer_write(0x82); /* command "set data bits high byte" */
4218 buffer_write(high_output);
4219 buffer_write(high_direction);
4222 /********************************************************************
4223 * Support for Digilent HS-1
4224 * JTAG adapter from Digilent
4225 * http://www.digilent.com
4226 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4227 *******************************************************************/
4229 static int digilent_hs1_init(void)
4231 /* the adapter only supports the base JTAG signals, no nTRST
4234 low_direction = 0x8b;
4236 /* initialize low byte for jtag */
4237 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4238 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4239 return ERROR_JTAG_INIT_FAILED;
4244 static void digilent_hs1_reset(int trst, int srst)
4246 /* Dummy function, no reset signals supported. */
4249 static const struct command_registration ft2232_command_handlers[] = {
4251 .name = "ft2232_device_desc",
4252 .handler = &ft2232_handle_device_desc_command,
4253 .mode = COMMAND_CONFIG,
4254 .help = "set the USB device description of the FTDI FT2232 device",
4255 .usage = "description_string",
4258 .name = "ft2232_serial",
4259 .handler = &ft2232_handle_serial_command,
4260 .mode = COMMAND_CONFIG,
4261 .help = "set the serial number of the FTDI FT2232 device",
4262 .usage = "serial_string",
4265 .name = "ft2232_layout",
4266 .handler = &ft2232_handle_layout_command,
4267 .mode = COMMAND_CONFIG,
4268 .help = "set the layout of the FT2232 GPIO signals used "
4269 "to control output-enables and reset signals",
4270 .usage = "layout_name",
4273 .name = "ft2232_vid_pid",
4274 .handler = &ft2232_handle_vid_pid_command,
4275 .mode = COMMAND_CONFIG,
4276 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4277 .usage = "(vid pid)* ",
4280 .name = "ft2232_latency",
4281 .handler = &ft2232_handle_latency_command,
4282 .mode = COMMAND_CONFIG,
4283 .help = "set the FT2232 latency timer to a new value",
4287 .name = "ft2232_channel",
4288 .handler = &ft2232_handle_channel_command,
4289 .mode = COMMAND_CONFIG,
4290 .help = "set the FT2232 channel to a new value",
4293 COMMAND_REGISTRATION_DONE
4296 struct jtag_interface ft2232_interface = {
4298 .supported = DEBUG_CAP_TMS_SEQ,
4299 .commands = ft2232_command_handlers,
4300 .transports = jtag_only,
4302 .init = ft2232_init,
4303 .quit = ft2232_quit,
4304 .speed = ft2232_speed,
4305 .speed_div = ft2232_speed_div,
4307 .execute_queue = ft2232_execute_queue,