Zach Welch <zw@superlucidity.net> add --enable-verbose* options
[openocd.git] / src / jtag / ft2232.c
1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24
25 /* This code uses information contained in the MPSSE specification which was
26 * found here:
27 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
28 * Hereafter this is called the "MPSSE Spec".
29 */
30
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #if IS_CYGWIN == 1
37 #include "windows.h"
38 #endif
39
40 #include "replacements.h"
41
42 /* project specific includes */
43 #include "log.h"
44 #include "types.h"
45 #include "jtag.h"
46 #include "configuration.h"
47 #include "time_support.h"
48
49 /* system includes */
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53
54 /* FT2232 access library includes */
55 #if BUILD_FT2232_FTD2XX == 1
56 #include <ftd2xx.h>
57 #elif BUILD_FT2232_LIBFTDI == 1
58 #include <ftdi.h>
59 #endif
60
61 static int ft2232_execute_queue(void);
62
63 static int ft2232_speed(int speed);
64 static int ft2232_speed_div(int speed, int* khz);
65 static int ft2232_khz(int khz, int* jtag_speed);
66 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
67 static int ft2232_init(void);
68 static int ft2232_quit(void);
69
70 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
71 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
72 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
73 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
74 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
75
76
77 /**
78 * Function ft2232_stableclocks
79 * will send out \a num_cycles on the TCK line while the TAP(s)
80 * are in a stable state. Calling code must ensure that current state is
81 * stable, that verification is not done in here.
82 * @param num_cycles is the count of clocks cycles to send.
83 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
84 */
85 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
86
87
88 static char * ft2232_device_desc_A = NULL;
89 static char* ft2232_device_desc = NULL;
90 static char* ft2232_serial = NULL;
91 static char* ft2232_layout = NULL;
92 static unsigned char ft2232_latency = 2;
93
94 #define MAX_USB_IDS 8
95 /* vid = pid = 0 marks the end of the list */
96 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
97 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
98
99 typedef struct ft2232_layout_s
100 {
101 char* name;
102 int (*init)(void);
103 void (*reset)(int trst, int srst);
104 void (*blink)(void);
105 } ft2232_layout_t;
106
107 /* init procedures for supported layouts */
108 static int usbjtag_init(void);
109 static int jtagkey_init(void);
110 static int olimex_jtag_init(void);
111 static int flyswatter_init(void);
112 static int turtle_init(void);
113 static int comstick_init(void);
114 static int stm32stick_init(void);
115 static int axm0432_jtag_init(void);
116 static int sheevaplug_init(void);
117
118 /* reset procedures for supported layouts */
119 static void usbjtag_reset(int trst, int srst);
120 static void jtagkey_reset(int trst, int srst);
121 static void olimex_jtag_reset(int trst, int srst);
122 static void flyswatter_reset(int trst, int srst);
123 static void turtle_reset(int trst, int srst);
124 static void comstick_reset(int trst, int srst);
125 static void stm32stick_reset(int trst, int srst);
126 static void axm0432_jtag_reset(int trst, int srst);
127 static void sheevaplug_reset(int trst, int srst);
128
129 /* blink procedures for layouts that support a blinking led */
130 static void olimex_jtag_blink(void);
131 static void flyswatter_jtag_blink(void);
132 static void turtle_jtag_blink(void);
133
134 ft2232_layout_t ft2232_layouts[] =
135 {
136 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
137 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
138 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
139 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
140 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
141 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
142 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
143 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
144 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
145 { "comstick", comstick_init, comstick_reset, NULL },
146 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
147 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
148 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
149 { NULL, NULL, NULL, NULL },
150 };
151
152 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
153
154 static ft2232_layout_t* layout;
155 static u8 low_output = 0x0;
156 static u8 low_direction = 0x0;
157 static u8 high_output = 0x0;
158 static u8 high_direction = 0x0;
159
160 #if BUILD_FT2232_FTD2XX == 1
161 static FT_HANDLE ftdih = NULL;
162 #elif BUILD_FT2232_LIBFTDI == 1
163 static struct ftdi_context ftdic;
164 #endif
165
166
167 static jtag_command_t* first_unsent; /* next command that has to be sent */
168 static int require_send;
169
170 static u8* ft2232_buffer = NULL;
171 static int ft2232_buffer_size = 0;
172 static int ft2232_read_pointer = 0;
173 static int ft2232_expect_read = 0;
174
175 #define FT2232_BUFFER_SIZE 131072
176 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
177 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
178
179 jtag_interface_t ft2232_interface =
180 {
181 .name = "ft2232",
182 .execute_queue = ft2232_execute_queue,
183 .speed = ft2232_speed,
184 .speed_div = ft2232_speed_div,
185 .khz = ft2232_khz,
186 .register_commands = ft2232_register_commands,
187 .init = ft2232_init,
188 .quit = ft2232_quit,
189 };
190
191 static int ft2232_write(u8* buf, int size, u32* bytes_written)
192 {
193 #if BUILD_FT2232_FTD2XX == 1
194 FT_STATUS status;
195 DWORD dw_bytes_written;
196 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
197 {
198 *bytes_written = dw_bytes_written;
199 LOG_ERROR("FT_Write returned: %lu", status);
200 return ERROR_JTAG_DEVICE_ERROR;
201 }
202 else
203 {
204 *bytes_written = dw_bytes_written;
205 return ERROR_OK;
206 }
207 #elif BUILD_FT2232_LIBFTDI == 1
208 int retval;
209 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
210 {
211 *bytes_written = 0;
212 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
213 return ERROR_JTAG_DEVICE_ERROR;
214 }
215 else
216 {
217 *bytes_written = retval;
218 return ERROR_OK;
219 }
220 #endif
221 }
222
223
224 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
225 {
226 #if BUILD_FT2232_FTD2XX == 1
227 DWORD dw_bytes_read;
228 FT_STATUS status;
229 int timeout = 5;
230 *bytes_read = 0;
231
232 while ( (*bytes_read < size) && timeout-- )
233 {
234 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
235 *bytes_read, &dw_bytes_read) ) != FT_OK )
236 {
237 *bytes_read = 0;
238 LOG_ERROR("FT_Read returned: %lu", status);
239 return ERROR_JTAG_DEVICE_ERROR;
240 }
241 *bytes_read += dw_bytes_read;
242 }
243
244 #elif BUILD_FT2232_LIBFTDI == 1
245 int retval;
246 int timeout = 100;
247 *bytes_read = 0;
248
249 while ( (*bytes_read < size) && timeout-- )
250 {
251 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
252 {
253 *bytes_read = 0;
254 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
255 return ERROR_JTAG_DEVICE_ERROR;
256 }
257 *bytes_read += retval;
258 }
259
260 #endif
261
262 if (*bytes_read < size)
263 {
264 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
265 return ERROR_JTAG_DEVICE_ERROR;
266 }
267
268 return ERROR_OK;
269 }
270
271
272 static int ft2232_speed(int speed)
273 {
274 u8 buf[3];
275 int retval;
276 u32 bytes_written;
277
278 buf[0] = 0x86; /* command "set divisor" */
279 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
280 buf[2] = (speed >> 8) & 0xff; /* valueH */
281
282 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
283 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
284 {
285 LOG_ERROR("couldn't set FT2232 TCK speed");
286 return retval;
287 }
288
289 return ERROR_OK;
290 }
291
292
293 static int ft2232_speed_div(int speed, int* khz)
294 {
295 /* Take a look in the FT2232 manual,
296 * AN2232C-01 Command Processor for
297 * MPSSE and MCU Host Bus. Chapter 3.8 */
298
299 *khz = 6000 / (1 + speed);
300
301 return ERROR_OK;
302 }
303
304
305 static int ft2232_khz(int khz, int* jtag_speed)
306 {
307 if (khz==0)
308 {
309 LOG_ERROR("RCLK not supported");
310 return ERROR_FAIL;
311 }
312
313 /* Take a look in the FT2232 manual,
314 * AN2232C-01 Command Processor for
315 * MPSSE and MCU Host Bus. Chapter 3.8
316 *
317 * We will calc here with a multiplier
318 * of 10 for better rounding later. */
319
320 /* Calc speed, (6000 / khz) - 1 */
321 /* Use 65000 for better rounding */
322 *jtag_speed = (60000 / khz) - 10;
323
324 /* Add 0.9 for rounding */
325 *jtag_speed += 9;
326
327 /* Calc real speed */
328 *jtag_speed = *jtag_speed / 10;
329
330 /* Check if speed is greater than 0 */
331 if (*jtag_speed < 0)
332 {
333 *jtag_speed = 0;
334 }
335
336 /* Check max value */
337 if (*jtag_speed > 0xFFFF)
338 {
339 *jtag_speed = 0xFFFF;
340 }
341
342 return ERROR_OK;
343 }
344
345
346 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
347 {
348 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
349 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
350 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
351 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
352 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
353 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
354 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
355 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
356 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
357 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
358 return ERROR_OK;
359 }
360
361
362 void ft2232_end_state(tap_state_t state)
363 {
364 if (tap_is_state_stable(state))
365 tap_set_end_state(state);
366 else
367 {
368 LOG_ERROR("BUG: %i is not a valid end state", state);
369 exit(-1);
370 }
371 }
372
373
374 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
375 {
376 int num_bytes = (scan_size + 7) / 8;
377 int bits_left = scan_size;
378 int cur_byte = 0;
379
380 while (num_bytes-- > 1)
381 {
382 buffer[cur_byte] = BUFFER_READ;
383 cur_byte++;
384 bits_left -= 8;
385 }
386
387 buffer[cur_byte] = 0x0;
388
389 if (bits_left > 1)
390 {
391 buffer[cur_byte] = BUFFER_READ >> 1;
392 }
393
394 buffer[cur_byte] = ( buffer[cur_byte] | ( (BUFFER_READ & 0x02) << 6 ) ) >> (8 - bits_left);
395 }
396
397
398 static void ft2232_debug_dump_buffer(void)
399 {
400 int i;
401 char line[256];
402 char* line_p = line;
403
404 for (i = 0; i < ft2232_buffer_size; i++)
405 {
406 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
407 if (i % 16 == 15)
408 {
409 LOG_DEBUG("%s", line);
410 line_p = line;
411 }
412 }
413
414 if (line_p != line)
415 LOG_DEBUG("%s", line);
416 }
417
418
419 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
420 {
421 jtag_command_t* cmd;
422 u8* buffer;
423 int scan_size;
424 enum scan_type type;
425 int retval;
426 u32 bytes_written;
427 u32 bytes_read;
428
429 #ifdef _DEBUG_USB_IO_
430 struct timeval start, inter, inter2, end;
431 struct timeval d_inter, d_inter2, d_end;
432 #endif
433
434 #ifdef _DEBUG_USB_COMMS_
435 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
436 ft2232_debug_dump_buffer();
437 #endif
438
439 #ifdef _DEBUG_USB_IO_
440 gettimeofday(&start, NULL);
441 #endif
442
443 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
444 {
445 LOG_ERROR("couldn't write MPSSE commands to FT2232");
446 return retval;
447 }
448
449 #ifdef _DEBUG_USB_IO_
450 gettimeofday(&inter, NULL);
451 #endif
452
453 if (ft2232_expect_read)
454 {
455 int timeout = 100;
456 ft2232_buffer_size = 0;
457
458 #ifdef _DEBUG_USB_IO_
459 gettimeofday(&inter2, NULL);
460 #endif
461
462 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
463 {
464 LOG_ERROR("couldn't read from FT2232");
465 return retval;
466 }
467
468 #ifdef _DEBUG_USB_IO_
469 gettimeofday(&end, NULL);
470
471 timeval_subtract(&d_inter, &inter, &start);
472 timeval_subtract(&d_inter2, &inter2, &start);
473 timeval_subtract(&d_end, &end, &start);
474
475 LOG_INFO("inter: %i.%06i, inter2: %i.%06i end: %i.%06i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec,
476 d_inter2.tv_usec, d_end.tv_sec,
477 d_end.tv_usec);
478 #endif
479
480 ft2232_buffer_size = bytes_read;
481
482 if (ft2232_expect_read != ft2232_buffer_size)
483 {
484 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
485 ft2232_buffer_size,
486 100 - timeout);
487 ft2232_debug_dump_buffer();
488
489 exit(-1);
490 }
491
492 #ifdef _DEBUG_USB_COMMS_
493 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
494 ft2232_debug_dump_buffer();
495 #endif
496 }
497
498 ft2232_expect_read = 0;
499 ft2232_read_pointer = 0;
500
501 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
502 * that wasn't handled by a caller-provided error handler
503 */
504 retval = ERROR_OK;
505
506 cmd = first;
507 while (cmd != last)
508 {
509 switch (cmd->type)
510 {
511 case JTAG_SCAN:
512 type = jtag_scan_type(cmd->cmd.scan);
513 if (type != SCAN_OUT)
514 {
515 scan_size = jtag_scan_size(cmd->cmd.scan);
516 buffer = calloc(CEIL(scan_size, 8), 1);
517 ft2232_read_scan(type, buffer, scan_size);
518 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
519 retval = ERROR_JTAG_QUEUE_FAILED;
520 free(buffer);
521 }
522 break;
523
524 default:
525 break;
526 }
527
528 cmd = cmd->next;
529 }
530
531 ft2232_buffer_size = 0;
532
533 return retval;
534 }
535
536
537 static void ft2232_add_pathmove(pathmove_command_t* cmd)
538 {
539 int num_states = cmd->num_states;
540 int state_count = 0;
541
542 while (num_states)
543 {
544 u8 tms_byte = 0; /* zero this on each MPSSE batch */
545
546 int bit_count = 0;
547
548 int num_states_batch = num_states > 7 ? 7 : num_states;
549
550 /* command "Clock Data to TMS/CS Pin (no Read)" */
551 BUFFER_ADD = 0x4b;
552
553 /* number of states remaining */
554 BUFFER_ADD = num_states_batch - 1;
555
556 while (num_states_batch--)
557 {
558 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
559 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
560 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
561 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
562 else
563 {
564 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
565 tap_get_state() ), tap_state_name(cmd->path[state_count]) );
566 exit(-1);
567 }
568
569 tap_set_state(cmd->path[state_count]);
570 state_count++;
571 num_states--;
572 }
573
574 BUFFER_ADD = tms_byte;
575 }
576
577 tap_set_end_state(tap_get_state());
578 }
579
580
581 void ft2232_add_scan(int ir_scan, enum scan_type type, u8* buffer, int scan_size)
582 {
583 int num_bytes = (scan_size + 7) / 8;
584 int bits_left = scan_size;
585 int cur_byte = 0;
586 int last_bit;
587
588 if ( !( ( !ir_scan && (tap_get_state() == TAP_DRSHIFT) )
589 || ( ir_scan && (tap_get_state() == TAP_IRSHIFT) ) ) )
590 {
591 /* command "Clock Data to TMS/CS Pin (no Read)" */
592 BUFFER_ADD = 0x4b;
593
594 BUFFER_ADD = 0x6; /* scan 7 bits */
595
596 /* TMS data bits */
597 if (ir_scan)
598 {
599 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IRSHIFT);
600 tap_set_state(TAP_IRSHIFT);
601 }
602 else
603 {
604 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
605 tap_set_state(TAP_DRSHIFT);
606 }
607 /* LOG_DEBUG("added TMS scan (no read)"); */
608 }
609
610 /* add command for complete bytes */
611 while (num_bytes > 1)
612 {
613 int thisrun_bytes;
614 if (type == SCAN_IO)
615 {
616 /* Clock Data Bytes In and Out LSB First */
617 BUFFER_ADD = 0x39;
618 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
619 }
620 else if (type == SCAN_OUT)
621 {
622 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
623 BUFFER_ADD = 0x19;
624 /* LOG_DEBUG("added TDI bytes (o)"); */
625 }
626 else if (type == SCAN_IN)
627 {
628 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
629 BUFFER_ADD = 0x28;
630 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
631 }
632
633 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
634 num_bytes -= thisrun_bytes;
635 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
636 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
637
638 if (type != SCAN_IN)
639 {
640 /* add complete bytes */
641 while (thisrun_bytes-- > 0)
642 {
643 BUFFER_ADD = buffer[cur_byte];
644 cur_byte++;
645 bits_left -= 8;
646 }
647 }
648 else /* (type == SCAN_IN) */
649 {
650 bits_left -= 8 * (thisrun_bytes);
651 }
652 }
653
654 /* the most signifcant bit is scanned during TAP movement */
655 if (type != SCAN_IN)
656 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
657 else
658 last_bit = 0;
659
660 /* process remaining bits but the last one */
661 if (bits_left > 1)
662 {
663 if (type == SCAN_IO)
664 {
665 /* Clock Data Bits In and Out LSB First */
666 BUFFER_ADD = 0x3b;
667 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
668 }
669 else if (type == SCAN_OUT)
670 {
671 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
672 BUFFER_ADD = 0x1b;
673 /* LOG_DEBUG("added TDI bits (o)"); */
674 }
675 else if (type == SCAN_IN)
676 {
677 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
678 BUFFER_ADD = 0x2a;
679 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
680 }
681 BUFFER_ADD = bits_left - 2;
682 if (type != SCAN_IN)
683 BUFFER_ADD = buffer[cur_byte];
684 }
685
686 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
687 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
688 {
689 if (type == SCAN_IO)
690 {
691 /* Clock Data Bits In and Out LSB First */
692 BUFFER_ADD = 0x3b;
693 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
694 }
695 else if (type == SCAN_OUT)
696 {
697 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
698 BUFFER_ADD = 0x1b;
699 /* LOG_DEBUG("added TDI bits (o)"); */
700 }
701 else if (type == SCAN_IN)
702 {
703 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
704 BUFFER_ADD = 0x2a;
705 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
706 }
707 BUFFER_ADD = 0x0;
708 BUFFER_ADD = last_bit;
709 }
710 else
711 {
712 /* move from Shift-IR/DR to end state */
713 if (type != SCAN_OUT)
714 {
715 /* Clock Data to TMS/CS Pin with Read */
716 BUFFER_ADD = 0x6b;
717 /* LOG_DEBUG("added TMS scan (read)"); */
718 }
719 else
720 {
721 /* Clock Data to TMS/CS Pin (no Read) */
722 BUFFER_ADD = 0x4b;
723 /* LOG_DEBUG("added TMS scan (no read)"); */
724 }
725 BUFFER_ADD = 0x6; /* scan 7 bits */
726
727 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
728 tap_set_state( tap_get_end_state() );
729 }
730 }
731
732
733 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
734 {
735 int num_bytes = (scan_size + 7) / 8;
736 int bits_left = scan_size;
737 int cur_byte = 0;
738 int last_bit;
739 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
740 u8* receive_pointer = receive_buffer;
741 u32 bytes_written;
742 u32 bytes_read;
743 int retval;
744 int thisrun_read = 0;
745
746 if (cmd->ir_scan)
747 {
748 LOG_ERROR("BUG: large IR scans are not supported");
749 exit(-1);
750 }
751
752 if (tap_get_state() != TAP_DRSHIFT)
753 {
754 /* command "Clock Data to TMS/CS Pin (no Read)" */
755 BUFFER_ADD = 0x4b;
756
757 BUFFER_ADD = 0x6; /* scan 7 bits */
758
759 /* TMS data bits */
760 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
761 tap_set_state(TAP_DRSHIFT);
762 }
763
764 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
765 {
766 LOG_ERROR("couldn't write MPSSE commands to FT2232");
767 exit(-1);
768 }
769 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
770 ft2232_buffer_size = 0;
771
772 /* add command for complete bytes */
773 while (num_bytes > 1)
774 {
775 int thisrun_bytes;
776
777 if (type == SCAN_IO)
778 {
779 /* Clock Data Bytes In and Out LSB First */
780 BUFFER_ADD = 0x39;
781 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
782 }
783 else if (type == SCAN_OUT)
784 {
785 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
786 BUFFER_ADD = 0x19;
787 /* LOG_DEBUG("added TDI bytes (o)"); */
788 }
789 else if (type == SCAN_IN)
790 {
791 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
792 BUFFER_ADD = 0x28;
793 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
794 }
795
796 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
797 thisrun_read = thisrun_bytes;
798 num_bytes -= thisrun_bytes;
799 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
800 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
801
802 if (type != SCAN_IN)
803 {
804 /* add complete bytes */
805 while (thisrun_bytes-- > 0)
806 {
807 BUFFER_ADD = buffer[cur_byte];
808 cur_byte++;
809 bits_left -= 8;
810 }
811 }
812 else /* (type == SCAN_IN) */
813 {
814 bits_left -= 8 * (thisrun_bytes);
815 }
816
817 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
818 {
819 LOG_ERROR("couldn't write MPSSE commands to FT2232");
820 exit(-1);
821 }
822 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
823 ft2232_buffer_size = 0;
824
825 if (type != SCAN_OUT)
826 {
827 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
828 {
829 LOG_ERROR("couldn't read from FT2232");
830 exit(-1);
831 }
832 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
833 receive_pointer += bytes_read;
834 }
835 }
836
837 thisrun_read = 0;
838
839 /* the most signifcant bit is scanned during TAP movement */
840 if (type != SCAN_IN)
841 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
842 else
843 last_bit = 0;
844
845 /* process remaining bits but the last one */
846 if (bits_left > 1)
847 {
848 if (type == SCAN_IO)
849 {
850 /* Clock Data Bits In and Out LSB First */
851 BUFFER_ADD = 0x3b;
852 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
853 }
854 else if (type == SCAN_OUT)
855 {
856 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
857 BUFFER_ADD = 0x1b;
858 /* LOG_DEBUG("added TDI bits (o)"); */
859 }
860 else if (type == SCAN_IN)
861 {
862 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
863 BUFFER_ADD = 0x2a;
864 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
865 }
866 BUFFER_ADD = bits_left - 2;
867 if (type != SCAN_IN)
868 BUFFER_ADD = buffer[cur_byte];
869
870 if (type != SCAN_OUT)
871 thisrun_read += 2;
872 }
873
874 if (tap_get_end_state() == TAP_DRSHIFT)
875 {
876 if (type == SCAN_IO)
877 {
878 /* Clock Data Bits In and Out LSB First */
879 BUFFER_ADD = 0x3b;
880 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
881 }
882 else if (type == SCAN_OUT)
883 {
884 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
885 BUFFER_ADD = 0x1b;
886 /* LOG_DEBUG("added TDI bits (o)"); */
887 }
888 else if (type == SCAN_IN)
889 {
890 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
891 BUFFER_ADD = 0x2a;
892 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
893 }
894 BUFFER_ADD = 0x0;
895 BUFFER_ADD = last_bit;
896 }
897 else
898 {
899 /* move from Shift-IR/DR to end state */
900 if (type != SCAN_OUT)
901 {
902 /* Clock Data to TMS/CS Pin with Read */
903 BUFFER_ADD = 0x6b;
904 /* LOG_DEBUG("added TMS scan (read)"); */
905 }
906 else
907 {
908 /* Clock Data to TMS/CS Pin (no Read) */
909 BUFFER_ADD = 0x4b;
910 /* LOG_DEBUG("added TMS scan (no read)"); */
911 }
912 BUFFER_ADD = 0x6;
913 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
914 tap_set_state( tap_get_end_state() );
915 }
916
917 if (type != SCAN_OUT)
918 thisrun_read += 1;
919
920 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
921 {
922 LOG_ERROR("couldn't write MPSSE commands to FT2232");
923 exit(-1);
924 }
925 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
926 ft2232_buffer_size = 0;
927
928 if (type != SCAN_OUT)
929 {
930 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
931 {
932 LOG_ERROR("couldn't read from FT2232");
933 exit(-1);
934 }
935 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
936 receive_pointer += bytes_read;
937 }
938
939 return ERROR_OK;
940 }
941
942
943 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
944 {
945 int predicted_size = 3;
946 int num_bytes = (scan_size - 1) / 8;
947
948 if (tap_get_state() != TAP_DRSHIFT)
949 predicted_size += 3;
950
951 if (type == SCAN_IN) /* only from device to host */
952 {
953 /* complete bytes */
954 predicted_size += CEIL(num_bytes, 65536) * 3;
955 /* remaining bits - 1 (up to 7) */
956 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
957 }
958 else /* host to device, or bidirectional */
959 {
960 /* complete bytes */
961 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
962 /* remaining bits -1 (up to 7) */
963 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
964 }
965
966 return predicted_size;
967 }
968
969
970 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
971 {
972 int predicted_size = 0;
973
974 if (type != SCAN_OUT)
975 {
976 /* complete bytes */
977 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
978
979 /* remaining bits - 1 */
980 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
981
982 /* last bit (from TMS scan) */
983 predicted_size += 1;
984 }
985
986 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
987
988 return predicted_size;
989 }
990
991
992 static void usbjtag_reset(int trst, int srst)
993 {
994 if (trst == 1)
995 {
996 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
997 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
998 else
999 low_output &= ~nTRST; /* switch output low */
1000 }
1001 else if (trst == 0)
1002 {
1003 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1004 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1005 else
1006 low_output |= nTRST; /* switch output high */
1007 }
1008
1009 if (srst == 1)
1010 {
1011 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1012 low_output &= ~nSRST; /* switch output low */
1013 else
1014 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1015 }
1016 else if (srst == 0)
1017 {
1018 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1019 low_output |= nSRST; /* switch output high */
1020 else
1021 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1022 }
1023
1024 /* command "set data bits low byte" */
1025 BUFFER_ADD = 0x80;
1026 BUFFER_ADD = low_output;
1027 BUFFER_ADD = low_direction;
1028 }
1029
1030
1031 static void jtagkey_reset(int trst, int srst)
1032 {
1033 if (trst == 1)
1034 {
1035 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1036 high_output &= ~nTRSTnOE;
1037 else
1038 high_output &= ~nTRST;
1039 }
1040 else if (trst == 0)
1041 {
1042 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1043 high_output |= nTRSTnOE;
1044 else
1045 high_output |= nTRST;
1046 }
1047
1048 if (srst == 1)
1049 {
1050 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1051 high_output &= ~nSRST;
1052 else
1053 high_output &= ~nSRSTnOE;
1054 }
1055 else if (srst == 0)
1056 {
1057 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1058 high_output |= nSRST;
1059 else
1060 high_output |= nSRSTnOE;
1061 }
1062
1063 /* command "set data bits high byte" */
1064 BUFFER_ADD = 0x82;
1065 BUFFER_ADD = high_output;
1066 BUFFER_ADD = high_direction;
1067 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1068 high_direction);
1069 }
1070
1071
1072 static void olimex_jtag_reset(int trst, int srst)
1073 {
1074 if (trst == 1)
1075 {
1076 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1077 high_output &= ~nTRSTnOE;
1078 else
1079 high_output &= ~nTRST;
1080 }
1081 else if (trst == 0)
1082 {
1083 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1084 high_output |= nTRSTnOE;
1085 else
1086 high_output |= nTRST;
1087 }
1088
1089 if (srst == 1)
1090 {
1091 high_output |= nSRST;
1092 }
1093 else if (srst == 0)
1094 {
1095 high_output &= ~nSRST;
1096 }
1097
1098 /* command "set data bits high byte" */
1099 BUFFER_ADD = 0x82;
1100 BUFFER_ADD = high_output;
1101 BUFFER_ADD = high_direction;
1102 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1103 high_direction);
1104 }
1105
1106
1107 static void axm0432_jtag_reset(int trst, int srst)
1108 {
1109 if (trst == 1)
1110 {
1111 tap_set_state(TAP_RESET);
1112 high_output &= ~nTRST;
1113 }
1114 else if (trst == 0)
1115 {
1116 high_output |= nTRST;
1117 }
1118
1119 if (srst == 1)
1120 {
1121 high_output &= ~nSRST;
1122 }
1123 else if (srst == 0)
1124 {
1125 high_output |= nSRST;
1126 }
1127
1128 /* command "set data bits low byte" */
1129 BUFFER_ADD = 0x82;
1130 BUFFER_ADD = high_output;
1131 BUFFER_ADD = high_direction;
1132 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1133 high_direction);
1134 }
1135
1136
1137 static void flyswatter_reset(int trst, int srst)
1138 {
1139 if (trst == 1)
1140 {
1141 low_output &= ~nTRST;
1142 }
1143 else if (trst == 0)
1144 {
1145 low_output |= nTRST;
1146 }
1147
1148 if (srst == 1)
1149 {
1150 low_output |= nSRST;
1151 }
1152 else if (srst == 0)
1153 {
1154 low_output &= ~nSRST;
1155 }
1156
1157 /* command "set data bits low byte" */
1158 BUFFER_ADD = 0x80;
1159 BUFFER_ADD = low_output;
1160 BUFFER_ADD = low_direction;
1161 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1162 }
1163
1164
1165 static void turtle_reset(int trst, int srst)
1166 {
1167 trst = trst;
1168
1169 if (srst == 1)
1170 {
1171 low_output |= nSRST;
1172 }
1173 else if (srst == 0)
1174 {
1175 low_output &= ~nSRST;
1176 }
1177
1178 /* command "set data bits low byte" */
1179 BUFFER_ADD = 0x80;
1180 BUFFER_ADD = low_output;
1181 BUFFER_ADD = low_direction;
1182 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1183 }
1184
1185
1186 static void comstick_reset(int trst, int srst)
1187 {
1188 if (trst == 1)
1189 {
1190 high_output &= ~nTRST;
1191 }
1192 else if (trst == 0)
1193 {
1194 high_output |= nTRST;
1195 }
1196
1197 if (srst == 1)
1198 {
1199 high_output &= ~nSRST;
1200 }
1201 else if (srst == 0)
1202 {
1203 high_output |= nSRST;
1204 }
1205
1206 /* command "set data bits high byte" */
1207 BUFFER_ADD = 0x82;
1208 BUFFER_ADD = high_output;
1209 BUFFER_ADD = high_direction;
1210 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1211 high_direction);
1212 }
1213
1214
1215 static void stm32stick_reset(int trst, int srst)
1216 {
1217 if (trst == 1)
1218 {
1219 high_output &= ~nTRST;
1220 }
1221 else if (trst == 0)
1222 {
1223 high_output |= nTRST;
1224 }
1225
1226 if (srst == 1)
1227 {
1228 low_output &= ~nSRST;
1229 }
1230 else if (srst == 0)
1231 {
1232 low_output |= nSRST;
1233 }
1234
1235 /* command "set data bits low byte" */
1236 BUFFER_ADD = 0x80;
1237 BUFFER_ADD = low_output;
1238 BUFFER_ADD = low_direction;
1239
1240 /* command "set data bits high byte" */
1241 BUFFER_ADD = 0x82;
1242 BUFFER_ADD = high_output;
1243 BUFFER_ADD = high_direction;
1244 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1245 high_direction);
1246 }
1247
1248
1249
1250 static void sheevaplug_reset(int trst, int srst)
1251 {
1252 if (trst == 1)
1253 high_output &= ~nTRST;
1254 else if (trst == 0)
1255 high_output |= nTRST;
1256
1257 if (srst == 1)
1258 high_output &= ~nSRSTnOE;
1259 else if (srst == 0)
1260 high_output |= nSRSTnOE;
1261
1262 /* command "set data bits high byte" */
1263 BUFFER_ADD = 0x82;
1264 BUFFER_ADD = high_output;
1265 BUFFER_ADD = high_direction;
1266 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1267 }
1268
1269 static int ft2232_execute_queue()
1270 {
1271 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1272 u8* buffer;
1273 int scan_size; /* size of IR or DR scan */
1274 enum scan_type type;
1275 int i;
1276 int predicted_size = 0;
1277 int retval;
1278
1279 first_unsent = cmd; /* next command that has to be sent */
1280 require_send = 0;
1281
1282 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1283 * that wasn't handled by a caller-provided error handler
1284 */
1285 retval = ERROR_OK;
1286
1287 ft2232_buffer_size = 0;
1288 ft2232_expect_read = 0;
1289
1290 /* blink, if the current layout has that feature */
1291 if (layout->blink)
1292 layout->blink();
1293
1294 while (cmd)
1295 {
1296 switch (cmd->type)
1297 {
1298 case JTAG_END_STATE:
1299 if (cmd->cmd.end_state->end_state != TAP_INVALID)
1300 ft2232_end_state(cmd->cmd.end_state->end_state);
1301 break;
1302
1303 case JTAG_RESET:
1304 /* only send the maximum buffer size that FT2232C can handle */
1305 predicted_size = 3;
1306 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1307 {
1308 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1309 retval = ERROR_JTAG_QUEUE_FAILED;
1310 require_send = 0;
1311 first_unsent = cmd;
1312 }
1313
1314 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1315 {
1316 tap_set_state(TAP_RESET);
1317 }
1318 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1319 require_send = 1;
1320
1321 #ifdef _DEBUG_JTAG_IO_
1322 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1323 #endif
1324 break;
1325
1326 case JTAG_RUNTEST:
1327 /* only send the maximum buffer size that FT2232C can handle */
1328 predicted_size = 0;
1329 if (tap_get_state() != TAP_IDLE)
1330 predicted_size += 3;
1331 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1332 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1333 predicted_size += 3;
1334 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1335 predicted_size += 3;
1336 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1337 {
1338 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1339 retval = ERROR_JTAG_QUEUE_FAILED;
1340 require_send = 0;
1341 first_unsent = cmd;
1342 }
1343 if (tap_get_state() != TAP_IDLE)
1344 {
1345 /* command "Clock Data to TMS/CS Pin (no Read)" */
1346 BUFFER_ADD = 0x4b;
1347 BUFFER_ADD = 0x6; /* scan 7 bits */
1348
1349 /* TMS data bits */
1350 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IDLE);
1351 tap_set_state(TAP_IDLE);
1352 require_send = 1;
1353 }
1354 i = cmd->cmd.runtest->num_cycles;
1355 while (i > 0)
1356 {
1357 /* command "Clock Data to TMS/CS Pin (no Read)" */
1358 BUFFER_ADD = 0x4b;
1359
1360 /* scan 7 bits */
1361 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1362
1363 /* TMS data bits */
1364 BUFFER_ADD = 0x0;
1365 tap_set_state(TAP_IDLE);
1366 i -= (i > 7) ? 7 : i;
1367 /* LOG_DEBUG("added TMS scan (no read)"); */
1368 }
1369
1370 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1371 ft2232_end_state(cmd->cmd.runtest->end_state);
1372
1373 if ( tap_get_state() != tap_get_end_state() )
1374 {
1375 /* command "Clock Data to TMS/CS Pin (no Read)" */
1376 BUFFER_ADD = 0x4b;
1377 /* scan 7 bit */
1378 BUFFER_ADD = 0x6;
1379 /* TMS data bits */
1380 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1381 tap_set_state( tap_get_end_state() );
1382 /* LOG_DEBUG("added TMS scan (no read)"); */
1383 }
1384 require_send = 1;
1385 #ifdef _DEBUG_JTAG_IO_
1386 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1387 #endif
1388 break;
1389
1390 case JTAG_STATEMOVE:
1391 /* only send the maximum buffer size that FT2232C can handle */
1392 predicted_size = 3;
1393 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1394 {
1395 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1396 retval = ERROR_JTAG_QUEUE_FAILED;
1397 require_send = 0;
1398 first_unsent = cmd;
1399 }
1400 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1401 ft2232_end_state(cmd->cmd.statemove->end_state);
1402
1403 /* command "Clock Data to TMS/CS Pin (no Read)" */
1404 BUFFER_ADD = 0x4b;
1405
1406 BUFFER_ADD = 0x6; /* scan 7 bits */
1407
1408 /* TMS data bits */
1409 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1410 /* LOG_DEBUG("added TMS scan (no read)"); */
1411 tap_set_state( tap_get_end_state() );
1412 require_send = 1;
1413 #ifdef _DEBUG_JTAG_IO_
1414 LOG_DEBUG( "statemove: %s", tap_state_name( tap_get_end_state() ) );
1415 #endif
1416 break;
1417
1418 case JTAG_PATHMOVE:
1419 /* only send the maximum buffer size that FT2232C can handle */
1420 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1421 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1422 {
1423 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1424 retval = ERROR_JTAG_QUEUE_FAILED;
1425 require_send = 0;
1426 first_unsent = cmd;
1427 }
1428 ft2232_add_pathmove(cmd->cmd.pathmove);
1429 require_send = 1;
1430 #ifdef _DEBUG_JTAG_IO_
1431 LOG_DEBUG( "pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1432 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]) );
1433 #endif
1434 break;
1435
1436 case JTAG_SCAN:
1437 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1438 type = jtag_scan_type(cmd->cmd.scan);
1439 predicted_size = ft2232_predict_scan_out(scan_size, type);
1440 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1441 {
1442 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1443 /* unsent commands before this */
1444 if (first_unsent != cmd)
1445 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1446 retval = ERROR_JTAG_QUEUE_FAILED;
1447
1448 /* current command */
1449 if (cmd->cmd.scan->end_state != TAP_INVALID)
1450 ft2232_end_state(cmd->cmd.scan->end_state);
1451 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1452 require_send = 0;
1453 first_unsent = cmd->next;
1454 if (buffer)
1455 free(buffer);
1456 break;
1457 }
1458 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1459 {
1460 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1461 first_unsent,
1462 cmd);
1463 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1464 retval = ERROR_JTAG_QUEUE_FAILED;
1465 require_send = 0;
1466 first_unsent = cmd;
1467 }
1468 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1469 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1470 if (cmd->cmd.scan->end_state != TAP_INVALID)
1471 ft2232_end_state(cmd->cmd.scan->end_state);
1472 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1473 require_send = 1;
1474 if (buffer)
1475 free(buffer);
1476 #ifdef _DEBUG_JTAG_IO_
1477 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1478 tap_state_name( tap_get_end_state() ) );
1479 #endif
1480 break;
1481
1482 case JTAG_SLEEP:
1483 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1484 retval = ERROR_JTAG_QUEUE_FAILED;
1485 first_unsent = cmd->next;
1486 jtag_sleep(cmd->cmd.sleep->us);
1487 #ifdef _DEBUG_JTAG_IO_
1488 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1489 #endif
1490 break;
1491
1492 case JTAG_STABLECLOCKS:
1493
1494 /* this is only allowed while in a stable state. A check for a stable
1495 * state was done in jtag_add_clocks()
1496 */
1497 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1498 retval = ERROR_JTAG_QUEUE_FAILED;
1499 #ifdef _DEBUG_JTAG_IO_
1500 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1501 #endif
1502 break;
1503
1504 default:
1505 LOG_ERROR("BUG: unknown JTAG command type encountered");
1506 exit(-1);
1507 }
1508
1509 cmd = cmd->next;
1510 }
1511
1512 if (require_send > 0)
1513 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1514 retval = ERROR_JTAG_QUEUE_FAILED;
1515
1516 return retval;
1517 }
1518
1519
1520 #if BUILD_FT2232_FTD2XX == 1
1521 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1522 {
1523 FT_STATUS status;
1524 DWORD openex_flags = 0;
1525 char* openex_string = NULL;
1526 u8 latency_timer;
1527
1528 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1529
1530 #if IS_WIN32 == 0
1531 /* Add non-standard Vid/Pid to the linux driver */
1532 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1533 {
1534 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1535 }
1536 #endif
1537
1538 if (ft2232_device_desc && ft2232_serial)
1539 {
1540 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1541 ft2232_device_desc = NULL;
1542 }
1543
1544 if (ft2232_device_desc)
1545 {
1546 openex_string = ft2232_device_desc;
1547 openex_flags = FT_OPEN_BY_DESCRIPTION;
1548 }
1549 else if (ft2232_serial)
1550 {
1551 openex_string = ft2232_serial;
1552 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1553 }
1554 else
1555 {
1556 LOG_ERROR("neither device description nor serial number specified");
1557 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1558
1559 return ERROR_JTAG_INIT_FAILED;
1560 }
1561
1562 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1563 if( status != FT_OK ){
1564 // under Win32, the FTD2XX driver appends an "A" to the end
1565 // of the description, if we tried by the desc, then
1566 // try by the alternate "A" description.
1567 if( openex_string == ft2232_device_desc ){
1568 // Try the alternate method.
1569 openex_string = ft2232_device_desc_A;
1570 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1571 if( status == FT_OK ){
1572 // yea, the "alternate" method worked!
1573 } else {
1574 // drat, give the user a meaningfull message.
1575 // telling the use we tried *BOTH* methods.
1576 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1577 ft2232_device_desc,
1578 ft2232_device_desc_A );
1579 }
1580 }
1581 }
1582
1583 if ( status != FT_OK )
1584 {
1585 DWORD num_devices;
1586
1587 if (more)
1588 {
1589 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1590 *try_more = 1;
1591 return ERROR_JTAG_INIT_FAILED;
1592 }
1593 LOG_ERROR("unable to open ftdi device: %lu", status);
1594 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1595 if (status == FT_OK)
1596 {
1597 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1598 u32 i;
1599
1600 for (i = 0; i < num_devices; i++)
1601 desc_array[i] = malloc(64);
1602
1603 desc_array[num_devices] = NULL;
1604
1605 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1606
1607 if (status == FT_OK)
1608 {
1609 LOG_ERROR("ListDevices: %lu\n", num_devices);
1610 for (i = 0; i < num_devices; i++)
1611 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1612 }
1613
1614 for (i = 0; i < num_devices; i++)
1615 free(desc_array[i]);
1616
1617 free(desc_array);
1618 }
1619 else
1620 {
1621 LOG_ERROR("ListDevices: NONE\n");
1622 }
1623 return ERROR_JTAG_INIT_FAILED;
1624 }
1625
1626 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1627 {
1628 LOG_ERROR("unable to set latency timer: %lu", status);
1629 return ERROR_JTAG_INIT_FAILED;
1630 }
1631
1632 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1633 {
1634 LOG_ERROR("unable to get latency timer: %lu", status);
1635 return ERROR_JTAG_INIT_FAILED;
1636 }
1637 else
1638 {
1639 LOG_DEBUG("current latency timer: %i", latency_timer);
1640 }
1641
1642 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1643 {
1644 LOG_ERROR("unable to set timeouts: %lu", status);
1645 return ERROR_JTAG_INIT_FAILED;
1646 }
1647
1648 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1649 {
1650 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1651 return ERROR_JTAG_INIT_FAILED;
1652 }
1653
1654 return ERROR_OK;
1655 }
1656
1657
1658 static int ft2232_purge_ftd2xx(void)
1659 {
1660 FT_STATUS status;
1661
1662 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1663 {
1664 LOG_ERROR("error purging ftd2xx device: %lu", status);
1665 return ERROR_JTAG_INIT_FAILED;
1666 }
1667
1668 return ERROR_OK;
1669 }
1670
1671
1672 #endif /* BUILD_FT2232_FTD2XX == 1 */
1673
1674 #if BUILD_FT2232_LIBFTDI == 1
1675 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1676 {
1677 u8 latency_timer;
1678
1679 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1680 ft2232_layout, vid, pid);
1681
1682 if (ftdi_init(&ftdic) < 0)
1683 return ERROR_JTAG_INIT_FAILED;
1684
1685 /* context, vendor id, product id */
1686 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1687 ft2232_serial) < 0)
1688 {
1689 if (more)
1690 LOG_WARNING("unable to open ftdi device (trying more): %s",
1691 ftdic.error_str);
1692 else
1693 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1694 *try_more = 1;
1695 return ERROR_JTAG_INIT_FAILED;
1696 }
1697
1698 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1699 {
1700 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1701 return ERROR_JTAG_INIT_FAILED;
1702 }
1703
1704 if (ftdi_usb_reset(&ftdic) < 0)
1705 {
1706 LOG_ERROR("unable to reset ftdi device");
1707 return ERROR_JTAG_INIT_FAILED;
1708 }
1709
1710 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1711 {
1712 LOG_ERROR("unable to set latency timer");
1713 return ERROR_JTAG_INIT_FAILED;
1714 }
1715
1716 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1717 {
1718 LOG_ERROR("unable to get latency timer");
1719 return ERROR_JTAG_INIT_FAILED;
1720 }
1721 else
1722 {
1723 LOG_DEBUG("current latency timer: %i", latency_timer);
1724 }
1725
1726 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1727
1728 return ERROR_OK;
1729 }
1730
1731
1732 static int ft2232_purge_libftdi(void)
1733 {
1734 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1735 {
1736 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1737 return ERROR_JTAG_INIT_FAILED;
1738 }
1739
1740 return ERROR_OK;
1741 }
1742
1743
1744 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1745
1746 static int ft2232_init(void)
1747 {
1748 u8 buf[1];
1749 int retval;
1750 u32 bytes_written;
1751 ft2232_layout_t* cur_layout = ft2232_layouts;
1752 int i;
1753
1754 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1755 {
1756 ft2232_layout = "usbjtag";
1757 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1758 }
1759
1760 while (cur_layout->name)
1761 {
1762 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1763 {
1764 layout = cur_layout;
1765 break;
1766 }
1767 cur_layout++;
1768 }
1769
1770 if (!layout)
1771 {
1772 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1773 return ERROR_JTAG_INIT_FAILED;
1774 }
1775
1776 for (i = 0; 1; i++)
1777 {
1778 /*
1779 * "more indicates that there are more IDs to try, so we should
1780 * not print an error for an ID mismatch (but for anything
1781 * else, we should).
1782 *
1783 * try_more indicates that the error code returned indicates an
1784 * ID mismatch (and nothing else) and that we should proceeed
1785 * with the next ID pair.
1786 */
1787 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
1788 int try_more = 0;
1789
1790 #if BUILD_FT2232_FTD2XX == 1
1791 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1792 more, &try_more);
1793 #elif BUILD_FT2232_LIBFTDI == 1
1794 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1795 more, &try_more);
1796 #endif
1797 if (retval >= 0)
1798 break;
1799 if (!more || !try_more)
1800 return retval;
1801 }
1802
1803 ft2232_buffer_size = 0;
1804 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1805
1806 if (layout->init() != ERROR_OK)
1807 return ERROR_JTAG_INIT_FAILED;
1808
1809 ft2232_speed(jtag_speed);
1810
1811 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1812 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
1813 {
1814 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1815 return ERROR_JTAG_INIT_FAILED;
1816 }
1817
1818 #if BUILD_FT2232_FTD2XX == 1
1819 return ft2232_purge_ftd2xx();
1820 #elif BUILD_FT2232_LIBFTDI == 1
1821 return ft2232_purge_libftdi();
1822 #endif
1823
1824 return ERROR_OK;
1825 }
1826
1827
1828 static int usbjtag_init(void)
1829 {
1830 u8 buf[3];
1831 u32 bytes_written;
1832
1833 low_output = 0x08;
1834 low_direction = 0x0b;
1835
1836 if (strcmp(ft2232_layout, "usbjtag") == 0)
1837 {
1838 nTRST = 0x10;
1839 nTRSTnOE = 0x10;
1840 nSRST = 0x40;
1841 nSRSTnOE = 0x40;
1842 }
1843 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1844 {
1845 nTRST = 0x10;
1846 nTRSTnOE = 0x10;
1847 nSRST = 0x20;
1848 nSRSTnOE = 0x20;
1849 }
1850 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1851 {
1852 nTRST = 0x0;
1853 nTRSTnOE = 0x00;
1854 nSRST = 0x20;
1855 nSRSTnOE = 0x20;
1856 low_output = 0x88;
1857 low_direction = 0x8b;
1858 }
1859 else
1860 {
1861 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1862 return ERROR_JTAG_INIT_FAILED;
1863 }
1864
1865 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1866 {
1867 low_direction &= ~nTRSTnOE; /* nTRST input */
1868 low_output &= ~nTRST; /* nTRST = 0 */
1869 }
1870 else
1871 {
1872 low_direction |= nTRSTnOE; /* nTRST output */
1873 low_output |= nTRST; /* nTRST = 1 */
1874 }
1875
1876 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1877 {
1878 low_direction |= nSRSTnOE; /* nSRST output */
1879 low_output |= nSRST; /* nSRST = 1 */
1880 }
1881 else
1882 {
1883 low_direction &= ~nSRSTnOE; /* nSRST input */
1884 low_output &= ~nSRST; /* nSRST = 0 */
1885 }
1886
1887 /* initialize low byte for jtag */
1888 buf[0] = 0x80; /* command "set data bits low byte" */
1889 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1890 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1891 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1892
1893 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1894 {
1895 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1896 return ERROR_JTAG_INIT_FAILED;
1897 }
1898
1899 return ERROR_OK;
1900 }
1901
1902
1903 static int axm0432_jtag_init(void)
1904 {
1905 u8 buf[3];
1906 u32 bytes_written;
1907
1908 low_output = 0x08;
1909 low_direction = 0x2b;
1910
1911 /* initialize low byte for jtag */
1912 buf[0] = 0x80; /* command "set data bits low byte" */
1913 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1914 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1915 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1916
1917 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1918 {
1919 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1920 return ERROR_JTAG_INIT_FAILED;
1921 }
1922
1923 if (strcmp(layout->name, "axm0432_jtag") == 0)
1924 {
1925 nTRST = 0x08;
1926 nTRSTnOE = 0x0; /* No output enable for TRST*/
1927 nSRST = 0x04;
1928 nSRSTnOE = 0x0; /* No output enable for SRST*/
1929 }
1930 else
1931 {
1932 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1933 exit(-1);
1934 }
1935
1936 high_output = 0x0;
1937 high_direction = 0x0c;
1938
1939 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1940 {
1941 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1942 }
1943 else
1944 {
1945 high_output |= nTRST;
1946 }
1947
1948 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1949 {
1950 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1951 }
1952 else
1953 {
1954 high_output |= nSRST;
1955 }
1956
1957 /* initialize high port */
1958 buf[0] = 0x82; /* command "set data bits high byte" */
1959 buf[1] = high_output; /* value */
1960 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1961 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1962
1963 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1964 {
1965 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1966 return ERROR_JTAG_INIT_FAILED;
1967 }
1968
1969 return ERROR_OK;
1970 }
1971
1972
1973 static int jtagkey_init(void)
1974 {
1975 u8 buf[3];
1976 u32 bytes_written;
1977
1978 low_output = 0x08;
1979 low_direction = 0x1b;
1980
1981 /* initialize low byte for jtag */
1982 buf[0] = 0x80; /* command "set data bits low byte" */
1983 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1984 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1985 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1986
1987 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1988 {
1989 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1990 return ERROR_JTAG_INIT_FAILED;
1991 }
1992
1993 if (strcmp(layout->name, "jtagkey") == 0)
1994 {
1995 nTRST = 0x01;
1996 nTRSTnOE = 0x4;
1997 nSRST = 0x02;
1998 nSRSTnOE = 0x08;
1999 }
2000 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2001 || (strcmp(layout->name, "oocdlink") == 0) )
2002 {
2003 nTRST = 0x02;
2004 nTRSTnOE = 0x1;
2005 nSRST = 0x08;
2006 nSRSTnOE = 0x04;
2007 }
2008 else
2009 {
2010 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2011 exit(-1);
2012 }
2013
2014 high_output = 0x0;
2015 high_direction = 0x0f;
2016
2017 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2018 {
2019 high_output |= nTRSTnOE;
2020 high_output &= ~nTRST;
2021 }
2022 else
2023 {
2024 high_output &= ~nTRSTnOE;
2025 high_output |= nTRST;
2026 }
2027
2028 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2029 {
2030 high_output &= ~nSRSTnOE;
2031 high_output |= nSRST;
2032 }
2033 else
2034 {
2035 high_output |= nSRSTnOE;
2036 high_output &= ~nSRST;
2037 }
2038
2039 /* initialize high port */
2040 buf[0] = 0x82; /* command "set data bits high byte" */
2041 buf[1] = high_output; /* value */
2042 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2043 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2044
2045 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2046 {
2047 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2048 return ERROR_JTAG_INIT_FAILED;
2049 }
2050
2051 return ERROR_OK;
2052 }
2053
2054
2055 static int olimex_jtag_init(void)
2056 {
2057 u8 buf[3];
2058 u32 bytes_written;
2059
2060 low_output = 0x08;
2061 low_direction = 0x1b;
2062
2063 /* initialize low byte for jtag */
2064 buf[0] = 0x80; /* command "set data bits low byte" */
2065 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2066 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2067 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2068
2069 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2070 {
2071 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2072 return ERROR_JTAG_INIT_FAILED;
2073 }
2074
2075 nTRST = 0x01;
2076 nTRSTnOE = 0x4;
2077 nSRST = 0x02;
2078 nSRSTnOE = 0x00; /* no output enable for nSRST */
2079
2080 high_output = 0x0;
2081 high_direction = 0x0f;
2082
2083 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2084 {
2085 high_output |= nTRSTnOE;
2086 high_output &= ~nTRST;
2087 }
2088 else
2089 {
2090 high_output &= ~nTRSTnOE;
2091 high_output |= nTRST;
2092 }
2093
2094 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2095 {
2096 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2097 }
2098 else
2099 {
2100 high_output &= ~nSRST;
2101 }
2102
2103 /* turn red LED on */
2104 high_output |= 0x08;
2105
2106 /* initialize high port */
2107 buf[0] = 0x82; /* command "set data bits high byte" */
2108 buf[1] = high_output; /* value */
2109 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2110 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2111
2112 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2113 {
2114 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2115 return ERROR_JTAG_INIT_FAILED;
2116 }
2117
2118 return ERROR_OK;
2119 }
2120
2121
2122 static int flyswatter_init(void)
2123 {
2124 u8 buf[3];
2125 u32 bytes_written;
2126
2127 low_output = 0x18;
2128 low_direction = 0xfb;
2129
2130 /* initialize low byte for jtag */
2131 buf[0] = 0x80; /* command "set data bits low byte" */
2132 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2133 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2134 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2135
2136 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2137 {
2138 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2139 return ERROR_JTAG_INIT_FAILED;
2140 }
2141
2142 nTRST = 0x10;
2143 nTRSTnOE = 0x0; /* not output enable for nTRST */
2144 nSRST = 0x20;
2145 nSRSTnOE = 0x00; /* no output enable for nSRST */
2146
2147 high_output = 0x00;
2148 high_direction = 0x0c;
2149
2150 /* turn red LED3 on, LED2 off */
2151 high_output |= 0x08;
2152
2153 /* initialize high port */
2154 buf[0] = 0x82; /* command "set data bits high byte" */
2155 buf[1] = high_output; /* value */
2156 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2157 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2158
2159 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2160 {
2161 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2162 return ERROR_JTAG_INIT_FAILED;
2163 }
2164
2165 return ERROR_OK;
2166 }
2167
2168
2169 static int turtle_init(void)
2170 {
2171 u8 buf[3];
2172 u32 bytes_written;
2173
2174 low_output = 0x08;
2175 low_direction = 0x5b;
2176
2177 /* initialize low byte for jtag */
2178 buf[0] = 0x80; /* command "set data bits low byte" */
2179 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2180 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2181 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2182
2183 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2184 {
2185 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2186 return ERROR_JTAG_INIT_FAILED;
2187 }
2188
2189 nSRST = 0x40;
2190
2191 high_output = 0x00;
2192 high_direction = 0x0C;
2193
2194 /* initialize high port */
2195 buf[0] = 0x82; /* command "set data bits high byte" */
2196 buf[1] = high_output;
2197 buf[2] = high_direction;
2198 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2199
2200 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2201 {
2202 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2203 return ERROR_JTAG_INIT_FAILED;
2204 }
2205
2206 return ERROR_OK;
2207 }
2208
2209
2210 static int comstick_init(void)
2211 {
2212 u8 buf[3];
2213 u32 bytes_written;
2214
2215 low_output = 0x08;
2216 low_direction = 0x0b;
2217
2218 /* initialize low byte for jtag */
2219 buf[0] = 0x80; /* command "set data bits low byte" */
2220 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2221 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2222 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2223
2224 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2225 {
2226 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2227 return ERROR_JTAG_INIT_FAILED;
2228 }
2229
2230 nTRST = 0x01;
2231 nTRSTnOE = 0x00; /* no output enable for nTRST */
2232 nSRST = 0x02;
2233 nSRSTnOE = 0x00; /* no output enable for nSRST */
2234
2235 high_output = 0x03;
2236 high_direction = 0x03;
2237
2238 /* initialize high port */
2239 buf[0] = 0x82; /* command "set data bits high byte" */
2240 buf[1] = high_output;
2241 buf[2] = high_direction;
2242 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2243
2244 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2245 {
2246 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2247 return ERROR_JTAG_INIT_FAILED;
2248 }
2249
2250 return ERROR_OK;
2251 }
2252
2253
2254 static int stm32stick_init(void)
2255 {
2256 u8 buf[3];
2257 u32 bytes_written;
2258
2259 low_output = 0x88;
2260 low_direction = 0x8b;
2261
2262 /* initialize low byte for jtag */
2263 buf[0] = 0x80; /* command "set data bits low byte" */
2264 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2265 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2266 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2267
2268 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2269 {
2270 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2271 return ERROR_JTAG_INIT_FAILED;
2272 }
2273
2274 nTRST = 0x01;
2275 nTRSTnOE = 0x00; /* no output enable for nTRST */
2276 nSRST = 0x80;
2277 nSRSTnOE = 0x00; /* no output enable for nSRST */
2278
2279 high_output = 0x01;
2280 high_direction = 0x03;
2281
2282 /* initialize high port */
2283 buf[0] = 0x82; /* command "set data bits high byte" */
2284 buf[1] = high_output;
2285 buf[2] = high_direction;
2286 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2287
2288 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2289 {
2290 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2291 return ERROR_JTAG_INIT_FAILED;
2292 }
2293
2294 return ERROR_OK;
2295 }
2296
2297
2298 static int sheevaplug_init(void)
2299 {
2300 u8 buf[3];
2301 u32 bytes_written;
2302
2303 low_output = 0x08;
2304 low_direction = 0x1b;
2305
2306 /* initialize low byte for jtag */
2307 buf[0] = 0x80; /* command "set data bits low byte" */
2308 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2309 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2310 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2311
2312 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2313 {
2314 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2315 return ERROR_JTAG_INIT_FAILED;
2316 }
2317
2318 nTRSTnOE = 0x1;
2319 nTRST = 0x02;
2320 nSRSTnOE = 0x4;
2321 nSRST = 0x08;
2322
2323 high_output = 0x0;
2324 high_direction = 0x0f;
2325
2326 /* nTRST is always push-pull */
2327 high_output &= ~nTRSTnOE;
2328 high_output |= nTRST;
2329
2330 /* nSRST is always open-drain */
2331 high_output |= nSRSTnOE;
2332 high_output &= ~nSRST;
2333
2334 /* initialize high port */
2335 buf[0] = 0x82; /* command "set data bits high byte" */
2336 buf[1] = high_output; /* value */
2337 buf[2] = high_direction; /* all outputs - xRST */
2338 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2339
2340 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2341 {
2342 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2343 return ERROR_JTAG_INIT_FAILED;
2344 }
2345
2346 return ERROR_OK;
2347 }
2348
2349 static void olimex_jtag_blink(void)
2350 {
2351 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2352 * ACBUS3 is bit 3 of the GPIOH port
2353 */
2354 if (high_output & 0x08)
2355 {
2356 /* set port pin high */
2357 high_output &= 0x07;
2358 }
2359 else
2360 {
2361 /* set port pin low */
2362 high_output |= 0x08;
2363 }
2364
2365 BUFFER_ADD = 0x82;
2366 BUFFER_ADD = high_output;
2367 BUFFER_ADD = high_direction;
2368 }
2369
2370
2371 static void flyswatter_jtag_blink(void)
2372 {
2373 /*
2374 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2375 */
2376 high_output ^= 0x0c;
2377
2378 BUFFER_ADD = 0x82;
2379 BUFFER_ADD = high_output;
2380 BUFFER_ADD = high_direction;
2381 }
2382
2383
2384 static void turtle_jtag_blink(void)
2385 {
2386 /*
2387 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2388 */
2389 if (high_output & 0x08)
2390 {
2391 high_output = 0x04;
2392 }
2393 else
2394 {
2395 high_output = 0x08;
2396 }
2397
2398 BUFFER_ADD = 0x82;
2399 BUFFER_ADD = high_output;
2400 BUFFER_ADD = high_direction;
2401 }
2402
2403
2404 static int ft2232_quit(void)
2405 {
2406 #if BUILD_FT2232_FTD2XX == 1
2407 FT_STATUS status;
2408
2409 status = FT_Close(ftdih);
2410 #elif BUILD_FT2232_LIBFTDI == 1
2411 ftdi_disable_bitbang(&ftdic);
2412
2413 ftdi_usb_close(&ftdic);
2414
2415 ftdi_deinit(&ftdic);
2416 #endif
2417
2418 free(ft2232_buffer);
2419 ft2232_buffer = NULL;
2420
2421 return ERROR_OK;
2422 }
2423
2424
2425 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2426 {
2427 char *cp;
2428 char buf[200];
2429 if (argc == 1)
2430 {
2431 ft2232_device_desc = strdup(args[0]);
2432 cp = strchr( ft2232_device_desc, 0 );
2433 // under Win32, the FTD2XX driver appends an "A" to the end
2434 // of the description, this examines the given desc
2435 // and creates the 'missing' _A or non_A variable.
2436 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2437 // it was, so make this the "A" version.
2438 ft2232_device_desc_A = ft2232_device_desc;
2439 // and *CREATE* the non-A version.
2440 strcpy( buf, ft2232_device_desc );
2441 cp = strchr( buf, 0 );
2442 cp[-2] = 0;
2443 ft2232_device_desc = strdup( buf );
2444 } else {
2445 // <space>A not defined
2446 // so create it
2447 sprintf( buf, "%s A", ft2232_device_desc );
2448 ft2232_device_desc_A = strdup( buf );
2449 }
2450 }
2451 else
2452 {
2453 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2454 }
2455
2456 return ERROR_OK;
2457 }
2458
2459
2460 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2461 {
2462 if (argc == 1)
2463 {
2464 ft2232_serial = strdup(args[0]);
2465 }
2466 else
2467 {
2468 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2469 }
2470
2471 return ERROR_OK;
2472 }
2473
2474
2475 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2476 {
2477 if (argc == 0)
2478 return ERROR_OK;
2479
2480 ft2232_layout = malloc(strlen(args[0]) + 1);
2481 strcpy(ft2232_layout, args[0]);
2482
2483 return ERROR_OK;
2484 }
2485
2486
2487 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2488 {
2489 int i;
2490
2491 if (argc > MAX_USB_IDS * 2)
2492 {
2493 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2494 "(maximum is %d pairs)", MAX_USB_IDS);
2495 argc = MAX_USB_IDS * 2;
2496 }
2497 if ( argc < 2 || (argc & 1) )
2498 {
2499 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2500 if (argc < 2)
2501 return ERROR_OK;
2502 }
2503
2504 for (i = 0; i + 1 < argc; i += 2)
2505 {
2506 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2507 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2508 }
2509
2510 /*
2511 * Explicitly terminate, in case there are multiples instances of
2512 * ft2232_vid_pid.
2513 */
2514 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2515
2516 return ERROR_OK;
2517 }
2518
2519
2520 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2521 {
2522 if (argc == 1)
2523 {
2524 ft2232_latency = atoi(args[0]);
2525 }
2526 else
2527 {
2528 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2529 }
2530
2531 return ERROR_OK;
2532 }
2533
2534
2535 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2536 {
2537 int retval = 0;
2538
2539 /* 7 bits of either ones or zeros. */
2540 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2541
2542 while (num_cycles > 0)
2543 {
2544 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2545 * at most 7 bits per invocation. Here we invoke it potentially
2546 * several times.
2547 */
2548 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2549
2550 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2551 {
2552 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2553 retval = ERROR_JTAG_QUEUE_FAILED;
2554
2555 first_unsent = cmd;
2556 }
2557
2558 /* command "Clock Data to TMS/CS Pin (no Read)" */
2559 BUFFER_ADD = 0x4b;
2560
2561 /* scan 7 bit */
2562 BUFFER_ADD = bitcount_per_command - 1;
2563
2564 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2565 BUFFER_ADD = tms;
2566
2567 require_send = 1;
2568
2569 num_cycles -= bitcount_per_command;
2570 }
2571
2572 return retval;
2573 }

Linking to existing account procedure

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

SSH host keys fingerprints

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