1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods. Since JTAG uses
36 * only one of the two ports on these devices, on integrated boards the
37 * second port often serves as a USB-to-serial adapter for the target's
38 * console UART even when the JTAG port is not in use. (Systems which
39 * support ARM's SWD in addition to JTAG, or instead of it, may use that
40 * second port for reading SWV trace data.)
42 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
43 * request/response interactions involve round trips over the USB link.
44 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
45 * can for example poll quickly for a status change (usually taking on the
46 * order of microseconds not milliseconds) before beginning a queued
47 * transaction which require the previous one to have completed.
49 * There are dozens of adapters of this type, differing in details which
50 * this driver needs to understand. Those "layout" details are required
51 * as part of FT2232 driver configuration.
53 * This code uses information contained in the MPSSE specification which was
55 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
56 * Hereafter this is called the "MPSSE Spec".
58 * The datasheet for the ftdichip.com's FT2232D part is here:
59 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
61 * Also note the issue with code 0x4b (clock data to TMS) noted in
62 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
63 * which can affect longer JTAG state paths.
70 /* project specific includes */
71 #include <jtag/interface.h>
72 #include <helper/time_support.h>
80 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
81 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
82 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
83 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
86 /* FT2232 access library includes */
87 #if BUILD_FT2232_FTD2XX == 1
99 #elif BUILD_FT2232_LIBFTDI == 1
103 /* max TCK for the high speed devices 30000 kHz */
104 #define FTDI_2232H_4232H_MAX_TCK 30000
105 /* max TCK for the full speed devices 6000 kHz */
106 #define FTDI_2232C_MAX_TCK 6000
107 /* this speed value tells that RTCK is requested */
108 #define RTCK_SPEED -1
111 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
112 * errors with a retry count of 100. Increasing it solves the problem for me.
115 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
116 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
119 #define LIBFTDI_READ_RETRY_COUNT 2000
121 #ifndef BUILD_FT2232_HIGHSPEED
122 #if BUILD_FT2232_FTD2XX == 1
123 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
124 #elif BUILD_FT2232_LIBFTDI == 1
125 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
130 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
131 * stable state. Calling code must ensure that current state is stable,
132 * that verification is not done in here.
134 * @param num_cycles The number of clocks cycles to send.
135 * @param cmd The command to send.
137 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
139 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
141 static char * ft2232_device_desc_A
= NULL
;
142 static char* ft2232_device_desc
= NULL
;
143 static char* ft2232_serial
= NULL
;
144 static uint8_t ft2232_latency
= 2;
145 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
147 #define MAX_USB_IDS 8
148 /* vid = pid = 0 marks the end of the list */
149 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
150 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
152 struct ft2232_layout
{
155 void (*reset
)(int trst
, int srst
);
160 /* init procedures for supported layouts */
161 static int usbjtag_init(void);
162 static int jtagkey_init(void);
163 static int olimex_jtag_init(void);
164 static int flyswatter_init(void);
165 static int turtle_init(void);
166 static int comstick_init(void);
167 static int stm32stick_init(void);
168 static int axm0432_jtag_init(void);
169 static int sheevaplug_init(void);
170 static int icebear_jtag_init(void);
171 static int cortino_jtag_init(void);
172 static int signalyzer_h_init(void);
173 static int ktlink_init(void);
174 static int redbee_init(void);
176 /* reset procedures for supported layouts */
177 static void usbjtag_reset(int trst
, int srst
);
178 static void jtagkey_reset(int trst
, int srst
);
179 static void olimex_jtag_reset(int trst
, int srst
);
180 static void flyswatter_reset(int trst
, int srst
);
181 static void turtle_reset(int trst
, int srst
);
182 static void comstick_reset(int trst
, int srst
);
183 static void stm32stick_reset(int trst
, int srst
);
184 static void axm0432_jtag_reset(int trst
, int srst
);
185 static void sheevaplug_reset(int trst
, int srst
);
186 static void icebear_jtag_reset(int trst
, int srst
);
187 static void signalyzer_h_reset(int trst
, int srst
);
188 static void ktlink_reset(int trst
, int srst
);
189 static void redbee_reset(int trst
, int srst
);
191 /* blink procedures for layouts that support a blinking led */
192 static void olimex_jtag_blink(void);
193 static void flyswatter_jtag_blink(void);
194 static void turtle_jtag_blink(void);
195 static void signalyzer_h_blink(void);
196 static void ktlink_blink(void);
198 static const struct ft2232_layout ft2232_layouts
[] =
201 .init
= usbjtag_init
,
202 .reset
= usbjtag_reset
,
205 .init
= jtagkey_init
,
206 .reset
= jtagkey_reset
,
208 { .name
= "jtagkey_prototype_v1",
209 .init
= jtagkey_init
,
210 .reset
= jtagkey_reset
,
212 { .name
= "oocdlink",
213 .init
= jtagkey_init
,
214 .reset
= jtagkey_reset
,
216 { .name
= "signalyzer",
217 .init
= usbjtag_init
,
218 .reset
= usbjtag_reset
,
220 { .name
= "evb_lm3s811",
221 .init
= usbjtag_init
,
222 .reset
= usbjtag_reset
,
224 { .name
= "luminary_icdi",
225 .init
= usbjtag_init
,
226 .reset
= usbjtag_reset
,
228 { .name
= "olimex-jtag",
229 .init
= olimex_jtag_init
,
230 .reset
= olimex_jtag_reset
,
231 .blink
= olimex_jtag_blink
233 { .name
= "flyswatter",
234 .init
= flyswatter_init
,
235 .reset
= flyswatter_reset
,
236 .blink
= flyswatter_jtag_blink
238 { .name
= "turtelizer2",
240 .reset
= turtle_reset
,
241 .blink
= turtle_jtag_blink
243 { .name
= "comstick",
244 .init
= comstick_init
,
245 .reset
= comstick_reset
,
247 { .name
= "stm32stick",
248 .init
= stm32stick_init
,
249 .reset
= stm32stick_reset
,
251 { .name
= "axm0432_jtag",
252 .init
= axm0432_jtag_init
,
253 .reset
= axm0432_jtag_reset
,
255 { .name
= "sheevaplug",
256 .init
= sheevaplug_init
,
257 .reset
= sheevaplug_reset
,
260 .init
= icebear_jtag_init
,
261 .reset
= icebear_jtag_reset
,
264 .init
= cortino_jtag_init
,
265 .reset
= comstick_reset
,
267 { .name
= "signalyzer-h",
268 .init
= signalyzer_h_init
,
269 .reset
= signalyzer_h_reset
,
270 .blink
= signalyzer_h_blink
274 .reset
= ktlink_reset
,
275 .blink
= ktlink_blink
277 { .name
= "redbee-econotag",
279 .reset
= redbee_reset
,
281 { .name
= "redbee-usb",
283 .reset
= redbee_reset
,
284 .channel
= INTERFACE_B
,
286 { .name
= NULL
, /* END OF TABLE */ },
289 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
291 /** the layout being used with this debug session */
292 static const struct ft2232_layout
*layout
;
294 static uint8_t low_output
= 0x0;
295 static uint8_t low_direction
= 0x0;
296 static uint8_t high_output
= 0x0;
297 static uint8_t high_direction
= 0x0;
299 #if BUILD_FT2232_FTD2XX == 1
300 static FT_HANDLE ftdih
= NULL
;
301 static FT_DEVICE ftdi_device
= 0;
302 #elif BUILD_FT2232_LIBFTDI == 1
303 static struct ftdi_context ftdic
;
304 static enum ftdi_chip_type ftdi_device
;
307 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
308 static int require_send
;
310 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
312 "There is a significant difference between libftdi and libftd2xx. The latter
313 one allows to schedule up to 64*64 bytes of result data while libftdi fails
314 with more than 4*64. As a consequence, the FT2232 driver is forced to
315 perform around 16x more USB transactions for long command streams with TDO
316 capture when running with libftdi."
319 #define FT2232_BUFFER_SIZE 131072
320 a comment would have been nice.
323 #define FT2232_BUFFER_SIZE 131072
325 static uint8_t* ft2232_buffer
= NULL
;
326 static int ft2232_buffer_size
= 0;
327 static int ft2232_read_pointer
= 0;
328 static int ft2232_expect_read
= 0;
331 * Function buffer_write
332 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
333 * @param val is the byte to send.
335 static inline void buffer_write(uint8_t val
)
337 assert(ft2232_buffer
);
338 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
339 ft2232_buffer
[ft2232_buffer_size
++] = val
;
343 * Function buffer_read
344 * returns a byte from the byte buffer.
346 static inline uint8_t buffer_read(void)
348 assert(ft2232_buffer
);
349 assert(ft2232_read_pointer
< ft2232_buffer_size
);
350 return ft2232_buffer
[ft2232_read_pointer
++];
354 * Clocks out \a bit_count bits on the TMS line, starting with the least
355 * significant bit of tms_bits and progressing to more significant bits.
356 * Rigorous state transition logging is done here via tap_set_state().
358 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
359 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
360 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
361 * is often used for this, 0x4b.
363 * @param tms_bits Holds the sequence of bits to send.
364 * @param tms_count Tells how many bits in the sequence.
365 * @param tdi_bit A single bit to pass on to TDI before the first TCK
366 * cycle and held static for the duration of TMS clocking.
368 * See the MPSSE spec referenced above.
370 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
374 int tms_ndx
; /* bit index into tms_byte */
376 assert(tms_count
> 0);
378 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
379 mpsse_cmd
, tms_bits
, tms_count
);
381 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
383 bool bit
= tms_bits
& 1;
386 tms_byte
|= (1 << tms_ndx
);
388 /* always do state transitions in public view */
389 tap_set_state(tap_state_transition(tap_get_state(), bit
));
391 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
396 if (tms_ndx
== 7 || i
== tms_count
-1)
398 buffer_write(mpsse_cmd
);
399 buffer_write(tms_ndx
- 1);
401 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
402 TMS/CS and is held static for the duration of TMS/CS clocking.
404 buffer_write(tms_byte
| (tdi_bit
<< 7));
410 * Function get_tms_buffer_requirements
411 * returns what clock_tms() will consume if called with
414 static inline int get_tms_buffer_requirements(int bit_count
)
416 return ((bit_count
+ 6)/7) * 3;
420 * Function move_to_state
421 * moves the TAP controller from the current state to a
422 * \a goal_state through a path given by tap_get_tms_path(). State transition
423 * logging is performed by delegation to clock_tms().
425 * @param goal_state is the destination state for the move.
427 static void move_to_state(tap_state_t goal_state
)
429 tap_state_t start_state
= tap_get_state();
431 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
432 lookup of the required TMS pattern to move to this state from the
436 /* do the 2 lookups */
437 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
438 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
440 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
442 clock_tms(0x4b, tms_bits
, tms_count
, 0);
445 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
447 #if BUILD_FT2232_FTD2XX == 1
449 DWORD dw_bytes_written
;
450 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
452 *bytes_written
= dw_bytes_written
;
453 LOG_ERROR("FT_Write returned: %lu", status
);
454 return ERROR_JTAG_DEVICE_ERROR
;
458 *bytes_written
= dw_bytes_written
;
461 #elif BUILD_FT2232_LIBFTDI == 1
463 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
466 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
467 return ERROR_JTAG_DEVICE_ERROR
;
471 *bytes_written
= retval
;
477 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
479 #if BUILD_FT2232_FTD2XX == 1
485 while ((*bytes_read
< size
) && timeout
--)
487 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
488 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
491 LOG_ERROR("FT_Read returned: %lu", status
);
492 return ERROR_JTAG_DEVICE_ERROR
;
494 *bytes_read
+= dw_bytes_read
;
497 #elif BUILD_FT2232_LIBFTDI == 1
499 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
502 while ((*bytes_read
< size
) && timeout
--)
504 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
507 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
508 return ERROR_JTAG_DEVICE_ERROR
;
510 *bytes_read
+= retval
;
515 if (*bytes_read
< size
)
517 LOG_ERROR("couldn't read enough bytes from "
518 "FT2232 device (%i < %i)",
519 (unsigned)*bytes_read
,
521 return ERROR_JTAG_DEVICE_ERROR
;
527 static bool ft2232_device_is_highspeed(void)
529 #if BUILD_FT2232_FTD2XX == 1
530 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
531 #elif BUILD_FT2232_LIBFTDI == 1
532 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
537 * Commands that only apply to the FT2232H and FT4232H devices.
538 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
539 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
542 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
544 uint8_t buf
= enable
? 0x96 : 0x97;
545 LOG_DEBUG("%2.2x", buf
);
547 uint32_t bytes_written
;
548 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
549 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
551 LOG_ERROR("couldn't write command to %s adaptive clocking"
552 , enable
? "enable" : "disable");
560 * Enable/disable the clk divide by 5 of the 60MHz master clock.
561 * This result in a JTAG clock speed range of 91.553Hz-6MHz
562 * respective 457.763Hz-30MHz.
564 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
566 uint32_t bytes_written
;
567 uint8_t buf
= enable
? 0x8b : 0x8a;
568 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
569 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
571 LOG_ERROR("couldn't write command to %s clk divide by 5"
572 , enable
? "enable" : "disable");
573 return ERROR_JTAG_INIT_FAILED
;
575 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
576 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
581 static int ft2232_speed(int speed
)
585 uint32_t bytes_written
;
588 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
589 if (ft2232_device_is_highspeed())
590 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
591 else if (enable_adaptive_clocking
)
593 LOG_ERROR("ft2232 device %lu does not support RTCK"
594 , (long unsigned int)ftdi_device
);
598 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
601 buf
[0] = 0x86; /* command "set divisor" */
602 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
603 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
605 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
606 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
608 LOG_ERROR("couldn't set FT2232 TCK speed");
615 static int ft2232_speed_div(int speed
, int* khz
)
617 /* Take a look in the FT2232 manual,
618 * AN2232C-01 Command Processor for
619 * MPSSE and MCU Host Bus. Chapter 3.8 */
621 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
626 static int ft2232_khz(int khz
, int* jtag_speed
)
630 if (ft2232_device_is_highspeed())
632 *jtag_speed
= RTCK_SPEED
;
637 LOG_DEBUG("RCLK not supported");
642 /* Take a look in the FT2232 manual,
643 * AN2232C-01 Command Processor for
644 * MPSSE and MCU Host Bus. Chapter 3.8
646 * We will calc here with a multiplier
647 * of 10 for better rounding later. */
649 /* Calc speed, (ft2232_max_tck / khz) - 1 */
650 /* Use 65000 for better rounding */
651 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
653 /* Add 0.9 for rounding */
656 /* Calc real speed */
657 *jtag_speed
= *jtag_speed
/ 10;
659 /* Check if speed is greater than 0 */
665 /* Check max value */
666 if (*jtag_speed
> 0xFFFF)
668 *jtag_speed
= 0xFFFF;
674 static void ft2232_end_state(tap_state_t state
)
676 if (tap_is_state_stable(state
))
677 tap_set_end_state(state
);
680 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
685 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
687 int num_bytes
= (scan_size
+ 7) / 8;
688 int bits_left
= scan_size
;
691 while (num_bytes
-- > 1)
693 buffer
[cur_byte
++] = buffer_read();
697 buffer
[cur_byte
] = 0x0;
699 /* There is one more partial byte left from the clock data in/out instructions */
702 buffer
[cur_byte
] = buffer_read() >> 1;
704 /* 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 */
705 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
708 static void ft2232_debug_dump_buffer(void)
714 for (i
= 0; i
< ft2232_buffer_size
; i
++)
716 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
719 LOG_DEBUG("%s", line
);
725 LOG_DEBUG("%s", line
);
728 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
730 struct jtag_command
* cmd
;
735 uint32_t bytes_written
= 0;
736 uint32_t bytes_read
= 0;
738 #ifdef _DEBUG_USB_IO_
739 struct timeval start
, inter
, inter2
, end
;
740 struct timeval d_inter
, d_inter2
, d_end
;
743 #ifdef _DEBUG_USB_COMMS_
744 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
745 ft2232_debug_dump_buffer();
748 #ifdef _DEBUG_USB_IO_
749 gettimeofday(&start
, NULL
);
752 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
754 LOG_ERROR("couldn't write MPSSE commands to FT2232");
758 #ifdef _DEBUG_USB_IO_
759 gettimeofday(&inter
, NULL
);
762 if (ft2232_expect_read
)
764 /* FIXME this "timeout" is never changed ... */
765 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
766 ft2232_buffer_size
= 0;
768 #ifdef _DEBUG_USB_IO_
769 gettimeofday(&inter2
, NULL
);
772 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
774 LOG_ERROR("couldn't read from FT2232");
778 #ifdef _DEBUG_USB_IO_
779 gettimeofday(&end
, NULL
);
781 timeval_subtract(&d_inter
, &inter
, &start
);
782 timeval_subtract(&d_inter2
, &inter2
, &start
);
783 timeval_subtract(&d_end
, &end
, &start
);
785 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
786 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
787 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
788 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
791 ft2232_buffer_size
= bytes_read
;
793 if (ft2232_expect_read
!= ft2232_buffer_size
)
795 LOG_ERROR("ft2232_expect_read (%i) != "
796 "ft2232_buffer_size (%i) "
800 LIBFTDI_READ_RETRY_COUNT
- timeout
);
801 ft2232_debug_dump_buffer();
806 #ifdef _DEBUG_USB_COMMS_
807 LOG_DEBUG("read buffer (%i retries): %i bytes",
808 LIBFTDI_READ_RETRY_COUNT
- timeout
,
810 ft2232_debug_dump_buffer();
814 ft2232_expect_read
= 0;
815 ft2232_read_pointer
= 0;
817 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
818 * that wasn't handled by a caller-provided error handler
828 type
= jtag_scan_type(cmd
->cmd
.scan
);
829 if (type
!= SCAN_OUT
)
831 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
832 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
833 ft2232_read_scan(type
, buffer
, scan_size
);
834 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
835 retval
= ERROR_JTAG_QUEUE_FAILED
;
847 ft2232_buffer_size
= 0;
853 * Function ft2232_add_pathmove
854 * moves the TAP controller from the current state to a new state through the
855 * given path, where path is an array of tap_state_t's.
857 * @param path is an array of tap_stat_t which gives the states to traverse through
858 * ending with the last state at path[num_states-1]
859 * @param num_states is the count of state steps to move through
861 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
865 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
869 /* this loop verifies that the path is legal and logs each state in the path */
872 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
874 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
876 /* command "Clock Data to TMS/CS Pin (no Read)" */
879 /* number of states remaining */
880 buffer_write(num_states_batch
- 1);
882 while (num_states_batch
--) {
883 /* either TMS=0 or TMS=1 must work ... */
884 if (tap_state_transition(tap_get_state(), false)
885 == path
[state_count
])
886 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
887 else if (tap_state_transition(tap_get_state(), true)
888 == path
[state_count
])
889 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
891 /* ... or else the caller goofed BADLY */
893 LOG_ERROR("BUG: %s -> %s isn't a valid "
894 "TAP state transition",
895 tap_state_name(tap_get_state()),
896 tap_state_name(path
[state_count
]));
900 tap_set_state(path
[state_count
]);
905 buffer_write(tms_byte
);
907 tap_set_end_state(tap_get_state());
910 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
912 int num_bytes
= (scan_size
+ 7) / 8;
913 int bits_left
= scan_size
;
919 if (tap_get_state() != TAP_DRSHIFT
)
921 move_to_state(TAP_DRSHIFT
);
926 if (tap_get_state() != TAP_IRSHIFT
)
928 move_to_state(TAP_IRSHIFT
);
932 /* add command for complete bytes */
933 while (num_bytes
> 1)
938 /* Clock Data Bytes In and Out LSB First */
940 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
942 else if (type
== SCAN_OUT
)
944 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
946 /* LOG_DEBUG("added TDI bytes (o)"); */
948 else if (type
== SCAN_IN
)
950 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
952 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
955 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
956 num_bytes
-= thisrun_bytes
;
958 buffer_write((uint8_t) (thisrun_bytes
- 1));
959 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
963 /* add complete bytes */
964 while (thisrun_bytes
-- > 0)
966 buffer_write(buffer
[cur_byte
++]);
970 else /* (type == SCAN_IN) */
972 bits_left
-= 8 * (thisrun_bytes
);
976 /* the most signifcant bit is scanned during TAP movement */
978 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
982 /* process remaining bits but the last one */
987 /* Clock Data Bits In and Out LSB First */
989 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
991 else if (type
== SCAN_OUT
)
993 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
995 /* LOG_DEBUG("added TDI bits (o)"); */
997 else if (type
== SCAN_IN
)
999 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1001 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1004 buffer_write(bits_left
- 2);
1005 if (type
!= SCAN_IN
)
1006 buffer_write(buffer
[cur_byte
]);
1009 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
1010 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
1012 if (type
== SCAN_IO
)
1014 /* Clock Data Bits In and Out LSB First */
1016 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1018 else if (type
== SCAN_OUT
)
1020 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1022 /* LOG_DEBUG("added TDI bits (o)"); */
1024 else if (type
== SCAN_IN
)
1026 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1028 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1031 buffer_write(last_bit
);
1039 /* move from Shift-IR/DR to end state */
1040 if (type
!= SCAN_OUT
)
1042 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1043 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1046 /* Clock Data to TMS/CS Pin with Read */
1051 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1052 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1053 /* Clock Data to TMS/CS Pin (no Read) */
1057 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
1058 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1061 if (tap_get_state() != tap_get_end_state())
1063 move_to_state(tap_get_end_state());
1067 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1069 int num_bytes
= (scan_size
+ 7) / 8;
1070 int bits_left
= scan_size
;
1073 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
1074 uint8_t* receive_pointer
= receive_buffer
;
1075 uint32_t bytes_written
;
1076 uint32_t bytes_read
;
1078 int thisrun_read
= 0;
1082 LOG_ERROR("BUG: large IR scans are not supported");
1086 if (tap_get_state() != TAP_DRSHIFT
)
1088 move_to_state(TAP_DRSHIFT
);
1091 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1093 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1096 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1097 ft2232_buffer_size
, (int)bytes_written
);
1098 ft2232_buffer_size
= 0;
1100 /* add command for complete bytes */
1101 while (num_bytes
> 1)
1105 if (type
== SCAN_IO
)
1107 /* Clock Data Bytes In and Out LSB First */
1109 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1111 else if (type
== SCAN_OUT
)
1113 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1115 /* LOG_DEBUG("added TDI bytes (o)"); */
1117 else if (type
== SCAN_IN
)
1119 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1121 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1124 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1125 thisrun_read
= thisrun_bytes
;
1126 num_bytes
-= thisrun_bytes
;
1127 buffer_write((uint8_t) (thisrun_bytes
- 1));
1128 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1130 if (type
!= SCAN_IN
)
1132 /* add complete bytes */
1133 while (thisrun_bytes
-- > 0)
1135 buffer_write(buffer
[cur_byte
]);
1140 else /* (type == SCAN_IN) */
1142 bits_left
-= 8 * (thisrun_bytes
);
1145 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1147 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1150 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1152 (int)bytes_written
);
1153 ft2232_buffer_size
= 0;
1155 if (type
!= SCAN_OUT
)
1157 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1159 LOG_ERROR("couldn't read from FT2232");
1162 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1165 receive_pointer
+= bytes_read
;
1171 /* the most signifcant bit is scanned during TAP movement */
1172 if (type
!= SCAN_IN
)
1173 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1177 /* process remaining bits but the last one */
1180 if (type
== SCAN_IO
)
1182 /* Clock Data Bits In and Out LSB First */
1184 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1186 else if (type
== SCAN_OUT
)
1188 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1190 /* LOG_DEBUG("added TDI bits (o)"); */
1192 else if (type
== SCAN_IN
)
1194 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1196 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1198 buffer_write(bits_left
- 2);
1199 if (type
!= SCAN_IN
)
1200 buffer_write(buffer
[cur_byte
]);
1202 if (type
!= SCAN_OUT
)
1206 if (tap_get_end_state() == TAP_DRSHIFT
)
1208 if (type
== SCAN_IO
)
1210 /* Clock Data Bits In and Out LSB First */
1212 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1214 else if (type
== SCAN_OUT
)
1216 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1218 /* LOG_DEBUG("added TDI bits (o)"); */
1220 else if (type
== SCAN_IN
)
1222 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1224 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1227 buffer_write(last_bit
);
1231 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1232 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1235 /* move from Shift-IR/DR to end state */
1236 if (type
!= SCAN_OUT
)
1238 /* Clock Data to TMS/CS Pin with Read */
1240 /* LOG_DEBUG("added TMS scan (read)"); */
1244 /* Clock Data to TMS/CS Pin (no Read) */
1246 /* LOG_DEBUG("added TMS scan (no read)"); */
1249 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1250 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1253 if (type
!= SCAN_OUT
)
1256 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1258 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1261 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1263 (int)bytes_written
);
1264 ft2232_buffer_size
= 0;
1266 if (type
!= SCAN_OUT
)
1268 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1270 LOG_ERROR("couldn't read from FT2232");
1273 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1276 receive_pointer
+= bytes_read
;
1282 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1284 int predicted_size
= 3;
1285 int num_bytes
= (scan_size
- 1) / 8;
1287 if (tap_get_state() != TAP_DRSHIFT
)
1288 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1290 if (type
== SCAN_IN
) /* only from device to host */
1292 /* complete bytes */
1293 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1295 /* remaining bits - 1 (up to 7) */
1296 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1298 else /* host to device, or bidirectional */
1300 /* complete bytes */
1301 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1303 /* remaining bits -1 (up to 7) */
1304 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1307 return predicted_size
;
1310 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1312 int predicted_size
= 0;
1314 if (type
!= SCAN_OUT
)
1316 /* complete bytes */
1317 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1319 /* remaining bits - 1 */
1320 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1322 /* last bit (from TMS scan) */
1323 predicted_size
+= 1;
1326 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1328 return predicted_size
;
1331 static void usbjtag_reset(int trst
, int srst
)
1333 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1336 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1337 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1339 low_output
&= ~nTRST
; /* switch output low */
1343 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1344 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1346 low_output
|= nTRST
; /* switch output high */
1351 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1352 low_output
&= ~nSRST
; /* switch output low */
1354 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1358 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1359 low_output
|= nSRST
; /* switch output high */
1361 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1364 /* command "set data bits low byte" */
1366 buffer_write(low_output
);
1367 buffer_write(low_direction
);
1370 static void jtagkey_reset(int trst
, int srst
)
1372 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1375 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1376 high_output
&= ~nTRSTnOE
;
1378 high_output
&= ~nTRST
;
1382 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1383 high_output
|= nTRSTnOE
;
1385 high_output
|= nTRST
;
1390 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1391 high_output
&= ~nSRST
;
1393 high_output
&= ~nSRSTnOE
;
1397 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1398 high_output
|= nSRST
;
1400 high_output
|= nSRSTnOE
;
1403 /* command "set data bits high byte" */
1405 buffer_write(high_output
);
1406 buffer_write(high_direction
);
1407 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1411 static void olimex_jtag_reset(int trst
, int srst
)
1413 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1416 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1417 high_output
&= ~nTRSTnOE
;
1419 high_output
&= ~nTRST
;
1423 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1424 high_output
|= nTRSTnOE
;
1426 high_output
|= nTRST
;
1431 high_output
|= nSRST
;
1435 high_output
&= ~nSRST
;
1438 /* command "set data bits high byte" */
1440 buffer_write(high_output
);
1441 buffer_write(high_direction
);
1442 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1446 static void axm0432_jtag_reset(int trst
, int srst
)
1450 tap_set_state(TAP_RESET
);
1451 high_output
&= ~nTRST
;
1455 high_output
|= nTRST
;
1460 high_output
&= ~nSRST
;
1464 high_output
|= nSRST
;
1467 /* command "set data bits low byte" */
1469 buffer_write(high_output
);
1470 buffer_write(high_direction
);
1471 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1475 static void flyswatter_reset(int trst
, int srst
)
1479 low_output
&= ~nTRST
;
1483 low_output
|= nTRST
;
1488 low_output
|= nSRST
;
1492 low_output
&= ~nSRST
;
1495 /* command "set data bits low byte" */
1497 buffer_write(low_output
);
1498 buffer_write(low_direction
);
1499 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1502 static void turtle_reset(int trst
, int srst
)
1508 low_output
|= nSRST
;
1512 low_output
&= ~nSRST
;
1515 /* command "set data bits low byte" */
1517 buffer_write(low_output
);
1518 buffer_write(low_direction
);
1519 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1522 static void comstick_reset(int trst
, int srst
)
1526 high_output
&= ~nTRST
;
1530 high_output
|= nTRST
;
1535 high_output
&= ~nSRST
;
1539 high_output
|= nSRST
;
1542 /* command "set data bits high byte" */
1544 buffer_write(high_output
);
1545 buffer_write(high_direction
);
1546 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1550 static void stm32stick_reset(int trst
, int srst
)
1554 high_output
&= ~nTRST
;
1558 high_output
|= nTRST
;
1563 low_output
&= ~nSRST
;
1567 low_output
|= nSRST
;
1570 /* command "set data bits low byte" */
1572 buffer_write(low_output
);
1573 buffer_write(low_direction
);
1575 /* command "set data bits high byte" */
1577 buffer_write(high_output
);
1578 buffer_write(high_direction
);
1579 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1583 static void sheevaplug_reset(int trst
, int srst
)
1586 high_output
&= ~nTRST
;
1588 high_output
|= nTRST
;
1591 high_output
&= ~nSRSTnOE
;
1593 high_output
|= nSRSTnOE
;
1595 /* command "set data bits high byte" */
1597 buffer_write(high_output
);
1598 buffer_write(high_direction
);
1599 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1602 static void redbee_reset(int trst
, int srst
)
1606 tap_set_state(TAP_RESET
);
1607 high_output
&= ~nTRST
;
1611 high_output
|= nTRST
;
1616 high_output
&= ~nSRST
;
1620 high_output
|= nSRST
;
1623 /* command "set data bits low byte" */
1625 buffer_write(high_output
);
1626 buffer_write(high_direction
);
1627 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1628 "high_direction: 0x%2.2x", trst
, srst
, high_output
,
1632 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1636 int predicted_size
= 0;
1639 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1640 cmd
->cmd
.runtest
->num_cycles
,
1641 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1643 /* only send the maximum buffer size that FT2232C can handle */
1645 if (tap_get_state() != TAP_IDLE
)
1646 predicted_size
+= 3;
1647 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1648 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1649 predicted_size
+= 3;
1650 if (tap_get_end_state() != TAP_IDLE
)
1651 predicted_size
+= 3;
1652 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1654 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1655 retval
= ERROR_JTAG_QUEUE_FAILED
;
1659 if (tap_get_state() != TAP_IDLE
)
1661 move_to_state(TAP_IDLE
);
1664 i
= cmd
->cmd
.runtest
->num_cycles
;
1667 /* there are no state transitions in this code, so omit state tracking */
1669 /* command "Clock Data to TMS/CS Pin (no Read)" */
1673 buffer_write((i
> 7) ? 6 : (i
- 1));
1678 i
-= (i
> 7) ? 7 : i
;
1679 /* LOG_DEBUG("added TMS scan (no read)"); */
1682 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1684 if (tap_get_state() != tap_get_end_state())
1686 move_to_state(tap_get_end_state());
1690 DEBUG_JTAG_IO("runtest: %i, end in %s",
1691 cmd
->cmd
.runtest
->num_cycles
,
1692 tap_state_name(tap_get_end_state()));
1696 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1698 int predicted_size
= 0;
1699 int retval
= ERROR_OK
;
1701 DEBUG_JTAG_IO("statemove end in %s",
1702 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1704 /* only send the maximum buffer size that FT2232C can handle */
1706 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1708 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1709 retval
= ERROR_JTAG_QUEUE_FAILED
;
1713 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1715 /* For TAP_RESET, ignore the current recorded state. It's often
1716 * wrong at server startup, and this transation is critical whenever
1719 if (tap_get_end_state() == TAP_RESET
) {
1720 clock_tms(0x4b, 0xff, 5, 0);
1723 /* shortest-path move to desired end state */
1724 } else if (tap_get_state() != tap_get_end_state())
1726 move_to_state(tap_get_end_state());
1734 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1735 * (or SWD) state machine.
1737 static int ft2232_execute_tms(struct jtag_command
*cmd
)
1739 int retval
= ERROR_OK
;
1740 unsigned num_bits
= cmd
->cmd
.tms
->num_bits
;
1741 const uint8_t *bits
= cmd
->cmd
.tms
->bits
;
1744 DEBUG_JTAG_IO("TMS: %d bits", num_bits
);
1746 /* only send the maximum buffer size that FT2232C can handle */
1747 count
= 3 * DIV_ROUND_UP(num_bits
, 4);
1748 if (ft2232_buffer_size
+ 3*count
+ 1 > FT2232_BUFFER_SIZE
) {
1749 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1750 retval
= ERROR_JTAG_QUEUE_FAILED
;
1756 /* Shift out in batches of at most 6 bits; there's a report of an
1757 * FT2232 bug in this area, where shifting exactly 7 bits can make
1758 * problems with TMS signaling for the last clock cycle:
1760 * http://developer.intra2net.com/mailarchive/html/
1761 * libftdi/2009/msg00292.html
1763 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1765 * Note that pathmoves in JTAG are not often seven bits, so that
1766 * isn't a particularly likely situation outside of "special"
1767 * signaling such as switching between JTAG and SWD modes.
1770 if (num_bits
<= 6) {
1772 buffer_write(num_bits
- 1);
1773 buffer_write(*bits
& 0x3f);
1777 /* Yes, this is lazy ... we COULD shift out more data
1778 * bits per operation, but doing it in nybbles is easy
1782 buffer_write(*bits
& 0xf);
1785 count
= (num_bits
> 4) ? 4 : num_bits
;
1788 buffer_write(count
- 1);
1789 buffer_write((*bits
>> 4) & 0xf);
1799 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1801 int predicted_size
= 0;
1802 int retval
= ERROR_OK
;
1804 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1805 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1807 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1808 tap_state_name(tap_get_state()),
1809 tap_state_name(path
[num_states
-1]));
1811 /* only send the maximum buffer size that FT2232C can handle */
1812 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1813 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1815 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1816 retval
= ERROR_JTAG_QUEUE_FAILED
;
1822 ft2232_add_pathmove(path
, num_states
);
1828 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1831 int scan_size
; /* size of IR or DR scan */
1832 int predicted_size
= 0;
1833 int retval
= ERROR_OK
;
1835 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1837 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1839 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1841 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1842 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1844 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1845 /* unsent commands before this */
1846 if (first_unsent
!= cmd
)
1847 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1848 retval
= ERROR_JTAG_QUEUE_FAILED
;
1850 /* current command */
1851 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1852 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1854 first_unsent
= cmd
->next
;
1859 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1861 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1864 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1865 retval
= ERROR_JTAG_QUEUE_FAILED
;
1869 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1870 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1871 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1872 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1876 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1877 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1878 tap_state_name(tap_get_end_state()));
1883 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1886 int predicted_size
= 0;
1889 DEBUG_JTAG_IO("reset trst: %i srst %i",
1890 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1892 /* only send the maximum buffer size that FT2232C can handle */
1894 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1896 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1897 retval
= ERROR_JTAG_QUEUE_FAILED
;
1902 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1904 tap_set_state(TAP_RESET
);
1907 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1910 DEBUG_JTAG_IO("trst: %i, srst: %i",
1911 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1915 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1920 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1922 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1923 retval
= ERROR_JTAG_QUEUE_FAILED
;
1924 first_unsent
= cmd
->next
;
1925 jtag_sleep(cmd
->cmd
.sleep
->us
);
1926 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1928 tap_state_name(tap_get_state()));
1932 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1937 /* this is only allowed while in a stable state. A check for a stable
1938 * state was done in jtag_add_clocks()
1940 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1941 retval
= ERROR_JTAG_QUEUE_FAILED
;
1942 DEBUG_JTAG_IO("clocks %i while in %s",
1943 cmd
->cmd
.stableclocks
->num_cycles
,
1944 tap_state_name(tap_get_state()));
1948 static int ft2232_execute_command(struct jtag_command
*cmd
)
1954 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1955 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1956 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1957 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1958 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1959 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1960 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1962 retval
= ft2232_execute_tms(cmd
);
1965 LOG_ERROR("BUG: unknown JTAG command type encountered");
1966 retval
= ERROR_JTAG_QUEUE_FAILED
;
1972 static int ft2232_execute_queue(void)
1974 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
1977 first_unsent
= cmd
; /* next command that has to be sent */
1980 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1981 * that wasn't handled by a caller-provided error handler
1985 ft2232_buffer_size
= 0;
1986 ft2232_expect_read
= 0;
1988 /* blink, if the current layout has that feature */
1994 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1995 retval
= ERROR_JTAG_QUEUE_FAILED
;
1996 /* Start reading input before FT2232 TX buffer fills up */
1998 if (ft2232_expect_read
> 256)
2000 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2001 retval
= ERROR_JTAG_QUEUE_FAILED
;
2006 if (require_send
> 0)
2007 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2008 retval
= ERROR_JTAG_QUEUE_FAILED
;
2013 #if BUILD_FT2232_FTD2XX == 1
2014 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2018 char SerialNumber
[16];
2019 char Description
[64];
2020 DWORD openex_flags
= 0;
2021 char* openex_string
= NULL
;
2022 uint8_t latency_timer
;
2024 if (layout
== NULL
) {
2025 LOG_WARNING("No ft2232 layout specified'");
2026 return ERROR_JTAG_INIT_FAILED
;
2029 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout
->name
, vid
, pid
);
2032 /* Add non-standard Vid/Pid to the linux driver */
2033 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
2035 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
2039 if (ft2232_device_desc
&& ft2232_serial
)
2041 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2042 ft2232_device_desc
= NULL
;
2045 if (ft2232_device_desc
)
2047 openex_string
= ft2232_device_desc
;
2048 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
2050 else if (ft2232_serial
)
2052 openex_string
= ft2232_serial
;
2053 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
2057 LOG_ERROR("neither device description nor serial number specified");
2058 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2060 return ERROR_JTAG_INIT_FAILED
;
2063 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2064 if (status
!= FT_OK
) {
2065 /* under Win32, the FTD2XX driver appends an "A" to the end
2066 * of the description, if we tried by the desc, then
2067 * try by the alternate "A" description. */
2068 if (openex_string
== ft2232_device_desc
) {
2069 /* Try the alternate method. */
2070 openex_string
= ft2232_device_desc_A
;
2071 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
2072 if (status
== FT_OK
) {
2073 /* yea, the "alternate" method worked! */
2075 /* drat, give the user a meaningfull message.
2076 * telling the use we tried *BOTH* methods. */
2077 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2079 ft2232_device_desc_A
);
2084 if (status
!= FT_OK
)
2090 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
2092 return ERROR_JTAG_INIT_FAILED
;
2094 LOG_ERROR("unable to open ftdi device: %lu", status
);
2095 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
2096 if (status
== FT_OK
)
2098 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
2101 for (i
= 0; i
< num_devices
; i
++)
2102 desc_array
[i
] = malloc(64);
2104 desc_array
[num_devices
] = NULL
;
2106 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
2108 if (status
== FT_OK
)
2110 LOG_ERROR("ListDevices: %lu\n", num_devices
);
2111 for (i
= 0; i
< num_devices
; i
++)
2112 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
2115 for (i
= 0; i
< num_devices
; i
++)
2116 free(desc_array
[i
]);
2122 LOG_ERROR("ListDevices: NONE\n");
2124 return ERROR_JTAG_INIT_FAILED
;
2127 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
2129 LOG_ERROR("unable to set latency timer: %lu", status
);
2130 return ERROR_JTAG_INIT_FAILED
;
2133 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
2135 LOG_ERROR("unable to get latency timer: %lu", status
);
2136 return ERROR_JTAG_INIT_FAILED
;
2140 LOG_DEBUG("current latency timer: %i", latency_timer
);
2143 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
2145 LOG_ERROR("unable to set timeouts: %lu", status
);
2146 return ERROR_JTAG_INIT_FAILED
;
2149 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
2151 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
2152 return ERROR_JTAG_INIT_FAILED
;
2155 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
2157 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
2158 return ERROR_JTAG_INIT_FAILED
;
2162 static const char* type_str
[] =
2163 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2164 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2165 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
2166 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
2167 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
2168 LOG_INFO("deviceID: %lu", deviceID
);
2169 LOG_INFO("SerialNumber: %s", SerialNumber
);
2170 LOG_INFO("Description: %s", Description
);
2176 static int ft2232_purge_ftd2xx(void)
2180 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2182 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2183 return ERROR_JTAG_INIT_FAILED
;
2189 #endif /* BUILD_FT2232_FTD2XX == 1 */
2191 #if BUILD_FT2232_LIBFTDI == 1
2192 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
, int channel
)
2194 uint8_t latency_timer
;
2196 if (layout
== NULL
) {
2197 LOG_WARNING("No ft2232 layout specified'");
2198 return ERROR_JTAG_INIT_FAILED
;
2201 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2202 layout
->name
, vid
, pid
);
2204 if (ftdi_init(&ftdic
) < 0)
2205 return ERROR_JTAG_INIT_FAILED
;
2207 /* default to INTERFACE_A */
2208 if(channel
== INTERFACE_ANY
) { channel
= INTERFACE_A
; }
2210 if (ftdi_set_interface(&ftdic
, channel
) < 0)
2212 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2213 return ERROR_JTAG_INIT_FAILED
;
2216 /* context, vendor id, product id */
2217 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2221 LOG_WARNING("unable to open ftdi device (trying more): %s",
2224 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2226 return ERROR_JTAG_INIT_FAILED
;
2229 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2230 if (ftdi_usb_reset(&ftdic
) < 0)
2232 LOG_ERROR("unable to reset ftdi device");
2233 return ERROR_JTAG_INIT_FAILED
;
2236 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2238 LOG_ERROR("unable to set latency timer");
2239 return ERROR_JTAG_INIT_FAILED
;
2242 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2244 LOG_ERROR("unable to get latency timer");
2245 return ERROR_JTAG_INIT_FAILED
;
2249 LOG_DEBUG("current latency timer: %i", latency_timer
);
2252 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2254 ftdi_device
= ftdic
.type
;
2255 static const char* type_str
[] =
2256 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2257 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2258 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2259 ? ftdi_device
: no_of_known_types
;
2260 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2264 static int ft2232_purge_libftdi(void)
2266 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2268 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2269 return ERROR_JTAG_INIT_FAILED
;
2275 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2277 static int ft2232_init(void)
2281 uint32_t bytes_written
;
2283 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2285 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2289 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2292 if (layout
== NULL
) {
2293 LOG_WARNING("No ft2232 layout specified'");
2294 return ERROR_JTAG_INIT_FAILED
;
2297 for (int i
= 0; 1; i
++)
2300 * "more indicates that there are more IDs to try, so we should
2301 * not print an error for an ID mismatch (but for anything
2304 * try_more indicates that the error code returned indicates an
2305 * ID mismatch (and nothing else) and that we should proceeed
2306 * with the next ID pair.
2308 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2311 #if BUILD_FT2232_FTD2XX == 1
2312 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2314 #elif BUILD_FT2232_LIBFTDI == 1
2315 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2316 more
, &try_more
, layout
->channel
);
2320 if (!more
|| !try_more
)
2324 ft2232_buffer_size
= 0;
2325 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2327 if (layout
->init() != ERROR_OK
)
2328 return ERROR_JTAG_INIT_FAILED
;
2330 if (ft2232_device_is_highspeed())
2332 #ifndef BUILD_FT2232_HIGHSPEED
2333 #if BUILD_FT2232_FTD2XX == 1
2334 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2335 #elif BUILD_FT2232_LIBFTDI == 1
2336 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2339 /* make sure the legacy mode is disabled */
2340 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2341 return ERROR_JTAG_INIT_FAILED
;
2344 ft2232_speed(jtag_get_speed());
2346 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2347 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2349 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2350 return ERROR_JTAG_INIT_FAILED
;
2353 #if BUILD_FT2232_FTD2XX == 1
2354 return ft2232_purge_ftd2xx();
2355 #elif BUILD_FT2232_LIBFTDI == 1
2356 return ft2232_purge_libftdi();
2362 static int usbjtag_init(void)
2365 uint32_t bytes_written
;
2366 char *ft2232_layout
= layout
->name
;
2369 low_direction
= 0x0b;
2371 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2378 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2385 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2387 /* There are multiple revisions of LM3S811 eval boards:
2388 * - Rev B (and older?) boards have no SWO trace support.
2389 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2390 * they should use the "luminary_icdi" layout instead.
2397 low_direction
= 0x8b;
2399 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2401 /* Most Luminary eval boards support SWO trace output,
2402 * and should use this "luminary_icdi" layout.
2409 low_direction
= 0xcb;
2413 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2414 return ERROR_JTAG_INIT_FAILED
;
2417 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2418 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2420 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2421 low_output
&= ~nTRST
; /* nTRST = 0 */
2425 low_direction
|= nTRSTnOE
; /* nTRST output */
2426 low_output
|= nTRST
; /* nTRST = 1 */
2429 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2431 low_direction
|= nSRSTnOE
; /* nSRST output */
2432 low_output
|= nSRST
; /* nSRST = 1 */
2436 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2437 low_output
&= ~nSRST
; /* nSRST = 0 */
2440 /* initialize low byte for jtag */
2441 buf
[0] = 0x80; /* command "set data bits low byte" */
2442 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2443 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2444 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2446 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2448 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2449 return ERROR_JTAG_INIT_FAILED
;
2455 static int axm0432_jtag_init(void)
2458 uint32_t bytes_written
;
2461 low_direction
= 0x2b;
2463 /* initialize low byte for jtag */
2464 buf
[0] = 0x80; /* command "set data bits low byte" */
2465 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2466 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2467 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2469 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2471 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2472 return ERROR_JTAG_INIT_FAILED
;
2475 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2478 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2480 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2484 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2489 high_direction
= 0x0c;
2491 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2492 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2494 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2498 high_output
|= nTRST
;
2501 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2503 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2507 high_output
|= nSRST
;
2510 /* initialize high port */
2511 buf
[0] = 0x82; /* command "set data bits high byte" */
2512 buf
[1] = high_output
; /* value */
2513 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2514 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2516 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2518 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2519 return ERROR_JTAG_INIT_FAILED
;
2525 static int redbee_init(void)
2528 uint32_t bytes_written
;
2531 low_direction
= 0x2b;
2533 /* initialize low byte for jtag */
2534 /* command "set data bits low byte" */
2536 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2537 buf
[2] = low_direction
;
2538 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2539 buf
[1] = low_output
;
2540 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2542 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2543 || (bytes_written
!= 3))
2545 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2546 return ERROR_JTAG_INIT_FAILED
;
2550 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2552 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2555 high_direction
= 0x0c;
2557 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2558 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2560 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2564 high_output
|= nTRST
;
2567 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2569 LOG_ERROR("can't set nSRST to push-pull on redbee");
2573 high_output
|= nSRST
;
2576 /* initialize high port */
2577 buf
[0] = 0x82; /* command "set data bits high byte" */
2578 buf
[1] = high_output
; /* value */
2579 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2580 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2582 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
2583 || (bytes_written
!= 3))
2585 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2586 return ERROR_JTAG_INIT_FAILED
;
2592 static int jtagkey_init(void)
2595 uint32_t bytes_written
;
2598 low_direction
= 0x1b;
2600 /* initialize low byte for jtag */
2601 buf
[0] = 0x80; /* command "set data bits low byte" */
2602 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2603 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2604 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2606 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2608 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2609 return ERROR_JTAG_INIT_FAILED
;
2612 if (strcmp(layout
->name
, "jtagkey") == 0)
2619 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2620 || (strcmp(layout
->name
, "oocdlink") == 0))
2629 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2634 high_direction
= 0x0f;
2636 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2637 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2639 high_output
|= nTRSTnOE
;
2640 high_output
&= ~nTRST
;
2644 high_output
&= ~nTRSTnOE
;
2645 high_output
|= nTRST
;
2648 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2650 high_output
&= ~nSRSTnOE
;
2651 high_output
|= nSRST
;
2655 high_output
|= nSRSTnOE
;
2656 high_output
&= ~nSRST
;
2659 /* initialize high port */
2660 buf
[0] = 0x82; /* command "set data bits high byte" */
2661 buf
[1] = high_output
; /* value */
2662 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2663 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2665 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2667 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2668 return ERROR_JTAG_INIT_FAILED
;
2674 static int olimex_jtag_init(void)
2677 uint32_t bytes_written
;
2680 low_direction
= 0x1b;
2682 /* initialize low byte for jtag */
2683 buf
[0] = 0x80; /* command "set data bits low byte" */
2684 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2685 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2686 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2688 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2690 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2691 return ERROR_JTAG_INIT_FAILED
;
2697 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2700 high_direction
= 0x0f;
2702 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2703 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2705 high_output
|= nTRSTnOE
;
2706 high_output
&= ~nTRST
;
2710 high_output
&= ~nTRSTnOE
;
2711 high_output
|= nTRST
;
2714 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2716 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2720 high_output
&= ~nSRST
;
2723 /* turn red LED on */
2724 high_output
|= 0x08;
2726 /* initialize high port */
2727 buf
[0] = 0x82; /* command "set data bits high byte" */
2728 buf
[1] = high_output
; /* value */
2729 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2730 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2732 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2734 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2735 return ERROR_JTAG_INIT_FAILED
;
2741 static int flyswatter_init(void)
2744 uint32_t bytes_written
;
2747 low_direction
= 0xfb;
2749 /* initialize low byte for jtag */
2750 buf
[0] = 0x80; /* command "set data bits low byte" */
2751 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2752 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2753 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2755 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2757 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2758 return ERROR_JTAG_INIT_FAILED
;
2762 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2764 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2767 high_direction
= 0x0c;
2769 /* turn red LED3 on, LED2 off */
2770 high_output
|= 0x08;
2772 /* initialize high port */
2773 buf
[0] = 0x82; /* command "set data bits high byte" */
2774 buf
[1] = high_output
; /* value */
2775 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2776 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2778 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2780 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2781 return ERROR_JTAG_INIT_FAILED
;
2787 static int turtle_init(void)
2790 uint32_t bytes_written
;
2793 low_direction
= 0x5b;
2795 /* initialize low byte for jtag */
2796 buf
[0] = 0x80; /* command "set data bits low byte" */
2797 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2798 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2799 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2801 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2803 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2804 return ERROR_JTAG_INIT_FAILED
;
2810 high_direction
= 0x0C;
2812 /* initialize high port */
2813 buf
[0] = 0x82; /* command "set data bits high byte" */
2814 buf
[1] = high_output
;
2815 buf
[2] = high_direction
;
2816 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2818 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2820 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2821 return ERROR_JTAG_INIT_FAILED
;
2827 static int comstick_init(void)
2830 uint32_t bytes_written
;
2833 low_direction
= 0x0b;
2835 /* initialize low byte for jtag */
2836 buf
[0] = 0x80; /* command "set data bits low byte" */
2837 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2838 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2839 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2841 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2843 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2844 return ERROR_JTAG_INIT_FAILED
;
2848 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2850 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2853 high_direction
= 0x03;
2855 /* initialize high port */
2856 buf
[0] = 0x82; /* command "set data bits high byte" */
2857 buf
[1] = high_output
;
2858 buf
[2] = high_direction
;
2859 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2861 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2863 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2864 return ERROR_JTAG_INIT_FAILED
;
2870 static int stm32stick_init(void)
2873 uint32_t bytes_written
;
2876 low_direction
= 0x8b;
2878 /* initialize low byte for jtag */
2879 buf
[0] = 0x80; /* command "set data bits low byte" */
2880 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2881 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2882 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2884 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2886 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2887 return ERROR_JTAG_INIT_FAILED
;
2891 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2893 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2896 high_direction
= 0x03;
2898 /* initialize high port */
2899 buf
[0] = 0x82; /* command "set data bits high byte" */
2900 buf
[1] = high_output
;
2901 buf
[2] = high_direction
;
2902 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2904 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2906 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2907 return ERROR_JTAG_INIT_FAILED
;
2913 static int sheevaplug_init(void)
2916 uint32_t bytes_written
;
2919 low_direction
= 0x1b;
2921 /* initialize low byte for jtag */
2922 buf
[0] = 0x80; /* command "set data bits low byte" */
2923 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2924 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2925 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2927 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2929 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2930 return ERROR_JTAG_INIT_FAILED
;
2939 high_direction
= 0x0f;
2941 /* nTRST is always push-pull */
2942 high_output
&= ~nTRSTnOE
;
2943 high_output
|= nTRST
;
2945 /* nSRST is always open-drain */
2946 high_output
|= nSRSTnOE
;
2947 high_output
&= ~nSRST
;
2949 /* initialize high port */
2950 buf
[0] = 0x82; /* command "set data bits high byte" */
2951 buf
[1] = high_output
; /* value */
2952 buf
[2] = high_direction
; /* all outputs - xRST */
2953 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2955 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2957 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2958 return ERROR_JTAG_INIT_FAILED
;
2964 static int cortino_jtag_init(void)
2967 uint32_t bytes_written
;
2970 low_direction
= 0x1b;
2972 /* initialize low byte for jtag */
2973 buf
[0] = 0x80; /* command "set data bits low byte" */
2974 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2975 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2976 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2978 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2980 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2981 return ERROR_JTAG_INIT_FAILED
;
2985 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2987 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2990 high_direction
= 0x03;
2992 /* initialize high port */
2993 buf
[0] = 0x82; /* command "set data bits high byte" */
2994 buf
[1] = high_output
;
2995 buf
[2] = high_direction
;
2996 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2998 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3000 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3001 return ERROR_JTAG_INIT_FAILED
;
3007 static void olimex_jtag_blink(void)
3009 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3010 * ACBUS3 is bit 3 of the GPIOH port
3012 if (high_output
& 0x08)
3014 /* set port pin high */
3015 high_output
&= 0x07;
3019 /* set port pin low */
3020 high_output
|= 0x08;
3024 buffer_write(high_output
);
3025 buffer_write(high_direction
);
3028 static void flyswatter_jtag_blink(void)
3031 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3033 high_output
^= 0x0c;
3036 buffer_write(high_output
);
3037 buffer_write(high_direction
);
3040 static void turtle_jtag_blink(void)
3043 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3045 if (high_output
& 0x08)
3055 buffer_write(high_output
);
3056 buffer_write(high_direction
);
3059 static int ft2232_quit(void)
3061 #if BUILD_FT2232_FTD2XX == 1
3064 status
= FT_Close(ftdih
);
3065 #elif BUILD_FT2232_LIBFTDI == 1
3066 ftdi_usb_close(&ftdic
);
3068 ftdi_deinit(&ftdic
);
3071 free(ft2232_buffer
);
3072 ft2232_buffer
= NULL
;
3077 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
3083 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
3084 cp
= strchr(ft2232_device_desc
, 0);
3085 /* under Win32, the FTD2XX driver appends an "A" to the end
3086 * of the description, this examines the given desc
3087 * and creates the 'missing' _A or non_A variable. */
3088 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
3089 /* it was, so make this the "A" version. */
3090 ft2232_device_desc_A
= ft2232_device_desc
;
3091 /* and *CREATE* the non-A version. */
3092 strcpy(buf
, ft2232_device_desc
);
3093 cp
= strchr(buf
, 0);
3095 ft2232_device_desc
= strdup(buf
);
3097 /* <space > A not defined
3099 sprintf(buf
, "%s A", ft2232_device_desc
);
3100 ft2232_device_desc_A
= strdup(buf
);
3105 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3111 COMMAND_HANDLER(ft2232_handle_serial_command
)
3115 ft2232_serial
= strdup(CMD_ARGV
[0]);
3119 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3125 COMMAND_HANDLER(ft2232_handle_layout_command
)
3127 if (CMD_ARGC
!= 1) {
3128 LOG_ERROR("Need exactly one argument to ft2232_layout");
3133 LOG_ERROR("already specified ft2232_layout %s",
3135 return (strcmp(layout
->name
, CMD_ARGV
[0]) != 0)
3140 for (const struct ft2232_layout
*l
= ft2232_layouts
; l
->name
; l
++) {
3141 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
3147 LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV
[0]);
3151 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
3153 if (CMD_ARGC
> MAX_USB_IDS
* 2)
3155 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3156 "(maximum is %d pairs)", MAX_USB_IDS
);
3157 CMD_ARGC
= MAX_USB_IDS
* 2;
3159 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
3161 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3163 return ERROR_COMMAND_SYNTAX_ERROR
;
3164 /* remove the incomplete trailing id */
3169 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
3171 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
3172 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
3176 * Explicitly terminate, in case there are multiples instances of
3179 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
3184 COMMAND_HANDLER(ft2232_handle_latency_command
)
3188 ft2232_latency
= atoi(CMD_ARGV
[0]);
3192 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3198 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
3202 /* 7 bits of either ones or zeros. */
3203 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
3205 while (num_cycles
> 0)
3207 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3208 * at most 7 bits per invocation. Here we invoke it potentially
3211 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
3213 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
3215 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
3216 retval
= ERROR_JTAG_QUEUE_FAILED
;
3221 /* there are no state transitions in this code, so omit state tracking */
3223 /* command "Clock Data to TMS/CS Pin (no Read)" */
3227 buffer_write(bitcount_per_command
- 1);
3229 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3234 num_cycles
-= bitcount_per_command
;
3240 /* ---------------------------------------------------------------------
3241 * Support for IceBear JTAG adapter from Section5:
3242 * http://section5.ch/icebear
3244 * Author: Sten, debian@sansys-electronic.com
3247 /* Icebear pin layout
3249 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3250 * GND GND | 4 3| n.c.
3251 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3252 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3253 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3254 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3255 * ADBUS2 TDO |14 13| GND GND
3257 * ADBUS0 O L TCK ACBUS0 GND
3258 * ADBUS1 O L TDI ACBUS1 GND
3259 * ADBUS2 I TDO ACBUS2 n.c.
3260 * ADBUS3 O H TMS ACBUS3 n.c.
3266 static int icebear_jtag_init(void) {
3268 uint32_t bytes_written
;
3270 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3271 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3275 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3276 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3277 low_direction
&= ~nTRST
; /* nTRST high impedance */
3280 low_direction
|= nTRST
;
3281 low_output
|= nTRST
;
3284 low_direction
|= nSRST
;
3285 low_output
|= nSRST
;
3287 /* initialize low byte for jtag */
3288 buf
[0] = 0x80; /* command "set data bits low byte" */
3289 buf
[1] = low_output
;
3290 buf
[2] = low_direction
;
3291 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3293 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3294 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3295 return ERROR_JTAG_INIT_FAILED
;
3299 high_direction
= 0x00;
3302 /* initialize high port */
3303 buf
[0] = 0x82; /* command "set data bits high byte" */
3304 buf
[1] = high_output
; /* value */
3305 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3306 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3308 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3309 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3310 return ERROR_JTAG_INIT_FAILED
;
3316 static void icebear_jtag_reset(int trst
, int srst
) {
3319 low_direction
|= nTRST
;
3320 low_output
&= ~nTRST
;
3322 else if (trst
== 0) {
3323 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3324 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3325 low_direction
&= ~nTRST
;
3327 low_output
|= nTRST
;
3331 low_output
&= ~nSRST
;
3333 else if (srst
== 0) {
3334 low_output
|= nSRST
;
3337 /* command "set data bits low byte" */
3339 buffer_write(low_output
);
3340 buffer_write(low_direction
);
3342 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3345 /* ---------------------------------------------------------------------
3346 * Support for Signalyzer H2 and Signalyzer H4
3347 * JTAG adapter from Xverve Technologies Inc.
3348 * http://www.signalyzer.com or http://www.xverve.com
3350 * Author: Oleg Seiljus, oleg@signalyzer.com
3352 static unsigned char signalyzer_h_side
;
3353 static unsigned int signalyzer_h_adapter_type
;
3355 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3357 #if BUILD_FT2232_FTD2XX == 1
3358 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3361 #define SIGNALYZER_COMMAND_ADDR 128
3362 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3364 #define SIGNALYZER_COMMAND_VERSION 0x41
3365 #define SIGNALYZER_COMMAND_RESET 0x42
3366 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3367 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3368 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3369 #define SIGNALYZER_COMMAND_LED_SET 0x53
3370 #define SIGNALYZER_COMMAND_ADC 0x54
3371 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3372 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3373 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3374 #define SIGNALYZER_COMMAND_I2C 0x58
3376 #define SIGNALYZER_CHAN_A 1
3377 #define SIGNALYZER_CHAN_B 2
3378 /* LEDS use channel C */
3379 #define SIGNALYZER_CHAN_C 4
3381 #define SIGNALYZER_LED_GREEN 1
3382 #define SIGNALYZER_LED_RED 2
3384 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3385 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3386 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3387 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3388 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3391 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3393 #if BUILD_FT2232_FTD2XX == 1
3394 return FT_WriteEE(ftdih
, address
, value
);
3395 #elif BUILD_FT2232_LIBFTDI == 1
3400 #if BUILD_FT2232_FTD2XX == 1
3401 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3403 return FT_ReadEE(ftdih
, address
, value
);
3407 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3408 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3410 unsigned char on_time
;
3411 unsigned char off_time
;
3413 if (on_time_ms
< 0xFFFF)
3414 on_time
= (unsigned char)(on_time_ms
/ 62);
3418 off_time
= (unsigned char)(off_time_ms
/ 62);
3420 #if BUILD_FT2232_FTD2XX == 1
3423 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3424 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3426 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3427 return ERROR_JTAG_DEVICE_ERROR
;
3430 if ((status
= signalyzer_h_ctrl_write(
3431 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3432 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3434 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3435 return ERROR_JTAG_DEVICE_ERROR
;
3438 if ((status
= signalyzer_h_ctrl_write(
3439 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3440 ((uint32_t)cycles
))) != FT_OK
)
3442 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3443 return ERROR_JTAG_DEVICE_ERROR
;
3446 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3447 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3449 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3450 return ERROR_JTAG_DEVICE_ERROR
;
3454 #elif BUILD_FT2232_LIBFTDI == 1
3457 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3458 ((uint32_t)(channel
<< 8) | led
))) < 0)
3460 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3461 ftdi_get_error_string(&ftdic
));
3462 return ERROR_JTAG_DEVICE_ERROR
;
3465 if ((retval
= signalyzer_h_ctrl_write(
3466 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3467 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3469 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3470 ftdi_get_error_string(&ftdic
));
3471 return ERROR_JTAG_DEVICE_ERROR
;
3474 if ((retval
= signalyzer_h_ctrl_write(
3475 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3476 (uint32_t)cycles
)) < 0)
3478 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3479 ftdi_get_error_string(&ftdic
));
3480 return ERROR_JTAG_DEVICE_ERROR
;
3483 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3484 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3486 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3487 ftdi_get_error_string(&ftdic
));
3488 return ERROR_JTAG_DEVICE_ERROR
;
3495 static int signalyzer_h_init(void)
3497 #if BUILD_FT2232_FTD2XX == 1
3504 uint16_t read_buf
[12] = { 0 };
3506 uint32_t bytes_written
;
3508 /* turn on center green led */
3509 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3510 0xFFFF, 0x00, 0x00);
3512 /* determine what channel config wants to open
3513 * TODO: change me... current implementation is made to work
3514 * with openocd description parsing.
3516 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3520 signalyzer_h_side
= *(end_of_desc
- 1);
3521 if (signalyzer_h_side
== 'B')
3522 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3524 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3528 LOG_ERROR("No Channel was specified");
3532 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3535 #if BUILD_FT2232_FTD2XX == 1
3536 /* read signalyzer versionining information */
3537 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3538 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3540 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3541 return ERROR_JTAG_DEVICE_ERROR
;
3544 for (i
= 0; i
< 10; i
++)
3546 if ((status
= signalyzer_h_ctrl_read(
3547 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3548 &read_buf
[i
])) != FT_OK
)
3550 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3552 return ERROR_JTAG_DEVICE_ERROR
;
3556 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3557 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3558 read_buf
[4], read_buf
[5], read_buf
[6]);
3560 /* set gpio register */
3561 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3562 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3564 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3565 return ERROR_JTAG_DEVICE_ERROR
;
3568 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3571 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3572 return ERROR_JTAG_DEVICE_ERROR
;
3575 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3576 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3578 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3579 return ERROR_JTAG_DEVICE_ERROR
;
3582 /* read adapter type information */
3583 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3584 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3586 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3587 return ERROR_JTAG_DEVICE_ERROR
;
3590 if ((status
= signalyzer_h_ctrl_write(
3591 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3593 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3594 return ERROR_JTAG_DEVICE_ERROR
;
3597 if ((status
= signalyzer_h_ctrl_write(
3598 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3600 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3601 return ERROR_JTAG_DEVICE_ERROR
;
3604 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3605 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3607 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3608 return ERROR_JTAG_DEVICE_ERROR
;
3613 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3614 &read_buf
[0])) != FT_OK
)
3616 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3617 return ERROR_JTAG_DEVICE_ERROR
;
3620 if (read_buf
[0] != 0x0498)
3621 signalyzer_h_adapter_type
= 0x0000;
3624 for (i
= 0; i
< 4; i
++)
3626 if ((status
= signalyzer_h_ctrl_read(
3627 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3628 &read_buf
[i
])) != FT_OK
)
3630 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3632 return ERROR_JTAG_DEVICE_ERROR
;
3636 signalyzer_h_adapter_type
= read_buf
[0];
3639 #elif BUILD_FT2232_LIBFTDI == 1
3640 /* currently libftdi does not allow reading individual eeprom
3641 * locations, therefore adapter type cannot be detected.
3642 * override with most common type
3644 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3647 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3649 /* ADAPTOR: EM_LT16_A */
3650 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3652 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3653 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3661 low_direction
= 0x1b;
3664 high_direction
= 0x0;
3666 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3668 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3669 low_output
&= ~nTRST
; /* nTRST = 0 */
3673 low_direction
|= nTRSTnOE
; /* nTRST output */
3674 low_output
|= nTRST
; /* nTRST = 1 */
3677 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3679 low_direction
|= nSRSTnOE
; /* nSRST output */
3680 low_output
|= nSRST
; /* nSRST = 1 */
3684 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3685 low_output
&= ~nSRST
; /* nSRST = 0 */
3688 #if BUILD_FT2232_FTD2XX == 1
3689 /* enable power to the module */
3690 if ((status
= signalyzer_h_ctrl_write(
3691 SIGNALYZER_DATA_BUFFER_ADDR
,
3692 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3695 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3697 return ERROR_JTAG_DEVICE_ERROR
;
3700 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3701 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3703 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3705 return ERROR_JTAG_DEVICE_ERROR
;
3708 /* set gpio mode register */
3709 if ((status
= signalyzer_h_ctrl_write(
3710 SIGNALYZER_DATA_BUFFER_ADDR
,
3711 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3713 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3715 return ERROR_JTAG_DEVICE_ERROR
;
3718 if ((status
= signalyzer_h_ctrl_write(
3719 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3722 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3724 return ERROR_JTAG_DEVICE_ERROR
;
3727 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3728 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3730 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3732 return ERROR_JTAG_DEVICE_ERROR
;
3735 /* set gpio register */
3736 if ((status
= signalyzer_h_ctrl_write(
3737 SIGNALYZER_DATA_BUFFER_ADDR
,
3738 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3740 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3742 return ERROR_JTAG_DEVICE_ERROR
;
3745 if ((status
= signalyzer_h_ctrl_write(
3746 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3749 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3751 return ERROR_JTAG_DEVICE_ERROR
;
3754 if ((status
= signalyzer_h_ctrl_write(
3755 SIGNALYZER_COMMAND_ADDR
,
3756 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3758 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3760 return ERROR_JTAG_DEVICE_ERROR
;
3765 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3766 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3767 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3768 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3769 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3771 if (signalyzer_h_adapter_type
3772 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3773 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3774 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3775 else if (signalyzer_h_adapter_type
3776 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3777 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3778 "(ARM JTAG with PSU) detected. (HW: %2x).",
3779 (read_buf
[1] >> 8));
3780 else if (signalyzer_h_adapter_type
3781 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3782 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3783 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3784 else if (signalyzer_h_adapter_type
3785 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3786 LOG_INFO("Signalyzer: EM-JTAG-P "
3787 "(Generic JTAG with PSU) detected. (HW: %2x).",
3788 (read_buf
[1] >> 8));
3796 low_direction
= 0x1b;
3799 high_direction
= 0x1f;
3801 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3803 high_output
|= nTRSTnOE
;
3804 high_output
&= ~nTRST
;
3808 high_output
&= ~nTRSTnOE
;
3809 high_output
|= nTRST
;
3812 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3814 high_output
&= ~nSRSTnOE
;
3815 high_output
|= nSRST
;
3819 high_output
|= nSRSTnOE
;
3820 high_output
&= ~nSRST
;
3823 #if BUILD_FT2232_FTD2XX == 1
3824 /* enable power to the module */
3825 if ((status
= signalyzer_h_ctrl_write(
3826 SIGNALYZER_DATA_BUFFER_ADDR
,
3827 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3830 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3832 return ERROR_JTAG_DEVICE_ERROR
;
3835 if ((status
= signalyzer_h_ctrl_write(
3836 SIGNALYZER_COMMAND_ADDR
,
3837 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3839 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3841 return ERROR_JTAG_DEVICE_ERROR
;
3844 /* set gpio mode register (IO_16 and IO_17 set as analog
3845 * inputs, other is gpio)
3847 if ((status
= signalyzer_h_ctrl_write(
3848 SIGNALYZER_DATA_BUFFER_ADDR
,
3849 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3851 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3853 return ERROR_JTAG_DEVICE_ERROR
;
3856 if ((status
= signalyzer_h_ctrl_write(
3857 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3860 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3862 return ERROR_JTAG_DEVICE_ERROR
;
3865 if ((status
= signalyzer_h_ctrl_write(
3866 SIGNALYZER_COMMAND_ADDR
,
3867 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3869 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3871 return ERROR_JTAG_DEVICE_ERROR
;
3874 /* set gpio register (all inputs, for -P modules,
3875 * PSU will be turned off)
3877 if ((status
= signalyzer_h_ctrl_write(
3878 SIGNALYZER_DATA_BUFFER_ADDR
,
3879 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3881 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3883 return ERROR_JTAG_DEVICE_ERROR
;
3886 if ((status
= signalyzer_h_ctrl_write(
3887 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3890 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3892 return ERROR_JTAG_DEVICE_ERROR
;
3895 if ((status
= signalyzer_h_ctrl_write(
3896 SIGNALYZER_COMMAND_ADDR
,
3897 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3899 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3901 return ERROR_JTAG_DEVICE_ERROR
;
3906 else if (signalyzer_h_adapter_type
== 0x0000)
3908 LOG_INFO("Signalyzer: No external modules were detected.");
3916 low_direction
= 0x1b;
3919 high_direction
= 0x0;
3921 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3923 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3924 low_output
&= ~nTRST
; /* nTRST = 0 */
3928 low_direction
|= nTRSTnOE
; /* nTRST output */
3929 low_output
|= nTRST
; /* nTRST = 1 */
3932 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3934 low_direction
|= nSRSTnOE
; /* nSRST output */
3935 low_output
|= nSRST
; /* nSRST = 1 */
3939 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3940 low_output
&= ~nSRST
; /* nSRST = 0 */
3945 LOG_ERROR("Unknown module type is detected: %.4x",
3946 signalyzer_h_adapter_type
);
3947 return ERROR_JTAG_DEVICE_ERROR
;
3950 /* initialize low byte of controller for jtag operation */
3952 buf
[1] = low_output
;
3953 buf
[2] = low_direction
;
3955 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3956 || (bytes_written
!= 3))
3958 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3959 return ERROR_JTAG_INIT_FAILED
;
3962 #if BUILD_FT2232_FTD2XX == 1
3963 if (ftdi_device
== FT_DEVICE_2232H
)
3965 /* initialize high byte of controller for jtag operation */
3967 buf
[1] = high_output
;
3968 buf
[2] = high_direction
;
3970 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3971 || (bytes_written
!= 3))
3973 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3974 return ERROR_JTAG_INIT_FAILED
;
3977 #elif BUILD_FT2232_LIBFTDI == 1
3978 if (ftdi_device
== TYPE_2232H
)
3980 /* initialize high byte of controller for jtag operation */
3982 buf
[1] = high_output
;
3983 buf
[2] = high_direction
;
3985 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3986 || (bytes_written
!= 3))
3988 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3989 return ERROR_JTAG_INIT_FAILED
;
3996 static void signalyzer_h_reset(int trst
, int srst
)
3998 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4000 /* ADAPTOR: EM_LT16_A */
4001 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
4005 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4006 /* switch to output pin (output is low) */
4007 low_direction
|= nTRSTnOE
;
4009 /* switch output low */
4010 low_output
&= ~nTRST
;
4014 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4015 /* switch to input pin (high-Z + internal
4016 * and external pullup) */
4017 low_direction
&= ~nTRSTnOE
;
4019 /* switch output high */
4020 low_output
|= nTRST
;
4025 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4026 /* switch output low */
4027 low_output
&= ~nSRST
;
4029 /* switch to output pin (output is low) */
4030 low_direction
|= nSRSTnOE
;
4034 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4035 /* switch output high */
4036 low_output
|= nSRST
;
4038 /* switch to input pin (high-Z) */
4039 low_direction
&= ~nSRSTnOE
;
4042 /* command "set data bits low byte" */
4044 buffer_write(low_output
);
4045 buffer_write(low_direction
);
4046 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4047 "low_direction: 0x%2.2x",
4048 trst
, srst
, low_output
, low_direction
);
4050 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4051 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
4052 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
4053 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
4054 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
4058 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4059 high_output
&= ~nTRSTnOE
;
4061 high_output
&= ~nTRST
;
4065 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4066 high_output
|= nTRSTnOE
;
4068 high_output
|= nTRST
;
4073 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4074 high_output
&= ~nSRST
;
4076 high_output
&= ~nSRSTnOE
;
4080 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4081 high_output
|= nSRST
;
4083 high_output
|= nSRSTnOE
;
4086 /* command "set data bits high byte" */
4088 buffer_write(high_output
);
4089 buffer_write(high_direction
);
4090 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4091 "high_direction: 0x%2.2x",
4092 trst
, srst
, high_output
, high_direction
);
4094 else if (signalyzer_h_adapter_type
== 0x0000)
4098 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4099 /* switch to output pin (output is low) */
4100 low_direction
|= nTRSTnOE
;
4102 /* switch output low */
4103 low_output
&= ~nTRST
;
4107 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4108 /* switch to input pin (high-Z + internal
4109 * and external pullup) */
4110 low_direction
&= ~nTRSTnOE
;
4112 /* switch output high */
4113 low_output
|= nTRST
;
4118 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4119 /* switch output low */
4120 low_output
&= ~nSRST
;
4122 /* switch to output pin (output is low) */
4123 low_direction
|= nSRSTnOE
;
4127 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4128 /* switch output high */
4129 low_output
|= nSRST
;
4131 /* switch to input pin (high-Z) */
4132 low_direction
&= ~nSRSTnOE
;
4135 /* command "set data bits low byte" */
4137 buffer_write(low_output
);
4138 buffer_write(low_direction
);
4139 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4140 "low_direction: 0x%2.2x",
4141 trst
, srst
, low_output
, low_direction
);
4145 static void signalyzer_h_blink(void)
4147 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
4150 /********************************************************************
4151 * Support for KT-LINK
4152 * JTAG adapter from KRISTECH
4153 * http://www.kristech.eu
4154 *******************************************************************/
4155 static int ktlink_init(void)
4158 uint32_t bytes_written
;
4159 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4161 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4162 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4164 // initialize low port
4165 buf
[0] = 0x80; // command "set data bits low byte"
4166 buf
[1] = low_output
;
4167 buf
[2] = low_direction
;
4168 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4170 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4172 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4173 return ERROR_JTAG_INIT_FAILED
;
4181 high_output
= 0x80; // turn LED on
4182 high_direction
= 0xFF; // all outputs
4184 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4186 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
4187 high_output
|= nTRSTnOE
;
4188 high_output
&= ~nTRST
;
4190 high_output
&= ~nTRSTnOE
;
4191 high_output
|= nTRST
;
4194 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
4195 high_output
&= ~nSRSTnOE
;
4196 high_output
|= nSRST
;
4198 high_output
|= nSRSTnOE
;
4199 high_output
&= ~nSRST
;
4202 // initialize high port
4203 buf
[0] = 0x82; // command "set data bits high byte"
4204 buf
[1] = high_output
; // value
4205 buf
[2] = high_direction
;
4206 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
4208 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
4210 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4211 return ERROR_JTAG_INIT_FAILED
;
4217 static void ktlink_reset(int trst
, int srst
)
4219 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4222 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4223 high_output
&= ~nTRSTnOE
;
4225 high_output
&= ~nTRST
;
4226 } else if (trst
== 0) {
4227 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
4228 high_output
|= nTRSTnOE
;
4230 high_output
|= nTRST
;
4234 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4235 high_output
&= ~nSRST
;
4237 high_output
&= ~nSRSTnOE
;
4238 } else if (srst
== 0) {
4239 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
4240 high_output
|= nSRST
;
4242 high_output
|= nSRSTnOE
;
4245 buffer_write(0x82); // command "set data bits high byte"
4246 buffer_write(high_output
);
4247 buffer_write(high_direction
);
4248 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4251 static void ktlink_blink(void)
4253 /* LED connected to ACBUS7 */
4254 if (high_output
& 0x80)
4255 high_output
&= 0x7F;
4257 high_output
|= 0x80;
4259 buffer_write(0x82); // command "set data bits high byte"
4260 buffer_write(high_output
);
4261 buffer_write(high_direction
);
4264 static const struct command_registration ft2232_command_handlers
[] = {
4266 .name
= "ft2232_device_desc",
4267 .handler
= &ft2232_handle_device_desc_command
,
4268 .mode
= COMMAND_CONFIG
,
4269 .help
= "set the USB device description of the FTDI FT2232 device",
4270 .usage
= "description_string",
4273 .name
= "ft2232_serial",
4274 .handler
= &ft2232_handle_serial_command
,
4275 .mode
= COMMAND_CONFIG
,
4276 .help
= "set the serial number of the FTDI FT2232 device",
4277 .usage
= "serial_string",
4280 .name
= "ft2232_layout",
4281 .handler
= &ft2232_handle_layout_command
,
4282 .mode
= COMMAND_CONFIG
,
4283 .help
= "set the layout of the FT2232 GPIO signals used "
4284 "to control output-enables and reset signals",
4285 .usage
= "layout_name",
4288 .name
= "ft2232_vid_pid",
4289 .handler
= &ft2232_handle_vid_pid_command
,
4290 .mode
= COMMAND_CONFIG
,
4291 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4292 .usage
= "(vid pid)* ",
4295 .name
= "ft2232_latency",
4296 .handler
= &ft2232_handle_latency_command
,
4297 .mode
= COMMAND_CONFIG
,
4298 .help
= "set the FT2232 latency timer to a new value",
4301 COMMAND_REGISTRATION_DONE
4304 struct jtag_interface ft2232_interface
= {
4306 .supported
= DEBUG_CAP_TMS_SEQ
,
4307 .commands
= ft2232_command_handlers
,
4309 .init
= ft2232_init
,
4310 .quit
= ft2232_quit
,
4311 .speed
= ft2232_speed
,
4312 .speed_div
= ft2232_speed_div
,
4314 .execute_queue
= ft2232_execute_queue
,