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

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)