ST-LINK USB initial release
[openocd/andreasf.git] / src / jtag / drivers / ft2232.c
blob73aea01f673a0830bf6792f6e64ccee86696b3a8
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
106 INTERFACE_ANY = 0,
107 INTERFACE_A = 1,
108 INTERFACE_B = 2,
109 INTERFACE_C = 3,
110 INTERFACE_D = 4
113 #elif BUILD_FT2232_LIBFTDI == 1
114 #include <ftdi.h>
115 #endif
117 /* max TCK for the high speed devices 30000 kHz */
118 #define FTDI_2232H_4232H_MAX_TCK 30000
119 /* max TCK for the full speed devices 6000 kHz */
120 #define FTDI_2232C_MAX_TCK 6000
121 /* this speed value tells that RTCK is requested */
122 #define RTCK_SPEED -1
125 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
126 * errors with a retry count of 100. Increasing it solves the problem for me.
127 * - Dimitar
129 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
130 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
131 * to something sane.
133 #define LIBFTDI_READ_RETRY_COUNT 2000
135 #ifndef BUILD_FT2232_HIGHSPEED
136 #if BUILD_FT2232_FTD2XX == 1
137 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
138 #elif BUILD_FT2232_LIBFTDI == 1
139 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
140 #endif
141 #endif
144 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
145 * stable state. Calling code must ensure that current state is stable,
146 * that verification is not done in here.
148 * @param num_cycles The number of clocks cycles to send.
149 * @param cmd The command to send.
151 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
153 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
155 static char * ft2232_device_desc_A = NULL;
156 static char* ft2232_device_desc = NULL;
157 static char* ft2232_serial = NULL;
158 static uint8_t ft2232_latency = 2;
159 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
161 #define MAX_USB_IDS 8
162 /* vid = pid = 0 marks the end of the list */
163 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
164 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
166 struct ft2232_layout {
167 char* name;
168 int (*init)(void);
169 void (*reset)(int trst, int srst);
170 void (*blink)(void);
171 int channel;
174 /* init procedures for supported layouts */
175 static int usbjtag_init(void);
176 static int jtagkey_init(void);
177 static int lm3s811_jtag_init(void);
178 static int icdi_jtag_init(void);
179 static int olimex_jtag_init(void);
180 static int flyswatter1_init(void);
181 static int flyswatter2_init(void);
182 static int minimodule_init(void);
183 static int turtle_init(void);
184 static int comstick_init(void);
185 static int stm32stick_init(void);
186 static int axm0432_jtag_init(void);
187 static int sheevaplug_init(void);
188 static int icebear_jtag_init(void);
189 static int cortino_jtag_init(void);
190 static int signalyzer_init(void);
191 static int signalyzer_h_init(void);
192 static int ktlink_init(void);
193 static int redbee_init(void);
194 static int lisa_l_init(void);
195 static int flossjtag_init(void);
196 static int xds100v2_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);
216 /* blink procedures for layouts that support a blinking led */
217 static void olimex_jtag_blink(void);
218 static void flyswatter1_jtag_blink(void);
219 static void flyswatter2_jtag_blink(void);
220 static void turtle_jtag_blink(void);
221 static void signalyzer_h_blink(void);
222 static void ktlink_blink(void);
223 static void lisa_l_blink(void);
224 static void flossjtag_blink(void);
226 /* common transport support options */
228 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
230 static const struct ft2232_layout ft2232_layouts[] =
232 { .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 = NULL, /* END OF TABLE */ },
345 /* bitmask used to drive nTRST; usually a GPIOLx signal */
346 static uint8_t nTRST;
347 static uint8_t nTRSTnOE;
348 /* bitmask used to drive nSRST; usually a GPIOLx signal */
349 static uint8_t nSRST;
350 static uint8_t nSRSTnOE;
352 /** the layout being used with this debug session */
353 static const struct ft2232_layout *layout;
355 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
356 static uint8_t low_output = 0x0;
358 /* note that direction bit == 1 means that signal is an output */
360 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
361 static uint8_t low_direction = 0x0;
362 /** default value bitmask for CBUS GPIOH(0..4) */
363 static uint8_t high_output = 0x0;
364 /** default direction bitmask for CBUS GPIOH(0..4) */
365 static uint8_t high_direction = 0x0;
367 #if BUILD_FT2232_FTD2XX == 1
368 static FT_HANDLE ftdih = NULL;
369 static FT_DEVICE ftdi_device = 0;
370 #elif BUILD_FT2232_LIBFTDI == 1
371 static struct ftdi_context ftdic;
372 static enum ftdi_chip_type ftdi_device;
373 #endif
375 static struct jtag_command* first_unsent; /* next command that has to be sent */
376 static int require_send;
378 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
380 "There is a significant difference between libftdi and libftd2xx. The latter
381 one allows to schedule up to 64*64 bytes of result data while libftdi fails
382 with more than 4*64. As a consequence, the FT2232 driver is forced to
383 perform around 16x more USB transactions for long command streams with TDO
384 capture when running with libftdi."
386 No idea how we get
387 #define FT2232_BUFFER_SIZE 131072
388 a comment would have been nice.
391 #if BUILD_FT2232_FTD2XX == 1
392 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
393 #else
394 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
395 #endif
397 #define FT2232_BUFFER_SIZE 131072
399 static uint8_t* ft2232_buffer = NULL;
400 static int ft2232_buffer_size = 0;
401 static int ft2232_read_pointer = 0;
402 static int ft2232_expect_read = 0;
405 * Function buffer_write
406 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
407 * @param val is the byte to send.
409 static inline void buffer_write(uint8_t val)
411 assert(ft2232_buffer);
412 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
413 ft2232_buffer[ft2232_buffer_size++] = val;
417 * Function buffer_read
418 * returns a byte from the byte buffer.
420 static inline uint8_t buffer_read(void)
422 assert(ft2232_buffer);
423 assert(ft2232_read_pointer < ft2232_buffer_size);
424 return ft2232_buffer[ft2232_read_pointer++];
428 * Clocks out \a bit_count bits on the TMS line, starting with the least
429 * significant bit of tms_bits and progressing to more significant bits.
430 * Rigorous state transition logging is done here via tap_set_state().
432 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
433 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
434 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
435 * is often used for this, 0x4b.
437 * @param tms_bits Holds the sequence of bits to send.
438 * @param tms_count Tells how many bits in the sequence.
439 * @param tdi_bit A single bit to pass on to TDI before the first TCK
440 * cycle and held static for the duration of TMS clocking.
442 * See the MPSSE spec referenced above.
444 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
446 uint8_t tms_byte;
447 int i;
448 int tms_ndx; /* bit index into tms_byte */
450 assert(tms_count > 0);
452 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
453 mpsse_cmd, tms_bits, tms_count);
455 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
457 bool bit = tms_bits & 1;
459 if (bit)
460 tms_byte |= (1 << tms_ndx);
462 /* always do state transitions in public view */
463 tap_set_state(tap_state_transition(tap_get_state(), bit));
465 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
466 also increment.
468 ++tms_ndx;
470 if (tms_ndx == 7 || i == tms_count-1)
472 buffer_write(mpsse_cmd);
473 buffer_write(tms_ndx - 1);
475 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
476 TMS/CS and is held static for the duration of TMS/CS clocking.
478 buffer_write(tms_byte | (tdi_bit << 7));
484 * Function get_tms_buffer_requirements
485 * returns what clock_tms() will consume if called with
486 * same \a bit_count.
488 static inline int get_tms_buffer_requirements(int bit_count)
490 return ((bit_count + 6)/7) * 3;
494 * Function move_to_state
495 * moves the TAP controller from the current state to a
496 * \a goal_state through a path given by tap_get_tms_path(). State transition
497 * logging is performed by delegation to clock_tms().
499 * @param goal_state is the destination state for the move.
501 static void move_to_state(tap_state_t goal_state)
503 tap_state_t start_state = tap_get_state();
505 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
506 lookup of the required TMS pattern to move to this state from the
507 start state.
510 /* do the 2 lookups */
511 int tms_bits = tap_get_tms_path(start_state, goal_state);
512 int tms_count = tap_get_tms_path_len(start_state, goal_state);
514 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
516 clock_tms(0x4b, tms_bits, tms_count, 0);
519 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
521 #if BUILD_FT2232_FTD2XX == 1
522 FT_STATUS status;
523 DWORD dw_bytes_written = 0;
524 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
526 *bytes_written = dw_bytes_written;
527 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
528 return ERROR_JTAG_DEVICE_ERROR;
530 else
532 *bytes_written = dw_bytes_written;
534 #elif BUILD_FT2232_LIBFTDI == 1
535 int retval;
536 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
538 *bytes_written = 0;
539 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
540 return ERROR_JTAG_DEVICE_ERROR;
542 else
544 *bytes_written = retval;
546 #endif
548 if (*bytes_written != (uint32_t)size)
550 return ERROR_JTAG_DEVICE_ERROR;
553 return ERROR_OK;
556 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
558 #if BUILD_FT2232_FTD2XX == 1
559 DWORD dw_bytes_read;
560 FT_STATUS status;
561 int timeout = 5;
562 *bytes_read = 0;
564 while ((*bytes_read < size) && timeout--)
566 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
567 *bytes_read, &dw_bytes_read)) != FT_OK)
569 *bytes_read = 0;
570 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
571 return ERROR_JTAG_DEVICE_ERROR;
573 *bytes_read += dw_bytes_read;
576 #elif BUILD_FT2232_LIBFTDI == 1
577 int retval;
578 int timeout = LIBFTDI_READ_RETRY_COUNT;
579 *bytes_read = 0;
581 while ((*bytes_read < size) && timeout--)
583 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
585 *bytes_read = 0;
586 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
587 return ERROR_JTAG_DEVICE_ERROR;
589 *bytes_read += retval;
592 #endif
594 if (*bytes_read < size)
596 LOG_ERROR("couldn't read enough bytes from "
597 "FT2232 device (%i < %i)",
598 (unsigned)*bytes_read,
599 (unsigned)size);
600 return ERROR_JTAG_DEVICE_ERROR;
603 return ERROR_OK;
606 static bool ft2232_device_is_highspeed(void)
608 #if BUILD_FT2232_FTD2XX == 1
609 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
610 #elif BUILD_FT2232_LIBFTDI == 1
611 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
612 #endif
616 * Commands that only apply to the FT2232H and FT4232H devices.
617 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
618 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
621 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
623 uint8_t buf = enable ? 0x96 : 0x97;
624 LOG_DEBUG("%2.2x", buf);
626 uint32_t bytes_written;
627 int retval;
629 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
631 LOG_ERROR("couldn't write command to %s adaptive clocking"
632 , enable ? "enable" : "disable");
633 return retval;
636 return ERROR_OK;
640 * Enable/disable the clk divide by 5 of the 60MHz master clock.
641 * This result in a JTAG clock speed range of 91.553Hz-6MHz
642 * respective 457.763Hz-30MHz.
644 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
646 uint32_t bytes_written;
647 uint8_t buf = enable ? 0x8b : 0x8a;
649 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
651 LOG_ERROR("couldn't write command to %s clk divide by 5"
652 , enable ? "enable" : "disable");
653 return ERROR_JTAG_INIT_FAILED;
655 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
656 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
658 return ERROR_OK;
661 static int ft2232_speed(int speed)
663 uint8_t buf[3];
664 int retval;
665 uint32_t bytes_written;
667 retval = ERROR_OK;
668 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
669 if (ft2232_device_is_highspeed())
670 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
671 else if (enable_adaptive_clocking)
673 LOG_ERROR("ft2232 device %lu does not support RTCK"
674 , (long unsigned int)ftdi_device);
675 return ERROR_FAIL;
678 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
679 return retval;
681 buf[0] = 0x86; /* command "set divisor" */
682 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
683 buf[2] = (speed >> 8) & 0xff; /* valueH */
685 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
686 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
688 LOG_ERROR("couldn't set FT2232 TCK speed");
689 return retval;
692 return ERROR_OK;
695 static int ft2232_speed_div(int speed, int* khz)
697 /* Take a look in the FT2232 manual,
698 * AN2232C-01 Command Processor for
699 * MPSSE and MCU Host Bus. Chapter 3.8 */
701 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
703 return ERROR_OK;
706 static int ft2232_khz(int khz, int* jtag_speed)
708 if (khz == 0)
710 if (ft2232_device_is_highspeed())
712 *jtag_speed = RTCK_SPEED;
713 return ERROR_OK;
715 else
717 LOG_DEBUG("RCLK not supported");
718 return ERROR_FAIL;
722 /* Take a look in the FT2232 manual,
723 * AN2232C-01 Command Processor for
724 * MPSSE and MCU Host Bus. Chapter 3.8
726 * We will calc here with a multiplier
727 * of 10 for better rounding later. */
729 /* Calc speed, (ft2232_max_tck / khz) - 1 */
730 /* Use 65000 for better rounding */
731 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
733 /* Add 0.9 for rounding */
734 *jtag_speed += 9;
736 /* Calc real speed */
737 *jtag_speed = *jtag_speed / 10;
739 /* Check if speed is greater than 0 */
740 if (*jtag_speed < 0)
742 *jtag_speed = 0;
745 /* Check max value */
746 if (*jtag_speed > 0xFFFF)
748 *jtag_speed = 0xFFFF;
751 return ERROR_OK;
754 static void ft2232_end_state(tap_state_t state)
756 if (tap_is_state_stable(state))
757 tap_set_end_state(state);
758 else
760 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
761 exit(-1);
765 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
767 int num_bytes = (scan_size + 7) / 8;
768 int bits_left = scan_size;
769 int cur_byte = 0;
771 while (num_bytes-- > 1)
773 buffer[cur_byte++] = buffer_read();
774 bits_left -= 8;
777 buffer[cur_byte] = 0x0;
779 /* There is one more partial byte left from the clock data in/out instructions */
780 if (bits_left > 1)
782 buffer[cur_byte] = buffer_read() >> 1;
784 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
785 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
788 static void ft2232_debug_dump_buffer(void)
790 int i;
791 char line[256];
792 char* line_p = line;
794 for (i = 0; i < ft2232_buffer_size; i++)
796 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
797 if (i % 16 == 15)
799 LOG_DEBUG("%s", line);
800 line_p = line;
804 if (line_p != line)
805 LOG_DEBUG("%s", line);
808 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
810 struct jtag_command* cmd;
811 uint8_t* buffer;
812 int scan_size;
813 enum scan_type type;
814 int retval;
815 uint32_t bytes_written = 0;
816 uint32_t bytes_read = 0;
818 #ifdef _DEBUG_USB_IO_
819 struct timeval start, inter, inter2, end;
820 struct timeval d_inter, d_inter2, d_end;
821 #endif
823 #ifdef _DEBUG_USB_COMMS_
824 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
825 ft2232_debug_dump_buffer();
826 #endif
828 #ifdef _DEBUG_USB_IO_
829 gettimeofday(&start, NULL);
830 #endif
832 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
834 LOG_ERROR("couldn't write MPSSE commands to FT2232");
835 return retval;
838 #ifdef _DEBUG_USB_IO_
839 gettimeofday(&inter, NULL);
840 #endif
842 if (ft2232_expect_read)
844 /* FIXME this "timeout" is never changed ... */
845 int timeout = LIBFTDI_READ_RETRY_COUNT;
846 ft2232_buffer_size = 0;
848 #ifdef _DEBUG_USB_IO_
849 gettimeofday(&inter2, NULL);
850 #endif
852 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
854 LOG_ERROR("couldn't read from FT2232");
855 return retval;
858 #ifdef _DEBUG_USB_IO_
859 gettimeofday(&end, NULL);
861 timeval_subtract(&d_inter, &inter, &start);
862 timeval_subtract(&d_inter2, &inter2, &start);
863 timeval_subtract(&d_end, &end, &start);
865 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
866 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
867 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
868 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
869 #endif
871 ft2232_buffer_size = bytes_read;
873 if (ft2232_expect_read != ft2232_buffer_size)
875 LOG_ERROR("ft2232_expect_read (%i) != "
876 "ft2232_buffer_size (%i) "
877 "(%i retries)",
878 ft2232_expect_read,
879 ft2232_buffer_size,
880 LIBFTDI_READ_RETRY_COUNT - timeout);
881 ft2232_debug_dump_buffer();
883 exit(-1);
886 #ifdef _DEBUG_USB_COMMS_
887 LOG_DEBUG("read buffer (%i retries): %i bytes",
888 LIBFTDI_READ_RETRY_COUNT - timeout,
889 ft2232_buffer_size);
890 ft2232_debug_dump_buffer();
891 #endif
894 ft2232_expect_read = 0;
895 ft2232_read_pointer = 0;
897 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
898 * that wasn't handled by a caller-provided error handler
900 retval = ERROR_OK;
902 cmd = first;
903 while (cmd != last)
905 switch (cmd->type)
907 case JTAG_SCAN:
908 type = jtag_scan_type(cmd->cmd.scan);
909 if (type != SCAN_OUT)
911 scan_size = jtag_scan_size(cmd->cmd.scan);
912 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
913 ft2232_read_scan(type, buffer, scan_size);
914 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
915 retval = ERROR_JTAG_QUEUE_FAILED;
916 free(buffer);
918 break;
920 default:
921 break;
924 cmd = cmd->next;
927 ft2232_buffer_size = 0;
929 return retval;
933 * Function ft2232_add_pathmove
934 * moves the TAP controller from the current state to a new state through the
935 * given path, where path is an array of tap_state_t's.
937 * @param path is an array of tap_stat_t which gives the states to traverse through
938 * ending with the last state at path[num_states-1]
939 * @param num_states is the count of state steps to move through
941 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
943 int state_count = 0;
945 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
947 DEBUG_JTAG_IO("-");
949 /* this loop verifies that the path is legal and logs each state in the path */
950 while (num_states)
952 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
953 int bit_count = 0;
954 int num_states_batch = num_states > 7 ? 7 : num_states;
956 /* command "Clock Data to TMS/CS Pin (no Read)" */
957 buffer_write(0x4b);
959 /* number of states remaining */
960 buffer_write(num_states_batch - 1);
962 while (num_states_batch--) {
963 /* either TMS=0 or TMS=1 must work ... */
964 if (tap_state_transition(tap_get_state(), false)
965 == path[state_count])
966 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
967 else if (tap_state_transition(tap_get_state(), true)
968 == path[state_count])
969 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
971 /* ... or else the caller goofed BADLY */
972 else {
973 LOG_ERROR("BUG: %s -> %s isn't a valid "
974 "TAP state transition",
975 tap_state_name(tap_get_state()),
976 tap_state_name(path[state_count]));
977 exit(-1);
980 tap_set_state(path[state_count]);
981 state_count++;
982 num_states--;
985 buffer_write(tms_byte);
987 tap_set_end_state(tap_get_state());
990 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
992 int num_bytes = (scan_size + 7) / 8;
993 int bits_left = scan_size;
994 int cur_byte = 0;
995 int last_bit;
997 if (!ir_scan)
999 if (tap_get_state() != TAP_DRSHIFT)
1001 move_to_state(TAP_DRSHIFT);
1004 else
1006 if (tap_get_state() != TAP_IRSHIFT)
1008 move_to_state(TAP_IRSHIFT);
1012 /* add command for complete bytes */
1013 while (num_bytes > 1)
1015 int thisrun_bytes;
1016 if (type == SCAN_IO)
1018 /* Clock Data Bytes In and Out LSB First */
1019 buffer_write(0x39);
1020 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1022 else if (type == SCAN_OUT)
1024 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1025 buffer_write(0x19);
1026 /* LOG_DEBUG("added TDI bytes (o)"); */
1028 else if (type == SCAN_IN)
1030 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1031 buffer_write(0x28);
1032 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1035 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1036 num_bytes -= thisrun_bytes;
1038 buffer_write((uint8_t) (thisrun_bytes - 1));
1039 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1041 if (type != SCAN_IN)
1043 /* add complete bytes */
1044 while (thisrun_bytes-- > 0)
1046 buffer_write(buffer[cur_byte++]);
1047 bits_left -= 8;
1050 else /* (type == SCAN_IN) */
1052 bits_left -= 8 * (thisrun_bytes);
1056 /* the most signifcant bit is scanned during TAP movement */
1057 if (type != SCAN_IN)
1058 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1059 else
1060 last_bit = 0;
1062 /* process remaining bits but the last one */
1063 if (bits_left > 1)
1065 if (type == SCAN_IO)
1067 /* Clock Data Bits In and Out LSB First */
1068 buffer_write(0x3b);
1069 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1071 else if (type == SCAN_OUT)
1073 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1074 buffer_write(0x1b);
1075 /* LOG_DEBUG("added TDI bits (o)"); */
1077 else if (type == SCAN_IN)
1079 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1080 buffer_write(0x2a);
1081 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1084 buffer_write(bits_left - 2);
1085 if (type != SCAN_IN)
1086 buffer_write(buffer[cur_byte]);
1089 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1090 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1092 if (type == SCAN_IO)
1094 /* Clock Data Bits In and Out LSB First */
1095 buffer_write(0x3b);
1096 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1098 else if (type == SCAN_OUT)
1100 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1101 buffer_write(0x1b);
1102 /* LOG_DEBUG("added TDI bits (o)"); */
1104 else if (type == SCAN_IN)
1106 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1107 buffer_write(0x2a);
1108 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1110 buffer_write(0x0);
1111 buffer_write(last_bit);
1113 else
1115 int tms_bits;
1116 int tms_count;
1117 uint8_t mpsse_cmd;
1119 /* move from Shift-IR/DR to end state */
1120 if (type != SCAN_OUT)
1122 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1123 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1124 tms_bits = 0x01;
1125 tms_count = 2;
1126 /* Clock Data to TMS/CS Pin with Read */
1127 mpsse_cmd = 0x6b;
1129 else
1131 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1132 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1133 /* Clock Data to TMS/CS Pin (no Read) */
1134 mpsse_cmd = 0x4b;
1137 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1138 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1141 if (tap_get_state() != tap_get_end_state())
1143 move_to_state(tap_get_end_state());
1147 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1149 int num_bytes = (scan_size + 7) / 8;
1150 int bits_left = scan_size;
1151 int cur_byte = 0;
1152 int last_bit;
1153 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1154 uint8_t* receive_pointer = receive_buffer;
1155 uint32_t bytes_written;
1156 uint32_t bytes_read;
1157 int retval;
1158 int thisrun_read = 0;
1160 if (cmd->ir_scan)
1162 LOG_ERROR("BUG: large IR scans are not supported");
1163 exit(-1);
1166 if (tap_get_state() != TAP_DRSHIFT)
1168 move_to_state(TAP_DRSHIFT);
1171 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1173 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1174 exit(-1);
1176 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1177 ft2232_buffer_size, (int)bytes_written);
1178 ft2232_buffer_size = 0;
1180 /* add command for complete bytes */
1181 while (num_bytes > 1)
1183 int thisrun_bytes;
1185 if (type == SCAN_IO)
1187 /* Clock Data Bytes In and Out LSB First */
1188 buffer_write(0x39);
1189 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1191 else if (type == SCAN_OUT)
1193 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1194 buffer_write(0x19);
1195 /* LOG_DEBUG("added TDI bytes (o)"); */
1197 else if (type == SCAN_IN)
1199 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1200 buffer_write(0x28);
1201 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1204 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1205 thisrun_read = thisrun_bytes;
1206 num_bytes -= thisrun_bytes;
1207 buffer_write((uint8_t) (thisrun_bytes - 1));
1208 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1210 if (type != SCAN_IN)
1212 /* add complete bytes */
1213 while (thisrun_bytes-- > 0)
1215 buffer_write(buffer[cur_byte]);
1216 cur_byte++;
1217 bits_left -= 8;
1220 else /* (type == SCAN_IN) */
1222 bits_left -= 8 * (thisrun_bytes);
1225 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1227 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1228 exit(-1);
1230 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1231 ft2232_buffer_size,
1232 (int)bytes_written);
1233 ft2232_buffer_size = 0;
1235 if (type != SCAN_OUT)
1237 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1239 LOG_ERROR("couldn't read from FT2232");
1240 exit(-1);
1242 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1243 thisrun_read,
1244 (int)bytes_read);
1245 receive_pointer += bytes_read;
1249 thisrun_read = 0;
1251 /* the most signifcant bit is scanned during TAP movement */
1252 if (type != SCAN_IN)
1253 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1254 else
1255 last_bit = 0;
1257 /* process remaining bits but the last one */
1258 if (bits_left > 1)
1260 if (type == SCAN_IO)
1262 /* Clock Data Bits In and Out LSB First */
1263 buffer_write(0x3b);
1264 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1266 else if (type == SCAN_OUT)
1268 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1269 buffer_write(0x1b);
1270 /* LOG_DEBUG("added TDI bits (o)"); */
1272 else if (type == SCAN_IN)
1274 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1275 buffer_write(0x2a);
1276 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1278 buffer_write(bits_left - 2);
1279 if (type != SCAN_IN)
1280 buffer_write(buffer[cur_byte]);
1282 if (type != SCAN_OUT)
1283 thisrun_read += 2;
1286 if (tap_get_end_state() == TAP_DRSHIFT)
1288 if (type == SCAN_IO)
1290 /* Clock Data Bits In and Out LSB First */
1291 buffer_write(0x3b);
1292 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1294 else if (type == SCAN_OUT)
1296 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1297 buffer_write(0x1b);
1298 /* LOG_DEBUG("added TDI bits (o)"); */
1300 else if (type == SCAN_IN)
1302 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1303 buffer_write(0x2a);
1304 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1306 buffer_write(0x0);
1307 buffer_write(last_bit);
1309 else
1311 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1312 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1313 uint8_t mpsse_cmd;
1315 /* move from Shift-IR/DR to end state */
1316 if (type != SCAN_OUT)
1318 /* Clock Data to TMS/CS Pin with Read */
1319 mpsse_cmd = 0x6b;
1320 /* LOG_DEBUG("added TMS scan (read)"); */
1322 else
1324 /* Clock Data to TMS/CS Pin (no Read) */
1325 mpsse_cmd = 0x4b;
1326 /* LOG_DEBUG("added TMS scan (no read)"); */
1329 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1330 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1333 if (type != SCAN_OUT)
1334 thisrun_read += 1;
1336 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1338 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1339 exit(-1);
1341 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1342 ft2232_buffer_size,
1343 (int)bytes_written);
1344 ft2232_buffer_size = 0;
1346 if (type != SCAN_OUT)
1348 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1350 LOG_ERROR("couldn't read from FT2232");
1351 exit(-1);
1353 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1354 thisrun_read,
1355 (int)bytes_read);
1358 return ERROR_OK;
1361 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1363 int predicted_size = 3;
1364 int num_bytes = (scan_size - 1) / 8;
1366 if (tap_get_state() != TAP_DRSHIFT)
1367 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1369 if (type == SCAN_IN) /* only from device to host */
1371 /* complete bytes */
1372 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1374 /* remaining bits - 1 (up to 7) */
1375 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1377 else /* host to device, or bidirectional */
1379 /* complete bytes */
1380 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1382 /* remaining bits -1 (up to 7) */
1383 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1386 return predicted_size;
1389 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1391 int predicted_size = 0;
1393 if (type != SCAN_OUT)
1395 /* complete bytes */
1396 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1398 /* remaining bits - 1 */
1399 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1401 /* last bit (from TMS scan) */
1402 predicted_size += 1;
1405 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1407 return predicted_size;
1410 /* semi-generic FT2232/FT4232 reset code */
1411 static void ftx23_reset(int trst, int srst)
1413 enum reset_types jtag_reset_config = jtag_get_reset_config();
1414 if (trst == 1)
1416 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1417 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1418 else
1419 low_output &= ~nTRST; /* switch output low */
1421 else if (trst == 0)
1423 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1424 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1425 else
1426 low_output |= nTRST; /* switch output high */
1429 if (srst == 1)
1431 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1432 low_output &= ~nSRST; /* switch output low */
1433 else
1434 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1436 else if (srst == 0)
1438 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1439 low_output |= nSRST; /* switch output high */
1440 else
1441 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1444 /* command "set data bits low byte" */
1445 buffer_write(0x80);
1446 buffer_write(low_output);
1447 buffer_write(low_direction);
1450 static void jtagkey_reset(int trst, int srst)
1452 enum reset_types jtag_reset_config = jtag_get_reset_config();
1453 if (trst == 1)
1455 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1456 high_output &= ~nTRSTnOE;
1457 else
1458 high_output &= ~nTRST;
1460 else if (trst == 0)
1462 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1463 high_output |= nTRSTnOE;
1464 else
1465 high_output |= nTRST;
1468 if (srst == 1)
1470 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1471 high_output &= ~nSRST;
1472 else
1473 high_output &= ~nSRSTnOE;
1475 else if (srst == 0)
1477 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1478 high_output |= nSRST;
1479 else
1480 high_output |= nSRSTnOE;
1483 /* command "set data bits high byte" */
1484 buffer_write(0x82);
1485 buffer_write(high_output);
1486 buffer_write(high_direction);
1487 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1488 high_direction);
1491 static void olimex_jtag_reset(int trst, int srst)
1493 enum reset_types jtag_reset_config = jtag_get_reset_config();
1494 if (trst == 1)
1496 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1497 high_output &= ~nTRSTnOE;
1498 else
1499 high_output &= ~nTRST;
1501 else if (trst == 0)
1503 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1504 high_output |= nTRSTnOE;
1505 else
1506 high_output |= nTRST;
1509 if (srst == 1)
1511 high_output |= nSRST;
1513 else if (srst == 0)
1515 high_output &= ~nSRST;
1518 /* command "set data bits high byte" */
1519 buffer_write(0x82);
1520 buffer_write(high_output);
1521 buffer_write(high_direction);
1522 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1523 high_direction);
1526 static void axm0432_jtag_reset(int trst, int srst)
1528 if (trst == 1)
1530 tap_set_state(TAP_RESET);
1531 high_output &= ~nTRST;
1533 else if (trst == 0)
1535 high_output |= nTRST;
1538 if (srst == 1)
1540 high_output &= ~nSRST;
1542 else if (srst == 0)
1544 high_output |= nSRST;
1547 /* command "set data bits low byte" */
1548 buffer_write(0x82);
1549 buffer_write(high_output);
1550 buffer_write(high_direction);
1551 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1552 high_direction);
1555 static void flyswatter_reset(int trst, int srst)
1557 if (trst == 1)
1559 low_output &= ~nTRST;
1561 else if (trst == 0)
1563 low_output |= nTRST;
1566 if (srst == 1)
1568 low_output |= nSRST;
1570 else if (srst == 0)
1572 low_output &= ~nSRST;
1575 /* command "set data bits low byte" */
1576 buffer_write(0x80);
1577 buffer_write(low_output);
1578 buffer_write(low_direction);
1579 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1582 static void flyswatter1_reset(int trst, int srst)
1584 flyswatter_reset(trst, srst);
1587 static void flyswatter2_reset(int trst, int srst)
1589 flyswatter_reset(trst, !srst);
1592 static void minimodule_reset(int trst, int srst)
1594 if (srst == 1)
1596 low_output &= ~nSRST;
1598 else if (srst == 0)
1600 low_output |= nSRST;
1603 /* command "set data bits low byte" */
1604 buffer_write(0x80);
1605 buffer_write(low_output);
1606 buffer_write(low_direction);
1607 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1610 static void turtle_reset(int trst, int srst)
1612 trst = trst;
1614 if (srst == 1)
1616 low_output |= nSRST;
1618 else if (srst == 0)
1620 low_output &= ~nSRST;
1623 /* command "set data bits low byte" */
1624 buffer_write(0x80);
1625 buffer_write(low_output);
1626 buffer_write(low_direction);
1627 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1630 static void comstick_reset(int trst, int srst)
1632 if (trst == 1)
1634 high_output &= ~nTRST;
1636 else if (trst == 0)
1638 high_output |= nTRST;
1641 if (srst == 1)
1643 high_output &= ~nSRST;
1645 else if (srst == 0)
1647 high_output |= nSRST;
1650 /* command "set data bits high byte" */
1651 buffer_write(0x82);
1652 buffer_write(high_output);
1653 buffer_write(high_direction);
1654 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1655 high_direction);
1658 static void stm32stick_reset(int trst, int srst)
1660 if (trst == 1)
1662 high_output &= ~nTRST;
1664 else if (trst == 0)
1666 high_output |= nTRST;
1669 if (srst == 1)
1671 low_output &= ~nSRST;
1673 else if (srst == 0)
1675 low_output |= nSRST;
1678 /* command "set data bits low byte" */
1679 buffer_write(0x80);
1680 buffer_write(low_output);
1681 buffer_write(low_direction);
1683 /* command "set data bits high byte" */
1684 buffer_write(0x82);
1685 buffer_write(high_output);
1686 buffer_write(high_direction);
1687 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1688 high_direction);
1691 static void sheevaplug_reset(int trst, int srst)
1693 if (trst == 1)
1694 high_output &= ~nTRST;
1695 else if (trst == 0)
1696 high_output |= nTRST;
1698 if (srst == 1)
1699 high_output &= ~nSRSTnOE;
1700 else if (srst == 0)
1701 high_output |= nSRSTnOE;
1703 /* command "set data bits high byte" */
1704 buffer_write(0x82);
1705 buffer_write(high_output);
1706 buffer_write(high_direction);
1707 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1710 static void redbee_reset(int trst, int srst)
1712 if (trst == 1)
1714 tap_set_state(TAP_RESET);
1715 high_output &= ~nTRST;
1717 else if (trst == 0)
1719 high_output |= nTRST;
1722 if (srst == 1)
1724 high_output &= ~nSRST;
1726 else if (srst == 0)
1728 high_output |= nSRST;
1731 /* command "set data bits low byte" */
1732 buffer_write(0x82);
1733 buffer_write(high_output);
1734 buffer_write(high_direction);
1735 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1736 "high_direction: 0x%2.2x", trst, srst, high_output,
1737 high_direction);
1740 static void xds100v2_reset(int trst, int srst)
1742 if (trst == 1)
1744 tap_set_state(TAP_RESET);
1745 high_output &= ~nTRST;
1747 else if (trst == 0)
1749 high_output |= nTRST;
1752 if (srst == 1)
1754 high_output |= nSRST;
1756 else if (srst == 0)
1758 high_output &= ~nSRST;
1761 /* command "set data bits low byte" */
1762 buffer_write(0x82);
1763 buffer_write(high_output);
1764 buffer_write(high_direction);
1765 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1766 "high_direction: 0x%2.2x", trst, srst, high_output,
1767 high_direction);
1770 static int ft2232_execute_runtest(struct jtag_command *cmd)
1772 int retval;
1773 int i;
1774 int predicted_size = 0;
1775 retval = ERROR_OK;
1777 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1778 cmd->cmd.runtest->num_cycles,
1779 tap_state_name(cmd->cmd.runtest->end_state));
1781 /* only send the maximum buffer size that FT2232C can handle */
1782 predicted_size = 0;
1783 if (tap_get_state() != TAP_IDLE)
1784 predicted_size += 3;
1785 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1786 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1787 predicted_size += 3;
1788 if (tap_get_end_state() != TAP_IDLE)
1789 predicted_size += 3;
1790 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1792 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1793 retval = ERROR_JTAG_QUEUE_FAILED;
1794 require_send = 0;
1795 first_unsent = cmd;
1797 if (tap_get_state() != TAP_IDLE)
1799 move_to_state(TAP_IDLE);
1800 require_send = 1;
1802 i = cmd->cmd.runtest->num_cycles;
1803 while (i > 0)
1805 /* there are no state transitions in this code, so omit state tracking */
1807 /* command "Clock Data to TMS/CS Pin (no Read)" */
1808 buffer_write(0x4b);
1810 /* scan 7 bits */
1811 buffer_write((i > 7) ? 6 : (i - 1));
1813 /* TMS data bits */
1814 buffer_write(0x0);
1816 i -= (i > 7) ? 7 : i;
1817 /* LOG_DEBUG("added TMS scan (no read)"); */
1820 ft2232_end_state(cmd->cmd.runtest->end_state);
1822 if (tap_get_state() != tap_get_end_state())
1824 move_to_state(tap_get_end_state());
1827 require_send = 1;
1828 DEBUG_JTAG_IO("runtest: %i, end in %s",
1829 cmd->cmd.runtest->num_cycles,
1830 tap_state_name(tap_get_end_state()));
1831 return retval;
1834 static int ft2232_execute_statemove(struct jtag_command *cmd)
1836 int predicted_size = 0;
1837 int retval = ERROR_OK;
1839 DEBUG_JTAG_IO("statemove end in %s",
1840 tap_state_name(cmd->cmd.statemove->end_state));
1842 /* only send the maximum buffer size that FT2232C can handle */
1843 predicted_size = 3;
1844 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1846 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1847 retval = ERROR_JTAG_QUEUE_FAILED;
1848 require_send = 0;
1849 first_unsent = cmd;
1851 ft2232_end_state(cmd->cmd.statemove->end_state);
1853 /* For TAP_RESET, ignore the current recorded state. It's often
1854 * wrong at server startup, and this transation is critical whenever
1855 * it's requested.
1857 if (tap_get_end_state() == TAP_RESET) {
1858 clock_tms(0x4b, 0xff, 5, 0);
1859 require_send = 1;
1861 /* shortest-path move to desired end state */
1862 } else if (tap_get_state() != tap_get_end_state())
1864 move_to_state(tap_get_end_state());
1865 require_send = 1;
1868 return retval;
1872 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1873 * (or SWD) state machine.
1875 static int ft2232_execute_tms(struct jtag_command *cmd)
1877 int retval = ERROR_OK;
1878 unsigned num_bits = cmd->cmd.tms->num_bits;
1879 const uint8_t *bits = cmd->cmd.tms->bits;
1880 unsigned count;
1882 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1884 /* only send the maximum buffer size that FT2232C can handle */
1885 count = 3 * DIV_ROUND_UP(num_bits, 4);
1886 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1887 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1888 retval = ERROR_JTAG_QUEUE_FAILED;
1890 require_send = 0;
1891 first_unsent = cmd;
1894 /* Shift out in batches of at most 6 bits; there's a report of an
1895 * FT2232 bug in this area, where shifting exactly 7 bits can make
1896 * problems with TMS signaling for the last clock cycle:
1898 * http://developer.intra2net.com/mailarchive/html/
1899 * libftdi/2009/msg00292.html
1901 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1903 * Note that pathmoves in JTAG are not often seven bits, so that
1904 * isn't a particularly likely situation outside of "special"
1905 * signaling such as switching between JTAG and SWD modes.
1907 while (num_bits) {
1908 if (num_bits <= 6) {
1909 buffer_write(0x4b);
1910 buffer_write(num_bits - 1);
1911 buffer_write(*bits & 0x3f);
1912 break;
1915 /* Yes, this is lazy ... we COULD shift out more data
1916 * bits per operation, but doing it in nybbles is easy
1918 buffer_write(0x4b);
1919 buffer_write(3);
1920 buffer_write(*bits & 0xf);
1921 num_bits -= 4;
1923 count = (num_bits > 4) ? 4 : num_bits;
1925 buffer_write(0x4b);
1926 buffer_write(count - 1);
1927 buffer_write((*bits >> 4) & 0xf);
1928 num_bits -= count;
1930 bits++;
1933 require_send = 1;
1934 return retval;
1937 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1939 int predicted_size = 0;
1940 int retval = ERROR_OK;
1942 tap_state_t* path = cmd->cmd.pathmove->path;
1943 int num_states = cmd->cmd.pathmove->num_states;
1945 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1946 tap_state_name(tap_get_state()),
1947 tap_state_name(path[num_states-1]));
1949 /* only send the maximum buffer size that FT2232C can handle */
1950 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1951 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1953 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1954 retval = ERROR_JTAG_QUEUE_FAILED;
1956 require_send = 0;
1957 first_unsent = cmd;
1960 ft2232_add_pathmove(path, num_states);
1961 require_send = 1;
1963 return retval;
1966 static int ft2232_execute_scan(struct jtag_command *cmd)
1968 uint8_t* buffer;
1969 int scan_size; /* size of IR or DR scan */
1970 int predicted_size = 0;
1971 int retval = ERROR_OK;
1973 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1975 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1977 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1979 predicted_size = ft2232_predict_scan_out(scan_size, type);
1980 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1982 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1983 /* unsent commands before this */
1984 if (first_unsent != cmd)
1985 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1986 retval = ERROR_JTAG_QUEUE_FAILED;
1988 /* current command */
1989 ft2232_end_state(cmd->cmd.scan->end_state);
1990 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1991 require_send = 0;
1992 first_unsent = cmd->next;
1993 if (buffer)
1994 free(buffer);
1995 return retval;
1997 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1999 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
2000 first_unsent,
2001 cmd);
2002 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2003 retval = ERROR_JTAG_QUEUE_FAILED;
2004 require_send = 0;
2005 first_unsent = cmd;
2007 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
2008 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
2009 ft2232_end_state(cmd->cmd.scan->end_state);
2010 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
2011 require_send = 1;
2012 if (buffer)
2013 free(buffer);
2014 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
2015 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
2016 tap_state_name(tap_get_end_state()));
2017 return retval;
2021 static int ft2232_execute_reset(struct jtag_command *cmd)
2023 int retval;
2024 int predicted_size = 0;
2025 retval = ERROR_OK;
2027 DEBUG_JTAG_IO("reset trst: %i srst %i",
2028 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2030 /* only send the maximum buffer size that FT2232C can handle */
2031 predicted_size = 3;
2032 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
2034 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2035 retval = ERROR_JTAG_QUEUE_FAILED;
2036 require_send = 0;
2037 first_unsent = cmd;
2040 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
2042 tap_set_state(TAP_RESET);
2045 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2046 require_send = 1;
2048 DEBUG_JTAG_IO("trst: %i, srst: %i",
2049 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2050 return retval;
2053 static int ft2232_execute_sleep(struct jtag_command *cmd)
2055 int retval;
2056 retval = ERROR_OK;
2058 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2060 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2061 retval = ERROR_JTAG_QUEUE_FAILED;
2062 first_unsent = cmd->next;
2063 jtag_sleep(cmd->cmd.sleep->us);
2064 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2065 cmd->cmd.sleep->us,
2066 tap_state_name(tap_get_state()));
2067 return retval;
2070 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2072 int retval;
2073 retval = ERROR_OK;
2075 /* this is only allowed while in a stable state. A check for a stable
2076 * state was done in jtag_add_clocks()
2078 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2079 retval = ERROR_JTAG_QUEUE_FAILED;
2080 DEBUG_JTAG_IO("clocks %i while in %s",
2081 cmd->cmd.stableclocks->num_cycles,
2082 tap_state_name(tap_get_state()));
2083 return retval;
2086 static int ft2232_execute_command(struct jtag_command *cmd)
2088 int retval;
2090 switch (cmd->type)
2092 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2093 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2094 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2095 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2096 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2097 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2098 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2099 case JTAG_TMS:
2100 retval = ft2232_execute_tms(cmd);
2101 break;
2102 default:
2103 LOG_ERROR("BUG: unknown JTAG command type encountered");
2104 retval = ERROR_JTAG_QUEUE_FAILED;
2105 break;
2107 return retval;
2110 static int ft2232_execute_queue(void)
2112 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2113 int retval;
2115 first_unsent = cmd; /* next command that has to be sent */
2116 require_send = 0;
2118 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2119 * that wasn't handled by a caller-provided error handler
2121 retval = ERROR_OK;
2123 ft2232_buffer_size = 0;
2124 ft2232_expect_read = 0;
2126 /* blink, if the current layout has that feature */
2127 if (layout->blink)
2128 layout->blink();
2130 while (cmd)
2132 /* fill the write buffer with the desired command */
2133 if (ft2232_execute_command(cmd) != ERROR_OK)
2134 retval = ERROR_JTAG_QUEUE_FAILED;
2135 /* Start reading input before FT2232 TX buffer fills up.
2136 * Sometimes this happens because we don't know the
2137 * length of the last command before we execute it. So
2138 * we simple inform the user.
2140 cmd = cmd->next;
2142 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE )
2144 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1) )
2145 LOG_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read,(FT2232_BUFFER_READ_QUEUE_SIZE+1));
2146 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2147 retval = ERROR_JTAG_QUEUE_FAILED;
2148 first_unsent = cmd;
2152 if (require_send > 0)
2153 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2154 retval = ERROR_JTAG_QUEUE_FAILED;
2156 return retval;
2159 #if BUILD_FT2232_FTD2XX == 1
2160 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2162 FT_STATUS status;
2163 DWORD deviceID;
2164 char SerialNumber[16];
2165 char Description[64];
2166 DWORD openex_flags = 0;
2167 char* openex_string = NULL;
2168 uint8_t latency_timer;
2170 if (layout == NULL) {
2171 LOG_WARNING("No ft2232 layout specified'");
2172 return ERROR_JTAG_INIT_FAILED;
2175 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2177 #if IS_WIN32 == 0
2178 /* Add non-standard Vid/Pid to the linux driver */
2179 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2181 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2183 #endif
2185 if (ft2232_device_desc && ft2232_serial)
2187 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2188 ft2232_device_desc = NULL;
2191 if (ft2232_device_desc)
2193 openex_string = ft2232_device_desc;
2194 openex_flags = FT_OPEN_BY_DESCRIPTION;
2196 else if (ft2232_serial)
2198 openex_string = ft2232_serial;
2199 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2201 else
2203 LOG_ERROR("neither device description nor serial number specified");
2204 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2206 return ERROR_JTAG_INIT_FAILED;
2209 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2210 if (status != FT_OK) {
2211 /* under Win32, the FTD2XX driver appends an "A" to the end
2212 * of the description, if we tried by the desc, then
2213 * try by the alternate "A" description. */
2214 if (openex_string == ft2232_device_desc) {
2215 /* Try the alternate method. */
2216 openex_string = ft2232_device_desc_A;
2217 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2218 if (status == FT_OK) {
2219 /* yea, the "alternate" method worked! */
2220 } else {
2221 /* drat, give the user a meaningfull message.
2222 * telling the use we tried *BOTH* methods. */
2223 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2224 ft2232_device_desc,
2225 ft2232_device_desc_A);
2230 if (status != FT_OK)
2232 DWORD num_devices;
2234 if (more)
2236 LOG_WARNING("unable to open ftdi device (trying more): %s",
2237 ftd2xx_status_string(status));
2238 *try_more = 1;
2239 return ERROR_JTAG_INIT_FAILED;
2241 LOG_ERROR("unable to open ftdi device: %s",
2242 ftd2xx_status_string(status));
2243 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2244 if (status == FT_OK)
2246 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2247 uint32_t i;
2249 for (i = 0; i < num_devices; i++)
2250 desc_array[i] = malloc(64);
2252 desc_array[num_devices] = NULL;
2254 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2256 if (status == FT_OK)
2258 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2259 for (i = 0; i < num_devices; i++)
2260 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2263 for (i = 0; i < num_devices; i++)
2264 free(desc_array[i]);
2266 free(desc_array);
2268 else
2270 LOG_ERROR("ListDevices: NONE");
2272 return ERROR_JTAG_INIT_FAILED;
2275 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2277 LOG_ERROR("unable to set latency timer: %s",
2278 ftd2xx_status_string(status));
2279 return ERROR_JTAG_INIT_FAILED;
2282 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2284 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2285 * so ignore errors if using this driver version */
2286 DWORD dw_version;
2288 status = FT_GetDriverVersion(ftdih, &dw_version);
2289 LOG_ERROR("unable to get latency timer: %s",
2290 ftd2xx_status_string(status));
2292 if ((status == FT_OK) && (dw_version == 0x10004)) {
2293 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2294 "with FT_GetLatencyTimer, upgrade to a newer version");
2296 else {
2297 return ERROR_JTAG_INIT_FAILED;
2300 else
2302 LOG_DEBUG("current latency timer: %i", latency_timer);
2305 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2307 LOG_ERROR("unable to set timeouts: %s",
2308 ftd2xx_status_string(status));
2309 return ERROR_JTAG_INIT_FAILED;
2312 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2314 LOG_ERROR("unable to enable bit i/o mode: %s",
2315 ftd2xx_status_string(status));
2316 return ERROR_JTAG_INIT_FAILED;
2319 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2321 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2322 ftd2xx_status_string(status));
2323 return ERROR_JTAG_INIT_FAILED;
2325 else
2327 static const char* type_str[] =
2328 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2329 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2330 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2331 ? ftdi_device : FT_DEVICE_UNKNOWN;
2332 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2333 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2334 LOG_INFO("SerialNumber: %s", SerialNumber);
2335 LOG_INFO("Description: %s", Description);
2338 return ERROR_OK;
2341 static int ft2232_purge_ftd2xx(void)
2343 FT_STATUS status;
2345 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2347 LOG_ERROR("error purging ftd2xx device: %s",
2348 ftd2xx_status_string(status));
2349 return ERROR_JTAG_INIT_FAILED;
2352 return ERROR_OK;
2355 #endif /* BUILD_FT2232_FTD2XX == 1 */
2357 #if BUILD_FT2232_LIBFTDI == 1
2358 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2360 uint8_t latency_timer;
2362 if (layout == NULL) {
2363 LOG_WARNING("No ft2232 layout specified'");
2364 return ERROR_JTAG_INIT_FAILED;
2367 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2368 layout->name, vid, pid);
2370 if (ftdi_init(&ftdic) < 0)
2371 return ERROR_JTAG_INIT_FAILED;
2373 /* default to INTERFACE_A */
2374 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2376 if (ftdi_set_interface(&ftdic, channel) < 0)
2378 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2379 return ERROR_JTAG_INIT_FAILED;
2382 /* context, vendor id, product id */
2383 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2384 ft2232_serial) < 0)
2386 if (more)
2387 LOG_WARNING("unable to open ftdi device (trying more): %s",
2388 ftdic.error_str);
2389 else
2390 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2391 *try_more = 1;
2392 return ERROR_JTAG_INIT_FAILED;
2395 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2396 if (ftdi_usb_reset(&ftdic) < 0)
2398 LOG_ERROR("unable to reset ftdi device");
2399 return ERROR_JTAG_INIT_FAILED;
2402 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2404 LOG_ERROR("unable to set latency timer");
2405 return ERROR_JTAG_INIT_FAILED;
2408 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2410 LOG_ERROR("unable to get latency timer");
2411 return ERROR_JTAG_INIT_FAILED;
2413 else
2415 LOG_DEBUG("current latency timer: %i", latency_timer);
2418 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2420 ftdi_device = ftdic.type;
2421 static const char* type_str[] =
2422 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2423 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2424 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2425 ? ftdi_device : no_of_known_types;
2426 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2427 return ERROR_OK;
2430 static int ft2232_purge_libftdi(void)
2432 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2434 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2435 return ERROR_JTAG_INIT_FAILED;
2438 return ERROR_OK;
2441 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2443 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2445 uint8_t buf[3];
2446 uint32_t bytes_written;
2448 buf[0] = 0x80; /* command "set data bits low byte" */
2449 buf[1] = value; /* value */
2450 buf[2] = direction; /* direction */
2452 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2454 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2456 LOG_ERROR("couldn't initialize data bits low byte");
2457 return ERROR_JTAG_INIT_FAILED;
2460 return ERROR_OK;
2463 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2465 uint8_t buf[3];
2466 uint32_t bytes_written;
2468 buf[0] = 0x82; /* command "set data bits high byte" */
2469 buf[1] = value; /* value */
2470 buf[2] = direction; /* direction */
2472 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2474 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2476 LOG_ERROR("couldn't initialize data bits high byte");
2477 return ERROR_JTAG_INIT_FAILED;
2480 return ERROR_OK;
2483 static int ft2232_init(void)
2485 uint8_t buf[1];
2486 int retval;
2487 uint32_t bytes_written;
2489 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2491 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2493 else
2495 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2498 if (layout == NULL) {
2499 LOG_WARNING("No ft2232 layout specified'");
2500 return ERROR_JTAG_INIT_FAILED;
2503 for (int i = 0; 1; i++)
2506 * "more indicates that there are more IDs to try, so we should
2507 * not print an error for an ID mismatch (but for anything
2508 * else, we should).
2510 * try_more indicates that the error code returned indicates an
2511 * ID mismatch (and nothing else) and that we should proceeed
2512 * with the next ID pair.
2514 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2515 int try_more = 0;
2517 #if BUILD_FT2232_FTD2XX == 1
2518 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2519 more, &try_more);
2520 #elif BUILD_FT2232_LIBFTDI == 1
2521 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2522 more, &try_more, layout->channel);
2523 #endif
2524 if (retval >= 0)
2525 break;
2526 if (!more || !try_more)
2527 return retval;
2530 ft2232_buffer_size = 0;
2531 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2533 if (layout->init() != ERROR_OK)
2534 return ERROR_JTAG_INIT_FAILED;
2536 if (ft2232_device_is_highspeed())
2538 #ifndef BUILD_FT2232_HIGHSPEED
2539 #if BUILD_FT2232_FTD2XX == 1
2540 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2541 #elif BUILD_FT2232_LIBFTDI == 1
2542 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2543 #endif
2544 #endif
2545 /* make sure the legacy mode is disabled */
2546 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2547 return ERROR_JTAG_INIT_FAILED;
2550 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2551 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2553 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2554 return ERROR_JTAG_INIT_FAILED;
2557 #if BUILD_FT2232_FTD2XX == 1
2558 return ft2232_purge_ftd2xx();
2559 #elif BUILD_FT2232_LIBFTDI == 1
2560 return ft2232_purge_libftdi();
2561 #endif
2563 return ERROR_OK;
2566 /** Updates defaults for DBUS signals: the four JTAG signals
2567 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2569 static inline void ftx232_dbus_init(void)
2571 low_output = 0x08;
2572 low_direction = 0x0b;
2575 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2576 * the four GPIOL signals. Initialization covers value and direction,
2577 * as customized for each layout.
2579 static int ftx232_dbus_write(void)
2581 enum reset_types jtag_reset_config = jtag_get_reset_config();
2582 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2584 low_direction &= ~nTRSTnOE; /* nTRST input */
2585 low_output &= ~nTRST; /* nTRST = 0 */
2587 else
2589 low_direction |= nTRSTnOE; /* nTRST output */
2590 low_output |= nTRST; /* nTRST = 1 */
2593 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2595 low_direction |= nSRSTnOE; /* nSRST output */
2596 low_output |= nSRST; /* nSRST = 1 */
2598 else
2600 low_direction &= ~nSRSTnOE; /* nSRST input */
2601 low_output &= ~nSRST; /* nSRST = 0 */
2604 /* initialize low byte for jtag */
2605 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2607 LOG_ERROR("couldn't initialize FT2232 DBUS");
2608 return ERROR_JTAG_INIT_FAILED;
2611 return ERROR_OK;
2614 static int usbjtag_init(void)
2617 * NOTE: This is now _specific_ to the "usbjtag" layout.
2618 * Don't try cram any more layouts into this.
2620 ftx232_dbus_init();
2622 nTRST = 0x10;
2623 nTRSTnOE = 0x10;
2624 nSRST = 0x40;
2625 nSRSTnOE = 0x40;
2627 return ftx232_dbus_write();
2630 static int lm3s811_jtag_init(void)
2632 ftx232_dbus_init();
2634 /* There are multiple revisions of LM3S811 eval boards:
2635 * - Rev B (and older?) boards have no SWO trace support.
2636 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2637 * they should use the "luminary_icdi" layout instead.
2639 nTRST = 0x0;
2640 nTRSTnOE = 0x00;
2641 nSRST = 0x20;
2642 nSRSTnOE = 0x20;
2643 low_output = 0x88;
2644 low_direction = 0x8b;
2646 return ftx232_dbus_write();
2649 static int icdi_jtag_init(void)
2651 ftx232_dbus_init();
2653 /* Most Luminary eval boards support SWO trace output,
2654 * and should use this "luminary_icdi" layout.
2656 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2657 * to switch between JTAG and SWD, or switch the ft2232 UART
2658 * on the second MPSSE channel/interface (BDBUS)
2659 * between (i) the stellaris UART (on Luminary boards)
2660 * or (ii) SWO trace data (generic).
2662 * We come up in JTAG mode and may switch to SWD later (with
2663 * SWO/trace option if SWD is active).
2665 * DBUS == GPIO-Lx
2666 * CBUS == GPIO-Hx
2670 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2671 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2672 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2675 /* GPIOs on second channel/interface (UART) ... */
2676 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2677 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2678 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2680 nTRST = 0x0;
2681 nTRSTnOE = 0x00;
2682 nSRST = ICDI_SRST;
2683 nSRSTnOE = ICDI_SRST;
2685 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2686 low_output |= ICDI_JTAG_EN;
2687 low_output &= ~ICDI_DBG_ENn;
2689 return ftx232_dbus_write();
2692 static int signalyzer_init(void)
2694 ftx232_dbus_init();
2696 nTRST = 0x10;
2697 nTRSTnOE = 0x10;
2698 nSRST = 0x20;
2699 nSRSTnOE = 0x20;
2700 return ftx232_dbus_write();
2703 static int axm0432_jtag_init(void)
2705 low_output = 0x08;
2706 low_direction = 0x2b;
2708 /* initialize low byte for jtag */
2709 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2711 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2712 return ERROR_JTAG_INIT_FAILED;
2715 if (strcmp(layout->name, "axm0432_jtag") == 0)
2717 nTRST = 0x08;
2718 nTRSTnOE = 0x0; /* No output enable for TRST*/
2719 nSRST = 0x04;
2720 nSRSTnOE = 0x0; /* No output enable for SRST*/
2722 else
2724 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2725 exit(-1);
2728 high_output = 0x0;
2729 high_direction = 0x0c;
2731 enum reset_types jtag_reset_config = jtag_get_reset_config();
2732 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2734 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2736 else
2738 high_output |= nTRST;
2741 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2743 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2745 else
2747 high_output |= nSRST;
2750 /* initialize high byte for jtag */
2751 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2753 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2754 return ERROR_JTAG_INIT_FAILED;
2757 return ERROR_OK;
2760 static int redbee_init(void)
2762 low_output = 0x08;
2763 low_direction = 0x2b;
2765 /* initialize low byte for jtag */
2766 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2768 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2769 return ERROR_JTAG_INIT_FAILED;
2772 nTRST = 0x08;
2773 nTRSTnOE = 0x0; /* No output enable for TRST*/
2774 nSRST = 0x04;
2775 nSRSTnOE = 0x0; /* No output enable for SRST*/
2777 high_output = 0x0;
2778 high_direction = 0x0c;
2780 enum reset_types jtag_reset_config = jtag_get_reset_config();
2781 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2783 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2785 else
2787 high_output |= nTRST;
2790 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2792 LOG_ERROR("can't set nSRST to push-pull on redbee");
2794 else
2796 high_output |= nSRST;
2799 /* initialize high byte for jtag */
2800 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2802 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2803 return ERROR_JTAG_INIT_FAILED;
2806 return ERROR_OK;
2809 static int jtagkey_init(void)
2811 low_output = 0x08;
2812 low_direction = 0x1b;
2814 /* initialize low byte for jtag */
2815 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2817 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2818 return ERROR_JTAG_INIT_FAILED;
2821 if (strcmp(layout->name, "jtagkey") == 0)
2823 nTRST = 0x01;
2824 nTRSTnOE = 0x4;
2825 nSRST = 0x02;
2826 nSRSTnOE = 0x08;
2828 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2829 || (strcmp(layout->name, "oocdlink") == 0))
2831 nTRST = 0x02;
2832 nTRSTnOE = 0x1;
2833 nSRST = 0x08;
2834 nSRSTnOE = 0x04;
2836 else
2838 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2839 exit(-1);
2842 high_output = 0x0;
2843 high_direction = 0x0f;
2845 enum reset_types jtag_reset_config = jtag_get_reset_config();
2846 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2848 high_output |= nTRSTnOE;
2849 high_output &= ~nTRST;
2851 else
2853 high_output &= ~nTRSTnOE;
2854 high_output |= nTRST;
2857 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2859 high_output &= ~nSRSTnOE;
2860 high_output |= nSRST;
2862 else
2864 high_output |= nSRSTnOE;
2865 high_output &= ~nSRST;
2868 /* initialize high byte for jtag */
2869 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2871 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2872 return ERROR_JTAG_INIT_FAILED;
2875 return ERROR_OK;
2878 static int olimex_jtag_init(void)
2880 low_output = 0x08;
2881 low_direction = 0x1b;
2883 /* initialize low byte for jtag */
2884 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2886 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2887 return ERROR_JTAG_INIT_FAILED;
2890 nTRST = 0x01;
2891 nTRSTnOE = 0x4;
2892 nSRST = 0x02;
2893 nSRSTnOE = 0x00; /* no output enable for nSRST */
2895 high_output = 0x0;
2896 high_direction = 0x0f;
2898 enum reset_types jtag_reset_config = jtag_get_reset_config();
2899 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2901 high_output |= nTRSTnOE;
2902 high_output &= ~nTRST;
2904 else
2906 high_output &= ~nTRSTnOE;
2907 high_output |= nTRST;
2910 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2912 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2914 else
2916 high_output &= ~nSRST;
2919 /* turn red LED on */
2920 high_output |= 0x08;
2922 /* initialize high byte for jtag */
2923 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2925 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2926 return ERROR_JTAG_INIT_FAILED;
2929 return ERROR_OK;
2932 static int flyswatter_init(int rev)
2934 low_output = 0x18;
2935 low_direction = 0x7b;
2937 if ((rev < 0) || (rev > 3)) {
2938 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2939 return ERROR_JTAG_INIT_FAILED;
2942 if (rev == 1)
2943 low_direction |= 1 << 7;
2945 /* initialize low byte for jtag */
2946 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2948 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2949 return ERROR_JTAG_INIT_FAILED;
2952 nTRST = 0x10;
2953 nTRSTnOE = 0x0; /* not output enable for nTRST */
2954 nSRST = 0x20;
2955 nSRSTnOE = 0x00; /* no output enable for nSRST */
2957 high_output = 0x00;
2959 if (rev == 1)
2960 high_direction = 0x0c;
2961 else
2962 high_direction = 0x01;
2964 /* turn red LED3 on, LED2 off */
2965 high_output |= 0x08;
2967 /* initialize high byte for jtag */
2968 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2970 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2971 return ERROR_JTAG_INIT_FAILED;
2974 return ERROR_OK;
2977 static int flyswatter1_init(void)
2979 return flyswatter_init(1);
2982 static int flyswatter2_init(void)
2984 return flyswatter_init(2);
2987 static int minimodule_init(void)
2989 low_output = 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18)
2990 low_direction = 0xfb;//0xfb;
2992 /* initialize low byte for jtag */
2993 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2995 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2996 return ERROR_JTAG_INIT_FAILED;
3000 nSRST = 0x20;
3002 high_output = 0x00;
3003 high_direction = 0x05;
3005 /* turn red LED3 on, LED2 off */
3006 //high_output |= 0x08;
3008 /* initialize high byte for jtag */
3009 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3011 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
3012 return ERROR_JTAG_INIT_FAILED;
3015 return ERROR_OK;
3018 static int turtle_init(void)
3020 low_output = 0x08;
3021 low_direction = 0x5b;
3023 /* initialize low byte for jtag */
3024 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3026 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
3027 return ERROR_JTAG_INIT_FAILED;
3030 nSRST = 0x40;
3032 high_output = 0x00;
3033 high_direction = 0x0C;
3035 /* initialize high byte for jtag */
3036 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3038 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
3039 return ERROR_JTAG_INIT_FAILED;
3042 return ERROR_OK;
3045 static int comstick_init(void)
3047 low_output = 0x08;
3048 low_direction = 0x0b;
3050 /* initialize low byte for jtag */
3051 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3053 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3054 return ERROR_JTAG_INIT_FAILED;
3057 nTRST = 0x01;
3058 nTRSTnOE = 0x00; /* no output enable for nTRST */
3059 nSRST = 0x02;
3060 nSRSTnOE = 0x00; /* no output enable for nSRST */
3062 high_output = 0x03;
3063 high_direction = 0x03;
3065 /* initialize high byte for jtag */
3066 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3068 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3069 return ERROR_JTAG_INIT_FAILED;
3072 return ERROR_OK;
3075 static int stm32stick_init(void)
3077 low_output = 0x88;
3078 low_direction = 0x8b;
3080 /* initialize low byte for jtag */
3081 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3083 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3084 return ERROR_JTAG_INIT_FAILED;
3087 nTRST = 0x01;
3088 nTRSTnOE = 0x00; /* no output enable for nTRST */
3089 nSRST = 0x80;
3090 nSRSTnOE = 0x00; /* no output enable for nSRST */
3092 high_output = 0x01;
3093 high_direction = 0x03;
3095 /* initialize high byte for jtag */
3096 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3098 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3099 return ERROR_JTAG_INIT_FAILED;
3102 return ERROR_OK;
3105 static int sheevaplug_init(void)
3107 low_output = 0x08;
3108 low_direction = 0x1b;
3110 /* initialize low byte for jtag */
3111 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3113 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3114 return ERROR_JTAG_INIT_FAILED;
3117 nTRSTnOE = 0x1;
3118 nTRST = 0x02;
3119 nSRSTnOE = 0x4;
3120 nSRST = 0x08;
3122 high_output = 0x0;
3123 high_direction = 0x0f;
3125 /* nTRST is always push-pull */
3126 high_output &= ~nTRSTnOE;
3127 high_output |= nTRST;
3129 /* nSRST is always open-drain */
3130 high_output |= nSRSTnOE;
3131 high_output &= ~nSRST;
3133 /* initialize high byte for jtag */
3134 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3136 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3137 return ERROR_JTAG_INIT_FAILED;
3140 return ERROR_OK;
3143 static int cortino_jtag_init(void)
3145 low_output = 0x08;
3146 low_direction = 0x1b;
3148 /* initialize low byte for jtag */
3149 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3151 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3152 return ERROR_JTAG_INIT_FAILED;
3155 nTRST = 0x01;
3156 nTRSTnOE = 0x00; /* no output enable for nTRST */
3157 nSRST = 0x02;
3158 nSRSTnOE = 0x00; /* no output enable for nSRST */
3160 high_output = 0x03;
3161 high_direction = 0x03;
3163 /* initialize high byte for jtag */
3164 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3166 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3167 return ERROR_JTAG_INIT_FAILED;
3170 return ERROR_OK;
3173 static int lisa_l_init(void)
3175 ftx232_dbus_init();
3177 nTRST = 0x10;
3178 nTRSTnOE = 0x10;
3179 nSRST = 0x40;
3180 nSRSTnOE = 0x40;
3182 high_output = 0x00;
3183 high_direction = 0x18;
3185 /* initialize high byte for jtag */
3186 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3188 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3189 return ERROR_JTAG_INIT_FAILED;
3192 return ftx232_dbus_write();
3195 static int flossjtag_init(void)
3197 ftx232_dbus_init();
3199 nTRST = 0x10;
3200 nTRSTnOE = 0x10;
3201 nSRST = 0x40;
3202 nSRSTnOE = 0x40;
3204 high_output = 0x00;
3205 high_direction = 0x18;
3207 /* initialize high byte for jtag */
3208 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3210 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3211 return ERROR_JTAG_INIT_FAILED;
3214 return ftx232_dbus_write();
3218 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3219 * the door for a number of different configurations
3221 * Known Implementations:
3222 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3224 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3225 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3226 * * ACBUS3 to transition 0->1 (OE rising edge)
3227 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3228 * * ADBUS5/GPIOL1 = EMU_EN = 1
3229 * * ADBUS6/GPIOL2 = EMU0 = 0
3230 * * ACBUS4/SPARE0 = EMU1 = 0
3231 * * CPLD logic: Disable loopback
3232 * * ACBUS6/SPARE2 = LOOPBACK = 0
3234 #define XDS100_nEMU_EN (1<<5)
3235 #define XDS100_nEMU0 (1<<6)
3237 #define XDS100_PWR_RST (1<<3)
3238 #define XDS100_nEMU1 (1<<4)
3239 #define XDS100_LOOPBACK (1<<6)
3240 static int xds100v2_init(void)
3242 /* These are in the lower byte */
3243 nTRST = 0x10;
3244 nTRSTnOE = 0x10;
3246 /* These aren't actually used on 14 pin connectors */
3247 /* These are in the upper byte */
3248 nSRST = 0x01;
3249 nSRSTnOE = 0x01;
3251 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3252 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3254 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3256 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3257 return ERROR_JTAG_INIT_FAILED;
3260 high_output = 0;
3261 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3263 /* initialize high byte for jtag */
3264 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3266 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3267 return ERROR_JTAG_INIT_FAILED;
3270 high_output |= XDS100_PWR_RST;
3272 /* initialize high byte for jtag */
3273 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3275 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3276 return ERROR_JTAG_INIT_FAILED;
3279 return ERROR_OK;
3282 static void olimex_jtag_blink(void)
3284 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3285 * ACBUS3 is bit 3 of the GPIOH port
3287 high_output ^= 0x08;
3289 buffer_write(0x82);
3290 buffer_write(high_output);
3291 buffer_write(high_direction);
3294 static void flyswatter_jtag_blink(unsigned char led)
3296 buffer_write(0x82);
3297 buffer_write(high_output ^ led);
3298 buffer_write(high_direction);
3301 static void flyswatter1_jtag_blink(void)
3304 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3306 flyswatter_jtag_blink(0xc);
3309 static void flyswatter2_jtag_blink(void)
3312 * Flyswatter2 only has one LED connected to ACBUS2
3314 flyswatter_jtag_blink(0x4);
3317 static void turtle_jtag_blink(void)
3320 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3322 if (high_output & 0x08)
3324 high_output = 0x04;
3326 else
3328 high_output = 0x08;
3331 buffer_write(0x82);
3332 buffer_write(high_output);
3333 buffer_write(high_direction);
3336 static void lisa_l_blink(void)
3339 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3341 if (high_output & 0x10)
3343 high_output = 0x08;
3345 else
3347 high_output = 0x10;
3350 buffer_write(0x82);
3351 buffer_write(high_output);
3352 buffer_write(high_direction);
3355 static void flossjtag_blink(void)
3358 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3360 if (high_output & 0x10)
3362 high_output = 0x08;
3364 else
3366 high_output = 0x10;
3369 buffer_write(0x82);
3370 buffer_write(high_output);
3371 buffer_write(high_direction);
3374 static int ft2232_quit(void)
3376 #if BUILD_FT2232_FTD2XX == 1
3377 FT_STATUS status;
3379 status = FT_Close(ftdih);
3380 #elif BUILD_FT2232_LIBFTDI == 1
3381 ftdi_usb_close(&ftdic);
3383 ftdi_deinit(&ftdic);
3384 #endif
3386 free(ft2232_buffer);
3387 ft2232_buffer = NULL;
3389 return ERROR_OK;
3392 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3394 char *cp;
3395 char buf[200];
3396 if (CMD_ARGC == 1)
3398 ft2232_device_desc = strdup(CMD_ARGV[0]);
3399 cp = strchr(ft2232_device_desc, 0);
3400 /* under Win32, the FTD2XX driver appends an "A" to the end
3401 * of the description, this examines the given desc
3402 * and creates the 'missing' _A or non_A variable. */
3403 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3404 /* it was, so make this the "A" version. */
3405 ft2232_device_desc_A = ft2232_device_desc;
3406 /* and *CREATE* the non-A version. */
3407 strcpy(buf, ft2232_device_desc);
3408 cp = strchr(buf, 0);
3409 cp[-2] = 0;
3410 ft2232_device_desc = strdup(buf);
3411 } else {
3412 /* <space > A not defined
3413 * so create it */
3414 sprintf(buf, "%s A", ft2232_device_desc);
3415 ft2232_device_desc_A = strdup(buf);
3418 else
3420 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3423 return ERROR_OK;
3426 COMMAND_HANDLER(ft2232_handle_serial_command)
3428 if (CMD_ARGC == 1)
3430 ft2232_serial = strdup(CMD_ARGV[0]);
3432 else
3434 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3437 return ERROR_OK;
3440 COMMAND_HANDLER(ft2232_handle_layout_command)
3442 if (CMD_ARGC != 1) {
3443 LOG_ERROR("Need exactly one argument to ft2232_layout");
3444 return ERROR_FAIL;
3447 if (layout) {
3448 LOG_ERROR("already specified ft2232_layout %s",
3449 layout->name);
3450 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3451 ? ERROR_FAIL
3452 : ERROR_OK;
3455 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3456 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3457 layout = l;
3458 return ERROR_OK;
3462 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3463 return ERROR_FAIL;
3466 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3468 if (CMD_ARGC > MAX_USB_IDS * 2)
3470 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3471 "(maximum is %d pairs)", MAX_USB_IDS);
3472 CMD_ARGC = MAX_USB_IDS * 2;
3474 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3476 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3477 if (CMD_ARGC < 2)
3478 return ERROR_COMMAND_SYNTAX_ERROR;
3479 /* remove the incomplete trailing id */
3480 CMD_ARGC -= 1;
3483 unsigned i;
3484 for (i = 0; i < CMD_ARGC; i += 2)
3486 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3487 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3491 * Explicitly terminate, in case there are multiples instances of
3492 * ft2232_vid_pid.
3494 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3496 return ERROR_OK;
3499 COMMAND_HANDLER(ft2232_handle_latency_command)
3501 if (CMD_ARGC == 1)
3503 ft2232_latency = atoi(CMD_ARGV[0]);
3505 else
3507 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3510 return ERROR_OK;
3513 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3515 int retval = 0;
3517 /* 7 bits of either ones or zeros. */
3518 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3520 while (num_cycles > 0)
3522 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3523 * at most 7 bits per invocation. Here we invoke it potentially
3524 * several times.
3526 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3528 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3530 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3531 retval = ERROR_JTAG_QUEUE_FAILED;
3533 first_unsent = cmd;
3536 /* there are no state transitions in this code, so omit state tracking */
3538 /* command "Clock Data to TMS/CS Pin (no Read)" */
3539 buffer_write(0x4b);
3541 /* scan 7 bit */
3542 buffer_write(bitcount_per_command - 1);
3544 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3545 buffer_write(tms);
3547 require_send = 1;
3549 num_cycles -= bitcount_per_command;
3552 return retval;
3555 /* ---------------------------------------------------------------------
3556 * Support for IceBear JTAG adapter from Section5:
3557 * http://section5.ch/icebear
3559 * Author: Sten, debian@sansys-electronic.com
3562 /* Icebear pin layout
3564 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3565 * GND GND | 4 3| n.c.
3566 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3567 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3568 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3569 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3570 * ADBUS2 TDO |14 13| GND GND
3572 * ADBUS0 O L TCK ACBUS0 GND
3573 * ADBUS1 O L TDI ACBUS1 GND
3574 * ADBUS2 I TDO ACBUS2 n.c.
3575 * ADBUS3 O H TMS ACBUS3 n.c.
3576 * ADBUS4 O H nTRST
3577 * ADBUS5 O H nSRST
3578 * ADBUS6 - VCC
3579 * ADBUS7 - GND
3581 static int icebear_jtag_init(void) {
3582 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3583 low_output = 0x08; /* high: TMS; low: TCK TDI */
3584 nTRST = 0x10;
3585 nSRST = 0x20;
3587 enum reset_types jtag_reset_config = jtag_get_reset_config();
3588 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3589 low_direction &= ~nTRST; /* nTRST high impedance */
3591 else {
3592 low_direction |= nTRST;
3593 low_output |= nTRST;
3596 low_direction |= nSRST;
3597 low_output |= nSRST;
3599 /* initialize low byte for jtag */
3600 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3601 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3602 return ERROR_JTAG_INIT_FAILED;
3605 high_output = 0x0;
3606 high_direction = 0x00;
3608 /* initialize high byte for jtag */
3609 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3610 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3611 return ERROR_JTAG_INIT_FAILED;
3614 return ERROR_OK;
3617 static void icebear_jtag_reset(int trst, int srst) {
3619 if (trst == 1) {
3620 low_direction |= nTRST;
3621 low_output &= ~nTRST;
3623 else if (trst == 0) {
3624 enum reset_types jtag_reset_config = jtag_get_reset_config();
3625 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3626 low_direction &= ~nTRST;
3627 else
3628 low_output |= nTRST;
3631 if (srst == 1) {
3632 low_output &= ~nSRST;
3634 else if (srst == 0) {
3635 low_output |= nSRST;
3638 /* command "set data bits low byte" */
3639 buffer_write(0x80);
3640 buffer_write(low_output);
3641 buffer_write(low_direction);
3643 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3646 /* ---------------------------------------------------------------------
3647 * Support for Signalyzer H2 and Signalyzer H4
3648 * JTAG adapter from Xverve Technologies Inc.
3649 * http://www.signalyzer.com or http://www.xverve.com
3651 * Author: Oleg Seiljus, oleg@signalyzer.com
3653 static unsigned char signalyzer_h_side;
3654 static unsigned int signalyzer_h_adapter_type;
3656 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3658 #if BUILD_FT2232_FTD2XX == 1
3659 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3660 #endif
3662 #define SIGNALYZER_COMMAND_ADDR 128
3663 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3665 #define SIGNALYZER_COMMAND_VERSION 0x41
3666 #define SIGNALYZER_COMMAND_RESET 0x42
3667 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3668 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3669 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3670 #define SIGNALYZER_COMMAND_LED_SET 0x53
3671 #define SIGNALYZER_COMMAND_ADC 0x54
3672 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3673 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3674 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3675 #define SIGNALYZER_COMMAND_I2C 0x58
3677 #define SIGNALYZER_CHAN_A 1
3678 #define SIGNALYZER_CHAN_B 2
3679 /* LEDS use channel C */
3680 #define SIGNALYZER_CHAN_C 4
3682 #define SIGNALYZER_LED_GREEN 1
3683 #define SIGNALYZER_LED_RED 2
3685 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3686 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3687 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3688 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3689 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3692 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3694 #if BUILD_FT2232_FTD2XX == 1
3695 return FT_WriteEE(ftdih, address, value);
3696 #elif BUILD_FT2232_LIBFTDI == 1
3697 return 0;
3698 #endif
3701 #if BUILD_FT2232_FTD2XX == 1
3702 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3704 return FT_ReadEE(ftdih, address, value);
3706 #endif
3708 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3709 int on_time_ms, int off_time_ms, unsigned char cycles)
3711 unsigned char on_time;
3712 unsigned char off_time;
3714 if (on_time_ms < 0xFFFF)
3715 on_time = (unsigned char)(on_time_ms / 62);
3716 else
3717 on_time = 0xFF;
3719 off_time = (unsigned char)(off_time_ms / 62);
3721 #if BUILD_FT2232_FTD2XX == 1
3722 FT_STATUS status;
3724 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3725 ((uint32_t)(channel << 8) | led))) != FT_OK)
3727 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3728 ftd2xx_status_string(status));
3729 return ERROR_JTAG_DEVICE_ERROR;
3732 if ((status = signalyzer_h_ctrl_write(
3733 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3734 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3736 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3737 ftd2xx_status_string(status));
3738 return ERROR_JTAG_DEVICE_ERROR;
3741 if ((status = signalyzer_h_ctrl_write(
3742 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3743 ((uint32_t)cycles))) != FT_OK)
3745 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3746 ftd2xx_status_string(status));
3747 return ERROR_JTAG_DEVICE_ERROR;
3750 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3751 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3753 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3754 ftd2xx_status_string(status));
3755 return ERROR_JTAG_DEVICE_ERROR;
3758 return ERROR_OK;
3759 #elif BUILD_FT2232_LIBFTDI == 1
3760 int retval;
3762 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3763 ((uint32_t)(channel << 8) | led))) < 0)
3765 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3766 ftdi_get_error_string(&ftdic));
3767 return ERROR_JTAG_DEVICE_ERROR;
3770 if ((retval = signalyzer_h_ctrl_write(
3771 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3772 ((uint32_t)(on_time << 8) | off_time))) < 0)
3774 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3775 ftdi_get_error_string(&ftdic));
3776 return ERROR_JTAG_DEVICE_ERROR;
3779 if ((retval = signalyzer_h_ctrl_write(
3780 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3781 (uint32_t)cycles)) < 0)
3783 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3784 ftdi_get_error_string(&ftdic));
3785 return ERROR_JTAG_DEVICE_ERROR;
3788 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3789 SIGNALYZER_COMMAND_LED_SET)) < 0)
3791 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3792 ftdi_get_error_string(&ftdic));
3793 return ERROR_JTAG_DEVICE_ERROR;
3796 return ERROR_OK;
3797 #endif
3800 static int signalyzer_h_init(void)
3802 #if BUILD_FT2232_FTD2XX == 1
3803 FT_STATUS status;
3804 int i;
3805 #endif
3807 char *end_of_desc;
3809 uint16_t read_buf[12] = { 0 };
3811 /* turn on center green led */
3812 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3813 0xFFFF, 0x00, 0x00);
3815 /* determine what channel config wants to open
3816 * TODO: change me... current implementation is made to work
3817 * with openocd description parsing.
3819 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3821 if (end_of_desc)
3823 signalyzer_h_side = *(end_of_desc - 1);
3824 if (signalyzer_h_side == 'B')
3825 signalyzer_h_side = SIGNALYZER_CHAN_B;
3826 else
3827 signalyzer_h_side = SIGNALYZER_CHAN_A;
3829 else
3831 LOG_ERROR("No Channel was specified");
3832 return ERROR_FAIL;
3835 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3836 1000, 1000, 0xFF);
3838 #if BUILD_FT2232_FTD2XX == 1
3839 /* read signalyzer versionining information */
3840 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3841 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3843 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3844 ftd2xx_status_string(status));
3845 return ERROR_JTAG_DEVICE_ERROR;
3848 for (i = 0; i < 10; i++)
3850 if ((status = signalyzer_h_ctrl_read(
3851 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3852 &read_buf[i])) != FT_OK)
3854 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3855 ftd2xx_status_string(status));
3856 return ERROR_JTAG_DEVICE_ERROR;
3860 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3861 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3862 read_buf[4], read_buf[5], read_buf[6]);
3864 /* set gpio register */
3865 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3866 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3868 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3869 ftd2xx_status_string(status));
3870 return ERROR_JTAG_DEVICE_ERROR;
3873 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3874 0x0404)) != FT_OK)
3876 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3877 ftd2xx_status_string(status));
3878 return ERROR_JTAG_DEVICE_ERROR;
3881 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3882 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3884 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3885 ftd2xx_status_string(status));
3886 return ERROR_JTAG_DEVICE_ERROR;
3889 /* read adapter type information */
3890 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3891 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3893 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3894 ftd2xx_status_string(status));
3895 return ERROR_JTAG_DEVICE_ERROR;
3898 if ((status = signalyzer_h_ctrl_write(
3899 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3901 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3902 ftd2xx_status_string(status));
3903 return ERROR_JTAG_DEVICE_ERROR;
3906 if ((status = signalyzer_h_ctrl_write(
3907 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3909 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3910 ftd2xx_status_string(status));
3911 return ERROR_JTAG_DEVICE_ERROR;
3914 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3915 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3917 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3918 ftd2xx_status_string(status));
3919 return ERROR_JTAG_DEVICE_ERROR;
3922 usleep(100000);
3924 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3925 &read_buf[0])) != FT_OK)
3927 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3928 ftd2xx_status_string(status));
3929 return ERROR_JTAG_DEVICE_ERROR;
3932 if (read_buf[0] != 0x0498)
3933 signalyzer_h_adapter_type = 0x0000;
3934 else
3936 for (i = 0; i < 4; i++)
3938 if ((status = signalyzer_h_ctrl_read(
3939 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3940 &read_buf[i])) != FT_OK)
3942 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3943 ftd2xx_status_string(status));
3944 return ERROR_JTAG_DEVICE_ERROR;
3948 signalyzer_h_adapter_type = read_buf[0];
3951 #elif BUILD_FT2232_LIBFTDI == 1
3952 /* currently libftdi does not allow reading individual eeprom
3953 * locations, therefore adapter type cannot be detected.
3954 * override with most common type
3956 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3957 #endif
3959 enum reset_types jtag_reset_config = jtag_get_reset_config();
3961 /* ADAPTOR: EM_LT16_A */
3962 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3964 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3965 "detected. (HW: %2x).", (read_buf[1] >> 8));
3967 nTRST = 0x10;
3968 nTRSTnOE = 0x10;
3969 nSRST = 0x20;
3970 nSRSTnOE = 0x20;
3972 low_output = 0x08;
3973 low_direction = 0x1b;
3975 high_output = 0x0;
3976 high_direction = 0x0;
3978 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3980 low_direction &= ~nTRSTnOE; /* nTRST input */
3981 low_output &= ~nTRST; /* nTRST = 0 */
3983 else
3985 low_direction |= nTRSTnOE; /* nTRST output */
3986 low_output |= nTRST; /* nTRST = 1 */
3989 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3991 low_direction |= nSRSTnOE; /* nSRST output */
3992 low_output |= nSRST; /* nSRST = 1 */
3994 else
3996 low_direction &= ~nSRSTnOE; /* nSRST input */
3997 low_output &= ~nSRST; /* nSRST = 0 */
4000 #if BUILD_FT2232_FTD2XX == 1
4001 /* enable power to the module */
4002 if ((status = signalyzer_h_ctrl_write(
4003 SIGNALYZER_DATA_BUFFER_ADDR,
4004 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4005 != FT_OK)
4007 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4008 ftd2xx_status_string(status));
4009 return ERROR_JTAG_DEVICE_ERROR;
4012 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4013 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4015 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4016 ftd2xx_status_string(status));
4017 return ERROR_JTAG_DEVICE_ERROR;
4020 /* set gpio mode register */
4021 if ((status = signalyzer_h_ctrl_write(
4022 SIGNALYZER_DATA_BUFFER_ADDR,
4023 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4025 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4026 ftd2xx_status_string(status));
4027 return ERROR_JTAG_DEVICE_ERROR;
4030 if ((status = signalyzer_h_ctrl_write(
4031 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4032 != FT_OK)
4034 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4035 ftd2xx_status_string(status));
4036 return ERROR_JTAG_DEVICE_ERROR;
4039 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4040 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4042 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4043 ftd2xx_status_string(status));
4044 return ERROR_JTAG_DEVICE_ERROR;
4047 /* set gpio register */
4048 if ((status = signalyzer_h_ctrl_write(
4049 SIGNALYZER_DATA_BUFFER_ADDR,
4050 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4052 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4053 ftd2xx_status_string(status));
4054 return ERROR_JTAG_DEVICE_ERROR;
4057 if ((status = signalyzer_h_ctrl_write(
4058 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
4059 != FT_OK)
4061 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4062 ftd2xx_status_string(status));
4063 return ERROR_JTAG_DEVICE_ERROR;
4066 if ((status = signalyzer_h_ctrl_write(
4067 SIGNALYZER_COMMAND_ADDR,
4068 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4070 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4071 ftd2xx_status_string(status));
4072 return ERROR_JTAG_DEVICE_ERROR;
4074 #endif
4077 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4078 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4079 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4080 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4081 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4083 if (signalyzer_h_adapter_type
4084 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
4085 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
4086 "detected. (HW: %2x).", (read_buf[1] >> 8));
4087 else if (signalyzer_h_adapter_type
4088 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
4089 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
4090 "(ARM JTAG with PSU) detected. (HW: %2x).",
4091 (read_buf[1] >> 8));
4092 else if (signalyzer_h_adapter_type
4093 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
4094 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
4095 "detected. (HW: %2x).", (read_buf[1] >> 8));
4096 else if (signalyzer_h_adapter_type
4097 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
4098 LOG_INFO("Signalyzer: EM-JTAG-P "
4099 "(Generic JTAG with PSU) detected. (HW: %2x).",
4100 (read_buf[1] >> 8));
4102 nTRST = 0x02;
4103 nTRSTnOE = 0x04;
4104 nSRST = 0x08;
4105 nSRSTnOE = 0x10;
4107 low_output = 0x08;
4108 low_direction = 0x1b;
4110 high_output = 0x0;
4111 high_direction = 0x1f;
4113 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4115 high_output |= nTRSTnOE;
4116 high_output &= ~nTRST;
4118 else
4120 high_output &= ~nTRSTnOE;
4121 high_output |= nTRST;
4124 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4126 high_output &= ~nSRSTnOE;
4127 high_output |= nSRST;
4129 else
4131 high_output |= nSRSTnOE;
4132 high_output &= ~nSRST;
4135 #if BUILD_FT2232_FTD2XX == 1
4136 /* enable power to the module */
4137 if ((status = signalyzer_h_ctrl_write(
4138 SIGNALYZER_DATA_BUFFER_ADDR,
4139 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4140 != FT_OK)
4142 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4143 ftd2xx_status_string(status));
4144 return ERROR_JTAG_DEVICE_ERROR;
4147 if ((status = signalyzer_h_ctrl_write(
4148 SIGNALYZER_COMMAND_ADDR,
4149 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4151 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4152 ftd2xx_status_string(status));
4153 return ERROR_JTAG_DEVICE_ERROR;
4156 /* set gpio mode register (IO_16 and IO_17 set as analog
4157 * inputs, other is gpio)
4159 if ((status = signalyzer_h_ctrl_write(
4160 SIGNALYZER_DATA_BUFFER_ADDR,
4161 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4163 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4164 ftd2xx_status_string(status));
4165 return ERROR_JTAG_DEVICE_ERROR;
4168 if ((status = signalyzer_h_ctrl_write(
4169 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4170 != FT_OK)
4172 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4173 ftd2xx_status_string(status));
4174 return ERROR_JTAG_DEVICE_ERROR;
4177 if ((status = signalyzer_h_ctrl_write(
4178 SIGNALYZER_COMMAND_ADDR,
4179 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4181 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4182 ftd2xx_status_string(status));
4183 return ERROR_JTAG_DEVICE_ERROR;
4186 /* set gpio register (all inputs, for -P modules,
4187 * PSU will be turned off)
4189 if ((status = signalyzer_h_ctrl_write(
4190 SIGNALYZER_DATA_BUFFER_ADDR,
4191 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4193 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4194 ftd2xx_status_string(status));
4195 return ERROR_JTAG_DEVICE_ERROR;
4198 if ((status = signalyzer_h_ctrl_write(
4199 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4200 != FT_OK)
4202 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4203 ftd2xx_status_string(status));
4204 return ERROR_JTAG_DEVICE_ERROR;
4207 if ((status = signalyzer_h_ctrl_write(
4208 SIGNALYZER_COMMAND_ADDR,
4209 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4211 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4212 ftd2xx_status_string(status));
4213 return ERROR_JTAG_DEVICE_ERROR;
4215 #endif
4218 else if (signalyzer_h_adapter_type == 0x0000)
4220 LOG_INFO("Signalyzer: No external modules were detected.");
4222 nTRST = 0x10;
4223 nTRSTnOE = 0x10;
4224 nSRST = 0x20;
4225 nSRSTnOE = 0x20;
4227 low_output = 0x08;
4228 low_direction = 0x1b;
4230 high_output = 0x0;
4231 high_direction = 0x0;
4233 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4235 low_direction &= ~nTRSTnOE; /* nTRST input */
4236 low_output &= ~nTRST; /* nTRST = 0 */
4238 else
4240 low_direction |= nTRSTnOE; /* nTRST output */
4241 low_output |= nTRST; /* nTRST = 1 */
4244 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4246 low_direction |= nSRSTnOE; /* nSRST output */
4247 low_output |= nSRST; /* nSRST = 1 */
4249 else
4251 low_direction &= ~nSRSTnOE; /* nSRST input */
4252 low_output &= ~nSRST; /* nSRST = 0 */
4255 else
4257 LOG_ERROR("Unknown module type is detected: %.4x",
4258 signalyzer_h_adapter_type);
4259 return ERROR_JTAG_DEVICE_ERROR;
4262 /* initialize low byte of controller for jtag operation */
4263 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4265 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4266 return ERROR_JTAG_INIT_FAILED;
4269 #if BUILD_FT2232_FTD2XX == 1
4270 if (ftdi_device == FT_DEVICE_2232H)
4272 /* initialize high byte of controller for jtag operation */
4273 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4275 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4276 return ERROR_JTAG_INIT_FAILED;
4279 #elif BUILD_FT2232_LIBFTDI == 1
4280 if (ftdi_device == TYPE_2232H)
4282 /* initialize high byte of controller for jtag operation */
4283 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4285 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4286 return ERROR_JTAG_INIT_FAILED;
4289 #endif
4290 return ERROR_OK;
4293 static void signalyzer_h_reset(int trst, int srst)
4295 enum reset_types jtag_reset_config = jtag_get_reset_config();
4297 /* ADAPTOR: EM_LT16_A */
4298 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4300 if (trst == 1)
4302 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4303 /* switch to output pin (output is low) */
4304 low_direction |= nTRSTnOE;
4305 else
4306 /* switch output low */
4307 low_output &= ~nTRST;
4309 else if (trst == 0)
4311 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4312 /* switch to input pin (high-Z + internal
4313 * and external pullup) */
4314 low_direction &= ~nTRSTnOE;
4315 else
4316 /* switch output high */
4317 low_output |= nTRST;
4320 if (srst == 1)
4322 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4323 /* switch output low */
4324 low_output &= ~nSRST;
4325 else
4326 /* switch to output pin (output is low) */
4327 low_direction |= nSRSTnOE;
4329 else if (srst == 0)
4331 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4332 /* switch output high */
4333 low_output |= nSRST;
4334 else
4335 /* switch to input pin (high-Z) */
4336 low_direction &= ~nSRSTnOE;
4339 /* command "set data bits low byte" */
4340 buffer_write(0x80);
4341 buffer_write(low_output);
4342 buffer_write(low_direction);
4343 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4344 "low_direction: 0x%2.2x",
4345 trst, srst, low_output, low_direction);
4347 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4348 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4349 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4350 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4351 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4353 if (trst == 1)
4355 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4356 high_output &= ~nTRSTnOE;
4357 else
4358 high_output &= ~nTRST;
4360 else if (trst == 0)
4362 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4363 high_output |= nTRSTnOE;
4364 else
4365 high_output |= nTRST;
4368 if (srst == 1)
4370 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4371 high_output &= ~nSRST;
4372 else
4373 high_output &= ~nSRSTnOE;
4375 else if (srst == 0)
4377 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4378 high_output |= nSRST;
4379 else
4380 high_output |= nSRSTnOE;
4383 /* command "set data bits high byte" */
4384 buffer_write(0x82);
4385 buffer_write(high_output);
4386 buffer_write(high_direction);
4387 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4388 "high_direction: 0x%2.2x",
4389 trst, srst, high_output, high_direction);
4391 else if (signalyzer_h_adapter_type == 0x0000)
4393 if (trst == 1)
4395 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4396 /* switch to output pin (output is low) */
4397 low_direction |= nTRSTnOE;
4398 else
4399 /* switch output low */
4400 low_output &= ~nTRST;
4402 else if (trst == 0)
4404 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4405 /* switch to input pin (high-Z + internal
4406 * and external pullup) */
4407 low_direction &= ~nTRSTnOE;
4408 else
4409 /* switch output high */
4410 low_output |= nTRST;
4413 if (srst == 1)
4415 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4416 /* switch output low */
4417 low_output &= ~nSRST;
4418 else
4419 /* switch to output pin (output is low) */
4420 low_direction |= nSRSTnOE;
4422 else if (srst == 0)
4424 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4425 /* switch output high */
4426 low_output |= nSRST;
4427 else
4428 /* switch to input pin (high-Z) */
4429 low_direction &= ~nSRSTnOE;
4432 /* command "set data bits low byte" */
4433 buffer_write(0x80);
4434 buffer_write(low_output);
4435 buffer_write(low_direction);
4436 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4437 "low_direction: 0x%2.2x",
4438 trst, srst, low_output, low_direction);
4442 static void signalyzer_h_blink(void)
4444 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4447 /********************************************************************
4448 * Support for KT-LINK
4449 * JTAG adapter from KRISTECH
4450 * http://www.kristech.eu
4451 *******************************************************************/
4452 static int ktlink_init(void)
4454 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4456 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4457 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4459 /* initialize low byte for jtag */
4460 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4462 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4463 return ERROR_JTAG_INIT_FAILED;
4466 nTRST = 0x01;
4467 nSRST = 0x02;
4468 nTRSTnOE = 0x04;
4469 nSRSTnOE = 0x08;
4471 high_output = 0x80; // turn LED on
4472 high_direction = 0xFF; // all outputs
4474 enum reset_types jtag_reset_config = jtag_get_reset_config();
4476 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4477 high_output |= nTRSTnOE;
4478 high_output &= ~nTRST;
4479 } else {
4480 high_output &= ~nTRSTnOE;
4481 high_output |= nTRST;
4484 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4485 high_output &= ~nSRSTnOE;
4486 high_output |= nSRST;
4487 } else {
4488 high_output |= nSRSTnOE;
4489 high_output &= ~nSRST;
4492 /* initialize high byte for jtag */
4493 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4495 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4496 return ERROR_JTAG_INIT_FAILED;
4499 return ERROR_OK;
4502 static void ktlink_reset(int trst, int srst)
4504 enum reset_types jtag_reset_config = jtag_get_reset_config();
4506 if (trst == 1) {
4507 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4508 high_output &= ~nTRSTnOE;
4509 else
4510 high_output &= ~nTRST;
4511 } else if (trst == 0) {
4512 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4513 high_output |= nTRSTnOE;
4514 else
4515 high_output |= nTRST;
4518 if (srst == 1) {
4519 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4520 high_output &= ~nSRST;
4521 else
4522 high_output &= ~nSRSTnOE;
4523 } else if (srst == 0) {
4524 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4525 high_output |= nSRST;
4526 else
4527 high_output |= nSRSTnOE;
4530 buffer_write(0x82); // command "set data bits high byte"
4531 buffer_write(high_output);
4532 buffer_write(high_direction);
4533 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4536 static void ktlink_blink(void)
4538 /* LED connected to ACBUS7 */
4539 high_output ^= 0x80;
4541 buffer_write(0x82); // command "set data bits high byte"
4542 buffer_write(high_output);
4543 buffer_write(high_direction);
4546 static const struct command_registration ft2232_command_handlers[] = {
4548 .name = "ft2232_device_desc",
4549 .handler = &ft2232_handle_device_desc_command,
4550 .mode = COMMAND_CONFIG,
4551 .help = "set the USB device description of the FTDI FT2232 device",
4552 .usage = "description_string",
4555 .name = "ft2232_serial",
4556 .handler = &ft2232_handle_serial_command,
4557 .mode = COMMAND_CONFIG,
4558 .help = "set the serial number of the FTDI FT2232 device",
4559 .usage = "serial_string",
4562 .name = "ft2232_layout",
4563 .handler = &ft2232_handle_layout_command,
4564 .mode = COMMAND_CONFIG,
4565 .help = "set the layout of the FT2232 GPIO signals used "
4566 "to control output-enables and reset signals",
4567 .usage = "layout_name",
4570 .name = "ft2232_vid_pid",
4571 .handler = &ft2232_handle_vid_pid_command,
4572 .mode = COMMAND_CONFIG,
4573 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4574 .usage = "(vid pid)* ",
4577 .name = "ft2232_latency",
4578 .handler = &ft2232_handle_latency_command,
4579 .mode = COMMAND_CONFIG,
4580 .help = "set the FT2232 latency timer to a new value",
4581 .usage = "value",
4583 COMMAND_REGISTRATION_DONE
4586 struct jtag_interface ft2232_interface = {
4587 .name = "ft2232",
4588 .supported = DEBUG_CAP_TMS_SEQ,
4589 .commands = ft2232_command_handlers,
4590 .transports = jtag_only,
4592 .init = ft2232_init,
4593 .quit = ft2232_quit,
4594 .speed = ft2232_speed,
4595 .speed_div = ft2232_speed_div,
4596 .khz = ft2232_khz,
4597 .execute_queue = ft2232_execute_queue,