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