1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
23 #include <jtag/jtag.h>
25 #include <jtag/interface.h>
27 struct bitq_interface *bitq_interface; /* low level bit queue interface */
29 /* state of input queue */
31 struct jtag_command *cmd; /* command currently processed */
32 int field_idx; /* index of field currently being processed */
33 int bit_pos; /* position of bit currently being processed */
34 int status; /* processing status */
36 static struct bitq_state bitq_in_state;
39 * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
40 * no parameters, makes use of stored state information
42 static void bitq_in_proc(void)
44 /* loop through the queue */
45 while (bitq_in_state.cmd) {
46 /* only JTAG_SCAN command may return data */
47 if (bitq_in_state.cmd->type == JTAG_SCAN) {
48 /* loop through the fields */
49 while (bitq_in_state.field_idx < bitq_in_state.cmd->cmd.scan->num_fields) {
50 struct scan_field *field;
51 field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
52 if (field->in_value) {
54 while (bitq_in_state.bit_pos < field->num_bits) {
55 /* index of byte being scanned */
56 int in_idx = bitq_in_state.bit_pos / 8;
57 /* mask of next bit to be scanned */
58 uint8_t in_mask = 1 << (bitq_in_state.bit_pos % 8);
60 int tdo = bitq_interface->in();
62 LOG_DEBUG_IO("bitq in EOF");
66 field->in_value[in_idx] = 0;
68 field->in_value[in_idx] |= in_mask;
69 bitq_in_state.bit_pos++;
73 bitq_in_state.field_idx++; /* advance to next field */
74 bitq_in_state.bit_pos = 0; /* start next field from the first bit */
77 bitq_in_state.cmd = bitq_in_state.cmd->next; /* advance to next command */
78 bitq_in_state.field_idx = 0; /* preselect first field */
82 static void bitq_io(int tms, int tdi, int tdo_req)
84 bitq_interface->out(tms, tdi, tdo_req);
85 /* check and process the input queue */
86 if (bitq_interface->in_rdy())
90 static void bitq_end_state(tap_state_t state)
92 if (!tap_is_state_stable(state)) {
93 LOG_ERROR("BUG: %i is not a valid end state", state);
96 tap_set_end_state(state);
99 static void bitq_state_move(tap_state_t new_state)
104 if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state)) {
105 LOG_ERROR("TAP move from or to unstable state");
109 tms_scan = tap_get_tms_path(tap_get_state(), new_state);
110 int tms_count = tap_get_tms_path_len(tap_get_state(), new_state);
112 for (i = 0; i < tms_count; i++) {
113 bitq_io(tms_scan & 1, 0, 0);
117 tap_set_state(new_state);
120 static void bitq_path_move(struct pathmove_command *cmd)
124 for (i = 0; i < cmd->num_states; i++) {
125 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
127 else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
130 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
131 tap_get_state()), tap_state_name(cmd->path[i]));
135 tap_set_state(cmd->path[i]);
138 tap_set_end_state(tap_get_state());
141 static void bitq_runtest(int num_cycles)
145 /* only do a state_move when we're not already in IDLE */
146 if (tap_get_state() != TAP_IDLE)
147 bitq_state_move(TAP_IDLE);
149 /* execute num_cycles */
150 for (i = 0; i < num_cycles; i++)
153 /* finish in end_state */
154 if (tap_get_state() != tap_get_end_state())
155 bitq_state_move(tap_get_end_state());
158 static void bitq_scan_field(struct scan_field *field, int do_pause)
163 const uint8_t *out_ptr;
171 if (field->out_value == NULL) {
172 /* just send zeros and request data from TDO */
173 for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--)
174 bitq_io(0, 0, tdo_req);
176 bitq_io(do_pause, 0, tdo_req);
178 /* send data, and optionally request TDO */
180 out_ptr = field->out_value;
181 for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--) {
182 bitq_io(0, ((*out_ptr) & out_mask) != 0, tdo_req);
183 if (out_mask == 0x80) {
190 bitq_io(do_pause, ((*out_ptr) & out_mask) != 0, tdo_req);
195 if (tap_get_state() == TAP_IRSHIFT)
196 tap_set_state(TAP_IRPAUSE);
197 else if (tap_get_state() == TAP_DRSHIFT)
198 tap_set_state(TAP_DRPAUSE);
202 static void bitq_scan(struct scan_command *cmd)
207 bitq_state_move(TAP_IRSHIFT);
209 bitq_state_move(TAP_DRSHIFT);
211 for (i = 0; i < cmd->num_fields - 1; i++)
212 bitq_scan_field(&cmd->fields[i], 0);
214 bitq_scan_field(&cmd->fields[i], 1);
217 int bitq_execute_queue(void)
219 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
221 bitq_in_state.cmd = jtag_command_queue;
222 bitq_in_state.field_idx = 0;
223 bitq_in_state.bit_pos = 0;
224 bitq_in_state.status = ERROR_OK;
229 LOG_DEBUG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
230 if ((cmd->cmd.reset->trst == 1) ||
231 (cmd->cmd.reset->srst &&
232 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
233 tap_set_state(TAP_RESET);
234 bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
235 if (bitq_interface->in_rdy())
240 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
241 bitq_end_state(cmd->cmd.runtest->end_state);
242 bitq_runtest(cmd->cmd.runtest->num_cycles);
246 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
247 bitq_end_state(cmd->cmd.statemove->end_state);
248 bitq_state_move(tap_get_end_state()); /* unconditional TAP move */
252 LOG_DEBUG_IO("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
253 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
254 bitq_path_move(cmd->cmd.pathmove);
258 LOG_DEBUG_IO("scan end in %i", cmd->cmd.scan->end_state);
259 LOG_DEBUG_IO("scan %s", cmd->cmd.scan->ir_scan ? "ir" : "dr");
260 bitq_end_state(cmd->cmd.scan->end_state);
261 bitq_scan(cmd->cmd.scan);
262 if (tap_get_state() != tap_get_end_state())
263 bitq_state_move(tap_get_end_state());
267 LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
268 bitq_interface->sleep(cmd->cmd.sleep->us);
269 if (bitq_interface->in_rdy())
274 LOG_ERROR("BUG: unknown JTAG command type encountered");
281 bitq_interface->flush();
284 if (bitq_in_state.cmd) {
285 LOG_ERROR("missing data from bitq interface");
286 return ERROR_JTAG_QUEUE_FAILED;
288 if (bitq_interface->in() >= 0) {
289 LOG_ERROR("extra data from bitq interface");
290 return ERROR_JTAG_QUEUE_FAILED;
293 return bitq_in_state.status;
296 void bitq_cleanup(void)