Use parse_uint helper to replace strtoul call in jtag_tap_by_string.
[openocd.git] / src / jtag / core.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 "minidriver.h"
36 #include "interface.h"
37
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41
42
43 /// The number of JTAG queue flushes (for profiling and debugging purposes).
44 static int jtag_flush_queue_count;
45
46 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
47 int in_num_fields, scan_field_t *in_fields, tap_state_t state);
48
49 /**
50 * The jtag_error variable is set when an error occurs while executing
51 * the queue. Application code may set this using jtag_set_error(),
52 * when an error occurs during processing that should be reported during
53 * jtag_execute_queue().
54 *
55 * Tts value may be checked with jtag_get_error() and cleared with
56 * jtag_error_clear(). This value is returned (and cleared) by
57 * jtag_execute_queue().
58 */
59 static int jtag_error = ERROR_OK;
60
61 static char* jtag_event_strings[] =
62 {
63 "JTAG controller reset (RESET or TRST)"
64 };
65
66 static int jtag_trst = 0;
67 static int jtag_srst = 0;
68
69 /**
70 * List all TAPs that have been created.
71 */
72 static jtag_tap_t *__jtag_all_taps = NULL;
73 /**
74 * The number of TAPs in the __jtag_all_taps list, used to track the
75 * assigned chain position to new TAPs
76 */
77 static unsigned jtag_num_taps = 0;
78
79 static enum reset_types jtag_reset_config = RESET_NONE;
80 static tap_state_t cmd_queue_end_state = TAP_RESET;
81 tap_state_t cmd_queue_cur_state = TAP_RESET;
82
83 static bool jtag_verify_capture_ir = true;
84 static int jtag_verify = 1;
85
86 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
87 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
88 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
89
90 typedef struct jtag_event_callback_s
91 {
92 jtag_event_handler_t callback;
93 void* priv;
94 struct jtag_event_callback_s* next;
95 } jtag_event_callback_t;
96
97 /* callbacks to inform high-level handlers about JTAG state changes */
98 static jtag_event_callback_t *jtag_event_callbacks;
99
100 /* speed in kHz*/
101 static int speed_khz = 0;
102 /* flag if the kHz speed was defined */
103 static bool hasKHz = false;
104 static int jtag_speed = 0;
105
106 static struct jtag_interface_s *jtag = NULL;
107
108 /* configuration */
109 jtag_interface_t *jtag_interface = NULL;
110
111 void jtag_set_error(int error)
112 {
113 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
114 return;
115 jtag_error = error;
116 }
117 int jtag_get_error(void)
118 {
119 return jtag_error;
120 }
121 int jtag_error_clear(void)
122 {
123 int temp = jtag_error;
124 jtag_error = ERROR_OK;
125 return temp;
126 }
127
128
129 jtag_tap_t *jtag_all_taps(void)
130 {
131 return __jtag_all_taps;
132 };
133
134 unsigned jtag_tap_count(void)
135 {
136 return jtag_num_taps;
137 }
138
139 unsigned jtag_tap_count_enabled(void)
140 {
141 jtag_tap_t *t = jtag_all_taps();
142 unsigned n = 0;
143 while(t)
144 {
145 if (t->enabled)
146 n++;
147 t = t->next_tap;
148 }
149 return n;
150 }
151
152 /// Append a new TAP to the chain of all taps.
153 void jtag_tap_add(struct jtag_tap_s *t)
154 {
155 t->abs_chain_position = jtag_num_taps++;
156
157 jtag_tap_t **tap = &__jtag_all_taps;
158 while(*tap != NULL)
159 tap = &(*tap)->next_tap;
160 *tap = t;
161 }
162
163 jtag_tap_t *jtag_tap_by_string(const char *s)
164 {
165 /* try by name first */
166 jtag_tap_t *t = jtag_all_taps();
167 while (t)
168 {
169 if (0 == strcmp(t->dotted_name, s))
170 return t;
171 t = t->next_tap;
172 }
173
174 /* no tap found by name, so try to parse the name as a number */
175 unsigned n;
176 if (parse_uint(s, &n) != ERROR_OK)
177 return NULL;
178
179 return jtag_tap_by_position(n);
180 }
181
182 jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
183 {
184 const char *cp = Jim_GetString(o, NULL);
185 jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
186 if (NULL == cp)
187 cp = "(unknown)";
188 if (NULL == t)
189 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
190 return t;
191 }
192
193 /* returns a pointer to the n-th device in the scan chain */
194 jtag_tap_t *jtag_tap_by_position(unsigned n)
195 {
196 jtag_tap_t *t = jtag_all_taps();
197
198 while (t && n-- > 0)
199 t = t->next_tap;
200
201 return t;
202 }
203
204 jtag_tap_t* jtag_tap_next_enabled(jtag_tap_t* p)
205 {
206 p = p ? p->next_tap : jtag_all_taps();
207 while (p)
208 {
209 if (p->enabled)
210 return p;
211 p = p->next_tap;
212 }
213 return NULL;
214 }
215
216 const char *jtag_tap_name(const jtag_tap_t *tap)
217 {
218 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
219 }
220
221
222 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
223 {
224 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
225
226 if (callback == NULL)
227 {
228 return ERROR_INVALID_ARGUMENTS;
229 }
230
231 if (*callbacks_p)
232 {
233 while ((*callbacks_p)->next)
234 callbacks_p = &((*callbacks_p)->next);
235 callbacks_p = &((*callbacks_p)->next);
236 }
237
238 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
239 (*callbacks_p)->callback = callback;
240 (*callbacks_p)->priv = priv;
241 (*callbacks_p)->next = NULL;
242
243 return ERROR_OK;
244 }
245
246 int jtag_unregister_event_callback(jtag_event_handler_t callback)
247 {
248 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
249
250 if (callback == NULL)
251 {
252 return ERROR_INVALID_ARGUMENTS;
253 }
254
255 while (*callbacks_p)
256 {
257 jtag_event_callback_t **next = &((*callbacks_p)->next);
258 if ((*callbacks_p)->callback == callback)
259 {
260 free(*callbacks_p);
261 *callbacks_p = *next;
262 }
263 callbacks_p = next;
264 }
265
266 return ERROR_OK;
267 }
268
269 int jtag_call_event_callbacks(enum jtag_event event)
270 {
271 jtag_event_callback_t *callback = jtag_event_callbacks;
272
273 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
274
275 while (callback)
276 {
277 callback->callback(event, callback->priv);
278 callback = callback->next;
279 }
280
281 return ERROR_OK;
282 }
283
284 static void jtag_checks(void)
285 {
286 assert(jtag_trst == 0);
287 }
288
289 static void jtag_prelude(tap_state_t state)
290 {
291 jtag_checks();
292
293 assert(state!=TAP_INVALID);
294
295 cmd_queue_cur_state = state;
296 }
297
298 void jtag_alloc_in_value32(scan_field_t *field)
299 {
300 interface_jtag_alloc_in_value32(field);
301 }
302
303 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
304 tap_state_t state)
305 {
306 jtag_prelude(state);
307
308 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
309 jtag_set_error(retval);
310 }
311
312
313 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
314 {
315 if (jtag_verify&&jtag_verify_capture_ir)
316 {
317 /* 8 x 32 bit id's is enough for all invocations */
318
319 for (int j = 0; j < in_num_fields; j++)
320 {
321 /* if we are to run a verification of the ir scan, we need to get the input back.
322 * We may have to allocate space if the caller didn't ask for the input back.
323 */
324 in_fields[j].check_value=in_fields[j].tap->expected;
325 in_fields[j].check_mask=in_fields[j].tap->expected_mask;
326 }
327 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
328 } else
329 {
330 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
331 }
332 }
333
334 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
335 tap_state_t state)
336 {
337 jtag_prelude(state);
338
339 int retval = interface_jtag_add_plain_ir_scan(
340 in_num_fields, in_fields, state);
341 jtag_set_error(retval);
342 }
343
344 void jtag_add_callback(jtag_callback1_t f, u8 *in)
345 {
346 interface_jtag_add_callback(f, in);
347 }
348
349 void jtag_add_callback4(jtag_callback_t f, u8 *in,
350 jtag_callback_data_t data1, jtag_callback_data_t data2,
351 jtag_callback_data_t data3)
352 {
353 interface_jtag_add_callback4(f, in, data1, data2, data3);
354 }
355
356 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
357
358 static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
359 {
360 return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
361 }
362
363 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
364 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
365 {
366 for (int i = 0; i < in_num_fields; i++)
367 {
368 struct scan_field_s *field = &in_fields[i];
369 field->allocated = 0;
370 field->modified = 0;
371 if (field->check_value || field->in_value)
372 continue;
373 interface_jtag_add_scan_check_alloc(field);
374 field->modified = 1;
375 }
376
377 jtag_add_scan(in_num_fields, in_fields, state);
378
379 for (int i = 0; i < in_num_fields; i++)
380 {
381 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
382 {
383 /* this is synchronous for a minidriver */
384 jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
385 (jtag_callback_data_t)in_fields[i].check_value,
386 (jtag_callback_data_t)in_fields[i].check_mask,
387 (jtag_callback_data_t)in_fields[i].num_bits);
388 }
389 if (in_fields[i].allocated)
390 {
391 free(in_fields[i].in_value);
392 }
393 if (in_fields[i].modified)
394 {
395 in_fields[i].in_value = NULL;
396 }
397 }
398 }
399
400 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
401 {
402 if (jtag_verify)
403 {
404 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
405 } else
406 {
407 jtag_add_dr_scan(in_num_fields, in_fields, state);
408 }
409 }
410
411
412 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
413 tap_state_t state)
414 {
415 jtag_prelude(state);
416
417 int retval;
418 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
419 jtag_set_error(retval);
420 }
421
422 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
423 tap_state_t state)
424 {
425 jtag_prelude(state);
426
427 int retval;
428 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
429 jtag_set_error(retval);
430 }
431
432 void jtag_add_dr_out(jtag_tap_t* tap,
433 int num_fields, const int* num_bits, const u32* value,
434 tap_state_t end_state)
435 {
436 assert(end_state != TAP_INVALID);
437
438 cmd_queue_cur_state = end_state;
439
440 interface_jtag_add_dr_out(tap,
441 num_fields, num_bits, value,
442 end_state);
443 }
444
445 void jtag_add_tlr(void)
446 {
447 jtag_prelude(TAP_RESET);
448 jtag_set_error(interface_jtag_add_tlr());
449 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
450 }
451
452 void jtag_add_pathmove(int num_states, const tap_state_t *path)
453 {
454 tap_state_t cur_state = cmd_queue_cur_state;
455
456 /* the last state has to be a stable state */
457 if (!tap_is_state_stable(path[num_states - 1]))
458 {
459 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
460 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
461 return;
462 }
463
464 for (int i = 0; i < num_states; i++)
465 {
466 if (path[i] == TAP_RESET)
467 {
468 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
469 jtag_set_error(ERROR_JTAG_STATE_INVALID);
470 return;
471 }
472
473 if ( tap_state_transition(cur_state, true) != path[i]
474 && tap_state_transition(cur_state, false) != path[i])
475 {
476 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
477 tap_state_name(cur_state), tap_state_name(path[i]));
478 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
479 return;
480 }
481 cur_state = path[i];
482 }
483
484 jtag_checks();
485
486 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
487 cmd_queue_cur_state = path[num_states - 1];
488 }
489
490 int jtag_add_statemove(tap_state_t goal_state)
491 {
492 tap_state_t cur_state = cmd_queue_cur_state;
493
494 LOG_DEBUG( "cur_state=%s goal_state=%s",
495 tap_state_name(cur_state),
496 tap_state_name(goal_state) );
497
498
499 if (goal_state==cur_state )
500 ; /* nothing to do */
501 else if( goal_state==TAP_RESET )
502 {
503 jtag_add_tlr();
504 }
505 else if( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
506 {
507 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
508 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
509 tap_state_t moves[8];
510 assert(tms_count < DIM(moves));
511
512 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
513 {
514 bool bit = tms_bits & 1;
515
516 cur_state = tap_state_transition(cur_state, bit);
517 moves[i] = cur_state;
518 }
519
520 jtag_add_pathmove(tms_count, moves);
521 }
522 else if( tap_state_transition(cur_state, true) == goal_state
523 || tap_state_transition(cur_state, false) == goal_state )
524 {
525 jtag_add_pathmove(1, &goal_state);
526 }
527
528 else
529 return ERROR_FAIL;
530
531 return ERROR_OK;
532 }
533
534 void jtag_add_runtest(int num_cycles, tap_state_t state)
535 {
536 jtag_prelude(state);
537 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
538 }
539
540
541 void jtag_add_clocks(int num_cycles)
542 {
543 if (!tap_is_state_stable(cmd_queue_cur_state))
544 {
545 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
546 tap_state_name(cmd_queue_cur_state));
547 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
548 return;
549 }
550
551 if (num_cycles > 0)
552 {
553 jtag_checks();
554 jtag_set_error(interface_jtag_add_clocks(num_cycles));
555 }
556 }
557
558 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
559 {
560 int trst_with_tlr = 0;
561
562 /* FIX!!! there are *many* different cases here. A better
563 * approach is needed for legal combinations of transitions...
564 */
565 if ((jtag_reset_config & RESET_HAS_SRST)&&
566 (jtag_reset_config & RESET_HAS_TRST)&&
567 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
568 {
569 if (((req_tlr_or_trst&&!jtag_trst)||
570 (!req_tlr_or_trst&&jtag_trst))&&
571 ((req_srst&&!jtag_srst)||
572 (!req_srst&&jtag_srst)))
573 {
574 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
575 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
576 }
577 }
578
579 /* Make sure that jtag_reset_config allows the requested reset */
580 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
581 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
582 {
583 LOG_ERROR("BUG: requested reset would assert trst");
584 jtag_set_error(ERROR_FAIL);
585 return;
586 }
587
588 /* if TRST pulls SRST, we reset with TAP T-L-R */
589 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
590 {
591 trst_with_tlr = 1;
592 }
593
594 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
595 {
596 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
597 jtag_set_error(ERROR_FAIL);
598 return;
599 }
600
601 if (req_tlr_or_trst)
602 {
603 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
604 {
605 jtag_trst = 1;
606 } else
607 {
608 trst_with_tlr = 1;
609 }
610 } else
611 {
612 jtag_trst = 0;
613 }
614
615 jtag_srst = req_srst;
616
617 int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
618 if (retval != ERROR_OK)
619 {
620 jtag_set_error(retval);
621 return;
622 }
623 jtag_execute_queue();
624
625 if (jtag_srst)
626 {
627 LOG_DEBUG("SRST line asserted");
628 }
629 else
630 {
631 LOG_DEBUG("SRST line released");
632 if (jtag_nsrst_delay)
633 jtag_add_sleep(jtag_nsrst_delay * 1000);
634 }
635
636 if (trst_with_tlr)
637 {
638 LOG_DEBUG("JTAG reset with RESET instead of TRST");
639 jtag_set_end_state(TAP_RESET);
640 jtag_add_tlr();
641 return;
642 }
643
644 if (jtag_trst)
645 {
646 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
647 * and inform possible listeners about this
648 */
649 LOG_DEBUG("TRST line asserted");
650 tap_set_state(TAP_RESET);
651 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
652 }
653 else
654 {
655 if (jtag_ntrst_delay)
656 jtag_add_sleep(jtag_ntrst_delay * 1000);
657 }
658 }
659
660 tap_state_t jtag_set_end_state(tap_state_t state)
661 {
662 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
663 {
664 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
665 }
666
667 if (state!=TAP_INVALID)
668 cmd_queue_end_state = state;
669 return cmd_queue_end_state;
670 }
671
672 tap_state_t jtag_get_end_state(void)
673 {
674 return cmd_queue_end_state;
675 }
676
677 void jtag_add_sleep(u32 us)
678 {
679 /// @todo Here, keep_alive() appears to be a layering violation!!!
680 keep_alive();
681 jtag_set_error(interface_jtag_add_sleep(us));
682 }
683
684 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
685 {
686 int retval = ERROR_OK;
687
688 int compare_failed = 0;
689
690 if (in_check_mask)
691 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
692 else
693 compare_failed = buf_cmp(captured, in_check_value, num_bits);
694
695 if (compare_failed){
696 /* An error handler could have caught the failing check
697 * only report a problem when there wasn't a handler, or if the handler
698 * acknowledged the error
699 */
700 /*
701 LOG_WARNING("TAP %s:",
702 jtag_tap_name(field->tap));
703 */
704 if (compare_failed)
705 {
706 char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
707 char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
708
709 if (in_check_mask)
710 {
711 char *in_check_mask_char;
712 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
713 LOG_WARNING("value captured during scan didn't pass the requested check:");
714 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
715 captured_char, in_check_value_char, in_check_mask_char);
716 free(in_check_mask_char);
717 }
718 else
719 {
720 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
721 }
722
723 free(captured_char);
724 free(in_check_value_char);
725
726 retval = ERROR_JTAG_QUEUE_FAILED;
727 }
728
729 }
730 return retval;
731 }
732
733 void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
734 {
735 assert(field->in_value != NULL);
736
737 if (value==NULL)
738 {
739 /* no checking to do */
740 return;
741 }
742
743 jtag_execute_queue_noclear();
744
745 int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
746 jtag_set_error(retval);
747 }
748
749
750
751 int default_interface_jtag_execute_queue(void)
752 {
753 if (NULL == jtag)
754 {
755 LOG_ERROR("No JTAG interface configured yet. "
756 "Issue 'init' command in startup scripts "
757 "before communicating with targets.");
758 return ERROR_FAIL;
759 }
760
761 return jtag->execute_queue();
762 }
763
764 void jtag_execute_queue_noclear(void)
765 {
766 jtag_flush_queue_count++;
767 jtag_set_error(interface_jtag_execute_queue());
768 }
769
770 int jtag_get_flush_queue_count(void)
771 {
772 return jtag_flush_queue_count;
773 }
774
775 int jtag_execute_queue(void)
776 {
777 jtag_execute_queue_noclear();
778 return jtag_error_clear();
779 }
780
781 static int jtag_reset_callback(enum jtag_event event, void *priv)
782 {
783 jtag_tap_t *tap = priv;
784
785 LOG_DEBUG("-");
786
787 if (event == JTAG_TRST_ASSERTED)
788 {
789 buf_set_ones(tap->cur_instr, tap->ir_length);
790 tap->bypass = 1;
791 }
792
793 return ERROR_OK;
794 }
795
796 void jtag_sleep(u32 us)
797 {
798 alive_sleep(us/1000);
799 }
800
801 /// maximum number of JTAG devices expected in the chain
802 #define JTAG_MAX_CHAIN_SIZE 20
803
804 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
805 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
806 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
807
808 static int jtag_examine_chain_execute(u8 *idcode_buffer, unsigned num_idcode)
809 {
810 scan_field_t field = {
811 .tap = NULL,
812 .num_bits = num_idcode * 32,
813 .out_value = idcode_buffer,
814 .in_value = idcode_buffer,
815 };
816
817 // initialize to the end of chain ID value
818 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
819 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
820
821 jtag_add_plain_dr_scan(1, &field, TAP_RESET);
822 return jtag_execute_queue();
823 }
824
825 static bool jtag_examine_chain_check(u8 *idcodes, unsigned count)
826 {
827 u8 zero_check = 0x0;
828 u8 one_check = 0xff;
829
830 for (unsigned i = 0; i < count * 4; i++)
831 {
832 zero_check |= idcodes[i];
833 one_check &= idcodes[i];
834 }
835
836 /* if there wasn't a single non-zero bit or if all bits were one,
837 * the scan is not valid */
838 if (zero_check == 0x00 || one_check == 0xff)
839 {
840 LOG_ERROR("JTAG communication failure: check connection, "
841 "JTAG interface, target power etc.");
842 return false;
843 }
844 return true;
845 }
846
847 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
848 const char *name, u32 idcode)
849 {
850 log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
851 "JTAG tap: %s %16.16s: 0x%08x "
852 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
853 name, msg, idcode,
854 EXTRACT_MFG(idcode), EXTRACT_PART(idcode), EXTRACT_VER(idcode) );
855 }
856
857 static bool jtag_idcode_is_final(u32 idcode)
858 {
859 return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
860 }
861
862 /**
863 * This helper checks that remaining bits in the examined chain data are
864 * all as expected, but a single JTAG device requires only 64 bits to be
865 * read back correctly. This can help identify and diagnose problems
866 * with the JTAG chain earlier, gives more helpful/explicit error messages.
867 */
868 static void jtag_examine_chain_end(u8 *idcodes, unsigned count, unsigned max)
869 {
870 bool triggered = false;
871 for ( ; count < max - 31; count += 32)
872 {
873 u32 idcode = buf_get_u32(idcodes, count, 32);
874 // do not trigger the warning if the data looks good
875 if (!triggered && jtag_idcode_is_final(idcode))
876 continue;
877 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
878 count, idcode);
879 triggered = true;
880 }
881 }
882
883 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
884 {
885 if (0 == tap->expected_ids_cnt)
886 {
887 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
888 #if 0
889 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
890 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
891 #endif
892 return true;
893 }
894
895 /* Loop over the expected identification codes and test for a match */
896 u8 ii;
897 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
898 {
899 if (tap->idcode == tap->expected_ids[ii])
900 break;
901 }
902
903 /* If none of the expected ids matched, log an error */
904 if (ii != tap->expected_ids_cnt)
905 {
906 LOG_INFO("JTAG Tap/device matched");
907 return true;
908 }
909 jtag_examine_chain_display(LOG_LVL_ERROR, "got",
910 tap->dotted_name, tap->idcode);
911 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
912 {
913 char msg[32];
914 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
915 ii + 1, tap->expected_ids_cnt);
916 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
917 tap->dotted_name, tap->expected_ids[ii]);
918 }
919 return false;
920 }
921
922 /* Try to examine chain layout according to IEEE 1149.1 §12
923 */
924 int jtag_examine_chain(void)
925 {
926 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
927 unsigned device_count = 0;
928
929 jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
930
931 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
932 return ERROR_JTAG_INIT_FAILED;
933
934 /* point at the 1st tap */
935 jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
936 if (tap == NULL)
937 {
938 LOG_ERROR("JTAG: No taps enabled?");
939 return ERROR_JTAG_INIT_FAILED;
940 }
941
942 for (unsigned bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
943 {
944 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
945 if ((idcode & 1) == 0)
946 {
947 /* LSB must not be 0, this indicates a device in bypass */
948 LOG_WARNING("Tap/Device does not have IDCODE");
949 idcode = 0;
950
951 bit_count += 1;
952 }
953 else
954 {
955 /*
956 * End of chain (invalid manufacturer ID) some devices, such
957 * as AVR will output all 1's instead of TDI input value at
958 * end of chain.
959 */
960 if (jtag_idcode_is_final(idcode))
961 {
962 jtag_examine_chain_end(idcode_buffer,
963 bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
964 break;
965 }
966
967 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
968 tap ? tap->dotted_name : "(not-named)",
969 idcode);
970
971 bit_count += 32;
972 }
973 device_count++;
974 if (!tap)
975 continue;
976
977 tap->idcode = idcode;
978
979 // ensure the TAP ID does matches what was expected
980 if (!jtag_examine_chain_match_tap(tap))
981 return ERROR_JTAG_INIT_FAILED;
982
983 tap = jtag_tap_next_enabled(tap);
984 }
985
986 /* see if number of discovered devices matches configuration */
987 if (device_count != jtag_tap_count_enabled())
988 {
989 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
990 "does not match (enabled) configuration (%i), total taps: %d",
991 device_count, jtag_tap_count_enabled(), jtag_tap_count());
992 LOG_ERROR("check the config file and ensure proper JTAG communication"
993 " (connections, speed, ...)");
994 return ERROR_JTAG_INIT_FAILED;
995 }
996
997 return ERROR_OK;
998 }
999
1000 int jtag_validate_chain(void)
1001 {
1002 jtag_tap_t *tap;
1003 int total_ir_length = 0;
1004 u8 *ir_test = NULL;
1005 scan_field_t field;
1006 int chain_pos = 0;
1007
1008 tap = NULL;
1009 total_ir_length = 0;
1010 for(;;){
1011 tap = jtag_tap_next_enabled(tap);
1012 if( tap == NULL ){
1013 break;
1014 }
1015 total_ir_length += tap->ir_length;
1016 }
1017
1018 total_ir_length += 2;
1019 ir_test = malloc(CEIL(total_ir_length, 8));
1020 buf_set_ones(ir_test, total_ir_length);
1021
1022 field.tap = NULL;
1023 field.num_bits = total_ir_length;
1024 field.out_value = ir_test;
1025 field.in_value = ir_test;
1026
1027
1028 jtag_add_plain_ir_scan(1, &field, TAP_RESET);
1029 jtag_execute_queue();
1030
1031 tap = NULL;
1032 chain_pos = 0;
1033 int val;
1034 for(;;){
1035 tap = jtag_tap_next_enabled(tap);
1036 if( tap == NULL ){
1037 break;
1038 }
1039
1040 val = buf_get_u32(ir_test, chain_pos, 2);
1041 if (val != 0x1)
1042 {
1043 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1044 LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf, jtag_tap_name(tap), chain_pos, val);
1045 free(cbuf);
1046 free(ir_test);
1047 return ERROR_JTAG_INIT_FAILED;
1048 }
1049 chain_pos += tap->ir_length;
1050 }
1051
1052 val = buf_get_u32(ir_test, chain_pos, 2);
1053 if (val != 0x3)
1054 {
1055 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1056 LOG_ERROR("Could not validate end of JTAG scan chain, IR mismatch, scan returned 0x%s. pos=%d expected 0x3 got %0x", cbuf, chain_pos, val);
1057 free(cbuf);
1058 free(ir_test);
1059 return ERROR_JTAG_INIT_FAILED;
1060 }
1061
1062 free(ir_test);
1063
1064 return ERROR_OK;
1065 }
1066
1067
1068 void jtag_tap_init(jtag_tap_t *tap)
1069 {
1070 assert(0 != tap->ir_length);
1071
1072 tap->expected = malloc(tap->ir_length);
1073 tap->expected_mask = malloc(tap->ir_length);
1074 tap->cur_instr = malloc(tap->ir_length);
1075
1076 buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1077 buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1078 buf_set_ones(tap->cur_instr, tap->ir_length);
1079
1080 // place TAP in bypass mode
1081 tap->bypass = 1;
1082 // register the reset callback for the TAP
1083 jtag_register_event_callback(&jtag_reset_callback, tap);
1084
1085 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1086 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1087 tap->abs_chain_position, tap->ir_length,
1088 tap->ir_capture_value, tap->ir_capture_mask);
1089 jtag_tap_add(tap);
1090 }
1091
1092 void jtag_tap_free(jtag_tap_t *tap)
1093 {
1094 /// @todo is anything missing? no memory leaks please
1095 free((void *)tap->expected_ids);
1096 free((void *)tap->chip);
1097 free((void *)tap->tapname);
1098 free((void *)tap->dotted_name);
1099 free(tap);
1100 }
1101
1102 int jtag_interface_init(struct command_context_s *cmd_ctx)
1103 {
1104 if (jtag)
1105 return ERROR_OK;
1106
1107 if (!jtag_interface)
1108 {
1109 /* nothing was previously specified by "interface" command */
1110 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1111 return ERROR_JTAG_INVALID_INTERFACE;
1112 }
1113 if(hasKHz)
1114 {
1115 jtag_interface->khz(jtag_get_speed_khz(), &jtag_speed);
1116 hasKHz = false;
1117 }
1118
1119 if (jtag_interface->init() != ERROR_OK)
1120 return ERROR_JTAG_INIT_FAILED;
1121
1122 jtag = jtag_interface;
1123 return ERROR_OK;
1124 }
1125
1126 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1127 {
1128 jtag_tap_t *tap;
1129 int retval;
1130
1131 LOG_DEBUG("Init JTAG chain");
1132
1133 tap = jtag_tap_next_enabled(NULL);
1134 if( tap == NULL ){
1135 LOG_ERROR("There are no enabled taps?");
1136 return ERROR_JTAG_INIT_FAILED;
1137 }
1138
1139 jtag_add_tlr();
1140 if ((retval=jtag_execute_queue())!=ERROR_OK)
1141 return retval;
1142
1143 /* examine chain first, as this could discover the real chain layout */
1144 if (jtag_examine_chain() != ERROR_OK)
1145 {
1146 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1147 }
1148
1149 if (jtag_validate_chain() != ERROR_OK)
1150 {
1151 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1152 }
1153
1154 return ERROR_OK;
1155 }
1156
1157 int jtag_interface_quit(void)
1158 {
1159 if (!jtag || !jtag->quit)
1160 return ERROR_OK;
1161
1162 // close the JTAG interface
1163 int result = jtag->quit();
1164 if (ERROR_OK != result)
1165 LOG_ERROR("failed: %d", result);
1166
1167 return ERROR_OK;
1168 }
1169
1170
1171 int jtag_init_reset(struct command_context_s *cmd_ctx)
1172 {
1173 int retval;
1174
1175 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1176 return retval;
1177
1178 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
1179
1180 /* Reset can happen after a power cycle.
1181 *
1182 * Ideally we would only assert TRST or run RESET before the target reset.
1183 *
1184 * However w/srst_pulls_trst, trst is asserted together with the target
1185 * reset whether we want it or not.
1186 *
1187 * NB! Some targets have JTAG circuitry disabled until a
1188 * trst & srst has been asserted.
1189 *
1190 * NB! here we assume nsrst/ntrst delay are sufficient!
1191 *
1192 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1193 *
1194 */
1195 jtag_add_reset(1, 0); /* RESET or TRST */
1196 if (jtag_reset_config & RESET_HAS_SRST)
1197 {
1198 jtag_add_reset(1, 1);
1199 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1200 jtag_add_reset(0, 1);
1201 }
1202 jtag_add_reset(0, 0);
1203 if ((retval = jtag_execute_queue()) != ERROR_OK)
1204 return retval;
1205
1206 /* Check that we can communication on the JTAG chain + eventually we want to
1207 * be able to perform enumeration only after OpenOCD has started
1208 * telnet and GDB server
1209 *
1210 * That would allow users to more easily perform any magic they need to before
1211 * reset happens.
1212 */
1213 return jtag_init_inner(cmd_ctx);
1214 }
1215
1216 int jtag_init(struct command_context_s *cmd_ctx)
1217 {
1218 int retval;
1219 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1220 return retval;
1221 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1222 {
1223 return ERROR_OK;
1224 }
1225 return jtag_init_reset(cmd_ctx);
1226 }
1227
1228 void jtag_set_speed_khz(unsigned khz)
1229 {
1230 speed_khz = khz;
1231 }
1232 unsigned jtag_get_speed_khz(void)
1233 {
1234 return speed_khz;
1235 }
1236 int jtag_config_khz(unsigned khz)
1237 {
1238 LOG_DEBUG("handle jtag khz");
1239 jtag_set_speed_khz(khz);
1240
1241 int cur_speed = 0;
1242 if (jtag != NULL)
1243 {
1244 LOG_DEBUG("have interface set up");
1245 int speed_div1;
1246 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1247 if (ERROR_OK != retval)
1248 {
1249 jtag_set_speed_khz(0);
1250 return retval;
1251 }
1252 cur_speed = speed_div1;
1253 }
1254 return jtag_set_speed(cur_speed);
1255 }
1256
1257 int jtag_get_speed(void)
1258 {
1259 return jtag_speed;
1260 }
1261
1262 int jtag_set_speed(int speed)
1263 {
1264 jtag_speed = speed;
1265 /* this command can be called during CONFIG,
1266 * in which case jtag isn't initialized */
1267 hasKHz = !jtag;
1268 return jtag ? jtag->speed(speed) : ERROR_OK;
1269 }
1270
1271 int jtag_get_speed_readable(int *speed)
1272 {
1273 return jtag ? jtag->speed_div(jtag_get_speed(), speed) : ERROR_OK;
1274 }
1275
1276
1277 void jtag_set_verify(bool enable)
1278 {
1279 jtag_verify = enable;
1280 }
1281
1282 bool jtag_will_verify()
1283 {
1284 return jtag_verify;
1285 }
1286
1287 void jtag_set_verify_capture_ir(bool enable)
1288 {
1289 jtag_verify_capture_ir = enable;
1290 }
1291
1292 bool jtag_will_verify_capture_ir()
1293 {
1294 return jtag_verify_capture_ir;
1295 }
1296
1297 int jtag_power_dropout(int *dropout)
1298 {
1299 return jtag->power_dropout(dropout);
1300 }
1301
1302 int jtag_srst_asserted(int *srst_asserted)
1303 {
1304 return jtag->srst_asserted(srst_asserted);
1305 }
1306
1307 enum reset_types jtag_get_reset_config(void)
1308 {
1309 return jtag_reset_config;
1310 }
1311 void jtag_set_reset_config(enum reset_types type)
1312 {
1313 jtag_reset_config = type;
1314 }
1315
1316 int jtag_get_trst(void)
1317 {
1318 return jtag_trst;
1319 }
1320 int jtag_get_srst(void)
1321 {
1322 return jtag_srst;
1323 }
1324
1325 void jtag_set_nsrst_delay(unsigned delay)
1326 {
1327 jtag_nsrst_delay = delay;
1328 }
1329 unsigned jtag_get_nsrst_delay(void)
1330 {
1331 return jtag_nsrst_delay;
1332 }
1333 void jtag_set_ntrst_delay(unsigned delay)
1334 {
1335 jtag_ntrst_delay = delay;
1336 }
1337 unsigned jtag_get_ntrst_delay(void)
1338 {
1339 return jtag_ntrst_delay;
1340 }

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)