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