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