- prepare OpenOCD for branching, created ./trunk/
[fw/openocd] / src / jtag / ftd2xx.c
1 /***************************************************************************
2  *   Copyright (C) 2004 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 #include "config.h"
21 #if IS_CYGWIN == 1
22 #include "windows.h"
23 #undef ERROR
24 #endif
25
26 /* project specific includes */
27 #include "log.h"
28 #include "types.h"
29 #include "jtag.h"
30 #include "configuration.h"
31
32 /* system includes */
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <ftd2xx.h>
37
38 #include <sys/time.h>
39 #include <time.h>
40
41 /* enable this to debug io latency
42  */
43 #if 0
44 #define _DEBUG_USB_IO_
45 #endif
46
47 /* enable this to debug communication
48  */
49 #if 0
50 #define _DEBUG_USB_COMMS_
51 #endif
52
53 /* enable this to work around ftd2xx deadlock
54  */
55 #if 0
56 #define _FTD2XX_QUEUE_DELAY_
57 #endif
58
59 int ftd2xx_execute_queue(void);
60
61 int ftd2xx_speed(int speed);
62 int ftd2xx_register_commands(struct command_context_s *cmd_ctx);
63 int ftd2xx_init(void);
64 int ftd2xx_quit(void);
65
66 int ftd2xx_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int ftd2xx_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int ftd2xx_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69
70 char *ftd2xx_device_desc = NULL;
71 char *ftd2xx_layout = NULL;
72 u16 ftd2xx_vid = 0x0403;
73 u16 ftd2xx_pid = 0x6010;
74
75 typedef struct ftd2xx_layout_s
76 {
77         char* name;
78         int(*init)(void);
79         void(*reset)(int trst, int srst);
80 } ftd2xx_layout_t;
81
82 int usbjtag_init(void);
83 int jtagkey_init(void);
84 void usbjtag_reset(int trst, int srst);
85 void jtagkey_reset(int trst, int srst);
86
87 ftd2xx_layout_t ftd2xx_layouts[] =
88 {
89         {"usbjtag", usbjtag_init, usbjtag_reset},
90         {"jtagkey", jtagkey_init, jtagkey_reset},
91         {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset},
92         {NULL, NULL, NULL},
93 };
94
95 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
96
97 static ftd2xx_layout_t *layout;
98 static u8 low_output = 0x0;
99 static u8 low_direction = 0x0;
100 static u8 high_output = 0x0;
101 static u8 high_direction = 0x0;
102 static FT_HANDLE ftdih = NULL;
103
104 static u8 *ftd2xx_buffer = NULL;
105 static int ftd2xx_buffer_size = 0;
106 static int ftd2xx_read_pointer = 0;
107 static int ftd2xx_expect_read = 0;
108 #define FTD2XX_BUFFER_SIZE      131072
109 #define BUFFER_ADD ftd2xx_buffer[ftd2xx_buffer_size++]
110 #define BUFFER_READ ftd2xx_buffer[ftd2xx_read_pointer++]
111
112 jtag_interface_t ftd2xx_interface = 
113 {
114         
115         .name = "ftd2xx",
116         
117         .execute_queue = ftd2xx_execute_queue,
118         
119         .support_statemove = 1,
120         
121         .speed = ftd2xx_speed,
122         .register_commands = ftd2xx_register_commands,
123         .init = ftd2xx_init,
124         .quit = ftd2xx_quit,
125 };
126
127 int ftd2xx_speed(int speed)
128 {
129         u8 buf[3];
130         FT_STATUS status;
131         DWORD bytes_written;
132
133         buf[0] = 0x86; /* command "set divisor" */
134         buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
135         buf[2] = (speed >> 8) & 0xff; /* valueH */
136         
137         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
138         if (((status = FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
139         {
140                 ERROR("couldn't write to ftdi device: %i", status);
141                 return status;
142         }
143         
144         return ERROR_OK;
145 }
146
147 int ftd2xx_register_commands(struct command_context_s *cmd_ctx)
148 {
149         register_command(cmd_ctx, NULL, "ftd2xx_device_desc", ftd2xx_handle_device_desc_command,
150                 COMMAND_CONFIG, NULL);
151         register_command(cmd_ctx, NULL, "ftd2xx_layout", ftd2xx_handle_layout_command,
152                 COMMAND_CONFIG, NULL);
153         register_command(cmd_ctx, NULL, "ftd2xx_vid_pid", ftd2xx_handle_vid_pid_command,
154                                          COMMAND_CONFIG, NULL);
155         return ERROR_OK;
156 }
157
158 void ftd2xx_end_state(state)
159 {
160         if (tap_move_map[state] != -1)
161                 end_state = state;
162         else
163         {
164                 ERROR("BUG: %i is not a valid end state", state);
165                 exit(-1);
166         }
167 }
168
169 void ftd2xx_read_scan(enum scan_type type, u8* buffer, int scan_size)
170 {
171         int num_bytes = ((scan_size + 7) / 8);
172         int bits_left = scan_size;
173         int cur_byte = 0;
174
175         while(num_bytes-- > 1)
176         {
177                 buffer[cur_byte] = BUFFER_READ;
178                 cur_byte++;
179                 bits_left -= 8;
180         }
181
182         buffer[cur_byte] = 0x0;
183
184         if (bits_left > 1)
185         {
186                 buffer[cur_byte] = BUFFER_READ >> 1;
187         }
188
189         buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
190
191 }
192
193 void ftd2xx_debug_dump_buffer(void)
194 {
195         int i;
196         char line[256];
197         char *line_p = line;
198         
199         for (i = 0; i < ftd2xx_buffer_size; i++)
200         {
201                 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ftd2xx_buffer[i]);
202                 if (i % 16 == 15)
203                 {
204                         DEBUG("%s", line);
205                         line_p = line;
206                 }
207         }
208         
209         if (line_p != line)
210                 DEBUG("%s", line);
211 }
212
213 int ftd2xx_send_and_recv(jtag_command_t *first, jtag_command_t *last)
214 {
215         jtag_command_t *cmd;
216         u8 *buffer;
217         int scan_size;
218         enum scan_type type;
219         FT_STATUS status;
220         DWORD bytes_written;
221         DWORD bytes_read;
222         
223 #ifdef _DEBUG_USB_IO_
224         struct timeval start, inter, inter2, end;
225 #endif
226
227 #ifdef _DEBUG_USB_COMMS_
228         DEBUG("write buffer (size %i):", ftd2xx_buffer_size);
229         ftd2xx_debug_dump_buffer();
230 #endif
231
232 #ifdef _DEBUG_USB_IO_
233         gettimeofday(&start, NULL);     
234 #endif
235
236         if ((status = FT_Write(ftdih, ftd2xx_buffer, ftd2xx_buffer_size, &bytes_written)) != FT_OK)
237         {
238                 ERROR("couldn't write to ftdi device: %i", status);
239                 exit(-1);
240         }
241         
242 #ifdef _DEBUG_USB_IO_
243         gettimeofday(&inter, NULL);     
244 #endif
245         
246         if (ftd2xx_expect_read)
247         {
248                 int timeout = 100;
249                 ftd2xx_buffer_size = 0;
250                 
251 #ifdef _FTD2XX_QUEUE_DELAY_
252                 DWORD inrxqueue = 0;
253                 while (inrxqueue < ftd2xx_expect_read)
254                 {
255                         FT_GetQueueStatus(ftdih, &inrxqueue);
256                         if (inrxqueue >= ftd2xx_expect_read)
257                                 break;
258                         usleep(1000);
259                 };
260 #endif
261                 
262 #ifdef _DEBUG_USB_IO_
263         gettimeofday(&inter2, NULL);    
264 #endif
265                         
266                 if ((status = FT_Read(ftdih, ftd2xx_buffer, ftd2xx_expect_read, &bytes_read)) != FT_OK)
267                 {
268                         ERROR("couldn't read from ftdi device: %i", status);
269                         exit(-1);
270                 }
271
272 #ifdef _DEBUG_USB_IO_
273                 gettimeofday(&end, NULL);       
274
275                 INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", inter.tv_sec - start.tv_sec, inter.tv_usec - start.tv_usec,
276                         inter2.tv_sec - start.tv_sec, inter2.tv_usec - start.tv_usec,
277                         end.tv_sec - start.tv_sec, end.tv_usec - start.tv_usec);
278 #endif
279         
280                 
281                 ftd2xx_buffer_size = bytes_read;
282                 
283                 if (ftd2xx_expect_read != ftd2xx_buffer_size)
284                 {
285                         ERROR("ftd2xx_expect_read (%i) != ftd2xx_buffer_size (%i) (%i retries)", ftd2xx_expect_read, ftd2xx_buffer_size, 100 - timeout);
286                         ftd2xx_debug_dump_buffer();     
287
288                         exit(-1);
289                 }
290
291 #ifdef _DEBUG_USB_COMMS_
292                 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ftd2xx_buffer_size);
293                 ftd2xx_debug_dump_buffer();
294 #endif
295         }
296
297         ftd2xx_expect_read = 0;
298         ftd2xx_read_pointer = 0;
299
300         cmd = first;
301         while (cmd != last)
302         {
303                 switch (cmd->type)
304                 {
305                         case JTAG_SCAN:
306                                 type = jtag_scan_type(cmd->cmd.scan);
307                                 if (type != SCAN_OUT)
308                                 {
309                                         scan_size = jtag_scan_size(cmd->cmd.scan);
310                                         buffer = calloc(CEIL(scan_size, 8), 1);
311                                         ftd2xx_read_scan(type, buffer, scan_size);
312                                         jtag_read_buffer(buffer, cmd->cmd.scan);
313                                         free(buffer);
314                                 }
315                                 break;
316                         default:
317                                 break;
318                 }
319                 cmd = cmd->next;
320         }
321         
322         ftd2xx_buffer_size = 0;
323
324         return ERROR_OK;
325 }
326
327 void ftd2xx_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
328 {
329         int num_bytes = (scan_size + 7) / 8;
330         int bits_left = scan_size;
331         int cur_byte = 0;
332         int last_bit;
333
334         /* command "Clock Data to TMS/CS Pin (no Read)" */
335         BUFFER_ADD = 0x4b;
336         /* scan 7 bit */
337         BUFFER_ADD = 0x6;
338         /* TMS data bits */
339         if (ir_scan)
340         {
341                 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
342                 cur_state = TAP_SI;
343         }
344         else
345         {
346                 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
347                 cur_state = TAP_SD;
348         }
349         //DEBUG("added TMS scan (no read)");
350
351         /* add command for complete bytes */
352         if (num_bytes > 1)
353         {
354                 if (type == SCAN_IO)
355                 {
356                         /* Clock Data Bytes In and Out LSB First */
357                         BUFFER_ADD = 0x39;
358                         //DEBUG("added TDI bytes (io %i)", num_bytes);
359                 }
360                 else if (type == SCAN_OUT)
361                 {
362                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
363                         BUFFER_ADD = 0x19;
364                         //DEBUG("added TDI bytes (o)");
365                 }
366                 else if (type == SCAN_IN)
367                 {
368                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
369                         BUFFER_ADD = 0x28;
370                         //DEBUG("added TDI bytes (i %i)", num_bytes);
371                 }
372                 BUFFER_ADD = (num_bytes-2) & 0xff;
373                 BUFFER_ADD = (num_bytes >> 8) & 0xff;
374         }
375         if (type != SCAN_IN)
376         {
377                 /* add complete bytes */
378                 while(num_bytes-- > 1)
379                 {
380                         BUFFER_ADD = buffer[cur_byte];
381                         cur_byte++;
382                         bits_left -= 8;
383                 }
384         }
385         if (type == SCAN_IN)
386         {
387                 bits_left -= 8 * (num_bytes - 1);
388         }
389
390         /* the most signifcant bit is scanned during TAP movement */
391         if (type != SCAN_IN)
392                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
393         else
394                 last_bit = 0;
395
396         /* process remaining bits but the last one */
397         if (bits_left > 1)
398         {
399                 if (type == SCAN_IO)
400                 {
401                         /* Clock Data Bits In and Out LSB First */
402                         BUFFER_ADD = 0x3b;
403                         //DEBUG("added TDI bits (io) %i", bits_left - 1);
404                 }
405                 else if (type == SCAN_OUT)
406                 {
407                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
408                         BUFFER_ADD = 0x1b;
409                         //DEBUG("added TDI bits (o)");
410                 }
411                 else if (type == SCAN_IN)
412                 {
413                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
414                         BUFFER_ADD = 0x2a;
415                         //DEBUG("added TDI bits (i %i)", bits_left - 1);
416                 }
417                 BUFFER_ADD = bits_left - 2;
418                 if (type != SCAN_IN)
419                         BUFFER_ADD = buffer[cur_byte];
420         }
421
422         /* move from Shift-IR/DR to end state */
423         if (type != SCAN_OUT)
424         {
425                 /* Clock Data to TMS/CS Pin with Read */
426                 BUFFER_ADD = 0x6b;
427                 //DEBUG("added TMS scan (read)");
428         }
429         else
430         {
431                 /* Clock Data to TMS/CS Pin (no Read) */
432                 BUFFER_ADD = 0x4b;
433                 //DEBUG("added TMS scan (no read)");
434         }
435         BUFFER_ADD = 0x6;
436         BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
437         cur_state = end_state;
438
439 }
440
441 int ftd2xx_predict_scan_out(int scan_size, enum scan_type type)
442 {
443         int predicted_size = 6;
444         if (type == SCAN_IN)    /* only from device to host */
445         {
446                 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
447                 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
448         }
449         else                                    /* host to device, or bidirectional */
450         {
451                 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
452                 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
453         }
454
455         return predicted_size;
456 }
457
458 int ftd2xx_predict_scan_in(int scan_size, enum scan_type type)
459 {
460         int predicted_size = 0;
461         
462         if (type != SCAN_OUT)
463         {
464                 /* complete bytes */
465                 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
466                 /* remaining bits - 1 */
467                 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
468                 /* last bit (from TMS scan) */
469                 predicted_size += 1;
470         }
471         
472         //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
473
474         return predicted_size;
475 }
476
477 void usbjtag_reset(int trst, int srst)
478 {
479         if (trst == 1)
480         {
481                 cur_state = TAP_TLR;
482                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
483                         low_direction |= nTRSTnOE;      /* switch to output pin (output is low) */
484                 else
485                         low_output &= ~nTRST;   /* switch output low */
486         }
487         else if (trst == 0)
488         {
489                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
490                         low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
491                 else
492                         low_output |= nTRST; /* switch output high */
493         }
494
495         if (srst == 1)
496         {
497                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
498                         low_output &= ~nSRST;   /* switch output low */
499                 else
500                         low_direction |= nSRSTnOE;      /* switch to output pin (output is low) */
501         }
502         else if (srst == 0)
503         {
504                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
505                         low_output |= nSRST;    /* switch output high */
506                 else
507                         low_direction &= ~nSRSTnOE;     /* switch to input pin (high-Z) */
508         }
509         
510         /* command "set data bits low byte" */
511         BUFFER_ADD = 0x80;
512         BUFFER_ADD = low_output;
513         BUFFER_ADD = low_direction;
514
515 }
516
517 void jtagkey_reset(int trst, int srst)
518 {
519         if (trst == 1)
520         {
521                 cur_state = TAP_TLR;
522                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
523                         high_output &= ~nTRSTnOE;
524                 else
525                         high_output &= ~nTRST;
526         }
527         else if (trst == 0)
528         {
529                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
530                         high_output |= nTRSTnOE;
531                 else
532                         high_output |= nTRST;
533         }
534
535         if (srst == 1)
536         {
537                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
538                         high_output &= ~nSRST;
539                 else
540                         high_output &= ~nSRSTnOE;
541         }
542         else if (srst == 0)
543         {
544                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
545                         high_output |= nSRST;
546                 else
547                         high_output |= nSRSTnOE;
548         }
549         
550         /* command "set data bits high byte" */
551         BUFFER_ADD = 0x82;
552         BUFFER_ADD = high_output;
553         BUFFER_ADD = high_direction;
554         DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
555 }
556
557 int ftd2xx_execute_queue()
558 {
559         jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
560         jtag_command_t *first_unsent = cmd;     /* next command that has to be sent */
561         u8 *buffer;
562         int scan_size;  /* size of IR or DR scan */
563         enum scan_type type;
564         int i;
565         int predicted_size = 0;
566         int require_send = 0;
567
568         ftd2xx_buffer_size = 0;
569         ftd2xx_expect_read = 0;
570
571         while (cmd)
572         {
573                 switch(cmd->type)
574                 {
575                         case JTAG_END_STATE:
576                                 if (cmd->cmd.end_state->end_state != -1)
577                                         ftd2xx_end_state(cmd->cmd.end_state->end_state);
578                                 break;
579                         case JTAG_RESET:
580                                 /* only send the maximum buffer size that FT2232C can handle */
581                                 predicted_size = 3;
582                                 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
583                                 {
584                                         ftd2xx_send_and_recv(first_unsent, cmd);
585                                         require_send = 0;
586                                         first_unsent = cmd;
587                                 }
588
589                                 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
590                                 require_send = 1;
591
592                                 break;
593                         case JTAG_RUNTEST:
594                                 /* only send the maximum buffer size that FT2232C can handle */
595                                 predicted_size = 0;
596                                 if (cur_state != TAP_RTI)
597                                         predicted_size += 3;
598                                 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
599                                 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
600                                         predicted_size += 3;
601                                 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
602                                         predicted_size += 3;
603                                 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
604                                 {
605                                         ftd2xx_send_and_recv(first_unsent, cmd);
606                                         require_send = 0;
607                                         first_unsent = cmd;
608                                 }
609                                 if (cur_state != TAP_RTI)
610                                 {
611                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
612                                         BUFFER_ADD = 0x4b;
613                                         /* scan 7 bit */
614                                         BUFFER_ADD = 0x6;
615                                         /* TMS data bits */
616                                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
617                                         cur_state = TAP_RTI;
618                                         require_send = 1;
619                                 }
620                                 i = cmd->cmd.runtest->num_cycles;
621                                 while (i > 0)
622                                 {
623                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
624                                         BUFFER_ADD = 0x4b;
625                                         /* scan 7 bit */
626                                         BUFFER_ADD = (i > 7) ? 6 : (i - 1);
627                                         /* TMS data bits */
628                                         BUFFER_ADD = 0x0;
629                                         cur_state = TAP_RTI;
630                                         i -= (i > 7) ? 7 : i;
631                                         //DEBUG("added TMS scan (no read)");
632                                 }
633                                 if (cmd->cmd.runtest->end_state != -1)
634                                         ftd2xx_end_state(cmd->cmd.runtest->end_state);
635                                 if (cur_state != end_state)
636                                 {
637                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
638                                         BUFFER_ADD = 0x4b;
639                                         /* scan 7 bit */
640                                         BUFFER_ADD = 0x6;
641                                         /* TMS data bits */
642                                         BUFFER_ADD = TAP_MOVE(cur_state, end_state);
643                                         cur_state = end_state;
644                                         //DEBUG("added TMS scan (no read)");
645                                 }
646                                 require_send = 1;
647                                 break;
648                         case JTAG_STATEMOVE:
649                                 /* only send the maximum buffer size that FT2232C can handle */
650                                 predicted_size = 3;
651                                 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
652                                 {
653                                         ftd2xx_send_and_recv(first_unsent, cmd);
654                                         require_send = 0;
655                                         first_unsent = cmd;
656                                 }
657                                 if (cmd->cmd.statemove->end_state != -1)
658                                         ftd2xx_end_state(cmd->cmd.statemove->end_state);
659                                 /* command "Clock Data to TMS/CS Pin (no Read)" */
660                                 BUFFER_ADD = 0x4b;
661                                 /* scan 7 bit */
662                                 BUFFER_ADD = 0x6;
663                                 /* TMS data bits */
664                                 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
665                                 //DEBUG("added TMS scan (no read)");
666                                 cur_state = end_state;
667                                 require_send = 1;
668                                 break;
669                         case JTAG_SCAN:
670                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
671                                 type = jtag_scan_type(cmd->cmd.scan);
672                                 predicted_size = ftd2xx_predict_scan_out(scan_size, type);
673                                 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
674                                 {
675                                         DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
676                                         ftd2xx_send_and_recv(first_unsent, cmd);
677                                         require_send = 0;
678                                         first_unsent = cmd;
679                                 }
680                                 ftd2xx_expect_read += ftd2xx_predict_scan_in(scan_size, type);
681                                 //DEBUG("new read size: %i", ftd2xx_expect_read);
682                                 if (cmd->cmd.scan->end_state != -1)
683                                         ftd2xx_end_state(cmd->cmd.scan->end_state);
684                                 ftd2xx_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
685                                 require_send = 1;
686                                 if (buffer)
687                                         free(buffer);
688                                 break;
689                         case JTAG_SLEEP:
690                                 ftd2xx_send_and_recv(first_unsent, cmd);
691                                 first_unsent = cmd->next;
692                                 jtag_sleep(cmd->cmd.sleep->us);
693                                 break;
694                         default:
695                                 ERROR("BUG: unknown JTAG command type encountered");
696                                 exit(-1);
697                 }
698                 cmd = cmd->next;
699         }
700
701         if (require_send > 0)
702                 ftd2xx_send_and_recv(first_unsent, cmd);
703
704         return ERROR_OK;
705 }
706
707 int ftd2xx_init(void)
708 {
709         u8 latency_timer;
710         FT_STATUS status;
711         DWORD num_devices;
712         
713         ftd2xx_layout_t *cur_layout = ftd2xx_layouts;
714         
715         if ((ftd2xx_layout == NULL) || (ftd2xx_layout[0] == 0))
716         {
717                 ftd2xx_layout = "usbjtag";
718                 WARNING("No ftd2xx layout specified, using default 'usbjtag'");
719         }
720         
721         while (cur_layout->name)
722         {
723                 if (strcmp(cur_layout->name, ftd2xx_layout) == 0)
724                 {
725                         layout = cur_layout;
726                         break;
727                 }
728                 cur_layout++;
729         }
730
731         if (!layout)
732         {
733                 ERROR("No matching layout found for %s", ftd2xx_layout);
734                 return ERROR_JTAG_INIT_FAILED;
735         }
736         
737         if (ftd2xx_device_desc == NULL)
738         {
739                 WARNING("no ftd2xx device description specified, using default 'Dual RS232'");
740                 ftd2xx_device_desc = "Dual RS232";
741         }
742         
743 #if IS_CYGWIN != 1
744         /* Add JTAGkey Vid/Pid to the linux driver */
745         if ((status = FT_SetVIDPID(ftd2xx_vid, ftd2xx_pid)) != FT_OK)
746         {
747                 WARNING("couldn't add %4.4x:%4.4x", ftd2xx_vid, ftd2xx_pid);
748         }
749 #endif
750
751         if ((status = FT_OpenEx(ftd2xx_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
752         {
753                 ERROR("unable to open ftdi device: %i", status);
754                 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
755                 if (status == FT_OK)
756                 {
757                         char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
758                         int i;
759
760                         for (i = 0; i < num_devices; i++)
761                                 desc_array[i] = malloc(64);
762                         desc_array[num_devices] = NULL;
763
764                         status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
765
766                         if (status == FT_OK)
767                         {
768                                 ERROR("ListDevices: %d\n", num_devices);
769                                 for (i = 0; i < num_devices; i++)
770                                         ERROR("%i: %s", i, desc_array[i]);
771                         }
772                         
773                         for (i = 0; i < num_devices; i++)
774                                 free(desc_array[i]);
775                         free(desc_array);
776                 }
777                 else
778                 {
779                         printf("ListDevices: NONE\n");
780                 }
781                 return ERROR_JTAG_INIT_FAILED;
782         }
783
784         if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
785         {
786                 ERROR("unable to set latency timer: %i", status);
787                 return ERROR_JTAG_INIT_FAILED;
788         }
789         
790         if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
791         {
792                 ERROR("unable to get latency timer: %i", status);
793                 return ERROR_JTAG_INIT_FAILED;
794         }
795         else
796         {
797                 DEBUG("current latency timer: %i", latency_timer);
798         }
799
800         if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
801         {
802                 ERROR("unable to enable bit i/o mode: %i", status);
803                 return ERROR_JTAG_INIT_FAILED;
804         }
805
806         ftd2xx_buffer_size = 0;
807         ftd2xx_buffer = malloc(FTD2XX_BUFFER_SIZE);
808
809         if (layout->init() != ERROR_OK)
810                 return ERROR_JTAG_INIT_FAILED;
811
812         ftd2xx_speed(jtag_speed);
813         
814         if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
815         {
816                 ERROR("error purging ftd2xx device: %i", status);
817                 return ERROR_JTAG_INIT_FAILED;
818         }
819
820         return ERROR_OK;
821 }
822
823 int usbjtag_init(void)
824 {
825         u8 buf[3];
826         FT_STATUS status;
827         DWORD bytes_written;
828         
829         low_output = 0x08;
830         low_direction = 0x0b;
831         
832         nTRST = 0x10;
833         nTRSTnOE = 0x10;
834         nSRST = 0x40;
835         nSRSTnOE = 0x40;
836         
837         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
838         {
839                 low_direction &= ~nTRSTnOE; /* nTRST input */
840                 low_output &= ~nTRST; /* nTRST = 0 */
841         }
842         else
843         {
844                 low_direction |= nTRSTnOE; /* nTRST output */
845                 low_output |= nTRST; /* nTRST = 1 */
846         }
847         
848         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
849         {
850                 low_direction |= nSRSTnOE; /* nSRST output */
851                 low_output |= nSRST; /* nSRST = 1 */
852         }
853         else
854         {
855                 low_direction &= ~nSRSTnOE; /* nSRST input */
856                 low_output &= ~nSRST; /* nSRST = 0 */
857         }
858         
859         /* initialize low byte for jtag */
860         buf[0] = 0x80; /* command "set data bits low byte" */
861         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
862         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
863         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
864         
865         if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
866         {
867                 ERROR("couldn't write to ftdi device: %i", status);
868                 return ERROR_JTAG_INIT_FAILED;
869         }
870
871         return ERROR_OK;
872 }
873
874 int jtagkey_init(void)
875 {
876         u8 buf[3];
877         FT_STATUS status;
878         DWORD bytes_written;
879         
880         low_output = 0x08;
881         low_direction = 0x1b;
882         
883         /* initialize low byte for jtag */
884         buf[0] = 0x80; /* command "set data bits low byte" */
885         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
886         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
887         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
888         
889         if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
890         {
891                 ERROR("couldn't write to ftdi device: %i", status);
892                 return ERROR_JTAG_INIT_FAILED;
893         }
894         
895         if (strcmp(layout->name, "jtagkey") == 0)
896         {
897                 nTRST = 0x01;
898                 nTRSTnOE = 0x4;
899                 nSRST = 0x02;
900                 nSRSTnOE = 0x08;
901         }
902         else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
903         {
904                 nTRST = 0x02;
905                 nTRSTnOE = 0x1;
906                 nSRST = 0x08;
907                 nSRSTnOE = 0x04;
908         }
909         else
910         {
911                 ERROR("BUG: jtagkey_init called for non jtagkey layout");
912                 exit(-1);
913         }
914         
915         high_output = 0x0;
916         high_direction = 0x0f;
917
918         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
919         {
920                 high_output |= nTRSTnOE;
921                 high_output &= ~nTRST;
922         }
923         else
924         {
925                 high_output &= ~nTRSTnOE;
926                 high_output |= nTRST;
927         }
928         
929         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
930         {
931                 high_output &= ~nSRSTnOE;
932                 high_output |= nSRST;
933         }
934         else
935         {
936                 high_output |= nSRSTnOE;
937                 high_output &= ~nSRST;
938         }
939         
940         /* initialize high port */
941         buf[0] = 0x82; /* command "set data bits low byte" */
942         buf[1] = high_output; /* value */
943         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
944         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
945         
946         if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
947         {
948                 ERROR("couldn't write to ftdi device: %i", status);
949                 return ERROR_JTAG_INIT_FAILED;
950         }
951         
952         return ERROR_OK;
953 }
954
955 int ftd2xx_quit(void)
956 {
957         FT_STATUS status;
958
959         status = FT_Close(ftdih);
960
961         free(ftd2xx_buffer);
962
963         return ERROR_OK;
964 }
965
966 int ftd2xx_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
967 {
968         if (argc == 1)
969         {
970                 ftd2xx_device_desc = strdup(args[0]);
971         }
972         else
973         {
974                 ERROR("expected exactly one argument to ftd2xx_device_desc <description>");
975         }
976         
977         return ERROR_OK;
978 }
979
980 int ftd2xx_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
981 {
982         if (argc == 0)
983                 return ERROR_OK;
984
985         ftd2xx_layout = malloc(strlen(args[0]));
986         strcpy(ftd2xx_layout, args[0]);
987
988         return ERROR_OK;
989 }
990
991 int ftd2xx_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
992 {
993         if (argc >= 2)
994         {
995                 ftd2xx_vid = strtol(args[0], NULL, 0);
996                 ftd2xx_pid = strtol(args[1], NULL, 0);
997         }
998         else
999         {
1000                 WARNING("incomplete ftd2xx_vid_pid configuration directive");
1001         }
1002         
1003         return ERROR_OK;
1004 }