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