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