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