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