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