ftd2xx: handle FT_GetLatencyTimer bug in v1.04
[fw/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 <transport/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: %" PRIu32, 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: %" PRIu32, 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): %" PRIu32, status);
2219                         *try_more = 1;
2220                         return ERROR_JTAG_INIT_FAILED;
2221                 }
2222                 LOG_ERROR("unable to open ftdi device: %" PRIu32, 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: %" PRIu32, 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: %" PRIu32, status);
2258                 return ERROR_JTAG_INIT_FAILED;
2259         }
2260
2261         if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2262         {
2263                 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2264                  * so ignore errors if using this driver version */
2265                 DWORD dw_version;
2266                 
2267                 status = FT_GetDriverVersion(ftdih, &dw_version);
2268                 LOG_ERROR("unable to get latency timer: %" PRIu32, status);
2269                 
2270                 if ((status == FT_OK) && (dw_version == 0x10004)) {
2271                         LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2272                                         "with FT_GetLatencyTimer, upgrade to a newer version");
2273                 }
2274                 else {
2275                         return ERROR_JTAG_INIT_FAILED;
2276                 }
2277         }
2278         else
2279         {
2280                 LOG_DEBUG("current latency timer: %i", latency_timer);
2281         }
2282
2283         if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2284         {
2285                 LOG_ERROR("unable to set timeouts: %" PRIu32, status);
2286                 return ERROR_JTAG_INIT_FAILED;
2287         }
2288
2289         if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2290         {
2291                 LOG_ERROR("unable to enable bit i/o mode: %" PRIu32, status);
2292                 return ERROR_JTAG_INIT_FAILED;
2293         }
2294
2295         if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2296         {
2297                 LOG_ERROR("unable to get FT_GetDeviceInfo: %" PRIu32, status);
2298                 return ERROR_JTAG_INIT_FAILED;
2299         }
2300         else
2301         {
2302                 static const char* type_str[] =
2303                         {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2304                 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2305                 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2306                         ? ftdi_device : FT_DEVICE_UNKNOWN;
2307                 LOG_INFO("device: %" PRIu32 " \"%s\"", ftdi_device, type_str[type_index]);
2308                 LOG_INFO("deviceID: %" PRIu32, deviceID);
2309                 LOG_INFO("SerialNumber: %s", SerialNumber);
2310                 LOG_INFO("Description: %s", Description);
2311         }
2312
2313         return ERROR_OK;
2314 }
2315
2316 static int ft2232_purge_ftd2xx(void)
2317 {
2318         FT_STATUS status;
2319
2320         if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2321         {
2322                 LOG_ERROR("error purging ftd2xx device: %" PRIu32, status);
2323                 return ERROR_JTAG_INIT_FAILED;
2324         }
2325
2326         return ERROR_OK;
2327 }
2328
2329 #endif /* BUILD_FT2232_FTD2XX == 1 */
2330
2331 #if BUILD_FT2232_LIBFTDI == 1
2332 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2333 {
2334         uint8_t latency_timer;
2335
2336         if (layout == NULL) {
2337                 LOG_WARNING("No ft2232 layout specified'");
2338                 return ERROR_JTAG_INIT_FAILED;
2339         }
2340
2341         LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2342                         layout->name, vid, pid);
2343
2344         if (ftdi_init(&ftdic) < 0)
2345                 return ERROR_JTAG_INIT_FAILED;
2346
2347         /* default to INTERFACE_A */
2348         if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2349
2350         if (ftdi_set_interface(&ftdic, channel) < 0)
2351         {
2352                 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2353                 return ERROR_JTAG_INIT_FAILED;
2354         }
2355
2356         /* context, vendor id, product id */
2357         if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2358                                 ft2232_serial) < 0)
2359         {
2360                 if (more)
2361                         LOG_WARNING("unable to open ftdi device (trying more): %s",
2362                                         ftdic.error_str);
2363                 else
2364                         LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2365                 *try_more = 1;
2366                 return ERROR_JTAG_INIT_FAILED;
2367         }
2368
2369         /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2370         if (ftdi_usb_reset(&ftdic) < 0)
2371         {
2372                 LOG_ERROR("unable to reset ftdi device");
2373                 return ERROR_JTAG_INIT_FAILED;
2374         }
2375
2376         if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2377         {
2378                 LOG_ERROR("unable to set latency timer");
2379                 return ERROR_JTAG_INIT_FAILED;
2380         }
2381
2382         if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2383         {
2384                 LOG_ERROR("unable to get latency timer");
2385                 return ERROR_JTAG_INIT_FAILED;
2386         }
2387         else
2388         {
2389                 LOG_DEBUG("current latency timer: %i", latency_timer);
2390         }
2391
2392         ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2393
2394         ftdi_device = ftdic.type;
2395         static const char* type_str[] =
2396                 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2397         unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2398         unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2399                 ? ftdi_device : no_of_known_types;
2400         LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2401         return ERROR_OK;
2402 }
2403
2404 static int ft2232_purge_libftdi(void)
2405 {
2406         if (ftdi_usb_purge_buffers(&ftdic) < 0)
2407         {
2408                 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2409                 return ERROR_JTAG_INIT_FAILED;
2410         }
2411
2412         return ERROR_OK;
2413 }
2414
2415 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2416
2417 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2418 {
2419         uint8_t  buf[3];
2420         uint32_t bytes_written;
2421
2422         buf[0] = 0x80;          /* command "set data bits low byte" */
2423         buf[1] = value;         /* value */
2424         buf[2] = direction;     /* direction */
2425
2426         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2427
2428         if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2429         {
2430                 LOG_ERROR("couldn't initialize data bits low byte");
2431                 return ERROR_JTAG_INIT_FAILED;
2432         }
2433
2434         return ERROR_OK;
2435 }
2436
2437 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2438 {
2439         uint8_t  buf[3];
2440         uint32_t bytes_written;
2441
2442         buf[0] = 0x82;          /* command "set data bits high byte" */
2443         buf[1] = value;         /* value */
2444         buf[2] = direction;     /* direction */
2445
2446         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2447
2448         if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2449         {
2450                 LOG_ERROR("couldn't initialize data bits high byte");
2451                 return ERROR_JTAG_INIT_FAILED;
2452         }
2453
2454         return ERROR_OK;
2455 }
2456
2457 static int ft2232_init(void)
2458 {
2459         uint8_t  buf[1];
2460         int retval;
2461         uint32_t bytes_written;
2462
2463         if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2464         {
2465                 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2466         }
2467         else
2468         {
2469                 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2470
2471         }
2472         if (layout == NULL) {
2473                 LOG_WARNING("No ft2232 layout specified'");
2474                 return ERROR_JTAG_INIT_FAILED;
2475         }
2476
2477         for (int i = 0; 1; i++)
2478         {
2479                 /*
2480                  * "more indicates that there are more IDs to try, so we should
2481                  * not print an error for an ID mismatch (but for anything
2482                  * else, we should).
2483                  *
2484                  * try_more indicates that the error code returned indicates an
2485                  * ID mismatch (and nothing else) and that we should proceeed
2486                  * with the next ID pair.
2487                  */
2488                 int more     = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2489                 int try_more = 0;
2490
2491 #if BUILD_FT2232_FTD2XX == 1
2492                 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2493                                 more, &try_more);
2494 #elif BUILD_FT2232_LIBFTDI == 1
2495                 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2496                                              more, &try_more, layout->channel);
2497 #endif
2498                 if (retval >= 0)
2499                         break;
2500                 if (!more || !try_more)
2501                         return retval;
2502         }
2503
2504         ft2232_buffer_size = 0;
2505         ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2506
2507         if (layout->init() != ERROR_OK)
2508                 return ERROR_JTAG_INIT_FAILED;
2509
2510         if (ft2232_device_is_highspeed())
2511         {
2512 #ifndef BUILD_FT2232_HIGHSPEED
2513  #if BUILD_FT2232_FTD2XX == 1
2514                 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2515  #elif BUILD_FT2232_LIBFTDI == 1
2516                 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2517  #endif
2518 #endif
2519                 /* make sure the legacy mode is disabled */
2520                 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2521                         return ERROR_JTAG_INIT_FAILED;
2522         }
2523
2524         buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2525         if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2526         {
2527                 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2528                 return ERROR_JTAG_INIT_FAILED;
2529         }
2530
2531 #if BUILD_FT2232_FTD2XX == 1
2532         return ft2232_purge_ftd2xx();
2533 #elif BUILD_FT2232_LIBFTDI == 1
2534         return ft2232_purge_libftdi();
2535 #endif
2536
2537         return ERROR_OK;
2538 }
2539
2540 /** Updates defaults for DBUS signals:  the four JTAG signals
2541  * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2542  */
2543 static inline void ftx232_dbus_init(void)
2544 {
2545         low_output    = 0x08;
2546         low_direction = 0x0b;
2547 }
2548
2549 /** Initializes DBUS signals:  the four JTAG signals (TCK, TDI, TDO, TMS),
2550  * the four GPIOL signals.  Initialization covers value and direction,
2551  * as customized for each layout.
2552  */
2553 static int ftx232_dbus_write(void)
2554 {
2555         enum reset_types jtag_reset_config = jtag_get_reset_config();
2556         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2557         {
2558                 low_direction &= ~nTRSTnOE; /* nTRST input */
2559                 low_output    &= ~nTRST;    /* nTRST = 0 */
2560         }
2561         else
2562         {
2563                 low_direction |= nTRSTnOE;  /* nTRST output */
2564                 low_output    |= nTRST;     /* nTRST = 1 */
2565         }
2566
2567         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2568         {
2569                 low_direction |= nSRSTnOE;  /* nSRST output */
2570                 low_output    |= nSRST;     /* nSRST = 1 */
2571         }
2572         else
2573         {
2574                 low_direction &= ~nSRSTnOE; /* nSRST input */
2575                 low_output    &= ~nSRST;    /* nSRST = 0 */
2576         }
2577
2578         /* initialize low byte for jtag */
2579         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2580         {
2581                 LOG_ERROR("couldn't initialize FT2232 DBUS");
2582                 return ERROR_JTAG_INIT_FAILED;
2583         }
2584
2585         return ERROR_OK;
2586 }
2587
2588 static int usbjtag_init(void)
2589 {
2590         /*
2591          * NOTE:  This is now _specific_ to the "usbjtag" layout.
2592          * Don't try cram any more layouts into this.
2593          */
2594         ftx232_dbus_init();
2595
2596         nTRST    = 0x10;
2597         nTRSTnOE = 0x10;
2598         nSRST    = 0x40;
2599         nSRSTnOE = 0x40;
2600
2601         return ftx232_dbus_write();
2602 }
2603
2604 static int lm3s811_jtag_init(void)
2605 {
2606         ftx232_dbus_init();
2607
2608         /* There are multiple revisions of LM3S811 eval boards:
2609          * - Rev B (and older?) boards have no SWO trace support.
2610          * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2611          *   they should use the "luminary_icdi" layout instead.
2612          */
2613         nTRST = 0x0;
2614         nTRSTnOE = 0x00;
2615         nSRST = 0x20;
2616         nSRSTnOE = 0x20;
2617         low_output    = 0x88;
2618         low_direction = 0x8b;
2619
2620         return ftx232_dbus_write();
2621 }
2622
2623 static int icdi_jtag_init(void)
2624 {
2625         ftx232_dbus_init();
2626
2627         /* Most Luminary eval boards support SWO trace output,
2628          * and should use this "luminary_icdi" layout.
2629          *
2630          * ADBUS 0..3 are used for JTAG as usual.  GPIOs are used
2631          * to switch between JTAG and SWD, or switch the ft2232 UART
2632          * on the second MPSSE channel/interface (BDBUS)
2633          * between (i) the stellaris UART (on Luminary boards)
2634          * or (ii) SWO trace data (generic).
2635          *
2636          * We come up in JTAG mode and may switch to SWD later (with
2637          * SWO/trace option if SWD is active).
2638          *
2639          * DBUS == GPIO-Lx
2640          * CBUS == GPIO-Hx
2641          */
2642
2643
2644 #define ICDI_JTAG_EN (1 << 7)           /* ADBUS 7 (a.k.a. DBGMOD) */
2645 #define ICDI_DBG_ENn (1 << 6)           /* ADBUS 6 */
2646 #define ICDI_SRST (1 << 5)              /* ADBUS 5 */
2647
2648
2649         /* GPIOs on second channel/interface (UART) ... */
2650 #define ICDI_SWO_EN (1 << 4)            /* BDBUS 4 */
2651 #define ICDI_TX_SWO (1 << 1)            /* BDBUS 1 */
2652 #define ICDI_VCP_RX (1 << 0)            /* BDBUS 0 (to stellaris UART) */
2653
2654         nTRST = 0x0;
2655         nTRSTnOE = 0x00;
2656         nSRST = ICDI_SRST;
2657         nSRSTnOE = ICDI_SRST;
2658
2659         low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2660         low_output    |= ICDI_JTAG_EN;
2661         low_output    &= ~ICDI_DBG_ENn;
2662
2663         return ftx232_dbus_write();
2664 }
2665
2666 static int signalyzer_init(void)
2667 {
2668         ftx232_dbus_init();
2669
2670         nTRST    = 0x10;
2671         nTRSTnOE = 0x10;
2672         nSRST    = 0x20;
2673         nSRSTnOE = 0x20;
2674         return ftx232_dbus_write();
2675 }
2676
2677 static int axm0432_jtag_init(void)
2678 {
2679         low_output    = 0x08;
2680         low_direction = 0x2b;
2681
2682         /* initialize low byte for jtag */
2683         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2684         {
2685                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2686                 return ERROR_JTAG_INIT_FAILED;
2687         }
2688
2689         if (strcmp(layout->name, "axm0432_jtag") == 0)
2690         {
2691                 nTRST    = 0x08;
2692                 nTRSTnOE = 0x0;     /* No output enable for TRST*/
2693                 nSRST    = 0x04;
2694                 nSRSTnOE = 0x0;     /* No output enable for SRST*/
2695         }
2696         else
2697         {
2698                 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2699                 exit(-1);
2700         }
2701
2702         high_output    = 0x0;
2703         high_direction = 0x0c;
2704
2705         enum reset_types jtag_reset_config = jtag_get_reset_config();
2706         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2707         {
2708                 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2709         }
2710         else
2711         {
2712                 high_output |= nTRST;
2713         }
2714
2715         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2716         {
2717                 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2718         }
2719         else
2720         {
2721                 high_output |= nSRST;
2722         }
2723
2724         /* initialize high byte for jtag */
2725         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2726         {
2727                 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2728                 return ERROR_JTAG_INIT_FAILED;
2729         }
2730
2731         return ERROR_OK;
2732 }
2733
2734 static int redbee_init(void)
2735 {
2736         low_output    = 0x08;
2737         low_direction = 0x2b;
2738
2739         /* initialize low byte for jtag */
2740         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2741         {
2742                 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2743                 return ERROR_JTAG_INIT_FAILED;
2744         }
2745
2746         nTRST    = 0x08;
2747         nTRSTnOE = 0x0;     /* No output enable for TRST*/
2748         nSRST    = 0x04;
2749         nSRSTnOE = 0x0;     /* No output enable for SRST*/
2750
2751         high_output    = 0x0;
2752         high_direction = 0x0c;
2753
2754         enum reset_types jtag_reset_config = jtag_get_reset_config();
2755         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2756         {
2757                 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2758         }
2759         else
2760         {
2761                 high_output |= nTRST;
2762         }
2763
2764         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2765         {
2766                 LOG_ERROR("can't set nSRST to push-pull on redbee");
2767         }
2768         else
2769         {
2770                 high_output |= nSRST;
2771         }
2772
2773         /* initialize high byte for jtag */
2774         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2775         {
2776                 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2777                 return ERROR_JTAG_INIT_FAILED;
2778         }
2779
2780         return ERROR_OK;
2781 }
2782
2783 static int jtagkey_init(void)
2784 {
2785         low_output    = 0x08;
2786         low_direction = 0x1b;
2787
2788         /* initialize low byte for jtag */
2789         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2790         {
2791                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2792                 return ERROR_JTAG_INIT_FAILED;
2793         }
2794
2795         if (strcmp(layout->name, "jtagkey") == 0)
2796         {
2797                 nTRST    = 0x01;
2798                 nTRSTnOE = 0x4;
2799                 nSRST    = 0x02;
2800                 nSRSTnOE = 0x08;
2801         }
2802         else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2803                          || (strcmp(layout->name, "oocdlink") == 0))
2804         {
2805                 nTRST    = 0x02;
2806                 nTRSTnOE = 0x1;
2807                 nSRST    = 0x08;
2808                 nSRSTnOE = 0x04;
2809         }
2810         else
2811         {
2812                 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2813                 exit(-1);
2814         }
2815
2816         high_output    = 0x0;
2817         high_direction = 0x0f;
2818
2819         enum reset_types jtag_reset_config = jtag_get_reset_config();
2820         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2821         {
2822                 high_output |= nTRSTnOE;
2823                 high_output &= ~nTRST;
2824         }
2825         else
2826         {
2827                 high_output &= ~nTRSTnOE;
2828                 high_output |= nTRST;
2829         }
2830
2831         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2832         {
2833                 high_output &= ~nSRSTnOE;
2834                 high_output |= nSRST;
2835         }
2836         else
2837         {
2838                 high_output |= nSRSTnOE;
2839                 high_output &= ~nSRST;
2840         }
2841
2842         /* initialize high byte for jtag */
2843         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2844         {
2845                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2846                 return ERROR_JTAG_INIT_FAILED;
2847         }
2848
2849         return ERROR_OK;
2850 }
2851
2852 static int olimex_jtag_init(void)
2853 {
2854         low_output    = 0x08;
2855         low_direction = 0x1b;
2856
2857         /* initialize low byte for jtag */
2858         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2859         {
2860                 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2861                 return ERROR_JTAG_INIT_FAILED;
2862         }
2863
2864         nTRST    = 0x01;
2865         nTRSTnOE = 0x4;
2866         nSRST    = 0x02;
2867         nSRSTnOE = 0x00; /* no output enable for nSRST */
2868
2869         high_output    = 0x0;
2870         high_direction = 0x0f;
2871
2872         enum reset_types jtag_reset_config = jtag_get_reset_config();
2873         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2874         {
2875                 high_output |= nTRSTnOE;
2876                 high_output &= ~nTRST;
2877         }
2878         else
2879         {
2880                 high_output &= ~nTRSTnOE;
2881                 high_output |= nTRST;
2882         }
2883
2884         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2885         {
2886                 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2887         }
2888         else
2889         {
2890                 high_output &= ~nSRST;
2891         }
2892
2893         /* turn red LED on */
2894         high_output |= 0x08;
2895
2896         /* initialize high byte for jtag */
2897         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2898         {
2899                 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2900                 return ERROR_JTAG_INIT_FAILED;
2901         }
2902
2903         return ERROR_OK;
2904 }
2905
2906 static int flyswatter_init(void)
2907 {
2908         low_output    = 0x18;
2909         low_direction = 0xfb;
2910
2911         /* initialize low byte for jtag */
2912         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2913         {
2914                 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2915                 return ERROR_JTAG_INIT_FAILED;
2916         }
2917
2918         nTRST    = 0x10;
2919         nTRSTnOE = 0x0;     /* not output enable for nTRST */
2920         nSRST    = 0x20;
2921         nSRSTnOE = 0x00;    /* no output enable for nSRST */
2922
2923         high_output    = 0x00;
2924         high_direction = 0x0c;
2925
2926         /* turn red LED3 on, LED2 off */
2927         high_output |= 0x08;
2928
2929         /* initialize high byte for jtag */
2930         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2931         {
2932                 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2933                 return ERROR_JTAG_INIT_FAILED;
2934         }
2935
2936         return ERROR_OK;
2937 }
2938
2939 static int minimodule_init(void)
2940 {
2941         low_output    = 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18)
2942         low_direction = 0xfb;//0xfb;
2943
2944         /* initialize low byte for jtag */
2945         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2946         {
2947                 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2948                 return ERROR_JTAG_INIT_FAILED;
2949         }
2950
2951
2952         nSRST    = 0x20;
2953
2954         high_output    = 0x00;
2955         high_direction = 0x05;
2956
2957         /* turn red LED3 on, LED2 off */
2958         //high_output |= 0x08;
2959
2960         /* initialize high byte for jtag */
2961         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2962         {
2963                 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2964                 return ERROR_JTAG_INIT_FAILED;
2965         }
2966
2967         return ERROR_OK;
2968 }
2969
2970 static int turtle_init(void)
2971 {
2972         low_output    = 0x08;
2973         low_direction = 0x5b;
2974
2975         /* initialize low byte for jtag */
2976         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2977         {
2978                 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2979                 return ERROR_JTAG_INIT_FAILED;
2980         }
2981
2982         nSRST = 0x40;
2983
2984         high_output    = 0x00;
2985         high_direction = 0x0C;
2986
2987         /* initialize high byte for jtag */
2988         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2989         {
2990                 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2991                 return ERROR_JTAG_INIT_FAILED;
2992         }
2993
2994         return ERROR_OK;
2995 }
2996
2997 static int comstick_init(void)
2998 {
2999         low_output    = 0x08;
3000         low_direction = 0x0b;
3001
3002         /* initialize low byte for jtag */
3003         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3004         {
3005                 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3006                 return ERROR_JTAG_INIT_FAILED;
3007         }
3008
3009         nTRST    = 0x01;
3010         nTRSTnOE = 0x00;    /* no output enable for nTRST */
3011         nSRST    = 0x02;
3012         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3013
3014         high_output    = 0x03;
3015         high_direction = 0x03;
3016
3017         /* initialize high byte for jtag */
3018         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3019         {
3020                 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3021                 return ERROR_JTAG_INIT_FAILED;
3022         }
3023
3024         return ERROR_OK;
3025 }
3026
3027 static int stm32stick_init(void)
3028 {
3029         low_output    = 0x88;
3030         low_direction = 0x8b;
3031
3032         /* initialize low byte for jtag */
3033         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3034         {
3035                 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3036                 return ERROR_JTAG_INIT_FAILED;
3037         }
3038
3039         nTRST    = 0x01;
3040         nTRSTnOE = 0x00;    /* no output enable for nTRST */
3041         nSRST    = 0x80;
3042         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3043
3044         high_output    = 0x01;
3045         high_direction = 0x03;
3046
3047         /* initialize high byte for jtag */
3048         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3049         {
3050                 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3051                 return ERROR_JTAG_INIT_FAILED;
3052         }
3053
3054         return ERROR_OK;
3055 }
3056
3057 static int sheevaplug_init(void)
3058 {
3059         low_output = 0x08;
3060         low_direction = 0x1b;
3061
3062         /* initialize low byte for jtag */
3063         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3064         {
3065                 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3066                 return ERROR_JTAG_INIT_FAILED;
3067         }
3068
3069         nTRSTnOE = 0x1;
3070         nTRST = 0x02;
3071         nSRSTnOE = 0x4;
3072         nSRST = 0x08;
3073
3074         high_output = 0x0;
3075         high_direction = 0x0f;
3076
3077         /* nTRST is always push-pull */
3078         high_output &= ~nTRSTnOE;
3079         high_output |= nTRST;
3080
3081         /* nSRST is always open-drain */
3082         high_output |= nSRSTnOE;
3083         high_output &= ~nSRST;
3084
3085         /* initialize high byte for jtag */
3086         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3087         {
3088                 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3089                 return ERROR_JTAG_INIT_FAILED;
3090         }
3091
3092         return ERROR_OK;
3093 }
3094
3095 static int cortino_jtag_init(void)
3096 {
3097         low_output    = 0x08;
3098         low_direction = 0x1b;
3099
3100         /* initialize low byte for jtag */
3101         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3102         {
3103                 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3104                 return ERROR_JTAG_INIT_FAILED;
3105         }
3106
3107         nTRST    = 0x01;
3108         nTRSTnOE = 0x00;    /* no output enable for nTRST */
3109         nSRST    = 0x02;
3110         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3111
3112         high_output    = 0x03;
3113         high_direction = 0x03;
3114
3115         /* initialize high byte for jtag */
3116         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3117         {
3118                 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3119                 return ERROR_JTAG_INIT_FAILED;
3120         }
3121
3122         return ERROR_OK;
3123 }
3124
3125 static int lisa_l_init(void)
3126 {
3127         ftx232_dbus_init();
3128
3129         nTRST    = 0x10;
3130         nTRSTnOE = 0x10;
3131         nSRST    = 0x40;
3132         nSRSTnOE = 0x40;
3133
3134         high_output = 0x00;
3135         high_direction = 0x18;
3136
3137         /* initialize high byte for jtag */
3138         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3139         {
3140                 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3141                 return ERROR_JTAG_INIT_FAILED;
3142         }
3143
3144         return ftx232_dbus_write();
3145 }
3146
3147 static int flossjtag_init(void)
3148 {
3149         ftx232_dbus_init();
3150
3151         nTRST    = 0x10;
3152         nTRSTnOE = 0x10;
3153         nSRST    = 0x40;
3154         nSRSTnOE = 0x40;
3155
3156         high_output = 0x00;
3157         high_direction = 0x18;
3158
3159         /* initialize high byte for jtag */
3160         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3161         {
3162                 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3163                 return ERROR_JTAG_INIT_FAILED;
3164         }
3165
3166         return ftx232_dbus_write();
3167 }
3168
3169 static int xds100v2_init(void)
3170 {
3171         low_output    = 0x3A;
3172         low_direction = 0x7B;
3173
3174         /* initialize low byte for jtag */
3175         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3176         {
3177                 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3178                 return ERROR_JTAG_INIT_FAILED;
3179         }
3180
3181         nTRST    = 0x10;
3182         nTRSTnOE = 0x0;     /* not output enable for nTRST */
3183         nSRST    = 0x00;    /* TODO: SRST is not supported yet */
3184         nSRSTnOE = 0x00;    /* no output enable for nSRST */
3185
3186         high_output    = 0x00;
3187         high_direction = 0x59;
3188
3189         /* initialize high byte for jtag */
3190         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3191         {
3192                 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3193                 return ERROR_JTAG_INIT_FAILED;
3194         }
3195
3196         high_output    = 0x86;
3197         high_direction = 0x59;
3198
3199         /* initialize high byte for jtag */
3200         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3201         {
3202                 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3203                 return ERROR_JTAG_INIT_FAILED;
3204         }
3205
3206         return ERROR_OK;
3207 }
3208
3209 static void olimex_jtag_blink(void)
3210 {
3211         /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3212          * ACBUS3 is bit 3 of the GPIOH port
3213          */
3214         high_output ^= 0x08;
3215
3216         buffer_write(0x82);
3217         buffer_write(high_output);
3218         buffer_write(high_direction);
3219 }
3220
3221 static void flyswatter_jtag_blink(void)
3222 {
3223         /*
3224          * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3225          */
3226         high_output ^= 0x0c;
3227
3228         buffer_write(0x82);
3229         buffer_write(high_output);
3230         buffer_write(high_direction);
3231 }
3232
3233 static void turtle_jtag_blink(void)
3234 {
3235         /*
3236          * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3237          */
3238         if (high_output & 0x08)
3239         {
3240                 high_output = 0x04;
3241         }
3242         else
3243         {
3244                 high_output = 0x08;
3245         }
3246
3247         buffer_write(0x82);
3248         buffer_write(high_output);
3249         buffer_write(high_direction);
3250 }
3251
3252 static void lisa_l_blink(void)
3253 {
3254         /*
3255          * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3256          */
3257         if (high_output & 0x10)
3258         {
3259                 high_output = 0x08;
3260         }
3261         else
3262         {
3263                 high_output = 0x10;
3264         }
3265
3266         buffer_write(0x82);
3267         buffer_write(high_output);
3268         buffer_write(high_direction);
3269 }
3270
3271 static void flossjtag_blink(void)
3272 {
3273         /*
3274          * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3275          */
3276         if (high_output & 0x10)
3277         {
3278                 high_output = 0x08;
3279         }
3280         else
3281         {
3282                 high_output = 0x10;
3283         }
3284
3285         buffer_write(0x82);
3286         buffer_write(high_output);
3287         buffer_write(high_direction);
3288 }
3289
3290 static int ft2232_quit(void)
3291 {
3292 #if BUILD_FT2232_FTD2XX == 1
3293         FT_STATUS status;
3294
3295         status = FT_Close(ftdih);
3296 #elif BUILD_FT2232_LIBFTDI == 1
3297         ftdi_usb_close(&ftdic);
3298
3299         ftdi_deinit(&ftdic);
3300 #endif
3301
3302         free(ft2232_buffer);
3303         ft2232_buffer = NULL;
3304
3305         return ERROR_OK;
3306 }
3307
3308 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3309 {
3310         char *cp;
3311         char buf[200];
3312         if (CMD_ARGC == 1)
3313         {
3314                 ft2232_device_desc = strdup(CMD_ARGV[0]);
3315                 cp = strchr(ft2232_device_desc, 0);
3316                 /* under Win32, the FTD2XX driver appends an "A" to the end
3317                  * of the description, this examines the given desc
3318                  * and creates the 'missing' _A or non_A variable. */
3319                 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3320                         /* it was, so make this the "A" version. */
3321                         ft2232_device_desc_A = ft2232_device_desc;
3322                         /* and *CREATE* the non-A version. */
3323                         strcpy(buf, ft2232_device_desc);
3324                         cp = strchr(buf, 0);
3325                         cp[-2] = 0;
3326                         ft2232_device_desc =  strdup(buf);
3327                 } else {
3328                         /* <space > A not defined
3329                          * so create it */
3330                         sprintf(buf, "%s A", ft2232_device_desc);
3331                         ft2232_device_desc_A = strdup(buf);
3332                 }
3333         }
3334         else
3335         {
3336                 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3337         }
3338
3339         return ERROR_OK;
3340 }
3341
3342 COMMAND_HANDLER(ft2232_handle_serial_command)
3343 {
3344         if (CMD_ARGC == 1)
3345         {
3346                 ft2232_serial = strdup(CMD_ARGV[0]);
3347         }
3348         else
3349         {
3350                 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3351         }
3352
3353         return ERROR_OK;
3354 }
3355
3356 COMMAND_HANDLER(ft2232_handle_layout_command)
3357 {
3358         if (CMD_ARGC != 1) {
3359                 LOG_ERROR("Need exactly one argument to ft2232_layout");
3360                 return ERROR_FAIL;
3361         }
3362
3363         if (layout) {
3364                 LOG_ERROR("already specified ft2232_layout %s",
3365                                 layout->name);
3366                 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3367                                 ? ERROR_FAIL
3368                                 : ERROR_OK;
3369         }
3370
3371         for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3372                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3373                         layout = l;
3374                         return ERROR_OK;
3375                 }
3376         }
3377
3378         LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3379         return ERROR_FAIL;
3380 }
3381
3382 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3383 {
3384         if (CMD_ARGC > MAX_USB_IDS * 2)
3385         {
3386                 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3387                                         "(maximum is %d pairs)", MAX_USB_IDS);
3388                 CMD_ARGC = MAX_USB_IDS * 2;
3389         }
3390         if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3391         {
3392                 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3393                 if (CMD_ARGC < 2)
3394                         return ERROR_COMMAND_SYNTAX_ERROR;
3395                 /* remove the incomplete trailing id */
3396                 CMD_ARGC -= 1;
3397         }
3398
3399         unsigned i;
3400         for (i = 0; i < CMD_ARGC; i += 2)
3401         {
3402                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3403                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3404         }
3405
3406         /*
3407          * Explicitly terminate, in case there are multiples instances of
3408          * ft2232_vid_pid.
3409          */
3410         ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3411
3412         return ERROR_OK;
3413 }
3414
3415 COMMAND_HANDLER(ft2232_handle_latency_command)
3416 {
3417         if (CMD_ARGC == 1)
3418         {
3419                 ft2232_latency = atoi(CMD_ARGV[0]);
3420         }
3421         else
3422         {
3423                 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3424         }
3425
3426         return ERROR_OK;
3427 }
3428
3429 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3430 {
3431         int retval = 0;
3432
3433         /* 7 bits of either ones or zeros. */
3434         uint8_t  tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3435
3436         while (num_cycles > 0)
3437         {
3438                 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3439                  * at most 7 bits per invocation.  Here we invoke it potentially
3440                  * several times.
3441                  */
3442                 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3443
3444                 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3445                 {
3446                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3447                                 retval = ERROR_JTAG_QUEUE_FAILED;
3448
3449                         first_unsent = cmd;
3450                 }
3451
3452                 /* there are no state transitions in this code, so omit state tracking */
3453
3454                 /* command "Clock Data to TMS/CS Pin (no Read)" */
3455                 buffer_write(0x4b);
3456
3457                 /* scan 7 bit */
3458                 buffer_write(bitcount_per_command - 1);
3459
3460                 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3461                 buffer_write(tms);
3462
3463                 require_send = 1;
3464
3465                 num_cycles -= bitcount_per_command;
3466         }
3467
3468         return retval;
3469 }
3470
3471 /* ---------------------------------------------------------------------
3472  * Support for IceBear JTAG adapter from Section5:
3473  *      http://section5.ch/icebear
3474  *
3475  * Author: Sten, debian@sansys-electronic.com
3476  */
3477
3478 /* Icebear pin layout
3479  *
3480  * ADBUS5 (nEMU) nSRST  | 2   1|        GND (10k->VCC)
3481  * GND GND              | 4   3|        n.c.
3482  * ADBUS3 TMS           | 6   5|        ADBUS6 VCC
3483  * ADBUS0 TCK           | 8   7|        ADBUS7 (GND)
3484  * ADBUS4 nTRST         |10   9|        ACBUS0 (GND)
3485  * ADBUS1 TDI           |12  11|        ACBUS1 (GND)
3486  * ADBUS2 TDO           |14  13|        GND GND
3487  *
3488  * ADBUS0 O L TCK               ACBUS0 GND
3489  * ADBUS1 O L TDI               ACBUS1 GND
3490  * ADBUS2 I   TDO               ACBUS2 n.c.
3491  * ADBUS3 O H TMS               ACBUS3 n.c.
3492  * ADBUS4 O H nTRST
3493  * ADBUS5 O H nSRST
3494  * ADBUS6 -   VCC
3495  * ADBUS7 -   GND
3496  */
3497 static int icebear_jtag_init(void) {
3498         low_direction   = 0x0b; /* output: TCK TDI TMS; input: TDO */
3499         low_output      = 0x08; /* high: TMS; low: TCK TDI */
3500         nTRST           = 0x10;
3501         nSRST           = 0x20;
3502
3503         enum reset_types jtag_reset_config = jtag_get_reset_config();
3504         if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3505                 low_direction   &= ~nTRST;      /* nTRST high impedance */
3506         }
3507         else {
3508                 low_direction   |= nTRST;
3509                 low_output      |= nTRST;
3510         }
3511
3512         low_direction   |= nSRST;
3513         low_output      |= nSRST;
3514
3515         /* initialize low byte for jtag */
3516         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK) {
3517                 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3518                 return ERROR_JTAG_INIT_FAILED;
3519         }
3520
3521         high_output    = 0x0;
3522         high_direction = 0x00;
3523
3524         /* initialize high byte for jtag */
3525         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK) {
3526                 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3527                 return ERROR_JTAG_INIT_FAILED;
3528         }
3529
3530         return ERROR_OK;
3531 }
3532
3533 static void icebear_jtag_reset(int trst, int srst) {
3534
3535         if (trst == 1) {
3536                 low_direction   |= nTRST;
3537                 low_output      &= ~nTRST;
3538         }
3539         else if (trst == 0) {
3540                 enum reset_types jtag_reset_config = jtag_get_reset_config();
3541                 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3542                         low_direction   &= ~nTRST;
3543                 else
3544                         low_output      |= nTRST;
3545         }
3546
3547         if (srst == 1) {
3548                 low_output &= ~nSRST;
3549         }
3550         else if (srst == 0) {
3551                 low_output |= nSRST;
3552         }
3553
3554         /* command "set data bits low byte" */
3555         buffer_write(0x80);
3556         buffer_write(low_output);
3557         buffer_write(low_direction);
3558
3559         LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3560 }
3561
3562 /* ---------------------------------------------------------------------
3563  * Support for Signalyzer H2 and Signalyzer H4
3564  * JTAG adapter from Xverve Technologies Inc.
3565  * http://www.signalyzer.com or http://www.xverve.com
3566  *
3567  * Author: Oleg Seiljus, oleg@signalyzer.com
3568  */
3569 static unsigned char signalyzer_h_side;
3570 static unsigned int signalyzer_h_adapter_type;
3571
3572 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3573
3574 #if BUILD_FT2232_FTD2XX == 1
3575 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3576 #endif
3577
3578 #define SIGNALYZER_COMMAND_ADDR                                 128
3579 #define SIGNALYZER_DATA_BUFFER_ADDR                             129
3580
3581 #define SIGNALYZER_COMMAND_VERSION                              0x41
3582 #define SIGNALYZER_COMMAND_RESET                                0x42
3583 #define SIGNALYZER_COMMAND_POWERCONTROL_GET             0x50
3584 #define SIGNALYZER_COMMAND_POWERCONTROL_SET             0x51
3585 #define SIGNALYZER_COMMAND_PWM_SET                              0x52
3586 #define SIGNALYZER_COMMAND_LED_SET                              0x53
3587 #define SIGNALYZER_COMMAND_ADC                                  0x54
3588 #define SIGNALYZER_COMMAND_GPIO_STATE                   0x55
3589 #define SIGNALYZER_COMMAND_GPIO_MODE                    0x56
3590 #define SIGNALYZER_COMMAND_GPIO_PORT                    0x57
3591 #define SIGNALYZER_COMMAND_I2C                                  0x58
3592
3593 #define SIGNALYZER_CHAN_A                                               1
3594 #define SIGNALYZER_CHAN_B                                               2
3595 /* LEDS use channel C */
3596 #define SIGNALYZER_CHAN_C                                               4
3597
3598 #define SIGNALYZER_LED_GREEN                                    1
3599 #define SIGNALYZER_LED_RED                                              2
3600
3601 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A                0x0301
3602 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG              0x0302
3603 #define SIGNALYZER_MODULE_TYPE_EM_JTAG                  0x0303
3604 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P    0x0304
3605 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P                0x0305
3606
3607
3608 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3609 {
3610 #if BUILD_FT2232_FTD2XX == 1
3611         return FT_WriteEE(ftdih, address, value);
3612 #elif BUILD_FT2232_LIBFTDI == 1
3613         return 0;
3614 #endif
3615 }
3616
3617 #if BUILD_FT2232_FTD2XX == 1
3618 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3619 {
3620         return FT_ReadEE(ftdih, address, value);
3621 }
3622 #endif
3623
3624 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3625         int on_time_ms, int off_time_ms, unsigned char cycles)
3626 {
3627         unsigned char on_time;
3628         unsigned char off_time;
3629
3630         if (on_time_ms < 0xFFFF)
3631                 on_time = (unsigned char)(on_time_ms / 62);
3632         else
3633                 on_time = 0xFF;
3634
3635         off_time = (unsigned char)(off_time_ms / 62);
3636
3637 #if BUILD_FT2232_FTD2XX == 1
3638         FT_STATUS status;
3639
3640         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3641                         ((uint32_t)(channel << 8) | led))) != FT_OK)
3642         {
3643                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %" PRIu32, status);
3644                 return ERROR_JTAG_DEVICE_ERROR;
3645         }
3646
3647         if ((status = signalyzer_h_ctrl_write(
3648                         (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3649                         ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3650         {
3651                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %" PRIu32, status);
3652                 return ERROR_JTAG_DEVICE_ERROR;
3653         }
3654
3655         if ((status = signalyzer_h_ctrl_write(
3656                         (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3657                         ((uint32_t)cycles))) != FT_OK)
3658         {
3659                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %" PRIu32, status);
3660                 return ERROR_JTAG_DEVICE_ERROR;
3661         }
3662
3663         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3664                         SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3665         {
3666                 LOG_ERROR("signalyzer_h_ctrl_write  returned: %" PRIu32, status);
3667                 return ERROR_JTAG_DEVICE_ERROR;
3668         }
3669
3670         return ERROR_OK;
3671 #elif BUILD_FT2232_LIBFTDI == 1
3672         int retval;
3673
3674         if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3675                         ((uint32_t)(channel << 8) | led))) < 0)
3676         {
3677                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3678                                 ftdi_get_error_string(&ftdic));
3679                 return ERROR_JTAG_DEVICE_ERROR;
3680         }
3681
3682         if ((retval = signalyzer_h_ctrl_write(
3683                         (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3684                         ((uint32_t)(on_time << 8) | off_time))) < 0)
3685         {
3686                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3687                                 ftdi_get_error_string(&ftdic));
3688                 return ERROR_JTAG_DEVICE_ERROR;
3689         }
3690
3691         if ((retval = signalyzer_h_ctrl_write(
3692                         (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3693                         (uint32_t)cycles)) < 0)
3694         {
3695                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3696                                 ftdi_get_error_string(&ftdic));
3697                 return ERROR_JTAG_DEVICE_ERROR;
3698         }
3699
3700         if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3701                         SIGNALYZER_COMMAND_LED_SET)) < 0)
3702         {
3703                 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3704                                 ftdi_get_error_string(&ftdic));
3705                 return ERROR_JTAG_DEVICE_ERROR;
3706         }
3707
3708         return ERROR_OK;
3709 #endif
3710 }
3711
3712 static int signalyzer_h_init(void)
3713 {
3714 #if BUILD_FT2232_FTD2XX == 1
3715         FT_STATUS status;
3716         int i;
3717 #endif
3718
3719         char *end_of_desc;
3720
3721         uint16_t read_buf[12] = { 0 };
3722
3723         /* turn on center green led */
3724         signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3725                         0xFFFF, 0x00, 0x00);
3726
3727         /* determine what channel config wants to open
3728          * TODO: change me... current implementation is made to work
3729          * with openocd description parsing.
3730          */
3731         end_of_desc = strrchr(ft2232_device_desc, 0x00);
3732
3733         if (end_of_desc)
3734         {
3735                 signalyzer_h_side = *(end_of_desc - 1);
3736                 if (signalyzer_h_side == 'B')
3737                         signalyzer_h_side = SIGNALYZER_CHAN_B;
3738                 else
3739                         signalyzer_h_side = SIGNALYZER_CHAN_A;
3740         }
3741         else
3742         {
3743                 LOG_ERROR("No Channel was specified");
3744                 return ERROR_FAIL;
3745         }
3746
3747         signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3748                         1000, 1000, 0xFF);
3749
3750 #if BUILD_FT2232_FTD2XX == 1
3751         /* read signalyzer versionining information */
3752         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3753                         SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3754         {
3755                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3756                 return ERROR_JTAG_DEVICE_ERROR;
3757         }
3758
3759         for (i = 0; i < 10; i++)
3760         {
3761                 if ((status = signalyzer_h_ctrl_read(
3762                         (SIGNALYZER_DATA_BUFFER_ADDR + i),
3763                         &read_buf[i])) != FT_OK)
3764                 {
3765                         LOG_ERROR("signalyzer_h_ctrl_read returned: %" PRIu32,
3766                                         status);
3767                         return ERROR_JTAG_DEVICE_ERROR;
3768                 }
3769         }
3770
3771         LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3772                         read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3773                         read_buf[4], read_buf[5], read_buf[6]);
3774
3775         /* set gpio register */
3776         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3777                         (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3778         {
3779                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3780                 return ERROR_JTAG_DEVICE_ERROR;
3781         }
3782
3783         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3784                         0x0404)) != FT_OK)
3785         {
3786                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3787                 return ERROR_JTAG_DEVICE_ERROR;
3788         }
3789
3790         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3791                         SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3792         {
3793                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3794                 return ERROR_JTAG_DEVICE_ERROR;
3795         }
3796
3797         /* read adapter type information */
3798         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3799                         ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3800         {
3801                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3802                 return ERROR_JTAG_DEVICE_ERROR;
3803         }
3804
3805         if ((status = signalyzer_h_ctrl_write(
3806                         (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3807         {
3808                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3809                 return ERROR_JTAG_DEVICE_ERROR;
3810         }
3811
3812         if ((status = signalyzer_h_ctrl_write(
3813                         (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3814         {
3815                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3816                 return ERROR_JTAG_DEVICE_ERROR;
3817         }
3818
3819         if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3820                         SIGNALYZER_COMMAND_I2C)) != FT_OK)
3821         {
3822                 LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32, status);
3823                 return ERROR_JTAG_DEVICE_ERROR;
3824         }
3825
3826         usleep(100000);
3827
3828         if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3829                         &read_buf[0])) != FT_OK)
3830         {
3831                 LOG_ERROR("signalyzer_h_ctrl_read returned: %" PRIu32, status);
3832                 return ERROR_JTAG_DEVICE_ERROR;
3833         }
3834
3835         if (read_buf[0] != 0x0498)
3836                 signalyzer_h_adapter_type = 0x0000;
3837         else
3838         {
3839                 for (i = 0; i < 4; i++)
3840                 {
3841                         if ((status = signalyzer_h_ctrl_read(
3842                                         (SIGNALYZER_DATA_BUFFER_ADDR + i),
3843                                         &read_buf[i])) != FT_OK)
3844                         {
3845                                 LOG_ERROR("signalyzer_h_ctrl_read returned: %" PRIu32,
3846                                         status);
3847                                 return ERROR_JTAG_DEVICE_ERROR;
3848                         }
3849                 }
3850
3851                 signalyzer_h_adapter_type = read_buf[0];
3852         }
3853
3854 #elif BUILD_FT2232_LIBFTDI == 1
3855         /* currently libftdi does not allow reading individual eeprom
3856          * locations, therefore adapter type cannot be detected.
3857          * override with most common type
3858          */
3859         signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3860 #endif
3861
3862         enum reset_types jtag_reset_config = jtag_get_reset_config();
3863
3864         /* ADAPTOR: EM_LT16_A */
3865         if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3866         {
3867                 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3868                         "detected. (HW: %2x).", (read_buf[1] >> 8));
3869
3870                 nTRST    = 0x10;
3871                 nTRSTnOE = 0x10;
3872                 nSRST    = 0x20;
3873                 nSRSTnOE = 0x20;
3874
3875                 low_output     = 0x08;
3876                 low_direction  = 0x1b;
3877
3878                 high_output    = 0x0;
3879                 high_direction = 0x0;
3880
3881                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3882                 {
3883                         low_direction &= ~nTRSTnOE; /* nTRST input */
3884                         low_output    &= ~nTRST;    /* nTRST = 0 */
3885                 }
3886                 else
3887                 {
3888                         low_direction |= nTRSTnOE;  /* nTRST output */
3889                         low_output    |= nTRST;     /* nTRST = 1 */
3890                 }
3891
3892                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3893                 {
3894                         low_direction |= nSRSTnOE;  /* nSRST output */
3895                         low_output    |= nSRST;     /* nSRST = 1 */
3896                 }
3897                 else
3898                 {
3899                         low_direction &= ~nSRSTnOE; /* nSRST input */
3900                         low_output    &= ~nSRST;    /* nSRST = 0 */
3901                 }
3902
3903 #if BUILD_FT2232_FTD2XX == 1
3904                 /* enable power to the module */
3905                 if ((status = signalyzer_h_ctrl_write(
3906                                 SIGNALYZER_DATA_BUFFER_ADDR,
3907                                 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3908                         != FT_OK)
3909                 {
3910                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3911                                 status);
3912                         return ERROR_JTAG_DEVICE_ERROR;
3913                 }
3914
3915                 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3916                                 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3917                 {
3918                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3919                                         status);
3920                         return ERROR_JTAG_DEVICE_ERROR;
3921                 }
3922
3923                 /* set gpio mode register */
3924                 if ((status = signalyzer_h_ctrl_write(
3925                                 SIGNALYZER_DATA_BUFFER_ADDR,
3926                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3927                 {
3928                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3929                                         status);
3930                         return ERROR_JTAG_DEVICE_ERROR;
3931                 }
3932
3933                 if ((status = signalyzer_h_ctrl_write(
3934                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3935                         != FT_OK)
3936                 {
3937                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3938                                         status);
3939                         return ERROR_JTAG_DEVICE_ERROR;
3940                 }
3941
3942                 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3943                                 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3944                 {
3945                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3946                                         status);
3947                         return ERROR_JTAG_DEVICE_ERROR;
3948                 }
3949
3950                 /* set gpio register */
3951                 if ((status = signalyzer_h_ctrl_write(
3952                                 SIGNALYZER_DATA_BUFFER_ADDR,
3953                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3954                 {
3955                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3956                                         status);
3957                         return ERROR_JTAG_DEVICE_ERROR;
3958                 }
3959
3960                 if ((status = signalyzer_h_ctrl_write(
3961                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3962                         != FT_OK)
3963                 {
3964                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3965                                         status);
3966                         return ERROR_JTAG_DEVICE_ERROR;
3967                 }
3968
3969                 if ((status = signalyzer_h_ctrl_write(
3970                                 SIGNALYZER_COMMAND_ADDR,
3971                                 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3972                 {
3973                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
3974                                         status);
3975                         return ERROR_JTAG_DEVICE_ERROR;
3976                 }
3977 #endif
3978         }
3979
3980         /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3981         else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3982                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3983                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG)  ||
3984                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3985         {
3986                 if (signalyzer_h_adapter_type
3987                                 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3988                         LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3989                                 "detected. (HW: %2x).", (read_buf[1] >> 8));
3990                 else if (signalyzer_h_adapter_type
3991                                 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3992                         LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3993                                 "(ARM JTAG with PSU) detected. (HW: %2x).",
3994                                 (read_buf[1] >> 8));
3995                 else if (signalyzer_h_adapter_type
3996                                 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3997                         LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3998                                 "detected. (HW: %2x).", (read_buf[1] >> 8));
3999                 else if (signalyzer_h_adapter_type
4000                                 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
4001                         LOG_INFO("Signalyzer: EM-JTAG-P "
4002                                 "(Generic JTAG with PSU) detected. (HW: %2x).",
4003                                 (read_buf[1] >> 8));
4004
4005                 nTRST          = 0x02;
4006                 nTRSTnOE       = 0x04;
4007                 nSRST          = 0x08;
4008                 nSRSTnOE       = 0x10;
4009
4010                 low_output     = 0x08;
4011                 low_direction  = 0x1b;
4012
4013                 high_output    = 0x0;
4014                 high_direction = 0x1f;
4015
4016                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4017                 {
4018                         high_output |= nTRSTnOE;
4019                         high_output &= ~nTRST;
4020                 }
4021                 else
4022                 {
4023                         high_output &= ~nTRSTnOE;
4024                         high_output |= nTRST;
4025                 }
4026
4027                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4028                 {
4029                         high_output &= ~nSRSTnOE;
4030                         high_output |= nSRST;
4031                 }
4032                 else
4033                 {
4034                         high_output |= nSRSTnOE;
4035                         high_output &= ~nSRST;
4036                 }
4037
4038 #if BUILD_FT2232_FTD2XX == 1
4039                 /* enable power to the module */
4040                 if ((status = signalyzer_h_ctrl_write(
4041                                 SIGNALYZER_DATA_BUFFER_ADDR,
4042                                 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
4043                         != FT_OK)
4044                 {
4045                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4046                                         status);
4047                         return ERROR_JTAG_DEVICE_ERROR;
4048                 }
4049
4050                 if ((status = signalyzer_h_ctrl_write(
4051                                 SIGNALYZER_COMMAND_ADDR,
4052                                 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
4053                 {
4054                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4055                                         status);
4056                         return ERROR_JTAG_DEVICE_ERROR;
4057                 }
4058
4059                 /* set gpio mode register (IO_16 and IO_17 set as analog
4060                  * inputs, other is gpio)
4061                  */
4062                 if ((status = signalyzer_h_ctrl_write(
4063                                 SIGNALYZER_DATA_BUFFER_ADDR,
4064                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4065                 {
4066                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4067                                         status);
4068                         return ERROR_JTAG_DEVICE_ERROR;
4069                 }
4070
4071                 if ((status = signalyzer_h_ctrl_write(
4072                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4073                         != FT_OK)
4074                 {
4075                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4076                                         status);
4077                         return ERROR_JTAG_DEVICE_ERROR;
4078                 }
4079
4080                 if ((status = signalyzer_h_ctrl_write(
4081                                 SIGNALYZER_COMMAND_ADDR,
4082                                 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4083                 {
4084                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4085                                         status);
4086                         return ERROR_JTAG_DEVICE_ERROR;
4087                 }
4088
4089                 /* set gpio register (all inputs, for -P modules,
4090                  * PSU will be turned off)
4091                  */
4092                 if ((status = signalyzer_h_ctrl_write(
4093                                 SIGNALYZER_DATA_BUFFER_ADDR,
4094                                 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4095                 {
4096                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4097                                         status);
4098                         return ERROR_JTAG_DEVICE_ERROR;
4099                 }
4100
4101                 if ((status = signalyzer_h_ctrl_write(
4102                                 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4103                         != FT_OK)
4104                 {
4105                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4106                                         status);
4107                         return ERROR_JTAG_DEVICE_ERROR;
4108                 }
4109
4110                 if ((status = signalyzer_h_ctrl_write(
4111                                 SIGNALYZER_COMMAND_ADDR,
4112                                 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4113                 {
4114                         LOG_ERROR("signalyzer_h_ctrl_write returned: %" PRIu32,
4115                                         status);
4116                         return ERROR_JTAG_DEVICE_ERROR;
4117                 }
4118 #endif
4119         }
4120
4121         else if (signalyzer_h_adapter_type == 0x0000)
4122         {
4123                 LOG_INFO("Signalyzer: No external modules were detected.");
4124
4125                 nTRST    = 0x10;
4126                 nTRSTnOE = 0x10;
4127                 nSRST    = 0x20;
4128                 nSRSTnOE = 0x20;
4129
4130                 low_output     = 0x08;
4131                 low_direction  = 0x1b;
4132
4133                 high_output    = 0x0;
4134                 high_direction = 0x0;
4135
4136                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4137                 {
4138                         low_direction &= ~nTRSTnOE; /* nTRST input */
4139                         low_output    &= ~nTRST;    /* nTRST = 0 */
4140                 }
4141                 else
4142                 {
4143                         low_direction |= nTRSTnOE;  /* nTRST output */
4144                         low_output    |= nTRST;     /* nTRST = 1 */
4145                 }
4146
4147                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4148                 {
4149                         low_direction |= nSRSTnOE;  /* nSRST output */
4150                         low_output    |= nSRST;     /* nSRST = 1 */
4151                 }
4152                 else
4153                 {
4154                         low_direction &= ~nSRSTnOE; /* nSRST input */
4155                         low_output    &= ~nSRST;    /* nSRST = 0 */
4156                 }
4157         }
4158         else
4159         {
4160                 LOG_ERROR("Unknown module type is detected: %.4x",
4161                                 signalyzer_h_adapter_type);
4162                 return ERROR_JTAG_DEVICE_ERROR;
4163         }
4164
4165         /* initialize low byte of controller for jtag operation */
4166         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4167         {
4168                 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4169                 return ERROR_JTAG_INIT_FAILED;
4170         }
4171
4172 #if BUILD_FT2232_FTD2XX == 1
4173         if (ftdi_device == FT_DEVICE_2232H)
4174         {
4175                 /* initialize high byte of controller for jtag operation */
4176                 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4177                 {
4178                         LOG_ERROR("couldn't initialize Signalyzer-H layout");
4179                         return ERROR_JTAG_INIT_FAILED;
4180                 }
4181         }
4182 #elif BUILD_FT2232_LIBFTDI == 1
4183         if (ftdi_device == TYPE_2232H)
4184         {
4185                 /* initialize high byte of controller for jtag operation */
4186                 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4187                 {
4188                         LOG_ERROR("couldn't initialize Signalyzer-H layout");
4189                         return ERROR_JTAG_INIT_FAILED;
4190                 }
4191         }
4192 #endif
4193         return ERROR_OK;
4194 }
4195
4196 static void signalyzer_h_reset(int trst, int srst)
4197 {
4198         enum reset_types jtag_reset_config = jtag_get_reset_config();
4199
4200         /* ADAPTOR: EM_LT16_A */
4201         if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4202         {
4203                 if (trst == 1)
4204                 {
4205                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4206                                 /* switch to output pin (output is low) */
4207                                 low_direction |= nTRSTnOE;
4208                         else
4209                                 /* switch output low */
4210                                 low_output &= ~nTRST;
4211                 }
4212                 else if (trst == 0)
4213                 {
4214                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4215                                 /* switch to input pin (high-Z + internal
4216                                  * and external pullup) */
4217                                 low_direction &= ~nTRSTnOE;
4218                         else
4219                                 /* switch output high */
4220                                 low_output |= nTRST;
4221                 }
4222
4223                 if (srst == 1)
4224                 {
4225                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4226                                 /* switch output low */
4227                                 low_output &= ~nSRST;
4228                         else
4229                                 /* switch to output pin (output is low) */
4230                                 low_direction |= nSRSTnOE;
4231                 }
4232                 else if (srst == 0)
4233                 {
4234                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4235                                 /* switch output high */
4236                                 low_output |= nSRST;
4237                         else
4238                                 /* switch to input pin (high-Z) */
4239                                 low_direction &= ~nSRSTnOE;
4240                 }
4241
4242                 /* command "set data bits low byte" */
4243                 buffer_write(0x80);
4244                 buffer_write(low_output);
4245                 buffer_write(low_direction);
4246                 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4247                                 "low_direction: 0x%2.2x",
4248                                 trst, srst, low_output, low_direction);
4249         }
4250         /* ADAPTOR: EM_ARM_JTAG,  EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4251         else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4252                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4253                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG)  ||
4254                                 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4255         {
4256                 if (trst == 1)
4257                 {
4258                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4259                                 high_output &= ~nTRSTnOE;
4260                         else
4261                                 high_output &= ~nTRST;
4262                 }
4263                 else if (trst == 0)
4264                 {
4265                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4266                                 high_output |= nTRSTnOE;
4267                         else
4268                                 high_output |= nTRST;
4269                 }
4270
4271                 if (srst == 1)
4272                 {
4273                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4274                                 high_output &= ~nSRST;
4275                         else
4276                                 high_output &= ~nSRSTnOE;
4277                 }
4278                 else if (srst == 0)
4279                 {
4280                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4281                                 high_output |= nSRST;
4282                         else
4283                                 high_output |= nSRSTnOE;
4284                 }
4285
4286                 /* command "set data bits high byte" */
4287                 buffer_write(0x82);
4288                 buffer_write(high_output);
4289                 buffer_write(high_direction);
4290                 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4291                                 "high_direction: 0x%2.2x",
4292                                 trst, srst, high_output, high_direction);
4293         }
4294         else if (signalyzer_h_adapter_type == 0x0000)
4295         {
4296                 if (trst == 1)
4297                 {
4298                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4299                                 /* switch to output pin (output is low) */
4300                                 low_direction |= nTRSTnOE;
4301                         else
4302                                 /* switch output low */
4303                                 low_output &= ~nTRST;
4304                 }
4305                 else if (trst == 0)
4306                 {
4307                         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4308                                 /* switch to input pin (high-Z + internal
4309                                  * and external pullup) */
4310                                 low_direction &= ~nTRSTnOE;
4311                         else
4312                                 /* switch output high */
4313                                 low_output |= nTRST;
4314                 }
4315
4316                 if (srst == 1)
4317                 {
4318                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4319                                 /* switch output low */
4320                                 low_output &= ~nSRST;
4321                         else
4322                                 /* switch to output pin (output is low) */
4323                                 low_direction |= nSRSTnOE;
4324                 }
4325                 else if (srst == 0)
4326                 {
4327                         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4328                                 /* switch output high */
4329                                 low_output |= nSRST;
4330                         else
4331                                 /* switch to input pin (high-Z) */
4332                                 low_direction &= ~nSRSTnOE;
4333                 }
4334
4335                 /* command "set data bits low byte" */
4336                 buffer_write(0x80);
4337                 buffer_write(low_output);
4338                 buffer_write(low_direction);
4339                 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4340                                 "low_direction: 0x%2.2x",
4341                                 trst, srst, low_output, low_direction);
4342         }
4343 }
4344
4345 static void signalyzer_h_blink(void)
4346 {
4347         signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4348 }
4349
4350 /********************************************************************
4351  * Support for KT-LINK
4352  * JTAG adapter from KRISTECH
4353  * http://www.kristech.eu
4354  *******************************************************************/
4355 static int ktlink_init(void)
4356 {
4357         uint8_t  swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4358
4359         low_output    = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4360         low_direction = 0x3B;          // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4361
4362         /* initialize low byte for jtag */
4363         if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
4364         {
4365                 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4366                 return ERROR_JTAG_INIT_FAILED;
4367         }
4368
4369         nTRST    = 0x01;
4370         nSRST    = 0x02;
4371         nTRSTnOE = 0x04;
4372         nSRSTnOE = 0x08;
4373
4374         high_output    = 0x80; // turn LED on
4375         high_direction = 0xFF; // all outputs
4376
4377         enum reset_types jtag_reset_config = jtag_get_reset_config();
4378
4379         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4380                 high_output |= nTRSTnOE;
4381                 high_output &= ~nTRST;
4382         } else {
4383                 high_output &= ~nTRSTnOE;
4384                 high_output |= nTRST;
4385         }
4386
4387         if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4388                 high_output &= ~nSRSTnOE;
4389                 high_output |= nSRST;
4390         } else {
4391                 high_output |= nSRSTnOE;
4392                 high_output &= ~nSRST;
4393         }
4394
4395         /* initialize high byte for jtag */
4396         if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
4397         {
4398                 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4399                 return ERROR_JTAG_INIT_FAILED;
4400         }
4401
4402         return ERROR_OK;
4403 }
4404
4405 static void ktlink_reset(int trst, int srst)
4406 {
4407         enum reset_types jtag_reset_config = jtag_get_reset_config();
4408
4409         if (trst == 1) {
4410                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4411                         high_output &= ~nTRSTnOE;
4412                 else
4413                         high_output &= ~nTRST;
4414         } else if (trst == 0) {
4415                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4416                         high_output |= nTRSTnOE;
4417                 else
4418                         high_output |= nTRST;
4419         }
4420
4421         if (srst == 1) {
4422                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4423                         high_output &= ~nSRST;
4424                 else
4425                         high_output &= ~nSRSTnOE;
4426         } else if (srst == 0) {
4427                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4428                         high_output |= nSRST;
4429                 else
4430                         high_output |= nSRSTnOE;
4431         }
4432
4433         buffer_write(0x82); // command "set data bits high byte"
4434         buffer_write(high_output);
4435         buffer_write(high_direction);
4436         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4437 }
4438
4439 static void ktlink_blink(void)
4440 {
4441         /* LED connected to ACBUS7 */
4442         high_output ^= 0x80;
4443
4444         buffer_write(0x82);  // command "set data bits high byte"
4445         buffer_write(high_output);
4446         buffer_write(high_direction);
4447 }
4448
4449 static const struct command_registration ft2232_command_handlers[] = {
4450         {
4451                 .name = "ft2232_device_desc",
4452                 .handler = &ft2232_handle_device_desc_command,
4453                 .mode = COMMAND_CONFIG,
4454                 .help = "set the USB device description of the FTDI FT2232 device",
4455                 .usage = "description_string",
4456         },
4457         {
4458                 .name = "ft2232_serial",
4459                 .handler = &ft2232_handle_serial_command,
4460                 .mode = COMMAND_CONFIG,
4461                 .help = "set the serial number of the FTDI FT2232 device",
4462                 .usage = "serial_string",
4463         },
4464         {
4465                 .name = "ft2232_layout",
4466                 .handler = &ft2232_handle_layout_command,
4467                 .mode = COMMAND_CONFIG,
4468                 .help = "set the layout of the FT2232 GPIO signals used "
4469                         "to control output-enables and reset signals",
4470                 .usage = "layout_name",
4471         },
4472         {
4473                 .name = "ft2232_vid_pid",
4474                 .handler = &ft2232_handle_vid_pid_command,
4475                 .mode = COMMAND_CONFIG,
4476                 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4477                 .usage = "(vid pid)* ",
4478         },
4479         {
4480                 .name = "ft2232_latency",
4481                 .handler = &ft2232_handle_latency_command,
4482                 .mode = COMMAND_CONFIG,
4483                 .help = "set the FT2232 latency timer to a new value",
4484                 .usage = "value",
4485         },
4486         COMMAND_REGISTRATION_DONE
4487 };
4488
4489 struct jtag_interface ft2232_interface = {
4490         .name = "ft2232",
4491         .supported = DEBUG_CAP_TMS_SEQ,
4492         .commands = ft2232_command_handlers,
4493         .transports = jtag_only,
4494
4495         .init = ft2232_init,
4496         .quit = ft2232_quit,
4497         .speed = ft2232_speed,
4498         .speed_div = ft2232_speed_div,
4499         .khz = ft2232_khz,
4500         .execute_queue = ft2232_execute_queue,
4501 };