1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2007 by Pavel Chromy *
6 ***************************************************************************/
12 #include <jtag/jtag.h>
14 #include <jtag/interface.h>
16 struct bitq_interface
*bitq_interface
; /* low level bit queue interface */
18 /* state of input queue */
20 struct jtag_command
*cmd
; /* command currently processed */
21 int field_idx
; /* index of field currently being processed */
22 int bit_pos
; /* position of bit currently being processed */
23 int status
; /* processing status */
25 static struct bitq_state bitq_in_state
;
28 * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
29 * no parameters, makes use of stored state information
31 static void bitq_in_proc(void)
33 /* loop through the queue */
34 while (bitq_in_state
.cmd
) {
35 /* only JTAG_SCAN command may return data */
36 if (bitq_in_state
.cmd
->type
== JTAG_SCAN
) {
37 /* loop through the fields */
38 while (bitq_in_state
.field_idx
< bitq_in_state
.cmd
->cmd
.scan
->num_fields
) {
39 struct scan_field
*field
;
40 field
= &bitq_in_state
.cmd
->cmd
.scan
->fields
[bitq_in_state
.field_idx
];
41 if (field
->in_value
) {
43 while (bitq_in_state
.bit_pos
< field
->num_bits
) {
44 /* index of byte being scanned */
45 int in_idx
= bitq_in_state
.bit_pos
/ 8;
46 /* mask of next bit to be scanned */
47 uint8_t in_mask
= 1 << (bitq_in_state
.bit_pos
% 8);
49 int tdo
= bitq_interface
->in();
51 LOG_DEBUG_IO("bitq in EOF");
55 field
->in_value
[in_idx
] = 0;
57 field
->in_value
[in_idx
] |= in_mask
;
58 bitq_in_state
.bit_pos
++;
62 bitq_in_state
.field_idx
++; /* advance to next field */
63 bitq_in_state
.bit_pos
= 0; /* start next field from the first bit */
66 bitq_in_state
.cmd
= bitq_in_state
.cmd
->next
; /* advance to next command */
67 bitq_in_state
.field_idx
= 0; /* preselect first field */
71 static void bitq_io(int tms
, int tdi
, int tdo_req
)
73 bitq_interface
->out(tms
, tdi
, tdo_req
);
74 /* check and process the input queue */
75 if (bitq_interface
->in_rdy())
79 static void bitq_end_state(tap_state_t state
)
81 if (!tap_is_state_stable(state
)) {
82 LOG_ERROR("BUG: %i is not a valid end state", state
);
85 tap_set_end_state(state
);
88 static void bitq_state_move(tap_state_t new_state
)
93 if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state
)) {
94 LOG_ERROR("TAP move from or to unstable state");
98 tms_scan
= tap_get_tms_path(tap_get_state(), new_state
);
99 int tms_count
= tap_get_tms_path_len(tap_get_state(), new_state
);
101 for (i
= 0; i
< tms_count
; i
++) {
102 bitq_io(tms_scan
& 1, 0, 0);
106 tap_set_state(new_state
);
109 static void bitq_path_move(struct pathmove_command
*cmd
)
113 for (i
= 0; i
< cmd
->num_states
; i
++) {
114 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[i
])
116 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[i
])
119 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
120 tap_get_state()), tap_state_name(cmd
->path
[i
]));
124 tap_set_state(cmd
->path
[i
]);
127 tap_set_end_state(tap_get_state());
130 static void bitq_runtest(int num_cycles
)
134 /* only do a state_move when we're not already in IDLE */
135 if (tap_get_state() != TAP_IDLE
)
136 bitq_state_move(TAP_IDLE
);
138 /* execute num_cycles */
139 for (i
= 0; i
< num_cycles
; i
++)
142 /* finish in end_state */
143 if (tap_get_state() != tap_get_end_state())
144 bitq_state_move(tap_get_end_state());
147 static void bitq_scan_field(struct scan_field
*field
, int do_pause
)
152 const uint8_t *out_ptr
;
160 if (!field
->out_value
) {
161 /* just send zeros and request data from TDO */
162 for (bit_cnt
= field
->num_bits
; bit_cnt
> 1; bit_cnt
--)
163 bitq_io(0, 0, tdo_req
);
165 bitq_io(do_pause
, 0, tdo_req
);
167 /* send data, and optionally request TDO */
169 out_ptr
= field
->out_value
;
170 for (bit_cnt
= field
->num_bits
; bit_cnt
> 1; bit_cnt
--) {
171 bitq_io(0, ((*out_ptr
) & out_mask
) != 0, tdo_req
);
172 if (out_mask
== 0x80) {
179 bitq_io(do_pause
, ((*out_ptr
) & out_mask
) != 0, tdo_req
);
184 if (tap_get_state() == TAP_IRSHIFT
)
185 tap_set_state(TAP_IRPAUSE
);
186 else if (tap_get_state() == TAP_DRSHIFT
)
187 tap_set_state(TAP_DRPAUSE
);
191 static void bitq_scan(struct scan_command
*cmd
)
196 bitq_state_move(TAP_IRSHIFT
);
198 bitq_state_move(TAP_DRSHIFT
);
200 for (i
= 0; i
< cmd
->num_fields
- 1; i
++)
201 bitq_scan_field(&cmd
->fields
[i
], 0);
203 bitq_scan_field(&cmd
->fields
[i
], 1);
206 int bitq_execute_queue(struct jtag_command
*cmd_queue
)
208 struct jtag_command
*cmd
= cmd_queue
; /* currently processed command */
210 bitq_in_state
.cmd
= cmd_queue
;
211 bitq_in_state
.field_idx
= 0;
212 bitq_in_state
.bit_pos
= 0;
213 bitq_in_state
.status
= ERROR_OK
;
218 LOG_DEBUG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
219 if ((cmd
->cmd
.reset
->trst
== 1) ||
220 (cmd
->cmd
.reset
->srst
&&
221 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
222 tap_set_state(TAP_RESET
);
223 bitq_interface
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
224 if (bitq_interface
->in_rdy())
229 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
230 bitq_end_state(cmd
->cmd
.runtest
->end_state
);
231 bitq_runtest(cmd
->cmd
.runtest
->num_cycles
);
235 LOG_DEBUG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
236 bitq_end_state(cmd
->cmd
.statemove
->end_state
);
237 bitq_state_move(tap_get_end_state()); /* unconditional TAP move */
241 LOG_DEBUG_IO("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
,
242 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
243 bitq_path_move(cmd
->cmd
.pathmove
);
247 LOG_DEBUG_IO("scan end in %i", cmd
->cmd
.scan
->end_state
);
248 LOG_DEBUG_IO("scan %s", cmd
->cmd
.scan
->ir_scan
? "ir" : "dr");
249 bitq_end_state(cmd
->cmd
.scan
->end_state
);
250 bitq_scan(cmd
->cmd
.scan
);
251 if (tap_get_state() != tap_get_end_state())
252 bitq_state_move(tap_get_end_state());
256 LOG_DEBUG_IO("sleep %" PRIu32
, cmd
->cmd
.sleep
->us
);
257 bitq_interface
->sleep(cmd
->cmd
.sleep
->us
);
258 if (bitq_interface
->in_rdy())
263 LOG_ERROR("BUG: unknown JTAG command type encountered");
270 bitq_interface
->flush();
273 if (bitq_in_state
.cmd
) {
274 LOG_ERROR("missing data from bitq interface");
275 return ERROR_JTAG_QUEUE_FAILED
;
277 if (bitq_interface
->in() >= 0) {
278 LOG_ERROR("extra data from bitq interface");
279 return ERROR_JTAG_QUEUE_FAILED
;
282 return bitq_in_state
.status
;
285 void bitq_cleanup(void)