1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods. Since JTAG uses
36 * only one of the two ports on these devices, on integrated boards the
37 * second port often serves as a USB-to-serial adapter for the target's
38 * console UART even when the JTAG port is not in use. (Systems which
39 * support ARM's SWD in addition to JTAG, or instead of it, may use that
40 * second port for reading SWV trace data.)
42 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
43 * request/response interactions involve round trips over the USB link.
44 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
45 * can for example poll quickly for a status change (usually taking on the
46 * order of microseconds not milliseconds) before beginning a queued
47 * transaction which require the previous one to have completed.
49 * There are dozens of adapters of this type, differing in details which
50 * this driver needs to understand. Those "layout" details are required
51 * as part of FT2232 driver configuration.
53 * This code uses information contained in the MPSSE specification which was
55 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
56 * Hereafter this is called the "MPSSE Spec".
58 * The datasheet for the ftdichip.com's FT2232D part is here:
59 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
61 * Also note the issue with code 0x4b (clock data to TMS) noted in
62 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
63 * which can affect longer JTAG state paths.
70 /* project specific includes */
71 #include <jtag/interface.h>
72 #include <helper/time_support.h>
80 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
81 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
82 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
83 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
86 /* FT2232 access library includes */
87 #if BUILD_FT2232_FTD2XX == 1
89 #elif BUILD_FT2232_LIBFTDI == 1
93 /* max TCK for the high speed devices 30000 kHz */
94 #define FTDI_2232H_4232H_MAX_TCK 30000
95 /* max TCK for the full speed devices 6000 kHz */
96 #define FTDI_2232C_MAX_TCK 6000
97 /* this speed value tells that RTCK is requested */
101 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
102 * errors with a retry count of 100. Increasing it solves the problem for me.
105 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
106 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
109 #define LIBFTDI_READ_RETRY_COUNT 2000
111 #ifndef BUILD_FT2232_HIGHSPEED
112 #if BUILD_FT2232_FTD2XX == 1
113 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
114 #elif BUILD_FT2232_LIBFTDI == 1
115 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
120 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
121 * stable state. Calling code must ensure that current state is stable,
122 * that verification is not done in here.
124 * @param num_cycles The number of clocks cycles to send.
125 * @param cmd The command to send.
127 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
129 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
131 static char * ft2232_device_desc_A = NULL;
132 static char* ft2232_device_desc = NULL;
133 static char* ft2232_serial = NULL;
134 static char* ft2232_layout = NULL;
135 static uint8_t ft2232_latency = 2;
136 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
138 #define MAX_USB_IDS 8
139 /* vid = pid = 0 marks the end of the list */
140 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
141 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
143 struct ft2232_layout {
146 void (*reset)(int trst, int srst);
151 /* init procedures for supported layouts */
152 static int usbjtag_init(void);
153 static int jtagkey_init(void);
154 static int olimex_jtag_init(void);
155 static int flyswatter_init(void);
156 static int turtle_init(void);
157 static int comstick_init(void);
158 static int stm32stick_init(void);
159 static int axm0432_jtag_init(void);
160 static int sheevaplug_init(void);
161 static int icebear_jtag_init(void);
162 static int cortino_jtag_init(void);
163 static int signalyzer_h_init(void);
164 static int ktlink_init(void);
166 /* reset procedures for supported layouts */
167 static void usbjtag_reset(int trst, int srst);
168 static void jtagkey_reset(int trst, int srst);
169 static void olimex_jtag_reset(int trst, int srst);
170 static void flyswatter_reset(int trst, int srst);
171 static void turtle_reset(int trst, int srst);
172 static void comstick_reset(int trst, int srst);
173 static void stm32stick_reset(int trst, int srst);
174 static void axm0432_jtag_reset(int trst, int srst);
175 static void sheevaplug_reset(int trst, int srst);
176 static void icebear_jtag_reset(int trst, int srst);
177 static void signalyzer_h_reset(int trst, int srst);
178 static void ktlink_reset(int trst, int srst);
180 /* blink procedures for layouts that support a blinking led */
181 static void olimex_jtag_blink(void);
182 static void flyswatter_jtag_blink(void);
183 static void turtle_jtag_blink(void);
184 static void signalyzer_h_blink(void);
185 static void ktlink_blink(void);
187 static const struct ft2232_layout ft2232_layouts[] =
190 .init = usbjtag_init,
191 .reset = usbjtag_reset,
194 .init = jtagkey_init,
195 .reset = jtagkey_reset,
197 { .name = "jtagkey_prototype_v1",
198 .init = jtagkey_init,
199 .reset = jtagkey_reset,
201 { .name = "oocdlink",
202 .init = jtagkey_init,
203 .reset = jtagkey_reset,
205 { .name = "signalyzer",
206 .init = usbjtag_init,
207 .reset = usbjtag_reset,
209 { .name = "evb_lm3s811",
210 .init = usbjtag_init,
211 .reset = usbjtag_reset,
213 { .name = "luminary_icdi",
214 .init = usbjtag_init,
215 .reset = usbjtag_reset,
217 { .name = "olimex-jtag",
218 .init = olimex_jtag_init,
219 .reset = olimex_jtag_reset,
220 .blink = olimex_jtag_blink
222 { .name = "flyswatter",
223 .init = flyswatter_init,
224 .reset = flyswatter_reset,
225 .blink = flyswatter_jtag_blink
227 { .name = "turtelizer2",
229 .reset = turtle_reset,
230 .blink = turtle_jtag_blink
232 { .name = "comstick",
233 .init = comstick_init,
234 .reset = comstick_reset,
236 { .name = "stm32stick",
237 .init = stm32stick_init,
238 .reset = stm32stick_reset,
240 { .name = "axm0432_jtag",
241 .init = axm0432_jtag_init,
242 .reset = axm0432_jtag_reset,
244 { .name = "sheevaplug",
245 .init = sheevaplug_init,
246 .reset = sheevaplug_reset,
249 .init = icebear_jtag_init,
250 .reset = icebear_jtag_reset,
253 .init = cortino_jtag_init,
254 .reset = comstick_reset,
256 { .name = "signalyzer-h",
257 .init = signalyzer_h_init,
258 .reset = signalyzer_h_reset,
259 .blink = signalyzer_h_blink
263 .reset = ktlink_reset,
264 .blink = ktlink_blink
266 { .name = NULL, /* END OF TABLE */ },
269 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
271 static const struct ft2232_layout *layout;
272 static uint8_t low_output = 0x0;
273 static uint8_t low_direction = 0x0;
274 static uint8_t high_output = 0x0;
275 static uint8_t high_direction = 0x0;
277 #if BUILD_FT2232_FTD2XX == 1
278 static FT_HANDLE ftdih = NULL;
279 static FT_DEVICE ftdi_device = 0;
280 #elif BUILD_FT2232_LIBFTDI == 1
281 static struct ftdi_context ftdic;
282 static enum ftdi_chip_type ftdi_device;
285 static struct jtag_command* first_unsent; /* next command that has to be sent */
286 static int require_send;
288 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
290 "There is a significant difference between libftdi and libftd2xx. The latter
291 one allows to schedule up to 64*64 bytes of result data while libftdi fails
292 with more than 4*64. As a consequence, the FT2232 driver is forced to
293 perform around 16x more USB transactions for long command streams with TDO
294 capture when running with libftdi."
297 #define FT2232_BUFFER_SIZE 131072
298 a comment would have been nice.
301 #define FT2232_BUFFER_SIZE 131072
303 static uint8_t* ft2232_buffer = NULL;
304 static int ft2232_buffer_size = 0;
305 static int ft2232_read_pointer = 0;
306 static int ft2232_expect_read = 0;
309 * Function buffer_write
310 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
311 * @param val is the byte to send.
313 static inline void buffer_write(uint8_t val)
315 assert(ft2232_buffer);
316 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
317 ft2232_buffer[ft2232_buffer_size++] = val;
321 * Function buffer_read
322 * returns a byte from the byte buffer.
324 static inline uint8_t buffer_read(void)
326 assert(ft2232_buffer);
327 assert(ft2232_read_pointer < ft2232_buffer_size);
328 return ft2232_buffer[ft2232_read_pointer++];
332 * Clocks out \a bit_count bits on the TMS line, starting with the least
333 * significant bit of tms_bits and progressing to more significant bits.
334 * Rigorous state transition logging is done here via tap_set_state().
336 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
337 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
338 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
339 * is often used for this, 0x4b.
341 * @param tms_bits Holds the sequence of bits to send.
342 * @param tms_count Tells how many bits in the sequence.
343 * @param tdi_bit A single bit to pass on to TDI before the first TCK
344 * cycle and held static for the duration of TMS clocking.
346 * See the MPSSE spec referenced above.
348 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
352 int tms_ndx; /* bit index into tms_byte */
354 assert(tms_count > 0);
356 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
357 mpsse_cmd, tms_bits, tms_count);
359 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
361 bool bit = tms_bits & 1;
364 tms_byte |= (1 << tms_ndx);
366 /* always do state transitions in public view */
367 tap_set_state(tap_state_transition(tap_get_state(), bit));
369 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
374 if (tms_ndx == 7 || i == tms_count-1)
376 buffer_write(mpsse_cmd);
377 buffer_write(tms_ndx - 1);
379 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
380 TMS/CS and is held static for the duration of TMS/CS clocking.
382 buffer_write(tms_byte | (tdi_bit << 7));
388 * Function get_tms_buffer_requirements
389 * returns what clock_tms() will consume if called with
392 static inline int get_tms_buffer_requirements(int bit_count)
394 return ((bit_count + 6)/7) * 3;
398 * Function move_to_state
399 * moves the TAP controller from the current state to a
400 * \a goal_state through a path given by tap_get_tms_path(). State transition
401 * logging is performed by delegation to clock_tms().
403 * @param goal_state is the destination state for the move.
405 static void move_to_state(tap_state_t goal_state)
407 tap_state_t start_state = tap_get_state();
409 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
410 lookup of the required TMS pattern to move to this state from the
414 /* do the 2 lookups */
415 int tms_bits = tap_get_tms_path(start_state, goal_state);
416 int tms_count = tap_get_tms_path_len(start_state, goal_state);
418 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
420 clock_tms(0x4b, tms_bits, tms_count, 0);
423 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
425 #if BUILD_FT2232_FTD2XX == 1
427 DWORD dw_bytes_written;
428 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
430 *bytes_written = dw_bytes_written;
431 LOG_ERROR("FT_Write returned: %lu", status);
432 return ERROR_JTAG_DEVICE_ERROR;
436 *bytes_written = dw_bytes_written;
439 #elif BUILD_FT2232_LIBFTDI == 1
441 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
444 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
445 return ERROR_JTAG_DEVICE_ERROR;
449 *bytes_written = retval;
455 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
457 #if BUILD_FT2232_FTD2XX == 1
463 while ((*bytes_read < size) && timeout--)
465 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
466 *bytes_read, &dw_bytes_read)) != FT_OK)
469 LOG_ERROR("FT_Read returned: %lu", status);
470 return ERROR_JTAG_DEVICE_ERROR;
472 *bytes_read += dw_bytes_read;
475 #elif BUILD_FT2232_LIBFTDI == 1
477 int timeout = LIBFTDI_READ_RETRY_COUNT;
480 while ((*bytes_read < size) && timeout--)
482 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
485 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
486 return ERROR_JTAG_DEVICE_ERROR;
488 *bytes_read += retval;
493 if (*bytes_read < size)
495 LOG_ERROR("couldn't read enough bytes from "
496 "FT2232 device (%i < %i)",
497 (unsigned)*bytes_read,
499 return ERROR_JTAG_DEVICE_ERROR;
505 static bool ft2232_device_is_highspeed(void)
507 #if BUILD_FT2232_FTD2XX == 1
508 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
509 #elif BUILD_FT2232_LIBFTDI == 1
510 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
515 * Commands that only apply to the FT2232H and FT4232H devices.
516 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
517 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
520 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
522 uint8_t buf = enable ? 0x96 : 0x97;
523 LOG_DEBUG("%2.2x", buf);
525 uint32_t bytes_written;
526 int retval = ft2232_write(&buf, 1, &bytes_written);
527 if ((ERROR_OK != retval) || (bytes_written != 1))
529 LOG_ERROR("couldn't write command to %s adaptive clocking"
530 , enable ? "enable" : "disable");
538 * Enable/disable the clk divide by 5 of the 60MHz master clock.
539 * This result in a JTAG clock speed range of 91.553Hz-6MHz
540 * respective 457.763Hz-30MHz.
542 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
544 uint32_t bytes_written;
545 uint8_t buf = enable ? 0x8b : 0x8a;
546 int retval = ft2232_write(&buf, 1, &bytes_written);
547 if ((ERROR_OK != retval) || (bytes_written != 1))
549 LOG_ERROR("couldn't write command to %s clk divide by 5"
550 , enable ? "enable" : "disable");
551 return ERROR_JTAG_INIT_FAILED;
553 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
554 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
559 static int ft2232_speed(int speed)
563 uint32_t bytes_written;
566 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
567 if (ft2232_device_is_highspeed())
568 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
569 else if (enable_adaptive_clocking)
571 LOG_ERROR("ft2232 device %lu does not support RTCK"
572 , (long unsigned int)ftdi_device);
576 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
579 buf[0] = 0x86; /* command "set divisor" */
580 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
581 buf[2] = (speed >> 8) & 0xff; /* valueH */
583 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
584 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
586 LOG_ERROR("couldn't set FT2232 TCK speed");
593 static int ft2232_speed_div(int speed, int* khz)
595 /* Take a look in the FT2232 manual,
596 * AN2232C-01 Command Processor for
597 * MPSSE and MCU Host Bus. Chapter 3.8 */
599 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
604 static int ft2232_khz(int khz, int* jtag_speed)
608 if (ft2232_device_is_highspeed())
610 *jtag_speed = RTCK_SPEED;
615 LOG_DEBUG("RCLK not supported");
620 /* Take a look in the FT2232 manual,
621 * AN2232C-01 Command Processor for
622 * MPSSE and MCU Host Bus. Chapter 3.8
624 * We will calc here with a multiplier
625 * of 10 for better rounding later. */
627 /* Calc speed, (ft2232_max_tck / khz) - 1 */
628 /* Use 65000 for better rounding */
629 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
631 /* Add 0.9 for rounding */
634 /* Calc real speed */
635 *jtag_speed = *jtag_speed / 10;
637 /* Check if speed is greater than 0 */
643 /* Check max value */
644 if (*jtag_speed > 0xFFFF)
646 *jtag_speed = 0xFFFF;
652 static void ft2232_end_state(tap_state_t state)
654 if (tap_is_state_stable(state))
655 tap_set_end_state(state);
658 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
663 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
665 int num_bytes = (scan_size + 7) / 8;
666 int bits_left = scan_size;
669 while (num_bytes-- > 1)
671 buffer[cur_byte++] = buffer_read();
675 buffer[cur_byte] = 0x0;
677 /* There is one more partial byte left from the clock data in/out instructions */
680 buffer[cur_byte] = buffer_read() >> 1;
682 /* 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 */
683 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
686 static void ft2232_debug_dump_buffer(void)
692 for (i = 0; i < ft2232_buffer_size; i++)
694 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
697 LOG_DEBUG("%s", line);
703 LOG_DEBUG("%s", line);
706 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
708 struct jtag_command* cmd;
713 uint32_t bytes_written = 0;
714 uint32_t bytes_read = 0;
716 #ifdef _DEBUG_USB_IO_
717 struct timeval start, inter, inter2, end;
718 struct timeval d_inter, d_inter2, d_end;
721 #ifdef _DEBUG_USB_COMMS_
722 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
723 ft2232_debug_dump_buffer();
726 #ifdef _DEBUG_USB_IO_
727 gettimeofday(&start, NULL);
730 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
732 LOG_ERROR("couldn't write MPSSE commands to FT2232");
736 #ifdef _DEBUG_USB_IO_
737 gettimeofday(&inter, NULL);
740 if (ft2232_expect_read)
742 /* FIXME this "timeout" is never changed ... */
743 int timeout = LIBFTDI_READ_RETRY_COUNT;
744 ft2232_buffer_size = 0;
746 #ifdef _DEBUG_USB_IO_
747 gettimeofday(&inter2, NULL);
750 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
752 LOG_ERROR("couldn't read from FT2232");
756 #ifdef _DEBUG_USB_IO_
757 gettimeofday(&end, NULL);
759 timeval_subtract(&d_inter, &inter, &start);
760 timeval_subtract(&d_inter2, &inter2, &start);
761 timeval_subtract(&d_end, &end, &start);
763 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
764 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
765 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
766 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
769 ft2232_buffer_size = bytes_read;
771 if (ft2232_expect_read != ft2232_buffer_size)
773 LOG_ERROR("ft2232_expect_read (%i) != "
774 "ft2232_buffer_size (%i) "
778 LIBFTDI_READ_RETRY_COUNT - timeout);
779 ft2232_debug_dump_buffer();
784 #ifdef _DEBUG_USB_COMMS_
785 LOG_DEBUG("read buffer (%i retries): %i bytes",
786 LIBFTDI_READ_RETRY_COUNT - timeout,
788 ft2232_debug_dump_buffer();
792 ft2232_expect_read = 0;
793 ft2232_read_pointer = 0;
795 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
796 * that wasn't handled by a caller-provided error handler
806 type = jtag_scan_type(cmd->cmd.scan);
807 if (type != SCAN_OUT)
809 scan_size = jtag_scan_size(cmd->cmd.scan);
810 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
811 ft2232_read_scan(type, buffer, scan_size);
812 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
813 retval = ERROR_JTAG_QUEUE_FAILED;
825 ft2232_buffer_size = 0;
831 * Function ft2232_add_pathmove
832 * moves the TAP controller from the current state to a new state through the
833 * given path, where path is an array of tap_state_t's.
835 * @param path is an array of tap_stat_t which gives the states to traverse through
836 * ending with the last state at path[num_states-1]
837 * @param num_states is the count of state steps to move through
839 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
843 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
847 /* this loop verifies that the path is legal and logs each state in the path */
850 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
852 int num_states_batch = num_states > 7 ? 7 : num_states;
854 /* command "Clock Data to TMS/CS Pin (no Read)" */
857 /* number of states remaining */
858 buffer_write(num_states_batch - 1);
860 while (num_states_batch--) {
861 /* either TMS=0 or TMS=1 must work ... */
862 if (tap_state_transition(tap_get_state(), false)
863 == path[state_count])
864 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
865 else if (tap_state_transition(tap_get_state(), true)
866 == path[state_count])
867 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
869 /* ... or else the caller goofed BADLY */
871 LOG_ERROR("BUG: %s -> %s isn't a valid "
872 "TAP state transition",
873 tap_state_name(tap_get_state()),
874 tap_state_name(path[state_count]));
878 tap_set_state(path[state_count]);
883 buffer_write(tms_byte);
885 tap_set_end_state(tap_get_state());
888 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
890 int num_bytes = (scan_size + 7) / 8;
891 int bits_left = scan_size;
897 if (tap_get_state() != TAP_DRSHIFT)
899 move_to_state(TAP_DRSHIFT);
904 if (tap_get_state() != TAP_IRSHIFT)
906 move_to_state(TAP_IRSHIFT);
910 /* add command for complete bytes */
911 while (num_bytes > 1)
916 /* Clock Data Bytes In and Out LSB First */
918 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
920 else if (type == SCAN_OUT)
922 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
924 /* LOG_DEBUG("added TDI bytes (o)"); */
926 else if (type == SCAN_IN)
928 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
930 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
933 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
934 num_bytes -= thisrun_bytes;
936 buffer_write((uint8_t) (thisrun_bytes - 1));
937 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
941 /* add complete bytes */
942 while (thisrun_bytes-- > 0)
944 buffer_write(buffer[cur_byte++]);
948 else /* (type == SCAN_IN) */
950 bits_left -= 8 * (thisrun_bytes);
954 /* the most signifcant bit is scanned during TAP movement */
956 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
960 /* process remaining bits but the last one */
965 /* Clock Data Bits In and Out LSB First */
967 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
969 else if (type == SCAN_OUT)
971 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
973 /* LOG_DEBUG("added TDI bits (o)"); */
975 else if (type == SCAN_IN)
977 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
979 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
982 buffer_write(bits_left - 2);
984 buffer_write(buffer[cur_byte]);
987 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
988 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
992 /* Clock Data Bits In and Out LSB First */
994 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
996 else if (type == SCAN_OUT)
998 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1000 /* LOG_DEBUG("added TDI bits (o)"); */
1002 else if (type == SCAN_IN)
1004 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1006 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1009 buffer_write(last_bit);
1017 /* move from Shift-IR/DR to end state */
1018 if (type != SCAN_OUT)
1020 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1021 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1024 /* Clock Data to TMS/CS Pin with Read */
1029 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1030 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1031 /* Clock Data to TMS/CS Pin (no Read) */
1035 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1036 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1039 if (tap_get_state() != tap_get_end_state())
1041 move_to_state(tap_get_end_state());
1045 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1047 int num_bytes = (scan_size + 7) / 8;
1048 int bits_left = scan_size;
1051 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1052 uint8_t* receive_pointer = receive_buffer;
1053 uint32_t bytes_written;
1054 uint32_t bytes_read;
1056 int thisrun_read = 0;
1060 LOG_ERROR("BUG: large IR scans are not supported");
1064 if (tap_get_state() != TAP_DRSHIFT)
1066 move_to_state(TAP_DRSHIFT);
1069 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1071 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1074 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1075 ft2232_buffer_size, (int)bytes_written);
1076 ft2232_buffer_size = 0;
1078 /* add command for complete bytes */
1079 while (num_bytes > 1)
1083 if (type == SCAN_IO)
1085 /* Clock Data Bytes In and Out LSB First */
1087 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1089 else if (type == SCAN_OUT)
1091 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1093 /* LOG_DEBUG("added TDI bytes (o)"); */
1095 else if (type == SCAN_IN)
1097 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1099 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1102 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1103 thisrun_read = thisrun_bytes;
1104 num_bytes -= thisrun_bytes;
1105 buffer_write((uint8_t) (thisrun_bytes - 1));
1106 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1108 if (type != SCAN_IN)
1110 /* add complete bytes */
1111 while (thisrun_bytes-- > 0)
1113 buffer_write(buffer[cur_byte]);
1118 else /* (type == SCAN_IN) */
1120 bits_left -= 8 * (thisrun_bytes);
1123 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1125 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1128 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1130 (int)bytes_written);
1131 ft2232_buffer_size = 0;
1133 if (type != SCAN_OUT)
1135 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1137 LOG_ERROR("couldn't read from FT2232");
1140 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1143 receive_pointer += bytes_read;
1149 /* the most signifcant bit is scanned during TAP movement */
1150 if (type != SCAN_IN)
1151 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1155 /* process remaining bits but the last one */
1158 if (type == SCAN_IO)
1160 /* Clock Data Bits In and Out LSB First */
1162 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1164 else if (type == SCAN_OUT)
1166 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1168 /* LOG_DEBUG("added TDI bits (o)"); */
1170 else if (type == SCAN_IN)
1172 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1174 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1176 buffer_write(bits_left - 2);
1177 if (type != SCAN_IN)
1178 buffer_write(buffer[cur_byte]);
1180 if (type != SCAN_OUT)
1184 if (tap_get_end_state() == TAP_DRSHIFT)
1186 if (type == SCAN_IO)
1188 /* Clock Data Bits In and Out LSB First */
1190 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1192 else if (type == SCAN_OUT)
1194 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1196 /* LOG_DEBUG("added TDI bits (o)"); */
1198 else if (type == SCAN_IN)
1200 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1202 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1205 buffer_write(last_bit);
1209 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1210 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1213 /* move from Shift-IR/DR to end state */
1214 if (type != SCAN_OUT)
1216 /* Clock Data to TMS/CS Pin with Read */
1218 /* LOG_DEBUG("added TMS scan (read)"); */
1222 /* Clock Data to TMS/CS Pin (no Read) */
1224 /* LOG_DEBUG("added TMS scan (no read)"); */
1227 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1228 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1231 if (type != SCAN_OUT)
1234 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1236 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1239 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1241 (int)bytes_written);
1242 ft2232_buffer_size = 0;
1244 if (type != SCAN_OUT)
1246 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1248 LOG_ERROR("couldn't read from FT2232");
1251 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1254 receive_pointer += bytes_read;
1260 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1262 int predicted_size = 3;
1263 int num_bytes = (scan_size - 1) / 8;
1265 if (tap_get_state() != TAP_DRSHIFT)
1266 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1268 if (type == SCAN_IN) /* only from device to host */
1270 /* complete bytes */
1271 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1273 /* remaining bits - 1 (up to 7) */
1274 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1276 else /* host to device, or bidirectional */
1278 /* complete bytes */
1279 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1281 /* remaining bits -1 (up to 7) */
1282 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1285 return predicted_size;
1288 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1290 int predicted_size = 0;
1292 if (type != SCAN_OUT)
1294 /* complete bytes */
1295 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1297 /* remaining bits - 1 */
1298 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1300 /* last bit (from TMS scan) */
1301 predicted_size += 1;
1304 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1306 return predicted_size;
1309 static void usbjtag_reset(int trst, int srst)
1311 enum reset_types jtag_reset_config = jtag_get_reset_config();
1314 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1315 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1317 low_output &= ~nTRST; /* switch output low */
1321 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1322 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1324 low_output |= nTRST; /* switch output high */
1329 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1330 low_output &= ~nSRST; /* switch output low */
1332 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1336 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1337 low_output |= nSRST; /* switch output high */
1339 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1342 /* command "set data bits low byte" */
1344 buffer_write(low_output);
1345 buffer_write(low_direction);
1348 static void jtagkey_reset(int trst, int srst)
1350 enum reset_types jtag_reset_config = jtag_get_reset_config();
1353 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1354 high_output &= ~nTRSTnOE;
1356 high_output &= ~nTRST;
1360 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1361 high_output |= nTRSTnOE;
1363 high_output |= nTRST;
1368 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1369 high_output &= ~nSRST;
1371 high_output &= ~nSRSTnOE;
1375 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1376 high_output |= nSRST;
1378 high_output |= nSRSTnOE;
1381 /* command "set data bits high byte" */
1383 buffer_write(high_output);
1384 buffer_write(high_direction);
1385 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1389 static void olimex_jtag_reset(int trst, int srst)
1391 enum reset_types jtag_reset_config = jtag_get_reset_config();
1394 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1395 high_output &= ~nTRSTnOE;
1397 high_output &= ~nTRST;
1401 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1402 high_output |= nTRSTnOE;
1404 high_output |= nTRST;
1409 high_output |= nSRST;
1413 high_output &= ~nSRST;
1416 /* command "set data bits high byte" */
1418 buffer_write(high_output);
1419 buffer_write(high_direction);
1420 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1424 static void axm0432_jtag_reset(int trst, int srst)
1428 tap_set_state(TAP_RESET);
1429 high_output &= ~nTRST;
1433 high_output |= nTRST;
1438 high_output &= ~nSRST;
1442 high_output |= nSRST;
1445 /* command "set data bits low byte" */
1447 buffer_write(high_output);
1448 buffer_write(high_direction);
1449 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1453 static void flyswatter_reset(int trst, int srst)
1457 low_output &= ~nTRST;
1461 low_output |= nTRST;
1466 low_output |= nSRST;
1470 low_output &= ~nSRST;
1473 /* command "set data bits low byte" */
1475 buffer_write(low_output);
1476 buffer_write(low_direction);
1477 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1480 static void turtle_reset(int trst, int srst)
1486 low_output |= nSRST;
1490 low_output &= ~nSRST;
1493 /* command "set data bits low byte" */
1495 buffer_write(low_output);
1496 buffer_write(low_direction);
1497 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1500 static void comstick_reset(int trst, int srst)
1504 high_output &= ~nTRST;
1508 high_output |= nTRST;
1513 high_output &= ~nSRST;
1517 high_output |= nSRST;
1520 /* command "set data bits high byte" */
1522 buffer_write(high_output);
1523 buffer_write(high_direction);
1524 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1528 static void stm32stick_reset(int trst, int srst)
1532 high_output &= ~nTRST;
1536 high_output |= nTRST;
1541 low_output &= ~nSRST;
1545 low_output |= nSRST;
1548 /* command "set data bits low byte" */
1550 buffer_write(low_output);
1551 buffer_write(low_direction);
1553 /* command "set data bits high byte" */
1555 buffer_write(high_output);
1556 buffer_write(high_direction);
1557 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1561 static void sheevaplug_reset(int trst, int srst)
1564 high_output &= ~nTRST;
1566 high_output |= nTRST;
1569 high_output &= ~nSRSTnOE;
1571 high_output |= nSRSTnOE;
1573 /* command "set data bits high byte" */
1575 buffer_write(high_output);
1576 buffer_write(high_direction);
1577 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1580 static int ft2232_execute_runtest(struct jtag_command *cmd)
1584 int predicted_size = 0;
1587 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1588 cmd->cmd.runtest->num_cycles,
1589 tap_state_name(cmd->cmd.runtest->end_state));
1591 /* only send the maximum buffer size that FT2232C can handle */
1593 if (tap_get_state() != TAP_IDLE)
1594 predicted_size += 3;
1595 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1596 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1597 predicted_size += 3;
1598 if (tap_get_end_state() != TAP_IDLE)
1599 predicted_size += 3;
1600 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1602 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1603 retval = ERROR_JTAG_QUEUE_FAILED;
1607 if (tap_get_state() != TAP_IDLE)
1609 move_to_state(TAP_IDLE);
1612 i = cmd->cmd.runtest->num_cycles;
1615 /* there are no state transitions in this code, so omit state tracking */
1617 /* command "Clock Data to TMS/CS Pin (no Read)" */
1621 buffer_write((i > 7) ? 6 : (i - 1));
1626 i -= (i > 7) ? 7 : i;
1627 /* LOG_DEBUG("added TMS scan (no read)"); */
1630 ft2232_end_state(cmd->cmd.runtest->end_state);
1632 if (tap_get_state() != tap_get_end_state())
1634 move_to_state(tap_get_end_state());
1638 DEBUG_JTAG_IO("runtest: %i, end in %s",
1639 cmd->cmd.runtest->num_cycles,
1640 tap_state_name(tap_get_end_state()));
1644 static int ft2232_execute_statemove(struct jtag_command *cmd)
1646 int predicted_size = 0;
1647 int retval = ERROR_OK;
1649 DEBUG_JTAG_IO("statemove end in %s",
1650 tap_state_name(cmd->cmd.statemove->end_state));
1652 /* only send the maximum buffer size that FT2232C can handle */
1654 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1656 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1657 retval = ERROR_JTAG_QUEUE_FAILED;
1661 ft2232_end_state(cmd->cmd.statemove->end_state);
1663 /* For TAP_RESET, ignore the current recorded state. It's often
1664 * wrong at server startup, and this transation is critical whenever
1667 if (tap_get_end_state() == TAP_RESET) {
1668 clock_tms(0x4b, 0xff, 5, 0);
1671 /* shortest-path move to desired end state */
1672 } else if (tap_get_state() != tap_get_end_state())
1674 move_to_state(tap_get_end_state());
1681 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1683 int predicted_size = 0;
1684 int retval = ERROR_OK;
1686 tap_state_t* path = cmd->cmd.pathmove->path;
1687 int num_states = cmd->cmd.pathmove->num_states;
1689 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1690 tap_state_name(tap_get_state()),
1691 tap_state_name(path[num_states-1]));
1693 /* only send the maximum buffer size that FT2232C can handle */
1694 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1695 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1697 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1698 retval = ERROR_JTAG_QUEUE_FAILED;
1704 ft2232_add_pathmove(path, num_states);
1710 static int ft2232_execute_scan(struct jtag_command *cmd)
1713 int scan_size; /* size of IR or DR scan */
1714 int predicted_size = 0;
1715 int retval = ERROR_OK;
1717 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1719 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1721 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1723 predicted_size = ft2232_predict_scan_out(scan_size, type);
1724 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1726 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1727 /* unsent commands before this */
1728 if (first_unsent != cmd)
1729 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1730 retval = ERROR_JTAG_QUEUE_FAILED;
1732 /* current command */
1733 ft2232_end_state(cmd->cmd.scan->end_state);
1734 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1736 first_unsent = cmd->next;
1741 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1743 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1746 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1747 retval = ERROR_JTAG_QUEUE_FAILED;
1751 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1752 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1753 ft2232_end_state(cmd->cmd.scan->end_state);
1754 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1758 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1759 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1760 tap_state_name(tap_get_end_state()));
1765 static int ft2232_execute_reset(struct jtag_command *cmd)
1768 int predicted_size = 0;
1771 DEBUG_JTAG_IO("reset trst: %i srst %i",
1772 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1774 /* only send the maximum buffer size that FT2232C can handle */
1776 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1778 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1779 retval = ERROR_JTAG_QUEUE_FAILED;
1784 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1786 tap_set_state(TAP_RESET);
1789 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1792 DEBUG_JTAG_IO("trst: %i, srst: %i",
1793 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1797 static int ft2232_execute_sleep(struct jtag_command *cmd)
1802 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1804 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1805 retval = ERROR_JTAG_QUEUE_FAILED;
1806 first_unsent = cmd->next;
1807 jtag_sleep(cmd->cmd.sleep->us);
1808 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1810 tap_state_name(tap_get_state()));
1814 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1819 /* this is only allowed while in a stable state. A check for a stable
1820 * state was done in jtag_add_clocks()
1822 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1823 retval = ERROR_JTAG_QUEUE_FAILED;
1824 DEBUG_JTAG_IO("clocks %i while in %s",
1825 cmd->cmd.stableclocks->num_cycles,
1826 tap_state_name(tap_get_state()));
1830 static int ft2232_execute_command(struct jtag_command *cmd)
1837 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1838 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1839 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1840 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1841 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1842 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1843 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1845 LOG_ERROR("BUG: unknown JTAG command type encountered");
1851 static int ft2232_execute_queue(void)
1853 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
1856 first_unsent = cmd; /* next command that has to be sent */
1859 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1860 * that wasn't handled by a caller-provided error handler
1864 ft2232_buffer_size = 0;
1865 ft2232_expect_read = 0;
1867 /* blink, if the current layout has that feature */
1873 if (ft2232_execute_command(cmd) != ERROR_OK)
1874 retval = ERROR_JTAG_QUEUE_FAILED;
1875 /* Start reading input before FT2232 TX buffer fills up */
1877 if (ft2232_expect_read > 256)
1879 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1880 retval = ERROR_JTAG_QUEUE_FAILED;
1885 if (require_send > 0)
1886 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1887 retval = ERROR_JTAG_QUEUE_FAILED;
1892 #if BUILD_FT2232_FTD2XX == 1
1893 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1897 char SerialNumber[16];
1898 char Description[64];
1899 DWORD openex_flags = 0;
1900 char* openex_string = NULL;
1901 uint8_t latency_timer;
1903 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1906 /* Add non-standard Vid/Pid to the linux driver */
1907 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1909 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1913 if (ft2232_device_desc && ft2232_serial)
1915 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1916 ft2232_device_desc = NULL;
1919 if (ft2232_device_desc)
1921 openex_string = ft2232_device_desc;
1922 openex_flags = FT_OPEN_BY_DESCRIPTION;
1924 else if (ft2232_serial)
1926 openex_string = ft2232_serial;
1927 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1931 LOG_ERROR("neither device description nor serial number specified");
1932 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1934 return ERROR_JTAG_INIT_FAILED;
1937 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1938 if (status != FT_OK) {
1939 /* under Win32, the FTD2XX driver appends an "A" to the end
1940 * of the description, if we tried by the desc, then
1941 * try by the alternate "A" description. */
1942 if (openex_string == ft2232_device_desc) {
1943 /* Try the alternate method. */
1944 openex_string = ft2232_device_desc_A;
1945 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1946 if (status == FT_OK) {
1947 /* yea, the "alternate" method worked! */
1949 /* drat, give the user a meaningfull message.
1950 * telling the use we tried *BOTH* methods. */
1951 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1953 ft2232_device_desc_A);
1958 if (status != FT_OK)
1964 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1966 return ERROR_JTAG_INIT_FAILED;
1968 LOG_ERROR("unable to open ftdi device: %lu", status);
1969 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1970 if (status == FT_OK)
1972 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1975 for (i = 0; i < num_devices; i++)
1976 desc_array[i] = malloc(64);
1978 desc_array[num_devices] = NULL;
1980 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1982 if (status == FT_OK)
1984 LOG_ERROR("ListDevices: %lu\n", num_devices);
1985 for (i = 0; i < num_devices; i++)
1986 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1989 for (i = 0; i < num_devices; i++)
1990 free(desc_array[i]);
1996 LOG_ERROR("ListDevices: NONE\n");
1998 return ERROR_JTAG_INIT_FAILED;
2001 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2003 LOG_ERROR("unable to set latency timer: %lu", status);
2004 return ERROR_JTAG_INIT_FAILED;
2007 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2009 LOG_ERROR("unable to get latency timer: %lu", status);
2010 return ERROR_JTAG_INIT_FAILED;
2014 LOG_DEBUG("current latency timer: %i", latency_timer);
2017 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2019 LOG_ERROR("unable to set timeouts: %lu", status);
2020 return ERROR_JTAG_INIT_FAILED;
2023 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2025 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2026 return ERROR_JTAG_INIT_FAILED;
2029 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2031 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2032 return ERROR_JTAG_INIT_FAILED;
2036 static const char* type_str[] =
2037 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2038 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2039 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2040 ? ftdi_device : FT_DEVICE_UNKNOWN;
2041 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2042 LOG_INFO("deviceID: %lu", deviceID);
2043 LOG_INFO("SerialNumber: %s", SerialNumber);
2044 LOG_INFO("Description: %s", Description);
2050 static int ft2232_purge_ftd2xx(void)
2054 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2056 LOG_ERROR("error purging ftd2xx device: %lu", status);
2057 return ERROR_JTAG_INIT_FAILED;
2063 #endif /* BUILD_FT2232_FTD2XX == 1 */
2065 #if BUILD_FT2232_LIBFTDI == 1
2066 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2068 uint8_t latency_timer;
2070 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2071 ft2232_layout, vid, pid);
2073 if (ftdi_init(&ftdic) < 0)
2074 return ERROR_JTAG_INIT_FAILED;
2076 /* default to INTERFACE_A */
2077 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2079 if (ftdi_set_interface(&ftdic, channel) < 0)
2081 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2082 return ERROR_JTAG_INIT_FAILED;
2085 /* context, vendor id, product id */
2086 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2090 LOG_WARNING("unable to open ftdi device (trying more): %s",
2093 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2095 return ERROR_JTAG_INIT_FAILED;
2098 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2099 if (ftdi_usb_reset(&ftdic) < 0)
2101 LOG_ERROR("unable to reset ftdi device");
2102 return ERROR_JTAG_INIT_FAILED;
2105 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2107 LOG_ERROR("unable to set latency timer");
2108 return ERROR_JTAG_INIT_FAILED;
2111 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2113 LOG_ERROR("unable to get latency timer");
2114 return ERROR_JTAG_INIT_FAILED;
2118 LOG_DEBUG("current latency timer: %i", latency_timer);
2121 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2123 ftdi_device = ftdic.type;
2124 static const char* type_str[] =
2125 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2126 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2127 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2128 ? ftdi_device : no_of_known_types;
2129 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2133 static int ft2232_purge_libftdi(void)
2135 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2137 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2138 return ERROR_JTAG_INIT_FAILED;
2144 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2146 static int ft2232_init(void)
2150 uint32_t bytes_written;
2151 const struct ft2232_layout* cur_layout = ft2232_layouts;
2154 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2156 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2160 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2163 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2165 ft2232_layout = "usbjtag";
2166 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2169 while (cur_layout->name)
2171 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2173 layout = cur_layout;
2181 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2182 return ERROR_JTAG_INIT_FAILED;
2188 * "more indicates that there are more IDs to try, so we should
2189 * not print an error for an ID mismatch (but for anything
2192 * try_more indicates that the error code returned indicates an
2193 * ID mismatch (and nothing else) and that we should proceeed
2194 * with the next ID pair.
2196 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2199 #if BUILD_FT2232_FTD2XX == 1
2200 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2202 #elif BUILD_FT2232_LIBFTDI == 1
2203 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2204 more, &try_more, cur_layout->channel);
2208 if (!more || !try_more)
2212 ft2232_buffer_size = 0;
2213 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2215 if (layout->init() != ERROR_OK)
2216 return ERROR_JTAG_INIT_FAILED;
2218 if (ft2232_device_is_highspeed())
2220 #ifndef BUILD_FT2232_HIGHSPEED
2221 #if BUILD_FT2232_FTD2XX == 1
2222 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2223 #elif BUILD_FT2232_LIBFTDI == 1
2224 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2227 /* make sure the legacy mode is disabled */
2228 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2229 return ERROR_JTAG_INIT_FAILED;
2232 ft2232_speed(jtag_get_speed());
2234 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2235 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2237 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2238 return ERROR_JTAG_INIT_FAILED;
2241 #if BUILD_FT2232_FTD2XX == 1
2242 return ft2232_purge_ftd2xx();
2243 #elif BUILD_FT2232_LIBFTDI == 1
2244 return ft2232_purge_libftdi();
2250 static int usbjtag_init(void)
2253 uint32_t bytes_written;
2256 low_direction = 0x0b;
2258 if (strcmp(ft2232_layout, "usbjtag") == 0)
2265 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2272 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2274 /* There are multiple revisions of LM3S811 eval boards:
2275 * - Rev B (and older?) boards have no SWO trace support.
2276 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2277 * they should use the "luminary_icdi" layout instead.
2284 low_direction = 0x8b;
2286 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2288 /* Most Luminary eval boards support SWO trace output,
2289 * and should use this "luminary_icdi" layout.
2296 low_direction = 0xcb;
2300 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2301 return ERROR_JTAG_INIT_FAILED;
2304 enum reset_types jtag_reset_config = jtag_get_reset_config();
2305 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2307 low_direction &= ~nTRSTnOE; /* nTRST input */
2308 low_output &= ~nTRST; /* nTRST = 0 */
2312 low_direction |= nTRSTnOE; /* nTRST output */
2313 low_output |= nTRST; /* nTRST = 1 */
2316 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2318 low_direction |= nSRSTnOE; /* nSRST output */
2319 low_output |= nSRST; /* nSRST = 1 */
2323 low_direction &= ~nSRSTnOE; /* nSRST input */
2324 low_output &= ~nSRST; /* nSRST = 0 */
2327 /* initialize low byte for jtag */
2328 buf[0] = 0x80; /* command "set data bits low byte" */
2329 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2330 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2331 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2333 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2335 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2336 return ERROR_JTAG_INIT_FAILED;
2342 static int axm0432_jtag_init(void)
2345 uint32_t bytes_written;
2348 low_direction = 0x2b;
2350 /* initialize low byte for jtag */
2351 buf[0] = 0x80; /* command "set data bits low byte" */
2352 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2353 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2354 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2356 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2358 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2359 return ERROR_JTAG_INIT_FAILED;
2362 if (strcmp(layout->name, "axm0432_jtag") == 0)
2365 nTRSTnOE = 0x0; /* No output enable for TRST*/
2367 nSRSTnOE = 0x0; /* No output enable for SRST*/
2371 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2376 high_direction = 0x0c;
2378 enum reset_types jtag_reset_config = jtag_get_reset_config();
2379 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2381 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2385 high_output |= nTRST;
2388 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2390 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2394 high_output |= nSRST;
2397 /* initialize high port */
2398 buf[0] = 0x82; /* command "set data bits high byte" */
2399 buf[1] = high_output; /* value */
2400 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2401 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2403 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2405 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2406 return ERROR_JTAG_INIT_FAILED;
2412 static int jtagkey_init(void)
2415 uint32_t bytes_written;
2418 low_direction = 0x1b;
2420 /* initialize low byte for jtag */
2421 buf[0] = 0x80; /* command "set data bits low byte" */
2422 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2423 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2424 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2426 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2428 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2429 return ERROR_JTAG_INIT_FAILED;
2432 if (strcmp(layout->name, "jtagkey") == 0)
2439 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2440 || (strcmp(layout->name, "oocdlink") == 0))
2449 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2454 high_direction = 0x0f;
2456 enum reset_types jtag_reset_config = jtag_get_reset_config();
2457 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2459 high_output |= nTRSTnOE;
2460 high_output &= ~nTRST;
2464 high_output &= ~nTRSTnOE;
2465 high_output |= nTRST;
2468 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2470 high_output &= ~nSRSTnOE;
2471 high_output |= nSRST;
2475 high_output |= nSRSTnOE;
2476 high_output &= ~nSRST;
2479 /* initialize high port */
2480 buf[0] = 0x82; /* command "set data bits high byte" */
2481 buf[1] = high_output; /* value */
2482 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2483 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2485 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2487 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2488 return ERROR_JTAG_INIT_FAILED;
2494 static int olimex_jtag_init(void)
2497 uint32_t bytes_written;
2500 low_direction = 0x1b;
2502 /* initialize low byte for jtag */
2503 buf[0] = 0x80; /* command "set data bits low byte" */
2504 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2505 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2506 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2508 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2510 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2511 return ERROR_JTAG_INIT_FAILED;
2517 nSRSTnOE = 0x00; /* no output enable for nSRST */
2520 high_direction = 0x0f;
2522 enum reset_types jtag_reset_config = jtag_get_reset_config();
2523 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2525 high_output |= nTRSTnOE;
2526 high_output &= ~nTRST;
2530 high_output &= ~nTRSTnOE;
2531 high_output |= nTRST;
2534 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2536 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2540 high_output &= ~nSRST;
2543 /* turn red LED on */
2544 high_output |= 0x08;
2546 /* initialize high port */
2547 buf[0] = 0x82; /* command "set data bits high byte" */
2548 buf[1] = high_output; /* value */
2549 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2550 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2552 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2554 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2555 return ERROR_JTAG_INIT_FAILED;
2561 static int flyswatter_init(void)
2564 uint32_t bytes_written;
2567 low_direction = 0xfb;
2569 /* initialize low byte for jtag */
2570 buf[0] = 0x80; /* command "set data bits low byte" */
2571 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2572 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2573 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2575 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2577 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2578 return ERROR_JTAG_INIT_FAILED;
2582 nTRSTnOE = 0x0; /* not output enable for nTRST */
2584 nSRSTnOE = 0x00; /* no output enable for nSRST */
2587 high_direction = 0x0c;
2589 /* turn red LED3 on, LED2 off */
2590 high_output |= 0x08;
2592 /* initialize high port */
2593 buf[0] = 0x82; /* command "set data bits high byte" */
2594 buf[1] = high_output; /* value */
2595 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2596 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2598 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2600 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2601 return ERROR_JTAG_INIT_FAILED;
2607 static int turtle_init(void)
2610 uint32_t bytes_written;
2613 low_direction = 0x5b;
2615 /* initialize low byte for jtag */
2616 buf[0] = 0x80; /* command "set data bits low byte" */
2617 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2618 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2619 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2621 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2623 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2624 return ERROR_JTAG_INIT_FAILED;
2630 high_direction = 0x0C;
2632 /* initialize high port */
2633 buf[0] = 0x82; /* command "set data bits high byte" */
2634 buf[1] = high_output;
2635 buf[2] = high_direction;
2636 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2638 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2640 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2641 return ERROR_JTAG_INIT_FAILED;
2647 static int comstick_init(void)
2650 uint32_t bytes_written;
2653 low_direction = 0x0b;
2655 /* initialize low byte for jtag */
2656 buf[0] = 0x80; /* command "set data bits low byte" */
2657 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2658 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2659 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2661 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2663 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2664 return ERROR_JTAG_INIT_FAILED;
2668 nTRSTnOE = 0x00; /* no output enable for nTRST */
2670 nSRSTnOE = 0x00; /* no output enable for nSRST */
2673 high_direction = 0x03;
2675 /* initialize high port */
2676 buf[0] = 0x82; /* command "set data bits high byte" */
2677 buf[1] = high_output;
2678 buf[2] = high_direction;
2679 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2681 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2683 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2684 return ERROR_JTAG_INIT_FAILED;
2690 static int stm32stick_init(void)
2693 uint32_t bytes_written;
2696 low_direction = 0x8b;
2698 /* initialize low byte for jtag */
2699 buf[0] = 0x80; /* command "set data bits low byte" */
2700 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2701 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2702 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2704 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2706 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2707 return ERROR_JTAG_INIT_FAILED;
2711 nTRSTnOE = 0x00; /* no output enable for nTRST */
2713 nSRSTnOE = 0x00; /* no output enable for nSRST */
2716 high_direction = 0x03;
2718 /* initialize high port */
2719 buf[0] = 0x82; /* command "set data bits high byte" */
2720 buf[1] = high_output;
2721 buf[2] = high_direction;
2722 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2724 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2726 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2727 return ERROR_JTAG_INIT_FAILED;
2733 static int sheevaplug_init(void)
2736 uint32_t bytes_written;
2739 low_direction = 0x1b;
2741 /* initialize low byte for jtag */
2742 buf[0] = 0x80; /* command "set data bits low byte" */
2743 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2744 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2745 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2747 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2749 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2750 return ERROR_JTAG_INIT_FAILED;
2759 high_direction = 0x0f;
2761 /* nTRST is always push-pull */
2762 high_output &= ~nTRSTnOE;
2763 high_output |= nTRST;
2765 /* nSRST is always open-drain */
2766 high_output |= nSRSTnOE;
2767 high_output &= ~nSRST;
2769 /* initialize high port */
2770 buf[0] = 0x82; /* command "set data bits high byte" */
2771 buf[1] = high_output; /* value */
2772 buf[2] = high_direction; /* all outputs - xRST */
2773 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2775 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2777 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2778 return ERROR_JTAG_INIT_FAILED;
2784 static int cortino_jtag_init(void)
2787 uint32_t bytes_written;
2790 low_direction = 0x1b;
2792 /* initialize low byte for jtag */
2793 buf[0] = 0x80; /* command "set data bits low byte" */
2794 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2795 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2796 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2798 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2800 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2801 return ERROR_JTAG_INIT_FAILED;
2805 nTRSTnOE = 0x00; /* no output enable for nTRST */
2807 nSRSTnOE = 0x00; /* no output enable for nSRST */
2810 high_direction = 0x03;
2812 /* initialize high port */
2813 buf[0] = 0x82; /* command "set data bits high byte" */
2814 buf[1] = high_output;
2815 buf[2] = high_direction;
2816 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2818 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2820 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2821 return ERROR_JTAG_INIT_FAILED;
2827 static void olimex_jtag_blink(void)
2829 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2830 * ACBUS3 is bit 3 of the GPIOH port
2832 if (high_output & 0x08)
2834 /* set port pin high */
2835 high_output &= 0x07;
2839 /* set port pin low */
2840 high_output |= 0x08;
2844 buffer_write(high_output);
2845 buffer_write(high_direction);
2848 static void flyswatter_jtag_blink(void)
2851 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2853 high_output ^= 0x0c;
2856 buffer_write(high_output);
2857 buffer_write(high_direction);
2860 static void turtle_jtag_blink(void)
2863 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2865 if (high_output & 0x08)
2875 buffer_write(high_output);
2876 buffer_write(high_direction);
2879 static int ft2232_quit(void)
2881 #if BUILD_FT2232_FTD2XX == 1
2884 status = FT_Close(ftdih);
2885 #elif BUILD_FT2232_LIBFTDI == 1
2886 ftdi_usb_close(&ftdic);
2888 ftdi_deinit(&ftdic);
2891 free(ft2232_buffer);
2892 ft2232_buffer = NULL;
2897 COMMAND_HANDLER(ft2232_handle_device_desc_command)
2903 ft2232_device_desc = strdup(CMD_ARGV[0]);
2904 cp = strchr(ft2232_device_desc, 0);
2905 /* under Win32, the FTD2XX driver appends an "A" to the end
2906 * of the description, this examines the given desc
2907 * and creates the 'missing' _A or non_A variable. */
2908 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2909 /* it was, so make this the "A" version. */
2910 ft2232_device_desc_A = ft2232_device_desc;
2911 /* and *CREATE* the non-A version. */
2912 strcpy(buf, ft2232_device_desc);
2913 cp = strchr(buf, 0);
2915 ft2232_device_desc = strdup(buf);
2917 /* <space > A not defined
2919 sprintf(buf, "%s A", ft2232_device_desc);
2920 ft2232_device_desc_A = strdup(buf);
2925 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2931 COMMAND_HANDLER(ft2232_handle_serial_command)
2935 ft2232_serial = strdup(CMD_ARGV[0]);
2939 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2945 COMMAND_HANDLER(ft2232_handle_layout_command)
2950 ft2232_layout = malloc(strlen(CMD_ARGV[0]) + 1);
2951 strcpy(ft2232_layout, CMD_ARGV[0]);
2956 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
2958 if (CMD_ARGC > MAX_USB_IDS * 2)
2960 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2961 "(maximum is %d pairs)", MAX_USB_IDS);
2962 CMD_ARGC = MAX_USB_IDS * 2;
2964 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
2966 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2968 return ERROR_COMMAND_SYNTAX_ERROR;
2969 /* remove the incomplete trailing id */
2974 for (i = 0; i < CMD_ARGC; i += 2)
2976 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
2977 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
2981 * Explicitly terminate, in case there are multiples instances of
2984 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2989 COMMAND_HANDLER(ft2232_handle_latency_command)
2993 ft2232_latency = atoi(CMD_ARGV[0]);
2997 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3003 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3007 /* 7 bits of either ones or zeros. */
3008 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3010 while (num_cycles > 0)
3012 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3013 * at most 7 bits per invocation. Here we invoke it potentially
3016 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3018 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3020 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3021 retval = ERROR_JTAG_QUEUE_FAILED;
3026 /* there are no state transitions in this code, so omit state tracking */
3028 /* command "Clock Data to TMS/CS Pin (no Read)" */
3032 buffer_write(bitcount_per_command - 1);
3034 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3039 num_cycles -= bitcount_per_command;
3045 /* ---------------------------------------------------------------------
3046 * Support for IceBear JTAG adapter from Section5:
3047 * http://section5.ch/icebear
3049 * Author: Sten, debian@sansys-electronic.com
3052 /* Icebear pin layout
3054 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3055 * GND GND | 4 3| n.c.
3056 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3057 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3058 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3059 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3060 * ADBUS2 TDO |14 13| GND GND
3062 * ADBUS0 O L TCK ACBUS0 GND
3063 * ADBUS1 O L TDI ACBUS1 GND
3064 * ADBUS2 I TDO ACBUS2 n.c.
3065 * ADBUS3 O H TMS ACBUS3 n.c.
3071 static int icebear_jtag_init(void) {
3073 uint32_t bytes_written;
3075 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3076 low_output = 0x08; /* high: TMS; low: TCK TDI */
3080 enum reset_types jtag_reset_config = jtag_get_reset_config();
3081 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3082 low_direction &= ~nTRST; /* nTRST high impedance */
3085 low_direction |= nTRST;
3086 low_output |= nTRST;
3089 low_direction |= nSRST;
3090 low_output |= nSRST;
3092 /* initialize low byte for jtag */
3093 buf[0] = 0x80; /* command "set data bits low byte" */
3094 buf[1] = low_output;
3095 buf[2] = low_direction;
3096 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3098 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3099 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3100 return ERROR_JTAG_INIT_FAILED;
3104 high_direction = 0x00;
3107 /* initialize high port */
3108 buf[0] = 0x82; /* command "set data bits high byte" */
3109 buf[1] = high_output; /* value */
3110 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3111 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3113 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3114 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3115 return ERROR_JTAG_INIT_FAILED;
3121 static void icebear_jtag_reset(int trst, int srst) {
3124 low_direction |= nTRST;
3125 low_output &= ~nTRST;
3127 else if (trst == 0) {
3128 enum reset_types jtag_reset_config = jtag_get_reset_config();
3129 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3130 low_direction &= ~nTRST;
3132 low_output |= nTRST;
3136 low_output &= ~nSRST;
3138 else if (srst == 0) {
3139 low_output |= nSRST;
3142 /* command "set data bits low byte" */
3144 buffer_write(low_output);
3145 buffer_write(low_direction);
3147 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3150 /* ---------------------------------------------------------------------
3151 * Support for Signalyzer H2 and Signalyzer H4
3152 * JTAG adapter from Xverve Technologies Inc.
3153 * http://www.signalyzer.com or http://www.xverve.com
3155 * Author: Oleg Seiljus, oleg@signalyzer.com
3157 static unsigned char signalyzer_h_side;
3158 static unsigned int signalyzer_h_adapter_type;
3160 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3162 #if BUILD_FT2232_FTD2XX == 1
3163 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3166 #define SIGNALYZER_COMMAND_ADDR 128
3167 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3169 #define SIGNALYZER_COMMAND_VERSION 0x41
3170 #define SIGNALYZER_COMMAND_RESET 0x42
3171 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3172 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3173 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3174 #define SIGNALYZER_COMMAND_LED_SET 0x53
3175 #define SIGNALYZER_COMMAND_ADC 0x54
3176 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3177 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3178 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3179 #define SIGNALYZER_COMMAND_I2C 0x58
3181 #define SIGNALYZER_CHAN_A 1
3182 #define SIGNALYZER_CHAN_B 2
3183 /* LEDS use channel C */
3184 #define SIGNALYZER_CHAN_C 4
3186 #define SIGNALYZER_LED_GREEN 1
3187 #define SIGNALYZER_LED_RED 2
3189 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3190 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3191 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3192 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3193 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3196 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3198 #if BUILD_FT2232_FTD2XX == 1
3199 return FT_WriteEE(ftdih, address, value);
3200 #elif BUILD_FT2232_LIBFTDI == 1
3205 #if BUILD_FT2232_FTD2XX == 1
3206 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3208 return FT_ReadEE(ftdih, address, value);
3212 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3213 int on_time_ms, int off_time_ms, unsigned char cycles)
3215 unsigned char on_time;
3216 unsigned char off_time;
3218 if (on_time_ms < 0xFFFF)
3219 on_time = (unsigned char)(on_time_ms / 62);
3223 off_time = (unsigned char)(off_time_ms / 62);
3225 #if BUILD_FT2232_FTD2XX == 1
3228 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3229 ((uint32_t)(channel << 8) | led))) != FT_OK)
3231 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3232 return ERROR_JTAG_DEVICE_ERROR;
3235 if ((status = signalyzer_h_ctrl_write(
3236 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3237 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3239 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3240 return ERROR_JTAG_DEVICE_ERROR;
3243 if ((status = signalyzer_h_ctrl_write(
3244 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3245 ((uint32_t)cycles))) != FT_OK)
3247 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3248 return ERROR_JTAG_DEVICE_ERROR;
3251 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3252 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3254 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3255 return ERROR_JTAG_DEVICE_ERROR;
3259 #elif BUILD_FT2232_LIBFTDI == 1
3262 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3263 ((uint32_t)(channel << 8) | led))) < 0)
3265 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3266 ftdi_get_error_string(&ftdic));
3267 return ERROR_JTAG_DEVICE_ERROR;
3270 if ((retval = signalyzer_h_ctrl_write(
3271 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3272 ((uint32_t)(on_time << 8) | off_time))) < 0)
3274 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3275 ftdi_get_error_string(&ftdic));
3276 return ERROR_JTAG_DEVICE_ERROR;
3279 if ((retval = signalyzer_h_ctrl_write(
3280 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3281 (uint32_t)cycles)) < 0)
3283 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3284 ftdi_get_error_string(&ftdic));
3285 return ERROR_JTAG_DEVICE_ERROR;
3288 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3289 SIGNALYZER_COMMAND_LED_SET)) < 0)
3291 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3292 ftdi_get_error_string(&ftdic));
3293 return ERROR_JTAG_DEVICE_ERROR;
3300 static int signalyzer_h_init(void)
3302 #if BUILD_FT2232_FTD2XX == 1
3309 uint16_t read_buf[12] = { 0 };
3311 uint32_t bytes_written;
3313 /* turn on center green led */
3314 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3315 0xFFFF, 0x00, 0x00);
3317 /* determine what channel config wants to open
3318 * TODO: change me... current implementation is made to work
3319 * with openocd description parsing.
3321 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3325 signalyzer_h_side = *(end_of_desc - 1);
3326 if (signalyzer_h_side == 'B')
3327 signalyzer_h_side = SIGNALYZER_CHAN_B;
3329 signalyzer_h_side = SIGNALYZER_CHAN_A;
3333 LOG_ERROR("No Channel was specified");
3337 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3340 #if BUILD_FT2232_FTD2XX == 1
3341 /* read signalyzer versionining information */
3342 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3343 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3345 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3346 return ERROR_JTAG_DEVICE_ERROR;
3349 for (i = 0; i < 10; i++)
3351 if ((status = signalyzer_h_ctrl_read(
3352 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3353 &read_buf[i])) != FT_OK)
3355 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3357 return ERROR_JTAG_DEVICE_ERROR;
3361 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3362 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3363 read_buf[4], read_buf[5], read_buf[6]);
3365 /* set gpio register */
3366 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3367 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3369 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3370 return ERROR_JTAG_DEVICE_ERROR;
3373 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3376 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3377 return ERROR_JTAG_DEVICE_ERROR;
3380 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3381 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3383 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3384 return ERROR_JTAG_DEVICE_ERROR;
3387 /* read adapter type information */
3388 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3389 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3391 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3392 return ERROR_JTAG_DEVICE_ERROR;
3395 if ((status = signalyzer_h_ctrl_write(
3396 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3398 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3399 return ERROR_JTAG_DEVICE_ERROR;
3402 if ((status = signalyzer_h_ctrl_write(
3403 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3405 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3406 return ERROR_JTAG_DEVICE_ERROR;
3409 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3410 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3412 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3413 return ERROR_JTAG_DEVICE_ERROR;
3418 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3419 &read_buf[0])) != FT_OK)
3421 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3422 return ERROR_JTAG_DEVICE_ERROR;
3425 if (read_buf[0] != 0x0498)
3426 signalyzer_h_adapter_type = 0x0000;
3429 for (i = 0; i < 4; i++)
3431 if ((status = signalyzer_h_ctrl_read(
3432 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3433 &read_buf[i])) != FT_OK)
3435 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3437 return ERROR_JTAG_DEVICE_ERROR;
3441 signalyzer_h_adapter_type = read_buf[0];
3444 #elif BUILD_FT2232_LIBFTDI == 1
3445 /* currently libftdi does not allow reading individual eeprom
3446 * locations, therefore adapter type cannot be detected.
3447 * override with most common type
3449 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3452 enum reset_types jtag_reset_config = jtag_get_reset_config();
3454 /* ADAPTOR: EM_LT16_A */
3455 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3457 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3458 "detected. (HW: %2x).", (read_buf[1] >> 8));
3466 low_direction = 0x1b;
3469 high_direction = 0x0;
3471 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3473 low_direction &= ~nTRSTnOE; /* nTRST input */
3474 low_output &= ~nTRST; /* nTRST = 0 */
3478 low_direction |= nTRSTnOE; /* nTRST output */
3479 low_output |= nTRST; /* nTRST = 1 */
3482 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3484 low_direction |= nSRSTnOE; /* nSRST output */
3485 low_output |= nSRST; /* nSRST = 1 */
3489 low_direction &= ~nSRSTnOE; /* nSRST input */
3490 low_output &= ~nSRST; /* nSRST = 0 */
3493 #if BUILD_FT2232_FTD2XX == 1
3494 /* enable power to the module */
3495 if ((status = signalyzer_h_ctrl_write(
3496 SIGNALYZER_DATA_BUFFER_ADDR,
3497 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3500 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3502 return ERROR_JTAG_DEVICE_ERROR;
3505 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3506 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3508 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3510 return ERROR_JTAG_DEVICE_ERROR;
3513 /* set gpio mode register */
3514 if ((status = signalyzer_h_ctrl_write(
3515 SIGNALYZER_DATA_BUFFER_ADDR,
3516 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3518 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3520 return ERROR_JTAG_DEVICE_ERROR;
3523 if ((status = signalyzer_h_ctrl_write(
3524 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3527 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3529 return ERROR_JTAG_DEVICE_ERROR;
3532 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3533 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3535 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3537 return ERROR_JTAG_DEVICE_ERROR;
3540 /* set gpio register */
3541 if ((status = signalyzer_h_ctrl_write(
3542 SIGNALYZER_DATA_BUFFER_ADDR,
3543 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3545 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3547 return ERROR_JTAG_DEVICE_ERROR;
3550 if ((status = signalyzer_h_ctrl_write(
3551 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3554 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3556 return ERROR_JTAG_DEVICE_ERROR;
3559 if ((status = signalyzer_h_ctrl_write(
3560 SIGNALYZER_COMMAND_ADDR,
3561 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3563 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3565 return ERROR_JTAG_DEVICE_ERROR;
3570 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3571 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3572 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3573 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3574 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3576 if (signalyzer_h_adapter_type
3577 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3578 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3579 "detected. (HW: %2x).", (read_buf[1] >> 8));
3580 else if (signalyzer_h_adapter_type
3581 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3582 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3583 "(ARM JTAG with PSU) detected. (HW: %2x).",
3584 (read_buf[1] >> 8));
3585 else if (signalyzer_h_adapter_type
3586 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3587 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3588 "detected. (HW: %2x).", (read_buf[1] >> 8));
3589 else if (signalyzer_h_adapter_type
3590 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3591 LOG_INFO("Signalyzer: EM-JTAG-P "
3592 "(Generic JTAG with PSU) detected. (HW: %2x).",
3593 (read_buf[1] >> 8));
3601 low_direction = 0x1b;
3604 high_direction = 0x1f;
3606 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3608 high_output |= nTRSTnOE;
3609 high_output &= ~nTRST;
3613 high_output &= ~nTRSTnOE;
3614 high_output |= nTRST;
3617 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3619 high_output &= ~nSRSTnOE;
3620 high_output |= nSRST;
3624 high_output |= nSRSTnOE;
3625 high_output &= ~nSRST;
3628 #if BUILD_FT2232_FTD2XX == 1
3629 /* enable power to the module */
3630 if ((status = signalyzer_h_ctrl_write(
3631 SIGNALYZER_DATA_BUFFER_ADDR,
3632 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3635 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3637 return ERROR_JTAG_DEVICE_ERROR;
3640 if ((status = signalyzer_h_ctrl_write(
3641 SIGNALYZER_COMMAND_ADDR,
3642 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3644 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3646 return ERROR_JTAG_DEVICE_ERROR;
3649 /* set gpio mode register (IO_16 and IO_17 set as analog
3650 * inputs, other is gpio)
3652 if ((status = signalyzer_h_ctrl_write(
3653 SIGNALYZER_DATA_BUFFER_ADDR,
3654 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3656 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3658 return ERROR_JTAG_DEVICE_ERROR;
3661 if ((status = signalyzer_h_ctrl_write(
3662 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3665 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3667 return ERROR_JTAG_DEVICE_ERROR;
3670 if ((status = signalyzer_h_ctrl_write(
3671 SIGNALYZER_COMMAND_ADDR,
3672 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3674 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3676 return ERROR_JTAG_DEVICE_ERROR;
3679 /* set gpio register (all inputs, for -P modules,
3680 * PSU will be turned off)
3682 if ((status = signalyzer_h_ctrl_write(
3683 SIGNALYZER_DATA_BUFFER_ADDR,
3684 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3686 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3688 return ERROR_JTAG_DEVICE_ERROR;
3691 if ((status = signalyzer_h_ctrl_write(
3692 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3695 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3697 return ERROR_JTAG_DEVICE_ERROR;
3700 if ((status = signalyzer_h_ctrl_write(
3701 SIGNALYZER_COMMAND_ADDR,
3702 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3704 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3706 return ERROR_JTAG_DEVICE_ERROR;
3711 else if (signalyzer_h_adapter_type == 0x0000)
3713 LOG_INFO("Signalyzer: No external modules were detected.");
3721 low_direction = 0x1b;
3724 high_direction = 0x0;
3726 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3728 low_direction &= ~nTRSTnOE; /* nTRST input */
3729 low_output &= ~nTRST; /* nTRST = 0 */
3733 low_direction |= nTRSTnOE; /* nTRST output */
3734 low_output |= nTRST; /* nTRST = 1 */
3737 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3739 low_direction |= nSRSTnOE; /* nSRST output */
3740 low_output |= nSRST; /* nSRST = 1 */
3744 low_direction &= ~nSRSTnOE; /* nSRST input */
3745 low_output &= ~nSRST; /* nSRST = 0 */
3750 LOG_ERROR("Unknown module type is detected: %.4x",
3751 signalyzer_h_adapter_type);
3752 return ERROR_JTAG_DEVICE_ERROR;
3755 /* initialize low byte of controller for jtag operation */
3757 buf[1] = low_output;
3758 buf[2] = low_direction;
3760 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
3761 || (bytes_written != 3))
3763 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3764 return ERROR_JTAG_INIT_FAILED;
3767 #if BUILD_FT2232_FTD2XX == 1
3768 if (ftdi_device == FT_DEVICE_2232H)
3770 /* initialize high byte of controller for jtag operation */
3772 buf[1] = high_output;
3773 buf[2] = high_direction;
3775 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3776 || (bytes_written != 3))
3778 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3779 return ERROR_JTAG_INIT_FAILED;
3782 #elif BUILD_FT2232_LIBFTDI == 1
3783 if (ftdi_device == TYPE_2232H)
3785 /* initialize high byte of controller for jtag operation */
3787 buf[1] = high_output;
3788 buf[2] = high_direction;
3790 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3791 || (bytes_written != 3))
3793 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3794 return ERROR_JTAG_INIT_FAILED;
3801 static void signalyzer_h_reset(int trst, int srst)
3803 enum reset_types jtag_reset_config = jtag_get_reset_config();
3805 /* ADAPTOR: EM_LT16_A */
3806 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3810 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3811 /* switch to output pin (output is low) */
3812 low_direction |= nTRSTnOE;
3814 /* switch output low */
3815 low_output &= ~nTRST;
3819 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3820 /* switch to input pin (high-Z + internal
3821 * and external pullup) */
3822 low_direction &= ~nTRSTnOE;
3824 /* switch output high */
3825 low_output |= nTRST;
3830 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3831 /* switch output low */
3832 low_output &= ~nSRST;
3834 /* switch to output pin (output is low) */
3835 low_direction |= nSRSTnOE;
3839 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3840 /* switch output high */
3841 low_output |= nSRST;
3843 /* switch to input pin (high-Z) */
3844 low_direction &= ~nSRSTnOE;
3847 /* command "set data bits low byte" */
3849 buffer_write(low_output);
3850 buffer_write(low_direction);
3851 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3852 "low_direction: 0x%2.2x",
3853 trst, srst, low_output, low_direction);
3855 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3856 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3857 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3858 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3859 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3863 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3864 high_output &= ~nTRSTnOE;
3866 high_output &= ~nTRST;
3870 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3871 high_output |= nTRSTnOE;
3873 high_output |= nTRST;
3878 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3879 high_output &= ~nSRST;
3881 high_output &= ~nSRSTnOE;
3885 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3886 high_output |= nSRST;
3888 high_output |= nSRSTnOE;
3891 /* command "set data bits high byte" */
3893 buffer_write(high_output);
3894 buffer_write(high_direction);
3895 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
3896 "high_direction: 0x%2.2x",
3897 trst, srst, high_output, high_direction);
3899 else if (signalyzer_h_adapter_type == 0x0000)
3903 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3904 /* switch to output pin (output is low) */
3905 low_direction |= nTRSTnOE;
3907 /* switch output low */
3908 low_output &= ~nTRST;
3912 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3913 /* switch to input pin (high-Z + internal
3914 * and external pullup) */
3915 low_direction &= ~nTRSTnOE;
3917 /* switch output high */
3918 low_output |= nTRST;
3923 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3924 /* switch output low */
3925 low_output &= ~nSRST;
3927 /* switch to output pin (output is low) */
3928 low_direction |= nSRSTnOE;
3932 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3933 /* switch output high */
3934 low_output |= nSRST;
3936 /* switch to input pin (high-Z) */
3937 low_direction &= ~nSRSTnOE;
3940 /* command "set data bits low byte" */
3942 buffer_write(low_output);
3943 buffer_write(low_direction);
3944 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3945 "low_direction: 0x%2.2x",
3946 trst, srst, low_output, low_direction);
3950 static void signalyzer_h_blink(void)
3952 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
3955 /********************************************************************
3956 * Support for KT-LINK
3957 * JTAG adapter from KRISTECH
3958 * http://www.kristech.eu
3959 *******************************************************************/
3960 static int ktlink_init(void)
3963 uint32_t bytes_written;
3964 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
3966 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
3967 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
3969 // initialize low port
3970 buf[0] = 0x80; // command "set data bits low byte"
3971 buf[1] = low_output;
3972 buf[2] = low_direction;
3973 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3975 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3977 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3978 return ERROR_JTAG_INIT_FAILED;
3986 high_output = 0x80; // turn LED on
3987 high_direction = 0xFF; // all outputs
3989 enum reset_types jtag_reset_config = jtag_get_reset_config();
3991 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3992 high_output |= nTRSTnOE;
3993 high_output &= ~nTRST;
3995 high_output &= ~nTRSTnOE;
3996 high_output |= nTRST;
3999 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4000 high_output &= ~nSRSTnOE;
4001 high_output |= nSRST;
4003 high_output |= nSRSTnOE;
4004 high_output &= ~nSRST;
4007 // initialize high port
4008 buf[0] = 0x82; // command "set data bits high byte"
4009 buf[1] = high_output; // value
4010 buf[2] = high_direction;
4011 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4013 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
4015 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4016 return ERROR_JTAG_INIT_FAILED;
4022 static void ktlink_reset(int trst, int srst)
4024 enum reset_types jtag_reset_config = jtag_get_reset_config();
4027 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4028 high_output &= ~nTRSTnOE;
4030 high_output &= ~nTRST;
4031 } else if (trst == 0) {
4032 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4033 high_output |= nTRSTnOE;
4035 high_output |= nTRST;
4039 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4040 high_output &= ~nSRST;
4042 high_output &= ~nSRSTnOE;
4043 } else if (srst == 0) {
4044 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4045 high_output |= nSRST;
4047 high_output |= nSRSTnOE;
4050 buffer_write(0x82); // command "set data bits high byte"
4051 buffer_write(high_output);
4052 buffer_write(high_direction);
4053 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4056 static void ktlink_blink(void)
4058 /* LED connected to ACBUS7 */
4059 if (high_output & 0x80)
4060 high_output &= 0x7F;
4062 high_output |= 0x80;
4064 buffer_write(0x82); // command "set data bits high byte"
4065 buffer_write(high_output);
4066 buffer_write(high_direction);
4069 static const struct command_registration ft2232_command_handlers[] = {
4071 .name = "ft2232_device_desc",
4072 .handler = &ft2232_handle_device_desc_command,
4073 .mode = COMMAND_CONFIG,
4074 .help = "set the USB device description of the FTDI FT2232 device",
4075 .usage = "description_string",
4078 .name = "ft2232_serial",
4079 .handler = &ft2232_handle_serial_command,
4080 .mode = COMMAND_CONFIG,
4081 .help = "set the serial number of the FTDI FT2232 device",
4082 .usage = "serial_string",
4085 .name = "ft2232_layout",
4086 .handler = &ft2232_handle_layout_command,
4087 .mode = COMMAND_CONFIG,
4088 .help = "set the layout of the FT2232 GPIO signals used "
4089 "to control output-enables and reset signals",
4090 .usage = "layout_name",
4093 .name = "ft2232_vid_pid",
4094 .handler = &ft2232_handle_vid_pid_command,
4095 .mode = COMMAND_CONFIG,
4096 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4097 .usage = "(vid pid)* ",
4100 .name = "ft2232_latency",
4101 .handler = &ft2232_handle_latency_command,
4102 .mode = COMMAND_CONFIG,
4103 .help = "set the FT2232 latency timer to a new value",
4106 COMMAND_REGISTRATION_DONE
4109 struct jtag_interface ft2232_interface = {
4111 .commands = ft2232_command_handlers,
4113 .init = ft2232_init,
4114 .quit = ft2232_quit,
4115 .speed = ft2232_speed,
4116 .speed_div = ft2232_speed_div,
4118 .execute_queue = ft2232_execute_queue,