change #include "commands.h" to <jtag/commands.h>
[openocd/ztw.git] / src / jtag / drivers / ft2232.c
blobc7503f8a46c19bebaab118c7048d3c394091f6b6
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 ***************************************************************************/
30 /* This code uses information contained in the MPSSE specification which was
31 * found here:
32 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
33 * Hereafter this is called the "MPSSE Spec".
35 * The datasheet for the ftdichip.com's FT2232D part is here:
36 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
43 /* project specific includes */
44 #include "interface.h"
45 #include <jtag/commands.h>
46 #include <helper/time_support.h>
48 #if IS_CYGWIN == 1
49 #include <windows.h>
50 #endif
52 #include <assert.h>
54 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
55 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
56 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
57 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
58 #endif
60 /* FT2232 access library includes */
61 #if BUILD_FT2232_FTD2XX == 1
62 #include <ftd2xx.h>
63 #elif BUILD_FT2232_LIBFTDI == 1
64 #include <ftdi.h>
65 #endif
67 /* max TCK for the high speed devices 30000 kHz */
68 #define FTDI_2232H_4232H_MAX_TCK 30000
69 /* max TCK for the full speed devices 6000 kHz */
70 #define FTDI_2232C_MAX_TCK 6000
71 /* this speed value tells that RTCK is requested */
72 #define RTCK_SPEED -1
75 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
76 * errors with a retry count of 100. Increasing it solves the problem for me.
77 * - Dimitar
79 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
80 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
81 * to something sane.
83 #define LIBFTDI_READ_RETRY_COUNT 2000
85 #ifndef BUILD_FT2232_HIGHSPEED
86 #if BUILD_FT2232_FTD2XX == 1
87 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
88 #elif BUILD_FT2232_LIBFTDI == 1
89 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
90 #endif
91 #endif
93 /**
94 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
95 * stable state. Calling code must ensure that current state is stable,
96 * that verification is not done in here.
98 * @param num_cycles The number of clocks cycles to send.
99 * @param cmd The command to send.
101 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
103 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
105 static char * ft2232_device_desc_A = NULL;
106 static char* ft2232_device_desc = NULL;
107 static char* ft2232_serial = NULL;
108 static char* ft2232_layout = NULL;
109 static uint8_t ft2232_latency = 2;
110 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
112 #define MAX_USB_IDS 8
113 /* vid = pid = 0 marks the end of the list */
114 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
115 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
117 struct ft2232_layout {
118 char* name;
119 int (*init)(void);
120 void (*reset)(int trst, int srst);
121 void (*blink)(void);
124 /* init procedures for supported layouts */
125 static int usbjtag_init(void);
126 static int jtagkey_init(void);
127 static int olimex_jtag_init(void);
128 static int flyswatter_init(void);
129 static int turtle_init(void);
130 static int comstick_init(void);
131 static int stm32stick_init(void);
132 static int axm0432_jtag_init(void);
133 static int sheevaplug_init(void);
134 static int icebear_jtag_init(void);
135 static int cortino_jtag_init(void);
136 static int signalyzer_h_init(void);
137 static int ktlink_init(void);
139 /* reset procedures for supported layouts */
140 static void usbjtag_reset(int trst, int srst);
141 static void jtagkey_reset(int trst, int srst);
142 static void olimex_jtag_reset(int trst, int srst);
143 static void flyswatter_reset(int trst, int srst);
144 static void turtle_reset(int trst, int srst);
145 static void comstick_reset(int trst, int srst);
146 static void stm32stick_reset(int trst, int srst);
147 static void axm0432_jtag_reset(int trst, int srst);
148 static void sheevaplug_reset(int trst, int srst);
149 static void icebear_jtag_reset(int trst, int srst);
150 static void signalyzer_h_reset(int trst, int srst);
151 static void ktlink_reset(int trst, int srst);
153 /* blink procedures for layouts that support a blinking led */
154 static void olimex_jtag_blink(void);
155 static void flyswatter_jtag_blink(void);
156 static void turtle_jtag_blink(void);
157 static void signalyzer_h_blink(void);
158 static void ktlink_blink(void);
160 static const struct ft2232_layout ft2232_layouts[] =
162 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
163 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
164 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
165 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
166 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
167 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
168 { "luminary_icdi", usbjtag_init, usbjtag_reset, NULL },
169 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
170 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
171 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
172 { "comstick", comstick_init, comstick_reset, NULL },
173 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
174 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
175 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
176 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
177 { "cortino", cortino_jtag_init, comstick_reset, NULL },
178 { "signalyzer-h", signalyzer_h_init, signalyzer_h_reset, signalyzer_h_blink },
179 { "ktlink", ktlink_init, ktlink_reset, ktlink_blink },
180 { NULL, NULL, NULL, NULL },
183 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
185 static const struct ft2232_layout *layout;
186 static uint8_t low_output = 0x0;
187 static uint8_t low_direction = 0x0;
188 static uint8_t high_output = 0x0;
189 static uint8_t high_direction = 0x0;
191 #if BUILD_FT2232_FTD2XX == 1
192 static FT_HANDLE ftdih = NULL;
193 static FT_DEVICE ftdi_device = 0;
194 #elif BUILD_FT2232_LIBFTDI == 1
195 static struct ftdi_context ftdic;
196 static enum ftdi_chip_type ftdi_device;
197 #endif
199 static struct jtag_command* first_unsent; /* next command that has to be sent */
200 static int require_send;
202 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
204 "There is a significant difference between libftdi and libftd2xx. The latter
205 one allows to schedule up to 64*64 bytes of result data while libftdi fails
206 with more than 4*64. As a consequence, the FT2232 driver is forced to
207 perform around 16x more USB transactions for long command streams with TDO
208 capture when running with libftdi."
210 No idea how we get
211 #define FT2232_BUFFER_SIZE 131072
212 a comment would have been nice.
215 #define FT2232_BUFFER_SIZE 131072
217 static uint8_t* ft2232_buffer = NULL;
218 static int ft2232_buffer_size = 0;
219 static int ft2232_read_pointer = 0;
220 static int ft2232_expect_read = 0;
223 * Function buffer_write
224 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
225 * @param val is the byte to send.
227 static inline void buffer_write(uint8_t val)
229 assert(ft2232_buffer);
230 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
231 ft2232_buffer[ft2232_buffer_size++] = val;
235 * Function buffer_read
236 * returns a byte from the byte buffer.
238 static inline uint8_t buffer_read(void)
240 assert(ft2232_buffer);
241 assert(ft2232_read_pointer < ft2232_buffer_size);
242 return ft2232_buffer[ft2232_read_pointer++];
246 * Clocks out \a bit_count bits on the TMS line, starting with the least
247 * significant bit of tms_bits and progressing to more significant bits.
248 * Rigorous state transition logging is done here via tap_set_state().
250 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
251 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
252 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
253 * is often used for this, 0x4b.
255 * @param tms_bits Holds the sequence of bits to send.
256 * @param tms_count Tells how many bits in the sequence.
257 * @param tdi_bit A single bit to pass on to TDI before the first TCK
258 * cycle and held static for the duration of TMS clocking.
260 * See the MPSSE spec referenced above.
262 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
264 uint8_t tms_byte;
265 int i;
266 int tms_ndx; /* bit index into tms_byte */
268 assert(tms_count > 0);
270 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
271 mpsse_cmd, tms_bits, tms_count);
273 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
275 bool bit = tms_bits & 1;
277 if (bit)
278 tms_byte |= (1 << tms_ndx);
280 /* always do state transitions in public view */
281 tap_set_state(tap_state_transition(tap_get_state(), bit));
283 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
284 also increment.
286 ++tms_ndx;
288 if (tms_ndx == 7 || i == tms_count-1)
290 buffer_write(mpsse_cmd);
291 buffer_write(tms_ndx - 1);
293 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
294 TMS/CS and is held static for the duration of TMS/CS clocking.
296 buffer_write(tms_byte | (tdi_bit << 7));
302 * Function get_tms_buffer_requirements
303 * returns what clock_tms() will consume if called with
304 * same \a bit_count.
306 static inline int get_tms_buffer_requirements(int bit_count)
308 return ((bit_count + 6)/7) * 3;
312 * Function move_to_state
313 * moves the TAP controller from the current state to a
314 * \a goal_state through a path given by tap_get_tms_path(). State transition
315 * logging is performed by delegation to clock_tms().
317 * @param goal_state is the destination state for the move.
319 static void move_to_state(tap_state_t goal_state)
321 tap_state_t start_state = tap_get_state();
323 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
324 lookup of the required TMS pattern to move to this state from the
325 start state.
328 /* do the 2 lookups */
329 int tms_bits = tap_get_tms_path(start_state, goal_state);
330 int tms_count = tap_get_tms_path_len(start_state, goal_state);
332 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
334 clock_tms(0x4b, tms_bits, tms_count, 0);
337 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
339 #if BUILD_FT2232_FTD2XX == 1
340 FT_STATUS status;
341 DWORD dw_bytes_written;
342 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
344 *bytes_written = dw_bytes_written;
345 LOG_ERROR("FT_Write returned: %lu", status);
346 return ERROR_JTAG_DEVICE_ERROR;
348 else
350 *bytes_written = dw_bytes_written;
351 return ERROR_OK;
353 #elif BUILD_FT2232_LIBFTDI == 1
354 int retval;
355 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
357 *bytes_written = 0;
358 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
359 return ERROR_JTAG_DEVICE_ERROR;
361 else
363 *bytes_written = retval;
364 return ERROR_OK;
366 #endif
369 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
371 #if BUILD_FT2232_FTD2XX == 1
372 DWORD dw_bytes_read;
373 FT_STATUS status;
374 int timeout = 5;
375 *bytes_read = 0;
377 while ((*bytes_read < size) && timeout--)
379 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
380 *bytes_read, &dw_bytes_read)) != FT_OK)
382 *bytes_read = 0;
383 LOG_ERROR("FT_Read returned: %lu", status);
384 return ERROR_JTAG_DEVICE_ERROR;
386 *bytes_read += dw_bytes_read;
389 #elif BUILD_FT2232_LIBFTDI == 1
390 int retval;
391 int timeout = LIBFTDI_READ_RETRY_COUNT;
392 *bytes_read = 0;
394 while ((*bytes_read < size) && timeout--)
396 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
398 *bytes_read = 0;
399 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
400 return ERROR_JTAG_DEVICE_ERROR;
402 *bytes_read += retval;
405 #endif
407 if (*bytes_read < size)
409 LOG_ERROR("couldn't read enough bytes from "
410 "FT2232 device (%i < %i)",
411 (unsigned)*bytes_read,
412 (unsigned)size);
413 return ERROR_JTAG_DEVICE_ERROR;
416 return ERROR_OK;
419 static bool ft2232_device_is_highspeed(void)
421 #if BUILD_FT2232_FTD2XX == 1
422 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
423 #elif BUILD_FT2232_LIBFTDI == 1
424 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
425 #endif
429 * Commands that only apply to the FT2232H and FT4232H devices.
430 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
431 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
434 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
436 uint8_t buf = enable ? 0x96 : 0x97;
437 LOG_DEBUG("%2.2x", buf);
439 uint32_t bytes_written;
440 int retval = ft2232_write(&buf, 1, &bytes_written);
441 if ((ERROR_OK != retval) || (bytes_written != 1))
443 LOG_ERROR("couldn't write command to %s adaptive clocking"
444 , enable ? "enable" : "disable");
445 return retval;
448 return ERROR_OK;
452 * Enable/disable the clk divide by 5 of the 60MHz master clock.
453 * This result in a JTAG clock speed range of 91.553Hz-6MHz
454 * respective 457.763Hz-30MHz.
456 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
458 uint32_t bytes_written;
459 uint8_t buf = enable ? 0x8b : 0x8a;
460 int retval = ft2232_write(&buf, 1, &bytes_written);
461 if ((ERROR_OK != retval) || (bytes_written != 1))
463 LOG_ERROR("couldn't write command to %s clk divide by 5"
464 , enable ? "enable" : "disable");
465 return ERROR_JTAG_INIT_FAILED;
467 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
468 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
470 return ERROR_OK;
473 static int ft2232_speed(int speed)
475 uint8_t buf[3];
476 int retval;
477 uint32_t bytes_written;
479 retval = ERROR_OK;
480 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
481 if (ft2232_device_is_highspeed())
482 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
483 else if (enable_adaptive_clocking)
485 LOG_ERROR("ft2232 device %lu does not support RTCK"
486 , (long unsigned int)ftdi_device);
487 return ERROR_FAIL;
490 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
491 return retval;
493 buf[0] = 0x86; /* command "set divisor" */
494 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
495 buf[2] = (speed >> 8) & 0xff; /* valueH */
497 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
498 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
500 LOG_ERROR("couldn't set FT2232 TCK speed");
501 return retval;
504 return ERROR_OK;
507 static int ft2232_speed_div(int speed, int* khz)
509 /* Take a look in the FT2232 manual,
510 * AN2232C-01 Command Processor for
511 * MPSSE and MCU Host Bus. Chapter 3.8 */
513 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
515 return ERROR_OK;
518 static int ft2232_khz(int khz, int* jtag_speed)
520 if (khz == 0)
522 if (ft2232_device_is_highspeed())
524 *jtag_speed = RTCK_SPEED;
525 return ERROR_OK;
527 else
529 LOG_DEBUG("RCLK not supported");
530 return ERROR_FAIL;
534 /* Take a look in the FT2232 manual,
535 * AN2232C-01 Command Processor for
536 * MPSSE and MCU Host Bus. Chapter 3.8
538 * We will calc here with a multiplier
539 * of 10 for better rounding later. */
541 /* Calc speed, (ft2232_max_tck / khz) - 1 */
542 /* Use 65000 for better rounding */
543 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
545 /* Add 0.9 for rounding */
546 *jtag_speed += 9;
548 /* Calc real speed */
549 *jtag_speed = *jtag_speed / 10;
551 /* Check if speed is greater than 0 */
552 if (*jtag_speed < 0)
554 *jtag_speed = 0;
557 /* Check max value */
558 if (*jtag_speed > 0xFFFF)
560 *jtag_speed = 0xFFFF;
563 return ERROR_OK;
566 static void ft2232_end_state(tap_state_t state)
568 if (tap_is_state_stable(state))
569 tap_set_end_state(state);
570 else
572 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
573 exit(-1);
577 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
579 int num_bytes = (scan_size + 7) / 8;
580 int bits_left = scan_size;
581 int cur_byte = 0;
583 while (num_bytes-- > 1)
585 buffer[cur_byte++] = buffer_read();
586 bits_left -= 8;
589 buffer[cur_byte] = 0x0;
591 /* There is one more partial byte left from the clock data in/out instructions */
592 if (bits_left > 1)
594 buffer[cur_byte] = buffer_read() >> 1;
596 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
597 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
600 static void ft2232_debug_dump_buffer(void)
602 int i;
603 char line[256];
604 char* line_p = line;
606 for (i = 0; i < ft2232_buffer_size; i++)
608 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
609 if (i % 16 == 15)
611 LOG_DEBUG("%s", line);
612 line_p = line;
616 if (line_p != line)
617 LOG_DEBUG("%s", line);
620 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
622 struct jtag_command* cmd;
623 uint8_t* buffer;
624 int scan_size;
625 enum scan_type type;
626 int retval;
627 uint32_t bytes_written = 0;
628 uint32_t bytes_read = 0;
630 #ifdef _DEBUG_USB_IO_
631 struct timeval start, inter, inter2, end;
632 struct timeval d_inter, d_inter2, d_end;
633 #endif
635 #ifdef _DEBUG_USB_COMMS_
636 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
637 ft2232_debug_dump_buffer();
638 #endif
640 #ifdef _DEBUG_USB_IO_
641 gettimeofday(&start, NULL);
642 #endif
644 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
646 LOG_ERROR("couldn't write MPSSE commands to FT2232");
647 return retval;
650 #ifdef _DEBUG_USB_IO_
651 gettimeofday(&inter, NULL);
652 #endif
654 if (ft2232_expect_read)
656 /* FIXME this "timeout" is never changed ... */
657 int timeout = LIBFTDI_READ_RETRY_COUNT;
658 ft2232_buffer_size = 0;
660 #ifdef _DEBUG_USB_IO_
661 gettimeofday(&inter2, NULL);
662 #endif
664 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
666 LOG_ERROR("couldn't read from FT2232");
667 return retval;
670 #ifdef _DEBUG_USB_IO_
671 gettimeofday(&end, NULL);
673 timeval_subtract(&d_inter, &inter, &start);
674 timeval_subtract(&d_inter2, &inter2, &start);
675 timeval_subtract(&d_end, &end, &start);
677 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
678 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
679 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
680 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
681 #endif
683 ft2232_buffer_size = bytes_read;
685 if (ft2232_expect_read != ft2232_buffer_size)
687 LOG_ERROR("ft2232_expect_read (%i) != "
688 "ft2232_buffer_size (%i) "
689 "(%i retries)",
690 ft2232_expect_read,
691 ft2232_buffer_size,
692 LIBFTDI_READ_RETRY_COUNT - timeout);
693 ft2232_debug_dump_buffer();
695 exit(-1);
698 #ifdef _DEBUG_USB_COMMS_
699 LOG_DEBUG("read buffer (%i retries): %i bytes",
700 LIBFTDI_READ_RETRY_COUNT - timeout,
701 ft2232_buffer_size);
702 ft2232_debug_dump_buffer();
703 #endif
706 ft2232_expect_read = 0;
707 ft2232_read_pointer = 0;
709 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
710 * that wasn't handled by a caller-provided error handler
712 retval = ERROR_OK;
714 cmd = first;
715 while (cmd != last)
717 switch (cmd->type)
719 case JTAG_SCAN:
720 type = jtag_scan_type(cmd->cmd.scan);
721 if (type != SCAN_OUT)
723 scan_size = jtag_scan_size(cmd->cmd.scan);
724 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
725 ft2232_read_scan(type, buffer, scan_size);
726 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
727 retval = ERROR_JTAG_QUEUE_FAILED;
728 free(buffer);
730 break;
732 default:
733 break;
736 cmd = cmd->next;
739 ft2232_buffer_size = 0;
741 return retval;
745 * Function ft2232_add_pathmove
746 * moves the TAP controller from the current state to a new state through the
747 * given path, where path is an array of tap_state_t's.
749 * @param path is an array of tap_stat_t which gives the states to traverse through
750 * ending with the last state at path[num_states-1]
751 * @param num_states is the count of state steps to move through
753 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
755 int state_count = 0;
757 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
759 DEBUG_JTAG_IO("-");
761 /* this loop verifies that the path is legal and logs each state in the path */
762 while (num_states)
764 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
765 int bit_count = 0;
766 int num_states_batch = num_states > 7 ? 7 : num_states;
768 /* command "Clock Data to TMS/CS Pin (no Read)" */
769 buffer_write(0x4b);
771 /* number of states remaining */
772 buffer_write(num_states_batch - 1);
774 while (num_states_batch--) {
775 /* either TMS=0 or TMS=1 must work ... */
776 if (tap_state_transition(tap_get_state(), false)
777 == path[state_count])
778 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
779 else if (tap_state_transition(tap_get_state(), true)
780 == path[state_count])
781 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
783 /* ... or else the caller goofed BADLY */
784 else {
785 LOG_ERROR("BUG: %s -> %s isn't a valid "
786 "TAP state transition",
787 tap_state_name(tap_get_state()),
788 tap_state_name(path[state_count]));
789 exit(-1);
792 tap_set_state(path[state_count]);
793 state_count++;
794 num_states--;
797 buffer_write(tms_byte);
799 tap_set_end_state(tap_get_state());
802 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
804 int num_bytes = (scan_size + 7) / 8;
805 int bits_left = scan_size;
806 int cur_byte = 0;
807 int last_bit;
809 if (!ir_scan)
811 if (tap_get_state() != TAP_DRSHIFT)
813 move_to_state(TAP_DRSHIFT);
816 else
818 if (tap_get_state() != TAP_IRSHIFT)
820 move_to_state(TAP_IRSHIFT);
824 /* add command for complete bytes */
825 while (num_bytes > 1)
827 int thisrun_bytes;
828 if (type == SCAN_IO)
830 /* Clock Data Bytes In and Out LSB First */
831 buffer_write(0x39);
832 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
834 else if (type == SCAN_OUT)
836 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
837 buffer_write(0x19);
838 /* LOG_DEBUG("added TDI bytes (o)"); */
840 else if (type == SCAN_IN)
842 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
843 buffer_write(0x28);
844 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
847 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
848 num_bytes -= thisrun_bytes;
850 buffer_write((uint8_t) (thisrun_bytes - 1));
851 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
853 if (type != SCAN_IN)
855 /* add complete bytes */
856 while (thisrun_bytes-- > 0)
858 buffer_write(buffer[cur_byte++]);
859 bits_left -= 8;
862 else /* (type == SCAN_IN) */
864 bits_left -= 8 * (thisrun_bytes);
868 /* the most signifcant bit is scanned during TAP movement */
869 if (type != SCAN_IN)
870 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
871 else
872 last_bit = 0;
874 /* process remaining bits but the last one */
875 if (bits_left > 1)
877 if (type == SCAN_IO)
879 /* Clock Data Bits In and Out LSB First */
880 buffer_write(0x3b);
881 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
883 else if (type == SCAN_OUT)
885 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
886 buffer_write(0x1b);
887 /* LOG_DEBUG("added TDI bits (o)"); */
889 else if (type == SCAN_IN)
891 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
892 buffer_write(0x2a);
893 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
896 buffer_write(bits_left - 2);
897 if (type != SCAN_IN)
898 buffer_write(buffer[cur_byte]);
901 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
902 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
904 if (type == SCAN_IO)
906 /* Clock Data Bits In and Out LSB First */
907 buffer_write(0x3b);
908 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
910 else if (type == SCAN_OUT)
912 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
913 buffer_write(0x1b);
914 /* LOG_DEBUG("added TDI bits (o)"); */
916 else if (type == SCAN_IN)
918 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
919 buffer_write(0x2a);
920 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
922 buffer_write(0x0);
923 buffer_write(last_bit);
925 else
927 int tms_bits;
928 int tms_count;
929 uint8_t mpsse_cmd;
931 /* move from Shift-IR/DR to end state */
932 if (type != SCAN_OUT)
934 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
935 /* This must be coordinated with the bit shifts in ft2232_read_scan */
936 tms_bits = 0x01;
937 tms_count = 2;
938 /* Clock Data to TMS/CS Pin with Read */
939 mpsse_cmd = 0x6b;
941 else
943 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
944 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
945 /* Clock Data to TMS/CS Pin (no Read) */
946 mpsse_cmd = 0x4b;
949 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
950 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
953 if (tap_get_state() != tap_get_end_state())
955 move_to_state(tap_get_end_state());
959 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
961 int num_bytes = (scan_size + 7) / 8;
962 int bits_left = scan_size;
963 int cur_byte = 0;
964 int last_bit;
965 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
966 uint8_t* receive_pointer = receive_buffer;
967 uint32_t bytes_written;
968 uint32_t bytes_read;
969 int retval;
970 int thisrun_read = 0;
972 if (cmd->ir_scan)
974 LOG_ERROR("BUG: large IR scans are not supported");
975 exit(-1);
978 if (tap_get_state() != TAP_DRSHIFT)
980 move_to_state(TAP_DRSHIFT);
983 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
985 LOG_ERROR("couldn't write MPSSE commands to FT2232");
986 exit(-1);
988 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
989 ft2232_buffer_size, (int)bytes_written);
990 ft2232_buffer_size = 0;
992 /* add command for complete bytes */
993 while (num_bytes > 1)
995 int thisrun_bytes;
997 if (type == SCAN_IO)
999 /* Clock Data Bytes In and Out LSB First */
1000 buffer_write(0x39);
1001 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1003 else if (type == SCAN_OUT)
1005 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1006 buffer_write(0x19);
1007 /* LOG_DEBUG("added TDI bytes (o)"); */
1009 else if (type == SCAN_IN)
1011 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1012 buffer_write(0x28);
1013 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1016 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1017 thisrun_read = thisrun_bytes;
1018 num_bytes -= thisrun_bytes;
1019 buffer_write((uint8_t) (thisrun_bytes - 1));
1020 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1022 if (type != SCAN_IN)
1024 /* add complete bytes */
1025 while (thisrun_bytes-- > 0)
1027 buffer_write(buffer[cur_byte]);
1028 cur_byte++;
1029 bits_left -= 8;
1032 else /* (type == SCAN_IN) */
1034 bits_left -= 8 * (thisrun_bytes);
1037 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1039 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1040 exit(-1);
1042 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1043 ft2232_buffer_size,
1044 (int)bytes_written);
1045 ft2232_buffer_size = 0;
1047 if (type != SCAN_OUT)
1049 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1051 LOG_ERROR("couldn't read from FT2232");
1052 exit(-1);
1054 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1055 thisrun_read,
1056 (int)bytes_read);
1057 receive_pointer += bytes_read;
1061 thisrun_read = 0;
1063 /* the most signifcant bit is scanned during TAP movement */
1064 if (type != SCAN_IN)
1065 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1066 else
1067 last_bit = 0;
1069 /* process remaining bits but the last one */
1070 if (bits_left > 1)
1072 if (type == SCAN_IO)
1074 /* Clock Data Bits In and Out LSB First */
1075 buffer_write(0x3b);
1076 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1078 else if (type == SCAN_OUT)
1080 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1081 buffer_write(0x1b);
1082 /* LOG_DEBUG("added TDI bits (o)"); */
1084 else if (type == SCAN_IN)
1086 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1087 buffer_write(0x2a);
1088 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1090 buffer_write(bits_left - 2);
1091 if (type != SCAN_IN)
1092 buffer_write(buffer[cur_byte]);
1094 if (type != SCAN_OUT)
1095 thisrun_read += 2;
1098 if (tap_get_end_state() == TAP_DRSHIFT)
1100 if (type == SCAN_IO)
1102 /* Clock Data Bits In and Out LSB First */
1103 buffer_write(0x3b);
1104 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1106 else if (type == SCAN_OUT)
1108 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1109 buffer_write(0x1b);
1110 /* LOG_DEBUG("added TDI bits (o)"); */
1112 else if (type == SCAN_IN)
1114 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1115 buffer_write(0x2a);
1116 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1118 buffer_write(0x0);
1119 buffer_write(last_bit);
1121 else
1123 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1124 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1125 uint8_t mpsse_cmd;
1127 /* move from Shift-IR/DR to end state */
1128 if (type != SCAN_OUT)
1130 /* Clock Data to TMS/CS Pin with Read */
1131 mpsse_cmd = 0x6b;
1132 /* LOG_DEBUG("added TMS scan (read)"); */
1134 else
1136 /* Clock Data to TMS/CS Pin (no Read) */
1137 mpsse_cmd = 0x4b;
1138 /* LOG_DEBUG("added TMS scan (no read)"); */
1141 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1142 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1145 if (type != SCAN_OUT)
1146 thisrun_read += 1;
1148 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1150 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1151 exit(-1);
1153 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1154 ft2232_buffer_size,
1155 (int)bytes_written);
1156 ft2232_buffer_size = 0;
1158 if (type != SCAN_OUT)
1160 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1162 LOG_ERROR("couldn't read from FT2232");
1163 exit(-1);
1165 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1166 thisrun_read,
1167 (int)bytes_read);
1168 receive_pointer += bytes_read;
1171 return ERROR_OK;
1174 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1176 int predicted_size = 3;
1177 int num_bytes = (scan_size - 1) / 8;
1179 if (tap_get_state() != TAP_DRSHIFT)
1180 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1182 if (type == SCAN_IN) /* only from device to host */
1184 /* complete bytes */
1185 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1187 /* remaining bits - 1 (up to 7) */
1188 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1190 else /* host to device, or bidirectional */
1192 /* complete bytes */
1193 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1195 /* remaining bits -1 (up to 7) */
1196 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1199 return predicted_size;
1202 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1204 int predicted_size = 0;
1206 if (type != SCAN_OUT)
1208 /* complete bytes */
1209 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1211 /* remaining bits - 1 */
1212 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1214 /* last bit (from TMS scan) */
1215 predicted_size += 1;
1218 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1220 return predicted_size;
1223 static void usbjtag_reset(int trst, int srst)
1225 enum reset_types jtag_reset_config = jtag_get_reset_config();
1226 if (trst == 1)
1228 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1229 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1230 else
1231 low_output &= ~nTRST; /* switch output low */
1233 else if (trst == 0)
1235 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1236 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1237 else
1238 low_output |= nTRST; /* switch output high */
1241 if (srst == 1)
1243 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1244 low_output &= ~nSRST; /* switch output low */
1245 else
1246 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1248 else if (srst == 0)
1250 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1251 low_output |= nSRST; /* switch output high */
1252 else
1253 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1256 /* command "set data bits low byte" */
1257 buffer_write(0x80);
1258 buffer_write(low_output);
1259 buffer_write(low_direction);
1262 static void jtagkey_reset(int trst, int srst)
1264 enum reset_types jtag_reset_config = jtag_get_reset_config();
1265 if (trst == 1)
1267 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1268 high_output &= ~nTRSTnOE;
1269 else
1270 high_output &= ~nTRST;
1272 else if (trst == 0)
1274 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1275 high_output |= nTRSTnOE;
1276 else
1277 high_output |= nTRST;
1280 if (srst == 1)
1282 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1283 high_output &= ~nSRST;
1284 else
1285 high_output &= ~nSRSTnOE;
1287 else if (srst == 0)
1289 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1290 high_output |= nSRST;
1291 else
1292 high_output |= nSRSTnOE;
1295 /* command "set data bits high byte" */
1296 buffer_write(0x82);
1297 buffer_write(high_output);
1298 buffer_write(high_direction);
1299 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1300 high_direction);
1303 static void olimex_jtag_reset(int trst, int srst)
1305 enum reset_types jtag_reset_config = jtag_get_reset_config();
1306 if (trst == 1)
1308 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1309 high_output &= ~nTRSTnOE;
1310 else
1311 high_output &= ~nTRST;
1313 else if (trst == 0)
1315 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1316 high_output |= nTRSTnOE;
1317 else
1318 high_output |= nTRST;
1321 if (srst == 1)
1323 high_output |= nSRST;
1325 else if (srst == 0)
1327 high_output &= ~nSRST;
1330 /* command "set data bits high byte" */
1331 buffer_write(0x82);
1332 buffer_write(high_output);
1333 buffer_write(high_direction);
1334 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1335 high_direction);
1338 static void axm0432_jtag_reset(int trst, int srst)
1340 if (trst == 1)
1342 tap_set_state(TAP_RESET);
1343 high_output &= ~nTRST;
1345 else if (trst == 0)
1347 high_output |= nTRST;
1350 if (srst == 1)
1352 high_output &= ~nSRST;
1354 else if (srst == 0)
1356 high_output |= nSRST;
1359 /* command "set data bits low byte" */
1360 buffer_write(0x82);
1361 buffer_write(high_output);
1362 buffer_write(high_direction);
1363 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1364 high_direction);
1367 static void flyswatter_reset(int trst, int srst)
1369 if (trst == 1)
1371 low_output &= ~nTRST;
1373 else if (trst == 0)
1375 low_output |= nTRST;
1378 if (srst == 1)
1380 low_output |= nSRST;
1382 else if (srst == 0)
1384 low_output &= ~nSRST;
1387 /* command "set data bits low byte" */
1388 buffer_write(0x80);
1389 buffer_write(low_output);
1390 buffer_write(low_direction);
1391 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1394 static void turtle_reset(int trst, int srst)
1396 trst = trst;
1398 if (srst == 1)
1400 low_output |= nSRST;
1402 else if (srst == 0)
1404 low_output &= ~nSRST;
1407 /* command "set data bits low byte" */
1408 buffer_write(0x80);
1409 buffer_write(low_output);
1410 buffer_write(low_direction);
1411 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1414 static void comstick_reset(int trst, int srst)
1416 if (trst == 1)
1418 high_output &= ~nTRST;
1420 else if (trst == 0)
1422 high_output |= nTRST;
1425 if (srst == 1)
1427 high_output &= ~nSRST;
1429 else if (srst == 0)
1431 high_output |= nSRST;
1434 /* command "set data bits high byte" */
1435 buffer_write(0x82);
1436 buffer_write(high_output);
1437 buffer_write(high_direction);
1438 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1439 high_direction);
1442 static void stm32stick_reset(int trst, int srst)
1444 if (trst == 1)
1446 high_output &= ~nTRST;
1448 else if (trst == 0)
1450 high_output |= nTRST;
1453 if (srst == 1)
1455 low_output &= ~nSRST;
1457 else if (srst == 0)
1459 low_output |= nSRST;
1462 /* command "set data bits low byte" */
1463 buffer_write(0x80);
1464 buffer_write(low_output);
1465 buffer_write(low_direction);
1467 /* command "set data bits high byte" */
1468 buffer_write(0x82);
1469 buffer_write(high_output);
1470 buffer_write(high_direction);
1471 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1472 high_direction);
1475 static void sheevaplug_reset(int trst, int srst)
1477 if (trst == 1)
1478 high_output &= ~nTRST;
1479 else if (trst == 0)
1480 high_output |= nTRST;
1482 if (srst == 1)
1483 high_output &= ~nSRSTnOE;
1484 else if (srst == 0)
1485 high_output |= nSRSTnOE;
1487 /* command "set data bits high byte" */
1488 buffer_write(0x82);
1489 buffer_write(high_output);
1490 buffer_write(high_direction);
1491 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1494 static int ft2232_execute_runtest(struct jtag_command *cmd)
1496 int retval;
1497 int i;
1498 int predicted_size = 0;
1499 retval = ERROR_OK;
1501 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1502 cmd->cmd.runtest->num_cycles,
1503 tap_state_name(cmd->cmd.runtest->end_state));
1505 /* only send the maximum buffer size that FT2232C can handle */
1506 predicted_size = 0;
1507 if (tap_get_state() != TAP_IDLE)
1508 predicted_size += 3;
1509 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1510 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1511 predicted_size += 3;
1512 if (tap_get_end_state() != TAP_IDLE)
1513 predicted_size += 3;
1514 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1516 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1517 retval = ERROR_JTAG_QUEUE_FAILED;
1518 require_send = 0;
1519 first_unsent = cmd;
1521 if (tap_get_state() != TAP_IDLE)
1523 move_to_state(TAP_IDLE);
1524 require_send = 1;
1526 i = cmd->cmd.runtest->num_cycles;
1527 while (i > 0)
1529 /* there are no state transitions in this code, so omit state tracking */
1531 /* command "Clock Data to TMS/CS Pin (no Read)" */
1532 buffer_write(0x4b);
1534 /* scan 7 bits */
1535 buffer_write((i > 7) ? 6 : (i - 1));
1537 /* TMS data bits */
1538 buffer_write(0x0);
1540 i -= (i > 7) ? 7 : i;
1541 /* LOG_DEBUG("added TMS scan (no read)"); */
1544 ft2232_end_state(cmd->cmd.runtest->end_state);
1546 if (tap_get_state() != tap_get_end_state())
1548 move_to_state(tap_get_end_state());
1551 require_send = 1;
1552 DEBUG_JTAG_IO("runtest: %i, end in %s",
1553 cmd->cmd.runtest->num_cycles,
1554 tap_state_name(tap_get_end_state()));
1555 return retval;
1558 static int ft2232_execute_statemove(struct jtag_command *cmd)
1560 int predicted_size = 0;
1561 int retval = ERROR_OK;
1563 DEBUG_JTAG_IO("statemove end in %s",
1564 tap_state_name(cmd->cmd.statemove->end_state));
1566 /* only send the maximum buffer size that FT2232C can handle */
1567 predicted_size = 3;
1568 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1570 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1571 retval = ERROR_JTAG_QUEUE_FAILED;
1572 require_send = 0;
1573 first_unsent = cmd;
1575 ft2232_end_state(cmd->cmd.statemove->end_state);
1577 /* For TAP_RESET, ignore the current recorded state. It's often
1578 * wrong at server startup, and this transation is critical whenever
1579 * it's requested.
1581 if (tap_get_end_state() == TAP_RESET) {
1582 clock_tms(0x4b, 0xff, 5, 0);
1583 require_send = 1;
1585 /* shortest-path move to desired end state */
1586 } else if (tap_get_state() != tap_get_end_state())
1588 move_to_state(tap_get_end_state());
1589 require_send = 1;
1592 return retval;
1595 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1597 int predicted_size = 0;
1598 int retval = ERROR_OK;
1600 tap_state_t* path = cmd->cmd.pathmove->path;
1601 int num_states = cmd->cmd.pathmove->num_states;
1603 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1604 tap_state_name(tap_get_state()),
1605 tap_state_name(path[num_states-1]));
1607 /* only send the maximum buffer size that FT2232C can handle */
1608 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1609 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1611 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1612 retval = ERROR_JTAG_QUEUE_FAILED;
1614 require_send = 0;
1615 first_unsent = cmd;
1618 ft2232_add_pathmove(path, num_states);
1619 require_send = 1;
1621 return retval;
1624 static int ft2232_execute_scan(struct jtag_command *cmd)
1626 uint8_t* buffer;
1627 int scan_size; /* size of IR or DR scan */
1628 int predicted_size = 0;
1629 int retval = ERROR_OK;
1631 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1633 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1635 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1637 predicted_size = ft2232_predict_scan_out(scan_size, type);
1638 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1640 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1641 /* unsent commands before this */
1642 if (first_unsent != cmd)
1643 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1644 retval = ERROR_JTAG_QUEUE_FAILED;
1646 /* current command */
1647 ft2232_end_state(cmd->cmd.scan->end_state);
1648 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1649 require_send = 0;
1650 first_unsent = cmd->next;
1651 if (buffer)
1652 free(buffer);
1653 return retval;
1655 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1657 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1658 first_unsent,
1659 cmd);
1660 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1661 retval = ERROR_JTAG_QUEUE_FAILED;
1662 require_send = 0;
1663 first_unsent = cmd;
1665 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1666 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1667 ft2232_end_state(cmd->cmd.scan->end_state);
1668 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1669 require_send = 1;
1670 if (buffer)
1671 free(buffer);
1672 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1673 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1674 tap_state_name(tap_get_end_state()));
1675 return retval;
1679 static int ft2232_execute_reset(struct jtag_command *cmd)
1681 int retval;
1682 int predicted_size = 0;
1683 retval = ERROR_OK;
1685 DEBUG_JTAG_IO("reset trst: %i srst %i",
1686 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1688 /* only send the maximum buffer size that FT2232C can handle */
1689 predicted_size = 3;
1690 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1692 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1693 retval = ERROR_JTAG_QUEUE_FAILED;
1694 require_send = 0;
1695 first_unsent = cmd;
1698 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1700 tap_set_state(TAP_RESET);
1703 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1704 require_send = 1;
1706 DEBUG_JTAG_IO("trst: %i, srst: %i",
1707 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1708 return retval;
1711 static int ft2232_execute_sleep(struct jtag_command *cmd)
1713 int retval;
1714 retval = ERROR_OK;
1716 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1718 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1719 retval = ERROR_JTAG_QUEUE_FAILED;
1720 first_unsent = cmd->next;
1721 jtag_sleep(cmd->cmd.sleep->us);
1722 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1723 cmd->cmd.sleep->us,
1724 tap_state_name(tap_get_state()));
1725 return retval;
1728 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1730 int retval;
1731 retval = ERROR_OK;
1733 /* this is only allowed while in a stable state. A check for a stable
1734 * state was done in jtag_add_clocks()
1736 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1737 retval = ERROR_JTAG_QUEUE_FAILED;
1738 DEBUG_JTAG_IO("clocks %i while in %s",
1739 cmd->cmd.stableclocks->num_cycles,
1740 tap_state_name(tap_get_state()));
1741 return retval;
1744 static int ft2232_execute_command(struct jtag_command *cmd)
1746 int retval;
1747 retval = ERROR_OK;
1749 switch (cmd->type)
1751 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1752 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1753 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1754 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1755 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1756 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1757 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1758 default:
1759 LOG_ERROR("BUG: unknown JTAG command type encountered");
1760 exit(-1);
1762 return retval;
1765 static int ft2232_execute_queue(void)
1767 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
1768 int retval;
1770 first_unsent = cmd; /* next command that has to be sent */
1771 require_send = 0;
1773 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1774 * that wasn't handled by a caller-provided error handler
1776 retval = ERROR_OK;
1778 ft2232_buffer_size = 0;
1779 ft2232_expect_read = 0;
1781 /* blink, if the current layout has that feature */
1782 if (layout->blink)
1783 layout->blink();
1785 while (cmd)
1787 if (ft2232_execute_command(cmd) != ERROR_OK)
1788 retval = ERROR_JTAG_QUEUE_FAILED;
1789 /* Start reading input before FT2232 TX buffer fills up */
1790 cmd = cmd->next;
1791 if (ft2232_expect_read > 256)
1793 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1794 retval = ERROR_JTAG_QUEUE_FAILED;
1795 first_unsent = cmd;
1799 if (require_send > 0)
1800 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1801 retval = ERROR_JTAG_QUEUE_FAILED;
1803 return retval;
1806 #if BUILD_FT2232_FTD2XX == 1
1807 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1809 FT_STATUS status;
1810 DWORD deviceID;
1811 char SerialNumber[16];
1812 char Description[64];
1813 DWORD openex_flags = 0;
1814 char* openex_string = NULL;
1815 uint8_t latency_timer;
1817 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1819 #if IS_WIN32 == 0
1820 /* Add non-standard Vid/Pid to the linux driver */
1821 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1823 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1825 #endif
1827 if (ft2232_device_desc && ft2232_serial)
1829 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1830 ft2232_device_desc = NULL;
1833 if (ft2232_device_desc)
1835 openex_string = ft2232_device_desc;
1836 openex_flags = FT_OPEN_BY_DESCRIPTION;
1838 else if (ft2232_serial)
1840 openex_string = ft2232_serial;
1841 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1843 else
1845 LOG_ERROR("neither device description nor serial number specified");
1846 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1848 return ERROR_JTAG_INIT_FAILED;
1851 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1852 if (status != FT_OK) {
1853 /* under Win32, the FTD2XX driver appends an "A" to the end
1854 * of the description, if we tried by the desc, then
1855 * try by the alternate "A" description. */
1856 if (openex_string == ft2232_device_desc) {
1857 /* Try the alternate method. */
1858 openex_string = ft2232_device_desc_A;
1859 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1860 if (status == FT_OK) {
1861 /* yea, the "alternate" method worked! */
1862 } else {
1863 /* drat, give the user a meaningfull message.
1864 * telling the use we tried *BOTH* methods. */
1865 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1866 ft2232_device_desc,
1867 ft2232_device_desc_A);
1872 if (status != FT_OK)
1874 DWORD num_devices;
1876 if (more)
1878 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1879 *try_more = 1;
1880 return ERROR_JTAG_INIT_FAILED;
1882 LOG_ERROR("unable to open ftdi device: %lu", status);
1883 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1884 if (status == FT_OK)
1886 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1887 uint32_t i;
1889 for (i = 0; i < num_devices; i++)
1890 desc_array[i] = malloc(64);
1892 desc_array[num_devices] = NULL;
1894 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1896 if (status == FT_OK)
1898 LOG_ERROR("ListDevices: %lu\n", num_devices);
1899 for (i = 0; i < num_devices; i++)
1900 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1903 for (i = 0; i < num_devices; i++)
1904 free(desc_array[i]);
1906 free(desc_array);
1908 else
1910 LOG_ERROR("ListDevices: NONE\n");
1912 return ERROR_JTAG_INIT_FAILED;
1915 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1917 LOG_ERROR("unable to set latency timer: %lu", status);
1918 return ERROR_JTAG_INIT_FAILED;
1921 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1923 LOG_ERROR("unable to get latency timer: %lu", status);
1924 return ERROR_JTAG_INIT_FAILED;
1926 else
1928 LOG_DEBUG("current latency timer: %i", latency_timer);
1931 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1933 LOG_ERROR("unable to set timeouts: %lu", status);
1934 return ERROR_JTAG_INIT_FAILED;
1937 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1939 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1940 return ERROR_JTAG_INIT_FAILED;
1943 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1945 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1946 return ERROR_JTAG_INIT_FAILED;
1948 else
1950 static const char* type_str[] =
1951 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1952 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
1953 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
1954 ? ftdi_device : FT_DEVICE_UNKNOWN;
1955 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
1956 LOG_INFO("deviceID: %lu", deviceID);
1957 LOG_INFO("SerialNumber: %s", SerialNumber);
1958 LOG_INFO("Description: %s", Description);
1961 return ERROR_OK;
1964 static int ft2232_purge_ftd2xx(void)
1966 FT_STATUS status;
1968 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1970 LOG_ERROR("error purging ftd2xx device: %lu", status);
1971 return ERROR_JTAG_INIT_FAILED;
1974 return ERROR_OK;
1977 #endif /* BUILD_FT2232_FTD2XX == 1 */
1979 #if BUILD_FT2232_LIBFTDI == 1
1980 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
1982 uint8_t latency_timer;
1984 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1985 ft2232_layout, vid, pid);
1987 if (ftdi_init(&ftdic) < 0)
1988 return ERROR_JTAG_INIT_FAILED;
1990 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1992 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1993 return ERROR_JTAG_INIT_FAILED;
1996 /* context, vendor id, product id */
1997 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1998 ft2232_serial) < 0)
2000 if (more)
2001 LOG_WARNING("unable to open ftdi device (trying more): %s",
2002 ftdic.error_str);
2003 else
2004 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2005 *try_more = 1;
2006 return ERROR_JTAG_INIT_FAILED;
2009 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2010 if (ftdi_usb_reset(&ftdic) < 0)
2012 LOG_ERROR("unable to reset ftdi device");
2013 return ERROR_JTAG_INIT_FAILED;
2016 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2018 LOG_ERROR("unable to set latency timer");
2019 return ERROR_JTAG_INIT_FAILED;
2022 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2024 LOG_ERROR("unable to get latency timer");
2025 return ERROR_JTAG_INIT_FAILED;
2027 else
2029 LOG_DEBUG("current latency timer: %i", latency_timer);
2032 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2034 ftdi_device = ftdic.type;
2035 static const char* type_str[] =
2036 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2037 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2038 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2039 ? ftdi_device : no_of_known_types;
2040 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2041 return ERROR_OK;
2044 static int ft2232_purge_libftdi(void)
2046 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2048 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2049 return ERROR_JTAG_INIT_FAILED;
2052 return ERROR_OK;
2055 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2057 static int ft2232_init(void)
2059 uint8_t buf[1];
2060 int retval;
2061 uint32_t bytes_written;
2062 const struct ft2232_layout* cur_layout = ft2232_layouts;
2063 int i;
2065 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2067 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2069 else
2071 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2074 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2076 ft2232_layout = "usbjtag";
2077 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2080 while (cur_layout->name)
2082 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2084 layout = cur_layout;
2085 break;
2087 cur_layout++;
2090 if (!layout)
2092 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2093 return ERROR_JTAG_INIT_FAILED;
2096 for (i = 0; 1; i++)
2099 * "more indicates that there are more IDs to try, so we should
2100 * not print an error for an ID mismatch (but for anything
2101 * else, we should).
2103 * try_more indicates that the error code returned indicates an
2104 * ID mismatch (and nothing else) and that we should proceeed
2105 * with the next ID pair.
2107 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2108 int try_more = 0;
2110 #if BUILD_FT2232_FTD2XX == 1
2111 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2112 more, &try_more);
2113 #elif BUILD_FT2232_LIBFTDI == 1
2114 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2115 more, &try_more);
2116 #endif
2117 if (retval >= 0)
2118 break;
2119 if (!more || !try_more)
2120 return retval;
2123 ft2232_buffer_size = 0;
2124 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2126 if (layout->init() != ERROR_OK)
2127 return ERROR_JTAG_INIT_FAILED;
2129 if (ft2232_device_is_highspeed())
2131 #ifndef BUILD_FT2232_HIGHSPEED
2132 #if BUILD_FT2232_FTD2XX == 1
2133 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2134 #elif BUILD_FT2232_LIBFTDI == 1
2135 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2136 #endif
2137 #endif
2138 /* make sure the legacy mode is disabled */
2139 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2140 return ERROR_JTAG_INIT_FAILED;
2143 ft2232_speed(jtag_get_speed());
2145 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2146 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2148 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2149 return ERROR_JTAG_INIT_FAILED;
2152 #if BUILD_FT2232_FTD2XX == 1
2153 return ft2232_purge_ftd2xx();
2154 #elif BUILD_FT2232_LIBFTDI == 1
2155 return ft2232_purge_libftdi();
2156 #endif
2158 return ERROR_OK;
2161 static int usbjtag_init(void)
2163 uint8_t buf[3];
2164 uint32_t bytes_written;
2166 low_output = 0x08;
2167 low_direction = 0x0b;
2169 if (strcmp(ft2232_layout, "usbjtag") == 0)
2171 nTRST = 0x10;
2172 nTRSTnOE = 0x10;
2173 nSRST = 0x40;
2174 nSRSTnOE = 0x40;
2176 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2178 nTRST = 0x10;
2179 nTRSTnOE = 0x10;
2180 nSRST = 0x20;
2181 nSRSTnOE = 0x20;
2183 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2185 nTRST = 0x0;
2186 nTRSTnOE = 0x00;
2187 nSRST = 0x20;
2188 nSRSTnOE = 0x20;
2189 low_output = 0x88;
2190 low_direction = 0x8b;
2192 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2194 nTRST = 0x0;
2195 nTRSTnOE = 0x00;
2196 nSRST = 0x20;
2197 nSRSTnOE = 0x20;
2198 low_output = 0x88;
2199 low_direction = 0xcb;
2201 else
2203 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2204 return ERROR_JTAG_INIT_FAILED;
2207 enum reset_types jtag_reset_config = jtag_get_reset_config();
2208 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2210 low_direction &= ~nTRSTnOE; /* nTRST input */
2211 low_output &= ~nTRST; /* nTRST = 0 */
2213 else
2215 low_direction |= nTRSTnOE; /* nTRST output */
2216 low_output |= nTRST; /* nTRST = 1 */
2219 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2221 low_direction |= nSRSTnOE; /* nSRST output */
2222 low_output |= nSRST; /* nSRST = 1 */
2224 else
2226 low_direction &= ~nSRSTnOE; /* nSRST input */
2227 low_output &= ~nSRST; /* nSRST = 0 */
2230 /* initialize low byte for jtag */
2231 buf[0] = 0x80; /* command "set data bits low byte" */
2232 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2233 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2234 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2236 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2238 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2239 return ERROR_JTAG_INIT_FAILED;
2242 return ERROR_OK;
2245 static int axm0432_jtag_init(void)
2247 uint8_t buf[3];
2248 uint32_t bytes_written;
2250 low_output = 0x08;
2251 low_direction = 0x2b;
2253 /* initialize low byte for jtag */
2254 buf[0] = 0x80; /* command "set data bits low byte" */
2255 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2256 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2257 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2259 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2261 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2262 return ERROR_JTAG_INIT_FAILED;
2265 if (strcmp(layout->name, "axm0432_jtag") == 0)
2267 nTRST = 0x08;
2268 nTRSTnOE = 0x0; /* No output enable for TRST*/
2269 nSRST = 0x04;
2270 nSRSTnOE = 0x0; /* No output enable for SRST*/
2272 else
2274 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2275 exit(-1);
2278 high_output = 0x0;
2279 high_direction = 0x0c;
2281 enum reset_types jtag_reset_config = jtag_get_reset_config();
2282 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2284 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2286 else
2288 high_output |= nTRST;
2291 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2293 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2295 else
2297 high_output |= nSRST;
2300 /* initialize high port */
2301 buf[0] = 0x82; /* command "set data bits high byte" */
2302 buf[1] = high_output; /* value */
2303 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2304 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2306 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2308 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2309 return ERROR_JTAG_INIT_FAILED;
2312 return ERROR_OK;
2315 static int jtagkey_init(void)
2317 uint8_t buf[3];
2318 uint32_t bytes_written;
2320 low_output = 0x08;
2321 low_direction = 0x1b;
2323 /* initialize low byte for jtag */
2324 buf[0] = 0x80; /* command "set data bits low byte" */
2325 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2326 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2327 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2329 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2331 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2332 return ERROR_JTAG_INIT_FAILED;
2335 if (strcmp(layout->name, "jtagkey") == 0)
2337 nTRST = 0x01;
2338 nTRSTnOE = 0x4;
2339 nSRST = 0x02;
2340 nSRSTnOE = 0x08;
2342 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2343 || (strcmp(layout->name, "oocdlink") == 0))
2345 nTRST = 0x02;
2346 nTRSTnOE = 0x1;
2347 nSRST = 0x08;
2348 nSRSTnOE = 0x04;
2350 else
2352 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2353 exit(-1);
2356 high_output = 0x0;
2357 high_direction = 0x0f;
2359 enum reset_types jtag_reset_config = jtag_get_reset_config();
2360 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2362 high_output |= nTRSTnOE;
2363 high_output &= ~nTRST;
2365 else
2367 high_output &= ~nTRSTnOE;
2368 high_output |= nTRST;
2371 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2373 high_output &= ~nSRSTnOE;
2374 high_output |= nSRST;
2376 else
2378 high_output |= nSRSTnOE;
2379 high_output &= ~nSRST;
2382 /* initialize high port */
2383 buf[0] = 0x82; /* command "set data bits high byte" */
2384 buf[1] = high_output; /* value */
2385 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2386 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2388 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2390 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2391 return ERROR_JTAG_INIT_FAILED;
2394 return ERROR_OK;
2397 static int olimex_jtag_init(void)
2399 uint8_t buf[3];
2400 uint32_t bytes_written;
2402 low_output = 0x08;
2403 low_direction = 0x1b;
2405 /* initialize low byte for jtag */
2406 buf[0] = 0x80; /* command "set data bits low byte" */
2407 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2408 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2409 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2411 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2413 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2414 return ERROR_JTAG_INIT_FAILED;
2417 nTRST = 0x01;
2418 nTRSTnOE = 0x4;
2419 nSRST = 0x02;
2420 nSRSTnOE = 0x00; /* no output enable for nSRST */
2422 high_output = 0x0;
2423 high_direction = 0x0f;
2425 enum reset_types jtag_reset_config = jtag_get_reset_config();
2426 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2428 high_output |= nTRSTnOE;
2429 high_output &= ~nTRST;
2431 else
2433 high_output &= ~nTRSTnOE;
2434 high_output |= nTRST;
2437 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2439 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2441 else
2443 high_output &= ~nSRST;
2446 /* turn red LED on */
2447 high_output |= 0x08;
2449 /* initialize high port */
2450 buf[0] = 0x82; /* command "set data bits high byte" */
2451 buf[1] = high_output; /* value */
2452 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2453 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2455 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2457 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2458 return ERROR_JTAG_INIT_FAILED;
2461 return ERROR_OK;
2464 static int flyswatter_init(void)
2466 uint8_t buf[3];
2467 uint32_t bytes_written;
2469 low_output = 0x18;
2470 low_direction = 0xfb;
2472 /* initialize low byte for jtag */
2473 buf[0] = 0x80; /* command "set data bits low byte" */
2474 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2475 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2476 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2478 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2480 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2481 return ERROR_JTAG_INIT_FAILED;
2484 nTRST = 0x10;
2485 nTRSTnOE = 0x0; /* not output enable for nTRST */
2486 nSRST = 0x20;
2487 nSRSTnOE = 0x00; /* no output enable for nSRST */
2489 high_output = 0x00;
2490 high_direction = 0x0c;
2492 /* turn red LED3 on, LED2 off */
2493 high_output |= 0x08;
2495 /* initialize high port */
2496 buf[0] = 0x82; /* command "set data bits high byte" */
2497 buf[1] = high_output; /* value */
2498 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2499 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2501 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2503 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2504 return ERROR_JTAG_INIT_FAILED;
2507 return ERROR_OK;
2510 static int turtle_init(void)
2512 uint8_t buf[3];
2513 uint32_t bytes_written;
2515 low_output = 0x08;
2516 low_direction = 0x5b;
2518 /* initialize low byte for jtag */
2519 buf[0] = 0x80; /* command "set data bits low byte" */
2520 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2521 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2522 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2524 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2526 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2527 return ERROR_JTAG_INIT_FAILED;
2530 nSRST = 0x40;
2532 high_output = 0x00;
2533 high_direction = 0x0C;
2535 /* initialize high port */
2536 buf[0] = 0x82; /* command "set data bits high byte" */
2537 buf[1] = high_output;
2538 buf[2] = high_direction;
2539 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2541 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2543 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2544 return ERROR_JTAG_INIT_FAILED;
2547 return ERROR_OK;
2550 static int comstick_init(void)
2552 uint8_t buf[3];
2553 uint32_t bytes_written;
2555 low_output = 0x08;
2556 low_direction = 0x0b;
2558 /* initialize low byte for jtag */
2559 buf[0] = 0x80; /* command "set data bits low byte" */
2560 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2561 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2562 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2564 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2566 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2567 return ERROR_JTAG_INIT_FAILED;
2570 nTRST = 0x01;
2571 nTRSTnOE = 0x00; /* no output enable for nTRST */
2572 nSRST = 0x02;
2573 nSRSTnOE = 0x00; /* no output enable for nSRST */
2575 high_output = 0x03;
2576 high_direction = 0x03;
2578 /* initialize high port */
2579 buf[0] = 0x82; /* command "set data bits high byte" */
2580 buf[1] = high_output;
2581 buf[2] = high_direction;
2582 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2584 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2586 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2587 return ERROR_JTAG_INIT_FAILED;
2590 return ERROR_OK;
2593 static int stm32stick_init(void)
2595 uint8_t buf[3];
2596 uint32_t bytes_written;
2598 low_output = 0x88;
2599 low_direction = 0x8b;
2601 /* initialize low byte for jtag */
2602 buf[0] = 0x80; /* command "set data bits low byte" */
2603 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2604 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2605 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2607 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2609 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2610 return ERROR_JTAG_INIT_FAILED;
2613 nTRST = 0x01;
2614 nTRSTnOE = 0x00; /* no output enable for nTRST */
2615 nSRST = 0x80;
2616 nSRSTnOE = 0x00; /* no output enable for nSRST */
2618 high_output = 0x01;
2619 high_direction = 0x03;
2621 /* initialize high port */
2622 buf[0] = 0x82; /* command "set data bits high byte" */
2623 buf[1] = high_output;
2624 buf[2] = high_direction;
2625 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2627 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2629 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2630 return ERROR_JTAG_INIT_FAILED;
2633 return ERROR_OK;
2636 static int sheevaplug_init(void)
2638 uint8_t buf[3];
2639 uint32_t bytes_written;
2641 low_output = 0x08;
2642 low_direction = 0x1b;
2644 /* initialize low byte for jtag */
2645 buf[0] = 0x80; /* command "set data bits low byte" */
2646 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2647 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2648 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2650 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2652 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2653 return ERROR_JTAG_INIT_FAILED;
2656 nTRSTnOE = 0x1;
2657 nTRST = 0x02;
2658 nSRSTnOE = 0x4;
2659 nSRST = 0x08;
2661 high_output = 0x0;
2662 high_direction = 0x0f;
2664 /* nTRST is always push-pull */
2665 high_output &= ~nTRSTnOE;
2666 high_output |= nTRST;
2668 /* nSRST is always open-drain */
2669 high_output |= nSRSTnOE;
2670 high_output &= ~nSRST;
2672 /* initialize high port */
2673 buf[0] = 0x82; /* command "set data bits high byte" */
2674 buf[1] = high_output; /* value */
2675 buf[2] = high_direction; /* all outputs - xRST */
2676 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2678 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2680 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2681 return ERROR_JTAG_INIT_FAILED;
2684 return ERROR_OK;
2687 static int cortino_jtag_init(void)
2689 uint8_t buf[3];
2690 uint32_t bytes_written;
2692 low_output = 0x08;
2693 low_direction = 0x1b;
2695 /* initialize low byte for jtag */
2696 buf[0] = 0x80; /* command "set data bits low byte" */
2697 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2698 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2699 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2701 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2703 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2704 return ERROR_JTAG_INIT_FAILED;
2707 nTRST = 0x01;
2708 nTRSTnOE = 0x00; /* no output enable for nTRST */
2709 nSRST = 0x02;
2710 nSRSTnOE = 0x00; /* no output enable for nSRST */
2712 high_output = 0x03;
2713 high_direction = 0x03;
2715 /* initialize high port */
2716 buf[0] = 0x82; /* command "set data bits high byte" */
2717 buf[1] = high_output;
2718 buf[2] = high_direction;
2719 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2721 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2723 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2724 return ERROR_JTAG_INIT_FAILED;
2727 return ERROR_OK;
2730 static void olimex_jtag_blink(void)
2732 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2733 * ACBUS3 is bit 3 of the GPIOH port
2735 if (high_output & 0x08)
2737 /* set port pin high */
2738 high_output &= 0x07;
2740 else
2742 /* set port pin low */
2743 high_output |= 0x08;
2746 buffer_write(0x82);
2747 buffer_write(high_output);
2748 buffer_write(high_direction);
2751 static void flyswatter_jtag_blink(void)
2754 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2756 high_output ^= 0x0c;
2758 buffer_write(0x82);
2759 buffer_write(high_output);
2760 buffer_write(high_direction);
2763 static void turtle_jtag_blink(void)
2766 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2768 if (high_output & 0x08)
2770 high_output = 0x04;
2772 else
2774 high_output = 0x08;
2777 buffer_write(0x82);
2778 buffer_write(high_output);
2779 buffer_write(high_direction);
2782 static int ft2232_quit(void)
2784 #if BUILD_FT2232_FTD2XX == 1
2785 FT_STATUS status;
2787 status = FT_Close(ftdih);
2788 #elif BUILD_FT2232_LIBFTDI == 1
2789 ftdi_usb_close(&ftdic);
2791 ftdi_deinit(&ftdic);
2792 #endif
2794 free(ft2232_buffer);
2795 ft2232_buffer = NULL;
2797 return ERROR_OK;
2800 COMMAND_HANDLER(ft2232_handle_device_desc_command)
2802 char *cp;
2803 char buf[200];
2804 if (CMD_ARGC == 1)
2806 ft2232_device_desc = strdup(CMD_ARGV[0]);
2807 cp = strchr(ft2232_device_desc, 0);
2808 /* under Win32, the FTD2XX driver appends an "A" to the end
2809 * of the description, this examines the given desc
2810 * and creates the 'missing' _A or non_A variable. */
2811 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2812 /* it was, so make this the "A" version. */
2813 ft2232_device_desc_A = ft2232_device_desc;
2814 /* and *CREATE* the non-A version. */
2815 strcpy(buf, ft2232_device_desc);
2816 cp = strchr(buf, 0);
2817 cp[-2] = 0;
2818 ft2232_device_desc = strdup(buf);
2819 } else {
2820 /* <space > A not defined
2821 * so create it */
2822 sprintf(buf, "%s A", ft2232_device_desc);
2823 ft2232_device_desc_A = strdup(buf);
2826 else
2828 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2831 return ERROR_OK;
2834 COMMAND_HANDLER(ft2232_handle_serial_command)
2836 if (CMD_ARGC == 1)
2838 ft2232_serial = strdup(CMD_ARGV[0]);
2840 else
2842 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2845 return ERROR_OK;
2848 COMMAND_HANDLER(ft2232_handle_layout_command)
2850 if (CMD_ARGC == 0)
2851 return ERROR_OK;
2853 ft2232_layout = malloc(strlen(CMD_ARGV[0]) + 1);
2854 strcpy(ft2232_layout, CMD_ARGV[0]);
2856 return ERROR_OK;
2859 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
2861 if (CMD_ARGC > MAX_USB_IDS * 2)
2863 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2864 "(maximum is %d pairs)", MAX_USB_IDS);
2865 CMD_ARGC = MAX_USB_IDS * 2;
2867 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
2869 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2870 if (CMD_ARGC < 2)
2871 return ERROR_COMMAND_SYNTAX_ERROR;
2872 /* remove the incomplete trailing id */
2873 CMD_ARGC -= 1;
2876 unsigned i;
2877 for (i = 0; i < CMD_ARGC; i += 2)
2879 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
2880 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
2884 * Explicitly terminate, in case there are multiples instances of
2885 * ft2232_vid_pid.
2887 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2889 return ERROR_OK;
2892 COMMAND_HANDLER(ft2232_handle_latency_command)
2894 if (CMD_ARGC == 1)
2896 ft2232_latency = atoi(CMD_ARGV[0]);
2898 else
2900 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2903 return ERROR_OK;
2906 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
2908 int retval = 0;
2910 /* 7 bits of either ones or zeros. */
2911 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2913 while (num_cycles > 0)
2915 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2916 * at most 7 bits per invocation. Here we invoke it potentially
2917 * several times.
2919 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2921 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2923 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2924 retval = ERROR_JTAG_QUEUE_FAILED;
2926 first_unsent = cmd;
2929 /* there are no state transitions in this code, so omit state tracking */
2931 /* command "Clock Data to TMS/CS Pin (no Read)" */
2932 buffer_write(0x4b);
2934 /* scan 7 bit */
2935 buffer_write(bitcount_per_command - 1);
2937 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2938 buffer_write(tms);
2940 require_send = 1;
2942 num_cycles -= bitcount_per_command;
2945 return retval;
2948 /* ---------------------------------------------------------------------
2949 * Support for IceBear JTAG adapter from Section5:
2950 * http://section5.ch/icebear
2952 * Author: Sten, debian@sansys-electronic.com
2955 /* Icebear pin layout
2957 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2958 * GND GND | 4 3| n.c.
2959 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2960 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2961 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2962 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2963 * ADBUS2 TDO |14 13| GND GND
2965 * ADBUS0 O L TCK ACBUS0 GND
2966 * ADBUS1 O L TDI ACBUS1 GND
2967 * ADBUS2 I TDO ACBUS2 n.c.
2968 * ADBUS3 O H TMS ACBUS3 n.c.
2969 * ADBUS4 O H nTRST
2970 * ADBUS5 O H nSRST
2971 * ADBUS6 - VCC
2972 * ADBUS7 - GND
2974 static int icebear_jtag_init(void) {
2975 uint8_t buf[3];
2976 uint32_t bytes_written;
2978 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2979 low_output = 0x08; /* high: TMS; low: TCK TDI */
2980 nTRST = 0x10;
2981 nSRST = 0x20;
2983 enum reset_types jtag_reset_config = jtag_get_reset_config();
2984 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2985 low_direction &= ~nTRST; /* nTRST high impedance */
2987 else {
2988 low_direction |= nTRST;
2989 low_output |= nTRST;
2992 low_direction |= nSRST;
2993 low_output |= nSRST;
2995 /* initialize low byte for jtag */
2996 buf[0] = 0x80; /* command "set data bits low byte" */
2997 buf[1] = low_output;
2998 buf[2] = low_direction;
2999 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3001 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3002 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3003 return ERROR_JTAG_INIT_FAILED;
3006 high_output = 0x0;
3007 high_direction = 0x00;
3010 /* initialize high port */
3011 buf[0] = 0x82; /* command "set data bits high byte" */
3012 buf[1] = high_output; /* value */
3013 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3014 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3016 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3017 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3018 return ERROR_JTAG_INIT_FAILED;
3021 return ERROR_OK;
3024 static void icebear_jtag_reset(int trst, int srst) {
3026 if (trst == 1) {
3027 low_direction |= nTRST;
3028 low_output &= ~nTRST;
3030 else if (trst == 0) {
3031 enum reset_types jtag_reset_config = jtag_get_reset_config();
3032 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3033 low_direction &= ~nTRST;
3034 else
3035 low_output |= nTRST;
3038 if (srst == 1) {
3039 low_output &= ~nSRST;
3041 else if (srst == 0) {
3042 low_output |= nSRST;
3045 /* command "set data bits low byte" */
3046 buffer_write(0x80);
3047 buffer_write(low_output);
3048 buffer_write(low_direction);
3050 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3053 /* ---------------------------------------------------------------------
3054 * Support for Signalyzer H2 and Signalyzer H4
3055 * JTAG adapter from Xverve Technologies Inc.
3056 * http://www.signalyzer.com or http://www.xverve.com
3058 * Author: Oleg Seiljus, oleg@signalyzer.com
3060 static unsigned char signalyzer_h_side;
3061 static unsigned int signalyzer_h_adapter_type;
3063 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3065 #if BUILD_FT2232_FTD2XX == 1
3066 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3067 #endif
3069 #define SIGNALYZER_COMMAND_ADDR 128
3070 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3072 #define SIGNALYZER_COMMAND_VERSION 0x41
3073 #define SIGNALYZER_COMMAND_RESET 0x42
3074 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3075 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3076 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3077 #define SIGNALYZER_COMMAND_LED_SET 0x53
3078 #define SIGNALYZER_COMMAND_ADC 0x54
3079 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3080 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3081 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3082 #define SIGNALYZER_COMMAND_I2C 0x58
3084 #define SIGNALYZER_CHAN_A 1
3085 #define SIGNALYZER_CHAN_B 2
3086 /* LEDS use channel C */
3087 #define SIGNALYZER_CHAN_C 4
3089 #define SIGNALYZER_LED_GREEN 1
3090 #define SIGNALYZER_LED_RED 2
3092 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3093 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3094 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3095 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3096 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3099 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3101 #if BUILD_FT2232_FTD2XX == 1
3102 return FT_WriteEE(ftdih, address, value);
3103 #elif BUILD_FT2232_LIBFTDI == 1
3104 return 0;
3105 #endif
3108 #if BUILD_FT2232_FTD2XX == 1
3109 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3111 return FT_ReadEE(ftdih, address, value);
3113 #endif
3115 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3116 int on_time_ms, int off_time_ms, unsigned char cycles)
3118 unsigned char on_time;
3119 unsigned char off_time;
3121 if (on_time_ms < 0xFFFF)
3122 on_time = (unsigned char)(on_time_ms / 62);
3123 else
3124 on_time = 0xFF;
3126 off_time = (unsigned char)(off_time_ms / 62);
3128 #if BUILD_FT2232_FTD2XX == 1
3129 FT_STATUS status;
3131 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3132 ((uint32_t)(channel << 8) | led))) != FT_OK)
3134 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3135 return ERROR_JTAG_DEVICE_ERROR;
3138 if ((status = signalyzer_h_ctrl_write(
3139 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3140 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3142 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3143 return ERROR_JTAG_DEVICE_ERROR;
3146 if ((status = signalyzer_h_ctrl_write(
3147 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3148 ((uint32_t)cycles))) != FT_OK)
3150 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3151 return ERROR_JTAG_DEVICE_ERROR;
3154 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3155 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3157 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3158 return ERROR_JTAG_DEVICE_ERROR;
3161 return ERROR_OK;
3162 #elif BUILD_FT2232_LIBFTDI == 1
3163 int retval;
3165 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3166 ((uint32_t)(channel << 8) | led))) < 0)
3168 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3169 ftdi_get_error_string(&ftdic));
3170 return ERROR_JTAG_DEVICE_ERROR;
3173 if ((retval = signalyzer_h_ctrl_write(
3174 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3175 ((uint32_t)(on_time << 8) | off_time))) < 0)
3177 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3178 ftdi_get_error_string(&ftdic));
3179 return ERROR_JTAG_DEVICE_ERROR;
3182 if ((retval = signalyzer_h_ctrl_write(
3183 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3184 (uint32_t)cycles)) < 0)
3186 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3187 ftdi_get_error_string(&ftdic));
3188 return ERROR_JTAG_DEVICE_ERROR;
3191 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3192 SIGNALYZER_COMMAND_LED_SET)) < 0)
3194 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3195 ftdi_get_error_string(&ftdic));
3196 return ERROR_JTAG_DEVICE_ERROR;
3199 return ERROR_OK;
3200 #endif
3203 static int signalyzer_h_init(void)
3205 #if BUILD_FT2232_FTD2XX == 1
3206 FT_STATUS status;
3207 int i;
3208 #endif
3210 char *end_of_desc;
3212 uint16_t read_buf[12] = { 0 };
3213 uint8_t buf[3];
3214 uint32_t bytes_written;
3216 /* turn on center green led */
3217 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3218 0xFFFF, 0x00, 0x00);
3220 /* determine what channel config wants to open
3221 * TODO: change me... current implementation is made to work
3222 * with openocd description parsing.
3224 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3226 if (end_of_desc)
3228 signalyzer_h_side = *(end_of_desc - 1);
3229 if (signalyzer_h_side == 'B')
3230 signalyzer_h_side = SIGNALYZER_CHAN_B;
3231 else
3232 signalyzer_h_side = SIGNALYZER_CHAN_A;
3234 else
3236 LOG_ERROR("No Channel was specified");
3237 return ERROR_FAIL;
3240 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3241 1000, 1000, 0xFF);
3243 #if BUILD_FT2232_FTD2XX == 1
3244 /* read signalyzer versionining information */
3245 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3246 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3248 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3249 return ERROR_JTAG_DEVICE_ERROR;
3252 for (i = 0; i < 10; i++)
3254 if ((status = signalyzer_h_ctrl_read(
3255 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3256 &read_buf[i])) != FT_OK)
3258 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3259 status);
3260 return ERROR_JTAG_DEVICE_ERROR;
3264 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3265 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3266 read_buf[4], read_buf[5], read_buf[6]);
3268 /* set gpio register */
3269 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3270 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3272 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3273 return ERROR_JTAG_DEVICE_ERROR;
3276 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3277 0x0404)) != FT_OK)
3279 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3280 return ERROR_JTAG_DEVICE_ERROR;
3283 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3284 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3286 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3287 return ERROR_JTAG_DEVICE_ERROR;
3290 /* read adapter type information */
3291 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3292 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3294 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3295 return ERROR_JTAG_DEVICE_ERROR;
3298 if ((status = signalyzer_h_ctrl_write(
3299 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3301 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3302 return ERROR_JTAG_DEVICE_ERROR;
3305 if ((status = signalyzer_h_ctrl_write(
3306 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3308 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3309 return ERROR_JTAG_DEVICE_ERROR;
3312 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3313 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3315 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3316 return ERROR_JTAG_DEVICE_ERROR;
3319 usleep(100000);
3321 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3322 &read_buf[0])) != FT_OK)
3324 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3325 return ERROR_JTAG_DEVICE_ERROR;
3328 if (read_buf[0] != 0x0498)
3329 signalyzer_h_adapter_type = 0x0000;
3330 else
3332 for (i = 0; i < 4; i++)
3334 if ((status = signalyzer_h_ctrl_read(
3335 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3336 &read_buf[i])) != FT_OK)
3338 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3339 status);
3340 return ERROR_JTAG_DEVICE_ERROR;
3344 signalyzer_h_adapter_type = read_buf[0];
3347 #elif BUILD_FT2232_LIBFTDI == 1
3348 /* currently libftdi does not allow reading individual eeprom
3349 * locations, therefore adapter type cannot be detected.
3350 * override with most common type
3352 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3353 #endif
3355 enum reset_types jtag_reset_config = jtag_get_reset_config();
3357 /* ADAPTOR: EM_LT16_A */
3358 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3360 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3361 "detected. (HW: %2x).", (read_buf[1] >> 8));
3363 nTRST = 0x10;
3364 nTRSTnOE = 0x10;
3365 nSRST = 0x20;
3366 nSRSTnOE = 0x20;
3368 low_output = 0x08;
3369 low_direction = 0x1b;
3371 high_output = 0x0;
3372 high_direction = 0x0;
3374 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3376 low_direction &= ~nTRSTnOE; /* nTRST input */
3377 low_output &= ~nTRST; /* nTRST = 0 */
3379 else
3381 low_direction |= nTRSTnOE; /* nTRST output */
3382 low_output |= nTRST; /* nTRST = 1 */
3385 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3387 low_direction |= nSRSTnOE; /* nSRST output */
3388 low_output |= nSRST; /* nSRST = 1 */
3390 else
3392 low_direction &= ~nSRSTnOE; /* nSRST input */
3393 low_output &= ~nSRST; /* nSRST = 0 */
3396 #if BUILD_FT2232_FTD2XX == 1
3397 /* enable power to the module */
3398 if ((status = signalyzer_h_ctrl_write(
3399 SIGNALYZER_DATA_BUFFER_ADDR,
3400 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3401 != FT_OK)
3403 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3404 status);
3405 return ERROR_JTAG_DEVICE_ERROR;
3408 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3409 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3411 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3412 status);
3413 return ERROR_JTAG_DEVICE_ERROR;
3416 /* set gpio mode register */
3417 if ((status = signalyzer_h_ctrl_write(
3418 SIGNALYZER_DATA_BUFFER_ADDR,
3419 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3421 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3422 status);
3423 return ERROR_JTAG_DEVICE_ERROR;
3426 if ((status = signalyzer_h_ctrl_write(
3427 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3428 != FT_OK)
3430 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3431 status);
3432 return ERROR_JTAG_DEVICE_ERROR;
3435 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3436 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3438 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3439 status);
3440 return ERROR_JTAG_DEVICE_ERROR;
3443 /* set gpio register */
3444 if ((status = signalyzer_h_ctrl_write(
3445 SIGNALYZER_DATA_BUFFER_ADDR,
3446 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3448 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3449 status);
3450 return ERROR_JTAG_DEVICE_ERROR;
3453 if ((status = signalyzer_h_ctrl_write(
3454 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3455 != FT_OK)
3457 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3458 status);
3459 return ERROR_JTAG_DEVICE_ERROR;
3462 if ((status = signalyzer_h_ctrl_write(
3463 SIGNALYZER_COMMAND_ADDR,
3464 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3466 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3467 status);
3468 return ERROR_JTAG_DEVICE_ERROR;
3470 #endif
3473 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3474 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3475 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3476 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3477 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3479 if (signalyzer_h_adapter_type
3480 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3481 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3482 "detected. (HW: %2x).", (read_buf[1] >> 8));
3483 else if (signalyzer_h_adapter_type
3484 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3485 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3486 "(ARM JTAG with PSU) detected. (HW: %2x).",
3487 (read_buf[1] >> 8));
3488 else if (signalyzer_h_adapter_type
3489 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3490 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3491 "detected. (HW: %2x).", (read_buf[1] >> 8));
3492 else if (signalyzer_h_adapter_type
3493 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3494 LOG_INFO("Signalyzer: EM-JTAG-P "
3495 "(Generic JTAG with PSU) detected. (HW: %2x).",
3496 (read_buf[1] >> 8));
3498 nTRST = 0x02;
3499 nTRSTnOE = 0x04;
3500 nSRST = 0x08;
3501 nSRSTnOE = 0x10;
3503 low_output = 0x08;
3504 low_direction = 0x1b;
3506 high_output = 0x0;
3507 high_direction = 0x1f;
3509 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3511 high_output |= nTRSTnOE;
3512 high_output &= ~nTRST;
3514 else
3516 high_output &= ~nTRSTnOE;
3517 high_output |= nTRST;
3520 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3522 high_output &= ~nSRSTnOE;
3523 high_output |= nSRST;
3525 else
3527 high_output |= nSRSTnOE;
3528 high_output &= ~nSRST;
3531 #if BUILD_FT2232_FTD2XX == 1
3532 /* enable power to the module */
3533 if ((status = signalyzer_h_ctrl_write(
3534 SIGNALYZER_DATA_BUFFER_ADDR,
3535 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3536 != FT_OK)
3538 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3539 status);
3540 return ERROR_JTAG_DEVICE_ERROR;
3543 if ((status = signalyzer_h_ctrl_write(
3544 SIGNALYZER_COMMAND_ADDR,
3545 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3547 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3548 status);
3549 return ERROR_JTAG_DEVICE_ERROR;
3552 /* set gpio mode register (IO_16 and IO_17 set as analog
3553 * inputs, other is gpio)
3555 if ((status = signalyzer_h_ctrl_write(
3556 SIGNALYZER_DATA_BUFFER_ADDR,
3557 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3559 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3560 status);
3561 return ERROR_JTAG_DEVICE_ERROR;
3564 if ((status = signalyzer_h_ctrl_write(
3565 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3566 != FT_OK)
3568 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3569 status);
3570 return ERROR_JTAG_DEVICE_ERROR;
3573 if ((status = signalyzer_h_ctrl_write(
3574 SIGNALYZER_COMMAND_ADDR,
3575 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3577 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3578 status);
3579 return ERROR_JTAG_DEVICE_ERROR;
3582 /* set gpio register (all inputs, for -P modules,
3583 * PSU will be turned off)
3585 if ((status = signalyzer_h_ctrl_write(
3586 SIGNALYZER_DATA_BUFFER_ADDR,
3587 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3589 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3590 status);
3591 return ERROR_JTAG_DEVICE_ERROR;
3594 if ((status = signalyzer_h_ctrl_write(
3595 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3596 != FT_OK)
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3599 status);
3600 return ERROR_JTAG_DEVICE_ERROR;
3603 if ((status = signalyzer_h_ctrl_write(
3604 SIGNALYZER_COMMAND_ADDR,
3605 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3607 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3608 status);
3609 return ERROR_JTAG_DEVICE_ERROR;
3611 #endif
3614 else if (signalyzer_h_adapter_type == 0x0000)
3616 LOG_INFO("Signalyzer: No external modules were detected.");
3618 nTRST = 0x10;
3619 nTRSTnOE = 0x10;
3620 nSRST = 0x20;
3621 nSRSTnOE = 0x20;
3623 low_output = 0x08;
3624 low_direction = 0x1b;
3626 high_output = 0x0;
3627 high_direction = 0x0;
3629 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3631 low_direction &= ~nTRSTnOE; /* nTRST input */
3632 low_output &= ~nTRST; /* nTRST = 0 */
3634 else
3636 low_direction |= nTRSTnOE; /* nTRST output */
3637 low_output |= nTRST; /* nTRST = 1 */
3640 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3642 low_direction |= nSRSTnOE; /* nSRST output */
3643 low_output |= nSRST; /* nSRST = 1 */
3645 else
3647 low_direction &= ~nSRSTnOE; /* nSRST input */
3648 low_output &= ~nSRST; /* nSRST = 0 */
3651 else
3653 LOG_ERROR("Unknown module type is detected: %.4x",
3654 signalyzer_h_adapter_type);
3655 return ERROR_JTAG_DEVICE_ERROR;
3658 /* initialize low byte of controller for jtag operation */
3659 buf[0] = 0x80;
3660 buf[1] = low_output;
3661 buf[2] = low_direction;
3663 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
3664 || (bytes_written != 3))
3666 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3667 return ERROR_JTAG_INIT_FAILED;
3670 #if BUILD_FT2232_FTD2XX == 1
3671 if (ftdi_device == FT_DEVICE_2232H)
3673 /* initialize high byte of controller for jtag operation */
3674 buf[0] = 0x82;
3675 buf[1] = high_output;
3676 buf[2] = high_direction;
3678 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3679 || (bytes_written != 3))
3681 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3682 return ERROR_JTAG_INIT_FAILED;
3685 #elif BUILD_FT2232_LIBFTDI == 1
3686 if (ftdi_device == TYPE_2232H)
3688 /* initialize high byte of controller for jtag operation */
3689 buf[0] = 0x82;
3690 buf[1] = high_output;
3691 buf[2] = high_direction;
3693 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3694 || (bytes_written != 3))
3696 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3697 return ERROR_JTAG_INIT_FAILED;
3700 #endif
3701 return ERROR_OK;
3704 static void signalyzer_h_reset(int trst, int srst)
3706 enum reset_types jtag_reset_config = jtag_get_reset_config();
3708 /* ADAPTOR: EM_LT16_A */
3709 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3711 if (trst == 1)
3713 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3714 /* switch to output pin (output is low) */
3715 low_direction |= nTRSTnOE;
3716 else
3717 /* switch output low */
3718 low_output &= ~nTRST;
3720 else if (trst == 0)
3722 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3723 /* switch to input pin (high-Z + internal
3724 * and external pullup) */
3725 low_direction &= ~nTRSTnOE;
3726 else
3727 /* switch output high */
3728 low_output |= nTRST;
3731 if (srst == 1)
3733 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3734 /* switch output low */
3735 low_output &= ~nSRST;
3736 else
3737 /* switch to output pin (output is low) */
3738 low_direction |= nSRSTnOE;
3740 else if (srst == 0)
3742 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3743 /* switch output high */
3744 low_output |= nSRST;
3745 else
3746 /* switch to input pin (high-Z) */
3747 low_direction &= ~nSRSTnOE;
3750 /* command "set data bits low byte" */
3751 buffer_write(0x80);
3752 buffer_write(low_output);
3753 buffer_write(low_direction);
3754 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3755 "low_direction: 0x%2.2x",
3756 trst, srst, low_output, low_direction);
3758 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3759 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3760 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3761 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3762 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3764 if (trst == 1)
3766 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3767 high_output &= ~nTRSTnOE;
3768 else
3769 high_output &= ~nTRST;
3771 else if (trst == 0)
3773 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3774 high_output |= nTRSTnOE;
3775 else
3776 high_output |= nTRST;
3779 if (srst == 1)
3781 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3782 high_output &= ~nSRST;
3783 else
3784 high_output &= ~nSRSTnOE;
3786 else if (srst == 0)
3788 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3789 high_output |= nSRST;
3790 else
3791 high_output |= nSRSTnOE;
3794 /* command "set data bits high byte" */
3795 buffer_write(0x82);
3796 buffer_write(high_output);
3797 buffer_write(high_direction);
3798 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
3799 "high_direction: 0x%2.2x",
3800 trst, srst, high_output, high_direction);
3802 else if (signalyzer_h_adapter_type == 0x0000)
3804 if (trst == 1)
3806 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3807 /* switch to output pin (output is low) */
3808 low_direction |= nTRSTnOE;
3809 else
3810 /* switch output low */
3811 low_output &= ~nTRST;
3813 else if (trst == 0)
3815 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3816 /* switch to input pin (high-Z + internal
3817 * and external pullup) */
3818 low_direction &= ~nTRSTnOE;
3819 else
3820 /* switch output high */
3821 low_output |= nTRST;
3824 if (srst == 1)
3826 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3827 /* switch output low */
3828 low_output &= ~nSRST;
3829 else
3830 /* switch to output pin (output is low) */
3831 low_direction |= nSRSTnOE;
3833 else if (srst == 0)
3835 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3836 /* switch output high */
3837 low_output |= nSRST;
3838 else
3839 /* switch to input pin (high-Z) */
3840 low_direction &= ~nSRSTnOE;
3843 /* command "set data bits low byte" */
3844 buffer_write(0x80);
3845 buffer_write(low_output);
3846 buffer_write(low_direction);
3847 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3848 "low_direction: 0x%2.2x",
3849 trst, srst, low_output, low_direction);
3853 static void signalyzer_h_blink(void)
3855 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
3858 /********************************************************************
3859 * Support for KT-LINK
3860 * JTAG adapter from KRISTECH
3861 * http://www.kristech.eu
3862 *******************************************************************/
3863 static int ktlink_init(void)
3865 uint8_t buf[3];
3866 uint32_t bytes_written;
3867 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
3869 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
3870 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
3872 // initialize low port
3873 buf[0] = 0x80; // command "set data bits low byte"
3874 buf[1] = low_output;
3875 buf[2] = low_direction;
3876 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3878 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3880 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3881 return ERROR_JTAG_INIT_FAILED;
3884 nTRST = 0x01;
3885 nSRST = 0x02;
3886 nTRSTnOE = 0x04;
3887 nSRSTnOE = 0x08;
3889 high_output = 0x80; // turn LED on
3890 high_direction = 0xFF; // all outputs
3892 enum reset_types jtag_reset_config = jtag_get_reset_config();
3894 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3895 high_output |= nTRSTnOE;
3896 high_output &= ~nTRST;
3897 } else {
3898 high_output &= ~nTRSTnOE;
3899 high_output |= nTRST;
3902 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3903 high_output &= ~nSRSTnOE;
3904 high_output |= nSRST;
3905 } else {
3906 high_output |= nSRSTnOE;
3907 high_output &= ~nSRST;
3910 // initialize high port
3911 buf[0] = 0x82; // command "set data bits high byte"
3912 buf[1] = high_output; // value
3913 buf[2] = high_direction;
3914 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3916 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3918 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3919 return ERROR_JTAG_INIT_FAILED;
3922 return ERROR_OK;
3925 static void ktlink_reset(int trst, int srst)
3927 enum reset_types jtag_reset_config = jtag_get_reset_config();
3929 if (trst == 1) {
3930 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3931 high_output &= ~nTRSTnOE;
3932 else
3933 high_output &= ~nTRST;
3934 } else if (trst == 0) {
3935 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3936 high_output |= nTRSTnOE;
3937 else
3938 high_output |= nTRST;
3941 if (srst == 1) {
3942 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3943 high_output &= ~nSRST;
3944 else
3945 high_output &= ~nSRSTnOE;
3946 } else if (srst == 0) {
3947 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3948 high_output |= nSRST;
3949 else
3950 high_output |= nSRSTnOE;
3953 buffer_write(0x82); // command "set data bits high byte"
3954 buffer_write(high_output);
3955 buffer_write(high_direction);
3956 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
3959 static void ktlink_blink(void)
3961 /* LED connected to ACBUS7 */
3962 if (high_output & 0x80)
3963 high_output &= 0x7F;
3964 else
3965 high_output |= 0x80;
3967 buffer_write(0x82); // command "set data bits high byte"
3968 buffer_write(high_output);
3969 buffer_write(high_direction);
3972 static const struct command_registration ft2232_command_handlers[] = {
3974 .name = "ft2232_device_desc",
3975 .handler = &ft2232_handle_device_desc_command,
3976 .mode = COMMAND_CONFIG,
3977 .help = "set the USB device description of the FTDI FT2232 device",
3978 .usage = "<description>",
3981 .name = "ft2232_serial",
3982 .handler = &ft2232_handle_serial_command,
3983 .mode = COMMAND_CONFIG,
3984 .help = "set the serial number of the FTDI FT2232 device",
3985 .usage = "<serial#>",
3988 .name = "ft2232_layout",
3989 .handler = &ft2232_handle_layout_command,
3990 .mode = COMMAND_CONFIG,
3991 .help = "set the layout of the FT2232 GPIO signals used "
3992 "to control output-enables and reset signals",
3993 .usage = "<layout>",
3996 .name = "ft2232_vid_pid",
3997 .handler = &ft2232_handle_vid_pid_command,
3998 .mode = COMMAND_CONFIG,
3999 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4000 .usage = "<vid> <pid> [...]",
4003 .name = "ft2232_latency",
4004 .handler = &ft2232_handle_latency_command,
4005 .mode = COMMAND_CONFIG,
4006 .help = "set the FT2232 latency timer to a new value",
4007 .usage = "<vid> <pid> [...]",
4009 COMMAND_REGISTRATION_DONE
4012 struct jtag_interface ft2232_interface = {
4013 .name = "ft2232",
4014 .commands = ft2232_command_handlers,
4015 .init = &ft2232_init,
4016 .quit = &ft2232_quit,
4017 .speed = &ft2232_speed,
4018 .speed_div = &ft2232_speed_div,
4019 .khz = &ft2232_khz,
4020 .execute_queue = &ft2232_execute_queue,