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