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