JTAG: fix autoprobe failure.
[openocd.git] / src / jtag / core.c
1 /***************************************************************************
2 * Copyright (C) 2009 Zachary T Welch *
3 * zw@superlucidity.net *
4 * *
5 * Copyright (C) 2007,2008,2009 Ø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) 2005 by Dominic Rath *
13 * Dominic.Rath@gmx.de *
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 struct scan_field *in_fields, tap_state_t state),
47 int in_num_fields, struct scan_field *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 const char *jtag_event_strings[] =
62 {
63 [JTAG_TRST_ASSERTED] = "TAP reset",
64 [JTAG_TAP_EVENT_SETUP] = "TAP setup",
65 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
66 [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
67 };
68
69 /*
70 * JTAG adapters must initialize with TRST and SRST de-asserted
71 * (they're negative logic, so that means *high*). But some
72 * hardware doesn't necessarily work that way ... so set things
73 * up so that jtag_init() always forces that state.
74 */
75 static int jtag_trst = -1;
76 static int jtag_srst = -1;
77
78 /**
79 * List all TAPs that have been created.
80 */
81 static struct jtag_tap *__jtag_all_taps = NULL;
82 /**
83 * The number of TAPs in the __jtag_all_taps list, used to track the
84 * assigned chain position to new TAPs
85 */
86 static unsigned jtag_num_taps = 0;
87
88 static enum reset_types jtag_reset_config = RESET_NONE;
89 static tap_state_t cmd_queue_end_state = TAP_RESET;
90 tap_state_t cmd_queue_cur_state = TAP_RESET;
91
92 static bool jtag_verify_capture_ir = true;
93 static int jtag_verify = 1;
94
95 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
96 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
97 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
98 static int jtag_nsrst_assert_width = 0; /* width of assertion */
99 static int jtag_ntrst_assert_width = 0; /* width of assertion */
100
101 /**
102 * Contains a single callback along with a pointer that will be passed
103 * when an event occurs.
104 */
105 struct jtag_event_callback {
106 /// a event callback
107 jtag_event_handler_t callback;
108 /// the private data to pass to the callback
109 void* priv;
110 /// the next callback
111 struct jtag_event_callback* next;
112 };
113
114 /* callbacks to inform high-level handlers about JTAG state changes */
115 static struct jtag_event_callback *jtag_event_callbacks;
116
117 /* speed in kHz*/
118 static int speed_khz = 0;
119 /* speed to fallback to when RCLK is requested but not supported */
120 static int rclk_fallback_speed_khz = 0;
121 static enum {CLOCK_MODE_SPEED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
122 static int jtag_speed = 0;
123
124 static struct jtag_interface *jtag = NULL;
125
126 /* configuration */
127 struct jtag_interface *jtag_interface = NULL;
128
129 void jtag_set_error(int error)
130 {
131 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
132 return;
133 jtag_error = error;
134 }
135 int jtag_get_error(void)
136 {
137 return jtag_error;
138 }
139 int jtag_error_clear(void)
140 {
141 int temp = jtag_error;
142 jtag_error = ERROR_OK;
143 return temp;
144 }
145
146 /************/
147
148 static bool jtag_poll = 1;
149
150 bool is_jtag_poll_safe(void)
151 {
152 /* Polling can be disabled explicitly with set_enabled(false).
153 * It is also implicitly disabled while TRST is active and
154 * while SRST is gating the JTAG clock.
155 */
156 if (!jtag_poll || jtag_trst != 0)
157 return false;
158 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
159 }
160
161 bool jtag_poll_get_enabled(void)
162 {
163 return jtag_poll;
164 }
165
166 void jtag_poll_set_enabled(bool value)
167 {
168 jtag_poll = value;
169 }
170
171 /************/
172
173 struct jtag_tap *jtag_all_taps(void)
174 {
175 return __jtag_all_taps;
176 };
177
178 unsigned jtag_tap_count(void)
179 {
180 return jtag_num_taps;
181 }
182
183 unsigned jtag_tap_count_enabled(void)
184 {
185 struct jtag_tap *t = jtag_all_taps();
186 unsigned n = 0;
187 while (t)
188 {
189 if (t->enabled)
190 n++;
191 t = t->next_tap;
192 }
193 return n;
194 }
195
196 /// Append a new TAP to the chain of all taps.
197 void jtag_tap_add(struct jtag_tap *t)
198 {
199 t->abs_chain_position = jtag_num_taps++;
200
201 struct jtag_tap **tap = &__jtag_all_taps;
202 while (*tap != NULL)
203 tap = &(*tap)->next_tap;
204 *tap = t;
205 }
206
207 /* returns a pointer to the n-th device in the scan chain */
208 static inline struct jtag_tap *jtag_tap_by_position(unsigned n)
209 {
210 struct jtag_tap *t = jtag_all_taps();
211
212 while (t && n-- > 0)
213 t = t->next_tap;
214
215 return t;
216 }
217
218 struct jtag_tap *jtag_tap_by_string(const char *s)
219 {
220 /* try by name first */
221 struct jtag_tap *t = jtag_all_taps();
222
223 while (t)
224 {
225 if (0 == strcmp(t->dotted_name, s))
226 return t;
227 t = t->next_tap;
228 }
229
230 /* no tap found by name, so try to parse the name as a number */
231 unsigned n;
232 if (parse_uint(s, &n) != ERROR_OK)
233 return NULL;
234
235 /* FIXME remove this numeric fallback code late June 2010, along
236 * with all info in the User's Guide that TAPs have numeric IDs.
237 * Also update "scan_chain" output to not display the numbers.
238 */
239 t = jtag_tap_by_position(n);
240 if (t)
241 LOG_WARNING("Specify TAP '%s' by name, not number %u",
242 t->dotted_name, n);
243
244 return t;
245 }
246
247 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
248 {
249 const char *cp = Jim_GetString(o, NULL);
250 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
251 if (NULL == cp)
252 cp = "(unknown)";
253 if (NULL == t)
254 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
255 return t;
256 }
257
258 struct jtag_tap* jtag_tap_next_enabled(struct jtag_tap* p)
259 {
260 p = p ? p->next_tap : jtag_all_taps();
261 while (p)
262 {
263 if (p->enabled)
264 return p;
265 p = p->next_tap;
266 }
267 return NULL;
268 }
269
270 const char *jtag_tap_name(const struct jtag_tap *tap)
271 {
272 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
273 }
274
275
276 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
277 {
278 struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
279
280 if (callback == NULL)
281 {
282 return ERROR_INVALID_ARGUMENTS;
283 }
284
285 if (*callbacks_p)
286 {
287 while ((*callbacks_p)->next)
288 callbacks_p = &((*callbacks_p)->next);
289 callbacks_p = &((*callbacks_p)->next);
290 }
291
292 (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
293 (*callbacks_p)->callback = callback;
294 (*callbacks_p)->priv = priv;
295 (*callbacks_p)->next = NULL;
296
297 return ERROR_OK;
298 }
299
300 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
301 {
302 struct jtag_event_callback **callbacks_p;
303 struct jtag_event_callback **next;
304
305 if (callback == NULL)
306 {
307 return ERROR_INVALID_ARGUMENTS;
308 }
309
310 for (callbacks_p = &jtag_event_callbacks;
311 *callbacks_p != NULL;
312 callbacks_p = next)
313 {
314 next = &((*callbacks_p)->next);
315
316 if ((*callbacks_p)->priv != priv)
317 continue;
318
319 if ((*callbacks_p)->callback == callback)
320 {
321 free(*callbacks_p);
322 *callbacks_p = *next;
323 }
324 }
325
326 return ERROR_OK;
327 }
328
329 int jtag_call_event_callbacks(enum jtag_event event)
330 {
331 struct jtag_event_callback *callback = jtag_event_callbacks;
332
333 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
334
335 while (callback)
336 {
337 struct jtag_event_callback *next;
338
339 /* callback may remove itself */
340 next = callback->next;
341 callback->callback(event, callback->priv);
342 callback = next;
343 }
344
345 return ERROR_OK;
346 }
347
348 static void jtag_checks(void)
349 {
350 assert(jtag_trst == 0);
351 }
352
353 static void jtag_prelude(tap_state_t state)
354 {
355 jtag_checks();
356
357 assert(state != TAP_INVALID);
358
359 cmd_queue_cur_state = state;
360 }
361
362 void jtag_alloc_in_value32(struct scan_field *field)
363 {
364 interface_jtag_alloc_in_value32(field);
365 }
366
367 void jtag_add_ir_scan_noverify(int in_count, const struct scan_field *in_fields,
368 tap_state_t state)
369 {
370 jtag_prelude(state);
371
372 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
373 jtag_set_error(retval);
374 }
375
376
377 void jtag_add_ir_scan(int in_num_fields, struct scan_field *in_fields, tap_state_t state)
378 {
379 assert(state != TAP_RESET);
380
381 if (jtag_verify && jtag_verify_capture_ir)
382 {
383 /* 8 x 32 bit id's is enough for all invocations */
384
385 for (int j = 0; j < in_num_fields; j++)
386 {
387 /* if we are to run a verification of the ir scan, we need to get the input back.
388 * We may have to allocate space if the caller didn't ask for the input back.
389 */
390 in_fields[j].check_value = in_fields[j].tap->expected;
391 in_fields[j].check_mask = in_fields[j].tap->expected_mask;
392 }
393 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
394 } else
395 {
396 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
397 }
398 }
399
400 void jtag_add_plain_ir_scan(int in_num_fields, const struct scan_field *in_fields,
401 tap_state_t state)
402 {
403 assert(state != TAP_RESET);
404
405 jtag_prelude(state);
406
407 int retval = interface_jtag_add_plain_ir_scan(
408 in_num_fields, in_fields, state);
409 jtag_set_error(retval);
410 }
411
412 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
413 {
414 interface_jtag_add_callback(f, data0);
415 }
416
417 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
418 jtag_callback_data_t data1, jtag_callback_data_t data2,
419 jtag_callback_data_t data3)
420 {
421 interface_jtag_add_callback4(f, data0, data1, data2, data3);
422 }
423
424 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
425 uint8_t *in_check_mask, int num_bits);
426
427 static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
428 {
429 return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
430 }
431
432 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const struct scan_field *in_fields, tap_state_t state),
433 int in_num_fields, struct scan_field *in_fields, tap_state_t state)
434 {
435 for (int i = 0; i < in_num_fields; i++)
436 {
437 struct scan_field *field = &in_fields[i];
438 field->allocated = 0;
439 field->modified = 0;
440 if (field->check_value || field->in_value)
441 continue;
442 interface_jtag_add_scan_check_alloc(field);
443 field->modified = 1;
444 }
445
446 jtag_add_scan(in_num_fields, in_fields, state);
447
448 for (int i = 0; i < in_num_fields; i++)
449 {
450 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
451 {
452 /* this is synchronous for a minidriver */
453 jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
454 (jtag_callback_data_t)in_fields[i].check_value,
455 (jtag_callback_data_t)in_fields[i].check_mask,
456 (jtag_callback_data_t)in_fields[i].num_bits);
457 }
458 if (in_fields[i].allocated)
459 {
460 free(in_fields[i].in_value);
461 }
462 if (in_fields[i].modified)
463 {
464 in_fields[i].in_value = NULL;
465 }
466 }
467 }
468
469 void jtag_add_dr_scan_check(int in_num_fields, struct scan_field *in_fields, tap_state_t state)
470 {
471 if (jtag_verify)
472 {
473 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
474 } else
475 {
476 jtag_add_dr_scan(in_num_fields, in_fields, state);
477 }
478 }
479
480
481 void jtag_add_dr_scan(int in_num_fields, const struct scan_field *in_fields,
482 tap_state_t state)
483 {
484 assert(state != TAP_RESET);
485
486 jtag_prelude(state);
487
488 int retval;
489 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
490 jtag_set_error(retval);
491 }
492
493 void jtag_add_plain_dr_scan(int in_num_fields, const struct scan_field *in_fields,
494 tap_state_t state)
495 {
496 assert(state != TAP_RESET);
497
498 jtag_prelude(state);
499
500 int retval;
501 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
502 jtag_set_error(retval);
503 }
504
505 void jtag_add_dr_out(struct jtag_tap* tap,
506 int num_fields, const int* num_bits, const uint32_t* value,
507 tap_state_t end_state)
508 {
509 assert(end_state != TAP_RESET);
510 assert(end_state != TAP_INVALID);
511
512 cmd_queue_cur_state = end_state;
513
514 interface_jtag_add_dr_out(tap,
515 num_fields, num_bits, value,
516 end_state);
517 }
518
519 void jtag_add_tlr(void)
520 {
521 jtag_prelude(TAP_RESET);
522 jtag_set_error(interface_jtag_add_tlr());
523
524 /* NOTE: order here matches TRST path in jtag_add_reset() */
525 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
526 jtag_notify_event(JTAG_TRST_ASSERTED);
527 }
528
529 void jtag_add_pathmove(int num_states, const tap_state_t *path)
530 {
531 tap_state_t cur_state = cmd_queue_cur_state;
532
533 /* the last state has to be a stable state */
534 if (!tap_is_state_stable(path[num_states - 1]))
535 {
536 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
537 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
538 return;
539 }
540
541 for (int i = 0; i < num_states; i++)
542 {
543 if (path[i] == TAP_RESET)
544 {
545 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
546 jtag_set_error(ERROR_JTAG_STATE_INVALID);
547 return;
548 }
549
550 if (tap_state_transition(cur_state, true) != path[i]
551 && tap_state_transition(cur_state, false) != path[i])
552 {
553 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
554 tap_state_name(cur_state), tap_state_name(path[i]));
555 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
556 return;
557 }
558 cur_state = path[i];
559 }
560
561 jtag_checks();
562
563 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
564 cmd_queue_cur_state = path[num_states - 1];
565 }
566
567 int jtag_add_statemove(tap_state_t goal_state)
568 {
569 tap_state_t cur_state = cmd_queue_cur_state;
570
571 LOG_DEBUG("cur_state=%s goal_state=%s",
572 tap_state_name(cur_state),
573 tap_state_name(goal_state));
574
575
576 /* If goal is RESET, be paranoid and force that that transition
577 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
578 */
579 if (goal_state == TAP_RESET)
580 jtag_add_tlr();
581 else if (goal_state == cur_state)
582 /* nothing to do */ ;
583
584 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
585 {
586 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
587 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
588 tap_state_t moves[8];
589 assert(tms_count < ARRAY_SIZE(moves));
590
591 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
592 {
593 bool bit = tms_bits & 1;
594
595 cur_state = tap_state_transition(cur_state, bit);
596 moves[i] = cur_state;
597 }
598
599 jtag_add_pathmove(tms_count, moves);
600 }
601 else if (tap_state_transition(cur_state, true) == goal_state
602 || tap_state_transition(cur_state, false) == goal_state)
603 {
604 jtag_add_pathmove(1, &goal_state);
605 }
606
607 else
608 return ERROR_FAIL;
609
610 return ERROR_OK;
611 }
612
613 void jtag_add_runtest(int num_cycles, tap_state_t state)
614 {
615 jtag_prelude(state);
616 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
617 }
618
619
620 void jtag_add_clocks(int num_cycles)
621 {
622 if (!tap_is_state_stable(cmd_queue_cur_state))
623 {
624 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
625 tap_state_name(cmd_queue_cur_state));
626 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
627 return;
628 }
629
630 if (num_cycles > 0)
631 {
632 jtag_checks();
633 jtag_set_error(interface_jtag_add_clocks(num_cycles));
634 }
635 }
636
637 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
638 {
639 int trst_with_tlr = 0;
640 int new_srst = 0;
641 int new_trst = 0;
642
643 /* Without SRST, we must use target-specific JTAG operations
644 * on each target; callers should not be requesting SRST when
645 * that signal doesn't exist.
646 *
647 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
648 * can kick in even if the JTAG adapter can't drive TRST.
649 */
650 if (req_srst) {
651 if (!(jtag_reset_config & RESET_HAS_SRST)) {
652 LOG_ERROR("BUG: can't assert SRST");
653 jtag_set_error(ERROR_FAIL);
654 return;
655 }
656 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
657 && !req_tlr_or_trst) {
658 LOG_ERROR("BUG: can't assert only SRST");
659 jtag_set_error(ERROR_FAIL);
660 return;
661 }
662 new_srst = 1;
663 }
664
665 /* JTAG reset (entry to TAP_RESET state) can always be achieved
666 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
667 * state first. TRST accelerates it, and bypasses those states.
668 *
669 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
670 * can kick in even if the JTAG adapter can't drive SRST.
671 */
672 if (req_tlr_or_trst) {
673 if (!(jtag_reset_config & RESET_HAS_TRST))
674 trst_with_tlr = 1;
675 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
676 && !req_srst)
677 trst_with_tlr = 1;
678 else
679 new_trst = 1;
680 }
681
682 /* Maybe change TRST and/or SRST signal state */
683 if (jtag_srst != new_srst || jtag_trst != new_trst) {
684 int retval;
685
686 retval = interface_jtag_add_reset(new_trst, new_srst);
687 if (retval != ERROR_OK)
688 jtag_set_error(retval);
689 else
690 retval = jtag_execute_queue();
691
692 if (retval != ERROR_OK) {
693 LOG_ERROR("TRST/SRST error %d", retval);
694 return;
695 }
696 }
697
698 /* SRST resets everything hooked up to that signal */
699 if (jtag_srst != new_srst) {
700 jtag_srst = new_srst;
701 if (jtag_srst)
702 {
703 LOG_DEBUG("SRST line asserted");
704 if (jtag_nsrst_assert_width)
705 jtag_add_sleep(jtag_nsrst_assert_width * 1000);
706 }
707 else {
708 LOG_DEBUG("SRST line released");
709 if (jtag_nsrst_delay)
710 jtag_add_sleep(jtag_nsrst_delay * 1000);
711 }
712 }
713
714 /* Maybe enter the JTAG TAP_RESET state ...
715 * - using only TMS, TCK, and the JTAG state machine
716 * - or else more directly, using TRST
717 *
718 * TAP_RESET should be invisible to non-debug parts of the system.
719 */
720 if (trst_with_tlr) {
721 LOG_DEBUG("JTAG reset with TLR instead of TRST");
722 jtag_set_end_state(TAP_RESET);
723 jtag_add_tlr();
724
725 } else if (jtag_trst != new_trst) {
726 jtag_trst = new_trst;
727 if (jtag_trst) {
728 LOG_DEBUG("TRST line asserted");
729 tap_set_state(TAP_RESET);
730 if (jtag_ntrst_assert_width)
731 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
732 } else {
733 LOG_DEBUG("TRST line released");
734 if (jtag_ntrst_delay)
735 jtag_add_sleep(jtag_ntrst_delay * 1000);
736
737 /* We just asserted nTRST, so we're now in TAP_RESET.
738 * Inform possible listeners about this, now that
739 * JTAG instructions and data can be shifted. This
740 * sequence must match jtag_add_tlr().
741 */
742 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
743 jtag_notify_event(JTAG_TRST_ASSERTED);
744 }
745 }
746 }
747
748 tap_state_t jtag_set_end_state(tap_state_t state)
749 {
750 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
751 {
752 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
753 }
754
755 if (state != TAP_INVALID)
756 cmd_queue_end_state = state;
757 return cmd_queue_end_state;
758 }
759
760 tap_state_t jtag_get_end_state(void)
761 {
762 return cmd_queue_end_state;
763 }
764
765 void jtag_add_sleep(uint32_t us)
766 {
767 /// @todo Here, keep_alive() appears to be a layering violation!!!
768 keep_alive();
769 jtag_set_error(interface_jtag_add_sleep(us));
770 }
771
772 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
773 uint8_t *in_check_mask, int num_bits)
774 {
775 int retval = ERROR_OK;
776
777 int compare_failed = 0;
778
779 if (in_check_mask)
780 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
781 else
782 compare_failed = buf_cmp(captured, in_check_value, num_bits);
783
784 if (compare_failed) {
785 char *captured_str, *in_check_value_str;
786 int bits = (num_bits > DEBUG_JTAG_IOZ)
787 ? DEBUG_JTAG_IOZ
788 : num_bits;
789
790 /* NOTE: we've lost diagnostic context here -- 'which tap' */
791
792 captured_str = buf_to_str(captured, bits, 16);
793 in_check_value_str = buf_to_str(in_check_value, bits, 16);
794
795 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
796 captured_str);
797 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
798
799 free(captured_str);
800 free(in_check_value_str);
801
802 if (in_check_mask) {
803 char *in_check_mask_str;
804
805 in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
806 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
807 free(in_check_mask_str);
808 }
809
810 retval = ERROR_JTAG_QUEUE_FAILED;
811 }
812 return retval;
813 }
814
815 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
816 {
817 assert(field->in_value != NULL);
818
819 if (value == NULL)
820 {
821 /* no checking to do */
822 return;
823 }
824
825 jtag_execute_queue_noclear();
826
827 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
828 jtag_set_error(retval);
829 }
830
831
832
833 int default_interface_jtag_execute_queue(void)
834 {
835 if (NULL == jtag)
836 {
837 LOG_ERROR("No JTAG interface configured yet. "
838 "Issue 'init' command in startup scripts "
839 "before communicating with targets.");
840 return ERROR_FAIL;
841 }
842
843 return jtag->execute_queue();
844 }
845
846 void jtag_execute_queue_noclear(void)
847 {
848 jtag_flush_queue_count++;
849 jtag_set_error(interface_jtag_execute_queue());
850 }
851
852 int jtag_get_flush_queue_count(void)
853 {
854 return jtag_flush_queue_count;
855 }
856
857 int jtag_execute_queue(void)
858 {
859 jtag_execute_queue_noclear();
860 return jtag_error_clear();
861 }
862
863 static int jtag_reset_callback(enum jtag_event event, void *priv)
864 {
865 struct jtag_tap *tap = priv;
866
867 if (event == JTAG_TRST_ASSERTED)
868 {
869 tap->enabled = !tap->disabled_after_reset;
870
871 /* current instruction is either BYPASS or IDCODE */
872 buf_set_ones(tap->cur_instr, tap->ir_length);
873 tap->bypass = 1;
874 }
875
876 return ERROR_OK;
877 }
878
879 void jtag_sleep(uint32_t us)
880 {
881 alive_sleep(us/1000);
882 }
883
884 /* Maximum number of enabled JTAG devices we expect in the scan chain,
885 * plus one (to detect garbage at the end). Devices that don't support
886 * IDCODE take up fewer bits, possibly allowing a few more devices.
887 */
888 #define JTAG_MAX_CHAIN_SIZE 20
889
890 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
891 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
892 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
893
894 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
895 * know that no valid TAP will have it as an IDCODE value.
896 */
897 #define END_OF_CHAIN_FLAG 0x000000ff
898
899 /* a larger IR length than we ever expect to autoprobe */
900 #define JTAG_IRLEN_MAX 60
901
902 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
903 {
904 struct scan_field field = {
905 .tap = NULL,
906 .num_bits = num_idcode * 32,
907 .out_value = idcode_buffer,
908 .in_value = idcode_buffer,
909 };
910
911 // initialize to the end of chain ID value
912 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
913 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
914
915 jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
916 jtag_add_tlr();
917 return jtag_execute_queue();
918 }
919
920 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
921 {
922 uint8_t zero_check = 0x0;
923 uint8_t one_check = 0xff;
924
925 for (unsigned i = 0; i < count * 4; i++)
926 {
927 zero_check |= idcodes[i];
928 one_check &= idcodes[i];
929 }
930
931 /* if there wasn't a single non-zero bit or if all bits were one,
932 * the scan is not valid. We wrote a mix of both values; either
933 *
934 * - There's a hardware issue (almost certainly):
935 * + all-zeroes can mean a target stuck in JTAG reset
936 * + all-ones tends to mean no target
937 * - The scan chain is WAY longer than we can handle, *AND* either
938 * + there are several hundreds of TAPs in bypass, or
939 * + at least a few dozen TAPs all have an all-ones IDCODE
940 */
941 if (zero_check == 0x00 || one_check == 0xff)
942 {
943 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
944 (zero_check == 0x00) ? "zeroes" : "ones");
945 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
946 return false;
947 }
948 return true;
949 }
950
951 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
952 const char *name, uint32_t idcode)
953 {
954 log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
955 "JTAG tap: %s %16.16s: 0x%08x "
956 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
957 name, msg,
958 (unsigned int)idcode,
959 (unsigned int)EXTRACT_MFG(idcode),
960 (unsigned int)EXTRACT_PART(idcode),
961 (unsigned int)EXTRACT_VER(idcode));
962 }
963
964 static bool jtag_idcode_is_final(uint32_t idcode)
965 {
966 /*
967 * Some devices, such as AVR8, will output all 1's instead
968 * of TDI input value at end of chain. Allow those values
969 * instead of failing.
970 */
971 return idcode == END_OF_CHAIN_FLAG || idcode == 0xFFFFFFFF;
972 }
973
974 /**
975 * This helper checks that remaining bits in the examined chain data are
976 * all as expected, but a single JTAG device requires only 64 bits to be
977 * read back correctly. This can help identify and diagnose problems
978 * with the JTAG chain earlier, gives more helpful/explicit error messages.
979 * Returns TRUE iff garbage was found.
980 */
981 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
982 {
983 bool triggered = false;
984 for (; count < max - 31; count += 32)
985 {
986 uint32_t idcode = buf_get_u32(idcodes, count, 32);
987
988 /* do not trigger the warning if the data looks good */
989 if (jtag_idcode_is_final(idcode))
990 continue;
991 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
992 count, (unsigned int)idcode);
993 triggered = true;
994 }
995 return triggered;
996 }
997
998 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
999 {
1000 /* ignore expected BYPASS codes; warn otherwise */
1001 if (0 == tap->expected_ids_cnt && !tap->idcode)
1002 return true;
1003
1004 /* Loop over the expected identification codes and test for a match */
1005 unsigned ii, limit = tap->expected_ids_cnt;
1006
1007 for (ii = 0; ii < limit; ii++)
1008 {
1009 if (tap->idcode == tap->expected_ids[ii])
1010 return true;
1011
1012 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1013 if (0 == tap->expected_ids[ii])
1014 return true;
1015 }
1016
1017 /* If none of the expected ids matched, warn */
1018 jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
1019 tap->dotted_name, tap->idcode);
1020 for (ii = 0; ii < limit; ii++)
1021 {
1022 char msg[32];
1023
1024 snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, limit);
1025 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1026 tap->dotted_name, tap->expected_ids[ii]);
1027 }
1028 return false;
1029 }
1030
1031 /* Try to examine chain layout according to IEEE 1149.1 §12
1032 * This is called a "blind interrogation" of the scan chain.
1033 */
1034 static int jtag_examine_chain(void)
1035 {
1036 uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1037 unsigned bit_count;
1038 int retval;
1039 int tapcount = 0;
1040 bool autoprobe = false;
1041
1042 /* DR scan to collect BYPASS or IDCODE register contents.
1043 * Then make sure the scan data has both ones and zeroes.
1044 */
1045 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1046 retval = jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
1047 if (retval != ERROR_OK)
1048 return retval;
1049 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
1050 return ERROR_JTAG_INIT_FAILED;
1051
1052 /* point at the 1st tap */
1053 struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
1054
1055 if (!tap)
1056 autoprobe = true;
1057
1058 for (bit_count = 0;
1059 tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
1060 tap = jtag_tap_next_enabled(tap))
1061 {
1062 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1063
1064 if ((idcode & 1) == 0)
1065 {
1066 /* Zero for LSB indicates a device in bypass */
1067 LOG_INFO("TAP %s does not have IDCODE",
1068 tap->dotted_name);
1069 idcode = 0;
1070 tap->hasidcode = false;
1071
1072 bit_count += 1;
1073 }
1074 else
1075 {
1076 /* Friendly devices support IDCODE */
1077 tap->hasidcode = true;
1078 jtag_examine_chain_display(LOG_LVL_INFO,
1079 "tap/device found",
1080 tap->dotted_name, idcode);
1081
1082 bit_count += 32;
1083 }
1084 tap->idcode = idcode;
1085
1086 /* ensure the TAP ID matches what was expected */
1087 if (!jtag_examine_chain_match_tap(tap))
1088 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1089 }
1090
1091 /* Fail if too many TAPs were enabled for us to verify them all. */
1092 if (tap) {
1093 LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
1094 tap->dotted_name);
1095 return ERROR_JTAG_INIT_FAILED;
1096 }
1097
1098 /* if autoprobing, the tap list is still empty ... populate it! */
1099 while (autoprobe && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31) {
1100 uint32_t idcode;
1101 char buf[12];
1102
1103 /* Is there another TAP? */
1104 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1105 if (jtag_idcode_is_final(idcode))
1106 break;
1107
1108 /* Default everything in this TAP except IR length.
1109 *
1110 * REVISIT create a jtag_alloc(chip, tap) routine, and
1111 * share it with jim_newtap_cmd().
1112 */
1113 tap = calloc(1, sizeof *tap);
1114 if (!tap)
1115 return ERROR_FAIL;
1116
1117 sprintf(buf, "auto%d", tapcount++);
1118 tap->chip = strdup(buf);
1119 tap->tapname = strdup("tap");
1120
1121 sprintf(buf, "%s.%s", tap->chip, tap->tapname);
1122 tap->dotted_name = strdup(buf);
1123
1124 /* tap->ir_length == 0 ... signifying irlen autoprobe */
1125 tap->ir_capture_mask = 0x03;
1126 tap->ir_capture_value = 0x01;
1127
1128 tap->enabled = true;
1129
1130 if ((idcode & 1) == 0) {
1131 bit_count += 1;
1132 tap->hasidcode = false;
1133 } else {
1134 bit_count += 32;
1135 tap->hasidcode = true;
1136 tap->idcode = idcode;
1137
1138 tap->expected_ids_cnt = 1;
1139 tap->expected_ids = malloc(sizeof(uint32_t));
1140 tap->expected_ids[0] = idcode;
1141 }
1142
1143 LOG_WARNING("AUTO %s - use \"jtag newtap "
1144 "%s %s -expected-id 0x%8.8" PRIx32 " ...\"",
1145 tap->dotted_name, tap->chip, tap->tapname,
1146 tap->idcode);
1147
1148 jtag_tap_init(tap);
1149 }
1150
1151 /* After those IDCODE or BYPASS register values should be
1152 * only the data we fed into the scan chain.
1153 */
1154 if (jtag_examine_chain_end(idcode_buffer, bit_count,
1155 8 * sizeof(idcode_buffer))) {
1156 LOG_ERROR("double-check your JTAG setup (interface, "
1157 "speed, missing TAPs, ...)");
1158 return ERROR_JTAG_INIT_FAILED;
1159 }
1160
1161 /* Return success or, for backwards compatibility if only
1162 * some IDCODE values mismatched, a soft/continuable fault.
1163 */
1164 return retval;
1165 }
1166
1167 /*
1168 * Validate the date loaded by entry to the Capture-IR state, to help
1169 * find errors related to scan chain configuration (wrong IR lengths)
1170 * or communication.
1171 *
1172 * Entry state can be anything. On non-error exit, all TAPs are in
1173 * bypass mode. On error exits, the scan chain is reset.
1174 */
1175 static int jtag_validate_ircapture(void)
1176 {
1177 struct jtag_tap *tap;
1178 int total_ir_length = 0;
1179 uint8_t *ir_test = NULL;
1180 struct scan_field field;
1181 int val;
1182 int chain_pos = 0;
1183 int retval;
1184
1185 /* when autoprobing, accomodate huge IR lengths */
1186 for (tap = NULL, total_ir_length = 0;
1187 (tap = jtag_tap_next_enabled(tap)) != NULL;
1188 total_ir_length += tap->ir_length) {
1189 if (tap->ir_length == 0)
1190 total_ir_length += JTAG_IRLEN_MAX;
1191 }
1192
1193 /* increase length to add 2 bit sentinel after scan */
1194 total_ir_length += 2;
1195
1196 ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1197 if (ir_test == NULL)
1198 return ERROR_FAIL;
1199
1200 /* after this scan, all TAPs will capture BYPASS instructions */
1201 buf_set_ones(ir_test, total_ir_length);
1202
1203 field.tap = NULL;
1204 field.num_bits = total_ir_length;
1205 field.out_value = ir_test;
1206 field.in_value = ir_test;
1207
1208 jtag_add_plain_ir_scan(1, &field, TAP_IDLE);
1209
1210 LOG_DEBUG("IR capture validation scan");
1211 retval = jtag_execute_queue();
1212 if (retval != ERROR_OK)
1213 goto done;
1214
1215 tap = NULL;
1216 chain_pos = 0;
1217
1218 for (;;) {
1219 tap = jtag_tap_next_enabled(tap);
1220 if (tap == NULL) {
1221 break;
1222 }
1223
1224 /* If we're autoprobing, guess IR lengths. They must be at
1225 * least two bits. Guessing will fail if (a) any TAP does
1226 * not conform to the JTAG spec; or (b) when the upper bits
1227 * captured from some conforming TAP are nonzero. Or if
1228 * (c) an IR length is longer than 32 bits -- which is only
1229 * an implementation limit, which could someday be raised.
1230 *
1231 * REVISIT optimization: if there's a *single* TAP we can
1232 * lift restrictions (a) and (b) by scanning a recognizable
1233 * pattern before the all-ones BYPASS. Check for where the
1234 * pattern starts in the result, instead of an 0...01 value.
1235 *
1236 * REVISIT alternative approach: escape to some tcl code
1237 * which could provide more knowledge, based on IDCODE; and
1238 * only guess when that has no success.
1239 */
1240 if (tap->ir_length == 0) {
1241 tap->ir_length = 2;
1242 while ((val = buf_get_u32(ir_test, chain_pos,
1243 tap->ir_length + 1)) == 1
1244 && tap->ir_length <= 32) {
1245 tap->ir_length++;
1246 }
1247 LOG_WARNING("AUTO %s - use \"... -irlen %d\"",
1248 jtag_tap_name(tap), tap->ir_length);
1249 }
1250
1251 /* Validate the two LSBs, which must be 01 per JTAG spec.
1252 *
1253 * Or ... more bits could be provided by TAP declaration.
1254 * Plus, some taps (notably in i.MX series chips) violate
1255 * this part of the JTAG spec, so their capture mask/value
1256 * attributes might disable this test.
1257 */
1258 val = buf_get_u32(ir_test, chain_pos, tap->ir_length);
1259 if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1260 LOG_ERROR("%s: IR capture error; saw 0x%0*x not 0x%0*x",
1261 jtag_tap_name(tap),
1262 (tap->ir_length + 7) / tap->ir_length,
1263 val,
1264 (tap->ir_length + 7) / tap->ir_length,
1265 (unsigned) tap->ir_capture_value);
1266
1267 retval = ERROR_JTAG_INIT_FAILED;
1268 goto done;
1269 }
1270 LOG_DEBUG("%s: IR capture 0x%0*x", jtag_tap_name(tap),
1271 (tap->ir_length + 7) / tap->ir_length, val);
1272 chain_pos += tap->ir_length;
1273 }
1274
1275 /* verify the '11' sentinel we wrote is returned at the end */
1276 val = buf_get_u32(ir_test, chain_pos, 2);
1277 if (val != 0x3)
1278 {
1279 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1280
1281 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1282 chain_pos, cbuf);
1283 free(cbuf);
1284 retval = ERROR_JTAG_INIT_FAILED;
1285 }
1286
1287 done:
1288 free(ir_test);
1289 if (retval != ERROR_OK) {
1290 jtag_add_tlr();
1291 jtag_execute_queue();
1292 }
1293 return retval;
1294 }
1295
1296
1297 void jtag_tap_init(struct jtag_tap *tap)
1298 {
1299 unsigned ir_len_bits;
1300 unsigned ir_len_bytes;
1301
1302 /* if we're autoprobing, cope with potentially huge ir_length */
1303 ir_len_bits = tap->ir_length ? : JTAG_IRLEN_MAX;
1304 ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
1305
1306 tap->expected = calloc(1, ir_len_bytes);
1307 tap->expected_mask = calloc(1, ir_len_bytes);
1308 tap->cur_instr = malloc(ir_len_bytes);
1309
1310 /// @todo cope better with ir_length bigger than 32 bits
1311 if (ir_len_bits > 32)
1312 ir_len_bits = 32;
1313
1314 buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
1315 buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
1316
1317 // TAP will be in bypass mode after jtag_validate_ircapture()
1318 tap->bypass = 1;
1319 buf_set_ones(tap->cur_instr, tap->ir_length);
1320
1321 // register the reset callback for the TAP
1322 jtag_register_event_callback(&jtag_reset_callback, tap);
1323
1324 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1325 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1326 tap->abs_chain_position, tap->ir_length,
1327 (unsigned) tap->ir_capture_value,
1328 (unsigned) tap->ir_capture_mask);
1329 jtag_tap_add(tap);
1330 }
1331
1332 void jtag_tap_free(struct jtag_tap *tap)
1333 {
1334 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1335
1336 /// @todo is anything missing? no memory leaks please
1337 free((void *)tap->expected);
1338 free((void *)tap->expected_ids);
1339 free((void *)tap->chip);
1340 free((void *)tap->tapname);
1341 free((void *)tap->dotted_name);
1342 free(tap);
1343 }
1344
1345 int jtag_interface_init(struct command_context *cmd_ctx)
1346 {
1347 if (jtag)
1348 return ERROR_OK;
1349
1350 if (!jtag_interface)
1351 {
1352 /* nothing was previously specified by "interface" command */
1353 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1354 return ERROR_JTAG_INVALID_INTERFACE;
1355 }
1356
1357 jtag = jtag_interface;
1358 if (jtag_interface->init() != ERROR_OK)
1359 {
1360 jtag = NULL;
1361 return ERROR_JTAG_INIT_FAILED;
1362 }
1363
1364 int requested_khz = jtag_get_speed_khz();
1365 int actual_khz = requested_khz;
1366 int retval = jtag_get_speed_readable(&actual_khz);
1367 if (ERROR_OK != retval)
1368 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1369 else if (actual_khz)
1370 {
1371 if ((CLOCK_MODE_RCLK == clock_mode)
1372 || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
1373 {
1374 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1375 , actual_khz);
1376 }
1377 else
1378 LOG_INFO("clock speed %d kHz", actual_khz);
1379 }
1380 else
1381 LOG_INFO("RCLK (adaptive clock speed)");
1382
1383 return ERROR_OK;
1384 }
1385
1386 int jtag_init_inner(struct command_context *cmd_ctx)
1387 {
1388 struct jtag_tap *tap;
1389 int retval;
1390 bool issue_setup = true;
1391
1392 LOG_DEBUG("Init JTAG chain");
1393
1394 tap = jtag_tap_next_enabled(NULL);
1395 if (tap == NULL) {
1396 /* Once JTAG itself is properly set up, and the scan chain
1397 * isn't absurdly large, IDCODE autoprobe should work fine.
1398 *
1399 * But ... IRLEN autoprobe can fail even on systems which
1400 * are fully conformant to JTAG. Also, JTAG setup can be
1401 * quite finicky on some systems.
1402 *
1403 * REVISIT: if TAP autoprobe works OK, then in many cases
1404 * we could escape to tcl code and set up targets based on
1405 * the TAP's IDCODE values.
1406 */
1407 LOG_WARNING("There are no enabled taps. "
1408 "AUTO PROBING MIGHT NOT WORK!!");
1409
1410 /* REVISIT default clock will often be too fast ... */
1411 }
1412
1413 jtag_add_tlr();
1414 if ((retval = jtag_execute_queue()) != ERROR_OK)
1415 return retval;
1416
1417 /* Examine DR values first. This discovers problems which will
1418 * prevent communication ... hardware issues like TDO stuck, or
1419 * configuring the wrong number of (enabled) TAPs.
1420 */
1421 retval = jtag_examine_chain();
1422 switch (retval) {
1423 case ERROR_OK:
1424 /* complete success */
1425 break;
1426 case ERROR_JTAG_INIT_SOFT_FAIL:
1427 /* For backward compatibility reasons, try coping with
1428 * configuration errors involving only ID mismatches.
1429 * We might be able to talk to the devices.
1430 */
1431 LOG_ERROR("Trying to use configured scan chain anyway...");
1432 issue_setup = false;
1433 break;
1434 default:
1435 /* some hard error; already issued diagnostics */
1436 return retval;
1437 }
1438
1439 /* Now look at IR values. Problems here will prevent real
1440 * communication. They mostly mean that the IR length is
1441 * wrong ... or that the IR capture value is wrong. (The
1442 * latter is uncommon, but easily worked around: provide
1443 * ircapture/irmask values during TAP setup.)
1444 */
1445 retval = jtag_validate_ircapture();
1446 if (retval != ERROR_OK)
1447 return retval;
1448
1449 if (issue_setup)
1450 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1451 else
1452 LOG_WARNING("Bypassing JTAG setup events due to errors");
1453
1454
1455 return ERROR_OK;
1456 }
1457
1458 int jtag_interface_quit(void)
1459 {
1460 if (!jtag || !jtag->quit)
1461 return ERROR_OK;
1462
1463 // close the JTAG interface
1464 int result = jtag->quit();
1465 if (ERROR_OK != result)
1466 LOG_ERROR("failed: %d", result);
1467
1468 return ERROR_OK;
1469 }
1470
1471
1472 int jtag_init_reset(struct command_context *cmd_ctx)
1473 {
1474 int retval;
1475
1476 if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1477 return retval;
1478
1479 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1480
1481 /*
1482 * This procedure is used by default when OpenOCD triggers a reset.
1483 * It's now done through an overridable Tcl "init_reset" wrapper.
1484 *
1485 * This started out as a more powerful "get JTAG working" reset than
1486 * jtag_init_inner(), applying TRST because some chips won't activate
1487 * JTAG without a TRST cycle (presumed to be async, though some of
1488 * those chips synchronize JTAG activation using TCK).
1489 *
1490 * But some chips only activate JTAG as part of an SRST cycle; SRST
1491 * got mixed in. So it became a hard reset routine, which got used
1492 * in more places, and which coped with JTAG reset being forced as
1493 * part of SRST (srst_pulls_trst).
1494 *
1495 * And even more corner cases started to surface: TRST and/or SRST
1496 * assertion timings matter; some chips need other JTAG operations;
1497 * TRST/SRST sequences can need to be different from these, etc.
1498 *
1499 * Systems should override that wrapper to support system-specific
1500 * requirements that this not-fully-generic code doesn't handle.
1501 *
1502 * REVISIT once Tcl code can read the reset_config modes, this won't
1503 * need to be a C routine at all...
1504 */
1505 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1506 if (jtag_reset_config & RESET_HAS_SRST)
1507 {
1508 jtag_add_reset(1, 1);
1509 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1510 jtag_add_reset(0, 1);
1511 }
1512 jtag_add_reset(0, 0);
1513 if ((retval = jtag_execute_queue()) != ERROR_OK)
1514 return retval;
1515
1516 /* Check that we can communication on the JTAG chain + eventually we want to
1517 * be able to perform enumeration only after OpenOCD has started
1518 * telnet and GDB server
1519 *
1520 * That would allow users to more easily perform any magic they need to before
1521 * reset happens.
1522 */
1523 return jtag_init_inner(cmd_ctx);
1524 }
1525
1526 int jtag_init(struct command_context *cmd_ctx)
1527 {
1528 int retval;
1529
1530 if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1531 return retval;
1532
1533 /* guard against oddball hardware: force resets to be inactive */
1534 jtag_add_reset(0, 0);
1535 if ((retval = jtag_execute_queue()) != ERROR_OK)
1536 return retval;
1537
1538 if (Jim_Eval_Named(interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1539 return ERROR_FAIL;
1540
1541 return ERROR_OK;
1542 }
1543
1544 unsigned jtag_get_speed_khz(void)
1545 {
1546 return speed_khz;
1547 }
1548
1549 static int jtag_khz_to_speed(unsigned khz, int* speed)
1550 {
1551 LOG_DEBUG("convert khz to interface specific speed value");
1552 speed_khz = khz;
1553 if (jtag != NULL)
1554 {
1555 LOG_DEBUG("have interface set up");
1556 int speed_div1;
1557 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1558 if (ERROR_OK != retval)
1559 {
1560 return retval;
1561 }
1562 *speed = speed_div1;
1563 }
1564 return ERROR_OK;
1565 }
1566
1567 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
1568 {
1569 int retval = jtag_khz_to_speed(0, speed);
1570 if ((ERROR_OK != retval) && fallback_speed_khz)
1571 {
1572 LOG_DEBUG("trying fallback speed...");
1573 retval = jtag_khz_to_speed(fallback_speed_khz, speed);
1574 }
1575 return retval;
1576 }
1577
1578 static int jtag_set_speed(int speed)
1579 {
1580 jtag_speed = speed;
1581 /* this command can be called during CONFIG,
1582 * in which case jtag isn't initialized */
1583 return jtag ? jtag->speed(speed) : ERROR_OK;
1584 }
1585
1586 int jtag_config_khz(unsigned khz)
1587 {
1588 LOG_DEBUG("handle jtag khz");
1589 clock_mode = CLOCK_MODE_KHZ;
1590 int speed = 0;
1591 int retval = jtag_khz_to_speed(khz, &speed);
1592 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1593 }
1594
1595 int jtag_config_rclk(unsigned fallback_speed_khz)
1596 {
1597 LOG_DEBUG("handle jtag rclk");
1598 clock_mode = CLOCK_MODE_RCLK;
1599 rclk_fallback_speed_khz = fallback_speed_khz;
1600 int speed = 0;
1601 int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1602 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1603 }
1604
1605 int jtag_get_speed(void)
1606 {
1607 int speed;
1608 switch(clock_mode)
1609 {
1610 case CLOCK_MODE_SPEED:
1611 speed = jtag_speed;
1612 break;
1613 case CLOCK_MODE_KHZ:
1614 jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
1615 break;
1616 case CLOCK_MODE_RCLK:
1617 jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
1618 break;
1619 default:
1620 LOG_ERROR("BUG: unknown jtag clock mode");
1621 speed = 0;
1622 break;
1623 }
1624 return speed;
1625 }
1626
1627 int jtag_get_speed_readable(int *khz)
1628 {
1629 return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
1630 }
1631
1632 void jtag_set_verify(bool enable)
1633 {
1634 jtag_verify = enable;
1635 }
1636
1637 bool jtag_will_verify()
1638 {
1639 return jtag_verify;
1640 }
1641
1642 void jtag_set_verify_capture_ir(bool enable)
1643 {
1644 jtag_verify_capture_ir = enable;
1645 }
1646
1647 bool jtag_will_verify_capture_ir()
1648 {
1649 return jtag_verify_capture_ir;
1650 }
1651
1652 int jtag_power_dropout(int *dropout)
1653 {
1654 return jtag->power_dropout(dropout);
1655 }
1656
1657 int jtag_srst_asserted(int *srst_asserted)
1658 {
1659 return jtag->srst_asserted(srst_asserted);
1660 }
1661
1662 enum reset_types jtag_get_reset_config(void)
1663 {
1664 return jtag_reset_config;
1665 }
1666 void jtag_set_reset_config(enum reset_types type)
1667 {
1668 jtag_reset_config = type;
1669 }
1670
1671 int jtag_get_trst(void)
1672 {
1673 return jtag_trst;
1674 }
1675 int jtag_get_srst(void)
1676 {
1677 return jtag_srst;
1678 }
1679
1680 void jtag_set_nsrst_delay(unsigned delay)
1681 {
1682 jtag_nsrst_delay = delay;
1683 }
1684 unsigned jtag_get_nsrst_delay(void)
1685 {
1686 return jtag_nsrst_delay;
1687 }
1688 void jtag_set_ntrst_delay(unsigned delay)
1689 {
1690 jtag_ntrst_delay = delay;
1691 }
1692 unsigned jtag_get_ntrst_delay(void)
1693 {
1694 return jtag_ntrst_delay;
1695 }
1696
1697
1698 void jtag_set_nsrst_assert_width(unsigned delay)
1699 {
1700 jtag_nsrst_assert_width = delay;
1701 }
1702 unsigned jtag_get_nsrst_assert_width(void)
1703 {
1704 return jtag_nsrst_assert_width;
1705 }
1706 void jtag_set_ntrst_assert_width(unsigned delay)
1707 {
1708 jtag_ntrst_assert_width = delay;
1709 }
1710 unsigned jtag_get_ntrst_assert_width(void)
1711 {
1712 return jtag_ntrst_assert_width;
1713 }

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)