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

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)