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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
27 bitq_interface_t* bitq_interface; /* low level bit queue interface */
29 static bitq_state_t bitq_in_state; /* state of input queue */
31 static u8* bitq_in_buffer; /* buffer dynamically reallocated as needed */
32 static int bitq_in_bufsize = 32; /* min. buffer size */
35 * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
36 * also the buffer for incomming data is reallocated only if necessary
37 * no parameters, makes use of stored state information
39 void bitq_in_proc(void)
41 /* static information preserved between calls to increase performance */
42 static u8* in_buff; /* pointer to buffer for scanned data */
43 static int in_idx; /* index of byte being scanned */
44 static u8 in_mask; /* mask of next bit to be scanned */
49 /* loop through the queue */
50 while (bitq_in_state.cmd)
52 /* only JTAG_SCAN command may return data */
53 if (bitq_in_state.cmd->type==JTAG_SCAN)
55 /* loop through the fields */
56 while (bitq_in_state.field_idx<bitq_in_state.cmd->cmd.scan->num_fields)
58 field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
61 if (bitq_in_state.bit_pos==0)
63 /* initialize field scanning */
67 in_buff = field->in_value;
70 /* buffer reallocation needed? */
71 if (field->num_bits>bitq_in_bufsize * 8)
73 /* buffer previously allocated? */
74 if (bitq_in_buffer!=NULL)
78 bitq_in_buffer = NULL;
80 /* double the buffer size until it fits */
81 while (field->num_bits>bitq_in_bufsize * 8)
84 /* if necessary, allocate buffer and check for malloc error */
85 if (bitq_in_buffer==NULL && ( bitq_in_buffer = malloc(bitq_in_bufsize) )==NULL)
87 LOG_ERROR("malloc error");
90 in_buff = (void*) bitq_in_buffer;
95 while (bitq_in_state.bit_pos<field->num_bits)
97 if ( ( tdo = bitq_interface->in() )<0 )
99 #ifdef _DEBUG_JTAG_IO_
100 LOG_DEBUG("bitq in EOF");
107 in_buff[in_idx] |= in_mask;
115 bitq_in_state.bit_pos++;
119 bitq_in_state.field_idx++; /* advance to next field */
120 bitq_in_state.bit_pos = 0; /* start next field from the first bit */
123 bitq_in_state.cmd = bitq_in_state.cmd->next; /* advance to next command */
124 bitq_in_state.field_idx = 0; /* preselect first field */
129 void bitq_io(int tms, int tdi, int tdo_req)
131 bitq_interface->out(tms, tdi, tdo_req);
132 /* check and process the input queue */
133 if ( bitq_interface->in_rdy() )
138 void bitq_end_state(tap_state_t state)
140 if (state==TAP_INVALID)
143 if (!tap_is_state_stable(state))
145 LOG_ERROR("BUG: %i is not a valid end state", state);
148 tap_set_end_state(state);
152 void bitq_state_move(tap_state_t new_state)
157 if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state))
159 LOG_ERROR("TAP move from or to unstable state");
163 tms_scan = tap_get_tms_path(tap_get_state(), new_state);
165 for (i = 0; i<7; i++)
167 bitq_io(tms_scan & 1, 0, 0);
171 tap_set_state(new_state);
175 void bitq_path_move(pathmove_command_t* cmd)
179 for (i = 0; i<=cmd->num_states; i++)
181 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
183 else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
187 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
188 tap_get_state() ), tap_state_name(cmd->path[i]) );
192 tap_set_state(cmd->path[i]);
195 tap_set_end_state( tap_get_state() );
199 void bitq_runtest(int num_cycles)
203 /* only do a state_move when we're not already in IDLE */
204 if (tap_get_state() != TAP_IDLE)
205 bitq_state_move(TAP_IDLE);
207 /* execute num_cycles */
208 for (i = 0; i < num_cycles; i++)
211 /* finish in end_state */
212 if ( tap_get_state() != tap_get_end_state() )
213 bitq_state_move( tap_get_end_state() );
217 void bitq_scan_field(scan_field_t* field, int pause)
230 if (field->out_value==NULL)
232 /* just send zeros and request data from TDO */
233 for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
234 bitq_io(0, 0, tdo_req);
236 bitq_io(pause, 0, tdo_req);
240 /* send data, and optionally request TDO */
242 out_ptr = field->out_value;
243 for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
245 bitq_io(0, ( (*out_ptr) & out_mask )!=0, tdo_req);
255 bitq_io(pause, ( (*out_ptr) & out_mask )!=0, tdo_req);
261 if (tap_get_state()==TAP_IRSHIFT)
262 tap_set_state(TAP_IRPAUSE);
263 else if (tap_get_state()==TAP_DRSHIFT)
264 tap_set_state(TAP_DRPAUSE);
269 void bitq_scan(scan_command_t* cmd)
274 bitq_state_move(TAP_IRSHIFT);
276 bitq_state_move(TAP_DRSHIFT);
278 for (i = 0; i < cmd->num_fields - 1; i++)
279 bitq_scan_field(&cmd->fields[i], 0);
281 bitq_scan_field(&cmd->fields[i], 1);
285 int bitq_execute_queue(void)
287 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
289 bitq_in_state.cmd = jtag_command_queue;
290 bitq_in_state.field_idx = 0;
291 bitq_in_state.bit_pos = 0;
292 bitq_in_state.status = ERROR_OK;
299 #ifdef _DEBUG_JTAG_IO_
300 LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
302 bitq_end_state(cmd->cmd.end_state->end_state);
306 #ifdef _DEBUG_JTAG_IO_
307 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
309 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
311 tap_set_state(TAP_RESET);
313 bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
314 if ( bitq_interface->in_rdy() )
319 #ifdef _DEBUG_JTAG_IO_
320 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
322 bitq_end_state(cmd->cmd.runtest->end_state);
323 bitq_runtest(cmd->cmd.runtest->num_cycles);
327 #ifdef _DEBUG_JTAG_IO_
328 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
330 bitq_end_state(cmd->cmd.statemove->end_state);
331 bitq_state_move( tap_get_end_state() ); /* uncoditional TAP move */
335 #ifdef _DEBUG_JTAG_IO_
336 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
337 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
339 bitq_path_move(cmd->cmd.pathmove);
343 #ifdef _DEBUG_JTAG_IO_
344 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
345 if (cmd->cmd.scan->ir_scan)
346 LOG_DEBUG("scan ir");
348 LOG_DEBUG("scan dr");
350 bitq_end_state(cmd->cmd.scan->end_state);
351 bitq_scan(cmd->cmd.scan);
352 if ( tap_get_state() != tap_get_end_state() )
353 bitq_state_move( tap_get_end_state() );
357 #ifdef _DEBUG_JTAG_IO_
358 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
360 bitq_interface->sleep(cmd->cmd.sleep->us);
361 if ( bitq_interface->in_rdy() )
366 LOG_ERROR("BUG: unknown JTAG command type encountered");
373 bitq_interface->flush();
376 if (bitq_in_state.cmd)
378 LOG_ERROR("missing data from bitq interface");
379 return ERROR_JTAG_QUEUE_FAILED;
381 if (bitq_interface->in()>=0)
383 LOG_ERROR("extra data from bitq interface");
384 return ERROR_JTAG_QUEUE_FAILED;
387 return bitq_in_state.status;
391 void bitq_cleanup(void)
393 if (bitq_in_buffer!=NULL)
395 free(bitq_in_buffer);
396 bitq_in_buffer = NULL;