- correctly enter debug state on a "soft_reset_halt" command
[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
97 /* reset procedures for supported layouts */
98 void usbjtag_reset(int trst, int srst);
99 void jtagkey_reset(int trst, int srst);
100 void olimex_jtag_reset(int trst, int srst);
101 void m5960_reset(int trst, int srst);
102
103 /* blink procedures for layouts that support a blinking led */
104 void olimex_jtag_blink(void);
105
106 ft2232_layout_t ft2232_layouts[] =
107 {
108         {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
109         {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
110         {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
111         {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
112         {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
113         {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
114         {"m5960", m5960_init, m5960_reset, NULL},
115         {NULL, NULL, NULL},
116 };
117
118 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
119
120 static ft2232_layout_t *layout;
121 static u8 low_output = 0x0;
122 static u8 low_direction = 0x0;
123 static u8 high_output = 0x0;
124 static u8 high_direction = 0x0;
125
126 #if BUILD_FT2232_FTD2XX == 1
127 static FT_HANDLE ftdih = NULL;
128 #elif BUILD_FT2232_LIBFTDI == 1
129 static struct ftdi_context ftdic;
130 #endif
131
132 static u8 *ft2232_buffer = NULL;
133 static int ft2232_buffer_size = 0;
134 static int ft2232_read_pointer = 0;
135 static int ft2232_expect_read = 0;
136 #define FT2232_BUFFER_SIZE      131072
137 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
138 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
139
140 jtag_interface_t ft2232_interface = 
141 {
142         
143         .name = "ft2232",
144         
145         .execute_queue = ft2232_execute_queue,
146         
147         .support_pathmove = 1,
148         
149         .speed = ft2232_speed,
150         .register_commands = ft2232_register_commands,
151         .init = ft2232_init,
152         .quit = ft2232_quit,
153 };
154
155 int ft2232_write(u8 *buf, int size, u32* bytes_written)
156 {
157 #if BUILD_FT2232_FTD2XX == 1
158         FT_STATUS status;
159         DWORD dw_bytes_written;
160         if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
161         {
162                 *bytes_written = dw_bytes_written;
163                 ERROR("FT_Write returned: %i", status);
164                 return ERROR_JTAG_DEVICE_ERROR;
165         }
166         else
167         {
168                 *bytes_written = dw_bytes_written;
169                 return ERROR_OK;        
170         }
171 #elif BUILD_FT2232_LIBFTDI == 1
172         int retval;
173         if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
174         {
175                 *bytes_written = 0;
176                 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
177                 return ERROR_JTAG_DEVICE_ERROR;
178         }
179         else
180         {
181                 *bytes_written = retval;
182                 return ERROR_OK;        
183         }
184 #endif
185 }
186
187 int ft2232_read(u8* buf, int size, u32* bytes_read)
188 {
189 #if BUILD_FT2232_FTD2XX == 1
190         DWORD dw_bytes_read;
191         FT_STATUS status;
192         int timeout = 5;
193         *bytes_read = 0;
194
195         while ((*bytes_read < size) && timeout--)
196         {
197                 if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
198                 {
199                         *bytes_read = 0; 
200                         ERROR("FT_Read returned: %i", status);
201                         return ERROR_JTAG_DEVICE_ERROR;
202                 }
203                 *bytes_read += dw_bytes_read; 
204         }
205 #elif BUILD_FT2232_LIBFTDI == 1
206         int retval;
207         int timeout = 100;
208         *bytes_read = 0;
209         
210         while ((*bytes_read < size) && timeout--)
211         {
212                 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
213                 {
214                         *bytes_read = 0;
215                         ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
216                         return ERROR_JTAG_DEVICE_ERROR;
217                 }
218                 *bytes_read += retval;
219         }
220 #endif
221
222         if (*bytes_read < size)
223         {
224                 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
225                 return ERROR_JTAG_DEVICE_ERROR;
226         }
227         
228         return ERROR_OK;
229 }
230
231 int ft2232_speed(int speed)
232 {
233         u8 buf[3];
234         int retval;
235         u32 bytes_written;
236
237         buf[0] = 0x86; /* command "set divisor" */
238         buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
239         buf[2] = (speed >> 8) & 0xff; /* valueH */
240         
241         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
242         if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
243         {
244                 ERROR("couldn't set FT2232 TCK speed");
245                 return retval;
246         }
247         
248         return ERROR_OK;
249 }
250
251 int ft2232_register_commands(struct command_context_s *cmd_ctx)
252 {
253         register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
254                 COMMAND_CONFIG, NULL);
255         register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
256                 COMMAND_CONFIG, NULL);
257         register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
258                 COMMAND_CONFIG, NULL);
259         register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
260                                          COMMAND_CONFIG, NULL);
261         return ERROR_OK;
262 }
263
264 void ft2232_end_state(state)
265 {
266         if (tap_move_map[state] != -1)
267                 end_state = state;
268         else
269         {
270                 ERROR("BUG: %i is not a valid end state", state);
271                 exit(-1);
272         }
273 }
274
275 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
276 {
277         int num_bytes = ((scan_size + 7) / 8);
278         int bits_left = scan_size;
279         int cur_byte = 0;
280
281         while(num_bytes-- > 1)
282         {
283                 buffer[cur_byte] = BUFFER_READ;
284                 cur_byte++;
285                 bits_left -= 8;
286         }
287
288         buffer[cur_byte] = 0x0;
289
290         if (bits_left > 1)
291         {
292                 buffer[cur_byte] = BUFFER_READ >> 1;
293         }
294
295         buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
296
297 }
298
299 void ft2232_debug_dump_buffer(void)
300 {
301         int i;
302         char line[256];
303         char *line_p = line;
304         
305         for (i = 0; i < ft2232_buffer_size; i++)
306         {
307                 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
308                 if (i % 16 == 15)
309                 {
310                         DEBUG("%s", line);
311                         line_p = line;
312                 }
313         }
314         
315         if (line_p != line)
316                 DEBUG("%s", line);
317 }
318
319 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
320 {
321         jtag_command_t *cmd;
322         u8 *buffer;
323         int scan_size;
324         enum scan_type type;
325         int retval;
326         u32 bytes_written;
327         u32 bytes_read;
328         
329 #ifdef _DEBUG_USB_IO_
330         struct timeval start, inter, inter2, end;
331         struct timeval d_inter, d_inter2, d_end;
332 #endif
333
334 #ifdef _DEBUG_USB_COMMS_
335         DEBUG("write buffer (size %i):", ft2232_buffer_size);
336         ft2232_debug_dump_buffer();
337 #endif
338
339 #ifdef _DEBUG_USB_IO_
340         gettimeofday(&start, NULL);     
341 #endif
342
343         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
344         {
345                 ERROR("couldn't write MPSSE commands to FT2232");
346                 exit(-1);
347         }
348         
349 #ifdef _DEBUG_USB_IO_
350         gettimeofday(&inter, NULL);     
351 #endif
352         
353         if (ft2232_expect_read)
354         {
355                 int timeout = 100;
356                 ft2232_buffer_size = 0;
357                 
358 #ifdef _DEBUG_USB_IO_
359                 gettimeofday(&inter2, NULL);    
360 #endif
361                 
362                 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
363                 {
364                         ERROR("couldn't read from FT2232");
365                         exit(-1);
366                 }
367                 
368 #ifdef _DEBUG_USB_IO_
369                 gettimeofday(&end, NULL);       
370
371                 timeval_subtract(&d_inter, &inter, &start);
372                 timeval_subtract(&d_inter2, &inter2, &start);
373                 timeval_subtract(&d_end, &end, &start);
374
375                 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);
376 #endif
377         
378                 
379                 ft2232_buffer_size = bytes_read;
380                 
381                 if (ft2232_expect_read != ft2232_buffer_size)
382                 {
383                         ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
384                         ft2232_debug_dump_buffer();     
385
386                         exit(-1);
387                 }
388
389 #ifdef _DEBUG_USB_COMMS_
390                 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
391                 ft2232_debug_dump_buffer();
392 #endif
393         }
394
395         ft2232_expect_read = 0;
396         ft2232_read_pointer = 0;
397
398         cmd = first;
399         while (cmd != last)
400         {
401                 switch (cmd->type)
402                 {
403                         case JTAG_SCAN:
404                                 type = jtag_scan_type(cmd->cmd.scan);
405                                 if (type != SCAN_OUT)
406                                 {
407                                         scan_size = jtag_scan_size(cmd->cmd.scan);
408                                         buffer = calloc(CEIL(scan_size, 8), 1);
409                                         ft2232_read_scan(type, buffer, scan_size);
410                                         jtag_read_buffer(buffer, cmd->cmd.scan);
411                                         free(buffer);
412                                 }
413                                 break;
414                         default:
415                                 break;
416                 }
417                 cmd = cmd->next;
418         }
419         
420         ft2232_buffer_size = 0;
421
422         return ERROR_OK;
423 }
424
425 void ft2232_add_pathmove(pathmove_command_t *cmd)
426 {
427         int num_states = cmd->num_states;
428         u8 tms_byte;
429         int state_count;
430
431         state_count = 0;
432         while (num_states)
433         {
434                 tms_byte = 0x0;
435                 int bit_count = 0;
436                 
437                 /* command "Clock Data to TMS/CS Pin (no Read)" */
438                 BUFFER_ADD = 0x4b;
439                 /* number of states remaining */
440                 BUFFER_ADD = (num_states % 7) - 1;
441                 
442                 while (num_states % 7)
443                 {
444                         if (tap_transitions[cur_state].low == cmd->path[state_count])
445                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
446                         else if (tap_transitions[cur_state].high == cmd->path[state_count])
447                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
448                         else
449                         {
450                                 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
451                                 exit(-1);
452                         }
453
454                         cur_state = cmd->path[state_count];
455                         state_count++;
456                         num_states--;
457                 }
458                 
459                 BUFFER_ADD = tms_byte;
460         }
461         
462         end_state = cur_state;
463 }
464
465 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
466 {
467         int num_bytes = (scan_size + 7) / 8;
468         int bits_left = scan_size;
469         int cur_byte = 0;
470         int last_bit;
471
472         if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
473         {
474                 /* command "Clock Data to TMS/CS Pin (no Read)" */
475                 BUFFER_ADD = 0x4b;
476                 /* scan 7 bit */
477                 BUFFER_ADD = 0x6;
478                 /* TMS data bits */
479                 if (ir_scan)
480                 {
481                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
482                         cur_state = TAP_SI;
483                 }
484                 else
485                 {
486                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
487                         cur_state = TAP_SD;
488                 }
489                 //DEBUG("added TMS scan (no read)");
490         }
491         
492         /* add command for complete bytes */
493         if (num_bytes > 1)
494         {
495                 if (type == SCAN_IO)
496                 {
497                         /* Clock Data Bytes In and Out LSB First */
498                         BUFFER_ADD = 0x39;
499                         //DEBUG("added TDI bytes (io %i)", num_bytes);
500                 }
501                 else if (type == SCAN_OUT)
502                 {
503                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
504                         BUFFER_ADD = 0x19;
505                         //DEBUG("added TDI bytes (o)");
506                 }
507                 else if (type == SCAN_IN)
508                 {
509                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
510                         BUFFER_ADD = 0x28;
511                         //DEBUG("added TDI bytes (i %i)", num_bytes);
512                 }
513                 BUFFER_ADD = (num_bytes-2) & 0xff;
514                 BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
515         }
516         if (type != SCAN_IN)
517         {
518                 /* add complete bytes */
519                 while(num_bytes-- > 1)
520                 {
521                         BUFFER_ADD = buffer[cur_byte];
522                         cur_byte++;
523                         bits_left -= 8;
524                 }
525         }
526         if (type == SCAN_IN)
527         {
528                 bits_left -= 8 * (num_bytes - 1);
529         }
530
531         /* the most signifcant bit is scanned during TAP movement */
532         if (type != SCAN_IN)
533                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
534         else
535                 last_bit = 0;
536
537         /* process remaining bits but the last one */
538         if (bits_left > 1)
539         {
540                 if (type == SCAN_IO)
541                 {
542                         /* Clock Data Bits In and Out LSB First */
543                         BUFFER_ADD = 0x3b;
544                         //DEBUG("added TDI bits (io) %i", bits_left - 1);
545                 }
546                 else if (type == SCAN_OUT)
547                 {
548                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
549                         BUFFER_ADD = 0x1b;
550                         //DEBUG("added TDI bits (o)");
551                 }
552                 else if (type == SCAN_IN)
553                 {
554                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
555                         BUFFER_ADD = 0x2a;
556                         //DEBUG("added TDI bits (i %i)", bits_left - 1);
557                 }
558                 BUFFER_ADD = bits_left - 2;
559                 if (type != SCAN_IN)
560                         BUFFER_ADD = buffer[cur_byte];
561         }
562
563         /* move from Shift-IR/DR to end state */
564         if (type != SCAN_OUT)
565         {
566                 /* Clock Data to TMS/CS Pin with Read */
567                 BUFFER_ADD = 0x6b;
568                 //DEBUG("added TMS scan (read)");
569         }
570         else
571         {
572                 /* Clock Data to TMS/CS Pin (no Read) */
573                 BUFFER_ADD = 0x4b;
574                 //DEBUG("added TMS scan (no read)");
575         }
576         BUFFER_ADD = 0x6;
577         BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
578         cur_state = end_state;
579
580 }
581
582 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
583 {
584         int predicted_size = 3;
585         
586         if (cur_state != TAP_SD)
587                 predicted_size += 3;
588         
589         if (type == SCAN_IN)    /* only from device to host */
590         {
591                 /* complete bytes */
592                 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
593                 /* remaining bits - 1 (up to 7) */
594                 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
595         }
596         else                                    /* host to device, or bidirectional */
597         {
598                 /* complete bytes */
599                 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
600                 /* remaining bits -1 (up to 7) */
601                 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
602         }
603
604         return predicted_size;
605 }
606
607 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
608 {
609         int predicted_size = 0;
610         
611         if (type != SCAN_OUT)
612         {
613                 /* complete bytes */
614                 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
615                 /* remaining bits - 1 */
616                 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
617                 /* last bit (from TMS scan) */
618                 predicted_size += 1;
619         }
620         
621         //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
622
623         return predicted_size;
624 }
625
626 void usbjtag_reset(int trst, int srst)
627 {
628         if (trst == 1)
629         {
630                 cur_state = TAP_TLR;
631                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
632                         low_direction |= nTRSTnOE;      /* switch to output pin (output is low) */
633                 else
634                         low_output &= ~nTRST;   /* switch output low */
635         }
636         else if (trst == 0)
637         {
638                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
639                         low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
640                 else
641                         low_output |= nTRST; /* switch output high */
642         }
643
644         if (srst == 1)
645         {
646                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
647                         low_output &= ~nSRST;   /* switch output low */
648                 else
649                         low_direction |= nSRSTnOE;      /* switch to output pin (output is low) */
650         }
651         else if (srst == 0)
652         {
653                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
654                         low_output |= nSRST;    /* switch output high */
655                 else
656                         low_direction &= ~nSRSTnOE;     /* switch to input pin (high-Z) */
657         }
658         
659         /* command "set data bits low byte" */
660         BUFFER_ADD = 0x80;
661         BUFFER_ADD = low_output;
662         BUFFER_ADD = low_direction;
663
664 }
665
666 void jtagkey_reset(int trst, int srst)
667 {
668         if (trst == 1)
669         {
670                 cur_state = TAP_TLR;
671                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
672                         high_output &= ~nTRSTnOE;
673                 else
674                         high_output &= ~nTRST;
675         }
676         else if (trst == 0)
677         {
678                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
679                         high_output |= nTRSTnOE;
680                 else
681                         high_output |= nTRST;
682         }
683
684         if (srst == 1)
685         {
686                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
687                         high_output &= ~nSRST;
688                 else
689                         high_output &= ~nSRSTnOE;
690         }
691         else if (srst == 0)
692         {
693                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
694                         high_output |= nSRST;
695                 else
696                         high_output |= nSRSTnOE;
697         }
698         
699         /* command "set data bits high byte" */
700         BUFFER_ADD = 0x82;
701         BUFFER_ADD = high_output;
702         BUFFER_ADD = high_direction;
703         DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
704 }
705
706 void olimex_jtag_reset(int trst, int srst)
707 {
708         if (trst == 1)
709         {
710                 cur_state = TAP_TLR;
711                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
712                         high_output &= ~nTRSTnOE;
713                 else
714                         high_output &= ~nTRST;
715         }
716         else if (trst == 0)
717         {
718                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
719                         high_output |= nTRSTnOE;
720                 else
721                         high_output |= nTRST;
722         }
723
724     if (srst == 1)
725     {
726         high_output |= nSRST;
727     }
728     else if (srst == 0)
729     {
730         high_output &= ~nSRST;
731     }
732
733     /* command "set data bits high byte" */
734     BUFFER_ADD = 0x82;
735     BUFFER_ADD = high_output;
736     BUFFER_ADD = high_direction;
737     DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
738 }
739
740 void m5960_reset(int trst, int srst)
741 {
742         if (trst == 1)
743         {
744                 cur_state = TAP_TLR;
745                 low_output &= ~nTRST;
746         }
747         else if (trst == 0)
748         {
749                 low_output |= nTRST;
750         }
751
752     if (srst == 1)
753     {
754         low_output |= nSRST;
755     }
756     else if (srst == 0)
757     {
758         low_output &= ~nSRST;
759     }
760
761     /* command "set data bits low byte" */
762     BUFFER_ADD = 0x80;
763     BUFFER_ADD = low_output;
764     BUFFER_ADD = low_direction;
765     DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
766 }
767
768 int ft2232_execute_queue()
769 {
770         jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
771         jtag_command_t *first_unsent = cmd;     /* next command that has to be sent */
772         u8 *buffer;
773         int scan_size;  /* size of IR or DR scan */
774         enum scan_type type;
775         int i;
776         int predicted_size = 0;
777         int require_send = 0;
778
779         ft2232_buffer_size = 0;
780         ft2232_expect_read = 0;
781         
782         /* blink, if the current layout has that feature */
783         if (layout->blink)
784                 layout->blink();
785
786         while (cmd)
787         {
788                 switch(cmd->type)
789                 {
790                         case JTAG_END_STATE:
791                                 if (cmd->cmd.end_state->end_state != -1)
792                                         ft2232_end_state(cmd->cmd.end_state->end_state);
793                                 break;
794                         case JTAG_RESET:
795                                 /* only send the maximum buffer size that FT2232C can handle */
796                                 predicted_size = 3;
797                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
798                                 {
799                                         ft2232_send_and_recv(first_unsent, cmd);
800                                         require_send = 0;
801                                         first_unsent = cmd;
802                                 }
803
804                                 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
805                                 require_send = 1;
806                                 
807 #ifdef _DEBUG_JTAG_IO_                          
808                                 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
809 #endif
810                                 break;
811                         case JTAG_RUNTEST:
812                                 /* only send the maximum buffer size that FT2232C can handle */
813                                 predicted_size = 0;
814                                 if (cur_state != TAP_RTI)
815                                         predicted_size += 3;
816                                 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
817                                 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
818                                         predicted_size += 3;
819                                 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
820                                         predicted_size += 3;
821                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
822                                 {
823                                         ft2232_send_and_recv(first_unsent, cmd);
824                                         require_send = 0;
825                                         first_unsent = cmd;
826                                 }
827                                 if (cur_state != TAP_RTI)
828                                 {
829                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
830                                         BUFFER_ADD = 0x4b;
831                                         /* scan 7 bit */
832                                         BUFFER_ADD = 0x6;
833                                         /* TMS data bits */
834                                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
835                                         cur_state = TAP_RTI;
836                                         require_send = 1;
837                                 }
838                                 i = cmd->cmd.runtest->num_cycles;
839                                 while (i > 0)
840                                 {
841                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
842                                         BUFFER_ADD = 0x4b;
843                                         /* scan 7 bit */
844                                         BUFFER_ADD = (i > 7) ? 6 : (i - 1);
845                                         /* TMS data bits */
846                                         BUFFER_ADD = 0x0;
847                                         cur_state = TAP_RTI;
848                                         i -= (i > 7) ? 7 : i;
849                                         //DEBUG("added TMS scan (no read)");
850                                 }
851                                 if (cmd->cmd.runtest->end_state != -1)
852                                         ft2232_end_state(cmd->cmd.runtest->end_state);
853                                 if (cur_state != end_state)
854                                 {
855                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
856                                         BUFFER_ADD = 0x4b;
857                                         /* scan 7 bit */
858                                         BUFFER_ADD = 0x6;
859                                         /* TMS data bits */
860                                         BUFFER_ADD = TAP_MOVE(cur_state, end_state);
861                                         cur_state = end_state;
862                                         //DEBUG("added TMS scan (no read)");
863                                 }
864                                 require_send = 1;
865 #ifdef _DEBUG_JTAG_IO_                          
866                                 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
867 #endif
868                                 break;
869                         case JTAG_STATEMOVE:
870                                 /* only send the maximum buffer size that FT2232C can handle */
871                                 predicted_size = 3;
872                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
873                                 {
874                                         ft2232_send_and_recv(first_unsent, cmd);
875                                         require_send = 0;
876                                         first_unsent = cmd;
877                                 }
878                                 if (cmd->cmd.statemove->end_state != -1)
879                                         ft2232_end_state(cmd->cmd.statemove->end_state);
880                                 /* command "Clock Data to TMS/CS Pin (no Read)" */
881                                 BUFFER_ADD = 0x4b;
882                                 /* scan 7 bit */
883                                 BUFFER_ADD = 0x6;
884                                 /* TMS data bits */
885                                 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
886                                 //DEBUG("added TMS scan (no read)");
887                                 cur_state = end_state;
888                                 require_send = 1;
889 #ifdef _DEBUG_JTAG_IO_                          
890                                 DEBUG("statemove: %i", end_state);
891 #endif
892                                 break;
893                         case JTAG_PATHMOVE:
894                                 /* only send the maximum buffer size that FT2232C can handle */
895                                 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
896                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
897                                 {
898                                         ft2232_send_and_recv(first_unsent, cmd);
899                                         require_send = 0;
900                                         first_unsent = cmd;
901                                 }
902                                 ft2232_add_pathmove(cmd->cmd.pathmove);
903                                 require_send = 1;
904 #ifdef _DEBUG_JTAG_IO_                          
905                                 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
906 #endif
907                                 break;
908                         case JTAG_SCAN:
909                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
910                                 type = jtag_scan_type(cmd->cmd.scan);
911                                 predicted_size = ft2232_predict_scan_out(scan_size, type);
912                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
913                                 {
914                                         DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
915                                         ft2232_send_and_recv(first_unsent, cmd);
916                                         require_send = 0;
917                                         first_unsent = cmd;
918                                 }
919                                 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
920                                 //DEBUG("new read size: %i", ft2232_expect_read);
921                                 if (cmd->cmd.scan->end_state != -1)
922                                         ft2232_end_state(cmd->cmd.scan->end_state);
923                                 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
924                                 require_send = 1;
925                                 if (buffer)
926                                         free(buffer);
927 #ifdef _DEBUG_JTAG_IO_                          
928                                 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
929 #endif
930                                 break;
931                         case JTAG_SLEEP:
932                                 ft2232_send_and_recv(first_unsent, cmd);
933                                 first_unsent = cmd->next;
934                                 jtag_sleep(cmd->cmd.sleep->us);
935 #ifdef _DEBUG_JTAG_IO_                          
936                                 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
937 #endif
938                                 break;
939                         default:
940                                 ERROR("BUG: unknown JTAG command type encountered");
941                                 exit(-1);
942                 }
943                 cmd = cmd->next;
944         }
945
946         if (require_send > 0)
947                 ft2232_send_and_recv(first_unsent, cmd);
948
949         return ERROR_OK;
950 }
951
952 int ft2232_init(void)
953 {
954         u8 latency_timer;
955         u8 buf[1];
956         int retval;
957         u32 bytes_written;
958         
959 #if BUILD_FT2232_FTD2XX == 1
960         FT_STATUS status;
961         DWORD openex_flags = 0;
962         char *openex_string = NULL;
963 #endif
964
965         ft2232_layout_t *cur_layout = ft2232_layouts;
966         
967         if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
968         {
969                 ft2232_layout = "usbjtag";
970                 WARNING("No ft2232 layout specified, using default 'usbjtag'");
971         }
972         
973         while (cur_layout->name)
974         {
975                 if (strcmp(cur_layout->name, ft2232_layout) == 0)
976                 {
977                         layout = cur_layout;
978                         break;
979                 }
980                 cur_layout++;
981         }
982
983         if (!layout)
984         {
985                 ERROR("No matching layout found for %s", ft2232_layout);
986                 return ERROR_JTAG_INIT_FAILED;
987         }
988         
989 #if BUILD_FT2232_FTD2XX == 1
990         DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout);
991 #elif BUILD_FT2232_LIBFTDI == 1
992         DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout);
993 #endif
994
995 #if BUILD_FT2232_FTD2XX == 1
996 #if IS_WIN32 == 0
997         /* Add non-standard Vid/Pid to the linux driver */
998         if ((status = FT_SetVIDPID(ft2232_vid, ft2232_pid)) != FT_OK)
999         {
1000                 WARNING("couldn't add %4.4x:%4.4x", ft2232_vid, ft2232_pid);
1001         }
1002 #endif
1003
1004         if (ft2232_device_desc && ft2232_serial)
1005         {
1006                 WARNING("can't open by device description and serial number, giving precedence to serial");
1007                 ft2232_device_desc = NULL;
1008         }
1009         
1010         if (ft2232_device_desc)
1011         {
1012                 openex_string = ft2232_device_desc;
1013                 openex_flags = FT_OPEN_BY_DESCRIPTION;
1014         }
1015         else if (ft2232_serial)
1016         {
1017                 openex_string = ft2232_serial;
1018                 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1019         }
1020         else
1021         {
1022                 ERROR("neither device description nor serial number specified");
1023                 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1024                 
1025                 return ERROR_JTAG_INIT_FAILED;  
1026         }
1027
1028         if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1029         {
1030                 DWORD num_devices;
1031                 
1032                 ERROR("unable to open ftdi device: %i", status);
1033                 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1034                 if (status == FT_OK)
1035                 {
1036                         char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1037                         int i;
1038
1039                         for (i = 0; i < num_devices; i++)
1040                                 desc_array[i] = malloc(64);
1041                         desc_array[num_devices] = NULL;
1042
1043                         status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1044
1045                         if (status == FT_OK)
1046                         {
1047                                 ERROR("ListDevices: %d\n", num_devices);
1048                                 for (i = 0; i < num_devices; i++)
1049                                         ERROR("%i: %s", i, desc_array[i]);
1050                         }
1051                         
1052                         for (i = 0; i < num_devices; i++)
1053                                 free(desc_array[i]);
1054                         free(desc_array);
1055                 }
1056                 else
1057                 {
1058                         printf("ListDevices: NONE\n");
1059                 }
1060                 return ERROR_JTAG_INIT_FAILED;
1061         }
1062
1063         if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1064         {
1065                 ERROR("unable to set latency timer: %i", status);
1066                 return ERROR_JTAG_INIT_FAILED;
1067         }
1068         
1069         if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1070         {
1071                 ERROR("unable to get latency timer: %i", status);
1072                 return ERROR_JTAG_INIT_FAILED;
1073         }
1074         else
1075         {
1076                 DEBUG("current latency timer: %i", latency_timer);
1077         }
1078         
1079         if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1080         {
1081                 ERROR("unable to set timeouts: %i", status);
1082                 return ERROR_JTAG_INIT_FAILED;
1083         }
1084
1085         if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1086         {
1087                 ERROR("unable to enable bit i/o mode: %i", status);
1088                 return ERROR_JTAG_INIT_FAILED;
1089         }
1090 #elif BUILD_FT2232_LIBFTDI == 1
1091         if (ftdi_init(&ftdic) < 0)
1092                 return ERROR_JTAG_INIT_FAILED;
1093
1094         /* context, vendor id, product id */
1095         if (ftdi_usb_open(&ftdic, ft2232_vid, ft2232_pid) < 0)
1096         {
1097                 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1098                 return ERROR_JTAG_INIT_FAILED;
1099         }
1100
1101         if (ftdi_usb_reset(&ftdic) < 0)
1102         {
1103                 ERROR("unable to reset ftdi device");
1104                 return ERROR_JTAG_INIT_FAILED;
1105         }
1106
1107         if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1108         {
1109                 ERROR("unable to set latency timer");
1110                 return ERROR_JTAG_INIT_FAILED;
1111         }
1112         
1113         if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1114         {
1115                 ERROR("unable to get latency timer");
1116                 return ERROR_JTAG_INIT_FAILED;
1117         }
1118         else
1119         {
1120                 DEBUG("current latency timer: %i", latency_timer);
1121         }
1122
1123         ftdic.bitbang_mode = 0; /* Reset controller */
1124         ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1125
1126         ftdic.bitbang_mode = 2; /* MPSSE mode */
1127         ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
1128 #endif  
1129
1130         ft2232_buffer_size = 0;
1131         ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1132
1133         if (layout->init() != ERROR_OK)
1134                 return ERROR_JTAG_INIT_FAILED;
1135
1136         ft2232_speed(jtag_speed);
1137
1138         buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1139         if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1140         {
1141                 ERROR("couldn't write to FT2232 to disable loopback");
1142                 return ERROR_JTAG_INIT_FAILED;
1143         }
1144
1145 #if BUILD_FT2232_FTD2XX == 1
1146         if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1147         {
1148                 ERROR("error purging ftd2xx device: %i", status);
1149                 return ERROR_JTAG_INIT_FAILED;
1150         }
1151 #elif BUILD_FT2232_LIBFTDI == 1
1152         if (ftdi_usb_purge_buffers(&ftdic) < 0)
1153         {
1154                 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1155                 return ERROR_JTAG_INIT_FAILED;
1156         }
1157 #endif  
1158
1159         return ERROR_OK;
1160 }
1161
1162 int usbjtag_init(void)
1163 {
1164         u8 buf[3];
1165         u32 bytes_written;
1166         
1167         low_output = 0x08;
1168         low_direction = 0x0b;
1169         
1170         if (strcmp(ft2232_layout, "usbjtag") == 0)
1171         {
1172                 nTRST = 0x10;
1173                 nTRSTnOE = 0x10;
1174                 nSRST = 0x40;
1175                 nSRSTnOE = 0x40;
1176         }
1177         else if (strcmp(ft2232_layout, "signalyzer") == 0)
1178         {
1179                 nTRST = 0x10;
1180                 nTRSTnOE = 0x10;
1181                 nSRST = 0x20;
1182                 nSRSTnOE = 0x20;
1183         }
1184         else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1185         {
1186                 nTRST = 0x0;
1187                 nTRSTnOE = 0x00;
1188                 nSRST = 0x20;
1189                 nSRSTnOE = 0x20;
1190                 low_output = 0x88;
1191                 low_direction = 0x8b;
1192         }
1193         else
1194         {
1195                 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1196                 return ERROR_JTAG_INIT_FAILED;  
1197         }
1198         
1199         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1200         {
1201                 low_direction &= ~nTRSTnOE; /* nTRST input */
1202                 low_output &= ~nTRST; /* nTRST = 0 */
1203         }
1204         else
1205         {
1206                 low_direction |= nTRSTnOE; /* nTRST output */
1207                 low_output |= nTRST; /* nTRST = 1 */
1208         }
1209         
1210         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1211         {
1212                 low_direction |= nSRSTnOE; /* nSRST output */
1213                 low_output |= nSRST; /* nSRST = 1 */
1214         }
1215         else
1216         {
1217                 low_direction &= ~nSRSTnOE; /* nSRST input */
1218                 low_output &= ~nSRST; /* nSRST = 0 */
1219         }
1220         
1221         /* initialize low byte for jtag */
1222         buf[0] = 0x80; /* command "set data bits low byte" */
1223         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1224         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1225         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1226         
1227         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1228         {
1229                 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout"); 
1230                 return ERROR_JTAG_INIT_FAILED;
1231         }
1232
1233         return ERROR_OK;
1234 }
1235
1236 int jtagkey_init(void)
1237 {
1238         u8 buf[3];
1239         u32 bytes_written;
1240         
1241         low_output = 0x08;
1242         low_direction = 0x1b;
1243         
1244         /* initialize low byte for jtag */
1245         buf[0] = 0x80; /* command "set data bits low byte" */
1246         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1247         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1248         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1249         
1250         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1251         {
1252                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1253                 return ERROR_JTAG_INIT_FAILED;
1254         }
1255         
1256         if (strcmp(layout->name, "jtagkey") == 0)
1257         {
1258                 nTRST = 0x01;
1259                 nTRSTnOE = 0x4;
1260                 nSRST = 0x02;
1261                 nSRSTnOE = 0x08;
1262         }
1263         else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
1264         {
1265                 nTRST = 0x02;
1266                 nTRSTnOE = 0x1;
1267                 nSRST = 0x08;
1268                 nSRSTnOE = 0x04;
1269         }
1270         else
1271         {
1272                 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1273                 exit(-1);
1274         }
1275         
1276         high_output = 0x0;
1277         high_direction = 0x0f;
1278
1279         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1280         {
1281                 high_output |= nTRSTnOE;
1282                 high_output &= ~nTRST;
1283         }
1284         else
1285         {
1286                 high_output &= ~nTRSTnOE;
1287                 high_output |= nTRST;
1288         }
1289         
1290         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1291         {
1292                 high_output &= ~nSRSTnOE;
1293                 high_output |= nSRST;
1294         }
1295         else
1296         {
1297                 high_output |= nSRSTnOE;
1298                 high_output &= ~nSRST;
1299         }
1300         
1301         /* initialize high port */
1302         buf[0] = 0x82; /* command "set data bits high byte" */
1303         buf[1] = high_output; /* value */
1304         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1305         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1306         
1307         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1308         {
1309                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1310                 return ERROR_JTAG_INIT_FAILED;
1311         }
1312         
1313         return ERROR_OK;
1314 }
1315
1316 int olimex_jtag_init(void)
1317 {
1318         u8 buf[3];
1319         u32 bytes_written;
1320         
1321         low_output = 0x08;
1322         low_direction = 0x1b;
1323         
1324         /* initialize low byte for jtag */
1325         buf[0] = 0x80; /* command "set data bits low byte" */
1326         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1327         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1328         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1329         
1330         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1331         {
1332                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1333                 return ERROR_JTAG_INIT_FAILED;
1334         }
1335         
1336         nTRST = 0x01;
1337         nTRSTnOE = 0x4;
1338         nSRST = 0x02;
1339         nSRSTnOE = 0x00; /* no output enable for nSRST */
1340
1341         high_output = 0x0;
1342         high_direction = 0x0f;
1343
1344         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1345         {
1346                 high_output |= nTRSTnOE;
1347                 high_output &= ~nTRST;
1348         }
1349         else
1350         {
1351                 high_output &= ~nTRSTnOE;
1352                 high_output |= nTRST;
1353         }
1354         
1355         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1356         {
1357                 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1358         }
1359         else
1360         {
1361                 high_output &= ~nSRST;
1362         }
1363         
1364         /* turn red LED on */
1365         high_output |= 0x08;
1366         
1367         /* initialize high port */
1368         buf[0] = 0x82; /* command "set data bits high byte" */
1369         buf[1] = high_output; /* value */
1370         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1371         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1372         
1373         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1374         {
1375                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1376                 return ERROR_JTAG_INIT_FAILED;
1377         }
1378         
1379         return ERROR_OK;
1380 }
1381
1382 int m5960_init(void)
1383 {
1384         u8 buf[3];
1385         u32 bytes_written;
1386         
1387         low_output = 0x18;
1388         low_direction = 0xfb;
1389         
1390         /* initialize low byte for jtag */
1391         buf[0] = 0x80; /* command "set data bits low byte" */
1392         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1393         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1394         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1395         
1396         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1397         {
1398                 ERROR("couldn't initialize FT2232 with 'm5960' layout"); 
1399                 return ERROR_JTAG_INIT_FAILED;
1400         }
1401         
1402         nTRST = 0x10;
1403         nTRSTnOE = 0x0; /* not output enable for nTRST */
1404         nSRST = 0x20;
1405         nSRSTnOE = 0x00; /* no output enable for nSRST */
1406
1407         high_output = 0x00;
1408         high_direction = 0x0c;
1409
1410         /* turn red LED1 on, LED2 off */
1411         high_output |= 0x08;
1412         
1413         /* initialize high port */
1414         buf[0] = 0x82; /* command "set data bits high byte" */
1415         buf[1] = high_output; /* value */
1416         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1417         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1418         
1419         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1420         {
1421                 ERROR("couldn't initialize FT2232 with 'm5960' layout"); 
1422                 return ERROR_JTAG_INIT_FAILED;
1423         }
1424         
1425         return ERROR_OK;
1426 }
1427
1428 void olimex_jtag_blink(void)
1429 {
1430         /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1431          * ACBUS3 is bit 3 of the GPIOH port
1432          */
1433         if (high_output & 0x08)
1434         {
1435                 /* set port pin high */
1436                 high_output &= 0x07;
1437         }
1438         else
1439         {
1440                 /* set port pin low */
1441                 high_output |= 0x08;
1442         }
1443         
1444         BUFFER_ADD = 0x82;
1445         BUFFER_ADD = high_output;
1446         BUFFER_ADD = high_direction;
1447 }
1448
1449 int ft2232_quit(void)
1450 {
1451 #if BUILD_FT2232_FTD2XX == 1
1452         FT_STATUS status;
1453
1454         status = FT_Close(ftdih);
1455 #elif BUILD_FT2232_LIBFTDI == 1
1456         ftdi_disable_bitbang(&ftdic);
1457         
1458         ftdi_usb_close(&ftdic);
1459         
1460         ftdi_deinit(&ftdic);
1461 #endif
1462
1463         free(ft2232_buffer);
1464
1465         return ERROR_OK;
1466 }
1467
1468 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1469 {
1470         if (argc == 1)
1471         {
1472                 ft2232_device_desc = strdup(args[0]);
1473         }
1474         else
1475         {
1476                 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1477         }
1478         
1479         return ERROR_OK;
1480 }
1481
1482 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1483 {
1484         if (argc == 1)
1485         {
1486                 ft2232_serial = strdup(args[0]);
1487         }
1488         else
1489         {
1490                 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
1491         }
1492         
1493         return ERROR_OK;
1494 }
1495
1496 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1497 {
1498         if (argc == 0)
1499                 return ERROR_OK;
1500
1501         ft2232_layout = malloc(strlen(args[0]) + 1);
1502         strcpy(ft2232_layout, args[0]);
1503
1504         return ERROR_OK;
1505 }
1506
1507 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1508 {
1509         if (argc >= 2)
1510         {
1511                 ft2232_vid = strtol(args[0], NULL, 0);
1512                 ft2232_pid = strtol(args[1], NULL, 0);
1513         }
1514         else
1515         {
1516                 WARNING("incomplete ft2232_vid_pid configuration directive");
1517         }
1518         
1519         return ERROR_OK;
1520 }