RCLK is not supported, return error instead of crashing.
[fw/openocd] / src / jtag / ft2232.c
1 /***************************************************************************
2  *   Copyright (C) 2004, 2006 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #if IS_CYGWIN == 1
25 #include "windows.h"
26 #endif
27
28 #include "replacements.h"
29
30 /* project specific includes */
31 #include "log.h"
32 #include "types.h"
33 #include "jtag.h"
34 #include "configuration.h"
35 #include "time_support.h"
36
37 /* system includes */
38 #include <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 /* FT2232 access library includes */
43 #if BUILD_FT2232_FTD2XX == 1
44 #include <ftd2xx.h>
45 #elif BUILD_FT2232_LIBFTDI == 1
46 #include <ftdi.h>
47 #endif
48
49 /* enable this to debug io latency
50  */
51 #if 0
52 #define _DEBUG_USB_IO_
53 #endif
54
55 /* enable this to debug communication
56  */
57 #if 0
58 #define _DEBUG_USB_COMMS_
59 #endif
60
61 int ft2232_execute_queue(void);
62
63 int ft2232_speed(int speed);
64 int ft2232_speed_div(int speed, int *khz);
65 int ft2232_khz(int khz, int *jtag_speed);
66 int ft2232_register_commands(struct command_context_s *cmd_ctx);
67 int ft2232_init(void);
68 int ft2232_quit(void);
69
70 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75
76 char *ft2232_device_desc = NULL;
77 char *ft2232_serial = NULL;
78 char *ft2232_layout = NULL;
79 unsigned char ft2232_latency = 2;
80
81 #define MAX_USB_IDS     8
82 /* vid = pid = 0 marks the end of the list */
83 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
84 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
85
86 typedef struct ft2232_layout_s
87 {
88         char* name;
89         int(*init)(void);
90         void(*reset)(int trst, int srst);
91         void(*blink)(void);
92 } ft2232_layout_t;
93
94 /* init procedures for supported layouts */
95 int usbjtag_init(void);
96 int jtagkey_init(void);
97 int olimex_jtag_init(void);
98 int flyswatter_init(void);
99 int turtle_init(void);
100 int comstick_init(void);
101 int stm32stick_init(void);
102
103 /* reset procedures for supported layouts */
104 void usbjtag_reset(int trst, int srst);
105 void jtagkey_reset(int trst, int srst);
106 void olimex_jtag_reset(int trst, int srst);
107 void flyswatter_reset(int trst, int srst);
108 void turtle_reset(int trst, int srst);
109 void comstick_reset(int trst, int srst);
110 void stm32stick_reset(int trst, int srst);
111
112 /* blink procedures for layouts that support a blinking led */
113 void olimex_jtag_blink(void);
114 void turtle_jtag_blink(void);
115
116 ft2232_layout_t ft2232_layouts[] =
117 {
118         {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
119         {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
120         {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
121         {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
122         {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
123         {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
124         {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
125         {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
126         {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
127         {"comstick", comstick_init, comstick_reset, NULL},
128         {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
129         {NULL, NULL, NULL},
130 };
131
132 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
133
134 static ft2232_layout_t *layout;
135 static u8 low_output = 0x0;
136 static u8 low_direction = 0x0;
137 static u8 high_output = 0x0;
138 static u8 high_direction = 0x0;
139
140 #if BUILD_FT2232_FTD2XX == 1
141 static FT_HANDLE ftdih = NULL;
142 #elif BUILD_FT2232_LIBFTDI == 1
143 static struct ftdi_context ftdic;
144 #endif
145
146 static u8 *ft2232_buffer = NULL;
147 static int ft2232_buffer_size = 0;
148 static int ft2232_read_pointer = 0;
149 static int ft2232_expect_read = 0;
150 #define FT2232_BUFFER_SIZE      131072
151 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
152 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
153
154 jtag_interface_t ft2232_interface = 
155 {
156         .name = "ft2232",
157         .execute_queue = ft2232_execute_queue,
158         .speed = ft2232_speed,
159         .speed_div = ft2232_speed_div,
160         .khz = ft2232_khz, 
161         .register_commands = ft2232_register_commands,
162         .init = ft2232_init,
163         .quit = ft2232_quit,
164 };
165
166 int ft2232_write(u8 *buf, int size, u32* bytes_written)
167 {
168 #if BUILD_FT2232_FTD2XX == 1
169         FT_STATUS status;
170         DWORD dw_bytes_written;
171         if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
172         {
173                 *bytes_written = dw_bytes_written;
174                 LOG_ERROR("FT_Write returned: %lu", status);
175                 return ERROR_JTAG_DEVICE_ERROR;
176         }
177         else
178         {
179                 *bytes_written = dw_bytes_written;
180                 return ERROR_OK;        
181         }
182 #elif BUILD_FT2232_LIBFTDI == 1
183         int retval;
184         if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
185         {
186                 *bytes_written = 0;
187                 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
188                 return ERROR_JTAG_DEVICE_ERROR;
189         }
190         else
191         {
192                 *bytes_written = retval;
193                 return ERROR_OK;        
194         }
195 #endif
196 }
197
198 int ft2232_read(u8* buf, int size, u32* bytes_read)
199 {
200 #if BUILD_FT2232_FTD2XX == 1
201         DWORD dw_bytes_read;
202         FT_STATUS status;
203         int timeout = 5;
204         *bytes_read = 0;
205
206         while ((*bytes_read < size) && timeout--)
207         {
208                 if ((status = FT_Read(ftdih, buf + *bytes_read, size - 
209                         *bytes_read, &dw_bytes_read)) != FT_OK)         
210                 {
211                         *bytes_read = 0; 
212                         LOG_ERROR("FT_Read returned: %lu", status);
213                         return ERROR_JTAG_DEVICE_ERROR;
214                 }
215                 *bytes_read += dw_bytes_read; 
216         }
217 #elif BUILD_FT2232_LIBFTDI == 1
218         int retval;
219         int timeout = 100;
220         *bytes_read = 0;
221         
222         while ((*bytes_read < size) && timeout--)
223         {
224                 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
225                 {
226                         *bytes_read = 0;
227                         LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
228                         return ERROR_JTAG_DEVICE_ERROR;
229                 }
230                 *bytes_read += retval;
231         }
232 #endif
233
234         if (*bytes_read < size)
235         {
236                 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
237                 return ERROR_JTAG_DEVICE_ERROR;
238         }
239         
240         return ERROR_OK;
241 }
242
243 int ft2232_speed(int speed)
244 {
245         u8 buf[3];
246         int retval;
247         u32 bytes_written;
248
249         buf[0] = 0x86; /* command "set divisor" */
250         buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
251         buf[2] = (speed >> 8) & 0xff; /* valueH */
252         
253         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
254         if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
255         {
256                 LOG_ERROR("couldn't set FT2232 TCK speed");
257                 return retval;
258         }
259
260         return ERROR_OK;
261 }
262
263 int ft2232_speed_div(int speed, int *khz)
264 {
265         /* Take a look in the FT2232 manual, 
266          * AN2232C-01 Command Processor for
267          * MPSSE and MCU Host Bus. Chapter 3.8 */
268         
269         *khz = 6000 / (1+speed);
270         
271         return ERROR_OK;
272 }
273
274 int ft2232_khz(int khz, int *jtag_speed)
275 {
276         if (khz==0)
277         {
278                 LOG_ERROR("RCLK not supported");
279                 return ERROR_FAIL;
280         }
281         /* Take a look in the FT2232 manual, 
282          * AN2232C-01 Command Processor for
283          * MPSSE and MCU Host Bus. Chapter 3.8
284          * 
285          * We will calc here with a multiplier
286          * of 10 for better rounding later. */
287         
288         /* Calc speed, (6000 / khz) - 1 */
289         /* Use 65000 for better rounding */
290         *jtag_speed = (60000 / khz) - 10;
291         
292         /* Add 0.9 for rounding */
293         *jtag_speed += 9;
294         
295         /* Calc real speed */
296         *jtag_speed = *jtag_speed / 10;
297         
298         /* Check if speed is greater than 0 */
299         if (*jtag_speed < 0)
300         {
301                 *jtag_speed = 0;
302         }
303         
304         /* Check max value */
305         if (*jtag_speed > 0xFFFF)
306         {
307                 *jtag_speed = 0xFFFF;
308         }
309         
310         return ERROR_OK;
311 }
312
313 int ft2232_register_commands(struct command_context_s *cmd_ctx)
314 {
315         register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
316                 COMMAND_CONFIG, NULL);
317         register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
318                 COMMAND_CONFIG, NULL);
319         register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
320                 COMMAND_CONFIG, NULL);
321         register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
322                                          COMMAND_CONFIG, NULL);
323         register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
324                                          COMMAND_CONFIG, NULL);
325         return ERROR_OK;
326 }
327
328 void ft2232_end_state(enum tap_state state)
329 {
330         if (tap_move_map[state] != -1)
331                 end_state = state;
332         else
333         {
334                 LOG_ERROR("BUG: %i is not a valid end state", state);
335                 exit(-1);
336         }
337 }
338
339 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
340 {
341         int num_bytes = ((scan_size + 7) / 8);
342         int bits_left = scan_size;
343         int cur_byte = 0;
344
345         while(num_bytes-- > 1)
346         {
347                 buffer[cur_byte] = BUFFER_READ;
348                 cur_byte++;
349                 bits_left -= 8;
350         }
351
352         buffer[cur_byte] = 0x0;
353
354         if (bits_left > 1)
355         {
356                 buffer[cur_byte] = BUFFER_READ >> 1;
357         }
358
359         buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
360
361 }
362
363 void ft2232_debug_dump_buffer(void)
364 {
365         int i;
366         char line[256];
367         char *line_p = line;
368         
369         for (i = 0; i < ft2232_buffer_size; i++)
370         {
371                 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
372                 if (i % 16 == 15)
373                 {
374                         LOG_DEBUG("%s", line);
375                         line_p = line;
376                 }
377         }
378         
379         if (line_p != line)
380                 LOG_DEBUG("%s", line);
381 }
382
383 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
384 {
385         jtag_command_t *cmd;
386         u8 *buffer;
387         int scan_size;
388         enum scan_type type;
389         int retval;
390         u32 bytes_written;
391         u32 bytes_read;
392         
393 #ifdef _DEBUG_USB_IO_
394         struct timeval start, inter, inter2, end;
395         struct timeval d_inter, d_inter2, d_end;
396 #endif
397
398 #ifdef _DEBUG_USB_COMMS_
399         LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
400         ft2232_debug_dump_buffer();
401 #endif
402
403 #ifdef _DEBUG_USB_IO_
404         gettimeofday(&start, NULL);     
405 #endif
406
407         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
408         {
409                 LOG_ERROR("couldn't write MPSSE commands to FT2232");
410                 exit(-1);
411         }
412         
413 #ifdef _DEBUG_USB_IO_
414         gettimeofday(&inter, NULL);     
415 #endif
416         
417         if (ft2232_expect_read)
418         {
419                 int timeout = 100;
420                 ft2232_buffer_size = 0;
421                 
422 #ifdef _DEBUG_USB_IO_
423                 gettimeofday(&inter2, NULL);    
424 #endif
425                 
426                 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
427                 {
428                         LOG_ERROR("couldn't read from FT2232");
429                         exit(-1);
430                 }
431                 
432 #ifdef _DEBUG_USB_IO_
433                 gettimeofday(&end, NULL);       
434
435                 timeval_subtract(&d_inter, &inter, &start);
436                 timeval_subtract(&d_inter2, &inter2, &start);
437                 timeval_subtract(&d_end, &end, &start);
438
439                 LOG_INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
440 #endif
441         
442                 
443                 ft2232_buffer_size = bytes_read;
444                 
445                 if (ft2232_expect_read != ft2232_buffer_size)
446                 {
447                         LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
448                         ft2232_debug_dump_buffer();     
449
450                         exit(-1);
451                 }
452
453 #ifdef _DEBUG_USB_COMMS_
454                 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
455                 ft2232_debug_dump_buffer();
456 #endif
457         }
458
459         ft2232_expect_read = 0;
460         ft2232_read_pointer = 0;
461         
462         /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
463          * that wasn't handled by a caller-provided error handler
464          */ 
465         retval = ERROR_OK;
466         
467         cmd = first;
468         while (cmd != last)
469         {
470                 switch (cmd->type)
471                 {
472                         case JTAG_SCAN:
473                                 type = jtag_scan_type(cmd->cmd.scan);
474                                 if (type != SCAN_OUT)
475                                 {
476                                         scan_size = jtag_scan_size(cmd->cmd.scan);
477                                         buffer = calloc(CEIL(scan_size, 8), 1);
478                                         ft2232_read_scan(type, buffer, scan_size);
479                                         if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
480                                                 retval = ERROR_JTAG_QUEUE_FAILED;
481                                         free(buffer);
482                                 }
483                                 break;
484                         default:
485                                 break;
486                 }
487                 cmd = cmd->next;
488         }
489         
490         ft2232_buffer_size = 0;
491
492         return retval;
493 }
494
495 void ft2232_add_pathmove(pathmove_command_t *cmd)
496 {
497         int num_states = cmd->num_states;
498         u8 tms_byte;
499         int state_count;
500
501         state_count = 0;
502         while (num_states)
503         {
504                 int bit_count = 0;
505                 
506                 int num_states_batch = num_states > 7 ? 7 : num_states;
507
508                 tms_byte = 0x0;
509                 /* command "Clock Data to TMS/CS Pin (no Read)" */
510                 BUFFER_ADD = 0x4b;
511                 /* number of states remaining */
512                 BUFFER_ADD = num_states_batch - 1;
513                 
514                 while (num_states_batch--)
515                 {
516                         if (tap_transitions[cur_state].low == cmd->path[state_count])
517                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
518                         else if (tap_transitions[cur_state].high == cmd->path[state_count])
519                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
520                         else
521                         {
522                                 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
523                                 exit(-1);
524                         }
525
526                         cur_state = cmd->path[state_count];
527                         state_count++;
528                         num_states--;
529                 }
530                 
531                 BUFFER_ADD = tms_byte;
532         }
533         
534         end_state = cur_state;
535 }
536
537 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
538 {
539         int num_bytes = (scan_size + 7) / 8;
540         int bits_left = scan_size;
541         int cur_byte = 0;
542         int last_bit;
543
544         if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
545         {
546                 /* command "Clock Data to TMS/CS Pin (no Read)" */
547                 BUFFER_ADD = 0x4b;
548                 /* scan 7 bit */
549                 BUFFER_ADD = 0x6;
550                 /* TMS data bits */
551                 if (ir_scan)
552                 {
553                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
554                         cur_state = TAP_SI;
555                 }
556                 else
557                 {
558                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
559                         cur_state = TAP_SD;
560                 }
561                 /* LOG_DEBUG("added TMS scan (no read)"); */
562         }
563         
564         /* add command for complete bytes */
565         while (num_bytes > 1)
566         {
567                 int thisrun_bytes;
568                 if (type == SCAN_IO)
569                 {
570                         /* Clock Data Bytes In and Out LSB First */
571                         BUFFER_ADD = 0x39;
572                         /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
573                 }
574                 else if (type == SCAN_OUT)
575                 {
576                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
577                         BUFFER_ADD = 0x19;
578                         /* LOG_DEBUG("added TDI bytes (o)"); */
579                 }
580                 else if (type == SCAN_IN)
581                 {
582                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
583                         BUFFER_ADD = 0x28;
584                         /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
585                 }
586                 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
587                 num_bytes -= thisrun_bytes;
588                 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
589                 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
590                 if (type != SCAN_IN)
591                 {
592                         /* add complete bytes */
593                         while(thisrun_bytes-- > 0)
594                         {
595                                 BUFFER_ADD = buffer[cur_byte];
596                                 cur_byte++;
597                                 bits_left -= 8;
598                         }
599                 }
600                 else /* (type == SCAN_IN) */
601                 {
602                         bits_left -= 8 * (thisrun_bytes);
603                 }
604         }
605         
606         /* the most signifcant bit is scanned during TAP movement */
607         if (type != SCAN_IN)
608                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
609         else
610                 last_bit = 0;
611
612         /* process remaining bits but the last one */
613         if (bits_left > 1)
614         {
615                 if (type == SCAN_IO)
616                 {
617                         /* Clock Data Bits In and Out LSB First */
618                         BUFFER_ADD = 0x3b;
619                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
620                 }
621                 else if (type == SCAN_OUT)
622                 {
623                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
624                         BUFFER_ADD = 0x1b;
625                         /* LOG_DEBUG("added TDI bits (o)"); */
626                 }
627                 else if (type == SCAN_IN)
628                 {
629                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
630                         BUFFER_ADD = 0x2a;
631                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
632                 }
633                 BUFFER_ADD = bits_left - 2;
634                 if (type != SCAN_IN)
635                         BUFFER_ADD = buffer[cur_byte];
636         }
637
638         if ((ir_scan && (end_state == TAP_SI)) ||
639                 (!ir_scan && (end_state == TAP_SD)))
640         {
641                 if (type == SCAN_IO)
642                 {
643                         /* Clock Data Bits In and Out LSB First */
644                         BUFFER_ADD = 0x3b;
645                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
646                 }
647                 else if (type == SCAN_OUT)
648                 {
649                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
650                         BUFFER_ADD = 0x1b;
651                         /* LOG_DEBUG("added TDI bits (o)"); */
652                 }
653                 else if (type == SCAN_IN)
654                 {
655                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
656                         BUFFER_ADD = 0x2a;
657                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
658                 }
659                 BUFFER_ADD = 0x0;
660                 BUFFER_ADD = last_bit;
661         }
662         else
663         {
664                 /* move from Shift-IR/DR to end state */
665                 if (type != SCAN_OUT)
666                 {
667                         /* Clock Data to TMS/CS Pin with Read */
668                         BUFFER_ADD = 0x6b;
669                         /* LOG_DEBUG("added TMS scan (read)"); */
670                 }
671                 else
672                 {
673                         /* Clock Data to TMS/CS Pin (no Read) */
674                         BUFFER_ADD = 0x4b;
675                         /* LOG_DEBUG("added TMS scan (no read)"); */
676                 }
677                 BUFFER_ADD = 0x6;
678                 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
679                 cur_state = end_state;
680         }
681 }
682
683 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
684 {
685         int num_bytes = (scan_size + 7) / 8;
686         int bits_left = scan_size;
687         int cur_byte = 0;
688         int last_bit;
689         u8 *receive_buffer = malloc(CEIL(scan_size, 8));
690         u8 *receive_pointer = receive_buffer;
691         u32 bytes_written;
692         u32 bytes_read;
693         int retval;
694         int thisrun_read = 0;
695         
696         if (cmd->ir_scan)
697         {
698                 LOG_ERROR("BUG: large IR scans are not supported");
699                 exit(-1);
700         }
701
702         if (cur_state != TAP_SD)
703         {
704                 /* command "Clock Data to TMS/CS Pin (no Read)" */
705                 BUFFER_ADD = 0x4b;
706                 /* scan 7 bit */
707                 BUFFER_ADD = 0x6;
708                 /* TMS data bits */
709                 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
710                 cur_state = TAP_SD;
711         }
712         
713         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
714         {
715                 LOG_ERROR("couldn't write MPSSE commands to FT2232");
716                 exit(-1);
717         }
718         LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
719         ft2232_buffer_size = 0;
720         
721         /* add command for complete bytes */
722         while (num_bytes > 1)
723         {
724                 int thisrun_bytes;
725                 
726                 if (type == SCAN_IO)
727                 {
728                         /* Clock Data Bytes In and Out LSB First */
729                         BUFFER_ADD = 0x39;
730                         /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
731                 }
732                 else if (type == SCAN_OUT)
733                 {
734                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
735                         BUFFER_ADD = 0x19;
736                         /* LOG_DEBUG("added TDI bytes (o)"); */
737                 }
738                 else if (type == SCAN_IN)
739                 {
740                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
741                         BUFFER_ADD = 0x28;
742                         /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
743                 }
744                 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
745                 thisrun_read = thisrun_bytes;
746                 num_bytes -= thisrun_bytes;
747                 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
748                 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
749                 if (type != SCAN_IN)
750                 {
751                         /* add complete bytes */
752                         while(thisrun_bytes-- > 0)
753                         {
754                                 BUFFER_ADD = buffer[cur_byte];
755                                 cur_byte++;
756                                 bits_left -= 8;
757                         }
758                 }
759                 else /* (type == SCAN_IN) */
760                 {
761                         bits_left -= 8 * (thisrun_bytes);
762                 }
763
764                 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
765                 {
766                         LOG_ERROR("couldn't write MPSSE commands to FT2232");
767                         exit(-1);
768                 }
769                 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
770                 ft2232_buffer_size = 0;
771                 
772                 if (type != SCAN_OUT)
773                 {
774                         if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
775                         {
776                                 LOG_ERROR("couldn't read from FT2232");
777                                 exit(-1);
778                         }
779                         LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
780                         receive_pointer += bytes_read;
781                 }
782         }
783         
784         thisrun_read = 0;
785         
786         /* the most signifcant bit is scanned during TAP movement */
787         if (type != SCAN_IN)
788                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
789         else
790                 last_bit = 0;
791
792         /* process remaining bits but the last one */
793         if (bits_left > 1)
794         {
795                 if (type == SCAN_IO)
796                 {
797                         /* Clock Data Bits In and Out LSB First */
798                         BUFFER_ADD = 0x3b;
799                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
800                 }
801                 else if (type == SCAN_OUT)
802                 {
803                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
804                         BUFFER_ADD = 0x1b;
805                         /* LOG_DEBUG("added TDI bits (o)"); */
806                 }
807                 else if (type == SCAN_IN)
808                 {
809                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
810                         BUFFER_ADD = 0x2a;
811                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
812                 }
813                 BUFFER_ADD = bits_left - 2;
814                 if (type != SCAN_IN)
815                         BUFFER_ADD = buffer[cur_byte];
816                         
817                 if (type != SCAN_OUT)
818                         thisrun_read += 2;
819         }
820
821         if (end_state == TAP_SD)
822         {
823                 if (type == SCAN_IO)
824                 {
825                         /* Clock Data Bits In and Out LSB First */
826                         BUFFER_ADD = 0x3b;
827                         /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
828                 }
829                 else if (type == SCAN_OUT)
830                 {
831                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
832                         BUFFER_ADD = 0x1b;
833                         /* LOG_DEBUG("added TDI bits (o)"); */
834                 }
835                 else if (type == SCAN_IN)
836                 {
837                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
838                         BUFFER_ADD = 0x2a;
839                         /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
840                 }
841                 BUFFER_ADD = 0x0;
842                 BUFFER_ADD = last_bit;
843         }
844         else
845         {
846                 /* move from Shift-IR/DR to end state */
847                 if (type != SCAN_OUT)
848                 {
849                         /* Clock Data to TMS/CS Pin with Read */
850                         BUFFER_ADD = 0x6b;
851                         /* LOG_DEBUG("added TMS scan (read)"); */
852                 }
853                 else
854                 {
855                         /* Clock Data to TMS/CS Pin (no Read) */
856                         BUFFER_ADD = 0x4b;
857                         /* LOG_DEBUG("added TMS scan (no read)"); */
858                 }
859                 BUFFER_ADD = 0x6;
860                 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
861                 cur_state = end_state;
862         }
863         
864         if (type != SCAN_OUT)
865                 thisrun_read += 1;
866         
867         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
868         {
869                 LOG_ERROR("couldn't write MPSSE commands to FT2232");
870                 exit(-1);
871         }
872         LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
873         ft2232_buffer_size = 0;
874         
875         if (type != SCAN_OUT)
876         {
877                 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
878                 {
879                         LOG_ERROR("couldn't read from FT2232");
880                         exit(-1);
881                 }
882                 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
883                 receive_pointer += bytes_read;
884         }
885         
886         return ERROR_OK;
887 }
888
889 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
890 {
891         int predicted_size = 3;
892         int num_bytes = (scan_size - 1) / 8;
893         
894         if (cur_state != TAP_SD)
895                 predicted_size += 3;
896         
897         if (type == SCAN_IN)    /* only from device to host */
898         {
899                 /* complete bytes */
900                 predicted_size += (CEIL(num_bytes, 65536)) * 3;
901                 /* remaining bits - 1 (up to 7) */
902                 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
903         }
904         else                                    /* host to device, or bidirectional */
905         {
906                 /* complete bytes */
907                 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
908                 /* remaining bits -1 (up to 7) */
909                 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
910         }
911
912         return predicted_size;
913 }
914
915 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
916 {
917         int predicted_size = 0;
918         
919         if (type != SCAN_OUT)
920         {
921                 /* complete bytes */
922                 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
923                 /* remaining bits - 1 */
924                 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
925                 /* last bit (from TMS scan) */
926                 predicted_size += 1;
927         }
928         
929         /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
930
931         return predicted_size;
932 }
933
934 void usbjtag_reset(int trst, int srst)
935 {
936         if (trst == 1)
937         {
938                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
939                         low_direction |= nTRSTnOE;      /* switch to output pin (output is low) */
940                 else
941                         low_output &= ~nTRST;   /* switch output low */
942         }
943         else if (trst == 0)
944         {
945                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
946                         low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
947                 else
948                         low_output |= nTRST; /* switch output high */
949         }
950
951         if (srst == 1)
952         {
953                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
954                         low_output &= ~nSRST;   /* switch output low */
955                 else
956                         low_direction |= nSRSTnOE;      /* switch to output pin (output is low) */
957         }
958         else if (srst == 0)
959         {
960                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
961                         low_output |= nSRST;    /* switch output high */
962                 else
963                         low_direction &= ~nSRSTnOE;     /* switch to input pin (high-Z) */
964         }
965         
966         /* command "set data bits low byte" */
967         BUFFER_ADD = 0x80;
968         BUFFER_ADD = low_output;
969         BUFFER_ADD = low_direction;
970
971 }
972
973 void jtagkey_reset(int trst, int srst)
974 {
975         if (trst == 1)
976         {
977                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
978                         high_output &= ~nTRSTnOE;
979                 else
980                         high_output &= ~nTRST;
981         }
982         else if (trst == 0)
983         {
984                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
985                         high_output |= nTRSTnOE;
986                 else
987                         high_output |= nTRST;
988         }
989
990         if (srst == 1)
991         {
992                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
993                         high_output &= ~nSRST;
994                 else
995                         high_output &= ~nSRSTnOE;
996         }
997         else if (srst == 0)
998         {
999                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1000                         high_output |= nSRST;
1001                 else
1002                         high_output |= nSRSTnOE;
1003         }
1004         
1005         /* command "set data bits high byte" */
1006         BUFFER_ADD = 0x82;
1007         BUFFER_ADD = high_output;
1008         BUFFER_ADD = high_direction;
1009         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1010 }
1011
1012 void olimex_jtag_reset(int trst, int srst)
1013 {
1014         if (trst == 1)
1015         {
1016                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1017                         high_output &= ~nTRSTnOE;
1018                 else
1019                         high_output &= ~nTRST;
1020         }
1021         else if (trst == 0)
1022         {
1023                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1024                         high_output |= nTRSTnOE;
1025                 else
1026                         high_output |= nTRST;
1027         }
1028
1029         if (srst == 1)
1030         {
1031                 high_output |= nSRST;
1032         }
1033         else if (srst == 0)
1034         {
1035                 high_output &= ~nSRST;
1036         }
1037
1038         /* command "set data bits high byte" */
1039         BUFFER_ADD = 0x82;
1040         BUFFER_ADD = high_output;
1041         BUFFER_ADD = high_direction;
1042         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1043 }
1044
1045 void flyswatter_reset(int trst, int srst)
1046 {
1047         if (trst == 1)
1048         {
1049                 low_output &= ~nTRST;
1050         }
1051         else if (trst == 0)
1052         {
1053                 low_output |= nTRST;
1054         }
1055
1056         if (srst == 1)
1057         {
1058                 low_output |= nSRST;
1059         }
1060         else if (srst == 0)
1061         {
1062                 low_output &= ~nSRST;
1063         }
1064
1065         /* command "set data bits low byte" */
1066         BUFFER_ADD = 0x80;
1067         BUFFER_ADD = low_output;
1068         BUFFER_ADD = low_direction;
1069         LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1070 }
1071
1072 void turtle_reset(int trst, int srst)
1073 {
1074         trst = trst;
1075         
1076         if (srst == 1)
1077         {
1078                 low_output |= nSRST;
1079         }
1080         else if (srst == 0)
1081         {
1082                 low_output &= ~nSRST;
1083         }
1084         
1085         /* command "set data bits low byte" */
1086         BUFFER_ADD = 0x80;
1087         BUFFER_ADD = low_output;
1088         BUFFER_ADD = low_direction;
1089         LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1090 }
1091
1092 void comstick_reset(int trst, int srst)
1093 {
1094         if (trst == 1)
1095         {
1096                 high_output &= ~nTRST;
1097         }
1098         else if (trst == 0)
1099         {
1100                 high_output |= nTRST;
1101         }
1102
1103         if (srst == 1)
1104         {
1105                 high_output &= ~nSRST;
1106         }
1107         else if (srst == 0)
1108         {
1109                 high_output |= nSRST;
1110         }
1111         
1112         /* command "set data bits high byte" */
1113         BUFFER_ADD = 0x82;
1114         BUFFER_ADD = high_output;
1115         BUFFER_ADD = high_direction;
1116         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1117 }
1118
1119 void stm32stick_reset(int trst, int srst)
1120 {
1121         if (trst == 1)
1122         {
1123                 high_output &= ~nTRST;
1124         }
1125         else if (trst == 0)
1126         {
1127                 high_output |= nTRST;
1128         }
1129
1130         if (srst == 1)
1131         {
1132                 low_output &= ~nSRST;
1133         }
1134         else if (srst == 0)
1135         {
1136                 low_output |= nSRST;
1137         }
1138         
1139         /* command "set data bits low byte" */
1140         BUFFER_ADD = 0x80;
1141         BUFFER_ADD = low_output;
1142         BUFFER_ADD = low_direction;
1143         
1144         /* command "set data bits high byte" */
1145         BUFFER_ADD = 0x82;
1146         BUFFER_ADD = high_output;
1147         BUFFER_ADD = high_direction;
1148         LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1149 }
1150
1151 int ft2232_execute_queue()
1152 {
1153         jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1154         jtag_command_t *first_unsent = cmd;     /* next command that has to be sent */
1155         u8 *buffer;
1156         int scan_size;  /* size of IR or DR scan */
1157         enum scan_type type;
1158         int i;
1159         int predicted_size = 0;
1160         int require_send = 0;
1161         int retval;
1162         
1163         /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1164          * that wasn't handled by a caller-provided error handler
1165          */ 
1166         retval = ERROR_OK;
1167
1168         ft2232_buffer_size = 0;
1169         ft2232_expect_read = 0;
1170         
1171         /* blink, if the current layout has that feature */
1172         if (layout->blink)
1173                 layout->blink();
1174
1175         while (cmd)
1176         {
1177                 switch(cmd->type)
1178                 {
1179                         case JTAG_END_STATE:
1180                                 if (cmd->cmd.end_state->end_state != -1)
1181                                         ft2232_end_state(cmd->cmd.end_state->end_state);
1182                                 break;
1183                         case JTAG_RESET:
1184                                 /* only send the maximum buffer size that FT2232C can handle */
1185                                 predicted_size = 3;
1186                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1187                                 {
1188                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1189                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1190                                         require_send = 0;
1191                                         first_unsent = cmd;
1192                                 }
1193
1194                                 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1195                                 {
1196                                         cur_state = TAP_TLR;
1197                                 }
1198                                 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1199                                 require_send = 1;
1200                                 
1201 #ifdef _DEBUG_JTAG_IO_                          
1202                                 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1203 #endif
1204                                 break;
1205                         case JTAG_RUNTEST:
1206                                 /* only send the maximum buffer size that FT2232C can handle */
1207                                 predicted_size = 0;
1208                                 if (cur_state != TAP_RTI)
1209                                         predicted_size += 3;
1210                                 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1211                                 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1212                                         predicted_size += 3;
1213                                 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1214                                         predicted_size += 3;
1215                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1216                                 {
1217                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1218                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1219                                         require_send = 0;
1220                                         first_unsent = cmd;
1221                                 }
1222                                 if (cur_state != TAP_RTI)
1223                                 {
1224                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
1225                                         BUFFER_ADD = 0x4b;
1226                                         /* scan 7 bit */
1227                                         BUFFER_ADD = 0x6;
1228                                         /* TMS data bits */
1229                                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1230                                         cur_state = TAP_RTI;
1231                                         require_send = 1;
1232                                 }
1233                                 i = cmd->cmd.runtest->num_cycles;
1234                                 while (i > 0)
1235                                 {
1236                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
1237                                         BUFFER_ADD = 0x4b;
1238                                         /* scan 7 bit */
1239                                         BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1240                                         /* TMS data bits */
1241                                         BUFFER_ADD = 0x0;
1242                                         cur_state = TAP_RTI;
1243                                         i -= (i > 7) ? 7 : i;
1244                                         /* LOG_DEBUG("added TMS scan (no read)"); */
1245                                 }
1246                                 if (cmd->cmd.runtest->end_state != -1)
1247                                         ft2232_end_state(cmd->cmd.runtest->end_state);
1248                                 if (cur_state != end_state)
1249                                 {
1250                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
1251                                         BUFFER_ADD = 0x4b;
1252                                         /* scan 7 bit */
1253                                         BUFFER_ADD = 0x6;
1254                                         /* TMS data bits */
1255                                         BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1256                                         cur_state = end_state;
1257                                         /* LOG_DEBUG("added TMS scan (no read)"); */
1258                                 }
1259                                 require_send = 1;
1260 #ifdef _DEBUG_JTAG_IO_                          
1261                                 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1262 #endif
1263                                 break;
1264                         case JTAG_STATEMOVE:
1265                                 /* only send the maximum buffer size that FT2232C can handle */
1266                                 predicted_size = 3;
1267                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1268                                 {
1269                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1270                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1271                                         require_send = 0;
1272                                         first_unsent = cmd;
1273                                 }
1274                                 if (cmd->cmd.statemove->end_state != -1)
1275                                         ft2232_end_state(cmd->cmd.statemove->end_state);
1276                                 /* command "Clock Data to TMS/CS Pin (no Read)" */
1277                                 BUFFER_ADD = 0x4b;
1278                                 /* scan 7 bit */
1279                                 BUFFER_ADD = 0x6;
1280                                 /* TMS data bits */
1281                                 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1282                                 /* LOG_DEBUG("added TMS scan (no read)"); */
1283                                 cur_state = end_state;
1284                                 require_send = 1;
1285 #ifdef _DEBUG_JTAG_IO_                          
1286                                 LOG_DEBUG("statemove: %i", end_state);
1287 #endif
1288                                 break;
1289                         case JTAG_PATHMOVE:
1290                                 /* only send the maximum buffer size that FT2232C can handle */
1291                                 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1292                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1293                                 {
1294                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1295                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1296                                         require_send = 0;
1297                                         first_unsent = cmd;
1298                                 }
1299                                 ft2232_add_pathmove(cmd->cmd.pathmove);
1300                                 require_send = 1;
1301 #ifdef _DEBUG_JTAG_IO_                          
1302                                 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1303 #endif
1304                                 break;
1305                         case JTAG_SCAN:
1306                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1307                                 type = jtag_scan_type(cmd->cmd.scan);
1308                                 predicted_size = ft2232_predict_scan_out(scan_size, type);
1309                                 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1310                                 {
1311                                         LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1312                                         /* unsent commands before this */
1313                                         if (first_unsent != cmd)
1314                                                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1315                                                         retval = ERROR_JTAG_QUEUE_FAILED;
1316                                         
1317                                         /* current command */
1318                                         if (cmd->cmd.scan->end_state != -1)
1319                                                 ft2232_end_state(cmd->cmd.scan->end_state);
1320                                         ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1321                                         require_send = 0;
1322                                         first_unsent = cmd->next;
1323                                         if (buffer)
1324                                                 free(buffer);
1325                                         break;
1326                                 }
1327                                 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1328                                 {
1329                                         LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1330                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1331                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1332                                         require_send = 0;
1333                                         first_unsent = cmd;
1334                                 }
1335                                 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1336                                 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1337                                 if (cmd->cmd.scan->end_state != -1)
1338                                         ft2232_end_state(cmd->cmd.scan->end_state);
1339                                 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1340                                 require_send = 1;
1341                                 if (buffer)
1342                                         free(buffer);
1343 #ifdef _DEBUG_JTAG_IO_                          
1344                                 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1345 #endif
1346                                 break;
1347                         case JTAG_SLEEP:
1348                                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1349                                         retval = ERROR_JTAG_QUEUE_FAILED;
1350                                 first_unsent = cmd->next;
1351                                 jtag_sleep(cmd->cmd.sleep->us);
1352 #ifdef _DEBUG_JTAG_IO_                          
1353                                 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1354 #endif
1355                                 break;
1356                         default:
1357                                 LOG_ERROR("BUG: unknown JTAG command type encountered");
1358                                 exit(-1);
1359                 }
1360                 cmd = cmd->next;
1361         }
1362
1363         if (require_send > 0)
1364                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1365                         retval = ERROR_JTAG_QUEUE_FAILED;
1366
1367         return retval;
1368 }
1369
1370 #if BUILD_FT2232_FTD2XX == 1
1371 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1372 {
1373         FT_STATUS status;
1374         DWORD openex_flags = 0;
1375         char *openex_string = NULL;
1376         u8 latency_timer;
1377
1378         LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1379             ft2232_layout, vid, pid);
1380
1381 #if IS_WIN32 == 0
1382         /* Add non-standard Vid/Pid to the linux driver */
1383         if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1384         {
1385                 LOG_WARNING("couldn't add %4.4x:%4.4x",
1386                     vid, pid);
1387         }
1388 #endif
1389
1390         if (ft2232_device_desc && ft2232_serial)
1391         {
1392                 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1393                 ft2232_device_desc = NULL;
1394         }
1395         
1396         if (ft2232_device_desc)
1397         {
1398                 openex_string = ft2232_device_desc;
1399                 openex_flags = FT_OPEN_BY_DESCRIPTION;
1400         }
1401         else if (ft2232_serial)
1402         {
1403                 openex_string = ft2232_serial;
1404                 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1405         }
1406         else
1407         {
1408                 LOG_ERROR("neither device description nor serial number specified");
1409                 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1410                 
1411                 return ERROR_JTAG_INIT_FAILED;  
1412         }
1413
1414         if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1415         {
1416                 DWORD num_devices;
1417                 
1418                 if (more) {
1419                         LOG_WARNING("unable to open ftdi device (trying more): %lu",
1420                             status);
1421                         *try_more = 1;
1422                         return ERROR_JTAG_INIT_FAILED;
1423                 }
1424                 LOG_ERROR("unable to open ftdi device: %lu", status);
1425                 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1426                 if (status == FT_OK)
1427                 {
1428                         char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1429                         int i;
1430
1431                         for (i = 0; i < num_devices; i++)
1432                                 desc_array[i] = malloc(64);
1433                         desc_array[num_devices] = NULL;
1434
1435                         status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1436
1437                         if (status == FT_OK)
1438                         {
1439                                 LOG_ERROR("ListDevices: %lu\n", num_devices);
1440                                 for (i = 0; i < num_devices; i++)
1441                                         LOG_ERROR("%i: %s", i, desc_array[i]);
1442                         }
1443                         
1444                         for (i = 0; i < num_devices; i++)
1445                                 free(desc_array[i]);
1446                         free(desc_array);
1447                 }
1448                 else
1449                 {
1450                         LOG_ERROR("ListDevices: NONE\n");
1451                 }
1452                 return ERROR_JTAG_INIT_FAILED;
1453         }
1454
1455         if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1456         {
1457                 LOG_ERROR("unable to set latency timer: %lu", status);
1458                 return ERROR_JTAG_INIT_FAILED;
1459         }
1460         
1461         if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1462         {
1463                 LOG_ERROR("unable to get latency timer: %lu", status);
1464                 return ERROR_JTAG_INIT_FAILED;
1465         }
1466         else
1467         {
1468                 LOG_DEBUG("current latency timer: %i", latency_timer);
1469         }
1470         
1471         if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1472         {
1473                 LOG_ERROR("unable to set timeouts: %lu", status);
1474                 return ERROR_JTAG_INIT_FAILED;
1475         }
1476
1477         if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1478         {
1479                 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1480                 return ERROR_JTAG_INIT_FAILED;
1481         }
1482
1483         return ERROR_OK;
1484 }
1485
1486 static int ft2232_purge_ftd2xx(void)
1487 {
1488         FT_STATUS status;
1489
1490         if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1491         {
1492                 LOG_ERROR("error purging ftd2xx device: %lu", status);
1493                 return ERROR_JTAG_INIT_FAILED;
1494         }
1495
1496         return ERROR_OK;
1497 }
1498 #endif /* BUILD_FT2232_FTD2XX == 1 */
1499
1500 #if BUILD_FT2232_LIBFTDI == 1
1501 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1502 {
1503         u8 latency_timer;
1504
1505         LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1506                 ft2232_layout, vid, pid);
1507
1508         if (ftdi_init(&ftdic) < 0)
1509                 return ERROR_JTAG_INIT_FAILED;
1510
1511         /* context, vendor id, product id */
1512         if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1513                 ft2232_serial) < 0) {
1514                 if (more)
1515                         LOG_WARNING("unable to open ftdi device (trying more): %s",
1516                                 ftdic.error_str);
1517                 else
1518                         LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1519                 *try_more = 1;
1520                 return ERROR_JTAG_INIT_FAILED;
1521         }
1522
1523         if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1524         {
1525                 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1526                 return ERROR_JTAG_INIT_FAILED;
1527         }
1528
1529         if (ftdi_usb_reset(&ftdic) < 0)
1530         {
1531                 LOG_ERROR("unable to reset ftdi device");
1532                 return ERROR_JTAG_INIT_FAILED;
1533         }
1534
1535         if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1536         {
1537                 LOG_ERROR("unable to set latency timer");
1538                 return ERROR_JTAG_INIT_FAILED;
1539         }
1540         
1541         if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1542         {
1543                 LOG_ERROR("unable to get latency timer");
1544                 return ERROR_JTAG_INIT_FAILED;
1545         }
1546         else
1547         {
1548                 LOG_DEBUG("current latency timer: %i", latency_timer);
1549         }
1550
1551         ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1552
1553         return ERROR_OK;
1554 }
1555
1556 static int ft2232_purge_libftdi(void)
1557 {
1558         if (ftdi_usb_purge_buffers(&ftdic) < 0)
1559         {
1560                 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1561                 return ERROR_JTAG_INIT_FAILED;
1562         }
1563
1564         return ERROR_OK;
1565 }
1566 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1567
1568 int ft2232_init(void)
1569 {
1570         u8 buf[1];
1571         int retval;
1572         u32 bytes_written;
1573         ft2232_layout_t *cur_layout = ft2232_layouts;
1574         int i;
1575         
1576         if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1577         {
1578                 ft2232_layout = "usbjtag";
1579                 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1580         }
1581         
1582         while (cur_layout->name)
1583         {
1584                 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1585                 {
1586                         layout = cur_layout;
1587                         break;
1588                 }
1589                 cur_layout++;
1590         }
1591
1592         if (!layout)
1593         {
1594                 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1595                 return ERROR_JTAG_INIT_FAILED;
1596         }
1597         
1598         for (i = 0; 1; i++) {
1599                 /*
1600                  * "more indicates that there are more IDs to try, so we should
1601                  * not print an error for an ID mismatch (but for anything
1602                  * else, we should).
1603                  *
1604                  * try_more indicates that the error code returned indicates an
1605                  * ID mismatch (and nothing else) and that we should proceeed
1606                  * with the next ID pair.
1607                  */
1608                 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1609                 int try_more = 0;
1610
1611 #if BUILD_FT2232_FTD2XX == 1
1612                 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1613                         more, &try_more);
1614 #elif BUILD_FT2232_LIBFTDI == 1
1615                 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1616                         more, &try_more);
1617 #endif  
1618                 if (retval >= 0)
1619                         break;
1620                 if (!more || !try_more)
1621                         return retval;
1622         }
1623
1624         ft2232_buffer_size = 0;
1625         ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1626
1627         if (layout->init() != ERROR_OK)
1628                 return ERROR_JTAG_INIT_FAILED;
1629
1630         ft2232_speed(jtag_speed);
1631
1632         buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1633         if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1634         {
1635                 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1636                 return ERROR_JTAG_INIT_FAILED;
1637         }
1638
1639 #if BUILD_FT2232_FTD2XX == 1
1640         return ft2232_purge_ftd2xx();
1641 #elif BUILD_FT2232_LIBFTDI == 1
1642         return ft2232_purge_libftdi();
1643 #endif  
1644
1645         return ERROR_OK;
1646 }
1647
1648 int usbjtag_init(void)
1649 {
1650         u8 buf[3];
1651         u32 bytes_written;
1652         
1653         low_output = 0x08;
1654         low_direction = 0x0b;
1655         
1656         if (strcmp(ft2232_layout, "usbjtag") == 0)
1657         {
1658                 nTRST = 0x10;
1659                 nTRSTnOE = 0x10;
1660                 nSRST = 0x40;
1661                 nSRSTnOE = 0x40;
1662         }
1663         else if (strcmp(ft2232_layout, "signalyzer") == 0)
1664         {
1665                 nTRST = 0x10;
1666                 nTRSTnOE = 0x10;
1667                 nSRST = 0x20;
1668                 nSRSTnOE = 0x20;
1669         }
1670         else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1671         {
1672                 nTRST = 0x0;
1673                 nTRSTnOE = 0x00;
1674                 nSRST = 0x20;
1675                 nSRSTnOE = 0x20;
1676                 low_output = 0x88;
1677                 low_direction = 0x8b;
1678         }
1679         else
1680         {
1681                 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1682                 return ERROR_JTAG_INIT_FAILED;  
1683         }
1684         
1685         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1686         {
1687                 low_direction &= ~nTRSTnOE; /* nTRST input */
1688                 low_output &= ~nTRST; /* nTRST = 0 */
1689         }
1690         else
1691         {
1692                 low_direction |= nTRSTnOE; /* nTRST output */
1693                 low_output |= nTRST; /* nTRST = 1 */
1694         }
1695         
1696         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1697         {
1698                 low_direction |= nSRSTnOE; /* nSRST output */
1699                 low_output |= nSRST; /* nSRST = 1 */
1700         }
1701         else
1702         {
1703                 low_direction &= ~nSRSTnOE; /* nSRST input */
1704                 low_output &= ~nSRST; /* nSRST = 0 */
1705         }
1706         
1707         /* initialize low byte for jtag */
1708         buf[0] = 0x80; /* command "set data bits low byte" */
1709         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1710         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1711         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1712         
1713         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1714         {
1715                 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout"); 
1716                 return ERROR_JTAG_INIT_FAILED;
1717         }
1718
1719         return ERROR_OK;
1720 }
1721
1722 int jtagkey_init(void)
1723 {
1724         u8 buf[3];
1725         u32 bytes_written;
1726         
1727         low_output = 0x08;
1728         low_direction = 0x1b;
1729         
1730         /* initialize low byte for jtag */
1731         buf[0] = 0x80; /* command "set data bits low byte" */
1732         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1733         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1734         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1735         
1736         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1737         {
1738                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1739                 return ERROR_JTAG_INIT_FAILED;
1740         }
1741         
1742         if (strcmp(layout->name, "jtagkey") == 0)
1743         {
1744                 nTRST = 0x01;
1745                 nTRSTnOE = 0x4;
1746                 nSRST = 0x02;
1747                 nSRSTnOE = 0x08;
1748         }
1749         else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1750                 (strcmp(layout->name, "oocdlink") == 0))
1751         {
1752                 nTRST = 0x02;
1753                 nTRSTnOE = 0x1;
1754                 nSRST = 0x08;
1755                 nSRSTnOE = 0x04;
1756         }
1757         else
1758         {
1759                 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1760                 exit(-1);
1761         }
1762         
1763         high_output = 0x0;
1764         high_direction = 0x0f;
1765
1766         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1767         {
1768                 high_output |= nTRSTnOE;
1769                 high_output &= ~nTRST;
1770         }
1771         else
1772         {
1773                 high_output &= ~nTRSTnOE;
1774                 high_output |= nTRST;
1775         }
1776         
1777         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1778         {
1779                 high_output &= ~nSRSTnOE;
1780                 high_output |= nSRST;
1781         }
1782         else
1783         {
1784                 high_output |= nSRSTnOE;
1785                 high_output &= ~nSRST;
1786         }
1787         
1788         /* initialize high port */
1789         buf[0] = 0x82; /* command "set data bits high byte" */
1790         buf[1] = high_output; /* value */
1791         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1792         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1793         
1794         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1795         {
1796                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1797                 return ERROR_JTAG_INIT_FAILED;
1798         }
1799         
1800         return ERROR_OK;
1801 }
1802
1803 int olimex_jtag_init(void)
1804 {
1805         u8 buf[3];
1806         u32 bytes_written;
1807         
1808         low_output = 0x08;
1809         low_direction = 0x1b;
1810         
1811         /* initialize low byte for jtag */
1812         buf[0] = 0x80; /* command "set data bits low byte" */
1813         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1814         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1815         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1816         
1817         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1818         {
1819                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1820                 return ERROR_JTAG_INIT_FAILED;
1821         }
1822         
1823         nTRST = 0x01;
1824         nTRSTnOE = 0x4;
1825         nSRST = 0x02;
1826         nSRSTnOE = 0x00; /* no output enable for nSRST */
1827
1828         high_output = 0x0;
1829         high_direction = 0x0f;
1830
1831         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1832         {
1833                 high_output |= nTRSTnOE;
1834                 high_output &= ~nTRST;
1835         }
1836         else
1837         {
1838                 high_output &= ~nTRSTnOE;
1839                 high_output |= nTRST;
1840         }
1841         
1842         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1843         {
1844                 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1845         }
1846         else
1847         {
1848                 high_output &= ~nSRST;
1849         }
1850         
1851         /* turn red LED on */
1852         high_output |= 0x08;
1853         
1854         /* initialize high port */
1855         buf[0] = 0x82; /* command "set data bits high byte" */
1856         buf[1] = high_output; /* value */
1857         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1858         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1859         
1860         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1861         {
1862                 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1863                 return ERROR_JTAG_INIT_FAILED;
1864         }
1865         
1866         return ERROR_OK;
1867 }
1868
1869 int flyswatter_init(void)
1870 {
1871         u8 buf[3];
1872         u32 bytes_written;
1873         
1874         low_output = 0x18;
1875         low_direction = 0xfb;
1876         
1877         /* initialize low byte for jtag */
1878         buf[0] = 0x80; /* command "set data bits low byte" */
1879         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1880         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1881         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1882         
1883         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1884         {
1885                 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout"); 
1886                 return ERROR_JTAG_INIT_FAILED;
1887         }
1888         
1889         nTRST = 0x10;
1890         nTRSTnOE = 0x0; /* not output enable for nTRST */
1891         nSRST = 0x20;
1892         nSRSTnOE = 0x00; /* no output enable for nSRST */
1893
1894         high_output = 0x00;
1895         high_direction = 0x0c;
1896
1897         /* turn red LED1 on, LED2 off */
1898         high_output |= 0x08;
1899         
1900         /* initialize high port */
1901         buf[0] = 0x82; /* command "set data bits high byte" */
1902         buf[1] = high_output; /* value */
1903         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1904         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1905         
1906         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1907         {
1908                 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout"); 
1909                 return ERROR_JTAG_INIT_FAILED;
1910         }
1911         
1912         return ERROR_OK;
1913 }
1914
1915 int turtle_init(void)
1916 {
1917         u8 buf[3];
1918         u32 bytes_written;
1919         
1920         low_output = 0x08;
1921         low_direction = 0x5b;
1922         
1923         /* initialize low byte for jtag */
1924         buf[0] = 0x80; /* command "set data bits low byte" */
1925         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1926         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1927         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1928         
1929         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1930         {
1931                 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout"); 
1932                 return ERROR_JTAG_INIT_FAILED;
1933         }
1934         
1935         nSRST = 0x40;
1936         
1937         high_output = 0x00;
1938         high_direction = 0x0C;
1939         
1940         /* initialize high port */
1941         buf[0] = 0x82; /* command "set data bits high byte" */
1942         buf[1] = high_output;
1943         buf[2] = high_direction;
1944         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1945         
1946         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1947         {
1948                 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout"); 
1949                 return ERROR_JTAG_INIT_FAILED;
1950         }
1951         
1952         return ERROR_OK;
1953 }
1954
1955 int comstick_init(void)
1956 {
1957         u8 buf[3];
1958         u32 bytes_written;
1959         
1960         low_output = 0x08;
1961         low_direction = 0x0b;
1962         
1963         /* initialize low byte for jtag */
1964         buf[0] = 0x80; /* command "set data bits low byte" */
1965         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1966         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1967         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1968         
1969         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1970         {
1971                 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout"); 
1972                 return ERROR_JTAG_INIT_FAILED;
1973         }
1974         
1975         nTRST = 0x01;
1976         nTRSTnOE = 0x00; /* no output enable for nTRST */
1977         nSRST = 0x02;
1978         nSRSTnOE = 0x00; /* no output enable for nSRST */
1979         
1980         high_output = 0x03;
1981         high_direction = 0x03;
1982         
1983         /* initialize high port */
1984         buf[0] = 0x82; /* command "set data bits high byte" */
1985         buf[1] = high_output;
1986         buf[2] = high_direction;
1987         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1988         
1989         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1990         {
1991                 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout"); 
1992                 return ERROR_JTAG_INIT_FAILED;
1993         }
1994         
1995         return ERROR_OK;
1996 }
1997
1998 int stm32stick_init(void)
1999 {
2000         u8 buf[3];
2001         u32 bytes_written;
2002         
2003         low_output = 0x88;
2004         low_direction = 0x8b;
2005         
2006         /* initialize low byte for jtag */
2007         buf[0] = 0x80; /* command "set data bits low byte" */
2008         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2009         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2010         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2011         
2012         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2013         {
2014                 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout"); 
2015                 return ERROR_JTAG_INIT_FAILED;
2016         }
2017                 
2018         nTRST = 0x01;
2019         nTRSTnOE = 0x00; /* no output enable for nTRST */
2020         nSRST = 0x80;
2021         nSRSTnOE = 0x00; /* no output enable for nSRST */
2022         
2023         high_output = 0x01;
2024         high_direction = 0x03;
2025         
2026         /* initialize high port */
2027         buf[0] = 0x82; /* command "set data bits high byte" */
2028         buf[1] = high_output;
2029         buf[2] = high_direction;
2030         LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2031         
2032         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2033         {
2034                 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout"); 
2035                 return ERROR_JTAG_INIT_FAILED;
2036         }
2037         
2038         return ERROR_OK;
2039 }
2040
2041 void olimex_jtag_blink(void)
2042 {
2043         /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2044          * ACBUS3 is bit 3 of the GPIOH port
2045          */
2046         if (high_output & 0x08)
2047         {
2048                 /* set port pin high */
2049                 high_output &= 0x07;
2050         }
2051         else
2052         {
2053                 /* set port pin low */
2054                 high_output |= 0x08;
2055         }
2056         
2057         BUFFER_ADD = 0x82;
2058         BUFFER_ADD = high_output;
2059         BUFFER_ADD = high_direction;
2060 }
2061
2062 void turtle_jtag_blink(void)
2063 {
2064         /* 
2065    * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2066          */
2067         if (high_output & 0x08)
2068         {
2069                 high_output = 0x04;
2070         }
2071         else
2072         {
2073                 high_output = 0x08;
2074         }
2075         
2076         BUFFER_ADD = 0x82;
2077         BUFFER_ADD = high_output;
2078         BUFFER_ADD = high_direction;
2079 }
2080
2081 int ft2232_quit(void)
2082 {
2083 #if BUILD_FT2232_FTD2XX == 1
2084         FT_STATUS status;
2085
2086         status = FT_Close(ftdih);
2087 #elif BUILD_FT2232_LIBFTDI == 1
2088         ftdi_disable_bitbang(&ftdic);
2089         
2090         ftdi_usb_close(&ftdic);
2091         
2092         ftdi_deinit(&ftdic);
2093 #endif
2094
2095         free(ft2232_buffer);
2096         ft2232_buffer = NULL;
2097
2098         return ERROR_OK;
2099 }
2100
2101 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2102 {
2103         if (argc == 1)
2104         {
2105                 ft2232_device_desc = strdup(args[0]);
2106         }
2107         else
2108         {
2109                 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2110         }
2111         
2112         return ERROR_OK;
2113 }
2114
2115 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2116 {
2117         if (argc == 1)
2118         {
2119                 ft2232_serial = strdup(args[0]);
2120         }
2121         else
2122         {
2123                 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2124         }
2125         
2126         return ERROR_OK;
2127 }
2128
2129 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2130 {
2131         if (argc == 0)
2132                 return ERROR_OK;
2133
2134         ft2232_layout = malloc(strlen(args[0]) + 1);
2135         strcpy(ft2232_layout, args[0]);
2136
2137         return ERROR_OK;
2138 }
2139
2140 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2141 {
2142         int i;
2143
2144         if (argc > MAX_USB_IDS*2) {
2145                 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2146                         "(maximum is %d pairs)", MAX_USB_IDS);
2147                 argc = MAX_USB_IDS*2;
2148         }
2149         if (argc < 2 || (argc & 1))
2150         {
2151                 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2152                 if (argc < 2)
2153                         return ERROR_OK;
2154         }
2155
2156         for (i = 0; i+1 < argc; i += 2) {
2157                 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2158                 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2159         }
2160         /*
2161          * Explicitly terminate, in case there are multiples instances of
2162          * ft2232_vid_pid.
2163          */
2164         ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2165
2166         return ERROR_OK;
2167 }
2168
2169 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2170 {
2171         if (argc == 1)
2172         {
2173                 ft2232_latency = atoi(args[0]);
2174         }
2175         else
2176         {
2177                 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2178         }
2179         
2180         return ERROR_OK;
2181 }