- Fixes '=' whitespace
[openocd.git] / src / jtag / ft2232.c
blob80de04f9ecfef2078b69356ab5ede7b6419fad2d
1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
9 * Dick Hollenbeck <dick@softplc.com> *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
28 /* This code uses information contained in the MPSSE specification which was
29 * found here:
30 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
31 * Hereafter this is called the "MPSSE Spec".
33 * The datasheet for the ftdichip.com's FT2232D part is here:
34 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 /* project specific includes */
43 #include "interface.h"
44 #include "commands.h"
45 #include "time_support.h"
47 #if IS_CYGWIN == 1
48 #include <windows.h>
49 #endif
51 #include <assert.h>
53 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
54 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
55 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
56 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
57 #endif
59 /* FT2232 access library includes */
60 #if BUILD_FT2232_FTD2XX == 1
61 #include <ftd2xx.h>
62 #elif BUILD_FT2232_LIBFTDI == 1
63 #include <ftdi.h>
64 #endif
66 /* max TCK for the high speed devices 30000 kHz */
67 #define FTDI_2232H_4232H_MAX_TCK 30000
69 static int ft2232_execute_queue(void);
71 static int ft2232_speed(int speed);
72 static int ft2232_speed_div(int speed, int* khz);
73 static int ft2232_khz(int khz, int* jtag_speed);
74 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
75 static int ft2232_init(void);
76 static int ft2232_quit(void);
78 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
79 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
80 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
81 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
82 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
85 /**
86 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
87 * stable state. Calling code must ensure that current state is stable,
88 * that verification is not done in here.
90 * @param num_cycles The number of clocks cycles to send.
91 * @param cmd The command to send.
93 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
95 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
97 /* max TCK for the high speed devices 30000 kHz */
98 #define FTDI_2232H_4232H_MAX_TCK 30000
100 static char * ft2232_device_desc_A = NULL;
101 static char* ft2232_device_desc = NULL;
102 static char* ft2232_serial = NULL;
103 static char* ft2232_layout = NULL;
104 static uint8_t ft2232_latency = 2;
105 static unsigned ft2232_max_tck = 6000;
108 #define MAX_USB_IDS 8
109 /* vid = pid = 0 marks the end of the list */
110 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
111 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
113 typedef struct ft2232_layout_s
115 char* name;
116 int (*init)(void);
117 void (*reset)(int trst, int srst);
118 void (*blink)(void);
119 } ft2232_layout_t;
121 /* init procedures for supported layouts */
122 static int usbjtag_init(void);
123 static int jtagkey_init(void);
124 static int olimex_jtag_init(void);
125 static int flyswatter_init(void);
126 static int turtle_init(void);
127 static int comstick_init(void);
128 static int stm32stick_init(void);
129 static int axm0432_jtag_init(void);
130 static int sheevaplug_init(void);
131 static int icebear_jtag_init(void);
132 static int cortino_jtag_init(void);
134 /* reset procedures for supported layouts */
135 static void usbjtag_reset(int trst, int srst);
136 static void jtagkey_reset(int trst, int srst);
137 static void olimex_jtag_reset(int trst, int srst);
138 static void flyswatter_reset(int trst, int srst);
139 static void turtle_reset(int trst, int srst);
140 static void comstick_reset(int trst, int srst);
141 static void stm32stick_reset(int trst, int srst);
142 static void axm0432_jtag_reset(int trst, int srst);
143 static void sheevaplug_reset(int trst, int srst);
144 static void icebear_jtag_reset(int trst, int srst);
146 /* blink procedures for layouts that support a blinking led */
147 static void olimex_jtag_blink(void);
148 static void flyswatter_jtag_blink(void);
149 static void turtle_jtag_blink(void);
151 static const ft2232_layout_t ft2232_layouts[] =
153 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
154 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
155 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
156 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
157 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
158 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
159 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
160 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
161 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
162 { "comstick", comstick_init, comstick_reset, NULL },
163 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
164 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
165 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
166 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
167 { "cortino", cortino_jtag_init, comstick_reset, NULL },
168 { NULL, NULL, NULL, NULL },
171 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
173 static const ft2232_layout_t *layout;
174 static uint8_t low_output = 0x0;
175 static uint8_t low_direction = 0x0;
176 static uint8_t high_output = 0x0;
177 static uint8_t high_direction = 0x0;
179 #if BUILD_FT2232_FTD2XX == 1
180 static FT_HANDLE ftdih = NULL;
181 static FT_DEVICE ftdi_device = 0;
182 #elif BUILD_FT2232_LIBFTDI == 1
183 static struct ftdi_context ftdic;
184 #endif
187 static jtag_command_t* first_unsent; /* next command that has to be sent */
188 static int require_send;
191 /* http://urjtag.wiki.sourceforge.net/Cable+FT2232 says:
193 "There is a significant difference between libftdi and libftd2xx. The latter
194 one allows to schedule up to 64*64 bytes of result data while libftdi fails
195 with more than 4*64. As a consequence, the FT2232 driver is forced to
196 perform around 16x more USB transactions for long command streams with TDO
197 capture when running with libftdi."
199 No idea how we get
200 #define FT2232_BUFFER_SIZE 131072
201 a comment would have been nice.
204 #define FT2232_BUFFER_SIZE 131072
206 static uint8_t* ft2232_buffer = NULL;
207 static int ft2232_buffer_size = 0;
208 static int ft2232_read_pointer = 0;
209 static int ft2232_expect_read = 0;
212 * Function buffer_write
213 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
214 * @param val is the byte to send.
216 static inline void buffer_write(uint8_t val)
218 assert(ft2232_buffer);
219 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
220 ft2232_buffer[ft2232_buffer_size++] = val;
224 * Function buffer_read
225 * returns a byte from the byte buffer.
227 static inline uint8_t buffer_read(void)
229 assert(ft2232_buffer);
230 assert(ft2232_read_pointer < ft2232_buffer_size);
231 return ft2232_buffer[ft2232_read_pointer++];
236 * Clocks out \a bit_count bits on the TMS line, starting with the least
237 * significant bit of tms_bits and progressing to more significant bits.
238 * Rigorous state transition logging is done here via tap_set_state().
240 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
241 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
242 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
243 * is often used for this, 0x4b.
245 * @param tms_bits Holds the sequence of bits to send.
246 * @param tms_count Tells how many bits in the sequence.
247 * @param tdi_bit A single bit to pass on to TDI before the first TCK
248 * cycle and held static for the duration of TMS clocking.
250 * See the MPSSE spec referenced above.
252 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
254 uint8_t tms_byte;
255 int i;
256 int tms_ndx; /* bit index into tms_byte */
258 assert(tms_count > 0);
260 // LOG_DEBUG("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d", mpsse_cmd, tms_bits, tms_count);
262 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
264 bool bit = tms_bits & 1;
266 if (bit)
267 tms_byte |= (1 << tms_ndx);
269 /* always do state transitions in public view */
270 tap_set_state(tap_state_transition(tap_get_state(), bit));
272 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
273 also increment.
275 ++tms_ndx;
277 if (tms_ndx == 7 || i == tms_count-1)
279 buffer_write(mpsse_cmd);
280 buffer_write(tms_ndx - 1);
282 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
283 TMS/CS and is held static for the duration of TMS/CS clocking.
285 buffer_write(tms_byte | (tdi_bit << 7));
292 * Function get_tms_buffer_requirements
293 * returns what clock_tms() will consume if called with
294 * same \a bit_count.
296 static inline int get_tms_buffer_requirements(int bit_count)
298 return ((bit_count + 6)/7) * 3;
303 * Function move_to_state
304 * moves the TAP controller from the current state to a
305 * \a goal_state through a path given by tap_get_tms_path(). State transition
306 * logging is performed by delegation to clock_tms().
308 * @param goal_state is the destination state for the move.
310 static void move_to_state(tap_state_t goal_state)
312 tap_state_t start_state = tap_get_state();
314 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
315 lookup of the required TMS pattern to move to this state from the
316 start state.
319 /* do the 2 lookups */
320 int tms_bits = tap_get_tms_path(start_state, goal_state);
321 int tms_count = tap_get_tms_path_len(start_state, goal_state);
323 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
325 clock_tms(0x4b, tms_bits, tms_count, 0);
329 jtag_interface_t ft2232_interface =
331 .name = "ft2232",
332 .execute_queue = ft2232_execute_queue,
333 .speed = ft2232_speed,
334 .speed_div = ft2232_speed_div,
335 .khz = ft2232_khz,
336 .register_commands = ft2232_register_commands,
337 .init = ft2232_init,
338 .quit = ft2232_quit,
341 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
343 #if BUILD_FT2232_FTD2XX == 1
344 FT_STATUS status;
345 DWORD dw_bytes_written;
346 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
348 *bytes_written = dw_bytes_written;
349 LOG_ERROR("FT_Write returned: %lu", status);
350 return ERROR_JTAG_DEVICE_ERROR;
352 else
354 *bytes_written = dw_bytes_written;
355 return ERROR_OK;
357 #elif BUILD_FT2232_LIBFTDI == 1
358 int retval;
359 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
361 *bytes_written = 0;
362 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
363 return ERROR_JTAG_DEVICE_ERROR;
365 else
367 *bytes_written = retval;
368 return ERROR_OK;
370 #endif
374 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
376 #if BUILD_FT2232_FTD2XX == 1
377 DWORD dw_bytes_read;
378 FT_STATUS status;
379 int timeout = 5;
380 *bytes_read = 0;
382 while ((*bytes_read < size) && timeout--)
384 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
385 *bytes_read, &dw_bytes_read)) != FT_OK)
387 *bytes_read = 0;
388 LOG_ERROR("FT_Read returned: %lu", status);
389 return ERROR_JTAG_DEVICE_ERROR;
391 *bytes_read += dw_bytes_read;
394 #elif BUILD_FT2232_LIBFTDI == 1
395 int retval;
396 int timeout = 100;
397 *bytes_read = 0;
399 while ((*bytes_read < size) && timeout--)
401 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
403 *bytes_read = 0;
404 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
405 return ERROR_JTAG_DEVICE_ERROR;
407 *bytes_read += retval;
410 #endif
412 if (*bytes_read < size)
414 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)",
415 (unsigned int)(*bytes_read),
416 (unsigned int)size);
417 return ERROR_JTAG_DEVICE_ERROR;
420 return ERROR_OK;
423 #ifdef BUILD_FTD2XX_HIGHSPEED
424 static bool ft2232_device_is_highspeed(void)
426 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
429 static int ft2232_adaptive_clocking(int speed)
431 bool use_adaptive_clocking = FALSE;
432 if (0 == speed)
434 if (ft2232_device_is_highspeed())
435 use_adaptive_clocking = TRUE;
436 else
438 LOG_ERROR("ft2232 device %lu does not support RTCK", ftdi_device);
439 return ERROR_OK;
443 uint8_t buf = use_adaptive_clocking ? 0x96 : 0x97;
444 LOG_DEBUG("%2.2x", buf);
446 uint32_t bytes_written;
447 int retval = ft2232_write(&buf, 1, &bytes_written);
448 if (ERROR_OK != retval || bytes_written != 1)
450 LOG_ERROR("unable to set adative clocking: %d", retval);
451 return retval;
454 return ERROR_OK;
456 #else
457 static int ft2232_adaptive_clocking(int speed)
459 // not implemented on low-speed devices
460 return speed ? ERROR_OK : -1234;
462 #endif
464 static int ft2232_speed(int speed)
466 uint8_t buf[3];
467 int retval;
468 uint32_t bytes_written;
470 ft2232_adaptive_clocking(speed);
472 buf[0] = 0x86; /* command "set divisor" */
473 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
474 buf[2] = (speed >> 8) & 0xff; /* valueH */
476 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
477 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
479 LOG_ERROR("couldn't set FT2232 TCK speed");
480 return retval;
483 return ERROR_OK;
487 static int ft2232_speed_div(int speed, int* khz)
489 /* Take a look in the FT2232 manual,
490 * AN2232C-01 Command Processor for
491 * MPSSE and MCU Host Bus. Chapter 3.8 */
493 *khz = ft2232_max_tck / (1 + speed);
495 return ERROR_OK;
499 static int ft2232_khz(int khz, int* jtag_speed)
501 if (khz == 0)
503 #ifdef BUILD_FTD2XX_HIGHSPEED
504 *jtag_speed = 0;
505 return ERROR_OK;
506 #else
507 LOG_DEBUG("RCLK not supported");
508 LOG_DEBUG("If you have a high-speed FTDI device, then "
509 "OpenOCD may be built with --enable-ftd2xx-highspeed.");
510 return ERROR_FAIL;
511 #endif
514 /* Take a look in the FT2232 manual,
515 * AN2232C-01 Command Processor for
516 * MPSSE and MCU Host Bus. Chapter 3.8
518 * We will calc here with a multiplier
519 * of 10 for better rounding later. */
521 /* Calc speed, (ft2232_max_tck / khz) - 1 */
522 /* Use 65000 for better rounding */
523 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
525 /* Add 0.9 for rounding */
526 *jtag_speed += 9;
528 /* Calc real speed */
529 *jtag_speed = *jtag_speed / 10;
531 /* Check if speed is greater than 0 */
532 if (*jtag_speed < 0)
534 *jtag_speed = 0;
537 /* Check max value */
538 if (*jtag_speed > 0xFFFF)
540 *jtag_speed = 0xFFFF;
543 return ERROR_OK;
547 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
549 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
550 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
551 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
552 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
553 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
554 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
555 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
556 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
557 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
558 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
559 return ERROR_OK;
563 static void ft2232_end_state(tap_state_t state)
565 if (tap_is_state_stable(state))
566 tap_set_end_state(state);
567 else
569 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
570 exit(-1);
574 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
576 int num_bytes = (scan_size + 7) / 8;
577 int bits_left = scan_size;
578 int cur_byte = 0;
580 while (num_bytes-- > 1)
582 buffer[cur_byte++] = buffer_read();
583 bits_left -= 8;
586 buffer[cur_byte] = 0x0;
588 /* There is one more partial byte left from the clock data in/out instructions */
589 if (bits_left > 1)
591 buffer[cur_byte] = buffer_read() >> 1;
593 /* 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 */
594 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
598 static void ft2232_debug_dump_buffer(void)
600 int i;
601 char line[256];
602 char* line_p = line;
604 for (i = 0; i < ft2232_buffer_size; i++)
606 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
607 if (i % 16 == 15)
609 LOG_DEBUG("%s", line);
610 line_p = line;
614 if (line_p != line)
615 LOG_DEBUG("%s", line);
619 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
621 jtag_command_t* cmd;
622 uint8_t* buffer;
623 int scan_size;
624 enum scan_type type;
625 int retval;
626 uint32_t bytes_written = 0;
627 uint32_t bytes_read = 0;
629 #ifdef _DEBUG_USB_IO_
630 struct timeval start, inter, inter2, end;
631 struct timeval d_inter, d_inter2, d_end;
632 #endif
634 #ifdef _DEBUG_USB_COMMS_
635 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
636 ft2232_debug_dump_buffer();
637 #endif
639 #ifdef _DEBUG_USB_IO_
640 gettimeofday(&start, NULL);
641 #endif
643 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
645 LOG_ERROR("couldn't write MPSSE commands to FT2232");
646 return retval;
649 #ifdef _DEBUG_USB_IO_
650 gettimeofday(&inter, NULL);
651 #endif
653 if (ft2232_expect_read)
655 int timeout = 100;
656 ft2232_buffer_size = 0;
658 #ifdef _DEBUG_USB_IO_
659 gettimeofday(&inter2, NULL);
660 #endif
662 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
664 LOG_ERROR("couldn't read from FT2232");
665 return retval;
668 #ifdef _DEBUG_USB_IO_
669 gettimeofday(&end, NULL);
671 timeval_subtract(&d_inter, &inter, &start);
672 timeval_subtract(&d_inter2, &inter2, &start);
673 timeval_subtract(&d_end, &end, &start);
675 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
676 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
677 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
678 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
679 #endif
681 ft2232_buffer_size = bytes_read;
683 if (ft2232_expect_read != ft2232_buffer_size)
685 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
686 ft2232_buffer_size,
687 100 - timeout);
688 ft2232_debug_dump_buffer();
690 exit(-1);
693 #ifdef _DEBUG_USB_COMMS_
694 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
695 ft2232_debug_dump_buffer();
696 #endif
699 ft2232_expect_read = 0;
700 ft2232_read_pointer = 0;
702 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
703 * that wasn't handled by a caller-provided error handler
705 retval = ERROR_OK;
707 cmd = first;
708 while (cmd != last)
710 switch (cmd->type)
712 case JTAG_SCAN:
713 type = jtag_scan_type(cmd->cmd.scan);
714 if (type != SCAN_OUT)
716 scan_size = jtag_scan_size(cmd->cmd.scan);
717 buffer = calloc(CEIL(scan_size, 8), 1);
718 ft2232_read_scan(type, buffer, scan_size);
719 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
720 retval = ERROR_JTAG_QUEUE_FAILED;
721 free(buffer);
723 break;
725 default:
726 break;
729 cmd = cmd->next;
732 ft2232_buffer_size = 0;
734 return retval;
739 * Function ft2232_add_pathmove
740 * moves the TAP controller from the current state to a new state through the
741 * given path, where path is an array of tap_state_t's.
743 * @param path is an array of tap_stat_t which gives the states to traverse through
744 * ending with the last state at path[num_states-1]
745 * @param num_states is the count of state steps to move through
747 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
749 int tms_bits = 0;
750 int state_ndx;
751 tap_state_t walker = tap_get_state();
753 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
755 /* this loop verifies that the path is legal and logs each state in the path */
756 for (state_ndx = 0; state_ndx < num_states; ++state_ndx)
758 tap_state_t desired_next_state = path[state_ndx];
760 if (tap_state_transition(walker, false) == desired_next_state)
761 ; /* bit within tms_bits at index state_ndx is already zero */
762 else if (tap_state_transition(walker, true) == desired_next_state)
763 tms_bits |= (1 << state_ndx);
764 else
766 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
767 tap_state_name(walker), tap_state_name(desired_next_state));
768 exit(-1);
771 walker = desired_next_state;
774 clock_tms(0x4b, tms_bits, num_states, 0);
776 tap_set_end_state(tap_get_state());
780 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
782 int num_bytes = (scan_size + 7) / 8;
783 int bits_left = scan_size;
784 int cur_byte = 0;
785 int last_bit;
787 if (!ir_scan)
789 if (tap_get_state() != TAP_DRSHIFT)
791 move_to_state(TAP_DRSHIFT);
794 else
796 if (tap_get_state() != TAP_IRSHIFT)
798 move_to_state(TAP_IRSHIFT);
802 /* add command for complete bytes */
803 while (num_bytes > 1)
805 int thisrun_bytes;
806 if (type == SCAN_IO)
808 /* Clock Data Bytes In and Out LSB First */
809 buffer_write(0x39);
810 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
812 else if (type == SCAN_OUT)
814 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
815 buffer_write(0x19);
816 /* LOG_DEBUG("added TDI bytes (o)"); */
818 else if (type == SCAN_IN)
820 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
821 buffer_write(0x28);
822 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
825 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
826 num_bytes -= thisrun_bytes;
828 buffer_write((uint8_t) (thisrun_bytes - 1));
829 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
831 if (type != SCAN_IN)
833 /* add complete bytes */
834 while (thisrun_bytes-- > 0)
836 buffer_write(buffer[cur_byte++]);
837 bits_left -= 8;
840 else /* (type == SCAN_IN) */
842 bits_left -= 8 * (thisrun_bytes);
846 /* the most signifcant bit is scanned during TAP movement */
847 if (type != SCAN_IN)
848 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
849 else
850 last_bit = 0;
852 /* process remaining bits but the last one */
853 if (bits_left > 1)
855 if (type == SCAN_IO)
857 /* Clock Data Bits In and Out LSB First */
858 buffer_write(0x3b);
859 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
861 else if (type == SCAN_OUT)
863 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
864 buffer_write(0x1b);
865 /* LOG_DEBUG("added TDI bits (o)"); */
867 else if (type == SCAN_IN)
869 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
870 buffer_write(0x2a);
871 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
874 buffer_write(bits_left - 2);
875 if (type != SCAN_IN)
876 buffer_write(buffer[cur_byte]);
879 if (( ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
880 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
882 if (type == SCAN_IO)
884 /* Clock Data Bits In and Out LSB First */
885 buffer_write(0x3b);
886 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
888 else if (type == SCAN_OUT)
890 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
891 buffer_write(0x1b);
892 /* LOG_DEBUG("added TDI bits (o)"); */
894 else if (type == SCAN_IN)
896 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
897 buffer_write(0x2a);
898 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
900 buffer_write(0x0);
901 buffer_write(last_bit);
903 else
905 int tms_bits;
906 int tms_count;
907 uint8_t mpsse_cmd;
909 /* move from Shift-IR/DR to end state */
910 if (type != SCAN_OUT)
912 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
913 /* This must be coordinated with the bit shifts in ft2232_read_scan */
914 tms_bits = 0x01;
915 tms_count = 2;
916 /* Clock Data to TMS/CS Pin with Read */
917 mpsse_cmd = 0x6b;
918 /* LOG_DEBUG("added TMS scan (read)"); */
920 else
922 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
923 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
924 /* Clock Data to TMS/CS Pin (no Read) */
925 mpsse_cmd = 0x4b;
926 /* LOG_DEBUG("added TMS scan (no read)"); */
929 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
932 if (tap_get_state() != tap_get_end_state())
934 move_to_state(tap_get_end_state());
939 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
941 int num_bytes = (scan_size + 7) / 8;
942 int bits_left = scan_size;
943 int cur_byte = 0;
944 int last_bit;
945 uint8_t* receive_buffer = malloc(CEIL(scan_size, 8));
946 uint8_t* receive_pointer = receive_buffer;
947 uint32_t bytes_written;
948 uint32_t bytes_read;
949 int retval;
950 int thisrun_read = 0;
952 if (cmd->ir_scan)
954 LOG_ERROR("BUG: large IR scans are not supported");
955 exit(-1);
958 if (tap_get_state() != TAP_DRSHIFT)
960 move_to_state(TAP_DRSHIFT);
963 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
965 LOG_ERROR("couldn't write MPSSE commands to FT2232");
966 exit(-1);
968 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
969 ft2232_buffer_size, (int)bytes_written);
970 ft2232_buffer_size = 0;
972 /* add command for complete bytes */
973 while (num_bytes > 1)
975 int thisrun_bytes;
977 if (type == SCAN_IO)
979 /* Clock Data Bytes In and Out LSB First */
980 buffer_write(0x39);
981 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
983 else if (type == SCAN_OUT)
985 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
986 buffer_write(0x19);
987 /* LOG_DEBUG("added TDI bytes (o)"); */
989 else if (type == SCAN_IN)
991 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
992 buffer_write(0x28);
993 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
996 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
997 thisrun_read = thisrun_bytes;
998 num_bytes -= thisrun_bytes;
999 buffer_write((uint8_t) (thisrun_bytes - 1));
1000 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1002 if (type != SCAN_IN)
1004 /* add complete bytes */
1005 while (thisrun_bytes-- > 0)
1007 buffer_write(buffer[cur_byte]);
1008 cur_byte++;
1009 bits_left -= 8;
1012 else /* (type == SCAN_IN) */
1014 bits_left -= 8 * (thisrun_bytes);
1017 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1019 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1020 exit(-1);
1022 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1023 ft2232_buffer_size,
1024 (int)bytes_written);
1025 ft2232_buffer_size = 0;
1027 if (type != SCAN_OUT)
1029 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1031 LOG_ERROR("couldn't read from FT2232");
1032 exit(-1);
1034 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1035 thisrun_read,
1036 (int)bytes_read);
1037 receive_pointer += bytes_read;
1041 thisrun_read = 0;
1043 /* the most signifcant bit is scanned during TAP movement */
1044 if (type != SCAN_IN)
1045 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1046 else
1047 last_bit = 0;
1049 /* process remaining bits but the last one */
1050 if (bits_left > 1)
1052 if (type == SCAN_IO)
1054 /* Clock Data Bits In and Out LSB First */
1055 buffer_write(0x3b);
1056 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1058 else if (type == SCAN_OUT)
1060 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1061 buffer_write(0x1b);
1062 /* LOG_DEBUG("added TDI bits (o)"); */
1064 else if (type == SCAN_IN)
1066 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1067 buffer_write(0x2a);
1068 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1070 buffer_write(bits_left - 2);
1071 if (type != SCAN_IN)
1072 buffer_write(buffer[cur_byte]);
1074 if (type != SCAN_OUT)
1075 thisrun_read += 2;
1078 if (tap_get_end_state() == TAP_DRSHIFT)
1080 if (type == SCAN_IO)
1082 /* Clock Data Bits In and Out LSB First */
1083 buffer_write(0x3b);
1084 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1086 else if (type == SCAN_OUT)
1088 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1089 buffer_write(0x1b);
1090 /* LOG_DEBUG("added TDI bits (o)"); */
1092 else if (type == SCAN_IN)
1094 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1095 buffer_write(0x2a);
1096 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1098 buffer_write(0x0);
1099 buffer_write(last_bit);
1101 else
1103 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1104 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1105 uint8_t mpsse_cmd;
1107 /* move from Shift-IR/DR to end state */
1108 if (type != SCAN_OUT)
1110 /* Clock Data to TMS/CS Pin with Read */
1111 mpsse_cmd = 0x6b;
1112 /* LOG_DEBUG("added TMS scan (read)"); */
1114 else
1116 /* Clock Data to TMS/CS Pin (no Read) */
1117 mpsse_cmd = 0x4b;
1118 /* LOG_DEBUG("added TMS scan (no read)"); */
1121 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1124 if (type != SCAN_OUT)
1125 thisrun_read += 1;
1127 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1129 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1130 exit(-1);
1132 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1133 ft2232_buffer_size,
1134 (int)bytes_written);
1135 ft2232_buffer_size = 0;
1137 if (type != SCAN_OUT)
1139 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1141 LOG_ERROR("couldn't read from FT2232");
1142 exit(-1);
1144 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1145 thisrun_read,
1146 (int)bytes_read);
1147 receive_pointer += bytes_read;
1150 return ERROR_OK;
1154 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1156 int predicted_size = 3;
1157 int num_bytes = (scan_size - 1) / 8;
1159 if (tap_get_state() != TAP_DRSHIFT)
1160 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1162 if (type == SCAN_IN) /* only from device to host */
1164 /* complete bytes */
1165 predicted_size += CEIL(num_bytes, 65536) * 3;
1167 /* remaining bits - 1 (up to 7) */
1168 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1170 else /* host to device, or bidirectional */
1172 /* complete bytes */
1173 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
1175 /* remaining bits -1 (up to 7) */
1176 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1179 return predicted_size;
1183 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1185 int predicted_size = 0;
1187 if (type != SCAN_OUT)
1189 /* complete bytes */
1190 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
1192 /* remaining bits - 1 */
1193 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1195 /* last bit (from TMS scan) */
1196 predicted_size += 1;
1199 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1201 return predicted_size;
1205 static void usbjtag_reset(int trst, int srst)
1207 enum reset_types jtag_reset_config = jtag_get_reset_config();
1208 if (trst == 1)
1210 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1211 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1212 else
1213 low_output &= ~nTRST; /* switch output low */
1215 else if (trst == 0)
1217 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1218 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1219 else
1220 low_output |= nTRST; /* switch output high */
1223 if (srst == 1)
1225 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1226 low_output &= ~nSRST; /* switch output low */
1227 else
1228 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1230 else if (srst == 0)
1232 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1233 low_output |= nSRST; /* switch output high */
1234 else
1235 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1238 /* command "set data bits low byte" */
1239 buffer_write(0x80);
1240 buffer_write(low_output);
1241 buffer_write(low_direction);
1245 static void jtagkey_reset(int trst, int srst)
1247 enum reset_types jtag_reset_config = jtag_get_reset_config();
1248 if (trst == 1)
1250 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1251 high_output &= ~nTRSTnOE;
1252 else
1253 high_output &= ~nTRST;
1255 else if (trst == 0)
1257 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1258 high_output |= nTRSTnOE;
1259 else
1260 high_output |= nTRST;
1263 if (srst == 1)
1265 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1266 high_output &= ~nSRST;
1267 else
1268 high_output &= ~nSRSTnOE;
1270 else if (srst == 0)
1272 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1273 high_output |= nSRST;
1274 else
1275 high_output |= nSRSTnOE;
1278 /* command "set data bits high byte" */
1279 buffer_write(0x82);
1280 buffer_write(high_output);
1281 buffer_write(high_direction);
1282 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1283 high_direction);
1287 static void olimex_jtag_reset(int trst, int srst)
1289 enum reset_types jtag_reset_config = jtag_get_reset_config();
1290 if (trst == 1)
1292 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1293 high_output &= ~nTRSTnOE;
1294 else
1295 high_output &= ~nTRST;
1297 else if (trst == 0)
1299 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1300 high_output |= nTRSTnOE;
1301 else
1302 high_output |= nTRST;
1305 if (srst == 1)
1307 high_output |= nSRST;
1309 else if (srst == 0)
1311 high_output &= ~nSRST;
1314 /* command "set data bits high byte" */
1315 buffer_write(0x82);
1316 buffer_write(high_output);
1317 buffer_write(high_direction);
1318 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1319 high_direction);
1323 static void axm0432_jtag_reset(int trst, int srst)
1325 if (trst == 1)
1327 tap_set_state(TAP_RESET);
1328 high_output &= ~nTRST;
1330 else if (trst == 0)
1332 high_output |= nTRST;
1335 if (srst == 1)
1337 high_output &= ~nSRST;
1339 else if (srst == 0)
1341 high_output |= nSRST;
1344 /* command "set data bits low byte" */
1345 buffer_write(0x82);
1346 buffer_write(high_output);
1347 buffer_write(high_direction);
1348 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1349 high_direction);
1353 static void flyswatter_reset(int trst, int srst)
1355 if (trst == 1)
1357 low_output &= ~nTRST;
1359 else if (trst == 0)
1361 low_output |= nTRST;
1364 if (srst == 1)
1366 low_output |= nSRST;
1368 else if (srst == 0)
1370 low_output &= ~nSRST;
1373 /* command "set data bits low byte" */
1374 buffer_write(0x80);
1375 buffer_write(low_output);
1376 buffer_write(low_direction);
1377 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1381 static void turtle_reset(int trst, int srst)
1383 trst = trst;
1385 if (srst == 1)
1387 low_output |= nSRST;
1389 else if (srst == 0)
1391 low_output &= ~nSRST;
1394 /* command "set data bits low byte" */
1395 buffer_write(0x80);
1396 buffer_write(low_output);
1397 buffer_write(low_direction);
1398 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1402 static void comstick_reset(int trst, int srst)
1404 if (trst == 1)
1406 high_output &= ~nTRST;
1408 else if (trst == 0)
1410 high_output |= nTRST;
1413 if (srst == 1)
1415 high_output &= ~nSRST;
1417 else if (srst == 0)
1419 high_output |= nSRST;
1422 /* command "set data bits high byte" */
1423 buffer_write(0x82);
1424 buffer_write(high_output);
1425 buffer_write(high_direction);
1426 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1427 high_direction);
1431 static void stm32stick_reset(int trst, int srst)
1433 if (trst == 1)
1435 high_output &= ~nTRST;
1437 else if (trst == 0)
1439 high_output |= nTRST;
1442 if (srst == 1)
1444 low_output &= ~nSRST;
1446 else if (srst == 0)
1448 low_output |= nSRST;
1451 /* command "set data bits low byte" */
1452 buffer_write(0x80);
1453 buffer_write(low_output);
1454 buffer_write(low_direction);
1456 /* command "set data bits high byte" */
1457 buffer_write(0x82);
1458 buffer_write(high_output);
1459 buffer_write(high_direction);
1460 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1461 high_direction);
1466 static void sheevaplug_reset(int trst, int srst)
1468 if (trst == 1)
1469 high_output &= ~nTRST;
1470 else if (trst == 0)
1471 high_output |= nTRST;
1473 if (srst == 1)
1474 high_output &= ~nSRSTnOE;
1475 else if (srst == 0)
1476 high_output |= nSRSTnOE;
1478 /* command "set data bits high byte" */
1479 buffer_write(0x82);
1480 buffer_write(high_output);
1481 buffer_write(high_direction);
1482 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1485 static int ft2232_execute_runtest(jtag_command_t *cmd)
1487 int retval;
1488 int i;
1489 int predicted_size = 0;
1490 retval = ERROR_OK;
1492 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1493 cmd->cmd.runtest->num_cycles,
1494 tap_state_name(cmd->cmd.runtest->end_state));
1496 /* only send the maximum buffer size that FT2232C can handle */
1497 predicted_size = 0;
1498 if (tap_get_state() != TAP_IDLE)
1499 predicted_size += 3;
1500 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1501 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1502 predicted_size += 3;
1503 if (tap_get_end_state() != TAP_IDLE)
1504 predicted_size += 3;
1505 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1507 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1508 retval = ERROR_JTAG_QUEUE_FAILED;
1509 require_send = 0;
1510 first_unsent = cmd;
1512 if (tap_get_state() != TAP_IDLE)
1514 move_to_state(TAP_IDLE);
1515 require_send = 1;
1517 i = cmd->cmd.runtest->num_cycles;
1518 while (i > 0)
1520 /* there are no state transitions in this code, so omit state tracking */
1522 /* command "Clock Data to TMS/CS Pin (no Read)" */
1523 buffer_write(0x4b);
1525 /* scan 7 bits */
1526 buffer_write((i > 7) ? 6 : (i - 1));
1528 /* TMS data bits */
1529 buffer_write(0x0);
1530 tap_set_state(TAP_IDLE);
1532 i -= (i > 7) ? 7 : i;
1533 /* LOG_DEBUG("added TMS scan (no read)"); */
1536 ft2232_end_state(cmd->cmd.runtest->end_state);
1538 if (tap_get_state() != tap_get_end_state())
1540 move_to_state(tap_get_end_state());
1543 require_send = 1;
1544 #ifdef _DEBUG_JTAG_IO_
1545 LOG_DEBUG("runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(tap_get_end_state()));
1546 #endif
1548 return retval;
1552 static int ft2232_execute_statemove(jtag_command_t *cmd)
1554 int predicted_size = 0;
1555 int retval = ERROR_OK;
1557 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1559 /* only send the maximum buffer size that FT2232C can handle */
1560 predicted_size = 3;
1561 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1563 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1564 retval = ERROR_JTAG_QUEUE_FAILED;
1565 require_send = 0;
1566 first_unsent = cmd;
1568 ft2232_end_state(cmd->cmd.statemove->end_state);
1570 /* move to end state */
1571 if (tap_get_state() != tap_get_end_state())
1573 move_to_state(tap_get_end_state());
1574 require_send = 1;
1577 return retval;
1580 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1582 int predicted_size = 0;
1583 int retval = ERROR_OK;
1585 tap_state_t* path = cmd->cmd.pathmove->path;
1586 int num_states = cmd->cmd.pathmove->num_states;
1588 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1589 tap_state_name(tap_get_state()),
1590 tap_state_name(path[num_states-1])
1593 /* only send the maximum buffer size that FT2232C can handle */
1594 predicted_size = 3 * CEIL(num_states, 7);
1595 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1597 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1598 retval = ERROR_JTAG_QUEUE_FAILED;
1600 require_send = 0;
1601 first_unsent = cmd;
1604 ft2232_add_pathmove(path, num_states);
1605 require_send = 1;
1607 return retval;
1611 static int ft2232_execute_scan(jtag_command_t *cmd)
1613 uint8_t* buffer;
1614 int scan_size; /* size of IR or DR scan */
1615 int predicted_size = 0;
1616 int retval = ERROR_OK;
1618 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1620 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1622 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1624 predicted_size = ft2232_predict_scan_out(scan_size, type);
1625 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1627 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1628 /* unsent commands before this */
1629 if (first_unsent != cmd)
1630 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1631 retval = ERROR_JTAG_QUEUE_FAILED;
1633 /* current command */
1634 ft2232_end_state(cmd->cmd.scan->end_state);
1635 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1636 require_send = 0;
1637 first_unsent = cmd->next;
1638 if (buffer)
1639 free(buffer);
1640 return retval;
1642 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1644 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1645 first_unsent,
1646 cmd);
1647 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1648 retval = ERROR_JTAG_QUEUE_FAILED;
1649 require_send = 0;
1650 first_unsent = cmd;
1652 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1653 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1654 ft2232_end_state(cmd->cmd.scan->end_state);
1655 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1656 require_send = 1;
1657 if (buffer)
1658 free(buffer);
1659 #ifdef _DEBUG_JTAG_IO_
1660 LOG_DEBUG("%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1661 tap_state_name(tap_get_end_state()));
1662 #endif
1663 return retval;
1667 static int ft2232_execute_reset(jtag_command_t *cmd)
1669 int retval;
1670 int predicted_size = 0;
1671 retval = ERROR_OK;
1673 DEBUG_JTAG_IO("reset trst: %i srst %i",
1674 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1676 /* only send the maximum buffer size that FT2232C can handle */
1677 predicted_size = 3;
1678 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1680 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1681 retval = ERROR_JTAG_QUEUE_FAILED;
1682 require_send = 0;
1683 first_unsent = cmd;
1686 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1687 require_send = 1;
1689 #ifdef _DEBUG_JTAG_IO_
1690 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1691 #endif
1692 return retval;
1695 static int ft2232_execute_sleep(jtag_command_t *cmd)
1697 int retval;
1698 retval = ERROR_OK;
1700 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1702 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1703 retval = ERROR_JTAG_QUEUE_FAILED;
1704 first_unsent = cmd->next;
1705 jtag_sleep(cmd->cmd.sleep->us);
1706 #ifdef _DEBUG_JTAG_IO_
1707 LOG_DEBUG("sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name(tap_get_state()));
1708 #endif
1710 return retval;
1713 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1715 int retval;
1716 retval = ERROR_OK;
1718 /* this is only allowed while in a stable state. A check for a stable
1719 * state was done in jtag_add_clocks()
1721 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1722 retval = ERROR_JTAG_QUEUE_FAILED;
1723 #ifdef _DEBUG_JTAG_IO_
1724 LOG_DEBUG("clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name(tap_get_state()));
1725 #endif
1727 return retval;
1730 static int ft2232_execute_command(jtag_command_t *cmd)
1732 int retval;
1733 retval = ERROR_OK;
1735 switch (cmd->type)
1737 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1738 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1739 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1740 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1741 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1742 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1743 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1744 default:
1745 LOG_ERROR("BUG: unknown JTAG command type encountered");
1746 exit(-1);
1748 return retval;
1751 static int ft2232_execute_queue()
1753 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1754 int retval;
1756 first_unsent = cmd; /* next command that has to be sent */
1757 require_send = 0;
1759 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1760 * that wasn't handled by a caller-provided error handler
1762 retval = ERROR_OK;
1764 ft2232_buffer_size = 0;
1765 ft2232_expect_read = 0;
1767 /* blink, if the current layout has that feature */
1768 if (layout->blink)
1769 layout->blink();
1771 while (cmd)
1773 if (ft2232_execute_command(cmd) != ERROR_OK)
1774 retval = ERROR_JTAG_QUEUE_FAILED;
1775 /* Start reading input before FT2232 TX buffer fills up */
1776 cmd = cmd->next;
1777 if (ft2232_expect_read > 256)
1779 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1780 retval = ERROR_JTAG_QUEUE_FAILED;
1781 first_unsent = cmd;
1785 if (require_send > 0)
1786 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1787 retval = ERROR_JTAG_QUEUE_FAILED;
1789 return retval;
1793 #if BUILD_FT2232_FTD2XX == 1
1794 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1796 FT_STATUS status;
1797 DWORD deviceID;
1798 char SerialNumber[16];
1799 char Description[64];
1800 DWORD openex_flags = 0;
1801 char* openex_string = NULL;
1802 uint8_t latency_timer;
1804 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1806 #if IS_WIN32 == 0
1807 /* Add non-standard Vid/Pid to the linux driver */
1808 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1810 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1812 #endif
1814 if (ft2232_device_desc && ft2232_serial)
1816 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1817 ft2232_device_desc = NULL;
1820 if (ft2232_device_desc)
1822 openex_string = ft2232_device_desc;
1823 openex_flags = FT_OPEN_BY_DESCRIPTION;
1825 else if (ft2232_serial)
1827 openex_string = ft2232_serial;
1828 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1830 else
1832 LOG_ERROR("neither device description nor serial number specified");
1833 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1835 return ERROR_JTAG_INIT_FAILED;
1838 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1839 if (status != FT_OK) {
1840 // under Win32, the FTD2XX driver appends an "A" to the end
1841 // of the description, if we tried by the desc, then
1842 // try by the alternate "A" description.
1843 if (openex_string == ft2232_device_desc) {
1844 // Try the alternate method.
1845 openex_string = ft2232_device_desc_A;
1846 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1847 if (status == FT_OK) {
1848 // yea, the "alternate" method worked!
1849 } else {
1850 // drat, give the user a meaningfull message.
1851 // telling the use we tried *BOTH* methods.
1852 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1853 ft2232_device_desc,
1854 ft2232_device_desc_A);
1859 if (status != FT_OK)
1861 DWORD num_devices;
1863 if (more)
1865 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1866 *try_more = 1;
1867 return ERROR_JTAG_INIT_FAILED;
1869 LOG_ERROR("unable to open ftdi device: %lu", status);
1870 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1871 if (status == FT_OK)
1873 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1874 uint32_t i;
1876 for (i = 0; i < num_devices; i++)
1877 desc_array[i] = malloc(64);
1879 desc_array[num_devices] = NULL;
1881 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1883 if (status == FT_OK)
1885 LOG_ERROR("ListDevices: %lu\n", num_devices);
1886 for (i = 0; i < num_devices; i++)
1887 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1890 for (i = 0; i < num_devices; i++)
1891 free(desc_array[i]);
1893 free(desc_array);
1895 else
1897 LOG_ERROR("ListDevices: NONE\n");
1899 return ERROR_JTAG_INIT_FAILED;
1902 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1904 LOG_ERROR("unable to set latency timer: %lu", status);
1905 return ERROR_JTAG_INIT_FAILED;
1908 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1910 LOG_ERROR("unable to get latency timer: %lu", status);
1911 return ERROR_JTAG_INIT_FAILED;
1913 else
1915 LOG_DEBUG("current latency timer: %i", latency_timer);
1918 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1920 LOG_ERROR("unable to set timeouts: %lu", status);
1921 return ERROR_JTAG_INIT_FAILED;
1924 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1926 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1927 return ERROR_JTAG_INIT_FAILED;
1930 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1932 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1933 return ERROR_JTAG_INIT_FAILED;
1935 else
1937 LOG_INFO("device: %lu", ftdi_device);
1938 LOG_INFO("deviceID: %lu", deviceID);
1939 LOG_INFO("SerialNumber: %s", SerialNumber);
1940 LOG_INFO("Description: %s", Description);
1942 #ifdef BUILD_FTD2XX_HIGHSPEED
1943 if (ft2232_device_is_highspeed())
1945 ft2232_max_tck = FTDI_2232H_4232H_MAX_TCK;
1946 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
1948 #endif
1951 return ERROR_OK;
1955 static int ft2232_purge_ftd2xx(void)
1957 FT_STATUS status;
1959 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1961 LOG_ERROR("error purging ftd2xx device: %lu", status);
1962 return ERROR_JTAG_INIT_FAILED;
1965 return ERROR_OK;
1969 #endif /* BUILD_FT2232_FTD2XX == 1 */
1971 #if BUILD_FT2232_LIBFTDI == 1
1972 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
1974 uint8_t latency_timer;
1976 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1977 ft2232_layout, vid, pid);
1979 if (ftdi_init(&ftdic) < 0)
1980 return ERROR_JTAG_INIT_FAILED;
1982 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1984 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1985 return ERROR_JTAG_INIT_FAILED;
1988 /* context, vendor id, product id */
1989 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1990 ft2232_serial) < 0)
1992 if (more)
1993 LOG_WARNING("unable to open ftdi device (trying more): %s",
1994 ftdic.error_str);
1995 else
1996 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1997 *try_more = 1;
1998 return ERROR_JTAG_INIT_FAILED;
2001 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2002 if (ftdi_usb_reset(&ftdic) < 0)
2004 LOG_ERROR("unable to reset ftdi device");
2005 return ERROR_JTAG_INIT_FAILED;
2008 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2010 LOG_ERROR("unable to set latency timer");
2011 return ERROR_JTAG_INIT_FAILED;
2014 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2016 LOG_ERROR("unable to get latency timer");
2017 return ERROR_JTAG_INIT_FAILED;
2019 else
2021 LOG_DEBUG("current latency timer: %i", latency_timer);
2024 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2026 return ERROR_OK;
2030 static int ft2232_purge_libftdi(void)
2032 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2034 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2035 return ERROR_JTAG_INIT_FAILED;
2038 return ERROR_OK;
2042 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2044 static int ft2232_init(void)
2046 uint8_t buf[1];
2047 int retval;
2048 uint32_t bytes_written;
2049 const ft2232_layout_t* cur_layout = ft2232_layouts;
2050 int i;
2052 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2054 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2056 else
2058 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2061 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2063 ft2232_layout = "usbjtag";
2064 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2067 while (cur_layout->name)
2069 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2071 layout = cur_layout;
2072 break;
2074 cur_layout++;
2077 if (!layout)
2079 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2080 return ERROR_JTAG_INIT_FAILED;
2083 for (i = 0; 1; i++)
2086 * "more indicates that there are more IDs to try, so we should
2087 * not print an error for an ID mismatch (but for anything
2088 * else, we should).
2090 * try_more indicates that the error code returned indicates an
2091 * ID mismatch (and nothing else) and that we should proceeed
2092 * with the next ID pair.
2094 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2095 int try_more = 0;
2097 #if BUILD_FT2232_FTD2XX == 1
2098 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2099 more, &try_more);
2100 #elif BUILD_FT2232_LIBFTDI == 1
2101 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2102 more, &try_more);
2103 #endif
2104 if (retval >= 0)
2105 break;
2106 if (!more || !try_more)
2107 return retval;
2110 ft2232_buffer_size = 0;
2111 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2113 if (layout->init() != ERROR_OK)
2114 return ERROR_JTAG_INIT_FAILED;
2116 ft2232_speed(jtag_get_speed());
2118 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2119 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2121 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2122 return ERROR_JTAG_INIT_FAILED;
2125 #if BUILD_FT2232_FTD2XX == 1
2126 return ft2232_purge_ftd2xx();
2127 #elif BUILD_FT2232_LIBFTDI == 1
2128 return ft2232_purge_libftdi();
2129 #endif
2131 return ERROR_OK;
2135 static int usbjtag_init(void)
2137 uint8_t buf[3];
2138 uint32_t bytes_written;
2140 low_output = 0x08;
2141 low_direction = 0x0b;
2143 if (strcmp(ft2232_layout, "usbjtag") == 0)
2145 nTRST = 0x10;
2146 nTRSTnOE = 0x10;
2147 nSRST = 0x40;
2148 nSRSTnOE = 0x40;
2150 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2152 nTRST = 0x10;
2153 nTRSTnOE = 0x10;
2154 nSRST = 0x20;
2155 nSRSTnOE = 0x20;
2157 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2159 nTRST = 0x0;
2160 nTRSTnOE = 0x00;
2161 nSRST = 0x20;
2162 nSRSTnOE = 0x20;
2163 low_output = 0x88;
2164 low_direction = 0x8b;
2166 else
2168 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2169 return ERROR_JTAG_INIT_FAILED;
2172 enum reset_types jtag_reset_config = jtag_get_reset_config();
2173 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2175 low_direction &= ~nTRSTnOE; /* nTRST input */
2176 low_output &= ~nTRST; /* nTRST = 0 */
2178 else
2180 low_direction |= nTRSTnOE; /* nTRST output */
2181 low_output |= nTRST; /* nTRST = 1 */
2184 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2186 low_direction |= nSRSTnOE; /* nSRST output */
2187 low_output |= nSRST; /* nSRST = 1 */
2189 else
2191 low_direction &= ~nSRSTnOE; /* nSRST input */
2192 low_output &= ~nSRST; /* nSRST = 0 */
2195 /* initialize low byte for jtag */
2196 buf[0] = 0x80; /* command "set data bits low byte" */
2197 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2198 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2199 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2201 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2203 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2204 return ERROR_JTAG_INIT_FAILED;
2207 return ERROR_OK;
2211 static int axm0432_jtag_init(void)
2213 uint8_t buf[3];
2214 uint32_t bytes_written;
2216 low_output = 0x08;
2217 low_direction = 0x2b;
2219 /* initialize low byte for jtag */
2220 buf[0] = 0x80; /* command "set data bits low byte" */
2221 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2222 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2223 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2225 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2227 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2228 return ERROR_JTAG_INIT_FAILED;
2231 if (strcmp(layout->name, "axm0432_jtag") == 0)
2233 nTRST = 0x08;
2234 nTRSTnOE = 0x0; /* No output enable for TRST*/
2235 nSRST = 0x04;
2236 nSRSTnOE = 0x0; /* No output enable for SRST*/
2238 else
2240 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2241 exit(-1);
2244 high_output = 0x0;
2245 high_direction = 0x0c;
2247 enum reset_types jtag_reset_config = jtag_get_reset_config();
2248 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2250 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2252 else
2254 high_output |= nTRST;
2257 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2259 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2261 else
2263 high_output |= nSRST;
2266 /* initialize high port */
2267 buf[0] = 0x82; /* command "set data bits high byte" */
2268 buf[1] = high_output; /* value */
2269 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2270 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2272 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2274 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2275 return ERROR_JTAG_INIT_FAILED;
2278 return ERROR_OK;
2282 static int jtagkey_init(void)
2284 uint8_t buf[3];
2285 uint32_t bytes_written;
2287 low_output = 0x08;
2288 low_direction = 0x1b;
2290 /* initialize low byte for jtag */
2291 buf[0] = 0x80; /* command "set data bits low byte" */
2292 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2293 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2294 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2296 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2298 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2299 return ERROR_JTAG_INIT_FAILED;
2302 if (strcmp(layout->name, "jtagkey") == 0)
2304 nTRST = 0x01;
2305 nTRSTnOE = 0x4;
2306 nSRST = 0x02;
2307 nSRSTnOE = 0x08;
2309 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2310 || (strcmp(layout->name, "oocdlink") == 0))
2312 nTRST = 0x02;
2313 nTRSTnOE = 0x1;
2314 nSRST = 0x08;
2315 nSRSTnOE = 0x04;
2317 else
2319 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2320 exit(-1);
2323 high_output = 0x0;
2324 high_direction = 0x0f;
2326 enum reset_types jtag_reset_config = jtag_get_reset_config();
2327 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2329 high_output |= nTRSTnOE;
2330 high_output &= ~nTRST;
2332 else
2334 high_output &= ~nTRSTnOE;
2335 high_output |= nTRST;
2338 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2340 high_output &= ~nSRSTnOE;
2341 high_output |= nSRST;
2343 else
2345 high_output |= nSRSTnOE;
2346 high_output &= ~nSRST;
2349 /* initialize high port */
2350 buf[0] = 0x82; /* command "set data bits high byte" */
2351 buf[1] = high_output; /* value */
2352 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2353 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2355 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2357 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2358 return ERROR_JTAG_INIT_FAILED;
2361 return ERROR_OK;
2365 static int olimex_jtag_init(void)
2367 uint8_t buf[3];
2368 uint32_t bytes_written;
2370 low_output = 0x08;
2371 low_direction = 0x1b;
2373 /* initialize low byte for jtag */
2374 buf[0] = 0x80; /* command "set data bits low byte" */
2375 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2376 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2377 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2379 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2381 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2382 return ERROR_JTAG_INIT_FAILED;
2385 nTRST = 0x01;
2386 nTRSTnOE = 0x4;
2387 nSRST = 0x02;
2388 nSRSTnOE = 0x00; /* no output enable for nSRST */
2390 high_output = 0x0;
2391 high_direction = 0x0f;
2393 enum reset_types jtag_reset_config = jtag_get_reset_config();
2394 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2396 high_output |= nTRSTnOE;
2397 high_output &= ~nTRST;
2399 else
2401 high_output &= ~nTRSTnOE;
2402 high_output |= nTRST;
2405 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2407 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2409 else
2411 high_output &= ~nSRST;
2414 /* turn red LED on */
2415 high_output |= 0x08;
2417 /* initialize high port */
2418 buf[0] = 0x82; /* command "set data bits high byte" */
2419 buf[1] = high_output; /* value */
2420 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2421 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2423 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2425 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2426 return ERROR_JTAG_INIT_FAILED;
2429 return ERROR_OK;
2433 static int flyswatter_init(void)
2435 uint8_t buf[3];
2436 uint32_t bytes_written;
2438 low_output = 0x18;
2439 low_direction = 0xfb;
2441 /* initialize low byte for jtag */
2442 buf[0] = 0x80; /* command "set data bits low byte" */
2443 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2444 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2445 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2447 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2449 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2450 return ERROR_JTAG_INIT_FAILED;
2453 nTRST = 0x10;
2454 nTRSTnOE = 0x0; /* not output enable for nTRST */
2455 nSRST = 0x20;
2456 nSRSTnOE = 0x00; /* no output enable for nSRST */
2458 high_output = 0x00;
2459 high_direction = 0x0c;
2461 /* turn red LED3 on, LED2 off */
2462 high_output |= 0x08;
2464 /* initialize high port */
2465 buf[0] = 0x82; /* command "set data bits high byte" */
2466 buf[1] = high_output; /* value */
2467 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2468 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2470 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2472 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2473 return ERROR_JTAG_INIT_FAILED;
2476 return ERROR_OK;
2480 static int turtle_init(void)
2482 uint8_t buf[3];
2483 uint32_t bytes_written;
2485 low_output = 0x08;
2486 low_direction = 0x5b;
2488 /* initialize low byte for jtag */
2489 buf[0] = 0x80; /* command "set data bits low byte" */
2490 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2491 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2492 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2494 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2496 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2497 return ERROR_JTAG_INIT_FAILED;
2500 nSRST = 0x40;
2502 high_output = 0x00;
2503 high_direction = 0x0C;
2505 /* initialize high port */
2506 buf[0] = 0x82; /* command "set data bits high byte" */
2507 buf[1] = high_output;
2508 buf[2] = high_direction;
2509 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2511 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2513 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2514 return ERROR_JTAG_INIT_FAILED;
2517 return ERROR_OK;
2521 static int comstick_init(void)
2523 uint8_t buf[3];
2524 uint32_t bytes_written;
2526 low_output = 0x08;
2527 low_direction = 0x0b;
2529 /* initialize low byte for jtag */
2530 buf[0] = 0x80; /* command "set data bits low byte" */
2531 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2532 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2533 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2535 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2537 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2538 return ERROR_JTAG_INIT_FAILED;
2541 nTRST = 0x01;
2542 nTRSTnOE = 0x00; /* no output enable for nTRST */
2543 nSRST = 0x02;
2544 nSRSTnOE = 0x00; /* no output enable for nSRST */
2546 high_output = 0x03;
2547 high_direction = 0x03;
2549 /* initialize high port */
2550 buf[0] = 0x82; /* command "set data bits high byte" */
2551 buf[1] = high_output;
2552 buf[2] = high_direction;
2553 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2555 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2557 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2558 return ERROR_JTAG_INIT_FAILED;
2561 return ERROR_OK;
2565 static int stm32stick_init(void)
2567 uint8_t buf[3];
2568 uint32_t bytes_written;
2570 low_output = 0x88;
2571 low_direction = 0x8b;
2573 /* initialize low byte for jtag */
2574 buf[0] = 0x80; /* command "set data bits low byte" */
2575 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2576 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2577 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2579 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2581 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2582 return ERROR_JTAG_INIT_FAILED;
2585 nTRST = 0x01;
2586 nTRSTnOE = 0x00; /* no output enable for nTRST */
2587 nSRST = 0x80;
2588 nSRSTnOE = 0x00; /* no output enable for nSRST */
2590 high_output = 0x01;
2591 high_direction = 0x03;
2593 /* initialize high port */
2594 buf[0] = 0x82; /* command "set data bits high byte" */
2595 buf[1] = high_output;
2596 buf[2] = high_direction;
2597 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2599 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2601 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2602 return ERROR_JTAG_INIT_FAILED;
2605 return ERROR_OK;
2609 static int sheevaplug_init(void)
2611 uint8_t buf[3];
2612 uint32_t bytes_written;
2614 low_output = 0x08;
2615 low_direction = 0x1b;
2617 /* initialize low byte for jtag */
2618 buf[0] = 0x80; /* command "set data bits low byte" */
2619 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2620 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2621 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2623 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2625 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2626 return ERROR_JTAG_INIT_FAILED;
2629 nTRSTnOE = 0x1;
2630 nTRST = 0x02;
2631 nSRSTnOE = 0x4;
2632 nSRST = 0x08;
2634 high_output = 0x0;
2635 high_direction = 0x0f;
2637 /* nTRST is always push-pull */
2638 high_output &= ~nTRSTnOE;
2639 high_output |= nTRST;
2641 /* nSRST is always open-drain */
2642 high_output |= nSRSTnOE;
2643 high_output &= ~nSRST;
2645 /* initialize high port */
2646 buf[0] = 0x82; /* command "set data bits high byte" */
2647 buf[1] = high_output; /* value */
2648 buf[2] = high_direction; /* all outputs - xRST */
2649 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2651 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2653 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2654 return ERROR_JTAG_INIT_FAILED;
2657 return ERROR_OK;
2660 static int cortino_jtag_init(void)
2662 uint8_t buf[3];
2663 uint32_t bytes_written;
2665 low_output = 0x08;
2666 low_direction = 0x1b;
2668 /* initialize low byte for jtag */
2669 buf[0] = 0x80; /* command "set data bits low byte" */
2670 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2671 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2672 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2674 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2676 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2677 return ERROR_JTAG_INIT_FAILED;
2680 nTRST = 0x01;
2681 nTRSTnOE = 0x00; /* no output enable for nTRST */
2682 nSRST = 0x02;
2683 nSRSTnOE = 0x00; /* no output enable for nSRST */
2685 high_output = 0x03;
2686 high_direction = 0x03;
2688 /* initialize high port */
2689 buf[0] = 0x82; /* command "set data bits high byte" */
2690 buf[1] = high_output;
2691 buf[2] = high_direction;
2692 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2694 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2696 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2697 return ERROR_JTAG_INIT_FAILED;
2700 return ERROR_OK;
2703 static void olimex_jtag_blink(void)
2705 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2706 * ACBUS3 is bit 3 of the GPIOH port
2708 if (high_output & 0x08)
2710 /* set port pin high */
2711 high_output &= 0x07;
2713 else
2715 /* set port pin low */
2716 high_output |= 0x08;
2719 buffer_write(0x82);
2720 buffer_write(high_output);
2721 buffer_write(high_direction);
2725 static void flyswatter_jtag_blink(void)
2728 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2730 high_output ^= 0x0c;
2732 buffer_write(0x82);
2733 buffer_write(high_output);
2734 buffer_write(high_direction);
2738 static void turtle_jtag_blink(void)
2741 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2743 if (high_output & 0x08)
2745 high_output = 0x04;
2747 else
2749 high_output = 0x08;
2752 buffer_write(0x82);
2753 buffer_write(high_output);
2754 buffer_write(high_direction);
2758 static int ft2232_quit(void)
2760 #if BUILD_FT2232_FTD2XX == 1
2761 FT_STATUS status;
2763 status = FT_Close(ftdih);
2764 #elif BUILD_FT2232_LIBFTDI == 1
2765 ftdi_usb_close(&ftdic);
2767 ftdi_deinit(&ftdic);
2768 #endif
2770 free(ft2232_buffer);
2771 ft2232_buffer = NULL;
2773 return ERROR_OK;
2777 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2779 char *cp;
2780 char buf[200];
2781 if (argc == 1)
2783 ft2232_device_desc = strdup(args[0]);
2784 cp = strchr(ft2232_device_desc, 0);
2785 // under Win32, the FTD2XX driver appends an "A" to the end
2786 // of the description, this examines the given desc
2787 // and creates the 'missing' _A or non_A variable.
2788 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2789 // it was, so make this the "A" version.
2790 ft2232_device_desc_A = ft2232_device_desc;
2791 // and *CREATE* the non-A version.
2792 strcpy(buf, ft2232_device_desc);
2793 cp = strchr(buf, 0);
2794 cp[-2] = 0;
2795 ft2232_device_desc = strdup(buf);
2796 } else {
2797 // <space>A not defined
2798 // so create it
2799 sprintf(buf, "%s A", ft2232_device_desc);
2800 ft2232_device_desc_A = strdup(buf);
2803 else
2805 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2808 return ERROR_OK;
2812 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2814 if (argc == 1)
2816 ft2232_serial = strdup(args[0]);
2818 else
2820 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2823 return ERROR_OK;
2827 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2829 if (argc == 0)
2830 return ERROR_OK;
2832 ft2232_layout = malloc(strlen(args[0]) + 1);
2833 strcpy(ft2232_layout, args[0]);
2835 return ERROR_OK;
2839 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2841 if (argc > MAX_USB_IDS * 2)
2843 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2844 "(maximum is %d pairs)", MAX_USB_IDS);
2845 argc = MAX_USB_IDS * 2;
2847 if (argc < 2 || (argc & 1))
2849 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2850 if (argc < 2)
2851 return ERROR_COMMAND_SYNTAX_ERROR;
2852 // remove the incomplete trailing id
2853 argc -= 1;
2856 int i;
2857 int retval = ERROR_OK;
2858 for (i = 0; i < argc; i += 2)
2860 retval = parse_u16(args[i], &ft2232_vid[i >> 1]);
2861 if (ERROR_OK != retval)
2862 break;
2863 retval = parse_u16(args[i + 1], &ft2232_pid[i >> 1]);
2864 if (ERROR_OK != retval)
2865 break;
2869 * Explicitly terminate, in case there are multiples instances of
2870 * ft2232_vid_pid.
2872 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2874 return retval;
2878 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2880 if (argc == 1)
2882 ft2232_latency = atoi(args[0]);
2884 else
2886 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2889 return ERROR_OK;
2893 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2895 int retval = 0;
2897 /* 7 bits of either ones or zeros. */
2898 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2900 while (num_cycles > 0)
2902 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2903 * at most 7 bits per invocation. Here we invoke it potentially
2904 * several times.
2906 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2908 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2910 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2911 retval = ERROR_JTAG_QUEUE_FAILED;
2913 first_unsent = cmd;
2916 /* there are no state transitions in this code, so omit state tracking */
2918 /* command "Clock Data to TMS/CS Pin (no Read)" */
2919 buffer_write(0x4b);
2921 /* scan 7 bit */
2922 buffer_write(bitcount_per_command - 1);
2924 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2925 buffer_write(tms);
2927 require_send = 1;
2929 num_cycles -= bitcount_per_command;
2932 return retval;
2936 /* ---------------------------------------------------------------------
2937 * Support for IceBear JTAG adapter from Section5:
2938 * http://section5.ch/icebear
2940 * Author: Sten, debian@sansys-electronic.com
2943 /* Icebear pin layout
2945 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2946 * GND GND | 4 3| n.c.
2947 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2948 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2949 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2950 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2951 * ADBUS2 TDO |14 13| GND GND
2953 * ADBUS0 O L TCK ACBUS0 GND
2954 * ADBUS1 O L TDI ACBUS1 GND
2955 * ADBUS2 I TDO ACBUS2 n.c.
2956 * ADBUS3 O H TMS ACBUS3 n.c.
2957 * ADBUS4 O H nTRST
2958 * ADBUS5 O H nSRST
2959 * ADBUS6 - VCC
2960 * ADBUS7 - GND
2962 static int icebear_jtag_init(void) {
2963 uint8_t buf[3];
2964 uint32_t bytes_written;
2966 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2967 low_output = 0x08; /* high: TMS; low: TCK TDI */
2968 nTRST = 0x10;
2969 nSRST = 0x20;
2971 enum reset_types jtag_reset_config = jtag_get_reset_config();
2972 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2973 low_direction &= ~nTRST; /* nTRST high impedance */
2975 else {
2976 low_direction |= nTRST;
2977 low_output |= nTRST;
2980 low_direction |= nSRST;
2981 low_output |= nSRST;
2983 /* initialize low byte for jtag */
2984 buf[0] = 0x80; /* command "set data bits low byte" */
2985 buf[1] = low_output;
2986 buf[2] = low_direction;
2987 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2989 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
2990 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2991 return ERROR_JTAG_INIT_FAILED;
2994 high_output = 0x0;
2995 high_direction = 0x00;
2998 /* initialize high port */
2999 buf[0] = 0x82; /* command "set data bits high byte" */
3000 buf[1] = high_output; /* value */
3001 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3002 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3004 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3005 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3006 return ERROR_JTAG_INIT_FAILED;
3009 return ERROR_OK;
3012 static void icebear_jtag_reset(int trst, int srst) {
3014 if (trst == 1) {
3015 low_direction |= nTRST;
3016 low_output &= ~nTRST;
3018 else if (trst == 0) {
3019 enum reset_types jtag_reset_config = jtag_get_reset_config();
3020 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3021 low_direction &= ~nTRST;
3022 else
3023 low_output |= nTRST;
3026 if (srst == 1) {
3027 low_output &= ~nSRST;
3029 else if (srst == 0) {
3030 low_output |= nSRST;
3033 /* command "set data bits low byte" */
3034 buffer_write(0x80);
3035 buffer_write(low_output);
3036 buffer_write(low_direction);
3038 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);