Extended ft2232_layout structure with bitbang_deny field that will define port pins...
[openocd/libswd.git] / src / jtag / drivers / ft2232.c
blob35df00a9baf8231b6046204f1ced6c9b65511958
1 /***************************************************************************
2 * Copyright (C) 2009 by Oyvind Harboe *
3 * Oyvind 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 * Copyright (C) 2011-2012 Tomasz Boleslaw CEDRO *
15 * cederom@tlen.pl, http://www.tomek.cedro.info *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
33 /**
34 * @file
35 * JTAG adapters based on the FT2232 full and high speed USB parts are
36 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
37 * are discrete, but development boards may integrate them as alternatives
38 * to more capable (and expensive) third party JTAG pods.
40 * JTAG uses only one of the two communications channels ("MPSSE engines")
41 * on these devices. Adapters based on FT4232 parts have four ports/channels
42 * (A/B/C/D), instead of just two (A/B).
44 * Especially on development boards integrating one of these chips (as
45 * opposed to discrete pods/dongles), the additional channels can be used
46 * for a variety of purposes, but OpenOCD only uses one channel at a time.
48 * - As a USB-to-serial adapter for the target's console UART ...
49 * which may be able to support ROM boot loaders that load initial
50 * firmware images to flash (or SRAM).
52 * - On systems which support ARM's SWD in addition to JTAG, or instead
53 * of it, that second port can be used for reading SWV/SWO trace data.
55 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
57 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
58 * request/response interactions involve round trips over the USB link.
59 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
60 * can for example poll quickly for a status change (usually taking on the
61 * order of microseconds not milliseconds) before beginning a queued
62 * transaction which require the previous one to have completed.
64 * There are dozens of adapters of this type, differing in details which
65 * this driver needs to understand. Those "layout" details are required
66 * as part of FT2232 driver configuration.
68 * This code uses information contained in the MPSSE specification which was
69 * found here:
70 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
71 * Hereafter this is called the "MPSSE Spec".
73 * The datasheet for the ftdichip.com's FT2232D part is here:
74 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
76 * Also note the issue with code 0x4b (clock data to TMS) noted in
77 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
78 * which can affect longer JTAG state paths.
81 #ifdef HAVE_CONFIG_H
82 #include "config.h"
83 #endif
85 /* project specific includes */
86 #include <jtag/interface.h>
87 #include <transport/transport.h>
88 #include <helper/time_support.h>
90 #if IS_CYGWIN == 1
91 #include <windows.h>
92 #endif
94 #include <assert.h>
96 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
97 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
98 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
99 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
100 #endif
102 /* FT2232 access library includes */
103 #if BUILD_FT2232_FTD2XX == 1
104 #include <ftd2xx.h>
105 #include "ftd2xx_common.h"
107 enum ftdi_interface {
108 INTERFACE_ANY = 0,
109 INTERFACE_A = 1,
110 INTERFACE_B = 2,
111 INTERFACE_C = 3,
112 INTERFACE_D = 4
115 #elif BUILD_FT2232_LIBFTDI == 1
116 #include <ftdi.h>
117 #endif
119 /* max TCK for the high speed devices 30000 kHz */
120 #define FTDI_x232H_MAX_TCK 30000
121 /* max TCK for the full speed devices 6000 kHz */
122 #define FTDI_2232C_MAX_TCK 6000
123 /* this speed value tells that RTCK is requested */
124 #define RTCK_SPEED -1
127 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
128 * errors with a retry count of 100. Increasing it solves the problem for me.
129 * - Dimitar
131 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
132 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
133 * to something sane.
135 #define LIBFTDI_READ_RETRY_COUNT 2000
137 #ifndef BUILD_FT2232_HIGHSPEED
138 #if BUILD_FT2232_FTD2XX == 1
139 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H, FT_DEVICE_232H };
140 #elif BUILD_FT2232_LIBFTDI == 1
141 enum ftdi_chip_type { TYPE_2232H = 4, TYPE_4232H = 5, TYPE_232H = 6 };
142 #endif
143 #endif
146 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
147 * stable state. Calling code must ensure that current state is stable,
148 * that verification is not done in here.
150 * @param num_cycles The number of clocks cycles to send.
151 * @param cmd The command to send.
153 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
155 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
157 static char *ft2232_device_desc_A;
158 static char *ft2232_device_desc;
159 static char *ft2232_serial;
160 static uint8_t ft2232_latency = 2;
161 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
163 #define MAX_USB_IDS 8
164 /* vid = pid = 0 marks the end of the list */
165 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
166 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
168 /** This structure describes different layout of FT2232 based devices. */
169 struct ft2232_layout {
170 /** Layout name. */
171 char *name;
172 /** Layout specific initialization routine. */
173 int (*init)(void);
174 /** Layout specific reset routine. */
175 void (*reset)(int trst, int srst);
176 /** Layout specific LED blink routine. */
177 void (*blink)(void);
178 /** Which FTDI channel does this layout use. */
179 int channel;
180 /** This will forbid bitbanging selected port pins. */
181 int bitbang_deny;
184 /* init procedures for supported layouts */
185 static int usbjtag_init(void);
186 static int jtagkey_init(void);
187 static int lm3s811_jtag_init(void);
188 static int icdi_jtag_init(void);
189 static int olimex_jtag_init(void);
190 static int flyswatter1_init(void);
191 static int flyswatter2_init(void);
192 static int minimodule_init(void);
193 static int turtle_init(void);
194 static int comstick_init(void);
195 static int stm32stick_init(void);
196 static int axm0432_jtag_init(void);
197 static int sheevaplug_init(void);
198 static int icebear_jtag_init(void);
199 static int cortino_jtag_init(void);
200 static int signalyzer_init(void);
201 static int signalyzer_h_init(void);
202 static int ktlink_init(void);
203 static int redbee_init(void);
204 static int lisa_l_init(void);
205 static int flossjtag_init(void);
206 static int xds100v2_init(void);
207 static int digilent_hs1_init(void);
209 /* reset procedures for supported layouts */
210 static void ftx23_reset(int trst, int srst);
211 static void jtagkey_reset(int trst, int srst);
212 static void olimex_jtag_reset(int trst, int srst);
213 static void flyswatter1_reset(int trst, int srst);
214 static void flyswatter2_reset(int trst, int srst);
215 static void minimodule_reset(int trst, int srst);
216 static void turtle_reset(int trst, int srst);
217 static void comstick_reset(int trst, int srst);
218 static void stm32stick_reset(int trst, int srst);
219 static void axm0432_jtag_reset(int trst, int srst);
220 static void sheevaplug_reset(int trst, int srst);
221 static void icebear_jtag_reset(int trst, int srst);
222 static void signalyzer_h_reset(int trst, int srst);
223 static void ktlink_reset(int trst, int srst);
224 static void redbee_reset(int trst, int srst);
225 static void xds100v2_reset(int trst, int srst);
226 static void digilent_hs1_reset(int trst, int srst);
228 /* blink procedures for layouts that support a blinking led */
229 static void olimex_jtag_blink(void);
230 static void flyswatter1_jtag_blink(void);
231 static void flyswatter2_jtag_blink(void);
232 static void turtle_jtag_blink(void);
233 static void signalyzer_h_blink(void);
234 static void ktlink_blink(void);
235 static void lisa_l_blink(void);
236 static void flossjtag_blink(void);
238 /* common transport support options */
240 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
242 static const struct ft2232_layout ft2232_layouts[] = {
243 { .name = "usbjtag",
244 .init = usbjtag_init,
245 .reset = ftx23_reset,
247 { .name = "jtagkey",
248 .init = jtagkey_init,
249 .reset = jtagkey_reset,
251 { .name = "jtagkey_prototype_v1",
252 .init = jtagkey_init,
253 .reset = jtagkey_reset,
255 { .name = "oocdlink",
256 .init = jtagkey_init,
257 .reset = jtagkey_reset,
259 { .name = "signalyzer",
260 .init = signalyzer_init,
261 .reset = ftx23_reset,
263 { .name = "evb_lm3s811",
264 .init = lm3s811_jtag_init,
265 .reset = ftx23_reset,
267 { .name = "luminary_icdi",
268 .init = icdi_jtag_init,
269 .reset = ftx23_reset,
271 { .name = "olimex-jtag",
272 .init = olimex_jtag_init,
273 .reset = olimex_jtag_reset,
274 .blink = olimex_jtag_blink
276 { .name = "flyswatter",
277 .init = flyswatter1_init,
278 .reset = flyswatter1_reset,
279 .blink = flyswatter1_jtag_blink
281 { .name = "flyswatter2",
282 .init = flyswatter2_init,
283 .reset = flyswatter2_reset,
284 .blink = flyswatter2_jtag_blink
286 { .name = "minimodule",
287 .init = minimodule_init,
288 .reset = minimodule_reset,
290 { .name = "turtelizer2",
291 .init = turtle_init,
292 .reset = turtle_reset,
293 .blink = turtle_jtag_blink
295 { .name = "comstick",
296 .init = comstick_init,
297 .reset = comstick_reset,
299 { .name = "stm32stick",
300 .init = stm32stick_init,
301 .reset = stm32stick_reset,
303 { .name = "axm0432_jtag",
304 .init = axm0432_jtag_init,
305 .reset = axm0432_jtag_reset,
307 { .name = "sheevaplug",
308 .init = sheevaplug_init,
309 .reset = sheevaplug_reset,
311 { .name = "icebear",
312 .init = icebear_jtag_init,
313 .reset = icebear_jtag_reset,
315 { .name = "cortino",
316 .init = cortino_jtag_init,
317 .reset = comstick_reset,
319 { .name = "signalyzer-h",
320 .init = signalyzer_h_init,
321 .reset = signalyzer_h_reset,
322 .blink = signalyzer_h_blink
324 { .name = "ktlink",
325 .init = ktlink_init,
326 .reset = ktlink_reset,
327 .blink = ktlink_blink
329 { .name = "redbee-econotag",
330 .init = redbee_init,
331 .reset = redbee_reset,
333 { .name = "redbee-usb",
334 .init = redbee_init,
335 .reset = redbee_reset,
336 .channel = INTERFACE_B,
338 { .name = "lisa-l",
339 .init = lisa_l_init,
340 .reset = ftx23_reset,
341 .blink = lisa_l_blink,
342 .channel = INTERFACE_B,
344 { .name = "flossjtag",
345 .init = flossjtag_init,
346 .reset = ftx23_reset,
347 .blink = flossjtag_blink,
349 { .name = "xds100v2",
350 .init = xds100v2_init,
351 .reset = xds100v2_reset,
353 { .name = "digilent-hs1",
354 .init = digilent_hs1_init,
355 .reset = digilent_hs1_reset,
356 .channel = INTERFACE_A,
358 { .name = NULL, /* END OF TABLE */ },
361 /* bitmask used to drive nTRST; usually a GPIOLx signal */
362 static uint8_t nTRST;
363 static uint8_t nTRSTnOE;
364 /* bitmask used to drive nSRST; usually a GPIOLx signal */
365 static uint8_t nSRST;
366 static uint8_t nSRSTnOE;
368 /** the layout being used with this debug session */
369 static const struct ft2232_layout *layout;
371 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
372 static uint8_t low_output;
374 /* note that direction bit == 1 means that signal is an output */
376 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
377 static uint8_t low_direction;
378 /** default value bitmask for CBUS GPIOH(0..4) */
379 static uint8_t high_output;
380 /** default direction bitmask for CBUS GPIOH(0..4) */
381 static uint8_t high_direction;
383 #if BUILD_FT2232_FTD2XX == 1
384 static FT_HANDLE ftdih;
385 static FT_DEVICE ftdi_device;
386 #elif BUILD_FT2232_LIBFTDI == 1
387 static struct ftdi_context ftdic;
388 static enum ftdi_chip_type ftdi_device;
389 #endif
391 static struct jtag_command *first_unsent; /* next command that has to be sent */
392 static int require_send;
394 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
396 "There is a significant difference between libftdi and libftd2xx. The latter
397 one allows to schedule up to 64*64 bytes of result data while libftdi fails
398 with more than 4*64. As a consequence, the FT2232 driver is forced to
399 perform around 16x more USB transactions for long command streams with TDO
400 capture when running with libftdi."
402 No idea how we get
403 #define FT2232_BUFFER_SIZE 131072
404 a comment would have been nice.
407 #if BUILD_FT2232_FTD2XX == 1
408 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
409 #else
410 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
411 #endif
413 #define FT2232_BUFFER_SIZE 131072
415 static uint8_t *ft2232_buffer;
416 static int ft2232_buffer_size;
417 static int ft2232_read_pointer;
418 static int ft2232_expect_read;
421 * Function buffer_write
422 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
423 * @param val is the byte to send.
425 static inline void buffer_write(uint8_t val)
427 assert(ft2232_buffer);
428 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
429 ft2232_buffer[ft2232_buffer_size++] = val;
433 * Function buffer_read
434 * returns a byte from the byte buffer.
436 static inline uint8_t buffer_read(void)
438 assert(ft2232_buffer);
439 assert(ft2232_read_pointer < ft2232_buffer_size);
440 return ft2232_buffer[ft2232_read_pointer++];
444 * Clocks out \a bit_count bits on the TMS line, starting with the least
445 * significant bit of tms_bits and progressing to more significant bits.
446 * Rigorous state transition logging is done here via tap_set_state().
448 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
449 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
450 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
451 * is often used for this, 0x4b.
453 * @param tms_bits Holds the sequence of bits to send.
454 * @param tms_count Tells how many bits in the sequence.
455 * @param tdi_bit A single bit to pass on to TDI before the first TCK
456 * cycle and held static for the duration of TMS clocking.
458 * See the MPSSE spec referenced above.
460 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
462 uint8_t tms_byte;
463 int i;
464 int tms_ndx; /* bit index into tms_byte */
466 assert(tms_count > 0);
468 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
469 mpsse_cmd, tms_bits, tms_count);
471 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
472 bool bit = tms_bits & 1;
474 if (bit)
475 tms_byte |= (1 << tms_ndx);
477 /* always do state transitions in public view */
478 tap_set_state(tap_state_transition(tap_get_state(), bit));
480 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
481 * also increment.
483 ++tms_ndx;
485 if (tms_ndx == 7 || i == tms_count-1) {
486 buffer_write(mpsse_cmd);
487 buffer_write(tms_ndx - 1);
489 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
490 * TMS/CS and is held static for the duration of TMS/CS clocking.
492 buffer_write(tms_byte | (tdi_bit << 7));
498 * Function get_tms_buffer_requirements
499 * returns what clock_tms() will consume if called with
500 * same \a bit_count.
502 static inline int get_tms_buffer_requirements(int bit_count)
504 return ((bit_count + 6)/7) * 3;
508 * Function move_to_state
509 * moves the TAP controller from the current state to a
510 * \a goal_state through a path given by tap_get_tms_path(). State transition
511 * logging is performed by delegation to clock_tms().
513 * @param goal_state is the destination state for the move.
515 static void move_to_state(tap_state_t goal_state)
517 tap_state_t start_state = tap_get_state();
519 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
520 * lookup of the required TMS pattern to move to this state from the start state.
523 /* do the 2 lookups */
524 int tms_bits = tap_get_tms_path(start_state, goal_state);
525 int tms_count = tap_get_tms_path_len(start_state, goal_state);
527 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
529 clock_tms(0x4b, tms_bits, tms_count, 0);
532 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
534 #if BUILD_FT2232_FTD2XX == 1
535 FT_STATUS status;
536 DWORD dw_bytes_written = 0;
537 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
538 if (status != FT_OK) {
539 *bytes_written = dw_bytes_written;
540 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
541 return ERROR_JTAG_DEVICE_ERROR;
542 } else
543 *bytes_written = dw_bytes_written;
545 #elif BUILD_FT2232_LIBFTDI == 1
546 int retval = ftdi_write_data(&ftdic, buf, size);
547 if (retval < 0) {
548 *bytes_written = 0;
549 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
550 return ERROR_JTAG_DEVICE_ERROR;
551 } else
552 *bytes_written = retval;
554 #endif
556 if (*bytes_written != (uint32_t)size)
557 return ERROR_JTAG_DEVICE_ERROR;
559 return ERROR_OK;
562 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
564 #if BUILD_FT2232_FTD2XX == 1
565 DWORD dw_bytes_read;
566 FT_STATUS status;
567 int timeout = 5;
568 *bytes_read = 0;
570 while ((*bytes_read < size) && timeout--) {
571 status = FT_Read(ftdih, buf + *bytes_read, size -
572 *bytes_read, &dw_bytes_read);
573 if (status != FT_OK) {
574 *bytes_read = 0;
575 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
576 return ERROR_JTAG_DEVICE_ERROR;
578 *bytes_read += dw_bytes_read;
581 #elif BUILD_FT2232_LIBFTDI == 1
582 int retval;
583 int timeout = LIBFTDI_READ_RETRY_COUNT;
584 *bytes_read = 0;
586 while ((*bytes_read < size) && timeout--) {
587 retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
588 if (retval < 0) {
589 *bytes_read = 0;
590 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
591 return ERROR_JTAG_DEVICE_ERROR;
593 *bytes_read += retval;
596 #endif
598 if (*bytes_read < size) {
599 LOG_ERROR("couldn't read enough bytes from "
600 "FT2232 device (%i < %i)",
601 (unsigned)*bytes_read,
602 (unsigned)size);
603 return ERROR_JTAG_DEVICE_ERROR;
606 return ERROR_OK;
609 static bool ft2232_device_is_highspeed(void)
611 #if BUILD_FT2232_FTD2XX == 1
612 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H)
613 #ifdef HAS_ENUM_FT232H
614 || (ftdi_device == FT_DEVICE_232H)
615 #endif
617 #elif BUILD_FT2232_LIBFTDI == 1
618 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H
619 #ifdef HAS_ENUM_FT232H
620 || ftdi_device == TYPE_232H
621 #endif
623 #endif
627 * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
628 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
629 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
632 static int ftx232h_adaptive_clocking(bool enable)
634 uint8_t buf = enable ? 0x96 : 0x97;
635 LOG_DEBUG("%2.2x", buf);
637 uint32_t bytes_written;
638 int retval;
640 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
641 if (retval != ERROR_OK) {
642 LOG_ERROR("couldn't write command to %s adaptive clocking"
643 , enable ? "enable" : "disable");
644 return retval;
647 return ERROR_OK;
651 * Enable/disable the clk divide by 5 of the 60MHz master clock.
652 * This result in a JTAG clock speed range of 91.553Hz-6MHz
653 * respective 457.763Hz-30MHz.
655 static int ftx232h_clk_divide_by_5(bool enable)
657 uint32_t bytes_written;
658 uint8_t buf = enable ? 0x8b : 0x8a;
660 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
661 LOG_ERROR("couldn't write command to %s clk divide by 5"
662 , enable ? "enable" : "disable");
663 return ERROR_JTAG_INIT_FAILED;
665 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_x232H_MAX_TCK;
666 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
668 return ERROR_OK;
671 static int ft2232_speed(int speed)
673 uint8_t buf[3];
674 int retval;
675 uint32_t bytes_written;
677 retval = ERROR_OK;
678 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
679 if (ft2232_device_is_highspeed())
680 retval = ftx232h_adaptive_clocking(enable_adaptive_clocking);
681 else if (enable_adaptive_clocking) {
682 LOG_ERROR("ft2232 device %lu does not support RTCK"
683 , (long unsigned int)ftdi_device);
684 return ERROR_FAIL;
687 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
688 return retval;
690 buf[0] = 0x86; /* command "set divisor" */
691 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
692 buf[2] = (speed >> 8) & 0xff; /* valueH */
694 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
695 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
696 if (retval != ERROR_OK) {
697 LOG_ERROR("couldn't set FT2232 TCK speed");
698 return retval;
701 return ERROR_OK;
704 static int ft2232_speed_div(int speed, int *khz)
706 /* Take a look in the FT2232 manual,
707 * AN2232C-01 Command Processor for
708 * MPSSE and MCU Host Bus. Chapter 3.8 */
710 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
712 return ERROR_OK;
715 static int ft2232_khz(int khz, int *jtag_speed)
717 if (khz == 0) {
718 if (ft2232_device_is_highspeed()) {
719 *jtag_speed = RTCK_SPEED;
720 return ERROR_OK;
721 } else {
722 LOG_DEBUG("RCLK not supported");
723 return ERROR_FAIL;
727 /* Take a look in the FT2232 manual,
728 * AN2232C-01 Command Processor for
729 * MPSSE and MCU Host Bus. Chapter 3.8
731 * We will calc here with a multiplier
732 * of 10 for better rounding later. */
734 /* Calc speed, (ft2232_max_tck / khz) - 1
735 * Use 65000 for better rounding */
736 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
738 /* Add 0.9 for rounding */
739 *jtag_speed += 9;
741 /* Calc real speed */
742 *jtag_speed = *jtag_speed / 10;
744 /* Check if speed is greater than 0 */
745 if (*jtag_speed < 0)
746 *jtag_speed = 0;
748 /* Check max value */
749 if (*jtag_speed > 0xFFFF)
750 *jtag_speed = 0xFFFF;
752 return ERROR_OK;
755 static void ft2232_end_state(tap_state_t state)
757 if (tap_is_state_stable(state))
758 tap_set_end_state(state);
759 else {
760 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
761 exit(-1);
765 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
767 int num_bytes = (scan_size + 7) / 8;
768 int bits_left = scan_size;
769 int cur_byte = 0;
771 while (num_bytes-- > 1) {
772 buffer[cur_byte++] = buffer_read();
773 bits_left -= 8;
776 buffer[cur_byte] = 0x0;
778 /* There is one more partial byte left from the clock data in/out instructions */
779 if (bits_left > 1)
780 buffer[cur_byte] = buffer_read() >> 1;
781 /* This shift depends on the length of the
782 *clock data to tms instruction, insterted
783 *at end of the scan, now fixed to a two
784 *step transition in ft2232_add_scan */
785 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
788 static void ft2232_debug_dump_buffer(void)
790 int i;
791 char line[256];
792 char *line_p = line;
794 for (i = 0; i < ft2232_buffer_size; i++) {
795 line_p += snprintf(line_p,
796 sizeof(line) - (line_p - line),
797 "%2.2x ",
798 ft2232_buffer[i]);
799 if (i % 16 == 15) {
800 LOG_DEBUG("%s", line);
801 line_p = line;
805 if (line_p != line)
806 LOG_DEBUG("%s", line);
809 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
811 struct jtag_command *cmd;
812 uint8_t *buffer;
813 int scan_size;
814 enum scan_type type;
815 int retval;
816 uint32_t bytes_written = 0;
817 uint32_t bytes_read = 0;
819 #ifdef _DEBUG_USB_IO_
820 struct timeval start, inter, inter2, end;
821 struct timeval d_inter, d_inter2, d_end;
822 #endif
824 #ifdef _DEBUG_USB_COMMS_
825 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
826 ft2232_debug_dump_buffer();
827 #endif
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&start, NULL);
831 #endif
833 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
834 if (retval != ERROR_OK) {
835 LOG_ERROR("couldn't write MPSSE commands to FT2232");
836 return retval;
839 #ifdef _DEBUG_USB_IO_
840 gettimeofday(&inter, NULL);
841 #endif
843 if (ft2232_expect_read) {
844 /* FIXME this "timeout" is never changed ... */
845 int timeout = LIBFTDI_READ_RETRY_COUNT;
846 ft2232_buffer_size = 0;
848 #ifdef _DEBUG_USB_IO_
849 gettimeofday(&inter2, NULL);
850 #endif
852 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
853 if (retval != ERROR_OK) {
854 LOG_ERROR("couldn't read from FT2232");
855 return retval;
858 #ifdef _DEBUG_USB_IO_
859 gettimeofday(&end, NULL);
861 timeval_subtract(&d_inter, &inter, &start);
862 timeval_subtract(&d_inter2, &inter2, &start);
863 timeval_subtract(&d_end, &end, &start);
865 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
866 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
867 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
868 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
869 #endif
871 ft2232_buffer_size = bytes_read;
873 if (ft2232_expect_read != ft2232_buffer_size) {
874 LOG_ERROR("ft2232_expect_read (%i) != "
875 "ft2232_buffer_size (%i) "
876 "(%i retries)",
877 ft2232_expect_read,
878 ft2232_buffer_size,
879 LIBFTDI_READ_RETRY_COUNT - timeout);
880 ft2232_debug_dump_buffer();
882 exit(-1);
885 #ifdef _DEBUG_USB_COMMS_
886 LOG_DEBUG("read buffer (%i retries): %i bytes",
887 LIBFTDI_READ_RETRY_COUNT - timeout,
888 ft2232_buffer_size);
889 ft2232_debug_dump_buffer();
890 #endif
893 ft2232_expect_read = 0;
894 ft2232_read_pointer = 0;
896 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
897 * that wasn't handled by a caller-provided error handler
899 retval = ERROR_OK;
901 cmd = first;
902 while (cmd != last) {
903 switch (cmd->type) {
904 case JTAG_SCAN:
905 type = jtag_scan_type(cmd->cmd.scan);
906 if (type != SCAN_OUT) {
907 scan_size = jtag_scan_size(cmd->cmd.scan);
908 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
909 ft2232_read_scan(type, buffer, scan_size);
910 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
911 retval = ERROR_JTAG_QUEUE_FAILED;
912 free(buffer);
914 break;
916 default:
917 break;
920 cmd = cmd->next;
923 ft2232_buffer_size = 0;
925 return retval;
929 * Function ft2232_add_pathmove
930 * moves the TAP controller from the current state to a new state through the
931 * given path, where path is an array of tap_state_t's.
933 * @param path is an array of tap_stat_t which gives the states to traverse through
934 * ending with the last state at path[num_states-1]
935 * @param num_states is the count of state steps to move through
937 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
939 int state_count = 0;
941 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
943 DEBUG_JTAG_IO("-");
945 /* this loop verifies that the path is legal and logs each state in the path */
946 while (num_states) {
947 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
948 int bit_count = 0;
949 int num_states_batch = num_states > 7 ? 7 : num_states;
951 /* command "Clock Data to TMS/CS Pin (no Read)" */
952 buffer_write(0x4b);
954 /* number of states remaining */
955 buffer_write(num_states_batch - 1);
957 while (num_states_batch--) {
958 /* either TMS=0 or TMS=1 must work ... */
959 if (tap_state_transition(tap_get_state(), false) == path[state_count])
960 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
961 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
962 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
964 /* ... or else the caller goofed BADLY */
965 else {
966 LOG_ERROR("BUG: %s -> %s isn't a valid "
967 "TAP state transition",
968 tap_state_name(tap_get_state()),
969 tap_state_name(path[state_count]));
970 exit(-1);
973 tap_set_state(path[state_count]);
974 state_count++;
975 num_states--;
978 buffer_write(tms_byte);
980 tap_set_end_state(tap_get_state());
983 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
985 int num_bytes = (scan_size + 7) / 8;
986 int bits_left = scan_size;
987 int cur_byte = 0;
988 int last_bit;
990 if (!ir_scan) {
991 if (tap_get_state() != TAP_DRSHIFT)
992 move_to_state(TAP_DRSHIFT);
993 } else {
994 if (tap_get_state() != TAP_IRSHIFT)
995 move_to_state(TAP_IRSHIFT);
998 /* add command for complete bytes */
999 while (num_bytes > 1) {
1000 int thisrun_bytes;
1001 if (type == SCAN_IO) {
1002 /* Clock Data Bytes In and Out LSB First */
1003 buffer_write(0x39);
1004 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1005 } else if (type == SCAN_OUT) {
1006 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1007 buffer_write(0x19);
1008 /* LOG_DEBUG("added TDI bytes (o)"); */
1009 } else if (type == SCAN_IN) {
1010 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1011 buffer_write(0x28);
1012 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1015 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1016 num_bytes -= thisrun_bytes;
1018 buffer_write((uint8_t) (thisrun_bytes - 1));
1019 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1021 if (type != SCAN_IN) {
1022 /* add complete bytes */
1023 while (thisrun_bytes-- > 0) {
1024 buffer_write(buffer[cur_byte++]);
1025 bits_left -= 8;
1027 } else /* (type == SCAN_IN) */
1028 bits_left -= 8 * (thisrun_bytes);
1031 /* the most signifcant bit is scanned during TAP movement */
1032 if (type != SCAN_IN)
1033 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1034 else
1035 last_bit = 0;
1037 /* process remaining bits but the last one */
1038 if (bits_left > 1) {
1039 if (type == SCAN_IO) {
1040 /* Clock Data Bits In and Out LSB First */
1041 buffer_write(0x3b);
1042 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1043 } 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)"); */
1047 } else if (type == SCAN_IN) {
1048 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1049 buffer_write(0x2a);
1050 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1053 buffer_write(bits_left - 2);
1054 if (type != SCAN_IN)
1055 buffer_write(buffer[cur_byte]);
1058 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1059 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1060 if (type == SCAN_IO) {
1061 /* Clock Data Bits In and Out LSB First */
1062 buffer_write(0x3b);
1063 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1064 } else if (type == SCAN_OUT) {
1065 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1066 buffer_write(0x1b);
1067 /* LOG_DEBUG("added TDI bits (o)"); */
1068 } else if (type == SCAN_IN) {
1069 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1070 buffer_write(0x2a);
1071 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1073 buffer_write(0x0);
1074 buffer_write(last_bit);
1075 } else {
1076 int tms_bits;
1077 int tms_count;
1078 uint8_t mpsse_cmd;
1080 /* move from Shift-IR/DR to end state */
1081 if (type != SCAN_OUT) {
1082 /* We always go to the PAUSE state in two step at the end of an IN or IO
1083 *scan
1084 * This must be coordinated with the bit shifts in ft2232_read_scan */
1085 tms_bits = 0x01;
1086 tms_count = 2;
1087 /* Clock Data to TMS/CS Pin with Read */
1088 mpsse_cmd = 0x6b;
1089 } else {
1090 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1091 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1092 /* Clock Data to TMS/CS Pin (no Read) */
1093 mpsse_cmd = 0x4b;
1096 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1097 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1100 if (tap_get_state() != tap_get_end_state())
1101 move_to_state(tap_get_end_state());
1104 static int ft2232_large_scan(struct scan_command *cmd,
1105 enum scan_type type,
1106 uint8_t *buffer,
1107 int scan_size)
1109 int num_bytes = (scan_size + 7) / 8;
1110 int bits_left = scan_size;
1111 int cur_byte = 0;
1112 int last_bit;
1113 uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1114 uint8_t *receive_pointer = receive_buffer;
1115 uint32_t bytes_written;
1116 uint32_t bytes_read;
1117 int retval;
1118 int thisrun_read = 0;
1120 if (cmd->ir_scan) {
1121 LOG_ERROR("BUG: large IR scans are not supported");
1122 exit(-1);
1125 if (tap_get_state() != TAP_DRSHIFT)
1126 move_to_state(TAP_DRSHIFT);
1128 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1129 if (retval != ERROR_OK) {
1130 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1131 exit(-1);
1133 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1134 ft2232_buffer_size, (int)bytes_written);
1135 ft2232_buffer_size = 0;
1137 /* add command for complete bytes */
1138 while (num_bytes > 1) {
1139 int thisrun_bytes;
1141 if (type == SCAN_IO) {
1142 /* Clock Data Bytes In and Out LSB First */
1143 buffer_write(0x39);
1144 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1145 } else if (type == SCAN_OUT) {
1146 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1147 buffer_write(0x19);
1148 /* LOG_DEBUG("added TDI bytes (o)"); */
1149 } else if (type == SCAN_IN) {
1150 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1151 buffer_write(0x28);
1152 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1155 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1156 thisrun_read = thisrun_bytes;
1157 num_bytes -= thisrun_bytes;
1158 buffer_write((uint8_t) (thisrun_bytes - 1));
1159 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1161 if (type != SCAN_IN) {
1162 /* add complete bytes */
1163 while (thisrun_bytes-- > 0) {
1164 buffer_write(buffer[cur_byte]);
1165 cur_byte++;
1166 bits_left -= 8;
1168 } else /* (type == SCAN_IN) */
1169 bits_left -= 8 * (thisrun_bytes);
1171 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1172 if (retval != ERROR_OK) {
1173 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1174 exit(-1);
1176 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1177 ft2232_buffer_size,
1178 (int)bytes_written);
1179 ft2232_buffer_size = 0;
1181 if (type != SCAN_OUT) {
1182 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1183 if (retval != ERROR_OK) {
1184 LOG_ERROR("couldn't read from FT2232");
1185 exit(-1);
1187 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1188 thisrun_read,
1189 (int)bytes_read);
1190 receive_pointer += bytes_read;
1194 thisrun_read = 0;
1196 /* the most signifcant bit is scanned during TAP movement */
1197 if (type != SCAN_IN)
1198 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1199 else
1200 last_bit = 0;
1202 /* process remaining bits but the last one */
1203 if (bits_left > 1) {
1204 if (type == SCAN_IO) {
1205 /* Clock Data Bits In and Out LSB First */
1206 buffer_write(0x3b);
1207 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1208 } else if (type == SCAN_OUT) {
1209 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1210 buffer_write(0x1b);
1211 /* LOG_DEBUG("added TDI bits (o)"); */
1212 } else if (type == SCAN_IN) {
1213 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1214 buffer_write(0x2a);
1215 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1217 buffer_write(bits_left - 2);
1218 if (type != SCAN_IN)
1219 buffer_write(buffer[cur_byte]);
1221 if (type != SCAN_OUT)
1222 thisrun_read += 2;
1225 if (tap_get_end_state() == TAP_DRSHIFT) {
1226 if (type == SCAN_IO) {
1227 /* Clock Data Bits In and Out LSB First */
1228 buffer_write(0x3b);
1229 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1230 } else if (type == SCAN_OUT) {
1231 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1232 buffer_write(0x1b);
1233 /* LOG_DEBUG("added TDI bits (o)"); */
1234 } else if (type == SCAN_IN) {
1235 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1236 buffer_write(0x2a);
1237 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1239 buffer_write(0x0);
1240 buffer_write(last_bit);
1241 } else {
1242 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1243 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1244 uint8_t mpsse_cmd;
1246 /* move from Shift-IR/DR to end state */
1247 if (type != SCAN_OUT) {
1248 /* Clock Data to TMS/CS Pin with Read */
1249 mpsse_cmd = 0x6b;
1250 /* LOG_DEBUG("added TMS scan (read)"); */
1251 } else {
1252 /* Clock Data to TMS/CS Pin (no Read) */
1253 mpsse_cmd = 0x4b;
1254 /* LOG_DEBUG("added TMS scan (no read)"); */
1257 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1258 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1261 if (type != SCAN_OUT)
1262 thisrun_read += 1;
1264 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1265 if (retval != ERROR_OK) {
1266 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1267 exit(-1);
1269 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1270 ft2232_buffer_size,
1271 (int)bytes_written);
1272 ft2232_buffer_size = 0;
1274 if (type != SCAN_OUT) {
1275 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1276 if (retval != ERROR_OK) {
1277 LOG_ERROR("couldn't read from FT2232");
1278 exit(-1);
1280 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1281 thisrun_read,
1282 (int)bytes_read);
1285 return ERROR_OK;
1288 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1290 int predicted_size = 3;
1291 int num_bytes = (scan_size - 1) / 8;
1293 if (tap_get_state() != TAP_DRSHIFT)
1294 predicted_size += get_tms_buffer_requirements(
1295 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1297 if (type == SCAN_IN) { /* only from device to host */
1298 /* complete bytes */
1299 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1301 /* remaining bits - 1 (up to 7) */
1302 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1303 } else {/* host to device, or bidirectional
1304 * complete bytes */
1305 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1307 /* remaining bits -1 (up to 7) */
1308 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1311 return predicted_size;
1314 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1316 int predicted_size = 0;
1318 if (type != SCAN_OUT) {
1319 /* complete bytes */
1320 predicted_size +=
1321 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1323 /* remaining bits - 1 */
1324 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1326 /* last bit (from TMS scan) */
1327 predicted_size += 1;
1330 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1332 return predicted_size;
1335 /* semi-generic FT2232/FT4232 reset code */
1336 static void ftx23_reset(int trst, int srst)
1338 enum reset_types jtag_reset_config = jtag_get_reset_config();
1339 if (trst == 1) {
1340 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1341 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1342 else
1343 low_output &= ~nTRST; /* switch output low */
1344 } else if (trst == 0) {
1345 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1346 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1347 *and external pullup) */
1348 else
1349 low_output |= nTRST; /* switch output high */
1352 if (srst == 1) {
1353 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1354 low_output &= ~nSRST; /* switch output low */
1355 else
1356 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1357 } else if (srst == 0) {
1358 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1359 low_output |= nSRST; /* switch output high */
1360 else
1361 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1364 /* command "set data bits low byte" */
1365 buffer_write(0x80);
1366 buffer_write(low_output);
1367 buffer_write(low_direction);
1370 static void jtagkey_reset(int trst, int srst)
1372 enum reset_types jtag_reset_config = jtag_get_reset_config();
1373 if (trst == 1) {
1374 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1375 high_output &= ~nTRSTnOE;
1376 else
1377 high_output &= ~nTRST;
1378 } else if (trst == 0) {
1379 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1380 high_output |= nTRSTnOE;
1381 else
1382 high_output |= nTRST;
1385 if (srst == 1) {
1386 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1387 high_output &= ~nSRST;
1388 else
1389 high_output &= ~nSRSTnOE;
1390 } else if (srst == 0) {
1391 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1392 high_output |= nSRST;
1393 else
1394 high_output |= nSRSTnOE;
1397 /* command "set data bits high byte" */
1398 buffer_write(0x82);
1399 buffer_write(high_output);
1400 buffer_write(high_direction);
1401 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1402 trst,
1403 srst,
1404 high_output,
1405 high_direction);
1408 static void olimex_jtag_reset(int trst, int srst)
1410 enum reset_types jtag_reset_config = jtag_get_reset_config();
1411 if (trst == 1) {
1412 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1413 high_output &= ~nTRSTnOE;
1414 else
1415 high_output &= ~nTRST;
1416 } else if (trst == 0) {
1417 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1418 high_output |= nTRSTnOE;
1419 else
1420 high_output |= nTRST;
1423 if (srst == 1)
1424 high_output |= nSRST;
1425 else if (srst == 0)
1426 high_output &= ~nSRST;
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",
1433 trst,
1434 srst,
1435 high_output,
1436 high_direction);
1439 static void axm0432_jtag_reset(int trst, int srst)
1441 if (trst == 1) {
1442 tap_set_state(TAP_RESET);
1443 high_output &= ~nTRST;
1444 } else if (trst == 0)
1445 high_output |= nTRST;
1447 if (srst == 1)
1448 high_output &= ~nSRST;
1449 else if (srst == 0)
1450 high_output |= nSRST;
1452 /* command "set data bits low byte" */
1453 buffer_write(0x82);
1454 buffer_write(high_output);
1455 buffer_write(high_direction);
1456 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1457 trst,
1458 srst,
1459 high_output,
1460 high_direction);
1463 static void flyswatter_reset(int trst, int srst)
1465 if (trst == 1)
1466 low_output &= ~nTRST;
1467 else if (trst == 0)
1468 low_output |= nTRST;
1470 if (srst == 1)
1471 low_output |= nSRST;
1472 else if (srst == 0)
1473 low_output &= ~nSRST;
1475 /* command "set data bits low byte" */
1476 buffer_write(0x80);
1477 buffer_write(low_output);
1478 buffer_write(low_direction);
1479 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1480 trst,
1481 srst,
1482 low_output,
1483 low_direction);
1486 static void flyswatter1_reset(int trst, int srst)
1488 flyswatter_reset(trst, srst);
1491 static void flyswatter2_reset(int trst, int srst)
1493 flyswatter_reset(trst, !srst);
1496 static void minimodule_reset(int trst, int srst)
1498 if (srst == 1)
1499 low_output &= ~nSRST;
1500 else if (srst == 0)
1501 low_output |= nSRST;
1503 /* command "set data bits low byte" */
1504 buffer_write(0x80);
1505 buffer_write(low_output);
1506 buffer_write(low_direction);
1507 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1508 trst,
1509 srst,
1510 low_output,
1511 low_direction);
1514 static void turtle_reset(int trst, int srst)
1516 trst = trst;
1518 if (srst == 1)
1519 low_output |= nSRST;
1520 else if (srst == 0)
1521 low_output &= ~nSRST;
1523 /* command "set data bits low byte" */
1524 buffer_write(0x80);
1525 buffer_write(low_output);
1526 buffer_write(low_direction);
1527 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1528 srst,
1529 low_output,
1530 low_direction);
1533 static void comstick_reset(int trst, int srst)
1535 if (trst == 1)
1536 high_output &= ~nTRST;
1537 else if (trst == 0)
1538 high_output |= nTRST;
1540 if (srst == 1)
1541 high_output &= ~nSRST;
1542 else if (srst == 0)
1543 high_output |= nSRST;
1545 /* command "set data bits high byte" */
1546 buffer_write(0x82);
1547 buffer_write(high_output);
1548 buffer_write(high_direction);
1549 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1550 trst,
1551 srst,
1552 high_output,
1553 high_direction);
1556 static void stm32stick_reset(int trst, int srst)
1558 if (trst == 1)
1559 high_output &= ~nTRST;
1560 else if (trst == 0)
1561 high_output |= nTRST;
1563 if (srst == 1)
1564 low_output &= ~nSRST;
1565 else if (srst == 0)
1566 low_output |= nSRST;
1568 /* command "set data bits low byte" */
1569 buffer_write(0x80);
1570 buffer_write(low_output);
1571 buffer_write(low_direction);
1573 /* command "set data bits high byte" */
1574 buffer_write(0x82);
1575 buffer_write(high_output);
1576 buffer_write(high_direction);
1577 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1578 trst,
1579 srst,
1580 high_output,
1581 high_direction);
1584 static void sheevaplug_reset(int trst, int srst)
1586 if (trst == 1)
1587 high_output &= ~nTRST;
1588 else if (trst == 0)
1589 high_output |= nTRST;
1591 if (srst == 1)
1592 high_output &= ~nSRSTnOE;
1593 else if (srst == 0)
1594 high_output |= nSRSTnOE;
1596 /* command "set data bits high byte" */
1597 buffer_write(0x82);
1598 buffer_write(high_output);
1599 buffer_write(high_direction);
1600 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1601 trst,
1602 srst,
1603 high_output,
1604 high_direction);
1607 static void redbee_reset(int trst, int srst)
1609 if (trst == 1) {
1610 tap_set_state(TAP_RESET);
1611 high_output &= ~nTRST;
1612 } else if (trst == 0)
1613 high_output |= nTRST;
1615 if (srst == 1)
1616 high_output &= ~nSRST;
1617 else if (srst == 0)
1618 high_output |= nSRST;
1620 /* command "set data bits low 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, "
1625 "high_direction: 0x%2.2x", trst, srst, high_output,
1626 high_direction);
1629 static void xds100v2_reset(int trst, int srst)
1631 if (trst == 1) {
1632 tap_set_state(TAP_RESET);
1633 high_output &= ~nTRST;
1634 } else if (trst == 0)
1635 high_output |= nTRST;
1637 if (srst == 1)
1638 high_output |= nSRST;
1639 else if (srst == 0)
1640 high_output &= ~nSRST;
1642 /* command "set data bits low byte" */
1643 buffer_write(0x82);
1644 buffer_write(high_output);
1645 buffer_write(high_direction);
1646 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1647 "high_direction: 0x%2.2x", trst, srst, high_output,
1648 high_direction);
1651 static int ft2232_execute_runtest(struct jtag_command *cmd)
1653 int retval;
1654 int i;
1655 int predicted_size = 0;
1656 retval = ERROR_OK;
1658 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1659 cmd->cmd.runtest->num_cycles,
1660 tap_state_name(cmd->cmd.runtest->end_state));
1662 /* only send the maximum buffer size that FT2232C can handle */
1663 predicted_size = 0;
1664 if (tap_get_state() != TAP_IDLE)
1665 predicted_size += 3;
1666 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1667 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1668 predicted_size += 3;
1669 if (tap_get_end_state() != TAP_IDLE)
1670 predicted_size += 3;
1671 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1672 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1673 retval = ERROR_JTAG_QUEUE_FAILED;
1674 require_send = 0;
1675 first_unsent = cmd;
1677 if (tap_get_state() != TAP_IDLE) {
1678 move_to_state(TAP_IDLE);
1679 require_send = 1;
1681 i = cmd->cmd.runtest->num_cycles;
1682 while (i > 0) {
1683 /* there are no state transitions in this code, so omit state tracking */
1685 /* command "Clock Data to TMS/CS Pin (no Read)" */
1686 buffer_write(0x4b);
1688 /* scan 7 bits */
1689 buffer_write((i > 7) ? 6 : (i - 1));
1691 /* TMS data bits */
1692 buffer_write(0x0);
1694 i -= (i > 7) ? 7 : i;
1695 /* LOG_DEBUG("added TMS scan (no read)"); */
1698 ft2232_end_state(cmd->cmd.runtest->end_state);
1700 if (tap_get_state() != tap_get_end_state())
1701 move_to_state(tap_get_end_state());
1703 require_send = 1;
1704 DEBUG_JTAG_IO("runtest: %i, end in %s",
1705 cmd->cmd.runtest->num_cycles,
1706 tap_state_name(tap_get_end_state()));
1707 return retval;
1710 static int ft2232_execute_statemove(struct jtag_command *cmd)
1712 int predicted_size = 0;
1713 int retval = ERROR_OK;
1715 DEBUG_JTAG_IO("statemove end in %s",
1716 tap_state_name(cmd->cmd.statemove->end_state));
1718 /* only send the maximum buffer size that FT2232C can handle */
1719 predicted_size = 3;
1720 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1721 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1722 retval = ERROR_JTAG_QUEUE_FAILED;
1723 require_send = 0;
1724 first_unsent = cmd;
1726 ft2232_end_state(cmd->cmd.statemove->end_state);
1728 /* For TAP_RESET, ignore the current recorded state. It's often
1729 * wrong at server startup, and this transation is critical whenever
1730 * it's requested.
1732 if (tap_get_end_state() == TAP_RESET) {
1733 clock_tms(0x4b, 0xff, 5, 0);
1734 require_send = 1;
1736 /* shortest-path move to desired end state */
1737 } else if (tap_get_state() != tap_get_end_state()) {
1738 move_to_state(tap_get_end_state());
1739 require_send = 1;
1742 return retval;
1746 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1747 * (or SWD) state machine.
1749 static int ft2232_execute_tms(struct jtag_command *cmd)
1751 int retval = ERROR_OK;
1752 unsigned num_bits = cmd->cmd.tms->num_bits;
1753 const uint8_t *bits = cmd->cmd.tms->bits;
1754 unsigned count;
1756 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1758 /* only send the maximum buffer size that FT2232C can handle */
1759 count = 3 * DIV_ROUND_UP(num_bits, 4);
1760 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1761 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1762 retval = ERROR_JTAG_QUEUE_FAILED;
1764 require_send = 0;
1765 first_unsent = cmd;
1768 /* Shift out in batches of at most 6 bits; there's a report of an
1769 * FT2232 bug in this area, where shifting exactly 7 bits can make
1770 * problems with TMS signaling for the last clock cycle:
1772 * http://developer.intra2net.com/mailarchive/html/
1773 * libftdi/2009/msg00292.html
1775 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1777 * Note that pathmoves in JTAG are not often seven bits, so that
1778 * isn't a particularly likely situation outside of "special"
1779 * signaling such as switching between JTAG and SWD modes.
1781 while (num_bits) {
1782 if (num_bits <= 6) {
1783 buffer_write(0x4b);
1784 buffer_write(num_bits - 1);
1785 buffer_write(*bits & 0x3f);
1786 break;
1789 /* Yes, this is lazy ... we COULD shift out more data
1790 * bits per operation, but doing it in nybbles is easy
1792 buffer_write(0x4b);
1793 buffer_write(3);
1794 buffer_write(*bits & 0xf);
1795 num_bits -= 4;
1797 count = (num_bits > 4) ? 4 : num_bits;
1799 buffer_write(0x4b);
1800 buffer_write(count - 1);
1801 buffer_write((*bits >> 4) & 0xf);
1802 num_bits -= count;
1804 bits++;
1807 require_send = 1;
1808 return retval;
1811 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1813 int predicted_size = 0;
1814 int retval = ERROR_OK;
1816 tap_state_t *path = cmd->cmd.pathmove->path;
1817 int num_states = cmd->cmd.pathmove->num_states;
1819 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1820 tap_state_name(tap_get_state()),
1821 tap_state_name(path[num_states-1]));
1823 /* only send the maximum buffer size that FT2232C can handle */
1824 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1825 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1826 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1827 retval = ERROR_JTAG_QUEUE_FAILED;
1829 require_send = 0;
1830 first_unsent = cmd;
1833 ft2232_add_pathmove(path, num_states);
1834 require_send = 1;
1836 return retval;
1839 static int ft2232_execute_scan(struct jtag_command *cmd)
1841 uint8_t *buffer;
1842 int scan_size; /* size of IR or DR scan */
1843 int predicted_size = 0;
1844 int retval = ERROR_OK;
1846 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1848 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1850 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1852 predicted_size = ft2232_predict_scan_out(scan_size, type);
1853 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1854 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1855 /* unsent commands before this */
1856 if (first_unsent != cmd)
1857 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1858 retval = ERROR_JTAG_QUEUE_FAILED;
1860 /* current command */
1861 ft2232_end_state(cmd->cmd.scan->end_state);
1862 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1863 require_send = 0;
1864 first_unsent = cmd->next;
1865 if (buffer)
1866 free(buffer);
1867 return retval;
1868 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1869 LOG_DEBUG(
1870 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1871 first_unsent,
1872 cmd);
1873 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1874 retval = ERROR_JTAG_QUEUE_FAILED;
1875 require_send = 0;
1876 first_unsent = cmd;
1878 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1879 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1880 ft2232_end_state(cmd->cmd.scan->end_state);
1881 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1882 require_send = 1;
1883 if (buffer)
1884 free(buffer);
1885 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1886 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1887 tap_state_name(tap_get_end_state()));
1888 return retval;
1892 static int ft2232_execute_reset(struct jtag_command *cmd)
1894 int retval;
1895 int predicted_size = 0;
1896 retval = ERROR_OK;
1898 DEBUG_JTAG_IO("reset trst: %i srst %i",
1899 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1901 /* only send the maximum buffer size that FT2232C can handle */
1902 predicted_size = 3;
1903 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1904 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1905 retval = ERROR_JTAG_QUEUE_FAILED;
1906 require_send = 0;
1907 first_unsent = cmd;
1910 if ((cmd->cmd.reset->trst == 1) ||
1911 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1912 tap_set_state(TAP_RESET);
1914 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1915 require_send = 1;
1917 DEBUG_JTAG_IO("trst: %i, srst: %i",
1918 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1919 return retval;
1922 static int ft2232_execute_sleep(struct jtag_command *cmd)
1924 int retval;
1925 retval = ERROR_OK;
1927 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1929 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1930 retval = ERROR_JTAG_QUEUE_FAILED;
1931 first_unsent = cmd->next;
1932 jtag_sleep(cmd->cmd.sleep->us);
1933 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1934 cmd->cmd.sleep->us,
1935 tap_state_name(tap_get_state()));
1936 return retval;
1939 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1941 int retval;
1942 retval = ERROR_OK;
1944 /* this is only allowed while in a stable state. A check for a stable
1945 * state was done in jtag_add_clocks()
1947 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1948 retval = ERROR_JTAG_QUEUE_FAILED;
1949 DEBUG_JTAG_IO("clocks %i while in %s",
1950 cmd->cmd.stableclocks->num_cycles,
1951 tap_state_name(tap_get_state()));
1952 return retval;
1955 static int ft2232_execute_command(struct jtag_command *cmd)
1957 int retval;
1959 switch (cmd->type) {
1960 case JTAG_RESET:
1961 retval = ft2232_execute_reset(cmd);
1962 break;
1963 case JTAG_RUNTEST:
1964 retval = ft2232_execute_runtest(cmd);
1965 break;
1966 case JTAG_TLR_RESET:
1967 retval = ft2232_execute_statemove(cmd);
1968 break;
1969 case JTAG_PATHMOVE:
1970 retval = ft2232_execute_pathmove(cmd);
1971 break;
1972 case JTAG_SCAN:
1973 retval = ft2232_execute_scan(cmd);
1974 break;
1975 case JTAG_SLEEP:
1976 retval = ft2232_execute_sleep(cmd);
1977 break;
1978 case JTAG_STABLECLOCKS:
1979 retval = ft2232_execute_stableclocks(cmd);
1980 break;
1981 case JTAG_TMS:
1982 retval = ft2232_execute_tms(cmd);
1983 break;
1984 default:
1985 LOG_ERROR("BUG: unknown JTAG command type encountered");
1986 retval = ERROR_JTAG_QUEUE_FAILED;
1987 break;
1989 return retval;
1992 static int ft2232_execute_queue(void)
1994 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1995 int retval;
1997 first_unsent = cmd; /* next command that has to be sent */
1998 require_send = 0;
2000 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2001 * that wasn't handled by a caller-provided error handler
2003 retval = ERROR_OK;
2005 ft2232_buffer_size = 0;
2006 ft2232_expect_read = 0;
2008 /* blink, if the current layout has that feature */
2009 if (layout->blink)
2010 layout->blink();
2012 while (cmd) {
2013 /* fill the write buffer with the desired command */
2014 if (ft2232_execute_command(cmd) != ERROR_OK)
2015 retval = ERROR_JTAG_QUEUE_FAILED;
2016 /* Start reading input before FT2232 TX buffer fills up.
2017 * Sometimes this happens because we don't know the
2018 * length of the last command before we execute it. So
2019 * we simple inform the user.
2021 cmd = cmd->next;
2023 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2024 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2025 LOG_DEBUG("read buffer size looks too high %d/%d",
2026 ft2232_expect_read,
2027 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2028 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2029 retval = ERROR_JTAG_QUEUE_FAILED;
2030 first_unsent = cmd;
2034 if (require_send > 0)
2035 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2036 retval = ERROR_JTAG_QUEUE_FAILED;
2038 return retval;
2041 #if BUILD_FT2232_FTD2XX == 1
2042 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2044 FT_STATUS status;
2045 DWORD deviceID;
2046 char SerialNumber[16];
2047 char Description[64];
2048 DWORD openex_flags = 0;
2049 char *openex_string = NULL;
2050 uint8_t latency_timer;
2052 if (layout == NULL) {
2053 LOG_WARNING("No ft2232 layout specified'");
2054 return ERROR_JTAG_INIT_FAILED;
2057 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2058 layout->name, vid, pid);
2060 #if IS_WIN32 == 0
2061 /* Add non-standard Vid/Pid to the linux driver */
2062 status = FT_SetVIDPID(vid, pid);
2063 if (status != FT_OK)
2064 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2066 #endif
2068 if (ft2232_device_desc && ft2232_serial) {
2069 LOG_WARNING(
2070 "can't open by device description and serial number, giving precedence to serial");
2071 ft2232_device_desc = NULL;
2074 if (ft2232_device_desc) {
2075 openex_string = ft2232_device_desc;
2076 openex_flags = FT_OPEN_BY_DESCRIPTION;
2077 } else if (ft2232_serial) {
2078 openex_string = ft2232_serial;
2079 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2080 } else {
2081 LOG_ERROR("neither device description nor serial number specified");
2082 LOG_ERROR(
2083 "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'",
2103 ft2232_device_desc,
2104 ft2232_device_desc_A);
2109 if (status != FT_OK) {
2110 DWORD num_devices;
2112 if (more) {
2113 LOG_WARNING("unable to open ftdi device (trying more): %s",
2114 ftd2xx_status_string(status));
2115 *try_more = 1;
2116 return ERROR_JTAG_INIT_FAILED;
2118 LOG_ERROR("unable to open ftdi device: %s",
2119 ftd2xx_status_string(status));
2120 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2121 if (status == FT_OK) {
2122 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2123 uint32_t i;
2125 for (i = 0; i < num_devices; i++)
2126 desc_array[i] = malloc(64);
2128 desc_array[num_devices] = NULL;
2130 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2132 if (status == FT_OK) {
2133 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2134 for (i = 0; i < num_devices; i++)
2135 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2138 for (i = 0; i < num_devices; i++)
2139 free(desc_array[i]);
2141 free(desc_array);
2142 } else
2143 LOG_ERROR("ListDevices: NONE");
2144 return ERROR_JTAG_INIT_FAILED;
2147 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2148 if (status != FT_OK) {
2149 LOG_ERROR("unable to set latency timer: %s",
2150 ftd2xx_status_string(status));
2151 return ERROR_JTAG_INIT_FAILED;
2154 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2155 if (status != FT_OK) {
2156 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2157 * so ignore errors if using this driver version */
2158 DWORD dw_version;
2160 status = FT_GetDriverVersion(ftdih, &dw_version);
2161 LOG_ERROR("unable to get latency timer: %s",
2162 ftd2xx_status_string(status));
2164 if ((status == FT_OK) && (dw_version == 0x10004)) {
2165 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2166 "with FT_GetLatencyTimer, upgrade to a newer version");
2167 } else
2168 return ERROR_JTAG_INIT_FAILED;
2169 } else
2170 LOG_DEBUG("current latency timer: %i", latency_timer);
2172 status = FT_SetTimeouts(ftdih, 5000, 5000);
2173 if (status != FT_OK) {
2174 LOG_ERROR("unable to set timeouts: %s",
2175 ftd2xx_status_string(status));
2176 return ERROR_JTAG_INIT_FAILED;
2179 status = FT_SetBitMode(ftdih, 0x0b, 2);
2180 if (status != FT_OK) {
2181 LOG_ERROR("unable to enable bit i/o mode: %s",
2182 ftd2xx_status_string(status));
2183 return ERROR_JTAG_INIT_FAILED;
2186 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2187 SerialNumber, Description, NULL);
2188 if (status != FT_OK) {
2189 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2190 ftd2xx_status_string(status));
2191 return ERROR_JTAG_INIT_FAILED;
2192 } else {
2193 static const char *type_str[] = {
2194 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2196 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2197 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2198 ? ftdi_device : FT_DEVICE_UNKNOWN;
2199 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2200 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2201 LOG_INFO("SerialNumber: %s", SerialNumber);
2202 LOG_INFO("Description: %s", Description);
2205 return ERROR_OK;
2208 static int ft2232_purge_ftd2xx(void)
2210 FT_STATUS status;
2212 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2213 if (status != FT_OK) {
2214 LOG_ERROR("error purging ftd2xx device: %s",
2215 ftd2xx_status_string(status));
2216 return ERROR_JTAG_INIT_FAILED;
2219 return ERROR_OK;
2222 #endif /* BUILD_FT2232_FTD2XX == 1 */
2224 #if BUILD_FT2232_LIBFTDI == 1
2225 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2227 uint8_t latency_timer;
2229 if (layout == NULL) {
2230 LOG_WARNING("No ft2232 layout specified'");
2231 return ERROR_JTAG_INIT_FAILED;
2234 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2235 layout->name, vid, pid);
2237 if (ftdi_init(&ftdic) < 0)
2238 return ERROR_JTAG_INIT_FAILED;
2240 /* default to INTERFACE_A */
2241 if (channel == INTERFACE_ANY)
2242 channel = INTERFACE_A;
2243 if (ftdi_set_interface(&ftdic, channel) < 0) {
2244 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2245 return ERROR_JTAG_INIT_FAILED;
2248 /* context, vendor id, product id */
2249 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2250 if (more)
2251 LOG_WARNING("unable to open ftdi device (trying more): %s",
2252 ftdic.error_str);
2253 else
2254 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2255 *try_more = 1;
2256 return ERROR_JTAG_INIT_FAILED;
2259 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2260 if (ftdi_usb_reset(&ftdic) < 0) {
2261 LOG_ERROR("unable to reset ftdi device");
2262 return ERROR_JTAG_INIT_FAILED;
2265 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2266 LOG_ERROR("unable to set latency timer");
2267 return ERROR_JTAG_INIT_FAILED;
2270 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2271 LOG_ERROR("unable to get latency timer");
2272 return ERROR_JTAG_INIT_FAILED;
2273 } else
2274 LOG_DEBUG("current latency timer: %i", latency_timer);
2276 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2278 ftdi_device = ftdic.type;
2279 static const char *type_str[] = {
2280 "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "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) {
2292 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2293 return ERROR_JTAG_INIT_FAILED;
2296 return ERROR_OK;
2299 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2301 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2303 uint8_t buf[3];
2304 uint32_t bytes_written;
2306 buf[0] = 0x80; /* command "set data bits low byte" */
2307 buf[1] = value; /* value */
2308 buf[2] = direction; /* direction */
2310 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2312 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2313 LOG_ERROR("couldn't initialize data bits low byte");
2314 return ERROR_JTAG_INIT_FAILED;
2317 return ERROR_OK;
2320 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2322 uint8_t buf[3];
2323 uint32_t bytes_written;
2325 buf[0] = 0x82; /* command "set data bits high byte" */
2326 buf[1] = value; /* value */
2327 buf[2] = direction; /* direction */
2329 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2331 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2332 LOG_ERROR("couldn't initialize data bits high byte");
2333 return ERROR_JTAG_INIT_FAILED;
2336 return ERROR_OK;
2339 static int ft2232_init(void)
2341 uint8_t buf[1];
2342 int retval;
2343 uint32_t bytes_written;
2345 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2346 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2347 else
2348 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2349 if (layout == NULL) {
2350 LOG_WARNING("No ft2232 layout specified'");
2351 return ERROR_JTAG_INIT_FAILED;
2354 for (int i = 0; 1; i++) {
2356 * "more indicates that there are more IDs to try, so we should
2357 * not print an error for an ID mismatch (but for anything
2358 * else, we should).
2360 * try_more indicates that the error code returned indicates an
2361 * ID mismatch (and nothing else) and that we should proceeed
2362 * with the next ID pair.
2364 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2365 int try_more = 0;
2367 #if BUILD_FT2232_FTD2XX == 1
2368 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2369 more, &try_more);
2370 #elif BUILD_FT2232_LIBFTDI == 1
2371 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2372 more, &try_more, layout->channel);
2373 #endif
2374 if (retval >= 0)
2375 break;
2376 if (!more || !try_more)
2377 return retval;
2380 ft2232_buffer_size = 0;
2381 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2383 if (layout->init() != ERROR_OK)
2384 return ERROR_JTAG_INIT_FAILED;
2386 if (ft2232_device_is_highspeed()) {
2387 #ifndef BUILD_FT2232_HIGHSPEED
2388 #if BUILD_FT2232_FTD2XX == 1
2389 LOG_WARNING(
2390 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2391 #elif BUILD_FT2232_LIBFTDI == 1
2392 LOG_WARNING(
2393 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2394 #endif
2395 #endif
2396 /* make sure the legacy mode is disabled */
2397 if (ftx232h_clk_divide_by_5(false) != ERROR_OK)
2398 return ERROR_JTAG_INIT_FAILED;
2401 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2402 retval = ft2232_write(buf, 1, &bytes_written);
2403 if (retval != ERROR_OK) {
2404 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2405 return ERROR_JTAG_INIT_FAILED;
2408 #if BUILD_FT2232_FTD2XX == 1
2409 return ft2232_purge_ftd2xx();
2410 #elif BUILD_FT2232_LIBFTDI == 1
2411 return ft2232_purge_libftdi();
2412 #endif
2414 return ERROR_OK;
2417 /** Updates defaults for DBUS signals: the four JTAG signals
2418 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2420 static inline void ftx232_dbus_init(void)
2422 low_output = 0x08;
2423 low_direction = 0x0b;
2426 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2427 * the four GPIOL signals. Initialization covers value and direction,
2428 * as customized for each layout.
2430 static int ftx232_dbus_write(void)
2432 enum reset_types jtag_reset_config = jtag_get_reset_config();
2433 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2434 low_direction &= ~nTRSTnOE; /* nTRST input */
2435 low_output &= ~nTRST; /* nTRST = 0 */
2436 } else {
2437 low_direction |= nTRSTnOE; /* nTRST output */
2438 low_output |= nTRST; /* nTRST = 1 */
2441 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2442 low_direction |= nSRSTnOE; /* nSRST output */
2443 low_output |= nSRST; /* nSRST = 1 */
2444 } else {
2445 low_direction &= ~nSRSTnOE; /* nSRST input */
2446 low_output &= ~nSRST; /* nSRST = 0 */
2449 /* initialize low byte for jtag */
2450 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2451 LOG_ERROR("couldn't initialize FT2232 DBUS");
2452 return ERROR_JTAG_INIT_FAILED;
2455 return ERROR_OK;
2458 static int usbjtag_init(void)
2461 * NOTE: This is now _specific_ to the "usbjtag" layout.
2462 * Don't try cram any more layouts into this.
2464 ftx232_dbus_init();
2466 nTRST = 0x10;
2467 nTRSTnOE = 0x10;
2468 nSRST = 0x40;
2469 nSRSTnOE = 0x40;
2471 return ftx232_dbus_write();
2474 static int lm3s811_jtag_init(void)
2476 ftx232_dbus_init();
2478 /* There are multiple revisions of LM3S811 eval boards:
2479 * - Rev B (and older?) boards have no SWO trace support.
2480 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2481 * they should use the "luminary_icdi" layout instead.
2483 nTRST = 0x0;
2484 nTRSTnOE = 0x00;
2485 nSRST = 0x20;
2486 nSRSTnOE = 0x20;
2487 low_output = 0x88;
2488 low_direction = 0x8b;
2490 return ftx232_dbus_write();
2493 static int icdi_jtag_init(void)
2495 ftx232_dbus_init();
2497 /* Most Luminary eval boards support SWO trace output,
2498 * and should use this "luminary_icdi" layout.
2500 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2501 * to switch between JTAG and SWD, or switch the ft2232 UART
2502 * on the second MPSSE channel/interface (BDBUS)
2503 * between (i) the stellaris UART (on Luminary boards)
2504 * or (ii) SWO trace data (generic).
2506 * We come up in JTAG mode and may switch to SWD later (with
2507 * SWO/trace option if SWD is active).
2509 * DBUS == GPIO-Lx
2510 * CBUS == GPIO-Hx
2514 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2515 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2516 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2519 /* GPIOs on second channel/interface (UART) ... */
2520 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2521 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2522 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2524 nTRST = 0x0;
2525 nTRSTnOE = 0x00;
2526 nSRST = ICDI_SRST;
2527 nSRSTnOE = ICDI_SRST;
2529 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2530 low_output |= ICDI_JTAG_EN;
2531 low_output &= ~ICDI_DBG_ENn;
2533 return ftx232_dbus_write();
2536 static int signalyzer_init(void)
2538 ftx232_dbus_init();
2540 nTRST = 0x10;
2541 nTRSTnOE = 0x10;
2542 nSRST = 0x20;
2543 nSRSTnOE = 0x20;
2544 return ftx232_dbus_write();
2547 static int axm0432_jtag_init(void)
2549 low_output = 0x08;
2550 low_direction = 0x2b;
2552 /* initialize low byte for jtag */
2553 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2554 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2555 return ERROR_JTAG_INIT_FAILED;
2558 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2559 nTRST = 0x08;
2560 nTRSTnOE = 0x0; /* No output enable for TRST*/
2561 nSRST = 0x04;
2562 nSRSTnOE = 0x0; /* No output enable for SRST*/
2563 } else {
2564 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2565 exit(-1);
2568 high_output = 0x0;
2569 high_direction = 0x0c;
2571 enum reset_types jtag_reset_config = jtag_get_reset_config();
2572 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2573 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2574 else
2575 high_output |= nTRST;
2577 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2578 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2579 else
2580 high_output |= nSRST;
2582 /* initialize high byte for jtag */
2583 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2584 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2585 return ERROR_JTAG_INIT_FAILED;
2588 return ERROR_OK;
2591 static int redbee_init(void)
2593 low_output = 0x08;
2594 low_direction = 0x2b;
2596 /* initialize low byte for jtag */
2597 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2598 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2599 return ERROR_JTAG_INIT_FAILED;
2602 nTRST = 0x08;
2603 nTRSTnOE = 0x0; /* No output enable for TRST*/
2604 nSRST = 0x04;
2605 nSRSTnOE = 0x0; /* No output enable for SRST*/
2607 high_output = 0x0;
2608 high_direction = 0x0c;
2610 enum reset_types jtag_reset_config = jtag_get_reset_config();
2611 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2612 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2613 else
2614 high_output |= nTRST;
2616 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2617 LOG_ERROR("can't set nSRST to push-pull on redbee");
2618 else
2619 high_output |= nSRST;
2621 /* initialize high byte for jtag */
2622 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2623 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2624 return ERROR_JTAG_INIT_FAILED;
2627 return ERROR_OK;
2630 static int jtagkey_init(void)
2632 low_output = 0x08;
2633 low_direction = 0x1b;
2635 /* initialize low byte for jtag */
2636 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2637 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2638 return ERROR_JTAG_INIT_FAILED;
2641 if (strcmp(layout->name, "jtagkey") == 0) {
2642 nTRST = 0x01;
2643 nTRSTnOE = 0x4;
2644 nSRST = 0x02;
2645 nSRSTnOE = 0x08;
2646 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2647 || (strcmp(layout->name, "oocdlink") == 0)) {
2648 nTRST = 0x02;
2649 nTRSTnOE = 0x1;
2650 nSRST = 0x08;
2651 nSRSTnOE = 0x04;
2652 } else {
2653 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2654 exit(-1);
2657 high_output = 0x0;
2658 high_direction = 0x0f;
2660 enum reset_types jtag_reset_config = jtag_get_reset_config();
2661 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2662 high_output |= nTRSTnOE;
2663 high_output &= ~nTRST;
2664 } else {
2665 high_output &= ~nTRSTnOE;
2666 high_output |= nTRST;
2669 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2670 high_output &= ~nSRSTnOE;
2671 high_output |= nSRST;
2672 } else {
2673 high_output |= nSRSTnOE;
2674 high_output &= ~nSRST;
2677 /* initialize high byte for jtag */
2678 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2679 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2680 return ERROR_JTAG_INIT_FAILED;
2683 return ERROR_OK;
2686 static int olimex_jtag_init(void)
2688 low_output = 0x08;
2689 low_direction = 0x1b;
2691 /* initialize low byte for jtag */
2692 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2693 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2694 return ERROR_JTAG_INIT_FAILED;
2697 nTRST = 0x01;
2698 nTRSTnOE = 0x4;
2699 nSRST = 0x02;
2700 nSRSTnOE = 0x00;/* no output enable for nSRST */
2702 high_output = 0x0;
2703 high_direction = 0x0f;
2705 enum reset_types jtag_reset_config = jtag_get_reset_config();
2706 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2707 high_output |= nTRSTnOE;
2708 high_output &= ~nTRST;
2709 } else {
2710 high_output &= ~nTRSTnOE;
2711 high_output |= nTRST;
2714 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2715 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2716 else
2717 high_output &= ~nSRST;
2719 /* turn red LED on */
2720 high_output |= 0x08;
2722 /* initialize high byte for jtag */
2723 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2724 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2725 return ERROR_JTAG_INIT_FAILED;
2728 return ERROR_OK;
2731 static int flyswatter_init(int rev)
2733 low_output = 0x18;
2734 low_direction = 0x7b;
2736 if ((rev < 0) || (rev > 3)) {
2737 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2738 return ERROR_JTAG_INIT_FAILED;
2741 if (rev == 1)
2742 low_direction |= 1 << 7;
2744 /* initialize low byte for jtag */
2745 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2746 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2747 return ERROR_JTAG_INIT_FAILED;
2750 nTRST = 0x10;
2751 nTRSTnOE = 0x0; /* not output enable for nTRST */
2752 nSRST = 0x20;
2753 nSRSTnOE = 0x00; /* no output enable for nSRST */
2755 high_output = 0x00;
2757 if (rev == 1)
2758 high_direction = 0x0c;
2759 else
2760 high_direction = 0x01;
2762 /* turn red LED3 on, LED2 off */
2763 high_output |= 0x08;
2765 /* initialize high byte for jtag */
2766 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2767 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2768 return ERROR_JTAG_INIT_FAILED;
2771 return ERROR_OK;
2774 static int flyswatter1_init(void)
2776 return flyswatter_init(1);
2779 static int flyswatter2_init(void)
2781 return flyswatter_init(2);
2784 static int minimodule_init(void)
2786 low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2787 * 0x18) */
2788 low_direction = 0xfb; /* 0xfb; */
2790 /* initialize low byte for jtag */
2791 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2792 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2793 return ERROR_JTAG_INIT_FAILED;
2797 nSRST = 0x20;
2799 high_output = 0x00;
2800 high_direction = 0x05;
2802 /* turn red LED3 on, LED2 off */
2803 /* high_output |= 0x08; */
2805 /* initialize high byte for jtag */
2806 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2807 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2808 return ERROR_JTAG_INIT_FAILED;
2811 return ERROR_OK;
2814 static int turtle_init(void)
2816 low_output = 0x08;
2817 low_direction = 0x5b;
2819 /* initialize low byte for jtag */
2820 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2821 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2822 return ERROR_JTAG_INIT_FAILED;
2825 nSRST = 0x40;
2827 high_output = 0x00;
2828 high_direction = 0x0C;
2830 /* initialize high byte for jtag */
2831 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2832 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2833 return ERROR_JTAG_INIT_FAILED;
2836 return ERROR_OK;
2839 static int comstick_init(void)
2841 low_output = 0x08;
2842 low_direction = 0x0b;
2844 /* initialize low byte for jtag */
2845 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2846 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2847 return ERROR_JTAG_INIT_FAILED;
2850 nTRST = 0x01;
2851 nTRSTnOE = 0x00; /* no output enable for nTRST */
2852 nSRST = 0x02;
2853 nSRSTnOE = 0x00; /* no output enable for nSRST */
2855 high_output = 0x03;
2856 high_direction = 0x03;
2858 /* initialize high byte for jtag */
2859 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2860 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2861 return ERROR_JTAG_INIT_FAILED;
2864 return ERROR_OK;
2867 static int stm32stick_init(void)
2869 low_output = 0x88;
2870 low_direction = 0x8b;
2872 /* initialize low byte for jtag */
2873 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2874 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2875 return ERROR_JTAG_INIT_FAILED;
2878 nTRST = 0x01;
2879 nTRSTnOE = 0x00; /* no output enable for nTRST */
2880 nSRST = 0x80;
2881 nSRSTnOE = 0x00; /* no output enable for nSRST */
2883 high_output = 0x01;
2884 high_direction = 0x03;
2886 /* initialize high byte for jtag */
2887 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2888 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2889 return ERROR_JTAG_INIT_FAILED;
2892 return ERROR_OK;
2895 static int sheevaplug_init(void)
2897 low_output = 0x08;
2898 low_direction = 0x1b;
2900 /* initialize low byte for jtag */
2901 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2902 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2903 return ERROR_JTAG_INIT_FAILED;
2906 nTRSTnOE = 0x1;
2907 nTRST = 0x02;
2908 nSRSTnOE = 0x4;
2909 nSRST = 0x08;
2911 high_output = 0x0;
2912 high_direction = 0x0f;
2914 /* nTRST is always push-pull */
2915 high_output &= ~nTRSTnOE;
2916 high_output |= nTRST;
2918 /* nSRST is always open-drain */
2919 high_output |= nSRSTnOE;
2920 high_output &= ~nSRST;
2922 /* initialize high byte for jtag */
2923 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2924 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2925 return ERROR_JTAG_INIT_FAILED;
2928 return ERROR_OK;
2931 static int cortino_jtag_init(void)
2933 low_output = 0x08;
2934 low_direction = 0x1b;
2936 /* initialize low byte for jtag */
2937 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2938 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2939 return ERROR_JTAG_INIT_FAILED;
2942 nTRST = 0x01;
2943 nTRSTnOE = 0x00; /* no output enable for nTRST */
2944 nSRST = 0x02;
2945 nSRSTnOE = 0x00; /* no output enable for nSRST */
2947 high_output = 0x03;
2948 high_direction = 0x03;
2950 /* initialize high byte for jtag */
2951 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2952 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2953 return ERROR_JTAG_INIT_FAILED;
2956 return ERROR_OK;
2959 static int lisa_l_init(void)
2961 ftx232_dbus_init();
2963 nTRST = 0x10;
2964 nTRSTnOE = 0x10;
2965 nSRST = 0x40;
2966 nSRSTnOE = 0x40;
2968 high_output = 0x00;
2969 high_direction = 0x18;
2971 /* initialize high byte for jtag */
2972 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2973 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2974 return ERROR_JTAG_INIT_FAILED;
2977 return ftx232_dbus_write();
2980 static int flossjtag_init(void)
2982 ftx232_dbus_init();
2984 nTRST = 0x10;
2985 nTRSTnOE = 0x10;
2986 nSRST = 0x40;
2987 nSRSTnOE = 0x40;
2989 high_output = 0x00;
2990 high_direction = 0x18;
2992 /* initialize high byte for jtag */
2993 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2994 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2995 return ERROR_JTAG_INIT_FAILED;
2998 return ftx232_dbus_write();
3002 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3003 * the door for a number of different configurations
3005 * Known Implementations:
3006 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3008 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3009 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3010 * * ACBUS3 to transition 0->1 (OE rising edge)
3011 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3012 * * ADBUS5/GPIOL1 = EMU_EN = 1
3013 * * ADBUS6/GPIOL2 = EMU0 = 0
3014 * * ACBUS4/SPARE0 = EMU1 = 0
3015 * * CPLD logic: Disable loopback
3016 * * ACBUS6/SPARE2 = LOOPBACK = 0
3018 #define XDS100_nEMU_EN (1<<5)
3019 #define XDS100_nEMU0 (1<<6)
3021 #define XDS100_PWR_RST (1<<3)
3022 #define XDS100_nEMU1 (1<<4)
3023 #define XDS100_LOOPBACK (1<<6)
3024 static int xds100v2_init(void)
3026 /* These are in the lower byte */
3027 nTRST = 0x10;
3028 nTRSTnOE = 0x10;
3030 /* These aren't actually used on 14 pin connectors
3031 * These are in the upper byte */
3032 nSRST = 0x01;
3033 nSRSTnOE = 0x01;
3035 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3036 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3038 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3039 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3040 return ERROR_JTAG_INIT_FAILED;
3043 high_output = 0;
3044 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3046 /* initialize high byte for jtag */
3047 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3048 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3049 return ERROR_JTAG_INIT_FAILED;
3052 high_output |= XDS100_PWR_RST;
3054 /* initialize high byte for jtag */
3055 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3056 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3057 return ERROR_JTAG_INIT_FAILED;
3060 return ERROR_OK;
3063 static void olimex_jtag_blink(void)
3065 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3066 * ACBUS3 is bit 3 of the GPIOH port
3068 high_output ^= 0x08;
3070 buffer_write(0x82);
3071 buffer_write(high_output);
3072 buffer_write(high_direction);
3075 static void flyswatter_jtag_blink(unsigned char led)
3077 buffer_write(0x82);
3078 buffer_write(high_output ^ led);
3079 buffer_write(high_direction);
3082 static void flyswatter1_jtag_blink(void)
3085 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3087 flyswatter_jtag_blink(0xc);
3090 static void flyswatter2_jtag_blink(void)
3093 * Flyswatter2 only has one LED connected to ACBUS2
3095 flyswatter_jtag_blink(0x4);
3098 static void turtle_jtag_blink(void)
3101 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3103 if (high_output & 0x08)
3104 high_output = 0x04;
3105 else
3106 high_output = 0x08;
3108 buffer_write(0x82);
3109 buffer_write(high_output);
3110 buffer_write(high_direction);
3113 static void lisa_l_blink(void)
3116 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3118 if (high_output & 0x10)
3119 high_output = 0x08;
3120 else
3121 high_output = 0x10;
3123 buffer_write(0x82);
3124 buffer_write(high_output);
3125 buffer_write(high_direction);
3128 static void flossjtag_blink(void)
3131 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3133 if (high_output & 0x10)
3134 high_output = 0x08;
3135 else
3136 high_output = 0x10;
3138 buffer_write(0x82);
3139 buffer_write(high_output);
3140 buffer_write(high_direction);
3143 static int ft2232_quit(void)
3145 #if BUILD_FT2232_FTD2XX == 1
3146 FT_STATUS status;
3148 status = FT_Close(ftdih);
3149 #elif BUILD_FT2232_LIBFTDI == 1
3150 ftdi_usb_close(&ftdic);
3152 ftdi_deinit(&ftdic);
3153 #endif
3155 free(ft2232_buffer);
3156 ft2232_buffer = NULL;
3158 return ERROR_OK;
3161 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3163 char *cp;
3164 char buf[200];
3165 if (CMD_ARGC == 1) {
3166 ft2232_device_desc = strdup(CMD_ARGV[0]);
3167 cp = strchr(ft2232_device_desc, 0);
3168 /* under Win32, the FTD2XX driver appends an "A" to the end
3169 * of the description, this examines the given desc
3170 * and creates the 'missing' _A or non_A variable. */
3171 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3172 /* it was, so make this the "A" version. */
3173 ft2232_device_desc_A = ft2232_device_desc;
3174 /* and *CREATE* the non-A version. */
3175 strcpy(buf, ft2232_device_desc);
3176 cp = strchr(buf, 0);
3177 cp[-2] = 0;
3178 ft2232_device_desc = strdup(buf);
3179 } else {
3180 /* <space > A not defined
3181 * so create it */
3182 sprintf(buf, "%s A", ft2232_device_desc);
3183 ft2232_device_desc_A = strdup(buf);
3185 } else
3186 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3188 return ERROR_OK;
3191 COMMAND_HANDLER(ft2232_handle_serial_command)
3193 if (CMD_ARGC == 1)
3194 ft2232_serial = strdup(CMD_ARGV[0]);
3195 else
3196 return ERROR_COMMAND_SYNTAX_ERROR;
3198 return ERROR_OK;
3201 COMMAND_HANDLER(ft2232_handle_layout_command)
3203 if (CMD_ARGC != 1)
3204 return ERROR_COMMAND_SYNTAX_ERROR;
3206 if (layout) {
3207 LOG_ERROR("already specified ft2232_layout %s",
3208 layout->name);
3209 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3210 ? ERROR_FAIL
3211 : ERROR_OK;
3214 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3215 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3216 layout = l;
3217 return ERROR_OK;
3221 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3222 return ERROR_FAIL;
3225 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3227 if (CMD_ARGC > MAX_USB_IDS * 2) {
3228 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3229 "(maximum is %d pairs)", MAX_USB_IDS);
3230 CMD_ARGC = MAX_USB_IDS * 2;
3232 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3233 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3234 if (CMD_ARGC < 2)
3235 return ERROR_COMMAND_SYNTAX_ERROR;
3236 /* remove the incomplete trailing id */
3237 CMD_ARGC -= 1;
3240 unsigned i;
3241 for (i = 0; i < CMD_ARGC; i += 2) {
3242 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3243 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3247 * Explicitly terminate, in case there are multiples instances of
3248 * ft2232_vid_pid.
3250 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3252 return ERROR_OK;
3255 COMMAND_HANDLER(ft2232_handle_latency_command)
3257 if (CMD_ARGC == 1)
3258 ft2232_latency = atoi(CMD_ARGV[0]);
3259 else
3260 return ERROR_COMMAND_SYNTAX_ERROR;
3262 return ERROR_OK;
3265 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3267 int retval = 0;
3269 /* 7 bits of either ones or zeros. */
3270 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3272 while (num_cycles > 0) {
3273 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3274 * at most 7 bits per invocation. Here we invoke it potentially
3275 * several times.
3277 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3279 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3280 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3281 retval = ERROR_JTAG_QUEUE_FAILED;
3283 first_unsent = cmd;
3286 /* there are no state transitions in this code, so omit state tracking */
3288 /* command "Clock Data to TMS/CS Pin (no Read)" */
3289 buffer_write(0x4b);
3291 /* scan 7 bit */
3292 buffer_write(bitcount_per_command - 1);
3294 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3295 buffer_write(tms);
3297 require_send = 1;
3299 num_cycles -= bitcount_per_command;
3302 return retval;
3305 /* ---------------------------------------------------------------------
3306 * Support for IceBear JTAG adapter from Section5:
3307 * http://section5.ch/icebear
3309 * Author: Sten, debian@sansys-electronic.com
3312 /* Icebear pin layout
3314 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3315 * GND GND | 4 3| n.c.
3316 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3317 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3318 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3319 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3320 * ADBUS2 TDO |14 13| GND GND
3322 * ADBUS0 O L TCK ACBUS0 GND
3323 * ADBUS1 O L TDI ACBUS1 GND
3324 * ADBUS2 I TDO ACBUS2 n.c.
3325 * ADBUS3 O H TMS ACBUS3 n.c.
3326 * ADBUS4 O H nTRST
3327 * ADBUS5 O H nSRST
3328 * ADBUS6 - VCC
3329 * ADBUS7 - GND
3331 static int icebear_jtag_init(void)
3333 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3334 low_output = 0x08; /* high: TMS; low: TCK TDI */
3335 nTRST = 0x10;
3336 nSRST = 0x20;
3338 enum reset_types jtag_reset_config = jtag_get_reset_config();
3339 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3340 low_direction &= ~nTRST; /* nTRST high impedance */
3341 else {
3342 low_direction |= nTRST;
3343 low_output |= nTRST;
3346 low_direction |= nSRST;
3347 low_output |= nSRST;
3349 /* initialize low byte for jtag */
3350 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3351 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3352 return ERROR_JTAG_INIT_FAILED;
3355 high_output = 0x0;
3356 high_direction = 0x00;
3358 /* initialize high byte for jtag */
3359 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
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;
3372 } else if (trst == 0) {
3373 enum reset_types jtag_reset_config = jtag_get_reset_config();
3374 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3375 low_direction &= ~nTRST;
3376 else
3377 low_output |= nTRST;
3380 if (srst == 1)
3381 low_output &= ~nSRST;
3382 else if (srst == 0)
3383 low_output |= nSRST;
3385 /* command "set data bits low byte" */
3386 buffer_write(0x80);
3387 buffer_write(low_output);
3388 buffer_write(low_direction);
3390 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3391 trst,
3392 srst,
3393 low_output,
3394 low_direction);
3397 /* ---------------------------------------------------------------------
3398 * Support for Signalyzer H2 and Signalyzer H4
3399 * JTAG adapter from Xverve Technologies Inc.
3400 * http://www.signalyzer.com or http://www.xverve.com
3402 * Author: Oleg Seiljus, oleg@signalyzer.com
3404 static unsigned char signalyzer_h_side;
3405 static unsigned int signalyzer_h_adapter_type;
3407 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3409 #if BUILD_FT2232_FTD2XX == 1
3410 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3411 #endif
3413 #define SIGNALYZER_COMMAND_ADDR 128
3414 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3416 #define SIGNALYZER_COMMAND_VERSION 0x41
3417 #define SIGNALYZER_COMMAND_RESET 0x42
3418 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3419 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3420 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3421 #define SIGNALYZER_COMMAND_LED_SET 0x53
3422 #define SIGNALYZER_COMMAND_ADC 0x54
3423 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3424 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3425 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3426 #define SIGNALYZER_COMMAND_I2C 0x58
3428 #define SIGNALYZER_CHAN_A 1
3429 #define SIGNALYZER_CHAN_B 2
3430 /* LEDS use channel C */
3431 #define SIGNALYZER_CHAN_C 4
3433 #define SIGNALYZER_LED_GREEN 1
3434 #define SIGNALYZER_LED_RED 2
3436 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3437 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3438 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3439 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3440 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3443 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3445 #if BUILD_FT2232_FTD2XX == 1
3446 return FT_WriteEE(ftdih, address, value);
3447 #elif BUILD_FT2232_LIBFTDI == 1
3448 return 0;
3449 #endif
3452 #if BUILD_FT2232_FTD2XX == 1
3453 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3455 return FT_ReadEE(ftdih, address, value);
3457 #endif
3459 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3460 int on_time_ms, int off_time_ms, unsigned char cycles)
3462 unsigned char on_time;
3463 unsigned char off_time;
3465 if (on_time_ms < 0xFFFF)
3466 on_time = (unsigned char)(on_time_ms / 62);
3467 else
3468 on_time = 0xFF;
3470 off_time = (unsigned char)(off_time_ms / 62);
3472 #if BUILD_FT2232_FTD2XX == 1
3473 FT_STATUS status;
3475 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3476 ((uint32_t)(channel << 8) | led));
3477 if (status != FT_OK) {
3478 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3479 ftd2xx_status_string(status));
3480 return ERROR_JTAG_DEVICE_ERROR;
3483 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3484 ((uint32_t)(on_time << 8) | off_time));
3485 if (status != FT_OK) {
3486 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3487 ftd2xx_status_string(status));
3488 return ERROR_JTAG_DEVICE_ERROR;
3491 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3492 ((uint32_t)cycles));
3493 if (status != FT_OK) {
3494 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3495 ftd2xx_status_string(status));
3496 return ERROR_JTAG_DEVICE_ERROR;
3499 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3500 SIGNALYZER_COMMAND_LED_SET);
3501 if (status != FT_OK) {
3502 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3503 ftd2xx_status_string(status));
3504 return ERROR_JTAG_DEVICE_ERROR;
3507 return ERROR_OK;
3508 #elif BUILD_FT2232_LIBFTDI == 1
3509 int retval;
3511 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3512 ((uint32_t)(channel << 8) | led));
3513 if (retval < 0) {
3514 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3515 ftdi_get_error_string(&ftdic));
3516 return ERROR_JTAG_DEVICE_ERROR;
3519 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3520 ((uint32_t)(on_time << 8) | off_time));
3521 if (retval < 0) {
3522 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3523 ftdi_get_error_string(&ftdic));
3524 return ERROR_JTAG_DEVICE_ERROR;
3527 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3528 (uint32_t)cycles);
3529 if (retval < 0) {
3530 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3531 ftdi_get_error_string(&ftdic));
3532 return ERROR_JTAG_DEVICE_ERROR;
3535 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3536 SIGNALYZER_COMMAND_LED_SET);
3537 if (retval < 0) {
3538 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3539 ftdi_get_error_string(&ftdic));
3540 return ERROR_JTAG_DEVICE_ERROR;
3543 return ERROR_OK;
3544 #endif
3547 static int signalyzer_h_init(void)
3549 #if BUILD_FT2232_FTD2XX == 1
3550 FT_STATUS status;
3551 int i;
3552 #endif
3554 char *end_of_desc;
3556 uint16_t read_buf[12] = { 0 };
3558 /* turn on center green led */
3559 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3560 0xFFFF, 0x00, 0x00);
3562 /* determine what channel config wants to open
3563 * TODO: change me... current implementation is made to work
3564 * with openocd description parsing.
3566 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3568 if (end_of_desc) {
3569 signalyzer_h_side = *(end_of_desc - 1);
3570 if (signalyzer_h_side == 'B')
3571 signalyzer_h_side = SIGNALYZER_CHAN_B;
3572 else
3573 signalyzer_h_side = SIGNALYZER_CHAN_A;
3574 } else {
3575 LOG_ERROR("No Channel was specified");
3576 return ERROR_FAIL;
3579 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3580 1000, 1000, 0xFF);
3582 #if BUILD_FT2232_FTD2XX == 1
3583 /* read signalyzer versionining information */
3584 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3585 SIGNALYZER_COMMAND_VERSION);
3586 if (status != FT_OK) {
3587 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3588 ftd2xx_status_string(status));
3589 return ERROR_JTAG_DEVICE_ERROR;
3592 for (i = 0; i < 10; i++) {
3593 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3594 &read_buf[i]);
3595 if (status != FT_OK) {
3596 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3597 ftd2xx_status_string(status));
3598 return ERROR_JTAG_DEVICE_ERROR;
3602 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3603 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3604 read_buf[4], read_buf[5], read_buf[6]);
3606 /* set gpio register */
3607 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3608 (uint32_t)(signalyzer_h_side << 8));
3609 if (status != FT_OK) {
3610 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3611 ftd2xx_status_string(status));
3612 return ERROR_JTAG_DEVICE_ERROR;
3615 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
3616 if (status != FT_OK) {
3617 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3618 ftd2xx_status_string(status));
3619 return ERROR_JTAG_DEVICE_ERROR;
3622 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3623 SIGNALYZER_COMMAND_GPIO_STATE);
3624 if (status != FT_OK) {
3625 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3626 ftd2xx_status_string(status));
3627 return ERROR_JTAG_DEVICE_ERROR;
3630 /* read adapter type information */
3631 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3632 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3633 if (status != FT_OK) {
3634 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3635 ftd2xx_status_string(status));
3636 return ERROR_JTAG_DEVICE_ERROR;
3639 status = signalyzer_h_ctrl_write(
3640 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3641 if (status != FT_OK) {
3642 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3643 ftd2xx_status_string(status));
3644 return ERROR_JTAG_DEVICE_ERROR;
3647 status = signalyzer_h_ctrl_write(
3648 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3649 if (status != FT_OK) {
3650 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3651 ftd2xx_status_string(status));
3652 return ERROR_JTAG_DEVICE_ERROR;
3655 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3656 SIGNALYZER_COMMAND_I2C);
3657 if (status != FT_OK) {
3658 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3659 ftd2xx_status_string(status));
3660 return ERROR_JTAG_DEVICE_ERROR;
3663 usleep(100000);
3665 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3666 if (status != FT_OK) {
3667 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3668 ftd2xx_status_string(status));
3669 return ERROR_JTAG_DEVICE_ERROR;
3672 if (read_buf[0] != 0x0498)
3673 signalyzer_h_adapter_type = 0x0000;
3674 else {
3675 for (i = 0; i < 4; i++) {
3676 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3677 if (status != FT_OK) {
3678 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3679 ftd2xx_status_string(status));
3680 return ERROR_JTAG_DEVICE_ERROR;
3684 signalyzer_h_adapter_type = read_buf[0];
3687 #elif BUILD_FT2232_LIBFTDI == 1
3688 /* currently libftdi does not allow reading individual eeprom
3689 * locations, therefore adapter type cannot be detected.
3690 * override with most common type
3692 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3693 #endif
3695 enum reset_types jtag_reset_config = jtag_get_reset_config();
3697 /* ADAPTOR: EM_LT16_A */
3698 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3699 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3700 "detected. (HW: %2x).", (read_buf[1] >> 8));
3702 nTRST = 0x10;
3703 nTRSTnOE = 0x10;
3704 nSRST = 0x20;
3705 nSRSTnOE = 0x20;
3707 low_output = 0x08;
3708 low_direction = 0x1b;
3710 high_output = 0x0;
3711 high_direction = 0x0;
3713 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3714 low_direction &= ~nTRSTnOE; /* nTRST input */
3715 low_output &= ~nTRST; /* nTRST = 0 */
3716 } else {
3717 low_direction |= nTRSTnOE; /* nTRST output */
3718 low_output |= nTRST; /* nTRST = 1 */
3721 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3722 low_direction |= nSRSTnOE; /* nSRST output */
3723 low_output |= nSRST; /* nSRST = 1 */
3724 } else {
3725 low_direction &= ~nSRSTnOE; /* nSRST input */
3726 low_output &= ~nSRST; /* nSRST = 0 */
3729 #if BUILD_FT2232_FTD2XX == 1
3730 /* enable power to the module */
3731 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3732 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3733 if (status != FT_OK) {
3734 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3735 ftd2xx_status_string(status));
3736 return ERROR_JTAG_DEVICE_ERROR;
3739 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3740 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3741 if (status != FT_OK) {
3742 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3743 ftd2xx_status_string(status));
3744 return ERROR_JTAG_DEVICE_ERROR;
3747 /* set gpio mode register */
3748 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3749 (uint32_t)(signalyzer_h_side << 8));
3750 if (status != FT_OK) {
3751 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3752 ftd2xx_status_string(status));
3753 return ERROR_JTAG_DEVICE_ERROR;
3756 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3757 if (status != FT_OK) {
3758 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3759 ftd2xx_status_string(status));
3760 return ERROR_JTAG_DEVICE_ERROR;
3763 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3764 if (status != FT_OK) {
3765 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3766 ftd2xx_status_string(status));
3767 return ERROR_JTAG_DEVICE_ERROR;
3770 /* set gpio register */
3771 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3772 (uint32_t)(signalyzer_h_side << 8));
3773 if (status != FT_OK) {
3774 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3775 ftd2xx_status_string(status));
3776 return ERROR_JTAG_DEVICE_ERROR;
3779 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040);
3780 if (status != FT_OK) {
3781 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3782 ftd2xx_status_string(status));
3783 return ERROR_JTAG_DEVICE_ERROR;
3786 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3787 SIGNALYZER_COMMAND_GPIO_STATE);
3788 if (status != FT_OK) {
3789 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3790 ftd2xx_status_string(status));
3791 return ERROR_JTAG_DEVICE_ERROR;
3793 #endif
3795 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3796 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3797 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3798 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3799 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3800 if (signalyzer_h_adapter_type
3801 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3802 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3803 "detected. (HW: %2x).", (read_buf[1] >> 8));
3804 else if (signalyzer_h_adapter_type
3805 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3806 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3807 "(ARM JTAG with PSU) detected. (HW: %2x).",
3808 (read_buf[1] >> 8));
3809 else if (signalyzer_h_adapter_type
3810 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3811 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3812 "detected. (HW: %2x).", (read_buf[1] >> 8));
3813 else if (signalyzer_h_adapter_type
3814 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3815 LOG_INFO("Signalyzer: EM-JTAG-P "
3816 "(Generic JTAG with PSU) detected. (HW: %2x).",
3817 (read_buf[1] >> 8));
3819 nTRST = 0x02;
3820 nTRSTnOE = 0x04;
3821 nSRST = 0x08;
3822 nSRSTnOE = 0x10;
3824 low_output = 0x08;
3825 low_direction = 0x1b;
3827 high_output = 0x0;
3828 high_direction = 0x1f;
3830 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3831 high_output |= nTRSTnOE;
3832 high_output &= ~nTRST;
3833 } else {
3834 high_output &= ~nTRSTnOE;
3835 high_output |= nTRST;
3838 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3839 high_output &= ~nSRSTnOE;
3840 high_output |= nSRST;
3841 } else {
3842 high_output |= nSRSTnOE;
3843 high_output &= ~nSRST;
3846 #if BUILD_FT2232_FTD2XX == 1
3847 /* enable power to the module */
3848 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3849 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3850 if (status != FT_OK) {
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3852 ftd2xx_status_string(status));
3853 return ERROR_JTAG_DEVICE_ERROR;
3856 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3857 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3858 if (status != FT_OK) {
3859 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3860 ftd2xx_status_string(status));
3861 return ERROR_JTAG_DEVICE_ERROR;
3864 /* set gpio mode register (IO_16 and IO_17 set as analog
3865 * inputs, other is gpio)
3867 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3868 (uint32_t)(signalyzer_h_side << 8));
3869 if (status != FT_OK) {
3870 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3871 ftd2xx_status_string(status));
3872 return ERROR_JTAG_DEVICE_ERROR;
3875 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060);
3876 if (status != FT_OK) {
3877 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3878 ftd2xx_status_string(status));
3879 return ERROR_JTAG_DEVICE_ERROR;
3882 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3883 if (status != FT_OK) {
3884 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3885 ftd2xx_status_string(status));
3886 return ERROR_JTAG_DEVICE_ERROR;
3889 /* set gpio register (all inputs, for -P modules,
3890 * PSU will be turned off)
3892 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3893 (uint32_t)(signalyzer_h_side << 8));
3894 if (status != FT_OK) {
3895 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3896 ftd2xx_status_string(status));
3897 return ERROR_JTAG_DEVICE_ERROR;
3900 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3901 if (status != FT_OK) {
3902 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3903 ftd2xx_status_string(status));
3904 return ERROR_JTAG_DEVICE_ERROR;
3907 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
3908 if (status != FT_OK) {
3909 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3910 ftd2xx_status_string(status));
3911 return ERROR_JTAG_DEVICE_ERROR;
3913 #endif
3914 } else if (signalyzer_h_adapter_type == 0x0000) {
3915 LOG_INFO("Signalyzer: No external modules were detected.");
3917 nTRST = 0x10;
3918 nTRSTnOE = 0x10;
3919 nSRST = 0x20;
3920 nSRSTnOE = 0x20;
3922 low_output = 0x08;
3923 low_direction = 0x1b;
3925 high_output = 0x0;
3926 high_direction = 0x0;
3928 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3929 low_direction &= ~nTRSTnOE; /* nTRST input */
3930 low_output &= ~nTRST; /* nTRST = 0 */
3931 } else {
3932 low_direction |= nTRSTnOE; /* nTRST output */
3933 low_output |= nTRST; /* nTRST = 1 */
3936 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3937 low_direction |= nSRSTnOE; /* nSRST output */
3938 low_output |= nSRST; /* nSRST = 1 */
3939 } else {
3940 low_direction &= ~nSRSTnOE; /* nSRST input */
3941 low_output &= ~nSRST; /* nSRST = 0 */
3943 } else {
3944 LOG_ERROR("Unknown module type is detected: %.4x",
3945 signalyzer_h_adapter_type);
3946 return ERROR_JTAG_DEVICE_ERROR;
3949 /* initialize low byte of controller for jtag operation */
3950 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3951 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3952 return ERROR_JTAG_INIT_FAILED;
3955 #if BUILD_FT2232_FTD2XX == 1
3956 if (ftdi_device == FT_DEVICE_2232H) {
3957 /* initialize high byte of controller for jtag operation */
3958 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3959 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3960 return ERROR_JTAG_INIT_FAILED;
3963 #elif BUILD_FT2232_LIBFTDI == 1
3964 if (ftdi_device == TYPE_2232H) {
3965 /* initialize high byte of controller for jtag operation */
3966 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3967 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3968 return ERROR_JTAG_INIT_FAILED;
3971 #endif
3972 return ERROR_OK;
3975 static void signalyzer_h_reset(int trst, int srst)
3977 enum reset_types jtag_reset_config = jtag_get_reset_config();
3979 /* ADAPTOR: EM_LT16_A */
3980 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3981 if (trst == 1) {
3982 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3983 /* switch to output pin (output is low) */
3984 low_direction |= nTRSTnOE;
3985 else
3986 /* switch output low */
3987 low_output &= ~nTRST;
3988 } else if (trst == 0) {
3989 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3990 /* switch to input pin (high-Z + internal
3991 * and external pullup) */
3992 low_direction &= ~nTRSTnOE;
3993 else
3994 /* switch output high */
3995 low_output |= nTRST;
3998 if (srst == 1) {
3999 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4000 /* switch output low */
4001 low_output &= ~nSRST;
4002 else
4003 /* switch to output pin (output is low) */
4004 low_direction |= nSRSTnOE;
4005 } else if (srst == 0) {
4006 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4007 /* switch output high */
4008 low_output |= nSRST;
4009 else
4010 /* switch to input pin (high-Z) */
4011 low_direction &= ~nSRSTnOE;
4014 /* command "set data bits low byte" */
4015 buffer_write(0x80);
4016 buffer_write(low_output);
4017 buffer_write(low_direction);
4018 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4019 "low_direction: 0x%2.2x",
4020 trst, srst, low_output, low_direction);
4022 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4023 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4024 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4025 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4026 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4027 if (trst == 1) {
4028 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4029 high_output &= ~nTRSTnOE;
4030 else
4031 high_output &= ~nTRST;
4032 } else if (trst == 0) {
4033 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4034 high_output |= nTRSTnOE;
4035 else
4036 high_output |= nTRST;
4039 if (srst == 1) {
4040 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4041 high_output &= ~nSRST;
4042 else
4043 high_output &= ~nSRSTnOE;
4044 } else if (srst == 0) {
4045 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4046 high_output |= nSRST;
4047 else
4048 high_output |= nSRSTnOE;
4051 /* command "set data bits high byte" */
4052 buffer_write(0x82);
4053 buffer_write(high_output);
4054 buffer_write(high_direction);
4055 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4056 "high_direction: 0x%2.2x",
4057 trst, srst, high_output, high_direction);
4058 } else if (signalyzer_h_adapter_type == 0x0000) {
4059 if (trst == 1) {
4060 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4061 /* switch to output pin (output is low) */
4062 low_direction |= nTRSTnOE;
4063 else
4064 /* switch output low */
4065 low_output &= ~nTRST;
4066 } else if (trst == 0) {
4067 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4068 /* switch to input pin (high-Z + internal
4069 * and external pullup) */
4070 low_direction &= ~nTRSTnOE;
4071 else
4072 /* switch output high */
4073 low_output |= nTRST;
4076 if (srst == 1) {
4077 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4078 /* switch output low */
4079 low_output &= ~nSRST;
4080 else
4081 /* switch to output pin (output is low) */
4082 low_direction |= nSRSTnOE;
4083 } else if (srst == 0) {
4084 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4085 /* switch output high */
4086 low_output |= nSRST;
4087 else
4088 /* switch to input pin (high-Z) */
4089 low_direction &= ~nSRSTnOE;
4092 /* command "set data bits low byte" */
4093 buffer_write(0x80);
4094 buffer_write(low_output);
4095 buffer_write(low_direction);
4096 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4097 "low_direction: 0x%2.2x",
4098 trst, srst, low_output, low_direction);
4102 static void signalyzer_h_blink(void)
4104 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4107 /********************************************************************
4108 * Support for KT-LINK
4109 * JTAG adapter from KRISTECH
4110 * http://www.kristech.eu
4111 *******************************************************************/
4112 static int ktlink_init(void)
4114 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4116 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4117 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4119 /* initialize low byte for jtag */
4120 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4121 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4122 return ERROR_JTAG_INIT_FAILED;
4125 nTRST = 0x01;
4126 nSRST = 0x02;
4127 nTRSTnOE = 0x04;
4128 nSRSTnOE = 0x08;
4130 high_output = 0x80; /* turn LED on */
4131 high_direction = 0xFF; /* all outputs */
4133 enum reset_types jtag_reset_config = jtag_get_reset_config();
4135 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4136 high_output |= nTRSTnOE;
4137 high_output &= ~nTRST;
4138 } else {
4139 high_output &= ~nTRSTnOE;
4140 high_output |= nTRST;
4143 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4144 high_output &= ~nSRSTnOE;
4145 high_output |= nSRST;
4146 } else {
4147 high_output |= nSRSTnOE;
4148 high_output &= ~nSRST;
4151 /* initialize high byte for jtag */
4152 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4153 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4154 return ERROR_JTAG_INIT_FAILED;
4157 return ERROR_OK;
4160 static void ktlink_reset(int trst, int srst)
4162 enum reset_types jtag_reset_config = jtag_get_reset_config();
4164 if (trst == 1) {
4165 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4166 high_output &= ~nTRSTnOE;
4167 else
4168 high_output &= ~nTRST;
4169 } else if (trst == 0) {
4170 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4171 high_output |= nTRSTnOE;
4172 else
4173 high_output |= nTRST;
4176 if (srst == 1) {
4177 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4178 high_output &= ~nSRST;
4179 else
4180 high_output &= ~nSRSTnOE;
4181 } else if (srst == 0) {
4182 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4183 high_output |= nSRST;
4184 else
4185 high_output |= nSRSTnOE;
4188 buffer_write(0x82); /* command "set data bits high byte" */
4189 buffer_write(high_output);
4190 buffer_write(high_direction);
4191 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4192 trst,
4193 srst,
4194 high_output,
4195 high_direction);
4198 static void ktlink_blink(void)
4200 /* LED connected to ACBUS7 */
4201 high_output ^= 0x80;
4203 buffer_write(0x82); /* command "set data bits high byte" */
4204 buffer_write(high_output);
4205 buffer_write(high_direction);
4208 /********************************************************************
4209 * Support for Digilent HS-1
4210 * JTAG adapter from Digilent
4211 * http://www.digilent.com
4212 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4213 *******************************************************************/
4215 static int digilent_hs1_init(void)
4217 /* the adapter only supports the base JTAG signals, no nTRST
4218 nor nSRST */
4219 low_output = 0x88;
4220 low_direction = 0x8b;
4222 /* initialize low byte for jtag */
4223 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4224 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4225 return ERROR_JTAG_INIT_FAILED;
4227 return ERROR_OK;
4230 static void digilent_hs1_reset(int trst, int srst)
4232 /* Dummy function, no reset signals supported. */
4235 static const struct command_registration ft2232_command_handlers[] = {
4237 .name = "ft2232_device_desc",
4238 .handler = &ft2232_handle_device_desc_command,
4239 .mode = COMMAND_CONFIG,
4240 .help = "set the USB device description of the FTDI FT2232 device",
4241 .usage = "description_string",
4244 .name = "ft2232_serial",
4245 .handler = &ft2232_handle_serial_command,
4246 .mode = COMMAND_CONFIG,
4247 .help = "set the serial number of the FTDI FT2232 device",
4248 .usage = "serial_string",
4251 .name = "ft2232_layout",
4252 .handler = &ft2232_handle_layout_command,
4253 .mode = COMMAND_CONFIG,
4254 .help = "set the layout of the FT2232 GPIO signals used "
4255 "to control output-enables and reset signals",
4256 .usage = "layout_name",
4259 .name = "ft2232_vid_pid",
4260 .handler = &ft2232_handle_vid_pid_command,
4261 .mode = COMMAND_CONFIG,
4262 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4263 .usage = "(vid pid)* ",
4266 .name = "ft2232_latency",
4267 .handler = &ft2232_handle_latency_command,
4268 .mode = COMMAND_CONFIG,
4269 .help = "set the FT2232 latency timer to a new value",
4270 .usage = "value",
4272 COMMAND_REGISTRATION_DONE
4275 struct jtag_interface ft2232_interface = {
4276 .name = "ft2232",
4277 .supported = DEBUG_CAP_TMS_SEQ,
4278 .commands = ft2232_command_handlers,
4279 .transports = jtag_only,
4281 .init = ft2232_init,
4282 .quit = ft2232_quit,
4283 .speed = ft2232_speed,
4284 .speed_div = ft2232_speed_div,
4285 .khz = ft2232_khz,
4286 .execute_queue = ft2232_execute_queue,