ft2232: simplify ft2232_read_scan
[openocd/cortex.git] / src / jtag / drivers / ft2232.c
blob9d40b1c040f5790ccef5e3a1fa7eaca2404b45dc
1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
7 * *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
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. *
18 * *
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. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
30 /**
31 * @file
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
66 * found here:
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <helper/time_support.h>
86 #if IS_CYGWIN == 1
87 #include <windows.h>
88 #endif
90 #include <assert.h>
92 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
93 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
94 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
95 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
96 #endif
98 /* FT2232 access library includes */
99 #if BUILD_FT2232_FTD2XX == 1
100 #include <ftd2xx.h>
102 enum ftdi_interface
104 INTERFACE_ANY = 0,
105 INTERFACE_A = 1,
106 INTERFACE_B = 2,
107 INTERFACE_C = 3,
108 INTERFACE_D = 4
111 #elif BUILD_FT2232_LIBFTDI == 1
112 #include <ftdi.h>
113 #endif
115 /* max TCK for the high speed devices 30000 kHz */
116 #define FTDI_2232H_4232H_MAX_TCK 30000
117 /* max TCK for the full speed devices 6000 kHz */
118 #define FTDI_2232C_MAX_TCK 6000
119 /* this speed value tells that RTCK is requested */
120 #define RTCK_SPEED -1
123 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
124 * errors with a retry count of 100. Increasing it solves the problem for me.
125 * - Dimitar
127 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
128 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
129 * to something sane.
131 #define LIBFTDI_READ_RETRY_COUNT 2000
133 #ifndef BUILD_FT2232_HIGHSPEED
134 #if BUILD_FT2232_FTD2XX == 1
135 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
136 #elif BUILD_FT2232_LIBFTDI == 1
137 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
138 #endif
139 #endif
142 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
143 * stable state. Calling code must ensure that current state is stable,
144 * that verification is not done in here.
146 * @param num_cycles The number of clocks cycles to send.
147 * @param cmd The command to send.
149 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
151 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
153 static char * ft2232_device_desc_A = NULL;
154 static char* ft2232_device_desc = NULL;
155 static char* ft2232_serial = NULL;
156 static uint8_t ft2232_latency = 2;
157 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
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 {
165 char* name;
166 int (*init)(void);
167 void (*reset)(int trst, int srst);
168 void (*blink)(void);
169 int channel;
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 flyswatter_init(void);
179 static int turtle_init(void);
180 static int comstick_init(void);
181 static int stm32stick_init(void);
182 static int axm0432_jtag_init(void);
183 static int sheevaplug_init(void);
184 static int icebear_jtag_init(void);
185 static int cortino_jtag_init(void);
186 static int signalyzer_init(void);
187 static int signalyzer_h_init(void);
188 static int ktlink_init(void);
189 static int redbee_init(void);
191 /* reset procedures for supported layouts */
192 static void ftx23_reset(int trst, int srst);
193 static void jtagkey_reset(int trst, int srst);
194 static void olimex_jtag_reset(int trst, int srst);
195 static void flyswatter_reset(int trst, int srst);
196 static void turtle_reset(int trst, int srst);
197 static void comstick_reset(int trst, int srst);
198 static void stm32stick_reset(int trst, int srst);
199 static void axm0432_jtag_reset(int trst, int srst);
200 static void sheevaplug_reset(int trst, int srst);
201 static void icebear_jtag_reset(int trst, int srst);
202 static void signalyzer_h_reset(int trst, int srst);
203 static void ktlink_reset(int trst, int srst);
204 static void redbee_reset(int trst, int srst);
206 /* blink procedures for layouts that support a blinking led */
207 static void olimex_jtag_blink(void);
208 static void flyswatter_jtag_blink(void);
209 static void turtle_jtag_blink(void);
210 static void signalyzer_h_blink(void);
211 static void ktlink_blink(void);
213 static const struct ft2232_layout ft2232_layouts[] =
215 { .name = "usbjtag",
216 .init = usbjtag_init,
217 .reset = ftx23_reset,
219 { .name = "jtagkey",
220 .init = jtagkey_init,
221 .reset = jtagkey_reset,
223 { .name = "jtagkey_prototype_v1",
224 .init = jtagkey_init,
225 .reset = jtagkey_reset,
227 { .name = "oocdlink",
228 .init = jtagkey_init,
229 .reset = jtagkey_reset,
231 { .name = "signalyzer",
232 .init = signalyzer_init,
233 .reset = ftx23_reset,
235 { .name = "evb_lm3s811",
236 .init = lm3s811_jtag_init,
237 .reset = ftx23_reset,
239 { .name = "luminary_icdi",
240 .init = icdi_jtag_init,
241 .reset = ftx23_reset,
243 { .name = "olimex-jtag",
244 .init = olimex_jtag_init,
245 .reset = olimex_jtag_reset,
246 .blink = olimex_jtag_blink
248 { .name = "flyswatter",
249 .init = flyswatter_init,
250 .reset = flyswatter_reset,
251 .blink = flyswatter_jtag_blink
253 { .name = "turtelizer2",
254 .init = turtle_init,
255 .reset = turtle_reset,
256 .blink = turtle_jtag_blink
258 { .name = "comstick",
259 .init = comstick_init,
260 .reset = comstick_reset,
262 { .name = "stm32stick",
263 .init = stm32stick_init,
264 .reset = stm32stick_reset,
266 { .name = "axm0432_jtag",
267 .init = axm0432_jtag_init,
268 .reset = axm0432_jtag_reset,
270 { .name = "sheevaplug",
271 .init = sheevaplug_init,
272 .reset = sheevaplug_reset,
274 { .name = "icebear",
275 .init = icebear_jtag_init,
276 .reset = icebear_jtag_reset,
278 { .name = "cortino",
279 .init = cortino_jtag_init,
280 .reset = comstick_reset,
282 { .name = "signalyzer-h",
283 .init = signalyzer_h_init,
284 .reset = signalyzer_h_reset,
285 .blink = signalyzer_h_blink
287 { .name = "ktlink",
288 .init = ktlink_init,
289 .reset = ktlink_reset,
290 .blink = ktlink_blink
292 { .name = "redbee-econotag",
293 .init = redbee_init,
294 .reset = redbee_reset,
296 { .name = "redbee-usb",
297 .init = redbee_init,
298 .reset = redbee_reset,
299 .channel = INTERFACE_B,
301 { .name = NULL, /* END OF TABLE */ },
304 /* bitmask used to drive nTRST; usually a GPIOLx signal */
305 static uint8_t nTRST;
306 static uint8_t nTRSTnOE;
307 /* bitmask used to drive nSRST; usually a GPIOLx signal */
308 static uint8_t nSRST;
309 static uint8_t nSRSTnOE;
311 /** the layout being used with this debug session */
312 static const struct ft2232_layout *layout;
314 /** default bitmask values ddriven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
315 static uint8_t low_output = 0x0;
316 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
317 static uint8_t low_direction = 0x0;
318 /** default value bitmask for CBUS GPIOH(0..4) */
319 static uint8_t high_output = 0x0;
320 /** default direction bitmask for CBUS GPIOH(0..4) */
321 static uint8_t high_direction = 0x0;
323 #if BUILD_FT2232_FTD2XX == 1
324 static FT_HANDLE ftdih = NULL;
325 static FT_DEVICE ftdi_device = 0;
326 #elif BUILD_FT2232_LIBFTDI == 1
327 static struct ftdi_context ftdic;
328 static enum ftdi_chip_type ftdi_device;
329 #endif
331 static struct jtag_command* first_unsent; /* next command that has to be sent */
332 static int require_send;
334 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
336 "There is a significant difference between libftdi and libftd2xx. The latter
337 one allows to schedule up to 64*64 bytes of result data while libftdi fails
338 with more than 4*64. As a consequence, the FT2232 driver is forced to
339 perform around 16x more USB transactions for long command streams with TDO
340 capture when running with libftdi."
342 No idea how we get
343 #define FT2232_BUFFER_SIZE 131072
344 a comment would have been nice.
347 #define FT2232_BUFFER_SIZE 131072
349 static uint8_t* ft2232_buffer = NULL;
350 static int ft2232_buffer_size = 0;
351 static int ft2232_read_pointer = 0;
352 static int ft2232_expect_read = 0;
355 * Function buffer_write
356 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
357 * @param val is the byte to send.
359 static inline void buffer_write(uint8_t val)
361 assert(ft2232_buffer);
362 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
363 ft2232_buffer[ft2232_buffer_size++] = val;
367 * Function buffer_read
368 * returns a byte from the byte buffer.
370 static inline uint8_t buffer_read(void)
372 assert(ft2232_buffer);
373 assert(ft2232_read_pointer < ft2232_buffer_size);
374 return ft2232_buffer[ft2232_read_pointer++];
378 * Clocks out \a bit_count bits on the TMS line, starting with the least
379 * significant bit of tms_bits and progressing to more significant bits.
380 * Rigorous state transition logging is done here via tap_set_state().
382 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
383 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
384 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
385 * is often used for this, 0x4b.
387 * @param tms_bits Holds the sequence of bits to send.
388 * @param tms_count Tells how many bits in the sequence.
389 * @param tdi_bit A single bit to pass on to TDI before the first TCK
390 * cycle and held static for the duration of TMS clocking.
392 * See the MPSSE spec referenced above.
394 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
396 uint8_t tms_byte;
397 int i;
398 int tms_ndx; /* bit index into tms_byte */
400 assert(tms_count > 0);
402 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
403 mpsse_cmd, tms_bits, tms_count);
405 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
407 bool bit = tms_bits & 1;
409 if (bit)
410 tms_byte |= (1 << tms_ndx);
412 /* always do state transitions in public view */
413 tap_set_state(tap_state_transition(tap_get_state(), bit));
415 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
416 also increment.
418 ++tms_ndx;
420 if (tms_ndx == 7 || i == tms_count-1)
422 buffer_write(mpsse_cmd);
423 buffer_write(tms_ndx - 1);
425 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
426 TMS/CS and is held static for the duration of TMS/CS clocking.
428 buffer_write(tms_byte | (tdi_bit << 7));
434 * Function get_tms_buffer_requirements
435 * returns what clock_tms() will consume if called with
436 * same \a bit_count.
438 static inline int get_tms_buffer_requirements(int bit_count)
440 return ((bit_count + 6)/7) * 3;
444 * Function move_to_state
445 * moves the TAP controller from the current state to a
446 * \a goal_state through a path given by tap_get_tms_path(). State transition
447 * logging is performed by delegation to clock_tms().
449 * @param goal_state is the destination state for the move.
451 static void move_to_state(tap_state_t goal_state)
453 tap_state_t start_state = tap_get_state();
455 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
456 lookup of the required TMS pattern to move to this state from the
457 start state.
460 /* do the 2 lookups */
461 int tms_bits = tap_get_tms_path(start_state, goal_state);
462 int tms_count = tap_get_tms_path_len(start_state, goal_state);
464 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
466 clock_tms(0x4b, tms_bits, tms_count, 0);
469 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
471 #if BUILD_FT2232_FTD2XX == 1
472 FT_STATUS status;
473 DWORD dw_bytes_written;
474 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
476 *bytes_written = dw_bytes_written;
477 LOG_ERROR("FT_Write returned: %lu", status);
478 return ERROR_JTAG_DEVICE_ERROR;
480 else
482 *bytes_written = dw_bytes_written;
484 #elif BUILD_FT2232_LIBFTDI == 1
485 int retval;
486 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
488 *bytes_written = 0;
489 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
490 return ERROR_JTAG_DEVICE_ERROR;
492 else
494 *bytes_written = retval;
496 #endif
498 if (*bytes_written != (uint32_t)size)
500 return ERROR_JTAG_DEVICE_ERROR;
503 return ERROR_OK;
506 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
508 #if BUILD_FT2232_FTD2XX == 1
509 DWORD dw_bytes_read;
510 FT_STATUS status;
511 int timeout = 5;
512 *bytes_read = 0;
514 while ((*bytes_read < size) && timeout--)
516 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
517 *bytes_read, &dw_bytes_read)) != FT_OK)
519 *bytes_read = 0;
520 LOG_ERROR("FT_Read returned: %lu", status);
521 return ERROR_JTAG_DEVICE_ERROR;
523 *bytes_read += dw_bytes_read;
526 #elif BUILD_FT2232_LIBFTDI == 1
527 int retval;
528 int timeout = LIBFTDI_READ_RETRY_COUNT;
529 *bytes_read = 0;
531 while ((*bytes_read < size) && timeout--)
533 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
535 *bytes_read = 0;
536 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
537 return ERROR_JTAG_DEVICE_ERROR;
539 *bytes_read += retval;
542 #endif
544 if (*bytes_read < size)
546 LOG_ERROR("couldn't read enough bytes from "
547 "FT2232 device (%i < %i)",
548 (unsigned)*bytes_read,
549 (unsigned)size);
550 return ERROR_JTAG_DEVICE_ERROR;
553 return ERROR_OK;
556 static bool ft2232_device_is_highspeed(void)
558 #if BUILD_FT2232_FTD2XX == 1
559 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
560 #elif BUILD_FT2232_LIBFTDI == 1
561 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
562 #endif
566 * Commands that only apply to the FT2232H and FT4232H devices.
567 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
568 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
571 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
573 uint8_t buf = enable ? 0x96 : 0x97;
574 LOG_DEBUG("%2.2x", buf);
576 uint32_t bytes_written;
577 int retval;
579 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
581 LOG_ERROR("couldn't write command to %s adaptive clocking"
582 , enable ? "enable" : "disable");
583 return retval;
586 return ERROR_OK;
590 * Enable/disable the clk divide by 5 of the 60MHz master clock.
591 * This result in a JTAG clock speed range of 91.553Hz-6MHz
592 * respective 457.763Hz-30MHz.
594 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
596 uint32_t bytes_written;
597 uint8_t buf = enable ? 0x8b : 0x8a;
599 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
601 LOG_ERROR("couldn't write command to %s clk divide by 5"
602 , enable ? "enable" : "disable");
603 return ERROR_JTAG_INIT_FAILED;
605 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
606 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
608 return ERROR_OK;
611 static int ft2232_speed(int speed)
613 uint8_t buf[3];
614 int retval;
615 uint32_t bytes_written;
617 retval = ERROR_OK;
618 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
619 if (ft2232_device_is_highspeed())
620 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
621 else if (enable_adaptive_clocking)
623 LOG_ERROR("ft2232 device %lu does not support RTCK"
624 , (long unsigned int)ftdi_device);
625 return ERROR_FAIL;
628 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
629 return retval;
631 buf[0] = 0x86; /* command "set divisor" */
632 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
633 buf[2] = (speed >> 8) & 0xff; /* valueH */
635 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
636 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
638 LOG_ERROR("couldn't set FT2232 TCK speed");
639 return retval;
642 return ERROR_OK;
645 static int ft2232_speed_div(int speed, int* khz)
647 /* Take a look in the FT2232 manual,
648 * AN2232C-01 Command Processor for
649 * MPSSE and MCU Host Bus. Chapter 3.8 */
651 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
653 return ERROR_OK;
656 static int ft2232_khz(int khz, int* jtag_speed)
658 if (khz == 0)
660 if (ft2232_device_is_highspeed())
662 *jtag_speed = RTCK_SPEED;
663 return ERROR_OK;
665 else
667 LOG_DEBUG("RCLK not supported");
668 return ERROR_FAIL;
672 /* Take a look in the FT2232 manual,
673 * AN2232C-01 Command Processor for
674 * MPSSE and MCU Host Bus. Chapter 3.8
676 * We will calc here with a multiplier
677 * of 10 for better rounding later. */
679 /* Calc speed, (ft2232_max_tck / khz) - 1 */
680 /* Use 65000 for better rounding */
681 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
683 /* Add 0.9 for rounding */
684 *jtag_speed += 9;
686 /* Calc real speed */
687 *jtag_speed = *jtag_speed / 10;
689 /* Check if speed is greater than 0 */
690 if (*jtag_speed < 0)
692 *jtag_speed = 0;
695 /* Check max value */
696 if (*jtag_speed > 0xFFFF)
698 *jtag_speed = 0xFFFF;
701 return ERROR_OK;
704 static void ft2232_end_state(tap_state_t state)
706 if (tap_is_state_stable(state))
707 tap_set_end_state(state);
708 else
710 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
711 exit(-1);
715 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
717 int num_bytes = scan_size / 8;
718 int bits_left = scan_size % 8;
719 int cur_byte;
721 for (cur_byte = 0; cur_byte < num_bytes; cur_byte++)
723 buffer[cur_byte] = buffer_read();
726 /* Manage partial byte left from the clock data in/out instructions, if any */
727 if (bits_left > 1)
729 buffer[cur_byte] = buffer_read() >> 1;
731 else
733 buffer[cur_byte] = 0x0;
735 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
736 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
739 static void ft2232_debug_dump_buffer(void)
741 int i;
742 char line[256];
743 char* line_p = line;
745 for (i = 0; i < ft2232_buffer_size; i++)
747 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
748 if (i % 16 == 15)
750 LOG_DEBUG("%s", line);
751 line_p = line;
755 if (line_p != line)
756 LOG_DEBUG("%s", line);
759 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
761 struct jtag_command* cmd;
762 uint8_t* buffer;
763 int scan_size;
764 enum scan_type type;
765 int retval;
766 uint32_t bytes_written = 0;
767 uint32_t bytes_read = 0;
769 #ifdef _DEBUG_USB_IO_
770 struct timeval start, inter, inter2, end;
771 struct timeval d_inter, d_inter2, d_end;
772 #endif
774 #ifdef _DEBUG_USB_COMMS_
775 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
776 ft2232_debug_dump_buffer();
777 #endif
779 #ifdef _DEBUG_USB_IO_
780 gettimeofday(&start, NULL);
781 #endif
783 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
785 LOG_ERROR("couldn't write MPSSE commands to FT2232");
786 return retval;
789 #ifdef _DEBUG_USB_IO_
790 gettimeofday(&inter, NULL);
791 #endif
793 if (ft2232_expect_read)
795 /* FIXME this "timeout" is never changed ... */
796 int timeout = LIBFTDI_READ_RETRY_COUNT;
797 ft2232_buffer_size = 0;
799 #ifdef _DEBUG_USB_IO_
800 gettimeofday(&inter2, NULL);
801 #endif
803 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
805 LOG_ERROR("couldn't read from FT2232");
806 return retval;
809 #ifdef _DEBUG_USB_IO_
810 gettimeofday(&end, NULL);
812 timeval_subtract(&d_inter, &inter, &start);
813 timeval_subtract(&d_inter2, &inter2, &start);
814 timeval_subtract(&d_end, &end, &start);
816 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
817 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
818 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
819 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
820 #endif
822 ft2232_buffer_size = bytes_read;
824 if (ft2232_expect_read != ft2232_buffer_size)
826 LOG_ERROR("ft2232_expect_read (%i) != "
827 "ft2232_buffer_size (%i) "
828 "(%i retries)",
829 ft2232_expect_read,
830 ft2232_buffer_size,
831 LIBFTDI_READ_RETRY_COUNT - timeout);
832 ft2232_debug_dump_buffer();
834 exit(-1);
837 #ifdef _DEBUG_USB_COMMS_
838 LOG_DEBUG("read buffer (%i retries): %i bytes",
839 LIBFTDI_READ_RETRY_COUNT - timeout,
840 ft2232_buffer_size);
841 ft2232_debug_dump_buffer();
842 #endif
845 ft2232_expect_read = 0;
846 ft2232_read_pointer = 0;
848 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
849 * that wasn't handled by a caller-provided error handler
851 retval = ERROR_OK;
853 cmd = first;
854 while (cmd != last)
856 switch (cmd->type)
858 case JTAG_SCAN:
859 type = jtag_scan_type(cmd->cmd.scan);
860 if (type != SCAN_OUT)
862 scan_size = jtag_scan_size(cmd->cmd.scan);
863 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
864 ft2232_read_scan(type, buffer, scan_size);
865 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
866 retval = ERROR_JTAG_QUEUE_FAILED;
867 free(buffer);
869 break;
871 default:
872 break;
875 cmd = cmd->next;
878 ft2232_buffer_size = 0;
880 return retval;
884 * Function ft2232_add_pathmove
885 * moves the TAP controller from the current state to a new state through the
886 * given path, where path is an array of tap_state_t's.
888 * @param path is an array of tap_stat_t which gives the states to traverse through
889 * ending with the last state at path[num_states-1]
890 * @param num_states is the count of state steps to move through
892 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
894 int state_count = 0;
896 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
898 DEBUG_JTAG_IO("-");
900 /* this loop verifies that the path is legal and logs each state in the path */
901 while (num_states)
903 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
904 int bit_count = 0;
905 int num_states_batch = num_states > 7 ? 7 : num_states;
907 /* command "Clock Data to TMS/CS Pin (no Read)" */
908 buffer_write(0x4b);
910 /* number of states remaining */
911 buffer_write(num_states_batch - 1);
913 while (num_states_batch--) {
914 /* either TMS=0 or TMS=1 must work ... */
915 if (tap_state_transition(tap_get_state(), false)
916 == path[state_count])
917 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
918 else if (tap_state_transition(tap_get_state(), true)
919 == path[state_count])
920 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
922 /* ... or else the caller goofed BADLY */
923 else {
924 LOG_ERROR("BUG: %s -> %s isn't a valid "
925 "TAP state transition",
926 tap_state_name(tap_get_state()),
927 tap_state_name(path[state_count]));
928 exit(-1);
931 tap_set_state(path[state_count]);
932 state_count++;
933 num_states--;
936 buffer_write(tms_byte);
938 tap_set_end_state(tap_get_state());
941 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
943 int num_bytes = (scan_size + 7) / 8;
944 int bits_left = scan_size;
945 int cur_byte = 0;
946 int last_bit;
948 if (!ir_scan)
950 if (tap_get_state() != TAP_DRSHIFT)
952 move_to_state(TAP_DRSHIFT);
955 else
957 if (tap_get_state() != TAP_IRSHIFT)
959 move_to_state(TAP_IRSHIFT);
963 /* add command for complete bytes */
964 while (num_bytes > 1)
966 int thisrun_bytes;
967 if (type == SCAN_IO)
969 /* Clock Data Bytes In and Out LSB First */
970 buffer_write(0x39);
971 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
973 else if (type == SCAN_OUT)
975 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
976 buffer_write(0x19);
977 /* LOG_DEBUG("added TDI bytes (o)"); */
979 else if (type == SCAN_IN)
981 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
982 buffer_write(0x28);
983 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
986 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
987 num_bytes -= thisrun_bytes;
989 buffer_write((uint8_t) (thisrun_bytes - 1));
990 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
992 if (type != SCAN_IN)
994 /* add complete bytes */
995 while (thisrun_bytes-- > 0)
997 buffer_write(buffer[cur_byte++]);
998 bits_left -= 8;
1001 else /* (type == SCAN_IN) */
1003 bits_left -= 8 * (thisrun_bytes);
1007 /* the most signifcant bit is scanned during TAP movement */
1008 if (type != SCAN_IN)
1009 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1010 else
1011 last_bit = 0;
1013 /* process remaining bits but the last one */
1014 if (bits_left > 1)
1016 if (type == SCAN_IO)
1018 /* Clock Data Bits In and Out LSB First */
1019 buffer_write(0x3b);
1020 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1022 else if (type == SCAN_OUT)
1024 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1025 buffer_write(0x1b);
1026 /* LOG_DEBUG("added TDI bits (o)"); */
1028 else if (type == SCAN_IN)
1030 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1031 buffer_write(0x2a);
1032 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1035 buffer_write(bits_left - 2);
1036 if (type != SCAN_IN)
1037 buffer_write(buffer[cur_byte]);
1040 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1041 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1043 if (type == SCAN_IO)
1045 /* Clock Data Bits In and Out LSB First */
1046 buffer_write(0x3b);
1047 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1049 else if (type == SCAN_OUT)
1051 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1052 buffer_write(0x1b);
1053 /* LOG_DEBUG("added TDI bits (o)"); */
1055 else if (type == SCAN_IN)
1057 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1058 buffer_write(0x2a);
1059 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1061 buffer_write(0x0);
1062 buffer_write(last_bit);
1064 else
1066 int tms_bits;
1067 int tms_count;
1068 uint8_t mpsse_cmd;
1070 /* move from Shift-IR/DR to end state */
1071 if (type != SCAN_OUT)
1073 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1074 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1075 tms_bits = 0x01;
1076 tms_count = 2;
1077 /* Clock Data to TMS/CS Pin with Read */
1078 mpsse_cmd = 0x6b;
1080 else
1082 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1083 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1084 /* Clock Data to TMS/CS Pin (no Read) */
1085 mpsse_cmd = 0x4b;
1088 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1089 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1092 if (tap_get_state() != tap_get_end_state())
1094 move_to_state(tap_get_end_state());
1098 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1100 int num_bytes = (scan_size + 7) / 8;
1101 int bits_left = scan_size;
1102 int cur_byte = 0;
1103 int last_bit;
1104 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1105 uint8_t* receive_pointer = receive_buffer;
1106 uint32_t bytes_written;
1107 uint32_t bytes_read;
1108 int retval;
1109 int thisrun_read = 0;
1111 if (cmd->ir_scan)
1113 LOG_ERROR("BUG: large IR scans are not supported");
1114 exit(-1);
1117 if (tap_get_state() != TAP_DRSHIFT)
1119 move_to_state(TAP_DRSHIFT);
1122 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1124 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1125 exit(-1);
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)
1134 int thisrun_bytes;
1136 if (type == SCAN_IO)
1138 /* Clock Data Bytes In and Out LSB First */
1139 buffer_write(0x39);
1140 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1142 else if (type == SCAN_OUT)
1144 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1145 buffer_write(0x19);
1146 /* LOG_DEBUG("added TDI bytes (o)"); */
1148 else if (type == SCAN_IN)
1150 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1151 buffer_write(0x28);
1152 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1155 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1156 thisrun_read = thisrun_bytes;
1157 num_bytes -= thisrun_bytes;
1158 buffer_write((uint8_t) (thisrun_bytes - 1));
1159 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1161 if (type != SCAN_IN)
1163 /* add complete bytes */
1164 while (thisrun_bytes-- > 0)
1166 buffer_write(buffer[cur_byte]);
1167 cur_byte++;
1168 bits_left -= 8;
1171 else /* (type == SCAN_IN) */
1173 bits_left -= 8 * (thisrun_bytes);
1176 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1178 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1179 exit(-1);
1181 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1182 ft2232_buffer_size,
1183 (int)bytes_written);
1184 ft2232_buffer_size = 0;
1186 if (type != SCAN_OUT)
1188 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1190 LOG_ERROR("couldn't read from FT2232");
1191 exit(-1);
1193 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1194 thisrun_read,
1195 (int)bytes_read);
1196 receive_pointer += bytes_read;
1200 thisrun_read = 0;
1202 /* the most signifcant bit is scanned during TAP movement */
1203 if (type != SCAN_IN)
1204 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1205 else
1206 last_bit = 0;
1208 /* process remaining bits but the last one */
1209 if (bits_left > 1)
1211 if (type == SCAN_IO)
1213 /* Clock Data Bits In and Out LSB First */
1214 buffer_write(0x3b);
1215 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1217 else if (type == SCAN_OUT)
1219 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1220 buffer_write(0x1b);
1221 /* LOG_DEBUG("added TDI bits (o)"); */
1223 else if (type == SCAN_IN)
1225 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1226 buffer_write(0x2a);
1227 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1229 buffer_write(bits_left - 2);
1230 if (type != SCAN_IN)
1231 buffer_write(buffer[cur_byte]);
1233 if (type != SCAN_OUT)
1234 thisrun_read += 2;
1237 if (tap_get_end_state() == TAP_DRSHIFT)
1239 if (type == SCAN_IO)
1241 /* Clock Data Bits In and Out LSB First */
1242 buffer_write(0x3b);
1243 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1245 else if (type == SCAN_OUT)
1247 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1248 buffer_write(0x1b);
1249 /* LOG_DEBUG("added TDI bits (o)"); */
1251 else if (type == SCAN_IN)
1253 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1254 buffer_write(0x2a);
1255 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1257 buffer_write(0x0);
1258 buffer_write(last_bit);
1260 else
1262 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1263 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1264 uint8_t mpsse_cmd;
1266 /* move from Shift-IR/DR to end state */
1267 if (type != SCAN_OUT)
1269 /* Clock Data to TMS/CS Pin with Read */
1270 mpsse_cmd = 0x6b;
1271 /* LOG_DEBUG("added TMS scan (read)"); */
1273 else
1275 /* Clock Data to TMS/CS Pin (no Read) */
1276 mpsse_cmd = 0x4b;
1277 /* LOG_DEBUG("added TMS scan (no read)"); */
1280 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1281 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1284 if (type != SCAN_OUT)
1285 thisrun_read += 1;
1287 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1289 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1290 exit(-1);
1292 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1293 ft2232_buffer_size,
1294 (int)bytes_written);
1295 ft2232_buffer_size = 0;
1297 if (type != SCAN_OUT)
1299 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1301 LOG_ERROR("couldn't read from FT2232");
1302 exit(-1);
1304 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1305 thisrun_read,
1306 (int)bytes_read);
1307 receive_pointer += bytes_read;
1310 return ERROR_OK;
1313 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1315 int predicted_size = 3;
1316 int num_bytes = (scan_size - 1) / 8;
1318 if (tap_get_state() != TAP_DRSHIFT)
1319 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1321 if (type == SCAN_IN) /* only from device to host */
1323 /* complete bytes */
1324 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1326 /* remaining bits - 1 (up to 7) */
1327 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1329 else /* host to device, or bidirectional */
1331 /* complete bytes */
1332 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1334 /* remaining bits -1 (up to 7) */
1335 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1338 return predicted_size;
1341 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1343 int predicted_size = 0;
1345 if (type != SCAN_OUT)
1347 /* complete bytes */
1348 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1350 /* remaining bits - 1 */
1351 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1353 /* last bit (from TMS scan) */
1354 predicted_size += 1;
1357 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1359 return predicted_size;
1362 /* semi-generic FT2232/FT4232 reset code */
1363 static void ftx23_reset(int trst, int srst)
1365 enum reset_types jtag_reset_config = jtag_get_reset_config();
1366 if (trst == 1)
1368 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1369 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1370 else
1371 low_output &= ~nTRST; /* switch output low */
1373 else if (trst == 0)
1375 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1376 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1377 else
1378 low_output |= nTRST; /* switch output high */
1381 if (srst == 1)
1383 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1384 low_output &= ~nSRST; /* switch output low */
1385 else
1386 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1388 else if (srst == 0)
1390 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1391 low_output |= nSRST; /* switch output high */
1392 else
1393 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1396 /* command "set data bits low byte" */
1397 buffer_write(0x80);
1398 buffer_write(low_output);
1399 buffer_write(low_direction);
1402 static void jtagkey_reset(int trst, int srst)
1404 enum reset_types jtag_reset_config = jtag_get_reset_config();
1405 if (trst == 1)
1407 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1408 high_output &= ~nTRSTnOE;
1409 else
1410 high_output &= ~nTRST;
1412 else if (trst == 0)
1414 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1415 high_output |= nTRSTnOE;
1416 else
1417 high_output |= nTRST;
1420 if (srst == 1)
1422 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1423 high_output &= ~nSRST;
1424 else
1425 high_output &= ~nSRSTnOE;
1427 else if (srst == 0)
1429 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1430 high_output |= nSRST;
1431 else
1432 high_output |= nSRSTnOE;
1435 /* command "set data bits high byte" */
1436 buffer_write(0x82);
1437 buffer_write(high_output);
1438 buffer_write(high_direction);
1439 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1440 high_direction);
1443 static void olimex_jtag_reset(int trst, int srst)
1445 enum reset_types jtag_reset_config = jtag_get_reset_config();
1446 if (trst == 1)
1448 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1449 high_output &= ~nTRSTnOE;
1450 else
1451 high_output &= ~nTRST;
1453 else if (trst == 0)
1455 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1456 high_output |= nTRSTnOE;
1457 else
1458 high_output |= nTRST;
1461 if (srst == 1)
1463 high_output |= nSRST;
1465 else if (srst == 0)
1467 high_output &= ~nSRST;
1470 /* command "set data bits high byte" */
1471 buffer_write(0x82);
1472 buffer_write(high_output);
1473 buffer_write(high_direction);
1474 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1475 high_direction);
1478 static void axm0432_jtag_reset(int trst, int srst)
1480 if (trst == 1)
1482 tap_set_state(TAP_RESET);
1483 high_output &= ~nTRST;
1485 else if (trst == 0)
1487 high_output |= nTRST;
1490 if (srst == 1)
1492 high_output &= ~nSRST;
1494 else if (srst == 0)
1496 high_output |= nSRST;
1499 /* command "set data bits low byte" */
1500 buffer_write(0x82);
1501 buffer_write(high_output);
1502 buffer_write(high_direction);
1503 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1504 high_direction);
1507 static void flyswatter_reset(int trst, int srst)
1509 if (trst == 1)
1511 low_output &= ~nTRST;
1513 else if (trst == 0)
1515 low_output |= nTRST;
1518 if (srst == 1)
1520 low_output |= nSRST;
1522 else if (srst == 0)
1524 low_output &= ~nSRST;
1527 /* command "set data bits low byte" */
1528 buffer_write(0x80);
1529 buffer_write(low_output);
1530 buffer_write(low_direction);
1531 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1534 static void turtle_reset(int trst, int srst)
1536 trst = trst;
1538 if (srst == 1)
1540 low_output |= nSRST;
1542 else if (srst == 0)
1544 low_output &= ~nSRST;
1547 /* command "set data bits low byte" */
1548 buffer_write(0x80);
1549 buffer_write(low_output);
1550 buffer_write(low_direction);
1551 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1554 static void comstick_reset(int trst, int srst)
1556 if (trst == 1)
1558 high_output &= ~nTRST;
1560 else if (trst == 0)
1562 high_output |= nTRST;
1565 if (srst == 1)
1567 high_output &= ~nSRST;
1569 else if (srst == 0)
1571 high_output |= nSRST;
1574 /* command "set data bits high byte" */
1575 buffer_write(0x82);
1576 buffer_write(high_output);
1577 buffer_write(high_direction);
1578 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1579 high_direction);
1582 static void stm32stick_reset(int trst, int srst)
1584 if (trst == 1)
1586 high_output &= ~nTRST;
1588 else if (trst == 0)
1590 high_output |= nTRST;
1593 if (srst == 1)
1595 low_output &= ~nSRST;
1597 else if (srst == 0)
1599 low_output |= nSRST;
1602 /* command "set data bits low byte" */
1603 buffer_write(0x80);
1604 buffer_write(low_output);
1605 buffer_write(low_direction);
1607 /* command "set data bits high byte" */
1608 buffer_write(0x82);
1609 buffer_write(high_output);
1610 buffer_write(high_direction);
1611 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1612 high_direction);
1615 static void sheevaplug_reset(int trst, int srst)
1617 if (trst == 1)
1618 high_output &= ~nTRST;
1619 else if (trst == 0)
1620 high_output |= nTRST;
1622 if (srst == 1)
1623 high_output &= ~nSRSTnOE;
1624 else if (srst == 0)
1625 high_output |= nSRSTnOE;
1627 /* command "set data bits high byte" */
1628 buffer_write(0x82);
1629 buffer_write(high_output);
1630 buffer_write(high_direction);
1631 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1634 static void redbee_reset(int trst, int srst)
1636 if (trst == 1)
1638 tap_set_state(TAP_RESET);
1639 high_output &= ~nTRST;
1641 else if (trst == 0)
1643 high_output |= nTRST;
1646 if (srst == 1)
1648 high_output &= ~nSRST;
1650 else if (srst == 0)
1652 high_output |= nSRST;
1655 /* command "set data bits low byte" */
1656 buffer_write(0x82);
1657 buffer_write(high_output);
1658 buffer_write(high_direction);
1659 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1660 "high_direction: 0x%2.2x", trst, srst, high_output,
1661 high_direction);
1664 static int ft2232_execute_runtest(struct jtag_command *cmd)
1666 int retval;
1667 int i;
1668 int predicted_size = 0;
1669 retval = ERROR_OK;
1671 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1672 cmd->cmd.runtest->num_cycles,
1673 tap_state_name(cmd->cmd.runtest->end_state));
1675 /* only send the maximum buffer size that FT2232C can handle */
1676 predicted_size = 0;
1677 if (tap_get_state() != TAP_IDLE)
1678 predicted_size += 3;
1679 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1680 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1681 predicted_size += 3;
1682 if (tap_get_end_state() != TAP_IDLE)
1683 predicted_size += 3;
1684 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1686 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1687 retval = ERROR_JTAG_QUEUE_FAILED;
1688 require_send = 0;
1689 first_unsent = cmd;
1691 if (tap_get_state() != TAP_IDLE)
1693 move_to_state(TAP_IDLE);
1694 require_send = 1;
1696 i = cmd->cmd.runtest->num_cycles;
1697 while (i > 0)
1699 /* there are no state transitions in this code, so omit state tracking */
1701 /* command "Clock Data to TMS/CS Pin (no Read)" */
1702 buffer_write(0x4b);
1704 /* scan 7 bits */
1705 buffer_write((i > 7) ? 6 : (i - 1));
1707 /* TMS data bits */
1708 buffer_write(0x0);
1710 i -= (i > 7) ? 7 : i;
1711 /* LOG_DEBUG("added TMS scan (no read)"); */
1714 ft2232_end_state(cmd->cmd.runtest->end_state);
1716 if (tap_get_state() != tap_get_end_state())
1718 move_to_state(tap_get_end_state());
1721 require_send = 1;
1722 DEBUG_JTAG_IO("runtest: %i, end in %s",
1723 cmd->cmd.runtest->num_cycles,
1724 tap_state_name(tap_get_end_state()));
1725 return retval;
1728 static int ft2232_execute_statemove(struct jtag_command *cmd)
1730 int predicted_size = 0;
1731 int retval = ERROR_OK;
1733 DEBUG_JTAG_IO("statemove end in %s",
1734 tap_state_name(cmd->cmd.statemove->end_state));
1736 /* only send the maximum buffer size that FT2232C can handle */
1737 predicted_size = 3;
1738 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1740 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1741 retval = ERROR_JTAG_QUEUE_FAILED;
1742 require_send = 0;
1743 first_unsent = cmd;
1745 ft2232_end_state(cmd->cmd.statemove->end_state);
1747 /* For TAP_RESET, ignore the current recorded state. It's often
1748 * wrong at server startup, and this transation is critical whenever
1749 * it's requested.
1751 if (tap_get_end_state() == TAP_RESET) {
1752 clock_tms(0x4b, 0xff, 5, 0);
1753 require_send = 1;
1755 /* shortest-path move to desired end state */
1756 } else if (tap_get_state() != tap_get_end_state())
1758 move_to_state(tap_get_end_state());
1759 require_send = 1;
1762 return retval;
1766 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1767 * (or SWD) state machine.
1769 static int ft2232_execute_tms(struct jtag_command *cmd)
1771 int retval = ERROR_OK;
1772 unsigned num_bits = cmd->cmd.tms->num_bits;
1773 const uint8_t *bits = cmd->cmd.tms->bits;
1774 unsigned count;
1776 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1778 /* only send the maximum buffer size that FT2232C can handle */
1779 count = 3 * DIV_ROUND_UP(num_bits, 4);
1780 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1781 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1782 retval = ERROR_JTAG_QUEUE_FAILED;
1784 require_send = 0;
1785 first_unsent = cmd;
1788 /* Shift out in batches of at most 6 bits; there's a report of an
1789 * FT2232 bug in this area, where shifting exactly 7 bits can make
1790 * problems with TMS signaling for the last clock cycle:
1792 * http://developer.intra2net.com/mailarchive/html/
1793 * libftdi/2009/msg00292.html
1795 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1797 * Note that pathmoves in JTAG are not often seven bits, so that
1798 * isn't a particularly likely situation outside of "special"
1799 * signaling such as switching between JTAG and SWD modes.
1801 while (num_bits) {
1802 if (num_bits <= 6) {
1803 buffer_write(0x4b);
1804 buffer_write(num_bits - 1);
1805 buffer_write(*bits & 0x3f);
1806 break;
1809 /* Yes, this is lazy ... we COULD shift out more data
1810 * bits per operation, but doing it in nybbles is easy
1812 buffer_write(0x4b);
1813 buffer_write(3);
1814 buffer_write(*bits & 0xf);
1815 num_bits -= 4;
1817 count = (num_bits > 4) ? 4 : num_bits;
1819 buffer_write(0x4b);
1820 buffer_write(count - 1);
1821 buffer_write((*bits >> 4) & 0xf);
1822 num_bits -= count;
1824 bits++;
1827 require_send = 1;
1828 return retval;
1831 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1833 int predicted_size = 0;
1834 int retval = ERROR_OK;
1836 tap_state_t* path = cmd->cmd.pathmove->path;
1837 int num_states = cmd->cmd.pathmove->num_states;
1839 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1840 tap_state_name(tap_get_state()),
1841 tap_state_name(path[num_states-1]));
1843 /* only send the maximum buffer size that FT2232C can handle */
1844 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1845 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1847 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1848 retval = ERROR_JTAG_QUEUE_FAILED;
1850 require_send = 0;
1851 first_unsent = cmd;
1854 ft2232_add_pathmove(path, num_states);
1855 require_send = 1;
1857 return retval;
1860 static int ft2232_execute_scan(struct jtag_command *cmd)
1862 uint8_t* buffer;
1863 int scan_size; /* size of IR or DR scan */
1864 int predicted_size = 0;
1865 int retval = ERROR_OK;
1867 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1869 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1871 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1873 predicted_size = ft2232_predict_scan_out(scan_size, type);
1874 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1876 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1877 /* unsent commands before this */
1878 if (first_unsent != cmd)
1879 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1880 retval = ERROR_JTAG_QUEUE_FAILED;
1882 /* current command */
1883 ft2232_end_state(cmd->cmd.scan->end_state);
1884 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1885 require_send = 0;
1886 first_unsent = cmd->next;
1887 if (buffer)
1888 free(buffer);
1889 return retval;
1891 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1893 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1894 first_unsent,
1895 cmd);
1896 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1897 retval = ERROR_JTAG_QUEUE_FAILED;
1898 require_send = 0;
1899 first_unsent = cmd;
1901 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1902 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1903 ft2232_end_state(cmd->cmd.scan->end_state);
1904 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1905 require_send = 1;
1906 if (buffer)
1907 free(buffer);
1908 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1909 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1910 tap_state_name(tap_get_end_state()));
1911 return retval;
1915 static int ft2232_execute_reset(struct jtag_command *cmd)
1917 int retval;
1918 int predicted_size = 0;
1919 retval = ERROR_OK;
1921 DEBUG_JTAG_IO("reset trst: %i srst %i",
1922 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1924 /* only send the maximum buffer size that FT2232C can handle */
1925 predicted_size = 3;
1926 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1928 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1929 retval = ERROR_JTAG_QUEUE_FAILED;
1930 require_send = 0;
1931 first_unsent = cmd;
1934 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1936 tap_set_state(TAP_RESET);
1939 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1940 require_send = 1;
1942 DEBUG_JTAG_IO("trst: %i, srst: %i",
1943 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1944 return retval;
1947 static int ft2232_execute_sleep(struct jtag_command *cmd)
1949 int retval;
1950 retval = ERROR_OK;
1952 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1954 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1955 retval = ERROR_JTAG_QUEUE_FAILED;
1956 first_unsent = cmd->next;
1957 jtag_sleep(cmd->cmd.sleep->us);
1958 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1959 cmd->cmd.sleep->us,
1960 tap_state_name(tap_get_state()));
1961 return retval;
1964 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1966 int retval;
1967 retval = ERROR_OK;
1969 /* this is only allowed while in a stable state. A check for a stable
1970 * state was done in jtag_add_clocks()
1972 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1973 retval = ERROR_JTAG_QUEUE_FAILED;
1974 DEBUG_JTAG_IO("clocks %i while in %s",
1975 cmd->cmd.stableclocks->num_cycles,
1976 tap_state_name(tap_get_state()));
1977 return retval;
1980 static int ft2232_execute_command(struct jtag_command *cmd)
1982 int retval;
1984 switch (cmd->type)
1986 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1987 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1988 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
1989 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1990 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1991 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1992 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1993 case JTAG_TMS:
1994 retval = ft2232_execute_tms(cmd);
1995 break;
1996 default:
1997 LOG_ERROR("BUG: unknown JTAG command type encountered");
1998 retval = ERROR_JTAG_QUEUE_FAILED;
1999 break;
2001 return retval;
2004 static int ft2232_execute_queue(void)
2006 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2007 int retval;
2009 first_unsent = cmd; /* next command that has to be sent */
2010 require_send = 0;
2012 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2013 * that wasn't handled by a caller-provided error handler
2015 retval = ERROR_OK;
2017 ft2232_buffer_size = 0;
2018 ft2232_expect_read = 0;
2020 /* blink, if the current layout has that feature */
2021 if (layout->blink)
2022 layout->blink();
2024 while (cmd)
2026 if (ft2232_execute_command(cmd) != ERROR_OK)
2027 retval = ERROR_JTAG_QUEUE_FAILED;
2028 /* Start reading input before FT2232 TX buffer fills up */
2029 cmd = cmd->next;
2030 if (ft2232_expect_read > 256)
2032 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2033 retval = ERROR_JTAG_QUEUE_FAILED;
2034 first_unsent = cmd;
2038 if (require_send > 0)
2039 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2040 retval = ERROR_JTAG_QUEUE_FAILED;
2042 return retval;
2045 #if BUILD_FT2232_FTD2XX == 1
2046 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2048 FT_STATUS status;
2049 DWORD deviceID;
2050 char SerialNumber[16];
2051 char Description[64];
2052 DWORD openex_flags = 0;
2053 char* openex_string = NULL;
2054 uint8_t latency_timer;
2056 if (layout == NULL) {
2057 LOG_WARNING("No ft2232 layout specified'");
2058 return ERROR_JTAG_INIT_FAILED;
2061 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2063 #if IS_WIN32 == 0
2064 /* Add non-standard Vid/Pid to the linux driver */
2065 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2067 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2069 #endif
2071 if (ft2232_device_desc && ft2232_serial)
2073 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2074 ft2232_device_desc = NULL;
2077 if (ft2232_device_desc)
2079 openex_string = ft2232_device_desc;
2080 openex_flags = FT_OPEN_BY_DESCRIPTION;
2082 else if (ft2232_serial)
2084 openex_string = ft2232_serial;
2085 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2087 else
2089 LOG_ERROR("neither device description nor serial number specified");
2090 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2092 return ERROR_JTAG_INIT_FAILED;
2095 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2096 if (status != FT_OK) {
2097 /* under Win32, the FTD2XX driver appends an "A" to the end
2098 * of the description, if we tried by the desc, then
2099 * try by the alternate "A" description. */
2100 if (openex_string == ft2232_device_desc) {
2101 /* Try the alternate method. */
2102 openex_string = ft2232_device_desc_A;
2103 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2104 if (status == FT_OK) {
2105 /* yea, the "alternate" method worked! */
2106 } else {
2107 /* drat, give the user a meaningfull message.
2108 * telling the use we tried *BOTH* methods. */
2109 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2110 ft2232_device_desc,
2111 ft2232_device_desc_A);
2116 if (status != FT_OK)
2118 DWORD num_devices;
2120 if (more)
2122 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2123 *try_more = 1;
2124 return ERROR_JTAG_INIT_FAILED;
2126 LOG_ERROR("unable to open ftdi device: %lu", status);
2127 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2128 if (status == FT_OK)
2130 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2131 uint32_t i;
2133 for (i = 0; i < num_devices; i++)
2134 desc_array[i] = malloc(64);
2136 desc_array[num_devices] = NULL;
2138 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2140 if (status == FT_OK)
2142 LOG_ERROR("ListDevices: %lu\n", num_devices);
2143 for (i = 0; i < num_devices; i++)
2144 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2147 for (i = 0; i < num_devices; i++)
2148 free(desc_array[i]);
2150 free(desc_array);
2152 else
2154 LOG_ERROR("ListDevices: NONE\n");
2156 return ERROR_JTAG_INIT_FAILED;
2159 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2161 LOG_ERROR("unable to set latency timer: %lu", status);
2162 return ERROR_JTAG_INIT_FAILED;
2165 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2167 LOG_ERROR("unable to get latency timer: %lu", status);
2168 return ERROR_JTAG_INIT_FAILED;
2170 else
2172 LOG_DEBUG("current latency timer: %i", latency_timer);
2175 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2177 LOG_ERROR("unable to set timeouts: %lu", status);
2178 return ERROR_JTAG_INIT_FAILED;
2181 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2183 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2184 return ERROR_JTAG_INIT_FAILED;
2187 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2189 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2190 return ERROR_JTAG_INIT_FAILED;
2192 else
2194 static const char* type_str[] =
2195 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2196 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2197 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2198 ? ftdi_device : FT_DEVICE_UNKNOWN;
2199 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2200 LOG_INFO("deviceID: %lu", deviceID);
2201 LOG_INFO("SerialNumber: %s", SerialNumber);
2202 LOG_INFO("Description: %s", Description);
2205 return ERROR_OK;
2208 static int ft2232_purge_ftd2xx(void)
2210 FT_STATUS status;
2212 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2214 LOG_ERROR("error purging ftd2xx device: %lu", status);
2215 return ERROR_JTAG_INIT_FAILED;
2218 return ERROR_OK;
2221 #endif /* BUILD_FT2232_FTD2XX == 1 */
2223 #if BUILD_FT2232_LIBFTDI == 1
2224 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2226 uint8_t latency_timer;
2228 if (layout == NULL) {
2229 LOG_WARNING("No ft2232 layout specified'");
2230 return ERROR_JTAG_INIT_FAILED;
2233 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2234 layout->name, vid, pid);
2236 if (ftdi_init(&ftdic) < 0)
2237 return ERROR_JTAG_INIT_FAILED;
2239 /* default to INTERFACE_A */
2240 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2242 if (ftdi_set_interface(&ftdic, channel) < 0)
2244 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2245 return ERROR_JTAG_INIT_FAILED;
2248 /* context, vendor id, product id */
2249 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2250 ft2232_serial) < 0)
2252 if (more)
2253 LOG_WARNING("unable to open ftdi device (trying more): %s",
2254 ftdic.error_str);
2255 else
2256 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2257 *try_more = 1;
2258 return ERROR_JTAG_INIT_FAILED;
2261 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2262 if (ftdi_usb_reset(&ftdic) < 0)
2264 LOG_ERROR("unable to reset ftdi device");
2265 return ERROR_JTAG_INIT_FAILED;
2268 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2270 LOG_ERROR("unable to set latency timer");
2271 return ERROR_JTAG_INIT_FAILED;
2274 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2276 LOG_ERROR("unable to get latency timer");
2277 return ERROR_JTAG_INIT_FAILED;
2279 else
2281 LOG_DEBUG("current latency timer: %i", latency_timer);
2284 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2286 ftdi_device = ftdic.type;
2287 static const char* type_str[] =
2288 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2289 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2290 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2291 ? ftdi_device : no_of_known_types;
2292 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2293 return ERROR_OK;
2296 static int ft2232_purge_libftdi(void)
2298 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2300 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2301 return ERROR_JTAG_INIT_FAILED;
2304 return ERROR_OK;
2307 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2309 static int ft2232_init(void)
2311 uint8_t buf[1];
2312 int retval;
2313 uint32_t bytes_written;
2315 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2317 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2319 else
2321 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2324 if (layout == NULL) {
2325 LOG_WARNING("No ft2232 layout specified'");
2326 return ERROR_JTAG_INIT_FAILED;
2329 for (int i = 0; 1; i++)
2332 * "more indicates that there are more IDs to try, so we should
2333 * not print an error for an ID mismatch (but for anything
2334 * else, we should).
2336 * try_more indicates that the error code returned indicates an
2337 * ID mismatch (and nothing else) and that we should proceeed
2338 * with the next ID pair.
2340 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2341 int try_more = 0;
2343 #if BUILD_FT2232_FTD2XX == 1
2344 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2345 more, &try_more);
2346 #elif BUILD_FT2232_LIBFTDI == 1
2347 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2348 more, &try_more, layout->channel);
2349 #endif
2350 if (retval >= 0)
2351 break;
2352 if (!more || !try_more)
2353 return retval;
2356 ft2232_buffer_size = 0;
2357 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2359 if (layout->init() != ERROR_OK)
2360 return ERROR_JTAG_INIT_FAILED;
2362 if (ft2232_device_is_highspeed())
2364 #ifndef BUILD_FT2232_HIGHSPEED
2365 #if BUILD_FT2232_FTD2XX == 1
2366 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2367 #elif BUILD_FT2232_LIBFTDI == 1
2368 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2369 #endif
2370 #endif
2371 /* make sure the legacy mode is disabled */
2372 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2373 return ERROR_JTAG_INIT_FAILED;
2376 ft2232_speed(jtag_get_speed());
2378 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2379 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2381 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2382 return ERROR_JTAG_INIT_FAILED;
2385 #if BUILD_FT2232_FTD2XX == 1
2386 return ft2232_purge_ftd2xx();
2387 #elif BUILD_FT2232_LIBFTDI == 1
2388 return ft2232_purge_libftdi();
2389 #endif
2391 return ERROR_OK;
2394 /** Updates defaults for DBUS signals: the four JTAG signals
2395 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2397 static inline void ftx232_init_head(void)
2399 low_output = 0x08;
2400 low_direction = 0x0b;
2403 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2404 * the four GPIOL signals. Initialization covers value and direction,
2405 * as customized for each layout.
2407 static int ftx232_init_tail(void)
2409 uint8_t buf[3];
2410 uint32_t bytes_written;
2412 enum reset_types jtag_reset_config = jtag_get_reset_config();
2413 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2415 low_direction &= ~nTRSTnOE; /* nTRST input */
2416 low_output &= ~nTRST; /* nTRST = 0 */
2418 else
2420 low_direction |= nTRSTnOE; /* nTRST output */
2421 low_output |= nTRST; /* nTRST = 1 */
2424 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2426 low_direction |= nSRSTnOE; /* nSRST output */
2427 low_output |= nSRST; /* nSRST = 1 */
2429 else
2431 low_direction &= ~nSRSTnOE; /* nSRST input */
2432 low_output &= ~nSRST; /* nSRST = 0 */
2435 /* initialize low byte for jtag */
2436 buf[0] = 0x80; /* command "set data bits low byte" */
2437 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2438 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2439 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2441 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2443 LOG_ERROR("couldn't initialize FT2232 DBUS");
2444 return ERROR_JTAG_INIT_FAILED;
2447 return ERROR_OK;
2450 static int usbjtag_init(void)
2453 * NOTE: This is now _specific_ to the "usbjtag" layout.
2454 * Don't try cram any more layouts into this.
2456 ftx232_init_head();
2458 nTRST = 0x10;
2459 nTRSTnOE = 0x10;
2460 nSRST = 0x40;
2461 nSRSTnOE = 0x40;
2463 return ftx232_init_tail();
2466 static int lm3s811_jtag_init(void)
2468 ftx232_init_head();
2470 /* There are multiple revisions of LM3S811 eval boards:
2471 * - Rev B (and older?) boards have no SWO trace support.
2472 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2473 * they should use the "luminary_icdi" layout instead.
2475 nTRST = 0x0;
2476 nTRSTnOE = 0x00;
2477 nSRST = 0x20;
2478 nSRSTnOE = 0x20;
2479 low_output = 0x88;
2480 low_direction = 0x8b;
2482 return ftx232_init_tail();
2485 static int icdi_jtag_init(void)
2487 ftx232_init_head();
2489 /* Most Luminary eval boards support SWO trace output,
2490 * and should use this "luminary_icdi" layout.
2492 nTRST = 0x0;
2493 nTRSTnOE = 0x00;
2494 nSRST = 0x20;
2495 nSRSTnOE = 0x20;
2496 low_output = 0x88;
2497 low_direction = 0xcb;
2499 return ftx232_init_tail();
2502 static int signalyzer_init(void)
2504 ftx232_init_head();
2506 nTRST = 0x10;
2507 nTRSTnOE = 0x10;
2508 nSRST = 0x20;
2509 nSRSTnOE = 0x20;
2510 return ftx232_init_tail();
2513 static int axm0432_jtag_init(void)
2515 uint8_t buf[3];
2516 uint32_t bytes_written;
2518 low_output = 0x08;
2519 low_direction = 0x2b;
2521 /* initialize low byte for jtag */
2522 buf[0] = 0x80; /* command "set data bits low byte" */
2523 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2524 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2525 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2527 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2529 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2530 return ERROR_JTAG_INIT_FAILED;
2533 if (strcmp(layout->name, "axm0432_jtag") == 0)
2535 nTRST = 0x08;
2536 nTRSTnOE = 0x0; /* No output enable for TRST*/
2537 nSRST = 0x04;
2538 nSRSTnOE = 0x0; /* No output enable for SRST*/
2540 else
2542 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2543 exit(-1);
2546 high_output = 0x0;
2547 high_direction = 0x0c;
2549 enum reset_types jtag_reset_config = jtag_get_reset_config();
2550 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2552 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2554 else
2556 high_output |= nTRST;
2559 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2561 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2563 else
2565 high_output |= nSRST;
2568 /* initialize high port */
2569 buf[0] = 0x82; /* command "set data bits high byte" */
2570 buf[1] = high_output; /* value */
2571 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2572 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2574 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2576 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2577 return ERROR_JTAG_INIT_FAILED;
2580 return ERROR_OK;
2583 static int redbee_init(void)
2585 uint8_t buf[3];
2586 uint32_t bytes_written;
2588 low_output = 0x08;
2589 low_direction = 0x2b;
2591 /* initialize low byte for jtag */
2592 /* command "set data bits low byte" */
2593 buf[0] = 0x80;
2594 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2595 buf[2] = low_direction;
2596 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2597 buf[1] = low_output;
2598 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2600 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2602 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2603 return ERROR_JTAG_INIT_FAILED;
2606 nTRST = 0x08;
2607 nTRSTnOE = 0x0; /* No output enable for TRST*/
2608 nSRST = 0x04;
2609 nSRSTnOE = 0x0; /* No output enable for SRST*/
2611 high_output = 0x0;
2612 high_direction = 0x0c;
2614 enum reset_types jtag_reset_config = jtag_get_reset_config();
2615 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2617 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2619 else
2621 high_output |= nTRST;
2624 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2626 LOG_ERROR("can't set nSRST to push-pull on redbee");
2628 else
2630 high_output |= nSRST;
2633 /* initialize high port */
2634 buf[0] = 0x82; /* command "set data bits high byte" */
2635 buf[1] = high_output; /* value */
2636 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2637 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2639 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2641 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2642 return ERROR_JTAG_INIT_FAILED;
2645 return ERROR_OK;
2648 static int jtagkey_init(void)
2650 uint8_t buf[3];
2651 uint32_t bytes_written;
2653 low_output = 0x08;
2654 low_direction = 0x1b;
2656 /* initialize low byte for jtag */
2657 buf[0] = 0x80; /* command "set data bits low byte" */
2658 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2659 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2660 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2662 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2664 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2665 return ERROR_JTAG_INIT_FAILED;
2668 if (strcmp(layout->name, "jtagkey") == 0)
2670 nTRST = 0x01;
2671 nTRSTnOE = 0x4;
2672 nSRST = 0x02;
2673 nSRSTnOE = 0x08;
2675 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2676 || (strcmp(layout->name, "oocdlink") == 0))
2678 nTRST = 0x02;
2679 nTRSTnOE = 0x1;
2680 nSRST = 0x08;
2681 nSRSTnOE = 0x04;
2683 else
2685 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2686 exit(-1);
2689 high_output = 0x0;
2690 high_direction = 0x0f;
2692 enum reset_types jtag_reset_config = jtag_get_reset_config();
2693 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2695 high_output |= nTRSTnOE;
2696 high_output &= ~nTRST;
2698 else
2700 high_output &= ~nTRSTnOE;
2701 high_output |= nTRST;
2704 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2706 high_output &= ~nSRSTnOE;
2707 high_output |= nSRST;
2709 else
2711 high_output |= nSRSTnOE;
2712 high_output &= ~nSRST;
2715 /* initialize high port */
2716 buf[0] = 0x82; /* command "set data bits high byte" */
2717 buf[1] = high_output; /* value */
2718 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2719 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2721 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2723 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2724 return ERROR_JTAG_INIT_FAILED;
2727 return ERROR_OK;
2730 static int olimex_jtag_init(void)
2732 uint8_t buf[3];
2733 uint32_t bytes_written;
2735 low_output = 0x08;
2736 low_direction = 0x1b;
2738 /* initialize low byte for jtag */
2739 buf[0] = 0x80; /* command "set data bits low byte" */
2740 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2741 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2742 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2744 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2746 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2747 return ERROR_JTAG_INIT_FAILED;
2750 nTRST = 0x01;
2751 nTRSTnOE = 0x4;
2752 nSRST = 0x02;
2753 nSRSTnOE = 0x00; /* no output enable for nSRST */
2755 high_output = 0x0;
2756 high_direction = 0x0f;
2758 enum reset_types jtag_reset_config = jtag_get_reset_config();
2759 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2761 high_output |= nTRSTnOE;
2762 high_output &= ~nTRST;
2764 else
2766 high_output &= ~nTRSTnOE;
2767 high_output |= nTRST;
2770 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2772 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2774 else
2776 high_output &= ~nSRST;
2779 /* turn red LED on */
2780 high_output |= 0x08;
2782 /* initialize high port */
2783 buf[0] = 0x82; /* command "set data bits high byte" */
2784 buf[1] = high_output; /* value */
2785 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2786 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2788 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2790 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2791 return ERROR_JTAG_INIT_FAILED;
2794 return ERROR_OK;
2797 static int flyswatter_init(void)
2799 uint8_t buf[3];
2800 uint32_t bytes_written;
2802 low_output = 0x18;
2803 low_direction = 0xfb;
2805 /* initialize low byte for jtag */
2806 buf[0] = 0x80; /* command "set data bits low byte" */
2807 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2808 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2809 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2811 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2813 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2814 return ERROR_JTAG_INIT_FAILED;
2817 nTRST = 0x10;
2818 nTRSTnOE = 0x0; /* not output enable for nTRST */
2819 nSRST = 0x20;
2820 nSRSTnOE = 0x00; /* no output enable for nSRST */
2822 high_output = 0x00;
2823 high_direction = 0x0c;
2825 /* turn red LED3 on, LED2 off */
2826 high_output |= 0x08;
2828 /* initialize high port */
2829 buf[0] = 0x82; /* command "set data bits high byte" */
2830 buf[1] = high_output; /* value */
2831 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2832 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2834 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2836 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2837 return ERROR_JTAG_INIT_FAILED;
2840 return ERROR_OK;
2843 static int turtle_init(void)
2845 uint8_t buf[3];
2846 uint32_t bytes_written;
2848 low_output = 0x08;
2849 low_direction = 0x5b;
2851 /* initialize low byte for jtag */
2852 buf[0] = 0x80; /* command "set data bits low byte" */
2853 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2854 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2855 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2857 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2859 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2860 return ERROR_JTAG_INIT_FAILED;
2863 nSRST = 0x40;
2865 high_output = 0x00;
2866 high_direction = 0x0C;
2868 /* initialize high port */
2869 buf[0] = 0x82; /* command "set data bits high byte" */
2870 buf[1] = high_output;
2871 buf[2] = high_direction;
2872 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2874 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2876 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2877 return ERROR_JTAG_INIT_FAILED;
2880 return ERROR_OK;
2883 static int comstick_init(void)
2885 uint8_t buf[3];
2886 uint32_t bytes_written;
2888 low_output = 0x08;
2889 low_direction = 0x0b;
2891 /* initialize low byte for jtag */
2892 buf[0] = 0x80; /* command "set data bits low byte" */
2893 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2894 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2895 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2897 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2899 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2900 return ERROR_JTAG_INIT_FAILED;
2903 nTRST = 0x01;
2904 nTRSTnOE = 0x00; /* no output enable for nTRST */
2905 nSRST = 0x02;
2906 nSRSTnOE = 0x00; /* no output enable for nSRST */
2908 high_output = 0x03;
2909 high_direction = 0x03;
2911 /* initialize high port */
2912 buf[0] = 0x82; /* command "set data bits high byte" */
2913 buf[1] = high_output;
2914 buf[2] = high_direction;
2915 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2917 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2919 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2920 return ERROR_JTAG_INIT_FAILED;
2923 return ERROR_OK;
2926 static int stm32stick_init(void)
2928 uint8_t buf[3];
2929 uint32_t bytes_written;
2931 low_output = 0x88;
2932 low_direction = 0x8b;
2934 /* initialize low byte for jtag */
2935 buf[0] = 0x80; /* command "set data bits low byte" */
2936 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2937 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2938 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2940 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2942 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2943 return ERROR_JTAG_INIT_FAILED;
2946 nTRST = 0x01;
2947 nTRSTnOE = 0x00; /* no output enable for nTRST */
2948 nSRST = 0x80;
2949 nSRSTnOE = 0x00; /* no output enable for nSRST */
2951 high_output = 0x01;
2952 high_direction = 0x03;
2954 /* initialize high port */
2955 buf[0] = 0x82; /* command "set data bits high byte" */
2956 buf[1] = high_output;
2957 buf[2] = high_direction;
2958 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2960 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2962 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2963 return ERROR_JTAG_INIT_FAILED;
2966 return ERROR_OK;
2969 static int sheevaplug_init(void)
2971 uint8_t buf[3];
2972 uint32_t bytes_written;
2974 low_output = 0x08;
2975 low_direction = 0x1b;
2977 /* initialize low byte for jtag */
2978 buf[0] = 0x80; /* command "set data bits low byte" */
2979 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2980 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2981 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2983 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2985 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2986 return ERROR_JTAG_INIT_FAILED;
2989 nTRSTnOE = 0x1;
2990 nTRST = 0x02;
2991 nSRSTnOE = 0x4;
2992 nSRST = 0x08;
2994 high_output = 0x0;
2995 high_direction = 0x0f;
2997 /* nTRST is always push-pull */
2998 high_output &= ~nTRSTnOE;
2999 high_output |= nTRST;
3001 /* nSRST is always open-drain */
3002 high_output |= nSRSTnOE;
3003 high_output &= ~nSRST;
3005 /* initialize high port */
3006 buf[0] = 0x82; /* command "set data bits high byte" */
3007 buf[1] = high_output; /* value */
3008 buf[2] = high_direction; /* all outputs - xRST */
3009 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3011 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3013 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3014 return ERROR_JTAG_INIT_FAILED;
3017 return ERROR_OK;
3020 static int cortino_jtag_init(void)
3022 uint8_t buf[3];
3023 uint32_t bytes_written;
3025 low_output = 0x08;
3026 low_direction = 0x1b;
3028 /* initialize low byte for jtag */
3029 buf[0] = 0x80; /* command "set data bits low byte" */
3030 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3031 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3032 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3034 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3036 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3037 return ERROR_JTAG_INIT_FAILED;
3040 nTRST = 0x01;
3041 nTRSTnOE = 0x00; /* no output enable for nTRST */
3042 nSRST = 0x02;
3043 nSRSTnOE = 0x00; /* no output enable for nSRST */
3045 high_output = 0x03;
3046 high_direction = 0x03;
3048 /* initialize high port */
3049 buf[0] = 0x82; /* command "set data bits high byte" */
3050 buf[1] = high_output;
3051 buf[2] = high_direction;
3052 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3054 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3056 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3057 return ERROR_JTAG_INIT_FAILED;
3060 return ERROR_OK;
3063 static void olimex_jtag_blink(void)
3065 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3066 * ACBUS3 is bit 3 of the GPIOH port
3068 if (high_output & 0x08)
3070 /* set port pin high */
3071 high_output &= 0x07;
3073 else
3075 /* set port pin low */
3076 high_output |= 0x08;
3079 buffer_write(0x82);
3080 buffer_write(high_output);
3081 buffer_write(high_direction);
3084 static void flyswatter_jtag_blink(void)
3087 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3089 high_output ^= 0x0c;
3091 buffer_write(0x82);
3092 buffer_write(high_output);
3093 buffer_write(high_direction);
3096 static void turtle_jtag_blink(void)
3099 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3101 if (high_output & 0x08)
3103 high_output = 0x04;
3105 else
3107 high_output = 0x08;
3110 buffer_write(0x82);
3111 buffer_write(high_output);
3112 buffer_write(high_direction);
3115 static int ft2232_quit(void)
3117 #if BUILD_FT2232_FTD2XX == 1
3118 FT_STATUS status;
3120 status = FT_Close(ftdih);
3121 #elif BUILD_FT2232_LIBFTDI == 1
3122 ftdi_usb_close(&ftdic);
3124 ftdi_deinit(&ftdic);
3125 #endif
3127 free(ft2232_buffer);
3128 ft2232_buffer = NULL;
3130 return ERROR_OK;
3133 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3135 char *cp;
3136 char buf[200];
3137 if (CMD_ARGC == 1)
3139 ft2232_device_desc = strdup(CMD_ARGV[0]);
3140 cp = strchr(ft2232_device_desc, 0);
3141 /* under Win32, the FTD2XX driver appends an "A" to the end
3142 * of the description, this examines the given desc
3143 * and creates the 'missing' _A or non_A variable. */
3144 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3145 /* it was, so make this the "A" version. */
3146 ft2232_device_desc_A = ft2232_device_desc;
3147 /* and *CREATE* the non-A version. */
3148 strcpy(buf, ft2232_device_desc);
3149 cp = strchr(buf, 0);
3150 cp[-2] = 0;
3151 ft2232_device_desc = strdup(buf);
3152 } else {
3153 /* <space > A not defined
3154 * so create it */
3155 sprintf(buf, "%s A", ft2232_device_desc);
3156 ft2232_device_desc_A = strdup(buf);
3159 else
3161 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3164 return ERROR_OK;
3167 COMMAND_HANDLER(ft2232_handle_serial_command)
3169 if (CMD_ARGC == 1)
3171 ft2232_serial = strdup(CMD_ARGV[0]);
3173 else
3175 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3178 return ERROR_OK;
3181 COMMAND_HANDLER(ft2232_handle_layout_command)
3183 if (CMD_ARGC != 1) {
3184 LOG_ERROR("Need exactly one argument to ft2232_layout");
3185 return ERROR_FAIL;
3188 if (layout) {
3189 LOG_ERROR("already specified ft2232_layout %s",
3190 layout->name);
3191 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3192 ? ERROR_FAIL
3193 : ERROR_OK;
3196 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3197 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3198 layout = l;
3199 return ERROR_OK;
3203 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3204 return ERROR_FAIL;
3207 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3209 if (CMD_ARGC > MAX_USB_IDS * 2)
3211 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3212 "(maximum is %d pairs)", MAX_USB_IDS);
3213 CMD_ARGC = MAX_USB_IDS * 2;
3215 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3217 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3218 if (CMD_ARGC < 2)
3219 return ERROR_COMMAND_SYNTAX_ERROR;
3220 /* remove the incomplete trailing id */
3221 CMD_ARGC -= 1;
3224 unsigned i;
3225 for (i = 0; i < CMD_ARGC; i += 2)
3227 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3228 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3232 * Explicitly terminate, in case there are multiples instances of
3233 * ft2232_vid_pid.
3235 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3237 return ERROR_OK;
3240 COMMAND_HANDLER(ft2232_handle_latency_command)
3242 if (CMD_ARGC == 1)
3244 ft2232_latency = atoi(CMD_ARGV[0]);
3246 else
3248 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3251 return ERROR_OK;
3254 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3256 int retval = 0;
3258 /* 7 bits of either ones or zeros. */
3259 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3261 while (num_cycles > 0)
3263 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3264 * at most 7 bits per invocation. Here we invoke it potentially
3265 * several times.
3267 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3269 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3271 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3272 retval = ERROR_JTAG_QUEUE_FAILED;
3274 first_unsent = cmd;
3277 /* there are no state transitions in this code, so omit state tracking */
3279 /* command "Clock Data to TMS/CS Pin (no Read)" */
3280 buffer_write(0x4b);
3282 /* scan 7 bit */
3283 buffer_write(bitcount_per_command - 1);
3285 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3286 buffer_write(tms);
3288 require_send = 1;
3290 num_cycles -= bitcount_per_command;
3293 return retval;
3296 /* ---------------------------------------------------------------------
3297 * Support for IceBear JTAG adapter from Section5:
3298 * http://section5.ch/icebear
3300 * Author: Sten, debian@sansys-electronic.com
3303 /* Icebear pin layout
3305 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3306 * GND GND | 4 3| n.c.
3307 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3308 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3309 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3310 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3311 * ADBUS2 TDO |14 13| GND GND
3313 * ADBUS0 O L TCK ACBUS0 GND
3314 * ADBUS1 O L TDI ACBUS1 GND
3315 * ADBUS2 I TDO ACBUS2 n.c.
3316 * ADBUS3 O H TMS ACBUS3 n.c.
3317 * ADBUS4 O H nTRST
3318 * ADBUS5 O H nSRST
3319 * ADBUS6 - VCC
3320 * ADBUS7 - GND
3322 static int icebear_jtag_init(void) {
3323 uint8_t buf[3];
3324 uint32_t bytes_written;
3326 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3327 low_output = 0x08; /* high: TMS; low: TCK TDI */
3328 nTRST = 0x10;
3329 nSRST = 0x20;
3331 enum reset_types jtag_reset_config = jtag_get_reset_config();
3332 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3333 low_direction &= ~nTRST; /* nTRST high impedance */
3335 else {
3336 low_direction |= nTRST;
3337 low_output |= nTRST;
3340 low_direction |= nSRST;
3341 low_output |= nSRST;
3343 /* initialize low byte for jtag */
3344 buf[0] = 0x80; /* command "set data bits low byte" */
3345 buf[1] = low_output;
3346 buf[2] = low_direction;
3347 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3349 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3350 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3351 return ERROR_JTAG_INIT_FAILED;
3354 high_output = 0x0;
3355 high_direction = 0x00;
3358 /* initialize high port */
3359 buf[0] = 0x82; /* command "set data bits high byte" */
3360 buf[1] = high_output; /* value */
3361 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3362 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3364 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3365 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3366 return ERROR_JTAG_INIT_FAILED;
3369 return ERROR_OK;
3372 static void icebear_jtag_reset(int trst, int srst) {
3374 if (trst == 1) {
3375 low_direction |= nTRST;
3376 low_output &= ~nTRST;
3378 else if (trst == 0) {
3379 enum reset_types jtag_reset_config = jtag_get_reset_config();
3380 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3381 low_direction &= ~nTRST;
3382 else
3383 low_output |= nTRST;
3386 if (srst == 1) {
3387 low_output &= ~nSRST;
3389 else if (srst == 0) {
3390 low_output |= nSRST;
3393 /* command "set data bits low byte" */
3394 buffer_write(0x80);
3395 buffer_write(low_output);
3396 buffer_write(low_direction);
3398 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3401 /* ---------------------------------------------------------------------
3402 * Support for Signalyzer H2 and Signalyzer H4
3403 * JTAG adapter from Xverve Technologies Inc.
3404 * http://www.signalyzer.com or http://www.xverve.com
3406 * Author: Oleg Seiljus, oleg@signalyzer.com
3408 static unsigned char signalyzer_h_side;
3409 static unsigned int signalyzer_h_adapter_type;
3411 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3413 #if BUILD_FT2232_FTD2XX == 1
3414 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3415 #endif
3417 #define SIGNALYZER_COMMAND_ADDR 128
3418 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3420 #define SIGNALYZER_COMMAND_VERSION 0x41
3421 #define SIGNALYZER_COMMAND_RESET 0x42
3422 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3423 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3424 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3425 #define SIGNALYZER_COMMAND_LED_SET 0x53
3426 #define SIGNALYZER_COMMAND_ADC 0x54
3427 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3428 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3429 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3430 #define SIGNALYZER_COMMAND_I2C 0x58
3432 #define SIGNALYZER_CHAN_A 1
3433 #define SIGNALYZER_CHAN_B 2
3434 /* LEDS use channel C */
3435 #define SIGNALYZER_CHAN_C 4
3437 #define SIGNALYZER_LED_GREEN 1
3438 #define SIGNALYZER_LED_RED 2
3440 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3441 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3442 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3443 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3444 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3447 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3449 #if BUILD_FT2232_FTD2XX == 1
3450 return FT_WriteEE(ftdih, address, value);
3451 #elif BUILD_FT2232_LIBFTDI == 1
3452 return 0;
3453 #endif
3456 #if BUILD_FT2232_FTD2XX == 1
3457 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3459 return FT_ReadEE(ftdih, address, value);
3461 #endif
3463 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3464 int on_time_ms, int off_time_ms, unsigned char cycles)
3466 unsigned char on_time;
3467 unsigned char off_time;
3469 if (on_time_ms < 0xFFFF)
3470 on_time = (unsigned char)(on_time_ms / 62);
3471 else
3472 on_time = 0xFF;
3474 off_time = (unsigned char)(off_time_ms / 62);
3476 #if BUILD_FT2232_FTD2XX == 1
3477 FT_STATUS status;
3479 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3480 ((uint32_t)(channel << 8) | led))) != FT_OK)
3482 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3483 return ERROR_JTAG_DEVICE_ERROR;
3486 if ((status = signalyzer_h_ctrl_write(
3487 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3488 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3490 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3491 return ERROR_JTAG_DEVICE_ERROR;
3494 if ((status = signalyzer_h_ctrl_write(
3495 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3496 ((uint32_t)cycles))) != FT_OK)
3498 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3499 return ERROR_JTAG_DEVICE_ERROR;
3502 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3503 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3505 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3506 return ERROR_JTAG_DEVICE_ERROR;
3509 return ERROR_OK;
3510 #elif BUILD_FT2232_LIBFTDI == 1
3511 int retval;
3513 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3514 ((uint32_t)(channel << 8) | led))) < 0)
3516 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3517 ftdi_get_error_string(&ftdic));
3518 return ERROR_JTAG_DEVICE_ERROR;
3521 if ((retval = signalyzer_h_ctrl_write(
3522 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3523 ((uint32_t)(on_time << 8) | off_time))) < 0)
3525 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3526 ftdi_get_error_string(&ftdic));
3527 return ERROR_JTAG_DEVICE_ERROR;
3530 if ((retval = signalyzer_h_ctrl_write(
3531 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3532 (uint32_t)cycles)) < 0)
3534 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3535 ftdi_get_error_string(&ftdic));
3536 return ERROR_JTAG_DEVICE_ERROR;
3539 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3540 SIGNALYZER_COMMAND_LED_SET)) < 0)
3542 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3543 ftdi_get_error_string(&ftdic));
3544 return ERROR_JTAG_DEVICE_ERROR;
3547 return ERROR_OK;
3548 #endif
3551 static int signalyzer_h_init(void)
3553 #if BUILD_FT2232_FTD2XX == 1
3554 FT_STATUS status;
3555 int i;
3556 #endif
3558 char *end_of_desc;
3560 uint16_t read_buf[12] = { 0 };
3561 uint8_t buf[3];
3562 uint32_t bytes_written;
3564 /* turn on center green led */
3565 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3566 0xFFFF, 0x00, 0x00);
3568 /* determine what channel config wants to open
3569 * TODO: change me... current implementation is made to work
3570 * with openocd description parsing.
3572 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3574 if (end_of_desc)
3576 signalyzer_h_side = *(end_of_desc - 1);
3577 if (signalyzer_h_side == 'B')
3578 signalyzer_h_side = SIGNALYZER_CHAN_B;
3579 else
3580 signalyzer_h_side = SIGNALYZER_CHAN_A;
3582 else
3584 LOG_ERROR("No Channel was specified");
3585 return ERROR_FAIL;
3588 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3589 1000, 1000, 0xFF);
3591 #if BUILD_FT2232_FTD2XX == 1
3592 /* read signalyzer versionining information */
3593 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3594 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3596 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3597 return ERROR_JTAG_DEVICE_ERROR;
3600 for (i = 0; i < 10; i++)
3602 if ((status = signalyzer_h_ctrl_read(
3603 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3604 &read_buf[i])) != FT_OK)
3606 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3607 status);
3608 return ERROR_JTAG_DEVICE_ERROR;
3612 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3613 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3614 read_buf[4], read_buf[5], read_buf[6]);
3616 /* set gpio register */
3617 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3618 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3620 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3621 return ERROR_JTAG_DEVICE_ERROR;
3624 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3625 0x0404)) != FT_OK)
3627 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3628 return ERROR_JTAG_DEVICE_ERROR;
3631 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3632 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3635 return ERROR_JTAG_DEVICE_ERROR;
3638 /* read adapter type information */
3639 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3640 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3642 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3643 return ERROR_JTAG_DEVICE_ERROR;
3646 if ((status = signalyzer_h_ctrl_write(
3647 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3649 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3650 return ERROR_JTAG_DEVICE_ERROR;
3653 if ((status = signalyzer_h_ctrl_write(
3654 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3656 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3657 return ERROR_JTAG_DEVICE_ERROR;
3660 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3661 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3663 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3664 return ERROR_JTAG_DEVICE_ERROR;
3667 usleep(100000);
3669 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3670 &read_buf[0])) != FT_OK)
3672 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3673 return ERROR_JTAG_DEVICE_ERROR;
3676 if (read_buf[0] != 0x0498)
3677 signalyzer_h_adapter_type = 0x0000;
3678 else
3680 for (i = 0; i < 4; i++)
3682 if ((status = signalyzer_h_ctrl_read(
3683 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3684 &read_buf[i])) != FT_OK)
3686 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3687 status);
3688 return ERROR_JTAG_DEVICE_ERROR;
3692 signalyzer_h_adapter_type = read_buf[0];
3695 #elif BUILD_FT2232_LIBFTDI == 1
3696 /* currently libftdi does not allow reading individual eeprom
3697 * locations, therefore adapter type cannot be detected.
3698 * override with most common type
3700 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3701 #endif
3703 enum reset_types jtag_reset_config = jtag_get_reset_config();
3705 /* ADAPTOR: EM_LT16_A */
3706 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3708 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3709 "detected. (HW: %2x).", (read_buf[1] >> 8));
3711 nTRST = 0x10;
3712 nTRSTnOE = 0x10;
3713 nSRST = 0x20;
3714 nSRSTnOE = 0x20;
3716 low_output = 0x08;
3717 low_direction = 0x1b;
3719 high_output = 0x0;
3720 high_direction = 0x0;
3722 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3724 low_direction &= ~nTRSTnOE; /* nTRST input */
3725 low_output &= ~nTRST; /* nTRST = 0 */
3727 else
3729 low_direction |= nTRSTnOE; /* nTRST output */
3730 low_output |= nTRST; /* nTRST = 1 */
3733 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3735 low_direction |= nSRSTnOE; /* nSRST output */
3736 low_output |= nSRST; /* nSRST = 1 */
3738 else
3740 low_direction &= ~nSRSTnOE; /* nSRST input */
3741 low_output &= ~nSRST; /* nSRST = 0 */
3744 #if BUILD_FT2232_FTD2XX == 1
3745 /* enable power to the module */
3746 if ((status = signalyzer_h_ctrl_write(
3747 SIGNALYZER_DATA_BUFFER_ADDR,
3748 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3749 != FT_OK)
3751 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3752 status);
3753 return ERROR_JTAG_DEVICE_ERROR;
3756 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3757 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3759 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3760 status);
3761 return ERROR_JTAG_DEVICE_ERROR;
3764 /* set gpio mode register */
3765 if ((status = signalyzer_h_ctrl_write(
3766 SIGNALYZER_DATA_BUFFER_ADDR,
3767 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3769 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3770 status);
3771 return ERROR_JTAG_DEVICE_ERROR;
3774 if ((status = signalyzer_h_ctrl_write(
3775 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3776 != FT_OK)
3778 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3779 status);
3780 return ERROR_JTAG_DEVICE_ERROR;
3783 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3784 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3786 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3787 status);
3788 return ERROR_JTAG_DEVICE_ERROR;
3791 /* set gpio register */
3792 if ((status = signalyzer_h_ctrl_write(
3793 SIGNALYZER_DATA_BUFFER_ADDR,
3794 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3796 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3797 status);
3798 return ERROR_JTAG_DEVICE_ERROR;
3801 if ((status = signalyzer_h_ctrl_write(
3802 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3803 != FT_OK)
3805 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3806 status);
3807 return ERROR_JTAG_DEVICE_ERROR;
3810 if ((status = signalyzer_h_ctrl_write(
3811 SIGNALYZER_COMMAND_ADDR,
3812 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3814 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3815 status);
3816 return ERROR_JTAG_DEVICE_ERROR;
3818 #endif
3821 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3822 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3823 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3824 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3825 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3827 if (signalyzer_h_adapter_type
3828 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3829 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3830 "detected. (HW: %2x).", (read_buf[1] >> 8));
3831 else if (signalyzer_h_adapter_type
3832 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3833 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3834 "(ARM JTAG with PSU) detected. (HW: %2x).",
3835 (read_buf[1] >> 8));
3836 else if (signalyzer_h_adapter_type
3837 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3838 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3839 "detected. (HW: %2x).", (read_buf[1] >> 8));
3840 else if (signalyzer_h_adapter_type
3841 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3842 LOG_INFO("Signalyzer: EM-JTAG-P "
3843 "(Generic JTAG with PSU) detected. (HW: %2x).",
3844 (read_buf[1] >> 8));
3846 nTRST = 0x02;
3847 nTRSTnOE = 0x04;
3848 nSRST = 0x08;
3849 nSRSTnOE = 0x10;
3851 low_output = 0x08;
3852 low_direction = 0x1b;
3854 high_output = 0x0;
3855 high_direction = 0x1f;
3857 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3859 high_output |= nTRSTnOE;
3860 high_output &= ~nTRST;
3862 else
3864 high_output &= ~nTRSTnOE;
3865 high_output |= nTRST;
3868 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3870 high_output &= ~nSRSTnOE;
3871 high_output |= nSRST;
3873 else
3875 high_output |= nSRSTnOE;
3876 high_output &= ~nSRST;
3879 #if BUILD_FT2232_FTD2XX == 1
3880 /* enable power to the module */
3881 if ((status = signalyzer_h_ctrl_write(
3882 SIGNALYZER_DATA_BUFFER_ADDR,
3883 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3884 != FT_OK)
3886 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3887 status);
3888 return ERROR_JTAG_DEVICE_ERROR;
3891 if ((status = signalyzer_h_ctrl_write(
3892 SIGNALYZER_COMMAND_ADDR,
3893 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3895 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3896 status);
3897 return ERROR_JTAG_DEVICE_ERROR;
3900 /* set gpio mode register (IO_16 and IO_17 set as analog
3901 * inputs, other is gpio)
3903 if ((status = signalyzer_h_ctrl_write(
3904 SIGNALYZER_DATA_BUFFER_ADDR,
3905 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3907 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3908 status);
3909 return ERROR_JTAG_DEVICE_ERROR;
3912 if ((status = signalyzer_h_ctrl_write(
3913 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3914 != FT_OK)
3916 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3917 status);
3918 return ERROR_JTAG_DEVICE_ERROR;
3921 if ((status = signalyzer_h_ctrl_write(
3922 SIGNALYZER_COMMAND_ADDR,
3923 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3925 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3926 status);
3927 return ERROR_JTAG_DEVICE_ERROR;
3930 /* set gpio register (all inputs, for -P modules,
3931 * PSU will be turned off)
3933 if ((status = signalyzer_h_ctrl_write(
3934 SIGNALYZER_DATA_BUFFER_ADDR,
3935 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3937 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3938 status);
3939 return ERROR_JTAG_DEVICE_ERROR;
3942 if ((status = signalyzer_h_ctrl_write(
3943 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3944 != FT_OK)
3946 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3947 status);
3948 return ERROR_JTAG_DEVICE_ERROR;
3951 if ((status = signalyzer_h_ctrl_write(
3952 SIGNALYZER_COMMAND_ADDR,
3953 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3955 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3956 status);
3957 return ERROR_JTAG_DEVICE_ERROR;
3959 #endif
3962 else if (signalyzer_h_adapter_type == 0x0000)
3964 LOG_INFO("Signalyzer: No external modules were detected.");
3966 nTRST = 0x10;
3967 nTRSTnOE = 0x10;
3968 nSRST = 0x20;
3969 nSRSTnOE = 0x20;
3971 low_output = 0x08;
3972 low_direction = 0x1b;
3974 high_output = 0x0;
3975 high_direction = 0x0;
3977 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3979 low_direction &= ~nTRSTnOE; /* nTRST input */
3980 low_output &= ~nTRST; /* nTRST = 0 */
3982 else
3984 low_direction |= nTRSTnOE; /* nTRST output */
3985 low_output |= nTRST; /* nTRST = 1 */
3988 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3990 low_direction |= nSRSTnOE; /* nSRST output */
3991 low_output |= nSRST; /* nSRST = 1 */
3993 else
3995 low_direction &= ~nSRSTnOE; /* nSRST input */
3996 low_output &= ~nSRST; /* nSRST = 0 */
3999 else
4001 LOG_ERROR("Unknown module type is detected: %.4x",
4002 signalyzer_h_adapter_type);
4003 return ERROR_JTAG_DEVICE_ERROR;
4006 /* initialize low byte of controller for jtag operation */
4007 buf[0] = 0x80;
4008 buf[1] = low_output;
4009 buf[2] = low_direction;
4011 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4013 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4014 return ERROR_JTAG_INIT_FAILED;
4017 #if BUILD_FT2232_FTD2XX == 1
4018 if (ftdi_device == FT_DEVICE_2232H)
4020 /* initialize high byte of controller for jtag operation */
4021 buf[0] = 0x82;
4022 buf[1] = high_output;
4023 buf[2] = high_direction;
4025 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
4027 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4028 return ERROR_JTAG_INIT_FAILED;
4031 #elif BUILD_FT2232_LIBFTDI == 1
4032 if (ftdi_device == TYPE_2232H)
4034 /* initialize high byte of controller for jtag operation */
4035 buf[0] = 0x82;
4036 buf[1] = high_output;
4037 buf[2] = high_direction;
4039 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4041 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4042 return ERROR_JTAG_INIT_FAILED;
4045 #endif
4046 return ERROR_OK;
4049 static void signalyzer_h_reset(int trst, int srst)
4051 enum reset_types jtag_reset_config = jtag_get_reset_config();
4053 /* ADAPTOR: EM_LT16_A */
4054 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4056 if (trst == 1)
4058 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4059 /* switch to output pin (output is low) */
4060 low_direction |= nTRSTnOE;
4061 else
4062 /* switch output low */
4063 low_output &= ~nTRST;
4065 else if (trst == 0)
4067 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4068 /* switch to input pin (high-Z + internal
4069 * and external pullup) */
4070 low_direction &= ~nTRSTnOE;
4071 else
4072 /* switch output high */
4073 low_output |= nTRST;
4076 if (srst == 1)
4078 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4079 /* switch output low */
4080 low_output &= ~nSRST;
4081 else
4082 /* switch to output pin (output is low) */
4083 low_direction |= nSRSTnOE;
4085 else if (srst == 0)
4087 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4088 /* switch output high */
4089 low_output |= nSRST;
4090 else
4091 /* switch to input pin (high-Z) */
4092 low_direction &= ~nSRSTnOE;
4095 /* command "set data bits low byte" */
4096 buffer_write(0x80);
4097 buffer_write(low_output);
4098 buffer_write(low_direction);
4099 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4100 "low_direction: 0x%2.2x",
4101 trst, srst, low_output, low_direction);
4103 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4104 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4105 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4106 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4107 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4109 if (trst == 1)
4111 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4112 high_output &= ~nTRSTnOE;
4113 else
4114 high_output &= ~nTRST;
4116 else if (trst == 0)
4118 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4119 high_output |= nTRSTnOE;
4120 else
4121 high_output |= nTRST;
4124 if (srst == 1)
4126 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4127 high_output &= ~nSRST;
4128 else
4129 high_output &= ~nSRSTnOE;
4131 else if (srst == 0)
4133 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4134 high_output |= nSRST;
4135 else
4136 high_output |= nSRSTnOE;
4139 /* command "set data bits high byte" */
4140 buffer_write(0x82);
4141 buffer_write(high_output);
4142 buffer_write(high_direction);
4143 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4144 "high_direction: 0x%2.2x",
4145 trst, srst, high_output, high_direction);
4147 else if (signalyzer_h_adapter_type == 0x0000)
4149 if (trst == 1)
4151 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4152 /* switch to output pin (output is low) */
4153 low_direction |= nTRSTnOE;
4154 else
4155 /* switch output low */
4156 low_output &= ~nTRST;
4158 else if (trst == 0)
4160 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4161 /* switch to input pin (high-Z + internal
4162 * and external pullup) */
4163 low_direction &= ~nTRSTnOE;
4164 else
4165 /* switch output high */
4166 low_output |= nTRST;
4169 if (srst == 1)
4171 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4172 /* switch output low */
4173 low_output &= ~nSRST;
4174 else
4175 /* switch to output pin (output is low) */
4176 low_direction |= nSRSTnOE;
4178 else if (srst == 0)
4180 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4181 /* switch output high */
4182 low_output |= nSRST;
4183 else
4184 /* switch to input pin (high-Z) */
4185 low_direction &= ~nSRSTnOE;
4188 /* command "set data bits low byte" */
4189 buffer_write(0x80);
4190 buffer_write(low_output);
4191 buffer_write(low_direction);
4192 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4193 "low_direction: 0x%2.2x",
4194 trst, srst, low_output, low_direction);
4198 static void signalyzer_h_blink(void)
4200 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4203 /********************************************************************
4204 * Support for KT-LINK
4205 * JTAG adapter from KRISTECH
4206 * http://www.kristech.eu
4207 *******************************************************************/
4208 static int ktlink_init(void)
4210 uint8_t buf[3];
4211 uint32_t bytes_written;
4212 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4214 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4215 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4217 // initialize low port
4218 buf[0] = 0x80; // command "set data bits low byte"
4219 buf[1] = low_output;
4220 buf[2] = low_direction;
4221 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4223 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4225 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4226 return ERROR_JTAG_INIT_FAILED;
4229 nTRST = 0x01;
4230 nSRST = 0x02;
4231 nTRSTnOE = 0x04;
4232 nSRSTnOE = 0x08;
4234 high_output = 0x80; // turn LED on
4235 high_direction = 0xFF; // all outputs
4237 enum reset_types jtag_reset_config = jtag_get_reset_config();
4239 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4240 high_output |= nTRSTnOE;
4241 high_output &= ~nTRST;
4242 } else {
4243 high_output &= ~nTRSTnOE;
4244 high_output |= nTRST;
4247 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4248 high_output &= ~nSRSTnOE;
4249 high_output |= nSRST;
4250 } else {
4251 high_output |= nSRSTnOE;
4252 high_output &= ~nSRST;
4255 // initialize high port
4256 buf[0] = 0x82; // command "set data bits high byte"
4257 buf[1] = high_output; // value
4258 buf[2] = high_direction;
4259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4261 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4263 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4264 return ERROR_JTAG_INIT_FAILED;
4267 return ERROR_OK;
4270 static void ktlink_reset(int trst, int srst)
4272 enum reset_types jtag_reset_config = jtag_get_reset_config();
4274 if (trst == 1) {
4275 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4276 high_output &= ~nTRSTnOE;
4277 else
4278 high_output &= ~nTRST;
4279 } else if (trst == 0) {
4280 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4281 high_output |= nTRSTnOE;
4282 else
4283 high_output |= nTRST;
4286 if (srst == 1) {
4287 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4288 high_output &= ~nSRST;
4289 else
4290 high_output &= ~nSRSTnOE;
4291 } else if (srst == 0) {
4292 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4293 high_output |= nSRST;
4294 else
4295 high_output |= nSRSTnOE;
4298 buffer_write(0x82); // command "set data bits high byte"
4299 buffer_write(high_output);
4300 buffer_write(high_direction);
4301 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4304 static void ktlink_blink(void)
4306 /* LED connected to ACBUS7 */
4307 if (high_output & 0x80)
4308 high_output &= 0x7F;
4309 else
4310 high_output |= 0x80;
4312 buffer_write(0x82); // command "set data bits high byte"
4313 buffer_write(high_output);
4314 buffer_write(high_direction);
4317 static const struct command_registration ft2232_command_handlers[] = {
4319 .name = "ft2232_device_desc",
4320 .handler = &ft2232_handle_device_desc_command,
4321 .mode = COMMAND_CONFIG,
4322 .help = "set the USB device description of the FTDI FT2232 device",
4323 .usage = "description_string",
4326 .name = "ft2232_serial",
4327 .handler = &ft2232_handle_serial_command,
4328 .mode = COMMAND_CONFIG,
4329 .help = "set the serial number of the FTDI FT2232 device",
4330 .usage = "serial_string",
4333 .name = "ft2232_layout",
4334 .handler = &ft2232_handle_layout_command,
4335 .mode = COMMAND_CONFIG,
4336 .help = "set the layout of the FT2232 GPIO signals used "
4337 "to control output-enables and reset signals",
4338 .usage = "layout_name",
4341 .name = "ft2232_vid_pid",
4342 .handler = &ft2232_handle_vid_pid_command,
4343 .mode = COMMAND_CONFIG,
4344 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4345 .usage = "(vid pid)* ",
4348 .name = "ft2232_latency",
4349 .handler = &ft2232_handle_latency_command,
4350 .mode = COMMAND_CONFIG,
4351 .help = "set the FT2232 latency timer to a new value",
4352 .usage = "value",
4354 COMMAND_REGISTRATION_DONE
4357 struct jtag_interface ft2232_interface = {
4358 .name = "ft2232",
4359 .supported = DEBUG_CAP_TMS_SEQ,
4360 .commands = ft2232_command_handlers,
4362 .init = ft2232_init,
4363 .quit = ft2232_quit,
4364 .speed = ft2232_speed,
4365 .speed_div = ft2232_speed_div,
4366 .khz = ft2232_khz,
4367 .execute_queue = ft2232_execute_queue,