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