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