ft2232: Support for Digilent HS1 USB adapter
[openocd/cederom.git] / src / jtag / drivers / ft2232.c
blobe6a7363fc20f28742b0a15b1afcf4ea7c702c4d8
1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
7 * *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
30 /**
31 * @file
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
66 * found here:
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <transport/transport.h>
85 #include <helper/time_support.h>
87 #if IS_CYGWIN == 1
88 #include <windows.h>
89 #endif
91 #include <assert.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
97 #endif
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
101 #include <ftd2xx.h>
102 #include "ftd2xx_common.h"
104 enum ftdi_interface {
105 INTERFACE_ANY = 0,
106 INTERFACE_A = 1,
107 INTERFACE_B = 2,
108 INTERFACE_C = 3,
109 INTERFACE_D = 4
112 #elif BUILD_FT2232_LIBFTDI == 1
113 #include <ftdi.h>
114 #endif
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
126 * - Dimitar
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
130 * to something sane.
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
139 #endif
140 #endif
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
154 static char *ft2232_device_desc_A;
155 static char *ft2232_device_desc;
156 static char *ft2232_serial;
157 static uint8_t ft2232_latency = 2;
158 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165 struct ft2232_layout {
166 char *name;
167 int (*init)(void);
168 void (*reset)(int trst, int srst);
169 void (*blink)(void);
170 int channel;
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter1_init(void);
180 static int flyswatter2_init(void);
181 static int minimodule_init(void);
182 static int turtle_init(void);
183 static int comstick_init(void);
184 static int stm32stick_init(void);
185 static int axm0432_jtag_init(void);
186 static int sheevaplug_init(void);
187 static int icebear_jtag_init(void);
188 static int cortino_jtag_init(void);
189 static int signalyzer_init(void);
190 static int signalyzer_h_init(void);
191 static int ktlink_init(void);
192 static int redbee_init(void);
193 static int lisa_l_init(void);
194 static int flossjtag_init(void);
195 static int xds100v2_init(void);
196 static int digilent_hs1_init(void);
198 /* reset procedures for supported layouts */
199 static void ftx23_reset(int trst, int srst);
200 static void jtagkey_reset(int trst, int srst);
201 static void olimex_jtag_reset(int trst, int srst);
202 static void flyswatter1_reset(int trst, int srst);
203 static void flyswatter2_reset(int trst, int srst);
204 static void minimodule_reset(int trst, int srst);
205 static void turtle_reset(int trst, int srst);
206 static void comstick_reset(int trst, int srst);
207 static void stm32stick_reset(int trst, int srst);
208 static void axm0432_jtag_reset(int trst, int srst);
209 static void sheevaplug_reset(int trst, int srst);
210 static void icebear_jtag_reset(int trst, int srst);
211 static void signalyzer_h_reset(int trst, int srst);
212 static void ktlink_reset(int trst, int srst);
213 static void redbee_reset(int trst, int srst);
214 static void xds100v2_reset(int trst, int srst);
215 static void digilent_hs1_reset(int trst, int srst);
217 /* blink procedures for layouts that support a blinking led */
218 static void olimex_jtag_blink(void);
219 static void flyswatter1_jtag_blink(void);
220 static void flyswatter2_jtag_blink(void);
221 static void turtle_jtag_blink(void);
222 static void signalyzer_h_blink(void);
223 static void ktlink_blink(void);
224 static void lisa_l_blink(void);
225 static void flossjtag_blink(void);
227 /* common transport support options */
229 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
231 static const struct ft2232_layout ft2232_layouts[] = {
232 { .name = "usbjtag",
233 .init = usbjtag_init,
234 .reset = ftx23_reset,
236 { .name = "jtagkey",
237 .init = jtagkey_init,
238 .reset = jtagkey_reset,
240 { .name = "jtagkey_prototype_v1",
241 .init = jtagkey_init,
242 .reset = jtagkey_reset,
244 { .name = "oocdlink",
245 .init = jtagkey_init,
246 .reset = jtagkey_reset,
248 { .name = "signalyzer",
249 .init = signalyzer_init,
250 .reset = ftx23_reset,
252 { .name = "evb_lm3s811",
253 .init = lm3s811_jtag_init,
254 .reset = ftx23_reset,
256 { .name = "luminary_icdi",
257 .init = icdi_jtag_init,
258 .reset = ftx23_reset,
260 { .name = "olimex-jtag",
261 .init = olimex_jtag_init,
262 .reset = olimex_jtag_reset,
263 .blink = olimex_jtag_blink
265 { .name = "flyswatter",
266 .init = flyswatter1_init,
267 .reset = flyswatter1_reset,
268 .blink = flyswatter1_jtag_blink
270 { .name = "flyswatter2",
271 .init = flyswatter2_init,
272 .reset = flyswatter2_reset,
273 .blink = flyswatter2_jtag_blink
275 { .name = "minimodule",
276 .init = minimodule_init,
277 .reset = minimodule_reset,
279 { .name = "turtelizer2",
280 .init = turtle_init,
281 .reset = turtle_reset,
282 .blink = turtle_jtag_blink
284 { .name = "comstick",
285 .init = comstick_init,
286 .reset = comstick_reset,
288 { .name = "stm32stick",
289 .init = stm32stick_init,
290 .reset = stm32stick_reset,
292 { .name = "axm0432_jtag",
293 .init = axm0432_jtag_init,
294 .reset = axm0432_jtag_reset,
296 { .name = "sheevaplug",
297 .init = sheevaplug_init,
298 .reset = sheevaplug_reset,
300 { .name = "icebear",
301 .init = icebear_jtag_init,
302 .reset = icebear_jtag_reset,
304 { .name = "cortino",
305 .init = cortino_jtag_init,
306 .reset = comstick_reset,
308 { .name = "signalyzer-h",
309 .init = signalyzer_h_init,
310 .reset = signalyzer_h_reset,
311 .blink = signalyzer_h_blink
313 { .name = "ktlink",
314 .init = ktlink_init,
315 .reset = ktlink_reset,
316 .blink = ktlink_blink
318 { .name = "redbee-econotag",
319 .init = redbee_init,
320 .reset = redbee_reset,
322 { .name = "redbee-usb",
323 .init = redbee_init,
324 .reset = redbee_reset,
325 .channel = INTERFACE_B,
327 { .name = "lisa-l",
328 .init = lisa_l_init,
329 .reset = ftx23_reset,
330 .blink = lisa_l_blink,
331 .channel = INTERFACE_B,
333 { .name = "flossjtag",
334 .init = flossjtag_init,
335 .reset = ftx23_reset,
336 .blink = flossjtag_blink,
338 { .name = "xds100v2",
339 .init = xds100v2_init,
340 .reset = xds100v2_reset,
342 { .name = "digilent-hs1",
343 .init = digilent_hs1_init,
344 .reset = digilent_hs1_reset,
345 .channel = INTERFACE_A,
347 { .name = NULL, /* END OF TABLE */ },
350 /* bitmask used to drive nTRST; usually a GPIOLx signal */
351 static uint8_t nTRST;
352 static uint8_t nTRSTnOE;
353 /* bitmask used to drive nSRST; usually a GPIOLx signal */
354 static uint8_t nSRST;
355 static uint8_t nSRSTnOE;
357 /** the layout being used with this debug session */
358 static const struct ft2232_layout *layout;
360 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
361 static uint8_t low_output;
363 /* note that direction bit == 1 means that signal is an output */
365 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
366 static uint8_t low_direction;
367 /** default value bitmask for CBUS GPIOH(0..4) */
368 static uint8_t high_output;
369 /** default direction bitmask for CBUS GPIOH(0..4) */
370 static uint8_t high_direction;
372 #if BUILD_FT2232_FTD2XX == 1
373 static FT_HANDLE ftdih;
374 static FT_DEVICE ftdi_device;
375 #elif BUILD_FT2232_LIBFTDI == 1
376 static struct ftdi_context ftdic;
377 static enum ftdi_chip_type ftdi_device;
378 #endif
380 static struct jtag_command *first_unsent; /* next command that has to be sent */
381 static int require_send;
383 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
385 "There is a significant difference between libftdi and libftd2xx. The latter
386 one allows to schedule up to 64*64 bytes of result data while libftdi fails
387 with more than 4*64. As a consequence, the FT2232 driver is forced to
388 perform around 16x more USB transactions for long command streams with TDO
389 capture when running with libftdi."
391 No idea how we get
392 #define FT2232_BUFFER_SIZE 131072
393 a comment would have been nice.
396 #if BUILD_FT2232_FTD2XX == 1
397 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
398 #else
399 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
400 #endif
402 #define FT2232_BUFFER_SIZE 131072
404 static uint8_t *ft2232_buffer;
405 static int ft2232_buffer_size;
406 static int ft2232_read_pointer;
407 static int ft2232_expect_read;
410 * Function buffer_write
411 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
412 * @param val is the byte to send.
414 static inline void buffer_write(uint8_t val)
416 assert(ft2232_buffer);
417 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
418 ft2232_buffer[ft2232_buffer_size++] = val;
422 * Function buffer_read
423 * returns a byte from the byte buffer.
425 static inline uint8_t buffer_read(void)
427 assert(ft2232_buffer);
428 assert(ft2232_read_pointer < ft2232_buffer_size);
429 return ft2232_buffer[ft2232_read_pointer++];
433 * Clocks out \a bit_count bits on the TMS line, starting with the least
434 * significant bit of tms_bits and progressing to more significant bits.
435 * Rigorous state transition logging is done here via tap_set_state().
437 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
438 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
439 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
440 * is often used for this, 0x4b.
442 * @param tms_bits Holds the sequence of bits to send.
443 * @param tms_count Tells how many bits in the sequence.
444 * @param tdi_bit A single bit to pass on to TDI before the first TCK
445 * cycle and held static for the duration of TMS clocking.
447 * See the MPSSE spec referenced above.
449 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
451 uint8_t tms_byte;
452 int i;
453 int tms_ndx; /* bit index into tms_byte */
455 assert(tms_count > 0);
457 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
458 mpsse_cmd, tms_bits, tms_count);
460 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
461 bool bit = tms_bits & 1;
463 if (bit)
464 tms_byte |= (1 << tms_ndx);
466 /* always do state transitions in public view */
467 tap_set_state(tap_state_transition(tap_get_state(), bit));
469 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
470 * also increment.
472 ++tms_ndx;
474 if (tms_ndx == 7 || i == tms_count-1) {
475 buffer_write(mpsse_cmd);
476 buffer_write(tms_ndx - 1);
478 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
479 * TMS/CS and is held static for the duration of TMS/CS clocking.
481 buffer_write(tms_byte | (tdi_bit << 7));
487 * Function get_tms_buffer_requirements
488 * returns what clock_tms() will consume if called with
489 * same \a bit_count.
491 static inline int get_tms_buffer_requirements(int bit_count)
493 return ((bit_count + 6)/7) * 3;
497 * Function move_to_state
498 * moves the TAP controller from the current state to a
499 * \a goal_state through a path given by tap_get_tms_path(). State transition
500 * logging is performed by delegation to clock_tms().
502 * @param goal_state is the destination state for the move.
504 static void move_to_state(tap_state_t goal_state)
506 tap_state_t start_state = tap_get_state();
508 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
509 * lookup of the required TMS pattern to move to this state from the start state.
512 /* do the 2 lookups */
513 int tms_bits = tap_get_tms_path(start_state, goal_state);
514 int tms_count = tap_get_tms_path_len(start_state, goal_state);
516 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
518 clock_tms(0x4b, tms_bits, tms_count, 0);
521 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
523 #if BUILD_FT2232_FTD2XX == 1
524 FT_STATUS status;
525 DWORD dw_bytes_written = 0;
526 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
527 if (status != FT_OK) {
528 *bytes_written = dw_bytes_written;
529 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
530 return ERROR_JTAG_DEVICE_ERROR;
531 } else
532 *bytes_written = dw_bytes_written;
534 #elif BUILD_FT2232_LIBFTDI == 1
535 int retval = ftdi_write_data(&ftdic, buf, size);
536 if (retval < 0) {
537 *bytes_written = 0;
538 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
539 return ERROR_JTAG_DEVICE_ERROR;
540 } else
541 *bytes_written = retval;
543 #endif
545 if (*bytes_written != (uint32_t)size)
546 return ERROR_JTAG_DEVICE_ERROR;
548 return ERROR_OK;
551 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
553 #if BUILD_FT2232_FTD2XX == 1
554 DWORD dw_bytes_read;
555 FT_STATUS status;
556 int timeout = 5;
557 *bytes_read = 0;
559 while ((*bytes_read < size) && timeout--) {
560 status = FT_Read(ftdih, buf + *bytes_read, size -
561 *bytes_read, &dw_bytes_read);
562 if (status != FT_OK) {
563 *bytes_read = 0;
564 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
565 return ERROR_JTAG_DEVICE_ERROR;
567 *bytes_read += dw_bytes_read;
570 #elif BUILD_FT2232_LIBFTDI == 1
571 int retval;
572 int timeout = LIBFTDI_READ_RETRY_COUNT;
573 *bytes_read = 0;
575 while ((*bytes_read < size) && timeout--) {
576 retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
577 if (retval < 0) {
578 *bytes_read = 0;
579 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
580 return ERROR_JTAG_DEVICE_ERROR;
582 *bytes_read += retval;
585 #endif
587 if (*bytes_read < size) {
588 LOG_ERROR("couldn't read enough bytes from "
589 "FT2232 device (%i < %i)",
590 (unsigned)*bytes_read,
591 (unsigned)size);
592 return ERROR_JTAG_DEVICE_ERROR;
595 return ERROR_OK;
598 static bool ft2232_device_is_highspeed(void)
600 #if BUILD_FT2232_FTD2XX == 1
601 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
602 #elif BUILD_FT2232_LIBFTDI == 1
603 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
604 #endif
608 * Commands that only apply to the FT2232H and FT4232H devices.
609 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
610 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
613 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
615 uint8_t buf = enable ? 0x96 : 0x97;
616 LOG_DEBUG("%2.2x", buf);
618 uint32_t bytes_written;
619 int retval;
621 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
622 if (retval != ERROR_OK) {
623 LOG_ERROR("couldn't write command to %s adaptive clocking"
624 , enable ? "enable" : "disable");
625 return retval;
628 return ERROR_OK;
632 * Enable/disable the clk divide by 5 of the 60MHz master clock.
633 * This result in a JTAG clock speed range of 91.553Hz-6MHz
634 * respective 457.763Hz-30MHz.
636 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
638 uint32_t bytes_written;
639 uint8_t buf = enable ? 0x8b : 0x8a;
641 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
642 LOG_ERROR("couldn't write command to %s clk divide by 5"
643 , enable ? "enable" : "disable");
644 return ERROR_JTAG_INIT_FAILED;
646 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
647 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
649 return ERROR_OK;
652 static int ft2232_speed(int speed)
654 uint8_t buf[3];
655 int retval;
656 uint32_t bytes_written;
658 retval = ERROR_OK;
659 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
660 if (ft2232_device_is_highspeed())
661 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
662 else if (enable_adaptive_clocking) {
663 LOG_ERROR("ft2232 device %lu does not support RTCK"
664 , (long unsigned int)ftdi_device);
665 return ERROR_FAIL;
668 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
669 return retval;
671 buf[0] = 0x86; /* command "set divisor" */
672 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
673 buf[2] = (speed >> 8) & 0xff; /* valueH */
675 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
676 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
677 if (retval != ERROR_OK) {
678 LOG_ERROR("couldn't set FT2232 TCK speed");
679 return retval;
682 return ERROR_OK;
685 static int ft2232_speed_div(int speed, int *khz)
687 /* Take a look in the FT2232 manual,
688 * AN2232C-01 Command Processor for
689 * MPSSE and MCU Host Bus. Chapter 3.8 */
691 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
693 return ERROR_OK;
696 static int ft2232_khz(int khz, int *jtag_speed)
698 if (khz == 0) {
699 if (ft2232_device_is_highspeed()) {
700 *jtag_speed = RTCK_SPEED;
701 return ERROR_OK;
702 } else {
703 LOG_DEBUG("RCLK not supported");
704 return ERROR_FAIL;
708 /* Take a look in the FT2232 manual,
709 * AN2232C-01 Command Processor for
710 * MPSSE and MCU Host Bus. Chapter 3.8
712 * We will calc here with a multiplier
713 * of 10 for better rounding later. */
715 /* Calc speed, (ft2232_max_tck / khz) - 1
716 * Use 65000 for better rounding */
717 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
719 /* Add 0.9 for rounding */
720 *jtag_speed += 9;
722 /* Calc real speed */
723 *jtag_speed = *jtag_speed / 10;
725 /* Check if speed is greater than 0 */
726 if (*jtag_speed < 0)
727 *jtag_speed = 0;
729 /* Check max value */
730 if (*jtag_speed > 0xFFFF)
731 *jtag_speed = 0xFFFF;
733 return ERROR_OK;
736 static void ft2232_end_state(tap_state_t state)
738 if (tap_is_state_stable(state))
739 tap_set_end_state(state);
740 else {
741 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
742 exit(-1);
746 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
748 int num_bytes = (scan_size + 7) / 8;
749 int bits_left = scan_size;
750 int cur_byte = 0;
752 while (num_bytes-- > 1) {
753 buffer[cur_byte++] = buffer_read();
754 bits_left -= 8;
757 buffer[cur_byte] = 0x0;
759 /* There is one more partial byte left from the clock data in/out instructions */
760 if (bits_left > 1)
761 buffer[cur_byte] = buffer_read() >> 1;
762 /* This shift depends on the length of the
763 *clock data to tms instruction, insterted
764 *at end of the scan, now fixed to a two
765 *step transition in ft2232_add_scan */
766 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
769 static void ft2232_debug_dump_buffer(void)
771 int i;
772 char line[256];
773 char *line_p = line;
775 for (i = 0; i < ft2232_buffer_size; i++) {
776 line_p += snprintf(line_p,
777 sizeof(line) - (line_p - line),
778 "%2.2x ",
779 ft2232_buffer[i]);
780 if (i % 16 == 15) {
781 LOG_DEBUG("%s", line);
782 line_p = line;
786 if (line_p != line)
787 LOG_DEBUG("%s", line);
790 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
792 struct jtag_command *cmd;
793 uint8_t *buffer;
794 int scan_size;
795 enum scan_type type;
796 int retval;
797 uint32_t bytes_written = 0;
798 uint32_t bytes_read = 0;
800 #ifdef _DEBUG_USB_IO_
801 struct timeval start, inter, inter2, end;
802 struct timeval d_inter, d_inter2, d_end;
803 #endif
805 #ifdef _DEBUG_USB_COMMS_
806 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
807 ft2232_debug_dump_buffer();
808 #endif
810 #ifdef _DEBUG_USB_IO_
811 gettimeofday(&start, NULL);
812 #endif
814 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
815 if (retval != ERROR_OK) {
816 LOG_ERROR("couldn't write MPSSE commands to FT2232");
817 return retval;
820 #ifdef _DEBUG_USB_IO_
821 gettimeofday(&inter, NULL);
822 #endif
824 if (ft2232_expect_read) {
825 /* FIXME this "timeout" is never changed ... */
826 int timeout = LIBFTDI_READ_RETRY_COUNT;
827 ft2232_buffer_size = 0;
829 #ifdef _DEBUG_USB_IO_
830 gettimeofday(&inter2, NULL);
831 #endif
833 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
834 if (retval != ERROR_OK) {
835 LOG_ERROR("couldn't read from FT2232");
836 return retval;
839 #ifdef _DEBUG_USB_IO_
840 gettimeofday(&end, NULL);
842 timeval_subtract(&d_inter, &inter, &start);
843 timeval_subtract(&d_inter2, &inter2, &start);
844 timeval_subtract(&d_end, &end, &start);
846 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
847 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
848 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
849 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
850 #endif
852 ft2232_buffer_size = bytes_read;
854 if (ft2232_expect_read != ft2232_buffer_size) {
855 LOG_ERROR("ft2232_expect_read (%i) != "
856 "ft2232_buffer_size (%i) "
857 "(%i retries)",
858 ft2232_expect_read,
859 ft2232_buffer_size,
860 LIBFTDI_READ_RETRY_COUNT - timeout);
861 ft2232_debug_dump_buffer();
863 exit(-1);
866 #ifdef _DEBUG_USB_COMMS_
867 LOG_DEBUG("read buffer (%i retries): %i bytes",
868 LIBFTDI_READ_RETRY_COUNT - timeout,
869 ft2232_buffer_size);
870 ft2232_debug_dump_buffer();
871 #endif
874 ft2232_expect_read = 0;
875 ft2232_read_pointer = 0;
877 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
878 * that wasn't handled by a caller-provided error handler
880 retval = ERROR_OK;
882 cmd = first;
883 while (cmd != last) {
884 switch (cmd->type) {
885 case JTAG_SCAN:
886 type = jtag_scan_type(cmd->cmd.scan);
887 if (type != SCAN_OUT) {
888 scan_size = jtag_scan_size(cmd->cmd.scan);
889 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
890 ft2232_read_scan(type, buffer, scan_size);
891 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
892 retval = ERROR_JTAG_QUEUE_FAILED;
893 free(buffer);
895 break;
897 default:
898 break;
901 cmd = cmd->next;
904 ft2232_buffer_size = 0;
906 return retval;
910 * Function ft2232_add_pathmove
911 * moves the TAP controller from the current state to a new state through the
912 * given path, where path is an array of tap_state_t's.
914 * @param path is an array of tap_stat_t which gives the states to traverse through
915 * ending with the last state at path[num_states-1]
916 * @param num_states is the count of state steps to move through
918 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
920 int state_count = 0;
922 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
924 DEBUG_JTAG_IO("-");
926 /* this loop verifies that the path is legal and logs each state in the path */
927 while (num_states) {
928 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
929 int bit_count = 0;
930 int num_states_batch = num_states > 7 ? 7 : num_states;
932 /* command "Clock Data to TMS/CS Pin (no Read)" */
933 buffer_write(0x4b);
935 /* number of states remaining */
936 buffer_write(num_states_batch - 1);
938 while (num_states_batch--) {
939 /* either TMS=0 or TMS=1 must work ... */
940 if (tap_state_transition(tap_get_state(), false) == path[state_count])
941 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
942 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
943 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
945 /* ... or else the caller goofed BADLY */
946 else {
947 LOG_ERROR("BUG: %s -> %s isn't a valid "
948 "TAP state transition",
949 tap_state_name(tap_get_state()),
950 tap_state_name(path[state_count]));
951 exit(-1);
954 tap_set_state(path[state_count]);
955 state_count++;
956 num_states--;
959 buffer_write(tms_byte);
961 tap_set_end_state(tap_get_state());
964 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
966 int num_bytes = (scan_size + 7) / 8;
967 int bits_left = scan_size;
968 int cur_byte = 0;
969 int last_bit;
971 if (!ir_scan) {
972 if (tap_get_state() != TAP_DRSHIFT)
973 move_to_state(TAP_DRSHIFT);
974 } else {
975 if (tap_get_state() != TAP_IRSHIFT)
976 move_to_state(TAP_IRSHIFT);
979 /* add command for complete bytes */
980 while (num_bytes > 1) {
981 int thisrun_bytes;
982 if (type == SCAN_IO) {
983 /* Clock Data Bytes In and Out LSB First */
984 buffer_write(0x39);
985 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
986 } else if (type == SCAN_OUT) {
987 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
988 buffer_write(0x19);
989 /* LOG_DEBUG("added TDI bytes (o)"); */
990 } else if (type == SCAN_IN) {
991 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
992 buffer_write(0x28);
993 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
996 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
997 num_bytes -= thisrun_bytes;
999 buffer_write((uint8_t) (thisrun_bytes - 1));
1000 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1002 if (type != SCAN_IN) {
1003 /* add complete bytes */
1004 while (thisrun_bytes-- > 0) {
1005 buffer_write(buffer[cur_byte++]);
1006 bits_left -= 8;
1008 } else /* (type == SCAN_IN) */
1009 bits_left -= 8 * (thisrun_bytes);
1012 /* the most signifcant bit is scanned during TAP movement */
1013 if (type != SCAN_IN)
1014 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1015 else
1016 last_bit = 0;
1018 /* process remaining bits but the last one */
1019 if (bits_left > 1) {
1020 if (type == SCAN_IO) {
1021 /* Clock Data Bits In and Out LSB First */
1022 buffer_write(0x3b);
1023 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1024 } else if (type == SCAN_OUT) {
1025 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1026 buffer_write(0x1b);
1027 /* LOG_DEBUG("added TDI bits (o)"); */
1028 } else if (type == SCAN_IN) {
1029 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1030 buffer_write(0x2a);
1031 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1034 buffer_write(bits_left - 2);
1035 if (type != SCAN_IN)
1036 buffer_write(buffer[cur_byte]);
1039 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1040 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1041 if (type == SCAN_IO) {
1042 /* Clock Data Bits In and Out LSB First */
1043 buffer_write(0x3b);
1044 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1045 } else if (type == SCAN_OUT) {
1046 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1047 buffer_write(0x1b);
1048 /* LOG_DEBUG("added TDI bits (o)"); */
1049 } else if (type == SCAN_IN) {
1050 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1051 buffer_write(0x2a);
1052 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1054 buffer_write(0x0);
1055 buffer_write(last_bit);
1056 } else {
1057 int tms_bits;
1058 int tms_count;
1059 uint8_t mpsse_cmd;
1061 /* move from Shift-IR/DR to end state */
1062 if (type != SCAN_OUT) {
1063 /* We always go to the PAUSE state in two step at the end of an IN or IO
1064 *scan
1065 * This must be coordinated with the bit shifts in ft2232_read_scan */
1066 tms_bits = 0x01;
1067 tms_count = 2;
1068 /* Clock Data to TMS/CS Pin with Read */
1069 mpsse_cmd = 0x6b;
1070 } else {
1071 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1072 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1073 /* Clock Data to TMS/CS Pin (no Read) */
1074 mpsse_cmd = 0x4b;
1077 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1078 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1081 if (tap_get_state() != tap_get_end_state())
1082 move_to_state(tap_get_end_state());
1085 static int ft2232_large_scan(struct scan_command *cmd,
1086 enum scan_type type,
1087 uint8_t *buffer,
1088 int scan_size)
1090 int num_bytes = (scan_size + 7) / 8;
1091 int bits_left = scan_size;
1092 int cur_byte = 0;
1093 int last_bit;
1094 uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1095 uint8_t *receive_pointer = receive_buffer;
1096 uint32_t bytes_written;
1097 uint32_t bytes_read;
1098 int retval;
1099 int thisrun_read = 0;
1101 if (cmd->ir_scan) {
1102 LOG_ERROR("BUG: large IR scans are not supported");
1103 exit(-1);
1106 if (tap_get_state() != TAP_DRSHIFT)
1107 move_to_state(TAP_DRSHIFT);
1109 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1110 if (retval != ERROR_OK) {
1111 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1112 exit(-1);
1114 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1115 ft2232_buffer_size, (int)bytes_written);
1116 ft2232_buffer_size = 0;
1118 /* add command for complete bytes */
1119 while (num_bytes > 1) {
1120 int thisrun_bytes;
1122 if (type == SCAN_IO) {
1123 /* Clock Data Bytes In and Out LSB First */
1124 buffer_write(0x39);
1125 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1126 } else if (type == SCAN_OUT) {
1127 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1128 buffer_write(0x19);
1129 /* LOG_DEBUG("added TDI bytes (o)"); */
1130 } else if (type == SCAN_IN) {
1131 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1132 buffer_write(0x28);
1133 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1136 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1137 thisrun_read = thisrun_bytes;
1138 num_bytes -= thisrun_bytes;
1139 buffer_write((uint8_t) (thisrun_bytes - 1));
1140 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1142 if (type != SCAN_IN) {
1143 /* add complete bytes */
1144 while (thisrun_bytes-- > 0) {
1145 buffer_write(buffer[cur_byte]);
1146 cur_byte++;
1147 bits_left -= 8;
1149 } else /* (type == SCAN_IN) */
1150 bits_left -= 8 * (thisrun_bytes);
1152 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1153 if (retval != ERROR_OK) {
1154 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1155 exit(-1);
1157 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1158 ft2232_buffer_size,
1159 (int)bytes_written);
1160 ft2232_buffer_size = 0;
1162 if (type != SCAN_OUT) {
1163 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1164 if (retval != ERROR_OK) {
1165 LOG_ERROR("couldn't read from FT2232");
1166 exit(-1);
1168 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1169 thisrun_read,
1170 (int)bytes_read);
1171 receive_pointer += bytes_read;
1175 thisrun_read = 0;
1177 /* the most signifcant bit is scanned during TAP movement */
1178 if (type != SCAN_IN)
1179 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1180 else
1181 last_bit = 0;
1183 /* process remaining bits but the last one */
1184 if (bits_left > 1) {
1185 if (type == SCAN_IO) {
1186 /* Clock Data Bits In and Out LSB First */
1187 buffer_write(0x3b);
1188 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1189 } else if (type == SCAN_OUT) {
1190 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1191 buffer_write(0x1b);
1192 /* LOG_DEBUG("added TDI bits (o)"); */
1193 } else if (type == SCAN_IN) {
1194 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1195 buffer_write(0x2a);
1196 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1198 buffer_write(bits_left - 2);
1199 if (type != SCAN_IN)
1200 buffer_write(buffer[cur_byte]);
1202 if (type != SCAN_OUT)
1203 thisrun_read += 2;
1206 if (tap_get_end_state() == TAP_DRSHIFT) {
1207 if (type == SCAN_IO) {
1208 /* Clock Data Bits In and Out LSB First */
1209 buffer_write(0x3b);
1210 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1211 } else if (type == SCAN_OUT) {
1212 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1213 buffer_write(0x1b);
1214 /* LOG_DEBUG("added TDI bits (o)"); */
1215 } else if (type == SCAN_IN) {
1216 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1217 buffer_write(0x2a);
1218 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1220 buffer_write(0x0);
1221 buffer_write(last_bit);
1222 } else {
1223 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1224 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1225 uint8_t mpsse_cmd;
1227 /* move from Shift-IR/DR to end state */
1228 if (type != SCAN_OUT) {
1229 /* Clock Data to TMS/CS Pin with Read */
1230 mpsse_cmd = 0x6b;
1231 /* LOG_DEBUG("added TMS scan (read)"); */
1232 } else {
1233 /* Clock Data to TMS/CS Pin (no Read) */
1234 mpsse_cmd = 0x4b;
1235 /* LOG_DEBUG("added TMS scan (no read)"); */
1238 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1239 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1242 if (type != SCAN_OUT)
1243 thisrun_read += 1;
1245 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1246 if (retval != ERROR_OK) {
1247 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1248 exit(-1);
1250 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1251 ft2232_buffer_size,
1252 (int)bytes_written);
1253 ft2232_buffer_size = 0;
1255 if (type != SCAN_OUT) {
1256 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1257 if (retval != ERROR_OK) {
1258 LOG_ERROR("couldn't read from FT2232");
1259 exit(-1);
1261 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1262 thisrun_read,
1263 (int)bytes_read);
1266 return ERROR_OK;
1269 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1271 int predicted_size = 3;
1272 int num_bytes = (scan_size - 1) / 8;
1274 if (tap_get_state() != TAP_DRSHIFT)
1275 predicted_size += get_tms_buffer_requirements(
1276 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1278 if (type == SCAN_IN) { /* only from device to host */
1279 /* complete bytes */
1280 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1282 /* remaining bits - 1 (up to 7) */
1283 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1284 } else {/* host to device, or bidirectional
1285 * complete bytes */
1286 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1288 /* remaining bits -1 (up to 7) */
1289 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1292 return predicted_size;
1295 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1297 int predicted_size = 0;
1299 if (type != SCAN_OUT) {
1300 /* complete bytes */
1301 predicted_size +=
1302 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1304 /* remaining bits - 1 */
1305 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1307 /* last bit (from TMS scan) */
1308 predicted_size += 1;
1311 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1313 return predicted_size;
1316 /* semi-generic FT2232/FT4232 reset code */
1317 static void ftx23_reset(int trst, int srst)
1319 enum reset_types jtag_reset_config = jtag_get_reset_config();
1320 if (trst == 1) {
1321 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1322 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1323 else
1324 low_output &= ~nTRST; /* switch output low */
1325 } else if (trst == 0) {
1326 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1327 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1328 *and external pullup) */
1329 else
1330 low_output |= nTRST; /* switch output high */
1333 if (srst == 1) {
1334 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1335 low_output &= ~nSRST; /* switch output low */
1336 else
1337 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1338 } else if (srst == 0) {
1339 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1340 low_output |= nSRST; /* switch output high */
1341 else
1342 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1345 /* command "set data bits low byte" */
1346 buffer_write(0x80);
1347 buffer_write(low_output);
1348 buffer_write(low_direction);
1351 static void jtagkey_reset(int trst, int srst)
1353 enum reset_types jtag_reset_config = jtag_get_reset_config();
1354 if (trst == 1) {
1355 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1356 high_output &= ~nTRSTnOE;
1357 else
1358 high_output &= ~nTRST;
1359 } else if (trst == 0) {
1360 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1361 high_output |= nTRSTnOE;
1362 else
1363 high_output |= nTRST;
1366 if (srst == 1) {
1367 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1368 high_output &= ~nSRST;
1369 else
1370 high_output &= ~nSRSTnOE;
1371 } else if (srst == 0) {
1372 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1373 high_output |= nSRST;
1374 else
1375 high_output |= nSRSTnOE;
1378 /* command "set data bits high byte" */
1379 buffer_write(0x82);
1380 buffer_write(high_output);
1381 buffer_write(high_direction);
1382 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1383 trst,
1384 srst,
1385 high_output,
1386 high_direction);
1389 static void olimex_jtag_reset(int trst, int srst)
1391 enum reset_types jtag_reset_config = jtag_get_reset_config();
1392 if (trst == 1) {
1393 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1394 high_output &= ~nTRSTnOE;
1395 else
1396 high_output &= ~nTRST;
1397 } else if (trst == 0) {
1398 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1399 high_output |= nTRSTnOE;
1400 else
1401 high_output |= nTRST;
1404 if (srst == 1)
1405 high_output |= nSRST;
1406 else if (srst == 0)
1407 high_output &= ~nSRST;
1409 /* command "set data bits high byte" */
1410 buffer_write(0x82);
1411 buffer_write(high_output);
1412 buffer_write(high_direction);
1413 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1414 trst,
1415 srst,
1416 high_output,
1417 high_direction);
1420 static void axm0432_jtag_reset(int trst, int srst)
1422 if (trst == 1) {
1423 tap_set_state(TAP_RESET);
1424 high_output &= ~nTRST;
1425 } else if (trst == 0)
1426 high_output |= nTRST;
1428 if (srst == 1)
1429 high_output &= ~nSRST;
1430 else if (srst == 0)
1431 high_output |= nSRST;
1433 /* command "set data bits low byte" */
1434 buffer_write(0x82);
1435 buffer_write(high_output);
1436 buffer_write(high_direction);
1437 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1438 trst,
1439 srst,
1440 high_output,
1441 high_direction);
1444 static void flyswatter_reset(int trst, int srst)
1446 if (trst == 1)
1447 low_output &= ~nTRST;
1448 else if (trst == 0)
1449 low_output |= nTRST;
1451 if (srst == 1)
1452 low_output |= nSRST;
1453 else if (srst == 0)
1454 low_output &= ~nSRST;
1456 /* command "set data bits low byte" */
1457 buffer_write(0x80);
1458 buffer_write(low_output);
1459 buffer_write(low_direction);
1460 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1461 trst,
1462 srst,
1463 low_output,
1464 low_direction);
1467 static void flyswatter1_reset(int trst, int srst)
1469 flyswatter_reset(trst, srst);
1472 static void flyswatter2_reset(int trst, int srst)
1474 flyswatter_reset(trst, !srst);
1477 static void minimodule_reset(int trst, int srst)
1479 if (srst == 1)
1480 low_output &= ~nSRST;
1481 else if (srst == 0)
1482 low_output |= nSRST;
1484 /* command "set data bits low byte" */
1485 buffer_write(0x80);
1486 buffer_write(low_output);
1487 buffer_write(low_direction);
1488 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1489 trst,
1490 srst,
1491 low_output,
1492 low_direction);
1495 static void turtle_reset(int trst, int srst)
1497 trst = trst;
1499 if (srst == 1)
1500 low_output |= nSRST;
1501 else if (srst == 0)
1502 low_output &= ~nSRST;
1504 /* command "set data bits low byte" */
1505 buffer_write(0x80);
1506 buffer_write(low_output);
1507 buffer_write(low_direction);
1508 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1509 srst,
1510 low_output,
1511 low_direction);
1514 static void comstick_reset(int trst, int srst)
1516 if (trst == 1)
1517 high_output &= ~nTRST;
1518 else if (trst == 0)
1519 high_output |= nTRST;
1521 if (srst == 1)
1522 high_output &= ~nSRST;
1523 else if (srst == 0)
1524 high_output |= nSRST;
1526 /* command "set data bits high byte" */
1527 buffer_write(0x82);
1528 buffer_write(high_output);
1529 buffer_write(high_direction);
1530 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1531 trst,
1532 srst,
1533 high_output,
1534 high_direction);
1537 static void stm32stick_reset(int trst, int srst)
1539 if (trst == 1)
1540 high_output &= ~nTRST;
1541 else if (trst == 0)
1542 high_output |= nTRST;
1544 if (srst == 1)
1545 low_output &= ~nSRST;
1546 else if (srst == 0)
1547 low_output |= nSRST;
1549 /* command "set data bits low byte" */
1550 buffer_write(0x80);
1551 buffer_write(low_output);
1552 buffer_write(low_direction);
1554 /* command "set data bits high byte" */
1555 buffer_write(0x82);
1556 buffer_write(high_output);
1557 buffer_write(high_direction);
1558 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1559 trst,
1560 srst,
1561 high_output,
1562 high_direction);
1565 static void sheevaplug_reset(int trst, int srst)
1567 if (trst == 1)
1568 high_output &= ~nTRST;
1569 else if (trst == 0)
1570 high_output |= nTRST;
1572 if (srst == 1)
1573 high_output &= ~nSRSTnOE;
1574 else if (srst == 0)
1575 high_output |= nSRSTnOE;
1577 /* command "set data bits high byte" */
1578 buffer_write(0x82);
1579 buffer_write(high_output);
1580 buffer_write(high_direction);
1581 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1582 trst,
1583 srst,
1584 high_output,
1585 high_direction);
1588 static void redbee_reset(int trst, int srst)
1590 if (trst == 1) {
1591 tap_set_state(TAP_RESET);
1592 high_output &= ~nTRST;
1593 } else if (trst == 0)
1594 high_output |= nTRST;
1596 if (srst == 1)
1597 high_output &= ~nSRST;
1598 else if (srst == 0)
1599 high_output |= nSRST;
1601 /* command "set data bits low byte" */
1602 buffer_write(0x82);
1603 buffer_write(high_output);
1604 buffer_write(high_direction);
1605 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1606 "high_direction: 0x%2.2x", trst, srst, high_output,
1607 high_direction);
1610 static void xds100v2_reset(int trst, int srst)
1612 if (trst == 1) {
1613 tap_set_state(TAP_RESET);
1614 high_output &= ~nTRST;
1615 } else if (trst == 0)
1616 high_output |= nTRST;
1618 if (srst == 1)
1619 high_output |= nSRST;
1620 else if (srst == 0)
1621 high_output &= ~nSRST;
1623 /* command "set data bits low byte" */
1624 buffer_write(0x82);
1625 buffer_write(high_output);
1626 buffer_write(high_direction);
1627 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1628 "high_direction: 0x%2.2x", trst, srst, high_output,
1629 high_direction);
1632 static int ft2232_execute_runtest(struct jtag_command *cmd)
1634 int retval;
1635 int i;
1636 int predicted_size = 0;
1637 retval = ERROR_OK;
1639 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1640 cmd->cmd.runtest->num_cycles,
1641 tap_state_name(cmd->cmd.runtest->end_state));
1643 /* only send the maximum buffer size that FT2232C can handle */
1644 predicted_size = 0;
1645 if (tap_get_state() != TAP_IDLE)
1646 predicted_size += 3;
1647 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1648 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1649 predicted_size += 3;
1650 if (tap_get_end_state() != TAP_IDLE)
1651 predicted_size += 3;
1652 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1653 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1654 retval = ERROR_JTAG_QUEUE_FAILED;
1655 require_send = 0;
1656 first_unsent = cmd;
1658 if (tap_get_state() != TAP_IDLE) {
1659 move_to_state(TAP_IDLE);
1660 require_send = 1;
1662 i = cmd->cmd.runtest->num_cycles;
1663 while (i > 0) {
1664 /* there are no state transitions in this code, so omit state tracking */
1666 /* command "Clock Data to TMS/CS Pin (no Read)" */
1667 buffer_write(0x4b);
1669 /* scan 7 bits */
1670 buffer_write((i > 7) ? 6 : (i - 1));
1672 /* TMS data bits */
1673 buffer_write(0x0);
1675 i -= (i > 7) ? 7 : i;
1676 /* LOG_DEBUG("added TMS scan (no read)"); */
1679 ft2232_end_state(cmd->cmd.runtest->end_state);
1681 if (tap_get_state() != tap_get_end_state())
1682 move_to_state(tap_get_end_state());
1684 require_send = 1;
1685 DEBUG_JTAG_IO("runtest: %i, end in %s",
1686 cmd->cmd.runtest->num_cycles,
1687 tap_state_name(tap_get_end_state()));
1688 return retval;
1691 static int ft2232_execute_statemove(struct jtag_command *cmd)
1693 int predicted_size = 0;
1694 int retval = ERROR_OK;
1696 DEBUG_JTAG_IO("statemove end in %s",
1697 tap_state_name(cmd->cmd.statemove->end_state));
1699 /* only send the maximum buffer size that FT2232C can handle */
1700 predicted_size = 3;
1701 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1702 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1703 retval = ERROR_JTAG_QUEUE_FAILED;
1704 require_send = 0;
1705 first_unsent = cmd;
1707 ft2232_end_state(cmd->cmd.statemove->end_state);
1709 /* For TAP_RESET, ignore the current recorded state. It's often
1710 * wrong at server startup, and this transation is critical whenever
1711 * it's requested.
1713 if (tap_get_end_state() == TAP_RESET) {
1714 clock_tms(0x4b, 0xff, 5, 0);
1715 require_send = 1;
1717 /* shortest-path move to desired end state */
1718 } else if (tap_get_state() != tap_get_end_state()) {
1719 move_to_state(tap_get_end_state());
1720 require_send = 1;
1723 return retval;
1727 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1728 * (or SWD) state machine.
1730 static int ft2232_execute_tms(struct jtag_command *cmd)
1732 int retval = ERROR_OK;
1733 unsigned num_bits = cmd->cmd.tms->num_bits;
1734 const uint8_t *bits = cmd->cmd.tms->bits;
1735 unsigned count;
1737 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1739 /* only send the maximum buffer size that FT2232C can handle */
1740 count = 3 * DIV_ROUND_UP(num_bits, 4);
1741 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1742 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1743 retval = ERROR_JTAG_QUEUE_FAILED;
1745 require_send = 0;
1746 first_unsent = cmd;
1749 /* Shift out in batches of at most 6 bits; there's a report of an
1750 * FT2232 bug in this area, where shifting exactly 7 bits can make
1751 * problems with TMS signaling for the last clock cycle:
1753 * http://developer.intra2net.com/mailarchive/html/
1754 * libftdi/2009/msg00292.html
1756 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1758 * Note that pathmoves in JTAG are not often seven bits, so that
1759 * isn't a particularly likely situation outside of "special"
1760 * signaling such as switching between JTAG and SWD modes.
1762 while (num_bits) {
1763 if (num_bits <= 6) {
1764 buffer_write(0x4b);
1765 buffer_write(num_bits - 1);
1766 buffer_write(*bits & 0x3f);
1767 break;
1770 /* Yes, this is lazy ... we COULD shift out more data
1771 * bits per operation, but doing it in nybbles is easy
1773 buffer_write(0x4b);
1774 buffer_write(3);
1775 buffer_write(*bits & 0xf);
1776 num_bits -= 4;
1778 count = (num_bits > 4) ? 4 : num_bits;
1780 buffer_write(0x4b);
1781 buffer_write(count - 1);
1782 buffer_write((*bits >> 4) & 0xf);
1783 num_bits -= count;
1785 bits++;
1788 require_send = 1;
1789 return retval;
1792 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1794 int predicted_size = 0;
1795 int retval = ERROR_OK;
1797 tap_state_t *path = cmd->cmd.pathmove->path;
1798 int num_states = cmd->cmd.pathmove->num_states;
1800 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1801 tap_state_name(tap_get_state()),
1802 tap_state_name(path[num_states-1]));
1804 /* only send the maximum buffer size that FT2232C can handle */
1805 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1806 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1807 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1808 retval = ERROR_JTAG_QUEUE_FAILED;
1810 require_send = 0;
1811 first_unsent = cmd;
1814 ft2232_add_pathmove(path, num_states);
1815 require_send = 1;
1817 return retval;
1820 static int ft2232_execute_scan(struct jtag_command *cmd)
1822 uint8_t *buffer;
1823 int scan_size; /* size of IR or DR scan */
1824 int predicted_size = 0;
1825 int retval = ERROR_OK;
1827 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1829 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1831 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1833 predicted_size = ft2232_predict_scan_out(scan_size, type);
1834 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1835 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1836 /* unsent commands before this */
1837 if (first_unsent != cmd)
1838 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1839 retval = ERROR_JTAG_QUEUE_FAILED;
1841 /* current command */
1842 ft2232_end_state(cmd->cmd.scan->end_state);
1843 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1844 require_send = 0;
1845 first_unsent = cmd->next;
1846 if (buffer)
1847 free(buffer);
1848 return retval;
1849 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1850 LOG_DEBUG(
1851 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1852 first_unsent,
1853 cmd);
1854 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1855 retval = ERROR_JTAG_QUEUE_FAILED;
1856 require_send = 0;
1857 first_unsent = cmd;
1859 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1860 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1861 ft2232_end_state(cmd->cmd.scan->end_state);
1862 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1863 require_send = 1;
1864 if (buffer)
1865 free(buffer);
1866 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1867 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1868 tap_state_name(tap_get_end_state()));
1869 return retval;
1873 static int ft2232_execute_reset(struct jtag_command *cmd)
1875 int retval;
1876 int predicted_size = 0;
1877 retval = ERROR_OK;
1879 DEBUG_JTAG_IO("reset trst: %i srst %i",
1880 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1882 /* only send the maximum buffer size that FT2232C can handle */
1883 predicted_size = 3;
1884 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1885 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1886 retval = ERROR_JTAG_QUEUE_FAILED;
1887 require_send = 0;
1888 first_unsent = cmd;
1891 if ((cmd->cmd.reset->trst == 1) ||
1892 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1893 tap_set_state(TAP_RESET);
1895 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1896 require_send = 1;
1898 DEBUG_JTAG_IO("trst: %i, srst: %i",
1899 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1900 return retval;
1903 static int ft2232_execute_sleep(struct jtag_command *cmd)
1905 int retval;
1906 retval = ERROR_OK;
1908 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1910 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1911 retval = ERROR_JTAG_QUEUE_FAILED;
1912 first_unsent = cmd->next;
1913 jtag_sleep(cmd->cmd.sleep->us);
1914 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1915 cmd->cmd.sleep->us,
1916 tap_state_name(tap_get_state()));
1917 return retval;
1920 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1922 int retval;
1923 retval = ERROR_OK;
1925 /* this is only allowed while in a stable state. A check for a stable
1926 * state was done in jtag_add_clocks()
1928 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1929 retval = ERROR_JTAG_QUEUE_FAILED;
1930 DEBUG_JTAG_IO("clocks %i while in %s",
1931 cmd->cmd.stableclocks->num_cycles,
1932 tap_state_name(tap_get_state()));
1933 return retval;
1936 static int ft2232_execute_command(struct jtag_command *cmd)
1938 int retval;
1940 switch (cmd->type) {
1941 case JTAG_RESET:
1942 retval = ft2232_execute_reset(cmd);
1943 break;
1944 case JTAG_RUNTEST:
1945 retval = ft2232_execute_runtest(cmd);
1946 break;
1947 case JTAG_TLR_RESET:
1948 retval = ft2232_execute_statemove(cmd);
1949 break;
1950 case JTAG_PATHMOVE:
1951 retval = ft2232_execute_pathmove(cmd);
1952 break;
1953 case JTAG_SCAN:
1954 retval = ft2232_execute_scan(cmd);
1955 break;
1956 case JTAG_SLEEP:
1957 retval = ft2232_execute_sleep(cmd);
1958 break;
1959 case JTAG_STABLECLOCKS:
1960 retval = ft2232_execute_stableclocks(cmd);
1961 break;
1962 case JTAG_TMS:
1963 retval = ft2232_execute_tms(cmd);
1964 break;
1965 default:
1966 LOG_ERROR("BUG: unknown JTAG command type encountered");
1967 retval = ERROR_JTAG_QUEUE_FAILED;
1968 break;
1970 return retval;
1973 static int ft2232_execute_queue(void)
1975 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1976 int retval;
1978 first_unsent = cmd; /* next command that has to be sent */
1979 require_send = 0;
1981 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1982 * that wasn't handled by a caller-provided error handler
1984 retval = ERROR_OK;
1986 ft2232_buffer_size = 0;
1987 ft2232_expect_read = 0;
1989 /* blink, if the current layout has that feature */
1990 if (layout->blink)
1991 layout->blink();
1993 while (cmd) {
1994 /* fill the write buffer with the desired command */
1995 if (ft2232_execute_command(cmd) != ERROR_OK)
1996 retval = ERROR_JTAG_QUEUE_FAILED;
1997 /* Start reading input before FT2232 TX buffer fills up.
1998 * Sometimes this happens because we don't know the
1999 * length of the last command before we execute it. So
2000 * we simple inform the user.
2002 cmd = cmd->next;
2004 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2005 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2006 LOG_DEBUG("read buffer size looks too high %d/%d",
2007 ft2232_expect_read,
2008 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2009 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2010 retval = ERROR_JTAG_QUEUE_FAILED;
2011 first_unsent = cmd;
2015 if (require_send > 0)
2016 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2017 retval = ERROR_JTAG_QUEUE_FAILED;
2019 return retval;
2022 #if BUILD_FT2232_FTD2XX == 1
2023 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2025 FT_STATUS status;
2026 DWORD deviceID;
2027 char SerialNumber[16];
2028 char Description[64];
2029 DWORD openex_flags = 0;
2030 char *openex_string = NULL;
2031 uint8_t latency_timer;
2033 if (layout == NULL) {
2034 LOG_WARNING("No ft2232 layout specified'");
2035 return ERROR_JTAG_INIT_FAILED;
2038 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2039 layout->name, vid, pid);
2041 #if IS_WIN32 == 0
2042 /* Add non-standard Vid/Pid to the linux driver */
2043 status = FT_SetVIDPID(vid, pid);
2044 if (status != FT_OK)
2045 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2047 #endif
2049 if (ft2232_device_desc && ft2232_serial) {
2050 LOG_WARNING(
2051 "can't open by device description and serial number, giving precedence to serial");
2052 ft2232_device_desc = NULL;
2055 if (ft2232_device_desc) {
2056 openex_string = ft2232_device_desc;
2057 openex_flags = FT_OPEN_BY_DESCRIPTION;
2058 } else if (ft2232_serial) {
2059 openex_string = ft2232_serial;
2060 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2061 } else {
2062 LOG_ERROR("neither device description nor serial number specified");
2063 LOG_ERROR(
2064 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2066 return ERROR_JTAG_INIT_FAILED;
2069 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2070 if (status != FT_OK) {
2071 /* under Win32, the FTD2XX driver appends an "A" to the end
2072 * of the description, if we tried by the desc, then
2073 * try by the alternate "A" description. */
2074 if (openex_string == ft2232_device_desc) {
2075 /* Try the alternate method. */
2076 openex_string = ft2232_device_desc_A;
2077 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2078 if (status == FT_OK) {
2079 /* yea, the "alternate" method worked! */
2080 } else {
2081 /* drat, give the user a meaningfull message.
2082 * telling the use we tried *BOTH* methods. */
2083 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2084 ft2232_device_desc,
2085 ft2232_device_desc_A);
2090 if (status != FT_OK) {
2091 DWORD num_devices;
2093 if (more) {
2094 LOG_WARNING("unable to open ftdi device (trying more): %s",
2095 ftd2xx_status_string(status));
2096 *try_more = 1;
2097 return ERROR_JTAG_INIT_FAILED;
2099 LOG_ERROR("unable to open ftdi device: %s",
2100 ftd2xx_status_string(status));
2101 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2102 if (status == FT_OK) {
2103 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2104 uint32_t i;
2106 for (i = 0; i < num_devices; i++)
2107 desc_array[i] = malloc(64);
2109 desc_array[num_devices] = NULL;
2111 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2113 if (status == FT_OK) {
2114 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2115 for (i = 0; i < num_devices; i++)
2116 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2119 for (i = 0; i < num_devices; i++)
2120 free(desc_array[i]);
2122 free(desc_array);
2123 } else
2124 LOG_ERROR("ListDevices: NONE");
2125 return ERROR_JTAG_INIT_FAILED;
2128 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2129 if (status != FT_OK) {
2130 LOG_ERROR("unable to set latency timer: %s",
2131 ftd2xx_status_string(status));
2132 return ERROR_JTAG_INIT_FAILED;
2135 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2136 if (status != FT_OK) {
2137 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2138 * so ignore errors if using this driver version */
2139 DWORD dw_version;
2141 status = FT_GetDriverVersion(ftdih, &dw_version);
2142 LOG_ERROR("unable to get latency timer: %s",
2143 ftd2xx_status_string(status));
2145 if ((status == FT_OK) && (dw_version == 0x10004)) {
2146 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2147 "with FT_GetLatencyTimer, upgrade to a newer version");
2148 } else
2149 return ERROR_JTAG_INIT_FAILED;
2150 } else
2151 LOG_DEBUG("current latency timer: %i", latency_timer);
2153 status = FT_SetTimeouts(ftdih, 5000, 5000);
2154 if (status != FT_OK) {
2155 LOG_ERROR("unable to set timeouts: %s",
2156 ftd2xx_status_string(status));
2157 return ERROR_JTAG_INIT_FAILED;
2160 status = FT_SetBitMode(ftdih, 0x0b, 2);
2161 if (status != FT_OK) {
2162 LOG_ERROR("unable to enable bit i/o mode: %s",
2163 ftd2xx_status_string(status));
2164 return ERROR_JTAG_INIT_FAILED;
2167 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2168 SerialNumber, Description, NULL);
2169 if (status != FT_OK) {
2170 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2171 ftd2xx_status_string(status));
2172 return ERROR_JTAG_INIT_FAILED;
2173 } else {
2174 static const char *type_str[] = {
2175 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"
2177 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2178 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2179 ? ftdi_device : FT_DEVICE_UNKNOWN;
2180 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2181 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2182 LOG_INFO("SerialNumber: %s", SerialNumber);
2183 LOG_INFO("Description: %s", Description);
2186 return ERROR_OK;
2189 static int ft2232_purge_ftd2xx(void)
2191 FT_STATUS status;
2193 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2194 if (status != FT_OK) {
2195 LOG_ERROR("error purging ftd2xx device: %s",
2196 ftd2xx_status_string(status));
2197 return ERROR_JTAG_INIT_FAILED;
2200 return ERROR_OK;
2203 #endif /* BUILD_FT2232_FTD2XX == 1 */
2205 #if BUILD_FT2232_LIBFTDI == 1
2206 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2208 uint8_t latency_timer;
2210 if (layout == NULL) {
2211 LOG_WARNING("No ft2232 layout specified'");
2212 return ERROR_JTAG_INIT_FAILED;
2215 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2216 layout->name, vid, pid);
2218 if (ftdi_init(&ftdic) < 0)
2219 return ERROR_JTAG_INIT_FAILED;
2221 /* default to INTERFACE_A */
2222 if (channel == INTERFACE_ANY)
2223 channel = INTERFACE_A;
2224 if (ftdi_set_interface(&ftdic, channel) < 0) {
2225 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2226 return ERROR_JTAG_INIT_FAILED;
2229 /* context, vendor id, product id */
2230 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2231 if (more)
2232 LOG_WARNING("unable to open ftdi device (trying more): %s",
2233 ftdic.error_str);
2234 else
2235 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2236 *try_more = 1;
2237 return ERROR_JTAG_INIT_FAILED;
2240 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2241 if (ftdi_usb_reset(&ftdic) < 0) {
2242 LOG_ERROR("unable to reset ftdi device");
2243 return ERROR_JTAG_INIT_FAILED;
2246 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2247 LOG_ERROR("unable to set latency timer");
2248 return ERROR_JTAG_INIT_FAILED;
2251 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2252 LOG_ERROR("unable to get latency timer");
2253 return ERROR_JTAG_INIT_FAILED;
2254 } else
2255 LOG_DEBUG("current latency timer: %i", latency_timer);
2257 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2259 ftdi_device = ftdic.type;
2260 static const char *type_str[] = {
2261 "AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"
2263 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2264 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2265 ? ftdi_device : no_of_known_types;
2266 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2267 return ERROR_OK;
2270 static int ft2232_purge_libftdi(void)
2272 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2273 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2274 return ERROR_JTAG_INIT_FAILED;
2277 return ERROR_OK;
2280 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2282 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2284 uint8_t buf[3];
2285 uint32_t bytes_written;
2287 buf[0] = 0x80; /* command "set data bits low byte" */
2288 buf[1] = value; /* value */
2289 buf[2] = direction; /* direction */
2291 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2293 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2294 LOG_ERROR("couldn't initialize data bits low byte");
2295 return ERROR_JTAG_INIT_FAILED;
2298 return ERROR_OK;
2301 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2303 uint8_t buf[3];
2304 uint32_t bytes_written;
2306 buf[0] = 0x82; /* command "set data bits high 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 high byte");
2314 return ERROR_JTAG_INIT_FAILED;
2317 return ERROR_OK;
2320 static int ft2232_init(void)
2322 uint8_t buf[1];
2323 int retval;
2324 uint32_t bytes_written;
2326 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2327 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2328 else
2329 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2330 if (layout == NULL) {
2331 LOG_WARNING("No ft2232 layout specified'");
2332 return ERROR_JTAG_INIT_FAILED;
2335 for (int i = 0; 1; i++) {
2337 * "more indicates that there are more IDs to try, so we should
2338 * not print an error for an ID mismatch (but for anything
2339 * else, we should).
2341 * try_more indicates that the error code returned indicates an
2342 * ID mismatch (and nothing else) and that we should proceeed
2343 * with the next ID pair.
2345 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2346 int try_more = 0;
2348 #if BUILD_FT2232_FTD2XX == 1
2349 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2350 more, &try_more);
2351 #elif BUILD_FT2232_LIBFTDI == 1
2352 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2353 more, &try_more, layout->channel);
2354 #endif
2355 if (retval >= 0)
2356 break;
2357 if (!more || !try_more)
2358 return retval;
2361 ft2232_buffer_size = 0;
2362 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2364 if (layout->init() != ERROR_OK)
2365 return ERROR_JTAG_INIT_FAILED;
2367 if (ft2232_device_is_highspeed()) {
2368 #ifndef BUILD_FT2232_HIGHSPEED
2369 #if BUILD_FT2232_FTD2XX == 1
2370 LOG_WARNING(
2371 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2372 #elif BUILD_FT2232_LIBFTDI == 1
2373 LOG_WARNING(
2374 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2375 #endif
2376 #endif
2377 /* make sure the legacy mode is disabled */
2378 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2379 return ERROR_JTAG_INIT_FAILED;
2382 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2383 retval = ft2232_write(buf, 1, &bytes_written);
2384 if (retval != ERROR_OK) {
2385 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2386 return ERROR_JTAG_INIT_FAILED;
2389 #if BUILD_FT2232_FTD2XX == 1
2390 return ft2232_purge_ftd2xx();
2391 #elif BUILD_FT2232_LIBFTDI == 1
2392 return ft2232_purge_libftdi();
2393 #endif
2395 return ERROR_OK;
2398 /** Updates defaults for DBUS signals: the four JTAG signals
2399 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2401 static inline void ftx232_dbus_init(void)
2403 low_output = 0x08;
2404 low_direction = 0x0b;
2407 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2408 * the four GPIOL signals. Initialization covers value and direction,
2409 * as customized for each layout.
2411 static int ftx232_dbus_write(void)
2413 enum reset_types jtag_reset_config = jtag_get_reset_config();
2414 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2415 low_direction &= ~nTRSTnOE; /* nTRST input */
2416 low_output &= ~nTRST; /* nTRST = 0 */
2417 } else {
2418 low_direction |= nTRSTnOE; /* nTRST output */
2419 low_output |= nTRST; /* nTRST = 1 */
2422 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2423 low_direction |= nSRSTnOE; /* nSRST output */
2424 low_output |= nSRST; /* nSRST = 1 */
2425 } else {
2426 low_direction &= ~nSRSTnOE; /* nSRST input */
2427 low_output &= ~nSRST; /* nSRST = 0 */
2430 /* initialize low byte for jtag */
2431 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2432 LOG_ERROR("couldn't initialize FT2232 DBUS");
2433 return ERROR_JTAG_INIT_FAILED;
2436 return ERROR_OK;
2439 static int usbjtag_init(void)
2442 * NOTE: This is now _specific_ to the "usbjtag" layout.
2443 * Don't try cram any more layouts into this.
2445 ftx232_dbus_init();
2447 nTRST = 0x10;
2448 nTRSTnOE = 0x10;
2449 nSRST = 0x40;
2450 nSRSTnOE = 0x40;
2452 return ftx232_dbus_write();
2455 static int lm3s811_jtag_init(void)
2457 ftx232_dbus_init();
2459 /* There are multiple revisions of LM3S811 eval boards:
2460 * - Rev B (and older?) boards have no SWO trace support.
2461 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2462 * they should use the "luminary_icdi" layout instead.
2464 nTRST = 0x0;
2465 nTRSTnOE = 0x00;
2466 nSRST = 0x20;
2467 nSRSTnOE = 0x20;
2468 low_output = 0x88;
2469 low_direction = 0x8b;
2471 return ftx232_dbus_write();
2474 static int icdi_jtag_init(void)
2476 ftx232_dbus_init();
2478 /* Most Luminary eval boards support SWO trace output,
2479 * and should use this "luminary_icdi" layout.
2481 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2482 * to switch between JTAG and SWD, or switch the ft2232 UART
2483 * on the second MPSSE channel/interface (BDBUS)
2484 * between (i) the stellaris UART (on Luminary boards)
2485 * or (ii) SWO trace data (generic).
2487 * We come up in JTAG mode and may switch to SWD later (with
2488 * SWO/trace option if SWD is active).
2490 * DBUS == GPIO-Lx
2491 * CBUS == GPIO-Hx
2495 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2496 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2497 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2500 /* GPIOs on second channel/interface (UART) ... */
2501 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2502 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2503 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2505 nTRST = 0x0;
2506 nTRSTnOE = 0x00;
2507 nSRST = ICDI_SRST;
2508 nSRSTnOE = ICDI_SRST;
2510 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2511 low_output |= ICDI_JTAG_EN;
2512 low_output &= ~ICDI_DBG_ENn;
2514 return ftx232_dbus_write();
2517 static int signalyzer_init(void)
2519 ftx232_dbus_init();
2521 nTRST = 0x10;
2522 nTRSTnOE = 0x10;
2523 nSRST = 0x20;
2524 nSRSTnOE = 0x20;
2525 return ftx232_dbus_write();
2528 static int axm0432_jtag_init(void)
2530 low_output = 0x08;
2531 low_direction = 0x2b;
2533 /* initialize low byte for jtag */
2534 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2535 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2536 return ERROR_JTAG_INIT_FAILED;
2539 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2540 nTRST = 0x08;
2541 nTRSTnOE = 0x0; /* No output enable for TRST*/
2542 nSRST = 0x04;
2543 nSRSTnOE = 0x0; /* No output enable for SRST*/
2544 } else {
2545 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2546 exit(-1);
2549 high_output = 0x0;
2550 high_direction = 0x0c;
2552 enum reset_types jtag_reset_config = jtag_get_reset_config();
2553 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2554 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2555 else
2556 high_output |= nTRST;
2558 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2559 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2560 else
2561 high_output |= nSRST;
2563 /* initialize high byte for jtag */
2564 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2565 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2566 return ERROR_JTAG_INIT_FAILED;
2569 return ERROR_OK;
2572 static int redbee_init(void)
2574 low_output = 0x08;
2575 low_direction = 0x2b;
2577 /* initialize low byte for jtag */
2578 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2579 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2580 return ERROR_JTAG_INIT_FAILED;
2583 nTRST = 0x08;
2584 nTRSTnOE = 0x0; /* No output enable for TRST*/
2585 nSRST = 0x04;
2586 nSRSTnOE = 0x0; /* No output enable for SRST*/
2588 high_output = 0x0;
2589 high_direction = 0x0c;
2591 enum reset_types jtag_reset_config = jtag_get_reset_config();
2592 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2593 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2594 else
2595 high_output |= nTRST;
2597 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2598 LOG_ERROR("can't set nSRST to push-pull on redbee");
2599 else
2600 high_output |= nSRST;
2602 /* initialize high byte for jtag */
2603 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2604 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2605 return ERROR_JTAG_INIT_FAILED;
2608 return ERROR_OK;
2611 static int jtagkey_init(void)
2613 low_output = 0x08;
2614 low_direction = 0x1b;
2616 /* initialize low byte for jtag */
2617 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2618 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2619 return ERROR_JTAG_INIT_FAILED;
2622 if (strcmp(layout->name, "jtagkey") == 0) {
2623 nTRST = 0x01;
2624 nTRSTnOE = 0x4;
2625 nSRST = 0x02;
2626 nSRSTnOE = 0x08;
2627 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2628 || (strcmp(layout->name, "oocdlink") == 0)) {
2629 nTRST = 0x02;
2630 nTRSTnOE = 0x1;
2631 nSRST = 0x08;
2632 nSRSTnOE = 0x04;
2633 } else {
2634 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2635 exit(-1);
2638 high_output = 0x0;
2639 high_direction = 0x0f;
2641 enum reset_types jtag_reset_config = jtag_get_reset_config();
2642 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2643 high_output |= nTRSTnOE;
2644 high_output &= ~nTRST;
2645 } else {
2646 high_output &= ~nTRSTnOE;
2647 high_output |= nTRST;
2650 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2651 high_output &= ~nSRSTnOE;
2652 high_output |= nSRST;
2653 } else {
2654 high_output |= nSRSTnOE;
2655 high_output &= ~nSRST;
2658 /* initialize high byte for jtag */
2659 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2660 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2661 return ERROR_JTAG_INIT_FAILED;
2664 return ERROR_OK;
2667 static int olimex_jtag_init(void)
2669 low_output = 0x08;
2670 low_direction = 0x1b;
2672 /* initialize low byte for jtag */
2673 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2674 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2675 return ERROR_JTAG_INIT_FAILED;
2678 nTRST = 0x01;
2679 nTRSTnOE = 0x4;
2680 nSRST = 0x02;
2681 nSRSTnOE = 0x00;/* no output enable for nSRST */
2683 high_output = 0x0;
2684 high_direction = 0x0f;
2686 enum reset_types jtag_reset_config = jtag_get_reset_config();
2687 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2688 high_output |= nTRSTnOE;
2689 high_output &= ~nTRST;
2690 } else {
2691 high_output &= ~nTRSTnOE;
2692 high_output |= nTRST;
2695 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2696 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2697 else
2698 high_output &= ~nSRST;
2700 /* turn red LED on */
2701 high_output |= 0x08;
2703 /* initialize high byte for jtag */
2704 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2705 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2706 return ERROR_JTAG_INIT_FAILED;
2709 return ERROR_OK;
2712 static int flyswatter_init(int rev)
2714 low_output = 0x18;
2715 low_direction = 0x7b;
2717 if ((rev < 0) || (rev > 3)) {
2718 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2719 return ERROR_JTAG_INIT_FAILED;
2722 if (rev == 1)
2723 low_direction |= 1 << 7;
2725 /* initialize low byte for jtag */
2726 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2727 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2728 return ERROR_JTAG_INIT_FAILED;
2731 nTRST = 0x10;
2732 nTRSTnOE = 0x0; /* not output enable for nTRST */
2733 nSRST = 0x20;
2734 nSRSTnOE = 0x00; /* no output enable for nSRST */
2736 high_output = 0x00;
2738 if (rev == 1)
2739 high_direction = 0x0c;
2740 else
2741 high_direction = 0x01;
2743 /* turn red LED3 on, LED2 off */
2744 high_output |= 0x08;
2746 /* initialize high byte for jtag */
2747 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2748 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2749 return ERROR_JTAG_INIT_FAILED;
2752 return ERROR_OK;
2755 static int flyswatter1_init(void)
2757 return flyswatter_init(1);
2760 static int flyswatter2_init(void)
2762 return flyswatter_init(2);
2765 static int minimodule_init(void)
2767 low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2768 * 0x18) */
2769 low_direction = 0xfb; /* 0xfb; */
2771 /* initialize low byte for jtag */
2772 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2773 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2774 return ERROR_JTAG_INIT_FAILED;
2778 nSRST = 0x20;
2780 high_output = 0x00;
2781 high_direction = 0x05;
2783 /* turn red LED3 on, LED2 off */
2784 /* high_output |= 0x08; */
2786 /* initialize high byte for jtag */
2787 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2788 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2789 return ERROR_JTAG_INIT_FAILED;
2792 return ERROR_OK;
2795 static int turtle_init(void)
2797 low_output = 0x08;
2798 low_direction = 0x5b;
2800 /* initialize low byte for jtag */
2801 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2802 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2803 return ERROR_JTAG_INIT_FAILED;
2806 nSRST = 0x40;
2808 high_output = 0x00;
2809 high_direction = 0x0C;
2811 /* initialize high byte for jtag */
2812 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2813 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2814 return ERROR_JTAG_INIT_FAILED;
2817 return ERROR_OK;
2820 static int comstick_init(void)
2822 low_output = 0x08;
2823 low_direction = 0x0b;
2825 /* initialize low byte for jtag */
2826 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2827 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2828 return ERROR_JTAG_INIT_FAILED;
2831 nTRST = 0x01;
2832 nTRSTnOE = 0x00; /* no output enable for nTRST */
2833 nSRST = 0x02;
2834 nSRSTnOE = 0x00; /* no output enable for nSRST */
2836 high_output = 0x03;
2837 high_direction = 0x03;
2839 /* initialize high byte for jtag */
2840 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2841 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2842 return ERROR_JTAG_INIT_FAILED;
2845 return ERROR_OK;
2848 static int stm32stick_init(void)
2850 low_output = 0x88;
2851 low_direction = 0x8b;
2853 /* initialize low byte for jtag */
2854 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2855 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2856 return ERROR_JTAG_INIT_FAILED;
2859 nTRST = 0x01;
2860 nTRSTnOE = 0x00; /* no output enable for nTRST */
2861 nSRST = 0x80;
2862 nSRSTnOE = 0x00; /* no output enable for nSRST */
2864 high_output = 0x01;
2865 high_direction = 0x03;
2867 /* initialize high byte for jtag */
2868 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2869 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2870 return ERROR_JTAG_INIT_FAILED;
2873 return ERROR_OK;
2876 static int sheevaplug_init(void)
2878 low_output = 0x08;
2879 low_direction = 0x1b;
2881 /* initialize low byte for jtag */
2882 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2883 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2884 return ERROR_JTAG_INIT_FAILED;
2887 nTRSTnOE = 0x1;
2888 nTRST = 0x02;
2889 nSRSTnOE = 0x4;
2890 nSRST = 0x08;
2892 high_output = 0x0;
2893 high_direction = 0x0f;
2895 /* nTRST is always push-pull */
2896 high_output &= ~nTRSTnOE;
2897 high_output |= nTRST;
2899 /* nSRST is always open-drain */
2900 high_output |= nSRSTnOE;
2901 high_output &= ~nSRST;
2903 /* initialize high byte for jtag */
2904 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2905 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2906 return ERROR_JTAG_INIT_FAILED;
2909 return ERROR_OK;
2912 static int cortino_jtag_init(void)
2914 low_output = 0x08;
2915 low_direction = 0x1b;
2917 /* initialize low byte for jtag */
2918 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2919 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2920 return ERROR_JTAG_INIT_FAILED;
2923 nTRST = 0x01;
2924 nTRSTnOE = 0x00; /* no output enable for nTRST */
2925 nSRST = 0x02;
2926 nSRSTnOE = 0x00; /* no output enable for nSRST */
2928 high_output = 0x03;
2929 high_direction = 0x03;
2931 /* initialize high byte for jtag */
2932 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2933 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2934 return ERROR_JTAG_INIT_FAILED;
2937 return ERROR_OK;
2940 static int lisa_l_init(void)
2942 ftx232_dbus_init();
2944 nTRST = 0x10;
2945 nTRSTnOE = 0x10;
2946 nSRST = 0x40;
2947 nSRSTnOE = 0x40;
2949 high_output = 0x00;
2950 high_direction = 0x18;
2952 /* initialize high byte for jtag */
2953 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2954 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2955 return ERROR_JTAG_INIT_FAILED;
2958 return ftx232_dbus_write();
2961 static int flossjtag_init(void)
2963 ftx232_dbus_init();
2965 nTRST = 0x10;
2966 nTRSTnOE = 0x10;
2967 nSRST = 0x40;
2968 nSRSTnOE = 0x40;
2970 high_output = 0x00;
2971 high_direction = 0x18;
2973 /* initialize high byte for jtag */
2974 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2975 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2976 return ERROR_JTAG_INIT_FAILED;
2979 return ftx232_dbus_write();
2983 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
2984 * the door for a number of different configurations
2986 * Known Implementations:
2987 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
2989 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
2990 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
2991 * * ACBUS3 to transition 0->1 (OE rising edge)
2992 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
2993 * * ADBUS5/GPIOL1 = EMU_EN = 1
2994 * * ADBUS6/GPIOL2 = EMU0 = 0
2995 * * ACBUS4/SPARE0 = EMU1 = 0
2996 * * CPLD logic: Disable loopback
2997 * * ACBUS6/SPARE2 = LOOPBACK = 0
2999 #define XDS100_nEMU_EN (1<<5)
3000 #define XDS100_nEMU0 (1<<6)
3002 #define XDS100_PWR_RST (1<<3)
3003 #define XDS100_nEMU1 (1<<4)
3004 #define XDS100_LOOPBACK (1<<6)
3005 static int xds100v2_init(void)
3007 /* These are in the lower byte */
3008 nTRST = 0x10;
3009 nTRSTnOE = 0x10;
3011 /* These aren't actually used on 14 pin connectors
3012 * These are in the upper byte */
3013 nSRST = 0x01;
3014 nSRSTnOE = 0x01;
3016 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3017 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3019 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3020 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3021 return ERROR_JTAG_INIT_FAILED;
3024 high_output = 0;
3025 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3027 /* initialize high byte for jtag */
3028 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3029 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3030 return ERROR_JTAG_INIT_FAILED;
3033 high_output |= XDS100_PWR_RST;
3035 /* initialize high byte for jtag */
3036 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3037 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3038 return ERROR_JTAG_INIT_FAILED;
3041 return ERROR_OK;
3044 static void olimex_jtag_blink(void)
3046 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3047 * ACBUS3 is bit 3 of the GPIOH port
3049 high_output ^= 0x08;
3051 buffer_write(0x82);
3052 buffer_write(high_output);
3053 buffer_write(high_direction);
3056 static void flyswatter_jtag_blink(unsigned char led)
3058 buffer_write(0x82);
3059 buffer_write(high_output ^ led);
3060 buffer_write(high_direction);
3063 static void flyswatter1_jtag_blink(void)
3066 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3068 flyswatter_jtag_blink(0xc);
3071 static void flyswatter2_jtag_blink(void)
3074 * Flyswatter2 only has one LED connected to ACBUS2
3076 flyswatter_jtag_blink(0x4);
3079 static void turtle_jtag_blink(void)
3082 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3084 if (high_output & 0x08)
3085 high_output = 0x04;
3086 else
3087 high_output = 0x08;
3089 buffer_write(0x82);
3090 buffer_write(high_output);
3091 buffer_write(high_direction);
3094 static void lisa_l_blink(void)
3097 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3099 if (high_output & 0x10)
3100 high_output = 0x08;
3101 else
3102 high_output = 0x10;
3104 buffer_write(0x82);
3105 buffer_write(high_output);
3106 buffer_write(high_direction);
3109 static void flossjtag_blink(void)
3112 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3114 if (high_output & 0x10)
3115 high_output = 0x08;
3116 else
3117 high_output = 0x10;
3119 buffer_write(0x82);
3120 buffer_write(high_output);
3121 buffer_write(high_direction);
3124 static int ft2232_quit(void)
3126 #if BUILD_FT2232_FTD2XX == 1
3127 FT_STATUS status;
3129 status = FT_Close(ftdih);
3130 #elif BUILD_FT2232_LIBFTDI == 1
3131 ftdi_usb_close(&ftdic);
3133 ftdi_deinit(&ftdic);
3134 #endif
3136 free(ft2232_buffer);
3137 ft2232_buffer = NULL;
3139 return ERROR_OK;
3142 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3144 char *cp;
3145 char buf[200];
3146 if (CMD_ARGC == 1) {
3147 ft2232_device_desc = strdup(CMD_ARGV[0]);
3148 cp = strchr(ft2232_device_desc, 0);
3149 /* under Win32, the FTD2XX driver appends an "A" to the end
3150 * of the description, this examines the given desc
3151 * and creates the 'missing' _A or non_A variable. */
3152 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3153 /* it was, so make this the "A" version. */
3154 ft2232_device_desc_A = ft2232_device_desc;
3155 /* and *CREATE* the non-A version. */
3156 strcpy(buf, ft2232_device_desc);
3157 cp = strchr(buf, 0);
3158 cp[-2] = 0;
3159 ft2232_device_desc = strdup(buf);
3160 } else {
3161 /* <space > A not defined
3162 * so create it */
3163 sprintf(buf, "%s A", ft2232_device_desc);
3164 ft2232_device_desc_A = strdup(buf);
3166 } else
3167 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3169 return ERROR_OK;
3172 COMMAND_HANDLER(ft2232_handle_serial_command)
3174 if (CMD_ARGC == 1)
3175 ft2232_serial = strdup(CMD_ARGV[0]);
3176 else
3177 return ERROR_COMMAND_SYNTAX_ERROR;
3179 return ERROR_OK;
3182 COMMAND_HANDLER(ft2232_handle_layout_command)
3184 if (CMD_ARGC != 1)
3185 return ERROR_COMMAND_SYNTAX_ERROR;
3187 if (layout) {
3188 LOG_ERROR("already specified ft2232_layout %s",
3189 layout->name);
3190 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3191 ? ERROR_FAIL
3192 : ERROR_OK;
3195 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3196 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3197 layout = l;
3198 return ERROR_OK;
3202 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3203 return ERROR_FAIL;
3206 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3208 if (CMD_ARGC > MAX_USB_IDS * 2) {
3209 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3210 "(maximum is %d pairs)", MAX_USB_IDS);
3211 CMD_ARGC = MAX_USB_IDS * 2;
3213 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3214 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3215 if (CMD_ARGC < 2)
3216 return ERROR_COMMAND_SYNTAX_ERROR;
3217 /* remove the incomplete trailing id */
3218 CMD_ARGC -= 1;
3221 unsigned i;
3222 for (i = 0; i < CMD_ARGC; i += 2) {
3223 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3224 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3228 * Explicitly terminate, in case there are multiples instances of
3229 * ft2232_vid_pid.
3231 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3233 return ERROR_OK;
3236 COMMAND_HANDLER(ft2232_handle_latency_command)
3238 if (CMD_ARGC == 1)
3239 ft2232_latency = atoi(CMD_ARGV[0]);
3240 else
3241 return ERROR_COMMAND_SYNTAX_ERROR;
3243 return ERROR_OK;
3246 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3248 int retval = 0;
3250 /* 7 bits of either ones or zeros. */
3251 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3253 while (num_cycles > 0) {
3254 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3255 * at most 7 bits per invocation. Here we invoke it potentially
3256 * several times.
3258 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3260 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3261 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3262 retval = ERROR_JTAG_QUEUE_FAILED;
3264 first_unsent = cmd;
3267 /* there are no state transitions in this code, so omit state tracking */
3269 /* command "Clock Data to TMS/CS Pin (no Read)" */
3270 buffer_write(0x4b);
3272 /* scan 7 bit */
3273 buffer_write(bitcount_per_command - 1);
3275 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3276 buffer_write(tms);
3278 require_send = 1;
3280 num_cycles -= bitcount_per_command;
3283 return retval;
3286 /* ---------------------------------------------------------------------
3287 * Support for IceBear JTAG adapter from Section5:
3288 * http://section5.ch/icebear
3290 * Author: Sten, debian@sansys-electronic.com
3293 /* Icebear pin layout
3295 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3296 * GND GND | 4 3| n.c.
3297 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3298 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3299 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3300 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3301 * ADBUS2 TDO |14 13| GND GND
3303 * ADBUS0 O L TCK ACBUS0 GND
3304 * ADBUS1 O L TDI ACBUS1 GND
3305 * ADBUS2 I TDO ACBUS2 n.c.
3306 * ADBUS3 O H TMS ACBUS3 n.c.
3307 * ADBUS4 O H nTRST
3308 * ADBUS5 O H nSRST
3309 * ADBUS6 - VCC
3310 * ADBUS7 - GND
3312 static int icebear_jtag_init(void)
3314 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3315 low_output = 0x08; /* high: TMS; low: TCK TDI */
3316 nTRST = 0x10;
3317 nSRST = 0x20;
3319 enum reset_types jtag_reset_config = jtag_get_reset_config();
3320 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3321 low_direction &= ~nTRST; /* nTRST high impedance */
3322 else {
3323 low_direction |= nTRST;
3324 low_output |= nTRST;
3327 low_direction |= nSRST;
3328 low_output |= nSRST;
3330 /* initialize low byte for jtag */
3331 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3332 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3333 return ERROR_JTAG_INIT_FAILED;
3336 high_output = 0x0;
3337 high_direction = 0x00;
3339 /* initialize high byte for jtag */
3340 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3341 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3342 return ERROR_JTAG_INIT_FAILED;
3345 return ERROR_OK;
3348 static void icebear_jtag_reset(int trst, int srst)
3350 if (trst == 1) {
3351 low_direction |= nTRST;
3352 low_output &= ~nTRST;
3353 } else if (trst == 0) {
3354 enum reset_types jtag_reset_config = jtag_get_reset_config();
3355 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3356 low_direction &= ~nTRST;
3357 else
3358 low_output |= nTRST;
3361 if (srst == 1)
3362 low_output &= ~nSRST;
3363 else if (srst == 0)
3364 low_output |= nSRST;
3366 /* command "set data bits low byte" */
3367 buffer_write(0x80);
3368 buffer_write(low_output);
3369 buffer_write(low_direction);
3371 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3372 trst,
3373 srst,
3374 low_output,
3375 low_direction);
3378 /* ---------------------------------------------------------------------
3379 * Support for Signalyzer H2 and Signalyzer H4
3380 * JTAG adapter from Xverve Technologies Inc.
3381 * http://www.signalyzer.com or http://www.xverve.com
3383 * Author: Oleg Seiljus, oleg@signalyzer.com
3385 static unsigned char signalyzer_h_side;
3386 static unsigned int signalyzer_h_adapter_type;
3388 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3390 #if BUILD_FT2232_FTD2XX == 1
3391 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3392 #endif
3394 #define SIGNALYZER_COMMAND_ADDR 128
3395 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3397 #define SIGNALYZER_COMMAND_VERSION 0x41
3398 #define SIGNALYZER_COMMAND_RESET 0x42
3399 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3400 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3401 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3402 #define SIGNALYZER_COMMAND_LED_SET 0x53
3403 #define SIGNALYZER_COMMAND_ADC 0x54
3404 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3405 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3406 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3407 #define SIGNALYZER_COMMAND_I2C 0x58
3409 #define SIGNALYZER_CHAN_A 1
3410 #define SIGNALYZER_CHAN_B 2
3411 /* LEDS use channel C */
3412 #define SIGNALYZER_CHAN_C 4
3414 #define SIGNALYZER_LED_GREEN 1
3415 #define SIGNALYZER_LED_RED 2
3417 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3418 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3419 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3420 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3421 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3424 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3426 #if BUILD_FT2232_FTD2XX == 1
3427 return FT_WriteEE(ftdih, address, value);
3428 #elif BUILD_FT2232_LIBFTDI == 1
3429 return 0;
3430 #endif
3433 #if BUILD_FT2232_FTD2XX == 1
3434 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3436 return FT_ReadEE(ftdih, address, value);
3438 #endif
3440 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3441 int on_time_ms, int off_time_ms, unsigned char cycles)
3443 unsigned char on_time;
3444 unsigned char off_time;
3446 if (on_time_ms < 0xFFFF)
3447 on_time = (unsigned char)(on_time_ms / 62);
3448 else
3449 on_time = 0xFF;
3451 off_time = (unsigned char)(off_time_ms / 62);
3453 #if BUILD_FT2232_FTD2XX == 1
3454 FT_STATUS status;
3456 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3457 ((uint32_t)(channel << 8) | led));
3458 if (status != FT_OK) {
3459 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3460 ftd2xx_status_string(status));
3461 return ERROR_JTAG_DEVICE_ERROR;
3464 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3465 ((uint32_t)(on_time << 8) | off_time));
3466 if (status != FT_OK) {
3467 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3468 ftd2xx_status_string(status));
3469 return ERROR_JTAG_DEVICE_ERROR;
3472 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3473 ((uint32_t)cycles));
3474 if (status != FT_OK) {
3475 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3476 ftd2xx_status_string(status));
3477 return ERROR_JTAG_DEVICE_ERROR;
3480 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3481 SIGNALYZER_COMMAND_LED_SET);
3482 if (status != FT_OK) {
3483 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3484 ftd2xx_status_string(status));
3485 return ERROR_JTAG_DEVICE_ERROR;
3488 return ERROR_OK;
3489 #elif BUILD_FT2232_LIBFTDI == 1
3490 int retval;
3492 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3493 ((uint32_t)(channel << 8) | led));
3494 if (retval < 0) {
3495 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3496 ftdi_get_error_string(&ftdic));
3497 return ERROR_JTAG_DEVICE_ERROR;
3500 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3501 ((uint32_t)(on_time << 8) | off_time));
3502 if (retval < 0) {
3503 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3504 ftdi_get_error_string(&ftdic));
3505 return ERROR_JTAG_DEVICE_ERROR;
3508 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3509 (uint32_t)cycles);
3510 if (retval < 0) {
3511 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3512 ftdi_get_error_string(&ftdic));
3513 return ERROR_JTAG_DEVICE_ERROR;
3516 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3517 SIGNALYZER_COMMAND_LED_SET);
3518 if (retval < 0) {
3519 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3520 ftdi_get_error_string(&ftdic));
3521 return ERROR_JTAG_DEVICE_ERROR;
3524 return ERROR_OK;
3525 #endif
3528 static int signalyzer_h_init(void)
3530 #if BUILD_FT2232_FTD2XX == 1
3531 FT_STATUS status;
3532 int i;
3533 #endif
3535 char *end_of_desc;
3537 uint16_t read_buf[12] = { 0 };
3539 /* turn on center green led */
3540 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3541 0xFFFF, 0x00, 0x00);
3543 /* determine what channel config wants to open
3544 * TODO: change me... current implementation is made to work
3545 * with openocd description parsing.
3547 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3549 if (end_of_desc) {
3550 signalyzer_h_side = *(end_of_desc - 1);
3551 if (signalyzer_h_side == 'B')
3552 signalyzer_h_side = SIGNALYZER_CHAN_B;
3553 else
3554 signalyzer_h_side = SIGNALYZER_CHAN_A;
3555 } else {
3556 LOG_ERROR("No Channel was specified");
3557 return ERROR_FAIL;
3560 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3561 1000, 1000, 0xFF);
3563 #if BUILD_FT2232_FTD2XX == 1
3564 /* read signalyzer versionining information */
3565 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3566 SIGNALYZER_COMMAND_VERSION);
3567 if (status != FT_OK) {
3568 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3569 ftd2xx_status_string(status));
3570 return ERROR_JTAG_DEVICE_ERROR;
3573 for (i = 0; i < 10; i++) {
3574 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3575 &read_buf[i]);
3576 if (status != FT_OK) {
3577 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3578 ftd2xx_status_string(status));
3579 return ERROR_JTAG_DEVICE_ERROR;
3583 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3584 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3585 read_buf[4], read_buf[5], read_buf[6]);
3587 /* set gpio register */
3588 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3589 (uint32_t)(signalyzer_h_side << 8));
3590 if (status != FT_OK) {
3591 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3592 ftd2xx_status_string(status));
3593 return ERROR_JTAG_DEVICE_ERROR;
3596 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
3597 if (status != FT_OK) {
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3599 ftd2xx_status_string(status));
3600 return ERROR_JTAG_DEVICE_ERROR;
3603 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3604 SIGNALYZER_COMMAND_GPIO_STATE);
3605 if (status != FT_OK) {
3606 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3607 ftd2xx_status_string(status));
3608 return ERROR_JTAG_DEVICE_ERROR;
3611 /* read adapter type information */
3612 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3613 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3614 if (status != FT_OK) {
3615 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3616 ftd2xx_status_string(status));
3617 return ERROR_JTAG_DEVICE_ERROR;
3620 status = signalyzer_h_ctrl_write(
3621 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3622 if (status != FT_OK) {
3623 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3624 ftd2xx_status_string(status));
3625 return ERROR_JTAG_DEVICE_ERROR;
3628 status = signalyzer_h_ctrl_write(
3629 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3630 if (status != FT_OK) {
3631 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3632 ftd2xx_status_string(status));
3633 return ERROR_JTAG_DEVICE_ERROR;
3636 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3637 SIGNALYZER_COMMAND_I2C);
3638 if (status != FT_OK) {
3639 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3640 ftd2xx_status_string(status));
3641 return ERROR_JTAG_DEVICE_ERROR;
3644 usleep(100000);
3646 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3647 if (status != FT_OK) {
3648 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3649 ftd2xx_status_string(status));
3650 return ERROR_JTAG_DEVICE_ERROR;
3653 if (read_buf[0] != 0x0498)
3654 signalyzer_h_adapter_type = 0x0000;
3655 else {
3656 for (i = 0; i < 4; i++) {
3657 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3658 if (status != FT_OK) {
3659 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3660 ftd2xx_status_string(status));
3661 return ERROR_JTAG_DEVICE_ERROR;
3665 signalyzer_h_adapter_type = read_buf[0];
3668 #elif BUILD_FT2232_LIBFTDI == 1
3669 /* currently libftdi does not allow reading individual eeprom
3670 * locations, therefore adapter type cannot be detected.
3671 * override with most common type
3673 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3674 #endif
3676 enum reset_types jtag_reset_config = jtag_get_reset_config();
3678 /* ADAPTOR: EM_LT16_A */
3679 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3680 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3681 "detected. (HW: %2x).", (read_buf[1] >> 8));
3683 nTRST = 0x10;
3684 nTRSTnOE = 0x10;
3685 nSRST = 0x20;
3686 nSRSTnOE = 0x20;
3688 low_output = 0x08;
3689 low_direction = 0x1b;
3691 high_output = 0x0;
3692 high_direction = 0x0;
3694 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3695 low_direction &= ~nTRSTnOE; /* nTRST input */
3696 low_output &= ~nTRST; /* nTRST = 0 */
3697 } else {
3698 low_direction |= nTRSTnOE; /* nTRST output */
3699 low_output |= nTRST; /* nTRST = 1 */
3702 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3703 low_direction |= nSRSTnOE; /* nSRST output */
3704 low_output |= nSRST; /* nSRST = 1 */
3705 } else {
3706 low_direction &= ~nSRSTnOE; /* nSRST input */
3707 low_output &= ~nSRST; /* nSRST = 0 */
3710 #if BUILD_FT2232_FTD2XX == 1
3711 /* enable power to the module */
3712 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3713 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3714 if (status != FT_OK) {
3715 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3716 ftd2xx_status_string(status));
3717 return ERROR_JTAG_DEVICE_ERROR;
3720 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3721 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3722 if (status != FT_OK) {
3723 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3724 ftd2xx_status_string(status));
3725 return ERROR_JTAG_DEVICE_ERROR;
3728 /* set gpio mode register */
3729 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3730 (uint32_t)(signalyzer_h_side << 8));
3731 if (status != FT_OK) {
3732 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3733 ftd2xx_status_string(status));
3734 return ERROR_JTAG_DEVICE_ERROR;
3737 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3738 if (status != FT_OK) {
3739 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3740 ftd2xx_status_string(status));
3741 return ERROR_JTAG_DEVICE_ERROR;
3744 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3745 if (status != FT_OK) {
3746 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3747 ftd2xx_status_string(status));
3748 return ERROR_JTAG_DEVICE_ERROR;
3751 /* set gpio register */
3752 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3753 (uint32_t)(signalyzer_h_side << 8));
3754 if (status != FT_OK) {
3755 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3756 ftd2xx_status_string(status));
3757 return ERROR_JTAG_DEVICE_ERROR;
3760 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040);
3761 if (status != FT_OK) {
3762 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3763 ftd2xx_status_string(status));
3764 return ERROR_JTAG_DEVICE_ERROR;
3767 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3768 SIGNALYZER_COMMAND_GPIO_STATE);
3769 if (status != FT_OK) {
3770 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3771 ftd2xx_status_string(status));
3772 return ERROR_JTAG_DEVICE_ERROR;
3774 #endif
3776 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3777 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3778 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3779 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3780 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3781 if (signalyzer_h_adapter_type
3782 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3783 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3784 "detected. (HW: %2x).", (read_buf[1] >> 8));
3785 else if (signalyzer_h_adapter_type
3786 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3787 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3788 "(ARM JTAG with PSU) detected. (HW: %2x).",
3789 (read_buf[1] >> 8));
3790 else if (signalyzer_h_adapter_type
3791 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3792 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3793 "detected. (HW: %2x).", (read_buf[1] >> 8));
3794 else if (signalyzer_h_adapter_type
3795 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3796 LOG_INFO("Signalyzer: EM-JTAG-P "
3797 "(Generic JTAG with PSU) detected. (HW: %2x).",
3798 (read_buf[1] >> 8));
3800 nTRST = 0x02;
3801 nTRSTnOE = 0x04;
3802 nSRST = 0x08;
3803 nSRSTnOE = 0x10;
3805 low_output = 0x08;
3806 low_direction = 0x1b;
3808 high_output = 0x0;
3809 high_direction = 0x1f;
3811 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3812 high_output |= nTRSTnOE;
3813 high_output &= ~nTRST;
3814 } else {
3815 high_output &= ~nTRSTnOE;
3816 high_output |= nTRST;
3819 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3820 high_output &= ~nSRSTnOE;
3821 high_output |= nSRST;
3822 } else {
3823 high_output |= nSRSTnOE;
3824 high_output &= ~nSRST;
3827 #if BUILD_FT2232_FTD2XX == 1
3828 /* enable power to the module */
3829 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3830 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3831 if (status != FT_OK) {
3832 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3833 ftd2xx_status_string(status));
3834 return ERROR_JTAG_DEVICE_ERROR;
3837 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3838 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3839 if (status != FT_OK) {
3840 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3841 ftd2xx_status_string(status));
3842 return ERROR_JTAG_DEVICE_ERROR;
3845 /* set gpio mode register (IO_16 and IO_17 set as analog
3846 * inputs, other is gpio)
3848 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3849 (uint32_t)(signalyzer_h_side << 8));
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_DATA_BUFFER_ADDR + 1, 0x0060);
3857 if (status != FT_OK) {
3858 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3859 ftd2xx_status_string(status));
3860 return ERROR_JTAG_DEVICE_ERROR;
3863 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3864 if (status != FT_OK) {
3865 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3866 ftd2xx_status_string(status));
3867 return ERROR_JTAG_DEVICE_ERROR;
3870 /* set gpio register (all inputs, for -P modules,
3871 * PSU will be turned off)
3873 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3874 (uint32_t)(signalyzer_h_side << 8));
3875 if (status != FT_OK) {
3876 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3877 ftd2xx_status_string(status));
3878 return ERROR_JTAG_DEVICE_ERROR;
3881 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3882 if (status != FT_OK) {
3883 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3884 ftd2xx_status_string(status));
3885 return ERROR_JTAG_DEVICE_ERROR;
3888 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
3889 if (status != FT_OK) {
3890 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3891 ftd2xx_status_string(status));
3892 return ERROR_JTAG_DEVICE_ERROR;
3894 #endif
3895 } else if (signalyzer_h_adapter_type == 0x0000) {
3896 LOG_INFO("Signalyzer: No external modules were detected.");
3898 nTRST = 0x10;
3899 nTRSTnOE = 0x10;
3900 nSRST = 0x20;
3901 nSRSTnOE = 0x20;
3903 low_output = 0x08;
3904 low_direction = 0x1b;
3906 high_output = 0x0;
3907 high_direction = 0x0;
3909 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3910 low_direction &= ~nTRSTnOE; /* nTRST input */
3911 low_output &= ~nTRST; /* nTRST = 0 */
3912 } else {
3913 low_direction |= nTRSTnOE; /* nTRST output */
3914 low_output |= nTRST; /* nTRST = 1 */
3917 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3918 low_direction |= nSRSTnOE; /* nSRST output */
3919 low_output |= nSRST; /* nSRST = 1 */
3920 } else {
3921 low_direction &= ~nSRSTnOE; /* nSRST input */
3922 low_output &= ~nSRST; /* nSRST = 0 */
3924 } else {
3925 LOG_ERROR("Unknown module type is detected: %.4x",
3926 signalyzer_h_adapter_type);
3927 return ERROR_JTAG_DEVICE_ERROR;
3930 /* initialize low byte of controller for jtag operation */
3931 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3932 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3933 return ERROR_JTAG_INIT_FAILED;
3936 #if BUILD_FT2232_FTD2XX == 1
3937 if (ftdi_device == FT_DEVICE_2232H) {
3938 /* initialize high byte of controller for jtag operation */
3939 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3940 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3941 return ERROR_JTAG_INIT_FAILED;
3944 #elif BUILD_FT2232_LIBFTDI == 1
3945 if (ftdi_device == TYPE_2232H) {
3946 /* initialize high byte of controller for jtag operation */
3947 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3948 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3949 return ERROR_JTAG_INIT_FAILED;
3952 #endif
3953 return ERROR_OK;
3956 static void signalyzer_h_reset(int trst, int srst)
3958 enum reset_types jtag_reset_config = jtag_get_reset_config();
3960 /* ADAPTOR: EM_LT16_A */
3961 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3962 if (trst == 1) {
3963 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3964 /* switch to output pin (output is low) */
3965 low_direction |= nTRSTnOE;
3966 else
3967 /* switch output low */
3968 low_output &= ~nTRST;
3969 } else if (trst == 0) {
3970 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3971 /* switch to input pin (high-Z + internal
3972 * and external pullup) */
3973 low_direction &= ~nTRSTnOE;
3974 else
3975 /* switch output high */
3976 low_output |= nTRST;
3979 if (srst == 1) {
3980 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3981 /* switch output low */
3982 low_output &= ~nSRST;
3983 else
3984 /* switch to output pin (output is low) */
3985 low_direction |= nSRSTnOE;
3986 } else if (srst == 0) {
3987 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3988 /* switch output high */
3989 low_output |= nSRST;
3990 else
3991 /* switch to input pin (high-Z) */
3992 low_direction &= ~nSRSTnOE;
3995 /* command "set data bits low byte" */
3996 buffer_write(0x80);
3997 buffer_write(low_output);
3998 buffer_write(low_direction);
3999 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4000 "low_direction: 0x%2.2x",
4001 trst, srst, low_output, low_direction);
4003 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4004 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4005 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4006 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4007 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4008 if (trst == 1) {
4009 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4010 high_output &= ~nTRSTnOE;
4011 else
4012 high_output &= ~nTRST;
4013 } else if (trst == 0) {
4014 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4015 high_output |= nTRSTnOE;
4016 else
4017 high_output |= nTRST;
4020 if (srst == 1) {
4021 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4022 high_output &= ~nSRST;
4023 else
4024 high_output &= ~nSRSTnOE;
4025 } else if (srst == 0) {
4026 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4027 high_output |= nSRST;
4028 else
4029 high_output |= nSRSTnOE;
4032 /* command "set data bits high byte" */
4033 buffer_write(0x82);
4034 buffer_write(high_output);
4035 buffer_write(high_direction);
4036 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4037 "high_direction: 0x%2.2x",
4038 trst, srst, high_output, high_direction);
4039 } else if (signalyzer_h_adapter_type == 0x0000) {
4040 if (trst == 1) {
4041 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4042 /* switch to output pin (output is low) */
4043 low_direction |= nTRSTnOE;
4044 else
4045 /* switch output low */
4046 low_output &= ~nTRST;
4047 } else if (trst == 0) {
4048 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4049 /* switch to input pin (high-Z + internal
4050 * and external pullup) */
4051 low_direction &= ~nTRSTnOE;
4052 else
4053 /* switch output high */
4054 low_output |= nTRST;
4057 if (srst == 1) {
4058 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4059 /* switch output low */
4060 low_output &= ~nSRST;
4061 else
4062 /* switch to output pin (output is low) */
4063 low_direction |= nSRSTnOE;
4064 } else if (srst == 0) {
4065 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4066 /* switch output high */
4067 low_output |= nSRST;
4068 else
4069 /* switch to input pin (high-Z) */
4070 low_direction &= ~nSRSTnOE;
4073 /* command "set data bits low byte" */
4074 buffer_write(0x80);
4075 buffer_write(low_output);
4076 buffer_write(low_direction);
4077 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4078 "low_direction: 0x%2.2x",
4079 trst, srst, low_output, low_direction);
4083 static void signalyzer_h_blink(void)
4085 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4088 /********************************************************************
4089 * Support for KT-LINK
4090 * JTAG adapter from KRISTECH
4091 * http://www.kristech.eu
4092 *******************************************************************/
4093 static int ktlink_init(void)
4095 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4097 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4098 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4100 /* initialize low byte for jtag */
4101 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4102 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4103 return ERROR_JTAG_INIT_FAILED;
4106 nTRST = 0x01;
4107 nSRST = 0x02;
4108 nTRSTnOE = 0x04;
4109 nSRSTnOE = 0x08;
4111 high_output = 0x80; /* turn LED on */
4112 high_direction = 0xFF; /* all outputs */
4114 enum reset_types jtag_reset_config = jtag_get_reset_config();
4116 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4117 high_output |= nTRSTnOE;
4118 high_output &= ~nTRST;
4119 } else {
4120 high_output &= ~nTRSTnOE;
4121 high_output |= nTRST;
4124 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4125 high_output &= ~nSRSTnOE;
4126 high_output |= nSRST;
4127 } else {
4128 high_output |= nSRSTnOE;
4129 high_output &= ~nSRST;
4132 /* initialize high byte for jtag */
4133 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4134 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4135 return ERROR_JTAG_INIT_FAILED;
4138 return ERROR_OK;
4141 static void ktlink_reset(int trst, int srst)
4143 enum reset_types jtag_reset_config = jtag_get_reset_config();
4145 if (trst == 1) {
4146 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4147 high_output &= ~nTRSTnOE;
4148 else
4149 high_output &= ~nTRST;
4150 } else if (trst == 0) {
4151 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4152 high_output |= nTRSTnOE;
4153 else
4154 high_output |= nTRST;
4157 if (srst == 1) {
4158 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4159 high_output &= ~nSRST;
4160 else
4161 high_output &= ~nSRSTnOE;
4162 } else if (srst == 0) {
4163 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4164 high_output |= nSRST;
4165 else
4166 high_output |= nSRSTnOE;
4169 buffer_write(0x82); /* command "set data bits high byte" */
4170 buffer_write(high_output);
4171 buffer_write(high_direction);
4172 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4173 trst,
4174 srst,
4175 high_output,
4176 high_direction);
4179 static void ktlink_blink(void)
4181 /* LED connected to ACBUS7 */
4182 high_output ^= 0x80;
4184 buffer_write(0x82); /* command "set data bits high byte" */
4185 buffer_write(high_output);
4186 buffer_write(high_direction);
4189 /********************************************************************
4190 * Support for Digilent HS-1
4191 * JTAG adapter from Digilent
4192 * http://www.digilent.com
4193 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4194 *******************************************************************/
4196 static int digilent_hs1_init(void)
4198 /* the adapter only supports the base JTAG signals, no nTRST
4199 nor nSRST */
4200 low_output = 0x88;
4201 low_direction = 0x8b;
4203 /* initialize low byte for jtag */
4204 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4205 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4206 return ERROR_JTAG_INIT_FAILED;
4208 return ERROR_OK;
4211 static void digilent_hs1_reset(int trst, int srst)
4213 /* Dummy function, no reset signals supported. */
4216 static const struct command_registration ft2232_command_handlers[] = {
4218 .name = "ft2232_device_desc",
4219 .handler = &ft2232_handle_device_desc_command,
4220 .mode = COMMAND_CONFIG,
4221 .help = "set the USB device description of the FTDI FT2232 device",
4222 .usage = "description_string",
4225 .name = "ft2232_serial",
4226 .handler = &ft2232_handle_serial_command,
4227 .mode = COMMAND_CONFIG,
4228 .help = "set the serial number of the FTDI FT2232 device",
4229 .usage = "serial_string",
4232 .name = "ft2232_layout",
4233 .handler = &ft2232_handle_layout_command,
4234 .mode = COMMAND_CONFIG,
4235 .help = "set the layout of the FT2232 GPIO signals used "
4236 "to control output-enables and reset signals",
4237 .usage = "layout_name",
4240 .name = "ft2232_vid_pid",
4241 .handler = &ft2232_handle_vid_pid_command,
4242 .mode = COMMAND_CONFIG,
4243 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4244 .usage = "(vid pid)* ",
4247 .name = "ft2232_latency",
4248 .handler = &ft2232_handle_latency_command,
4249 .mode = COMMAND_CONFIG,
4250 .help = "set the FT2232 latency timer to a new value",
4251 .usage = "value",
4253 COMMAND_REGISTRATION_DONE
4256 struct jtag_interface ft2232_interface = {
4257 .name = "ft2232",
4258 .supported = DEBUG_CAP_TMS_SEQ,
4259 .commands = ft2232_command_handlers,
4260 .transports = jtag_only,
4262 .init = ft2232_init,
4263 .quit = ft2232_quit,
4264 .speed = ft2232_speed,
4265 .speed_div = ft2232_speed_div,
4266 .khz = ft2232_khz,
4267 .execute_queue = ft2232_execute_queue,