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

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)