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