bitq: remove dead code
[openocd/libswd.git] / src / jtag / drivers / bitq.c
blob4e2dbc21c3fb21ca5f60801890b003aba9470d41
1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
3 * chromy@asix.cz *
4 * *
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. *
9 * *
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. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <jtag/jtag.h>
25 #include "bitq.h"
26 #include <jtag/interface.h>
29 struct bitq_interface* bitq_interface; /* low level bit queue interface */
31 /* state of input queue */
32 struct bitq_state {
33 struct jtag_command *cmd; /* command currently processed */
34 int field_idx; /* index of field currently being processed */
35 int bit_pos; /* position of bit curently being processed */
36 int status; /* processing status */
38 static struct bitq_state bitq_in_state;
41 * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
42 * no parameters, makes use of stored state information
44 void bitq_in_proc(void)
46 /* static information preserved between calls to increase performance */
47 static uint8_t* in_buff; /* pointer to buffer for scanned data */
48 static int in_idx; /* index of byte being scanned */
49 static uint8_t in_mask; /* mask of next bit to be scanned */
51 struct scan_field* field;
52 int tdo;
54 /* loop through the queue */
55 while (bitq_in_state.cmd)
57 /* only JTAG_SCAN command may return data */
58 if (bitq_in_state.cmd->type == JTAG_SCAN)
60 /* loop through the fields */
61 while (bitq_in_state.field_idx < bitq_in_state.cmd->cmd.scan->num_fields)
63 field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
64 if (field->in_value)
66 if (bitq_in_state.bit_pos == 0)
68 /* initialize field scanning */
69 in_mask = 0x01;
70 in_idx = 0;
71 in_buff = field->in_value;
74 /* field scanning */
75 while (bitq_in_state.bit_pos < field->num_bits)
77 if ((tdo = bitq_interface->in()) < 0)
79 #ifdef _DEBUG_JTAG_IO_
80 LOG_DEBUG("bitq in EOF");
81 #endif
82 return;
84 if (in_mask == 0x01)
85 in_buff[in_idx] = 0;
86 if (tdo)
87 in_buff[in_idx] |= in_mask;
88 if (in_mask == 0x80)
90 in_mask = 0x01;
91 in_idx++;
93 else
94 in_mask <<= 1;
95 bitq_in_state.bit_pos++;
99 bitq_in_state.field_idx++; /* advance to next field */
100 bitq_in_state.bit_pos = 0; /* start next field from the first bit */
103 bitq_in_state.cmd = bitq_in_state.cmd->next; /* advance to next command */
104 bitq_in_state.field_idx = 0; /* preselect first field */
109 void bitq_io(int tms, int tdi, int tdo_req)
111 bitq_interface->out(tms, tdi, tdo_req);
112 /* check and process the input queue */
113 if (bitq_interface->in_rdy())
114 bitq_in_proc();
118 void bitq_end_state(tap_state_t state)
120 if (!tap_is_state_stable(state))
122 LOG_ERROR("BUG: %i is not a valid end state", state);
123 exit(-1);
125 tap_set_end_state(state);
129 void bitq_state_move(tap_state_t new_state)
131 int i = 0;
132 uint8_t tms_scan;
134 if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state))
136 LOG_ERROR("TAP move from or to unstable state");
137 exit(-1);
140 tms_scan = tap_get_tms_path(tap_get_state(), new_state);
141 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
143 for (i = 0; i < tms_count; i++)
145 bitq_io(tms_scan & 1, 0, 0);
146 tms_scan >>= 1;
149 tap_set_state(new_state);
153 void bitq_path_move(struct pathmove_command* cmd)
155 int i;
157 for (i = 0; i <= cmd->num_states; i++)
159 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
160 bitq_io(0, 0, 0);
161 else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
162 bitq_io(1, 0, 0);
163 else
165 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
166 tap_get_state()), tap_state_name(cmd->path[i]));
167 exit(-1);
170 tap_set_state(cmd->path[i]);
173 tap_set_end_state(tap_get_state());
177 void bitq_runtest(int num_cycles)
179 int i;
181 /* only do a state_move when we're not already in IDLE */
182 if (tap_get_state() != TAP_IDLE)
183 bitq_state_move(TAP_IDLE);
185 /* execute num_cycles */
186 for (i = 0; i < num_cycles; i++)
187 bitq_io(0, 0, 0);
189 /* finish in end_state */
190 if (tap_get_state() != tap_get_end_state())
191 bitq_state_move(tap_get_end_state());
195 void bitq_scan_field(struct scan_field* field, int do_pause)
197 int bit_cnt;
198 int tdo_req;
200 const uint8_t* out_ptr;
201 uint8_t out_mask;
203 if (field->in_value)
204 tdo_req = 1;
205 else
206 tdo_req = 0;
208 if (field->out_value == NULL)
210 /* just send zeros and request data from TDO */
211 for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--)
212 bitq_io(0, 0, tdo_req);
214 bitq_io(do_pause, 0, tdo_req);
216 else
218 /* send data, and optionally request TDO */
219 out_mask = 0x01;
220 out_ptr = field->out_value;
221 for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--)
223 bitq_io(0, ((*out_ptr) & out_mask) != 0, tdo_req);
224 if (out_mask == 0x80)
226 out_mask = 0x01;
227 out_ptr++;
229 else
230 out_mask <<= 1;
233 bitq_io(do_pause, ((*out_ptr) & out_mask) != 0, tdo_req);
236 if (do_pause)
238 bitq_io(0, 0, 0);
239 if (tap_get_state() == TAP_IRSHIFT)
240 tap_set_state(TAP_IRPAUSE);
241 else if (tap_get_state() == TAP_DRSHIFT)
242 tap_set_state(TAP_DRPAUSE);
247 void bitq_scan(struct scan_command* cmd)
249 int i;
251 if (cmd->ir_scan)
252 bitq_state_move(TAP_IRSHIFT);
253 else
254 bitq_state_move(TAP_DRSHIFT);
256 for (i = 0; i < cmd->num_fields - 1; i++)
257 bitq_scan_field(&cmd->fields[i], 0);
259 bitq_scan_field(&cmd->fields[i], 1);
263 int bitq_execute_queue(void)
265 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
267 bitq_in_state.cmd = jtag_command_queue;
268 bitq_in_state.field_idx = 0;
269 bitq_in_state.bit_pos = 0;
270 bitq_in_state.status = ERROR_OK;
272 while (cmd)
274 switch (cmd->type)
276 case JTAG_RESET:
277 #ifdef _DEBUG_JTAG_IO_
278 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
279 #endif
280 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
282 tap_set_state(TAP_RESET);
284 bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
285 if (bitq_interface->in_rdy())
286 bitq_in_proc();
287 break;
289 case JTAG_RUNTEST:
290 #ifdef _DEBUG_JTAG_IO_
291 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
292 #endif
293 bitq_end_state(cmd->cmd.runtest->end_state);
294 bitq_runtest(cmd->cmd.runtest->num_cycles);
295 break;
297 case JTAG_TLR_RESET:
298 #ifdef _DEBUG_JTAG_IO_
299 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
300 #endif
301 bitq_end_state(cmd->cmd.statemove->end_state);
302 bitq_state_move(tap_get_end_state()); /* uncoditional TAP move */
303 break;
305 case JTAG_PATHMOVE:
306 #ifdef _DEBUG_JTAG_IO_
307 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
308 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
309 #endif
310 bitq_path_move(cmd->cmd.pathmove);
311 break;
313 case JTAG_SCAN:
314 #ifdef _DEBUG_JTAG_IO_
315 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
316 if (cmd->cmd.scan->ir_scan)
317 LOG_DEBUG("scan ir");
318 else
319 LOG_DEBUG("scan dr");
320 #endif
321 bitq_end_state(cmd->cmd.scan->end_state);
322 bitq_scan(cmd->cmd.scan);
323 if (tap_get_state() != tap_get_end_state())
324 bitq_state_move(tap_get_end_state());
325 break;
327 case JTAG_SLEEP:
328 #ifdef _DEBUG_JTAG_IO_
329 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
330 #endif
331 bitq_interface->sleep(cmd->cmd.sleep->us);
332 if (bitq_interface->in_rdy())
333 bitq_in_proc();
334 break;
336 default:
337 LOG_ERROR("BUG: unknown JTAG command type encountered");
338 exit(-1);
341 cmd = cmd->next;
344 bitq_interface->flush();
345 bitq_in_proc();
347 if (bitq_in_state.cmd)
349 LOG_ERROR("missing data from bitq interface");
350 return ERROR_JTAG_QUEUE_FAILED;
352 if (bitq_interface->in() >= 0)
354 LOG_ERROR("extra data from bitq interface");
355 return ERROR_JTAG_QUEUE_FAILED;
358 return bitq_in_state.status;
362 void bitq_cleanup(void)