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