Added support for the Lisa/L jtag LEDs.
[openocd/cortex.git] / src / jtag / drivers / ft2232.c
blob7440f0c951c1cdd55e10dbf9175bc9da1cab8c27
1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
7 * *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
30 /**
31 * @file
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods.
37 * JTAG uses only one of the two communications channels ("MPSSE engines")
38 * on these devices. Adapters based on FT4232 parts have four ports/channels
39 * (A/B/C/D), instead of just two (A/B).
41 * Especially on development boards integrating one of these chips (as
42 * opposed to discrete pods/dongles), the additional channels can be used
43 * for a variety of purposes, but OpenOCD only uses one channel at a time.
45 * - As a USB-to-serial adapter for the target's console UART ...
46 * which may be able to support ROM boot loaders that load initial
47 * firmware images to flash (or SRAM).
49 * - On systems which support ARM's SWD in addition to JTAG, or instead
50 * of it, that second port can be used for reading SWV/SWO trace data.
52 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
54 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
55 * request/response interactions involve round trips over the USB link.
56 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
57 * can for example poll quickly for a status change (usually taking on the
58 * order of microseconds not milliseconds) before beginning a queued
59 * transaction which require the previous one to have completed.
61 * There are dozens of adapters of this type, differing in details which
62 * this driver needs to understand. Those "layout" details are required
63 * as part of FT2232 driver configuration.
65 * This code uses information contained in the MPSSE specification which was
66 * found here:
67 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
68 * Hereafter this is called the "MPSSE Spec".
70 * The datasheet for the ftdichip.com's FT2232D part is here:
71 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
73 * Also note the issue with code 0x4b (clock data to TMS) noted in
74 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
75 * which can affect longer JTAG state paths.
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <jtag/transport.h>
85 #include <helper/time_support.h>
87 #if IS_CYGWIN == 1
88 #include <windows.h>
89 #endif
91 #include <assert.h>
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
97 #endif
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
101 #include <ftd2xx.h>
103 enum ftdi_interface
105 INTERFACE_ANY = 0,
106 INTERFACE_A = 1,
107 INTERFACE_B = 2,
108 INTERFACE_C = 3,
109 INTERFACE_D = 4
112 #elif BUILD_FT2232_LIBFTDI == 1
113 #include <ftdi.h>
114 #endif
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_2232H_4232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
124 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125 * errors with a retry count of 100. Increasing it solves the problem for me.
126 * - Dimitar
128 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
130 * to something sane.
132 #define LIBFTDI_READ_RETRY_COUNT 2000
134 #ifndef BUILD_FT2232_HIGHSPEED
135 #if BUILD_FT2232_FTD2XX == 1
136 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
137 #elif BUILD_FT2232_LIBFTDI == 1
138 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
139 #endif
140 #endif
143 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
144 * stable state. Calling code must ensure that current state is stable,
145 * that verification is not done in here.
147 * @param num_cycles The number of clocks cycles to send.
148 * @param cmd The command to send.
150 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
154 static char * ft2232_device_desc_A = NULL;
155 static char* ft2232_device_desc = NULL;
156 static char* ft2232_serial = NULL;
157 static uint8_t ft2232_latency = 2;
158 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
160 #define MAX_USB_IDS 8
161 /* vid = pid = 0 marks the end of the list */
162 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
163 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165 struct ft2232_layout {
166 char* name;
167 int (*init)(void);
168 void (*reset)(int trst, int srst);
169 void (*blink)(void);
170 int channel;
173 /* init procedures for supported layouts */
174 static int usbjtag_init(void);
175 static int jtagkey_init(void);
176 static int lm3s811_jtag_init(void);
177 static int icdi_jtag_init(void);
178 static int olimex_jtag_init(void);
179 static int flyswatter_init(void);
180 static int turtle_init(void);
181 static int comstick_init(void);
182 static int stm32stick_init(void);
183 static int axm0432_jtag_init(void);
184 static int sheevaplug_init(void);
185 static int icebear_jtag_init(void);
186 static int cortino_jtag_init(void);
187 static int signalyzer_init(void);
188 static int signalyzer_h_init(void);
189 static int ktlink_init(void);
190 static int redbee_init(void);
191 static int lisa_l_init(void);
193 /* reset procedures for supported layouts */
194 static void ftx23_reset(int trst, int srst);
195 static void jtagkey_reset(int trst, int srst);
196 static void olimex_jtag_reset(int trst, int srst);
197 static void flyswatter_reset(int trst, int srst);
198 static void turtle_reset(int trst, int srst);
199 static void comstick_reset(int trst, int srst);
200 static void stm32stick_reset(int trst, int srst);
201 static void axm0432_jtag_reset(int trst, int srst);
202 static void sheevaplug_reset(int trst, int srst);
203 static void icebear_jtag_reset(int trst, int srst);
204 static void signalyzer_h_reset(int trst, int srst);
205 static void ktlink_reset(int trst, int srst);
206 static void redbee_reset(int trst, int srst);
208 /* blink procedures for layouts that support a blinking led */
209 static void olimex_jtag_blink(void);
210 static void flyswatter_jtag_blink(void);
211 static void turtle_jtag_blink(void);
212 static void signalyzer_h_blink(void);
213 static void ktlink_blink(void);
214 static void lisa_l_blink(void);
216 /* common transport support options */
218 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
220 static const struct ft2232_layout ft2232_layouts[] =
222 { .name = "usbjtag",
223 .init = usbjtag_init,
224 .reset = ftx23_reset,
226 { .name = "jtagkey",
227 .init = jtagkey_init,
228 .reset = jtagkey_reset,
230 { .name = "jtagkey_prototype_v1",
231 .init = jtagkey_init,
232 .reset = jtagkey_reset,
234 { .name = "oocdlink",
235 .init = jtagkey_init,
236 .reset = jtagkey_reset,
238 { .name = "signalyzer",
239 .init = signalyzer_init,
240 .reset = ftx23_reset,
242 { .name = "evb_lm3s811",
243 .init = lm3s811_jtag_init,
244 .reset = ftx23_reset,
246 { .name = "luminary_icdi",
247 .init = icdi_jtag_init,
248 .reset = ftx23_reset,
250 { .name = "olimex-jtag",
251 .init = olimex_jtag_init,
252 .reset = olimex_jtag_reset,
253 .blink = olimex_jtag_blink
255 { .name = "flyswatter",
256 .init = flyswatter_init,
257 .reset = flyswatter_reset,
258 .blink = flyswatter_jtag_blink
260 { .name = "turtelizer2",
261 .init = turtle_init,
262 .reset = turtle_reset,
263 .blink = turtle_jtag_blink
265 { .name = "comstick",
266 .init = comstick_init,
267 .reset = comstick_reset,
269 { .name = "stm32stick",
270 .init = stm32stick_init,
271 .reset = stm32stick_reset,
273 { .name = "axm0432_jtag",
274 .init = axm0432_jtag_init,
275 .reset = axm0432_jtag_reset,
277 { .name = "sheevaplug",
278 .init = sheevaplug_init,
279 .reset = sheevaplug_reset,
281 { .name = "icebear",
282 .init = icebear_jtag_init,
283 .reset = icebear_jtag_reset,
285 { .name = "cortino",
286 .init = cortino_jtag_init,
287 .reset = comstick_reset,
289 { .name = "signalyzer-h",
290 .init = signalyzer_h_init,
291 .reset = signalyzer_h_reset,
292 .blink = signalyzer_h_blink
294 { .name = "ktlink",
295 .init = ktlink_init,
296 .reset = ktlink_reset,
297 .blink = ktlink_blink
299 { .name = "redbee-econotag",
300 .init = redbee_init,
301 .reset = redbee_reset,
303 { .name = "redbee-usb",
304 .init = redbee_init,
305 .reset = redbee_reset,
306 .channel = INTERFACE_B,
308 { .name = "lisa-l",
309 .init = lisa_l_init,
310 .reset = ftx23_reset,
311 .blink = lisa_l_blink,
312 .channel = INTERFACE_B,
314 { .name = NULL, /* END OF TABLE */ },
317 /* bitmask used to drive nTRST; usually a GPIOLx signal */
318 static uint8_t nTRST;
319 static uint8_t nTRSTnOE;
320 /* bitmask used to drive nSRST; usually a GPIOLx signal */
321 static uint8_t nSRST;
322 static uint8_t nSRSTnOE;
324 /** the layout being used with this debug session */
325 static const struct ft2232_layout *layout;
327 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
328 static uint8_t low_output = 0x0;
330 /* note that direction bit == 1 means that signal is an output */
332 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
333 static uint8_t low_direction = 0x0;
334 /** default value bitmask for CBUS GPIOH(0..4) */
335 static uint8_t high_output = 0x0;
336 /** default direction bitmask for CBUS GPIOH(0..4) */
337 static uint8_t high_direction = 0x0;
339 #if BUILD_FT2232_FTD2XX == 1
340 static FT_HANDLE ftdih = NULL;
341 static FT_DEVICE ftdi_device = 0;
342 #elif BUILD_FT2232_LIBFTDI == 1
343 static struct ftdi_context ftdic;
344 static enum ftdi_chip_type ftdi_device;
345 #endif
347 static struct jtag_command* first_unsent; /* next command that has to be sent */
348 static int require_send;
350 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
352 "There is a significant difference between libftdi and libftd2xx. The latter
353 one allows to schedule up to 64*64 bytes of result data while libftdi fails
354 with more than 4*64. As a consequence, the FT2232 driver is forced to
355 perform around 16x more USB transactions for long command streams with TDO
356 capture when running with libftdi."
358 No idea how we get
359 #define FT2232_BUFFER_SIZE 131072
360 a comment would have been nice.
363 #define FT2232_BUFFER_SIZE 131072
365 static uint8_t* ft2232_buffer = NULL;
366 static int ft2232_buffer_size = 0;
367 static int ft2232_read_pointer = 0;
368 static int ft2232_expect_read = 0;
371 * Function buffer_write
372 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
373 * @param val is the byte to send.
375 static inline void buffer_write(uint8_t val)
377 assert(ft2232_buffer);
378 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
379 ft2232_buffer[ft2232_buffer_size++] = val;
383 * Function buffer_read
384 * returns a byte from the byte buffer.
386 static inline uint8_t buffer_read(void)
388 assert(ft2232_buffer);
389 assert(ft2232_read_pointer < ft2232_buffer_size);
390 return ft2232_buffer[ft2232_read_pointer++];
394 * Clocks out \a bit_count bits on the TMS line, starting with the least
395 * significant bit of tms_bits and progressing to more significant bits.
396 * Rigorous state transition logging is done here via tap_set_state().
398 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
399 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
400 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
401 * is often used for this, 0x4b.
403 * @param tms_bits Holds the sequence of bits to send.
404 * @param tms_count Tells how many bits in the sequence.
405 * @param tdi_bit A single bit to pass on to TDI before the first TCK
406 * cycle and held static for the duration of TMS clocking.
408 * See the MPSSE spec referenced above.
410 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
412 uint8_t tms_byte;
413 int i;
414 int tms_ndx; /* bit index into tms_byte */
416 assert(tms_count > 0);
418 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
419 mpsse_cmd, tms_bits, tms_count);
421 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
423 bool bit = tms_bits & 1;
425 if (bit)
426 tms_byte |= (1 << tms_ndx);
428 /* always do state transitions in public view */
429 tap_set_state(tap_state_transition(tap_get_state(), bit));
431 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
432 also increment.
434 ++tms_ndx;
436 if (tms_ndx == 7 || i == tms_count-1)
438 buffer_write(mpsse_cmd);
439 buffer_write(tms_ndx - 1);
441 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
442 TMS/CS and is held static for the duration of TMS/CS clocking.
444 buffer_write(tms_byte | (tdi_bit << 7));
450 * Function get_tms_buffer_requirements
451 * returns what clock_tms() will consume if called with
452 * same \a bit_count.
454 static inline int get_tms_buffer_requirements(int bit_count)
456 return ((bit_count + 6)/7) * 3;
460 * Function move_to_state
461 * moves the TAP controller from the current state to a
462 * \a goal_state through a path given by tap_get_tms_path(). State transition
463 * logging is performed by delegation to clock_tms().
465 * @param goal_state is the destination state for the move.
467 static void move_to_state(tap_state_t goal_state)
469 tap_state_t start_state = tap_get_state();
471 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
472 lookup of the required TMS pattern to move to this state from the
473 start state.
476 /* do the 2 lookups */
477 int tms_bits = tap_get_tms_path(start_state, goal_state);
478 int tms_count = tap_get_tms_path_len(start_state, goal_state);
480 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
482 clock_tms(0x4b, tms_bits, tms_count, 0);
485 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
487 #if BUILD_FT2232_FTD2XX == 1
488 FT_STATUS status;
489 DWORD dw_bytes_written;
490 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
492 *bytes_written = dw_bytes_written;
493 LOG_ERROR("FT_Write returned: %lu", status);
494 return ERROR_JTAG_DEVICE_ERROR;
496 else
498 *bytes_written = dw_bytes_written;
500 #elif BUILD_FT2232_LIBFTDI == 1
501 int retval;
502 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
504 *bytes_written = 0;
505 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
506 return ERROR_JTAG_DEVICE_ERROR;
508 else
510 *bytes_written = retval;
512 #endif
514 if (*bytes_written != (uint32_t)size)
516 return ERROR_JTAG_DEVICE_ERROR;
519 return ERROR_OK;
522 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
524 #if BUILD_FT2232_FTD2XX == 1
525 DWORD dw_bytes_read;
526 FT_STATUS status;
527 int timeout = 5;
528 *bytes_read = 0;
530 while ((*bytes_read < size) && timeout--)
532 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
533 *bytes_read, &dw_bytes_read)) != FT_OK)
535 *bytes_read = 0;
536 LOG_ERROR("FT_Read returned: %lu", status);
537 return ERROR_JTAG_DEVICE_ERROR;
539 *bytes_read += dw_bytes_read;
542 #elif BUILD_FT2232_LIBFTDI == 1
543 int retval;
544 int timeout = LIBFTDI_READ_RETRY_COUNT;
545 *bytes_read = 0;
547 while ((*bytes_read < size) && timeout--)
549 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
551 *bytes_read = 0;
552 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
553 return ERROR_JTAG_DEVICE_ERROR;
555 *bytes_read += retval;
558 #endif
560 if (*bytes_read < size)
562 LOG_ERROR("couldn't read enough bytes from "
563 "FT2232 device (%i < %i)",
564 (unsigned)*bytes_read,
565 (unsigned)size);
566 return ERROR_JTAG_DEVICE_ERROR;
569 return ERROR_OK;
572 static bool ft2232_device_is_highspeed(void)
574 #if BUILD_FT2232_FTD2XX == 1
575 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
576 #elif BUILD_FT2232_LIBFTDI == 1
577 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
578 #endif
582 * Commands that only apply to the FT2232H and FT4232H devices.
583 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
584 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
587 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
589 uint8_t buf = enable ? 0x96 : 0x97;
590 LOG_DEBUG("%2.2x", buf);
592 uint32_t bytes_written;
593 int retval;
595 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
597 LOG_ERROR("couldn't write command to %s adaptive clocking"
598 , enable ? "enable" : "disable");
599 return retval;
602 return ERROR_OK;
606 * Enable/disable the clk divide by 5 of the 60MHz master clock.
607 * This result in a JTAG clock speed range of 91.553Hz-6MHz
608 * respective 457.763Hz-30MHz.
610 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
612 uint32_t bytes_written;
613 uint8_t buf = enable ? 0x8b : 0x8a;
615 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
617 LOG_ERROR("couldn't write command to %s clk divide by 5"
618 , enable ? "enable" : "disable");
619 return ERROR_JTAG_INIT_FAILED;
621 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
622 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
624 return ERROR_OK;
627 static int ft2232_speed(int speed)
629 uint8_t buf[3];
630 int retval;
631 uint32_t bytes_written;
633 retval = ERROR_OK;
634 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
635 if (ft2232_device_is_highspeed())
636 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
637 else if (enable_adaptive_clocking)
639 LOG_ERROR("ft2232 device %lu does not support RTCK"
640 , (long unsigned int)ftdi_device);
641 return ERROR_FAIL;
644 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
645 return retval;
647 buf[0] = 0x86; /* command "set divisor" */
648 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
649 buf[2] = (speed >> 8) & 0xff; /* valueH */
651 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
652 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
654 LOG_ERROR("couldn't set FT2232 TCK speed");
655 return retval;
658 return ERROR_OK;
661 static int ft2232_speed_div(int speed, int* khz)
663 /* Take a look in the FT2232 manual,
664 * AN2232C-01 Command Processor for
665 * MPSSE and MCU Host Bus. Chapter 3.8 */
667 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
669 return ERROR_OK;
672 static int ft2232_khz(int khz, int* jtag_speed)
674 if (khz == 0)
676 if (ft2232_device_is_highspeed())
678 *jtag_speed = RTCK_SPEED;
679 return ERROR_OK;
681 else
683 LOG_DEBUG("RCLK not supported");
684 return ERROR_FAIL;
688 /* Take a look in the FT2232 manual,
689 * AN2232C-01 Command Processor for
690 * MPSSE and MCU Host Bus. Chapter 3.8
692 * We will calc here with a multiplier
693 * of 10 for better rounding later. */
695 /* Calc speed, (ft2232_max_tck / khz) - 1 */
696 /* Use 65000 for better rounding */
697 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
699 /* Add 0.9 for rounding */
700 *jtag_speed += 9;
702 /* Calc real speed */
703 *jtag_speed = *jtag_speed / 10;
705 /* Check if speed is greater than 0 */
706 if (*jtag_speed < 0)
708 *jtag_speed = 0;
711 /* Check max value */
712 if (*jtag_speed > 0xFFFF)
714 *jtag_speed = 0xFFFF;
717 return ERROR_OK;
720 static void ft2232_end_state(tap_state_t state)
722 if (tap_is_state_stable(state))
723 tap_set_end_state(state);
724 else
726 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
727 exit(-1);
731 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
733 int num_bytes = (scan_size + 7) / 8;
734 int bits_left = scan_size;
735 int cur_byte = 0;
737 while (num_bytes-- > 1)
739 buffer[cur_byte++] = buffer_read();
740 bits_left -= 8;
743 buffer[cur_byte] = 0x0;
745 /* There is one more partial byte left from the clock data in/out instructions */
746 if (bits_left > 1)
748 buffer[cur_byte] = buffer_read() >> 1;
750 /* 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 */
751 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
754 static void ft2232_debug_dump_buffer(void)
756 int i;
757 char line[256];
758 char* line_p = line;
760 for (i = 0; i < ft2232_buffer_size; i++)
762 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
763 if (i % 16 == 15)
765 LOG_DEBUG("%s", line);
766 line_p = line;
770 if (line_p != line)
771 LOG_DEBUG("%s", line);
774 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
776 struct jtag_command* cmd;
777 uint8_t* buffer;
778 int scan_size;
779 enum scan_type type;
780 int retval;
781 uint32_t bytes_written = 0;
782 uint32_t bytes_read = 0;
784 #ifdef _DEBUG_USB_IO_
785 struct timeval start, inter, inter2, end;
786 struct timeval d_inter, d_inter2, d_end;
787 #endif
789 #ifdef _DEBUG_USB_COMMS_
790 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
791 ft2232_debug_dump_buffer();
792 #endif
794 #ifdef _DEBUG_USB_IO_
795 gettimeofday(&start, NULL);
796 #endif
798 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
800 LOG_ERROR("couldn't write MPSSE commands to FT2232");
801 return retval;
804 #ifdef _DEBUG_USB_IO_
805 gettimeofday(&inter, NULL);
806 #endif
808 if (ft2232_expect_read)
810 /* FIXME this "timeout" is never changed ... */
811 int timeout = LIBFTDI_READ_RETRY_COUNT;
812 ft2232_buffer_size = 0;
814 #ifdef _DEBUG_USB_IO_
815 gettimeofday(&inter2, NULL);
816 #endif
818 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
820 LOG_ERROR("couldn't read from FT2232");
821 return retval;
824 #ifdef _DEBUG_USB_IO_
825 gettimeofday(&end, NULL);
827 timeval_subtract(&d_inter, &inter, &start);
828 timeval_subtract(&d_inter2, &inter2, &start);
829 timeval_subtract(&d_end, &end, &start);
831 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
832 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
833 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
834 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
835 #endif
837 ft2232_buffer_size = bytes_read;
839 if (ft2232_expect_read != ft2232_buffer_size)
841 LOG_ERROR("ft2232_expect_read (%i) != "
842 "ft2232_buffer_size (%i) "
843 "(%i retries)",
844 ft2232_expect_read,
845 ft2232_buffer_size,
846 LIBFTDI_READ_RETRY_COUNT - timeout);
847 ft2232_debug_dump_buffer();
849 exit(-1);
852 #ifdef _DEBUG_USB_COMMS_
853 LOG_DEBUG("read buffer (%i retries): %i bytes",
854 LIBFTDI_READ_RETRY_COUNT - timeout,
855 ft2232_buffer_size);
856 ft2232_debug_dump_buffer();
857 #endif
860 ft2232_expect_read = 0;
861 ft2232_read_pointer = 0;
863 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
864 * that wasn't handled by a caller-provided error handler
866 retval = ERROR_OK;
868 cmd = first;
869 while (cmd != last)
871 switch (cmd->type)
873 case JTAG_SCAN:
874 type = jtag_scan_type(cmd->cmd.scan);
875 if (type != SCAN_OUT)
877 scan_size = jtag_scan_size(cmd->cmd.scan);
878 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
879 ft2232_read_scan(type, buffer, scan_size);
880 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
881 retval = ERROR_JTAG_QUEUE_FAILED;
882 free(buffer);
884 break;
886 default:
887 break;
890 cmd = cmd->next;
893 ft2232_buffer_size = 0;
895 return retval;
899 * Function ft2232_add_pathmove
900 * moves the TAP controller from the current state to a new state through the
901 * given path, where path is an array of tap_state_t's.
903 * @param path is an array of tap_stat_t which gives the states to traverse through
904 * ending with the last state at path[num_states-1]
905 * @param num_states is the count of state steps to move through
907 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
909 int state_count = 0;
911 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
913 DEBUG_JTAG_IO("-");
915 /* this loop verifies that the path is legal and logs each state in the path */
916 while (num_states)
918 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
919 int bit_count = 0;
920 int num_states_batch = num_states > 7 ? 7 : num_states;
922 /* command "Clock Data to TMS/CS Pin (no Read)" */
923 buffer_write(0x4b);
925 /* number of states remaining */
926 buffer_write(num_states_batch - 1);
928 while (num_states_batch--) {
929 /* either TMS=0 or TMS=1 must work ... */
930 if (tap_state_transition(tap_get_state(), false)
931 == path[state_count])
932 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
933 else if (tap_state_transition(tap_get_state(), true)
934 == path[state_count])
935 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
937 /* ... or else the caller goofed BADLY */
938 else {
939 LOG_ERROR("BUG: %s -> %s isn't a valid "
940 "TAP state transition",
941 tap_state_name(tap_get_state()),
942 tap_state_name(path[state_count]));
943 exit(-1);
946 tap_set_state(path[state_count]);
947 state_count++;
948 num_states--;
951 buffer_write(tms_byte);
953 tap_set_end_state(tap_get_state());
956 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
958 int num_bytes = (scan_size + 7) / 8;
959 int bits_left = scan_size;
960 int cur_byte = 0;
961 int last_bit;
963 if (!ir_scan)
965 if (tap_get_state() != TAP_DRSHIFT)
967 move_to_state(TAP_DRSHIFT);
970 else
972 if (tap_get_state() != TAP_IRSHIFT)
974 move_to_state(TAP_IRSHIFT);
978 /* add command for complete bytes */
979 while (num_bytes > 1)
981 int thisrun_bytes;
982 if (type == SCAN_IO)
984 /* Clock Data Bytes In and Out LSB First */
985 buffer_write(0x39);
986 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
988 else if (type == SCAN_OUT)
990 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
991 buffer_write(0x19);
992 /* LOG_DEBUG("added TDI bytes (o)"); */
994 else if (type == SCAN_IN)
996 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
997 buffer_write(0x28);
998 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1001 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1002 num_bytes -= thisrun_bytes;
1004 buffer_write((uint8_t) (thisrun_bytes - 1));
1005 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1007 if (type != SCAN_IN)
1009 /* add complete bytes */
1010 while (thisrun_bytes-- > 0)
1012 buffer_write(buffer[cur_byte++]);
1013 bits_left -= 8;
1016 else /* (type == SCAN_IN) */
1018 bits_left -= 8 * (thisrun_bytes);
1022 /* the most signifcant bit is scanned during TAP movement */
1023 if (type != SCAN_IN)
1024 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1025 else
1026 last_bit = 0;
1028 /* process remaining bits but the last one */
1029 if (bits_left > 1)
1031 if (type == SCAN_IO)
1033 /* Clock Data Bits In and Out LSB First */
1034 buffer_write(0x3b);
1035 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1037 else if (type == SCAN_OUT)
1039 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1040 buffer_write(0x1b);
1041 /* LOG_DEBUG("added TDI bits (o)"); */
1043 else if (type == SCAN_IN)
1045 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1046 buffer_write(0x2a);
1047 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1050 buffer_write(bits_left - 2);
1051 if (type != SCAN_IN)
1052 buffer_write(buffer[cur_byte]);
1055 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1056 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1058 if (type == SCAN_IO)
1060 /* Clock Data Bits In and Out LSB First */
1061 buffer_write(0x3b);
1062 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1064 else if (type == SCAN_OUT)
1066 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1067 buffer_write(0x1b);
1068 /* LOG_DEBUG("added TDI bits (o)"); */
1070 else if (type == SCAN_IN)
1072 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1073 buffer_write(0x2a);
1074 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1076 buffer_write(0x0);
1077 buffer_write(last_bit);
1079 else
1081 int tms_bits;
1082 int tms_count;
1083 uint8_t mpsse_cmd;
1085 /* move from Shift-IR/DR to end state */
1086 if (type != SCAN_OUT)
1088 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1089 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1090 tms_bits = 0x01;
1091 tms_count = 2;
1092 /* Clock Data to TMS/CS Pin with Read */
1093 mpsse_cmd = 0x6b;
1095 else
1097 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1098 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1099 /* Clock Data to TMS/CS Pin (no Read) */
1100 mpsse_cmd = 0x4b;
1103 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1104 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1107 if (tap_get_state() != tap_get_end_state())
1109 move_to_state(tap_get_end_state());
1113 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1115 int num_bytes = (scan_size + 7) / 8;
1116 int bits_left = scan_size;
1117 int cur_byte = 0;
1118 int last_bit;
1119 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1120 uint8_t* receive_pointer = receive_buffer;
1121 uint32_t bytes_written;
1122 uint32_t bytes_read;
1123 int retval;
1124 int thisrun_read = 0;
1126 if (cmd->ir_scan)
1128 LOG_ERROR("BUG: large IR scans are not supported");
1129 exit(-1);
1132 if (tap_get_state() != TAP_DRSHIFT)
1134 move_to_state(TAP_DRSHIFT);
1137 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1139 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1140 exit(-1);
1142 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1143 ft2232_buffer_size, (int)bytes_written);
1144 ft2232_buffer_size = 0;
1146 /* add command for complete bytes */
1147 while (num_bytes > 1)
1149 int thisrun_bytes;
1151 if (type == SCAN_IO)
1153 /* Clock Data Bytes In and Out LSB First */
1154 buffer_write(0x39);
1155 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1157 else if (type == SCAN_OUT)
1159 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1160 buffer_write(0x19);
1161 /* LOG_DEBUG("added TDI bytes (o)"); */
1163 else if (type == SCAN_IN)
1165 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1166 buffer_write(0x28);
1167 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1170 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1171 thisrun_read = thisrun_bytes;
1172 num_bytes -= thisrun_bytes;
1173 buffer_write((uint8_t) (thisrun_bytes - 1));
1174 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1176 if (type != SCAN_IN)
1178 /* add complete bytes */
1179 while (thisrun_bytes-- > 0)
1181 buffer_write(buffer[cur_byte]);
1182 cur_byte++;
1183 bits_left -= 8;
1186 else /* (type == SCAN_IN) */
1188 bits_left -= 8 * (thisrun_bytes);
1191 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1193 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1194 exit(-1);
1196 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1197 ft2232_buffer_size,
1198 (int)bytes_written);
1199 ft2232_buffer_size = 0;
1201 if (type != SCAN_OUT)
1203 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1205 LOG_ERROR("couldn't read from FT2232");
1206 exit(-1);
1208 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1209 thisrun_read,
1210 (int)bytes_read);
1211 receive_pointer += bytes_read;
1215 thisrun_read = 0;
1217 /* the most signifcant bit is scanned during TAP movement */
1218 if (type != SCAN_IN)
1219 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1220 else
1221 last_bit = 0;
1223 /* process remaining bits but the last one */
1224 if (bits_left > 1)
1226 if (type == SCAN_IO)
1228 /* Clock Data Bits In and Out LSB First */
1229 buffer_write(0x3b);
1230 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1232 else if (type == SCAN_OUT)
1234 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1235 buffer_write(0x1b);
1236 /* LOG_DEBUG("added TDI bits (o)"); */
1238 else if (type == SCAN_IN)
1240 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1241 buffer_write(0x2a);
1242 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1244 buffer_write(bits_left - 2);
1245 if (type != SCAN_IN)
1246 buffer_write(buffer[cur_byte]);
1248 if (type != SCAN_OUT)
1249 thisrun_read += 2;
1252 if (tap_get_end_state() == TAP_DRSHIFT)
1254 if (type == SCAN_IO)
1256 /* Clock Data Bits In and Out LSB First */
1257 buffer_write(0x3b);
1258 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1260 else if (type == SCAN_OUT)
1262 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1263 buffer_write(0x1b);
1264 /* LOG_DEBUG("added TDI bits (o)"); */
1266 else if (type == SCAN_IN)
1268 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1269 buffer_write(0x2a);
1270 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1272 buffer_write(0x0);
1273 buffer_write(last_bit);
1275 else
1277 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1278 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1279 uint8_t mpsse_cmd;
1281 /* move from Shift-IR/DR to end state */
1282 if (type != SCAN_OUT)
1284 /* Clock Data to TMS/CS Pin with Read */
1285 mpsse_cmd = 0x6b;
1286 /* LOG_DEBUG("added TMS scan (read)"); */
1288 else
1290 /* Clock Data to TMS/CS Pin (no Read) */
1291 mpsse_cmd = 0x4b;
1292 /* LOG_DEBUG("added TMS scan (no read)"); */
1295 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1296 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1299 if (type != SCAN_OUT)
1300 thisrun_read += 1;
1302 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1304 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1305 exit(-1);
1307 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1308 ft2232_buffer_size,
1309 (int)bytes_written);
1310 ft2232_buffer_size = 0;
1312 if (type != SCAN_OUT)
1314 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1316 LOG_ERROR("couldn't read from FT2232");
1317 exit(-1);
1319 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1320 thisrun_read,
1321 (int)bytes_read);
1322 receive_pointer += bytes_read;
1325 return ERROR_OK;
1328 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1330 int predicted_size = 3;
1331 int num_bytes = (scan_size - 1) / 8;
1333 if (tap_get_state() != TAP_DRSHIFT)
1334 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1336 if (type == SCAN_IN) /* only from device to host */
1338 /* complete bytes */
1339 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1341 /* remaining bits - 1 (up to 7) */
1342 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1344 else /* host to device, or bidirectional */
1346 /* complete bytes */
1347 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1349 /* remaining bits -1 (up to 7) */
1350 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1353 return predicted_size;
1356 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1358 int predicted_size = 0;
1360 if (type != SCAN_OUT)
1362 /* complete bytes */
1363 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1365 /* remaining bits - 1 */
1366 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1368 /* last bit (from TMS scan) */
1369 predicted_size += 1;
1372 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1374 return predicted_size;
1377 /* semi-generic FT2232/FT4232 reset code */
1378 static void ftx23_reset(int trst, int srst)
1380 enum reset_types jtag_reset_config = jtag_get_reset_config();
1381 if (trst == 1)
1383 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1384 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1385 else
1386 low_output &= ~nTRST; /* switch output low */
1388 else if (trst == 0)
1390 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1391 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1392 else
1393 low_output |= nTRST; /* switch output high */
1396 if (srst == 1)
1398 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1399 low_output &= ~nSRST; /* switch output low */
1400 else
1401 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1403 else if (srst == 0)
1405 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1406 low_output |= nSRST; /* switch output high */
1407 else
1408 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1411 /* command "set data bits low byte" */
1412 buffer_write(0x80);
1413 buffer_write(low_output);
1414 buffer_write(low_direction);
1417 static void jtagkey_reset(int trst, int srst)
1419 enum reset_types jtag_reset_config = jtag_get_reset_config();
1420 if (trst == 1)
1422 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1423 high_output &= ~nTRSTnOE;
1424 else
1425 high_output &= ~nTRST;
1427 else if (trst == 0)
1429 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1430 high_output |= nTRSTnOE;
1431 else
1432 high_output |= nTRST;
1435 if (srst == 1)
1437 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1438 high_output &= ~nSRST;
1439 else
1440 high_output &= ~nSRSTnOE;
1442 else if (srst == 0)
1444 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1445 high_output |= nSRST;
1446 else
1447 high_output |= nSRSTnOE;
1450 /* command "set data bits high byte" */
1451 buffer_write(0x82);
1452 buffer_write(high_output);
1453 buffer_write(high_direction);
1454 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1455 high_direction);
1458 static void olimex_jtag_reset(int trst, int srst)
1460 enum reset_types jtag_reset_config = jtag_get_reset_config();
1461 if (trst == 1)
1463 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1464 high_output &= ~nTRSTnOE;
1465 else
1466 high_output &= ~nTRST;
1468 else if (trst == 0)
1470 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1471 high_output |= nTRSTnOE;
1472 else
1473 high_output |= nTRST;
1476 if (srst == 1)
1478 high_output |= nSRST;
1480 else if (srst == 0)
1482 high_output &= ~nSRST;
1485 /* command "set data bits high byte" */
1486 buffer_write(0x82);
1487 buffer_write(high_output);
1488 buffer_write(high_direction);
1489 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1490 high_direction);
1493 static void axm0432_jtag_reset(int trst, int srst)
1495 if (trst == 1)
1497 tap_set_state(TAP_RESET);
1498 high_output &= ~nTRST;
1500 else if (trst == 0)
1502 high_output |= nTRST;
1505 if (srst == 1)
1507 high_output &= ~nSRST;
1509 else if (srst == 0)
1511 high_output |= nSRST;
1514 /* command "set data bits low byte" */
1515 buffer_write(0x82);
1516 buffer_write(high_output);
1517 buffer_write(high_direction);
1518 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1519 high_direction);
1522 static void flyswatter_reset(int trst, int srst)
1524 if (trst == 1)
1526 low_output &= ~nTRST;
1528 else if (trst == 0)
1530 low_output |= nTRST;
1533 if (srst == 1)
1535 low_output |= nSRST;
1537 else if (srst == 0)
1539 low_output &= ~nSRST;
1542 /* command "set data bits low byte" */
1543 buffer_write(0x80);
1544 buffer_write(low_output);
1545 buffer_write(low_direction);
1546 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1549 static void turtle_reset(int trst, int srst)
1551 trst = trst;
1553 if (srst == 1)
1555 low_output |= nSRST;
1557 else if (srst == 0)
1559 low_output &= ~nSRST;
1562 /* command "set data bits low byte" */
1563 buffer_write(0x80);
1564 buffer_write(low_output);
1565 buffer_write(low_direction);
1566 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1569 static void comstick_reset(int trst, int srst)
1571 if (trst == 1)
1573 high_output &= ~nTRST;
1575 else if (trst == 0)
1577 high_output |= nTRST;
1580 if (srst == 1)
1582 high_output &= ~nSRST;
1584 else if (srst == 0)
1586 high_output |= nSRST;
1589 /* command "set data bits high byte" */
1590 buffer_write(0x82);
1591 buffer_write(high_output);
1592 buffer_write(high_direction);
1593 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1594 high_direction);
1597 static void stm32stick_reset(int trst, int srst)
1599 if (trst == 1)
1601 high_output &= ~nTRST;
1603 else if (trst == 0)
1605 high_output |= nTRST;
1608 if (srst == 1)
1610 low_output &= ~nSRST;
1612 else if (srst == 0)
1614 low_output |= nSRST;
1617 /* command "set data bits low byte" */
1618 buffer_write(0x80);
1619 buffer_write(low_output);
1620 buffer_write(low_direction);
1622 /* command "set data bits high byte" */
1623 buffer_write(0x82);
1624 buffer_write(high_output);
1625 buffer_write(high_direction);
1626 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1627 high_direction);
1630 static void sheevaplug_reset(int trst, int srst)
1632 if (trst == 1)
1633 high_output &= ~nTRST;
1634 else if (trst == 0)
1635 high_output |= nTRST;
1637 if (srst == 1)
1638 high_output &= ~nSRSTnOE;
1639 else if (srst == 0)
1640 high_output |= nSRSTnOE;
1642 /* command "set data bits high byte" */
1643 buffer_write(0x82);
1644 buffer_write(high_output);
1645 buffer_write(high_direction);
1646 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1649 static void redbee_reset(int trst, int srst)
1651 if (trst == 1)
1653 tap_set_state(TAP_RESET);
1654 high_output &= ~nTRST;
1656 else if (trst == 0)
1658 high_output |= nTRST;
1661 if (srst == 1)
1663 high_output &= ~nSRST;
1665 else if (srst == 0)
1667 high_output |= nSRST;
1670 /* command "set data bits low byte" */
1671 buffer_write(0x82);
1672 buffer_write(high_output);
1673 buffer_write(high_direction);
1674 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1675 "high_direction: 0x%2.2x", trst, srst, high_output,
1676 high_direction);
1679 static int ft2232_execute_runtest(struct jtag_command *cmd)
1681 int retval;
1682 int i;
1683 int predicted_size = 0;
1684 retval = ERROR_OK;
1686 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1687 cmd->cmd.runtest->num_cycles,
1688 tap_state_name(cmd->cmd.runtest->end_state));
1690 /* only send the maximum buffer size that FT2232C can handle */
1691 predicted_size = 0;
1692 if (tap_get_state() != TAP_IDLE)
1693 predicted_size += 3;
1694 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1695 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1696 predicted_size += 3;
1697 if (tap_get_end_state() != TAP_IDLE)
1698 predicted_size += 3;
1699 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1701 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1702 retval = ERROR_JTAG_QUEUE_FAILED;
1703 require_send = 0;
1704 first_unsent = cmd;
1706 if (tap_get_state() != TAP_IDLE)
1708 move_to_state(TAP_IDLE);
1709 require_send = 1;
1711 i = cmd->cmd.runtest->num_cycles;
1712 while (i > 0)
1714 /* there are no state transitions in this code, so omit state tracking */
1716 /* command "Clock Data to TMS/CS Pin (no Read)" */
1717 buffer_write(0x4b);
1719 /* scan 7 bits */
1720 buffer_write((i > 7) ? 6 : (i - 1));
1722 /* TMS data bits */
1723 buffer_write(0x0);
1725 i -= (i > 7) ? 7 : i;
1726 /* LOG_DEBUG("added TMS scan (no read)"); */
1729 ft2232_end_state(cmd->cmd.runtest->end_state);
1731 if (tap_get_state() != tap_get_end_state())
1733 move_to_state(tap_get_end_state());
1736 require_send = 1;
1737 DEBUG_JTAG_IO("runtest: %i, end in %s",
1738 cmd->cmd.runtest->num_cycles,
1739 tap_state_name(tap_get_end_state()));
1740 return retval;
1743 static int ft2232_execute_statemove(struct jtag_command *cmd)
1745 int predicted_size = 0;
1746 int retval = ERROR_OK;
1748 DEBUG_JTAG_IO("statemove end in %s",
1749 tap_state_name(cmd->cmd.statemove->end_state));
1751 /* only send the maximum buffer size that FT2232C can handle */
1752 predicted_size = 3;
1753 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1755 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1756 retval = ERROR_JTAG_QUEUE_FAILED;
1757 require_send = 0;
1758 first_unsent = cmd;
1760 ft2232_end_state(cmd->cmd.statemove->end_state);
1762 /* For TAP_RESET, ignore the current recorded state. It's often
1763 * wrong at server startup, and this transation is critical whenever
1764 * it's requested.
1766 if (tap_get_end_state() == TAP_RESET) {
1767 clock_tms(0x4b, 0xff, 5, 0);
1768 require_send = 1;
1770 /* shortest-path move to desired end state */
1771 } else if (tap_get_state() != tap_get_end_state())
1773 move_to_state(tap_get_end_state());
1774 require_send = 1;
1777 return retval;
1781 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1782 * (or SWD) state machine.
1784 static int ft2232_execute_tms(struct jtag_command *cmd)
1786 int retval = ERROR_OK;
1787 unsigned num_bits = cmd->cmd.tms->num_bits;
1788 const uint8_t *bits = cmd->cmd.tms->bits;
1789 unsigned count;
1791 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1793 /* only send the maximum buffer size that FT2232C can handle */
1794 count = 3 * DIV_ROUND_UP(num_bits, 4);
1795 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1796 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1797 retval = ERROR_JTAG_QUEUE_FAILED;
1799 require_send = 0;
1800 first_unsent = cmd;
1803 /* Shift out in batches of at most 6 bits; there's a report of an
1804 * FT2232 bug in this area, where shifting exactly 7 bits can make
1805 * problems with TMS signaling for the last clock cycle:
1807 * http://developer.intra2net.com/mailarchive/html/
1808 * libftdi/2009/msg00292.html
1810 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1812 * Note that pathmoves in JTAG are not often seven bits, so that
1813 * isn't a particularly likely situation outside of "special"
1814 * signaling such as switching between JTAG and SWD modes.
1816 while (num_bits) {
1817 if (num_bits <= 6) {
1818 buffer_write(0x4b);
1819 buffer_write(num_bits - 1);
1820 buffer_write(*bits & 0x3f);
1821 break;
1824 /* Yes, this is lazy ... we COULD shift out more data
1825 * bits per operation, but doing it in nybbles is easy
1827 buffer_write(0x4b);
1828 buffer_write(3);
1829 buffer_write(*bits & 0xf);
1830 num_bits -= 4;
1832 count = (num_bits > 4) ? 4 : num_bits;
1834 buffer_write(0x4b);
1835 buffer_write(count - 1);
1836 buffer_write((*bits >> 4) & 0xf);
1837 num_bits -= count;
1839 bits++;
1842 require_send = 1;
1843 return retval;
1846 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1848 int predicted_size = 0;
1849 int retval = ERROR_OK;
1851 tap_state_t* path = cmd->cmd.pathmove->path;
1852 int num_states = cmd->cmd.pathmove->num_states;
1854 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1855 tap_state_name(tap_get_state()),
1856 tap_state_name(path[num_states-1]));
1858 /* only send the maximum buffer size that FT2232C can handle */
1859 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1860 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1862 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1863 retval = ERROR_JTAG_QUEUE_FAILED;
1865 require_send = 0;
1866 first_unsent = cmd;
1869 ft2232_add_pathmove(path, num_states);
1870 require_send = 1;
1872 return retval;
1875 static int ft2232_execute_scan(struct jtag_command *cmd)
1877 uint8_t* buffer;
1878 int scan_size; /* size of IR or DR scan */
1879 int predicted_size = 0;
1880 int retval = ERROR_OK;
1882 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1884 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1886 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1888 predicted_size = ft2232_predict_scan_out(scan_size, type);
1889 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1891 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1892 /* unsent commands before this */
1893 if (first_unsent != cmd)
1894 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1895 retval = ERROR_JTAG_QUEUE_FAILED;
1897 /* current command */
1898 ft2232_end_state(cmd->cmd.scan->end_state);
1899 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1900 require_send = 0;
1901 first_unsent = cmd->next;
1902 if (buffer)
1903 free(buffer);
1904 return retval;
1906 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1908 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1909 first_unsent,
1910 cmd);
1911 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1912 retval = ERROR_JTAG_QUEUE_FAILED;
1913 require_send = 0;
1914 first_unsent = cmd;
1916 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1917 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1918 ft2232_end_state(cmd->cmd.scan->end_state);
1919 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1920 require_send = 1;
1921 if (buffer)
1922 free(buffer);
1923 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1924 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1925 tap_state_name(tap_get_end_state()));
1926 return retval;
1930 static int ft2232_execute_reset(struct jtag_command *cmd)
1932 int retval;
1933 int predicted_size = 0;
1934 retval = ERROR_OK;
1936 DEBUG_JTAG_IO("reset trst: %i srst %i",
1937 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1939 /* only send the maximum buffer size that FT2232C can handle */
1940 predicted_size = 3;
1941 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1943 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1944 retval = ERROR_JTAG_QUEUE_FAILED;
1945 require_send = 0;
1946 first_unsent = cmd;
1949 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1951 tap_set_state(TAP_RESET);
1954 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1955 require_send = 1;
1957 DEBUG_JTAG_IO("trst: %i, srst: %i",
1958 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1959 return retval;
1962 static int ft2232_execute_sleep(struct jtag_command *cmd)
1964 int retval;
1965 retval = ERROR_OK;
1967 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1969 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1970 retval = ERROR_JTAG_QUEUE_FAILED;
1971 first_unsent = cmd->next;
1972 jtag_sleep(cmd->cmd.sleep->us);
1973 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1974 cmd->cmd.sleep->us,
1975 tap_state_name(tap_get_state()));
1976 return retval;
1979 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1981 int retval;
1982 retval = ERROR_OK;
1984 /* this is only allowed while in a stable state. A check for a stable
1985 * state was done in jtag_add_clocks()
1987 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1988 retval = ERROR_JTAG_QUEUE_FAILED;
1989 DEBUG_JTAG_IO("clocks %i while in %s",
1990 cmd->cmd.stableclocks->num_cycles,
1991 tap_state_name(tap_get_state()));
1992 return retval;
1995 static int ft2232_execute_command(struct jtag_command *cmd)
1997 int retval;
1999 switch (cmd->type)
2001 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2002 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2003 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2004 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2005 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2006 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2007 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2008 case JTAG_TMS:
2009 retval = ft2232_execute_tms(cmd);
2010 break;
2011 default:
2012 LOG_ERROR("BUG: unknown JTAG command type encountered");
2013 retval = ERROR_JTAG_QUEUE_FAILED;
2014 break;
2016 return retval;
2019 static int ft2232_execute_queue(void)
2021 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2022 int retval;
2024 first_unsent = cmd; /* next command that has to be sent */
2025 require_send = 0;
2027 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2028 * that wasn't handled by a caller-provided error handler
2030 retval = ERROR_OK;
2032 ft2232_buffer_size = 0;
2033 ft2232_expect_read = 0;
2035 /* blink, if the current layout has that feature */
2036 if (layout->blink)
2037 layout->blink();
2039 while (cmd)
2041 if (ft2232_execute_command(cmd) != ERROR_OK)
2042 retval = ERROR_JTAG_QUEUE_FAILED;
2043 /* Start reading input before FT2232 TX buffer fills up */
2044 cmd = cmd->next;
2045 if (ft2232_expect_read > 256)
2047 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2048 retval = ERROR_JTAG_QUEUE_FAILED;
2049 first_unsent = cmd;
2053 if (require_send > 0)
2054 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2055 retval = ERROR_JTAG_QUEUE_FAILED;
2057 return retval;
2060 #if BUILD_FT2232_FTD2XX == 1
2061 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2063 FT_STATUS status;
2064 DWORD deviceID;
2065 char SerialNumber[16];
2066 char Description[64];
2067 DWORD openex_flags = 0;
2068 char* openex_string = NULL;
2069 uint8_t latency_timer;
2071 if (layout == NULL) {
2072 LOG_WARNING("No ft2232 layout specified'");
2073 return ERROR_JTAG_INIT_FAILED;
2076 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2078 #if IS_WIN32 == 0
2079 /* Add non-standard Vid/Pid to the linux driver */
2080 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2082 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2084 #endif
2086 if (ft2232_device_desc && ft2232_serial)
2088 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2089 ft2232_device_desc = NULL;
2092 if (ft2232_device_desc)
2094 openex_string = ft2232_device_desc;
2095 openex_flags = FT_OPEN_BY_DESCRIPTION;
2097 else if (ft2232_serial)
2099 openex_string = ft2232_serial;
2100 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2102 else
2104 LOG_ERROR("neither device description nor serial number specified");
2105 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2107 return ERROR_JTAG_INIT_FAILED;
2110 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2111 if (status != FT_OK) {
2112 /* under Win32, the FTD2XX driver appends an "A" to the end
2113 * of the description, if we tried by the desc, then
2114 * try by the alternate "A" description. */
2115 if (openex_string == ft2232_device_desc) {
2116 /* Try the alternate method. */
2117 openex_string = ft2232_device_desc_A;
2118 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2119 if (status == FT_OK) {
2120 /* yea, the "alternate" method worked! */
2121 } else {
2122 /* drat, give the user a meaningfull message.
2123 * telling the use we tried *BOTH* methods. */
2124 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2125 ft2232_device_desc,
2126 ft2232_device_desc_A);
2131 if (status != FT_OK)
2133 DWORD num_devices;
2135 if (more)
2137 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2138 *try_more = 1;
2139 return ERROR_JTAG_INIT_FAILED;
2141 LOG_ERROR("unable to open ftdi device: %lu", status);
2142 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2143 if (status == FT_OK)
2145 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2146 uint32_t i;
2148 for (i = 0; i < num_devices; i++)
2149 desc_array[i] = malloc(64);
2151 desc_array[num_devices] = NULL;
2153 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2155 if (status == FT_OK)
2157 LOG_ERROR("ListDevices: %lu\n", num_devices);
2158 for (i = 0; i < num_devices; i++)
2159 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2162 for (i = 0; i < num_devices; i++)
2163 free(desc_array[i]);
2165 free(desc_array);
2167 else
2169 LOG_ERROR("ListDevices: NONE\n");
2171 return ERROR_JTAG_INIT_FAILED;
2174 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2176 LOG_ERROR("unable to set latency timer: %lu", status);
2177 return ERROR_JTAG_INIT_FAILED;
2180 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2182 LOG_ERROR("unable to get latency timer: %lu", status);
2183 return ERROR_JTAG_INIT_FAILED;
2185 else
2187 LOG_DEBUG("current latency timer: %i", latency_timer);
2190 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2192 LOG_ERROR("unable to set timeouts: %lu", status);
2193 return ERROR_JTAG_INIT_FAILED;
2196 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2198 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2199 return ERROR_JTAG_INIT_FAILED;
2202 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2204 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2205 return ERROR_JTAG_INIT_FAILED;
2207 else
2209 static const char* type_str[] =
2210 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2211 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2212 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2213 ? ftdi_device : FT_DEVICE_UNKNOWN;
2214 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2215 LOG_INFO("deviceID: %lu", deviceID);
2216 LOG_INFO("SerialNumber: %s", SerialNumber);
2217 LOG_INFO("Description: %s", Description);
2220 return ERROR_OK;
2223 static int ft2232_purge_ftd2xx(void)
2225 FT_STATUS status;
2227 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2229 LOG_ERROR("error purging ftd2xx device: %lu", status);
2230 return ERROR_JTAG_INIT_FAILED;
2233 return ERROR_OK;
2236 #endif /* BUILD_FT2232_FTD2XX == 1 */
2238 #if BUILD_FT2232_LIBFTDI == 1
2239 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2241 uint8_t latency_timer;
2243 if (layout == NULL) {
2244 LOG_WARNING("No ft2232 layout specified'");
2245 return ERROR_JTAG_INIT_FAILED;
2248 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2249 layout->name, vid, pid);
2251 if (ftdi_init(&ftdic) < 0)
2252 return ERROR_JTAG_INIT_FAILED;
2254 /* default to INTERFACE_A */
2255 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2257 if (ftdi_set_interface(&ftdic, channel) < 0)
2259 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2260 return ERROR_JTAG_INIT_FAILED;
2263 /* context, vendor id, product id */
2264 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2265 ft2232_serial) < 0)
2267 if (more)
2268 LOG_WARNING("unable to open ftdi device (trying more): %s",
2269 ftdic.error_str);
2270 else
2271 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2272 *try_more = 1;
2273 return ERROR_JTAG_INIT_FAILED;
2276 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2277 if (ftdi_usb_reset(&ftdic) < 0)
2279 LOG_ERROR("unable to reset ftdi device");
2280 return ERROR_JTAG_INIT_FAILED;
2283 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2285 LOG_ERROR("unable to set latency timer");
2286 return ERROR_JTAG_INIT_FAILED;
2289 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2291 LOG_ERROR("unable to get latency timer");
2292 return ERROR_JTAG_INIT_FAILED;
2294 else
2296 LOG_DEBUG("current latency timer: %i", latency_timer);
2299 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2301 ftdi_device = ftdic.type;
2302 static const char* type_str[] =
2303 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2304 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2305 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2306 ? ftdi_device : no_of_known_types;
2307 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2308 return ERROR_OK;
2311 static int ft2232_purge_libftdi(void)
2313 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2315 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2316 return ERROR_JTAG_INIT_FAILED;
2319 return ERROR_OK;
2322 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2324 static int ft2232_init(void)
2326 uint8_t buf[1];
2327 int retval;
2328 uint32_t bytes_written;
2330 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2332 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2334 else
2336 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2339 if (layout == NULL) {
2340 LOG_WARNING("No ft2232 layout specified'");
2341 return ERROR_JTAG_INIT_FAILED;
2344 for (int i = 0; 1; i++)
2347 * "more indicates that there are more IDs to try, so we should
2348 * not print an error for an ID mismatch (but for anything
2349 * else, we should).
2351 * try_more indicates that the error code returned indicates an
2352 * ID mismatch (and nothing else) and that we should proceeed
2353 * with the next ID pair.
2355 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2356 int try_more = 0;
2358 #if BUILD_FT2232_FTD2XX == 1
2359 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2360 more, &try_more);
2361 #elif BUILD_FT2232_LIBFTDI == 1
2362 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2363 more, &try_more, layout->channel);
2364 #endif
2365 if (retval >= 0)
2366 break;
2367 if (!more || !try_more)
2368 return retval;
2371 ft2232_buffer_size = 0;
2372 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2374 if (layout->init() != ERROR_OK)
2375 return ERROR_JTAG_INIT_FAILED;
2377 if (ft2232_device_is_highspeed())
2379 #ifndef BUILD_FT2232_HIGHSPEED
2380 #if BUILD_FT2232_FTD2XX == 1
2381 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2382 #elif BUILD_FT2232_LIBFTDI == 1
2383 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2384 #endif
2385 #endif
2386 /* make sure the legacy mode is disabled */
2387 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2388 return ERROR_JTAG_INIT_FAILED;
2391 ft2232_speed(jtag_get_speed());
2393 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2394 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2396 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2397 return ERROR_JTAG_INIT_FAILED;
2400 #if BUILD_FT2232_FTD2XX == 1
2401 return ft2232_purge_ftd2xx();
2402 #elif BUILD_FT2232_LIBFTDI == 1
2403 return ft2232_purge_libftdi();
2404 #endif
2406 return ERROR_OK;
2409 /** Updates defaults for DBUS signals: the four JTAG signals
2410 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2412 static inline void ftx232_dbus_init(void)
2414 low_output = 0x08;
2415 low_direction = 0x0b;
2418 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2419 * the four GPIOL signals. Initialization covers value and direction,
2420 * as customized for each layout.
2422 static int ftx232_dbus_write(void)
2424 uint8_t buf[3];
2425 uint32_t bytes_written;
2427 enum reset_types jtag_reset_config = jtag_get_reset_config();
2428 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2430 low_direction &= ~nTRSTnOE; /* nTRST input */
2431 low_output &= ~nTRST; /* nTRST = 0 */
2433 else
2435 low_direction |= nTRSTnOE; /* nTRST output */
2436 low_output |= nTRST; /* nTRST = 1 */
2439 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2441 low_direction |= nSRSTnOE; /* nSRST output */
2442 low_output |= nSRST; /* nSRST = 1 */
2444 else
2446 low_direction &= ~nSRSTnOE; /* nSRST input */
2447 low_output &= ~nSRST; /* nSRST = 0 */
2450 /* initialize low byte for jtag */
2451 buf[0] = 0x80; /* command "set data bits low byte" */
2452 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2453 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2454 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2456 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2458 LOG_ERROR("couldn't initialize FT2232 DBUS");
2459 return ERROR_JTAG_INIT_FAILED;
2462 return ERROR_OK;
2465 static int usbjtag_init(void)
2468 * NOTE: This is now _specific_ to the "usbjtag" layout.
2469 * Don't try cram any more layouts into this.
2471 ftx232_dbus_init();
2473 nTRST = 0x10;
2474 nTRSTnOE = 0x10;
2475 nSRST = 0x40;
2476 nSRSTnOE = 0x40;
2478 return ftx232_dbus_write();
2481 static int lm3s811_jtag_init(void)
2483 ftx232_dbus_init();
2485 /* There are multiple revisions of LM3S811 eval boards:
2486 * - Rev B (and older?) boards have no SWO trace support.
2487 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2488 * they should use the "luminary_icdi" layout instead.
2490 nTRST = 0x0;
2491 nTRSTnOE = 0x00;
2492 nSRST = 0x20;
2493 nSRSTnOE = 0x20;
2494 low_output = 0x88;
2495 low_direction = 0x8b;
2497 return ftx232_dbus_write();
2500 static int icdi_jtag_init(void)
2502 ftx232_dbus_init();
2504 /* Most Luminary eval boards support SWO trace output,
2505 * and should use this "luminary_icdi" layout.
2507 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2508 * to switch between JTAG and SWD, or switch the ft2232 UART
2509 * on the second MPSSE channel/interface (BDBUS)
2510 * between (i) the stellaris UART (on Luminary boards)
2511 * or (ii) SWO trace data (generic).
2513 * We come up in JTAG mode and may switch to SWD later (with
2514 * SWO/trace option if SWD is active).
2516 * DBUS == GPIO-Lx
2517 * CBUS == GPIO-Hx
2521 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2522 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2523 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2526 /* GPIOs on second channel/interface (UART) ... */
2527 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2528 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2529 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2531 nTRST = 0x0;
2532 nTRSTnOE = 0x00;
2533 nSRST = ICDI_SRST;
2534 nSRSTnOE = ICDI_SRST;
2536 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2537 low_output |= ICDI_JTAG_EN;
2538 low_output &= ~ICDI_DBG_ENn;
2540 return ftx232_dbus_write();
2543 static int signalyzer_init(void)
2545 ftx232_dbus_init();
2547 nTRST = 0x10;
2548 nTRSTnOE = 0x10;
2549 nSRST = 0x20;
2550 nSRSTnOE = 0x20;
2551 return ftx232_dbus_write();
2554 static int axm0432_jtag_init(void)
2556 uint8_t buf[3];
2557 uint32_t bytes_written;
2559 low_output = 0x08;
2560 low_direction = 0x2b;
2562 /* initialize low byte for jtag */
2563 buf[0] = 0x80; /* command "set data bits low byte" */
2564 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2565 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2566 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2568 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2570 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2571 return ERROR_JTAG_INIT_FAILED;
2574 if (strcmp(layout->name, "axm0432_jtag") == 0)
2576 nTRST = 0x08;
2577 nTRSTnOE = 0x0; /* No output enable for TRST*/
2578 nSRST = 0x04;
2579 nSRSTnOE = 0x0; /* No output enable for SRST*/
2581 else
2583 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2584 exit(-1);
2587 high_output = 0x0;
2588 high_direction = 0x0c;
2590 enum reset_types jtag_reset_config = jtag_get_reset_config();
2591 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2593 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2595 else
2597 high_output |= nTRST;
2600 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2602 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2604 else
2606 high_output |= nSRST;
2609 /* initialize high port */
2610 buf[0] = 0x82; /* command "set data bits high byte" */
2611 buf[1] = high_output; /* value */
2612 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2613 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2615 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2617 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2618 return ERROR_JTAG_INIT_FAILED;
2621 return ERROR_OK;
2624 static int redbee_init(void)
2626 uint8_t buf[3];
2627 uint32_t bytes_written;
2629 low_output = 0x08;
2630 low_direction = 0x2b;
2632 /* initialize low byte for jtag */
2633 /* command "set data bits low byte" */
2634 buf[0] = 0x80;
2635 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2636 buf[2] = low_direction;
2637 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2638 buf[1] = low_output;
2639 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2641 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2643 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2644 return ERROR_JTAG_INIT_FAILED;
2647 nTRST = 0x08;
2648 nTRSTnOE = 0x0; /* No output enable for TRST*/
2649 nSRST = 0x04;
2650 nSRSTnOE = 0x0; /* No output enable for SRST*/
2652 high_output = 0x0;
2653 high_direction = 0x0c;
2655 enum reset_types jtag_reset_config = jtag_get_reset_config();
2656 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2658 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2660 else
2662 high_output |= nTRST;
2665 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2667 LOG_ERROR("can't set nSRST to push-pull on redbee");
2669 else
2671 high_output |= nSRST;
2674 /* initialize high port */
2675 buf[0] = 0x82; /* command "set data bits high byte" */
2676 buf[1] = high_output; /* value */
2677 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2678 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2680 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2682 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2683 return ERROR_JTAG_INIT_FAILED;
2686 return ERROR_OK;
2689 static int jtagkey_init(void)
2691 uint8_t buf[3];
2692 uint32_t bytes_written;
2694 low_output = 0x08;
2695 low_direction = 0x1b;
2697 /* initialize low byte for jtag */
2698 buf[0] = 0x80; /* command "set data bits low byte" */
2699 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2700 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2701 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2703 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2705 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2706 return ERROR_JTAG_INIT_FAILED;
2709 if (strcmp(layout->name, "jtagkey") == 0)
2711 nTRST = 0x01;
2712 nTRSTnOE = 0x4;
2713 nSRST = 0x02;
2714 nSRSTnOE = 0x08;
2716 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2717 || (strcmp(layout->name, "oocdlink") == 0))
2719 nTRST = 0x02;
2720 nTRSTnOE = 0x1;
2721 nSRST = 0x08;
2722 nSRSTnOE = 0x04;
2724 else
2726 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2727 exit(-1);
2730 high_output = 0x0;
2731 high_direction = 0x0f;
2733 enum reset_types jtag_reset_config = jtag_get_reset_config();
2734 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2736 high_output |= nTRSTnOE;
2737 high_output &= ~nTRST;
2739 else
2741 high_output &= ~nTRSTnOE;
2742 high_output |= nTRST;
2745 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2747 high_output &= ~nSRSTnOE;
2748 high_output |= nSRST;
2750 else
2752 high_output |= nSRSTnOE;
2753 high_output &= ~nSRST;
2756 /* initialize high port */
2757 buf[0] = 0x82; /* command "set data bits high byte" */
2758 buf[1] = high_output; /* value */
2759 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2760 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2762 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2764 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2765 return ERROR_JTAG_INIT_FAILED;
2768 return ERROR_OK;
2771 static int olimex_jtag_init(void)
2773 uint8_t buf[3];
2774 uint32_t bytes_written;
2776 low_output = 0x08;
2777 low_direction = 0x1b;
2779 /* initialize low byte for jtag */
2780 buf[0] = 0x80; /* command "set data bits low byte" */
2781 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2782 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2783 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2785 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2787 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2788 return ERROR_JTAG_INIT_FAILED;
2791 nTRST = 0x01;
2792 nTRSTnOE = 0x4;
2793 nSRST = 0x02;
2794 nSRSTnOE = 0x00; /* no output enable for nSRST */
2796 high_output = 0x0;
2797 high_direction = 0x0f;
2799 enum reset_types jtag_reset_config = jtag_get_reset_config();
2800 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2802 high_output |= nTRSTnOE;
2803 high_output &= ~nTRST;
2805 else
2807 high_output &= ~nTRSTnOE;
2808 high_output |= nTRST;
2811 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2813 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2815 else
2817 high_output &= ~nSRST;
2820 /* turn red LED on */
2821 high_output |= 0x08;
2823 /* initialize high port */
2824 buf[0] = 0x82; /* command "set data bits high byte" */
2825 buf[1] = high_output; /* value */
2826 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2827 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2829 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2831 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2832 return ERROR_JTAG_INIT_FAILED;
2835 return ERROR_OK;
2838 static int flyswatter_init(void)
2840 uint8_t buf[3];
2841 uint32_t bytes_written;
2843 low_output = 0x18;
2844 low_direction = 0xfb;
2846 /* initialize low byte for jtag */
2847 buf[0] = 0x80; /* command "set data bits low byte" */
2848 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2849 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2850 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2852 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2854 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2855 return ERROR_JTAG_INIT_FAILED;
2858 nTRST = 0x10;
2859 nTRSTnOE = 0x0; /* not output enable for nTRST */
2860 nSRST = 0x20;
2861 nSRSTnOE = 0x00; /* no output enable for nSRST */
2863 high_output = 0x00;
2864 high_direction = 0x0c;
2866 /* turn red LED3 on, LED2 off */
2867 high_output |= 0x08;
2869 /* initialize high port */
2870 buf[0] = 0x82; /* command "set data bits high byte" */
2871 buf[1] = high_output; /* value */
2872 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2873 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2875 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2877 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2878 return ERROR_JTAG_INIT_FAILED;
2881 return ERROR_OK;
2884 static int turtle_init(void)
2886 uint8_t buf[3];
2887 uint32_t bytes_written;
2889 low_output = 0x08;
2890 low_direction = 0x5b;
2892 /* initialize low byte for jtag */
2893 buf[0] = 0x80; /* command "set data bits low byte" */
2894 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2895 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2896 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2898 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2900 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2901 return ERROR_JTAG_INIT_FAILED;
2904 nSRST = 0x40;
2906 high_output = 0x00;
2907 high_direction = 0x0C;
2909 /* initialize high port */
2910 buf[0] = 0x82; /* command "set data bits high byte" */
2911 buf[1] = high_output;
2912 buf[2] = high_direction;
2913 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2915 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2917 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2918 return ERROR_JTAG_INIT_FAILED;
2921 return ERROR_OK;
2924 static int comstick_init(void)
2926 uint8_t buf[3];
2927 uint32_t bytes_written;
2929 low_output = 0x08;
2930 low_direction = 0x0b;
2932 /* initialize low byte for jtag */
2933 buf[0] = 0x80; /* command "set data bits low byte" */
2934 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2935 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2936 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2938 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2940 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2941 return ERROR_JTAG_INIT_FAILED;
2944 nTRST = 0x01;
2945 nTRSTnOE = 0x00; /* no output enable for nTRST */
2946 nSRST = 0x02;
2947 nSRSTnOE = 0x00; /* no output enable for nSRST */
2949 high_output = 0x03;
2950 high_direction = 0x03;
2952 /* initialize high port */
2953 buf[0] = 0x82; /* command "set data bits high byte" */
2954 buf[1] = high_output;
2955 buf[2] = high_direction;
2956 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2958 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2960 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2961 return ERROR_JTAG_INIT_FAILED;
2964 return ERROR_OK;
2967 static int stm32stick_init(void)
2969 uint8_t buf[3];
2970 uint32_t bytes_written;
2972 low_output = 0x88;
2973 low_direction = 0x8b;
2975 /* initialize low byte for jtag */
2976 buf[0] = 0x80; /* command "set data bits low byte" */
2977 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2978 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2979 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2981 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2983 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2984 return ERROR_JTAG_INIT_FAILED;
2987 nTRST = 0x01;
2988 nTRSTnOE = 0x00; /* no output enable for nTRST */
2989 nSRST = 0x80;
2990 nSRSTnOE = 0x00; /* no output enable for nSRST */
2992 high_output = 0x01;
2993 high_direction = 0x03;
2995 /* initialize high port */
2996 buf[0] = 0x82; /* command "set data bits high byte" */
2997 buf[1] = high_output;
2998 buf[2] = high_direction;
2999 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3001 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3003 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3004 return ERROR_JTAG_INIT_FAILED;
3007 return ERROR_OK;
3010 static int sheevaplug_init(void)
3012 uint8_t buf[3];
3013 uint32_t bytes_written;
3015 low_output = 0x08;
3016 low_direction = 0x1b;
3018 /* initialize low byte for jtag */
3019 buf[0] = 0x80; /* command "set data bits low byte" */
3020 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3021 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
3022 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3024 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3026 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3027 return ERROR_JTAG_INIT_FAILED;
3030 nTRSTnOE = 0x1;
3031 nTRST = 0x02;
3032 nSRSTnOE = 0x4;
3033 nSRST = 0x08;
3035 high_output = 0x0;
3036 high_direction = 0x0f;
3038 /* nTRST is always push-pull */
3039 high_output &= ~nTRSTnOE;
3040 high_output |= nTRST;
3042 /* nSRST is always open-drain */
3043 high_output |= nSRSTnOE;
3044 high_output &= ~nSRST;
3046 /* initialize high port */
3047 buf[0] = 0x82; /* command "set data bits high byte" */
3048 buf[1] = high_output; /* value */
3049 buf[2] = high_direction; /* all outputs - xRST */
3050 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3052 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3054 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3055 return ERROR_JTAG_INIT_FAILED;
3058 return ERROR_OK;
3061 static int cortino_jtag_init(void)
3063 uint8_t buf[3];
3064 uint32_t bytes_written;
3066 low_output = 0x08;
3067 low_direction = 0x1b;
3069 /* initialize low byte for jtag */
3070 buf[0] = 0x80; /* command "set data bits low byte" */
3071 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
3072 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
3073 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3075 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3077 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3078 return ERROR_JTAG_INIT_FAILED;
3081 nTRST = 0x01;
3082 nTRSTnOE = 0x00; /* no output enable for nTRST */
3083 nSRST = 0x02;
3084 nSRSTnOE = 0x00; /* no output enable for nSRST */
3086 high_output = 0x03;
3087 high_direction = 0x03;
3089 /* initialize high port */
3090 buf[0] = 0x82; /* command "set data bits high byte" */
3091 buf[1] = high_output;
3092 buf[2] = high_direction;
3093 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3095 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3097 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3098 return ERROR_JTAG_INIT_FAILED;
3101 return ERROR_OK;
3104 static int lisa_l_init(void)
3106 uint8_t buf[3];
3107 uint32_t bytes_written;
3110 * NOTE: This is now _specific_ to the "usbjtag" layout.
3111 * Don't try cram any more layouts into this.
3113 ftx232_dbus_init();
3115 nTRST = 0x10;
3116 nTRSTnOE = 0x10;
3117 nSRST = 0x40;
3118 nSRSTnOE = 0x40;
3120 high_output = 0x00;
3121 high_direction = 0x18;
3123 /* initialize high port */
3124 buf[0] = 0x82; /* command "set data bits high byte" */
3125 buf[1] = high_output;
3126 buf[2] = high_direction;
3127 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3129 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
3131 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3132 return ERROR_JTAG_INIT_FAILED;
3135 return ftx232_dbus_write();
3137 static void olimex_jtag_blink(void)
3139 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3140 * ACBUS3 is bit 3 of the GPIOH port
3142 if (high_output & 0x08)
3144 /* set port pin high */
3145 high_output &= 0x07;
3147 else
3149 /* set port pin low */
3150 high_output |= 0x08;
3153 buffer_write(0x82);
3154 buffer_write(high_output);
3155 buffer_write(high_direction);
3158 static void flyswatter_jtag_blink(void)
3161 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3163 high_output ^= 0x0c;
3165 buffer_write(0x82);
3166 buffer_write(high_output);
3167 buffer_write(high_direction);
3170 static void turtle_jtag_blink(void)
3173 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3175 if (high_output & 0x08)
3177 high_output = 0x04;
3179 else
3181 high_output = 0x08;
3184 buffer_write(0x82);
3185 buffer_write(high_output);
3186 buffer_write(high_direction);
3189 static void lisa_l_blink(void)
3192 * Lisa/L has two LEDs connected to BCBUS3 and ACBUS4
3194 if (high_output & 0x10)
3196 high_output = 0x08;
3198 else
3200 high_output = 0x10;
3203 buffer_write(0x82);
3204 buffer_write(high_output);
3205 buffer_write(high_direction);
3208 static int ft2232_quit(void)
3210 #if BUILD_FT2232_FTD2XX == 1
3211 FT_STATUS status;
3213 status = FT_Close(ftdih);
3214 #elif BUILD_FT2232_LIBFTDI == 1
3215 ftdi_usb_close(&ftdic);
3217 ftdi_deinit(&ftdic);
3218 #endif
3220 free(ft2232_buffer);
3221 ft2232_buffer = NULL;
3223 return ERROR_OK;
3226 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3228 char *cp;
3229 char buf[200];
3230 if (CMD_ARGC == 1)
3232 ft2232_device_desc = strdup(CMD_ARGV[0]);
3233 cp = strchr(ft2232_device_desc, 0);
3234 /* under Win32, the FTD2XX driver appends an "A" to the end
3235 * of the description, this examines the given desc
3236 * and creates the 'missing' _A or non_A variable. */
3237 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3238 /* it was, so make this the "A" version. */
3239 ft2232_device_desc_A = ft2232_device_desc;
3240 /* and *CREATE* the non-A version. */
3241 strcpy(buf, ft2232_device_desc);
3242 cp = strchr(buf, 0);
3243 cp[-2] = 0;
3244 ft2232_device_desc = strdup(buf);
3245 } else {
3246 /* <space > A not defined
3247 * so create it */
3248 sprintf(buf, "%s A", ft2232_device_desc);
3249 ft2232_device_desc_A = strdup(buf);
3252 else
3254 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3257 return ERROR_OK;
3260 COMMAND_HANDLER(ft2232_handle_serial_command)
3262 if (CMD_ARGC == 1)
3264 ft2232_serial = strdup(CMD_ARGV[0]);
3266 else
3268 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3271 return ERROR_OK;
3274 COMMAND_HANDLER(ft2232_handle_layout_command)
3276 if (CMD_ARGC != 1) {
3277 LOG_ERROR("Need exactly one argument to ft2232_layout");
3278 return ERROR_FAIL;
3281 if (layout) {
3282 LOG_ERROR("already specified ft2232_layout %s",
3283 layout->name);
3284 return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3285 ? ERROR_FAIL
3286 : ERROR_OK;
3289 for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3290 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3291 layout = l;
3292 return ERROR_OK;
3296 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3297 return ERROR_FAIL;
3300 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3302 if (CMD_ARGC > MAX_USB_IDS * 2)
3304 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3305 "(maximum is %d pairs)", MAX_USB_IDS);
3306 CMD_ARGC = MAX_USB_IDS * 2;
3308 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3310 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3311 if (CMD_ARGC < 2)
3312 return ERROR_COMMAND_SYNTAX_ERROR;
3313 /* remove the incomplete trailing id */
3314 CMD_ARGC -= 1;
3317 unsigned i;
3318 for (i = 0; i < CMD_ARGC; i += 2)
3320 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3321 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3325 * Explicitly terminate, in case there are multiples instances of
3326 * ft2232_vid_pid.
3328 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3330 return ERROR_OK;
3333 COMMAND_HANDLER(ft2232_handle_latency_command)
3335 if (CMD_ARGC == 1)
3337 ft2232_latency = atoi(CMD_ARGV[0]);
3339 else
3341 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3344 return ERROR_OK;
3347 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3349 int retval = 0;
3351 /* 7 bits of either ones or zeros. */
3352 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3354 while (num_cycles > 0)
3356 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3357 * at most 7 bits per invocation. Here we invoke it potentially
3358 * several times.
3360 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3362 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3364 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3365 retval = ERROR_JTAG_QUEUE_FAILED;
3367 first_unsent = cmd;
3370 /* there are no state transitions in this code, so omit state tracking */
3372 /* command "Clock Data to TMS/CS Pin (no Read)" */
3373 buffer_write(0x4b);
3375 /* scan 7 bit */
3376 buffer_write(bitcount_per_command - 1);
3378 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3379 buffer_write(tms);
3381 require_send = 1;
3383 num_cycles -= bitcount_per_command;
3386 return retval;
3389 /* ---------------------------------------------------------------------
3390 * Support for IceBear JTAG adapter from Section5:
3391 * http://section5.ch/icebear
3393 * Author: Sten, debian@sansys-electronic.com
3396 /* Icebear pin layout
3398 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3399 * GND GND | 4 3| n.c.
3400 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3401 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3402 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3403 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3404 * ADBUS2 TDO |14 13| GND GND
3406 * ADBUS0 O L TCK ACBUS0 GND
3407 * ADBUS1 O L TDI ACBUS1 GND
3408 * ADBUS2 I TDO ACBUS2 n.c.
3409 * ADBUS3 O H TMS ACBUS3 n.c.
3410 * ADBUS4 O H nTRST
3411 * ADBUS5 O H nSRST
3412 * ADBUS6 - VCC
3413 * ADBUS7 - GND
3415 static int icebear_jtag_init(void) {
3416 uint8_t buf[3];
3417 uint32_t bytes_written;
3419 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3420 low_output = 0x08; /* high: TMS; low: TCK TDI */
3421 nTRST = 0x10;
3422 nSRST = 0x20;
3424 enum reset_types jtag_reset_config = jtag_get_reset_config();
3425 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3426 low_direction &= ~nTRST; /* nTRST high impedance */
3428 else {
3429 low_direction |= nTRST;
3430 low_output |= nTRST;
3433 low_direction |= nSRST;
3434 low_output |= nSRST;
3436 /* initialize low byte for jtag */
3437 buf[0] = 0x80; /* command "set data bits low byte" */
3438 buf[1] = low_output;
3439 buf[2] = low_direction;
3440 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3442 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3443 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3444 return ERROR_JTAG_INIT_FAILED;
3447 high_output = 0x0;
3448 high_direction = 0x00;
3451 /* initialize high port */
3452 buf[0] = 0x82; /* command "set data bits high byte" */
3453 buf[1] = high_output; /* value */
3454 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3455 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3457 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
3458 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3459 return ERROR_JTAG_INIT_FAILED;
3462 return ERROR_OK;
3465 static void icebear_jtag_reset(int trst, int srst) {
3467 if (trst == 1) {
3468 low_direction |= nTRST;
3469 low_output &= ~nTRST;
3471 else if (trst == 0) {
3472 enum reset_types jtag_reset_config = jtag_get_reset_config();
3473 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3474 low_direction &= ~nTRST;
3475 else
3476 low_output |= nTRST;
3479 if (srst == 1) {
3480 low_output &= ~nSRST;
3482 else if (srst == 0) {
3483 low_output |= nSRST;
3486 /* command "set data bits low byte" */
3487 buffer_write(0x80);
3488 buffer_write(low_output);
3489 buffer_write(low_direction);
3491 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3494 /* ---------------------------------------------------------------------
3495 * Support for Signalyzer H2 and Signalyzer H4
3496 * JTAG adapter from Xverve Technologies Inc.
3497 * http://www.signalyzer.com or http://www.xverve.com
3499 * Author: Oleg Seiljus, oleg@signalyzer.com
3501 static unsigned char signalyzer_h_side;
3502 static unsigned int signalyzer_h_adapter_type;
3504 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3506 #if BUILD_FT2232_FTD2XX == 1
3507 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3508 #endif
3510 #define SIGNALYZER_COMMAND_ADDR 128
3511 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3513 #define SIGNALYZER_COMMAND_VERSION 0x41
3514 #define SIGNALYZER_COMMAND_RESET 0x42
3515 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3516 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3517 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3518 #define SIGNALYZER_COMMAND_LED_SET 0x53
3519 #define SIGNALYZER_COMMAND_ADC 0x54
3520 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3521 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3522 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3523 #define SIGNALYZER_COMMAND_I2C 0x58
3525 #define SIGNALYZER_CHAN_A 1
3526 #define SIGNALYZER_CHAN_B 2
3527 /* LEDS use channel C */
3528 #define SIGNALYZER_CHAN_C 4
3530 #define SIGNALYZER_LED_GREEN 1
3531 #define SIGNALYZER_LED_RED 2
3533 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3534 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3535 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3536 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3537 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3540 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3542 #if BUILD_FT2232_FTD2XX == 1
3543 return FT_WriteEE(ftdih, address, value);
3544 #elif BUILD_FT2232_LIBFTDI == 1
3545 return 0;
3546 #endif
3549 #if BUILD_FT2232_FTD2XX == 1
3550 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3552 return FT_ReadEE(ftdih, address, value);
3554 #endif
3556 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3557 int on_time_ms, int off_time_ms, unsigned char cycles)
3559 unsigned char on_time;
3560 unsigned char off_time;
3562 if (on_time_ms < 0xFFFF)
3563 on_time = (unsigned char)(on_time_ms / 62);
3564 else
3565 on_time = 0xFF;
3567 off_time = (unsigned char)(off_time_ms / 62);
3569 #if BUILD_FT2232_FTD2XX == 1
3570 FT_STATUS status;
3572 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3573 ((uint32_t)(channel << 8) | led))) != FT_OK)
3575 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3576 return ERROR_JTAG_DEVICE_ERROR;
3579 if ((status = signalyzer_h_ctrl_write(
3580 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3581 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3583 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3584 return ERROR_JTAG_DEVICE_ERROR;
3587 if ((status = signalyzer_h_ctrl_write(
3588 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3589 ((uint32_t)cycles))) != FT_OK)
3591 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3592 return ERROR_JTAG_DEVICE_ERROR;
3595 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3596 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3599 return ERROR_JTAG_DEVICE_ERROR;
3602 return ERROR_OK;
3603 #elif BUILD_FT2232_LIBFTDI == 1
3604 int retval;
3606 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3607 ((uint32_t)(channel << 8) | led))) < 0)
3609 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3610 ftdi_get_error_string(&ftdic));
3611 return ERROR_JTAG_DEVICE_ERROR;
3614 if ((retval = signalyzer_h_ctrl_write(
3615 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3616 ((uint32_t)(on_time << 8) | off_time))) < 0)
3618 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3619 ftdi_get_error_string(&ftdic));
3620 return ERROR_JTAG_DEVICE_ERROR;
3623 if ((retval = signalyzer_h_ctrl_write(
3624 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3625 (uint32_t)cycles)) < 0)
3627 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3628 ftdi_get_error_string(&ftdic));
3629 return ERROR_JTAG_DEVICE_ERROR;
3632 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3633 SIGNALYZER_COMMAND_LED_SET)) < 0)
3635 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3636 ftdi_get_error_string(&ftdic));
3637 return ERROR_JTAG_DEVICE_ERROR;
3640 return ERROR_OK;
3641 #endif
3644 static int signalyzer_h_init(void)
3646 #if BUILD_FT2232_FTD2XX == 1
3647 FT_STATUS status;
3648 int i;
3649 #endif
3651 char *end_of_desc;
3653 uint16_t read_buf[12] = { 0 };
3654 uint8_t buf[3];
3655 uint32_t bytes_written;
3657 /* turn on center green led */
3658 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3659 0xFFFF, 0x00, 0x00);
3661 /* determine what channel config wants to open
3662 * TODO: change me... current implementation is made to work
3663 * with openocd description parsing.
3665 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3667 if (end_of_desc)
3669 signalyzer_h_side = *(end_of_desc - 1);
3670 if (signalyzer_h_side == 'B')
3671 signalyzer_h_side = SIGNALYZER_CHAN_B;
3672 else
3673 signalyzer_h_side = SIGNALYZER_CHAN_A;
3675 else
3677 LOG_ERROR("No Channel was specified");
3678 return ERROR_FAIL;
3681 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3682 1000, 1000, 0xFF);
3684 #if BUILD_FT2232_FTD2XX == 1
3685 /* read signalyzer versionining information */
3686 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3687 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3689 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3690 return ERROR_JTAG_DEVICE_ERROR;
3693 for (i = 0; i < 10; i++)
3695 if ((status = signalyzer_h_ctrl_read(
3696 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3697 &read_buf[i])) != FT_OK)
3699 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3700 status);
3701 return ERROR_JTAG_DEVICE_ERROR;
3705 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3706 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3707 read_buf[4], read_buf[5], read_buf[6]);
3709 /* set gpio register */
3710 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3711 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3713 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3714 return ERROR_JTAG_DEVICE_ERROR;
3717 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3718 0x0404)) != FT_OK)
3720 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3721 return ERROR_JTAG_DEVICE_ERROR;
3724 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3725 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3727 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3728 return ERROR_JTAG_DEVICE_ERROR;
3731 /* read adapter type information */
3732 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3733 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3735 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3736 return ERROR_JTAG_DEVICE_ERROR;
3739 if ((status = signalyzer_h_ctrl_write(
3740 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3742 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3743 return ERROR_JTAG_DEVICE_ERROR;
3746 if ((status = signalyzer_h_ctrl_write(
3747 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3749 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3750 return ERROR_JTAG_DEVICE_ERROR;
3753 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3754 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3756 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3757 return ERROR_JTAG_DEVICE_ERROR;
3760 usleep(100000);
3762 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3763 &read_buf[0])) != FT_OK)
3765 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3766 return ERROR_JTAG_DEVICE_ERROR;
3769 if (read_buf[0] != 0x0498)
3770 signalyzer_h_adapter_type = 0x0000;
3771 else
3773 for (i = 0; i < 4; i++)
3775 if ((status = signalyzer_h_ctrl_read(
3776 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3777 &read_buf[i])) != FT_OK)
3779 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3780 status);
3781 return ERROR_JTAG_DEVICE_ERROR;
3785 signalyzer_h_adapter_type = read_buf[0];
3788 #elif BUILD_FT2232_LIBFTDI == 1
3789 /* currently libftdi does not allow reading individual eeprom
3790 * locations, therefore adapter type cannot be detected.
3791 * override with most common type
3793 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3794 #endif
3796 enum reset_types jtag_reset_config = jtag_get_reset_config();
3798 /* ADAPTOR: EM_LT16_A */
3799 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3801 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3802 "detected. (HW: %2x).", (read_buf[1] >> 8));
3804 nTRST = 0x10;
3805 nTRSTnOE = 0x10;
3806 nSRST = 0x20;
3807 nSRSTnOE = 0x20;
3809 low_output = 0x08;
3810 low_direction = 0x1b;
3812 high_output = 0x0;
3813 high_direction = 0x0;
3815 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3817 low_direction &= ~nTRSTnOE; /* nTRST input */
3818 low_output &= ~nTRST; /* nTRST = 0 */
3820 else
3822 low_direction |= nTRSTnOE; /* nTRST output */
3823 low_output |= nTRST; /* nTRST = 1 */
3826 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3828 low_direction |= nSRSTnOE; /* nSRST output */
3829 low_output |= nSRST; /* nSRST = 1 */
3831 else
3833 low_direction &= ~nSRSTnOE; /* nSRST input */
3834 low_output &= ~nSRST; /* nSRST = 0 */
3837 #if BUILD_FT2232_FTD2XX == 1
3838 /* enable power to the module */
3839 if ((status = signalyzer_h_ctrl_write(
3840 SIGNALYZER_DATA_BUFFER_ADDR,
3841 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3842 != FT_OK)
3844 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3845 status);
3846 return ERROR_JTAG_DEVICE_ERROR;
3849 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3850 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3852 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3853 status);
3854 return ERROR_JTAG_DEVICE_ERROR;
3857 /* set gpio mode register */
3858 if ((status = signalyzer_h_ctrl_write(
3859 SIGNALYZER_DATA_BUFFER_ADDR,
3860 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3862 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3863 status);
3864 return ERROR_JTAG_DEVICE_ERROR;
3867 if ((status = signalyzer_h_ctrl_write(
3868 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3869 != FT_OK)
3871 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3872 status);
3873 return ERROR_JTAG_DEVICE_ERROR;
3876 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3877 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3879 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3880 status);
3881 return ERROR_JTAG_DEVICE_ERROR;
3884 /* set gpio register */
3885 if ((status = signalyzer_h_ctrl_write(
3886 SIGNALYZER_DATA_BUFFER_ADDR,
3887 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3889 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3890 status);
3891 return ERROR_JTAG_DEVICE_ERROR;
3894 if ((status = signalyzer_h_ctrl_write(
3895 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3896 != FT_OK)
3898 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3899 status);
3900 return ERROR_JTAG_DEVICE_ERROR;
3903 if ((status = signalyzer_h_ctrl_write(
3904 SIGNALYZER_COMMAND_ADDR,
3905 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3907 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3908 status);
3909 return ERROR_JTAG_DEVICE_ERROR;
3911 #endif
3914 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3915 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3916 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3917 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3918 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3920 if (signalyzer_h_adapter_type
3921 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3922 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3923 "detected. (HW: %2x).", (read_buf[1] >> 8));
3924 else if (signalyzer_h_adapter_type
3925 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3926 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3927 "(ARM JTAG with PSU) detected. (HW: %2x).",
3928 (read_buf[1] >> 8));
3929 else if (signalyzer_h_adapter_type
3930 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3931 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3932 "detected. (HW: %2x).", (read_buf[1] >> 8));
3933 else if (signalyzer_h_adapter_type
3934 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3935 LOG_INFO("Signalyzer: EM-JTAG-P "
3936 "(Generic JTAG with PSU) detected. (HW: %2x).",
3937 (read_buf[1] >> 8));
3939 nTRST = 0x02;
3940 nTRSTnOE = 0x04;
3941 nSRST = 0x08;
3942 nSRSTnOE = 0x10;
3944 low_output = 0x08;
3945 low_direction = 0x1b;
3947 high_output = 0x0;
3948 high_direction = 0x1f;
3950 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3952 high_output |= nTRSTnOE;
3953 high_output &= ~nTRST;
3955 else
3957 high_output &= ~nTRSTnOE;
3958 high_output |= nTRST;
3961 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3963 high_output &= ~nSRSTnOE;
3964 high_output |= nSRST;
3966 else
3968 high_output |= nSRSTnOE;
3969 high_output &= ~nSRST;
3972 #if BUILD_FT2232_FTD2XX == 1
3973 /* enable power to the module */
3974 if ((status = signalyzer_h_ctrl_write(
3975 SIGNALYZER_DATA_BUFFER_ADDR,
3976 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3977 != FT_OK)
3979 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3980 status);
3981 return ERROR_JTAG_DEVICE_ERROR;
3984 if ((status = signalyzer_h_ctrl_write(
3985 SIGNALYZER_COMMAND_ADDR,
3986 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3988 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3989 status);
3990 return ERROR_JTAG_DEVICE_ERROR;
3993 /* set gpio mode register (IO_16 and IO_17 set as analog
3994 * inputs, other is gpio)
3996 if ((status = signalyzer_h_ctrl_write(
3997 SIGNALYZER_DATA_BUFFER_ADDR,
3998 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4000 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4001 status);
4002 return ERROR_JTAG_DEVICE_ERROR;
4005 if ((status = signalyzer_h_ctrl_write(
4006 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
4007 != FT_OK)
4009 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4010 status);
4011 return ERROR_JTAG_DEVICE_ERROR;
4014 if ((status = signalyzer_h_ctrl_write(
4015 SIGNALYZER_COMMAND_ADDR,
4016 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
4018 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4019 status);
4020 return ERROR_JTAG_DEVICE_ERROR;
4023 /* set gpio register (all inputs, for -P modules,
4024 * PSU will be turned off)
4026 if ((status = signalyzer_h_ctrl_write(
4027 SIGNALYZER_DATA_BUFFER_ADDR,
4028 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
4030 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4031 status);
4032 return ERROR_JTAG_DEVICE_ERROR;
4035 if ((status = signalyzer_h_ctrl_write(
4036 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
4037 != FT_OK)
4039 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4040 status);
4041 return ERROR_JTAG_DEVICE_ERROR;
4044 if ((status = signalyzer_h_ctrl_write(
4045 SIGNALYZER_COMMAND_ADDR,
4046 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
4048 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
4049 status);
4050 return ERROR_JTAG_DEVICE_ERROR;
4052 #endif
4055 else if (signalyzer_h_adapter_type == 0x0000)
4057 LOG_INFO("Signalyzer: No external modules were detected.");
4059 nTRST = 0x10;
4060 nTRSTnOE = 0x10;
4061 nSRST = 0x20;
4062 nSRSTnOE = 0x20;
4064 low_output = 0x08;
4065 low_direction = 0x1b;
4067 high_output = 0x0;
4068 high_direction = 0x0;
4070 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4072 low_direction &= ~nTRSTnOE; /* nTRST input */
4073 low_output &= ~nTRST; /* nTRST = 0 */
4075 else
4077 low_direction |= nTRSTnOE; /* nTRST output */
4078 low_output |= nTRST; /* nTRST = 1 */
4081 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4083 low_direction |= nSRSTnOE; /* nSRST output */
4084 low_output |= nSRST; /* nSRST = 1 */
4086 else
4088 low_direction &= ~nSRSTnOE; /* nSRST input */
4089 low_output &= ~nSRST; /* nSRST = 0 */
4092 else
4094 LOG_ERROR("Unknown module type is detected: %.4x",
4095 signalyzer_h_adapter_type);
4096 return ERROR_JTAG_DEVICE_ERROR;
4099 /* initialize low byte of controller for jtag operation */
4100 buf[0] = 0x80;
4101 buf[1] = low_output;
4102 buf[2] = low_direction;
4104 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4106 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4107 return ERROR_JTAG_INIT_FAILED;
4110 #if BUILD_FT2232_FTD2XX == 1
4111 if (ftdi_device == FT_DEVICE_2232H)
4113 /* initialize high byte of controller for jtag operation */
4114 buf[0] = 0x82;
4115 buf[1] = high_output;
4116 buf[2] = high_direction;
4118 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4120 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4121 return ERROR_JTAG_INIT_FAILED;
4124 #elif BUILD_FT2232_LIBFTDI == 1
4125 if (ftdi_device == TYPE_2232H)
4127 /* initialize high byte of controller for jtag operation */
4128 buf[0] = 0x82;
4129 buf[1] = high_output;
4130 buf[2] = high_direction;
4132 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4134 LOG_ERROR("couldn't initialize Signalyzer-H layout");
4135 return ERROR_JTAG_INIT_FAILED;
4138 #endif
4139 return ERROR_OK;
4142 static void signalyzer_h_reset(int trst, int srst)
4144 enum reset_types jtag_reset_config = jtag_get_reset_config();
4146 /* ADAPTOR: EM_LT16_A */
4147 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
4149 if (trst == 1)
4151 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4152 /* switch to output pin (output is low) */
4153 low_direction |= nTRSTnOE;
4154 else
4155 /* switch output low */
4156 low_output &= ~nTRST;
4158 else if (trst == 0)
4160 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4161 /* switch to input pin (high-Z + internal
4162 * and external pullup) */
4163 low_direction &= ~nTRSTnOE;
4164 else
4165 /* switch output high */
4166 low_output |= nTRST;
4169 if (srst == 1)
4171 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4172 /* switch output low */
4173 low_output &= ~nSRST;
4174 else
4175 /* switch to output pin (output is low) */
4176 low_direction |= nSRSTnOE;
4178 else if (srst == 0)
4180 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4181 /* switch output high */
4182 low_output |= nSRST;
4183 else
4184 /* switch to input pin (high-Z) */
4185 low_direction &= ~nSRSTnOE;
4188 /* command "set data bits low byte" */
4189 buffer_write(0x80);
4190 buffer_write(low_output);
4191 buffer_write(low_direction);
4192 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4193 "low_direction: 0x%2.2x",
4194 trst, srst, low_output, low_direction);
4196 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4197 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4198 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4199 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4200 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4202 if (trst == 1)
4204 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4205 high_output &= ~nTRSTnOE;
4206 else
4207 high_output &= ~nTRST;
4209 else if (trst == 0)
4211 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4212 high_output |= nTRSTnOE;
4213 else
4214 high_output |= nTRST;
4217 if (srst == 1)
4219 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4220 high_output &= ~nSRST;
4221 else
4222 high_output &= ~nSRSTnOE;
4224 else if (srst == 0)
4226 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4227 high_output |= nSRST;
4228 else
4229 high_output |= nSRSTnOE;
4232 /* command "set data bits high byte" */
4233 buffer_write(0x82);
4234 buffer_write(high_output);
4235 buffer_write(high_direction);
4236 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4237 "high_direction: 0x%2.2x",
4238 trst, srst, high_output, high_direction);
4240 else if (signalyzer_h_adapter_type == 0x0000)
4242 if (trst == 1)
4244 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4245 /* switch to output pin (output is low) */
4246 low_direction |= nTRSTnOE;
4247 else
4248 /* switch output low */
4249 low_output &= ~nTRST;
4251 else if (trst == 0)
4253 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4254 /* switch to input pin (high-Z + internal
4255 * and external pullup) */
4256 low_direction &= ~nTRSTnOE;
4257 else
4258 /* switch output high */
4259 low_output |= nTRST;
4262 if (srst == 1)
4264 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4265 /* switch output low */
4266 low_output &= ~nSRST;
4267 else
4268 /* switch to output pin (output is low) */
4269 low_direction |= nSRSTnOE;
4271 else if (srst == 0)
4273 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4274 /* switch output high */
4275 low_output |= nSRST;
4276 else
4277 /* switch to input pin (high-Z) */
4278 low_direction &= ~nSRSTnOE;
4281 /* command "set data bits low byte" */
4282 buffer_write(0x80);
4283 buffer_write(low_output);
4284 buffer_write(low_direction);
4285 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4286 "low_direction: 0x%2.2x",
4287 trst, srst, low_output, low_direction);
4291 static void signalyzer_h_blink(void)
4293 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4296 /********************************************************************
4297 * Support for KT-LINK
4298 * JTAG adapter from KRISTECH
4299 * http://www.kristech.eu
4300 *******************************************************************/
4301 static int ktlink_init(void)
4303 uint8_t buf[3];
4304 uint32_t bytes_written;
4305 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4307 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4308 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4310 // initialize low port
4311 buf[0] = 0x80; // command "set data bits low byte"
4312 buf[1] = low_output;
4313 buf[2] = low_direction;
4314 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4316 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4318 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4319 return ERROR_JTAG_INIT_FAILED;
4322 nTRST = 0x01;
4323 nSRST = 0x02;
4324 nTRSTnOE = 0x04;
4325 nSRSTnOE = 0x08;
4327 high_output = 0x80; // turn LED on
4328 high_direction = 0xFF; // all outputs
4330 enum reset_types jtag_reset_config = jtag_get_reset_config();
4332 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4333 high_output |= nTRSTnOE;
4334 high_output &= ~nTRST;
4335 } else {
4336 high_output &= ~nTRSTnOE;
4337 high_output |= nTRST;
4340 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4341 high_output &= ~nSRSTnOE;
4342 high_output |= nSRST;
4343 } else {
4344 high_output |= nSRSTnOE;
4345 high_output &= ~nSRST;
4348 // initialize high port
4349 buf[0] = 0x82; // command "set data bits high byte"
4350 buf[1] = high_output; // value
4351 buf[2] = high_direction;
4352 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4354 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
4356 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4357 return ERROR_JTAG_INIT_FAILED;
4360 return ERROR_OK;
4363 static void ktlink_reset(int trst, int srst)
4365 enum reset_types jtag_reset_config = jtag_get_reset_config();
4367 if (trst == 1) {
4368 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4369 high_output &= ~nTRSTnOE;
4370 else
4371 high_output &= ~nTRST;
4372 } else if (trst == 0) {
4373 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4374 high_output |= nTRSTnOE;
4375 else
4376 high_output |= nTRST;
4379 if (srst == 1) {
4380 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4381 high_output &= ~nSRST;
4382 else
4383 high_output &= ~nSRSTnOE;
4384 } else if (srst == 0) {
4385 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4386 high_output |= nSRST;
4387 else
4388 high_output |= nSRSTnOE;
4391 buffer_write(0x82); // command "set data bits high byte"
4392 buffer_write(high_output);
4393 buffer_write(high_direction);
4394 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4397 static void ktlink_blink(void)
4399 /* LED connected to ACBUS7 */
4400 if (high_output & 0x80)
4401 high_output &= 0x7F;
4402 else
4403 high_output |= 0x80;
4405 buffer_write(0x82); // command "set data bits high byte"
4406 buffer_write(high_output);
4407 buffer_write(high_direction);
4410 static const struct command_registration ft2232_command_handlers[] = {
4412 .name = "ft2232_device_desc",
4413 .handler = &ft2232_handle_device_desc_command,
4414 .mode = COMMAND_CONFIG,
4415 .help = "set the USB device description of the FTDI FT2232 device",
4416 .usage = "description_string",
4419 .name = "ft2232_serial",
4420 .handler = &ft2232_handle_serial_command,
4421 .mode = COMMAND_CONFIG,
4422 .help = "set the serial number of the FTDI FT2232 device",
4423 .usage = "serial_string",
4426 .name = "ft2232_layout",
4427 .handler = &ft2232_handle_layout_command,
4428 .mode = COMMAND_CONFIG,
4429 .help = "set the layout of the FT2232 GPIO signals used "
4430 "to control output-enables and reset signals",
4431 .usage = "layout_name",
4434 .name = "ft2232_vid_pid",
4435 .handler = &ft2232_handle_vid_pid_command,
4436 .mode = COMMAND_CONFIG,
4437 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4438 .usage = "(vid pid)* ",
4441 .name = "ft2232_latency",
4442 .handler = &ft2232_handle_latency_command,
4443 .mode = COMMAND_CONFIG,
4444 .help = "set the FT2232 latency timer to a new value",
4445 .usage = "value",
4447 COMMAND_REGISTRATION_DONE
4450 struct jtag_interface ft2232_interface = {
4451 .name = "ft2232",
4452 .supported = DEBUG_CAP_TMS_SEQ,
4453 .commands = ft2232_command_handlers,
4454 .transports = jtag_only,
4456 .init = ft2232_init,
4457 .quit = ft2232_quit,
4458 .speed = ft2232_speed,
4459 .speed_div = ft2232_speed_div,
4460 .khz = ft2232_khz,
4461 .execute_queue = ft2232_execute_queue,