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