arm-jtag-ew: -Wshadow fix
[openocd.git] / src / jtag / drivers / ft2232.c
blob2ac410a5d968a056d2b5e23efdafa60a5a1fb13e
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;
483 return ERROR_OK;
485 #elif BUILD_FT2232_LIBFTDI == 1
486 int retval;
487 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
489 *bytes_written = 0;
490 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
491 return ERROR_JTAG_DEVICE_ERROR;
493 else
495 *bytes_written = retval;
496 return ERROR_OK;
498 #endif
501 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
503 #if BUILD_FT2232_FTD2XX == 1
504 DWORD dw_bytes_read;
505 FT_STATUS status;
506 int timeout = 5;
507 *bytes_read = 0;
509 while ((*bytes_read < size) && timeout--)
511 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
512 *bytes_read, &dw_bytes_read)) != FT_OK)
514 *bytes_read = 0;
515 LOG_ERROR("FT_Read returned: %lu", status);
516 return ERROR_JTAG_DEVICE_ERROR;
518 *bytes_read += dw_bytes_read;
521 #elif BUILD_FT2232_LIBFTDI == 1
522 int retval;
523 int timeout = LIBFTDI_READ_RETRY_COUNT;
524 *bytes_read = 0;
526 while ((*bytes_read < size) && timeout--)
528 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
530 *bytes_read = 0;
531 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
532 return ERROR_JTAG_DEVICE_ERROR;
534 *bytes_read += retval;
537 #endif
539 if (*bytes_read < size)
541 LOG_ERROR("couldn't read enough bytes from "
542 "FT2232 device (%i < %i)",
543 (unsigned)*bytes_read,
544 (unsigned)size);
545 return ERROR_JTAG_DEVICE_ERROR;
548 return ERROR_OK;
551 static bool ft2232_device_is_highspeed(void)
553 #if BUILD_FT2232_FTD2XX == 1
554 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
555 #elif BUILD_FT2232_LIBFTDI == 1
556 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
557 #endif
561 * Commands that only apply to the FT2232H and FT4232H devices.
562 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
563 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
566 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
568 uint8_t buf = enable ? 0x96 : 0x97;
569 LOG_DEBUG("%2.2x", buf);
571 uint32_t bytes_written;
572 int retval = ft2232_write(&buf, 1, &bytes_written);
573 if ((ERROR_OK != retval) || (bytes_written != 1))
575 LOG_ERROR("couldn't write command to %s adaptive clocking"
576 , enable ? "enable" : "disable");
577 return retval;
580 return ERROR_OK;
584 * Enable/disable the clk divide by 5 of the 60MHz master clock.
585 * This result in a JTAG clock speed range of 91.553Hz-6MHz
586 * respective 457.763Hz-30MHz.
588 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
590 uint32_t bytes_written;
591 uint8_t buf = enable ? 0x8b : 0x8a;
592 int retval = ft2232_write(&buf, 1, &bytes_written);
593 if ((ERROR_OK != retval) || (bytes_written != 1))
595 LOG_ERROR("couldn't write command to %s clk divide by 5"
596 , enable ? "enable" : "disable");
597 return ERROR_JTAG_INIT_FAILED;
599 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
600 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
602 return ERROR_OK;
605 static int ft2232_speed(int speed)
607 uint8_t buf[3];
608 int retval;
609 uint32_t bytes_written;
611 retval = ERROR_OK;
612 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
613 if (ft2232_device_is_highspeed())
614 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
615 else if (enable_adaptive_clocking)
617 LOG_ERROR("ft2232 device %lu does not support RTCK"
618 , (long unsigned int)ftdi_device);
619 return ERROR_FAIL;
622 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
623 return retval;
625 buf[0] = 0x86; /* command "set divisor" */
626 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
627 buf[2] = (speed >> 8) & 0xff; /* valueH */
629 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
630 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
632 LOG_ERROR("couldn't set FT2232 TCK speed");
633 return retval;
636 return ERROR_OK;
639 static int ft2232_speed_div(int speed, int* khz)
641 /* Take a look in the FT2232 manual,
642 * AN2232C-01 Command Processor for
643 * MPSSE and MCU Host Bus. Chapter 3.8 */
645 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
647 return ERROR_OK;
650 static int ft2232_khz(int khz, int* jtag_speed)
652 if (khz == 0)
654 if (ft2232_device_is_highspeed())
656 *jtag_speed = RTCK_SPEED;
657 return ERROR_OK;
659 else
661 LOG_DEBUG("RCLK not supported");
662 return ERROR_FAIL;
666 /* Take a look in the FT2232 manual,
667 * AN2232C-01 Command Processor for
668 * MPSSE and MCU Host Bus. Chapter 3.8
670 * We will calc here with a multiplier
671 * of 10 for better rounding later. */
673 /* Calc speed, (ft2232_max_tck / khz) - 1 */
674 /* Use 65000 for better rounding */
675 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
677 /* Add 0.9 for rounding */
678 *jtag_speed += 9;
680 /* Calc real speed */
681 *jtag_speed = *jtag_speed / 10;
683 /* Check if speed is greater than 0 */
684 if (*jtag_speed < 0)
686 *jtag_speed = 0;
689 /* Check max value */
690 if (*jtag_speed > 0xFFFF)
692 *jtag_speed = 0xFFFF;
695 return ERROR_OK;
698 static void ft2232_end_state(tap_state_t state)
700 if (tap_is_state_stable(state))
701 tap_set_end_state(state);
702 else
704 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
705 exit(-1);
709 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
711 int num_bytes = (scan_size + 7) / 8;
712 int bits_left = scan_size;
713 int cur_byte = 0;
715 while (num_bytes-- > 1)
717 buffer[cur_byte++] = buffer_read();
718 bits_left -= 8;
721 buffer[cur_byte] = 0x0;
723 /* There is one more partial byte left from the clock data in/out instructions */
724 if (bits_left > 1)
726 buffer[cur_byte] = buffer_read() >> 1;
728 /* 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 */
729 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
732 static void ft2232_debug_dump_buffer(void)
734 int i;
735 char line[256];
736 char* line_p = line;
738 for (i = 0; i < ft2232_buffer_size; i++)
740 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
741 if (i % 16 == 15)
743 LOG_DEBUG("%s", line);
744 line_p = line;
748 if (line_p != line)
749 LOG_DEBUG("%s", line);
752 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
754 struct jtag_command* cmd;
755 uint8_t* buffer;
756 int scan_size;
757 enum scan_type type;
758 int retval;
759 uint32_t bytes_written = 0;
760 uint32_t bytes_read = 0;
762 #ifdef _DEBUG_USB_IO_
763 struct timeval start, inter, inter2, end;
764 struct timeval d_inter, d_inter2, d_end;
765 #endif
767 #ifdef _DEBUG_USB_COMMS_
768 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
769 ft2232_debug_dump_buffer();
770 #endif
772 #ifdef _DEBUG_USB_IO_
773 gettimeofday(&start, NULL);
774 #endif
776 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
778 LOG_ERROR("couldn't write MPSSE commands to FT2232");
779 return retval;
782 #ifdef _DEBUG_USB_IO_
783 gettimeofday(&inter, NULL);
784 #endif
786 if (ft2232_expect_read)
788 /* FIXME this "timeout" is never changed ... */
789 int timeout = LIBFTDI_READ_RETRY_COUNT;
790 ft2232_buffer_size = 0;
792 #ifdef _DEBUG_USB_IO_
793 gettimeofday(&inter2, NULL);
794 #endif
796 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
798 LOG_ERROR("couldn't read from FT2232");
799 return retval;
802 #ifdef _DEBUG_USB_IO_
803 gettimeofday(&end, NULL);
805 timeval_subtract(&d_inter, &inter, &start);
806 timeval_subtract(&d_inter2, &inter2, &start);
807 timeval_subtract(&d_end, &end, &start);
809 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
810 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
811 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
812 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
813 #endif
815 ft2232_buffer_size = bytes_read;
817 if (ft2232_expect_read != ft2232_buffer_size)
819 LOG_ERROR("ft2232_expect_read (%i) != "
820 "ft2232_buffer_size (%i) "
821 "(%i retries)",
822 ft2232_expect_read,
823 ft2232_buffer_size,
824 LIBFTDI_READ_RETRY_COUNT - timeout);
825 ft2232_debug_dump_buffer();
827 exit(-1);
830 #ifdef _DEBUG_USB_COMMS_
831 LOG_DEBUG("read buffer (%i retries): %i bytes",
832 LIBFTDI_READ_RETRY_COUNT - timeout,
833 ft2232_buffer_size);
834 ft2232_debug_dump_buffer();
835 #endif
838 ft2232_expect_read = 0;
839 ft2232_read_pointer = 0;
841 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
842 * that wasn't handled by a caller-provided error handler
844 retval = ERROR_OK;
846 cmd = first;
847 while (cmd != last)
849 switch (cmd->type)
851 case JTAG_SCAN:
852 type = jtag_scan_type(cmd->cmd.scan);
853 if (type != SCAN_OUT)
855 scan_size = jtag_scan_size(cmd->cmd.scan);
856 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
857 ft2232_read_scan(type, buffer, scan_size);
858 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
859 retval = ERROR_JTAG_QUEUE_FAILED;
860 free(buffer);
862 break;
864 default:
865 break;
868 cmd = cmd->next;
871 ft2232_buffer_size = 0;
873 return retval;
877 * Function ft2232_add_pathmove
878 * moves the TAP controller from the current state to a new state through the
879 * given path, where path is an array of tap_state_t's.
881 * @param path is an array of tap_stat_t which gives the states to traverse through
882 * ending with the last state at path[num_states-1]
883 * @param num_states is the count of state steps to move through
885 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
887 int state_count = 0;
889 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
891 DEBUG_JTAG_IO("-");
893 /* this loop verifies that the path is legal and logs each state in the path */
894 while (num_states)
896 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
897 int bit_count = 0;
898 int num_states_batch = num_states > 7 ? 7 : num_states;
900 /* command "Clock Data to TMS/CS Pin (no Read)" */
901 buffer_write(0x4b);
903 /* number of states remaining */
904 buffer_write(num_states_batch - 1);
906 while (num_states_batch--) {
907 /* either TMS=0 or TMS=1 must work ... */
908 if (tap_state_transition(tap_get_state(), false)
909 == path[state_count])
910 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
911 else if (tap_state_transition(tap_get_state(), true)
912 == path[state_count])
913 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
915 /* ... or else the caller goofed BADLY */
916 else {
917 LOG_ERROR("BUG: %s -> %s isn't a valid "
918 "TAP state transition",
919 tap_state_name(tap_get_state()),
920 tap_state_name(path[state_count]));
921 exit(-1);
924 tap_set_state(path[state_count]);
925 state_count++;
926 num_states--;
929 buffer_write(tms_byte);
931 tap_set_end_state(tap_get_state());
934 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
936 int num_bytes = (scan_size + 7) / 8;
937 int bits_left = scan_size;
938 int cur_byte = 0;
939 int last_bit;
941 if (!ir_scan)
943 if (tap_get_state() != TAP_DRSHIFT)
945 move_to_state(TAP_DRSHIFT);
948 else
950 if (tap_get_state() != TAP_IRSHIFT)
952 move_to_state(TAP_IRSHIFT);
956 /* add command for complete bytes */
957 while (num_bytes > 1)
959 int thisrun_bytes;
960 if (type == SCAN_IO)
962 /* Clock Data Bytes In and Out LSB First */
963 buffer_write(0x39);
964 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
966 else if (type == SCAN_OUT)
968 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
969 buffer_write(0x19);
970 /* LOG_DEBUG("added TDI bytes (o)"); */
972 else if (type == SCAN_IN)
974 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
975 buffer_write(0x28);
976 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
979 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
980 num_bytes -= thisrun_bytes;
982 buffer_write((uint8_t) (thisrun_bytes - 1));
983 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
985 if (type != SCAN_IN)
987 /* add complete bytes */
988 while (thisrun_bytes-- > 0)
990 buffer_write(buffer[cur_byte++]);
991 bits_left -= 8;
994 else /* (type == SCAN_IN) */
996 bits_left -= 8 * (thisrun_bytes);
1000 /* the most signifcant bit is scanned during TAP movement */
1001 if (type != SCAN_IN)
1002 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1003 else
1004 last_bit = 0;
1006 /* process remaining bits but the last one */
1007 if (bits_left > 1)
1009 if (type == SCAN_IO)
1011 /* Clock Data Bits In and Out LSB First */
1012 buffer_write(0x3b);
1013 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1015 else if (type == SCAN_OUT)
1017 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1018 buffer_write(0x1b);
1019 /* LOG_DEBUG("added TDI bits (o)"); */
1021 else if (type == SCAN_IN)
1023 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1024 buffer_write(0x2a);
1025 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1028 buffer_write(bits_left - 2);
1029 if (type != SCAN_IN)
1030 buffer_write(buffer[cur_byte]);
1033 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1034 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1036 if (type == SCAN_IO)
1038 /* Clock Data Bits In and Out LSB First */
1039 buffer_write(0x3b);
1040 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1042 else if (type == SCAN_OUT)
1044 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1045 buffer_write(0x1b);
1046 /* LOG_DEBUG("added TDI bits (o)"); */
1048 else if (type == SCAN_IN)
1050 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1051 buffer_write(0x2a);
1052 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1054 buffer_write(0x0);
1055 buffer_write(last_bit);
1057 else
1059 int tms_bits;
1060 int tms_count;
1061 uint8_t mpsse_cmd;
1063 /* move from Shift-IR/DR to end state */
1064 if (type != SCAN_OUT)
1066 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1067 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1068 tms_bits = 0x01;
1069 tms_count = 2;
1070 /* Clock Data to TMS/CS Pin with Read */
1071 mpsse_cmd = 0x6b;
1073 else
1075 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1076 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1077 /* Clock Data to TMS/CS Pin (no Read) */
1078 mpsse_cmd = 0x4b;
1081 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1082 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1085 if (tap_get_state() != tap_get_end_state())
1087 move_to_state(tap_get_end_state());
1091 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1093 int num_bytes = (scan_size + 7) / 8;
1094 int bits_left = scan_size;
1095 int cur_byte = 0;
1096 int last_bit;
1097 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1098 uint8_t* receive_pointer = receive_buffer;
1099 uint32_t bytes_written;
1100 uint32_t bytes_read;
1101 int retval;
1102 int thisrun_read = 0;
1104 if (cmd->ir_scan)
1106 LOG_ERROR("BUG: large IR scans are not supported");
1107 exit(-1);
1110 if (tap_get_state() != TAP_DRSHIFT)
1112 move_to_state(TAP_DRSHIFT);
1115 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1117 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1118 exit(-1);
1120 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1121 ft2232_buffer_size, (int)bytes_written);
1122 ft2232_buffer_size = 0;
1124 /* add command for complete bytes */
1125 while (num_bytes > 1)
1127 int thisrun_bytes;
1129 if (type == SCAN_IO)
1131 /* Clock Data Bytes In and Out LSB First */
1132 buffer_write(0x39);
1133 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1135 else if (type == SCAN_OUT)
1137 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1138 buffer_write(0x19);
1139 /* LOG_DEBUG("added TDI bytes (o)"); */
1141 else if (type == SCAN_IN)
1143 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1144 buffer_write(0x28);
1145 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1148 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1149 thisrun_read = thisrun_bytes;
1150 num_bytes -= thisrun_bytes;
1151 buffer_write((uint8_t) (thisrun_bytes - 1));
1152 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1154 if (type != SCAN_IN)
1156 /* add complete bytes */
1157 while (thisrun_bytes-- > 0)
1159 buffer_write(buffer[cur_byte]);
1160 cur_byte++;
1161 bits_left -= 8;
1164 else /* (type == SCAN_IN) */
1166 bits_left -= 8 * (thisrun_bytes);
1169 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1171 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1172 exit(-1);
1174 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1175 ft2232_buffer_size,
1176 (int)bytes_written);
1177 ft2232_buffer_size = 0;
1179 if (type != SCAN_OUT)
1181 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1183 LOG_ERROR("couldn't read from FT2232");
1184 exit(-1);
1186 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1187 thisrun_read,
1188 (int)bytes_read);
1189 receive_pointer += bytes_read;
1193 thisrun_read = 0;
1195 /* the most signifcant bit is scanned during TAP movement */
1196 if (type != SCAN_IN)
1197 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1198 else
1199 last_bit = 0;
1201 /* process remaining bits but the last one */
1202 if (bits_left > 1)
1204 if (type == SCAN_IO)
1206 /* Clock Data Bits In and Out LSB First */
1207 buffer_write(0x3b);
1208 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1210 else if (type == SCAN_OUT)
1212 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1213 buffer_write(0x1b);
1214 /* LOG_DEBUG("added TDI bits (o)"); */
1216 else if (type == SCAN_IN)
1218 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1219 buffer_write(0x2a);
1220 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1222 buffer_write(bits_left - 2);
1223 if (type != SCAN_IN)
1224 buffer_write(buffer[cur_byte]);
1226 if (type != SCAN_OUT)
1227 thisrun_read += 2;
1230 if (tap_get_end_state() == TAP_DRSHIFT)
1232 if (type == SCAN_IO)
1234 /* Clock Data Bits In and Out LSB First */
1235 buffer_write(0x3b);
1236 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1238 else if (type == SCAN_OUT)
1240 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1241 buffer_write(0x1b);
1242 /* LOG_DEBUG("added TDI bits (o)"); */
1244 else if (type == SCAN_IN)
1246 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1247 buffer_write(0x2a);
1248 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1250 buffer_write(0x0);
1251 buffer_write(last_bit);
1253 else
1255 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1256 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1257 uint8_t mpsse_cmd;
1259 /* move from Shift-IR/DR to end state */
1260 if (type != SCAN_OUT)
1262 /* Clock Data to TMS/CS Pin with Read */
1263 mpsse_cmd = 0x6b;
1264 /* LOG_DEBUG("added TMS scan (read)"); */
1266 else
1268 /* Clock Data to TMS/CS Pin (no Read) */
1269 mpsse_cmd = 0x4b;
1270 /* LOG_DEBUG("added TMS scan (no read)"); */
1273 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1274 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1277 if (type != SCAN_OUT)
1278 thisrun_read += 1;
1280 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1282 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1283 exit(-1);
1285 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1286 ft2232_buffer_size,
1287 (int)bytes_written);
1288 ft2232_buffer_size = 0;
1290 if (type != SCAN_OUT)
1292 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1294 LOG_ERROR("couldn't read from FT2232");
1295 exit(-1);
1297 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1298 thisrun_read,
1299 (int)bytes_read);
1300 receive_pointer += bytes_read;
1303 return ERROR_OK;
1306 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1308 int predicted_size = 3;
1309 int num_bytes = (scan_size - 1) / 8;
1311 if (tap_get_state() != TAP_DRSHIFT)
1312 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1314 if (type == SCAN_IN) /* only from device to host */
1316 /* complete bytes */
1317 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1319 /* remaining bits - 1 (up to 7) */
1320 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1322 else /* host to device, or bidirectional */
1324 /* complete bytes */
1325 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1327 /* remaining bits -1 (up to 7) */
1328 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1331 return predicted_size;
1334 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1336 int predicted_size = 0;
1338 if (type != SCAN_OUT)
1340 /* complete bytes */
1341 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1343 /* remaining bits - 1 */
1344 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1346 /* last bit (from TMS scan) */
1347 predicted_size += 1;
1350 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1352 return predicted_size;
1355 /* semi-generic FT2232/FT4232 reset code */
1356 static void ftx23_reset(int trst, int srst)
1358 enum reset_types jtag_reset_config = jtag_get_reset_config();
1359 if (trst == 1)
1361 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1362 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1363 else
1364 low_output &= ~nTRST; /* switch output low */
1366 else if (trst == 0)
1368 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1369 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1370 else
1371 low_output |= nTRST; /* switch output high */
1374 if (srst == 1)
1376 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1377 low_output &= ~nSRST; /* switch output low */
1378 else
1379 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1381 else if (srst == 0)
1383 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1384 low_output |= nSRST; /* switch output high */
1385 else
1386 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1389 /* command "set data bits low byte" */
1390 buffer_write(0x80);
1391 buffer_write(low_output);
1392 buffer_write(low_direction);
1395 static void jtagkey_reset(int trst, int srst)
1397 enum reset_types jtag_reset_config = jtag_get_reset_config();
1398 if (trst == 1)
1400 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1401 high_output &= ~nTRSTnOE;
1402 else
1403 high_output &= ~nTRST;
1405 else if (trst == 0)
1407 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1408 high_output |= nTRSTnOE;
1409 else
1410 high_output |= nTRST;
1413 if (srst == 1)
1415 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1416 high_output &= ~nSRST;
1417 else
1418 high_output &= ~nSRSTnOE;
1420 else if (srst == 0)
1422 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1423 high_output |= nSRST;
1424 else
1425 high_output |= nSRSTnOE;
1428 /* command "set data bits high byte" */
1429 buffer_write(0x82);
1430 buffer_write(high_output);
1431 buffer_write(high_direction);
1432 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1433 high_direction);
1436 static void olimex_jtag_reset(int trst, int srst)
1438 enum reset_types jtag_reset_config = jtag_get_reset_config();
1439 if (trst == 1)
1441 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1442 high_output &= ~nTRSTnOE;
1443 else
1444 high_output &= ~nTRST;
1446 else if (trst == 0)
1448 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1449 high_output |= nTRSTnOE;
1450 else
1451 high_output |= nTRST;
1454 if (srst == 1)
1456 high_output |= nSRST;
1458 else if (srst == 0)
1460 high_output &= ~nSRST;
1463 /* command "set data bits high byte" */
1464 buffer_write(0x82);
1465 buffer_write(high_output);
1466 buffer_write(high_direction);
1467 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1468 high_direction);
1471 static void axm0432_jtag_reset(int trst, int srst)
1473 if (trst == 1)
1475 tap_set_state(TAP_RESET);
1476 high_output &= ~nTRST;
1478 else if (trst == 0)
1480 high_output |= nTRST;
1483 if (srst == 1)
1485 high_output &= ~nSRST;
1487 else if (srst == 0)
1489 high_output |= nSRST;
1492 /* command "set data bits low byte" */
1493 buffer_write(0x82);
1494 buffer_write(high_output);
1495 buffer_write(high_direction);
1496 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1497 high_direction);
1500 static void flyswatter_reset(int trst, int srst)
1502 if (trst == 1)
1504 low_output &= ~nTRST;
1506 else if (trst == 0)
1508 low_output |= nTRST;
1511 if (srst == 1)
1513 low_output |= nSRST;
1515 else if (srst == 0)
1517 low_output &= ~nSRST;
1520 /* command "set data bits low byte" */
1521 buffer_write(0x80);
1522 buffer_write(low_output);
1523 buffer_write(low_direction);
1524 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1527 static void turtle_reset(int trst, int srst)
1529 trst = trst;
1531 if (srst == 1)
1533 low_output |= nSRST;
1535 else if (srst == 0)
1537 low_output &= ~nSRST;
1540 /* command "set data bits low byte" */
1541 buffer_write(0x80);
1542 buffer_write(low_output);
1543 buffer_write(low_direction);
1544 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1547 static void comstick_reset(int trst, int srst)
1549 if (trst == 1)
1551 high_output &= ~nTRST;
1553 else if (trst == 0)
1555 high_output |= nTRST;
1558 if (srst == 1)
1560 high_output &= ~nSRST;
1562 else if (srst == 0)
1564 high_output |= nSRST;
1567 /* command "set data bits high byte" */
1568 buffer_write(0x82);
1569 buffer_write(high_output);
1570 buffer_write(high_direction);
1571 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1572 high_direction);
1575 static void stm32stick_reset(int trst, int srst)
1577 if (trst == 1)
1579 high_output &= ~nTRST;
1581 else if (trst == 0)
1583 high_output |= nTRST;
1586 if (srst == 1)
1588 low_output &= ~nSRST;
1590 else if (srst == 0)
1592 low_output |= nSRST;
1595 /* command "set data bits low byte" */
1596 buffer_write(0x80);
1597 buffer_write(low_output);
1598 buffer_write(low_direction);
1600 /* command "set data bits high byte" */
1601 buffer_write(0x82);
1602 buffer_write(high_output);
1603 buffer_write(high_direction);
1604 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1605 high_direction);
1608 static void sheevaplug_reset(int trst, int srst)
1610 if (trst == 1)
1611 high_output &= ~nTRST;
1612 else if (trst == 0)
1613 high_output |= nTRST;
1615 if (srst == 1)
1616 high_output &= ~nSRSTnOE;
1617 else if (srst == 0)
1618 high_output |= nSRSTnOE;
1620 /* command "set data bits high byte" */
1621 buffer_write(0x82);
1622 buffer_write(high_output);
1623 buffer_write(high_direction);
1624 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1627 static void redbee_reset(int trst, int srst)
1629 if (trst == 1)
1631 tap_set_state(TAP_RESET);
1632 high_output &= ~nTRST;
1634 else if (trst == 0)
1636 high_output |= nTRST;
1639 if (srst == 1)
1641 high_output &= ~nSRST;
1643 else if (srst == 0)
1645 high_output |= nSRST;
1648 /* command "set data bits low byte" */
1649 buffer_write(0x82);
1650 buffer_write(high_output);
1651 buffer_write(high_direction);
1652 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1653 "high_direction: 0x%2.2x", trst, srst, high_output,
1654 high_direction);
1657 static int ft2232_execute_runtest(struct jtag_command *cmd)
1659 int retval;
1660 int i;
1661 int predicted_size = 0;
1662 retval = ERROR_OK;
1664 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1665 cmd->cmd.runtest->num_cycles,
1666 tap_state_name(cmd->cmd.runtest->end_state));
1668 /* only send the maximum buffer size that FT2232C can handle */
1669 predicted_size = 0;
1670 if (tap_get_state() != TAP_IDLE)
1671 predicted_size += 3;
1672 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1673 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1674 predicted_size += 3;
1675 if (tap_get_end_state() != TAP_IDLE)
1676 predicted_size += 3;
1677 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1679 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1680 retval = ERROR_JTAG_QUEUE_FAILED;
1681 require_send = 0;
1682 first_unsent = cmd;
1684 if (tap_get_state() != TAP_IDLE)
1686 move_to_state(TAP_IDLE);
1687 require_send = 1;
1689 i = cmd->cmd.runtest->num_cycles;
1690 while (i > 0)
1692 /* there are no state transitions in this code, so omit state tracking */
1694 /* command "Clock Data to TMS/CS Pin (no Read)" */
1695 buffer_write(0x4b);
1697 /* scan 7 bits */
1698 buffer_write((i > 7) ? 6 : (i - 1));
1700 /* TMS data bits */
1701 buffer_write(0x0);
1703 i -= (i > 7) ? 7 : i;
1704 /* LOG_DEBUG("added TMS scan (no read)"); */
1707 ft2232_end_state(cmd->cmd.runtest->end_state);
1709 if (tap_get_state() != tap_get_end_state())
1711 move_to_state(tap_get_end_state());
1714 require_send = 1;
1715 DEBUG_JTAG_IO("runtest: %i, end in %s",
1716 cmd->cmd.runtest->num_cycles,
1717 tap_state_name(tap_get_end_state()));
1718 return retval;
1721 static int ft2232_execute_statemove(struct jtag_command *cmd)
1723 int predicted_size = 0;
1724 int retval = ERROR_OK;
1726 DEBUG_JTAG_IO("statemove end in %s",
1727 tap_state_name(cmd->cmd.statemove->end_state));
1729 /* only send the maximum buffer size that FT2232C can handle */
1730 predicted_size = 3;
1731 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1733 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1734 retval = ERROR_JTAG_QUEUE_FAILED;
1735 require_send = 0;
1736 first_unsent = cmd;
1738 ft2232_end_state(cmd->cmd.statemove->end_state);
1740 /* For TAP_RESET, ignore the current recorded state. It's often
1741 * wrong at server startup, and this transation is critical whenever
1742 * it's requested.
1744 if (tap_get_end_state() == TAP_RESET) {
1745 clock_tms(0x4b, 0xff, 5, 0);
1746 require_send = 1;
1748 /* shortest-path move to desired end state */
1749 } else if (tap_get_state() != tap_get_end_state())
1751 move_to_state(tap_get_end_state());
1752 require_send = 1;
1755 return retval;
1759 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1760 * (or SWD) state machine.
1762 static int ft2232_execute_tms(struct jtag_command *cmd)
1764 int retval = ERROR_OK;
1765 unsigned num_bits = cmd->cmd.tms->num_bits;
1766 const uint8_t *bits = cmd->cmd.tms->bits;
1767 unsigned count;
1769 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1771 /* only send the maximum buffer size that FT2232C can handle */
1772 count = 3 * DIV_ROUND_UP(num_bits, 4);
1773 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1774 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1775 retval = ERROR_JTAG_QUEUE_FAILED;
1777 require_send = 0;
1778 first_unsent = cmd;
1781 /* Shift out in batches of at most 6 bits; there's a report of an
1782 * FT2232 bug in this area, where shifting exactly 7 bits can make
1783 * problems with TMS signaling for the last clock cycle:
1785 * http://developer.intra2net.com/mailarchive/html/
1786 * libftdi/2009/msg00292.html
1788 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1790 * Note that pathmoves in JTAG are not often seven bits, so that
1791 * isn't a particularly likely situation outside of "special"
1792 * signaling such as switching between JTAG and SWD modes.
1794 while (num_bits) {
1795 if (num_bits <= 6) {
1796 buffer_write(0x4b);
1797 buffer_write(num_bits - 1);
1798 buffer_write(*bits & 0x3f);
1799 break;
1802 /* Yes, this is lazy ... we COULD shift out more data
1803 * bits per operation, but doing it in nybbles is easy
1805 buffer_write(0x4b);
1806 buffer_write(3);
1807 buffer_write(*bits & 0xf);
1808 num_bits -= 4;
1810 count = (num_bits > 4) ? 4 : num_bits;
1812 buffer_write(0x4b);
1813 buffer_write(count - 1);
1814 buffer_write((*bits >> 4) & 0xf);
1815 num_bits -= count;
1817 bits++;
1820 require_send = 1;
1821 return retval;
1824 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1826 int predicted_size = 0;
1827 int retval = ERROR_OK;
1829 tap_state_t* path = cmd->cmd.pathmove->path;
1830 int num_states = cmd->cmd.pathmove->num_states;
1832 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1833 tap_state_name(tap_get_state()),
1834 tap_state_name(path[num_states-1]));
1836 /* only send the maximum buffer size that FT2232C can handle */
1837 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1838 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1840 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1841 retval = ERROR_JTAG_QUEUE_FAILED;
1843 require_send = 0;
1844 first_unsent = cmd;
1847 ft2232_add_pathmove(path, num_states);
1848 require_send = 1;
1850 return retval;
1853 static int ft2232_execute_scan(struct jtag_command *cmd)
1855 uint8_t* buffer;
1856 int scan_size; /* size of IR or DR scan */
1857 int predicted_size = 0;
1858 int retval = ERROR_OK;
1860 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1862 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1864 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1866 predicted_size = ft2232_predict_scan_out(scan_size, type);
1867 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1869 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1870 /* unsent commands before this */
1871 if (first_unsent != cmd)
1872 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1873 retval = ERROR_JTAG_QUEUE_FAILED;
1875 /* current command */
1876 ft2232_end_state(cmd->cmd.scan->end_state);
1877 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1878 require_send = 0;
1879 first_unsent = cmd->next;
1880 if (buffer)
1881 free(buffer);
1882 return retval;
1884 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1886 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1887 first_unsent,
1888 cmd);
1889 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1890 retval = ERROR_JTAG_QUEUE_FAILED;
1891 require_send = 0;
1892 first_unsent = cmd;
1894 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1895 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1896 ft2232_end_state(cmd->cmd.scan->end_state);
1897 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1898 require_send = 1;
1899 if (buffer)
1900 free(buffer);
1901 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1902 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1903 tap_state_name(tap_get_end_state()));
1904 return retval;
1908 static int ft2232_execute_reset(struct jtag_command *cmd)
1910 int retval;
1911 int predicted_size = 0;
1912 retval = ERROR_OK;
1914 DEBUG_JTAG_IO("reset trst: %i srst %i",
1915 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1917 /* only send the maximum buffer size that FT2232C can handle */
1918 predicted_size = 3;
1919 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1921 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1922 retval = ERROR_JTAG_QUEUE_FAILED;
1923 require_send = 0;
1924 first_unsent = cmd;
1927 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1929 tap_set_state(TAP_RESET);
1932 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1933 require_send = 1;
1935 DEBUG_JTAG_IO("trst: %i, srst: %i",
1936 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1937 return retval;
1940 static int ft2232_execute_sleep(struct jtag_command *cmd)
1942 int retval;
1943 retval = ERROR_OK;
1945 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1947 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1948 retval = ERROR_JTAG_QUEUE_FAILED;
1949 first_unsent = cmd->next;
1950 jtag_sleep(cmd->cmd.sleep->us);
1951 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1952 cmd->cmd.sleep->us,
1953 tap_state_name(tap_get_state()));
1954 return retval;
1957 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1959 int retval;
1960 retval = ERROR_OK;
1962 /* this is only allowed while in a stable state. A check for a stable
1963 * state was done in jtag_add_clocks()
1965 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1966 retval = ERROR_JTAG_QUEUE_FAILED;
1967 DEBUG_JTAG_IO("clocks %i while in %s",
1968 cmd->cmd.stableclocks->num_cycles,
1969 tap_state_name(tap_get_state()));
1970 return retval;
1973 static int ft2232_execute_command(struct jtag_command *cmd)
1975 int retval;
1977 switch (cmd->type)
1979 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1980 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1981 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
1982 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1983 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1984 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1985 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1986 case JTAG_TMS:
1987 retval = ft2232_execute_tms(cmd);
1988 break;
1989 default:
1990 LOG_ERROR("BUG: unknown JTAG command type encountered");
1991 retval = ERROR_JTAG_QUEUE_FAILED;
1992 break;
1994 return retval;
1997 static int ft2232_execute_queue(void)
1999 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2000 int retval;
2002 first_unsent = cmd; /* next command that has to be sent */
2003 require_send = 0;
2005 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2006 * that wasn't handled by a caller-provided error handler
2008 retval = ERROR_OK;
2010 ft2232_buffer_size = 0;
2011 ft2232_expect_read = 0;
2013 /* blink, if the current layout has that feature */
2014 if (layout->blink)
2015 layout->blink();
2017 while (cmd)
2019 if (ft2232_execute_command(cmd) != ERROR_OK)
2020 retval = ERROR_JTAG_QUEUE_FAILED;
2021 /* Start reading input before FT2232 TX buffer fills up */
2022 cmd = cmd->next;
2023 if (ft2232_expect_read > 256)
2025 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2026 retval = ERROR_JTAG_QUEUE_FAILED;
2027 first_unsent = cmd;
2031 if (require_send > 0)
2032 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2033 retval = ERROR_JTAG_QUEUE_FAILED;
2035 return retval;
2038 #if BUILD_FT2232_FTD2XX == 1
2039 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2041 FT_STATUS status;
2042 DWORD deviceID;
2043 char SerialNumber[16];
2044 char Description[64];
2045 DWORD openex_flags = 0;
2046 char* openex_string = NULL;
2047 uint8_t latency_timer;
2049 if (layout == NULL) {
2050 LOG_WARNING("No ft2232 layout specified'");
2051 return ERROR_JTAG_INIT_FAILED;
2054 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2056 #if IS_WIN32 == 0
2057 /* Add non-standard Vid/Pid to the linux driver */
2058 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2060 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2062 #endif
2064 if (ft2232_device_desc && ft2232_serial)
2066 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2067 ft2232_device_desc = NULL;
2070 if (ft2232_device_desc)
2072 openex_string = ft2232_device_desc;
2073 openex_flags = FT_OPEN_BY_DESCRIPTION;
2075 else if (ft2232_serial)
2077 openex_string = ft2232_serial;
2078 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2080 else
2082 LOG_ERROR("neither device description nor serial number specified");
2083 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2085 return ERROR_JTAG_INIT_FAILED;
2088 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2089 if (status != FT_OK) {
2090 /* under Win32, the FTD2XX driver appends an "A" to the end
2091 * of the description, if we tried by the desc, then
2092 * try by the alternate "A" description. */
2093 if (openex_string == ft2232_device_desc) {
2094 /* Try the alternate method. */
2095 openex_string = ft2232_device_desc_A;
2096 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2097 if (status == FT_OK) {
2098 /* yea, the "alternate" method worked! */
2099 } else {
2100 /* drat, give the user a meaningfull message.
2101 * telling the use we tried *BOTH* methods. */
2102 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2103 ft2232_device_desc,
2104 ft2232_device_desc_A);
2109 if (status != FT_OK)
2111 DWORD num_devices;
2113 if (more)
2115 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2116 *try_more = 1;
2117 return ERROR_JTAG_INIT_FAILED;
2119 LOG_ERROR("unable to open ftdi device: %lu", status);
2120 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2121 if (status == FT_OK)
2123 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2124 uint32_t i;
2126 for (i = 0; i < num_devices; i++)
2127 desc_array[i] = malloc(64);
2129 desc_array[num_devices] = NULL;
2131 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2133 if (status == FT_OK)
2135 LOG_ERROR("ListDevices: %lu\n", num_devices);
2136 for (i = 0; i < num_devices; i++)
2137 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2140 for (i = 0; i < num_devices; i++)
2141 free(desc_array[i]);
2143 free(desc_array);
2145 else
2147 LOG_ERROR("ListDevices: NONE\n");
2149 return ERROR_JTAG_INIT_FAILED;
2152 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2154 LOG_ERROR("unable to set latency timer: %lu", status);
2155 return ERROR_JTAG_INIT_FAILED;
2158 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2160 LOG_ERROR("unable to get latency timer: %lu", status);
2161 return ERROR_JTAG_INIT_FAILED;
2163 else
2165 LOG_DEBUG("current latency timer: %i", latency_timer);
2168 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2170 LOG_ERROR("unable to set timeouts: %lu", status);
2171 return ERROR_JTAG_INIT_FAILED;
2174 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2176 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2177 return ERROR_JTAG_INIT_FAILED;
2180 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2182 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2183 return ERROR_JTAG_INIT_FAILED;
2185 else
2187 static const char* type_str[] =
2188 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2189 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2190 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2191 ? ftdi_device : FT_DEVICE_UNKNOWN;
2192 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2193 LOG_INFO("deviceID: %lu", deviceID);
2194 LOG_INFO("SerialNumber: %s", SerialNumber);
2195 LOG_INFO("Description: %s", Description);
2198 return ERROR_OK;
2201 static int ft2232_purge_ftd2xx(void)
2203 FT_STATUS status;
2205 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2207 LOG_ERROR("error purging ftd2xx device: %lu", status);
2208 return ERROR_JTAG_INIT_FAILED;
2211 return ERROR_OK;
2214 #endif /* BUILD_FT2232_FTD2XX == 1 */
2216 #if BUILD_FT2232_LIBFTDI == 1
2217 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2219 uint8_t latency_timer;
2221 if (layout == NULL) {
2222 LOG_WARNING("No ft2232 layout specified'");
2223 return ERROR_JTAG_INIT_FAILED;
2226 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2227 layout->name, vid, pid);
2229 if (ftdi_init(&ftdic) < 0)
2230 return ERROR_JTAG_INIT_FAILED;
2232 /* default to INTERFACE_A */
2233 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2235 if (ftdi_set_interface(&ftdic, channel) < 0)
2237 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2238 return ERROR_JTAG_INIT_FAILED;
2241 /* context, vendor id, product id */
2242 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2243 ft2232_serial) < 0)
2245 if (more)
2246 LOG_WARNING("unable to open ftdi device (trying more): %s",
2247 ftdic.error_str);
2248 else
2249 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2250 *try_more = 1;
2251 return ERROR_JTAG_INIT_FAILED;
2254 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2255 if (ftdi_usb_reset(&ftdic) < 0)
2257 LOG_ERROR("unable to reset ftdi device");
2258 return ERROR_JTAG_INIT_FAILED;
2261 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2263 LOG_ERROR("unable to set latency timer");
2264 return ERROR_JTAG_INIT_FAILED;
2267 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2269 LOG_ERROR("unable to get latency timer");
2270 return ERROR_JTAG_INIT_FAILED;
2272 else
2274 LOG_DEBUG("current latency timer: %i", latency_timer);
2277 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2279 ftdi_device = ftdic.type;
2280 static const char* type_str[] =
2281 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2282 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2283 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2284 ? ftdi_device : no_of_known_types;
2285 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2286 return ERROR_OK;
2289 static int ft2232_purge_libftdi(void)
2291 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2293 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2294 return ERROR_JTAG_INIT_FAILED;
2297 return ERROR_OK;
2300 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2302 static int ft2232_init(void)
2304 uint8_t buf[1];
2305 int retval;
2306 uint32_t bytes_written;
2308 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2310 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2312 else
2314 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2317 if (layout == NULL) {
2318 LOG_WARNING("No ft2232 layout specified'");
2319 return ERROR_JTAG_INIT_FAILED;
2322 for (int i = 0; 1; i++)
2325 * "more indicates that there are more IDs to try, so we should
2326 * not print an error for an ID mismatch (but for anything
2327 * else, we should).
2329 * try_more indicates that the error code returned indicates an
2330 * ID mismatch (and nothing else) and that we should proceeed
2331 * with the next ID pair.
2333 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2334 int try_more = 0;
2336 #if BUILD_FT2232_FTD2XX == 1
2337 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2338 more, &try_more);
2339 #elif BUILD_FT2232_LIBFTDI == 1
2340 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2341 more, &try_more, layout->channel);
2342 #endif
2343 if (retval >= 0)
2344 break;
2345 if (!more || !try_more)
2346 return retval;
2349 ft2232_buffer_size = 0;
2350 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2352 if (layout->init() != ERROR_OK)
2353 return ERROR_JTAG_INIT_FAILED;
2355 if (ft2232_device_is_highspeed())
2357 #ifndef BUILD_FT2232_HIGHSPEED
2358 #if BUILD_FT2232_FTD2XX == 1
2359 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2360 #elif BUILD_FT2232_LIBFTDI == 1
2361 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2362 #endif
2363 #endif
2364 /* make sure the legacy mode is disabled */
2365 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2366 return ERROR_JTAG_INIT_FAILED;
2369 ft2232_speed(jtag_get_speed());
2371 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2372 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2374 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2375 return ERROR_JTAG_INIT_FAILED;
2378 #if BUILD_FT2232_FTD2XX == 1
2379 return ft2232_purge_ftd2xx();
2380 #elif BUILD_FT2232_LIBFTDI == 1
2381 return ft2232_purge_libftdi();
2382 #endif
2384 return ERROR_OK;
2387 /** Updates defaults for DBUS signals: the four JTAG signals
2388 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2390 static inline void ftx232_init_head(void)
2392 low_output = 0x08;
2393 low_direction = 0x0b;
2396 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2397 * the four GPIOL signals. Initialization covers value and direction,
2398 * as customized for each layout.
2400 static int ftx232_init_tail(void)
2402 uint8_t buf[3];
2403 uint32_t bytes_written;
2405 enum reset_types jtag_reset_config = jtag_get_reset_config();
2406 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2408 low_direction &= ~nTRSTnOE; /* nTRST input */
2409 low_output &= ~nTRST; /* nTRST = 0 */
2411 else
2413 low_direction |= nTRSTnOE; /* nTRST output */
2414 low_output |= nTRST; /* nTRST = 1 */
2417 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2419 low_direction |= nSRSTnOE; /* nSRST output */
2420 low_output |= nSRST; /* nSRST = 1 */
2422 else
2424 low_direction &= ~nSRSTnOE; /* nSRST input */
2425 low_output &= ~nSRST; /* nSRST = 0 */
2428 /* initialize low byte for jtag */
2429 buf[0] = 0x80; /* command "set data bits low byte" */
2430 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2431 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2432 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2434 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2436 LOG_ERROR("couldn't initialize FT2232 DBUS");
2437 return ERROR_JTAG_INIT_FAILED;
2440 return ERROR_OK;
2443 static int usbjtag_init(void)
2446 * NOTE: This is now _specific_ to the "usbjtag" layout.
2447 * Don't try cram any more layouts into this.
2449 ftx232_init_head();
2451 nTRST = 0x10;
2452 nTRSTnOE = 0x10;
2453 nSRST = 0x40;
2454 nSRSTnOE = 0x40;
2456 return ftx232_init_tail();
2459 static int lm3s811_jtag_init(void)
2461 ftx232_init_head();
2463 /* There are multiple revisions of LM3S811 eval boards:
2464 * - Rev B (and older?) boards have no SWO trace support.
2465 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2466 * they should use the "luminary_icdi" layout instead.
2468 nTRST = 0x0;
2469 nTRSTnOE = 0x00;
2470 nSRST = 0x20;
2471 nSRSTnOE = 0x20;
2472 low_output = 0x88;
2473 low_direction = 0x8b;
2475 return ftx232_init_tail();
2478 static int icdi_jtag_init(void)
2480 ftx232_init_head();
2482 /* Most Luminary eval boards support SWO trace output,
2483 * and should use this "luminary_icdi" layout.
2485 nTRST = 0x0;
2486 nTRSTnOE = 0x00;
2487 nSRST = 0x20;
2488 nSRSTnOE = 0x20;
2489 low_output = 0x88;
2490 low_direction = 0xcb;
2492 return ftx232_init_tail();
2495 static int signalyzer_init(void)
2497 ftx232_init_head();
2499 nTRST = 0x10;
2500 nTRSTnOE = 0x10;
2501 nSRST = 0x20;
2502 nSRSTnOE = 0x20;
2503 return ftx232_init_tail();
2506 static int axm0432_jtag_init(void)
2508 uint8_t buf[3];
2509 uint32_t bytes_written;
2511 low_output = 0x08;
2512 low_direction = 0x2b;
2514 /* initialize low byte for jtag */
2515 buf[0] = 0x80; /* command "set data bits low byte" */
2516 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2517 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2518 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2520 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2522 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2523 return ERROR_JTAG_INIT_FAILED;
2526 if (strcmp(layout->name, "axm0432_jtag") == 0)
2528 nTRST = 0x08;
2529 nTRSTnOE = 0x0; /* No output enable for TRST*/
2530 nSRST = 0x04;
2531 nSRSTnOE = 0x0; /* No output enable for SRST*/
2533 else
2535 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2536 exit(-1);
2539 high_output = 0x0;
2540 high_direction = 0x0c;
2542 enum reset_types jtag_reset_config = jtag_get_reset_config();
2543 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2545 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2547 else
2549 high_output |= nTRST;
2552 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2554 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2556 else
2558 high_output |= nSRST;
2561 /* initialize high port */
2562 buf[0] = 0x82; /* command "set data bits high byte" */
2563 buf[1] = high_output; /* value */
2564 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2565 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2567 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2569 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2570 return ERROR_JTAG_INIT_FAILED;
2573 return ERROR_OK;
2576 static int redbee_init(void)
2578 uint8_t buf[3];
2579 uint32_t bytes_written;
2581 low_output = 0x08;
2582 low_direction = 0x2b;
2584 /* initialize low byte for jtag */
2585 /* command "set data bits low byte" */
2586 buf[0] = 0x80;
2587 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2588 buf[2] = low_direction;
2589 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2590 buf[1] = low_output;
2591 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2593 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
2594 || (bytes_written != 3))
2596 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2597 return ERROR_JTAG_INIT_FAILED;
2600 nTRST = 0x08;
2601 nTRSTnOE = 0x0; /* No output enable for TRST*/
2602 nSRST = 0x04;
2603 nSRSTnOE = 0x0; /* No output enable for SRST*/
2605 high_output = 0x0;
2606 high_direction = 0x0c;
2608 enum reset_types jtag_reset_config = jtag_get_reset_config();
2609 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2611 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2613 else
2615 high_output |= nTRST;
2618 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2620 LOG_ERROR("can't set nSRST to push-pull on redbee");
2622 else
2624 high_output |= nSRST;
2627 /* initialize high port */
2628 buf[0] = 0x82; /* command "set data bits high byte" */
2629 buf[1] = high_output; /* value */
2630 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2631 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2633 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
2634 || (bytes_written != 3))
2636 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2637 return ERROR_JTAG_INIT_FAILED;
2640 return ERROR_OK;
2643 static int jtagkey_init(void)
2645 uint8_t buf[3];
2646 uint32_t bytes_written;
2648 low_output = 0x08;
2649 low_direction = 0x1b;
2651 /* initialize low byte for jtag */
2652 buf[0] = 0x80; /* command "set data bits low byte" */
2653 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2654 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2655 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2657 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2659 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2660 return ERROR_JTAG_INIT_FAILED;
2663 if (strcmp(layout->name, "jtagkey") == 0)
2665 nTRST = 0x01;
2666 nTRSTnOE = 0x4;
2667 nSRST = 0x02;
2668 nSRSTnOE = 0x08;
2670 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2671 || (strcmp(layout->name, "oocdlink") == 0))
2673 nTRST = 0x02;
2674 nTRSTnOE = 0x1;
2675 nSRST = 0x08;
2676 nSRSTnOE = 0x04;
2678 else
2680 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2681 exit(-1);
2684 high_output = 0x0;
2685 high_direction = 0x0f;
2687 enum reset_types jtag_reset_config = jtag_get_reset_config();
2688 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2690 high_output |= nTRSTnOE;
2691 high_output &= ~nTRST;
2693 else
2695 high_output &= ~nTRSTnOE;
2696 high_output |= nTRST;
2699 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2701 high_output &= ~nSRSTnOE;
2702 high_output |= nSRST;
2704 else
2706 high_output |= nSRSTnOE;
2707 high_output &= ~nSRST;
2710 /* initialize high port */
2711 buf[0] = 0x82; /* command "set data bits high byte" */
2712 buf[1] = high_output; /* value */
2713 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2714 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2716 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2718 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2719 return ERROR_JTAG_INIT_FAILED;
2722 return ERROR_OK;
2725 static int olimex_jtag_init(void)
2727 uint8_t buf[3];
2728 uint32_t bytes_written;
2730 low_output = 0x08;
2731 low_direction = 0x1b;
2733 /* initialize low byte for jtag */
2734 buf[0] = 0x80; /* command "set data bits low byte" */
2735 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2736 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2737 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2739 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2741 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2742 return ERROR_JTAG_INIT_FAILED;
2745 nTRST = 0x01;
2746 nTRSTnOE = 0x4;
2747 nSRST = 0x02;
2748 nSRSTnOE = 0x00; /* no output enable for nSRST */
2750 high_output = 0x0;
2751 high_direction = 0x0f;
2753 enum reset_types jtag_reset_config = jtag_get_reset_config();
2754 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2756 high_output |= nTRSTnOE;
2757 high_output &= ~nTRST;
2759 else
2761 high_output &= ~nTRSTnOE;
2762 high_output |= nTRST;
2765 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2767 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2769 else
2771 high_output &= ~nSRST;
2774 /* turn red LED on */
2775 high_output |= 0x08;
2777 /* initialize high port */
2778 buf[0] = 0x82; /* command "set data bits high byte" */
2779 buf[1] = high_output; /* value */
2780 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2781 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2783 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2785 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2786 return ERROR_JTAG_INIT_FAILED;
2789 return ERROR_OK;
2792 static int flyswatter_init(void)
2794 uint8_t buf[3];
2795 uint32_t bytes_written;
2797 low_output = 0x18;
2798 low_direction = 0xfb;
2800 /* initialize low byte for jtag */
2801 buf[0] = 0x80; /* command "set data bits low byte" */
2802 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2803 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2804 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2806 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2808 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2809 return ERROR_JTAG_INIT_FAILED;
2812 nTRST = 0x10;
2813 nTRSTnOE = 0x0; /* not output enable for nTRST */
2814 nSRST = 0x20;
2815 nSRSTnOE = 0x00; /* no output enable for nSRST */
2817 high_output = 0x00;
2818 high_direction = 0x0c;
2820 /* turn red LED3 on, LED2 off */
2821 high_output |= 0x08;
2823 /* initialize high port */
2824 buf[0] = 0x82; /* command "set data bits high byte" */
2825 buf[1] = high_output; /* value */
2826 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2827 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2829 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2831 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2832 return ERROR_JTAG_INIT_FAILED;
2835 return ERROR_OK;
2838 static int turtle_init(void)
2840 uint8_t buf[3];
2841 uint32_t bytes_written;
2843 low_output = 0x08;
2844 low_direction = 0x5b;
2846 /* initialize low byte for jtag */
2847 buf[0] = 0x80; /* command "set data bits low byte" */
2848 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2849 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2850 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2852 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2854 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2855 return ERROR_JTAG_INIT_FAILED;
2858 nSRST = 0x40;
2860 high_output = 0x00;
2861 high_direction = 0x0C;
2863 /* initialize high port */
2864 buf[0] = 0x82; /* command "set data bits high byte" */
2865 buf[1] = high_output;
2866 buf[2] = high_direction;
2867 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2869 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2871 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2872 return ERROR_JTAG_INIT_FAILED;
2875 return ERROR_OK;
2878 static int comstick_init(void)
2880 uint8_t buf[3];
2881 uint32_t bytes_written;
2883 low_output = 0x08;
2884 low_direction = 0x0b;
2886 /* initialize low byte for jtag */
2887 buf[0] = 0x80; /* command "set data bits low byte" */
2888 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2889 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2890 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2892 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2894 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2895 return ERROR_JTAG_INIT_FAILED;
2898 nTRST = 0x01;
2899 nTRSTnOE = 0x00; /* no output enable for nTRST */
2900 nSRST = 0x02;
2901 nSRSTnOE = 0x00; /* no output enable for nSRST */
2903 high_output = 0x03;
2904 high_direction = 0x03;
2906 /* initialize high port */
2907 buf[0] = 0x82; /* command "set data bits high byte" */
2908 buf[1] = high_output;
2909 buf[2] = high_direction;
2910 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2912 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2914 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2915 return ERROR_JTAG_INIT_FAILED;
2918 return ERROR_OK;
2921 static int stm32stick_init(void)
2923 uint8_t buf[3];
2924 uint32_t bytes_written;
2926 low_output = 0x88;
2927 low_direction = 0x8b;
2929 /* initialize low byte for jtag */
2930 buf[0] = 0x80; /* command "set data bits low byte" */
2931 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2932 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2933 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2935 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2937 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2938 return ERROR_JTAG_INIT_FAILED;
2941 nTRST = 0x01;
2942 nTRSTnOE = 0x00; /* no output enable for nTRST */
2943 nSRST = 0x80;
2944 nSRSTnOE = 0x00; /* no output enable for nSRST */
2946 high_output = 0x01;
2947 high_direction = 0x03;
2949 /* initialize high port */
2950 buf[0] = 0x82; /* command "set data bits high byte" */
2951 buf[1] = high_output;
2952 buf[2] = high_direction;
2953 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2955 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2957 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2958 return ERROR_JTAG_INIT_FAILED;
2961 return ERROR_OK;
2964 static int sheevaplug_init(void)
2966 uint8_t buf[3];
2967 uint32_t bytes_written;
2969 low_output = 0x08;
2970 low_direction = 0x1b;
2972 /* initialize low byte for jtag */
2973 buf[0] = 0x80; /* command "set data bits low byte" */
2974 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2975 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2976 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2978 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2980 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2981 return ERROR_JTAG_INIT_FAILED;
2984 nTRSTnOE = 0x1;
2985 nTRST = 0x02;
2986 nSRSTnOE = 0x4;
2987 nSRST = 0x08;
2989 high_output = 0x0;
2990 high_direction = 0x0f;
2992 /* nTRST is always push-pull */
2993 high_output &= ~nTRSTnOE;
2994 high_output |= nTRST;
2996 /* nSRST is always open-drain */
2997 high_output |= nSRSTnOE;
2998 high_output &= ~nSRST;
3000 /* initialize high port */
3001 buf[0] = 0x82; /* command "set data bits high byte" */
3002 buf[1] = high_output; /* value */
3003 buf[2] = high_direction; /* all outputs - xRST */
3004 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3006 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3008 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3009 return ERROR_JTAG_INIT_FAILED;
3012 return ERROR_OK;
3015 static int cortino_jtag_init(void)
3017 uint8_t buf[3];
3018 uint32_t bytes_written;
3020 low_output = 0x08;
3021 low_direction = 0x1b;
3023 /* initialize low byte for jtag */
3024 buf[0] = 0x80; /* command "set data bits low byte" */
3025 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3026 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3027 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3029 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3031 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3032 return ERROR_JTAG_INIT_FAILED;
3035 nTRST = 0x01;
3036 nTRSTnOE = 0x00; /* no output enable for nTRST */
3037 nSRST = 0x02;
3038 nSRSTnOE = 0x00; /* no output enable for nSRST */
3040 high_output = 0x03;
3041 high_direction = 0x03;
3043 /* initialize high port */
3044 buf[0] = 0x82; /* command "set data bits high byte" */
3045 buf[1] = high_output;
3046 buf[2] = high_direction;
3047 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3049 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3051 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3052 return ERROR_JTAG_INIT_FAILED;
3055 return ERROR_OK;
3058 static void olimex_jtag_blink(void)
3060 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3061 * ACBUS3 is bit 3 of the GPIOH port
3063 if (high_output & 0x08)
3065 /* set port pin high */
3066 high_output &= 0x07;
3068 else
3070 /* set port pin low */
3071 high_output |= 0x08;
3074 buffer_write(0x82);
3075 buffer_write(high_output);
3076 buffer_write(high_direction);
3079 static void flyswatter_jtag_blink(void)
3082 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3084 high_output ^= 0x0c;
3086 buffer_write(0x82);
3087 buffer_write(high_output);
3088 buffer_write(high_direction);
3091 static void turtle_jtag_blink(void)
3094 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3096 if (high_output & 0x08)
3098 high_output = 0x04;
3100 else
3102 high_output = 0x08;
3105 buffer_write(0x82);
3106 buffer_write(high_output);
3107 buffer_write(high_direction);
3110 static int ft2232_quit(void)
3112 #if BUILD_FT2232_FTD2XX == 1
3113 FT_STATUS status;
3115 status = FT_Close(ftdih);
3116 #elif BUILD_FT2232_LIBFTDI == 1
3117 ftdi_usb_close(&ftdic);
3119 ftdi_deinit(&ftdic);
3120 #endif
3122 free(ft2232_buffer);
3123 ft2232_buffer = NULL;
3125 return ERROR_OK;
3128 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3130 char *cp;
3131 char buf[200];
3132 if (CMD_ARGC == 1)
3134 ft2232_device_desc = strdup(CMD_ARGV[0]);
3135 cp = strchr(ft2232_device_desc, 0);
3136 /* under Win32, the FTD2XX driver appends an "A" to the end
3137 * of the description, this examines the given desc
3138 * and creates the 'missing' _A or non_A variable. */
3139 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3140 /* it was, so make this the "A" version. */
3141 ft2232_device_desc_A = ft2232_device_desc;
3142 /* and *CREATE* the non-A version. */
3143 strcpy(buf, ft2232_device_desc);
3144 cp = strchr(buf, 0);
3145 cp[-2] = 0;
3146 ft2232_device_desc = strdup(buf);
3147 } else {
3148 /* <space > A not defined
3149 * so create it */
3150 sprintf(buf, "%s A", ft2232_device_desc);
3151 ft2232_device_desc_A = strdup(buf);
3154 else
3156 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3159 return ERROR_OK;
3162 COMMAND_HANDLER(ft2232_handle_serial_command)
3164 if (CMD_ARGC == 1)
3166 ft2232_serial = strdup(CMD_ARGV[0]);
3168 else
3170 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3173 return ERROR_OK;
3176 COMMAND_HANDLER(ft2232_handle_layout_command)
3178 if (CMD_ARGC != 1) {
3179 LOG_ERROR("Need exactly one argument to ft2232_layout");
3180 return ERROR_FAIL;
3183 if (layout) {
3184 LOG_ERROR("already specified ft2232_layout %s",
3185 layout->name);
3186 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3187 ? ERROR_FAIL
3188 : ERROR_OK;
3191 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3192 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3193 layout = l;
3194 return ERROR_OK;
3198 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3199 return ERROR_FAIL;
3202 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3204 if (CMD_ARGC > MAX_USB_IDS * 2)
3206 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3207 "(maximum is %d pairs)", MAX_USB_IDS);
3208 CMD_ARGC = MAX_USB_IDS * 2;
3210 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3212 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3213 if (CMD_ARGC < 2)
3214 return ERROR_COMMAND_SYNTAX_ERROR;
3215 /* remove the incomplete trailing id */
3216 CMD_ARGC -= 1;
3219 unsigned i;
3220 for (i = 0; i < CMD_ARGC; i += 2)
3222 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3223 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3227 * Explicitly terminate, in case there are multiples instances of
3228 * ft2232_vid_pid.
3230 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3232 return ERROR_OK;
3235 COMMAND_HANDLER(ft2232_handle_latency_command)
3237 if (CMD_ARGC == 1)
3239 ft2232_latency = atoi(CMD_ARGV[0]);
3241 else
3243 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3246 return ERROR_OK;
3249 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3251 int retval = 0;
3253 /* 7 bits of either ones or zeros. */
3254 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3256 while (num_cycles > 0)
3258 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3259 * at most 7 bits per invocation. Here we invoke it potentially
3260 * several times.
3262 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3264 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3266 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3267 retval = ERROR_JTAG_QUEUE_FAILED;
3269 first_unsent = cmd;
3272 /* there are no state transitions in this code, so omit state tracking */
3274 /* command "Clock Data to TMS/CS Pin (no Read)" */
3275 buffer_write(0x4b);
3277 /* scan 7 bit */
3278 buffer_write(bitcount_per_command - 1);
3280 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3281 buffer_write(tms);
3283 require_send = 1;
3285 num_cycles -= bitcount_per_command;
3288 return retval;
3291 /* ---------------------------------------------------------------------
3292 * Support for IceBear JTAG adapter from Section5:
3293 * http://section5.ch/icebear
3295 * Author: Sten, debian@sansys-electronic.com
3298 /* Icebear pin layout
3300 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3301 * GND GND | 4 3| n.c.
3302 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3303 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3304 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3305 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3306 * ADBUS2 TDO |14 13| GND GND
3308 * ADBUS0 O L TCK ACBUS0 GND
3309 * ADBUS1 O L TDI ACBUS1 GND
3310 * ADBUS2 I TDO ACBUS2 n.c.
3311 * ADBUS3 O H TMS ACBUS3 n.c.
3312 * ADBUS4 O H nTRST
3313 * ADBUS5 O H nSRST
3314 * ADBUS6 - VCC
3315 * ADBUS7 - GND
3317 static int icebear_jtag_init(void) {
3318 uint8_t buf[3];
3319 uint32_t bytes_written;
3321 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3322 low_output = 0x08; /* high: TMS; low: TCK TDI */
3323 nTRST = 0x10;
3324 nSRST = 0x20;
3326 enum reset_types jtag_reset_config = jtag_get_reset_config();
3327 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3328 low_direction &= ~nTRST; /* nTRST high impedance */
3330 else {
3331 low_direction |= nTRST;
3332 low_output |= nTRST;
3335 low_direction |= nSRST;
3336 low_output |= nSRST;
3338 /* initialize low byte for jtag */
3339 buf[0] = 0x80; /* command "set data bits low byte" */
3340 buf[1] = low_output;
3341 buf[2] = low_direction;
3342 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3344 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3345 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3346 return ERROR_JTAG_INIT_FAILED;
3349 high_output = 0x0;
3350 high_direction = 0x00;
3353 /* initialize high port */
3354 buf[0] = 0x82; /* command "set data bits high byte" */
3355 buf[1] = high_output; /* value */
3356 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3357 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3359 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3360 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3361 return ERROR_JTAG_INIT_FAILED;
3364 return ERROR_OK;
3367 static void icebear_jtag_reset(int trst, int srst) {
3369 if (trst == 1) {
3370 low_direction |= nTRST;
3371 low_output &= ~nTRST;
3373 else if (trst == 0) {
3374 enum reset_types jtag_reset_config = jtag_get_reset_config();
3375 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3376 low_direction &= ~nTRST;
3377 else
3378 low_output |= nTRST;
3381 if (srst == 1) {
3382 low_output &= ~nSRST;
3384 else if (srst == 0) {
3385 low_output |= nSRST;
3388 /* command "set data bits low byte" */
3389 buffer_write(0x80);
3390 buffer_write(low_output);
3391 buffer_write(low_direction);
3393 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3396 /* ---------------------------------------------------------------------
3397 * Support for Signalyzer H2 and Signalyzer H4
3398 * JTAG adapter from Xverve Technologies Inc.
3399 * http://www.signalyzer.com or http://www.xverve.com
3401 * Author: Oleg Seiljus, oleg@signalyzer.com
3403 static unsigned char signalyzer_h_side;
3404 static unsigned int signalyzer_h_adapter_type;
3406 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3408 #if BUILD_FT2232_FTD2XX == 1
3409 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3410 #endif
3412 #define SIGNALYZER_COMMAND_ADDR 128
3413 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3415 #define SIGNALYZER_COMMAND_VERSION 0x41
3416 #define SIGNALYZER_COMMAND_RESET 0x42
3417 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3418 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3419 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3420 #define SIGNALYZER_COMMAND_LED_SET 0x53
3421 #define SIGNALYZER_COMMAND_ADC 0x54
3422 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3423 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3424 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3425 #define SIGNALYZER_COMMAND_I2C 0x58
3427 #define SIGNALYZER_CHAN_A 1
3428 #define SIGNALYZER_CHAN_B 2
3429 /* LEDS use channel C */
3430 #define SIGNALYZER_CHAN_C 4
3432 #define SIGNALYZER_LED_GREEN 1
3433 #define SIGNALYZER_LED_RED 2
3435 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3436 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3437 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3438 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3439 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3442 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3444 #if BUILD_FT2232_FTD2XX == 1
3445 return FT_WriteEE(ftdih, address, value);
3446 #elif BUILD_FT2232_LIBFTDI == 1
3447 return 0;
3448 #endif
3451 #if BUILD_FT2232_FTD2XX == 1
3452 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3454 return FT_ReadEE(ftdih, address, value);
3456 #endif
3458 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3459 int on_time_ms, int off_time_ms, unsigned char cycles)
3461 unsigned char on_time;
3462 unsigned char off_time;
3464 if (on_time_ms < 0xFFFF)
3465 on_time = (unsigned char)(on_time_ms / 62);
3466 else
3467 on_time = 0xFF;
3469 off_time = (unsigned char)(off_time_ms / 62);
3471 #if BUILD_FT2232_FTD2XX == 1
3472 FT_STATUS status;
3474 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3475 ((uint32_t)(channel << 8) | led))) != FT_OK)
3477 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3478 return ERROR_JTAG_DEVICE_ERROR;
3481 if ((status = signalyzer_h_ctrl_write(
3482 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3483 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3485 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3486 return ERROR_JTAG_DEVICE_ERROR;
3489 if ((status = signalyzer_h_ctrl_write(
3490 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3491 ((uint32_t)cycles))) != FT_OK)
3493 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3494 return ERROR_JTAG_DEVICE_ERROR;
3497 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3498 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3500 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3501 return ERROR_JTAG_DEVICE_ERROR;
3504 return ERROR_OK;
3505 #elif BUILD_FT2232_LIBFTDI == 1
3506 int retval;
3508 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3509 ((uint32_t)(channel << 8) | led))) < 0)
3511 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3512 ftdi_get_error_string(&ftdic));
3513 return ERROR_JTAG_DEVICE_ERROR;
3516 if ((retval = signalyzer_h_ctrl_write(
3517 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3518 ((uint32_t)(on_time << 8) | off_time))) < 0)
3520 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3521 ftdi_get_error_string(&ftdic));
3522 return ERROR_JTAG_DEVICE_ERROR;
3525 if ((retval = signalyzer_h_ctrl_write(
3526 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3527 (uint32_t)cycles)) < 0)
3529 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3530 ftdi_get_error_string(&ftdic));
3531 return ERROR_JTAG_DEVICE_ERROR;
3534 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3535 SIGNALYZER_COMMAND_LED_SET)) < 0)
3537 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3538 ftdi_get_error_string(&ftdic));
3539 return ERROR_JTAG_DEVICE_ERROR;
3542 return ERROR_OK;
3543 #endif
3546 static int signalyzer_h_init(void)
3548 #if BUILD_FT2232_FTD2XX == 1
3549 FT_STATUS status;
3550 int i;
3551 #endif
3553 char *end_of_desc;
3555 uint16_t read_buf[12] = { 0 };
3556 uint8_t buf[3];
3557 uint32_t bytes_written;
3559 /* turn on center green led */
3560 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3561 0xFFFF, 0x00, 0x00);
3563 /* determine what channel config wants to open
3564 * TODO: change me... current implementation is made to work
3565 * with openocd description parsing.
3567 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3569 if (end_of_desc)
3571 signalyzer_h_side = *(end_of_desc - 1);
3572 if (signalyzer_h_side == 'B')
3573 signalyzer_h_side = SIGNALYZER_CHAN_B;
3574 else
3575 signalyzer_h_side = SIGNALYZER_CHAN_A;
3577 else
3579 LOG_ERROR("No Channel was specified");
3580 return ERROR_FAIL;
3583 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3584 1000, 1000, 0xFF);
3586 #if BUILD_FT2232_FTD2XX == 1
3587 /* read signalyzer versionining information */
3588 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3589 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3591 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3592 return ERROR_JTAG_DEVICE_ERROR;
3595 for (i = 0; i < 10; i++)
3597 if ((status = signalyzer_h_ctrl_read(
3598 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3599 &read_buf[i])) != FT_OK)
3601 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3602 status);
3603 return ERROR_JTAG_DEVICE_ERROR;
3607 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3608 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3609 read_buf[4], read_buf[5], read_buf[6]);
3611 /* set gpio register */
3612 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3613 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3615 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3616 return ERROR_JTAG_DEVICE_ERROR;
3619 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3620 0x0404)) != FT_OK)
3622 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3623 return ERROR_JTAG_DEVICE_ERROR;
3626 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3627 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3629 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3630 return ERROR_JTAG_DEVICE_ERROR;
3633 /* read adapter type information */
3634 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3635 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3637 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3638 return ERROR_JTAG_DEVICE_ERROR;
3641 if ((status = signalyzer_h_ctrl_write(
3642 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3644 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3645 return ERROR_JTAG_DEVICE_ERROR;
3648 if ((status = signalyzer_h_ctrl_write(
3649 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3651 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3652 return ERROR_JTAG_DEVICE_ERROR;
3655 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3656 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3658 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3659 return ERROR_JTAG_DEVICE_ERROR;
3662 usleep(100000);
3664 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3665 &read_buf[0])) != FT_OK)
3667 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3668 return ERROR_JTAG_DEVICE_ERROR;
3671 if (read_buf[0] != 0x0498)
3672 signalyzer_h_adapter_type = 0x0000;
3673 else
3675 for (i = 0; i < 4; i++)
3677 if ((status = signalyzer_h_ctrl_read(
3678 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3679 &read_buf[i])) != FT_OK)
3681 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3682 status);
3683 return ERROR_JTAG_DEVICE_ERROR;
3687 signalyzer_h_adapter_type = read_buf[0];
3690 #elif BUILD_FT2232_LIBFTDI == 1
3691 /* currently libftdi does not allow reading individual eeprom
3692 * locations, therefore adapter type cannot be detected.
3693 * override with most common type
3695 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3696 #endif
3698 enum reset_types jtag_reset_config = jtag_get_reset_config();
3700 /* ADAPTOR: EM_LT16_A */
3701 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3703 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3704 "detected. (HW: %2x).", (read_buf[1] >> 8));
3706 nTRST = 0x10;
3707 nTRSTnOE = 0x10;
3708 nSRST = 0x20;
3709 nSRSTnOE = 0x20;
3711 low_output = 0x08;
3712 low_direction = 0x1b;
3714 high_output = 0x0;
3715 high_direction = 0x0;
3717 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3719 low_direction &= ~nTRSTnOE; /* nTRST input */
3720 low_output &= ~nTRST; /* nTRST = 0 */
3722 else
3724 low_direction |= nTRSTnOE; /* nTRST output */
3725 low_output |= nTRST; /* nTRST = 1 */
3728 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3730 low_direction |= nSRSTnOE; /* nSRST output */
3731 low_output |= nSRST; /* nSRST = 1 */
3733 else
3735 low_direction &= ~nSRSTnOE; /* nSRST input */
3736 low_output &= ~nSRST; /* nSRST = 0 */
3739 #if BUILD_FT2232_FTD2XX == 1
3740 /* enable power to the module */
3741 if ((status = signalyzer_h_ctrl_write(
3742 SIGNALYZER_DATA_BUFFER_ADDR,
3743 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3744 != FT_OK)
3746 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3747 status);
3748 return ERROR_JTAG_DEVICE_ERROR;
3751 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3752 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3754 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3755 status);
3756 return ERROR_JTAG_DEVICE_ERROR;
3759 /* set gpio mode register */
3760 if ((status = signalyzer_h_ctrl_write(
3761 SIGNALYZER_DATA_BUFFER_ADDR,
3762 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3764 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3765 status);
3766 return ERROR_JTAG_DEVICE_ERROR;
3769 if ((status = signalyzer_h_ctrl_write(
3770 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3771 != FT_OK)
3773 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3774 status);
3775 return ERROR_JTAG_DEVICE_ERROR;
3778 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3779 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3781 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3782 status);
3783 return ERROR_JTAG_DEVICE_ERROR;
3786 /* set gpio register */
3787 if ((status = signalyzer_h_ctrl_write(
3788 SIGNALYZER_DATA_BUFFER_ADDR,
3789 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3791 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3792 status);
3793 return ERROR_JTAG_DEVICE_ERROR;
3796 if ((status = signalyzer_h_ctrl_write(
3797 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3798 != FT_OK)
3800 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3801 status);
3802 return ERROR_JTAG_DEVICE_ERROR;
3805 if ((status = signalyzer_h_ctrl_write(
3806 SIGNALYZER_COMMAND_ADDR,
3807 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3809 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3810 status);
3811 return ERROR_JTAG_DEVICE_ERROR;
3813 #endif
3816 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3817 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3818 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3819 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3820 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3822 if (signalyzer_h_adapter_type
3823 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3824 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3825 "detected. (HW: %2x).", (read_buf[1] >> 8));
3826 else if (signalyzer_h_adapter_type
3827 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3828 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3829 "(ARM JTAG with PSU) detected. (HW: %2x).",
3830 (read_buf[1] >> 8));
3831 else if (signalyzer_h_adapter_type
3832 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3833 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3834 "detected. (HW: %2x).", (read_buf[1] >> 8));
3835 else if (signalyzer_h_adapter_type
3836 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3837 LOG_INFO("Signalyzer: EM-JTAG-P "
3838 "(Generic JTAG with PSU) detected. (HW: %2x).",
3839 (read_buf[1] >> 8));
3841 nTRST = 0x02;
3842 nTRSTnOE = 0x04;
3843 nSRST = 0x08;
3844 nSRSTnOE = 0x10;
3846 low_output = 0x08;
3847 low_direction = 0x1b;
3849 high_output = 0x0;
3850 high_direction = 0x1f;
3852 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3854 high_output |= nTRSTnOE;
3855 high_output &= ~nTRST;
3857 else
3859 high_output &= ~nTRSTnOE;
3860 high_output |= nTRST;
3863 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3865 high_output &= ~nSRSTnOE;
3866 high_output |= nSRST;
3868 else
3870 high_output |= nSRSTnOE;
3871 high_output &= ~nSRST;
3874 #if BUILD_FT2232_FTD2XX == 1
3875 /* enable power to the module */
3876 if ((status = signalyzer_h_ctrl_write(
3877 SIGNALYZER_DATA_BUFFER_ADDR,
3878 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3879 != FT_OK)
3881 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3882 status);
3883 return ERROR_JTAG_DEVICE_ERROR;
3886 if ((status = signalyzer_h_ctrl_write(
3887 SIGNALYZER_COMMAND_ADDR,
3888 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3890 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3891 status);
3892 return ERROR_JTAG_DEVICE_ERROR;
3895 /* set gpio mode register (IO_16 and IO_17 set as analog
3896 * inputs, other is gpio)
3898 if ((status = signalyzer_h_ctrl_write(
3899 SIGNALYZER_DATA_BUFFER_ADDR,
3900 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3902 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3903 status);
3904 return ERROR_JTAG_DEVICE_ERROR;
3907 if ((status = signalyzer_h_ctrl_write(
3908 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3909 != FT_OK)
3911 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3912 status);
3913 return ERROR_JTAG_DEVICE_ERROR;
3916 if ((status = signalyzer_h_ctrl_write(
3917 SIGNALYZER_COMMAND_ADDR,
3918 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3920 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3921 status);
3922 return ERROR_JTAG_DEVICE_ERROR;
3925 /* set gpio register (all inputs, for -P modules,
3926 * PSU will be turned off)
3928 if ((status = signalyzer_h_ctrl_write(
3929 SIGNALYZER_DATA_BUFFER_ADDR,
3930 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3932 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3933 status);
3934 return ERROR_JTAG_DEVICE_ERROR;
3937 if ((status = signalyzer_h_ctrl_write(
3938 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3939 != FT_OK)
3941 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3942 status);
3943 return ERROR_JTAG_DEVICE_ERROR;
3946 if ((status = signalyzer_h_ctrl_write(
3947 SIGNALYZER_COMMAND_ADDR,
3948 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3950 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3951 status);
3952 return ERROR_JTAG_DEVICE_ERROR;
3954 #endif
3957 else if (signalyzer_h_adapter_type == 0x0000)
3959 LOG_INFO("Signalyzer: No external modules were detected.");
3961 nTRST = 0x10;
3962 nTRSTnOE = 0x10;
3963 nSRST = 0x20;
3964 nSRSTnOE = 0x20;
3966 low_output = 0x08;
3967 low_direction = 0x1b;
3969 high_output = 0x0;
3970 high_direction = 0x0;
3972 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3974 low_direction &= ~nTRSTnOE; /* nTRST input */
3975 low_output &= ~nTRST; /* nTRST = 0 */
3977 else
3979 low_direction |= nTRSTnOE; /* nTRST output */
3980 low_output |= nTRST; /* nTRST = 1 */
3983 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3985 low_direction |= nSRSTnOE; /* nSRST output */
3986 low_output |= nSRST; /* nSRST = 1 */
3988 else
3990 low_direction &= ~nSRSTnOE; /* nSRST input */
3991 low_output &= ~nSRST; /* nSRST = 0 */
3994 else
3996 LOG_ERROR("Unknown module type is detected: %.4x",
3997 signalyzer_h_adapter_type);
3998 return ERROR_JTAG_DEVICE_ERROR;
4001 /* initialize low byte of controller for jtag operation */
4002 buf[0] = 0x80;
4003 buf[1] = low_output;
4004 buf[2] = low_direction;
4006 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
4007 || (bytes_written != 3))
4009 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4010 return ERROR_JTAG_INIT_FAILED;
4013 #if BUILD_FT2232_FTD2XX == 1
4014 if (ftdi_device == FT_DEVICE_2232H)
4016 /* initialize high byte of controller for jtag operation */
4017 buf[0] = 0x82;
4018 buf[1] = high_output;
4019 buf[2] = high_direction;
4021 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
4022 || (bytes_written != 3))
4024 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4025 return ERROR_JTAG_INIT_FAILED;
4028 #elif BUILD_FT2232_LIBFTDI == 1
4029 if (ftdi_device == TYPE_2232H)
4031 /* initialize high byte of controller for jtag operation */
4032 buf[0] = 0x82;
4033 buf[1] = high_output;
4034 buf[2] = high_direction;
4036 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
4037 || (bytes_written != 3))
4039 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4040 return ERROR_JTAG_INIT_FAILED;
4043 #endif
4044 return ERROR_OK;
4047 static void signalyzer_h_reset(int trst, int srst)
4049 enum reset_types jtag_reset_config = jtag_get_reset_config();
4051 /* ADAPTOR: EM_LT16_A */
4052 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4054 if (trst == 1)
4056 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4057 /* switch to output pin (output is low) */
4058 low_direction |= nTRSTnOE;
4059 else
4060 /* switch output low */
4061 low_output &= ~nTRST;
4063 else if (trst == 0)
4065 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4066 /* switch to input pin (high-Z + internal
4067 * and external pullup) */
4068 low_direction &= ~nTRSTnOE;
4069 else
4070 /* switch output high */
4071 low_output |= nTRST;
4074 if (srst == 1)
4076 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4077 /* switch output low */
4078 low_output &= ~nSRST;
4079 else
4080 /* switch to output pin (output is low) */
4081 low_direction |= nSRSTnOE;
4083 else if (srst == 0)
4085 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4086 /* switch output high */
4087 low_output |= nSRST;
4088 else
4089 /* switch to input pin (high-Z) */
4090 low_direction &= ~nSRSTnOE;
4093 /* command "set data bits low byte" */
4094 buffer_write(0x80);
4095 buffer_write(low_output);
4096 buffer_write(low_direction);
4097 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4098 "low_direction: 0x%2.2x",
4099 trst, srst, low_output, low_direction);
4101 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4102 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4103 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4104 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4105 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4107 if (trst == 1)
4109 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4110 high_output &= ~nTRSTnOE;
4111 else
4112 high_output &= ~nTRST;
4114 else if (trst == 0)
4116 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4117 high_output |= nTRSTnOE;
4118 else
4119 high_output |= nTRST;
4122 if (srst == 1)
4124 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4125 high_output &= ~nSRST;
4126 else
4127 high_output &= ~nSRSTnOE;
4129 else if (srst == 0)
4131 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4132 high_output |= nSRST;
4133 else
4134 high_output |= nSRSTnOE;
4137 /* command "set data bits high byte" */
4138 buffer_write(0x82);
4139 buffer_write(high_output);
4140 buffer_write(high_direction);
4141 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4142 "high_direction: 0x%2.2x",
4143 trst, srst, high_output, high_direction);
4145 else if (signalyzer_h_adapter_type == 0x0000)
4147 if (trst == 1)
4149 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4150 /* switch to output pin (output is low) */
4151 low_direction |= nTRSTnOE;
4152 else
4153 /* switch output low */
4154 low_output &= ~nTRST;
4156 else if (trst == 0)
4158 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4159 /* switch to input pin (high-Z + internal
4160 * and external pullup) */
4161 low_direction &= ~nTRSTnOE;
4162 else
4163 /* switch output high */
4164 low_output |= nTRST;
4167 if (srst == 1)
4169 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4170 /* switch output low */
4171 low_output &= ~nSRST;
4172 else
4173 /* switch to output pin (output is low) */
4174 low_direction |= nSRSTnOE;
4176 else if (srst == 0)
4178 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4179 /* switch output high */
4180 low_output |= nSRST;
4181 else
4182 /* switch to input pin (high-Z) */
4183 low_direction &= ~nSRSTnOE;
4186 /* command "set data bits low byte" */
4187 buffer_write(0x80);
4188 buffer_write(low_output);
4189 buffer_write(low_direction);
4190 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4191 "low_direction: 0x%2.2x",
4192 trst, srst, low_output, low_direction);
4196 static void signalyzer_h_blink(void)
4198 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4201 /********************************************************************
4202 * Support for KT-LINK
4203 * JTAG adapter from KRISTECH
4204 * http://www.kristech.eu
4205 *******************************************************************/
4206 static int ktlink_init(void)
4208 uint8_t buf[3];
4209 uint32_t bytes_written;
4210 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4212 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4213 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4215 // initialize low port
4216 buf[0] = 0x80; // command "set data bits low byte"
4217 buf[1] = low_output;
4218 buf[2] = low_direction;
4219 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4221 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
4223 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4224 return ERROR_JTAG_INIT_FAILED;
4227 nTRST = 0x01;
4228 nSRST = 0x02;
4229 nTRSTnOE = 0x04;
4230 nSRSTnOE = 0x08;
4232 high_output = 0x80; // turn LED on
4233 high_direction = 0xFF; // all outputs
4235 enum reset_types jtag_reset_config = jtag_get_reset_config();
4237 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4238 high_output |= nTRSTnOE;
4239 high_output &= ~nTRST;
4240 } else {
4241 high_output &= ~nTRSTnOE;
4242 high_output |= nTRST;
4245 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4246 high_output &= ~nSRSTnOE;
4247 high_output |= nSRST;
4248 } else {
4249 high_output |= nSRSTnOE;
4250 high_output &= ~nSRST;
4253 // initialize high port
4254 buf[0] = 0x82; // command "set data bits high byte"
4255 buf[1] = high_output; // value
4256 buf[2] = high_direction;
4257 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4259 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
4261 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4262 return ERROR_JTAG_INIT_FAILED;
4265 return ERROR_OK;
4268 static void ktlink_reset(int trst, int srst)
4270 enum reset_types jtag_reset_config = jtag_get_reset_config();
4272 if (trst == 1) {
4273 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4274 high_output &= ~nTRSTnOE;
4275 else
4276 high_output &= ~nTRST;
4277 } else if (trst == 0) {
4278 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4279 high_output |= nTRSTnOE;
4280 else
4281 high_output |= nTRST;
4284 if (srst == 1) {
4285 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4286 high_output &= ~nSRST;
4287 else
4288 high_output &= ~nSRSTnOE;
4289 } else if (srst == 0) {
4290 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4291 high_output |= nSRST;
4292 else
4293 high_output |= nSRSTnOE;
4296 buffer_write(0x82); // command "set data bits high byte"
4297 buffer_write(high_output);
4298 buffer_write(high_direction);
4299 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4302 static void ktlink_blink(void)
4304 /* LED connected to ACBUS7 */
4305 if (high_output & 0x80)
4306 high_output &= 0x7F;
4307 else
4308 high_output |= 0x80;
4310 buffer_write(0x82); // command "set data bits high byte"
4311 buffer_write(high_output);
4312 buffer_write(high_direction);
4315 static const struct command_registration ft2232_command_handlers[] = {
4317 .name = "ft2232_device_desc",
4318 .handler = &ft2232_handle_device_desc_command,
4319 .mode = COMMAND_CONFIG,
4320 .help = "set the USB device description of the FTDI FT2232 device",
4321 .usage = "description_string",
4324 .name = "ft2232_serial",
4325 .handler = &ft2232_handle_serial_command,
4326 .mode = COMMAND_CONFIG,
4327 .help = "set the serial number of the FTDI FT2232 device",
4328 .usage = "serial_string",
4331 .name = "ft2232_layout",
4332 .handler = &ft2232_handle_layout_command,
4333 .mode = COMMAND_CONFIG,
4334 .help = "set the layout of the FT2232 GPIO signals used "
4335 "to control output-enables and reset signals",
4336 .usage = "layout_name",
4339 .name = "ft2232_vid_pid",
4340 .handler = &ft2232_handle_vid_pid_command,
4341 .mode = COMMAND_CONFIG,
4342 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4343 .usage = "(vid pid)* ",
4346 .name = "ft2232_latency",
4347 .handler = &ft2232_handle_latency_command,
4348 .mode = COMMAND_CONFIG,
4349 .help = "set the FT2232 latency timer to a new value",
4350 .usage = "value",
4352 COMMAND_REGISTRATION_DONE
4355 struct jtag_interface ft2232_interface = {
4356 .name = "ft2232",
4357 .supported = DEBUG_CAP_TMS_SEQ,
4358 .commands = ft2232_command_handlers,
4360 .init = ft2232_init,
4361 .quit = ft2232_quit,
4362 .speed = ft2232_speed,
4363 .speed_div = ft2232_speed_div,
4364 .khz = ft2232_khz,
4365 .execute_queue = ft2232_execute_queue,