- allow multiple USB vendor and product ids when searching for connected FT2232 based...
[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_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76
77 char *ft2232_device_desc = NULL;
78 char *ft2232_serial = NULL;
79 char *ft2232_layout = NULL;
80
81 #define MAX_USB_IDS 8
82 /* vid = pid = 0 marks the end of the list */
83 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
84 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
85
86 typedef struct ft2232_layout_s
87 {
88 char* name;
89 int(*init)(void);
90 void(*reset)(int trst, int srst);
91 void(*blink)(void);
92 } ft2232_layout_t;
93
94 /* init procedures for supported layouts */
95 int usbjtag_init(void);
96 int jtagkey_init(void);
97 int olimex_jtag_init(void);
98 int m5960_init(void);
99 int turtle_init(void);
100
101 /* reset procedures for supported layouts */
102 void usbjtag_reset(int trst, int srst);
103 void jtagkey_reset(int trst, int srst);
104 void olimex_jtag_reset(int trst, int srst);
105 void m5960_reset(int trst, int srst);
106 void turtle_reset(int trst, int srst);
107
108 /* blink procedures for layouts that support a blinking led */
109 void olimex_jtag_blink(void);
110 void turtle_jtag_blink(void);
111
112 ft2232_layout_t ft2232_layouts[] =
113 {
114 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
115 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
116 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
117 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
118 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
119 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
120 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
121 {"m5960", m5960_init, m5960_reset, NULL},
122 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
123 {NULL, NULL, NULL},
124 };
125
126 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
127
128 static ft2232_layout_t *layout;
129 static u8 low_output = 0x0;
130 static u8 low_direction = 0x0;
131 static u8 high_output = 0x0;
132 static u8 high_direction = 0x0;
133
134 #if BUILD_FT2232_FTD2XX == 1
135 static FT_HANDLE ftdih = NULL;
136 #elif BUILD_FT2232_LIBFTDI == 1
137 static struct ftdi_context ftdic;
138 #endif
139
140 static u8 *ft2232_buffer = NULL;
141 static int ft2232_buffer_size = 0;
142 static int ft2232_read_pointer = 0;
143 static int ft2232_expect_read = 0;
144 #define FT2232_BUFFER_SIZE 131072
145 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
146 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
147
148 jtag_interface_t ft2232_interface =
149 {
150
151 .name = "ft2232",
152
153 .execute_queue = ft2232_execute_queue,
154
155 .support_pathmove = 1,
156
157 .speed = ft2232_speed,
158 .register_commands = ft2232_register_commands,
159 .init = ft2232_init,
160 .quit = ft2232_quit,
161 };
162
163 int ft2232_write(u8 *buf, int size, u32* bytes_written)
164 {
165 #if BUILD_FT2232_FTD2XX == 1
166 FT_STATUS status;
167 DWORD dw_bytes_written;
168 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
169 {
170 *bytes_written = dw_bytes_written;
171 ERROR("FT_Write returned: %lu", status);
172 return ERROR_JTAG_DEVICE_ERROR;
173 }
174 else
175 {
176 *bytes_written = dw_bytes_written;
177 return ERROR_OK;
178 }
179 #elif BUILD_FT2232_LIBFTDI == 1
180 int retval;
181 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
182 {
183 *bytes_written = 0;
184 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
185 return ERROR_JTAG_DEVICE_ERROR;
186 }
187 else
188 {
189 *bytes_written = retval;
190 return ERROR_OK;
191 }
192 #endif
193 }
194
195 int ft2232_read(u8* buf, int size, u32* bytes_read)
196 {
197 #if BUILD_FT2232_FTD2XX == 1
198 DWORD dw_bytes_read;
199 FT_STATUS status;
200 int timeout = 5;
201 *bytes_read = 0;
202
203 while ((*bytes_read < size) && timeout--)
204 {
205 if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
206 {
207 *bytes_read = 0;
208 ERROR("FT_Read returned: %lu", status);
209 return ERROR_JTAG_DEVICE_ERROR;
210 }
211 *bytes_read += dw_bytes_read;
212 }
213 #elif BUILD_FT2232_LIBFTDI == 1
214 int retval;
215 int timeout = 100;
216 *bytes_read = 0;
217
218 while ((*bytes_read < size) && timeout--)
219 {
220 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
221 {
222 *bytes_read = 0;
223 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
224 return ERROR_JTAG_DEVICE_ERROR;
225 }
226 *bytes_read += retval;
227 }
228 #endif
229
230 if (*bytes_read < size)
231 {
232 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
233 return ERROR_JTAG_DEVICE_ERROR;
234 }
235
236 return ERROR_OK;
237 }
238
239 int ft2232_speed(int speed)
240 {
241 u8 buf[3];
242 int retval;
243 u32 bytes_written;
244
245 buf[0] = 0x86; /* command "set divisor" */
246 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
247 buf[2] = (speed >> 8) & 0xff; /* valueH */
248
249 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
250 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
251 {
252 ERROR("couldn't set FT2232 TCK speed");
253 return retval;
254 }
255
256 return ERROR_OK;
257 }
258
259 int ft2232_register_commands(struct command_context_s *cmd_ctx)
260 {
261 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
262 COMMAND_CONFIG, NULL);
263 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
264 COMMAND_CONFIG, NULL);
265 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
266 COMMAND_CONFIG, NULL);
267 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
268 COMMAND_CONFIG, NULL);
269 return ERROR_OK;
270 }
271
272 void ft2232_end_state(state)
273 {
274 if (tap_move_map[state] != -1)
275 end_state = state;
276 else
277 {
278 ERROR("BUG: %i is not a valid end state", state);
279 exit(-1);
280 }
281 }
282
283 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
284 {
285 int num_bytes = ((scan_size + 7) / 8);
286 int bits_left = scan_size;
287 int cur_byte = 0;
288
289 while(num_bytes-- > 1)
290 {
291 buffer[cur_byte] = BUFFER_READ;
292 cur_byte++;
293 bits_left -= 8;
294 }
295
296 buffer[cur_byte] = 0x0;
297
298 if (bits_left > 1)
299 {
300 buffer[cur_byte] = BUFFER_READ >> 1;
301 }
302
303 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
304
305 }
306
307 void ft2232_debug_dump_buffer(void)
308 {
309 int i;
310 char line[256];
311 char *line_p = line;
312
313 for (i = 0; i < ft2232_buffer_size; i++)
314 {
315 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
316 if (i % 16 == 15)
317 {
318 DEBUG("%s", line);
319 line_p = line;
320 }
321 }
322
323 if (line_p != line)
324 DEBUG("%s", line);
325 }
326
327 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
328 {
329 jtag_command_t *cmd;
330 u8 *buffer;
331 int scan_size;
332 enum scan_type type;
333 int retval;
334 u32 bytes_written;
335 u32 bytes_read;
336
337 #ifdef _DEBUG_USB_IO_
338 struct timeval start, inter, inter2, end;
339 struct timeval d_inter, d_inter2, d_end;
340 #endif
341
342 #ifdef _DEBUG_USB_COMMS_
343 DEBUG("write buffer (size %i):", ft2232_buffer_size);
344 ft2232_debug_dump_buffer();
345 #endif
346
347 #ifdef _DEBUG_USB_IO_
348 gettimeofday(&start, NULL);
349 #endif
350
351 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
352 {
353 ERROR("couldn't write MPSSE commands to FT2232");
354 exit(-1);
355 }
356
357 #ifdef _DEBUG_USB_IO_
358 gettimeofday(&inter, NULL);
359 #endif
360
361 if (ft2232_expect_read)
362 {
363 int timeout = 100;
364 ft2232_buffer_size = 0;
365
366 #ifdef _DEBUG_USB_IO_
367 gettimeofday(&inter2, NULL);
368 #endif
369
370 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
371 {
372 ERROR("couldn't read from FT2232");
373 exit(-1);
374 }
375
376 #ifdef _DEBUG_USB_IO_
377 gettimeofday(&end, NULL);
378
379 timeval_subtract(&d_inter, &inter, &start);
380 timeval_subtract(&d_inter2, &inter2, &start);
381 timeval_subtract(&d_end, &end, &start);
382
383 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);
384 #endif
385
386
387 ft2232_buffer_size = bytes_read;
388
389 if (ft2232_expect_read != ft2232_buffer_size)
390 {
391 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
392 ft2232_debug_dump_buffer();
393
394 exit(-1);
395 }
396
397 #ifdef _DEBUG_USB_COMMS_
398 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
399 ft2232_debug_dump_buffer();
400 #endif
401 }
402
403 ft2232_expect_read = 0;
404 ft2232_read_pointer = 0;
405
406 cmd = first;
407 while (cmd != last)
408 {
409 switch (cmd->type)
410 {
411 case JTAG_SCAN:
412 type = jtag_scan_type(cmd->cmd.scan);
413 if (type != SCAN_OUT)
414 {
415 scan_size = jtag_scan_size(cmd->cmd.scan);
416 buffer = calloc(CEIL(scan_size, 8), 1);
417 ft2232_read_scan(type, buffer, scan_size);
418 jtag_read_buffer(buffer, cmd->cmd.scan);
419 free(buffer);
420 }
421 break;
422 default:
423 break;
424 }
425 cmd = cmd->next;
426 }
427
428 ft2232_buffer_size = 0;
429
430 return ERROR_OK;
431 }
432
433 void ft2232_add_pathmove(pathmove_command_t *cmd)
434 {
435 int num_states = cmd->num_states;
436 u8 tms_byte;
437 int state_count;
438
439 state_count = 0;
440 while (num_states)
441 {
442 tms_byte = 0x0;
443 int bit_count = 0;
444
445 /* command "Clock Data to TMS/CS Pin (no Read)" */
446 BUFFER_ADD = 0x4b;
447 /* number of states remaining */
448 BUFFER_ADD = (num_states % 7) - 1;
449
450 while (num_states % 7)
451 {
452 if (tap_transitions[cur_state].low == cmd->path[state_count])
453 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
454 else if (tap_transitions[cur_state].high == cmd->path[state_count])
455 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
456 else
457 {
458 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
459 exit(-1);
460 }
461
462 cur_state = cmd->path[state_count];
463 state_count++;
464 num_states--;
465 }
466
467 BUFFER_ADD = tms_byte;
468 }
469
470 end_state = cur_state;
471 }
472
473 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
474 {
475 int num_bytes = (scan_size + 7) / 8;
476 int bits_left = scan_size;
477 int cur_byte = 0;
478 int last_bit;
479
480 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
481 {
482 /* command "Clock Data to TMS/CS Pin (no Read)" */
483 BUFFER_ADD = 0x4b;
484 /* scan 7 bit */
485 BUFFER_ADD = 0x6;
486 /* TMS data bits */
487 if (ir_scan)
488 {
489 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
490 cur_state = TAP_SI;
491 }
492 else
493 {
494 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
495 cur_state = TAP_SD;
496 }
497 //DEBUG("added TMS scan (no read)");
498 }
499
500 /* add command for complete bytes */
501 while (num_bytes > 1)
502 {
503 int thisrun_bytes;
504 if (type == SCAN_IO)
505 {
506 /* Clock Data Bytes In and Out LSB First */
507 BUFFER_ADD = 0x39;
508 //DEBUG("added TDI bytes (io %i)", num_bytes);
509 }
510 else if (type == SCAN_OUT)
511 {
512 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
513 BUFFER_ADD = 0x19;
514 //DEBUG("added TDI bytes (o)");
515 }
516 else if (type == SCAN_IN)
517 {
518 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
519 BUFFER_ADD = 0x28;
520 //DEBUG("added TDI bytes (i %i)", num_bytes);
521 }
522 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
523 num_bytes -= thisrun_bytes;
524 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
525 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
526 if (type != SCAN_IN)
527 {
528 /* add complete bytes */
529 while(thisrun_bytes-- > 0)
530 {
531 BUFFER_ADD = buffer[cur_byte];
532 cur_byte++;
533 bits_left -= 8;
534 }
535 }
536 else /* (type == SCAN_IN) */
537 {
538 bits_left -= 8 * (thisrun_bytes);
539 }
540 }
541
542 /* the most signifcant bit is scanned during TAP movement */
543 if (type != SCAN_IN)
544 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
545 else
546 last_bit = 0;
547
548 /* process remaining bits but the last one */
549 if (bits_left > 1)
550 {
551 if (type == SCAN_IO)
552 {
553 /* Clock Data Bits In and Out LSB First */
554 BUFFER_ADD = 0x3b;
555 //DEBUG("added TDI bits (io) %i", bits_left - 1);
556 }
557 else if (type == SCAN_OUT)
558 {
559 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
560 BUFFER_ADD = 0x1b;
561 //DEBUG("added TDI bits (o)");
562 }
563 else if (type == SCAN_IN)
564 {
565 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
566 BUFFER_ADD = 0x2a;
567 //DEBUG("added TDI bits (i %i)", bits_left - 1);
568 }
569 BUFFER_ADD = bits_left - 2;
570 if (type != SCAN_IN)
571 BUFFER_ADD = buffer[cur_byte];
572 }
573
574 if ((ir_scan && (end_state == TAP_SI)) ||
575 (!ir_scan && (end_state == TAP_SD)))
576 {
577 if (type == SCAN_IO)
578 {
579 /* Clock Data Bits In and Out LSB First */
580 BUFFER_ADD = 0x3b;
581 //DEBUG("added TDI bits (io) %i", bits_left - 1);
582 }
583 else if (type == SCAN_OUT)
584 {
585 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
586 BUFFER_ADD = 0x1b;
587 //DEBUG("added TDI bits (o)");
588 }
589 else if (type == SCAN_IN)
590 {
591 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
592 BUFFER_ADD = 0x2a;
593 //DEBUG("added TDI bits (i %i)", bits_left - 1);
594 }
595 BUFFER_ADD = 0x0;
596 BUFFER_ADD = last_bit;
597 }
598 else
599 {
600 /* move from Shift-IR/DR to end state */
601 if (type != SCAN_OUT)
602 {
603 /* Clock Data to TMS/CS Pin with Read */
604 BUFFER_ADD = 0x6b;
605 //DEBUG("added TMS scan (read)");
606 }
607 else
608 {
609 /* Clock Data to TMS/CS Pin (no Read) */
610 BUFFER_ADD = 0x4b;
611 //DEBUG("added TMS scan (no read)");
612 }
613 BUFFER_ADD = 0x6;
614 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
615 cur_state = end_state;
616 }
617 }
618
619 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
620 {
621 int num_bytes = (scan_size + 7) / 8;
622 int bits_left = scan_size;
623 int cur_byte = 0;
624 int last_bit;
625 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
626 u8 *receive_pointer = receive_buffer;
627 u32 bytes_written;
628 u32 bytes_read;
629 int retval;
630 int thisrun_read = 0;
631
632 if (cmd->ir_scan)
633 {
634 ERROR("BUG: large IR scans are not supported");
635 exit(-1);
636 }
637
638 if (cur_state != TAP_SD)
639 {
640 /* command "Clock Data to TMS/CS Pin (no Read)" */
641 BUFFER_ADD = 0x4b;
642 /* scan 7 bit */
643 BUFFER_ADD = 0x6;
644 /* TMS data bits */
645 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
646 cur_state = TAP_SD;
647 }
648
649 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
650 {
651 ERROR("couldn't write MPSSE commands to FT2232");
652 exit(-1);
653 }
654 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
655 ft2232_buffer_size = 0;
656
657 /* add command for complete bytes */
658 while (num_bytes > 1)
659 {
660 int thisrun_bytes;
661
662 if (type == SCAN_IO)
663 {
664 /* Clock Data Bytes In and Out LSB First */
665 BUFFER_ADD = 0x39;
666 //DEBUG("added TDI bytes (io %i)", num_bytes);
667 }
668 else if (type == SCAN_OUT)
669 {
670 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
671 BUFFER_ADD = 0x19;
672 //DEBUG("added TDI bytes (o)");
673 }
674 else if (type == SCAN_IN)
675 {
676 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
677 BUFFER_ADD = 0x28;
678 //DEBUG("added TDI bytes (i %i)", num_bytes);
679 }
680 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
681 thisrun_read = thisrun_bytes;
682 num_bytes -= thisrun_bytes;
683 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
684 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
685 if (type != SCAN_IN)
686 {
687 /* add complete bytes */
688 while(thisrun_bytes-- > 0)
689 {
690 BUFFER_ADD = buffer[cur_byte];
691 cur_byte++;
692 bits_left -= 8;
693 }
694 }
695 else /* (type == SCAN_IN) */
696 {
697 bits_left -= 8 * (thisrun_bytes);
698 }
699
700 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
701 {
702 ERROR("couldn't write MPSSE commands to FT2232");
703 exit(-1);
704 }
705 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
706 ft2232_buffer_size = 0;
707
708 if (type != SCAN_OUT)
709 {
710 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
711 {
712 ERROR("couldn't read from FT2232");
713 exit(-1);
714 }
715 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
716 receive_pointer += bytes_read;
717 }
718 }
719
720 thisrun_read = 0;
721
722 /* the most signifcant bit is scanned during TAP movement */
723 if (type != SCAN_IN)
724 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
725 else
726 last_bit = 0;
727
728 /* process remaining bits but the last one */
729 if (bits_left > 1)
730 {
731 if (type == SCAN_IO)
732 {
733 /* Clock Data Bits In and Out LSB First */
734 BUFFER_ADD = 0x3b;
735 //DEBUG("added TDI bits (io) %i", bits_left - 1);
736 }
737 else if (type == SCAN_OUT)
738 {
739 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
740 BUFFER_ADD = 0x1b;
741 //DEBUG("added TDI bits (o)");
742 }
743 else if (type == SCAN_IN)
744 {
745 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
746 BUFFER_ADD = 0x2a;
747 //DEBUG("added TDI bits (i %i)", bits_left - 1);
748 }
749 BUFFER_ADD = bits_left - 2;
750 if (type != SCAN_IN)
751 BUFFER_ADD = buffer[cur_byte];
752
753 if (type != SCAN_OUT)
754 thisrun_read += 2;
755 }
756
757 if (end_state == TAP_SD)
758 {
759 if (type == SCAN_IO)
760 {
761 /* Clock Data Bits In and Out LSB First */
762 BUFFER_ADD = 0x3b;
763 //DEBUG("added TDI bits (io) %i", bits_left - 1);
764 }
765 else if (type == SCAN_OUT)
766 {
767 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
768 BUFFER_ADD = 0x1b;
769 //DEBUG("added TDI bits (o)");
770 }
771 else if (type == SCAN_IN)
772 {
773 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
774 BUFFER_ADD = 0x2a;
775 //DEBUG("added TDI bits (i %i)", bits_left - 1);
776 }
777 BUFFER_ADD = 0x0;
778 BUFFER_ADD = last_bit;
779 }
780 else
781 {
782 /* move from Shift-IR/DR to end state */
783 if (type != SCAN_OUT)
784 {
785 /* Clock Data to TMS/CS Pin with Read */
786 BUFFER_ADD = 0x6b;
787 //DEBUG("added TMS scan (read)");
788 }
789 else
790 {
791 /* Clock Data to TMS/CS Pin (no Read) */
792 BUFFER_ADD = 0x4b;
793 //DEBUG("added TMS scan (no read)");
794 }
795 BUFFER_ADD = 0x6;
796 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
797 cur_state = end_state;
798 }
799
800 if (type != SCAN_OUT)
801 thisrun_read += 1;
802
803 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
804 {
805 ERROR("couldn't write MPSSE commands to FT2232");
806 exit(-1);
807 }
808 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
809 ft2232_buffer_size = 0;
810
811 if (type != SCAN_OUT)
812 {
813 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
814 {
815 ERROR("couldn't read from FT2232");
816 exit(-1);
817 }
818 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
819 receive_pointer += bytes_read;
820 }
821
822 return ERROR_OK;
823 }
824
825 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
826 {
827 int predicted_size = 3;
828 int num_bytes = (scan_size - 1) / 8;
829
830 if (cur_state != TAP_SD)
831 predicted_size += 3;
832
833 if (type == SCAN_IN) /* only from device to host */
834 {
835 /* complete bytes */
836 predicted_size += (CEIL(num_bytes, 65536)) * 3;
837 /* remaining bits - 1 (up to 7) */
838 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
839 }
840 else /* host to device, or bidirectional */
841 {
842 /* complete bytes */
843 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
844 /* remaining bits -1 (up to 7) */
845 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
846 }
847
848 return predicted_size;
849 }
850
851 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
852 {
853 int predicted_size = 0;
854
855 if (type != SCAN_OUT)
856 {
857 /* complete bytes */
858 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
859 /* remaining bits - 1 */
860 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
861 /* last bit (from TMS scan) */
862 predicted_size += 1;
863 }
864
865 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
866
867 return predicted_size;
868 }
869
870 void usbjtag_reset(int trst, int srst)
871 {
872 if (trst == 1)
873 {
874 cur_state = TAP_TLR;
875 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
876 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
877 else
878 low_output &= ~nTRST; /* switch output low */
879 }
880 else if (trst == 0)
881 {
882 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
883 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
884 else
885 low_output |= nTRST; /* switch output high */
886 }
887
888 if (srst == 1)
889 {
890 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
891 low_output &= ~nSRST; /* switch output low */
892 else
893 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
894 }
895 else if (srst == 0)
896 {
897 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
898 low_output |= nSRST; /* switch output high */
899 else
900 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
901 }
902
903 /* command "set data bits low byte" */
904 BUFFER_ADD = 0x80;
905 BUFFER_ADD = low_output;
906 BUFFER_ADD = low_direction;
907
908 }
909
910 void jtagkey_reset(int trst, int srst)
911 {
912 if (trst == 1)
913 {
914 cur_state = TAP_TLR;
915 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
916 high_output &= ~nTRSTnOE;
917 else
918 high_output &= ~nTRST;
919 }
920 else if (trst == 0)
921 {
922 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
923 high_output |= nTRSTnOE;
924 else
925 high_output |= nTRST;
926 }
927
928 if (srst == 1)
929 {
930 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
931 high_output &= ~nSRST;
932 else
933 high_output &= ~nSRSTnOE;
934 }
935 else if (srst == 0)
936 {
937 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
938 high_output |= nSRST;
939 else
940 high_output |= nSRSTnOE;
941 }
942
943 /* command "set data bits high byte" */
944 BUFFER_ADD = 0x82;
945 BUFFER_ADD = high_output;
946 BUFFER_ADD = high_direction;
947 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
948 }
949
950 void olimex_jtag_reset(int trst, int srst)
951 {
952 if (trst == 1)
953 {
954 cur_state = TAP_TLR;
955 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
956 high_output &= ~nTRSTnOE;
957 else
958 high_output &= ~nTRST;
959 }
960 else if (trst == 0)
961 {
962 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
963 high_output |= nTRSTnOE;
964 else
965 high_output |= nTRST;
966 }
967
968 if (srst == 1)
969 {
970 high_output |= nSRST;
971 }
972 else if (srst == 0)
973 {
974 high_output &= ~nSRST;
975 }
976
977 /* command "set data bits high byte" */
978 BUFFER_ADD = 0x82;
979 BUFFER_ADD = high_output;
980 BUFFER_ADD = high_direction;
981 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
982 }
983
984 void m5960_reset(int trst, int srst)
985 {
986 if (trst == 1)
987 {
988 cur_state = TAP_TLR;
989 low_output &= ~nTRST;
990 }
991 else if (trst == 0)
992 {
993 low_output |= nTRST;
994 }
995
996 if (srst == 1)
997 {
998 low_output |= nSRST;
999 }
1000 else if (srst == 0)
1001 {
1002 low_output &= ~nSRST;
1003 }
1004
1005 /* command "set data bits low byte" */
1006 BUFFER_ADD = 0x80;
1007 BUFFER_ADD = low_output;
1008 BUFFER_ADD = low_direction;
1009 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1010 }
1011
1012 void turtle_reset(int trst, int srst)
1013 {
1014 trst = trst;
1015
1016 if (srst == 1)
1017 {
1018 low_output |= nSRST;
1019 }
1020 else if (srst == 0)
1021 {
1022 low_output &= ~nSRST;
1023 }
1024
1025 /* command "set data bits high byte" */
1026 BUFFER_ADD = 0x80;
1027 BUFFER_ADD = low_output;
1028 BUFFER_ADD = low_direction;
1029 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1030 }
1031
1032 int ft2232_execute_queue()
1033 {
1034 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1035 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1036 u8 *buffer;
1037 int scan_size; /* size of IR or DR scan */
1038 enum scan_type type;
1039 int i;
1040 int predicted_size = 0;
1041 int require_send = 0;
1042
1043 ft2232_buffer_size = 0;
1044 ft2232_expect_read = 0;
1045
1046 /* blink, if the current layout has that feature */
1047 if (layout->blink)
1048 layout->blink();
1049
1050 while (cmd)
1051 {
1052 switch(cmd->type)
1053 {
1054 case JTAG_END_STATE:
1055 if (cmd->cmd.end_state->end_state != -1)
1056 ft2232_end_state(cmd->cmd.end_state->end_state);
1057 break;
1058 case JTAG_RESET:
1059 /* only send the maximum buffer size that FT2232C can handle */
1060 predicted_size = 3;
1061 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1062 {
1063 ft2232_send_and_recv(first_unsent, cmd);
1064 require_send = 0;
1065 first_unsent = cmd;
1066 }
1067
1068 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1069 require_send = 1;
1070
1071 #ifdef _DEBUG_JTAG_IO_
1072 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1073 #endif
1074 break;
1075 case JTAG_RUNTEST:
1076 /* only send the maximum buffer size that FT2232C can handle */
1077 predicted_size = 0;
1078 if (cur_state != TAP_RTI)
1079 predicted_size += 3;
1080 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1081 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1082 predicted_size += 3;
1083 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1084 predicted_size += 3;
1085 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1086 {
1087 ft2232_send_and_recv(first_unsent, cmd);
1088 require_send = 0;
1089 first_unsent = cmd;
1090 }
1091 if (cur_state != TAP_RTI)
1092 {
1093 /* command "Clock Data to TMS/CS Pin (no Read)" */
1094 BUFFER_ADD = 0x4b;
1095 /* scan 7 bit */
1096 BUFFER_ADD = 0x6;
1097 /* TMS data bits */
1098 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1099 cur_state = TAP_RTI;
1100 require_send = 1;
1101 }
1102 i = cmd->cmd.runtest->num_cycles;
1103 while (i > 0)
1104 {
1105 /* command "Clock Data to TMS/CS Pin (no Read)" */
1106 BUFFER_ADD = 0x4b;
1107 /* scan 7 bit */
1108 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1109 /* TMS data bits */
1110 BUFFER_ADD = 0x0;
1111 cur_state = TAP_RTI;
1112 i -= (i > 7) ? 7 : i;
1113 //DEBUG("added TMS scan (no read)");
1114 }
1115 if (cmd->cmd.runtest->end_state != -1)
1116 ft2232_end_state(cmd->cmd.runtest->end_state);
1117 if (cur_state != end_state)
1118 {
1119 /* command "Clock Data to TMS/CS Pin (no Read)" */
1120 BUFFER_ADD = 0x4b;
1121 /* scan 7 bit */
1122 BUFFER_ADD = 0x6;
1123 /* TMS data bits */
1124 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1125 cur_state = end_state;
1126 //DEBUG("added TMS scan (no read)");
1127 }
1128 require_send = 1;
1129 #ifdef _DEBUG_JTAG_IO_
1130 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1131 #endif
1132 break;
1133 case JTAG_STATEMOVE:
1134 /* only send the maximum buffer size that FT2232C can handle */
1135 predicted_size = 3;
1136 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1137 {
1138 ft2232_send_and_recv(first_unsent, cmd);
1139 require_send = 0;
1140 first_unsent = cmd;
1141 }
1142 if (cmd->cmd.statemove->end_state != -1)
1143 ft2232_end_state(cmd->cmd.statemove->end_state);
1144 /* command "Clock Data to TMS/CS Pin (no Read)" */
1145 BUFFER_ADD = 0x4b;
1146 /* scan 7 bit */
1147 BUFFER_ADD = 0x6;
1148 /* TMS data bits */
1149 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1150 //DEBUG("added TMS scan (no read)");
1151 cur_state = end_state;
1152 require_send = 1;
1153 #ifdef _DEBUG_JTAG_IO_
1154 DEBUG("statemove: %i", end_state);
1155 #endif
1156 break;
1157 case JTAG_PATHMOVE:
1158 /* only send the maximum buffer size that FT2232C can handle */
1159 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1160 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1161 {
1162 ft2232_send_and_recv(first_unsent, cmd);
1163 require_send = 0;
1164 first_unsent = cmd;
1165 }
1166 ft2232_add_pathmove(cmd->cmd.pathmove);
1167 require_send = 1;
1168 #ifdef _DEBUG_JTAG_IO_
1169 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1170 #endif
1171 break;
1172 case JTAG_SCAN:
1173 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1174 type = jtag_scan_type(cmd->cmd.scan);
1175 predicted_size = ft2232_predict_scan_out(scan_size, type);
1176 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1177 {
1178 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1179 /* unsent commands before this */
1180 if (first_unsent != cmd)
1181 ft2232_send_and_recv(first_unsent, cmd);
1182
1183 /* current command */
1184 if (cmd->cmd.scan->end_state != -1)
1185 ft2232_end_state(cmd->cmd.scan->end_state);
1186 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1187 require_send = 0;
1188 first_unsent = cmd->next;
1189 if (buffer)
1190 free(buffer);
1191 break;
1192 }
1193 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1194 {
1195 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1196 ft2232_send_and_recv(first_unsent, cmd);
1197 require_send = 0;
1198 first_unsent = cmd;
1199 }
1200 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1201 //DEBUG("new read size: %i", ft2232_expect_read);
1202 if (cmd->cmd.scan->end_state != -1)
1203 ft2232_end_state(cmd->cmd.scan->end_state);
1204 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1205 require_send = 1;
1206 if (buffer)
1207 free(buffer);
1208 #ifdef _DEBUG_JTAG_IO_
1209 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1210 #endif
1211 break;
1212 case JTAG_SLEEP:
1213 ft2232_send_and_recv(first_unsent, cmd);
1214 first_unsent = cmd->next;
1215 jtag_sleep(cmd->cmd.sleep->us);
1216 #ifdef _DEBUG_JTAG_IO_
1217 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1218 #endif
1219 break;
1220 default:
1221 ERROR("BUG: unknown JTAG command type encountered");
1222 exit(-1);
1223 }
1224 cmd = cmd->next;
1225 }
1226
1227 if (require_send > 0)
1228 ft2232_send_and_recv(first_unsent, cmd);
1229
1230 return ERROR_OK;
1231 }
1232
1233 #if BUILD_FT2232_FTD2XX == 1
1234 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1235 {
1236 FT_STATUS status;
1237 DWORD openex_flags = 0;
1238 char *openex_string = NULL;
1239 u8 latency_timer;
1240
1241 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1242 ft2232_layout, vid, pid);
1243
1244 #if IS_WIN32 == 0
1245 /* Add non-standard Vid/Pid to the linux driver */
1246 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1247 {
1248 WARNING("couldn't add %4.4x:%4.4x",
1249 vid, pid);
1250 }
1251 #endif
1252
1253 if (ft2232_device_desc && ft2232_serial)
1254 {
1255 WARNING("can't open by device description and serial number, giving precedence to serial");
1256 ft2232_device_desc = NULL;
1257 }
1258
1259 if (ft2232_device_desc)
1260 {
1261 openex_string = ft2232_device_desc;
1262 openex_flags = FT_OPEN_BY_DESCRIPTION;
1263 }
1264 else if (ft2232_serial)
1265 {
1266 openex_string = ft2232_serial;
1267 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1268 }
1269 else
1270 {
1271 ERROR("neither device description nor serial number specified");
1272 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1273
1274 return ERROR_JTAG_INIT_FAILED;
1275 }
1276
1277 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1278 {
1279 DWORD num_devices;
1280
1281 if (more) {
1282 WARNING("unable to open ftdi device (trying more): %lu",
1283 status);
1284 *try_more = 1;
1285 return ERROR_JTAG_INIT_FAILED;
1286 }
1287 ERROR("unable to open ftdi device: %lu", status);
1288 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1289 if (status == FT_OK)
1290 {
1291 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1292 int i;
1293
1294 for (i = 0; i < num_devices; i++)
1295 desc_array[i] = malloc(64);
1296 desc_array[num_devices] = NULL;
1297
1298 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1299
1300 if (status == FT_OK)
1301 {
1302 ERROR("ListDevices: %lu\n", num_devices);
1303 for (i = 0; i < num_devices; i++)
1304 ERROR("%i: %s", i, desc_array[i]);
1305 }
1306
1307 for (i = 0; i < num_devices; i++)
1308 free(desc_array[i]);
1309 free(desc_array);
1310 }
1311 else
1312 {
1313 printf("ListDevices: NONE\n");
1314 }
1315 return ERROR_JTAG_INIT_FAILED;
1316 }
1317
1318 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1319 {
1320 ERROR("unable to set latency timer: %lu", status);
1321 return ERROR_JTAG_INIT_FAILED;
1322 }
1323
1324 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1325 {
1326 ERROR("unable to get latency timer: %lu", status);
1327 return ERROR_JTAG_INIT_FAILED;
1328 }
1329 else
1330 {
1331 DEBUG("current latency timer: %i", latency_timer);
1332 }
1333
1334 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1335 {
1336 ERROR("unable to set timeouts: %lu", status);
1337 return ERROR_JTAG_INIT_FAILED;
1338 }
1339
1340 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1341 {
1342 ERROR("unable to enable bit i/o mode: %lu", status);
1343 return ERROR_JTAG_INIT_FAILED;
1344 }
1345
1346 return ERROR_OK;
1347 }
1348
1349 static int ft2232_purge_ftd2xx(void)
1350 {
1351 FT_STATUS status;
1352
1353 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1354 {
1355 ERROR("error purging ftd2xx device: %lu", status);
1356 return ERROR_JTAG_INIT_FAILED;
1357 }
1358
1359 return ERROR_OK;
1360 }
1361 #endif /* BUILD_FT2232_FTD2XX == 1 */
1362
1363 #if BUILD_FT2232_LIBFTDI == 1
1364 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1365 {
1366 u8 latency_timer;
1367
1368 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1369 ft2232_layout, vid, pid);
1370
1371 if (ftdi_init(&ftdic) < 0)
1372 return ERROR_JTAG_INIT_FAILED;
1373
1374 /* context, vendor id, product id */
1375 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1376 ft2232_serial) < 0) {
1377 if (more)
1378 WARNING("unable to open ftdi device (trying more): %s",
1379 ftdic.error_str);
1380 else
1381 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1382 *try_more = 1;
1383 return ERROR_JTAG_INIT_FAILED;
1384 }
1385
1386 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1387 {
1388 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1389 return ERROR_JTAG_INIT_FAILED;
1390 }
1391
1392 if (ftdi_usb_reset(&ftdic) < 0)
1393 {
1394 ERROR("unable to reset ftdi device");
1395 return ERROR_JTAG_INIT_FAILED;
1396 }
1397
1398 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1399 {
1400 ERROR("unable to set latency timer");
1401 return ERROR_JTAG_INIT_FAILED;
1402 }
1403
1404 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1405 {
1406 ERROR("unable to get latency timer");
1407 return ERROR_JTAG_INIT_FAILED;
1408 }
1409 else
1410 {
1411 DEBUG("current latency timer: %i", latency_timer);
1412 }
1413
1414 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1415
1416 return ERROR_OK;
1417 }
1418
1419 static int ft2232_purge_libftdi(void)
1420 {
1421 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1422 {
1423 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1424 return ERROR_JTAG_INIT_FAILED;
1425 }
1426
1427 return ERROR_OK;
1428 }
1429 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1430
1431 int ft2232_init(void)
1432 {
1433 u8 buf[1];
1434 int retval;
1435 u32 bytes_written;
1436 ft2232_layout_t *cur_layout = ft2232_layouts;
1437 int i;
1438
1439 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1440 {
1441 ft2232_layout = "usbjtag";
1442 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1443 }
1444
1445 while (cur_layout->name)
1446 {
1447 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1448 {
1449 layout = cur_layout;
1450 break;
1451 }
1452 cur_layout++;
1453 }
1454
1455 if (!layout)
1456 {
1457 ERROR("No matching layout found for %s", ft2232_layout);
1458 return ERROR_JTAG_INIT_FAILED;
1459 }
1460
1461 for (i = 0; 1; i++) {
1462 /*
1463 * "more indicates that there are more IDs to try, so we should
1464 * not print an error for an ID mismatch (but for anything
1465 * else, we should).
1466 *
1467 * try_more indicates that the error code returned indicates an
1468 * ID mismatch (and nothing else) and that we should proceeed
1469 * with the next ID pair.
1470 */
1471 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1472 int try_more = 0;
1473
1474 #if BUILD_FT2232_FTD2XX == 1
1475 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1476 more, &try_more);
1477 #elif BUILD_FT2232_LIBFTDI == 1
1478 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1479 more, &try_more);
1480 #endif
1481 if (retval >= 0)
1482 break;
1483 if (!more || !try_more)
1484 return retval;
1485 }
1486
1487 ft2232_buffer_size = 0;
1488 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1489
1490 if (layout->init() != ERROR_OK)
1491 return ERROR_JTAG_INIT_FAILED;
1492
1493 ft2232_speed(jtag_speed);
1494
1495 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1496 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1497 {
1498 ERROR("couldn't write to FT2232 to disable loopback");
1499 return ERROR_JTAG_INIT_FAILED;
1500 }
1501
1502 #if BUILD_FT2232_FTD2XX == 1
1503 return ft2232_purge_ftd2xx();
1504 #elif BUILD_FT2232_LIBFTDI == 1
1505 return ft2232_purge_libftdi();
1506 #endif
1507
1508 return ERROR_OK;
1509 }
1510
1511 int usbjtag_init(void)
1512 {
1513 u8 buf[3];
1514 u32 bytes_written;
1515
1516 low_output = 0x08;
1517 low_direction = 0x0b;
1518
1519 if (strcmp(ft2232_layout, "usbjtag") == 0)
1520 {
1521 nTRST = 0x10;
1522 nTRSTnOE = 0x10;
1523 nSRST = 0x40;
1524 nSRSTnOE = 0x40;
1525 }
1526 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1527 {
1528 nTRST = 0x10;
1529 nTRSTnOE = 0x10;
1530 nSRST = 0x20;
1531 nSRSTnOE = 0x20;
1532 }
1533 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1534 {
1535 nTRST = 0x0;
1536 nTRSTnOE = 0x00;
1537 nSRST = 0x20;
1538 nSRSTnOE = 0x20;
1539 low_output = 0x88;
1540 low_direction = 0x8b;
1541 }
1542 else
1543 {
1544 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1545 return ERROR_JTAG_INIT_FAILED;
1546 }
1547
1548 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1549 {
1550 low_direction &= ~nTRSTnOE; /* nTRST input */
1551 low_output &= ~nTRST; /* nTRST = 0 */
1552 }
1553 else
1554 {
1555 low_direction |= nTRSTnOE; /* nTRST output */
1556 low_output |= nTRST; /* nTRST = 1 */
1557 }
1558
1559 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1560 {
1561 low_direction |= nSRSTnOE; /* nSRST output */
1562 low_output |= nSRST; /* nSRST = 1 */
1563 }
1564 else
1565 {
1566 low_direction &= ~nSRSTnOE; /* nSRST input */
1567 low_output &= ~nSRST; /* nSRST = 0 */
1568 }
1569
1570 /* initialize low byte for jtag */
1571 buf[0] = 0x80; /* command "set data bits low byte" */
1572 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1573 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1574 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1575
1576 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1577 {
1578 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1579 return ERROR_JTAG_INIT_FAILED;
1580 }
1581
1582 return ERROR_OK;
1583 }
1584
1585 int jtagkey_init(void)
1586 {
1587 u8 buf[3];
1588 u32 bytes_written;
1589
1590 low_output = 0x08;
1591 low_direction = 0x1b;
1592
1593 /* initialize low byte for jtag */
1594 buf[0] = 0x80; /* command "set data bits low byte" */
1595 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1596 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1597 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1598
1599 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1600 {
1601 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1602 return ERROR_JTAG_INIT_FAILED;
1603 }
1604
1605 if (strcmp(layout->name, "jtagkey") == 0)
1606 {
1607 nTRST = 0x01;
1608 nTRSTnOE = 0x4;
1609 nSRST = 0x02;
1610 nSRSTnOE = 0x08;
1611 }
1612 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1613 (strcmp(layout->name, "oocdlink") == 0))
1614 {
1615 nTRST = 0x02;
1616 nTRSTnOE = 0x1;
1617 nSRST = 0x08;
1618 nSRSTnOE = 0x04;
1619 }
1620 else
1621 {
1622 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1623 exit(-1);
1624 }
1625
1626 high_output = 0x0;
1627 high_direction = 0x0f;
1628
1629 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1630 {
1631 high_output |= nTRSTnOE;
1632 high_output &= ~nTRST;
1633 }
1634 else
1635 {
1636 high_output &= ~nTRSTnOE;
1637 high_output |= nTRST;
1638 }
1639
1640 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1641 {
1642 high_output &= ~nSRSTnOE;
1643 high_output |= nSRST;
1644 }
1645 else
1646 {
1647 high_output |= nSRSTnOE;
1648 high_output &= ~nSRST;
1649 }
1650
1651 /* initialize high port */
1652 buf[0] = 0x82; /* command "set data bits high byte" */
1653 buf[1] = high_output; /* value */
1654 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1655 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1656
1657 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1658 {
1659 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1660 return ERROR_JTAG_INIT_FAILED;
1661 }
1662
1663 return ERROR_OK;
1664 }
1665
1666 int olimex_jtag_init(void)
1667 {
1668 u8 buf[3];
1669 u32 bytes_written;
1670
1671 low_output = 0x08;
1672 low_direction = 0x1b;
1673
1674 /* initialize low byte for jtag */
1675 buf[0] = 0x80; /* command "set data bits low byte" */
1676 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1677 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1678 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1679
1680 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1681 {
1682 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1683 return ERROR_JTAG_INIT_FAILED;
1684 }
1685
1686 nTRST = 0x01;
1687 nTRSTnOE = 0x4;
1688 nSRST = 0x02;
1689 nSRSTnOE = 0x00; /* no output enable for nSRST */
1690
1691 high_output = 0x0;
1692 high_direction = 0x0f;
1693
1694 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1695 {
1696 high_output |= nTRSTnOE;
1697 high_output &= ~nTRST;
1698 }
1699 else
1700 {
1701 high_output &= ~nTRSTnOE;
1702 high_output |= nTRST;
1703 }
1704
1705 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1706 {
1707 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1708 }
1709 else
1710 {
1711 high_output &= ~nSRST;
1712 }
1713
1714 /* turn red LED on */
1715 high_output |= 0x08;
1716
1717 /* initialize high port */
1718 buf[0] = 0x82; /* command "set data bits high byte" */
1719 buf[1] = high_output; /* value */
1720 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1721 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1722
1723 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1724 {
1725 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1726 return ERROR_JTAG_INIT_FAILED;
1727 }
1728
1729 return ERROR_OK;
1730 }
1731
1732 int m5960_init(void)
1733 {
1734 u8 buf[3];
1735 u32 bytes_written;
1736
1737 low_output = 0x18;
1738 low_direction = 0xfb;
1739
1740 /* initialize low byte for jtag */
1741 buf[0] = 0x80; /* command "set data bits low byte" */
1742 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1743 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1744 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1745
1746 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1747 {
1748 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1749 return ERROR_JTAG_INIT_FAILED;
1750 }
1751
1752 nTRST = 0x10;
1753 nTRSTnOE = 0x0; /* not output enable for nTRST */
1754 nSRST = 0x20;
1755 nSRSTnOE = 0x00; /* no output enable for nSRST */
1756
1757 high_output = 0x00;
1758 high_direction = 0x0c;
1759
1760 /* turn red LED1 on, LED2 off */
1761 high_output |= 0x08;
1762
1763 /* initialize high port */
1764 buf[0] = 0x82; /* command "set data bits high byte" */
1765 buf[1] = high_output; /* value */
1766 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1767 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1768
1769 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1770 {
1771 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1772 return ERROR_JTAG_INIT_FAILED;
1773 }
1774
1775 return ERROR_OK;
1776 }
1777
1778 int turtle_init(void)
1779 {
1780 u8 buf[3];
1781 u32 bytes_written;
1782
1783 low_output = 0x08;
1784 low_direction = 0x5b;
1785
1786 /* initialize low byte for jtag */
1787 buf[0] = 0x80; /* command "set data bits low byte" */
1788 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1789 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1790 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1791
1792 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1793 {
1794 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1795 return ERROR_JTAG_INIT_FAILED;
1796 }
1797
1798 nSRST = 0x40;
1799
1800 high_output = 0x00;
1801 high_direction = 0x0C;
1802
1803 /* initialize high port */
1804 buf[0] = 0x82; /* command "set data bits high byte" */
1805 buf[1] = high_output;
1806 buf[2] = high_direction;
1807 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1808
1809 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1810 {
1811 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1812 return ERROR_JTAG_INIT_FAILED;
1813 }
1814
1815 return ERROR_OK;
1816 }
1817
1818 void olimex_jtag_blink(void)
1819 {
1820 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1821 * ACBUS3 is bit 3 of the GPIOH port
1822 */
1823 if (high_output & 0x08)
1824 {
1825 /* set port pin high */
1826 high_output &= 0x07;
1827 }
1828 else
1829 {
1830 /* set port pin low */
1831 high_output |= 0x08;
1832 }
1833
1834 BUFFER_ADD = 0x82;
1835 BUFFER_ADD = high_output;
1836 BUFFER_ADD = high_direction;
1837 }
1838
1839 void turtle_jtag_blink(void)
1840 {
1841 /*
1842 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
1843 */
1844 if (high_output & 0x08)
1845 {
1846 high_output = 0x04;
1847 }
1848 else
1849 {
1850 high_output = 0x08;
1851 }
1852
1853 BUFFER_ADD = 0x82;
1854 BUFFER_ADD = high_output;
1855 BUFFER_ADD = high_direction;
1856 }
1857
1858
1859 int ft2232_quit(void)
1860 {
1861 #if BUILD_FT2232_FTD2XX == 1
1862 FT_STATUS status;
1863
1864 status = FT_Close(ftdih);
1865 #elif BUILD_FT2232_LIBFTDI == 1
1866 ftdi_disable_bitbang(&ftdic);
1867
1868 ftdi_usb_close(&ftdic);
1869
1870 ftdi_deinit(&ftdic);
1871 #endif
1872
1873 free(ft2232_buffer);
1874
1875 return ERROR_OK;
1876 }
1877
1878 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1879 {
1880 if (argc == 1)
1881 {
1882 ft2232_device_desc = strdup(args[0]);
1883 }
1884 else
1885 {
1886 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1887 }
1888
1889 return ERROR_OK;
1890 }
1891
1892 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1893 {
1894 if (argc == 1)
1895 {
1896 ft2232_serial = strdup(args[0]);
1897 }
1898 else
1899 {
1900 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
1901 }
1902
1903 return ERROR_OK;
1904 }
1905
1906 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1907 {
1908 if (argc == 0)
1909 return ERROR_OK;
1910
1911 ft2232_layout = malloc(strlen(args[0]) + 1);
1912 strcpy(ft2232_layout, args[0]);
1913
1914 return ERROR_OK;
1915 }
1916
1917 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1918 {
1919 int i;
1920
1921 if (argc > MAX_USB_IDS*2) {
1922 WARNING("ignoring extra IDs in ft2232_vid_pid "
1923 "(maximum is %d pairs)", MAX_USB_IDS);
1924 argc = MAX_USB_IDS*2;
1925 }
1926 if (argc < 2 || (argc & 1))
1927 {
1928 WARNING("incomplete ft2232_vid_pid configuration directive");
1929 if (argc < 2)
1930 return ERROR_OK;
1931 }
1932
1933 for (i = 0; i+1 < argc; i += 2) {
1934 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
1935 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
1936 }
1937 /*
1938 * Explicitly terminate, in case there are multiples instances of
1939 * ft2232_vid_pid.
1940 */
1941 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
1942
1943 return ERROR_OK;
1944 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)