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