1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /**************************************************************************
4 * Copyright (C) 2012 by Andreas Fritiofson *
5 * andreas.fritiofson@gmail.com *
6 ***************************************************************************/
10 * JTAG adapters based on the FT2232 full and high speed USB parts are
11 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
12 * are discrete, but development boards may integrate them as alternatives
13 * to more capable (and expensive) third party JTAG pods.
15 * JTAG uses only one of the two communications channels ("MPSSE engines")
16 * on these devices. Adapters based on FT4232 parts have four ports/channels
17 * (A/B/C/D), instead of just two (A/B).
19 * Especially on development boards integrating one of these chips (as
20 * opposed to discrete pods/dongles), the additional channels can be used
21 * for a variety of purposes, but OpenOCD only uses one channel at a time.
23 * - As a USB-to-serial adapter for the target's console UART ...
24 * which may be able to support ROM boot loaders that load initial
25 * firmware images to flash (or SRAM).
27 * - On systems which support ARM's SWD in addition to JTAG, or instead
28 * of it, that second port can be used for reading SWV/SWO trace data.
30 * - Additional JTAG links, e.g. to a CPLD or * FPGA.
32 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
33 * request/response interactions involve round trips over the USB link.
34 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
35 * can for example poll quickly for a status change (usually taking on the
36 * order of microseconds not milliseconds) before beginning a queued
37 * transaction which require the previous one to have completed.
39 * There are dozens of adapters of this type, differing in details which
40 * this driver needs to understand. Those "layout" details are required
41 * as part of FT2232 driver configuration.
43 * This code uses information contained in the MPSSE specification which was
45 * https://www.ftdichip.com/Support/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
46 * Hereafter this is called the "MPSSE Spec".
48 * The datasheet for the ftdichip.com's FT2232H part is here:
49 * https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT2232H.pdf
51 * Also note the issue with code 0x4b (clock data to TMS) noted in
52 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
53 * which can affect longer JTAG state paths.
60 /* project specific includes */
61 #include <jtag/adapter.h>
62 #include <jtag/interface.h>
64 #include <transport/transport.h>
65 #include <helper/time_support.h>
66 #include <helper/log.h>
67 #include <helper/nvp.h>
75 /* FTDI access library includes */
78 #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
79 #define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT)
80 #define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
82 static char *ftdi_device_desc
;
83 static uint8_t ftdi_channel
;
84 static uint8_t ftdi_jtag_mode
= JTAG_MODE
;
89 /* vid = pid = 0 marks the end of the list */
90 static uint16_t ftdi_vid
[MAX_USB_IDS
+ 1] = { 0 };
91 static uint16_t ftdi_pid
[MAX_USB_IDS
+ 1] = { 0 };
93 static struct mpsse_ctx
*mpsse_ctx
;
106 static struct signal
*signals
;
108 /* FIXME: Where to store per-instance data? We need an SWD context. */
109 static struct swd_cmd_queue_entry
{
112 uint8_t trn_ack_data_parity_trn
[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
114 static size_t swd_cmd_queue_length
;
115 static size_t swd_cmd_queue_alloced
;
116 static int queued_retval
;
119 static uint16_t output
;
120 static uint16_t direction
;
121 static uint16_t jtag_output_init
;
122 static uint16_t jtag_direction_init
;
124 static int ftdi_swd_switch_seq(enum swd_special_seq seq
);
126 static struct signal
*find_signal_by_name(const char *name
)
128 for (struct signal
*sig
= signals
; sig
; sig
= sig
->next
) {
129 if (strcmp(name
, sig
->name
) == 0)
135 static struct signal
*create_signal(const char *name
)
137 struct signal
**psig
= &signals
;
139 psig
= &(*psig
)->next
;
141 *psig
= calloc(1, sizeof(**psig
));
145 (*psig
)->name
= strdup(name
);
146 if (!(*psig
)->name
) {
153 static int ftdi_set_signal(const struct signal
*s
, char value
)
158 if (s
->data_mask
== 0 && s
->oe_mask
== 0) {
159 LOG_ERROR("interface doesn't provide signal '%s'", s
->name
);
164 data
= s
->invert_data
;
168 if (s
->data_mask
== 0) {
169 LOG_ERROR("interface can't drive '%s' high", s
->name
);
172 data
= !s
->invert_data
;
177 if (s
->oe_mask
== 0) {
178 LOG_ERROR("interface can't tri-state '%s'", s
->name
);
181 data
= s
->invert_data
;
185 LOG_ERROR("invalid signal level specifier \'%c\'(0x%02x)", value
, value
);
189 uint16_t old_output
= output
;
190 uint16_t old_direction
= direction
;
192 output
= data
? output
| s
->data_mask
: output
& ~s
->data_mask
;
193 if (s
->oe_mask
== s
->data_mask
)
194 direction
= oe
? direction
| s
->oe_mask
: direction
& ~s
->oe_mask
;
196 output
= oe
? output
| s
->oe_mask
: output
& ~s
->oe_mask
;
198 if ((output
& 0xff) != (old_output
& 0xff) || (direction
& 0xff) != (old_direction
& 0xff))
199 mpsse_set_data_bits_low_byte(mpsse_ctx
, output
& 0xff, direction
& 0xff);
200 if ((output
>> 8 != old_output
>> 8) || (direction
>> 8 != old_direction
>> 8))
201 mpsse_set_data_bits_high_byte(mpsse_ctx
, output
>> 8, direction
>> 8);
206 static int ftdi_get_signal(const struct signal
*s
, uint16_t *value_out
)
208 uint8_t data_low
= 0;
209 uint8_t data_high
= 0;
211 if (s
->input_mask
== 0) {
212 LOG_ERROR("interface doesn't provide signal '%s'", s
->name
);
216 if (s
->input_mask
& 0xff)
217 mpsse_read_data_bits_low_byte(mpsse_ctx
, &data_low
);
218 if (s
->input_mask
>> 8)
219 mpsse_read_data_bits_high_byte(mpsse_ctx
, &data_high
);
221 mpsse_flush(mpsse_ctx
);
223 *value_out
= (((uint16_t)data_high
) << 8) | data_low
;
226 *value_out
= ~(*value_out
);
228 *value_out
&= s
->input_mask
;
234 * Function move_to_state
235 * moves the TAP controller from the current state to a
236 * \a goal_state through a path given by tap_get_tms_path(). State transition
237 * logging is performed by delegation to clock_tms().
239 * @param goal_state is the destination state for the move.
241 static void move_to_state(tap_state_t goal_state
)
243 tap_state_t start_state
= tap_get_state();
245 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
246 lookup of the required TMS pattern to move to this state from the
250 /* do the 2 lookups */
251 uint8_t tms_bits
= tap_get_tms_path(start_state
, goal_state
);
252 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
253 assert(tms_count
<= 8);
255 LOG_DEBUG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
257 /* Track state transitions step by step */
258 for (int i
= 0; i
< tms_count
; i
++)
259 tap_set_state(tap_state_transition(tap_get_state(), (tms_bits
>> i
) & 1));
261 mpsse_clock_tms_cs_out(mpsse_ctx
,
269 static int ftdi_speed(int speed
)
272 retval
= mpsse_set_frequency(mpsse_ctx
, speed
);
275 LOG_ERROR("couldn't set FTDI TCK speed");
279 if (!swd_mode
&& speed
>= 10000000 && ftdi_jtag_mode
!= JTAG_MODE_ALT
)
280 LOG_INFO("ftdi: if you experience problems at higher adapter clocks, try "
281 "the command \"ftdi tdo_sample_edge falling\"");
285 static int ftdi_speed_div(int speed
, int *khz
)
291 static int ftdi_khz(int khz
, int *jtag_speed
)
293 if (khz
== 0 && !mpsse_is_high_speed(mpsse_ctx
)) {
294 LOG_DEBUG("RCLK not supported");
298 *jtag_speed
= khz
* 1000;
302 static void ftdi_end_state(tap_state_t state
)
304 if (tap_is_state_stable(state
))
305 tap_set_end_state(state
);
307 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
312 static void ftdi_execute_runtest(struct jtag_command
*cmd
)
317 LOG_DEBUG_IO("runtest %i cycles, end in %s",
318 cmd
->cmd
.runtest
->num_cycles
,
319 tap_state_name(cmd
->cmd
.runtest
->end_state
));
321 if (tap_get_state() != TAP_IDLE
)
322 move_to_state(TAP_IDLE
);
324 /* TODO: Reuse ftdi_execute_stableclocks */
325 i
= cmd
->cmd
.runtest
->num_cycles
;
327 /* there are no state transitions in this code, so omit state tracking */
328 unsigned this_len
= i
> 7 ? 7 : i
;
329 mpsse_clock_tms_cs_out(mpsse_ctx
, &zero
, 0, this_len
, false, ftdi_jtag_mode
);
333 ftdi_end_state(cmd
->cmd
.runtest
->end_state
);
335 if (tap_get_state() != tap_get_end_state())
336 move_to_state(tap_get_end_state());
338 LOG_DEBUG_IO("runtest: %i, end in %s",
339 cmd
->cmd
.runtest
->num_cycles
,
340 tap_state_name(tap_get_end_state()));
343 static void ftdi_execute_statemove(struct jtag_command
*cmd
)
345 LOG_DEBUG_IO("statemove end in %s",
346 tap_state_name(cmd
->cmd
.statemove
->end_state
));
348 ftdi_end_state(cmd
->cmd
.statemove
->end_state
);
350 /* shortest-path move to desired end state */
351 if (tap_get_state() != tap_get_end_state() || tap_get_end_state() == TAP_RESET
)
352 move_to_state(tap_get_end_state());
356 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
357 * (or SWD) state machine. REVISIT: Not the best method, perhaps.
359 static void ftdi_execute_tms(struct jtag_command
*cmd
)
361 LOG_DEBUG_IO("TMS: %d bits", cmd
->cmd
.tms
->num_bits
);
363 /* TODO: Missing tap state tracking, also missing from ft2232.c! */
364 mpsse_clock_tms_cs_out(mpsse_ctx
,
367 cmd
->cmd
.tms
->num_bits
,
372 static void ftdi_execute_pathmove(struct jtag_command
*cmd
)
374 tap_state_t
*path
= cmd
->cmd
.pathmove
->path
;
375 int num_states
= cmd
->cmd
.pathmove
->num_states
;
377 LOG_DEBUG_IO("pathmove: %i states, current: %s end: %s", num_states
,
378 tap_state_name(tap_get_state()),
379 tap_state_name(path
[num_states
-1]));
382 unsigned bit_count
= 0;
383 uint8_t tms_byte
= 0;
387 /* this loop verifies that the path is legal and logs each state in the path */
388 while (num_states
--) {
390 /* either TMS=0 or TMS=1 must work ... */
391 if (tap_state_transition(tap_get_state(), false)
392 == path
[state_count
])
393 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
394 else if (tap_state_transition(tap_get_state(), true)
395 == path
[state_count
]) {
396 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
398 /* ... or else the caller goofed BADLY */
400 LOG_ERROR("BUG: %s -> %s isn't a valid "
401 "TAP state transition",
402 tap_state_name(tap_get_state()),
403 tap_state_name(path
[state_count
]));
407 tap_set_state(path
[state_count
]);
410 if (bit_count
== 7 || num_states
== 0) {
411 mpsse_clock_tms_cs_out(mpsse_ctx
,
420 tap_set_end_state(tap_get_state());
423 static void ftdi_execute_scan(struct jtag_command
*cmd
)
425 LOG_DEBUG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN",
426 jtag_scan_type(cmd
->cmd
.scan
));
428 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
429 while (cmd
->cmd
.scan
->num_fields
> 0
430 && cmd
->cmd
.scan
->fields
[cmd
->cmd
.scan
->num_fields
- 1].num_bits
== 0) {
431 cmd
->cmd
.scan
->num_fields
--;
432 LOG_DEBUG_IO("discarding trailing empty field");
435 if (cmd
->cmd
.scan
->num_fields
== 0) {
436 LOG_DEBUG_IO("empty scan, doing nothing");
440 if (cmd
->cmd
.scan
->ir_scan
) {
441 if (tap_get_state() != TAP_IRSHIFT
)
442 move_to_state(TAP_IRSHIFT
);
444 if (tap_get_state() != TAP_DRSHIFT
)
445 move_to_state(TAP_DRSHIFT
);
448 ftdi_end_state(cmd
->cmd
.scan
->end_state
);
450 struct scan_field
*field
= cmd
->cmd
.scan
->fields
;
451 unsigned scan_size
= 0;
453 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++, field
++) {
454 scan_size
+= field
->num_bits
;
455 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
456 field
->in_value
? "in" : "",
457 field
->out_value
? "out" : "",
459 cmd
->cmd
.scan
->num_fields
,
462 if (i
== cmd
->cmd
.scan
->num_fields
- 1 && tap_get_state() != tap_get_end_state()) {
463 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
464 * movement. This last field can't have length zero, it was checked above. */
465 mpsse_clock_data(mpsse_ctx
,
472 uint8_t last_bit
= 0;
473 if (field
->out_value
)
474 bit_copy(&last_bit
, 0, field
->out_value
, field
->num_bits
- 1, 1);
476 /* If endstate is TAP_IDLE, clock out 1-1-0 (->EXIT1 ->UPDATE ->IDLE)
477 * Otherwise, clock out 1-0 (->EXIT1 ->PAUSE)
479 uint8_t tms_bits
= 0x03;
480 mpsse_clock_tms_cs(mpsse_ctx
,
488 tap_set_state(tap_state_transition(tap_get_state(), 1));
489 if (tap_get_end_state() == TAP_IDLE
) {
490 mpsse_clock_tms_cs_out(mpsse_ctx
,
496 tap_set_state(tap_state_transition(tap_get_state(), 1));
497 tap_set_state(tap_state_transition(tap_get_state(), 0));
499 mpsse_clock_tms_cs_out(mpsse_ctx
,
505 tap_set_state(tap_state_transition(tap_get_state(), 0));
508 mpsse_clock_data(mpsse_ctx
,
517 if (tap_get_state() != tap_get_end_state())
518 move_to_state(tap_get_end_state());
520 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
521 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
522 tap_state_name(tap_get_end_state()));
525 static int ftdi_reset(int trst
, int srst
)
527 struct signal
*sig_ntrst
= find_signal_by_name("nTRST");
528 struct signal
*sig_nsrst
= find_signal_by_name("nSRST");
530 LOG_DEBUG_IO("reset trst: %i srst %i", trst
, srst
);
535 ftdi_set_signal(sig_ntrst
, '0');
537 LOG_ERROR("Can't assert TRST: nTRST signal is not defined");
538 } else if (sig_ntrst
&& jtag_get_reset_config() & RESET_HAS_TRST
&&
540 if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN
)
541 ftdi_set_signal(sig_ntrst
, 'z');
543 ftdi_set_signal(sig_ntrst
, '1');
549 ftdi_set_signal(sig_nsrst
, '0');
551 LOG_ERROR("Can't assert SRST: nSRST signal is not defined");
552 } else if (sig_nsrst
&& jtag_get_reset_config() & RESET_HAS_SRST
&&
554 if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL
)
555 ftdi_set_signal(sig_nsrst
, '1');
557 ftdi_set_signal(sig_nsrst
, 'z');
560 return mpsse_flush(mpsse_ctx
);
563 static void ftdi_execute_sleep(struct jtag_command
*cmd
)
565 LOG_DEBUG_IO("sleep %" PRIu32
, cmd
->cmd
.sleep
->us
);
567 mpsse_flush(mpsse_ctx
);
568 jtag_sleep(cmd
->cmd
.sleep
->us
);
569 LOG_DEBUG_IO("sleep %" PRIu32
" usec while in %s",
571 tap_state_name(tap_get_state()));
574 static void ftdi_execute_stableclocks(struct jtag_command
*cmd
)
576 /* this is only allowed while in a stable state. A check for a stable
577 * state was done in jtag_add_clocks()
579 int num_cycles
= cmd
->cmd
.stableclocks
->num_cycles
;
581 /* 7 bits of either ones or zeros. */
582 uint8_t tms
= tap_get_state() == TAP_RESET
? 0x7f : 0x00;
584 /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to
585 * the correct level and remain there during the scan */
586 while (num_cycles
> 0) {
587 /* there are no state transitions in this code, so omit state tracking */
588 unsigned this_len
= num_cycles
> 7 ? 7 : num_cycles
;
589 mpsse_clock_tms_cs_out(mpsse_ctx
, &tms
, 0, this_len
, false, ftdi_jtag_mode
);
590 num_cycles
-= this_len
;
593 LOG_DEBUG_IO("clocks %i while in %s",
594 cmd
->cmd
.stableclocks
->num_cycles
,
595 tap_state_name(tap_get_state()));
598 static void ftdi_execute_command(struct jtag_command
*cmd
)
602 ftdi_execute_runtest(cmd
);
605 ftdi_execute_statemove(cmd
);
608 ftdi_execute_pathmove(cmd
);
611 ftdi_execute_scan(cmd
);
614 ftdi_execute_sleep(cmd
);
616 case JTAG_STABLECLOCKS
:
617 ftdi_execute_stableclocks(cmd
);
620 ftdi_execute_tms(cmd
);
623 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
628 static int ftdi_execute_queue(struct jtag_command
*cmd_queue
)
630 /* blink, if the current layout has that feature */
631 struct signal
*led
= find_signal_by_name("LED");
633 ftdi_set_signal(led
, '1');
635 for (struct jtag_command
*cmd
= cmd_queue
; cmd
; cmd
= cmd
->next
) {
636 /* fill the write buffer with the desired command */
637 ftdi_execute_command(cmd
);
641 ftdi_set_signal(led
, '0');
643 int retval
= mpsse_flush(mpsse_ctx
);
644 if (retval
!= ERROR_OK
)
645 LOG_ERROR("error while flushing MPSSE queue: %d", retval
);
650 static int ftdi_initialize(void)
652 if (tap_get_tms_path_len(TAP_IRPAUSE
, TAP_IRPAUSE
) == 7)
653 LOG_DEBUG("ftdi interface using 7 step jtag state transitions");
655 LOG_DEBUG("ftdi interface using shortest path jtag state transitions");
657 if (!ftdi_vid
[0] && !ftdi_pid
[0]) {
658 LOG_ERROR("Please specify ftdi vid_pid");
659 return ERROR_JTAG_INIT_FAILED
;
662 mpsse_ctx
= mpsse_open(ftdi_vid
, ftdi_pid
, ftdi_device_desc
,
663 adapter_get_required_serial(), adapter_usb_get_location(), ftdi_channel
);
665 return ERROR_JTAG_INIT_FAILED
;
667 output
= jtag_output_init
;
668 direction
= jtag_direction_init
;
671 struct signal
*sig
= find_signal_by_name("SWD_EN");
673 LOG_ERROR("SWD mode is active but SWD_EN signal is not defined");
674 return ERROR_JTAG_INIT_FAILED
;
676 /* A dummy SWD_EN would have zero mask */
678 ftdi_set_signal(sig
, '1');
681 mpsse_set_data_bits_low_byte(mpsse_ctx
, output
& 0xff, direction
& 0xff);
682 mpsse_set_data_bits_high_byte(mpsse_ctx
, output
>> 8, direction
>> 8);
684 mpsse_loopback_config(mpsse_ctx
, false);
686 freq
= mpsse_set_frequency(mpsse_ctx
, adapter_get_speed_khz() * 1000);
688 return mpsse_flush(mpsse_ctx
);
691 static int ftdi_quit(void)
693 mpsse_close(mpsse_ctx
);
695 struct signal
*sig
= signals
;
697 struct signal
*next
= sig
->next
;
698 free((void *)sig
->name
);
703 free(ftdi_device_desc
);
710 COMMAND_HANDLER(ftdi_handle_device_desc_command
)
713 free(ftdi_device_desc
);
714 ftdi_device_desc
= strdup(CMD_ARGV
[0]);
716 LOG_ERROR("expected exactly one argument to ftdi device_desc <description>");
722 COMMAND_HANDLER(ftdi_handle_channel_command
)
725 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], ftdi_channel
);
727 return ERROR_COMMAND_SYNTAX_ERROR
;
732 COMMAND_HANDLER(ftdi_handle_layout_init_command
)
735 return ERROR_COMMAND_SYNTAX_ERROR
;
737 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], jtag_output_init
);
738 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[1], jtag_direction_init
);
743 COMMAND_HANDLER(ftdi_handle_layout_signal_command
)
746 return ERROR_COMMAND_SYNTAX_ERROR
;
748 bool invert_data
= false;
749 uint16_t data_mask
= 0;
750 bool invert_input
= false;
751 uint16_t input_mask
= 0;
752 bool invert_oe
= false;
753 uint16_t oe_mask
= 0;
754 for (unsigned i
= 1; i
< CMD_ARGC
; i
+= 2) {
755 if (strcmp("-data", CMD_ARGV
[i
]) == 0) {
757 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], data_mask
);
758 } else if (strcmp("-ndata", CMD_ARGV
[i
]) == 0) {
760 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], data_mask
);
761 } else if (strcmp("-input", CMD_ARGV
[i
]) == 0) {
762 invert_input
= false;
763 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], input_mask
);
764 } else if (strcmp("-ninput", CMD_ARGV
[i
]) == 0) {
766 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], input_mask
);
767 } else if (strcmp("-oe", CMD_ARGV
[i
]) == 0) {
769 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], oe_mask
);
770 } else if (strcmp("-noe", CMD_ARGV
[i
]) == 0) {
772 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], oe_mask
);
773 } else if (!strcmp("-alias", CMD_ARGV
[i
]) ||
774 !strcmp("-nalias", CMD_ARGV
[i
])) {
775 if (!strcmp("-nalias", CMD_ARGV
[i
])) {
779 struct signal
*sig
= find_signal_by_name(CMD_ARGV
[i
+ 1]);
781 LOG_ERROR("signal %s is not defined", CMD_ARGV
[i
+ 1]);
784 data_mask
= sig
->data_mask
;
785 input_mask
= sig
->input_mask
;
786 oe_mask
= sig
->oe_mask
;
787 invert_input
^= sig
->invert_input
;
788 invert_oe
= sig
->invert_oe
;
789 invert_data
^= sig
->invert_data
;
791 LOG_ERROR("unknown option '%s'", CMD_ARGV
[i
]);
792 return ERROR_COMMAND_SYNTAX_ERROR
;
797 sig
= find_signal_by_name(CMD_ARGV
[0]);
799 sig
= create_signal(CMD_ARGV
[0]);
801 LOG_ERROR("failed to create signal %s", CMD_ARGV
[0]);
805 sig
->invert_data
= invert_data
;
806 sig
->data_mask
= data_mask
;
807 sig
->invert_input
= invert_input
;
808 sig
->input_mask
= input_mask
;
809 sig
->invert_oe
= invert_oe
;
810 sig
->oe_mask
= oe_mask
;
815 COMMAND_HANDLER(ftdi_handle_set_signal_command
)
818 return ERROR_COMMAND_SYNTAX_ERROR
;
821 sig
= find_signal_by_name(CMD_ARGV
[0]);
823 LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV
[0]);
827 switch (*CMD_ARGV
[1]) {
832 /* single character level specifier only */
833 if (CMD_ARGV
[1][1] == '\0') {
834 ftdi_set_signal(sig
, *CMD_ARGV
[1]);
839 LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV
[1]);
840 return ERROR_COMMAND_ARGUMENT_INVALID
;
843 return mpsse_flush(mpsse_ctx
);
846 COMMAND_HANDLER(ftdi_handle_get_signal_command
)
849 return ERROR_COMMAND_SYNTAX_ERROR
;
852 uint16_t sig_data
= 0;
853 sig
= find_signal_by_name(CMD_ARGV
[0]);
855 LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV
[0]);
859 int ret
= ftdi_get_signal(sig
, &sig_data
);
863 LOG_USER("Signal %s = %#06x", sig
->name
, sig_data
);
868 COMMAND_HANDLER(ftdi_handle_vid_pid_command
)
870 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
871 LOG_WARNING("ignoring extra IDs in ftdi vid_pid "
872 "(maximum is %d pairs)", MAX_USB_IDS
);
873 CMD_ARGC
= MAX_USB_IDS
* 2;
875 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
876 LOG_WARNING("incomplete ftdi vid_pid configuration directive");
878 return ERROR_COMMAND_SYNTAX_ERROR
;
879 /* remove the incomplete trailing id */
884 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
885 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], ftdi_vid
[i
>> 1]);
886 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], ftdi_pid
[i
>> 1]);
890 * Explicitly terminate, in case there are multiples instances of
893 ftdi_vid
[i
>> 1] = ftdi_pid
[i
>> 1] = 0;
898 COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command
)
901 static const struct nvp nvp_ftdi_jtag_modes
[] = {
902 { .name
= "rising", .value
= JTAG_MODE
},
903 { .name
= "falling", .value
= JTAG_MODE_ALT
},
904 { .name
= NULL
, .value
= -1 },
908 n
= nvp_name2value(nvp_ftdi_jtag_modes
, CMD_ARGV
[0]);
910 return ERROR_COMMAND_SYNTAX_ERROR
;
911 ftdi_jtag_mode
= n
->value
;
915 n
= nvp_value2name(nvp_ftdi_jtag_modes
, ftdi_jtag_mode
);
916 command_print(CMD
, "ftdi samples TDO on %s edge of TCK", n
->name
);
921 static const struct command_registration ftdi_subcommand_handlers
[] = {
923 .name
= "device_desc",
924 .handler
= &ftdi_handle_device_desc_command
,
925 .mode
= COMMAND_CONFIG
,
926 .help
= "set the USB device description of the FTDI device",
927 .usage
= "description_string",
931 .handler
= &ftdi_handle_channel_command
,
932 .mode
= COMMAND_CONFIG
,
933 .help
= "set the channel of the FTDI device that is used as JTAG",
937 .name
= "layout_init",
938 .handler
= &ftdi_handle_layout_init_command
,
939 .mode
= COMMAND_CONFIG
,
940 .help
= "initialize the FTDI GPIO signals used "
941 "to control output-enables and reset signals",
942 .usage
= "data direction",
945 .name
= "layout_signal",
946 .handler
= &ftdi_handle_layout_signal_command
,
948 .help
= "define a signal controlled by one or more FTDI GPIO as data "
949 "and/or output enable",
950 .usage
= "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
953 .name
= "set_signal",
954 .handler
= &ftdi_handle_set_signal_command
,
955 .mode
= COMMAND_EXEC
,
956 .help
= "control a layout-specific signal",
957 .usage
= "name (1|0|z)",
960 .name
= "get_signal",
961 .handler
= &ftdi_handle_get_signal_command
,
962 .mode
= COMMAND_EXEC
,
963 .help
= "read the value of a layout-specific signal",
968 .handler
= &ftdi_handle_vid_pid_command
,
969 .mode
= COMMAND_CONFIG
,
970 .help
= "the vendor ID and product ID of the FTDI device",
971 .usage
= "(vid pid)*",
974 .name
= "tdo_sample_edge",
975 .handler
= &ftdi_handle_tdo_sample_edge_command
,
977 .help
= "set which TCK clock edge is used for sampling TDO "
978 "- default is rising-edge (Setting to falling-edge may "
979 "allow signalling speed increase)",
980 .usage
= "(rising|falling)",
982 COMMAND_REGISTRATION_DONE
985 static const struct command_registration ftdi_command_handlers
[] = {
989 .help
= "perform ftdi management",
990 .chain
= ftdi_subcommand_handlers
,
993 COMMAND_REGISTRATION_DONE
996 static int create_default_signal(const char *name
, uint16_t data_mask
)
998 struct signal
*sig
= create_signal(name
);
1000 LOG_ERROR("failed to create signal %s", name
);
1003 sig
->invert_data
= false;
1004 sig
->data_mask
= data_mask
;
1005 sig
->invert_oe
= false;
1011 static int create_signals(void)
1013 if (create_default_signal("TCK", 0x01) != ERROR_OK
)
1015 if (create_default_signal("TDI", 0x02) != ERROR_OK
)
1017 if (create_default_signal("TDO", 0x04) != ERROR_OK
)
1019 if (create_default_signal("TMS", 0x08) != ERROR_OK
)
1024 static int ftdi_swd_init(void)
1026 LOG_INFO("FTDI SWD mode enabled");
1029 if (create_signals() != ERROR_OK
)
1032 swd_cmd_queue_alloced
= 10;
1033 swd_cmd_queue
= malloc(swd_cmd_queue_alloced
* sizeof(*swd_cmd_queue
));
1035 return swd_cmd_queue
? ERROR_OK
: ERROR_FAIL
;
1038 static void ftdi_swd_swdio_en(bool enable
)
1040 struct signal
*oe
= find_signal_by_name("SWDIO_OE");
1043 ftdi_set_signal(oe
, enable
? '1' : '0');
1045 /* Sets TDI/DO pin to input during rx when both pins are connected
1048 direction
|= jtag_direction_init
& 0x0002U
;
1050 direction
&= ~0x0002U
;
1051 mpsse_set_data_bits_low_byte(mpsse_ctx
, output
& 0xff, direction
& 0xff);
1057 * Flush the MPSSE queue and process the SWD transaction queue
1060 static int ftdi_swd_run_queue(void)
1062 LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length
);
1064 struct signal
*led
= find_signal_by_name("LED");
1066 if (queued_retval
!= ERROR_OK
) {
1067 LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval
);
1071 /* A transaction must be followed by another transaction or at least 8 idle cycles to
1072 * ensure that data is clocked through the AP. */
1073 mpsse_clock_data_out(mpsse_ctx
, NULL
, 0, 8, SWD_MODE
);
1075 /* Terminate the "blink", if the current layout has that feature */
1077 ftdi_set_signal(led
, '0');
1079 queued_retval
= mpsse_flush(mpsse_ctx
);
1080 if (queued_retval
!= ERROR_OK
) {
1081 LOG_ERROR("MPSSE failed");
1085 for (size_t i
= 0; i
< swd_cmd_queue_length
; i
++) {
1086 int ack
= buf_get_u32(swd_cmd_queue
[i
].trn_ack_data_parity_trn
, 1, 3);
1088 /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
1089 bool check_ack
= swd_cmd_returns_ack(swd_cmd_queue
[i
].cmd
);
1091 LOG_CUSTOM_LEVEL((check_ack
&& ack
!= SWD_ACK_OK
) ? LOG_LVL_DEBUG
: LOG_LVL_DEBUG_IO
,
1092 "%s%s %s %s reg %X = %08" PRIx32
,
1093 check_ack
? "" : "ack ignored ",
1094 ack
== SWD_ACK_OK
? "OK" : ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK",
1095 swd_cmd_queue
[i
].cmd
& SWD_CMD_APNDP
? "AP" : "DP",
1096 swd_cmd_queue
[i
].cmd
& SWD_CMD_RNW
? "read" : "write",
1097 (swd_cmd_queue
[i
].cmd
& SWD_CMD_A32
) >> 1,
1098 buf_get_u32(swd_cmd_queue
[i
].trn_ack_data_parity_trn
,
1099 1 + 3 + (swd_cmd_queue
[i
].cmd
& SWD_CMD_RNW
? 0 : 1), 32));
1101 if (ack
!= SWD_ACK_OK
&& check_ack
) {
1102 queued_retval
= swd_ack_to_error_code(ack
);
1105 } else if (swd_cmd_queue
[i
].cmd
& SWD_CMD_RNW
) {
1106 uint32_t data
= buf_get_u32(swd_cmd_queue
[i
].trn_ack_data_parity_trn
, 1 + 3, 32);
1107 int parity
= buf_get_u32(swd_cmd_queue
[i
].trn_ack_data_parity_trn
, 1 + 3 + 32, 1);
1109 if (parity
!= parity_u32(data
)) {
1110 LOG_ERROR("SWD Read data parity mismatch");
1111 queued_retval
= ERROR_FAIL
;
1115 if (swd_cmd_queue
[i
].dst
)
1116 *swd_cmd_queue
[i
].dst
= data
;
1121 swd_cmd_queue_length
= 0;
1122 retval
= queued_retval
;
1123 queued_retval
= ERROR_OK
;
1125 /* Queue a new "blink" */
1126 if (led
&& retval
== ERROR_OK
)
1127 ftdi_set_signal(led
, '1');
1132 static void ftdi_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
)
1134 if (swd_cmd_queue_length
>= swd_cmd_queue_alloced
) {
1135 /* Not enough room in the queue. Run the queue and increase its size for next time.
1136 * Note that it's not possible to avoid running the queue here, because mpsse contains
1137 * pointers into the queue which may be invalid after the realloc. */
1138 queued_retval
= ftdi_swd_run_queue();
1139 struct swd_cmd_queue_entry
*q
= realloc(swd_cmd_queue
, swd_cmd_queue_alloced
* 2 * sizeof(*swd_cmd_queue
));
1142 swd_cmd_queue_alloced
*= 2;
1143 LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced
);
1147 if (queued_retval
!= ERROR_OK
)
1150 size_t i
= swd_cmd_queue_length
++;
1151 swd_cmd_queue
[i
].cmd
= cmd
| SWD_CMD_START
| SWD_CMD_PARK
;
1153 mpsse_clock_data_out(mpsse_ctx
, &swd_cmd_queue
[i
].cmd
, 0, 8, SWD_MODE
);
1155 if (swd_cmd_queue
[i
].cmd
& SWD_CMD_RNW
) {
1156 /* Queue a read transaction */
1157 swd_cmd_queue
[i
].dst
= dst
;
1159 ftdi_swd_swdio_en(false);
1160 mpsse_clock_data_in(mpsse_ctx
, swd_cmd_queue
[i
].trn_ack_data_parity_trn
,
1161 0, 1 + 3 + 32 + 1 + 1, SWD_MODE
);
1162 ftdi_swd_swdio_en(true);
1164 /* Queue a write transaction */
1165 ftdi_swd_swdio_en(false);
1167 mpsse_clock_data_in(mpsse_ctx
, swd_cmd_queue
[i
].trn_ack_data_parity_trn
,
1168 0, 1 + 3 + 1, SWD_MODE
);
1170 ftdi_swd_swdio_en(true);
1172 buf_set_u32(swd_cmd_queue
[i
].trn_ack_data_parity_trn
, 1 + 3 + 1, 32, data
);
1173 buf_set_u32(swd_cmd_queue
[i
].trn_ack_data_parity_trn
, 1 + 3 + 1 + 32, 1, parity_u32(data
));
1175 mpsse_clock_data_out(mpsse_ctx
, swd_cmd_queue
[i
].trn_ack_data_parity_trn
,
1176 1 + 3 + 1, 32 + 1, SWD_MODE
);
1179 /* Insert idle cycles after AP accesses to avoid WAIT */
1180 if (cmd
& SWD_CMD_APNDP
)
1181 mpsse_clock_data_out(mpsse_ctx
, NULL
, 0, ap_delay_clk
, SWD_MODE
);
1185 static void ftdi_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
1187 assert(cmd
& SWD_CMD_RNW
);
1188 ftdi_swd_queue_cmd(cmd
, value
, 0, ap_delay_clk
);
1191 static void ftdi_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
1193 assert(!(cmd
& SWD_CMD_RNW
));
1194 ftdi_swd_queue_cmd(cmd
, NULL
, value
, ap_delay_clk
);
1197 static int ftdi_swd_switch_seq(enum swd_special_seq seq
)
1201 LOG_DEBUG("SWD line reset");
1202 ftdi_swd_swdio_en(true);
1203 mpsse_clock_data_out(mpsse_ctx
, swd_seq_line_reset
, 0, swd_seq_line_reset_len
, SWD_MODE
);
1206 LOG_DEBUG("JTAG-to-SWD");
1207 ftdi_swd_swdio_en(true);
1208 mpsse_clock_data_out(mpsse_ctx
, swd_seq_jtag_to_swd
, 0, swd_seq_jtag_to_swd_len
, SWD_MODE
);
1210 case JTAG_TO_DORMANT
:
1211 LOG_DEBUG("JTAG-to-DORMANT");
1212 ftdi_swd_swdio_en(true);
1213 mpsse_clock_data_out(mpsse_ctx
, swd_seq_jtag_to_dormant
, 0, swd_seq_jtag_to_dormant_len
, SWD_MODE
);
1216 LOG_DEBUG("SWD-to-JTAG");
1217 ftdi_swd_swdio_en(true);
1218 mpsse_clock_data_out(mpsse_ctx
, swd_seq_swd_to_jtag
, 0, swd_seq_swd_to_jtag_len
, SWD_MODE
);
1220 case SWD_TO_DORMANT
:
1221 LOG_DEBUG("SWD-to-DORMANT");
1222 ftdi_swd_swdio_en(true);
1223 mpsse_clock_data_out(mpsse_ctx
, swd_seq_swd_to_dormant
, 0, swd_seq_swd_to_dormant_len
, SWD_MODE
);
1225 case DORMANT_TO_SWD
:
1226 LOG_DEBUG("DORMANT-to-SWD");
1227 ftdi_swd_swdio_en(true);
1228 mpsse_clock_data_out(mpsse_ctx
, swd_seq_dormant_to_swd
, 0, swd_seq_dormant_to_swd_len
, SWD_MODE
);
1230 case DORMANT_TO_JTAG
:
1231 LOG_DEBUG("DORMANT-to-JTAG");
1232 ftdi_swd_swdio_en(true);
1233 mpsse_clock_data_out(mpsse_ctx
, swd_seq_dormant_to_jtag
, 0, swd_seq_dormant_to_jtag_len
, SWD_MODE
);
1236 LOG_ERROR("Sequence %d not supported", seq
);
1243 static const struct swd_driver ftdi_swd
= {
1244 .init
= ftdi_swd_init
,
1245 .switch_seq
= ftdi_swd_switch_seq
,
1246 .read_reg
= ftdi_swd_read_reg
,
1247 .write_reg
= ftdi_swd_write_reg
,
1248 .run
= ftdi_swd_run_queue
,
1251 static const char * const ftdi_transports
[] = { "jtag", "swd", NULL
};
1253 static struct jtag_interface ftdi_interface
= {
1254 .supported
= DEBUG_CAP_TMS_SEQ
,
1255 .execute_queue
= ftdi_execute_queue
,
1258 struct adapter_driver ftdi_adapter_driver
= {
1260 .transports
= ftdi_transports
,
1261 .commands
= ftdi_command_handlers
,
1263 .init
= ftdi_initialize
,
1265 .reset
= ftdi_reset
,
1266 .speed
= ftdi_speed
,
1268 .speed_div
= ftdi_speed_div
,
1270 .jtag_ops
= &ftdi_interface
,
1271 .swd_ops
= &ftdi_swd
,