]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/ft2232.c
bde170050d15bbea93c38011a48e094c0624925b
[openocd] / src / jtag / drivers / ft2232.c
1 /***************************************************************************
2 *   Copyright (C) 2009 by Øyvind Harboe                                   *
3 *       Øyvind Harboe <oyvind.harboe@zylin.com>                               *
4 *                                                                         *
5 *   Copyright (C) 2009 by SoftPLC Corporation.  http://softplc.com        *
6 *       Dick Hollenbeck <dick@softplc.com>                                    *
7 *                                                                         *
8 *   Copyright (C) 2004, 2006 by Dominic Rath                              *
9 *   Dominic.Rath@gmx.de                                                   *
10 *                                                                         *
11 *   Copyright (C) 2008 by Spencer Oliver                                  *
12 *   spen@spen-soft.co.uk                                                  *
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation; either version 2 of the License, or     *
17 *   (at your option) any later version.                                   *
18 *                                                                         *
19 *   This program is distributed in the hope that it will be useful,       *
20 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
21 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
22 *   GNU General Public License for more details.                          *
23 *                                                                         *
24 *   You should have received a copy of the GNU General Public License     *
25 *   along with this program; if not, write to the                         *
26 *   Free Software Foundation, Inc.,                                       *
27 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
28 ***************************************************************************/
29
30 /**
31  * @file
32  * JTAG adapters based on the FT2232 full and high speed USB parts are
33  * popular low cost JTAG debug solutions.  Many FT2232 based JTAG adapters
34  * are discrete, but development boards may integrate them as alternatives
35  * to more capable (and expensive) third party JTAG pods.
36  *
37  * JTAG uses only one of the two communications channels ("MPSSE engines")
38  * on these devices.  Adapters based on FT4232 parts have four ports/channels
39  * (A/B/C/D), instead of just two (A/B).
40  *
41  * Especially on development boards integrating one of these chips (as
42  * opposed to discrete pods/dongles), the additional channels can be used
43  * for a variety of purposes, but OpenOCD only uses one channel at a time.
44  *
45  *  - As a USB-to-serial adapter for the target's console UART ...
46  *    which may be able to support ROM boot loaders that load initial
47  *    firmware images to flash (or SRAM).
48  *
49  *  - On systems which support ARM's SWD in addition to JTAG, or instead
50  *    of it, that second port can be used for reading SWV/SWO trace data.
51  *
52  *  - Additional JTAG links, e.g. to a CPLD or * FPGA.
53  *
54  * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55  * request/response interactions involve round trips over the USB link.
56  * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57  * can for example poll quickly for a status change (usually taking on the
58  * order of microseconds not milliseconds) before beginning a queued
59  * transaction which require the previous one to have completed.
60  *
61  * There are dozens of adapters of this type, differing in details which
62  * this driver needs to understand.  Those "layout" details are required
63  * as part of FT2232 driver configuration.
64  *
65  * This code uses information contained in the MPSSE specification which was
66  * found here:
67  * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68  * Hereafter this is called the "MPSSE Spec".
69  *
70  * The datasheet for the ftdichip.com's FT2232D part is here:
71  * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
72  *
73  * Also note the issue with code 0x4b (clock data to TMS) noted in
74  * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75  * which can affect longer JTAG state paths.
76  */
77
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
81
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <jtag/transport.h>
85 #include <helper/time_support.h>
86
87 #if IS_CYGWIN == 1
88 #include <windows.h>
89 #endif
90
91 #include <assert.h>
92
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
97 #endif
98
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
101 #include <ftd2xx.h>
102
103 enum ftdi_interface
104 {
105     INTERFACE_ANY = 0,
106     INTERFACE_A   = 1,
107     INTERFACE_B   = 2,
108     INTERFACE_C   = 3,
109     INTERFACE_D   = 4
110 };
111
112 #elif BUILD_FT2232_LIBFTDI == 1
113 #include <ftdi.h>
114 #endif
115
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK        30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
122
123 /*
124  * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125  * errors with a retry count of 100. Increasing it solves the problem for me.
126  *      - Dimitar
127  *
128  * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129  * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
130  * to something sane.
131  */
132 #define LIBFTDI_READ_RETRY_COUNT                2000
133
134 #ifndef BUILD_FT2232_HIGHSPEED
135  #if BUILD_FT2232_FTD2XX == 1
136         enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
137  #elif BUILD_FT2232_LIBFTDI == 1
138         enum { TYPE_2232H = 4, TYPE_4232H = 5 };
139  #endif
140 #endif
141
142 /**
143  * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144  * stable state.  Calling code must ensure that current state is stable,
145  * that verification is not done in here.
146  *
147  * @param num_cycles The number of clocks cycles to send.
148  * @param cmd The command to send.
149  *
150  * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
151  */
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
153
154 static char *       ft2232_device_desc_A = NULL;
155 static char*        ft2232_device_desc = NULL;
156 static char*        ft2232_serial  = NULL;
157 static uint8_t          ft2232_latency = 2;
158 static unsigned         ft2232_max_tck = FTDI_2232C_MAX_TCK;
159
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
164
165 struct ft2232_layout {
166         char* name;
167         int (*init)(void);
168         void (*reset)(int trst, int srst);
169         void (*blink)(void);
170         int channel;
171 };
172
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter_init(void);
180 static int minimodule_init(void);
181 static int turtle_init(void);
182 static int comstick_init(void);
183 static int stm32stick_init(void);
184 static int axm0432_jtag_init(void);
185 static int sheevaplug_init(void);
186 static int icebear_jtag_init(void);
187 static int cortino_jtag_init(void);
188 static int signalyzer_init(void);
189 static int signalyzer_h_init(void);
190 static int ktlink_init(void);
191 static int redbee_init(void);
192 static int lisa_l_init(void);
193 static int flossjtag_init(void);
194 static int xds100v2_init(void);
195
196 /* reset procedures for supported layouts */
197 static void ftx23_reset(int trst, int srst);
198 static void jtagkey_reset(int trst, int srst);
199 static void olimex_jtag_reset(int trst, int srst);
200 static void flyswatter_reset(int trst, int srst);
201 static void minimodule_reset(int trst, int srst);
202 static void turtle_reset(int trst, int srst);
203 static void comstick_reset(int trst, int srst);
204 static void stm32stick_reset(int trst, int srst);
205 static void axm0432_jtag_reset(int trst, int srst);
206 static void sheevaplug_reset(int trst, int srst);
207 static void icebear_jtag_reset(int trst, int srst);
208 static void signalyzer_h_reset(int trst, int srst);
209 static void ktlink_reset(int trst, int srst);
210 static void redbee_reset(int trst, int srst);
211 static void xds100v2_reset(int trst, int srst);
212
213 /* blink procedures for layouts that support a blinking led */
214 static void olimex_jtag_blink(void);
215 static void flyswatter_jtag_blink(void);
216 static void turtle_jtag_blink(void);
217 static void signalyzer_h_blink(void);
218 static void ktlink_blink(void);
219 static void lisa_l_blink(void);
220 static void flossjtag_blink(void);
221
222 /* common transport support options */
223
224 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
225
226 static const struct ft2232_layout  ft2232_layouts[] =
227 {
228         { .name = "usbjtag",
229                 .init = usbjtag_init,
230                 .reset = ftx23_reset,
231         },
232         { .name = "jtagkey",
233                 .init = jtagkey_init,
234                 .reset = jtagkey_reset,
235         },
236         { .name = "jtagkey_prototype_v1",
237                 .init = jtagkey_init,
238                 .reset = jtagkey_reset,
239         },
240         { .name = "oocdlink",
241                 .init = jtagkey_init,
242                 .reset = jtagkey_reset,
243         },
244         { .name = "signalyzer",
245                 .init = signalyzer_init,
246                 .reset = ftx23_reset,
247         },
248         { .name = "evb_lm3s811",
249                 .init = lm3s811_jtag_init,
250                 .reset = ftx23_reset,
251         },
252         { .name = "luminary_icdi",
253                 .init = icdi_jtag_init,
254                 .reset = ftx23_reset,
255         },
256         { .name = "olimex-jtag",
257                 .init = olimex_jtag_init,
258                 .reset = olimex_jtag_reset,
259                 .blink = olimex_jtag_blink
260         },
261         { .name = "flyswatter",
262                 .init = flyswatter_init,
263                 .reset = flyswatter_reset,
264                 .blink = flyswatter_jtag_blink
265         },
266         { .name = "minimodule",
267                 .init = minimodule_init,
268                 .reset = minimodule_reset,
269         },
270         { .name = "turtelizer2",
271                 .init = turtle_init,
272                 .reset = turtle_reset,
273                 .blink = turtle_jtag_blink
274         },
275         { .name = "comstick",
276                 .init = comstick_init,
277                 .reset = comstick_reset,
278         },
279         { .name = "stm32stick",
280                 .init = stm32stick_init,
281                 .reset = stm32stick_reset,
282         },
283         { .name = "axm0432_jtag",
284                 .init = axm0432_jtag_init,
285                 .reset = axm0432_jtag_reset,
286         },
287         { .name = "sheevaplug",
288                 .init = sheevaplug_init,
289                 .reset = sheevaplug_reset,
290         },
291         { .name = "icebear",
292                 .init = icebear_jtag_init,
293                 .reset = icebear_jtag_reset,
294         },
295         { .name = "cortino",
296                 .init = cortino_jtag_init,
297                 .reset = comstick_reset,
298         },
299         { .name = "signalyzer-h",
300                 .init = signalyzer_h_init,
301                 .reset = signalyzer_h_reset,
302                 .blink = signalyzer_h_blink
303         },
304         { .name = "ktlink",
305                 .init = ktlink_init,
306                 .reset = ktlink_reset,
307                 .blink = ktlink_blink
308         },
309         { .name = "redbee-econotag",
310                 .init = redbee_init,
311                 .reset = redbee_reset,
312         },
313         { .name = "redbee-usb",
314                 .init = redbee_init,
315                 .reset = redbee_reset,
316                 .channel = INTERFACE_B,
317         },
318         { .name = "lisa-l",
319                 .init = lisa_l_init,
320                 .reset = ftx23_reset,
321                 .blink = lisa_l_blink,
322                 .channel = INTERFACE_B,
323         },
324         { .name = "flossjtag",
325                 .init = flossjtag_init,
326                 .reset = ftx23_reset,
327                 .blink = flossjtag_blink,
328         },
329         { .name = "xds100v2",
330                 .init = xds100v2_init,
331                 .reset = xds100v2_reset,
332         },
333         { .name = NULL, /* END OF TABLE */ },
334 };
335
336 /* bitmask used to drive nTRST; usually a GPIOLx signal */
337 static uint8_t                  nTRST;
338 static uint8_t                  nTRSTnOE;
339 /* bitmask used to drive nSRST; usually a GPIOLx signal */
340 static uint8_t                  nSRST;
341 static uint8_t                  nSRSTnOE;
342
343 /** the layout being used with this debug session */
344 static const struct ft2232_layout *layout;
345
346 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
347 static uint8_t                  low_output     = 0x0;
348
349 /* note that direction bit == 1 means that signal is an output */
350
351 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
352 static uint8_t                  low_direction  = 0x0;
353 /** default value bitmask for CBUS GPIOH(0..4) */
354 static uint8_t                  high_output    = 0x0;
355 /** default direction bitmask for CBUS GPIOH(0..4) */
356 static uint8_t                  high_direction = 0x0;
357
358 #if BUILD_FT2232_FTD2XX == 1
359 static FT_HANDLE        ftdih = NULL;
360 static FT_DEVICE        ftdi_device = 0;
361 #elif BUILD_FT2232_LIBFTDI == 1
362 static struct ftdi_context ftdic;
363 static enum ftdi_chip_type ftdi_device;
364 #endif
365
366 static struct jtag_command* first_unsent;        /* next command that has to be sent */
367 static int             require_send;
368
369 /*      http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
370
371         "There is a significant difference between libftdi and libftd2xx. The latter
372         one allows to schedule up to 64*64 bytes of result data while libftdi fails
373         with more than 4*64. As a consequence, the FT2232 driver is forced to
374         perform around 16x more USB transactions for long command streams with TDO
375         capture when running with libftdi."
376
377         No idea how we get
378         #define FT2232_BUFFER_SIZE 131072
379         a comment would have been nice.
380 */
381
382 #if BUILD_FT2232_FTD2XX == 1
383 #define FT2232_BUFFER_READ_QUEUE_SIZE   (64*64)
384 #else
385 #define FT2232_BUFFER_READ_QUEUE_SIZE   (64*4)
386 #endif
387
388 #define FT2232_BUFFER_SIZE 131072
389
390 static uint8_t*             ft2232_buffer = NULL;
391 static int             ft2232_buffer_size  = 0;
392 static int             ft2232_read_pointer = 0;
393 static int             ft2232_expect_read  = 0;
394
395 /**
396  * Function buffer_write
397  * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
398  * @param val is the byte to send.
399  */
400 static inline void buffer_write(uint8_t val)
401 {
402         assert(ft2232_buffer);
403         assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
404         ft2232_buffer[ft2232_buffer_size++] = val;
405 }
406
407 /**
408  * Function buffer_read
409  * returns a byte from the byte buffer.
410  */
411 static inline uint8_t buffer_read(void)
412 {
413         assert(ft2232_buffer);
414         assert(ft2232_read_pointer < ft2232_buffer_size);
415         return ft2232_buffer[ft2232_read_pointer++];
416 }
417
418 /**
419  * Clocks out \a bit_count bits on the TMS line, starting with the least
420  * significant bit of tms_bits and progressing to more significant bits.
421  * Rigorous state transition logging is done here via tap_set_state().
422  *
423  * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
424  *      0x4b or 0x6b.  See the MPSSE spec referenced above for their
425  *      functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
426  *      is often used for this, 0x4b.
427  *
428  * @param tms_bits Holds the sequence of bits to send.
429  * @param tms_count Tells how many bits in the sequence.
430  * @param tdi_bit A single bit to pass on to TDI before the first TCK
431  *      cycle and held static for the duration of TMS clocking.
432  *
433  * See the MPSSE spec referenced above.
434  */
435 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
436 {
437         uint8_t tms_byte;
438         int     i;
439         int     tms_ndx;                                /* bit index into tms_byte */
440
441         assert(tms_count > 0);
442
443         DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
444                         mpsse_cmd, tms_bits, tms_count);
445
446         for (tms_byte = tms_ndx = i = 0;   i < tms_count;   ++i, tms_bits>>=1)
447         {
448                 bool bit = tms_bits & 1;
449
450                 if (bit)
451                         tms_byte |= (1 << tms_ndx);
452
453                 /* always do state transitions in public view */
454                 tap_set_state(tap_state_transition(tap_get_state(), bit));
455
456                 /*      we wrote a bit to tms_byte just above, increment bit index.  if bit was zero
457                         also increment.
458                 */
459                 ++tms_ndx;
460
461                 if (tms_ndx == 7  || i == tms_count-1)
462                 {
463                         buffer_write(mpsse_cmd);
464                         buffer_write(tms_ndx - 1);
465
466                         /*      Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
467                                 TMS/CS and is held static for the duration of TMS/CS clocking.
468                         */
469                         buffer_write(tms_byte | (tdi_bit << 7));
470                 }
471         }
472 }
473
474 /**
475  * Function get_tms_buffer_requirements
476  * returns what clock_tms() will consume if called with
477  * same \a bit_count.
478  */
479 static inline int get_tms_buffer_requirements(int bit_count)
480 {
481         return ((bit_count + 6)/7) * 3;
482 }
483
484 /**
485  * Function move_to_state
486  * moves the TAP controller from the current state to a
487  * \a goal_state through a path given by tap_get_tms_path().  State transition
488  * logging is performed by delegation to clock_tms().
489  *
490  * @param goal_state is the destination state for the move.
491  */
492 static void move_to_state(tap_state_t goal_state)
493 {
494         tap_state_t     start_state = tap_get_state();
495
496         /*      goal_state is 1/2 of a tuple/pair of states which allow convenient
497                 lookup of the required TMS pattern to move to this state from the
498                 start state.
499         */
500
501         /* do the 2 lookups */
502         int tms_bits  = tap_get_tms_path(start_state, goal_state);
503         int tms_count = tap_get_tms_path_len(start_state, goal_state);
504
505         DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
506
507         clock_tms(0x4b,  tms_bits, tms_count, 0);
508 }
509
510 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
511 {
512 #if BUILD_FT2232_FTD2XX == 1
513         FT_STATUS status;
514         DWORD dw_bytes_written = 0;
515         if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
516         {
517                 *bytes_written = dw_bytes_written;
518                 LOG_ERROR("FT_Write returned: %lu", status);
519                 return ERROR_JTAG_DEVICE_ERROR;
520         }
521         else
522         {
523                 *bytes_written = dw_bytes_written;
524         }
525 #elif BUILD_FT2232_LIBFTDI == 1
526         int retval;
527         if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
528         {
529                 *bytes_written = 0;
530                 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
531                 return ERROR_JTAG_DEVICE_ERROR;
532         }
533         else
534         {
535                 *bytes_written = retval;
536         }
537 #endif
538
539         if (*bytes_written != (uint32_t)size)
540         {
541                 return ERROR_JTAG_DEVICE_ERROR;
542         }
543
544         return ERROR_OK;
545 }
546
547 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
548 {
549 #if BUILD_FT2232_FTD2XX == 1
550         DWORD dw_bytes_read;
551         FT_STATUS status;
552         int timeout = 5;
553         *bytes_read = 0;
554
555         while ((*bytes_read < size) && timeout--)
556         {
557                 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
558                                           *bytes_read, &dw_bytes_read)) != FT_OK)
559                 {
560                         *bytes_read = 0;
561                         LOG_ERROR("FT_Read returned: %lu", status);
562                         return ERROR_JTAG_DEVICE_ERROR;
563                 }
564                 *bytes_read += dw_bytes_read;
565         }
566
567 #elif BUILD_FT2232_LIBFTDI == 1
568         int retval;
569         int timeout = LIBFTDI_READ_RETRY_COUNT;
570         *bytes_read = 0;
571
572         while ((*bytes_read < size) && timeout--)
573         {
574                 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
575                 {
576                         *bytes_read = 0;
577                         LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
578                         return ERROR_JTAG_DEVICE_ERROR;
579                 }
580                 *bytes_read += retval;
581         }
582
583 #endif
584
585         if (*bytes_read < size)
586         {
587                 LOG_ERROR("couldn't read enough bytes from "
588                                 "FT2232 device (%i < %i)",
589                                 (unsigned)*bytes_read,
590                                 (unsigned)size);
591                 return ERROR_JTAG_DEVICE_ERROR;
592         }
593
594         return ERROR_OK;
595 }
596
597 static bool ft2232_device_is_highspeed(void)
598 {
599 #if BUILD_FT2232_FTD2XX == 1
600         return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
601 #elif BUILD_FT2232_LIBFTDI == 1
602         return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
603 #endif
604 }
605
606 /*
607  * Commands that only apply to the FT2232H and FT4232H devices.
608  * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
609  * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
610  */
611
612 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
613 {
614         uint8_t buf = enable ? 0x96 : 0x97;
615         LOG_DEBUG("%2.2x", buf);
616
617         uint32_t bytes_written;
618         int retval;
619
620         if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
621         {
622                 LOG_ERROR("couldn't write command to %s adaptive clocking"
623                         , enable ? "enable" : "disable");
624                 return retval;
625         }
626
627         return ERROR_OK;
628 }
629
630 /**
631  * Enable/disable the clk divide by 5 of the 60MHz master clock.
632  * This result in a JTAG clock speed range of 91.553Hz-6MHz
633  * respective 457.763Hz-30MHz.
634  */
635 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
636 {
637         uint32_t bytes_written;
638         uint8_t buf = enable ?  0x8b : 0x8a;
639
640         if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
641         {
642                 LOG_ERROR("couldn't write command to %s clk divide by 5"
643                         , enable ? "enable" : "disable");
644                 return ERROR_JTAG_INIT_FAILED;
645         }
646         ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
647         LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
648
649         return ERROR_OK;
650 }
651
652 static int ft2232_speed(int speed)
653 {
654         uint8_t buf[3];
655         int retval;
656         uint32_t bytes_written;
657
658         retval = ERROR_OK;
659         bool enable_adaptive_clocking = (RTCK_SPEED == speed);
660         if (ft2232_device_is_highspeed())
661                 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
662         else if (enable_adaptive_clocking)
663         {
664                 LOG_ERROR("ft2232 device %lu does not support RTCK"
665                         , (long unsigned int)ftdi_device);
666                 return ERROR_FAIL;
667         }
668
669         if ((enable_adaptive_clocking) || (ERROR_OK != retval))
670                 return retval;
671
672         buf[0] = 0x86;                                  /* command "set divisor" */
673         buf[1] = speed & 0xff;                  /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
674         buf[2] = (speed >> 8) & 0xff;   /* valueH */
675
676         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
677         if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
678         {
679                 LOG_ERROR("couldn't set FT2232 TCK speed");
680                 return retval;
681         }
682
683         return ERROR_OK;
684 }
685
686 static int ft2232_speed_div(int speed, int* khz)
687 {
688         /* Take a look in the FT2232 manual,
689          * AN2232C-01 Command Processor for
690          * MPSSE and MCU Host Bus. Chapter 3.8 */
691
692         *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
693
694         return ERROR_OK;
695 }
696
697 static int ft2232_khz(int khz, int* jtag_speed)
698 {
699         if (khz == 0)
700         {
701                 if (ft2232_device_is_highspeed())
702                 {
703                         *jtag_speed = RTCK_SPEED;
704                         return ERROR_OK;
705                 }
706                 else
707                 {
708                         LOG_DEBUG("RCLK not supported");
709                         return ERROR_FAIL;
710                 }
711         }
712
713         /* Take a look in the FT2232 manual,
714          * AN2232C-01 Command Processor for
715          * MPSSE and MCU Host Bus. Chapter 3.8
716          *
717          * We will calc here with a multiplier
718          * of 10 for better rounding later. */
719
720         /* Calc speed, (ft2232_max_tck / khz) - 1 */
721         /* Use 65000 for better rounding */
722         *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
723
724         /* Add 0.9 for rounding */
725         *jtag_speed += 9;
726
727         /* Calc real speed */
728         *jtag_speed = *jtag_speed / 10;
729
730         /* Check if speed is greater than 0 */
731         if (*jtag_speed < 0)
732         {
733                 *jtag_speed = 0;
734         }
735
736         /* Check max value */
737         if (*jtag_speed > 0xFFFF)
738         {
739                 *jtag_speed = 0xFFFF;
740         }
741
742         return ERROR_OK;
743 }
744
745 static void ft2232_end_state(tap_state_t state)
746 {
747         if (tap_is_state_stable(state))
748                 tap_set_end_state(state);
749         else
750         {
751                 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
752                 exit(-1);
753         }
754 }
755
756 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
757 {
758         int num_bytes = (scan_size + 7) / 8;
759         int bits_left = scan_size;
760         int cur_byte  = 0;
761
762         while (num_bytes-- > 1)
763         {
764                 buffer[cur_byte++] = buffer_read();
765                 bits_left -= 8;
766         }
767
768         buffer[cur_byte] = 0x0;
769
770         /* There is one more partial byte left from the clock data in/out instructions */
771         if (bits_left > 1)
772         {
773                 buffer[cur_byte] = buffer_read() >> 1;
774         }
775         /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
776         buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
777 }
778
779 static void ft2232_debug_dump_buffer(void)
780 {
781         int i;
782         char line[256];
783         char* line_p = line;
784
785         for (i = 0; i < ft2232_buffer_size; i++)
786         {
787                 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
788                 if (i % 16 == 15)
789                 {
790                         LOG_DEBUG("%s", line);
791                         line_p = line;
792                 }
793         }
794
795         if (line_p != line)
796                 LOG_DEBUG("%s", line);
797 }
798
799 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
800 {
801         struct jtag_command* cmd;
802         uint8_t* buffer;
803         int scan_size;
804         enum scan_type  type;
805         int retval;
806         uint32_t bytes_written = 0;
807         uint32_t bytes_read = 0;
808
809 #ifdef _DEBUG_USB_IO_
810         struct timeval  start, inter, inter2, end;
811         struct timeval  d_inter, d_inter2, d_end;
812 #endif
813
814 #ifdef _DEBUG_USB_COMMS_
815         LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
816         ft2232_debug_dump_buffer();
817 #endif
818
819 #ifdef _DEBUG_USB_IO_
820         gettimeofday(&start, NULL);
821 #endif
822
823         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
824         {
825                 LOG_ERROR("couldn't write MPSSE commands to FT2232");
826                 return retval;
827         }
828
829 #ifdef _DEBUG_USB_IO_
830         gettimeofday(&inter, NULL);
831 #endif
832
833         if (ft2232_expect_read)
834         {
835                 /* FIXME this "timeout" is never changed ... */
836                 int timeout = LIBFTDI_READ_RETRY_COUNT;
837                 ft2232_buffer_size = 0;
838
839 #ifdef _DEBUG_USB_IO_
840                 gettimeofday(&inter2, NULL);
841 #endif
842
843                 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
844                 {
845                         LOG_ERROR("couldn't read from FT2232");
846                         return retval;
847                 }
848
849 #ifdef _DEBUG_USB_IO_
850                 gettimeofday(&end, NULL);
851
852                 timeval_subtract(&d_inter, &inter, &start);
853                 timeval_subtract(&d_inter2, &inter2, &start);
854                 timeval_subtract(&d_end, &end, &start);
855
856                 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
857                         (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
858                         (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
859                         (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
860 #endif
861
862                 ft2232_buffer_size = bytes_read;
863
864                 if (ft2232_expect_read != ft2232_buffer_size)
865                 {
866                         LOG_ERROR("ft2232_expect_read (%i) != "
867                                         "ft2232_buffer_size (%i) "
868                                         "(%i retries)",
869                                         ft2232_expect_read,
870                                         ft2232_buffer_size,
871                                         LIBFTDI_READ_RETRY_COUNT - timeout);
872                         ft2232_debug_dump_buffer();
873
874                         exit(-1);
875                 }
876
877 #ifdef _DEBUG_USB_COMMS_
878                 LOG_DEBUG("read buffer (%i retries): %i bytes",
879                                 LIBFTDI_READ_RETRY_COUNT - timeout,
880                                 ft2232_buffer_size);
881                 ft2232_debug_dump_buffer();
882 #endif
883         }
884
885         ft2232_expect_read  = 0;
886         ft2232_read_pointer = 0;
887
888         /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
889          * that wasn't handled by a caller-provided error handler
890          */
891         retval = ERROR_OK;
892
893         cmd = first;
894         while (cmd != last)
895         {
896                 switch (cmd->type)
897                 {
898                 case JTAG_SCAN:
899                         type = jtag_scan_type(cmd->cmd.scan);
900                         if (type != SCAN_OUT)
901                         {
902                                 scan_size = jtag_scan_size(cmd->cmd.scan);
903                                 buffer    = calloc(DIV_ROUND_UP(scan_size, 8), 1);
904                                 ft2232_read_scan(type, buffer, scan_size);
905                                 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
906                                         retval = ERROR_JTAG_QUEUE_FAILED;
907                                 free(buffer);
908                         }
909                         break;
910
911                 default:
912                         break;
913                 }
914
915                 cmd = cmd->next;
916         }
917
918         ft2232_buffer_size = 0;
919
920         return retval;
921 }
922
923 /**
924  * Function ft2232_add_pathmove
925  * moves the TAP controller from the current state to a new state through the
926  * given path, where path is an array of tap_state_t's.
927  *
928  * @param path is an array of tap_stat_t which gives the states to traverse through
929  *   ending with the last state at path[num_states-1]
930  * @param num_states is the count of state steps to move through
931  */
932 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
933 {
934         int state_count = 0;
935
936         assert((unsigned) num_states <= 32u);           /* tms_bits only holds 32 bits */
937
938         DEBUG_JTAG_IO("-");
939
940         /* this loop verifies that the path is legal and logs each state in the path */
941         while (num_states)
942         {
943                 unsigned char   tms_byte = 0;       /* zero this on each MPSSE batch */
944                 int             bit_count = 0;
945                 int             num_states_batch = num_states > 7 ? 7 : num_states;
946
947                 /* command "Clock Data to TMS/CS Pin (no Read)" */
948                 buffer_write(0x4b);
949
950                 /* number of states remaining */
951                 buffer_write(num_states_batch - 1);
952
953                 while (num_states_batch--) {
954                         /* either TMS=0 or TMS=1 must work ... */
955                         if (tap_state_transition(tap_get_state(), false)
956                                                 == path[state_count])
957                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
958                         else if (tap_state_transition(tap_get_state(), true)
959                                                 == path[state_count])
960                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
961
962                         /* ... or else the caller goofed BADLY */
963                         else {
964                                 LOG_ERROR("BUG: %s -> %s isn't a valid "
965                                                 "TAP state transition",
966                                         tap_state_name(tap_get_state()),
967                                         tap_state_name(path[state_count]));
968                                 exit(-1);
969                         }
970
971                         tap_set_state(path[state_count]);
972                         state_count++;
973                         num_states--;
974                 }
975
976                 buffer_write(tms_byte);
977         }
978         tap_set_end_state(tap_get_state());
979 }
980
981 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
982 {
983         int num_bytes = (scan_size + 7) / 8;
984         int bits_left = scan_size;
985         int cur_byte  = 0;
986         int last_bit;
987
988         if (!ir_scan)
989         {
990                 if (tap_get_state() != TAP_DRSHIFT)
991                 {
992                         move_to_state(TAP_DRSHIFT);
993                 }
994         }
995         else
996         {
997                 if (tap_get_state() != TAP_IRSHIFT)
998                 {
999                         move_to_state(TAP_IRSHIFT);
1000                 }
1001         }
1002
1003         /* add command for complete bytes */
1004         while (num_bytes > 1)
1005         {
1006                 int thisrun_bytes;
1007                 if (type == SCAN_IO)
1008                 {
1009                         /* Clock Data Bytes In and Out LSB First */
1010                         buffer_write(0x39);
1011                         /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1012                 }
1013                 else if (type == SCAN_OUT)
1014                 {
1015                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1016                         buffer_write(0x19);
1017                         /* LOG_DEBUG("added TDI bytes (o)"); */
1018                 }
1019                 else if (type == SCAN_IN)
1020                 {
1021                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1022                         buffer_write(0x28);
1023                         /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1024                 }
1025
1026                 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1027                 num_bytes    -= thisrun_bytes;
1028
1029                 buffer_write((uint8_t) (thisrun_bytes - 1));
1030                 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1031
1032                 if (type != SCAN_IN)
1033                 {
1034                         /* add complete bytes */
1035                         while (thisrun_bytes-- > 0)
1036                         {
1037                                 buffer_write(buffer[cur_byte++]);
1038                                 bits_left -= 8;
1039                         }
1040                 }
1041                 else /* (type == SCAN_IN) */
1042                 {
1043                         bits_left -= 8 * (thisrun_bytes);
1044                 }
1045         }
1046
1047         /* the most signifcant bit is scanned during TAP movement */
1048         if (type != SCAN_IN)
1049                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1050         else
1051                 last_bit = 0;
1052
1053         /* process remaining bits but the last one */
1054         if (bits_left > 1)
1055         {
1056                 if (type == SCAN_IO)
1057                 {
1058                         /* Clock Data Bits In and Out LSB First */
1059                         buffer_write(0x3b);
1060                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1061                 }
1062                 else if (type == SCAN_OUT)
1063                 {
1064                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1065                         buffer_write(0x1b);
1066                         /* LOG_DEBUG("added TDI bits (o)"); */
1067                 }
1068                 else if (type == SCAN_IN)
1069                 {
1070                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1071                         buffer_write(0x2a);
1072                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1073                 }
1074
1075                 buffer_write(bits_left - 2);
1076                 if (type != SCAN_IN)
1077                         buffer_write(buffer[cur_byte]);
1078         }
1079
1080         if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1081           || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1082         {
1083                 if (type == SCAN_IO)
1084                 {
1085                         /* Clock Data Bits In and Out LSB First */
1086                         buffer_write(0x3b);
1087                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1088                 }
1089                 else if (type == SCAN_OUT)
1090                 {
1091                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1092                         buffer_write(0x1b);
1093                         /* LOG_DEBUG("added TDI bits (o)"); */
1094                 }
1095                 else if (type == SCAN_IN)
1096                 {
1097                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1098                         buffer_write(0x2a);
1099                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1100                 }
1101                 buffer_write(0x0);
1102                 buffer_write(last_bit);
1103         }
1104         else
1105         {
1106                 int tms_bits;
1107                 int tms_count;
1108                 uint8_t mpsse_cmd;
1109
1110                 /* move from Shift-IR/DR to end state */
1111                 if (type != SCAN_OUT)
1112                 {
1113                         /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1114                         /* This must be coordinated with the bit shifts in ft2232_read_scan    */
1115                         tms_bits  = 0x01;
1116                         tms_count = 2;
1117                         /* Clock Data to TMS/CS Pin with Read */
1118                         mpsse_cmd = 0x6b;
1119                 }
1120                 else
1121                 {
1122                         tms_bits  = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1123                         tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1124                         /* Clock Data to TMS/CS Pin (no Read) */
1125                         mpsse_cmd = 0x4b;
1126                 }
1127
1128                 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1129                 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1130         }
1131
1132         if (tap_get_state() != tap_get_end_state())
1133         {
1134                 move_to_state(tap_get_end_state());
1135         }
1136 }
1137
1138 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1139 {
1140         int num_bytes = (scan_size + 7) / 8;
1141         int bits_left = scan_size;
1142         int cur_byte  = 0;
1143         int last_bit;
1144         uint8_t* receive_buffer  = malloc(DIV_ROUND_UP(scan_size, 8));
1145         uint8_t* receive_pointer = receive_buffer;
1146         uint32_t bytes_written;
1147         uint32_t bytes_read;
1148         int retval;
1149         int thisrun_read = 0;
1150
1151         if (cmd->ir_scan)
1152         {
1153                 LOG_ERROR("BUG: large IR scans are not supported");
1154                 exit(-1);
1155         }
1156
1157         if (tap_get_state() != TAP_DRSHIFT)
1158         {
1159                 move_to_state(TAP_DRSHIFT);
1160         }
1161
1162         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1163         {
1164                 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1165                 exit(-1);
1166         }
1167         LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1168                   ft2232_buffer_size, (int)bytes_written);
1169         ft2232_buffer_size = 0;
1170
1171         /* add command for complete bytes */
1172         while (num_bytes > 1)
1173         {
1174                 int thisrun_bytes;
1175
1176                 if (type == SCAN_IO)
1177                 {
1178                         /* Clock Data Bytes In and Out LSB First */
1179                         buffer_write(0x39);
1180                         /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1181                 }
1182                 else if (type == SCAN_OUT)
1183                 {
1184                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1185                         buffer_write(0x19);
1186                         /* LOG_DEBUG("added TDI bytes (o)"); */
1187                 }
1188                 else if (type == SCAN_IN)
1189                 {
1190                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1191                         buffer_write(0x28);
1192                         /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1193                 }
1194
1195                 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1196                 thisrun_read  = thisrun_bytes;
1197                 num_bytes    -= thisrun_bytes;
1198                 buffer_write((uint8_t) (thisrun_bytes - 1));
1199                 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1200
1201                 if (type != SCAN_IN)
1202                 {
1203                         /* add complete bytes */
1204                         while (thisrun_bytes-- > 0)
1205                         {
1206                                 buffer_write(buffer[cur_byte]);
1207                                 cur_byte++;
1208                                 bits_left -= 8;
1209                         }
1210                 }
1211                 else /* (type == SCAN_IN) */
1212                 {
1213                         bits_left -= 8 * (thisrun_bytes);
1214                 }
1215
1216                 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1217                 {
1218                         LOG_ERROR("couldn't write MPSSE commands to FT2232");
1219                         exit(-1);
1220                 }
1221                 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1222                           ft2232_buffer_size,
1223                           (int)bytes_written);
1224                 ft2232_buffer_size = 0;
1225
1226                 if (type != SCAN_OUT)
1227                 {
1228                         if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1229                         {
1230                                 LOG_ERROR("couldn't read from FT2232");
1231                                 exit(-1);
1232                         }
1233                         LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1234                                   thisrun_read,
1235                                   (int)bytes_read);
1236                         receive_pointer += bytes_read;
1237                 }
1238         }
1239
1240         thisrun_read = 0;
1241
1242         /* the most signifcant bit is scanned during TAP movement */
1243         if (type != SCAN_IN)
1244                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1245         else
1246                 last_bit = 0;
1247
1248         /* process remaining bits but the last one */
1249         if (bits_left > 1)
1250         {
1251                 if (type == SCAN_IO)
1252                 {
1253                         /* Clock Data Bits In and Out LSB First */
1254                         buffer_write(0x3b);
1255                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1256                 }
1257                 else if (type == SCAN_OUT)
1258                 {
1259                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1260                         buffer_write(0x1b);
1261                         /* LOG_DEBUG("added TDI bits (o)"); */
1262                 }
1263                 else if (type == SCAN_IN)
1264                 {
1265                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1266                         buffer_write(0x2a);
1267                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1268                 }
1269                 buffer_write(bits_left - 2);
1270                 if (type != SCAN_IN)
1271                         buffer_write(buffer[cur_byte]);
1272
1273                 if (type != SCAN_OUT)
1274                         thisrun_read += 2;
1275         }
1276
1277         if (tap_get_end_state() == TAP_DRSHIFT)
1278         {
1279                 if (type == SCAN_IO)
1280                 {
1281                         /* Clock Data Bits In and Out LSB First */
1282                         buffer_write(0x3b);
1283                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1284                 }
1285                 else if (type == SCAN_OUT)
1286                 {
1287                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1288                         buffer_write(0x1b);
1289                         /* LOG_DEBUG("added TDI bits (o)"); */
1290                 }
1291                 else if (type == SCAN_IN)
1292                 {
1293                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1294                         buffer_write(0x2a);
1295                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1296                 }
1297                 buffer_write(0x0);
1298                 buffer_write(last_bit);
1299         }
1300         else
1301         {
1302                 int tms_bits  = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1303                 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1304                 uint8_t mpsse_cmd;
1305
1306                 /* move from Shift-IR/DR to end state */
1307                 if (type != SCAN_OUT)
1308                 {
1309                         /* Clock Data to TMS/CS Pin with Read */
1310                         mpsse_cmd = 0x6b;
1311                         /* LOG_DEBUG("added TMS scan (read)"); */
1312                 }
1313                 else
1314                 {
1315                         /* Clock Data to TMS/CS Pin (no Read) */
1316                         mpsse_cmd = 0x4b;
1317                         /* LOG_DEBUG("added TMS scan (no read)"); */
1318                 }
1319
1320                 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1321                 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1322         }
1323
1324         if (type != SCAN_OUT)
1325                 thisrun_read += 1;
1326
1327         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1328         {
1329                 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1330                 exit(-1);
1331         }
1332         LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1333                   ft2232_buffer_size,
1334                   (int)bytes_written);
1335         ft2232_buffer_size = 0;
1336
1337         if (type != SCAN_OUT)
1338         {
1339                 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1340                 {
1341                         LOG_ERROR("couldn't read from FT2232");
1342                         exit(-1);
1343                 }
1344                 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1345                           thisrun_read,
1346                           (int)bytes_read);
1347                 receive_pointer += bytes_read;
1348         }
1349
1350         return ERROR_OK;
1351 }
1352
1353 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1354 {
1355         int predicted_size = 3;
1356         int num_bytes = (scan_size - 1) / 8;
1357
1358         if (tap_get_state() != TAP_DRSHIFT)
1359                 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1360
1361         if (type == SCAN_IN)    /* only from device to host */
1362         {
1363                 /* complete bytes */
1364                 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1365
1366                 /* remaining bits - 1 (up to 7) */
1367                 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1368         }
1369         else    /* host to device, or bidirectional */
1370         {
1371                 /* complete bytes */
1372                 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1373
1374                 /* remaining bits -1 (up to 7) */
1375                 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1376         }
1377
1378         return predicted_size;
1379 }
1380
1381 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1382 {
1383         int predicted_size = 0;
1384
1385         if (type != SCAN_OUT)
1386         {
1387                 /* complete bytes */
1388                 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1389
1390                 /* remaining bits - 1 */
1391                 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1392
1393                 /* last bit (from TMS scan) */
1394                 predicted_size += 1;
1395         }
1396
1397         /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1398
1399         return predicted_size;
1400 }
1401
1402 /* semi-generic FT2232/FT4232 reset code */
1403 static void ftx23_reset(int trst, int srst)
1404 {
1405         enum reset_types jtag_reset_config = jtag_get_reset_config();
1406         if (trst == 1)
1407         {
1408                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1409                         low_direction |= nTRSTnOE;      /* switch to output pin (output is low) */
1410                 else
1411                         low_output &= ~nTRST;           /* switch output low */
1412         }
1413         else if (trst == 0)
1414         {
1415                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1416                         low_direction &= ~nTRSTnOE;     /* switch to input pin (high-Z + internal and external pullup) */
1417                 else
1418                         low_output |= nTRST;            /* switch output high */
1419         }
1420
1421         if (srst == 1)
1422         {
1423                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1424                         low_output &= ~nSRST;           /* switch output low */
1425                 else
1426                         low_direction |= nSRSTnOE;      /* switch to output pin (output is low) */
1427         }
1428         else if (srst == 0)
1429         {
1430                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1431                         low_output |= nSRST;            /* switch output high */
1432                 else
1433                         low_direction &= ~nSRSTnOE;     /* switch to input pin (high-Z) */
1434         }
1435
1436         /* command "set data bits low byte" */
1437         buffer_write(0x80);
1438         buffer_write(low_output);
1439         buffer_write(low_direction);
1440 }
1441
1442 static void jtagkey_reset(int trst, int srst)
1443 {
1444         enum reset_types jtag_reset_config = jtag_get_reset_config();
1445         if (trst == 1)
1446         {
1447                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1448                         high_output &= ~nTRSTnOE;
1449                 else
1450                         high_output &= ~nTRST;
1451         }
1452         else if (trst == 0)
1453         {
1454                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1455                         high_output |= nTRSTnOE;
1456                 else
1457                         high_output |= nTRST;
1458         }
1459
1460         if (srst == 1)
1461         {
1462                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1463                         high_output &= ~nSRST;
1464                 else
1465                         high_output &= ~nSRSTnOE;
1466         }
1467         else if (srst == 0)
1468         {
1469                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1470                         high_output |= nSRST;
1471                 else
1472                         high_output |= nSRSTnOE;
1473         }
1474
1475         /* command "set data bits high byte" */
1476         buffer_write(0x82);
1477         buffer_write(high_output);
1478         buffer_write(high_direction);
1479         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1480                         high_direction);
1481 }
1482
1483 static void olimex_jtag_reset(int trst, int srst)
1484 {
1485         enum reset_types jtag_reset_config = jtag_get_reset_config();
1486         if (trst == 1)
1487         {
1488                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1489                         high_output &= ~nTRSTnOE;
1490                 else
1491                         high_output &= ~nTRST;
1492         }
1493         else if (trst == 0)
1494         {
1495                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1496                         high_output |= nTRSTnOE;
1497                 else
1498                         high_output |= nTRST;
1499         }
1500
1501         if (srst == 1)
1502         {
1503                 high_output |= nSRST;
1504         }
1505         else if (srst == 0)
1506         {
1507                 high_output &= ~nSRST;
1508         }
1509
1510         /* command "set data bits high byte" */
1511         buffer_write(0x82);
1512         buffer_write(high_output);
1513         buffer_write(high_direction);
1514         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1515                         high_direction);
1516 }
1517
1518 static void axm0432_jtag_reset(int trst, int srst)
1519 {
1520         if (trst == 1)
1521         {
1522                 tap_set_state(TAP_RESET);
1523                 high_output &= ~nTRST;
1524         }
1525         else if (trst == 0)
1526         {
1527                 high_output |= nTRST;
1528         }
1529
1530         if (srst == 1)
1531         {
1532                 high_output &= ~nSRST;
1533         }
1534         else if (srst == 0)
1535         {
1536                 high_output |= nSRST;
1537         }
1538
1539         /* command "set data bits low byte" */
1540         buffer_write(0x82);
1541         buffer_write(high_output);
1542         buffer_write(high_direction);
1543         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1544                         high_direction);
1545 }
1546
1547 static void flyswatter_reset(int trst, int srst)
1548 {
1549         if (trst == 1)
1550         {
1551                 low_output &= ~nTRST;
1552         }
1553         else if (trst == 0)
1554         {
1555                 low_output |= nTRST;
1556         }
1557
1558         if (srst == 1)
1559         {
1560                 low_output |= nSRST;
1561         }
1562         else if (srst == 0)
1563         {
1564                 low_output &= ~nSRST;
1565         }
1566
1567         /* command "set data bits low byte" */
1568         buffer_write(0x80);
1569         buffer_write(low_output);
1570         buffer_write(low_direction);
1571         LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1572 }
1573
1574 static void minimodule_reset(int trst, int srst)
1575 {
1576         if (srst == 1)
1577         {
1578                 low_output &= ~nSRST;
1579         }
1580         else if (srst == 0)
1581         {
1582                 low_output |= nSRST;
1583         }
1584
1585         /* command "set data bits low byte" */
1586         buffer_write(0x80);
1587         buffer_write(low_output);
1588         buffer_write(low_direction);
1589         LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1590 }
1591
1592 static void turtle_reset(int trst, int srst)
1593 {
1594         trst = trst;
1595
1596         if (srst == 1)
1597         {
1598                 low_output |= nSRST;
1599         }
1600         else if (srst == 0)
1601         {
1602                 low_output &= ~nSRST;
1603         }
1604
1605         /* command "set data bits low byte" */
1606         buffer_write(0x80);
1607         buffer_write(low_output);
1608         buffer_write(low_direction);
1609         LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1610 }
1611
1612 static void comstick_reset(int trst, int srst)
1613 {
1614         if (trst == 1)
1615         {
1616                 high_output &= ~nTRST;
1617         }
1618         else if (trst == 0)
1619         {
1620                 high_output |= nTRST;
1621         }
1622
1623         if (srst == 1)
1624         {
1625                 high_output &= ~nSRST;
1626         }
1627         else if (srst == 0)
1628         {
1629                 high_output |= nSRST;
1630         }
1631
1632         /* command "set data bits high byte" */
1633         buffer_write(0x82);
1634         buffer_write(high_output);
1635         buffer_write(high_direction);
1636         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1637                         high_direction);
1638 }
1639
1640 static void stm32stick_reset(int trst, int srst)
1641 {
1642         if (trst == 1)
1643         {
1644                 high_output &= ~nTRST;
1645         }
1646         else if (trst == 0)
1647         {
1648                 high_output |= nTRST;
1649         }
1650
1651         if (srst == 1)
1652         {
1653                 low_output &= ~nSRST;
1654         }
1655         else if (srst == 0)
1656         {
1657                 low_output |= nSRST;
1658         }
1659
1660         /* command "set data bits low byte" */
1661         buffer_write(0x80);
1662         buffer_write(low_output);
1663         buffer_write(low_direction);
1664
1665         /* command "set data bits high byte" */
1666         buffer_write(0x82);
1667         buffer_write(high_output);
1668         buffer_write(high_direction);
1669         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1670                         high_direction);
1671 }
1672
1673 static void sheevaplug_reset(int trst, int srst)
1674 {
1675         if (trst == 1)
1676                 high_output &= ~nTRST;
1677         else if (trst == 0)
1678                 high_output |= nTRST;
1679
1680         if (srst == 1)
1681                 high_output &= ~nSRSTnOE;
1682         else if (srst == 0)
1683                 high_output |= nSRSTnOE;
1684
1685         /* command "set data bits high byte" */
1686         buffer_write(0x82);
1687         buffer_write(high_output);
1688         buffer_write(high_direction);
1689         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1690 }
1691
1692 static void redbee_reset(int trst, int srst)
1693 {
1694         if (trst == 1)
1695         {
1696                 tap_set_state(TAP_RESET);
1697                 high_output &= ~nTRST;
1698         }
1699         else if (trst == 0)
1700         {
1701                 high_output |= nTRST;
1702         }
1703
1704         if (srst == 1)
1705         {
1706                 high_output &= ~nSRST;
1707         }
1708         else if (srst == 0)
1709         {
1710                 high_output |= nSRST;
1711         }
1712
1713         /* command "set data bits low byte" */
1714         buffer_write(0x82);
1715         buffer_write(high_output);
1716         buffer_write(high_direction);
1717         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1718                         "high_direction: 0x%2.2x", trst, srst, high_output,
1719                         high_direction);
1720 }
1721
1722 static void xds100v2_reset(int trst, int srst)
1723 {
1724         if (trst == 1)
1725         {
1726                 tap_set_state(TAP_RESET);
1727                 high_output &= ~nTRST;
1728         }
1729         else if (trst == 0)
1730         {
1731                 high_output |= nTRST;
1732         }
1733
1734         if (srst == 1)
1735         {
1736                 high_output |= nSRST;
1737         }
1738         else if (srst == 0)
1739         {
1740                 high_output &= ~nSRST;
1741         }
1742
1743         /* command "set data bits low byte" */
1744         buffer_write(0x82);
1745         buffer_write(high_output);
1746         buffer_write(high_direction);
1747         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1748                         "high_direction: 0x%2.2x", trst, srst, high_output,
1749                         high_direction);
1750 }
1751
1752 static int ft2232_execute_runtest(struct jtag_command *cmd)
1753 {
1754         int retval;
1755         int i;
1756         int predicted_size = 0;
1757         retval = ERROR_OK;
1758
1759         DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1760                         cmd->cmd.runtest->num_cycles,
1761                         tap_state_name(cmd->cmd.runtest->end_state));
1762
1763         /* only send the maximum buffer size that FT2232C can handle */
1764         predicted_size = 0;
1765         if (tap_get_state() != TAP_IDLE)
1766                 predicted_size += 3;
1767         predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1768         if (cmd->cmd.runtest->end_state != TAP_IDLE)
1769                 predicted_size += 3;
1770         if (tap_get_end_state() != TAP_IDLE)
1771                 predicted_size += 3;
1772         if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1773         {
1774                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1775                         retval = ERROR_JTAG_QUEUE_FAILED;
1776                 require_send = 0;
1777                 first_unsent = cmd;
1778         }
1779         if (tap_get_state() != TAP_IDLE)
1780         {
1781                 move_to_state(TAP_IDLE);
1782                 require_send = 1;
1783         }
1784         i = cmd->cmd.runtest->num_cycles;
1785         while (i > 0)
1786         {
1787                 /* there are no state transitions in this code, so omit state tracking */
1788
1789                 /* command "Clock Data to TMS/CS Pin (no Read)" */
1790                 buffer_write(0x4b);
1791
1792                 /* scan 7 bits */
1793                 buffer_write((i > 7) ? 6 : (i - 1));
1794
1795                 /* TMS data bits */
1796                 buffer_write(0x0);
1797
1798                 i -= (i > 7) ? 7 : i;
1799                 /* LOG_DEBUG("added TMS scan (no read)"); */
1800         }
1801
1802         ft2232_end_state(cmd->cmd.runtest->end_state);
1803
1804         if (tap_get_state() != tap_get_end_state())
1805         {
1806                 move_to_state(tap_get_end_state());
1807         }
1808
1809         require_send = 1;
1810         DEBUG_JTAG_IO("runtest: %i, end in %s",
1811                         cmd->cmd.runtest->num_cycles,
1812                         tap_state_name(tap_get_end_state()));
1813         return retval;
1814 }
1815
1816 static int ft2232_execute_statemove(struct jtag_command *cmd)
1817 {
1818         int     predicted_size = 0;
1819         int     retval = ERROR_OK;
1820
1821         DEBUG_JTAG_IO("statemove end in %s",
1822                         tap_state_name(cmd->cmd.statemove->end_state));
1823
1824         /* only send the maximum buffer size that FT2232C can handle */
1825         predicted_size = 3;
1826         if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1827         {
1828                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1829                         retval = ERROR_JTAG_QUEUE_FAILED;
1830                 require_send = 0;
1831                 first_unsent = cmd;
1832         }
1833         ft2232_end_state(cmd->cmd.statemove->end_state);
1834
1835         /* For TAP_RESET, ignore the current recorded state.  It's often
1836          * wrong at server startup, and this transation is critical whenever
1837          * it's requested.
1838          */
1839         if (tap_get_end_state() == TAP_RESET) {
1840                 clock_tms(0x4b,  0xff, 5, 0);
1841                 require_send = 1;
1842
1843         /* shortest-path move to desired end state */
1844         } else if (tap_get_state() != tap_get_end_state())
1845         {
1846                 move_to_state(tap_get_end_state());
1847                 require_send = 1;
1848         }
1849
1850         return retval;
1851 }
1852
1853 /**
1854  * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1855  * (or SWD) state machine.
1856  */
1857 static int ft2232_execute_tms(struct jtag_command *cmd)
1858 {
1859         int             retval = ERROR_OK;
1860         unsigned        num_bits = cmd->cmd.tms->num_bits;
1861         const uint8_t   *bits = cmd->cmd.tms->bits;
1862         unsigned        count;
1863
1864         DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1865
1866         /* only send the maximum buffer size that FT2232C can handle */
1867         count = 3 * DIV_ROUND_UP(num_bits, 4);
1868         if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1869                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1870                         retval = ERROR_JTAG_QUEUE_FAILED;
1871
1872                 require_send = 0;
1873                 first_unsent = cmd;
1874         }
1875
1876         /* Shift out in batches of at most 6 bits; there's a report of an
1877          * FT2232 bug in this area, where shifting exactly 7 bits can make
1878          * problems with TMS signaling for the last clock cycle:
1879          *
1880          *    http://developer.intra2net.com/mailarchive/html/
1881          *              libftdi/2009/msg00292.html
1882          *
1883          * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1884          *
1885          * Note that pathmoves in JTAG are not often seven bits, so that
1886          * isn't a particularly likely situation outside of "special"
1887          * signaling such as switching between JTAG and SWD modes.
1888          */
1889         while (num_bits) {
1890                 if (num_bits <= 6) {
1891                         buffer_write(0x4b);
1892                         buffer_write(num_bits - 1);
1893                         buffer_write(*bits & 0x3f);
1894                         break;
1895                 }
1896
1897                 /* Yes, this is lazy ... we COULD shift out more data
1898                  * bits per operation, but doing it in nybbles is easy
1899                  */
1900                 buffer_write(0x4b);
1901                 buffer_write(3);
1902                 buffer_write(*bits & 0xf);
1903                 num_bits -= 4;
1904
1905                 count  = (num_bits > 4) ? 4 : num_bits;
1906
1907                 buffer_write(0x4b);
1908                 buffer_write(count - 1);
1909                 buffer_write((*bits >> 4) & 0xf);
1910                 num_bits -= count;
1911
1912                 bits++;
1913         }
1914
1915         require_send = 1;
1916         return retval;
1917 }
1918
1919 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1920 {
1921         int     predicted_size = 0;
1922         int     retval = ERROR_OK;
1923
1924         tap_state_t*     path = cmd->cmd.pathmove->path;
1925         int     num_states    = cmd->cmd.pathmove->num_states;
1926
1927         DEBUG_JTAG_IO("pathmove: %i states, current: %s  end: %s", num_states,
1928                         tap_state_name(tap_get_state()),
1929                         tap_state_name(path[num_states-1]));
1930
1931         /* only send the maximum buffer size that FT2232C can handle */
1932         predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1933         if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1934         {
1935                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1936                         retval = ERROR_JTAG_QUEUE_FAILED;
1937
1938                 require_send = 0;
1939                 first_unsent = cmd;
1940         }
1941
1942         ft2232_add_pathmove(path, num_states);
1943         require_send = 1;
1944
1945         return retval;
1946 }
1947
1948 static int ft2232_execute_scan(struct jtag_command *cmd)
1949 {
1950         uint8_t* buffer;
1951         int scan_size;                          /* size of IR or DR scan */
1952         int predicted_size = 0;
1953         int retval = ERROR_OK;
1954
1955         enum scan_type  type = jtag_scan_type(cmd->cmd.scan);
1956
1957         DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1958
1959         scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1960
1961         predicted_size = ft2232_predict_scan_out(scan_size, type);
1962         if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1963         {
1964                 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1965                 /* unsent commands before this */
1966                 if (first_unsent != cmd)
1967                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1968                                 retval = ERROR_JTAG_QUEUE_FAILED;
1969
1970                 /* current command */
1971                 ft2232_end_state(cmd->cmd.scan->end_state);
1972                 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1973                 require_send = 0;
1974                 first_unsent = cmd->next;
1975                 if (buffer)
1976                         free(buffer);
1977                 return retval;
1978         }
1979         else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1980         {
1981                 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1982                                 first_unsent,
1983                                 cmd);
1984                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1985                         retval = ERROR_JTAG_QUEUE_FAILED;
1986                 require_send = 0;
1987                 first_unsent = cmd;
1988         }
1989         ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1990         /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1991         ft2232_end_state(cmd->cmd.scan->end_state);
1992         ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1993         require_send = 1;
1994         if (buffer)
1995                 free(buffer);
1996         DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1997                         (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1998                         tap_state_name(tap_get_end_state()));
1999         return retval;
2000
2001 }
2002
2003 static int ft2232_execute_reset(struct jtag_command *cmd)
2004 {
2005         int retval;
2006         int predicted_size = 0;
2007         retval = ERROR_OK;
2008
2009         DEBUG_JTAG_IO("reset trst: %i srst %i",
2010                         cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2011
2012         /* only send the maximum buffer size that FT2232C can handle */
2013         predicted_size = 3;
2014         if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
2015         {
2016                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2017                         retval = ERROR_JTAG_QUEUE_FAILED;
2018                 require_send = 0;
2019                 first_unsent = cmd;
2020         }
2021
2022         if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
2023         {
2024                 tap_set_state(TAP_RESET);
2025         }
2026
2027         layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2028         require_send = 1;
2029
2030         DEBUG_JTAG_IO("trst: %i, srst: %i",
2031                         cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2032         return retval;
2033 }
2034
2035 static int ft2232_execute_sleep(struct jtag_command *cmd)
2036 {
2037         int retval;
2038         retval = ERROR_OK;
2039
2040         DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2041
2042         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2043                                 retval = ERROR_JTAG_QUEUE_FAILED;
2044         first_unsent = cmd->next;
2045         jtag_sleep(cmd->cmd.sleep->us);
2046         DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2047                         cmd->cmd.sleep->us,
2048                         tap_state_name(tap_get_state()));
2049         return retval;
2050 }
2051
2052 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2053 {
2054         int retval;
2055         retval = ERROR_OK;
2056
2057         /* this is only allowed while in a stable state.  A check for a stable
2058          * state was done in jtag_add_clocks()
2059          */
2060         if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2061                 retval = ERROR_JTAG_QUEUE_FAILED;
2062         DEBUG_JTAG_IO("clocks %i while in %s",
2063                         cmd->cmd.stableclocks->num_cycles,
2064                         tap_state_name(tap_get_state()));
2065         return retval;
2066 }
2067
2068 static int ft2232_execute_command(struct jtag_command *cmd)
2069 {
2070         int retval;
2071
2072         switch (cmd->type)
2073         {
2074         case JTAG_RESET:        retval = ft2232_execute_reset(cmd); break;
2075         case JTAG_RUNTEST:      retval = ft2232_execute_runtest(cmd); break;
2076         case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2077         case JTAG_PATHMOVE:     retval = ft2232_execute_pathmove(cmd); break;
2078         case JTAG_SCAN:         retval = ft2232_execute_scan(cmd); break;
2079         case JTAG_SLEEP:        retval = ft2232_execute_sleep(cmd); break;
2080         case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2081         case JTAG_TMS:
2082                 retval = ft2232_execute_tms(cmd);
2083                 break;
2084         default:
2085                 LOG_ERROR("BUG: unknown JTAG command type encountered");
2086                 retval = ERROR_JTAG_QUEUE_FAILED;
2087                 break;
2088         }
2089         return retval;
2090 }
2091
2092 static int ft2232_execute_queue(void)
2093 {
2094         struct jtag_command* cmd = jtag_command_queue;  /* currently processed command */
2095         int retval;
2096
2097         first_unsent = cmd;             /* next command that has to be sent */
2098         require_send = 0;
2099
2100         /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2101          * that wasn't handled by a caller-provided error handler
2102          */
2103         retval = ERROR_OK;
2104
2105         ft2232_buffer_size = 0;
2106         ft2232_expect_read = 0;
2107
2108         /* blink, if the current layout has that feature */
2109         if (layout->blink)
2110                 layout->blink();
2111
2112         while (cmd)
2113         {
2114                 /* fill the write buffer with the desired command */
2115                 if (ft2232_execute_command(cmd) != ERROR_OK)
2116                         retval = ERROR_JTAG_QUEUE_FAILED;
2117                 /* Start reading input before FT2232 TX buffer fills up.
2118                  * Sometimes this happens because we don't know the
2119                  * length of the last command before we execute it. So
2120                  * we simple inform the user.
2121                  */
2122                 cmd = cmd->next;
2123
2124                 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE )
2125                 {
2126                         if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1) )
2127                                 LOG_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read,(FT2232_BUFFER_READ_QUEUE_SIZE+1));
2128                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2129                                 retval = ERROR_JTAG_QUEUE_FAILED;
2130                         first_unsent = cmd;
2131                 }
2132         }
2133
2134         if (require_send > 0)
2135                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2136                         retval = ERROR_JTAG_QUEUE_FAILED;
2137
2138         return retval;
2139 }
2140
2141 #if BUILD_FT2232_FTD2XX == 1
2142 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2143 {
2144         FT_STATUS       status;
2145         DWORD           deviceID;
2146         char            SerialNumber[16];
2147         char            Description[64];
2148         DWORD   openex_flags  = 0;
2149         char*   openex_string = NULL;
2150         uint8_t latency_timer;
2151
2152         if (layout == NULL) {
2153                 LOG_WARNING("No ft2232 layout specified'");
2154                 return ERROR_JTAG_INIT_FAILED;
2155         }
2156
2157         LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2158
2159 #if IS_WIN32 == 0
2160         /* Add non-standard Vid/Pid to the linux driver */
2161         if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2162         {
2163                 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2164         }
2165 #endif
2166
2167         if (ft2232_device_desc && ft2232_serial)
2168         {
2169                 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2170                 ft2232_device_desc = NULL;
2171         }
2172
2173         if (ft2232_device_desc)
2174         {
2175                 openex_string = ft2232_device_desc;
2176                 openex_flags  = FT_OPEN_BY_DESCRIPTION;
2177         }
2178         else if (ft2232_serial)
2179         {
2180                 openex_string = ft2232_serial;
2181                 openex_flags  = FT_OPEN_BY_SERIAL_NUMBER;
2182         }
2183         else
2184         {
2185                 LOG_ERROR("neither device description nor serial number specified");
2186                 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2187
2188                 return ERROR_JTAG_INIT_FAILED;
2189         }
2190
2191         status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2192         if (status != FT_OK) {
2193                 /* under Win32, the FTD2XX driver appends an "A" to the end
2194                  * of the description, if we tried by the desc, then
2195                  * try by the alternate "A" description. */
2196                 if (openex_string == ft2232_device_desc) {
2197                         /* Try the alternate method. */
2198                         openex_string = ft2232_device_desc_A;
2199                         status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2200                         if (status == FT_OK) {
2201                                 /* yea, the "alternate" method worked! */
2202                         } else {
2203                                 /* drat, give the user a meaningfull message.
2204                                  * telling the use we tried *BOTH* methods. */
2205                                 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2206                                                         ft2232_device_desc,
2207                                                         ft2232_device_desc_A);
2208                         }
2209                 }
2210         }
2211
2212         if (status != FT_OK)
2213         {
2214                 DWORD num_devices;
2215
2216                 if (more)
2217                 {
2218                         LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2219                         *try_more = 1;
2220                         return ERROR_JTAG_INIT_FAILED;
2221                 }
2222                 LOG_ERROR("unable to open ftdi device: %lu", status);
2223                 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2224                 if (status == FT_OK)
2225                 {
2226                         char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2227                         uint32_t i;
2228
2229                         for (i = 0; i < num_devices; i++)
2230                                 desc_array[i] = malloc(64);
2231
2232                         desc_array[num_devices] = NULL;
2233
2234                         status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2235
2236                         if (status == FT_OK)
2237                         {
2238                                 LOG_ERROR("ListDevices: %lu", num_devices);
2239                                 for (i = 0; i < num_devices; i++)
2240                                         LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2241                         }
2242
2243                         for (i = 0; i < num_devices; i++)
2244                                 free(desc_array[i]);
2245
2246                         free(desc_array);
2247                 }
2248                 else
2249                 {
2250                         LOG_ERROR("ListDevices: NONE");
2251                 }
2252                 return ERROR_JTAG_INIT_FAILED;
2253         }
2254
2255         if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2256         {
2257                 LOG_ERROR("unable to set latency timer: %lu", status);
2258                 return ERROR_JTAG_INIT_FAILED;
2259         }
2260
2261         if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2262         {
2263                 LOG_ERROR("unable to get latency timer: %lu", status);
2264                 return ERROR_JTAG_INIT_FAILED;
2265         }
2266         else
2267         {
2268                 LOG_DEBUG("current latency timer: %i", latency_timer);
2269         }
2270
2271         if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2272         {
2273                 LOG_ERROR("unable to set timeouts: %lu", status);
2274                 return ERROR_JTAG_INIT_FAILED;
2275         }
2276
2277         if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2278         {
2279                 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2280                 return ERROR_JTAG_INIT_FAILED;
2281         }
2282
2283         if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2284         {
2285                 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2286                 return ERROR_JTAG_INIT_FAILED;
2287         }
2288         else
2289         {
2290                 static const char* type_str[] =
2291                         {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2292                 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2293                 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2294                         ? ftdi_device : FT_DEVICE_UNKNOWN;
2295                 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2296                 LOG_INFO("deviceID: %lu", deviceID);
2297                 LOG_INFO("SerialNumber: %s", SerialNumber);
2298                 LOG_INFO("Description: %s", Description);
2299         }
2300
2301         return ERROR_OK;
2302 }
2303
2304 static int ft2232_purge_ftd2xx(void)
2305 {
2306         FT_STATUS status;
2307
2308         if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2309         {
2310                 LOG_ERROR("error purging ftd2xx device: %lu", status);
2311                 return ERROR_JTAG_INIT_FAILED;
2312         }
2313
2314         return ERROR_OK;
2315 }
2316
2317 #endif /* BUILD_FT2232_FTD2XX == 1 */
2318
2319 #if BUILD_FT2232_LIBFTDI == 1
2320 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2321 {
2322         uint8_t latency_timer;
2323
2324         if (layout == NULL) {
2325                 LOG_WARNING("No ft2232 layout specified'");
2326                 return ERROR_JTAG_INIT_FAILED;
2327         }
2328
2329         LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2330                         layout->name, vid, pid);
2331
2332         if (ftdi_init(&ftdic) < 0)
2333                 return ERROR_JTAG_INIT_FAILED;
2334
2335         /* default to INTERFACE_A */
2336         if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2337
2338         if (ftdi_set_interface(&ftdic, channel) < 0)
2339         {
2340                 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2341                 return ERROR_JTAG_INIT_FAILED;
2342         }
2343
2344         /* context, vendor id, product id */
2345         if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2346                                 ft2232_serial) < 0)
2347         {
2348                 if (more)
2349                         LOG_WARNING("unable to open ftdi device (trying more): %s",
2350                                         ftdic.error_str);
2351                 else
2352                         LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2353                 *try_more = 1;
2354                 return ERROR_JTAG_INIT_FAILED;
2355         }
2356
2357         /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2358         if (ftdi_usb_reset(&ftdic) < 0)
2359         {
2360                 LOG_ERROR("unable to reset ftdi device");
2361                 return ERROR_JTAG_INIT_FAILED;
2362         }
2363
2364         if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2365         {
2366                 LOG_ERROR("unable to set latency timer");
2367                 return ERROR_JTAG_INIT_FAILED;
2368         }
2369
2370         if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2371         {
2372                 LOG_ERROR("unable to get latency timer");
2373                 return ERROR_JTAG_INIT_FAILED;
2374         }
2375         else
2376         {
2377                 LOG_DEBUG("current latency timer: %i", latency_timer);
2378         }
2379
2380         ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2381
2382         ftdi_device = ftdic.type;
2383         static const char* type_str[] =
2384                 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2385         unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2386         unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2387                 ? ftdi_device : no_of_known_types;
2388         LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2389         return ERROR_OK;
2390 }
2391
2392 static int ft2232_purge_libftdi(void)
2393 {
2394         if (ftdi_usb_purge_buffers(&ftdic) < 0)
2395         {
2396                 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2397                 return ERROR_JTAG_INIT_FAILED;
2398         }
2399
2400         return ERROR_OK;
2401 }
2402
2403 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2404
2405 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2406 {
2407         uint8_t  buf[3];
2408         uint32_t bytes_written;
2409
2410         buf[0] = 0x80;          /* command "set data bits low byte" */
2411         buf[1] = value;         /* value */
2412         buf[2] = direction;     /* direction */
2413
2414         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2415
2416         if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2417         {
2418                 LOG_ERROR("couldn't initialize data bits low byte");
2419                 return ERROR_JTAG_INIT_FAILED;
2420         }
2421
2422         return ERROR_OK;
2423 }
2424
2425 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2426 {
2427         uint8_t  buf[3];
2428         uint32_t bytes_written;
2429
2430         buf[0] = 0x82;          /* command "set data bits high byte" */
2431         buf[1] = value;         /* value */
2432         buf[2] = direction;     /* direction */
2433
2434         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2435
2436         if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2437         {
2438                 LOG_ERROR("couldn't initialize data bits high byte");
2439                 return ERROR_JTAG_INIT_FAILED;
2440         }
2441
2442         return ERROR_OK;
2443 }
2444
2445 static int ft2232_init(void)
2446 {
2447         uint8_t  buf[1];
2448         int retval;
2449         uint32_t bytes_written;
2450
2451         if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2452         {
2453                 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2454         }
2455         else
2456         {
2457                 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2458
2459         }
2460         if (layout == NULL) {
2461                 LOG_WARNING("No ft2232 layout specified'");
2462                 return ERROR_JTAG_INIT_FAILED;
2463         }
2464
2465         for (int i = 0; 1; i++)
2466         {
2467                 /*
2468                  * "more indicates that there are more IDs to try, so we should
2469                  * not print an error for an ID mismatch (but for anything
2470                  * else, we should).
2471                  *
2472                  * try_more indicates that the error code returned indicates an
2473                  * ID mismatch (and nothing else) and that we should proceeed
2474                  * with the next ID pair.
2475                  */
2476                 int more     = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2477                 int try_more = 0;
2478
2479 #if BUILD_FT2232_FTD2XX == 1
2480                 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2481                                 more, &try_more);
2482 #elif BUILD_FT2232_LIBFTDI == 1
2483                 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2484                                              more, &try_more, layout->channel);
2485 #endif
2486                 if (retval >= 0)
2487                         break;
2488                 if (!more || !try_more)
2489                         return retval;
2490         }
2491
2492         ft2232_buffer_size = 0;
2493         ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2494
2495         if (layout->init() != ERROR_OK)
2496                 return ERROR_JTAG_INIT_FAILED;
2497
2498         if (ft2232_device_is_highspeed())
2499         {
2500 #ifndef BUILD_FT2232_HIGHSPEED
2501  #if BUILD_FT2232_FTD2XX == 1
2502                 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2503  #elif BUILD_FT2232_LIBFTDI == 1
2504                 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2505  #endif
2506 #endif
2507                 /* make sure the legacy mode is disabled */
2508                 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2509                         return ERROR_JTAG_INIT_FAILED;
2510         }
2511
2512         buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2513         if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2514         {
2515                 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2516                 return ERROR_JTAG_INIT_FAILED;
2517         }
2518
2519 #if BUILD_FT2232_FTD2XX == 1
2520         return ft2232_purge_ftd2xx();
2521 #elif BUILD_FT2232_LIBFTDI == 1
2522         return ft2232_purge_libftdi();
2523 #endif
2524
2525         return ERROR_OK;
2526 }
2527
2528 /** Updates defaults for DBUS signals:  the four JTAG signals
2529  * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2530  */
2531 static inline void ftx232_dbus_init(void)
2532 {
2533         low_output    = 0x08;
2534         low_direction = 0x0b;
2535 }
2536
2537 /** Initializes DBUS signals:  the four JTAG signals (TCK, TDI, TDO, TMS),
2538  * the four GPIOL signals.  Initialization covers value and direction,
2539  * as customized for each layout.
2540  */
2541 static int ftx232_dbus_write(void)
2542 {
2543         enum reset_types jtag_reset_config = jtag_get_reset_config();
2544         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2545         {
2546                 low_direction &= ~nTRSTnOE; /* nTRST input */
2547                 low_output    &= ~nTRST;    /* nTRST = 0 */
2548         }
2549         else
2550         {
2551                 low_direction |= nTRSTnOE;  /* nTRST output */
2552                 low_output    |= nTRST;     /* nTRST = 1 */
2553         }
2554
2555         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2556         {
2557                 low_direction |= nSRSTnOE;  /* nSRST output */
2558                 low_output    |= nSRST;     /* nSRST = 1 */
2559         }
2560         else
2561         {
2562                 low_direction &= ~nSRSTnOE; /* nSRST input */
2563                 low_output    &= ~nSRST;    /* nSRST = 0 */
2564         }
2565
2566         /* initialize low byte for jtag */
2567         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2568         {
2569                 LOG_ERROR("couldn't initialize FT2232 DBUS");
2570                 return ERROR_JTAG_INIT_FAILED;
2571         }
2572
2573         return ERROR_OK;
2574 }
2575
2576 static int usbjtag_init(void)
2577 {
2578         /*
2579          * NOTE:  This is now _specific_ to the "usbjtag" layout.
2580          * Don't try cram any more layouts into this.
2581          */
2582         ftx232_dbus_init();
2583
2584         nTRST    = 0x10;
2585         nTRSTnOE = 0x10;
2586         nSRST    = 0x40;
2587         nSRSTnOE = 0x40;
2588
2589         return ftx232_dbus_write();
2590 }
2591
2592 static int lm3s811_jtag_init(void)
2593 {
2594         ftx232_dbus_init();
2595
2596         /* There are multiple revisions of LM3S811 eval boards:
2597          * - Rev B (and older?) boards have no SWO trace support.
2598          * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2599          *   they should use the "luminary_icdi" layout instead.
2600          */
2601         nTRST = 0x0;
2602         nTRSTnOE = 0x00;
2603         nSRST = 0x20;
2604         nSRSTnOE = 0x20;
2605         low_output    = 0x88;
2606         low_direction = 0x8b;
2607
2608         return ftx232_dbus_write();
2609 }
2610
2611 static int icdi_jtag_init(void)
2612 {
2613         ftx232_dbus_init();
2614
2615         /* Most Luminary eval boards support SWO trace output,
2616          * and should use this "luminary_icdi" layout.
2617          *
2618          * ADBUS 0..3 are used for JTAG as usual.  GPIOs are used
2619          * to switch between JTAG and SWD, or switch the ft2232 UART
2620          * on the second MPSSE channel/interface (BDBUS)
2621          * between (i) the stellaris UART (on Luminary boards)
2622          * or (ii) SWO trace data (generic).
2623          *
2624          * We come up in JTAG mode and may switch to SWD later (with
2625          * SWO/trace option if SWD is active).
2626          *
2627          * DBUS == GPIO-Lx
2628          * CBUS == GPIO-Hx
2629          */
2630
2631
2632 #define ICDI_JTAG_EN (1 << 7)           /* ADBUS 7 (a.k.a. DBGMOD) */
2633 #define ICDI_DBG_ENn (1 << 6)           /* ADBUS 6 */
2634 #define ICDI_SRST (1 << 5)              /* ADBUS 5 */
2635
2636
2637         /* GPIOs on second channel/interface (UART) ... */
2638 #define ICDI_SWO_EN (1 << 4)            /* BDBUS 4 */
2639 #define ICDI_TX_SWO (1 << 1)            /* BDBUS 1 */
2640 #define ICDI_VCP_RX (1 << 0)            /* BDBUS 0 (to stellaris UART) */
2641
2642         nTRST = 0x0;
2643         nTRSTnOE = 0x00;
2644         nSRST = ICDI_SRST;
2645         nSRSTnOE = ICDI_SRST;
2646
2647         low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2648         low_output    |= ICDI_JTAG_EN;
2649         low_output    &= ~ICDI_DBG_ENn;
2650
2651         return ftx232_dbus_write();
2652 }
2653
2654 static int signalyzer_init(void)
2655 {
2656         ftx232_dbus_init();
2657
2658         nTRST    = 0x10;
2659         nTRSTnOE = 0x10;
2660         nSRST    = 0x20;
2661         nSRSTnOE = 0x20;
2662         return ftx232_dbus_write();
2663 }
2664
2665 static int axm0432_jtag_init(void)
2666 {
2667         low_output    = 0x08;
2668         low_direction = 0x2b;
2669
2670         /* initialize low byte for jtag */
2671         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2672         {
2673                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2674                 return ERROR_JTAG_INIT_FAILED;
2675         }
2676
2677         if (strcmp(layout->name, "axm0432_jtag") == 0)
2678         {
2679                 nTRST    = 0x08;
2680                 nTRSTnOE = 0x0;     /* No output enable for TRST*/
2681                 nSRST    = 0x04;
2682                 nSRSTnOE = 0x0;     /* No output enable for SRST*/
2683         }
2684         else
2685         {
2686                 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2687                 exit(-1);
2688         }
2689
2690         high_output    = 0x0;
2691         high_direction = 0x0c;
2692
2693         enum reset_types jtag_reset_config = jtag_get_reset_config();
2694         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2695         {
2696                 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2697         }
2698         else
2699         {
2700                 high_output |= nTRST;
2701         }
2702
2703         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2704         {
2705                 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2706         }
2707         else
2708         {
2709                 high_output |= nSRST;
2710         }
2711
2712         /* initialize high byte for jtag */
2713         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2714         {
2715                 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2716                 return ERROR_JTAG_INIT_FAILED;
2717         }
2718
2719         return ERROR_OK;
2720 }
2721
2722 static int redbee_init(void)
2723 {
2724         low_output    = 0x08;
2725         low_direction = 0x2b;
2726
2727         /* initialize low byte for jtag */
2728         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2729         {
2730                 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2731                 return ERROR_JTAG_INIT_FAILED;
2732         }
2733
2734         nTRST    = 0x08;
2735         nTRSTnOE = 0x0;     /* No output enable for TRST*/
2736         nSRST    = 0x04;
2737         nSRSTnOE = 0x0;     /* No output enable for SRST*/
2738
2739         high_output    = 0x0;
2740         high_direction = 0x0c;
2741
2742         enum reset_types jtag_reset_config = jtag_get_reset_config();
2743         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2744         {
2745                 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2746         }
2747         else
2748         {
2749                 high_output |= nTRST;
2750         }
2751
2752         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2753         {
2754                 LOG_ERROR("can't set nSRST to push-pull on redbee");
2755         }
2756         else
2757         {
2758                 high_output |= nSRST;
2759         }
2760
2761         /* initialize high byte for jtag */
2762         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2763         {
2764                 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2765                 return ERROR_JTAG_INIT_FAILED;
2766         }
2767
2768         return ERROR_OK;
2769 }
2770
2771 static int jtagkey_init(void)
2772 {
2773         low_output    = 0x08;
2774         low_direction = 0x1b;
2775
2776         /* initialize low byte for jtag */
2777         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2778         {
2779                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2780                 return ERROR_JTAG_INIT_FAILED;
2781         }
2782
2783         if (strcmp(layout->name, "jtagkey") == 0)
2784         {
2785                 nTRST    = 0x01;
2786                 nTRSTnOE = 0x4;
2787                 nSRST    = 0x02;
2788                 nSRSTnOE = 0x08;
2789         }
2790         else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2791                          || (strcmp(layout->name, "oocdlink") == 0))
2792         {
2793                 nTRST    = 0x02;
2794                 nTRSTnOE = 0x1;
2795                 nSRST    = 0x08;
2796                 nSRSTnOE = 0x04;
2797         }
2798         else
2799         {
2800                 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2801                 exit(-1);
2802         }
2803
2804         high_output    = 0x0;
2805         high_direction = 0x0f;
2806
2807         enum reset_types jtag_reset_config = jtag_get_reset_config();
2808         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2809         {
2810                 high_output |= nTRSTnOE;
2811                 high_output &= ~nTRST;
2812         }
2813         else
2814         {
2815                 high_output &= ~nTRSTnOE;
2816                 high_output |= nTRST;
2817         }
2818
2819         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2820         {
2821                 high_output &= ~nSRSTnOE;
2822                 high_output |= nSRST;
2823         }
2824         else
2825         {
2826                 high_output |= nSRSTnOE;
2827                 high_output &= ~nSRST;
2828         }
2829
2830         /* initialize high byte for jtag */
2831         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2832         {
2833                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2834                 return ERROR_JTAG_INIT_FAILED;
2835         }
2836
2837         return ERROR_OK;
2838 }
2839
2840 static int olimex_jtag_init(void)
2841 {
2842         low_output    = 0x08;
2843         low_direction = 0x1b;
2844
2845         /* initialize low byte for jtag */
2846         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2847         {
2848                 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2849                 return ERROR_JTAG_INIT_FAILED;
2850         }
2851
2852         nTRST    = 0x01;
2853         nTRSTnOE = 0x4;
2854         nSRST    = 0x02;
2855         nSRSTnOE = 0x00; /* no output enable for nSRST */
2856
2857         high_output    = 0x0;
2858         high_direction = 0x0f;
2859
2860         enum reset_types jtag_reset_config = jtag_get_reset_config();
2861         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2862         {
2863                 high_output |= nTRSTnOE;
2864                 high_output &= ~nTRST;
2865         }
2866         else
2867         {
2868                 high_output &= ~nTRSTnOE;
2869                 high_output |= nTRST;
2870         }
2871
2872         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2873         {
2874                 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2875         }
2876         else
2877         {
2878                 high_output &= ~nSRST;
2879         }
2880
2881         /* turn red LED on */
2882         high_output |= 0x08;
2883
2884         /* initialize high byte for jtag */
2885         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2886         {
2887                 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2888                 return ERROR_JTAG_INIT_FAILED;
2889         }
2890
2891         return ERROR_OK;
2892 }
2893
2894 static int flyswatter_init(void)
2895 {
2896         low_output    = 0x18;
2897         low_direction = 0xfb;
2898
2899         /* initialize low byte for jtag */
2900         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2901         {
2902                 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2903                 return ERROR_JTAG_INIT_FAILED;
2904         }
2905
2906         nTRST    = 0x10;
2907         nTRSTnOE = 0x0;     /* not output enable for nTRST */
2908         nSRST    = 0x20;
2909         nSRSTnOE = 0x00;    /* no output enable for nSRST */
2910
2911         high_output    = 0x00;
2912         high_direction = 0x0c;
2913
2914         /* turn red LED3 on, LED2 off */
2915         high_output |= 0x08;
2916
2917         /* initialize high byte for jtag */
2918         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2919         {
2920                 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2921                 return ERROR_JTAG_INIT_FAILED;
2922         }
2923
2924         return ERROR_OK;
2925 }
2926
2927 static int minimodule_init(void)
2928 {
2929         low_output    = 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18)
2930         low_direction = 0xfb;//0xfb;
2931
2932         /* initialize low byte for jtag */
2933         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2934         {
2935                 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2936                 return ERROR_JTAG_INIT_FAILED;
2937         }
2938
2939
2940         nSRST    = 0x20;
2941
2942         high_output    = 0x00;
2943         high_direction = 0x05;
2944
2945         /* turn red LED3 on, LED2 off */
2946         //high_output |= 0x08;
2947
2948         /* initialize high byte for jtag */
2949         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2950         {
2951                 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2952                 return ERROR_JTAG_INIT_FAILED;
2953         }
2954
2955         return ERROR_OK;
2956 }
2957
2958 static int turtle_init(void)
2959 {
2960         low_output    = 0x08;
2961         low_direction = 0x5b;
2962
2963         /* initialize low byte for jtag */
2964         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2965         {
2966                 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2967                 return ERROR_JTAG_INIT_FAILED;
2968         }
2969
2970         nSRST = 0x40;
2971
2972         high_output    = 0x00;
2973         high_direction = 0x0C;
2974
2975         /* initialize high byte for jtag */
2976         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2977         {
2978                 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2979                 return ERROR_JTAG_INIT_FAILED;
2980         }
2981
2982         return ERROR_OK;
2983 }
2984
2985 static int comstick_init(void)
2986 {
2987         low_output    = 0x08;
2988         low_direction = 0x0b;
2989
2990         /* initialize low byte for jtag */
2991         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2992         {
2993                 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2994                 return ERROR_JTAG_INIT_FAILED;
2995         }
2996
2997         nTRST    = 0x01;
2998         nTRSTnOE = 0x00;    /* no output enable for nTRST */
2999         nSRST    = 0x02;
3000         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3001
3002         high_output    = 0x03;
3003         high_direction = 0x03;
3004
3005         /* initialize high byte for jtag */
3006         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3007         {
3008                 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3009                 return ERROR_JTAG_INIT_FAILED;
3010         }
3011
3012         return ERROR_OK;
3013 }
3014
3015 static int stm32stick_init(void)
3016 {
3017         low_output    = 0x88;
3018         low_direction = 0x8b;
3019
3020         /* initialize low byte for jtag */
3021         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3022         {
3023                 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3024                 return ERROR_JTAG_INIT_FAILED;
3025         }
3026
3027         nTRST    = 0x01;
3028         nTRSTnOE = 0x00;    /* no output enable for nTRST */
3029         nSRST    = 0x80;
3030         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3031
3032         high_output    = 0x01;
3033         high_direction = 0x03;
3034
3035         /* initialize high byte for jtag */
3036         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3037         {
3038                 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3039                 return ERROR_JTAG_INIT_FAILED;
3040         }
3041
3042         return ERROR_OK;
3043 }
3044
3045 static int sheevaplug_init(void)
3046 {
3047         low_output = 0x08;
3048         low_direction = 0x1b;
3049
3050         /* initialize low byte for jtag */
3051         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3052         {
3053                 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3054                 return ERROR_JTAG_INIT_FAILED;
3055         }
3056
3057         nTRSTnOE = 0x1;
3058         nTRST = 0x02;
3059         nSRSTnOE = 0x4;
3060         nSRST = 0x08;
3061
3062         high_output = 0x0;
3063         high_direction = 0x0f;
3064
3065         /* nTRST is always push-pull */
3066         high_output &= ~nTRSTnOE;
3067         high_output |= nTRST;
3068
3069         /* nSRST is always open-drain */
3070         high_output |= nSRSTnOE;
3071         high_output &= ~nSRST;
3072
3073         /* initialize high byte for jtag */
3074         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3075         {
3076                 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3077                 return ERROR_JTAG_INIT_FAILED;
3078         }
3079
3080         return ERROR_OK;
3081 }
3082
3083 static int cortino_jtag_init(void)
3084 {
3085         low_output    = 0x08;
3086         low_direction = 0x1b;
3087
3088         /* initialize low byte for jtag */
3089         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3090         {
3091                 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3092                 return ERROR_JTAG_INIT_FAILED;
3093         }
3094
3095         nTRST    = 0x01;
3096         nTRSTnOE = 0x00;    /* no output enable for nTRST */
3097         nSRST    = 0x02;
3098         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3099
3100         high_output    = 0x03;
3101         high_direction = 0x03;
3102
3103         /* initialize high byte for jtag */
3104         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3105         {
3106                 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3107                 return ERROR_JTAG_INIT_FAILED;
3108         }
3109
3110         return ERROR_OK;
3111 }
3112
3113 static int lisa_l_init(void)
3114 {
3115         ftx232_dbus_init();
3116
3117         nTRST    = 0x10;
3118         nTRSTnOE = 0x10;
3119         nSRST    = 0x40;
3120         nSRSTnOE = 0x40;
3121
3122         high_output = 0x00;
3123         high_direction = 0x18;
3124
3125         /* initialize high byte for jtag */
3126         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3127         {
3128                 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3129                 return ERROR_JTAG_INIT_FAILED;
3130         }
3131
3132         return ftx232_dbus_write();
3133 }
3134
3135 static int flossjtag_init(void)
3136 {
3137         ftx232_dbus_init();
3138
3139         nTRST    = 0x10;
3140         nTRSTnOE = 0x10;
3141         nSRST    = 0x40;
3142         nSRSTnOE = 0x40;
3143
3144         high_output = 0x00;
3145         high_direction = 0x18;
3146
3147         /* initialize high byte for jtag */
3148         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3149         {
3150                 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3151                 return ERROR_JTAG_INIT_FAILED;
3152         }
3153
3154         return ftx232_dbus_write();
3155 }
3156
3157 static int xds100v2_init(void)
3158 {
3159         low_output    = 0x3A;
3160         low_direction = 0x7B;
3161
3162         /* initialize low byte for jtag */
3163         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3164         {
3165                 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3166                 return ERROR_JTAG_INIT_FAILED;
3167         }
3168
3169         nTRST    = 0x10;
3170         nTRSTnOE = 0x0;     /* not output enable for nTRST */
3171         nSRST    = 0x00;    /* TODO: SRST is not supported yet */
3172         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3173
3174         high_output    = 0x00;
3175         high_direction = 0x59;
3176
3177         /* initialize high byte for jtag */
3178         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3179         {
3180                 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3181                 return ERROR_JTAG_INIT_FAILED;
3182         }
3183
3184         high_output    = 0x86;
3185         high_direction = 0x59;
3186
3187         /* initialize high byte for jtag */
3188         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3189         {
3190                 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3191                 return ERROR_JTAG_INIT_FAILED;
3192         }
3193
3194         return ERROR_OK;
3195 }
3196
3197 static void olimex_jtag_blink(void)
3198 {
3199         /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3200          * ACBUS3 is bit 3 of the GPIOH port
3201          */
3202         high_output ^= 0x08;
3203
3204         buffer_write(0x82);
3205         buffer_write(high_output);
3206         buffer_write(high_direction);
3207 }
3208
3209 static void flyswatter_jtag_blink(void)
3210 {
3211         /*
3212          * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3213          */
3214         high_output ^= 0x0c;
3215
3216         buffer_write(0x82);
3217         buffer_write(high_output);
3218         buffer_write(high_direction);
3219 }
3220
3221 static void turtle_jtag_blink(void)
3222 {
3223         /*
3224          * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3225          */
3226         if (high_output & 0x08)
3227         {
3228                 high_output = 0x04;
3229         }
3230         else
3231         {
3232                 high_output = 0x08;
3233         }
3234
3235         buffer_write(0x82);
3236         buffer_write(high_output);
3237         buffer_write(high_direction);
3238 }
3239
3240 static void lisa_l_blink(void)
3241 {
3242         /*
3243          * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3244          */
3245         if (high_output & 0x10)
3246         {
3247                 high_output = 0x08;
3248         }
3249         else
3250         {
3251                 high_output = 0x10;
3252         }
3253
3254         buffer_write(0x82);
3255         buffer_write(high_output);
3256         buffer_write(high_direction);
3257 }
3258
3259 static void flossjtag_blink(void)
3260 {
3261         /*
3262          * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3263          */
3264         if (high_output & 0x10)
3265         {
3266                 high_output = 0x08;
3267         }
3268         else
3269         {
3270                 high_output = 0x10;
3271         }
3272
3273         buffer_write(0x82);
3274         buffer_write(high_output);
3275         buffer_write(high_direction);
3276 }
3277
3278 static int ft2232_quit(void)
3279 {
3280 #if BUILD_FT2232_FTD2XX == 1
3281         FT_STATUS status;
3282
3283         status = FT_Close(ftdih);
3284 #elif BUILD_FT2232_LIBFTDI == 1
3285         ftdi_usb_close(&ftdic);
3286
3287         ftdi_deinit(&ftdic);
3288 #endif
3289
3290         free(ft2232_buffer);
3291         ft2232_buffer = NULL;
3292
3293         return ERROR_OK;
3294 }
3295
3296 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3297 {
3298         char *cp;
3299         char buf[200];
3300         if (CMD_ARGC == 1)
3301         {
3302                 ft2232_device_desc = strdup(CMD_ARGV[0]);
3303                 cp = strchr(ft2232_device_desc, 0);
3304                 /* under Win32, the FTD2XX driver appends an "A" to the end
3305                  * of the description, this examines the given desc
3306                  * and creates the 'missing' _A or non_A variable. */
3307                 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3308                         /* it was, so make this the "A" version. */
3309                         ft2232_device_desc_A = ft2232_device_desc;
3310                         /* and *CREATE* the non-A version. */
3311                         strcpy(buf, ft2232_device_desc);
3312                         cp = strchr(buf, 0);
3313                         cp[-2] = 0;
3314                         ft2232_device_desc =  strdup(buf);
3315                 } else {
3316                         /* <space > A not defined
3317                          * so create it */
3318                         sprintf(buf, "%s A", ft2232_device_desc);
3319                         ft2232_device_desc_A = strdup(buf);
3320                 }
3321         }
3322         else
3323         {
3324                 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3325         }
3326
3327         return ERROR_OK;
3328 }
3329
3330 COMMAND_HANDLER(ft2232_handle_serial_command)
3331 {
3332         if (CMD_ARGC == 1)
3333         {
3334                 ft2232_serial = strdup(CMD_ARGV[0]);
3335         }
3336         else
3337         {
3338                 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3339         }
3340
3341         return ERROR_OK;
3342 }
3343
3344 COMMAND_HANDLER(ft2232_handle_layout_command)
3345 {
3346         if (CMD_ARGC != 1) {
3347                 LOG_ERROR("Need exactly one argument to ft2232_layout");
3348                 return ERROR_FAIL;
3349         }
3350
3351         if (layout) {
3352                 LOG_ERROR("already specified ft2232_layout %s",
3353                                 layout->name);
3354                 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3355                                 ? ERROR_FAIL
3356                                 : ERROR_OK;
3357         }
3358
3359         for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3360                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3361                         layout = l;
3362                         return ERROR_OK;
3363                 }
3364         }
3365
3366         LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3367         return ERROR_FAIL;
3368 }
3369
3370 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3371 {
3372         if (CMD_ARGC > MAX_USB_IDS * 2)
3373         {
3374                 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3375                                         "(maximum is %d pairs)", MAX_USB_IDS);
3376                 CMD_ARGC = MAX_USB_IDS * 2;
3377         }
3378         if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3379         {
3380                 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3381                 if (CMD_ARGC < 2)
3382                         return ERROR_COMMAND_SYNTAX_ERROR;
3383                 /* remove the incomplete trailing id */
3384                 CMD_ARGC -= 1;
3385         }
3386
3387         unsigned i;
3388         for (i = 0; i < CMD_ARGC; i += 2)
3389         {
3390                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3391                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3392         }
3393
3394         /*
3395          * Explicitly terminate, in case there are multiples instances of
3396          * ft2232_vid_pid.
3397          */
3398         ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3399
3400         return ERROR_OK;
3401 }
3402
3403 COMMAND_HANDLER(ft2232_handle_latency_command)
3404 {
3405         if (CMD_ARGC == 1)
3406         {
3407                 ft2232_latency = atoi(CMD_ARGV[0]);
3408         }
3409         else
3410         {
3411                 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3412         }
3413
3414         return ERROR_OK;
3415 }
3416
3417 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3418 {
3419         int retval = 0;
3420
3421         /* 7 bits of either ones or zeros. */
3422         uint8_t  tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3423
3424         while (num_cycles > 0)
3425         {
3426                 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3427                  * at most 7 bits per invocation.  Here we invoke it potentially
3428                  * several times.
3429                  */
3430                 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3431
3432                 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3433                 {
3434                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3435                                 retval = ERROR_JTAG_QUEUE_FAILED;
3436
3437                         first_unsent = cmd;
3438                 }
3439
3440                 /* there are no state transitions in this code, so omit state tracking */
3441
3442                 /* command "Clock Data to TMS/CS Pin (no Read)" */
3443                 buffer_write(0x4b);
3444
3445                 /* scan 7 bit */
3446                 buffer_write(bitcount_per_command - 1);
3447
3448                 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3449                 buffer_write(tms);
3450
3451                 require_send = 1;
3452
3453                 num_cycles -= bitcount_per_command;
3454         }
3455
3456         return retval;
3457 }
3458
3459 /* ---------------------------------------------------------------------
3460  * Support for IceBear JTAG adapter from Section5:
3461  *      http://section5.ch/icebear
3462  *
3463  * Author: Sten, debian@sansys-electronic.com
3464  */
3465
3466 /* Icebear pin layout
3467  *
3468  * ADBUS5 (nEMU) nSRST  | 2   1|        GND (10k->VCC)
3469  * GND GND              | 4   3|        n.c.
3470  * ADBUS3 TMS           | 6   5|        ADBUS6 VCC
3471  * ADBUS0 TCK           | 8   7|        ADBUS7 (GND)
3472  * ADBUS4 nTRST         |10   9|        ACBUS0 (GND)
3473  * ADBUS1 TDI           |12  11|        ACBUS1 (GND)
3474  * ADBUS2 TDO           |14  13|        GND GND
3475  *
3476  * ADBUS0 O L TCK               ACBUS0 GND
3477  * ADBUS1 O L TDI               ACBUS1 GND
3478  * ADBUS2 I   TDO               ACBUS2 n.c.
3479  * ADBUS3 O H TMS               ACBUS3 n.c.
3480  * ADBUS4 O H nTRST
3481  * ADBUS5 O H nSRST
3482  * ADBUS6 -   VCC
3483  * ADBUS7 -   GND
3484  */
3485 static int icebear_jtag_init(void) {
3486         low_direction   = 0x0b; /* output: TCK TDI TMS; input: TDO */
3487         low_output      = 0x08; /* high: TMS; low: TCK TDI */
3488         nTRST           = 0x10;
3489         nSRST           = 0x20;
3490
3491         enum reset_types jtag_reset_config = jtag_get_reset_config();
3492         if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3493                 low_direction   &= ~nTRST;      /* nTRST high impedance */
3494         }
3495         else {
3496                 low_direction   |= nTRST;
3497                 low_output      |= nTRST;
3498         }
3499
3500         low_direction   |= nSRST;
3501         low_output      |= nSRST;
3502
3503         /* initialize low byte for jtag */
3504         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3505                 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3506                 return ERROR_JTAG_INIT_FAILED;
3507         }
3508
3509         high_output    = 0x0;
3510         high_direction = 0x00;
3511
3512         /* initialize high byte for jtag */
3513         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3514                 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3515                 return ERROR_JTAG_INIT_FAILED;
3516         }
3517
3518         return ERROR_OK;
3519 }
3520
3521 static void icebear_jtag_reset(int trst, int srst) {
3522
3523         if (trst == 1) {
3524                 low_direction   |= nTRST;
3525                 low_output      &= ~nTRST;
3526         }
3527         else if (trst == 0) {
3528                 enum reset_types jtag_reset_config = jtag_get_reset_config();
3529                 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3530                         low_direction   &= ~nTRST;
3531                 else
3532                         low_output      |= nTRST;
3533         }
3534
3535         if (srst == 1) {
3536                 low_output &= ~nSRST;
3537         }
3538         else if (srst == 0) {
3539                 low_output |= nSRST;
3540         }
3541
3542         /* command "set data bits low byte" */
3543         buffer_write(0x80);
3544         buffer_write(low_output);
3545         buffer_write(low_direction);
3546
3547         LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3548 }
3549
3550 /* ---------------------------------------------------------------------
3551  * Support for Signalyzer H2 and Signalyzer H4
3552  * JTAG adapter from Xverve Technologies Inc.
3553  * http://www.signalyzer.com or http://www.xverve.com
3554  *
3555  * Author: Oleg Seiljus, oleg@signalyzer.com
3556  */
3557 static unsigned char signalyzer_h_side;
3558 static unsigned int signalyzer_h_adapter_type;
3559
3560 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3561
3562 #if BUILD_FT2232_FTD2XX == 1
3563 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3564 #endif
3565
3566 #define SIGNALYZER_COMMAND_ADDR                                 128
3567 #define SIGNALYZER_DATA_BUFFER_ADDR                             129
3568
3569 #define SIGNALYZER_COMMAND_VERSION                              0x41
3570 #define SIGNALYZER_COMMAND_RESET                                0x42
3571 #define SIGNALYZER_COMMAND_POWERCONTROL_GET             0x50
3572 #define SIGNALYZER_COMMAND_POWERCONTROL_SET             0x51
3573 #define SIGNALYZER_COMMAND_PWM_SET                              0x52
3574 #define SIGNALYZER_COMMAND_LED_SET                              0x53
3575 #define SIGNALYZER_COMMAND_ADC                                  0x54
3576 #define SIGNALYZER_COMMAND_GPIO_STATE                   0x55
3577 #define SIGNALYZER_COMMAND_GPIO_MODE                    0x56
3578 #define SIGNALYZER_COMMAND_GPIO_PORT                    0x57
3579 #define SIGNALYZER_COMMAND_I2C                                  0x58
3580
3581 #define SIGNALYZER_CHAN_A                                               1
3582 #define SIGNALYZER_CHAN_B                                               2
3583 /* LEDS use channel C */
3584 #define SIGNALYZER_CHAN_C                                               4
3585
3586 #define SIGNALYZER_LED_GREEN                                    1
3587 #define SIGNALYZER_LED_RED                                              2
3588
3589 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A                0x0301
3590 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG              0x0302
3591 #define SIGNALYZER_MODULE_TYPE_EM_JTAG                  0x0303
3592 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P    0x0304
3593 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P                0x0305
3594
3595
3596 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3597 {
3598 #if BUILD_FT2232_FTD2XX == 1
3599         return FT_WriteEE(ftdih, address, value);
3600 #elif BUILD_FT2232_LIBFTDI == 1
3601         return 0;
3602 #endif
3603 }
3604
3605 #if BUILD_FT2232_FTD2XX == 1
3606 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3607 {
3608         return FT_ReadEE(ftdih, address, value);
3609 }
3610 #endif
3611
3612 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3613         int on_time_ms, int off_time_ms, unsigned char cycles)
3614 {
3615         unsigned char on_time;
3616         unsigned char off_time;
3617
3618         if (on_time_ms < 0xFFFF)
3619                 on_time = (unsigned char)(on_time_ms / 62);
3620         else
3621                 on_time = 0xFF;
3622
3623         off_time = (unsigned char)(off_time_ms / 62);
3624
3625 #if BUILD_FT2232_FTD2XX == 1
3626         FT_STATUS status;
3627
3628         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3629                         ((uint32_t)(channel << 8) | led))) != FT_OK)
3630         {
3631                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %lu", status);
3632                 return ERROR_JTAG_DEVICE_ERROR;
3633         }
3634
3635         if ((status = signalyzer_h_ctrl_write(
3636                         (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3637                         ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3638         {
3639                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %lu", status);
3640                 return ERROR_JTAG_DEVICE_ERROR;
3641         }
3642
3643         if ((status = signalyzer_h_ctrl_write(
3644                         (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3645                         ((uint32_t)cycles))) != FT_OK)
3646         {
3647                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %lu", status);
3648                 return ERROR_JTAG_DEVICE_ERROR;
3649         }
3650
3651         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3652                         SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3653         {
3654                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %lu", status);
3655                 return ERROR_JTAG_DEVICE_ERROR;
3656         }
3657
3658         return ERROR_OK;
3659 #elif BUILD_FT2232_LIBFTDI == 1
3660         int retval;
3661
3662         if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3663                         ((uint32_t)(channel << 8) | led))) < 0)
3664         {
3665                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3666                                 ftdi_get_error_string(&ftdic));
3667                 return ERROR_JTAG_DEVICE_ERROR;
3668         }
3669
3670         if ((retval = signalyzer_h_ctrl_write(
3671                         (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3672                         ((uint32_t)(on_time << 8) | off_time))) < 0)
3673         {
3674                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3675                                 ftdi_get_error_string(&ftdic));
3676                 return ERROR_JTAG_DEVICE_ERROR;
3677         }
3678
3679         if ((retval = signalyzer_h_ctrl_write(
3680                         (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3681                         (uint32_t)cycles)) < 0)
3682         {
3683                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3684                                 ftdi_get_error_string(&ftdic));
3685                 return ERROR_JTAG_DEVICE_ERROR;
3686         }
3687
3688         if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3689                         SIGNALYZER_COMMAND_LED_SET)) < 0)
3690         {
3691                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3692                                 ftdi_get_error_string(&ftdic));
3693                 return ERROR_JTAG_DEVICE_ERROR;
3694         }
3695
3696         return ERROR_OK;
3697 #endif
3698 }
3699
3700 static int signalyzer_h_init(void)
3701 {
3702 #if BUILD_FT2232_FTD2XX == 1
3703         FT_STATUS status;
3704         int i;
3705 #endif
3706
3707         char *end_of_desc;
3708
3709         uint16_t read_buf[12] = { 0 };
3710
3711         /* turn on center green led */
3712         signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3713                         0xFFFF, 0x00, 0x00);
3714
3715         /* determine what channel config wants to open
3716          * TODO: change me... current implementation is made to work
3717          * with openocd description parsing.
3718          */
3719         end_of_desc = strrchr(ft2232_device_desc, 0x00);
3720
3721         if (end_of_desc)
3722         {
3723                 signalyzer_h_side = *(end_of_desc - 1);
3724                 if (signalyzer_h_side == 'B')
3725                         signalyzer_h_side = SIGNALYZER_CHAN_B;
3726                 else
3727                         signalyzer_h_side = SIGNALYZER_CHAN_A;
3728         }
3729         else
3730         {
3731                 LOG_ERROR("No Channel was specified");
3732                 return ERROR_FAIL;
3733         }
3734
3735         signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3736                         1000, 1000, 0xFF);
3737
3738 #if BUILD_FT2232_FTD2XX == 1
3739         /* read signalyzer versionining information */
3740         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3741                         SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3742         {
3743                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3744                 return ERROR_JTAG_DEVICE_ERROR;
3745         }
3746
3747         for (i = 0; i < 10; i++)
3748         {
3749                 if ((status = signalyzer_h_ctrl_read(
3750                         (SIGNALYZER_DATA_BUFFER_ADDR + i),
3751                         &read_buf[i])) != FT_OK)
3752                 {
3753                         LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3754                                         status);
3755                         return ERROR_JTAG_DEVICE_ERROR;
3756                 }
3757         }
3758
3759         LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3760                         read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3761                         read_buf[4], read_buf[5], read_buf[6]);
3762
3763         /* set gpio register */
3764         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3765                         (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3766         {
3767                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3768                 return ERROR_JTAG_DEVICE_ERROR;
3769         }
3770
3771         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3772                         0x0404)) != FT_OK)
3773         {
3774                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3775                 return ERROR_JTAG_DEVICE_ERROR;
3776         }
3777
3778         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3779                         SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3780         {
3781                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3782                 return ERROR_JTAG_DEVICE_ERROR;
3783         }
3784
3785         /* read adapter type information */
3786         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3787                         ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3788         {
3789                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3790                 return ERROR_JTAG_DEVICE_ERROR;
3791         }
3792
3793         if ((status = signalyzer_h_ctrl_write(
3794                         (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3795         {
3796                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3797                 return ERROR_JTAG_DEVICE_ERROR;
3798         }
3799
3800         if ((status = signalyzer_h_ctrl_write(
3801                         (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3802         {
3803                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3804                 return ERROR_JTAG_DEVICE_ERROR;
3805         }
3806
3807         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3808                         SIGNALYZER_COMMAND_I2C)) != FT_OK)
3809         {
3810                 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3811                 return ERROR_JTAG_DEVICE_ERROR;
3812         }
3813
3814         usleep(100000);
3815
3816         if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3817                         &read_buf[0])) != FT_OK)
3818         {
3819                 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3820                 return ERROR_JTAG_DEVICE_ERROR;
3821         }
3822
3823         if (read_buf[0] != 0x0498)
3824                 signalyzer_h_adapter_type = 0x0000;
3825         else
3826         {
3827                 for (i = 0; i < 4; i++)
3828                 {
3829                         if ((status = signalyzer_h_ctrl_read(
3830                                         (SIGNALYZER_DATA_BUFFER_ADDR + i),
3831                                         &read_buf[i])) != FT_OK)
3832                         {
3833                                 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3834                                         status);
3835                                 return ERROR_JTAG_DEVICE_ERROR;
3836                         }
3837                 }
3838
3839                 signalyzer_h_adapter_type = read_buf[0];
3840         }
3841
3842 #elif BUILD_FT2232_LIBFTDI == 1
3843         /* currently libftdi does not allow reading individual eeprom
3844          * locations, therefore adapter type cannot be detected.
3845          * override with most common type
3846          */
3847         signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3848 #endif
3849
3850         enum reset_types jtag_reset_config = jtag_get_reset_config();
3851
3852         /* ADAPTOR: EM_LT16_A */
3853         if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3854         {
3855                 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3856                         "detected. (HW: %2x).", (read_buf[1] >> 8));
3857
3858                 nTRST    = 0x10;
3859                 nTRSTnOE = 0x10;
3860                 nSRST    = 0x20;
3861                 nSRSTnOE = 0x20;
3862
3863                 low_output     = 0x08;
3864                 low_direction  = 0x1b;
3865
3866                 high_output    = 0x0;
3867                 high_direction = 0x0;
3868
3869                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3870                 {
3871                         low_direction &= ~nTRSTnOE; /* nTRST input */
3872                         low_output    &= ~nTRST;    /* nTRST = 0 */
3873                 }
3874                 else
3875                 {
3876                         low_direction |= nTRSTnOE;  /* nTRST output */
3877                         low_output    |= nTRST;     /* nTRST = 1 */
3878                 }
3879
3880                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3881                 {
3882                         low_direction |= nSRSTnOE;  /* nSRST output */
3883                         low_output    |= nSRST;     /* nSRST = 1 */
3884                 }
3885                 else
3886                 {
3887                         low_direction &= ~nSRSTnOE; /* nSRST input */
3888                         low_output    &= ~nSRST;    /* nSRST = 0 */
3889                 }
3890
3891 #if BUILD_FT2232_FTD2XX == 1
3892                 /* enable power to the module */
3893                 if ((status = signalyzer_h_ctrl_write(
3894                                 SIGNALYZER_DATA_BUFFER_ADDR,
3895                                 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3896                         != FT_OK)
3897                 {
3898                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3899                                 status);
3900                         return ERROR_JTAG_DEVICE_ERROR;
3901                 }
3902
3903                 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3904                                 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3905                 {
3906                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3907                                         status);
3908                         return ERROR_JTAG_DEVICE_ERROR;
3909                 }
3910
3911                 /* set gpio mode register */
3912                 if ((status = signalyzer_h_ctrl_write(
3913                                 SIGNALYZER_DATA_BUFFER_ADDR,
3914                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3915                 {
3916                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3917                                         status);
3918                         return ERROR_JTAG_DEVICE_ERROR;
3919                 }
3920
3921                 if ((status = signalyzer_h_ctrl_write(
3922                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3923                         != FT_OK)
3924                 {
3925                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3926                                         status);
3927                         return ERROR_JTAG_DEVICE_ERROR;
3928                 }
3929
3930                 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3931                                 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3932                 {
3933                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3934                                         status);
3935                         return ERROR_JTAG_DEVICE_ERROR;
3936                 }
3937
3938                 /* set gpio register */
3939                 if ((status = signalyzer_h_ctrl_write(
3940                                 SIGNALYZER_DATA_BUFFER_ADDR,
3941                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3942                 {
3943                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3944                                         status);
3945                         return ERROR_JTAG_DEVICE_ERROR;
3946                 }
3947
3948                 if ((status = signalyzer_h_ctrl_write(
3949                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3950                         != FT_OK)
3951                 {
3952                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3953                                         status);
3954                         return ERROR_JTAG_DEVICE_ERROR;
3955                 }
3956
3957                 if ((status = signalyzer_h_ctrl_write(
3958                                 SIGNALYZER_COMMAND_ADDR,
3959                                 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3960                 {
3961                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3962                                         status);
3963                         return ERROR_JTAG_DEVICE_ERROR;
3964                 }
3965 #endif
3966         }
3967
3968         /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3969         else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3970                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3971                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG)  ||
3972                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3973         {
3974                 if (signalyzer_h_adapter_type
3975                                 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3976                         LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3977                                 "detected. (HW: %2x).", (read_buf[1] >> 8));
3978                 else if (signalyzer_h_adapter_type
3979                                 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3980                         LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3981                                 "(ARM JTAG with PSU) detected. (HW: %2x).",
3982                                 (read_buf[1] >> 8));
3983                 else if (signalyzer_h_adapter_type
3984                                 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3985                         LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3986                                 "detected. (HW: %2x).", (read_buf[1] >> 8));
3987                 else if (signalyzer_h_adapter_type
3988                                 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3989                         LOG_INFO("Signalyzer: EM-JTAG-P "
3990                                 "(Generic JTAG with PSU) detected. (HW: %2x).",
3991                                 (read_buf[1] >> 8));
3992
3993                 nTRST          = 0x02;
3994                 nTRSTnOE       = 0x04;
3995                 nSRST          = 0x08;
3996                 nSRSTnOE       = 0x10;
3997
3998                 low_output     = 0x08;
3999                 low_direction  = 0x1b;
4000
4001                 high_output    = 0x0;
4002                 high_direction = 0x1f;
4003
4004                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4005                 {
4006                         high_output |= nTRSTnOE;
4007                         high_output &= ~nTRST;
4008                 }
4009                 else
4010                 {
4011                         high_output &= ~nTRSTnOE;
4012                         high_output |= nTRST;
4013                 }
4014
4015                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4016                 {
4017                         high_output &= ~nSRSTnOE;
4018                         high_output |= nSRST;
4019                 }
4020                 else
4021                 {
4022                         high_output |= nSRSTnOE;
4023                         high_output &= ~nSRST;
4024                 }
4025
4026 #if BUILD_FT2232_FTD2XX == 1
4027                 /* enable power to the module */
4028                 if ((status = signalyzer_h_ctrl_write(
4029                                 SIGNALYZER_DATA_BUFFER_ADDR,
4030                                 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4031                         != FT_OK)
4032                 {
4033                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4034                                         status);
4035                         return ERROR_JTAG_DEVICE_ERROR;
4036                 }
4037
4038                 if ((status = signalyzer_h_ctrl_write(
4039                                 SIGNALYZER_COMMAND_ADDR,
4040                                 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4041                 {
4042                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4043                                         status);
4044                         return ERROR_JTAG_DEVICE_ERROR;
4045                 }
4046
4047                 /* set gpio mode register (IO_16 and IO_17 set as analog
4048                  * inputs, other is gpio)
4049                  */
4050                 if ((status = signalyzer_h_ctrl_write(
4051                                 SIGNALYZER_DATA_BUFFER_ADDR,
4052                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4053                 {
4054                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4055                                         status);
4056                         return ERROR_JTAG_DEVICE_ERROR;
4057                 }
4058
4059                 if ((status = signalyzer_h_ctrl_write(
4060                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4061                         != FT_OK)
4062                 {
4063                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4064                                         status);
4065                         return ERROR_JTAG_DEVICE_ERROR;
4066                 }
4067
4068                 if ((status = signalyzer_h_ctrl_write(
4069                                 SIGNALYZER_COMMAND_ADDR,
4070                                 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4071                 {
4072                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4073                                         status);
4074                         return ERROR_JTAG_DEVICE_ERROR;
4075                 }
4076
4077                 /* set gpio register (all inputs, for -P modules,
4078                  * PSU will be turned off)
4079                  */
4080                 if ((status = signalyzer_h_ctrl_write(
4081                                 SIGNALYZER_DATA_BUFFER_ADDR,
4082                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4083                 {
4084                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4085                                         status);
4086                         return ERROR_JTAG_DEVICE_ERROR;
4087                 }
4088
4089                 if ((status = signalyzer_h_ctrl_write(
4090                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4091                         != FT_OK)
4092                 {
4093                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4094                                         status);
4095                         return ERROR_JTAG_DEVICE_ERROR;
4096                 }
4097
4098                 if ((status = signalyzer_h_ctrl_write(
4099                                 SIGNALYZER_COMMAND_ADDR,
4100                                 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4101                 {
4102                         LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4103                                         status);
4104                         return ERROR_JTAG_DEVICE_ERROR;
4105                 }
4106 #endif
4107         }
4108
4109         else if (signalyzer_h_adapter_type == 0x0000)
4110         {
4111                 LOG_INFO("Signalyzer: No external modules were detected.");
4112
4113                 nTRST    = 0x10;
4114                 nTRSTnOE = 0x10;
4115                 nSRST    = 0x20;
4116                 nSRSTnOE = 0x20;
4117
4118                 low_output     = 0x08;
4119                 low_direction  = 0x1b;
4120
4121                 high_output    = 0x0;
4122                 high_direction = 0x0;
4123
4124                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4125                 {
4126                         low_direction &= ~nTRSTnOE; /* nTRST input */
4127                         low_output    &= ~nTRST;    /* nTRST = 0 */
4128                 }
4129                 else
4130                 {
4131                         low_direction |= nTRSTnOE;  /* nTRST output */
4132                         low_output    |= nTRST;     /* nTRST = 1 */
4133                 }
4134
4135                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4136                 {
4137                         low_direction |= nSRSTnOE;  /* nSRST output */
4138                         low_output    |= nSRST;     /* nSRST = 1 */
4139                 }
4140                 else
4141                 {
4142                         low_direction &= ~nSRSTnOE; /* nSRST input */
4143                         low_output    &= ~nSRST;    /* nSRST = 0 */
4144                 }
4145         }
4146         else
4147         {
4148                 LOG_ERROR("Unknown module type is detected: %.4x",
4149                                 signalyzer_h_adapter_type);
4150                 return ERROR_JTAG_DEVICE_ERROR;
4151         }
4152
4153         /* initialize low byte of controller for jtag operation */
4154         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4155         {
4156                 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4157                 return ERROR_JTAG_INIT_FAILED;
4158         }
4159
4160 #if BUILD_FT2232_FTD2XX == 1
4161         if (ftdi_device == FT_DEVICE_2232H)
4162         {
4163                 /* initialize high byte of controller for jtag operation */
4164                 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4165                 {
4166                         LOG_ERROR("couldn't initialize Signalyzer-H layout");
4167                         return ERROR_JTAG_INIT_FAILED;
4168                 }
4169         }
4170 #elif BUILD_FT2232_LIBFTDI == 1
4171         if (ftdi_device == TYPE_2232H)
4172         {
4173                 /* initialize high byte of controller for jtag operation */
4174                 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4175                 {
4176                         LOG_ERROR("couldn't initialize Signalyzer-H layout");
4177                         return ERROR_JTAG_INIT_FAILED;
4178                 }
4179         }
4180 #endif
4181         return ERROR_OK;
4182 }
4183
4184 static void signalyzer_h_reset(int trst, int srst)
4185 {
4186         enum reset_types jtag_reset_config = jtag_get_reset_config();
4187
4188         /* ADAPTOR: EM_LT16_A */
4189         if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4190         {
4191                 if (trst == 1)
4192                 {
4193                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4194                                 /* switch to output pin (output is low) */
4195                                 low_direction |= nTRSTnOE;
4196                         else
4197                                 /* switch output low */
4198                                 low_output &= ~nTRST;
4199                 }
4200                 else if (trst == 0)
4201                 {
4202                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4203                                 /* switch to input pin (high-Z + internal
4204                                  * and external pullup) */
4205                                 low_direction &= ~nTRSTnOE;
4206                         else
4207                                 /* switch output high */
4208                                 low_output |= nTRST;
4209                 }
4210
4211                 if (srst == 1)
4212                 {
4213                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4214                                 /* switch output low */
4215                                 low_output &= ~nSRST;
4216                         else
4217                                 /* switch to output pin (output is low) */
4218                                 low_direction |= nSRSTnOE;
4219                 }
4220                 else if (srst == 0)
4221                 {
4222                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4223                                 /* switch output high */
4224                                 low_output |= nSRST;
4225                         else
4226                                 /* switch to input pin (high-Z) */
4227                                 low_direction &= ~nSRSTnOE;
4228                 }
4229
4230                 /* command "set data bits low byte" */
4231                 buffer_write(0x80);
4232                 buffer_write(low_output);
4233                 buffer_write(low_direction);
4234                 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4235                                 "low_direction: 0x%2.2x",
4236                                 trst, srst, low_output, low_direction);
4237         }
4238         /* ADAPTOR: EM_ARM_JTAG,  EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4239         else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4240                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4241                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG)  ||
4242                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4243         {
4244                 if (trst == 1)
4245                 {
4246                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4247                                 high_output &= ~nTRSTnOE;
4248                         else
4249                                 high_output &= ~nTRST;
4250                 }
4251                 else if (trst == 0)
4252                 {
4253                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4254                                 high_output |= nTRSTnOE;
4255                         else
4256                                 high_output |= nTRST;
4257                 }
4258
4259                 if (srst == 1)
4260                 {
4261                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4262                                 high_output &= ~nSRST;
4263                         else
4264                                 high_output &= ~nSRSTnOE;
4265                 }
4266                 else if (srst == 0)
4267                 {
4268                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4269                                 high_output |= nSRST;
4270                         else
4271                                 high_output |= nSRSTnOE;
4272                 }
4273
4274                 /* command "set data bits high byte" */
4275                 buffer_write(0x82);
4276                 buffer_write(high_output);
4277                 buffer_write(high_direction);
4278                 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4279                                 "high_direction: 0x%2.2x",
4280                                 trst, srst, high_output, high_direction);
4281         }
4282         else if (signalyzer_h_adapter_type == 0x0000)
4283         {
4284                 if (trst == 1)
4285                 {
4286                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4287                                 /* switch to output pin (output is low) */
4288                                 low_direction |= nTRSTnOE;
4289                         else
4290                                 /* switch output low */
4291                                 low_output &= ~nTRST;
4292                 }
4293                 else if (trst == 0)
4294                 {
4295                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4296                                 /* switch to input pin (high-Z + internal
4297                                  * and external pullup) */
4298                                 low_direction &= ~nTRSTnOE;
4299                         else
4300                                 /* switch output high */
4301                                 low_output |= nTRST;
4302                 }
4303
4304                 if (srst == 1)
4305                 {
4306                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4307                                 /* switch output low */
4308                                 low_output &= ~nSRST;
4309                         else
4310                                 /* switch to output pin (output is low) */
4311                                 low_direction |= nSRSTnOE;
4312                 }
4313                 else if (srst == 0)
4314                 {
4315                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4316                                 /* switch output high */
4317                                 low_output |= nSRST;
4318                         else
4319                                 /* switch to input pin (high-Z) */
4320                                 low_direction &= ~nSRSTnOE;
4321                 }
4322
4323                 /* command "set data bits low byte" */
4324                 buffer_write(0x80);
4325                 buffer_write(low_output);
4326                 buffer_write(low_direction);
4327                 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4328                                 "low_direction: 0x%2.2x",
4329                                 trst, srst, low_output, low_direction);
4330         }
4331 }
4332
4333 static void signalyzer_h_blink(void)
4334 {
4335         signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4336 }
4337
4338 /********************************************************************
4339  * Support for KT-LINK
4340  * JTAG adapter from KRISTECH
4341  * http://www.kristech.eu
4342  *******************************************************************/
4343 static int ktlink_init(void)
4344 {
4345         uint8_t  swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4346
4347         low_output    = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4348         low_direction = 0x3B;          // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4349
4350         /* initialize low byte for jtag */
4351         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4352         {
4353                 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4354                 return ERROR_JTAG_INIT_FAILED;
4355         }
4356
4357         nTRST    = 0x01;
4358         nSRST    = 0x02;
4359         nTRSTnOE = 0x04;
4360         nSRSTnOE = 0x08;
4361
4362         high_output    = 0x80; // turn LED on
4363         high_direction = 0xFF; // all outputs
4364
4365         enum reset_types jtag_reset_config = jtag_get_reset_config();
4366
4367         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4368                 high_output |= nTRSTnOE;
4369                 high_output &= ~nTRST;
4370         } else {
4371                 high_output &= ~nTRSTnOE;
4372                 high_output |= nTRST;
4373         }
4374
4375         if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4376                 high_output &= ~nSRSTnOE;
4377                 high_output |= nSRST;
4378         } else {
4379                 high_output |= nSRSTnOE;
4380                 high_output &= ~nSRST;
4381         }
4382
4383         /* initialize high byte for jtag */
4384         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4385         {
4386                 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4387                 return ERROR_JTAG_INIT_FAILED;
4388         }
4389
4390         return ERROR_OK;
4391 }
4392
4393 static void ktlink_reset(int trst, int srst)
4394 {
4395         enum reset_types jtag_reset_config = jtag_get_reset_config();
4396
4397         if (trst == 1) {
4398                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4399                         high_output &= ~nTRSTnOE;
4400                 else
4401                         high_output &= ~nTRST;
4402         } else if (trst == 0) {
4403                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4404                         high_output |= nTRSTnOE;
4405                 else
4406                         high_output |= nTRST;
4407         }
4408
4409         if (srst == 1) {
4410                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4411                         high_output &= ~nSRST;
4412                 else
4413                         high_output &= ~nSRSTnOE;
4414         } else if (srst == 0) {
4415                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4416                         high_output |= nSRST;
4417                 else
4418                         high_output |= nSRSTnOE;
4419         }
4420
4421         buffer_write(0x82); // command "set data bits high byte"
4422         buffer_write(high_output);
4423         buffer_write(high_direction);
4424         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4425 }
4426
4427 static void ktlink_blink(void)
4428 {
4429         /* LED connected to ACBUS7 */
4430         high_output ^= 0x80;
4431
4432         buffer_write(0x82);  // command "set data bits high byte"
4433         buffer_write(high_output);
4434         buffer_write(high_direction);
4435 }
4436
4437 static const struct command_registration ft2232_command_handlers[] = {
4438         {
4439                 .name = "ft2232_device_desc",
4440                 .handler = &ft2232_handle_device_desc_command,
4441                 .mode = COMMAND_CONFIG,
4442                 .help = "set the USB device description of the FTDI FT2232 device",
4443                 .usage = "description_string",
4444         },
4445         {
4446                 .name = "ft2232_serial",
4447                 .handler = &ft2232_handle_serial_command,
4448                 .mode = COMMAND_CONFIG,
4449                 .help = "set the serial number of the FTDI FT2232 device",
4450                 .usage = "serial_string",
4451         },
4452         {
4453                 .name = "ft2232_layout",
4454                 .handler = &ft2232_handle_layout_command,
4455                 .mode = COMMAND_CONFIG,
4456                 .help = "set the layout of the FT2232 GPIO signals used "
4457                         "to control output-enables and reset signals",
4458                 .usage = "layout_name",
4459         },
4460         {
4461                 .name = "ft2232_vid_pid",
4462                 .handler = &ft2232_handle_vid_pid_command,
4463                 .mode = COMMAND_CONFIG,
4464                 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4465                 .usage = "(vid pid)* ",
4466         },
4467         {
4468                 .name = "ft2232_latency",
4469                 .handler = &ft2232_handle_latency_command,
4470                 .mode = COMMAND_CONFIG,
4471                 .help = "set the FT2232 latency timer to a new value",
4472                 .usage = "value",
4473         },
4474         COMMAND_REGISTRATION_DONE
4475 };
4476
4477 struct jtag_interface ft2232_interface = {
4478         .name = "ft2232",
4479         .supported = DEBUG_CAP_TMS_SEQ,
4480         .commands = ft2232_command_handlers,
4481         .transports = jtag_only,
4482
4483         .init = ft2232_init,
4484         .quit = ft2232_quit,
4485         .speed = ft2232_speed,
4486         .speed_div = ft2232_speed_div,
4487         .khz = ft2232_khz,
4488         .execute_queue = ft2232_execute_queue,
4489 };