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

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)