update files to correct FSF address
[openocd.git] / src / jtag / interface.c
blob19a0b052c08942d4839f81be23baa8b152e890ab
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
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. *
19 * *
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. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
29 ***************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include "jtag.h"
36 #include "interface.h"
38 /**
39 * @see tap_set_state() and tap_get_state() accessors.
40 * Actual name is not important since accessors hide it.
42 static tap_state_t state_follower = TAP_RESET;
44 void tap_set_state_impl(tap_state_t new_state)
46 /* this is the state we think the TAPs are in now, was cur_state */
47 state_follower = new_state;
50 tap_state_t tap_get_state()
52 return state_follower;
55 /**
56 * @see tap_set_end_state() and tap_get_end_state() accessors.
57 * Actual name is not important because accessors hide it.
59 static tap_state_t end_state_follower = TAP_RESET;
61 void tap_set_end_state(tap_state_t new_end_state)
63 /* this is the state we think the TAPs will be in at completion of the
64 * current TAP operation, was end_state
66 end_state_follower = new_end_state;
69 tap_state_t tap_get_end_state()
71 return end_state_follower;
74 int tap_move_ndx(tap_state_t astate)
76 /* given a stable state, return the index into the tms_seqs[]
77 * array within tap_get_tms_path()
80 int ndx;
82 switch (astate) {
83 case TAP_RESET:
84 ndx = 0;
85 break;
86 case TAP_IDLE:
87 ndx = 1;
88 break;
89 case TAP_DRSHIFT:
90 ndx = 2;
91 break;
92 case TAP_DRPAUSE:
93 ndx = 3;
94 break;
95 case TAP_IRSHIFT:
96 ndx = 4;
97 break;
98 case TAP_IRPAUSE:
99 ndx = 5;
100 break;
101 default:
102 LOG_ERROR("FATAL: unstable state \"%s\" in tap_move_ndx()",
103 tap_state_name(astate));
104 exit(1);
107 return ndx;
110 /* tap_move[i][j]: tap movement command to go from state i to state j
111 * encodings of i and j are what tap_move_ndx() reports.
113 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
115 struct tms_sequences {
116 uint8_t bits;
117 uint8_t bit_count;
121 * These macros allow us to specify TMS state transitions by bits rather than hex bytes.
122 * Read the bits from LSBit first to MSBit last (right-to-left).
124 #define HEX__(n) 0x##n##LU
126 #define B8__(x) \
127 ((((x) & 0x0000000FLU) ? (1 << 0) : 0) \
128 +(((x) & 0x000000F0LU) ? (1 << 1) : 0) \
129 +(((x) & 0x00000F00LU) ? (1 << 2) : 0) \
130 +(((x) & 0x0000F000LU) ? (1 << 3) : 0) \
131 +(((x) & 0x000F0000LU) ? (1 << 4) : 0) \
132 +(((x) & 0x00F00000LU) ? (1 << 5) : 0) \
133 +(((x) & 0x0F000000LU) ? (1 << 6) : 0) \
134 +(((x) & 0xF0000000LU) ? (1 << 7) : 0))
136 #define B8(bits, count) {((uint8_t)B8__(HEX__(bits))), (count)}
138 static const struct tms_sequences old_tms_seqs[6][6] = { /* [from_state_ndx][to_state_ndx] */
139 /* value clocked to TMS to move from one of six stable states to another.
140 * N.B. OOCD clocks TMS from LSB first, so read these right-to-left.
141 * N.B. Reset only needs to be 0b11111, but in JLink an even byte of 1's is more stable.
142 * These extra ones cause no TAP state problem, because we go into reset and stay in reset.
145 /* to state: */
146 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
147 {B8(1111111, 7), B8(0000000, 7), B8(0010111, 7), B8(0001010, 7), B8(0011011, 7), B8(0010110, 7)},/* RESET */
148 {B8(1111111, 7), B8(0000000, 7), B8(0100101, 7), B8(0000101, 7), B8(0101011, 7), B8(0001011, 7)},/* IDLE */
149 {B8(1111111, 7), B8(0110001, 7), B8(0000000, 7), B8(0000001, 7), B8(0001111, 7), B8(0101111, 7)},/* DRSHIFT */
150 {B8(1111111, 7), B8(0110000, 7), B8(0100000, 7), B8(0010111, 7), B8(0011110, 7), B8(0101111, 7)},/* DRPAUSE */
151 {B8(1111111, 7), B8(0110001, 7), B8(0000111, 7), B8(0010111, 7), B8(0000000, 7), B8(0000001, 7)},/* IRSHIFT */
152 {B8(1111111, 7), B8(0110000, 7), B8(0011100, 7), B8(0010111, 7), B8(0011110, 7), B8(0101111, 7)},/* IRPAUSE */
155 static const struct tms_sequences short_tms_seqs[6][6] = { /* [from_state_ndx][to_state_ndx] */
156 /* this is the table submitted by Jeff Williams on 3/30/2009 with this comment:
158 OK, I added Peter's version of the state table, and it works OK for
159 me on MC1322x. I've recreated the jlink portion of patch with this
160 new state table. His changes to my state table are pretty minor in
161 terms of total transitions, but Peter feels that his version fixes
162 some long-standing problems.
163 Jeff
165 I added the bit count into the table, reduced RESET column to 7 bits from 8.
166 Dick
168 state specific comments:
169 ------------------------
170 *->RESET tried the 5 bit reset and it gave me problems, 7 bits seems to
171 work better on ARM9 with ft2232 driver. (Dick)
173 RESET->DRSHIFT add 1 extra clock cycles in the RESET state before advancing.
174 needed on ARM9 with ft2232 driver. (Dick)
175 (For a total of *THREE* extra clocks in RESET; NOP.)
177 RESET->IRSHIFT add 1 extra clock cycles in the RESET state before advancing.
178 needed on ARM9 with ft2232 driver. (Dick)
179 (For a total of *TWO* extra clocks in RESET; NOP.)
181 RESET->* always adds one or more clocks in the target state,
182 which should be NOPS; except shift states which (as
183 noted above) add those clocks in RESET.
185 The X-to-X transitions always add clocks; from *SHIFT, they go
186 via IDLE and thus *DO HAVE SIDE EFFECTS* (capture and update).
189 /* to state: */
190 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
191 {B8(1111111, 7), B8(0000000, 7), B8(0010111, 7), B8(0001010, 7), B8(0011011, 7), B8(0010110, 7)}, /* RESET */
192 {B8(1111111, 7), B8(0000000, 7), B8(001, 3), B8(0101, 4), B8(0011, 4), B8(01011, 5)}, /* IDLE */
193 {B8(1111111, 7), B8(011, 3), B8(00111, 5), B8(01, 2), B8(001111, 6), B8(0101111, 7)}, /* DRSHIFT */
194 {B8(1111111, 7), B8(011, 3), B8(01, 2), B8(0, 1), B8(001111, 6), B8(0101111, 7)}, /* DRPAUSE */
195 {B8(1111111, 7), B8(011, 3), B8(00111, 5), B8(010111, 6), B8(001111, 6), B8(01, 2)}, /* IRSHIFT */
196 {B8(1111111, 7), B8(011, 3), B8(00111, 5), B8(010111, 6), B8(01, 2), B8(0, 1)} /* IRPAUSE */
199 typedef const struct tms_sequences tms_table[6][6];
201 static tms_table *tms_seqs = &short_tms_seqs;
203 int tap_get_tms_path(tap_state_t from, tap_state_t to)
205 return (*tms_seqs)[tap_move_ndx(from)][tap_move_ndx(to)].bits;
208 int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
210 return (*tms_seqs)[tap_move_ndx(from)][tap_move_ndx(to)].bit_count;
213 bool tap_is_state_stable(tap_state_t astate)
215 bool is_stable;
217 /* A switch () is used because it is symbol dependent
218 * (not value dependent like an array), and can also check bounds.
220 switch (astate) {
221 case TAP_RESET:
222 case TAP_IDLE:
223 case TAP_DRSHIFT:
224 case TAP_DRPAUSE:
225 case TAP_IRSHIFT:
226 case TAP_IRPAUSE:
227 is_stable = true;
228 break;
229 default:
230 is_stable = false;
233 return is_stable;
236 tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
238 tap_state_t new_state;
240 /* A switch is used because it is symbol dependent and not value dependent
241 * like an array. Also it can check for out of range conditions.
244 if (tms) {
245 switch (cur_state) {
246 case TAP_RESET:
247 new_state = cur_state;
248 break;
249 case TAP_IDLE:
250 case TAP_DRUPDATE:
251 case TAP_IRUPDATE:
252 new_state = TAP_DRSELECT;
253 break;
254 case TAP_DRSELECT:
255 new_state = TAP_IRSELECT;
256 break;
257 case TAP_DRCAPTURE:
258 case TAP_DRSHIFT:
259 new_state = TAP_DREXIT1;
260 break;
261 case TAP_DREXIT1:
262 case TAP_DREXIT2:
263 new_state = TAP_DRUPDATE;
264 break;
265 case TAP_DRPAUSE:
266 new_state = TAP_DREXIT2;
267 break;
268 case TAP_IRSELECT:
269 new_state = TAP_RESET;
270 break;
271 case TAP_IRCAPTURE:
272 case TAP_IRSHIFT:
273 new_state = TAP_IREXIT1;
274 break;
275 case TAP_IREXIT1:
276 case TAP_IREXIT2:
277 new_state = TAP_IRUPDATE;
278 break;
279 case TAP_IRPAUSE:
280 new_state = TAP_IREXIT2;
281 break;
282 default:
283 LOG_ERROR("fatal: invalid argument cur_state=%d", cur_state);
284 exit(1);
285 break;
287 } else {
288 switch (cur_state) {
289 case TAP_RESET:
290 case TAP_IDLE:
291 case TAP_DRUPDATE:
292 case TAP_IRUPDATE:
293 new_state = TAP_IDLE;
294 break;
295 case TAP_DRSELECT:
296 new_state = TAP_DRCAPTURE;
297 break;
298 case TAP_DRCAPTURE:
299 case TAP_DRSHIFT:
300 case TAP_DREXIT2:
301 new_state = TAP_DRSHIFT;
302 break;
303 case TAP_DREXIT1:
304 case TAP_DRPAUSE:
305 new_state = TAP_DRPAUSE;
306 break;
307 case TAP_IRSELECT:
308 new_state = TAP_IRCAPTURE;
309 break;
310 case TAP_IRCAPTURE:
311 case TAP_IRSHIFT:
312 case TAP_IREXIT2:
313 new_state = TAP_IRSHIFT;
314 break;
315 case TAP_IREXIT1:
316 case TAP_IRPAUSE:
317 new_state = TAP_IRPAUSE;
318 break;
319 default:
320 LOG_ERROR("fatal: invalid argument cur_state=%d", cur_state);
321 exit(1);
322 break;
326 return new_state;
329 /* NOTE: do not change these state names. They're documented,
330 * and we rely on them to match SVF input (except for "RUN/IDLE").
332 static const struct name_mapping {
333 enum tap_state symbol;
334 const char *name;
335 } tap_name_mapping[] = {
336 { TAP_RESET, "RESET", },
337 { TAP_IDLE, "RUN/IDLE", },
338 { TAP_DRSELECT, "DRSELECT", },
339 { TAP_DRCAPTURE, "DRCAPTURE", },
340 { TAP_DRSHIFT, "DRSHIFT", },
341 { TAP_DREXIT1, "DREXIT1", },
342 { TAP_DRPAUSE, "DRPAUSE", },
343 { TAP_DREXIT2, "DREXIT2", },
344 { TAP_DRUPDATE, "DRUPDATE", },
345 { TAP_IRSELECT, "IRSELECT", },
346 { TAP_IRCAPTURE, "IRCAPTURE", },
347 { TAP_IRSHIFT, "IRSHIFT", },
348 { TAP_IREXIT1, "IREXIT1", },
349 { TAP_IRPAUSE, "IRPAUSE", },
350 { TAP_IREXIT2, "IREXIT2", },
351 { TAP_IRUPDATE, "IRUPDATE", },
353 /* only for input: accept standard SVF name */
354 { TAP_IDLE, "IDLE", },
357 const char *tap_state_name(tap_state_t state)
359 unsigned i;
361 for (i = 0; i < ARRAY_SIZE(tap_name_mapping); i++) {
362 if (tap_name_mapping[i].symbol == state)
363 return tap_name_mapping[i].name;
365 return "???";
368 tap_state_t tap_state_by_name(const char *name)
370 unsigned i;
372 for (i = 0; i < ARRAY_SIZE(tap_name_mapping); i++) {
373 /* be nice to the human */
374 if (strcasecmp(name, tap_name_mapping[i].name) == 0)
375 return tap_name_mapping[i].symbol;
377 /* not found */
378 return TAP_INVALID;
381 #ifdef _DEBUG_JTAG_IO_
383 #define JTAG_DEBUG_STATE_APPEND(buf, len, bit) \
384 do { buf[len] = bit ? '1' : '0'; } while (0)
385 #define JTAG_DEBUG_STATE_PRINT(a, b, astr, bstr) \
386 DEBUG_JTAG_IO("TAP/SM: %9s -> %5s\tTMS: %s\tTDI: %s", \
387 tap_state_name(a), tap_state_name(b), astr, bstr)
389 tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf,
390 unsigned tap_bits, tap_state_t next_state)
392 const uint8_t *tms_buffer;
393 const uint8_t *tdi_buffer;
394 unsigned tap_bytes;
395 unsigned cur_byte;
396 unsigned cur_bit;
398 unsigned tap_out_bits;
399 char tms_str[33];
400 char tdi_str[33];
402 tap_state_t last_state;
404 /* set startstate (and possibly last, if tap_bits == 0) */
405 last_state = next_state;
406 DEBUG_JTAG_IO("TAP/SM: START state: %s", tap_state_name(next_state));
408 tms_buffer = (const uint8_t *)tms_buf;
409 tdi_buffer = (const uint8_t *)tdi_buf;
411 tap_bytes = DIV_ROUND_UP(tap_bits, 8);
412 DEBUG_JTAG_IO("TAP/SM: TMS bits: %u (bytes: %u)", tap_bits, tap_bytes);
414 tap_out_bits = 0;
415 for (cur_byte = 0; cur_byte < tap_bytes; cur_byte++) {
416 for (cur_bit = 0; cur_bit < 8; cur_bit++) {
417 /* make sure we do not run off the end of the buffers */
418 unsigned tap_bit = cur_byte * 8 + cur_bit;
419 if (tap_bit == tap_bits)
420 break;
422 /* check and save TMS bit */
423 tap_bit = !!(tms_buffer[cur_byte] & (1 << cur_bit));
424 JTAG_DEBUG_STATE_APPEND(tms_str, tap_out_bits, tap_bit);
426 /* use TMS bit to find the next TAP state */
427 next_state = tap_state_transition(last_state, tap_bit);
429 /* check and store TDI bit */
430 tap_bit = !!(tdi_buffer[cur_byte] & (1 << cur_bit));
431 JTAG_DEBUG_STATE_APPEND(tdi_str, tap_out_bits, tap_bit);
433 /* increment TAP bits */
434 tap_out_bits++;
436 /* Only show TDO bits on state transitions, or */
437 /* after some number of bits in the same state. */
438 if ((next_state == last_state) && (tap_out_bits < 32))
439 continue;
441 /* terminate strings and display state transition */
442 tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
443 JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
445 /* reset state */
446 last_state = next_state;
447 tap_out_bits = 0;
451 if (tap_out_bits) {
452 /* terminate strings and display state transition */
453 tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
454 JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
457 DEBUG_JTAG_IO("TAP/SM: FINAL state: %s", tap_state_name(next_state));
459 return next_state;
461 #endif /* _DEBUG_JTAG_IO_ */
463 void tap_use_new_tms_table(bool use_new)
465 tms_seqs = use_new ? &short_tms_seqs : &old_tms_seqs;
467 bool tap_uses_new_tms_table(void)
469 return tms_seqs == &short_tms_seqs;