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