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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)