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