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 ***************************************************************************/
30 /* This code uses information contained in the MPSSE specification which was
32 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
33 * Hereafter this is called the "MPSSE Spec".
35 * The datasheet for the ftdichip.com's FT2232D part is here:
36 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
43 /* project specific includes */
44 #include <jtag/interface.h>
45 #include <jtag/commands.h>
46 #include <helper/time_support.h>
54 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
55 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
56 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
57 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
60 /* FT2232 access library includes */
61 #if BUILD_FT2232_FTD2XX == 1
63 #elif BUILD_FT2232_LIBFTDI == 1
67 /* max TCK for the high speed devices 30000 kHz */
68 #define FTDI_2232H_4232H_MAX_TCK 30000
69 /* max TCK for the full speed devices 6000 kHz */
70 #define FTDI_2232C_MAX_TCK 6000
71 /* this speed value tells that RTCK is requested */
75 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
76 * errors with a retry count of 100. Increasing it solves the problem for me.
79 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
80 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
83 #define LIBFTDI_READ_RETRY_COUNT 2000
85 #ifndef BUILD_FT2232_HIGHSPEED
86 #if BUILD_FT2232_FTD2XX == 1
87 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
88 #elif BUILD_FT2232_LIBFTDI == 1
89 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
94 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
95 * stable state. Calling code must ensure that current state is stable,
96 * that verification is not done in here.
98 * @param num_cycles The number of clocks cycles to send.
99 * @param cmd The command to send.
101 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
103 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
);
105 static char * ft2232_device_desc_A
= NULL
;
106 static char* ft2232_device_desc
= NULL
;
107 static char* ft2232_serial
= NULL
;
108 static char* ft2232_layout
= NULL
;
109 static uint8_t ft2232_latency
= 2;
110 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
112 #define MAX_USB_IDS 8
113 /* vid = pid = 0 marks the end of the list */
114 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
115 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
117 struct ft2232_layout
{
120 void (*reset
)(int trst
, int srst
);
124 /* init procedures for supported layouts */
125 static int usbjtag_init(void);
126 static int jtagkey_init(void);
127 static int olimex_jtag_init(void);
128 static int flyswatter_init(void);
129 static int turtle_init(void);
130 static int comstick_init(void);
131 static int stm32stick_init(void);
132 static int axm0432_jtag_init(void);
133 static int sheevaplug_init(void);
134 static int icebear_jtag_init(void);
135 static int cortino_jtag_init(void);
136 static int signalyzer_h_init(void);
137 static int ktlink_init(void);
139 /* reset procedures for supported layouts */
140 static void usbjtag_reset(int trst
, int srst
);
141 static void jtagkey_reset(int trst
, int srst
);
142 static void olimex_jtag_reset(int trst
, int srst
);
143 static void flyswatter_reset(int trst
, int srst
);
144 static void turtle_reset(int trst
, int srst
);
145 static void comstick_reset(int trst
, int srst
);
146 static void stm32stick_reset(int trst
, int srst
);
147 static void axm0432_jtag_reset(int trst
, int srst
);
148 static void sheevaplug_reset(int trst
, int srst
);
149 static void icebear_jtag_reset(int trst
, int srst
);
150 static void signalyzer_h_reset(int trst
, int srst
);
151 static void ktlink_reset(int trst
, int srst
);
153 /* blink procedures for layouts that support a blinking led */
154 static void olimex_jtag_blink(void);
155 static void flyswatter_jtag_blink(void);
156 static void turtle_jtag_blink(void);
157 static void signalyzer_h_blink(void);
158 static void ktlink_blink(void);
160 static const struct ft2232_layout ft2232_layouts
[] =
162 { "usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
163 { "jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
164 { "jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
165 { "oocdlink", jtagkey_init
, jtagkey_reset
, NULL
},
166 { "signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
167 { "evb_lm3s811", usbjtag_init
, usbjtag_reset
, NULL
},
168 { "luminary_icdi", usbjtag_init
, usbjtag_reset
, NULL
},
169 { "olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
170 { "flyswatter", flyswatter_init
, flyswatter_reset
, flyswatter_jtag_blink
},
171 { "turtelizer2", turtle_init
, turtle_reset
, turtle_jtag_blink
},
172 { "comstick", comstick_init
, comstick_reset
, NULL
},
173 { "stm32stick", stm32stick_init
, stm32stick_reset
, NULL
},
174 { "axm0432_jtag", axm0432_jtag_init
, axm0432_jtag_reset
, NULL
},
175 { "sheevaplug", sheevaplug_init
, sheevaplug_reset
, NULL
},
176 { "icebear", icebear_jtag_init
, icebear_jtag_reset
, NULL
},
177 { "cortino", cortino_jtag_init
, comstick_reset
, NULL
},
178 { "signalyzer-h", signalyzer_h_init
, signalyzer_h_reset
, signalyzer_h_blink
},
179 { "ktlink", ktlink_init
, ktlink_reset
, ktlink_blink
},
180 { NULL
, NULL
, NULL
, NULL
},
183 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
185 static const struct ft2232_layout
*layout
;
186 static uint8_t low_output
= 0x0;
187 static uint8_t low_direction
= 0x0;
188 static uint8_t high_output
= 0x0;
189 static uint8_t high_direction
= 0x0;
191 #if BUILD_FT2232_FTD2XX == 1
192 static FT_HANDLE ftdih
= NULL
;
193 static FT_DEVICE ftdi_device
= 0;
194 #elif BUILD_FT2232_LIBFTDI == 1
195 static struct ftdi_context ftdic
;
196 static enum ftdi_chip_type ftdi_device
;
199 static struct jtag_command
* first_unsent
; /* next command that has to be sent */
200 static int require_send
;
202 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
204 "There is a significant difference between libftdi and libftd2xx. The latter
205 one allows to schedule up to 64*64 bytes of result data while libftdi fails
206 with more than 4*64. As a consequence, the FT2232 driver is forced to
207 perform around 16x more USB transactions for long command streams with TDO
208 capture when running with libftdi."
211 #define FT2232_BUFFER_SIZE 131072
212 a comment would have been nice.
215 #define FT2232_BUFFER_SIZE 131072
217 static uint8_t* ft2232_buffer
= NULL
;
218 static int ft2232_buffer_size
= 0;
219 static int ft2232_read_pointer
= 0;
220 static int ft2232_expect_read
= 0;
223 * Function buffer_write
224 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
225 * @param val is the byte to send.
227 static inline void buffer_write(uint8_t val
)
229 assert(ft2232_buffer
);
230 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
231 ft2232_buffer
[ft2232_buffer_size
++] = val
;
235 * Function buffer_read
236 * returns a byte from the byte buffer.
238 static inline uint8_t buffer_read(void)
240 assert(ft2232_buffer
);
241 assert(ft2232_read_pointer
< ft2232_buffer_size
);
242 return ft2232_buffer
[ft2232_read_pointer
++];
246 * Clocks out \a bit_count bits on the TMS line, starting with the least
247 * significant bit of tms_bits and progressing to more significant bits.
248 * Rigorous state transition logging is done here via tap_set_state().
250 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
251 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
252 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
253 * is often used for this, 0x4b.
255 * @param tms_bits Holds the sequence of bits to send.
256 * @param tms_count Tells how many bits in the sequence.
257 * @param tdi_bit A single bit to pass on to TDI before the first TCK
258 * cycle and held static for the duration of TMS clocking.
260 * See the MPSSE spec referenced above.
262 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
266 int tms_ndx
; /* bit index into tms_byte */
268 assert(tms_count
> 0);
270 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
271 mpsse_cmd
, tms_bits
, tms_count
);
273 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
275 bool bit
= tms_bits
& 1;
278 tms_byte
|= (1 << tms_ndx
);
280 /* always do state transitions in public view */
281 tap_set_state(tap_state_transition(tap_get_state(), bit
));
283 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
288 if (tms_ndx
== 7 || i
== tms_count
-1)
290 buffer_write(mpsse_cmd
);
291 buffer_write(tms_ndx
- 1);
293 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
294 TMS/CS and is held static for the duration of TMS/CS clocking.
296 buffer_write(tms_byte
| (tdi_bit
<< 7));
302 * Function get_tms_buffer_requirements
303 * returns what clock_tms() will consume if called with
306 static inline int get_tms_buffer_requirements(int bit_count
)
308 return ((bit_count
+ 6)/7) * 3;
312 * Function move_to_state
313 * moves the TAP controller from the current state to a
314 * \a goal_state through a path given by tap_get_tms_path(). State transition
315 * logging is performed by delegation to clock_tms().
317 * @param goal_state is the destination state for the move.
319 static void move_to_state(tap_state_t goal_state
)
321 tap_state_t start_state
= tap_get_state();
323 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
324 lookup of the required TMS pattern to move to this state from the
328 /* do the 2 lookups */
329 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
330 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
332 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
334 clock_tms(0x4b, tms_bits
, tms_count
, 0);
337 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
339 #if BUILD_FT2232_FTD2XX == 1
341 DWORD dw_bytes_written
;
342 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
344 *bytes_written
= dw_bytes_written
;
345 LOG_ERROR("FT_Write returned: %lu", status
);
346 return ERROR_JTAG_DEVICE_ERROR
;
350 *bytes_written
= dw_bytes_written
;
353 #elif BUILD_FT2232_LIBFTDI == 1
355 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
358 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
359 return ERROR_JTAG_DEVICE_ERROR
;
363 *bytes_written
= retval
;
369 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
371 #if BUILD_FT2232_FTD2XX == 1
377 while ((*bytes_read
< size
) && timeout
--)
379 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
380 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
383 LOG_ERROR("FT_Read returned: %lu", status
);
384 return ERROR_JTAG_DEVICE_ERROR
;
386 *bytes_read
+= dw_bytes_read
;
389 #elif BUILD_FT2232_LIBFTDI == 1
391 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
394 while ((*bytes_read
< size
) && timeout
--)
396 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
399 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
400 return ERROR_JTAG_DEVICE_ERROR
;
402 *bytes_read
+= retval
;
407 if (*bytes_read
< size
)
409 LOG_ERROR("couldn't read enough bytes from "
410 "FT2232 device (%i < %i)",
411 (unsigned)*bytes_read
,
413 return ERROR_JTAG_DEVICE_ERROR
;
419 static bool ft2232_device_is_highspeed(void)
421 #if BUILD_FT2232_FTD2XX == 1
422 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
423 #elif BUILD_FT2232_LIBFTDI == 1
424 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
429 * Commands that only apply to the FT2232H and FT4232H devices.
430 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
431 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
434 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
436 uint8_t buf
= enable
? 0x96 : 0x97;
437 LOG_DEBUG("%2.2x", buf
);
439 uint32_t bytes_written
;
440 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
441 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
443 LOG_ERROR("couldn't write command to %s adaptive clocking"
444 , enable
? "enable" : "disable");
452 * Enable/disable the clk divide by 5 of the 60MHz master clock.
453 * This result in a JTAG clock speed range of 91.553Hz-6MHz
454 * respective 457.763Hz-30MHz.
456 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
458 uint32_t bytes_written
;
459 uint8_t buf
= enable
? 0x8b : 0x8a;
460 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
461 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
463 LOG_ERROR("couldn't write command to %s clk divide by 5"
464 , enable
? "enable" : "disable");
465 return ERROR_JTAG_INIT_FAILED
;
467 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
468 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
473 static int ft2232_speed(int speed
)
477 uint32_t bytes_written
;
480 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
481 if (ft2232_device_is_highspeed())
482 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
483 else if (enable_adaptive_clocking
)
485 LOG_ERROR("ft2232 device %lu does not support RTCK"
486 , (long unsigned int)ftdi_device
);
490 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
493 buf
[0] = 0x86; /* command "set divisor" */
494 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
495 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
497 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
498 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
500 LOG_ERROR("couldn't set FT2232 TCK speed");
507 static int ft2232_speed_div(int speed
, int* khz
)
509 /* Take a look in the FT2232 manual,
510 * AN2232C-01 Command Processor for
511 * MPSSE and MCU Host Bus. Chapter 3.8 */
513 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
518 static int ft2232_khz(int khz
, int* jtag_speed
)
522 if (ft2232_device_is_highspeed())
524 *jtag_speed
= RTCK_SPEED
;
529 LOG_DEBUG("RCLK not supported");
534 /* Take a look in the FT2232 manual,
535 * AN2232C-01 Command Processor for
536 * MPSSE and MCU Host Bus. Chapter 3.8
538 * We will calc here with a multiplier
539 * of 10 for better rounding later. */
541 /* Calc speed, (ft2232_max_tck / khz) - 1 */
542 /* Use 65000 for better rounding */
543 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
545 /* Add 0.9 for rounding */
548 /* Calc real speed */
549 *jtag_speed
= *jtag_speed
/ 10;
551 /* Check if speed is greater than 0 */
557 /* Check max value */
558 if (*jtag_speed
> 0xFFFF)
560 *jtag_speed
= 0xFFFF;
566 static void ft2232_end_state(tap_state_t state
)
568 if (tap_is_state_stable(state
))
569 tap_set_end_state(state
);
572 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
577 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
579 int num_bytes
= (scan_size
+ 7) / 8;
580 int bits_left
= scan_size
;
583 while (num_bytes
-- > 1)
585 buffer
[cur_byte
++] = buffer_read();
589 buffer
[cur_byte
] = 0x0;
591 /* There is one more partial byte left from the clock data in/out instructions */
594 buffer
[cur_byte
] = buffer_read() >> 1;
596 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
597 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
600 static void ft2232_debug_dump_buffer(void)
606 for (i
= 0; i
< ft2232_buffer_size
; i
++)
608 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
611 LOG_DEBUG("%s", line
);
617 LOG_DEBUG("%s", line
);
620 static int ft2232_send_and_recv(struct jtag_command
* first
, struct jtag_command
* last
)
622 struct jtag_command
* cmd
;
627 uint32_t bytes_written
= 0;
628 uint32_t bytes_read
= 0;
630 #ifdef _DEBUG_USB_IO_
631 struct timeval start
, inter
, inter2
, end
;
632 struct timeval d_inter
, d_inter2
, d_end
;
635 #ifdef _DEBUG_USB_COMMS_
636 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
637 ft2232_debug_dump_buffer();
640 #ifdef _DEBUG_USB_IO_
641 gettimeofday(&start
, NULL
);
644 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
646 LOG_ERROR("couldn't write MPSSE commands to FT2232");
650 #ifdef _DEBUG_USB_IO_
651 gettimeofday(&inter
, NULL
);
654 if (ft2232_expect_read
)
656 /* FIXME this "timeout" is never changed ... */
657 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
658 ft2232_buffer_size
= 0;
660 #ifdef _DEBUG_USB_IO_
661 gettimeofday(&inter2
, NULL
);
664 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
666 LOG_ERROR("couldn't read from FT2232");
670 #ifdef _DEBUG_USB_IO_
671 gettimeofday(&end
, NULL
);
673 timeval_subtract(&d_inter
, &inter
, &start
);
674 timeval_subtract(&d_inter2
, &inter2
, &start
);
675 timeval_subtract(&d_end
, &end
, &start
);
677 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
678 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
679 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
680 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
683 ft2232_buffer_size
= bytes_read
;
685 if (ft2232_expect_read
!= ft2232_buffer_size
)
687 LOG_ERROR("ft2232_expect_read (%i) != "
688 "ft2232_buffer_size (%i) "
692 LIBFTDI_READ_RETRY_COUNT
- timeout
);
693 ft2232_debug_dump_buffer();
698 #ifdef _DEBUG_USB_COMMS_
699 LOG_DEBUG("read buffer (%i retries): %i bytes",
700 LIBFTDI_READ_RETRY_COUNT
- timeout
,
702 ft2232_debug_dump_buffer();
706 ft2232_expect_read
= 0;
707 ft2232_read_pointer
= 0;
709 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
710 * that wasn't handled by a caller-provided error handler
720 type
= jtag_scan_type(cmd
->cmd
.scan
);
721 if (type
!= SCAN_OUT
)
723 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
724 buffer
= calloc(DIV_ROUND_UP(scan_size
, 8), 1);
725 ft2232_read_scan(type
, buffer
, scan_size
);
726 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
727 retval
= ERROR_JTAG_QUEUE_FAILED
;
739 ft2232_buffer_size
= 0;
745 * Function ft2232_add_pathmove
746 * moves the TAP controller from the current state to a new state through the
747 * given path, where path is an array of tap_state_t's.
749 * @param path is an array of tap_stat_t which gives the states to traverse through
750 * ending with the last state at path[num_states-1]
751 * @param num_states is the count of state steps to move through
753 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
757 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
761 /* this loop verifies that the path is legal and logs each state in the path */
764 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
766 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
768 /* command "Clock Data to TMS/CS Pin (no Read)" */
771 /* number of states remaining */
772 buffer_write(num_states_batch
- 1);
774 while (num_states_batch
--) {
775 /* either TMS=0 or TMS=1 must work ... */
776 if (tap_state_transition(tap_get_state(), false)
777 == path
[state_count
])
778 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
779 else if (tap_state_transition(tap_get_state(), true)
780 == path
[state_count
])
781 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
783 /* ... or else the caller goofed BADLY */
785 LOG_ERROR("BUG: %s -> %s isn't a valid "
786 "TAP state transition",
787 tap_state_name(tap_get_state()),
788 tap_state_name(path
[state_count
]));
792 tap_set_state(path
[state_count
]);
797 buffer_write(tms_byte
);
799 tap_set_end_state(tap_get_state());
802 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
804 int num_bytes
= (scan_size
+ 7) / 8;
805 int bits_left
= scan_size
;
811 if (tap_get_state() != TAP_DRSHIFT
)
813 move_to_state(TAP_DRSHIFT
);
818 if (tap_get_state() != TAP_IRSHIFT
)
820 move_to_state(TAP_IRSHIFT
);
824 /* add command for complete bytes */
825 while (num_bytes
> 1)
830 /* Clock Data Bytes In and Out LSB First */
832 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
834 else if (type
== SCAN_OUT
)
836 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
838 /* LOG_DEBUG("added TDI bytes (o)"); */
840 else if (type
== SCAN_IN
)
842 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
844 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
847 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
848 num_bytes
-= thisrun_bytes
;
850 buffer_write((uint8_t) (thisrun_bytes
- 1));
851 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
855 /* add complete bytes */
856 while (thisrun_bytes
-- > 0)
858 buffer_write(buffer
[cur_byte
++]);
862 else /* (type == SCAN_IN) */
864 bits_left
-= 8 * (thisrun_bytes
);
868 /* the most signifcant bit is scanned during TAP movement */
870 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
874 /* process remaining bits but the last one */
879 /* Clock Data Bits In and Out LSB First */
881 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
883 else if (type
== SCAN_OUT
)
885 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
887 /* LOG_DEBUG("added TDI bits (o)"); */
889 else if (type
== SCAN_IN
)
891 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
893 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
896 buffer_write(bits_left
- 2);
898 buffer_write(buffer
[cur_byte
]);
901 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
902 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
906 /* Clock Data Bits In and Out LSB First */
908 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
910 else if (type
== SCAN_OUT
)
912 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
914 /* LOG_DEBUG("added TDI bits (o)"); */
916 else if (type
== SCAN_IN
)
918 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
920 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
923 buffer_write(last_bit
);
931 /* move from Shift-IR/DR to end state */
932 if (type
!= SCAN_OUT
)
934 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
935 /* This must be coordinated with the bit shifts in ft2232_read_scan */
938 /* Clock Data to TMS/CS Pin with Read */
943 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
944 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
945 /* Clock Data to TMS/CS Pin (no Read) */
949 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
950 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
953 if (tap_get_state() != tap_get_end_state())
955 move_to_state(tap_get_end_state());
959 static int ft2232_large_scan(struct scan_command
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
961 int num_bytes
= (scan_size
+ 7) / 8;
962 int bits_left
= scan_size
;
965 uint8_t* receive_buffer
= malloc(DIV_ROUND_UP(scan_size
, 8));
966 uint8_t* receive_pointer
= receive_buffer
;
967 uint32_t bytes_written
;
970 int thisrun_read
= 0;
974 LOG_ERROR("BUG: large IR scans are not supported");
978 if (tap_get_state() != TAP_DRSHIFT
)
980 move_to_state(TAP_DRSHIFT
);
983 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
985 LOG_ERROR("couldn't write MPSSE commands to FT2232");
988 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
989 ft2232_buffer_size
, (int)bytes_written
);
990 ft2232_buffer_size
= 0;
992 /* add command for complete bytes */
993 while (num_bytes
> 1)
999 /* Clock Data Bytes In and Out LSB First */
1001 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1003 else if (type
== SCAN_OUT
)
1005 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1007 /* LOG_DEBUG("added TDI bytes (o)"); */
1009 else if (type
== SCAN_IN
)
1011 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1013 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1016 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1017 thisrun_read
= thisrun_bytes
;
1018 num_bytes
-= thisrun_bytes
;
1019 buffer_write((uint8_t) (thisrun_bytes
- 1));
1020 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1022 if (type
!= SCAN_IN
)
1024 /* add complete bytes */
1025 while (thisrun_bytes
-- > 0)
1027 buffer_write(buffer
[cur_byte
]);
1032 else /* (type == SCAN_IN) */
1034 bits_left
-= 8 * (thisrun_bytes
);
1037 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1039 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1042 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1044 (int)bytes_written
);
1045 ft2232_buffer_size
= 0;
1047 if (type
!= SCAN_OUT
)
1049 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1051 LOG_ERROR("couldn't read from FT2232");
1054 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1057 receive_pointer
+= bytes_read
;
1063 /* the most signifcant bit is scanned during TAP movement */
1064 if (type
!= SCAN_IN
)
1065 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1069 /* process remaining bits but the last one */
1072 if (type
== SCAN_IO
)
1074 /* Clock Data Bits In and Out LSB First */
1076 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1078 else if (type
== SCAN_OUT
)
1080 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1082 /* LOG_DEBUG("added TDI bits (o)"); */
1084 else if (type
== SCAN_IN
)
1086 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1088 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1090 buffer_write(bits_left
- 2);
1091 if (type
!= SCAN_IN
)
1092 buffer_write(buffer
[cur_byte
]);
1094 if (type
!= SCAN_OUT
)
1098 if (tap_get_end_state() == TAP_DRSHIFT
)
1100 if (type
== SCAN_IO
)
1102 /* Clock Data Bits In and Out LSB First */
1104 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1106 else if (type
== SCAN_OUT
)
1108 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1110 /* LOG_DEBUG("added TDI bits (o)"); */
1112 else if (type
== SCAN_IN
)
1114 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1116 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1119 buffer_write(last_bit
);
1123 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1124 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1127 /* move from Shift-IR/DR to end state */
1128 if (type
!= SCAN_OUT
)
1130 /* Clock Data to TMS/CS Pin with Read */
1132 /* LOG_DEBUG("added TMS scan (read)"); */
1136 /* Clock Data to TMS/CS Pin (no Read) */
1138 /* LOG_DEBUG("added TMS scan (no read)"); */
1141 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1142 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1145 if (type
!= SCAN_OUT
)
1148 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1150 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1153 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1155 (int)bytes_written
);
1156 ft2232_buffer_size
= 0;
1158 if (type
!= SCAN_OUT
)
1160 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1162 LOG_ERROR("couldn't read from FT2232");
1165 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1168 receive_pointer
+= bytes_read
;
1174 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1176 int predicted_size
= 3;
1177 int num_bytes
= (scan_size
- 1) / 8;
1179 if (tap_get_state() != TAP_DRSHIFT
)
1180 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1182 if (type
== SCAN_IN
) /* only from device to host */
1184 /* complete bytes */
1185 predicted_size
+= DIV_ROUND_UP(num_bytes
, 65536) * 3;
1187 /* remaining bits - 1 (up to 7) */
1188 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1190 else /* host to device, or bidirectional */
1192 /* complete bytes */
1193 predicted_size
+= num_bytes
+ DIV_ROUND_UP(num_bytes
, 65536) * 3;
1195 /* remaining bits -1 (up to 7) */
1196 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1199 return predicted_size
;
1202 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1204 int predicted_size
= 0;
1206 if (type
!= SCAN_OUT
)
1208 /* complete bytes */
1209 predicted_size
+= (DIV_ROUND_UP(scan_size
, 8) > 1) ? (DIV_ROUND_UP(scan_size
, 8) - 1) : 0;
1211 /* remaining bits - 1 */
1212 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1214 /* last bit (from TMS scan) */
1215 predicted_size
+= 1;
1218 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1220 return predicted_size
;
1223 static void usbjtag_reset(int trst
, int srst
)
1225 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1228 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1229 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1231 low_output
&= ~nTRST
; /* switch output low */
1235 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1236 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1238 low_output
|= nTRST
; /* switch output high */
1243 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1244 low_output
&= ~nSRST
; /* switch output low */
1246 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1250 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1251 low_output
|= nSRST
; /* switch output high */
1253 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1256 /* command "set data bits low byte" */
1258 buffer_write(low_output
);
1259 buffer_write(low_direction
);
1262 static void jtagkey_reset(int trst
, int srst
)
1264 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1267 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1268 high_output
&= ~nTRSTnOE
;
1270 high_output
&= ~nTRST
;
1274 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1275 high_output
|= nTRSTnOE
;
1277 high_output
|= nTRST
;
1282 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1283 high_output
&= ~nSRST
;
1285 high_output
&= ~nSRSTnOE
;
1289 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1290 high_output
|= nSRST
;
1292 high_output
|= nSRSTnOE
;
1295 /* command "set data bits high byte" */
1297 buffer_write(high_output
);
1298 buffer_write(high_direction
);
1299 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1303 static void olimex_jtag_reset(int trst
, int srst
)
1305 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1308 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1309 high_output
&= ~nTRSTnOE
;
1311 high_output
&= ~nTRST
;
1315 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1316 high_output
|= nTRSTnOE
;
1318 high_output
|= nTRST
;
1323 high_output
|= nSRST
;
1327 high_output
&= ~nSRST
;
1330 /* command "set data bits high byte" */
1332 buffer_write(high_output
);
1333 buffer_write(high_direction
);
1334 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1338 static void axm0432_jtag_reset(int trst
, int srst
)
1342 tap_set_state(TAP_RESET
);
1343 high_output
&= ~nTRST
;
1347 high_output
|= nTRST
;
1352 high_output
&= ~nSRST
;
1356 high_output
|= nSRST
;
1359 /* command "set data bits low byte" */
1361 buffer_write(high_output
);
1362 buffer_write(high_direction
);
1363 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1367 static void flyswatter_reset(int trst
, int srst
)
1371 low_output
&= ~nTRST
;
1375 low_output
|= nTRST
;
1380 low_output
|= nSRST
;
1384 low_output
&= ~nSRST
;
1387 /* command "set data bits low byte" */
1389 buffer_write(low_output
);
1390 buffer_write(low_direction
);
1391 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1394 static void turtle_reset(int trst
, int srst
)
1400 low_output
|= nSRST
;
1404 low_output
&= ~nSRST
;
1407 /* command "set data bits low byte" */
1409 buffer_write(low_output
);
1410 buffer_write(low_direction
);
1411 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1414 static void comstick_reset(int trst
, int srst
)
1418 high_output
&= ~nTRST
;
1422 high_output
|= nTRST
;
1427 high_output
&= ~nSRST
;
1431 high_output
|= nSRST
;
1434 /* command "set data bits high byte" */
1436 buffer_write(high_output
);
1437 buffer_write(high_direction
);
1438 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1442 static void stm32stick_reset(int trst
, int srst
)
1446 high_output
&= ~nTRST
;
1450 high_output
|= nTRST
;
1455 low_output
&= ~nSRST
;
1459 low_output
|= nSRST
;
1462 /* command "set data bits low byte" */
1464 buffer_write(low_output
);
1465 buffer_write(low_direction
);
1467 /* command "set data bits high 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 sheevaplug_reset(int trst
, int srst
)
1478 high_output
&= ~nTRST
;
1480 high_output
|= nTRST
;
1483 high_output
&= ~nSRSTnOE
;
1485 high_output
|= nSRSTnOE
;
1487 /* command "set data bits high byte" */
1489 buffer_write(high_output
);
1490 buffer_write(high_direction
);
1491 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1494 static int ft2232_execute_runtest(struct jtag_command
*cmd
)
1498 int predicted_size
= 0;
1501 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1502 cmd
->cmd
.runtest
->num_cycles
,
1503 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1505 /* only send the maximum buffer size that FT2232C can handle */
1507 if (tap_get_state() != TAP_IDLE
)
1508 predicted_size
+= 3;
1509 predicted_size
+= 3 * DIV_ROUND_UP(cmd
->cmd
.runtest
->num_cycles
, 7);
1510 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1511 predicted_size
+= 3;
1512 if (tap_get_end_state() != TAP_IDLE
)
1513 predicted_size
+= 3;
1514 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1516 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1517 retval
= ERROR_JTAG_QUEUE_FAILED
;
1521 if (tap_get_state() != TAP_IDLE
)
1523 move_to_state(TAP_IDLE
);
1526 i
= cmd
->cmd
.runtest
->num_cycles
;
1529 /* there are no state transitions in this code, so omit state tracking */
1531 /* command "Clock Data to TMS/CS Pin (no Read)" */
1535 buffer_write((i
> 7) ? 6 : (i
- 1));
1540 i
-= (i
> 7) ? 7 : i
;
1541 /* LOG_DEBUG("added TMS scan (no read)"); */
1544 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1546 if (tap_get_state() != tap_get_end_state())
1548 move_to_state(tap_get_end_state());
1552 DEBUG_JTAG_IO("runtest: %i, end in %s",
1553 cmd
->cmd
.runtest
->num_cycles
,
1554 tap_state_name(tap_get_end_state()));
1558 static int ft2232_execute_statemove(struct jtag_command
*cmd
)
1560 int predicted_size
= 0;
1561 int retval
= ERROR_OK
;
1563 DEBUG_JTAG_IO("statemove end in %s",
1564 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1566 /* only send the maximum buffer size that FT2232C can handle */
1568 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1570 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1571 retval
= ERROR_JTAG_QUEUE_FAILED
;
1575 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1577 /* For TAP_RESET, ignore the current recorded state. It's often
1578 * wrong at server startup, and this transation is critical whenever
1581 if (tap_get_end_state() == TAP_RESET
) {
1582 clock_tms(0x4b, 0xff, 5, 0);
1585 /* shortest-path move to desired end state */
1586 } else if (tap_get_state() != tap_get_end_state())
1588 move_to_state(tap_get_end_state());
1595 static int ft2232_execute_pathmove(struct jtag_command
*cmd
)
1597 int predicted_size
= 0;
1598 int retval
= ERROR_OK
;
1600 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1601 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1603 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1604 tap_state_name(tap_get_state()),
1605 tap_state_name(path
[num_states
-1]));
1607 /* only send the maximum buffer size that FT2232C can handle */
1608 predicted_size
= 3 * DIV_ROUND_UP(num_states
, 7);
1609 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1611 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1612 retval
= ERROR_JTAG_QUEUE_FAILED
;
1618 ft2232_add_pathmove(path
, num_states
);
1624 static int ft2232_execute_scan(struct jtag_command
*cmd
)
1627 int scan_size
; /* size of IR or DR scan */
1628 int predicted_size
= 0;
1629 int retval
= ERROR_OK
;
1631 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1633 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1635 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1637 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1638 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1640 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1641 /* unsent commands before this */
1642 if (first_unsent
!= cmd
)
1643 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1644 retval
= ERROR_JTAG_QUEUE_FAILED
;
1646 /* current command */
1647 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1648 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1650 first_unsent
= cmd
->next
;
1655 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1657 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1660 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1661 retval
= ERROR_JTAG_QUEUE_FAILED
;
1665 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1666 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1667 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1668 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1672 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1673 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1674 tap_state_name(tap_get_end_state()));
1679 static int ft2232_execute_reset(struct jtag_command
*cmd
)
1682 int predicted_size
= 0;
1685 DEBUG_JTAG_IO("reset trst: %i srst %i",
1686 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1688 /* only send the maximum buffer size that FT2232C can handle */
1690 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1692 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1693 retval
= ERROR_JTAG_QUEUE_FAILED
;
1698 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1700 tap_set_state(TAP_RESET
);
1703 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1706 DEBUG_JTAG_IO("trst: %i, srst: %i",
1707 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1711 static int ft2232_execute_sleep(struct jtag_command
*cmd
)
1716 DEBUG_JTAG_IO("sleep %" PRIi32
, cmd
->cmd
.sleep
->us
);
1718 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1719 retval
= ERROR_JTAG_QUEUE_FAILED
;
1720 first_unsent
= cmd
->next
;
1721 jtag_sleep(cmd
->cmd
.sleep
->us
);
1722 DEBUG_JTAG_IO("sleep %" PRIi32
" usec while in %s",
1724 tap_state_name(tap_get_state()));
1728 static int ft2232_execute_stableclocks(struct jtag_command
*cmd
)
1733 /* this is only allowed while in a stable state. A check for a stable
1734 * state was done in jtag_add_clocks()
1736 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1737 retval
= ERROR_JTAG_QUEUE_FAILED
;
1738 DEBUG_JTAG_IO("clocks %i while in %s",
1739 cmd
->cmd
.stableclocks
->num_cycles
,
1740 tap_state_name(tap_get_state()));
1744 static int ft2232_execute_command(struct jtag_command
*cmd
)
1751 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1752 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1753 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1754 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1755 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1756 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1757 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1759 LOG_ERROR("BUG: unknown JTAG command type encountered");
1765 static int ft2232_execute_queue(void)
1767 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
1770 first_unsent
= cmd
; /* next command that has to be sent */
1773 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1774 * that wasn't handled by a caller-provided error handler
1778 ft2232_buffer_size
= 0;
1779 ft2232_expect_read
= 0;
1781 /* blink, if the current layout has that feature */
1787 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1788 retval
= ERROR_JTAG_QUEUE_FAILED
;
1789 /* Start reading input before FT2232 TX buffer fills up */
1791 if (ft2232_expect_read
> 256)
1793 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1794 retval
= ERROR_JTAG_QUEUE_FAILED
;
1799 if (require_send
> 0)
1800 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1801 retval
= ERROR_JTAG_QUEUE_FAILED
;
1806 #if BUILD_FT2232_FTD2XX == 1
1807 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1811 char SerialNumber
[16];
1812 char Description
[64];
1813 DWORD openex_flags
= 0;
1814 char* openex_string
= NULL
;
1815 uint8_t latency_timer
;
1817 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
1820 /* Add non-standard Vid/Pid to the linux driver */
1821 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1823 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
1827 if (ft2232_device_desc
&& ft2232_serial
)
1829 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1830 ft2232_device_desc
= NULL
;
1833 if (ft2232_device_desc
)
1835 openex_string
= ft2232_device_desc
;
1836 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1838 else if (ft2232_serial
)
1840 openex_string
= ft2232_serial
;
1841 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1845 LOG_ERROR("neither device description nor serial number specified");
1846 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1848 return ERROR_JTAG_INIT_FAILED
;
1851 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1852 if (status
!= FT_OK
) {
1853 /* under Win32, the FTD2XX driver appends an "A" to the end
1854 * of the description, if we tried by the desc, then
1855 * try by the alternate "A" description. */
1856 if (openex_string
== ft2232_device_desc
) {
1857 /* Try the alternate method. */
1858 openex_string
= ft2232_device_desc_A
;
1859 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1860 if (status
== FT_OK
) {
1861 /* yea, the "alternate" method worked! */
1863 /* drat, give the user a meaningfull message.
1864 * telling the use we tried *BOTH* methods. */
1865 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1867 ft2232_device_desc_A
);
1872 if (status
!= FT_OK
)
1878 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
1880 return ERROR_JTAG_INIT_FAILED
;
1882 LOG_ERROR("unable to open ftdi device: %lu", status
);
1883 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1884 if (status
== FT_OK
)
1886 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1889 for (i
= 0; i
< num_devices
; i
++)
1890 desc_array
[i
] = malloc(64);
1892 desc_array
[num_devices
] = NULL
;
1894 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1896 if (status
== FT_OK
)
1898 LOG_ERROR("ListDevices: %lu\n", num_devices
);
1899 for (i
= 0; i
< num_devices
; i
++)
1900 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
1903 for (i
= 0; i
< num_devices
; i
++)
1904 free(desc_array
[i
]);
1910 LOG_ERROR("ListDevices: NONE\n");
1912 return ERROR_JTAG_INIT_FAILED
;
1915 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1917 LOG_ERROR("unable to set latency timer: %lu", status
);
1918 return ERROR_JTAG_INIT_FAILED
;
1921 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1923 LOG_ERROR("unable to get latency timer: %lu", status
);
1924 return ERROR_JTAG_INIT_FAILED
;
1928 LOG_DEBUG("current latency timer: %i", latency_timer
);
1931 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1933 LOG_ERROR("unable to set timeouts: %lu", status
);
1934 return ERROR_JTAG_INIT_FAILED
;
1937 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1939 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
1940 return ERROR_JTAG_INIT_FAILED
;
1943 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
1945 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
1946 return ERROR_JTAG_INIT_FAILED
;
1950 static const char* type_str
[] =
1951 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1952 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
1953 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
1954 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
1955 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
1956 LOG_INFO("deviceID: %lu", deviceID
);
1957 LOG_INFO("SerialNumber: %s", SerialNumber
);
1958 LOG_INFO("Description: %s", Description
);
1964 static int ft2232_purge_ftd2xx(void)
1968 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1970 LOG_ERROR("error purging ftd2xx device: %lu", status
);
1971 return ERROR_JTAG_INIT_FAILED
;
1977 #endif /* BUILD_FT2232_FTD2XX == 1 */
1979 #if BUILD_FT2232_LIBFTDI == 1
1980 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1982 uint8_t latency_timer
;
1984 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1985 ft2232_layout
, vid
, pid
);
1987 if (ftdi_init(&ftdic
) < 0)
1988 return ERROR_JTAG_INIT_FAILED
;
1990 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
1992 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
1993 return ERROR_JTAG_INIT_FAILED
;
1996 /* context, vendor id, product id */
1997 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2001 LOG_WARNING("unable to open ftdi device (trying more): %s",
2004 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2006 return ERROR_JTAG_INIT_FAILED
;
2009 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2010 if (ftdi_usb_reset(&ftdic
) < 0)
2012 LOG_ERROR("unable to reset ftdi device");
2013 return ERROR_JTAG_INIT_FAILED
;
2016 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2018 LOG_ERROR("unable to set latency timer");
2019 return ERROR_JTAG_INIT_FAILED
;
2022 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2024 LOG_ERROR("unable to get latency timer");
2025 return ERROR_JTAG_INIT_FAILED
;
2029 LOG_DEBUG("current latency timer: %i", latency_timer
);
2032 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2034 ftdi_device
= ftdic
.type
;
2035 static const char* type_str
[] =
2036 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2037 unsigned no_of_known_types
= ARRAY_SIZE(type_str
) - 1;
2038 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2039 ? ftdi_device
: no_of_known_types
;
2040 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2044 static int ft2232_purge_libftdi(void)
2046 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2048 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2049 return ERROR_JTAG_INIT_FAILED
;
2055 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2057 static int ft2232_init(void)
2061 uint32_t bytes_written
;
2062 const struct ft2232_layout
* cur_layout
= ft2232_layouts
;
2065 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2067 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2071 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2074 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2076 ft2232_layout
= "usbjtag";
2077 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2080 while (cur_layout
->name
)
2082 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2084 layout
= cur_layout
;
2092 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2093 return ERROR_JTAG_INIT_FAILED
;
2099 * "more indicates that there are more IDs to try, so we should
2100 * not print an error for an ID mismatch (but for anything
2103 * try_more indicates that the error code returned indicates an
2104 * ID mismatch (and nothing else) and that we should proceeed
2105 * with the next ID pair.
2107 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2110 #if BUILD_FT2232_FTD2XX == 1
2111 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2113 #elif BUILD_FT2232_LIBFTDI == 1
2114 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2119 if (!more
|| !try_more
)
2123 ft2232_buffer_size
= 0;
2124 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2126 if (layout
->init() != ERROR_OK
)
2127 return ERROR_JTAG_INIT_FAILED
;
2129 if (ft2232_device_is_highspeed())
2131 #ifndef BUILD_FT2232_HIGHSPEED
2132 #if BUILD_FT2232_FTD2XX == 1
2133 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2134 #elif BUILD_FT2232_LIBFTDI == 1
2135 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2138 /* make sure the legacy mode is disabled */
2139 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2140 return ERROR_JTAG_INIT_FAILED
;
2143 ft2232_speed(jtag_get_speed());
2145 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2146 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2148 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2149 return ERROR_JTAG_INIT_FAILED
;
2152 #if BUILD_FT2232_FTD2XX == 1
2153 return ft2232_purge_ftd2xx();
2154 #elif BUILD_FT2232_LIBFTDI == 1
2155 return ft2232_purge_libftdi();
2161 static int usbjtag_init(void)
2164 uint32_t bytes_written
;
2167 low_direction
= 0x0b;
2169 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2176 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2183 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2185 /* There are multiple revisions of LM3S811 eval boards:
2186 * - Rev B (and older?) boards have no SWO trace support.
2187 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2188 * they should use the "luminary_icdi" layout instead.
2195 low_direction
= 0x8b;
2197 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2199 /* Most Luminary eval boards support SWO trace output,
2200 * and should use this "luminary_icdi" layout.
2207 low_direction
= 0xcb;
2211 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2212 return ERROR_JTAG_INIT_FAILED
;
2215 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2216 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2218 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2219 low_output
&= ~nTRST
; /* nTRST = 0 */
2223 low_direction
|= nTRSTnOE
; /* nTRST output */
2224 low_output
|= nTRST
; /* nTRST = 1 */
2227 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2229 low_direction
|= nSRSTnOE
; /* nSRST output */
2230 low_output
|= nSRST
; /* nSRST = 1 */
2234 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2235 low_output
&= ~nSRST
; /* nSRST = 0 */
2238 /* initialize low byte for jtag */
2239 buf
[0] = 0x80; /* command "set data bits low byte" */
2240 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2241 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2242 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2244 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2246 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2247 return ERROR_JTAG_INIT_FAILED
;
2253 static int axm0432_jtag_init(void)
2256 uint32_t bytes_written
;
2259 low_direction
= 0x2b;
2261 /* initialize low byte for jtag */
2262 buf
[0] = 0x80; /* command "set data bits low byte" */
2263 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2264 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2265 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2267 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2269 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2270 return ERROR_JTAG_INIT_FAILED
;
2273 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2276 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2278 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2282 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2287 high_direction
= 0x0c;
2289 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2290 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2292 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2296 high_output
|= nTRST
;
2299 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2301 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2305 high_output
|= nSRST
;
2308 /* initialize high port */
2309 buf
[0] = 0x82; /* command "set data bits high byte" */
2310 buf
[1] = high_output
; /* value */
2311 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2312 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2314 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2316 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2317 return ERROR_JTAG_INIT_FAILED
;
2323 static int jtagkey_init(void)
2326 uint32_t bytes_written
;
2329 low_direction
= 0x1b;
2331 /* initialize low byte for jtag */
2332 buf
[0] = 0x80; /* command "set data bits low byte" */
2333 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2334 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2335 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2337 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2339 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2340 return ERROR_JTAG_INIT_FAILED
;
2343 if (strcmp(layout
->name
, "jtagkey") == 0)
2350 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2351 || (strcmp(layout
->name
, "oocdlink") == 0))
2360 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2365 high_direction
= 0x0f;
2367 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2368 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2370 high_output
|= nTRSTnOE
;
2371 high_output
&= ~nTRST
;
2375 high_output
&= ~nTRSTnOE
;
2376 high_output
|= nTRST
;
2379 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2381 high_output
&= ~nSRSTnOE
;
2382 high_output
|= nSRST
;
2386 high_output
|= nSRSTnOE
;
2387 high_output
&= ~nSRST
;
2390 /* initialize high port */
2391 buf
[0] = 0x82; /* command "set data bits high byte" */
2392 buf
[1] = high_output
; /* value */
2393 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2394 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2396 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2398 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2399 return ERROR_JTAG_INIT_FAILED
;
2405 static int olimex_jtag_init(void)
2408 uint32_t bytes_written
;
2411 low_direction
= 0x1b;
2413 /* initialize low byte for jtag */
2414 buf
[0] = 0x80; /* command "set data bits low byte" */
2415 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2416 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2417 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2419 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2421 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2422 return ERROR_JTAG_INIT_FAILED
;
2428 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2431 high_direction
= 0x0f;
2433 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2434 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2436 high_output
|= nTRSTnOE
;
2437 high_output
&= ~nTRST
;
2441 high_output
&= ~nTRSTnOE
;
2442 high_output
|= nTRST
;
2445 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2447 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2451 high_output
&= ~nSRST
;
2454 /* turn red LED on */
2455 high_output
|= 0x08;
2457 /* initialize high port */
2458 buf
[0] = 0x82; /* command "set data bits high byte" */
2459 buf
[1] = high_output
; /* value */
2460 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2461 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2463 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2465 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2466 return ERROR_JTAG_INIT_FAILED
;
2472 static int flyswatter_init(void)
2475 uint32_t bytes_written
;
2478 low_direction
= 0xfb;
2480 /* initialize low byte for jtag */
2481 buf
[0] = 0x80; /* command "set data bits low byte" */
2482 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2483 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2484 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2486 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2488 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2489 return ERROR_JTAG_INIT_FAILED
;
2493 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2495 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2498 high_direction
= 0x0c;
2500 /* turn red LED3 on, LED2 off */
2501 high_output
|= 0x08;
2503 /* initialize high port */
2504 buf
[0] = 0x82; /* command "set data bits high byte" */
2505 buf
[1] = high_output
; /* value */
2506 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2507 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2509 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2511 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2512 return ERROR_JTAG_INIT_FAILED
;
2518 static int turtle_init(void)
2521 uint32_t bytes_written
;
2524 low_direction
= 0x5b;
2526 /* initialize low byte for jtag */
2527 buf
[0] = 0x80; /* command "set data bits low byte" */
2528 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2529 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2530 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2532 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2534 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2535 return ERROR_JTAG_INIT_FAILED
;
2541 high_direction
= 0x0C;
2543 /* initialize high port */
2544 buf
[0] = 0x82; /* command "set data bits high byte" */
2545 buf
[1] = high_output
;
2546 buf
[2] = high_direction
;
2547 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2549 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2551 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2552 return ERROR_JTAG_INIT_FAILED
;
2558 static int comstick_init(void)
2561 uint32_t bytes_written
;
2564 low_direction
= 0x0b;
2566 /* initialize low byte for jtag */
2567 buf
[0] = 0x80; /* command "set data bits low byte" */
2568 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2569 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2570 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2572 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2574 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2575 return ERROR_JTAG_INIT_FAILED
;
2579 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2581 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2584 high_direction
= 0x03;
2586 /* initialize high port */
2587 buf
[0] = 0x82; /* command "set data bits high byte" */
2588 buf
[1] = high_output
;
2589 buf
[2] = high_direction
;
2590 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2592 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2594 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2595 return ERROR_JTAG_INIT_FAILED
;
2601 static int stm32stick_init(void)
2604 uint32_t bytes_written
;
2607 low_direction
= 0x8b;
2609 /* initialize low byte for jtag */
2610 buf
[0] = 0x80; /* command "set data bits low byte" */
2611 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2612 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2613 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2615 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2617 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2618 return ERROR_JTAG_INIT_FAILED
;
2622 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2624 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2627 high_direction
= 0x03;
2629 /* initialize high port */
2630 buf
[0] = 0x82; /* command "set data bits high byte" */
2631 buf
[1] = high_output
;
2632 buf
[2] = high_direction
;
2633 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2635 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2637 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2638 return ERROR_JTAG_INIT_FAILED
;
2644 static int sheevaplug_init(void)
2647 uint32_t bytes_written
;
2650 low_direction
= 0x1b;
2652 /* initialize low byte for jtag */
2653 buf
[0] = 0x80; /* command "set data bits low byte" */
2654 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2655 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2656 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2658 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2660 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2661 return ERROR_JTAG_INIT_FAILED
;
2670 high_direction
= 0x0f;
2672 /* nTRST is always push-pull */
2673 high_output
&= ~nTRSTnOE
;
2674 high_output
|= nTRST
;
2676 /* nSRST is always open-drain */
2677 high_output
|= nSRSTnOE
;
2678 high_output
&= ~nSRST
;
2680 /* initialize high port */
2681 buf
[0] = 0x82; /* command "set data bits high byte" */
2682 buf
[1] = high_output
; /* value */
2683 buf
[2] = high_direction
; /* all outputs - xRST */
2684 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2686 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2688 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2689 return ERROR_JTAG_INIT_FAILED
;
2695 static int cortino_jtag_init(void)
2698 uint32_t bytes_written
;
2701 low_direction
= 0x1b;
2703 /* initialize low byte for jtag */
2704 buf
[0] = 0x80; /* command "set data bits low byte" */
2705 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2706 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2707 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2709 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2711 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2712 return ERROR_JTAG_INIT_FAILED
;
2716 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2718 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2721 high_direction
= 0x03;
2723 /* initialize high port */
2724 buf
[0] = 0x82; /* command "set data bits high byte" */
2725 buf
[1] = high_output
;
2726 buf
[2] = high_direction
;
2727 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2729 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2731 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2732 return ERROR_JTAG_INIT_FAILED
;
2738 static void olimex_jtag_blink(void)
2740 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2741 * ACBUS3 is bit 3 of the GPIOH port
2743 if (high_output
& 0x08)
2745 /* set port pin high */
2746 high_output
&= 0x07;
2750 /* set port pin low */
2751 high_output
|= 0x08;
2755 buffer_write(high_output
);
2756 buffer_write(high_direction
);
2759 static void flyswatter_jtag_blink(void)
2762 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2764 high_output
^= 0x0c;
2767 buffer_write(high_output
);
2768 buffer_write(high_direction
);
2771 static void turtle_jtag_blink(void)
2774 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2776 if (high_output
& 0x08)
2786 buffer_write(high_output
);
2787 buffer_write(high_direction
);
2790 static int ft2232_quit(void)
2792 #if BUILD_FT2232_FTD2XX == 1
2795 status
= FT_Close(ftdih
);
2796 #elif BUILD_FT2232_LIBFTDI == 1
2797 ftdi_usb_close(&ftdic
);
2799 ftdi_deinit(&ftdic
);
2802 free(ft2232_buffer
);
2803 ft2232_buffer
= NULL
;
2808 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
2814 ft2232_device_desc
= strdup(CMD_ARGV
[0]);
2815 cp
= strchr(ft2232_device_desc
, 0);
2816 /* under Win32, the FTD2XX driver appends an "A" to the end
2817 * of the description, this examines the given desc
2818 * and creates the 'missing' _A or non_A variable. */
2819 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
2820 /* it was, so make this the "A" version. */
2821 ft2232_device_desc_A
= ft2232_device_desc
;
2822 /* and *CREATE* the non-A version. */
2823 strcpy(buf
, ft2232_device_desc
);
2824 cp
= strchr(buf
, 0);
2826 ft2232_device_desc
= strdup(buf
);
2828 /* <space > A not defined
2830 sprintf(buf
, "%s A", ft2232_device_desc
);
2831 ft2232_device_desc_A
= strdup(buf
);
2836 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2842 COMMAND_HANDLER(ft2232_handle_serial_command
)
2846 ft2232_serial
= strdup(CMD_ARGV
[0]);
2850 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2856 COMMAND_HANDLER(ft2232_handle_layout_command
)
2861 ft2232_layout
= malloc(strlen(CMD_ARGV
[0]) + 1);
2862 strcpy(ft2232_layout
, CMD_ARGV
[0]);
2867 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
2869 if (CMD_ARGC
> MAX_USB_IDS
* 2)
2871 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2872 "(maximum is %d pairs)", MAX_USB_IDS
);
2873 CMD_ARGC
= MAX_USB_IDS
* 2;
2875 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1))
2877 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2879 return ERROR_COMMAND_SYNTAX_ERROR
;
2880 /* remove the incomplete trailing id */
2885 for (i
= 0; i
< CMD_ARGC
; i
+= 2)
2887 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ft2232_vid
[i
>> 1]);
2888 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ft2232_pid
[i
>> 1]);
2892 * Explicitly terminate, in case there are multiples instances of
2895 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2900 COMMAND_HANDLER(ft2232_handle_latency_command
)
2904 ft2232_latency
= atoi(CMD_ARGV
[0]);
2908 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2914 static int ft2232_stableclocks(int num_cycles
, struct jtag_command
* cmd
)
2918 /* 7 bits of either ones or zeros. */
2919 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
2921 while (num_cycles
> 0)
2923 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2924 * at most 7 bits per invocation. Here we invoke it potentially
2927 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
2929 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
2931 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2932 retval
= ERROR_JTAG_QUEUE_FAILED
;
2937 /* there are no state transitions in this code, so omit state tracking */
2939 /* command "Clock Data to TMS/CS Pin (no Read)" */
2943 buffer_write(bitcount_per_command
- 1);
2945 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2950 num_cycles
-= bitcount_per_command
;
2956 /* ---------------------------------------------------------------------
2957 * Support for IceBear JTAG adapter from Section5:
2958 * http://section5.ch/icebear
2960 * Author: Sten, debian@sansys-electronic.com
2963 /* Icebear pin layout
2965 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2966 * GND GND | 4 3| n.c.
2967 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2968 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2969 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2970 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2971 * ADBUS2 TDO |14 13| GND GND
2973 * ADBUS0 O L TCK ACBUS0 GND
2974 * ADBUS1 O L TDI ACBUS1 GND
2975 * ADBUS2 I TDO ACBUS2 n.c.
2976 * ADBUS3 O H TMS ACBUS3 n.c.
2982 static int icebear_jtag_init(void) {
2984 uint32_t bytes_written
;
2986 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
2987 low_output
= 0x08; /* high: TMS; low: TCK TDI */
2991 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2992 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
2993 low_direction
&= ~nTRST
; /* nTRST high impedance */
2996 low_direction
|= nTRST
;
2997 low_output
|= nTRST
;
3000 low_direction
|= nSRST
;
3001 low_output
|= nSRST
;
3003 /* initialize low byte for jtag */
3004 buf
[0] = 0x80; /* command "set data bits low byte" */
3005 buf
[1] = low_output
;
3006 buf
[2] = low_direction
;
3007 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3009 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3010 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3011 return ERROR_JTAG_INIT_FAILED
;
3015 high_direction
= 0x00;
3018 /* initialize high port */
3019 buf
[0] = 0x82; /* command "set data bits high byte" */
3020 buf
[1] = high_output
; /* value */
3021 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3022 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3024 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3025 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3026 return ERROR_JTAG_INIT_FAILED
;
3032 static void icebear_jtag_reset(int trst
, int srst
) {
3035 low_direction
|= nTRST
;
3036 low_output
&= ~nTRST
;
3038 else if (trst
== 0) {
3039 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3040 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3041 low_direction
&= ~nTRST
;
3043 low_output
|= nTRST
;
3047 low_output
&= ~nSRST
;
3049 else if (srst
== 0) {
3050 low_output
|= nSRST
;
3053 /* command "set data bits low byte" */
3055 buffer_write(low_output
);
3056 buffer_write(low_direction
);
3058 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3061 /* ---------------------------------------------------------------------
3062 * Support for Signalyzer H2 and Signalyzer H4
3063 * JTAG adapter from Xverve Technologies Inc.
3064 * http://www.signalyzer.com or http://www.xverve.com
3066 * Author: Oleg Seiljus, oleg@signalyzer.com
3068 static unsigned char signalyzer_h_side
;
3069 static unsigned int signalyzer_h_adapter_type
;
3071 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3073 #if BUILD_FT2232_FTD2XX == 1
3074 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3077 #define SIGNALYZER_COMMAND_ADDR 128
3078 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3080 #define SIGNALYZER_COMMAND_VERSION 0x41
3081 #define SIGNALYZER_COMMAND_RESET 0x42
3082 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3083 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3084 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3085 #define SIGNALYZER_COMMAND_LED_SET 0x53
3086 #define SIGNALYZER_COMMAND_ADC 0x54
3087 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3088 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3089 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3090 #define SIGNALYZER_COMMAND_I2C 0x58
3092 #define SIGNALYZER_CHAN_A 1
3093 #define SIGNALYZER_CHAN_B 2
3094 /* LEDS use channel C */
3095 #define SIGNALYZER_CHAN_C 4
3097 #define SIGNALYZER_LED_GREEN 1
3098 #define SIGNALYZER_LED_RED 2
3100 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3101 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3102 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3103 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3104 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3107 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3109 #if BUILD_FT2232_FTD2XX == 1
3110 return FT_WriteEE(ftdih
, address
, value
);
3111 #elif BUILD_FT2232_LIBFTDI == 1
3116 #if BUILD_FT2232_FTD2XX == 1
3117 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3119 return FT_ReadEE(ftdih
, address
, value
);
3123 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3124 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3126 unsigned char on_time
;
3127 unsigned char off_time
;
3129 if (on_time_ms
< 0xFFFF)
3130 on_time
= (unsigned char)(on_time_ms
/ 62);
3134 off_time
= (unsigned char)(off_time_ms
/ 62);
3136 #if BUILD_FT2232_FTD2XX == 1
3139 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3140 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3142 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3143 return ERROR_JTAG_DEVICE_ERROR
;
3146 if ((status
= signalyzer_h_ctrl_write(
3147 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3148 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3150 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3151 return ERROR_JTAG_DEVICE_ERROR
;
3154 if ((status
= signalyzer_h_ctrl_write(
3155 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3156 ((uint32_t)cycles
))) != FT_OK
)
3158 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3159 return ERROR_JTAG_DEVICE_ERROR
;
3162 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3163 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3165 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3166 return ERROR_JTAG_DEVICE_ERROR
;
3170 #elif BUILD_FT2232_LIBFTDI == 1
3173 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3174 ((uint32_t)(channel
<< 8) | led
))) < 0)
3176 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3177 ftdi_get_error_string(&ftdic
));
3178 return ERROR_JTAG_DEVICE_ERROR
;
3181 if ((retval
= signalyzer_h_ctrl_write(
3182 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3183 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3185 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3186 ftdi_get_error_string(&ftdic
));
3187 return ERROR_JTAG_DEVICE_ERROR
;
3190 if ((retval
= signalyzer_h_ctrl_write(
3191 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3192 (uint32_t)cycles
)) < 0)
3194 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3195 ftdi_get_error_string(&ftdic
));
3196 return ERROR_JTAG_DEVICE_ERROR
;
3199 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3200 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3202 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3203 ftdi_get_error_string(&ftdic
));
3204 return ERROR_JTAG_DEVICE_ERROR
;
3211 static int signalyzer_h_init(void)
3213 #if BUILD_FT2232_FTD2XX == 1
3220 uint16_t read_buf
[12] = { 0 };
3222 uint32_t bytes_written
;
3224 /* turn on center green led */
3225 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3226 0xFFFF, 0x00, 0x00);
3228 /* determine what channel config wants to open
3229 * TODO: change me... current implementation is made to work
3230 * with openocd description parsing.
3232 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3236 signalyzer_h_side
= *(end_of_desc
- 1);
3237 if (signalyzer_h_side
== 'B')
3238 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3240 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3244 LOG_ERROR("No Channel was specified");
3248 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3251 #if BUILD_FT2232_FTD2XX == 1
3252 /* read signalyzer versionining information */
3253 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3254 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3256 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3257 return ERROR_JTAG_DEVICE_ERROR
;
3260 for (i
= 0; i
< 10; i
++)
3262 if ((status
= signalyzer_h_ctrl_read(
3263 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3264 &read_buf
[i
])) != FT_OK
)
3266 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3268 return ERROR_JTAG_DEVICE_ERROR
;
3272 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3273 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3274 read_buf
[4], read_buf
[5], read_buf
[6]);
3276 /* set gpio register */
3277 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3278 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3280 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3281 return ERROR_JTAG_DEVICE_ERROR
;
3284 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3287 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3288 return ERROR_JTAG_DEVICE_ERROR
;
3291 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3292 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3294 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3295 return ERROR_JTAG_DEVICE_ERROR
;
3298 /* read adapter type information */
3299 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3300 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3302 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3303 return ERROR_JTAG_DEVICE_ERROR
;
3306 if ((status
= signalyzer_h_ctrl_write(
3307 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3309 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3310 return ERROR_JTAG_DEVICE_ERROR
;
3313 if ((status
= signalyzer_h_ctrl_write(
3314 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3316 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3317 return ERROR_JTAG_DEVICE_ERROR
;
3320 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3321 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3323 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3324 return ERROR_JTAG_DEVICE_ERROR
;
3329 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3330 &read_buf
[0])) != FT_OK
)
3332 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3333 return ERROR_JTAG_DEVICE_ERROR
;
3336 if (read_buf
[0] != 0x0498)
3337 signalyzer_h_adapter_type
= 0x0000;
3340 for (i
= 0; i
< 4; i
++)
3342 if ((status
= signalyzer_h_ctrl_read(
3343 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3344 &read_buf
[i
])) != FT_OK
)
3346 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3348 return ERROR_JTAG_DEVICE_ERROR
;
3352 signalyzer_h_adapter_type
= read_buf
[0];
3355 #elif BUILD_FT2232_LIBFTDI == 1
3356 /* currently libftdi does not allow reading individual eeprom
3357 * locations, therefore adapter type cannot be detected.
3358 * override with most common type
3360 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3363 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3365 /* ADAPTOR: EM_LT16_A */
3366 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3368 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3369 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3377 low_direction
= 0x1b;
3380 high_direction
= 0x0;
3382 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3384 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3385 low_output
&= ~nTRST
; /* nTRST = 0 */
3389 low_direction
|= nTRSTnOE
; /* nTRST output */
3390 low_output
|= nTRST
; /* nTRST = 1 */
3393 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3395 low_direction
|= nSRSTnOE
; /* nSRST output */
3396 low_output
|= nSRST
; /* nSRST = 1 */
3400 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3401 low_output
&= ~nSRST
; /* nSRST = 0 */
3404 #if BUILD_FT2232_FTD2XX == 1
3405 /* enable power to the module */
3406 if ((status
= signalyzer_h_ctrl_write(
3407 SIGNALYZER_DATA_BUFFER_ADDR
,
3408 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3411 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3413 return ERROR_JTAG_DEVICE_ERROR
;
3416 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3417 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3419 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3421 return ERROR_JTAG_DEVICE_ERROR
;
3424 /* set gpio mode register */
3425 if ((status
= signalyzer_h_ctrl_write(
3426 SIGNALYZER_DATA_BUFFER_ADDR
,
3427 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3429 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3431 return ERROR_JTAG_DEVICE_ERROR
;
3434 if ((status
= signalyzer_h_ctrl_write(
3435 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3438 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3440 return ERROR_JTAG_DEVICE_ERROR
;
3443 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3444 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3446 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3448 return ERROR_JTAG_DEVICE_ERROR
;
3451 /* set gpio register */
3452 if ((status
= signalyzer_h_ctrl_write(
3453 SIGNALYZER_DATA_BUFFER_ADDR
,
3454 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3456 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3458 return ERROR_JTAG_DEVICE_ERROR
;
3461 if ((status
= signalyzer_h_ctrl_write(
3462 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3465 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3467 return ERROR_JTAG_DEVICE_ERROR
;
3470 if ((status
= signalyzer_h_ctrl_write(
3471 SIGNALYZER_COMMAND_ADDR
,
3472 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3474 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3476 return ERROR_JTAG_DEVICE_ERROR
;
3481 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3482 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3483 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3484 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3485 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3487 if (signalyzer_h_adapter_type
3488 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3489 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3490 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3491 else if (signalyzer_h_adapter_type
3492 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3493 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3494 "(ARM JTAG with PSU) detected. (HW: %2x).",
3495 (read_buf
[1] >> 8));
3496 else if (signalyzer_h_adapter_type
3497 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3498 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3499 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3500 else if (signalyzer_h_adapter_type
3501 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3502 LOG_INFO("Signalyzer: EM-JTAG-P "
3503 "(Generic JTAG with PSU) detected. (HW: %2x).",
3504 (read_buf
[1] >> 8));
3512 low_direction
= 0x1b;
3515 high_direction
= 0x1f;
3517 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3519 high_output
|= nTRSTnOE
;
3520 high_output
&= ~nTRST
;
3524 high_output
&= ~nTRSTnOE
;
3525 high_output
|= nTRST
;
3528 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3530 high_output
&= ~nSRSTnOE
;
3531 high_output
|= nSRST
;
3535 high_output
|= nSRSTnOE
;
3536 high_output
&= ~nSRST
;
3539 #if BUILD_FT2232_FTD2XX == 1
3540 /* enable power to the module */
3541 if ((status
= signalyzer_h_ctrl_write(
3542 SIGNALYZER_DATA_BUFFER_ADDR
,
3543 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3546 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3548 return ERROR_JTAG_DEVICE_ERROR
;
3551 if ((status
= signalyzer_h_ctrl_write(
3552 SIGNALYZER_COMMAND_ADDR
,
3553 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3555 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3557 return ERROR_JTAG_DEVICE_ERROR
;
3560 /* set gpio mode register (IO_16 and IO_17 set as analog
3561 * inputs, other is gpio)
3563 if ((status
= signalyzer_h_ctrl_write(
3564 SIGNALYZER_DATA_BUFFER_ADDR
,
3565 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3567 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3569 return ERROR_JTAG_DEVICE_ERROR
;
3572 if ((status
= signalyzer_h_ctrl_write(
3573 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3576 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3578 return ERROR_JTAG_DEVICE_ERROR
;
3581 if ((status
= signalyzer_h_ctrl_write(
3582 SIGNALYZER_COMMAND_ADDR
,
3583 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3585 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3587 return ERROR_JTAG_DEVICE_ERROR
;
3590 /* set gpio register (all inputs, for -P modules,
3591 * PSU will be turned off)
3593 if ((status
= signalyzer_h_ctrl_write(
3594 SIGNALYZER_DATA_BUFFER_ADDR
,
3595 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3597 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3599 return ERROR_JTAG_DEVICE_ERROR
;
3602 if ((status
= signalyzer_h_ctrl_write(
3603 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3606 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3608 return ERROR_JTAG_DEVICE_ERROR
;
3611 if ((status
= signalyzer_h_ctrl_write(
3612 SIGNALYZER_COMMAND_ADDR
,
3613 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3615 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3617 return ERROR_JTAG_DEVICE_ERROR
;
3622 else if (signalyzer_h_adapter_type
== 0x0000)
3624 LOG_INFO("Signalyzer: No external modules were detected.");
3632 low_direction
= 0x1b;
3635 high_direction
= 0x0;
3637 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3639 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3640 low_output
&= ~nTRST
; /* nTRST = 0 */
3644 low_direction
|= nTRSTnOE
; /* nTRST output */
3645 low_output
|= nTRST
; /* nTRST = 1 */
3648 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3650 low_direction
|= nSRSTnOE
; /* nSRST output */
3651 low_output
|= nSRST
; /* nSRST = 1 */
3655 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3656 low_output
&= ~nSRST
; /* nSRST = 0 */
3661 LOG_ERROR("Unknown module type is detected: %.4x",
3662 signalyzer_h_adapter_type
);
3663 return ERROR_JTAG_DEVICE_ERROR
;
3666 /* initialize low byte of controller for jtag operation */
3668 buf
[1] = low_output
;
3669 buf
[2] = low_direction
;
3671 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3672 || (bytes_written
!= 3))
3674 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3675 return ERROR_JTAG_INIT_FAILED
;
3678 #if BUILD_FT2232_FTD2XX == 1
3679 if (ftdi_device
== FT_DEVICE_2232H
)
3681 /* initialize high byte of controller for jtag operation */
3683 buf
[1] = high_output
;
3684 buf
[2] = high_direction
;
3686 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3687 || (bytes_written
!= 3))
3689 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3690 return ERROR_JTAG_INIT_FAILED
;
3693 #elif BUILD_FT2232_LIBFTDI == 1
3694 if (ftdi_device
== TYPE_2232H
)
3696 /* initialize high byte of controller for jtag operation */
3698 buf
[1] = high_output
;
3699 buf
[2] = high_direction
;
3701 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3702 || (bytes_written
!= 3))
3704 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3705 return ERROR_JTAG_INIT_FAILED
;
3712 static void signalyzer_h_reset(int trst
, int srst
)
3714 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3716 /* ADAPTOR: EM_LT16_A */
3717 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3721 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3722 /* switch to output pin (output is low) */
3723 low_direction
|= nTRSTnOE
;
3725 /* switch output low */
3726 low_output
&= ~nTRST
;
3730 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3731 /* switch to input pin (high-Z + internal
3732 * and external pullup) */
3733 low_direction
&= ~nTRSTnOE
;
3735 /* switch output high */
3736 low_output
|= nTRST
;
3741 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3742 /* switch output low */
3743 low_output
&= ~nSRST
;
3745 /* switch to output pin (output is low) */
3746 low_direction
|= nSRSTnOE
;
3750 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3751 /* switch output high */
3752 low_output
|= nSRST
;
3754 /* switch to input pin (high-Z) */
3755 low_direction
&= ~nSRSTnOE
;
3758 /* command "set data bits low byte" */
3760 buffer_write(low_output
);
3761 buffer_write(low_direction
);
3762 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3763 "low_direction: 0x%2.2x",
3764 trst
, srst
, low_output
, low_direction
);
3766 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3767 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3768 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3769 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3770 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3774 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3775 high_output
&= ~nTRSTnOE
;
3777 high_output
&= ~nTRST
;
3781 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3782 high_output
|= nTRSTnOE
;
3784 high_output
|= nTRST
;
3789 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3790 high_output
&= ~nSRST
;
3792 high_output
&= ~nSRSTnOE
;
3796 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3797 high_output
|= nSRST
;
3799 high_output
|= nSRSTnOE
;
3802 /* command "set data bits high byte" */
3804 buffer_write(high_output
);
3805 buffer_write(high_direction
);
3806 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
3807 "high_direction: 0x%2.2x",
3808 trst
, srst
, high_output
, high_direction
);
3810 else if (signalyzer_h_adapter_type
== 0x0000)
3814 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3815 /* switch to output pin (output is low) */
3816 low_direction
|= nTRSTnOE
;
3818 /* switch output low */
3819 low_output
&= ~nTRST
;
3823 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3824 /* switch to input pin (high-Z + internal
3825 * and external pullup) */
3826 low_direction
&= ~nTRSTnOE
;
3828 /* switch output high */
3829 low_output
|= nTRST
;
3834 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3835 /* switch output low */
3836 low_output
&= ~nSRST
;
3838 /* switch to output pin (output is low) */
3839 low_direction
|= nSRSTnOE
;
3843 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3844 /* switch output high */
3845 low_output
|= nSRST
;
3847 /* switch to input pin (high-Z) */
3848 low_direction
&= ~nSRSTnOE
;
3851 /* command "set data bits low byte" */
3853 buffer_write(low_output
);
3854 buffer_write(low_direction
);
3855 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3856 "low_direction: 0x%2.2x",
3857 trst
, srst
, low_output
, low_direction
);
3861 static void signalyzer_h_blink(void)
3863 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
3866 /********************************************************************
3867 * Support for KT-LINK
3868 * JTAG adapter from KRISTECH
3869 * http://www.kristech.eu
3870 *******************************************************************/
3871 static int ktlink_init(void)
3874 uint32_t bytes_written
;
3875 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
3877 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
3878 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
3880 // initialize low port
3881 buf
[0] = 0x80; // command "set data bits low byte"
3882 buf
[1] = low_output
;
3883 buf
[2] = low_direction
;
3884 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3886 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3888 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3889 return ERROR_JTAG_INIT_FAILED
;
3897 high_output
= 0x80; // turn LED on
3898 high_direction
= 0xFF; // all outputs
3900 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3902 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3903 high_output
|= nTRSTnOE
;
3904 high_output
&= ~nTRST
;
3906 high_output
&= ~nTRSTnOE
;
3907 high_output
|= nTRST
;
3910 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3911 high_output
&= ~nSRSTnOE
;
3912 high_output
|= nSRST
;
3914 high_output
|= nSRSTnOE
;
3915 high_output
&= ~nSRST
;
3918 // initialize high port
3919 buf
[0] = 0x82; // command "set data bits high byte"
3920 buf
[1] = high_output
; // value
3921 buf
[2] = high_direction
;
3922 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3924 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3926 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3927 return ERROR_JTAG_INIT_FAILED
;
3933 static void ktlink_reset(int trst
, int srst
)
3935 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3938 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3939 high_output
&= ~nTRSTnOE
;
3941 high_output
&= ~nTRST
;
3942 } else if (trst
== 0) {
3943 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3944 high_output
|= nTRSTnOE
;
3946 high_output
|= nTRST
;
3950 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3951 high_output
&= ~nSRST
;
3953 high_output
&= ~nSRSTnOE
;
3954 } else if (srst
== 0) {
3955 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3956 high_output
|= nSRST
;
3958 high_output
|= nSRSTnOE
;
3961 buffer_write(0x82); // command "set data bits high byte"
3962 buffer_write(high_output
);
3963 buffer_write(high_direction
);
3964 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
3967 static void ktlink_blink(void)
3969 /* LED connected to ACBUS7 */
3970 if (high_output
& 0x80)
3971 high_output
&= 0x7F;
3973 high_output
|= 0x80;
3975 buffer_write(0x82); // command "set data bits high byte"
3976 buffer_write(high_output
);
3977 buffer_write(high_direction
);
3980 static const struct command_registration ft2232_command_handlers
[] = {
3982 .name
= "ft2232_device_desc",
3983 .handler
= &ft2232_handle_device_desc_command
,
3984 .mode
= COMMAND_CONFIG
,
3985 .help
= "set the USB device description of the FTDI FT2232 device",
3986 .usage
= "<description>",
3989 .name
= "ft2232_serial",
3990 .handler
= &ft2232_handle_serial_command
,
3991 .mode
= COMMAND_CONFIG
,
3992 .help
= "set the serial number of the FTDI FT2232 device",
3993 .usage
= "<serial#>",
3996 .name
= "ft2232_layout",
3997 .handler
= &ft2232_handle_layout_command
,
3998 .mode
= COMMAND_CONFIG
,
3999 .help
= "set the layout of the FT2232 GPIO signals used "
4000 "to control output-enables and reset signals",
4001 .usage
= "<layout>",
4004 .name
= "ft2232_vid_pid",
4005 .handler
= &ft2232_handle_vid_pid_command
,
4006 .mode
= COMMAND_CONFIG
,
4007 .help
= "the vendor ID and product ID of the FTDI FT2232 device",
4008 .usage
= "<vid> <pid> [...]",
4011 .name
= "ft2232_latency",
4012 .handler
= &ft2232_handle_latency_command
,
4013 .mode
= COMMAND_CONFIG
,
4014 .help
= "set the FT2232 latency timer to a new value",
4015 .usage
= "<vid> <pid> [...]",
4017 COMMAND_REGISTRATION_DONE
4020 struct jtag_interface ft2232_interface
= {
4022 .commands
= ft2232_command_handlers
,
4024 .init
= ft2232_init
,
4025 .quit
= ft2232_quit
,
4026 .speed
= ft2232_speed
,
4027 .speed_div
= ft2232_speed_div
,
4029 .execute_queue
= ft2232_execute_queue
,