1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
34 #include "interface.h"
37 * @see tap_set_state() and tap_get_state() accessors.
38 * Actual name is not important since accessors hide it.
40 static tap_state_t state_follower
= TAP_RESET
;
42 void tap_set_state_impl(tap_state_t new_state
)
44 /* this is the state we think the TAPs are in now, was cur_state */
45 state_follower
= new_state
;
48 tap_state_t
tap_get_state()
50 return state_follower
;
54 * @see tap_set_end_state() and tap_get_end_state() accessors.
55 * Actual name is not important because accessors hide it.
57 static tap_state_t end_state_follower
= TAP_RESET
;
59 void tap_set_end_state(tap_state_t new_end_state
)
61 /* this is the state we think the TAPs will be in at completion of the
62 * current TAP operation, was end_state
64 end_state_follower
= new_end_state
;
67 tap_state_t
tap_get_end_state()
69 return end_state_follower
;
72 int tap_move_ndx(tap_state_t astate
)
74 /* given a stable state, return the index into the tms_seqs[]
75 * array within tap_get_tms_path()
100 LOG_ERROR("FATAL: unstable state \"%s\" in tap_move_ndx()",
101 tap_state_name(astate
));
108 /* tap_move[i][j]: tap movement command to go from state i to state j
109 * encodings of i and j are what tap_move_ndx() reports.
111 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
113 struct tms_sequences
{
119 * These macros allow us to specify TMS state transitions by bits rather than hex bytes.
120 * Read the bits from LSBit first to MSBit last (right-to-left).
122 #define HEX__(n) 0x##n##LU
125 ((((x) & 0x0000000FLU) ? (1 << 0) : 0) \
126 +(((x) & 0x000000F0LU) ? (1 << 1) : 0) \
127 +(((x) & 0x00000F00LU) ? (1 << 2) : 0) \
128 +(((x) & 0x0000F000LU) ? (1 << 3) : 0) \
129 +(((x) & 0x000F0000LU) ? (1 << 4) : 0) \
130 +(((x) & 0x00F00000LU) ? (1 << 5) : 0) \
131 +(((x) & 0x0F000000LU) ? (1 << 6) : 0) \
132 +(((x) & 0xF0000000LU) ? (1 << 7) : 0))
134 #define B8(bits, count) {((uint8_t)B8__(HEX__(bits))), (count)}
136 static const struct tms_sequences old_tms_seqs
[6][6] = { /* [from_state_ndx][to_state_ndx] */
137 /* value clocked to TMS to move from one of six stable states to another.
138 * N.B. OOCD clocks TMS from LSB first, so read these right-to-left.
139 * N.B. Reset only needs to be 0b11111, but in JLink an even byte of 1's is more stable.
140 * These extra ones cause no TAP state problem, because we go into reset and stay in reset.
144 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
145 {B8(1111111, 7), B8(0000000, 7), B8(0010111, 7), B8(0001010, 7), B8(0011011, 7), B8(0010110, 7)},/* RESET */
146 {B8(1111111, 7), B8(0000000, 7), B8(0100101, 7), B8(0000101, 7), B8(0101011, 7), B8(0001011, 7)},/* IDLE */
147 {B8(1111111, 7), B8(0110001, 7), B8(0000000, 7), B8(0000001, 7), B8(0001111, 7), B8(0101111, 7)},/* DRSHIFT */
148 {B8(1111111, 7), B8(0110000, 7), B8(0100000, 7), B8(0010111, 7), B8(0011110, 7), B8(0101111, 7)},/* DRPAUSE */
149 {B8(1111111, 7), B8(0110001, 7), B8(0000111, 7), B8(0010111, 7), B8(0000000, 7), B8(0000001, 7)},/* IRSHIFT */
150 {B8(1111111, 7), B8(0110000, 7), B8(0011100, 7), B8(0010111, 7), B8(0011110, 7), B8(0101111, 7)},/* IRPAUSE */
153 static const struct tms_sequences short_tms_seqs
[6][6] = { /* [from_state_ndx][to_state_ndx] */
154 /* this is the table submitted by Jeff Williams on 3/30/2009 with this comment:
156 OK, I added Peter's version of the state table, and it works OK for
157 me on MC1322x. I've recreated the jlink portion of patch with this
158 new state table. His changes to my state table are pretty minor in
159 terms of total transitions, but Peter feels that his version fixes
160 some long-standing problems.
163 I added the bit count into the table, reduced RESET column to 7 bits from 8.
166 state specific comments:
167 ------------------------
168 *->RESET tried the 5 bit reset and it gave me problems, 7 bits seems to
169 work better on ARM9 with ft2232 driver. (Dick)
171 RESET->DRSHIFT add 1 extra clock cycles in the RESET state before advancing.
172 needed on ARM9 with ft2232 driver. (Dick)
173 (For a total of *THREE* extra clocks in RESET; NOP.)
175 RESET->IRSHIFT add 1 extra clock cycles in the RESET state before advancing.
176 needed on ARM9 with ft2232 driver. (Dick)
177 (For a total of *TWO* extra clocks in RESET; NOP.)
179 RESET->* always adds one or more clocks in the target state,
180 which should be NOPS; except shift states which (as
181 noted above) add those clocks in RESET.
183 The X-to-X transitions always add clocks; from *SHIFT, they go
184 via IDLE and thus *DO HAVE SIDE EFFECTS* (capture and update).
188 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
189 {B8(1111111, 7), B8(0000000, 7), B8(0010111, 7), B8(0001010, 7), B8(0011011, 7), B8(0010110, 7)}, /* RESET */
190 {B8(1111111, 7), B8(0000000, 7), B8(001, 3), B8(0101, 4), B8(0011, 4), B8(01011, 5)}, /* IDLE */
191 {B8(1111111, 7), B8(011, 3), B8(00111, 5), B8(01, 2), B8(001111, 6), B8(0101111, 7)}, /* DRSHIFT */
192 {B8(1111111, 7), B8(011, 3), B8(01, 2), B8(0, 1), B8(001111, 6), B8(0101111, 7)}, /* DRPAUSE */
193 {B8(1111111, 7), B8(011, 3), B8(00111, 5), B8(010111, 6), B8(001111, 6), B8(01, 2)}, /* IRSHIFT */
194 {B8(1111111, 7), B8(011, 3), B8(00111, 5), B8(010111, 6), B8(01, 2), B8(0, 1)} /* IRPAUSE */
197 typedef const struct tms_sequences tms_table
[6][6];
199 static tms_table
*tms_seqs
= &short_tms_seqs
;
201 int tap_get_tms_path(tap_state_t from
, tap_state_t to
)
203 return (*tms_seqs
)[tap_move_ndx(from
)][tap_move_ndx(to
)].bits
;
206 int tap_get_tms_path_len(tap_state_t from
, tap_state_t to
)
208 return (*tms_seqs
)[tap_move_ndx(from
)][tap_move_ndx(to
)].bit_count
;
211 bool tap_is_state_stable(tap_state_t astate
)
215 /* A switch () is used because it is symbol dependent
216 * (not value dependent like an array), and can also check bounds.
234 tap_state_t
tap_state_transition(tap_state_t cur_state
, bool tms
)
236 tap_state_t new_state
;
238 /* A switch is used because it is symbol dependent and not value dependent
239 * like an array. Also it can check for out of range conditions.
245 new_state
= cur_state
;
250 new_state
= TAP_DRSELECT
;
253 new_state
= TAP_IRSELECT
;
257 new_state
= TAP_DREXIT1
;
261 new_state
= TAP_DRUPDATE
;
264 new_state
= TAP_DREXIT2
;
267 new_state
= TAP_RESET
;
271 new_state
= TAP_IREXIT1
;
275 new_state
= TAP_IRUPDATE
;
278 new_state
= TAP_IREXIT2
;
281 LOG_ERROR("fatal: invalid argument cur_state=%d", cur_state
);
291 new_state
= TAP_IDLE
;
294 new_state
= TAP_DRCAPTURE
;
299 new_state
= TAP_DRSHIFT
;
303 new_state
= TAP_DRPAUSE
;
306 new_state
= TAP_IRCAPTURE
;
311 new_state
= TAP_IRSHIFT
;
315 new_state
= TAP_IRPAUSE
;
318 LOG_ERROR("fatal: invalid argument cur_state=%d", cur_state
);
327 /* NOTE: do not change these state names. They're documented,
328 * and we rely on them to match SVF input (except for "RUN/IDLE").
330 static const struct name_mapping
{
331 enum tap_state symbol
;
333 } tap_name_mapping
[] = {
334 { TAP_RESET
, "RESET", },
335 { TAP_IDLE
, "RUN/IDLE", },
336 { TAP_DRSELECT
, "DRSELECT", },
337 { TAP_DRCAPTURE
, "DRCAPTURE", },
338 { TAP_DRSHIFT
, "DRSHIFT", },
339 { TAP_DREXIT1
, "DREXIT1", },
340 { TAP_DRPAUSE
, "DRPAUSE", },
341 { TAP_DREXIT2
, "DREXIT2", },
342 { TAP_DRUPDATE
, "DRUPDATE", },
343 { TAP_IRSELECT
, "IRSELECT", },
344 { TAP_IRCAPTURE
, "IRCAPTURE", },
345 { TAP_IRSHIFT
, "IRSHIFT", },
346 { TAP_IREXIT1
, "IREXIT1", },
347 { TAP_IRPAUSE
, "IRPAUSE", },
348 { TAP_IREXIT2
, "IREXIT2", },
349 { TAP_IRUPDATE
, "IRUPDATE", },
351 /* only for input: accept standard SVF name */
352 { TAP_IDLE
, "IDLE", },
355 const char *tap_state_name(tap_state_t state
)
359 for (i
= 0; i
< ARRAY_SIZE(tap_name_mapping
); i
++) {
360 if (tap_name_mapping
[i
].symbol
== state
)
361 return tap_name_mapping
[i
].name
;
366 tap_state_t
tap_state_by_name(const char *name
)
370 for (i
= 0; i
< ARRAY_SIZE(tap_name_mapping
); i
++) {
371 /* be nice to the human */
372 if (strcasecmp(name
, tap_name_mapping
[i
].name
) == 0)
373 return tap_name_mapping
[i
].symbol
;
379 #ifdef _DEBUG_JTAG_IO_
381 #define JTAG_DEBUG_STATE_APPEND(buf, len, bit) \
382 do { buf[len] = bit ? '1' : '0'; } while (0)
383 #define JTAG_DEBUG_STATE_PRINT(a, b, astr, bstr) \
384 DEBUG_JTAG_IO("TAP/SM: %9s -> %5s\tTMS: %s\tTDI: %s", \
385 tap_state_name(a), tap_state_name(b), astr, bstr)
387 tap_state_t
jtag_debug_state_machine(const void *tms_buf
, const void *tdi_buf
,
388 unsigned tap_bits
, tap_state_t next_state
)
390 const uint8_t *tms_buffer
;
391 const uint8_t *tdi_buffer
;
396 unsigned tap_out_bits
;
400 tap_state_t last_state
;
402 /* set startstate (and possibly last, if tap_bits == 0) */
403 last_state
= next_state
;
404 DEBUG_JTAG_IO("TAP/SM: START state: %s", tap_state_name(next_state
));
406 tms_buffer
= (const uint8_t *)tms_buf
;
407 tdi_buffer
= (const uint8_t *)tdi_buf
;
409 tap_bytes
= DIV_ROUND_UP(tap_bits
, 8);
410 DEBUG_JTAG_IO("TAP/SM: TMS bits: %u (bytes: %u)", tap_bits
, tap_bytes
);
413 for (cur_byte
= 0; cur_byte
< tap_bytes
; cur_byte
++) {
414 for (cur_bit
= 0; cur_bit
< 8; cur_bit
++) {
415 /* make sure we do not run off the end of the buffers */
416 unsigned tap_bit
= cur_byte
* 8 + cur_bit
;
417 if (tap_bit
== tap_bits
)
420 /* check and save TMS bit */
421 tap_bit
= !!(tms_buffer
[cur_byte
] & (1 << cur_bit
));
422 JTAG_DEBUG_STATE_APPEND(tms_str
, tap_out_bits
, tap_bit
);
424 /* use TMS bit to find the next TAP state */
425 next_state
= tap_state_transition(last_state
, tap_bit
);
427 /* check and store TDI bit */
428 tap_bit
= !!(tdi_buffer
[cur_byte
] & (1 << cur_bit
));
429 JTAG_DEBUG_STATE_APPEND(tdi_str
, tap_out_bits
, tap_bit
);
431 /* increment TAP bits */
434 /* Only show TDO bits on state transitions, or */
435 /* after some number of bits in the same state. */
436 if ((next_state
== last_state
) && (tap_out_bits
< 32))
439 /* terminate strings and display state transition */
440 tms_str
[tap_out_bits
] = tdi_str
[tap_out_bits
] = 0;
441 JTAG_DEBUG_STATE_PRINT(last_state
, next_state
, tms_str
, tdi_str
);
444 last_state
= next_state
;
450 /* terminate strings and display state transition */
451 tms_str
[tap_out_bits
] = tdi_str
[tap_out_bits
] = 0;
452 JTAG_DEBUG_STATE_PRINT(last_state
, next_state
, tms_str
, tdi_str
);
455 DEBUG_JTAG_IO("TAP/SM: FINAL state: %s", tap_state_name(next_state
));
459 #endif /* _DEBUG_JTAG_IO_ */
461 void tap_use_new_tms_table(bool use_new
)
463 tms_seqs
= use_new
? &short_tms_seqs
: &old_tms_seqs
;
465 bool tap_uses_new_tms_table(void)
467 return tms_seqs
== &short_tms_seqs
;