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