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