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 ***************************************************************************/
25 #include "interface.h"
28 struct bitq_interface
* bitq_interface
; /* low level bit queue interface */
30 /* state of input queue */
32 struct jtag_command
*cmd
; /* command currently processed */
33 int field_idx
; /* index of field currently being processed */
34 int bit_pos
; /* position of bit curently being processed */
35 int status
; /* processing status */
37 static struct bitq_state bitq_in_state
;
39 static uint8_t* bitq_in_buffer
; /* buffer dynamically reallocated as needed */
40 static int bitq_in_bufsize
= 32; /* min. buffer size */
43 * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
44 * also the buffer for incomming data is reallocated only if necessary
45 * no parameters, makes use of stored state information
47 void bitq_in_proc(void)
49 /* static information preserved between calls to increase performance */
50 static uint8_t* in_buff
; /* pointer to buffer for scanned data */
51 static int in_idx
; /* index of byte being scanned */
52 static uint8_t in_mask
; /* mask of next bit to be scanned */
54 struct scan_field
* field
;
57 /* loop through the queue */
58 while (bitq_in_state
.cmd
)
60 /* only JTAG_SCAN command may return data */
61 if (bitq_in_state
.cmd
->type
== JTAG_SCAN
)
63 /* loop through the fields */
64 while (bitq_in_state
.field_idx
< bitq_in_state
.cmd
->cmd
.scan
->num_fields
)
66 field
= &bitq_in_state
.cmd
->cmd
.scan
->fields
[bitq_in_state
.field_idx
];
69 if (bitq_in_state
.bit_pos
== 0)
71 /* initialize field scanning */
75 in_buff
= field
->in_value
;
78 /* buffer reallocation needed? */
79 if (field
->num_bits
> bitq_in_bufsize
* 8)
81 /* buffer previously allocated? */
82 if (bitq_in_buffer
!= NULL
)
86 bitq_in_buffer
= NULL
;
88 /* double the buffer size until it fits */
89 while (field
->num_bits
> bitq_in_bufsize
* 8)
92 /* if necessary, allocate buffer and check for malloc error */
93 if (bitq_in_buffer
== NULL
&& (bitq_in_buffer
= malloc(bitq_in_bufsize
)) == NULL
)
95 LOG_ERROR("malloc error");
98 in_buff
= (void*) bitq_in_buffer
;
103 while (bitq_in_state
.bit_pos
< field
->num_bits
)
105 if ((tdo
= bitq_interface
->in()) < 0)
107 #ifdef _DEBUG_JTAG_IO_
108 LOG_DEBUG("bitq in EOF");
115 in_buff
[in_idx
] |= in_mask
;
123 bitq_in_state
.bit_pos
++;
127 bitq_in_state
.field_idx
++; /* advance to next field */
128 bitq_in_state
.bit_pos
= 0; /* start next field from the first bit */
131 bitq_in_state
.cmd
= bitq_in_state
.cmd
->next
; /* advance to next command */
132 bitq_in_state
.field_idx
= 0; /* preselect first field */
137 void bitq_io(int tms
, int tdi
, int tdo_req
)
139 bitq_interface
->out(tms
, tdi
, tdo_req
);
140 /* check and process the input queue */
141 if (bitq_interface
->in_rdy())
146 void bitq_end_state(tap_state_t state
)
148 if (!tap_is_state_stable(state
))
150 LOG_ERROR("BUG: %i is not a valid end state", state
);
153 tap_set_end_state(state
);
157 void bitq_state_move(tap_state_t new_state
)
162 if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state
))
164 LOG_ERROR("TAP move from or to unstable state");
168 tms_scan
= tap_get_tms_path(tap_get_state(), new_state
);
169 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
171 for (i
= 0; i
< tms_count
; i
++)
173 bitq_io(tms_scan
& 1, 0, 0);
177 tap_set_state(new_state
);
181 void bitq_path_move(struct pathmove_command
* cmd
)
185 for (i
= 0; i
<= cmd
->num_states
; i
++)
187 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[i
])
189 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[i
])
193 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
194 tap_get_state()), tap_state_name(cmd
->path
[i
]));
198 tap_set_state(cmd
->path
[i
]);
201 tap_set_end_state(tap_get_state());
205 void bitq_runtest(int num_cycles
)
209 /* only do a state_move when we're not already in IDLE */
210 if (tap_get_state() != TAP_IDLE
)
211 bitq_state_move(TAP_IDLE
);
213 /* execute num_cycles */
214 for (i
= 0; i
< num_cycles
; i
++)
217 /* finish in end_state */
218 if (tap_get_state() != tap_get_end_state())
219 bitq_state_move(tap_get_end_state());
223 void bitq_scan_field(struct scan_field
* field
, int pause
)
236 if (field
->out_value
== NULL
)
238 /* just send zeros and request data from TDO */
239 for (bit_cnt
= field
->num_bits
; bit_cnt
> 1; bit_cnt
--)
240 bitq_io(0, 0, tdo_req
);
242 bitq_io(pause
, 0, tdo_req
);
246 /* send data, and optionally request TDO */
248 out_ptr
= field
->out_value
;
249 for (bit_cnt
= field
->num_bits
; bit_cnt
> 1; bit_cnt
--)
251 bitq_io(0, ((*out_ptr
) & out_mask
) != 0, tdo_req
);
252 if (out_mask
== 0x80)
261 bitq_io(pause
, ((*out_ptr
) & out_mask
) != 0, tdo_req
);
267 if (tap_get_state() == TAP_IRSHIFT
)
268 tap_set_state(TAP_IRPAUSE
);
269 else if (tap_get_state() == TAP_DRSHIFT
)
270 tap_set_state(TAP_DRPAUSE
);
275 void bitq_scan(struct scan_command
* cmd
)
280 bitq_state_move(TAP_IRSHIFT
);
282 bitq_state_move(TAP_DRSHIFT
);
284 for (i
= 0; i
< cmd
->num_fields
- 1; i
++)
285 bitq_scan_field(&cmd
->fields
[i
], 0);
287 bitq_scan_field(&cmd
->fields
[i
], 1);
291 int bitq_execute_queue(void)
293 struct jtag_command
* cmd
= jtag_command_queue
; /* currently processed command */
295 bitq_in_state
.cmd
= jtag_command_queue
;
296 bitq_in_state
.field_idx
= 0;
297 bitq_in_state
.bit_pos
= 0;
298 bitq_in_state
.status
= ERROR_OK
;
305 #ifdef _DEBUG_JTAG_IO_
306 LOG_DEBUG("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
308 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
310 tap_set_state(TAP_RESET
);
312 bitq_interface
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
313 if (bitq_interface
->in_rdy())
318 #ifdef _DEBUG_JTAG_IO_
319 LOG_DEBUG("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
321 bitq_end_state(cmd
->cmd
.runtest
->end_state
);
322 bitq_runtest(cmd
->cmd
.runtest
->num_cycles
);
326 #ifdef _DEBUG_JTAG_IO_
327 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
329 bitq_end_state(cmd
->cmd
.statemove
->end_state
);
330 bitq_state_move(tap_get_end_state()); /* uncoditional TAP move */
334 #ifdef _DEBUG_JTAG_IO_
335 LOG_DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
,
336 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
338 bitq_path_move(cmd
->cmd
.pathmove
);
342 #ifdef _DEBUG_JTAG_IO_
343 LOG_DEBUG("scan end in %i", cmd
->cmd
.scan
->end_state
);
344 if (cmd
->cmd
.scan
->ir_scan
)
345 LOG_DEBUG("scan ir");
347 LOG_DEBUG("scan dr");
349 bitq_end_state(cmd
->cmd
.scan
->end_state
);
350 bitq_scan(cmd
->cmd
.scan
);
351 if (tap_get_state() != tap_get_end_state())
352 bitq_state_move(tap_get_end_state());
356 #ifdef _DEBUG_JTAG_IO_
357 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
359 bitq_interface
->sleep(cmd
->cmd
.sleep
->us
);
360 if (bitq_interface
->in_rdy())
365 LOG_ERROR("BUG: unknown JTAG command type encountered");
372 bitq_interface
->flush();
375 if (bitq_in_state
.cmd
)
377 LOG_ERROR("missing data from bitq interface");
378 return ERROR_JTAG_QUEUE_FAILED
;
380 if (bitq_interface
->in() >= 0)
382 LOG_ERROR("extra data from bitq interface");
383 return ERROR_JTAG_QUEUE_FAILED
;
386 return bitq_in_state
.status
;
390 void bitq_cleanup(void)
392 if (bitq_in_buffer
!= NULL
)
394 free(bitq_in_buffer
);
395 bitq_in_buffer
= NULL
;