Change return value on error.
[openocd.git] / src / jtag / drivers / ft2232.c
blob4b9bdef7dd5ae789faa4796f38fda6412f19f3ce
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 return ERROR_COMMAND_SYNTAX_ERROR;
3437 return ERROR_OK;
3440 COMMAND_HANDLER(ft2232_handle_layout_command)
3442 if (CMD_ARGC != 1) {
3443 return ERROR_COMMAND_SYNTAX_ERROR;
3446 if (layout) {
3447 LOG_ERROR("already specified ft2232_layout %s",
3448 layout->name);
3449 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3450 ? ERROR_FAIL
3451 : ERROR_OK;
3454 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3455 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3456 layout = l;
3457 return ERROR_OK;
3461 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3462 return ERROR_FAIL;
3465 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3467 if (CMD_ARGC > MAX_USB_IDS * 2)
3469 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3470 "(maximum is %d pairs)", MAX_USB_IDS);
3471 CMD_ARGC = MAX_USB_IDS * 2;
3473 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3475 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3476 if (CMD_ARGC < 2)
3477 return ERROR_COMMAND_SYNTAX_ERROR;
3478 /* remove the incomplete trailing id */
3479 CMD_ARGC -= 1;
3482 unsigned i;
3483 for (i = 0; i < CMD_ARGC; i += 2)
3485 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3486 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3490 * Explicitly terminate, in case there are multiples instances of
3491 * ft2232_vid_pid.
3493 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3495 return ERROR_OK;
3498 COMMAND_HANDLER(ft2232_handle_latency_command)
3500 if (CMD_ARGC == 1)
3502 ft2232_latency = atoi(CMD_ARGV[0]);
3504 else
3506 return ERROR_COMMAND_SYNTAX_ERROR;
3509 return ERROR_OK;
3512 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3514 int retval = 0;
3516 /* 7 bits of either ones or zeros. */
3517 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3519 while (num_cycles > 0)
3521 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3522 * at most 7 bits per invocation. Here we invoke it potentially
3523 * several times.
3525 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3527 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3529 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3530 retval = ERROR_JTAG_QUEUE_FAILED;
3532 first_unsent = cmd;
3535 /* there are no state transitions in this code, so omit state tracking */
3537 /* command "Clock Data to TMS/CS Pin (no Read)" */
3538 buffer_write(0x4b);
3540 /* scan 7 bit */
3541 buffer_write(bitcount_per_command - 1);
3543 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3544 buffer_write(tms);
3546 require_send = 1;
3548 num_cycles -= bitcount_per_command;
3551 return retval;
3554 /* ---------------------------------------------------------------------
3555 * Support for IceBear JTAG adapter from Section5:
3556 * http://section5.ch/icebear
3558 * Author: Sten, debian@sansys-electronic.com
3561 /* Icebear pin layout
3563 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3564 * GND GND | 4 3| n.c.
3565 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3566 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3567 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3568 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3569 * ADBUS2 TDO |14 13| GND GND
3571 * ADBUS0 O L TCK ACBUS0 GND
3572 * ADBUS1 O L TDI ACBUS1 GND
3573 * ADBUS2 I TDO ACBUS2 n.c.
3574 * ADBUS3 O H TMS ACBUS3 n.c.
3575 * ADBUS4 O H nTRST
3576 * ADBUS5 O H nSRST
3577 * ADBUS6 - VCC
3578 * ADBUS7 - GND
3580 static int icebear_jtag_init(void) {
3581 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3582 low_output = 0x08; /* high: TMS; low: TCK TDI */
3583 nTRST = 0x10;
3584 nSRST = 0x20;
3586 enum reset_types jtag_reset_config = jtag_get_reset_config();
3587 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3588 low_direction &= ~nTRST; /* nTRST high impedance */
3590 else {
3591 low_direction |= nTRST;
3592 low_output |= nTRST;
3595 low_direction |= nSRST;
3596 low_output |= nSRST;
3598 /* initialize low byte for jtag */
3599 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3600 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3601 return ERROR_JTAG_INIT_FAILED;
3604 high_output = 0x0;
3605 high_direction = 0x00;
3607 /* initialize high byte for jtag */
3608 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3609 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3610 return ERROR_JTAG_INIT_FAILED;
3613 return ERROR_OK;
3616 static void icebear_jtag_reset(int trst, int srst) {
3618 if (trst == 1) {
3619 low_direction |= nTRST;
3620 low_output &= ~nTRST;
3622 else if (trst == 0) {
3623 enum reset_types jtag_reset_config = jtag_get_reset_config();
3624 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3625 low_direction &= ~nTRST;
3626 else
3627 low_output |= nTRST;
3630 if (srst == 1) {
3631 low_output &= ~nSRST;
3633 else if (srst == 0) {
3634 low_output |= nSRST;
3637 /* command "set data bits low byte" */
3638 buffer_write(0x80);
3639 buffer_write(low_output);
3640 buffer_write(low_direction);
3642 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3645 /* ---------------------------------------------------------------------
3646 * Support for Signalyzer H2 and Signalyzer H4
3647 * JTAG adapter from Xverve Technologies Inc.
3648 * http://www.signalyzer.com or http://www.xverve.com
3650 * Author: Oleg Seiljus, oleg@signalyzer.com
3652 static unsigned char signalyzer_h_side;
3653 static unsigned int signalyzer_h_adapter_type;
3655 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3657 #if BUILD_FT2232_FTD2XX == 1
3658 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3659 #endif
3661 #define SIGNALYZER_COMMAND_ADDR 128
3662 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3664 #define SIGNALYZER_COMMAND_VERSION 0x41
3665 #define SIGNALYZER_COMMAND_RESET 0x42
3666 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3667 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3668 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3669 #define SIGNALYZER_COMMAND_LED_SET 0x53
3670 #define SIGNALYZER_COMMAND_ADC 0x54
3671 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3672 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3673 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3674 #define SIGNALYZER_COMMAND_I2C 0x58
3676 #define SIGNALYZER_CHAN_A 1
3677 #define SIGNALYZER_CHAN_B 2
3678 /* LEDS use channel C */
3679 #define SIGNALYZER_CHAN_C 4
3681 #define SIGNALYZER_LED_GREEN 1
3682 #define SIGNALYZER_LED_RED 2
3684 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3685 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3686 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3687 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3688 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3691 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3693 #if BUILD_FT2232_FTD2XX == 1
3694 return FT_WriteEE(ftdih, address, value);
3695 #elif BUILD_FT2232_LIBFTDI == 1
3696 return 0;
3697 #endif
3700 #if BUILD_FT2232_FTD2XX == 1
3701 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3703 return FT_ReadEE(ftdih, address, value);
3705 #endif
3707 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3708 int on_time_ms, int off_time_ms, unsigned char cycles)
3710 unsigned char on_time;
3711 unsigned char off_time;
3713 if (on_time_ms < 0xFFFF)
3714 on_time = (unsigned char)(on_time_ms / 62);
3715 else
3716 on_time = 0xFF;
3718 off_time = (unsigned char)(off_time_ms / 62);
3720 #if BUILD_FT2232_FTD2XX == 1
3721 FT_STATUS status;
3723 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3724 ((uint32_t)(channel << 8) | led))) != FT_OK)
3726 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3727 ftd2xx_status_string(status));
3728 return ERROR_JTAG_DEVICE_ERROR;
3731 if ((status = signalyzer_h_ctrl_write(
3732 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3733 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3735 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3736 ftd2xx_status_string(status));
3737 return ERROR_JTAG_DEVICE_ERROR;
3740 if ((status = signalyzer_h_ctrl_write(
3741 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3742 ((uint32_t)cycles))) != FT_OK)
3744 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3745 ftd2xx_status_string(status));
3746 return ERROR_JTAG_DEVICE_ERROR;
3749 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3750 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3752 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3753 ftd2xx_status_string(status));
3754 return ERROR_JTAG_DEVICE_ERROR;
3757 return ERROR_OK;
3758 #elif BUILD_FT2232_LIBFTDI == 1
3759 int retval;
3761 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3762 ((uint32_t)(channel << 8) | led))) < 0)
3764 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3765 ftdi_get_error_string(&ftdic));
3766 return ERROR_JTAG_DEVICE_ERROR;
3769 if ((retval = signalyzer_h_ctrl_write(
3770 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3771 ((uint32_t)(on_time << 8) | off_time))) < 0)
3773 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3774 ftdi_get_error_string(&ftdic));
3775 return ERROR_JTAG_DEVICE_ERROR;
3778 if ((retval = signalyzer_h_ctrl_write(
3779 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3780 (uint32_t)cycles)) < 0)
3782 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3783 ftdi_get_error_string(&ftdic));
3784 return ERROR_JTAG_DEVICE_ERROR;
3787 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3788 SIGNALYZER_COMMAND_LED_SET)) < 0)
3790 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3791 ftdi_get_error_string(&ftdic));
3792 return ERROR_JTAG_DEVICE_ERROR;
3795 return ERROR_OK;
3796 #endif
3799 static int signalyzer_h_init(void)
3801 #if BUILD_FT2232_FTD2XX == 1
3802 FT_STATUS status;
3803 int i;
3804 #endif
3806 char *end_of_desc;
3808 uint16_t read_buf[12] = { 0 };
3810 /* turn on center green led */
3811 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3812 0xFFFF, 0x00, 0x00);
3814 /* determine what channel config wants to open
3815 * TODO: change me... current implementation is made to work
3816 * with openocd description parsing.
3818 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3820 if (end_of_desc)
3822 signalyzer_h_side = *(end_of_desc - 1);
3823 if (signalyzer_h_side == 'B')
3824 signalyzer_h_side = SIGNALYZER_CHAN_B;
3825 else
3826 signalyzer_h_side = SIGNALYZER_CHAN_A;
3828 else
3830 LOG_ERROR("No Channel was specified");
3831 return ERROR_FAIL;
3834 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3835 1000, 1000, 0xFF);
3837 #if BUILD_FT2232_FTD2XX == 1
3838 /* read signalyzer versionining information */
3839 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3840 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3842 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3843 ftd2xx_status_string(status));
3844 return ERROR_JTAG_DEVICE_ERROR;
3847 for (i = 0; i < 10; i++)
3849 if ((status = signalyzer_h_ctrl_read(
3850 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3851 &read_buf[i])) != FT_OK)
3853 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3854 ftd2xx_status_string(status));
3855 return ERROR_JTAG_DEVICE_ERROR;
3859 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3860 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3861 read_buf[4], read_buf[5], read_buf[6]);
3863 /* set gpio register */
3864 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3865 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3867 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3868 ftd2xx_status_string(status));
3869 return ERROR_JTAG_DEVICE_ERROR;
3872 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3873 0x0404)) != FT_OK)
3875 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3876 ftd2xx_status_string(status));
3877 return ERROR_JTAG_DEVICE_ERROR;
3880 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3881 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3883 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3884 ftd2xx_status_string(status));
3885 return ERROR_JTAG_DEVICE_ERROR;
3888 /* read adapter type information */
3889 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3890 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3892 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3893 ftd2xx_status_string(status));
3894 return ERROR_JTAG_DEVICE_ERROR;
3897 if ((status = signalyzer_h_ctrl_write(
3898 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3900 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3901 ftd2xx_status_string(status));
3902 return ERROR_JTAG_DEVICE_ERROR;
3905 if ((status = signalyzer_h_ctrl_write(
3906 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3908 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3909 ftd2xx_status_string(status));
3910 return ERROR_JTAG_DEVICE_ERROR;
3913 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3914 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3916 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3917 ftd2xx_status_string(status));
3918 return ERROR_JTAG_DEVICE_ERROR;
3921 usleep(100000);
3923 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3924 &read_buf[0])) != FT_OK)
3926 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3927 ftd2xx_status_string(status));
3928 return ERROR_JTAG_DEVICE_ERROR;
3931 if (read_buf[0] != 0x0498)
3932 signalyzer_h_adapter_type = 0x0000;
3933 else
3935 for (i = 0; i < 4; i++)
3937 if ((status = signalyzer_h_ctrl_read(
3938 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3939 &read_buf[i])) != FT_OK)
3941 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3942 ftd2xx_status_string(status));
3943 return ERROR_JTAG_DEVICE_ERROR;
3947 signalyzer_h_adapter_type = read_buf[0];
3950 #elif BUILD_FT2232_LIBFTDI == 1
3951 /* currently libftdi does not allow reading individual eeprom
3952 * locations, therefore adapter type cannot be detected.
3953 * override with most common type
3955 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3956 #endif
3958 enum reset_types jtag_reset_config = jtag_get_reset_config();
3960 /* ADAPTOR: EM_LT16_A */
3961 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3963 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3964 "detected. (HW: %2x).", (read_buf[1] >> 8));
3966 nTRST = 0x10;
3967 nTRSTnOE = 0x10;
3968 nSRST = 0x20;
3969 nSRSTnOE = 0x20;
3971 low_output = 0x08;
3972 low_direction = 0x1b;
3974 high_output = 0x0;
3975 high_direction = 0x0;
3977 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3979 low_direction &= ~nTRSTnOE; /* nTRST input */
3980 low_output &= ~nTRST; /* nTRST = 0 */
3982 else
3984 low_direction |= nTRSTnOE; /* nTRST output */
3985 low_output |= nTRST; /* nTRST = 1 */
3988 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3990 low_direction |= nSRSTnOE; /* nSRST output */
3991 low_output |= nSRST; /* nSRST = 1 */
3993 else
3995 low_direction &= ~nSRSTnOE; /* nSRST input */
3996 low_output &= ~nSRST; /* nSRST = 0 */
3999 #if BUILD_FT2232_FTD2XX == 1
4000 /* enable power to the module */
4001 if ((status = signalyzer_h_ctrl_write(
4002 SIGNALYZER_DATA_BUFFER_ADDR,
4003 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4004 != FT_OK)
4006 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4007 ftd2xx_status_string(status));
4008 return ERROR_JTAG_DEVICE_ERROR;
4011 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4012 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4014 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4015 ftd2xx_status_string(status));
4016 return ERROR_JTAG_DEVICE_ERROR;
4019 /* set gpio mode register */
4020 if ((status = signalyzer_h_ctrl_write(
4021 SIGNALYZER_DATA_BUFFER_ADDR,
4022 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4024 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4025 ftd2xx_status_string(status));
4026 return ERROR_JTAG_DEVICE_ERROR;
4029 if ((status = signalyzer_h_ctrl_write(
4030 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4031 != FT_OK)
4033 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4034 ftd2xx_status_string(status));
4035 return ERROR_JTAG_DEVICE_ERROR;
4038 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4039 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4041 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4042 ftd2xx_status_string(status));
4043 return ERROR_JTAG_DEVICE_ERROR;
4046 /* set gpio register */
4047 if ((status = signalyzer_h_ctrl_write(
4048 SIGNALYZER_DATA_BUFFER_ADDR,
4049 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4051 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4052 ftd2xx_status_string(status));
4053 return ERROR_JTAG_DEVICE_ERROR;
4056 if ((status = signalyzer_h_ctrl_write(
4057 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
4058 != FT_OK)
4060 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4061 ftd2xx_status_string(status));
4062 return ERROR_JTAG_DEVICE_ERROR;
4065 if ((status = signalyzer_h_ctrl_write(
4066 SIGNALYZER_COMMAND_ADDR,
4067 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4069 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4070 ftd2xx_status_string(status));
4071 return ERROR_JTAG_DEVICE_ERROR;
4073 #endif
4076 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4077 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4078 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4079 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4080 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4082 if (signalyzer_h_adapter_type
4083 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
4084 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
4085 "detected. (HW: %2x).", (read_buf[1] >> 8));
4086 else if (signalyzer_h_adapter_type
4087 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
4088 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
4089 "(ARM JTAG with PSU) detected. (HW: %2x).",
4090 (read_buf[1] >> 8));
4091 else if (signalyzer_h_adapter_type
4092 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
4093 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
4094 "detected. (HW: %2x).", (read_buf[1] >> 8));
4095 else if (signalyzer_h_adapter_type
4096 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
4097 LOG_INFO("Signalyzer: EM-JTAG-P "
4098 "(Generic JTAG with PSU) detected. (HW: %2x).",
4099 (read_buf[1] >> 8));
4101 nTRST = 0x02;
4102 nTRSTnOE = 0x04;
4103 nSRST = 0x08;
4104 nSRSTnOE = 0x10;
4106 low_output = 0x08;
4107 low_direction = 0x1b;
4109 high_output = 0x0;
4110 high_direction = 0x1f;
4112 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4114 high_output |= nTRSTnOE;
4115 high_output &= ~nTRST;
4117 else
4119 high_output &= ~nTRSTnOE;
4120 high_output |= nTRST;
4123 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4125 high_output &= ~nSRSTnOE;
4126 high_output |= nSRST;
4128 else
4130 high_output |= nSRSTnOE;
4131 high_output &= ~nSRST;
4134 #if BUILD_FT2232_FTD2XX == 1
4135 /* enable power to the module */
4136 if ((status = signalyzer_h_ctrl_write(
4137 SIGNALYZER_DATA_BUFFER_ADDR,
4138 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4139 != FT_OK)
4141 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4142 ftd2xx_status_string(status));
4143 return ERROR_JTAG_DEVICE_ERROR;
4146 if ((status = signalyzer_h_ctrl_write(
4147 SIGNALYZER_COMMAND_ADDR,
4148 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4150 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4151 ftd2xx_status_string(status));
4152 return ERROR_JTAG_DEVICE_ERROR;
4155 /* set gpio mode register (IO_16 and IO_17 set as analog
4156 * inputs, other is gpio)
4158 if ((status = signalyzer_h_ctrl_write(
4159 SIGNALYZER_DATA_BUFFER_ADDR,
4160 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4162 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4163 ftd2xx_status_string(status));
4164 return ERROR_JTAG_DEVICE_ERROR;
4167 if ((status = signalyzer_h_ctrl_write(
4168 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4169 != FT_OK)
4171 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4172 ftd2xx_status_string(status));
4173 return ERROR_JTAG_DEVICE_ERROR;
4176 if ((status = signalyzer_h_ctrl_write(
4177 SIGNALYZER_COMMAND_ADDR,
4178 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4180 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4181 ftd2xx_status_string(status));
4182 return ERROR_JTAG_DEVICE_ERROR;
4185 /* set gpio register (all inputs, for -P modules,
4186 * PSU will be turned off)
4188 if ((status = signalyzer_h_ctrl_write(
4189 SIGNALYZER_DATA_BUFFER_ADDR,
4190 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4192 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4193 ftd2xx_status_string(status));
4194 return ERROR_JTAG_DEVICE_ERROR;
4197 if ((status = signalyzer_h_ctrl_write(
4198 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4199 != FT_OK)
4201 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4202 ftd2xx_status_string(status));
4203 return ERROR_JTAG_DEVICE_ERROR;
4206 if ((status = signalyzer_h_ctrl_write(
4207 SIGNALYZER_COMMAND_ADDR,
4208 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4210 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4211 ftd2xx_status_string(status));
4212 return ERROR_JTAG_DEVICE_ERROR;
4214 #endif
4217 else if (signalyzer_h_adapter_type == 0x0000)
4219 LOG_INFO("Signalyzer: No external modules were detected.");
4221 nTRST = 0x10;
4222 nTRSTnOE = 0x10;
4223 nSRST = 0x20;
4224 nSRSTnOE = 0x20;
4226 low_output = 0x08;
4227 low_direction = 0x1b;
4229 high_output = 0x0;
4230 high_direction = 0x0;
4232 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4234 low_direction &= ~nTRSTnOE; /* nTRST input */
4235 low_output &= ~nTRST; /* nTRST = 0 */
4237 else
4239 low_direction |= nTRSTnOE; /* nTRST output */
4240 low_output |= nTRST; /* nTRST = 1 */
4243 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4245 low_direction |= nSRSTnOE; /* nSRST output */
4246 low_output |= nSRST; /* nSRST = 1 */
4248 else
4250 low_direction &= ~nSRSTnOE; /* nSRST input */
4251 low_output &= ~nSRST; /* nSRST = 0 */
4254 else
4256 LOG_ERROR("Unknown module type is detected: %.4x",
4257 signalyzer_h_adapter_type);
4258 return ERROR_JTAG_DEVICE_ERROR;
4261 /* initialize low byte of controller for jtag operation */
4262 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4264 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4265 return ERROR_JTAG_INIT_FAILED;
4268 #if BUILD_FT2232_FTD2XX == 1
4269 if (ftdi_device == FT_DEVICE_2232H)
4271 /* initialize high byte of controller for jtag operation */
4272 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4274 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4275 return ERROR_JTAG_INIT_FAILED;
4278 #elif BUILD_FT2232_LIBFTDI == 1
4279 if (ftdi_device == TYPE_2232H)
4281 /* initialize high byte of controller for jtag operation */
4282 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4284 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4285 return ERROR_JTAG_INIT_FAILED;
4288 #endif
4289 return ERROR_OK;
4292 static void signalyzer_h_reset(int trst, int srst)
4294 enum reset_types jtag_reset_config = jtag_get_reset_config();
4296 /* ADAPTOR: EM_LT16_A */
4297 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4299 if (trst == 1)
4301 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4302 /* switch to output pin (output is low) */
4303 low_direction |= nTRSTnOE;
4304 else
4305 /* switch output low */
4306 low_output &= ~nTRST;
4308 else if (trst == 0)
4310 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4311 /* switch to input pin (high-Z + internal
4312 * and external pullup) */
4313 low_direction &= ~nTRSTnOE;
4314 else
4315 /* switch output high */
4316 low_output |= nTRST;
4319 if (srst == 1)
4321 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4322 /* switch output low */
4323 low_output &= ~nSRST;
4324 else
4325 /* switch to output pin (output is low) */
4326 low_direction |= nSRSTnOE;
4328 else if (srst == 0)
4330 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4331 /* switch output high */
4332 low_output |= nSRST;
4333 else
4334 /* switch to input pin (high-Z) */
4335 low_direction &= ~nSRSTnOE;
4338 /* command "set data bits low byte" */
4339 buffer_write(0x80);
4340 buffer_write(low_output);
4341 buffer_write(low_direction);
4342 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4343 "low_direction: 0x%2.2x",
4344 trst, srst, low_output, low_direction);
4346 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4347 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4348 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4349 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4350 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4352 if (trst == 1)
4354 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4355 high_output &= ~nTRSTnOE;
4356 else
4357 high_output &= ~nTRST;
4359 else if (trst == 0)
4361 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4362 high_output |= nTRSTnOE;
4363 else
4364 high_output |= nTRST;
4367 if (srst == 1)
4369 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4370 high_output &= ~nSRST;
4371 else
4372 high_output &= ~nSRSTnOE;
4374 else if (srst == 0)
4376 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4377 high_output |= nSRST;
4378 else
4379 high_output |= nSRSTnOE;
4382 /* command "set data bits high byte" */
4383 buffer_write(0x82);
4384 buffer_write(high_output);
4385 buffer_write(high_direction);
4386 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4387 "high_direction: 0x%2.2x",
4388 trst, srst, high_output, high_direction);
4390 else if (signalyzer_h_adapter_type == 0x0000)
4392 if (trst == 1)
4394 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4395 /* switch to output pin (output is low) */
4396 low_direction |= nTRSTnOE;
4397 else
4398 /* switch output low */
4399 low_output &= ~nTRST;
4401 else if (trst == 0)
4403 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4404 /* switch to input pin (high-Z + internal
4405 * and external pullup) */
4406 low_direction &= ~nTRSTnOE;
4407 else
4408 /* switch output high */
4409 low_output |= nTRST;
4412 if (srst == 1)
4414 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4415 /* switch output low */
4416 low_output &= ~nSRST;
4417 else
4418 /* switch to output pin (output is low) */
4419 low_direction |= nSRSTnOE;
4421 else if (srst == 0)
4423 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4424 /* switch output high */
4425 low_output |= nSRST;
4426 else
4427 /* switch to input pin (high-Z) */
4428 low_direction &= ~nSRSTnOE;
4431 /* command "set data bits low byte" */
4432 buffer_write(0x80);
4433 buffer_write(low_output);
4434 buffer_write(low_direction);
4435 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4436 "low_direction: 0x%2.2x",
4437 trst, srst, low_output, low_direction);
4441 static void signalyzer_h_blink(void)
4443 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4446 /********************************************************************
4447 * Support for KT-LINK
4448 * JTAG adapter from KRISTECH
4449 * http://www.kristech.eu
4450 *******************************************************************/
4451 static int ktlink_init(void)
4453 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4455 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4456 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4458 /* initialize low byte for jtag */
4459 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4461 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4462 return ERROR_JTAG_INIT_FAILED;
4465 nTRST = 0x01;
4466 nSRST = 0x02;
4467 nTRSTnOE = 0x04;
4468 nSRSTnOE = 0x08;
4470 high_output = 0x80; // turn LED on
4471 high_direction = 0xFF; // all outputs
4473 enum reset_types jtag_reset_config = jtag_get_reset_config();
4475 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4476 high_output |= nTRSTnOE;
4477 high_output &= ~nTRST;
4478 } else {
4479 high_output &= ~nTRSTnOE;
4480 high_output |= nTRST;
4483 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4484 high_output &= ~nSRSTnOE;
4485 high_output |= nSRST;
4486 } else {
4487 high_output |= nSRSTnOE;
4488 high_output &= ~nSRST;
4491 /* initialize high byte for jtag */
4492 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4494 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4495 return ERROR_JTAG_INIT_FAILED;
4498 return ERROR_OK;
4501 static void ktlink_reset(int trst, int srst)
4503 enum reset_types jtag_reset_config = jtag_get_reset_config();
4505 if (trst == 1) {
4506 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4507 high_output &= ~nTRSTnOE;
4508 else
4509 high_output &= ~nTRST;
4510 } else if (trst == 0) {
4511 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4512 high_output |= nTRSTnOE;
4513 else
4514 high_output |= nTRST;
4517 if (srst == 1) {
4518 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4519 high_output &= ~nSRST;
4520 else
4521 high_output &= ~nSRSTnOE;
4522 } else if (srst == 0) {
4523 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4524 high_output |= nSRST;
4525 else
4526 high_output |= nSRSTnOE;
4529 buffer_write(0x82); // command "set data bits high byte"
4530 buffer_write(high_output);
4531 buffer_write(high_direction);
4532 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4535 static void ktlink_blink(void)
4537 /* LED connected to ACBUS7 */
4538 high_output ^= 0x80;
4540 buffer_write(0x82); // command "set data bits high byte"
4541 buffer_write(high_output);
4542 buffer_write(high_direction);
4545 static const struct command_registration ft2232_command_handlers[] = {
4547 .name = "ft2232_device_desc",
4548 .handler = &ft2232_handle_device_desc_command,
4549 .mode = COMMAND_CONFIG,
4550 .help = "set the USB device description of the FTDI FT2232 device",
4551 .usage = "description_string",
4554 .name = "ft2232_serial",
4555 .handler = &ft2232_handle_serial_command,
4556 .mode = COMMAND_CONFIG,
4557 .help = "set the serial number of the FTDI FT2232 device",
4558 .usage = "serial_string",
4561 .name = "ft2232_layout",
4562 .handler = &ft2232_handle_layout_command,
4563 .mode = COMMAND_CONFIG,
4564 .help = "set the layout of the FT2232 GPIO signals used "
4565 "to control output-enables and reset signals",
4566 .usage = "layout_name",
4569 .name = "ft2232_vid_pid",
4570 .handler = &ft2232_handle_vid_pid_command,
4571 .mode = COMMAND_CONFIG,
4572 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4573 .usage = "(vid pid)* ",
4576 .name = "ft2232_latency",
4577 .handler = &ft2232_handle_latency_command,
4578 .mode = COMMAND_CONFIG,
4579 .help = "set the FT2232 latency timer to a new value",
4580 .usage = "value",
4582 COMMAND_REGISTRATION_DONE
4585 struct jtag_interface ft2232_interface = {
4586 .name = "ft2232",
4587 .supported = DEBUG_CAP_TMS_SEQ,
4588 .commands = ft2232_command_handlers,
4589 .transports = jtag_only,
4591 .init = ft2232_init,
4592 .quit = ft2232_quit,
4593 .speed = ft2232_speed,
4594 .speed_div = ft2232_speed_div,
4595 .khz = ft2232_khz,
4596 .execute_queue = ft2232_execute_queue,