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