Added ft2232_transfer() and ft2232_bitbang() implementation. New interface routines...
[openocd/libswd.git] / src / jtag / drivers / ft2232.c
blob864cc9e255380c78b457b8b8b4a424a2e47a6a41
1 /***************************************************************************
2 * Copyright (C) 2009 by Oyvind Harboe *
3 * Oyvind Harboe <oyvind.harboe@zylin.com> *
4 * *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
7 * *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2011-2012 Tomasz Boleslaw CEDRO *
15 * cederom@tlen.pl, http://www.tomek.cedro.info *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
33 /**
34 * @file
35 * JTAG adapters based on the FT2232 full and high speed USB parts are
36 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
37 * are discrete, but development boards may integrate them as alternatives
38 * to more capable (and expensive) third party JTAG pods.
40 * JTAG uses only one of the two communications channels ("MPSSE engines")
41 * on these devices. Adapters based on FT4232 parts have four ports/channels
42 * (A/B/C/D), instead of just two (A/B).
44 * Especially on development boards integrating one of these chips (as
45 * opposed to discrete pods/dongles), the additional channels can be used
46 * for a variety of purposes, but OpenOCD only uses one channel at a time.
48 * - As a USB-to-serial adapter for the target's console UART ...
49 * which may be able to support ROM boot loaders that load initial
50 * firmware images to flash (or SRAM).
52 * - On systems which support ARM's SWD in addition to JTAG, or instead
53 * of it, that second port can be used for reading SWV/SWO trace data.
55 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
57 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
58 * request/response interactions involve round trips over the USB link.
59 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
60 * can for example poll quickly for a status change (usually taking on the
61 * order of microseconds not milliseconds) before beginning a queued
62 * transaction which require the previous one to have completed.
64 * There are dozens of adapters of this type, differing in details which
65 * this driver needs to understand. Those "layout" details are required
66 * as part of FT2232 driver configuration.
68 * This code uses information contained in the MPSSE specification which was
69 * found here:
70 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
71 * Hereafter this is called the "MPSSE Spec".
73 * The datasheet for the ftdichip.com's FT2232D part is here:
74 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
76 * Also note the issue with code 0x4b (clock data to TMS) noted in
77 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
78 * which can affect longer JTAG state paths.
81 #ifdef HAVE_CONFIG_H
82 #include "config.h"
83 #endif
85 /* project specific includes */
86 #include <jtag/interface.h>
87 #include <transport/transport.h>
88 #include <helper/time_support.h>
89 #include <interface/interface.h>
91 #if IS_CYGWIN == 1
92 #include <windows.h>
93 #endif
95 #include <assert.h>
97 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
98 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
99 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
100 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
101 #endif
103 /* FT2232 access library includes */
104 #if BUILD_FT2232_FTD2XX == 1
105 #include <ftd2xx.h>
106 #include "ftd2xx_common.h"
108 enum ftdi_interface {
109 INTERFACE_ANY = 0,
110 INTERFACE_A = 1,
111 INTERFACE_B = 2,
112 INTERFACE_C = 3,
113 INTERFACE_D = 4
116 #elif BUILD_FT2232_LIBFTDI == 1
117 #include <ftdi.h>
118 #endif
120 /* max TCK for the high speed devices 30000 kHz */
121 #define FTDI_x232H_MAX_TCK 30000
122 /* max TCK for the full speed devices 6000 kHz */
123 #define FTDI_2232C_MAX_TCK 6000
124 /* this speed value tells that RTCK is requested */
125 #define RTCK_SPEED -1
128 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
129 * errors with a retry count of 100. Increasing it solves the problem for me.
130 * - Dimitar
132 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
133 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
134 * to something sane.
136 #define LIBFTDI_READ_RETRY_COUNT 2000
138 #ifndef BUILD_FT2232_HIGHSPEED
139 #if BUILD_FT2232_FTD2XX == 1
140 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H, FT_DEVICE_232H };
141 #elif BUILD_FT2232_LIBFTDI == 1
142 enum ftdi_chip_type { TYPE_2232H = 4, TYPE_4232H = 5, TYPE_232H = 6 };
143 #endif
144 #endif
147 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
148 * stable state. Calling code must ensure that current state is stable,
149 * that verification is not done in here.
151 * @param num_cycles The number of clocks cycles to send.
152 * @param cmd The command to send.
154 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
156 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
158 static char *ft2232_device_desc_A;
159 static char *ft2232_device_desc;
160 static char *ft2232_serial;
161 static uint8_t ft2232_latency = 2;
162 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
164 #define MAX_USB_IDS 8
165 /* vid = pid = 0 marks the end of the list */
166 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
167 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
169 /** This structure describes different layout of FT2232 based devices. */
170 struct ft2232_layout {
171 /** Layout name. */
172 char *name;
173 /** Layout specific initialization routine. */
174 int (*init)(void);
175 /** Layout specific reset routine. */
176 void (*reset)(int trst, int srst);
177 /** Layout specific LED blink routine. */
178 void (*blink)(void);
179 /** Which FTDI channel does this layout use. */
180 int channel;
181 /** This will forbid bitbanging selected port pins. */
182 int bitbang_deny;
185 /* init procedures for supported layouts */
186 static int usbjtag_init(void);
187 static int jtagkey_init(void);
188 static int lm3s811_jtag_init(void);
189 static int icdi_jtag_init(void);
190 static int olimex_jtag_init(void);
191 static int flyswatter1_init(void);
192 static int flyswatter2_init(void);
193 static int minimodule_init(void);
194 static int turtle_init(void);
195 static int comstick_init(void);
196 static int stm32stick_init(void);
197 static int axm0432_jtag_init(void);
198 static int sheevaplug_init(void);
199 static int icebear_jtag_init(void);
200 static int cortino_jtag_init(void);
201 static int signalyzer_init(void);
202 static int signalyzer_h_init(void);
203 static int ktlink_init(void);
204 static int redbee_init(void);
205 static int lisa_l_init(void);
206 static int flossjtag_init(void);
207 static int xds100v2_init(void);
208 static int digilent_hs1_init(void);
210 /* reset procedures for supported layouts */
211 static void ftx23_reset(int trst, int srst);
212 static void jtagkey_reset(int trst, int srst);
213 static void olimex_jtag_reset(int trst, int srst);
214 static void flyswatter1_reset(int trst, int srst);
215 static void flyswatter2_reset(int trst, int srst);
216 static void minimodule_reset(int trst, int srst);
217 static void turtle_reset(int trst, int srst);
218 static void comstick_reset(int trst, int srst);
219 static void stm32stick_reset(int trst, int srst);
220 static void axm0432_jtag_reset(int trst, int srst);
221 static void sheevaplug_reset(int trst, int srst);
222 static void icebear_jtag_reset(int trst, int srst);
223 static void signalyzer_h_reset(int trst, int srst);
224 static void ktlink_reset(int trst, int srst);
225 static void redbee_reset(int trst, int srst);
226 static void xds100v2_reset(int trst, int srst);
227 static void digilent_hs1_reset(int trst, int srst);
229 /* blink procedures for layouts that support a blinking led */
230 static void olimex_jtag_blink(void);
231 static void flyswatter1_jtag_blink(void);
232 static void flyswatter2_jtag_blink(void);
233 static void turtle_jtag_blink(void);
234 static void signalyzer_h_blink(void);
235 static void ktlink_blink(void);
236 static void lisa_l_blink(void);
237 static void flossjtag_blink(void);
239 /* common transport support options */
241 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
243 static const struct ft2232_layout ft2232_layouts[] = {
244 { .name = "usbjtag",
245 .init = usbjtag_init,
246 .reset = ftx23_reset,
248 { .name = "jtagkey",
249 .init = jtagkey_init,
250 .reset = jtagkey_reset,
252 { .name = "jtagkey_prototype_v1",
253 .init = jtagkey_init,
254 .reset = jtagkey_reset,
256 { .name = "oocdlink",
257 .init = jtagkey_init,
258 .reset = jtagkey_reset,
260 { .name = "signalyzer",
261 .init = signalyzer_init,
262 .reset = ftx23_reset,
264 { .name = "evb_lm3s811",
265 .init = lm3s811_jtag_init,
266 .reset = ftx23_reset,
268 { .name = "luminary_icdi",
269 .init = icdi_jtag_init,
270 .reset = ftx23_reset,
272 { .name = "olimex-jtag",
273 .init = olimex_jtag_init,
274 .reset = olimex_jtag_reset,
275 .blink = olimex_jtag_blink
277 { .name = "flyswatter",
278 .init = flyswatter1_init,
279 .reset = flyswatter1_reset,
280 .blink = flyswatter1_jtag_blink
282 { .name = "flyswatter2",
283 .init = flyswatter2_init,
284 .reset = flyswatter2_reset,
285 .blink = flyswatter2_jtag_blink
287 { .name = "minimodule",
288 .init = minimodule_init,
289 .reset = minimodule_reset,
291 { .name = "turtelizer2",
292 .init = turtle_init,
293 .reset = turtle_reset,
294 .blink = turtle_jtag_blink
296 { .name = "comstick",
297 .init = comstick_init,
298 .reset = comstick_reset,
300 { .name = "stm32stick",
301 .init = stm32stick_init,
302 .reset = stm32stick_reset,
304 { .name = "axm0432_jtag",
305 .init = axm0432_jtag_init,
306 .reset = axm0432_jtag_reset,
308 { .name = "sheevaplug",
309 .init = sheevaplug_init,
310 .reset = sheevaplug_reset,
312 { .name = "icebear",
313 .init = icebear_jtag_init,
314 .reset = icebear_jtag_reset,
316 { .name = "cortino",
317 .init = cortino_jtag_init,
318 .reset = comstick_reset,
320 { .name = "signalyzer-h",
321 .init = signalyzer_h_init,
322 .reset = signalyzer_h_reset,
323 .blink = signalyzer_h_blink
325 { .name = "ktlink",
326 .init = ktlink_init,
327 .reset = ktlink_reset,
328 .blink = ktlink_blink
330 { .name = "redbee-econotag",
331 .init = redbee_init,
332 .reset = redbee_reset,
334 { .name = "redbee-usb",
335 .init = redbee_init,
336 .reset = redbee_reset,
337 .channel = INTERFACE_B,
339 { .name = "lisa-l",
340 .init = lisa_l_init,
341 .reset = ftx23_reset,
342 .blink = lisa_l_blink,
343 .channel = INTERFACE_B,
345 { .name = "flossjtag",
346 .init = flossjtag_init,
347 .reset = ftx23_reset,
348 .blink = flossjtag_blink,
350 { .name = "xds100v2",
351 .init = xds100v2_init,
352 .reset = xds100v2_reset,
354 { .name = "digilent-hs1",
355 .init = digilent_hs1_init,
356 .reset = digilent_hs1_reset,
357 .channel = INTERFACE_A,
359 { .name = NULL, /* END OF TABLE */ },
362 /* bitmask used to drive nTRST; usually a GPIOLx signal */
363 static uint8_t nTRST;
364 static uint8_t nTRSTnOE;
365 /* bitmask used to drive nSRST; usually a GPIOLx signal */
366 static uint8_t nSRST;
367 static uint8_t nSRSTnOE;
369 /** the layout being used with this debug session */
370 static const struct ft2232_layout *layout;
372 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
373 static uint8_t low_output;
375 /* note that direction bit == 1 means that signal is an output */
377 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
378 static uint8_t low_direction;
379 /** default value bitmask for CBUS GPIOH(0..4) */
380 static uint8_t high_output;
381 /** default direction bitmask for CBUS GPIOH(0..4) */
382 static uint8_t high_direction;
384 #if BUILD_FT2232_FTD2XX == 1
385 static FT_HANDLE ftdih;
386 static FT_DEVICE ftdi_device;
387 #elif BUILD_FT2232_LIBFTDI == 1
388 static struct ftdi_context ftdic;
389 static enum ftdi_chip_type ftdi_device;
390 #endif
392 static struct jtag_command *first_unsent; /* next command that has to be sent */
393 static int require_send;
395 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
397 "There is a significant difference between libftdi and libftd2xx. The latter
398 one allows to schedule up to 64*64 bytes of result data while libftdi fails
399 with more than 4*64. As a consequence, the FT2232 driver is forced to
400 perform around 16x more USB transactions for long command streams with TDO
401 capture when running with libftdi."
403 No idea how we get
404 #define FT2232_BUFFER_SIZE 131072
405 a comment would have been nice.
408 #if BUILD_FT2232_FTD2XX == 1
409 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
410 #else
411 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
412 #endif
414 #define FT2232_BUFFER_SIZE 131072
416 static uint8_t *ft2232_buffer;
417 static int ft2232_buffer_size;
418 static int ft2232_read_pointer;
419 static int ft2232_expect_read;
422 * Function buffer_write
423 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
424 * @param val is the byte to send.
426 static inline void buffer_write(uint8_t val)
428 assert(ft2232_buffer);
429 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
430 ft2232_buffer[ft2232_buffer_size++] = val;
434 * Function buffer_read
435 * returns a byte from the byte buffer.
437 static inline uint8_t buffer_read(void)
439 assert(ft2232_buffer);
440 assert(ft2232_read_pointer < ft2232_buffer_size);
441 return ft2232_buffer[ft2232_read_pointer++];
445 * Clocks out \a bit_count bits on the TMS line, starting with the least
446 * significant bit of tms_bits and progressing to more significant bits.
447 * Rigorous state transition logging is done here via tap_set_state().
449 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
450 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
451 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
452 * is often used for this, 0x4b.
454 * @param tms_bits Holds the sequence of bits to send.
455 * @param tms_count Tells how many bits in the sequence.
456 * @param tdi_bit A single bit to pass on to TDI before the first TCK
457 * cycle and held static for the duration of TMS clocking.
459 * See the MPSSE spec referenced above.
461 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
463 uint8_t tms_byte;
464 int i;
465 int tms_ndx; /* bit index into tms_byte */
467 assert(tms_count > 0);
469 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
470 mpsse_cmd, tms_bits, tms_count);
472 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
473 bool bit = tms_bits & 1;
475 if (bit)
476 tms_byte |= (1 << tms_ndx);
478 /* always do state transitions in public view */
479 tap_set_state(tap_state_transition(tap_get_state(), bit));
481 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
482 * also increment.
484 ++tms_ndx;
486 if (tms_ndx == 7 || i == tms_count-1) {
487 buffer_write(mpsse_cmd);
488 buffer_write(tms_ndx - 1);
490 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
491 * TMS/CS and is held static for the duration of TMS/CS clocking.
493 buffer_write(tms_byte | (tdi_bit << 7));
499 * Function get_tms_buffer_requirements
500 * returns what clock_tms() will consume if called with
501 * same \a bit_count.
503 static inline int get_tms_buffer_requirements(int bit_count)
505 return ((bit_count + 6)/7) * 3;
509 * Function move_to_state
510 * moves the TAP controller from the current state to a
511 * \a goal_state through a path given by tap_get_tms_path(). State transition
512 * logging is performed by delegation to clock_tms().
514 * @param goal_state is the destination state for the move.
516 static void move_to_state(tap_state_t goal_state)
518 tap_state_t start_state = tap_get_state();
520 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
521 * lookup of the required TMS pattern to move to this state from the start state.
524 /* do the 2 lookups */
525 int tms_bits = tap_get_tms_path(start_state, goal_state);
526 int tms_count = tap_get_tms_path_len(start_state, goal_state);
528 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
530 clock_tms(0x4b, tms_bits, tms_count, 0);
533 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
535 #if BUILD_FT2232_FTD2XX == 1
536 FT_STATUS status;
537 DWORD dw_bytes_written = 0;
538 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
539 if (status != FT_OK) {
540 *bytes_written = dw_bytes_written;
541 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
542 return ERROR_JTAG_DEVICE_ERROR;
543 } else
544 *bytes_written = dw_bytes_written;
546 #elif BUILD_FT2232_LIBFTDI == 1
547 int retval = ftdi_write_data(&ftdic, buf, size);
548 if (retval < 0) {
549 *bytes_written = 0;
550 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
551 return ERROR_JTAG_DEVICE_ERROR;
552 } else
553 *bytes_written = retval;
555 #endif
557 if (*bytes_written != (uint32_t)size)
558 return ERROR_JTAG_DEVICE_ERROR;
560 return ERROR_OK;
563 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
565 #if BUILD_FT2232_FTD2XX == 1
566 DWORD dw_bytes_read;
567 FT_STATUS status;
568 int timeout = 5;
569 *bytes_read = 0;
571 while ((*bytes_read < size) && timeout--) {
572 status = FT_Read(ftdih, buf + *bytes_read, size -
573 *bytes_read, &dw_bytes_read);
574 if (status != FT_OK) {
575 *bytes_read = 0;
576 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
577 return ERROR_JTAG_DEVICE_ERROR;
579 *bytes_read += dw_bytes_read;
582 #elif BUILD_FT2232_LIBFTDI == 1
583 int retval;
584 int timeout = LIBFTDI_READ_RETRY_COUNT;
585 *bytes_read = 0;
587 while ((*bytes_read < size) && timeout--) {
588 retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
589 if (retval < 0) {
590 *bytes_read = 0;
591 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
592 return ERROR_JTAG_DEVICE_ERROR;
594 *bytes_read += retval;
597 #endif
599 if (*bytes_read < size) {
600 LOG_ERROR("couldn't read enough bytes from "
601 "FT2232 device (%i < %i)",
602 (unsigned)*bytes_read,
603 (unsigned)size);
604 return ERROR_JTAG_DEVICE_ERROR;
607 return ERROR_OK;
610 static bool ft2232_device_is_highspeed(void)
612 #if BUILD_FT2232_FTD2XX == 1
613 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H)
614 #ifdef HAS_ENUM_FT232H
615 || (ftdi_device == FT_DEVICE_232H)
616 #endif
618 #elif BUILD_FT2232_LIBFTDI == 1
619 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H
620 #ifdef HAS_ENUM_FT232H
621 || ftdi_device == TYPE_232H
622 #endif
624 #endif
628 /** Generic IO BITBANG Port Manipulation Routine.
629 * It can read and write port state using signal names. Each interface have its
630 * own specific signal names and fields. This function works on those fields
631 * and based on their values talks to the FT*232 chip on the interface device.
632 * ft2232 drivers use {low,high}_{output,direction} global variables to remember
633 * port direction and value, so we need to work on them as well not to change
634 * any other pin with our bit-baning performed only on selected pins.
635 * The function name 'bitbang' reflects ability to change selected pin states.
637 * @Note: FT2232 has special mechanism called MPSSE for serial communications
638 * that is far more efficient than pure 'bitbang' mode on this device family.
639 * Although our function is named 'bitbang' it does not use bitbang mode.
640 * MPSSE command send value and port bytes on port write, but does not on read.
641 * This happens every time we want to change pin value, so we need to use cache.
642 * On write we want to OR direction mask already set by init() procedure
643 * to mark bit-mask output. On read we want to clear bits given by mask
644 * to mark them input. To read we need to write/update port state first.
645 * Long story short: to read data we first need to set pins to input.
647 * @Warning: reading and writing will set pin direction input or output,
648 * so it is possible to disable basic data output pins with bad masking,
649 * but also gives chance to create and manage full TCL signal description,
650 * that can be used to take advantage of some additional interface hardware
651 * features installed on some devices (i.e. ADC, power supply, etc).
652 * This gives new way of signal handling that is still backward-compatible.
654 * \param *device void pointer to pass additional driver information to the routine.
655 * \param signal is the string representation of the signal mask stored in layout structure.
656 * \param GETnSET if zero then perform read operation, write otherwise.
657 * \param *value is the pointer that holds the value.
658 * \return ERROR_OK on success, or ERROR_FAIL on failure.
660 int ft2232_bitbang(void *device, char *signal_name, int GETnSET, int *value)
662 uint8_t buf[3];
663 int retval, vall = 0, valh = 0;
664 unsigned int sigmask;
665 uint32_t bytes_written, bytes_read;
666 oocd_interface_signal_t *sig = oocd_interface_signal_find(signal_name);
668 /* First get the signal mask, or return error if signal not defined. */
669 if (!sig) {
670 LOG_ERROR("Requested signal not found on this interface!");
671 return ERROR_FAIL;
673 /* Pin mask is also related with port direction and complicates it!!! */
674 sigmask = sig->mask;
676 /* First check against restricted port pins defined by the interface layout */
677 if (sigmask & layout->bitbang_deny) {
678 LOG_ERROR("This interface does not allow to bit-bang selected pins (0x%08X)!", layout->bitbang_deny);
679 return ERROR_FAIL;
682 if (!GETnSET) {
683 /* We will SET port pins selected by sigmask. */
684 /* Modify our pins value, but remember about other pins and their previous value */
685 low_output = (low_output & ~sigmask) | ((*value & sigmask) & 0x0ff);
686 high_output = (high_output & ~(sigmask >> 8)) | (((*value & sigmask) >> 8) & 0x0ff);
687 /* Modify our pins direction, but remember about other pins and their previous direction */
688 low_direction |= sigmask & 0x0ff;
689 high_direction |= (sigmask >> 8) & 0x0ff;
690 /* Now send those settings to the interface chip */
691 buf[0] = 0x80; /* Set Data Bits LowByte */
692 buf[1] = low_output;
693 buf[2] = low_direction;
694 retval = ft2232_write(buf, 3, &bytes_written);
695 if (retval != ERROR_OK)
696 return retval;
697 buf[0] = 0x82; /* Set Data Bits HighByte */
698 buf[1] = high_output;
699 buf[2] = high_direction;
700 retval = ft2232_write(buf, 3, &bytes_written);
701 if (retval != ERROR_OK)
702 return retval;
703 sig->value = ((high_output << 8) | low_output) & sig->mask;
704 } else {
705 /* Modify our pins value, but remember about other pins and their previous value */
706 /* DO WE REALLY NEED TO PULL-UP PINS TO READ THEIR STATE OR SIMPLY LEAVE AS IS? */
707 /* low_output = (low_output & ~sigmask) | (sigmask & 0x0ff); */
708 /* high_output = (high_output & ~sigmask) | (sigmask>>8) & 0x0ff); */
709 /* Modify our pins direction to input, but remember about other pins and their previous direction */
710 low_direction &= ~(sigmask);
711 high_direction &= ~(sigmask >> 8);
712 /* Now send those settings to the interface chip */
713 /* First change desired pins to input */
714 buf[0] = 0x80; /* Set Data Bits LowByte */
715 buf[1] = low_output;
716 buf[2] = low_direction;
717 retval = ft2232_write(buf, 3, &bytes_written);
718 if (retval != ERROR_OK)
719 return retval;
720 buf[0] = 0x82; /* Set Data Bits HighByte */
721 buf[1] = high_output;
722 buf[2] = high_direction;
723 retval = ft2232_write(buf, 3, &bytes_written);
724 if (retval != ERROR_OK)
725 return retval;
726 /* Then read pins designated by a signal mask */
727 buf[0] = 0x81; /* Read Data Bits LowByte. */
728 retval = ft2232_write(buf, 1, &bytes_written);
729 if (retval != ERROR_OK)
730 return retval;
731 retval = ft2232_read((uint8_t *)&vall, 1, &bytes_read);
732 if (retval != ERROR_OK)
733 return retval;
734 buf[0] = 0x83; /* Read Data Bits HighByte. */
735 retval = ft2232_write(buf, 1, &bytes_written);
736 if (retval != ERROR_OK)
737 return retval;
738 retval = ft2232_read((uint8_t *)&valh, 1, &bytes_read);
739 if (retval != ERROR_OK)
740 return retval;
741 sig->value = *value = ((valh << 8) | vall) & sig->mask; /* Join result bytes and apply signal bitmask */
743 return ERROR_OK;
747 /** Transfer bits in/out stored in char array starting from LSB first or MSB first,
748 * alternatively if you want to make MSB-first shift on LSB-first mode put data
749 * in reverse order into input/output array.
750 * \param *device void pointer to pass driver details to the function.
751 * \param bits is the number of bits (char array elements) to transfer.
752 * \param *mosidata pointer to char array with data to be send.
753 * \param *misodata pointer to char array with data to be received.
754 * \param nLSBfirst if zero shift data LSB-first, otherwise MSB-first.
755 * \return number of bits sent on success, or ERROR_FAIL on failure.
757 int ft2232_transfer(void *device, int bits, char *mosidata, char *misodata, int nLSBfirst)
759 static uint8_t buf[65539], databuf;
760 int i, retval, bit = 0, byte = 0, bytes = 0;
761 uint32_t bytes_written, bytes_read;
763 LOG_DEBUG("ft2232_transfer(device=@%p, bits=%d, mosidata=@%p, misodata=@%p, nLSDfirst=%d) ",\
764 (void *)device, bits, (void *)mosidata, (void *)misodata, nLSBfirst);
766 if (bits > 65535) {
767 LOG_ERROR("Cannot transfer more than 65536 bits at once!");
768 return ERROR_FAIL;
771 if (bits >= 8) {
772 /* Try to pack as many bits into bytes for better performance. */
773 bytes = bits / 8;
774 bytes--; /* MPSSE starts counting bytes from 0. */
775 buf[0] = (nLSBfirst) ? 0x31 : 0x39; /* Clock Bytes In and Out LSb or MSb first. */
776 buf[1] = (char)bytes & 0x0ff;
777 buf[2] = (char)((bytes >> 8) & 0x0ff);
778 bytes++;
779 for (byte = 0; byte * 8 < bits; byte++) {
780 databuf = 0;
781 for (i = 0; i < 8; i++)
782 databuf |= mosidata[byte * 8 + i] ? (1 << i) : 0;
783 buf[byte + 3] = databuf;
785 retval = ft2232_write(buf, bytes + 3, &bytes_written);
786 if (retval < 0) {
787 LOG_ERROR("ft2232_write() returns %d", retval);
788 return ERROR_FAIL;
790 retval = ft2232_read((uint8_t *)buf, bytes, &bytes_read);
791 if (retval < 0) {
792 LOG_ERROR("ft2232_read() returns %d", retval);
793 return ERROR_FAIL;
795 /* Explode read bytes into bit array. */
796 for (byte = 0; byte * 8 < bits; byte++)
797 for (bit = 0; bit < 8; bit++)
798 misodata[byte * 8 + bit] = buf[byte] & (1 << bit) ? 1 : 0;
801 /* Now send remaining bits that cannot be packed as bytes. */
802 /* Because "Clock Data Bits In and Out LSB/MSB" of FTDI is a mess, pack single */
803 /* bit read/writes into buffer and then flush it using single USB transfer. */
804 for (bit = bytes * 8; bit < bits; bit++) {
805 buf[3 * bit + 0] = (nLSBfirst) ? 0x33 : 0x3b; /* Clock Bits In and Out LSb or MSb first. */
806 buf[3 * bit + 1] = 0; /* One bit per element. */
807 buf[3 * bit + 2] = mosidata[bit] ? 0xff : 0; /* Take data from supplied array. */
809 retval = ft2232_write(buf, 3 * (bits - (bytes * 8)), &bytes_written);
810 if (retval < 0) {
811 LOG_ERROR("ft2232_write() returns %d", retval);
812 return ERROR_FAIL;
814 retval = ft2232_read((uint8_t *)misodata, bits - (bytes * 8), &bytes_read);
815 if (retval < 0) {
816 LOG_ERROR("ft2232_read() returns %d", retval);
817 return ERROR_FAIL;
819 /* FTDI MPSSE returns shift register value, our bit is MSb */
820 for (bit = bytes * 8; bit < bits; bit++)
821 misodata[bit] = (misodata[bit] & (nLSBfirst ? 0x01 : 0x80)) ? 1 : 0;
822 /* USE THIS FOR WIRE-LEVEL DEBUG */
823 /* LOG_DEBUG("read 0x%02X written 0x%02X", misodata[bit], mosidata[bit]); */
825 return bit;
830 * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
831 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
832 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
835 static int ftx232h_adaptive_clocking(bool enable)
837 uint8_t buf = enable ? 0x96 : 0x97;
838 LOG_DEBUG("%2.2x", buf);
840 uint32_t bytes_written;
841 int retval;
843 retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
844 if (retval != ERROR_OK) {
845 LOG_ERROR("couldn't write command to %s adaptive clocking"
846 , enable ? "enable" : "disable");
847 return retval;
850 return ERROR_OK;
854 * Enable/disable the clk divide by 5 of the 60MHz master clock.
855 * This result in a JTAG clock speed range of 91.553Hz-6MHz
856 * respective 457.763Hz-30MHz.
858 static int ftx232h_clk_divide_by_5(bool enable)
860 uint32_t bytes_written;
861 uint8_t buf = enable ? 0x8b : 0x8a;
863 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
864 LOG_ERROR("couldn't write command to %s clk divide by 5"
865 , enable ? "enable" : "disable");
866 return ERROR_JTAG_INIT_FAILED;
868 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_x232H_MAX_TCK;
869 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
871 return ERROR_OK;
874 static int ft2232_speed(int speed)
876 uint8_t buf[3];
877 int retval;
878 uint32_t bytes_written;
880 retval = ERROR_OK;
881 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
882 if (ft2232_device_is_highspeed())
883 retval = ftx232h_adaptive_clocking(enable_adaptive_clocking);
884 else if (enable_adaptive_clocking) {
885 LOG_ERROR("ft2232 device %lu does not support RTCK"
886 , (long unsigned int)ftdi_device);
887 return ERROR_FAIL;
890 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
891 return retval;
893 buf[0] = 0x86; /* command "set divisor" */
894 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
895 buf[2] = (speed >> 8) & 0xff; /* valueH */
897 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
898 retval = ft2232_write(buf, sizeof(buf), &bytes_written);
899 if (retval != ERROR_OK) {
900 LOG_ERROR("couldn't set FT2232 TCK speed");
901 return retval;
904 return ERROR_OK;
907 static int ft2232_speed_div(int speed, int *khz)
909 /* Take a look in the FT2232 manual,
910 * AN2232C-01 Command Processor for
911 * MPSSE and MCU Host Bus. Chapter 3.8 */
913 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
915 return ERROR_OK;
918 static int ft2232_khz(int khz, int *jtag_speed)
920 if (khz == 0) {
921 if (ft2232_device_is_highspeed()) {
922 *jtag_speed = RTCK_SPEED;
923 return ERROR_OK;
924 } else {
925 LOG_DEBUG("RCLK not supported");
926 return ERROR_FAIL;
930 /* Take a look in the FT2232 manual,
931 * AN2232C-01 Command Processor for
932 * MPSSE and MCU Host Bus. Chapter 3.8
934 * We will calc here with a multiplier
935 * of 10 for better rounding later. */
937 /* Calc speed, (ft2232_max_tck / khz) - 1
938 * Use 65000 for better rounding */
939 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
941 /* Add 0.9 for rounding */
942 *jtag_speed += 9;
944 /* Calc real speed */
945 *jtag_speed = *jtag_speed / 10;
947 /* Check if speed is greater than 0 */
948 if (*jtag_speed < 0)
949 *jtag_speed = 0;
951 /* Check max value */
952 if (*jtag_speed > 0xFFFF)
953 *jtag_speed = 0xFFFF;
955 return ERROR_OK;
958 static void ft2232_end_state(tap_state_t state)
960 if (tap_is_state_stable(state))
961 tap_set_end_state(state);
962 else {
963 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
964 exit(-1);
968 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
970 int num_bytes = (scan_size + 7) / 8;
971 int bits_left = scan_size;
972 int cur_byte = 0;
974 while (num_bytes-- > 1) {
975 buffer[cur_byte++] = buffer_read();
976 bits_left -= 8;
979 buffer[cur_byte] = 0x0;
981 /* There is one more partial byte left from the clock data in/out instructions */
982 if (bits_left > 1)
983 buffer[cur_byte] = buffer_read() >> 1;
984 /* This shift depends on the length of the
985 *clock data to tms instruction, insterted
986 *at end of the scan, now fixed to a two
987 *step transition in ft2232_add_scan */
988 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
991 static void ft2232_debug_dump_buffer(void)
993 int i;
994 char line[256];
995 char *line_p = line;
997 for (i = 0; i < ft2232_buffer_size; i++) {
998 line_p += snprintf(line_p,
999 sizeof(line) - (line_p - line),
1000 "%2.2x ",
1001 ft2232_buffer[i]);
1002 if (i % 16 == 15) {
1003 LOG_DEBUG("%s", line);
1004 line_p = line;
1008 if (line_p != line)
1009 LOG_DEBUG("%s", line);
1012 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
1014 struct jtag_command *cmd;
1015 uint8_t *buffer;
1016 int scan_size;
1017 enum scan_type type;
1018 int retval;
1019 uint32_t bytes_written = 0;
1020 uint32_t bytes_read = 0;
1022 #ifdef _DEBUG_USB_IO_
1023 struct timeval start, inter, inter2, end;
1024 struct timeval d_inter, d_inter2, d_end;
1025 #endif
1027 #ifdef _DEBUG_USB_COMMS_
1028 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
1029 ft2232_debug_dump_buffer();
1030 #endif
1032 #ifdef _DEBUG_USB_IO_
1033 gettimeofday(&start, NULL);
1034 #endif
1036 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1037 if (retval != ERROR_OK) {
1038 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1039 return retval;
1042 #ifdef _DEBUG_USB_IO_
1043 gettimeofday(&inter, NULL);
1044 #endif
1046 if (ft2232_expect_read) {
1047 /* FIXME this "timeout" is never changed ... */
1048 int timeout = LIBFTDI_READ_RETRY_COUNT;
1049 ft2232_buffer_size = 0;
1051 #ifdef _DEBUG_USB_IO_
1052 gettimeofday(&inter2, NULL);
1053 #endif
1055 retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
1056 if (retval != ERROR_OK) {
1057 LOG_ERROR("couldn't read from FT2232");
1058 return retval;
1061 #ifdef _DEBUG_USB_IO_
1062 gettimeofday(&end, NULL);
1064 timeval_subtract(&d_inter, &inter, &start);
1065 timeval_subtract(&d_inter2, &inter2, &start);
1066 timeval_subtract(&d_end, &end, &start);
1068 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
1069 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
1070 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
1071 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
1072 #endif
1074 ft2232_buffer_size = bytes_read;
1076 if (ft2232_expect_read != ft2232_buffer_size) {
1077 LOG_ERROR("ft2232_expect_read (%i) != "
1078 "ft2232_buffer_size (%i) "
1079 "(%i retries)",
1080 ft2232_expect_read,
1081 ft2232_buffer_size,
1082 LIBFTDI_READ_RETRY_COUNT - timeout);
1083 ft2232_debug_dump_buffer();
1085 exit(-1);
1088 #ifdef _DEBUG_USB_COMMS_
1089 LOG_DEBUG("read buffer (%i retries): %i bytes",
1090 LIBFTDI_READ_RETRY_COUNT - timeout,
1091 ft2232_buffer_size);
1092 ft2232_debug_dump_buffer();
1093 #endif
1096 ft2232_expect_read = 0;
1097 ft2232_read_pointer = 0;
1099 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
1100 * that wasn't handled by a caller-provided error handler
1102 retval = ERROR_OK;
1104 cmd = first;
1105 while (cmd != last) {
1106 switch (cmd->type) {
1107 case JTAG_SCAN:
1108 type = jtag_scan_type(cmd->cmd.scan);
1109 if (type != SCAN_OUT) {
1110 scan_size = jtag_scan_size(cmd->cmd.scan);
1111 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
1112 ft2232_read_scan(type, buffer, scan_size);
1113 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
1114 retval = ERROR_JTAG_QUEUE_FAILED;
1115 free(buffer);
1117 break;
1119 default:
1120 break;
1123 cmd = cmd->next;
1126 ft2232_buffer_size = 0;
1128 return retval;
1132 * Function ft2232_add_pathmove
1133 * moves the TAP controller from the current state to a new state through the
1134 * given path, where path is an array of tap_state_t's.
1136 * @param path is an array of tap_stat_t which gives the states to traverse through
1137 * ending with the last state at path[num_states-1]
1138 * @param num_states is the count of state steps to move through
1140 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
1142 int state_count = 0;
1144 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
1146 DEBUG_JTAG_IO("-");
1148 /* this loop verifies that the path is legal and logs each state in the path */
1149 while (num_states) {
1150 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
1151 int bit_count = 0;
1152 int num_states_batch = num_states > 7 ? 7 : num_states;
1154 /* command "Clock Data to TMS/CS Pin (no Read)" */
1155 buffer_write(0x4b);
1157 /* number of states remaining */
1158 buffer_write(num_states_batch - 1);
1160 while (num_states_batch--) {
1161 /* either TMS=0 or TMS=1 must work ... */
1162 if (tap_state_transition(tap_get_state(), false) == path[state_count])
1163 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
1164 else if (tap_state_transition(tap_get_state(), true) == path[state_count])
1165 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
1167 /* ... or else the caller goofed BADLY */
1168 else {
1169 LOG_ERROR("BUG: %s -> %s isn't a valid "
1170 "TAP state transition",
1171 tap_state_name(tap_get_state()),
1172 tap_state_name(path[state_count]));
1173 exit(-1);
1176 tap_set_state(path[state_count]);
1177 state_count++;
1178 num_states--;
1181 buffer_write(tms_byte);
1183 tap_set_end_state(tap_get_state());
1186 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
1188 int num_bytes = (scan_size + 7) / 8;
1189 int bits_left = scan_size;
1190 int cur_byte = 0;
1191 int last_bit;
1193 if (!ir_scan) {
1194 if (tap_get_state() != TAP_DRSHIFT)
1195 move_to_state(TAP_DRSHIFT);
1196 } else {
1197 if (tap_get_state() != TAP_IRSHIFT)
1198 move_to_state(TAP_IRSHIFT);
1201 /* add command for complete bytes */
1202 while (num_bytes > 1) {
1203 int thisrun_bytes;
1204 if (type == SCAN_IO) {
1205 /* Clock Data Bytes In and Out LSB First */
1206 buffer_write(0x39);
1207 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1208 } else if (type == SCAN_OUT) {
1209 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1210 buffer_write(0x19);
1211 /* LOG_DEBUG("added TDI bytes (o)"); */
1212 } else if (type == SCAN_IN) {
1213 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1214 buffer_write(0x28);
1215 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1218 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1219 num_bytes -= thisrun_bytes;
1221 buffer_write((uint8_t) (thisrun_bytes - 1));
1222 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1224 if (type != SCAN_IN) {
1225 /* add complete bytes */
1226 while (thisrun_bytes-- > 0) {
1227 buffer_write(buffer[cur_byte++]);
1228 bits_left -= 8;
1230 } else /* (type == SCAN_IN) */
1231 bits_left -= 8 * (thisrun_bytes);
1234 /* the most signifcant bit is scanned during TAP movement */
1235 if (type != SCAN_IN)
1236 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1237 else
1238 last_bit = 0;
1240 /* process remaining bits but the last one */
1241 if (bits_left > 1) {
1242 if (type == SCAN_IO) {
1243 /* Clock Data Bits In and Out LSB First */
1244 buffer_write(0x3b);
1245 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1246 } else if (type == SCAN_OUT) {
1247 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1248 buffer_write(0x1b);
1249 /* LOG_DEBUG("added TDI bits (o)"); */
1250 } else if (type == SCAN_IN) {
1251 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1252 buffer_write(0x2a);
1253 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1256 buffer_write(bits_left - 2);
1257 if (type != SCAN_IN)
1258 buffer_write(buffer[cur_byte]);
1261 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1262 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1263 if (type == SCAN_IO) {
1264 /* Clock Data Bits In and Out LSB First */
1265 buffer_write(0x3b);
1266 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1267 } 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)"); */
1271 } else if (type == SCAN_IN) {
1272 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1273 buffer_write(0x2a);
1274 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1276 buffer_write(0x0);
1277 buffer_write(last_bit);
1278 } else {
1279 int tms_bits;
1280 int tms_count;
1281 uint8_t mpsse_cmd;
1283 /* move from Shift-IR/DR to end state */
1284 if (type != SCAN_OUT) {
1285 /* We always go to the PAUSE state in two step at the end of an IN or IO
1286 *scan
1287 * This must be coordinated with the bit shifts in ft2232_read_scan */
1288 tms_bits = 0x01;
1289 tms_count = 2;
1290 /* Clock Data to TMS/CS Pin with Read */
1291 mpsse_cmd = 0x6b;
1292 } else {
1293 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1294 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1295 /* Clock Data to TMS/CS Pin (no Read) */
1296 mpsse_cmd = 0x4b;
1299 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1300 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1303 if (tap_get_state() != tap_get_end_state())
1304 move_to_state(tap_get_end_state());
1307 static int ft2232_large_scan(struct scan_command *cmd,
1308 enum scan_type type,
1309 uint8_t *buffer,
1310 int scan_size)
1312 int num_bytes = (scan_size + 7) / 8;
1313 int bits_left = scan_size;
1314 int cur_byte = 0;
1315 int last_bit;
1316 uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1317 uint8_t *receive_pointer = receive_buffer;
1318 uint32_t bytes_written;
1319 uint32_t bytes_read;
1320 int retval;
1321 int thisrun_read = 0;
1323 if (cmd->ir_scan) {
1324 LOG_ERROR("BUG: large IR scans are not supported");
1325 exit(-1);
1328 if (tap_get_state() != TAP_DRSHIFT)
1329 move_to_state(TAP_DRSHIFT);
1331 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1332 if (retval != ERROR_OK) {
1333 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1334 exit(-1);
1336 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1337 ft2232_buffer_size, (int)bytes_written);
1338 ft2232_buffer_size = 0;
1340 /* add command for complete bytes */
1341 while (num_bytes > 1) {
1342 int thisrun_bytes;
1344 if (type == SCAN_IO) {
1345 /* Clock Data Bytes In and Out LSB First */
1346 buffer_write(0x39);
1347 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1348 } else if (type == SCAN_OUT) {
1349 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1350 buffer_write(0x19);
1351 /* LOG_DEBUG("added TDI bytes (o)"); */
1352 } else if (type == SCAN_IN) {
1353 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1354 buffer_write(0x28);
1355 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1358 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1359 thisrun_read = thisrun_bytes;
1360 num_bytes -= thisrun_bytes;
1361 buffer_write((uint8_t) (thisrun_bytes - 1));
1362 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1364 if (type != SCAN_IN) {
1365 /* add complete bytes */
1366 while (thisrun_bytes-- > 0) {
1367 buffer_write(buffer[cur_byte]);
1368 cur_byte++;
1369 bits_left -= 8;
1371 } else /* (type == SCAN_IN) */
1372 bits_left -= 8 * (thisrun_bytes);
1374 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1375 if (retval != ERROR_OK) {
1376 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1377 exit(-1);
1379 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1380 ft2232_buffer_size,
1381 (int)bytes_written);
1382 ft2232_buffer_size = 0;
1384 if (type != SCAN_OUT) {
1385 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1386 if (retval != ERROR_OK) {
1387 LOG_ERROR("couldn't read from FT2232");
1388 exit(-1);
1390 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1391 thisrun_read,
1392 (int)bytes_read);
1393 receive_pointer += bytes_read;
1397 thisrun_read = 0;
1399 /* the most signifcant bit is scanned during TAP movement */
1400 if (type != SCAN_IN)
1401 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1402 else
1403 last_bit = 0;
1405 /* process remaining bits but the last one */
1406 if (bits_left > 1) {
1407 if (type == SCAN_IO) {
1408 /* Clock Data Bits In and Out LSB First */
1409 buffer_write(0x3b);
1410 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1411 } else if (type == SCAN_OUT) {
1412 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1413 buffer_write(0x1b);
1414 /* LOG_DEBUG("added TDI bits (o)"); */
1415 } else if (type == SCAN_IN) {
1416 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1417 buffer_write(0x2a);
1418 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1420 buffer_write(bits_left - 2);
1421 if (type != SCAN_IN)
1422 buffer_write(buffer[cur_byte]);
1424 if (type != SCAN_OUT)
1425 thisrun_read += 2;
1428 if (tap_get_end_state() == TAP_DRSHIFT) {
1429 if (type == SCAN_IO) {
1430 /* Clock Data Bits In and Out LSB First */
1431 buffer_write(0x3b);
1432 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1433 } else if (type == SCAN_OUT) {
1434 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1435 buffer_write(0x1b);
1436 /* LOG_DEBUG("added TDI bits (o)"); */
1437 } else if (type == SCAN_IN) {
1438 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1439 buffer_write(0x2a);
1440 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1442 buffer_write(0x0);
1443 buffer_write(last_bit);
1444 } else {
1445 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1446 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1447 uint8_t mpsse_cmd;
1449 /* move from Shift-IR/DR to end state */
1450 if (type != SCAN_OUT) {
1451 /* Clock Data to TMS/CS Pin with Read */
1452 mpsse_cmd = 0x6b;
1453 /* LOG_DEBUG("added TMS scan (read)"); */
1454 } else {
1455 /* Clock Data to TMS/CS Pin (no Read) */
1456 mpsse_cmd = 0x4b;
1457 /* LOG_DEBUG("added TMS scan (no read)"); */
1460 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1461 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1464 if (type != SCAN_OUT)
1465 thisrun_read += 1;
1467 retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1468 if (retval != ERROR_OK) {
1469 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1470 exit(-1);
1472 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1473 ft2232_buffer_size,
1474 (int)bytes_written);
1475 ft2232_buffer_size = 0;
1477 if (type != SCAN_OUT) {
1478 retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1479 if (retval != ERROR_OK) {
1480 LOG_ERROR("couldn't read from FT2232");
1481 exit(-1);
1483 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1484 thisrun_read,
1485 (int)bytes_read);
1488 return ERROR_OK;
1491 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1493 int predicted_size = 3;
1494 int num_bytes = (scan_size - 1) / 8;
1496 if (tap_get_state() != TAP_DRSHIFT)
1497 predicted_size += get_tms_buffer_requirements(
1498 tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1500 if (type == SCAN_IN) { /* only from device to host */
1501 /* complete bytes */
1502 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1504 /* remaining bits - 1 (up to 7) */
1505 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1506 } else {/* host to device, or bidirectional
1507 * complete bytes */
1508 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1510 /* remaining bits -1 (up to 7) */
1511 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1514 return predicted_size;
1517 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1519 int predicted_size = 0;
1521 if (type != SCAN_OUT) {
1522 /* complete bytes */
1523 predicted_size +=
1524 (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1526 /* remaining bits - 1 */
1527 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1529 /* last bit (from TMS scan) */
1530 predicted_size += 1;
1533 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1535 return predicted_size;
1538 /* semi-generic FT2232/FT4232 reset code */
1539 static void ftx23_reset(int trst, int srst)
1541 enum reset_types jtag_reset_config = jtag_get_reset_config();
1542 if (trst == 1) {
1543 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1544 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1545 else
1546 low_output &= ~nTRST; /* switch output low */
1547 } else if (trst == 0) {
1548 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1549 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1550 *and external pullup) */
1551 else
1552 low_output |= nTRST; /* switch output high */
1555 if (srst == 1) {
1556 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1557 low_output &= ~nSRST; /* switch output low */
1558 else
1559 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1560 } else if (srst == 0) {
1561 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1562 low_output |= nSRST; /* switch output high */
1563 else
1564 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1567 /* command "set data bits low byte" */
1568 buffer_write(0x80);
1569 buffer_write(low_output);
1570 buffer_write(low_direction);
1573 static void jtagkey_reset(int trst, int srst)
1575 enum reset_types jtag_reset_config = jtag_get_reset_config();
1576 if (trst == 1) {
1577 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1578 high_output &= ~nTRSTnOE;
1579 else
1580 high_output &= ~nTRST;
1581 } else if (trst == 0) {
1582 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1583 high_output |= nTRSTnOE;
1584 else
1585 high_output |= nTRST;
1588 if (srst == 1) {
1589 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1590 high_output &= ~nSRST;
1591 else
1592 high_output &= ~nSRSTnOE;
1593 } else if (srst == 0) {
1594 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1595 high_output |= nSRST;
1596 else
1597 high_output |= nSRSTnOE;
1600 /* command "set data bits high byte" */
1601 buffer_write(0x82);
1602 buffer_write(high_output);
1603 buffer_write(high_direction);
1604 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1605 trst,
1606 srst,
1607 high_output,
1608 high_direction);
1611 static void olimex_jtag_reset(int trst, int srst)
1613 enum reset_types jtag_reset_config = jtag_get_reset_config();
1614 if (trst == 1) {
1615 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1616 high_output &= ~nTRSTnOE;
1617 else
1618 high_output &= ~nTRST;
1619 } else if (trst == 0) {
1620 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1621 high_output |= nTRSTnOE;
1622 else
1623 high_output |= nTRST;
1626 if (srst == 1)
1627 high_output |= nSRST;
1628 else if (srst == 0)
1629 high_output &= ~nSRST;
1631 /* command "set data bits high byte" */
1632 buffer_write(0x82);
1633 buffer_write(high_output);
1634 buffer_write(high_direction);
1635 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1636 trst,
1637 srst,
1638 high_output,
1639 high_direction);
1642 static void axm0432_jtag_reset(int trst, int srst)
1644 if (trst == 1) {
1645 tap_set_state(TAP_RESET);
1646 high_output &= ~nTRST;
1647 } else if (trst == 0)
1648 high_output |= nTRST;
1650 if (srst == 1)
1651 high_output &= ~nSRST;
1652 else if (srst == 0)
1653 high_output |= nSRST;
1655 /* command "set data bits low byte" */
1656 buffer_write(0x82);
1657 buffer_write(high_output);
1658 buffer_write(high_direction);
1659 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1660 trst,
1661 srst,
1662 high_output,
1663 high_direction);
1666 static void flyswatter_reset(int trst, int srst)
1668 if (trst == 1)
1669 low_output &= ~nTRST;
1670 else if (trst == 0)
1671 low_output |= nTRST;
1673 if (srst == 1)
1674 low_output |= nSRST;
1675 else if (srst == 0)
1676 low_output &= ~nSRST;
1678 /* command "set data bits low byte" */
1679 buffer_write(0x80);
1680 buffer_write(low_output);
1681 buffer_write(low_direction);
1682 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1683 trst,
1684 srst,
1685 low_output,
1686 low_direction);
1689 static void flyswatter1_reset(int trst, int srst)
1691 flyswatter_reset(trst, srst);
1694 static void flyswatter2_reset(int trst, int srst)
1696 flyswatter_reset(trst, !srst);
1699 static void minimodule_reset(int trst, int srst)
1701 if (srst == 1)
1702 low_output &= ~nSRST;
1703 else if (srst == 0)
1704 low_output |= nSRST;
1706 /* command "set data bits low byte" */
1707 buffer_write(0x80);
1708 buffer_write(low_output);
1709 buffer_write(low_direction);
1710 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1711 trst,
1712 srst,
1713 low_output,
1714 low_direction);
1717 static void turtle_reset(int trst, int srst)
1719 trst = trst;
1721 if (srst == 1)
1722 low_output |= nSRST;
1723 else if (srst == 0)
1724 low_output &= ~nSRST;
1726 /* command "set data bits low byte" */
1727 buffer_write(0x80);
1728 buffer_write(low_output);
1729 buffer_write(low_direction);
1730 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1731 srst,
1732 low_output,
1733 low_direction);
1736 static void comstick_reset(int trst, int srst)
1738 if (trst == 1)
1739 high_output &= ~nTRST;
1740 else if (trst == 0)
1741 high_output |= nTRST;
1743 if (srst == 1)
1744 high_output &= ~nSRST;
1745 else if (srst == 0)
1746 high_output |= nSRST;
1748 /* command "set data bits high byte" */
1749 buffer_write(0x82);
1750 buffer_write(high_output);
1751 buffer_write(high_direction);
1752 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1753 trst,
1754 srst,
1755 high_output,
1756 high_direction);
1759 static void stm32stick_reset(int trst, int srst)
1761 if (trst == 1)
1762 high_output &= ~nTRST;
1763 else if (trst == 0)
1764 high_output |= nTRST;
1766 if (srst == 1)
1767 low_output &= ~nSRST;
1768 else if (srst == 0)
1769 low_output |= nSRST;
1771 /* command "set data bits low byte" */
1772 buffer_write(0x80);
1773 buffer_write(low_output);
1774 buffer_write(low_direction);
1776 /* command "set data bits high byte" */
1777 buffer_write(0x82);
1778 buffer_write(high_output);
1779 buffer_write(high_direction);
1780 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1781 trst,
1782 srst,
1783 high_output,
1784 high_direction);
1787 static void sheevaplug_reset(int trst, int srst)
1789 if (trst == 1)
1790 high_output &= ~nTRST;
1791 else if (trst == 0)
1792 high_output |= nTRST;
1794 if (srst == 1)
1795 high_output &= ~nSRSTnOE;
1796 else if (srst == 0)
1797 high_output |= nSRSTnOE;
1799 /* command "set data bits high byte" */
1800 buffer_write(0x82);
1801 buffer_write(high_output);
1802 buffer_write(high_direction);
1803 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1804 trst,
1805 srst,
1806 high_output,
1807 high_direction);
1810 static void redbee_reset(int trst, int srst)
1812 if (trst == 1) {
1813 tap_set_state(TAP_RESET);
1814 high_output &= ~nTRST;
1815 } else if (trst == 0)
1816 high_output |= nTRST;
1818 if (srst == 1)
1819 high_output &= ~nSRST;
1820 else if (srst == 0)
1821 high_output |= nSRST;
1823 /* command "set data bits low byte" */
1824 buffer_write(0x82);
1825 buffer_write(high_output);
1826 buffer_write(high_direction);
1827 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1828 "high_direction: 0x%2.2x", trst, srst, high_output,
1829 high_direction);
1832 static void xds100v2_reset(int trst, int srst)
1834 if (trst == 1) {
1835 tap_set_state(TAP_RESET);
1836 high_output &= ~nTRST;
1837 } else if (trst == 0)
1838 high_output |= nTRST;
1840 if (srst == 1)
1841 high_output |= nSRST;
1842 else if (srst == 0)
1843 high_output &= ~nSRST;
1845 /* command "set data bits low byte" */
1846 buffer_write(0x82);
1847 buffer_write(high_output);
1848 buffer_write(high_direction);
1849 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1850 "high_direction: 0x%2.2x", trst, srst, high_output,
1851 high_direction);
1854 static int ft2232_execute_runtest(struct jtag_command *cmd)
1856 int retval;
1857 int i;
1858 int predicted_size = 0;
1859 retval = ERROR_OK;
1861 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1862 cmd->cmd.runtest->num_cycles,
1863 tap_state_name(cmd->cmd.runtest->end_state));
1865 /* only send the maximum buffer size that FT2232C can handle */
1866 predicted_size = 0;
1867 if (tap_get_state() != TAP_IDLE)
1868 predicted_size += 3;
1869 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1870 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1871 predicted_size += 3;
1872 if (tap_get_end_state() != TAP_IDLE)
1873 predicted_size += 3;
1874 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1875 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1876 retval = ERROR_JTAG_QUEUE_FAILED;
1877 require_send = 0;
1878 first_unsent = cmd;
1880 if (tap_get_state() != TAP_IDLE) {
1881 move_to_state(TAP_IDLE);
1882 require_send = 1;
1884 i = cmd->cmd.runtest->num_cycles;
1885 while (i > 0) {
1886 /* there are no state transitions in this code, so omit state tracking */
1888 /* command "Clock Data to TMS/CS Pin (no Read)" */
1889 buffer_write(0x4b);
1891 /* scan 7 bits */
1892 buffer_write((i > 7) ? 6 : (i - 1));
1894 /* TMS data bits */
1895 buffer_write(0x0);
1897 i -= (i > 7) ? 7 : i;
1898 /* LOG_DEBUG("added TMS scan (no read)"); */
1901 ft2232_end_state(cmd->cmd.runtest->end_state);
1903 if (tap_get_state() != tap_get_end_state())
1904 move_to_state(tap_get_end_state());
1906 require_send = 1;
1907 DEBUG_JTAG_IO("runtest: %i, end in %s",
1908 cmd->cmd.runtest->num_cycles,
1909 tap_state_name(tap_get_end_state()));
1910 return retval;
1913 static int ft2232_execute_statemove(struct jtag_command *cmd)
1915 int predicted_size = 0;
1916 int retval = ERROR_OK;
1918 DEBUG_JTAG_IO("statemove end in %s",
1919 tap_state_name(cmd->cmd.statemove->end_state));
1921 /* only send the maximum buffer size that FT2232C can handle */
1922 predicted_size = 3;
1923 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1924 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1925 retval = ERROR_JTAG_QUEUE_FAILED;
1926 require_send = 0;
1927 first_unsent = cmd;
1929 ft2232_end_state(cmd->cmd.statemove->end_state);
1931 /* For TAP_RESET, ignore the current recorded state. It's often
1932 * wrong at server startup, and this transation is critical whenever
1933 * it's requested.
1935 if (tap_get_end_state() == TAP_RESET) {
1936 clock_tms(0x4b, 0xff, 5, 0);
1937 require_send = 1;
1939 /* shortest-path move to desired end state */
1940 } else if (tap_get_state() != tap_get_end_state()) {
1941 move_to_state(tap_get_end_state());
1942 require_send = 1;
1945 return retval;
1949 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1950 * (or SWD) state machine.
1952 static int ft2232_execute_tms(struct jtag_command *cmd)
1954 int retval = ERROR_OK;
1955 unsigned num_bits = cmd->cmd.tms->num_bits;
1956 const uint8_t *bits = cmd->cmd.tms->bits;
1957 unsigned count;
1959 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1961 /* only send the maximum buffer size that FT2232C can handle */
1962 count = 3 * DIV_ROUND_UP(num_bits, 4);
1963 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1964 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1965 retval = ERROR_JTAG_QUEUE_FAILED;
1967 require_send = 0;
1968 first_unsent = cmd;
1971 /* Shift out in batches of at most 6 bits; there's a report of an
1972 * FT2232 bug in this area, where shifting exactly 7 bits can make
1973 * problems with TMS signaling for the last clock cycle:
1975 * http://developer.intra2net.com/mailarchive/html/
1976 * libftdi/2009/msg00292.html
1978 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1980 * Note that pathmoves in JTAG are not often seven bits, so that
1981 * isn't a particularly likely situation outside of "special"
1982 * signaling such as switching between JTAG and SWD modes.
1984 while (num_bits) {
1985 if (num_bits <= 6) {
1986 buffer_write(0x4b);
1987 buffer_write(num_bits - 1);
1988 buffer_write(*bits & 0x3f);
1989 break;
1992 /* Yes, this is lazy ... we COULD shift out more data
1993 * bits per operation, but doing it in nybbles is easy
1995 buffer_write(0x4b);
1996 buffer_write(3);
1997 buffer_write(*bits & 0xf);
1998 num_bits -= 4;
2000 count = (num_bits > 4) ? 4 : num_bits;
2002 buffer_write(0x4b);
2003 buffer_write(count - 1);
2004 buffer_write((*bits >> 4) & 0xf);
2005 num_bits -= count;
2007 bits++;
2010 require_send = 1;
2011 return retval;
2014 static int ft2232_execute_pathmove(struct jtag_command *cmd)
2016 int predicted_size = 0;
2017 int retval = ERROR_OK;
2019 tap_state_t *path = cmd->cmd.pathmove->path;
2020 int num_states = cmd->cmd.pathmove->num_states;
2022 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
2023 tap_state_name(tap_get_state()),
2024 tap_state_name(path[num_states-1]));
2026 /* only send the maximum buffer size that FT2232C can handle */
2027 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
2028 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
2029 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2030 retval = ERROR_JTAG_QUEUE_FAILED;
2032 require_send = 0;
2033 first_unsent = cmd;
2036 ft2232_add_pathmove(path, num_states);
2037 require_send = 1;
2039 return retval;
2042 static int ft2232_execute_scan(struct jtag_command *cmd)
2044 uint8_t *buffer;
2045 int scan_size; /* size of IR or DR scan */
2046 int predicted_size = 0;
2047 int retval = ERROR_OK;
2049 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
2051 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
2053 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
2055 predicted_size = ft2232_predict_scan_out(scan_size, type);
2056 if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
2057 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
2058 /* unsent commands before this */
2059 if (first_unsent != cmd)
2060 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2061 retval = ERROR_JTAG_QUEUE_FAILED;
2063 /* current command */
2064 ft2232_end_state(cmd->cmd.scan->end_state);
2065 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
2066 require_send = 0;
2067 first_unsent = cmd->next;
2068 if (buffer)
2069 free(buffer);
2070 return retval;
2071 } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
2072 LOG_DEBUG(
2073 "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
2074 first_unsent,
2075 cmd);
2076 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2077 retval = ERROR_JTAG_QUEUE_FAILED;
2078 require_send = 0;
2079 first_unsent = cmd;
2081 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
2082 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
2083 ft2232_end_state(cmd->cmd.scan->end_state);
2084 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
2085 require_send = 1;
2086 if (buffer)
2087 free(buffer);
2088 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
2089 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
2090 tap_state_name(tap_get_end_state()));
2091 return retval;
2095 static int ft2232_execute_reset(struct jtag_command *cmd)
2097 int retval;
2098 int predicted_size = 0;
2099 retval = ERROR_OK;
2101 DEBUG_JTAG_IO("reset trst: %i srst %i",
2102 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2104 /* only send the maximum buffer size that FT2232C can handle */
2105 predicted_size = 3;
2106 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
2107 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2108 retval = ERROR_JTAG_QUEUE_FAILED;
2109 require_send = 0;
2110 first_unsent = cmd;
2113 if ((cmd->cmd.reset->trst == 1) ||
2114 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
2115 tap_set_state(TAP_RESET);
2117 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2118 require_send = 1;
2120 DEBUG_JTAG_IO("trst: %i, srst: %i",
2121 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2122 return retval;
2125 static int ft2232_execute_sleep(struct jtag_command *cmd)
2127 int retval;
2128 retval = ERROR_OK;
2130 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2132 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2133 retval = ERROR_JTAG_QUEUE_FAILED;
2134 first_unsent = cmd->next;
2135 jtag_sleep(cmd->cmd.sleep->us);
2136 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2137 cmd->cmd.sleep->us,
2138 tap_state_name(tap_get_state()));
2139 return retval;
2142 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2144 int retval;
2145 retval = ERROR_OK;
2147 /* this is only allowed while in a stable state. A check for a stable
2148 * state was done in jtag_add_clocks()
2150 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2151 retval = ERROR_JTAG_QUEUE_FAILED;
2152 DEBUG_JTAG_IO("clocks %i while in %s",
2153 cmd->cmd.stableclocks->num_cycles,
2154 tap_state_name(tap_get_state()));
2155 return retval;
2158 static int ft2232_execute_command(struct jtag_command *cmd)
2160 int retval;
2162 switch (cmd->type) {
2163 case JTAG_RESET:
2164 retval = ft2232_execute_reset(cmd);
2165 break;
2166 case JTAG_RUNTEST:
2167 retval = ft2232_execute_runtest(cmd);
2168 break;
2169 case JTAG_TLR_RESET:
2170 retval = ft2232_execute_statemove(cmd);
2171 break;
2172 case JTAG_PATHMOVE:
2173 retval = ft2232_execute_pathmove(cmd);
2174 break;
2175 case JTAG_SCAN:
2176 retval = ft2232_execute_scan(cmd);
2177 break;
2178 case JTAG_SLEEP:
2179 retval = ft2232_execute_sleep(cmd);
2180 break;
2181 case JTAG_STABLECLOCKS:
2182 retval = ft2232_execute_stableclocks(cmd);
2183 break;
2184 case JTAG_TMS:
2185 retval = ft2232_execute_tms(cmd);
2186 break;
2187 default:
2188 LOG_ERROR("BUG: unknown JTAG command type encountered");
2189 retval = ERROR_JTAG_QUEUE_FAILED;
2190 break;
2192 return retval;
2195 static int ft2232_execute_queue(void)
2197 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
2198 int retval;
2200 first_unsent = cmd; /* next command that has to be sent */
2201 require_send = 0;
2203 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2204 * that wasn't handled by a caller-provided error handler
2206 retval = ERROR_OK;
2208 ft2232_buffer_size = 0;
2209 ft2232_expect_read = 0;
2211 /* blink, if the current layout has that feature */
2212 if (layout->blink)
2213 layout->blink();
2215 while (cmd) {
2216 /* fill the write buffer with the desired command */
2217 if (ft2232_execute_command(cmd) != ERROR_OK)
2218 retval = ERROR_JTAG_QUEUE_FAILED;
2219 /* Start reading input before FT2232 TX buffer fills up.
2220 * Sometimes this happens because we don't know the
2221 * length of the last command before we execute it. So
2222 * we simple inform the user.
2224 cmd = cmd->next;
2226 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE) {
2227 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1))
2228 LOG_DEBUG("read buffer size looks too high %d/%d",
2229 ft2232_expect_read,
2230 (FT2232_BUFFER_READ_QUEUE_SIZE+1));
2231 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2232 retval = ERROR_JTAG_QUEUE_FAILED;
2233 first_unsent = cmd;
2237 if (require_send > 0)
2238 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2239 retval = ERROR_JTAG_QUEUE_FAILED;
2241 return retval;
2244 #if BUILD_FT2232_FTD2XX == 1
2245 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2247 FT_STATUS status;
2248 DWORD deviceID;
2249 char SerialNumber[16];
2250 char Description[64];
2251 DWORD openex_flags = 0;
2252 char *openex_string = NULL;
2253 uint8_t latency_timer;
2255 if (layout == NULL) {
2256 LOG_WARNING("No ft2232 layout specified'");
2257 return ERROR_JTAG_INIT_FAILED;
2260 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2261 layout->name, vid, pid);
2263 #if IS_WIN32 == 0
2264 /* Add non-standard Vid/Pid to the linux driver */
2265 status = FT_SetVIDPID(vid, pid);
2266 if (status != FT_OK)
2267 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2269 #endif
2271 if (ft2232_device_desc && ft2232_serial) {
2272 LOG_WARNING(
2273 "can't open by device description and serial number, giving precedence to serial");
2274 ft2232_device_desc = NULL;
2277 if (ft2232_device_desc) {
2278 openex_string = ft2232_device_desc;
2279 openex_flags = FT_OPEN_BY_DESCRIPTION;
2280 } else if (ft2232_serial) {
2281 openex_string = ft2232_serial;
2282 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2283 } else {
2284 LOG_ERROR("neither device description nor serial number specified");
2285 LOG_ERROR(
2286 "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2288 return ERROR_JTAG_INIT_FAILED;
2291 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2292 if (status != FT_OK) {
2293 /* under Win32, the FTD2XX driver appends an "A" to the end
2294 * of the description, if we tried by the desc, then
2295 * try by the alternate "A" description. */
2296 if (openex_string == ft2232_device_desc) {
2297 /* Try the alternate method. */
2298 openex_string = ft2232_device_desc_A;
2299 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2300 if (status == FT_OK) {
2301 /* yea, the "alternate" method worked! */
2302 } else {
2303 /* drat, give the user a meaningfull message.
2304 * telling the use we tried *BOTH* methods. */
2305 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2306 ft2232_device_desc,
2307 ft2232_device_desc_A);
2312 if (status != FT_OK) {
2313 DWORD num_devices;
2315 if (more) {
2316 LOG_WARNING("unable to open ftdi device (trying more): %s",
2317 ftd2xx_status_string(status));
2318 *try_more = 1;
2319 return ERROR_JTAG_INIT_FAILED;
2321 LOG_ERROR("unable to open ftdi device: %s",
2322 ftd2xx_status_string(status));
2323 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2324 if (status == FT_OK) {
2325 char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2326 uint32_t i;
2328 for (i = 0; i < num_devices; i++)
2329 desc_array[i] = malloc(64);
2331 desc_array[num_devices] = NULL;
2333 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2335 if (status == FT_OK) {
2336 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2337 for (i = 0; i < num_devices; i++)
2338 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2341 for (i = 0; i < num_devices; i++)
2342 free(desc_array[i]);
2344 free(desc_array);
2345 } else
2346 LOG_ERROR("ListDevices: NONE");
2347 return ERROR_JTAG_INIT_FAILED;
2350 status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2351 if (status != FT_OK) {
2352 LOG_ERROR("unable to set latency timer: %s",
2353 ftd2xx_status_string(status));
2354 return ERROR_JTAG_INIT_FAILED;
2357 status = FT_GetLatencyTimer(ftdih, &latency_timer);
2358 if (status != FT_OK) {
2359 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2360 * so ignore errors if using this driver version */
2361 DWORD dw_version;
2363 status = FT_GetDriverVersion(ftdih, &dw_version);
2364 LOG_ERROR("unable to get latency timer: %s",
2365 ftd2xx_status_string(status));
2367 if ((status == FT_OK) && (dw_version == 0x10004)) {
2368 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2369 "with FT_GetLatencyTimer, upgrade to a newer version");
2370 } else
2371 return ERROR_JTAG_INIT_FAILED;
2372 } else
2373 LOG_DEBUG("current latency timer: %i", latency_timer);
2375 status = FT_SetTimeouts(ftdih, 5000, 5000);
2376 if (status != FT_OK) {
2377 LOG_ERROR("unable to set timeouts: %s",
2378 ftd2xx_status_string(status));
2379 return ERROR_JTAG_INIT_FAILED;
2382 status = FT_SetBitMode(ftdih, 0x0b, 2);
2383 if (status != FT_OK) {
2384 LOG_ERROR("unable to enable bit i/o mode: %s",
2385 ftd2xx_status_string(status));
2386 return ERROR_JTAG_INIT_FAILED;
2389 status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2390 SerialNumber, Description, NULL);
2391 if (status != FT_OK) {
2392 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2393 ftd2xx_status_string(status));
2394 return ERROR_JTAG_INIT_FAILED;
2395 } else {
2396 static const char *type_str[] = {
2397 "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2399 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2400 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2401 ? ftdi_device : FT_DEVICE_UNKNOWN;
2402 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2403 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2404 LOG_INFO("SerialNumber: %s", SerialNumber);
2405 LOG_INFO("Description: %s", Description);
2408 return ERROR_OK;
2411 static int ft2232_purge_ftd2xx(void)
2413 FT_STATUS status;
2415 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2416 if (status != FT_OK) {
2417 LOG_ERROR("error purging ftd2xx device: %s",
2418 ftd2xx_status_string(status));
2419 return ERROR_JTAG_INIT_FAILED;
2422 return ERROR_OK;
2425 #endif /* BUILD_FT2232_FTD2XX == 1 */
2427 #if BUILD_FT2232_LIBFTDI == 1
2428 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2430 uint8_t latency_timer;
2432 if (layout == NULL) {
2433 LOG_WARNING("No ft2232 layout specified'");
2434 return ERROR_JTAG_INIT_FAILED;
2437 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2438 layout->name, vid, pid);
2440 if (ftdi_init(&ftdic) < 0)
2441 return ERROR_JTAG_INIT_FAILED;
2443 /* default to INTERFACE_A */
2444 if (channel == INTERFACE_ANY)
2445 channel = INTERFACE_A;
2446 if (ftdi_set_interface(&ftdic, channel) < 0) {
2447 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2448 return ERROR_JTAG_INIT_FAILED;
2451 /* context, vendor id, product id */
2452 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2453 if (more)
2454 LOG_WARNING("unable to open ftdi device (trying more): %s",
2455 ftdic.error_str);
2456 else
2457 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2458 *try_more = 1;
2459 return ERROR_JTAG_INIT_FAILED;
2462 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2463 if (ftdi_usb_reset(&ftdic) < 0) {
2464 LOG_ERROR("unable to reset ftdi device");
2465 return ERROR_JTAG_INIT_FAILED;
2468 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2469 LOG_ERROR("unable to set latency timer");
2470 return ERROR_JTAG_INIT_FAILED;
2473 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2474 LOG_ERROR("unable to get latency timer");
2475 return ERROR_JTAG_INIT_FAILED;
2476 } else
2477 LOG_DEBUG("current latency timer: %i", latency_timer);
2479 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2481 ftdi_device = ftdic.type;
2482 static const char *type_str[] = {
2483 "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "Unknown"
2485 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2486 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2487 ? ftdi_device : no_of_known_types;
2488 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2489 return ERROR_OK;
2492 static int ft2232_purge_libftdi(void)
2494 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2495 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2496 return ERROR_JTAG_INIT_FAILED;
2499 return ERROR_OK;
2502 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2504 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2506 uint8_t buf[3];
2507 uint32_t bytes_written;
2509 buf[0] = 0x80; /* command "set data bits low byte" */
2510 buf[1] = value; /* value */
2511 buf[2] = direction; /* direction */
2513 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2515 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2516 LOG_ERROR("couldn't initialize data bits low byte");
2517 return ERROR_JTAG_INIT_FAILED;
2520 return ERROR_OK;
2523 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2525 uint8_t buf[3];
2526 uint32_t bytes_written;
2528 buf[0] = 0x82; /* command "set data bits high byte" */
2529 buf[1] = value; /* value */
2530 buf[2] = direction; /* direction */
2532 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2534 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2535 LOG_ERROR("couldn't initialize data bits high byte");
2536 return ERROR_JTAG_INIT_FAILED;
2539 return ERROR_OK;
2542 static int ft2232_init(void)
2544 uint8_t buf[1];
2545 int retval;
2546 uint32_t bytes_written;
2548 if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
2549 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2550 else
2551 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2552 if (layout == NULL) {
2553 LOG_WARNING("No ft2232 layout specified'");
2554 return ERROR_JTAG_INIT_FAILED;
2557 for (int i = 0; 1; i++) {
2559 * "more indicates that there are more IDs to try, so we should
2560 * not print an error for an ID mismatch (but for anything
2561 * else, we should).
2563 * try_more indicates that the error code returned indicates an
2564 * ID mismatch (and nothing else) and that we should proceeed
2565 * with the next ID pair.
2567 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2568 int try_more = 0;
2570 #if BUILD_FT2232_FTD2XX == 1
2571 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2572 more, &try_more);
2573 #elif BUILD_FT2232_LIBFTDI == 1
2574 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2575 more, &try_more, layout->channel);
2576 #endif
2577 if (retval >= 0)
2578 break;
2579 if (!more || !try_more)
2580 return retval;
2583 ft2232_buffer_size = 0;
2584 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2586 if (layout->init() != ERROR_OK)
2587 return ERROR_JTAG_INIT_FAILED;
2589 if (ft2232_device_is_highspeed()) {
2590 #ifndef BUILD_FT2232_HIGHSPEED
2591 #if BUILD_FT2232_FTD2XX == 1
2592 LOG_WARNING(
2593 "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2594 #elif BUILD_FT2232_LIBFTDI == 1
2595 LOG_WARNING(
2596 "High Speed device found - You need a newer libftdi version (0.16 or later)");
2597 #endif
2598 #endif
2599 /* make sure the legacy mode is disabled */
2600 if (ftx232h_clk_divide_by_5(false) != ERROR_OK)
2601 return ERROR_JTAG_INIT_FAILED;
2604 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2605 retval = ft2232_write(buf, 1, &bytes_written);
2606 if (retval != ERROR_OK) {
2607 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2608 return ERROR_JTAG_INIT_FAILED;
2611 #if BUILD_FT2232_FTD2XX == 1
2612 return ft2232_purge_ftd2xx();
2613 #elif BUILD_FT2232_LIBFTDI == 1
2614 return ft2232_purge_libftdi();
2615 #endif
2617 return ERROR_OK;
2620 /** Updates defaults for DBUS signals: the four JTAG signals
2621 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2623 static inline void ftx232_dbus_init(void)
2625 low_output = 0x08;
2626 low_direction = 0x0b;
2629 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2630 * the four GPIOL signals. Initialization covers value and direction,
2631 * as customized for each layout.
2633 static int ftx232_dbus_write(void)
2635 enum reset_types jtag_reset_config = jtag_get_reset_config();
2636 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2637 low_direction &= ~nTRSTnOE; /* nTRST input */
2638 low_output &= ~nTRST; /* nTRST = 0 */
2639 } else {
2640 low_direction |= nTRSTnOE; /* nTRST output */
2641 low_output |= nTRST; /* nTRST = 1 */
2644 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2645 low_direction |= nSRSTnOE; /* nSRST output */
2646 low_output |= nSRST; /* nSRST = 1 */
2647 } else {
2648 low_direction &= ~nSRSTnOE; /* nSRST input */
2649 low_output &= ~nSRST; /* nSRST = 0 */
2652 /* initialize low byte for jtag */
2653 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2654 LOG_ERROR("couldn't initialize FT2232 DBUS");
2655 return ERROR_JTAG_INIT_FAILED;
2658 return ERROR_OK;
2661 static int usbjtag_init(void)
2664 * NOTE: This is now _specific_ to the "usbjtag" layout.
2665 * Don't try cram any more layouts into this.
2667 ftx232_dbus_init();
2669 nTRST = 0x10;
2670 nTRSTnOE = 0x10;
2671 nSRST = 0x40;
2672 nSRSTnOE = 0x40;
2674 return ftx232_dbus_write();
2677 static int lm3s811_jtag_init(void)
2679 ftx232_dbus_init();
2681 /* There are multiple revisions of LM3S811 eval boards:
2682 * - Rev B (and older?) boards have no SWO trace support.
2683 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2684 * they should use the "luminary_icdi" layout instead.
2686 nTRST = 0x0;
2687 nTRSTnOE = 0x00;
2688 nSRST = 0x20;
2689 nSRSTnOE = 0x20;
2690 low_output = 0x88;
2691 low_direction = 0x8b;
2693 return ftx232_dbus_write();
2696 static int icdi_jtag_init(void)
2698 ftx232_dbus_init();
2700 /* Most Luminary eval boards support SWO trace output,
2701 * and should use this "luminary_icdi" layout.
2703 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2704 * to switch between JTAG and SWD, or switch the ft2232 UART
2705 * on the second MPSSE channel/interface (BDBUS)
2706 * between (i) the stellaris UART (on Luminary boards)
2707 * or (ii) SWO trace data (generic).
2709 * We come up in JTAG mode and may switch to SWD later (with
2710 * SWO/trace option if SWD is active).
2712 * DBUS == GPIO-Lx
2713 * CBUS == GPIO-Hx
2717 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2718 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2719 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2722 /* GPIOs on second channel/interface (UART) ... */
2723 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2724 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2725 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2727 nTRST = 0x0;
2728 nTRSTnOE = 0x00;
2729 nSRST = ICDI_SRST;
2730 nSRSTnOE = ICDI_SRST;
2732 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2733 low_output |= ICDI_JTAG_EN;
2734 low_output &= ~ICDI_DBG_ENn;
2736 return ftx232_dbus_write();
2739 static int signalyzer_init(void)
2741 ftx232_dbus_init();
2743 nTRST = 0x10;
2744 nTRSTnOE = 0x10;
2745 nSRST = 0x20;
2746 nSRSTnOE = 0x20;
2747 return ftx232_dbus_write();
2750 static int axm0432_jtag_init(void)
2752 low_output = 0x08;
2753 low_direction = 0x2b;
2755 /* initialize low byte for jtag */
2756 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2757 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2758 return ERROR_JTAG_INIT_FAILED;
2761 if (strcmp(layout->name, "axm0432_jtag") == 0) {
2762 nTRST = 0x08;
2763 nTRSTnOE = 0x0; /* No output enable for TRST*/
2764 nSRST = 0x04;
2765 nSRSTnOE = 0x0; /* No output enable for SRST*/
2766 } else {
2767 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2768 exit(-1);
2771 high_output = 0x0;
2772 high_direction = 0x0c;
2774 enum reset_types jtag_reset_config = jtag_get_reset_config();
2775 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2776 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2777 else
2778 high_output |= nTRST;
2780 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2781 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2782 else
2783 high_output |= nSRST;
2785 /* initialize high byte for jtag */
2786 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2787 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2788 return ERROR_JTAG_INIT_FAILED;
2791 return ERROR_OK;
2794 static int redbee_init(void)
2796 low_output = 0x08;
2797 low_direction = 0x2b;
2799 /* initialize low byte for jtag */
2800 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2801 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2802 return ERROR_JTAG_INIT_FAILED;
2805 nTRST = 0x08;
2806 nTRSTnOE = 0x0; /* No output enable for TRST*/
2807 nSRST = 0x04;
2808 nSRSTnOE = 0x0; /* No output enable for SRST*/
2810 high_output = 0x0;
2811 high_direction = 0x0c;
2813 enum reset_types jtag_reset_config = jtag_get_reset_config();
2814 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2815 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2816 else
2817 high_output |= nTRST;
2819 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2820 LOG_ERROR("can't set nSRST to push-pull on redbee");
2821 else
2822 high_output |= nSRST;
2824 /* initialize high byte for jtag */
2825 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2826 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2827 return ERROR_JTAG_INIT_FAILED;
2830 return ERROR_OK;
2833 static int jtagkey_init(void)
2835 low_output = 0x08;
2836 low_direction = 0x1b;
2838 /* initialize low byte for jtag */
2839 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2840 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2841 return ERROR_JTAG_INIT_FAILED;
2844 if (strcmp(layout->name, "jtagkey") == 0) {
2845 nTRST = 0x01;
2846 nTRSTnOE = 0x4;
2847 nSRST = 0x02;
2848 nSRSTnOE = 0x08;
2849 } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2850 || (strcmp(layout->name, "oocdlink") == 0)) {
2851 nTRST = 0x02;
2852 nTRSTnOE = 0x1;
2853 nSRST = 0x08;
2854 nSRSTnOE = 0x04;
2855 } else {
2856 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2857 exit(-1);
2860 high_output = 0x0;
2861 high_direction = 0x0f;
2863 enum reset_types jtag_reset_config = jtag_get_reset_config();
2864 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2865 high_output |= nTRSTnOE;
2866 high_output &= ~nTRST;
2867 } else {
2868 high_output &= ~nTRSTnOE;
2869 high_output |= nTRST;
2872 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2873 high_output &= ~nSRSTnOE;
2874 high_output |= nSRST;
2875 } else {
2876 high_output |= nSRSTnOE;
2877 high_output &= ~nSRST;
2880 /* initialize high byte for jtag */
2881 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2882 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2883 return ERROR_JTAG_INIT_FAILED;
2886 return ERROR_OK;
2889 static int olimex_jtag_init(void)
2891 low_output = 0x08;
2892 low_direction = 0x1b;
2894 /* initialize low byte for jtag */
2895 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2896 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2897 return ERROR_JTAG_INIT_FAILED;
2900 nTRST = 0x01;
2901 nTRSTnOE = 0x4;
2902 nSRST = 0x02;
2903 nSRSTnOE = 0x00;/* no output enable for nSRST */
2905 high_output = 0x0;
2906 high_direction = 0x0f;
2908 enum reset_types jtag_reset_config = jtag_get_reset_config();
2909 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2910 high_output |= nTRSTnOE;
2911 high_output &= ~nTRST;
2912 } else {
2913 high_output &= ~nTRSTnOE;
2914 high_output |= nTRST;
2917 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2918 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2919 else
2920 high_output &= ~nSRST;
2922 /* turn red LED on */
2923 high_output |= 0x08;
2925 /* initialize high byte for jtag */
2926 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
2927 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2928 return ERROR_JTAG_INIT_FAILED;
2931 return ERROR_OK;
2934 static int flyswatter_init(int rev)
2936 low_output = 0x18;
2937 low_direction = 0x7b;
2939 if ((rev < 0) || (rev > 3)) {
2940 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2941 return ERROR_JTAG_INIT_FAILED;
2944 if (rev == 1)
2945 low_direction |= 1 << 7;
2947 /* initialize low byte for jtag */
2948 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
2949 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2950 return ERROR_JTAG_INIT_FAILED;
2953 nTRST = 0x10;
2954 nTRSTnOE = 0x0; /* not output enable for nTRST */
2955 nSRST = 0x20;
2956 nSRSTnOE = 0x00; /* no output enable for nSRST */
2958 high_output = 0x00;
2960 if (rev == 1)
2961 high_direction = 0x0c;
2962 else
2963 high_direction = 0x01;
2965 /* turn red LED3 on, LED2 off */
2966 high_output |= 0x08;
2968 /* initialize high byte for jtag */
2969 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
2990 * 0x18) */
2991 low_direction = 0xfb; /* 0xfb; */
2993 /* initialize low byte for jtag */
2994 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) {
3010 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
3011 return ERROR_JTAG_INIT_FAILED;
3014 return ERROR_OK;
3017 static int turtle_init(void)
3019 low_output = 0x08;
3020 low_direction = 0x5b;
3022 /* initialize low byte for jtag */
3023 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3024 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
3025 return ERROR_JTAG_INIT_FAILED;
3028 nSRST = 0x40;
3030 high_output = 0x00;
3031 high_direction = 0x0C;
3033 /* initialize high byte for jtag */
3034 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3035 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
3036 return ERROR_JTAG_INIT_FAILED;
3039 return ERROR_OK;
3042 static int comstick_init(void)
3044 low_output = 0x08;
3045 low_direction = 0x0b;
3047 /* initialize low byte for jtag */
3048 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3049 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3050 return ERROR_JTAG_INIT_FAILED;
3053 nTRST = 0x01;
3054 nTRSTnOE = 0x00; /* no output enable for nTRST */
3055 nSRST = 0x02;
3056 nSRSTnOE = 0x00; /* no output enable for nSRST */
3058 high_output = 0x03;
3059 high_direction = 0x03;
3061 /* initialize high byte for jtag */
3062 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3063 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3064 return ERROR_JTAG_INIT_FAILED;
3067 return ERROR_OK;
3070 static int stm32stick_init(void)
3072 low_output = 0x88;
3073 low_direction = 0x8b;
3075 /* initialize low byte for jtag */
3076 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3077 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3078 return ERROR_JTAG_INIT_FAILED;
3081 nTRST = 0x01;
3082 nTRSTnOE = 0x00; /* no output enable for nTRST */
3083 nSRST = 0x80;
3084 nSRSTnOE = 0x00; /* no output enable for nSRST */
3086 high_output = 0x01;
3087 high_direction = 0x03;
3089 /* initialize high byte for jtag */
3090 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3091 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3092 return ERROR_JTAG_INIT_FAILED;
3095 return ERROR_OK;
3098 static int sheevaplug_init(void)
3100 low_output = 0x08;
3101 low_direction = 0x1b;
3103 /* initialize low byte for jtag */
3104 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3105 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3106 return ERROR_JTAG_INIT_FAILED;
3109 nTRSTnOE = 0x1;
3110 nTRST = 0x02;
3111 nSRSTnOE = 0x4;
3112 nSRST = 0x08;
3114 high_output = 0x0;
3115 high_direction = 0x0f;
3117 /* nTRST is always push-pull */
3118 high_output &= ~nTRSTnOE;
3119 high_output |= nTRST;
3121 /* nSRST is always open-drain */
3122 high_output |= nSRSTnOE;
3123 high_output &= ~nSRST;
3125 /* initialize high byte for jtag */
3126 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3127 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3128 return ERROR_JTAG_INIT_FAILED;
3131 return ERROR_OK;
3134 static int cortino_jtag_init(void)
3136 low_output = 0x08;
3137 low_direction = 0x1b;
3139 /* initialize low byte for jtag */
3140 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3141 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3142 return ERROR_JTAG_INIT_FAILED;
3145 nTRST = 0x01;
3146 nTRSTnOE = 0x00; /* no output enable for nTRST */
3147 nSRST = 0x02;
3148 nSRSTnOE = 0x00; /* no output enable for nSRST */
3150 high_output = 0x03;
3151 high_direction = 0x03;
3153 /* initialize high byte for jtag */
3154 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3155 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3156 return ERROR_JTAG_INIT_FAILED;
3159 return ERROR_OK;
3162 static int lisa_l_init(void)
3164 ftx232_dbus_init();
3166 nTRST = 0x10;
3167 nTRSTnOE = 0x10;
3168 nSRST = 0x40;
3169 nSRSTnOE = 0x40;
3171 high_output = 0x00;
3172 high_direction = 0x18;
3174 /* initialize high byte for jtag */
3175 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3176 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3177 return ERROR_JTAG_INIT_FAILED;
3180 return ftx232_dbus_write();
3183 static int flossjtag_init(void)
3185 ftx232_dbus_init();
3187 nTRST = 0x10;
3188 nTRSTnOE = 0x10;
3189 nSRST = 0x40;
3190 nSRSTnOE = 0x40;
3192 high_output = 0x00;
3193 high_direction = 0x18;
3195 /* initialize high byte for jtag */
3196 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3197 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3198 return ERROR_JTAG_INIT_FAILED;
3201 return ftx232_dbus_write();
3205 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3206 * the door for a number of different configurations
3208 * Known Implementations:
3209 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3211 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3212 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3213 * * ACBUS3 to transition 0->1 (OE rising edge)
3214 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3215 * * ADBUS5/GPIOL1 = EMU_EN = 1
3216 * * ADBUS6/GPIOL2 = EMU0 = 0
3217 * * ACBUS4/SPARE0 = EMU1 = 0
3218 * * CPLD logic: Disable loopback
3219 * * ACBUS6/SPARE2 = LOOPBACK = 0
3221 #define XDS100_nEMU_EN (1<<5)
3222 #define XDS100_nEMU0 (1<<6)
3224 #define XDS100_PWR_RST (1<<3)
3225 #define XDS100_nEMU1 (1<<4)
3226 #define XDS100_LOOPBACK (1<<6)
3227 static int xds100v2_init(void)
3229 /* These are in the lower byte */
3230 nTRST = 0x10;
3231 nTRSTnOE = 0x10;
3233 /* These aren't actually used on 14 pin connectors
3234 * These are in the upper byte */
3235 nSRST = 0x01;
3236 nSRSTnOE = 0x01;
3238 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3239 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3241 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3242 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3243 return ERROR_JTAG_INIT_FAILED;
3246 high_output = 0;
3247 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3249 /* initialize high byte for jtag */
3250 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3251 LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3252 return ERROR_JTAG_INIT_FAILED;
3255 high_output |= XDS100_PWR_RST;
3257 /* initialize high byte for jtag */
3258 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3259 LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3260 return ERROR_JTAG_INIT_FAILED;
3263 return ERROR_OK;
3266 static void olimex_jtag_blink(void)
3268 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3269 * ACBUS3 is bit 3 of the GPIOH port
3271 high_output ^= 0x08;
3273 buffer_write(0x82);
3274 buffer_write(high_output);
3275 buffer_write(high_direction);
3278 static void flyswatter_jtag_blink(unsigned char led)
3280 buffer_write(0x82);
3281 buffer_write(high_output ^ led);
3282 buffer_write(high_direction);
3285 static void flyswatter1_jtag_blink(void)
3288 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3290 flyswatter_jtag_blink(0xc);
3293 static void flyswatter2_jtag_blink(void)
3296 * Flyswatter2 only has one LED connected to ACBUS2
3298 flyswatter_jtag_blink(0x4);
3301 static void turtle_jtag_blink(void)
3304 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3306 if (high_output & 0x08)
3307 high_output = 0x04;
3308 else
3309 high_output = 0x08;
3311 buffer_write(0x82);
3312 buffer_write(high_output);
3313 buffer_write(high_direction);
3316 static void lisa_l_blink(void)
3319 * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3321 if (high_output & 0x10)
3322 high_output = 0x08;
3323 else
3324 high_output = 0x10;
3326 buffer_write(0x82);
3327 buffer_write(high_output);
3328 buffer_write(high_direction);
3331 static void flossjtag_blink(void)
3334 * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3336 if (high_output & 0x10)
3337 high_output = 0x08;
3338 else
3339 high_output = 0x10;
3341 buffer_write(0x82);
3342 buffer_write(high_output);
3343 buffer_write(high_direction);
3346 static int ft2232_quit(void)
3348 #if BUILD_FT2232_FTD2XX == 1
3349 FT_STATUS status;
3351 status = FT_Close(ftdih);
3352 #elif BUILD_FT2232_LIBFTDI == 1
3353 ftdi_usb_close(&ftdic);
3355 ftdi_deinit(&ftdic);
3356 #endif
3358 free(ft2232_buffer);
3359 ft2232_buffer = NULL;
3361 return ERROR_OK;
3364 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3366 char *cp;
3367 char buf[200];
3368 if (CMD_ARGC == 1) {
3369 ft2232_device_desc = strdup(CMD_ARGV[0]);
3370 cp = strchr(ft2232_device_desc, 0);
3371 /* under Win32, the FTD2XX driver appends an "A" to the end
3372 * of the description, this examines the given desc
3373 * and creates the 'missing' _A or non_A variable. */
3374 if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3375 /* it was, so make this the "A" version. */
3376 ft2232_device_desc_A = ft2232_device_desc;
3377 /* and *CREATE* the non-A version. */
3378 strcpy(buf, ft2232_device_desc);
3379 cp = strchr(buf, 0);
3380 cp[-2] = 0;
3381 ft2232_device_desc = strdup(buf);
3382 } else {
3383 /* <space > A not defined
3384 * so create it */
3385 sprintf(buf, "%s A", ft2232_device_desc);
3386 ft2232_device_desc_A = strdup(buf);
3388 } else
3389 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3391 return ERROR_OK;
3394 COMMAND_HANDLER(ft2232_handle_serial_command)
3396 if (CMD_ARGC == 1)
3397 ft2232_serial = strdup(CMD_ARGV[0]);
3398 else
3399 return ERROR_COMMAND_SYNTAX_ERROR;
3401 return ERROR_OK;
3404 COMMAND_HANDLER(ft2232_handle_layout_command)
3406 if (CMD_ARGC != 1)
3407 return ERROR_COMMAND_SYNTAX_ERROR;
3409 if (layout) {
3410 LOG_ERROR("already specified ft2232_layout %s",
3411 layout->name);
3412 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3413 ? ERROR_FAIL
3414 : ERROR_OK;
3417 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3418 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3419 layout = l;
3420 return ERROR_OK;
3424 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3425 return ERROR_FAIL;
3428 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3430 if (CMD_ARGC > MAX_USB_IDS * 2) {
3431 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3432 "(maximum is %d pairs)", MAX_USB_IDS);
3433 CMD_ARGC = MAX_USB_IDS * 2;
3435 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3436 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3437 if (CMD_ARGC < 2)
3438 return ERROR_COMMAND_SYNTAX_ERROR;
3439 /* remove the incomplete trailing id */
3440 CMD_ARGC -= 1;
3443 unsigned i;
3444 for (i = 0; i < CMD_ARGC; i += 2) {
3445 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3446 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3450 * Explicitly terminate, in case there are multiples instances of
3451 * ft2232_vid_pid.
3453 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3455 return ERROR_OK;
3458 COMMAND_HANDLER(ft2232_handle_latency_command)
3460 if (CMD_ARGC == 1)
3461 ft2232_latency = atoi(CMD_ARGV[0]);
3462 else
3463 return ERROR_COMMAND_SYNTAX_ERROR;
3465 return ERROR_OK;
3468 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3470 int retval = 0;
3472 /* 7 bits of either ones or zeros. */
3473 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3475 while (num_cycles > 0) {
3476 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3477 * at most 7 bits per invocation. Here we invoke it potentially
3478 * several times.
3480 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3482 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE) {
3483 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3484 retval = ERROR_JTAG_QUEUE_FAILED;
3486 first_unsent = cmd;
3489 /* there are no state transitions in this code, so omit state tracking */
3491 /* command "Clock Data to TMS/CS Pin (no Read)" */
3492 buffer_write(0x4b);
3494 /* scan 7 bit */
3495 buffer_write(bitcount_per_command - 1);
3497 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3498 buffer_write(tms);
3500 require_send = 1;
3502 num_cycles -= bitcount_per_command;
3505 return retval;
3508 /* ---------------------------------------------------------------------
3509 * Support for IceBear JTAG adapter from Section5:
3510 * http://section5.ch/icebear
3512 * Author: Sten, debian@sansys-electronic.com
3515 /* Icebear pin layout
3517 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3518 * GND GND | 4 3| n.c.
3519 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3520 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3521 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3522 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3523 * ADBUS2 TDO |14 13| GND GND
3525 * ADBUS0 O L TCK ACBUS0 GND
3526 * ADBUS1 O L TDI ACBUS1 GND
3527 * ADBUS2 I TDO ACBUS2 n.c.
3528 * ADBUS3 O H TMS ACBUS3 n.c.
3529 * ADBUS4 O H nTRST
3530 * ADBUS5 O H nSRST
3531 * ADBUS6 - VCC
3532 * ADBUS7 - GND
3534 static int icebear_jtag_init(void)
3536 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3537 low_output = 0x08; /* high: TMS; low: TCK TDI */
3538 nTRST = 0x10;
3539 nSRST = 0x20;
3541 enum reset_types jtag_reset_config = jtag_get_reset_config();
3542 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3543 low_direction &= ~nTRST; /* nTRST high impedance */
3544 else {
3545 low_direction |= nTRST;
3546 low_output |= nTRST;
3549 low_direction |= nSRST;
3550 low_output |= nSRST;
3552 /* initialize low byte for jtag */
3553 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
3554 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3555 return ERROR_JTAG_INIT_FAILED;
3558 high_output = 0x0;
3559 high_direction = 0x00;
3561 /* initialize high byte for jtag */
3562 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
3563 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3564 return ERROR_JTAG_INIT_FAILED;
3567 return ERROR_OK;
3570 static void icebear_jtag_reset(int trst, int srst)
3572 if (trst == 1) {
3573 low_direction |= nTRST;
3574 low_output &= ~nTRST;
3575 } else if (trst == 0) {
3576 enum reset_types jtag_reset_config = jtag_get_reset_config();
3577 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3578 low_direction &= ~nTRST;
3579 else
3580 low_output |= nTRST;
3583 if (srst == 1)
3584 low_output &= ~nSRST;
3585 else if (srst == 0)
3586 low_output |= nSRST;
3588 /* command "set data bits low byte" */
3589 buffer_write(0x80);
3590 buffer_write(low_output);
3591 buffer_write(low_direction);
3593 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3594 trst,
3595 srst,
3596 low_output,
3597 low_direction);
3600 /* ---------------------------------------------------------------------
3601 * Support for Signalyzer H2 and Signalyzer H4
3602 * JTAG adapter from Xverve Technologies Inc.
3603 * http://www.signalyzer.com or http://www.xverve.com
3605 * Author: Oleg Seiljus, oleg@signalyzer.com
3607 static unsigned char signalyzer_h_side;
3608 static unsigned int signalyzer_h_adapter_type;
3610 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3612 #if BUILD_FT2232_FTD2XX == 1
3613 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3614 #endif
3616 #define SIGNALYZER_COMMAND_ADDR 128
3617 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3619 #define SIGNALYZER_COMMAND_VERSION 0x41
3620 #define SIGNALYZER_COMMAND_RESET 0x42
3621 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3622 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3623 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3624 #define SIGNALYZER_COMMAND_LED_SET 0x53
3625 #define SIGNALYZER_COMMAND_ADC 0x54
3626 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3627 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3628 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3629 #define SIGNALYZER_COMMAND_I2C 0x58
3631 #define SIGNALYZER_CHAN_A 1
3632 #define SIGNALYZER_CHAN_B 2
3633 /* LEDS use channel C */
3634 #define SIGNALYZER_CHAN_C 4
3636 #define SIGNALYZER_LED_GREEN 1
3637 #define SIGNALYZER_LED_RED 2
3639 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3640 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3641 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3642 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3643 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3646 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3648 #if BUILD_FT2232_FTD2XX == 1
3649 return FT_WriteEE(ftdih, address, value);
3650 #elif BUILD_FT2232_LIBFTDI == 1
3651 return 0;
3652 #endif
3655 #if BUILD_FT2232_FTD2XX == 1
3656 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3658 return FT_ReadEE(ftdih, address, value);
3660 #endif
3662 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3663 int on_time_ms, int off_time_ms, unsigned char cycles)
3665 unsigned char on_time;
3666 unsigned char off_time;
3668 if (on_time_ms < 0xFFFF)
3669 on_time = (unsigned char)(on_time_ms / 62);
3670 else
3671 on_time = 0xFF;
3673 off_time = (unsigned char)(off_time_ms / 62);
3675 #if BUILD_FT2232_FTD2XX == 1
3676 FT_STATUS status;
3678 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3679 ((uint32_t)(channel << 8) | led));
3680 if (status != FT_OK) {
3681 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3682 ftd2xx_status_string(status));
3683 return ERROR_JTAG_DEVICE_ERROR;
3686 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3687 ((uint32_t)(on_time << 8) | off_time));
3688 if (status != FT_OK) {
3689 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3690 ftd2xx_status_string(status));
3691 return ERROR_JTAG_DEVICE_ERROR;
3694 status = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3695 ((uint32_t)cycles));
3696 if (status != FT_OK) {
3697 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3698 ftd2xx_status_string(status));
3699 return ERROR_JTAG_DEVICE_ERROR;
3702 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3703 SIGNALYZER_COMMAND_LED_SET);
3704 if (status != FT_OK) {
3705 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3706 ftd2xx_status_string(status));
3707 return ERROR_JTAG_DEVICE_ERROR;
3710 return ERROR_OK;
3711 #elif BUILD_FT2232_LIBFTDI == 1
3712 int retval;
3714 retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3715 ((uint32_t)(channel << 8) | led));
3716 if (retval < 0) {
3717 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3718 ftdi_get_error_string(&ftdic));
3719 return ERROR_JTAG_DEVICE_ERROR;
3722 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 1),
3723 ((uint32_t)(on_time << 8) | off_time));
3724 if (retval < 0) {
3725 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3726 ftdi_get_error_string(&ftdic));
3727 return ERROR_JTAG_DEVICE_ERROR;
3730 retval = signalyzer_h_ctrl_write((SIGNALYZER_DATA_BUFFER_ADDR + 2),
3731 (uint32_t)cycles);
3732 if (retval < 0) {
3733 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3734 ftdi_get_error_string(&ftdic));
3735 return ERROR_JTAG_DEVICE_ERROR;
3738 retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3739 SIGNALYZER_COMMAND_LED_SET);
3740 if (retval < 0) {
3741 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3742 ftdi_get_error_string(&ftdic));
3743 return ERROR_JTAG_DEVICE_ERROR;
3746 return ERROR_OK;
3747 #endif
3750 static int signalyzer_h_init(void)
3752 #if BUILD_FT2232_FTD2XX == 1
3753 FT_STATUS status;
3754 int i;
3755 #endif
3757 char *end_of_desc;
3759 uint16_t read_buf[12] = { 0 };
3761 /* turn on center green led */
3762 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3763 0xFFFF, 0x00, 0x00);
3765 /* determine what channel config wants to open
3766 * TODO: change me... current implementation is made to work
3767 * with openocd description parsing.
3769 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3771 if (end_of_desc) {
3772 signalyzer_h_side = *(end_of_desc - 1);
3773 if (signalyzer_h_side == 'B')
3774 signalyzer_h_side = SIGNALYZER_CHAN_B;
3775 else
3776 signalyzer_h_side = SIGNALYZER_CHAN_A;
3777 } else {
3778 LOG_ERROR("No Channel was specified");
3779 return ERROR_FAIL;
3782 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3783 1000, 1000, 0xFF);
3785 #if BUILD_FT2232_FTD2XX == 1
3786 /* read signalyzer versionining information */
3787 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3788 SIGNALYZER_COMMAND_VERSION);
3789 if (status != FT_OK) {
3790 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3791 ftd2xx_status_string(status));
3792 return ERROR_JTAG_DEVICE_ERROR;
3795 for (i = 0; i < 10; i++) {
3796 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3797 &read_buf[i]);
3798 if (status != FT_OK) {
3799 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3800 ftd2xx_status_string(status));
3801 return ERROR_JTAG_DEVICE_ERROR;
3805 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3806 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3807 read_buf[4], read_buf[5], read_buf[6]);
3809 /* set gpio register */
3810 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3811 (uint32_t)(signalyzer_h_side << 8));
3812 if (status != FT_OK) {
3813 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3814 ftd2xx_status_string(status));
3815 return ERROR_JTAG_DEVICE_ERROR;
3818 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0404);
3819 if (status != FT_OK) {
3820 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3821 ftd2xx_status_string(status));
3822 return ERROR_JTAG_DEVICE_ERROR;
3825 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3826 SIGNALYZER_COMMAND_GPIO_STATE);
3827 if (status != FT_OK) {
3828 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3829 ftd2xx_status_string(status));
3830 return ERROR_JTAG_DEVICE_ERROR;
3833 /* read adapter type information */
3834 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3835 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3836 if (status != FT_OK) {
3837 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3838 ftd2xx_status_string(status));
3839 return ERROR_JTAG_DEVICE_ERROR;
3842 status = signalyzer_h_ctrl_write(
3843 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3844 if (status != FT_OK) {
3845 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3846 ftd2xx_status_string(status));
3847 return ERROR_JTAG_DEVICE_ERROR;
3850 status = signalyzer_h_ctrl_write(
3851 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3852 if (status != FT_OK) {
3853 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3854 ftd2xx_status_string(status));
3855 return ERROR_JTAG_DEVICE_ERROR;
3858 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3859 SIGNALYZER_COMMAND_I2C);
3860 if (status != FT_OK) {
3861 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3862 ftd2xx_status_string(status));
3863 return ERROR_JTAG_DEVICE_ERROR;
3866 usleep(100000);
3868 status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3869 if (status != FT_OK) {
3870 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3871 ftd2xx_status_string(status));
3872 return ERROR_JTAG_DEVICE_ERROR;
3875 if (read_buf[0] != 0x0498)
3876 signalyzer_h_adapter_type = 0x0000;
3877 else {
3878 for (i = 0; i < 4; i++) {
3879 status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3880 if (status != FT_OK) {
3881 LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3882 ftd2xx_status_string(status));
3883 return ERROR_JTAG_DEVICE_ERROR;
3887 signalyzer_h_adapter_type = read_buf[0];
3890 #elif BUILD_FT2232_LIBFTDI == 1
3891 /* currently libftdi does not allow reading individual eeprom
3892 * locations, therefore adapter type cannot be detected.
3893 * override with most common type
3895 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3896 #endif
3898 enum reset_types jtag_reset_config = jtag_get_reset_config();
3900 /* ADAPTOR: EM_LT16_A */
3901 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3902 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3903 "detected. (HW: %2x).", (read_buf[1] >> 8));
3905 nTRST = 0x10;
3906 nTRSTnOE = 0x10;
3907 nSRST = 0x20;
3908 nSRSTnOE = 0x20;
3910 low_output = 0x08;
3911 low_direction = 0x1b;
3913 high_output = 0x0;
3914 high_direction = 0x0;
3916 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3917 low_direction &= ~nTRSTnOE; /* nTRST input */
3918 low_output &= ~nTRST; /* nTRST = 0 */
3919 } else {
3920 low_direction |= nTRSTnOE; /* nTRST output */
3921 low_output |= nTRST; /* nTRST = 1 */
3924 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3925 low_direction |= nSRSTnOE; /* nSRST output */
3926 low_output |= nSRST; /* nSRST = 1 */
3927 } else {
3928 low_direction &= ~nSRSTnOE; /* nSRST input */
3929 low_output &= ~nSRST; /* nSRST = 0 */
3932 #if BUILD_FT2232_FTD2XX == 1
3933 /* enable power to the module */
3934 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3935 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3936 if (status != FT_OK) {
3937 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3938 ftd2xx_status_string(status));
3939 return ERROR_JTAG_DEVICE_ERROR;
3942 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3943 SIGNALYZER_COMMAND_POWERCONTROL_SET);
3944 if (status != FT_OK) {
3945 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3946 ftd2xx_status_string(status));
3947 return ERROR_JTAG_DEVICE_ERROR;
3950 /* set gpio mode register */
3951 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3952 (uint32_t)(signalyzer_h_side << 8));
3953 if (status != FT_OK) {
3954 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3955 ftd2xx_status_string(status));
3956 return ERROR_JTAG_DEVICE_ERROR;
3959 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
3960 if (status != FT_OK) {
3961 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3962 ftd2xx_status_string(status));
3963 return ERROR_JTAG_DEVICE_ERROR;
3966 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
3967 if (status != FT_OK) {
3968 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3969 ftd2xx_status_string(status));
3970 return ERROR_JTAG_DEVICE_ERROR;
3973 /* set gpio register */
3974 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3975 (uint32_t)(signalyzer_h_side << 8));
3976 if (status != FT_OK) {
3977 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3978 ftd2xx_status_string(status));
3979 return ERROR_JTAG_DEVICE_ERROR;
3982 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040);
3983 if (status != FT_OK) {
3984 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3985 ftd2xx_status_string(status));
3986 return ERROR_JTAG_DEVICE_ERROR;
3989 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3990 SIGNALYZER_COMMAND_GPIO_STATE);
3991 if (status != FT_OK) {
3992 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3993 ftd2xx_status_string(status));
3994 return ERROR_JTAG_DEVICE_ERROR;
3996 #endif
3998 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3999 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4000 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4001 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4002 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4003 if (signalyzer_h_adapter_type
4004 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
4005 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
4006 "detected. (HW: %2x).", (read_buf[1] >> 8));
4007 else if (signalyzer_h_adapter_type
4008 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
4009 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
4010 "(ARM JTAG with PSU) detected. (HW: %2x).",
4011 (read_buf[1] >> 8));
4012 else if (signalyzer_h_adapter_type
4013 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
4014 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
4015 "detected. (HW: %2x).", (read_buf[1] >> 8));
4016 else if (signalyzer_h_adapter_type
4017 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
4018 LOG_INFO("Signalyzer: EM-JTAG-P "
4019 "(Generic JTAG with PSU) detected. (HW: %2x).",
4020 (read_buf[1] >> 8));
4022 nTRST = 0x02;
4023 nTRSTnOE = 0x04;
4024 nSRST = 0x08;
4025 nSRSTnOE = 0x10;
4027 low_output = 0x08;
4028 low_direction = 0x1b;
4030 high_output = 0x0;
4031 high_direction = 0x1f;
4033 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4034 high_output |= nTRSTnOE;
4035 high_output &= ~nTRST;
4036 } else {
4037 high_output &= ~nTRSTnOE;
4038 high_output |= nTRST;
4041 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4042 high_output &= ~nSRSTnOE;
4043 high_output |= nSRST;
4044 } else {
4045 high_output |= nSRSTnOE;
4046 high_output &= ~nSRST;
4049 #if BUILD_FT2232_FTD2XX == 1
4050 /* enable power to the module */
4051 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
4052 ((uint32_t)(signalyzer_h_side << 8) | 0x01));
4053 if (status != FT_OK) {
4054 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4055 ftd2xx_status_string(status));
4056 return ERROR_JTAG_DEVICE_ERROR;
4059 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
4060 SIGNALYZER_COMMAND_POWERCONTROL_SET);
4061 if (status != FT_OK) {
4062 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4063 ftd2xx_status_string(status));
4064 return ERROR_JTAG_DEVICE_ERROR;
4067 /* set gpio mode register (IO_16 and IO_17 set as analog
4068 * inputs, other is gpio)
4070 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
4071 (uint32_t)(signalyzer_h_side << 8));
4072 if (status != FT_OK) {
4073 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4074 ftd2xx_status_string(status));
4075 return ERROR_JTAG_DEVICE_ERROR;
4078 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060);
4079 if (status != FT_OK) {
4080 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4081 ftd2xx_status_string(status));
4082 return ERROR_JTAG_DEVICE_ERROR;
4085 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_MODE);
4086 if (status != FT_OK) {
4087 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4088 ftd2xx_status_string(status));
4089 return ERROR_JTAG_DEVICE_ERROR;
4092 /* set gpio register (all inputs, for -P modules,
4093 * PSU will be turned off)
4095 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
4096 (uint32_t)(signalyzer_h_side << 8));
4097 if (status != FT_OK) {
4098 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4099 ftd2xx_status_string(status));
4100 return ERROR_JTAG_DEVICE_ERROR;
4103 status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000);
4104 if (status != FT_OK) {
4105 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4106 ftd2xx_status_string(status));
4107 return ERROR_JTAG_DEVICE_ERROR;
4110 status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR, SIGNALYZER_COMMAND_GPIO_STATE);
4111 if (status != FT_OK) {
4112 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
4113 ftd2xx_status_string(status));
4114 return ERROR_JTAG_DEVICE_ERROR;
4116 #endif
4117 } else if (signalyzer_h_adapter_type == 0x0000) {
4118 LOG_INFO("Signalyzer: No external modules were detected.");
4120 nTRST = 0x10;
4121 nTRSTnOE = 0x10;
4122 nSRST = 0x20;
4123 nSRSTnOE = 0x20;
4125 low_output = 0x08;
4126 low_direction = 0x1b;
4128 high_output = 0x0;
4129 high_direction = 0x0;
4131 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4132 low_direction &= ~nTRSTnOE; /* nTRST input */
4133 low_output &= ~nTRST; /* nTRST = 0 */
4134 } else {
4135 low_direction |= nTRSTnOE; /* nTRST output */
4136 low_output |= nTRST; /* nTRST = 1 */
4139 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4140 low_direction |= nSRSTnOE; /* nSRST output */
4141 low_output |= nSRST; /* nSRST = 1 */
4142 } else {
4143 low_direction &= ~nSRSTnOE; /* nSRST input */
4144 low_output &= ~nSRST; /* nSRST = 0 */
4146 } else {
4147 LOG_ERROR("Unknown module type is detected: %.4x",
4148 signalyzer_h_adapter_type);
4149 return ERROR_JTAG_DEVICE_ERROR;
4152 /* initialize low byte of controller for jtag operation */
4153 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4154 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4155 return ERROR_JTAG_INIT_FAILED;
4158 #if BUILD_FT2232_FTD2XX == 1
4159 if (ftdi_device == FT_DEVICE_2232H) {
4160 /* initialize high byte of controller for jtag operation */
4161 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4162 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4163 return ERROR_JTAG_INIT_FAILED;
4166 #elif BUILD_FT2232_LIBFTDI == 1
4167 if (ftdi_device == TYPE_2232H) {
4168 /* initialize high byte of controller for jtag operation */
4169 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4170 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4171 return ERROR_JTAG_INIT_FAILED;
4174 #endif
4175 return ERROR_OK;
4178 static void signalyzer_h_reset(int trst, int srst)
4180 enum reset_types jtag_reset_config = jtag_get_reset_config();
4182 /* ADAPTOR: EM_LT16_A */
4183 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
4184 if (trst == 1) {
4185 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4186 /* switch to output pin (output is low) */
4187 low_direction |= nTRSTnOE;
4188 else
4189 /* switch output low */
4190 low_output &= ~nTRST;
4191 } else if (trst == 0) {
4192 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4193 /* switch to input pin (high-Z + internal
4194 * and external pullup) */
4195 low_direction &= ~nTRSTnOE;
4196 else
4197 /* switch output high */
4198 low_output |= nTRST;
4201 if (srst == 1) {
4202 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4203 /* switch output low */
4204 low_output &= ~nSRST;
4205 else
4206 /* switch to output pin (output is low) */
4207 low_direction |= nSRSTnOE;
4208 } else if (srst == 0) {
4209 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4210 /* switch output high */
4211 low_output |= nSRST;
4212 else
4213 /* switch to input pin (high-Z) */
4214 low_direction &= ~nSRSTnOE;
4217 /* command "set data bits low byte" */
4218 buffer_write(0x80);
4219 buffer_write(low_output);
4220 buffer_write(low_direction);
4221 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4222 "low_direction: 0x%2.2x",
4223 trst, srst, low_output, low_direction);
4225 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4226 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4227 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4228 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4229 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4230 if (trst == 1) {
4231 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4232 high_output &= ~nTRSTnOE;
4233 else
4234 high_output &= ~nTRST;
4235 } else if (trst == 0) {
4236 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4237 high_output |= nTRSTnOE;
4238 else
4239 high_output |= nTRST;
4242 if (srst == 1) {
4243 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4244 high_output &= ~nSRST;
4245 else
4246 high_output &= ~nSRSTnOE;
4247 } else if (srst == 0) {
4248 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4249 high_output |= nSRST;
4250 else
4251 high_output |= nSRSTnOE;
4254 /* command "set data bits high byte" */
4255 buffer_write(0x82);
4256 buffer_write(high_output);
4257 buffer_write(high_direction);
4258 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4259 "high_direction: 0x%2.2x",
4260 trst, srst, high_output, high_direction);
4261 } else if (signalyzer_h_adapter_type == 0x0000) {
4262 if (trst == 1) {
4263 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4264 /* switch to output pin (output is low) */
4265 low_direction |= nTRSTnOE;
4266 else
4267 /* switch output low */
4268 low_output &= ~nTRST;
4269 } else if (trst == 0) {
4270 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4271 /* switch to input pin (high-Z + internal
4272 * and external pullup) */
4273 low_direction &= ~nTRSTnOE;
4274 else
4275 /* switch output high */
4276 low_output |= nTRST;
4279 if (srst == 1) {
4280 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4281 /* switch output low */
4282 low_output &= ~nSRST;
4283 else
4284 /* switch to output pin (output is low) */
4285 low_direction |= nSRSTnOE;
4286 } else if (srst == 0) {
4287 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4288 /* switch output high */
4289 low_output |= nSRST;
4290 else
4291 /* switch to input pin (high-Z) */
4292 low_direction &= ~nSRSTnOE;
4295 /* command "set data bits low byte" */
4296 buffer_write(0x80);
4297 buffer_write(low_output);
4298 buffer_write(low_direction);
4299 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4300 "low_direction: 0x%2.2x",
4301 trst, srst, low_output, low_direction);
4305 static void signalyzer_h_blink(void)
4307 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4310 /********************************************************************
4311 * Support for KT-LINK
4312 * JTAG adapter from KRISTECH
4313 * http://www.kristech.eu
4314 *******************************************************************/
4315 static int ktlink_init(void)
4317 uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4319 low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4320 low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4322 /* initialize low byte for jtag */
4323 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4324 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4325 return ERROR_JTAG_INIT_FAILED;
4328 nTRST = 0x01;
4329 nSRST = 0x02;
4330 nTRSTnOE = 0x04;
4331 nSRSTnOE = 0x08;
4333 high_output = 0x80; /* turn LED on */
4334 high_direction = 0xFF; /* all outputs */
4336 enum reset_types jtag_reset_config = jtag_get_reset_config();
4338 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4339 high_output |= nTRSTnOE;
4340 high_output &= ~nTRST;
4341 } else {
4342 high_output &= ~nTRSTnOE;
4343 high_output |= nTRST;
4346 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4347 high_output &= ~nSRSTnOE;
4348 high_output |= nSRST;
4349 } else {
4350 high_output |= nSRSTnOE;
4351 high_output &= ~nSRST;
4354 /* initialize high byte for jtag */
4355 if (ft2232_set_data_bits_high_byte(high_output, high_direction) != ERROR_OK) {
4356 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4357 return ERROR_JTAG_INIT_FAILED;
4360 return ERROR_OK;
4363 static void ktlink_reset(int trst, int srst)
4365 enum reset_types jtag_reset_config = jtag_get_reset_config();
4367 if (trst == 1) {
4368 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4369 high_output &= ~nTRSTnOE;
4370 else
4371 high_output &= ~nTRST;
4372 } else if (trst == 0) {
4373 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4374 high_output |= nTRSTnOE;
4375 else
4376 high_output |= nTRST;
4379 if (srst == 1) {
4380 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4381 high_output &= ~nSRST;
4382 else
4383 high_output &= ~nSRSTnOE;
4384 } else if (srst == 0) {
4385 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4386 high_output |= nSRST;
4387 else
4388 high_output |= nSRSTnOE;
4391 buffer_write(0x82); /* command "set data bits high byte" */
4392 buffer_write(high_output);
4393 buffer_write(high_direction);
4394 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4395 trst,
4396 srst,
4397 high_output,
4398 high_direction);
4401 static void ktlink_blink(void)
4403 /* LED connected to ACBUS7 */
4404 high_output ^= 0x80;
4406 buffer_write(0x82); /* command "set data bits high byte" */
4407 buffer_write(high_output);
4408 buffer_write(high_direction);
4411 /********************************************************************
4412 * Support for Digilent HS-1
4413 * JTAG adapter from Digilent
4414 * http://www.digilent.com
4415 * Author: Stephane Bonnet bonnetst@hds.utc.fr
4416 *******************************************************************/
4418 static int digilent_hs1_init(void)
4420 /* the adapter only supports the base JTAG signals, no nTRST
4421 nor nSRST */
4422 low_output = 0x88;
4423 low_direction = 0x8b;
4425 /* initialize low byte for jtag */
4426 if (ft2232_set_data_bits_low_byte(low_output, low_direction) != ERROR_OK) {
4427 LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4428 return ERROR_JTAG_INIT_FAILED;
4430 return ERROR_OK;
4433 static void digilent_hs1_reset(int trst, int srst)
4435 /* Dummy function, no reset signals supported. */
4438 static const struct command_registration ft2232_command_handlers[] = {
4440 .name = "ft2232_device_desc",
4441 .handler = &ft2232_handle_device_desc_command,
4442 .mode = COMMAND_CONFIG,
4443 .help = "set the USB device description of the FTDI FT2232 device",
4444 .usage = "description_string",
4447 .name = "ft2232_serial",
4448 .handler = &ft2232_handle_serial_command,
4449 .mode = COMMAND_CONFIG,
4450 .help = "set the serial number of the FTDI FT2232 device",
4451 .usage = "serial_string",
4454 .name = "ft2232_layout",
4455 .handler = &ft2232_handle_layout_command,
4456 .mode = COMMAND_CONFIG,
4457 .help = "set the layout of the FT2232 GPIO signals used "
4458 "to control output-enables and reset signals",
4459 .usage = "layout_name",
4462 .name = "ft2232_vid_pid",
4463 .handler = &ft2232_handle_vid_pid_command,
4464 .mode = COMMAND_CONFIG,
4465 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4466 .usage = "(vid pid)* ",
4469 .name = "ft2232_latency",
4470 .handler = &ft2232_handle_latency_command,
4471 .mode = COMMAND_CONFIG,
4472 .help = "set the FT2232 latency timer to a new value",
4473 .usage = "value",
4475 COMMAND_REGISTRATION_DONE
4478 struct jtag_interface ft2232_interface = {
4479 .name = "ft2232",
4480 .supported = DEBUG_CAP_TMS_SEQ,
4481 .commands = ft2232_command_handlers,
4482 .transports = jtag_only,
4484 .init = ft2232_init,
4485 .quit = ft2232_quit,
4486 .speed = ft2232_speed,
4487 .speed_div = ft2232_speed_div,
4488 .khz = ft2232_khz,
4489 .execute_queue = ft2232_execute_queue,
4490 .bitbang = ft2232_bitbang,