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