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 ***************************************************************************/
24 #include <jtag/jtag.h>
26 #include <jtag/interface.h>
29 struct bitq_interface
* bitq_interface
; /* low level bit queue interface */
31 /* state of input queue */
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
;
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
];
66 if (bitq_in_state
.bit_pos
== 0)
68 /* initialize field scanning */
71 in_buff
= field
->in_value
;
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");
87 in_buff
[in_idx
] |= in_mask
;
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())
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
);
125 tap_set_end_state(state
);
129 void bitq_state_move(tap_state_t new_state
)
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");
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);
149 tap_set_state(new_state
);
153 void bitq_path_move(struct pathmove_command
* cmd
)
157 for (i
= 0; i
<= cmd
->num_states
; i
++)
159 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[i
])
161 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[i
])
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
]));
170 tap_set_state(cmd
->path
[i
]);
173 tap_set_end_state(tap_get_state());
177 void bitq_runtest(int num_cycles
)
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
++)
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
)
200 const uint8_t* out_ptr
;
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
);
218 /* send data, and optionally request TDO */
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)
233 bitq_io(do_pause
, ((*out_ptr
) & out_mask
) != 0, tdo_req
);
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
)
252 bitq_state_move(TAP_IRSHIFT
);
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
;
277 #ifdef _DEBUG_JTAG_IO_
278 LOG_DEBUG("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
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())
290 #ifdef _DEBUG_JTAG_IO_
291 LOG_DEBUG("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
293 bitq_end_state(cmd
->cmd
.runtest
->end_state
);
294 bitq_runtest(cmd
->cmd
.runtest
->num_cycles
);
298 #ifdef _DEBUG_JTAG_IO_
299 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
301 bitq_end_state(cmd
->cmd
.statemove
->end_state
);
302 bitq_state_move(tap_get_end_state()); /* uncoditional TAP move */
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]);
310 bitq_path_move(cmd
->cmd
.pathmove
);
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");
319 LOG_DEBUG("scan dr");
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());
328 #ifdef _DEBUG_JTAG_IO_
329 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
331 bitq_interface
->sleep(cmd
->cmd
.sleep
->us
);
332 if (bitq_interface
->in_rdy())
337 LOG_ERROR("BUG: unknown JTAG command type encountered");
344 bitq_interface
->flush();
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)