1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2009 Zachary T Welch *
5 * zw@superlucidity.net *
7 * Copyright (C) 2007,2008,2009 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2009 SoftPLC Corporation *
11 * http://softplc.com *
14 * Copyright (C) 2005 by Dominic Rath *
15 * Dominic.Rath@gmx.de *
16 ***************************************************************************/
25 #include "interface.h"
26 #include <transport/transport.h>
27 #include <helper/jep106.h>
28 #include "helper/system.h"
34 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
36 #include "xsvf/xsvf.h"
38 /* ipdbg are utilities to debug IP-cores. It uses JTAG for transport. */
39 #include "server/ipdbg.h"
41 /** The number of JTAG queue flushes (for profiling and debugging purposes). */
42 static int jtag_flush_queue_count;
44 /* Sleep this # of ms after flushing the queue */
45 static int jtag_flush_queue_sleep;
47 static void jtag_add_scan_check(struct jtag_tap *active,
48 void (*jtag_add_scan)(struct jtag_tap *active,
50 const struct scan_field *in_fields,
52 int in_num_fields, struct scan_field *in_fields, tap_state_t state);
55 * The jtag_error variable is set when an error occurs while executing
56 * the queue. Application code may set this using jtag_set_error(),
57 * when an error occurs during processing that should be reported during
58 * jtag_execute_queue().
60 * The value is set and cleared, but never read by normal application code.
62 * This value is returned (and cleared) by jtag_execute_queue().
64 static int jtag_error = ERROR_OK;
66 static const char *jtag_event_strings[] = {
67 [JTAG_TRST_ASSERTED] = "TAP reset",
68 [JTAG_TAP_EVENT_SETUP] = "TAP setup",
69 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
70 [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
74 * JTAG adapters must initialize with TRST and SRST de-asserted
75 * (they're negative logic, so that means *high*). But some
76 * hardware doesn't necessarily work that way ... so set things
77 * up so that jtag_init() always forces that state.
79 static int jtag_trst = -1;
80 static int jtag_srst = -1;
83 * List all TAPs that have been created.
85 static struct jtag_tap *__jtag_all_taps;
87 static enum reset_types jtag_reset_config = RESET_NONE;
88 tap_state_t cmd_queue_cur_state = TAP_RESET;
90 static bool jtag_verify_capture_ir = true;
91 static int jtag_verify = 1;
93 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
94 *deasserted (in ms) */
95 static int adapter_nsrst_delay; /* default to no nSRST delay */
96 static int jtag_ntrst_delay;/* default to no nTRST delay */
97 static int adapter_nsrst_assert_width; /* width of assertion */
98 static int jtag_ntrst_assert_width; /* width of assertion */
101 * Contains a single callback along with a pointer that will be passed
102 * when an event occurs.
104 struct jtag_event_callback {
105 /** a event callback */
106 jtag_event_handler_t callback;
107 /** the private data to pass to the callback */
109 /** the next callback */
110 struct jtag_event_callback *next;
113 /* callbacks to inform high-level handlers about JTAG state changes */
114 static struct jtag_event_callback *jtag_event_callbacks;
116 extern struct adapter_driver *adapter_driver;
118 void jtag_set_flush_queue_sleep(int ms)
120 jtag_flush_queue_sleep = ms;
123 void jtag_set_error(int error)
125 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
130 int jtag_error_clear(void)
132 int temp = jtag_error;
133 jtag_error = ERROR_OK;
139 static bool jtag_poll = 1;
141 bool is_jtag_poll_safe(void)
143 /* Polling can be disabled explicitly with set_enabled(false).
144 * It is also implicitly disabled while TRST is active and
145 * while SRST is gating the JTAG clock.
147 if (!transport_is_jtag())
150 if (!jtag_poll || jtag_trst != 0)
152 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
155 bool jtag_poll_get_enabled(void)
160 void jtag_poll_set_enabled(bool value)
167 struct jtag_tap *jtag_all_taps(void)
169 return __jtag_all_taps;
172 unsigned jtag_tap_count(void)
174 struct jtag_tap *t = jtag_all_taps();
183 unsigned jtag_tap_count_enabled(void)
185 struct jtag_tap *t = jtag_all_taps();
195 /** Append a new TAP to the chain of all taps. */
196 static void jtag_tap_add(struct jtag_tap *t)
198 unsigned jtag_num_taps = 0;
200 struct jtag_tap **tap = &__jtag_all_taps;
203 tap = &(*tap)->next_tap;
206 t->abs_chain_position = jtag_num_taps;
209 /* returns a pointer to the n-th device in the scan chain */
210 struct jtag_tap *jtag_tap_by_position(unsigned n)
212 struct jtag_tap *t = jtag_all_taps();
220 struct jtag_tap *jtag_tap_by_string(const char *s)
222 /* try by name first */
223 struct jtag_tap *t = jtag_all_taps();
226 if (strcmp(t->dotted_name, s) == 0)
231 /* no tap found by name, so try to parse the name as a number */
233 if (parse_uint(s, &n) != ERROR_OK)
236 /* FIXME remove this numeric fallback code late June 2010, along
237 * with all info in the User's Guide that TAPs have numeric IDs.
238 * Also update "scan_chain" output to not display the numbers.
240 t = jtag_tap_by_position(n);
242 LOG_WARNING("Specify TAP '%s' by name, not number %u",
248 struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p)
250 p = p ? p->next_tap : jtag_all_taps();
259 const char *jtag_tap_name(const struct jtag_tap *tap)
261 return (!tap) ? "(unknown)" : tap->dotted_name;
265 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
267 struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
270 return ERROR_COMMAND_SYNTAX_ERROR;
273 while ((*callbacks_p)->next)
274 callbacks_p = &((*callbacks_p)->next);
275 callbacks_p = &((*callbacks_p)->next);
278 (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
279 (*callbacks_p)->callback = callback;
280 (*callbacks_p)->priv = priv;
281 (*callbacks_p)->next = NULL;
286 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
288 struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
291 return ERROR_COMMAND_SYNTAX_ERROR;
294 if (((*p)->priv != priv) || ((*p)->callback != callback)) {
307 int jtag_call_event_callbacks(enum jtag_event event)
309 struct jtag_event_callback *callback = jtag_event_callbacks;
311 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
314 struct jtag_event_callback *next;
316 /* callback may remove itself */
317 next = callback->next;
318 callback->callback(event, callback->priv);
325 static void jtag_checks(void)
327 assert(jtag_trst == 0);
330 static void jtag_prelude(tap_state_t state)
334 assert(state != TAP_INVALID);
336 cmd_queue_cur_state = state;
339 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
344 int retval = interface_jtag_add_ir_scan(active, in_fields, state);
345 jtag_set_error(retval);
348 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
350 const struct scan_field *in_fields,
353 jtag_add_ir_scan_noverify(active, in_fields, state);
356 /* If fields->in_value is filled out, then the captured IR value will be checked */
357 void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
359 assert(state != TAP_RESET);
361 if (jtag_verify && jtag_verify_capture_ir) {
362 /* 8 x 32 bit id's is enough for all invocations */
364 /* if we are to run a verification of the ir scan, we need to get the input back.
365 * We may have to allocate space if the caller didn't ask for the input back.
367 in_fields->check_value = active->expected;
368 in_fields->check_mask = active->expected_mask;
369 jtag_add_scan_check(active, jtag_add_ir_scan_noverify_callback, 1, in_fields,
372 jtag_add_ir_scan_noverify(active, in_fields, state);
375 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
379 assert(state != TAP_RESET);
383 int retval = interface_jtag_add_plain_ir_scan(
384 num_bits, out_bits, in_bits, state);
385 jtag_set_error(retval);
388 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
389 uint8_t *in_check_mask, int num_bits);
391 static int jtag_check_value_mask_callback(jtag_callback_data_t data0,
392 jtag_callback_data_t data1,
393 jtag_callback_data_t data2,
394 jtag_callback_data_t data3)
396 return jtag_check_value_inner((uint8_t *)data0,
402 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
403 struct jtag_tap *active,
405 const struct scan_field *in_fields,
407 int in_num_fields, struct scan_field *in_fields, tap_state_t state)
409 jtag_add_scan(active, in_num_fields, in_fields, state);
411 for (int i = 0; i < in_num_fields; i++) {
412 if ((in_fields[i].check_value) && (in_fields[i].in_value)) {
413 jtag_add_callback4(jtag_check_value_mask_callback,
414 (jtag_callback_data_t)in_fields[i].in_value,
415 (jtag_callback_data_t)in_fields[i].check_value,
416 (jtag_callback_data_t)in_fields[i].check_mask,
417 (jtag_callback_data_t)in_fields[i].num_bits);
422 void jtag_add_dr_scan_check(struct jtag_tap *active,
424 struct scan_field *in_fields,
428 jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
430 jtag_add_dr_scan(active, in_num_fields, in_fields, state);
434 void jtag_add_dr_scan(struct jtag_tap *active,
436 const struct scan_field *in_fields,
439 assert(state != TAP_RESET);
444 retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
445 jtag_set_error(retval);
448 void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
452 assert(state != TAP_RESET);
457 retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
458 jtag_set_error(retval);
461 void jtag_add_tlr(void)
463 jtag_prelude(TAP_RESET);
464 jtag_set_error(interface_jtag_add_tlr());
466 /* NOTE: order here matches TRST path in jtag_add_reset() */
467 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
468 jtag_notify_event(JTAG_TRST_ASSERTED);
472 * If supported by the underlying adapter, this clocks a raw bit sequence
473 * onto TMS for switching between JTAG and SWD modes.
475 * DO NOT use this to bypass the integrity checks and logging provided
476 * by the jtag_add_pathmove() and jtag_add_statemove() calls.
478 * @param nbits How many bits to clock out.
479 * @param seq The bit sequence. The LSB is bit 0 of seq[0].
480 * @param state The JTAG tap state to record on completion. Use
481 * TAP_INVALID to represent being in in SWD mode.
483 * @todo Update naming conventions to stop assuming everything is JTAG.
485 int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
489 if (!(adapter_driver->jtag_ops->supported & DEBUG_CAP_TMS_SEQ))
490 return ERROR_JTAG_NOT_IMPLEMENTED;
493 cmd_queue_cur_state = state;
495 retval = interface_add_tms_seq(nbits, seq, state);
496 jtag_set_error(retval);
500 void jtag_add_pathmove(int num_states, const tap_state_t *path)
502 tap_state_t cur_state = cmd_queue_cur_state;
504 /* the last state has to be a stable state */
505 if (!tap_is_state_stable(path[num_states - 1])) {
506 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
507 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
511 for (int i = 0; i < num_states; i++) {
512 if (path[i] == TAP_RESET) {
513 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
514 jtag_set_error(ERROR_JTAG_STATE_INVALID);
518 if (tap_state_transition(cur_state, true) != path[i] &&
519 tap_state_transition(cur_state, false) != path[i]) {
520 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
521 tap_state_name(cur_state), tap_state_name(path[i]));
522 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
530 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
531 cmd_queue_cur_state = path[num_states - 1];
534 int jtag_add_statemove(tap_state_t goal_state)
536 tap_state_t cur_state = cmd_queue_cur_state;
538 if (goal_state != cur_state) {
539 LOG_DEBUG("cur_state=%s goal_state=%s",
540 tap_state_name(cur_state),
541 tap_state_name(goal_state));
544 /* If goal is RESET, be paranoid and force that that transition
545 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
547 if (goal_state == TAP_RESET)
549 else if (goal_state == cur_state)
552 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state)) {
553 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
554 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
555 tap_state_t moves[8];
556 assert(tms_count < ARRAY_SIZE(moves));
558 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
559 bool bit = tms_bits & 1;
561 cur_state = tap_state_transition(cur_state, bit);
562 moves[i] = cur_state;
565 jtag_add_pathmove(tms_count, moves);
566 } else if (tap_state_transition(cur_state, true) == goal_state
567 || tap_state_transition(cur_state, false) == goal_state)
568 jtag_add_pathmove(1, &goal_state);
575 void jtag_add_runtest(int num_cycles, tap_state_t state)
578 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
582 void jtag_add_clocks(int num_cycles)
584 if (!tap_is_state_stable(cmd_queue_cur_state)) {
585 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
586 tap_state_name(cmd_queue_cur_state));
587 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
591 if (num_cycles > 0) {
593 jtag_set_error(interface_jtag_add_clocks(num_cycles));
597 static int adapter_system_reset(int req_srst)
602 if (!(jtag_reset_config & RESET_HAS_SRST)) {
603 LOG_ERROR("BUG: can't assert SRST");
609 /* Maybe change SRST signal state */
610 if (jtag_srst != req_srst) {
611 retval = adapter_driver->reset(0, req_srst);
612 if (retval != ERROR_OK) {
613 LOG_ERROR("SRST error");
616 jtag_srst = req_srst;
619 LOG_DEBUG("SRST line asserted");
620 if (adapter_nsrst_assert_width)
621 jtag_sleep(adapter_nsrst_assert_width * 1000);
623 LOG_DEBUG("SRST line released");
624 if (adapter_nsrst_delay)
625 jtag_sleep(adapter_nsrst_delay * 1000);
632 static void legacy_jtag_add_reset(int req_tlr_or_trst, int req_srst)
634 int trst_with_tlr = 0;
638 /* Without SRST, we must use target-specific JTAG operations
639 * on each target; callers should not be requesting SRST when
640 * that signal doesn't exist.
642 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
643 * can kick in even if the JTAG adapter can't drive TRST.
646 if (!(jtag_reset_config & RESET_HAS_SRST)) {
647 LOG_ERROR("BUG: can't assert SRST");
648 jtag_set_error(ERROR_FAIL);
651 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
652 && !req_tlr_or_trst) {
653 LOG_ERROR("BUG: can't assert only SRST");
654 jtag_set_error(ERROR_FAIL);
660 /* JTAG reset (entry to TAP_RESET state) can always be achieved
661 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
662 * state first. TRST accelerates it, and bypasses those states.
664 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
665 * can kick in even if the JTAG adapter can't drive SRST.
667 if (req_tlr_or_trst) {
668 if (!(jtag_reset_config & RESET_HAS_TRST))
670 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
677 /* Maybe change TRST and/or SRST signal state */
678 if (jtag_srst != new_srst || jtag_trst != new_trst) {
681 retval = interface_jtag_add_reset(new_trst, new_srst);
682 if (retval != ERROR_OK)
683 jtag_set_error(retval);
685 retval = jtag_execute_queue();
687 if (retval != ERROR_OK) {
688 LOG_ERROR("TRST/SRST error");
693 /* SRST resets everything hooked up to that signal */
694 if (jtag_srst != new_srst) {
695 jtag_srst = new_srst;
697 LOG_DEBUG("SRST line asserted");
698 if (adapter_nsrst_assert_width)
699 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
701 LOG_DEBUG("SRST line released");
702 if (adapter_nsrst_delay)
703 jtag_add_sleep(adapter_nsrst_delay * 1000);
707 /* Maybe enter the JTAG TAP_RESET state ...
708 * - using only TMS, TCK, and the JTAG state machine
709 * - or else more directly, using TRST
711 * TAP_RESET should be invisible to non-debug parts of the system.
714 LOG_DEBUG("JTAG reset with TLR instead of TRST");
717 } else if (jtag_trst != new_trst) {
718 jtag_trst = new_trst;
720 LOG_DEBUG("TRST line asserted");
721 tap_set_state(TAP_RESET);
722 if (jtag_ntrst_assert_width)
723 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
725 LOG_DEBUG("TRST line released");
726 if (jtag_ntrst_delay)
727 jtag_add_sleep(jtag_ntrst_delay * 1000);
729 /* We just asserted nTRST, so we're now in TAP_RESET.
730 * Inform possible listeners about this, now that
731 * JTAG instructions and data can be shifted. This
732 * sequence must match jtag_add_tlr().
734 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
735 jtag_notify_event(JTAG_TRST_ASSERTED);
740 /* FIXME: name is misleading; we do not plan to "add" reset into jtag queue */
741 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
744 int trst_with_tlr = 0;
748 if (!adapter_driver->reset) {
749 legacy_jtag_add_reset(req_tlr_or_trst, req_srst);
753 /* Without SRST, we must use target-specific JTAG operations
754 * on each target; callers should not be requesting SRST when
755 * that signal doesn't exist.
757 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
758 * can kick in even if the JTAG adapter can't drive TRST.
761 if (!(jtag_reset_config & RESET_HAS_SRST)) {
762 LOG_ERROR("BUG: can't assert SRST");
763 jtag_set_error(ERROR_FAIL);
766 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
767 && !req_tlr_or_trst) {
768 LOG_ERROR("BUG: can't assert only SRST");
769 jtag_set_error(ERROR_FAIL);
775 /* JTAG reset (entry to TAP_RESET state) can always be achieved
776 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
777 * state first. TRST accelerates it, and bypasses those states.
779 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
780 * can kick in even if the JTAG adapter can't drive SRST.
782 if (req_tlr_or_trst) {
783 if (!(jtag_reset_config & RESET_HAS_TRST))
785 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
792 /* Maybe change TRST and/or SRST signal state */
793 if (jtag_srst != new_srst || jtag_trst != new_trst) {
794 /* guarantee jtag queue empty before changing reset status */
795 jtag_execute_queue();
797 retval = adapter_driver->reset(new_trst, new_srst);
798 if (retval != ERROR_OK) {
799 jtag_set_error(retval);
800 LOG_ERROR("TRST/SRST error");
805 /* SRST resets everything hooked up to that signal */
806 if (jtag_srst != new_srst) {
807 jtag_srst = new_srst;
809 LOG_DEBUG("SRST line asserted");
810 if (adapter_nsrst_assert_width)
811 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
813 LOG_DEBUG("SRST line released");
814 if (adapter_nsrst_delay)
815 jtag_add_sleep(adapter_nsrst_delay * 1000);
819 /* Maybe enter the JTAG TAP_RESET state ...
820 * - using only TMS, TCK, and the JTAG state machine
821 * - or else more directly, using TRST
823 * TAP_RESET should be invisible to non-debug parts of the system.
826 LOG_DEBUG("JTAG reset with TLR instead of TRST");
828 jtag_execute_queue();
830 } else if (jtag_trst != new_trst) {
831 jtag_trst = new_trst;
833 LOG_DEBUG("TRST line asserted");
834 tap_set_state(TAP_RESET);
835 if (jtag_ntrst_assert_width)
836 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
838 LOG_DEBUG("TRST line released");
839 if (jtag_ntrst_delay)
840 jtag_add_sleep(jtag_ntrst_delay * 1000);
842 /* We just asserted nTRST, so we're now in TAP_RESET.
843 * Inform possible listeners about this, now that
844 * JTAG instructions and data can be shifted. This
845 * sequence must match jtag_add_tlr().
847 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
848 jtag_notify_event(JTAG_TRST_ASSERTED);
853 void jtag_add_sleep(uint32_t us)
855 /** @todo Here, keep_alive() appears to be a layering violation!!! */
857 jtag_set_error(interface_jtag_add_sleep(us));
860 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
861 uint8_t *in_check_mask, int num_bits)
863 int retval = ERROR_OK;
867 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
869 compare_failed = buf_cmp(captured, in_check_value, num_bits);
871 if (compare_failed) {
872 char *captured_str, *in_check_value_str;
873 int bits = (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits;
875 /* NOTE: we've lost diagnostic context here -- 'which tap' */
877 captured_str = buf_to_hex_str(captured, bits);
878 in_check_value_str = buf_to_hex_str(in_check_value, bits);
880 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
882 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
885 free(in_check_value_str);
888 char *in_check_mask_str;
890 in_check_mask_str = buf_to_hex_str(in_check_mask, bits);
891 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
892 free(in_check_mask_str);
895 retval = ERROR_JTAG_QUEUE_FAILED;
900 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
902 assert(field->in_value);
905 /* no checking to do */
909 jtag_execute_queue_noclear();
911 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
912 jtag_set_error(retval);
915 int default_interface_jtag_execute_queue(void)
917 if (!is_adapter_initialized()) {
918 LOG_ERROR("No JTAG interface configured yet. "
919 "Issue 'init' command in startup scripts "
920 "before communicating with targets.");
924 if (!transport_is_jtag()) {
926 * FIXME: This should not happen!
927 * There could be old code that queues jtag commands with non jtag interfaces so, for
928 * the moment simply highlight it by log an error and return on empty execute_queue.
929 * We should fix it quitting with assert(0) because it is an internal error.
930 * The fix can be applied immediately after next release (v0.11.0 ?)
932 LOG_ERROR("JTAG API jtag_execute_queue() called on non JTAG interface");
933 if (!adapter_driver->jtag_ops || !adapter_driver->jtag_ops->execute_queue)
937 int result = adapter_driver->jtag_ops->execute_queue();
939 struct jtag_command *cmd = jtag_command_queue;
940 while (debug_level >= LOG_LVL_DEBUG_IO && cmd) {
943 LOG_DEBUG_IO("JTAG %s SCAN to %s",
944 cmd->cmd.scan->ir_scan ? "IR" : "DR",
945 tap_state_name(cmd->cmd.scan->end_state));
946 for (int i = 0; i < cmd->cmd.scan->num_fields; i++) {
947 struct scan_field *field = cmd->cmd.scan->fields + i;
948 if (field->out_value) {
949 char *str = buf_to_hex_str(field->out_value, field->num_bits);
950 LOG_DEBUG_IO(" %db out: %s", field->num_bits, str);
953 if (field->in_value) {
954 char *str = buf_to_hex_str(field->in_value, field->num_bits);
955 LOG_DEBUG_IO(" %db in: %s", field->num_bits, str);
961 LOG_DEBUG_IO("JTAG TLR RESET to %s",
962 tap_state_name(cmd->cmd.statemove->end_state));
965 LOG_DEBUG_IO("JTAG RUNTEST %d cycles to %s",
966 cmd->cmd.runtest->num_cycles,
967 tap_state_name(cmd->cmd.runtest->end_state));
971 const char *reset_str[3] = {
972 "leave", "deassert", "assert"
974 LOG_DEBUG_IO("JTAG RESET %s TRST, %s SRST",
975 reset_str[cmd->cmd.reset->trst + 1],
976 reset_str[cmd->cmd.reset->srst + 1]);
980 LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
983 LOG_DEBUG_IO("JTAG SLEEP (TODO)");
985 case JTAG_STABLECLOCKS:
986 LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
989 LOG_DEBUG_IO("JTAG TMS (TODO)");
992 LOG_ERROR("Unknown JTAG command: %d", cmd->type);
1001 void jtag_execute_queue_noclear(void)
1003 jtag_flush_queue_count++;
1004 jtag_set_error(interface_jtag_execute_queue());
1006 if (jtag_flush_queue_sleep > 0) {
1007 /* For debug purposes it can be useful to test performance
1008 * or behavior when delaying after flushing the queue,
1009 * e.g. to simulate long roundtrip times.
1011 usleep(jtag_flush_queue_sleep * 1000);
1015 int jtag_get_flush_queue_count(void)
1017 return jtag_flush_queue_count;
1020 int jtag_execute_queue(void)
1022 jtag_execute_queue_noclear();
1023 return jtag_error_clear();
1026 static int jtag_reset_callback(enum jtag_event event, void *priv)
1028 struct jtag_tap *tap = priv;
1030 if (event == JTAG_TRST_ASSERTED) {
1031 tap->enabled = !tap->disabled_after_reset;
1033 /* current instruction is either BYPASS or IDCODE */
1034 buf_set_ones(tap->cur_instr, tap->ir_length);
1041 /* sleep at least us microseconds. When we sleep more than 1000ms we
1042 * do an alive sleep, i.e. keep GDB alive. Note that we could starve
1043 * GDB if we slept for <1000ms many times.
1045 void jtag_sleep(uint32_t us)
1050 alive_sleep((us+999)/1000);
1053 #define JTAG_MAX_AUTO_TAPS 20
1055 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1056 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1057 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1059 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
1060 * know that no valid TAP will have it as an IDCODE value.
1062 #define END_OF_CHAIN_FLAG 0xffffffff
1064 /* a larger IR length than we ever expect to autoprobe */
1065 #define JTAG_IRLEN_MAX 60
1067 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
1069 struct scan_field field = {
1070 .num_bits = num_idcode * 32,
1071 .out_value = idcode_buffer,
1072 .in_value = idcode_buffer,
1075 /* initialize to the end of chain ID value */
1076 for (unsigned i = 0; i < num_idcode; i++)
1077 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
1079 jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
1081 return jtag_execute_queue();
1084 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
1086 uint8_t zero_check = 0x0;
1087 uint8_t one_check = 0xff;
1089 for (unsigned i = 0; i < count * 4; i++) {
1090 zero_check |= idcodes[i];
1091 one_check &= idcodes[i];
1094 /* if there wasn't a single non-zero bit or if all bits were one,
1095 * the scan is not valid. We wrote a mix of both values; either
1097 * - There's a hardware issue (almost certainly):
1098 * + all-zeroes can mean a target stuck in JTAG reset
1099 * + all-ones tends to mean no target
1100 * - The scan chain is WAY longer than we can handle, *AND* either
1101 * + there are several hundreds of TAPs in bypass, or
1102 * + at least a few dozen TAPs all have an all-ones IDCODE
1104 if (zero_check == 0x00 || one_check == 0xff) {
1105 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
1106 (zero_check == 0x00) ? "zeroes" : "ones");
1107 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
1113 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
1114 const char *name, uint32_t idcode)
1116 log_printf_lf(level, __FILE__, __LINE__, __func__,
1117 "JTAG tap: %s %16.16s: 0x%08x "
1118 "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
1120 (unsigned int)idcode,
1121 (unsigned int)EXTRACT_MFG(idcode),
1122 jep106_manufacturer(EXTRACT_MFG(idcode)),
1123 (unsigned int)EXTRACT_PART(idcode),
1124 (unsigned int)EXTRACT_VER(idcode));
1127 static bool jtag_idcode_is_final(uint32_t idcode)
1130 * Some devices, such as AVR8, will output all 1's instead
1131 * of TDI input value at end of chain. Allow those values
1132 * instead of failing.
1134 return idcode == END_OF_CHAIN_FLAG;
1138 * This helper checks that remaining bits in the examined chain data are
1139 * all as expected, but a single JTAG device requires only 64 bits to be
1140 * read back correctly. This can help identify and diagnose problems
1141 * with the JTAG chain earlier, gives more helpful/explicit error messages.
1142 * Returns TRUE iff garbage was found.
1144 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
1146 bool triggered = false;
1147 for (; count < max - 31; count += 32) {
1148 uint32_t idcode = buf_get_u32(idcodes, count, 32);
1150 /* do not trigger the warning if the data looks good */
1151 if (jtag_idcode_is_final(idcode))
1153 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
1154 count, (unsigned int)idcode);
1160 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
1163 if (tap->expected_ids_cnt == 0 || !tap->hasidcode)
1166 /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1167 uint32_t mask = tap->ignore_version ? ~(0xfU << 28) : ~0U;
1168 uint32_t idcode = tap->idcode & mask;
1170 /* Loop over the expected identification codes and test for a match */
1171 for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1172 uint32_t expected = tap->expected_ids[ii] & mask;
1174 if (idcode == expected)
1177 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1178 if (tap->expected_ids[ii] == 0)
1182 /* If none of the expected ids matched, warn */
1183 jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
1184 tap->dotted_name, tap->idcode);
1185 for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1188 snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, tap->expected_ids_cnt);
1189 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1190 tap->dotted_name, tap->expected_ids[ii]);
1195 /* Try to examine chain layout according to IEEE 1149.1 §12
1196 * This is called a "blind interrogation" of the scan chain.
1198 static int jtag_examine_chain(void)
1201 unsigned max_taps = jtag_tap_count();
1203 /* Autoprobe up to this many. */
1204 if (max_taps < JTAG_MAX_AUTO_TAPS)
1205 max_taps = JTAG_MAX_AUTO_TAPS;
1207 /* Add room for end-of-chain marker. */
1210 uint8_t *idcode_buffer = calloc(4, max_taps);
1212 return ERROR_JTAG_INIT_FAILED;
1214 /* DR scan to collect BYPASS or IDCODE register contents.
1215 * Then make sure the scan data has both ones and zeroes.
1217 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1218 retval = jtag_examine_chain_execute(idcode_buffer, max_taps);
1219 if (retval != ERROR_OK)
1221 if (!jtag_examine_chain_check(idcode_buffer, max_taps)) {
1222 retval = ERROR_JTAG_INIT_FAILED;
1226 /* Point at the 1st predefined tap, if any */
1227 struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
1229 unsigned bit_count = 0;
1230 unsigned autocount = 0;
1231 for (unsigned i = 0; i < max_taps; i++) {
1232 assert(bit_count < max_taps * 32);
1233 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1235 /* No predefined TAP? Auto-probe. */
1237 /* Is there another TAP? */
1238 if (jtag_idcode_is_final(idcode))
1241 /* Default everything in this TAP except IR length.
1243 * REVISIT create a jtag_alloc(chip, tap) routine, and
1244 * share it with jim_newtap_cmd().
1246 tap = calloc(1, sizeof(*tap));
1248 retval = ERROR_FAIL;
1252 tap->chip = alloc_printf("auto%u", autocount++);
1253 tap->tapname = strdup("tap");
1254 tap->dotted_name = alloc_printf("%s.%s", tap->chip, tap->tapname);
1256 tap->ir_length = 0; /* ... signifying irlen autoprobe */
1257 tap->ir_capture_mask = 0x03;
1258 tap->ir_capture_value = 0x01;
1260 tap->enabled = true;
1265 if ((idcode & 1) == 0 && !tap->ignore_bypass) {
1266 /* Zero for LSB indicates a device in bypass */
1267 LOG_INFO("TAP %s does not have valid IDCODE (idcode=0x%" PRIx32 ")",
1268 tap->dotted_name, idcode);
1269 tap->hasidcode = false;
1274 /* Friendly devices support IDCODE */
1275 tap->hasidcode = true;
1276 tap->idcode = idcode;
1277 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found", tap->dotted_name, idcode);
1282 /* ensure the TAP ID matches what was expected */
1283 if (!jtag_examine_chain_match_tap(tap))
1284 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1286 tap = jtag_tap_next_enabled(tap);
1289 /* After those IDCODE or BYPASS register values should be
1290 * only the data we fed into the scan chain.
1292 if (jtag_examine_chain_end(idcode_buffer, bit_count, max_taps * 32)) {
1293 LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
1294 retval = ERROR_JTAG_INIT_FAILED;
1298 /* Return success or, for backwards compatibility if only
1299 * some IDCODE values mismatched, a soft/continuable fault.
1302 free(idcode_buffer);
1307 * Validate the date loaded by entry to the Capture-IR state, to help
1308 * find errors related to scan chain configuration (wrong IR lengths)
1311 * Entry state can be anything. On non-error exit, all TAPs are in
1312 * bypass mode. On error exits, the scan chain is reset.
1314 static int jtag_validate_ircapture(void)
1316 struct jtag_tap *tap;
1317 uint8_t *ir_test = NULL;
1318 struct scan_field field;
1322 /* when autoprobing, accommodate huge IR lengths */
1323 int total_ir_length = 0;
1324 for (tap = jtag_tap_next_enabled(NULL); tap; tap = jtag_tap_next_enabled(tap)) {
1325 if (tap->ir_length == 0)
1326 total_ir_length += JTAG_IRLEN_MAX;
1328 total_ir_length += tap->ir_length;
1331 /* increase length to add 2 bit sentinel after scan */
1332 total_ir_length += 2;
1334 ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1338 /* after this scan, all TAPs will capture BYPASS instructions */
1339 buf_set_ones(ir_test, total_ir_length);
1341 field.num_bits = total_ir_length;
1342 field.out_value = ir_test;
1343 field.in_value = ir_test;
1345 jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
1347 LOG_DEBUG("IR capture validation scan");
1348 retval = jtag_execute_queue();
1349 if (retval != ERROR_OK)
1356 tap = jtag_tap_next_enabled(tap);
1360 /* If we're autoprobing, guess IR lengths. They must be at
1361 * least two bits. Guessing will fail if (a) any TAP does
1362 * not conform to the JTAG spec; or (b) when the upper bits
1363 * captured from some conforming TAP are nonzero. Or if
1364 * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
1365 * an implementation limit, which could someday be raised.
1367 * REVISIT optimization: if there's a *single* TAP we can
1368 * lift restrictions (a) and (b) by scanning a recognizable
1369 * pattern before the all-ones BYPASS. Check for where the
1370 * pattern starts in the result, instead of an 0...01 value.
1372 * REVISIT alternative approach: escape to some tcl code
1373 * which could provide more knowledge, based on IDCODE; and
1374 * only guess when that has no success.
1376 if (tap->ir_length == 0) {
1378 while (buf_get_u64(ir_test, chain_pos, tap->ir_length + 1) == 1
1379 && tap->ir_length < JTAG_IRLEN_MAX) {
1382 LOG_WARNING("AUTO %s - use \"jtag newtap %s %s -irlen %d "
1383 "-expected-id 0x%08" PRIx32 "\"",
1384 tap->dotted_name, tap->chip, tap->tapname, tap->ir_length, tap->idcode);
1387 /* Validate the two LSBs, which must be 01 per JTAG spec.
1389 * Or ... more bits could be provided by TAP declaration.
1390 * Plus, some taps (notably in i.MX series chips) violate
1391 * this part of the JTAG spec, so their capture mask/value
1392 * attributes might disable this test.
1394 uint64_t val = buf_get_u64(ir_test, chain_pos, tap->ir_length);
1395 if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1396 LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64 " not 0x%0*" PRIx32,
1398 (tap->ir_length + 7) / tap->ir_length, val,
1399 (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
1401 retval = ERROR_JTAG_INIT_FAILED;
1404 LOG_DEBUG("%s: IR capture 0x%0*" PRIx64, jtag_tap_name(tap),
1405 (tap->ir_length + 7) / tap->ir_length, val);
1406 chain_pos += tap->ir_length;
1409 /* verify the '11' sentinel we wrote is returned at the end */
1410 uint64_t val = buf_get_u64(ir_test, chain_pos, 2);
1412 char *cbuf = buf_to_hex_str(ir_test, total_ir_length);
1414 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1417 retval = ERROR_JTAG_INIT_FAILED;
1422 if (retval != ERROR_OK) {
1424 jtag_execute_queue();
1429 void jtag_tap_init(struct jtag_tap *tap)
1431 unsigned ir_len_bits;
1432 unsigned ir_len_bytes;
1434 /* if we're autoprobing, cope with potentially huge ir_length */
1435 ir_len_bits = tap->ir_length ? tap->ir_length : JTAG_IRLEN_MAX;
1436 ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
1438 tap->expected = calloc(1, ir_len_bytes);
1439 tap->expected_mask = calloc(1, ir_len_bytes);
1440 tap->cur_instr = malloc(ir_len_bytes);
1442 /** @todo cope better with ir_length bigger than 32 bits */
1443 if (ir_len_bits > 32)
1446 buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
1447 buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
1449 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1451 buf_set_ones(tap->cur_instr, tap->ir_length);
1453 /* register the reset callback for the TAP */
1454 jtag_register_event_callback(&jtag_reset_callback, tap);
1457 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1458 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1459 tap->abs_chain_position, tap->ir_length,
1460 (unsigned) tap->ir_capture_value,
1461 (unsigned) tap->ir_capture_mask);
1464 void jtag_tap_free(struct jtag_tap *tap)
1466 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1468 struct jtag_tap_event_action *jteap = tap->event_action;
1470 struct jtag_tap_event_action *next = jteap->next;
1471 Jim_DecrRefCount(jteap->interp, jteap->body);
1476 free(tap->expected);
1477 free(tap->expected_mask);
1478 free(tap->expected_ids);
1479 free(tap->cur_instr);
1482 free(tap->dotted_name);
1486 int jtag_init_inner(struct command_context *cmd_ctx)
1488 struct jtag_tap *tap;
1490 bool issue_setup = true;
1492 LOG_DEBUG("Init JTAG chain");
1494 tap = jtag_tap_next_enabled(NULL);
1496 /* Once JTAG itself is properly set up, and the scan chain
1497 * isn't absurdly large, IDCODE autoprobe should work fine.
1499 * But ... IRLEN autoprobe can fail even on systems which
1500 * are fully conformant to JTAG. Also, JTAG setup can be
1501 * quite finicky on some systems.
1503 * REVISIT: if TAP autoprobe works OK, then in many cases
1504 * we could escape to tcl code and set up targets based on
1505 * the TAP's IDCODE values.
1507 LOG_WARNING("There are no enabled taps. "
1508 "AUTO PROBING MIGHT NOT WORK!!");
1510 /* REVISIT default clock will often be too fast ... */
1514 retval = jtag_execute_queue();
1515 if (retval != ERROR_OK)
1518 /* Examine DR values first. This discovers problems which will
1519 * prevent communication ... hardware issues like TDO stuck, or
1520 * configuring the wrong number of (enabled) TAPs.
1522 retval = jtag_examine_chain();
1525 /* complete success */
1528 /* For backward compatibility reasons, try coping with
1529 * configuration errors involving only ID mismatches.
1530 * We might be able to talk to the devices.
1532 * Also the device might be powered down during startup.
1534 * After OpenOCD starts, we can try to power on the device
1537 LOG_ERROR("Trying to use configured scan chain anyway...");
1538 issue_setup = false;
1542 /* Now look at IR values. Problems here will prevent real
1543 * communication. They mostly mean that the IR length is
1544 * wrong ... or that the IR capture value is wrong. (The
1545 * latter is uncommon, but easily worked around: provide
1546 * ircapture/irmask values during TAP setup.)
1548 retval = jtag_validate_ircapture();
1549 if (retval != ERROR_OK) {
1550 /* The target might be powered down. The user
1551 * can power it up and reset it after firing
1554 issue_setup = false;
1558 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1560 LOG_WARNING("Bypassing JTAG setup events due to errors");
1566 int swd_init_reset(struct command_context *cmd_ctx)
1568 int retval, retval1;
1570 retval = adapter_init(cmd_ctx);
1571 if (retval != ERROR_OK)
1574 LOG_DEBUG("Initializing with hard SRST reset");
1576 if (jtag_reset_config & RESET_HAS_SRST)
1577 retval = adapter_system_reset(1);
1578 retval1 = adapter_system_reset(0);
1580 return (retval == ERROR_OK) ? retval1 : retval;
1583 int jtag_init_reset(struct command_context *cmd_ctx)
1585 int retval = adapter_init(cmd_ctx);
1586 if (retval != ERROR_OK)
1589 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1592 * This procedure is used by default when OpenOCD triggers a reset.
1593 * It's now done through an overridable Tcl "init_reset" wrapper.
1595 * This started out as a more powerful "get JTAG working" reset than
1596 * jtag_init_inner(), applying TRST because some chips won't activate
1597 * JTAG without a TRST cycle (presumed to be async, though some of
1598 * those chips synchronize JTAG activation using TCK).
1600 * But some chips only activate JTAG as part of an SRST cycle; SRST
1601 * got mixed in. So it became a hard reset routine, which got used
1602 * in more places, and which coped with JTAG reset being forced as
1603 * part of SRST (srst_pulls_trst).
1605 * And even more corner cases started to surface: TRST and/or SRST
1606 * assertion timings matter; some chips need other JTAG operations;
1607 * TRST/SRST sequences can need to be different from these, etc.
1609 * Systems should override that wrapper to support system-specific
1610 * requirements that this not-fully-generic code doesn't handle.
1612 * REVISIT once Tcl code can read the reset_config modes, this won't
1613 * need to be a C routine at all...
1615 if (jtag_reset_config & RESET_HAS_SRST) {
1616 jtag_add_reset(1, 1);
1617 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1618 jtag_add_reset(0, 1);
1620 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1623 /* some targets enable us to connect with srst asserted */
1624 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1625 if (jtag_reset_config & RESET_SRST_NO_GATING)
1626 jtag_add_reset(0, 1);
1628 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1629 jtag_add_reset(0, 0);
1632 jtag_add_reset(0, 0);
1633 retval = jtag_execute_queue();
1634 if (retval != ERROR_OK)
1637 /* Check that we can communication on the JTAG chain + eventually we want to
1638 * be able to perform enumeration only after OpenOCD has started
1639 * telnet and GDB server
1641 * That would allow users to more easily perform any magic they need to before
1644 return jtag_init_inner(cmd_ctx);
1647 int jtag_init(struct command_context *cmd_ctx)
1649 int retval = adapter_init(cmd_ctx);
1650 if (retval != ERROR_OK)
1653 /* guard against oddball hardware: force resets to be inactive */
1654 jtag_add_reset(0, 0);
1656 /* some targets enable us to connect with srst asserted */
1657 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1658 if (jtag_reset_config & RESET_SRST_NO_GATING)
1659 jtag_add_reset(0, 1);
1661 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1663 retval = jtag_execute_queue();
1664 if (retval != ERROR_OK)
1667 if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1673 void jtag_set_verify(bool enable)
1675 jtag_verify = enable;
1678 bool jtag_will_verify(void)
1683 void jtag_set_verify_capture_ir(bool enable)
1685 jtag_verify_capture_ir = enable;
1688 bool jtag_will_verify_capture_ir(void)
1690 return jtag_verify_capture_ir;
1693 int jtag_power_dropout(int *dropout)
1695 if (!is_adapter_initialized()) {
1696 /* TODO: as the jtag interface is not valid all
1697 * we can do at the moment is exit OpenOCD */
1698 LOG_ERROR("No Valid JTAG Interface Configured.");
1701 if (adapter_driver->power_dropout)
1702 return adapter_driver->power_dropout(dropout);
1704 *dropout = 0; /* by default we can't detect power dropout */
1708 int jtag_srst_asserted(int *srst_asserted)
1710 if (adapter_driver->srst_asserted)
1711 return adapter_driver->srst_asserted(srst_asserted);
1713 *srst_asserted = 0; /* by default we can't detect srst asserted */
1717 enum reset_types jtag_get_reset_config(void)
1719 return jtag_reset_config;
1721 void jtag_set_reset_config(enum reset_types type)
1723 jtag_reset_config = type;
1726 int jtag_get_trst(void)
1728 return jtag_trst == 1;
1730 int jtag_get_srst(void)
1732 return jtag_srst == 1;
1735 void jtag_set_nsrst_delay(unsigned delay)
1737 adapter_nsrst_delay = delay;
1739 unsigned jtag_get_nsrst_delay(void)
1741 return adapter_nsrst_delay;
1743 void jtag_set_ntrst_delay(unsigned delay)
1745 jtag_ntrst_delay = delay;
1747 unsigned jtag_get_ntrst_delay(void)
1749 return jtag_ntrst_delay;
1753 void jtag_set_nsrst_assert_width(unsigned delay)
1755 adapter_nsrst_assert_width = delay;
1757 unsigned jtag_get_nsrst_assert_width(void)
1759 return adapter_nsrst_assert_width;
1761 void jtag_set_ntrst_assert_width(unsigned delay)
1763 jtag_ntrst_assert_width = delay;
1765 unsigned jtag_get_ntrst_assert_width(void)
1767 return jtag_ntrst_assert_width;
1770 static int jtag_select(struct command_context *ctx)
1774 /* NOTE: interface init must already have been done.
1775 * That works with only C code ... no Tcl glue required.
1778 retval = jtag_register_commands(ctx);
1780 if (retval != ERROR_OK)
1783 retval = svf_register_commands(ctx);
1785 if (retval != ERROR_OK)
1788 retval = xsvf_register_commands(ctx);
1790 if (retval != ERROR_OK)
1793 return ipdbg_register_commands(ctx);
1796 static struct transport jtag_transport = {
1798 .select = jtag_select,
1802 static void jtag_constructor(void) __attribute__((constructor));
1803 static void jtag_constructor(void)
1805 transport_register(&jtag_transport);
1808 /** Returns true if the current debug session
1809 * is using JTAG as its transport.
1811 bool transport_is_jtag(void)
1813 return get_current_transport() == &jtag_transport;
1816 int adapter_resets(int trst, int srst)
1818 if (!get_current_transport()) {
1819 LOG_ERROR("transport is not selected");
1823 if (transport_is_jtag()) {
1824 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1825 LOG_ERROR("adapter has no srst signal");
1829 /* adapters without trst signal will eventually use tlr sequence */
1830 jtag_add_reset(trst, srst);
1832 * The jtag queue is still used for reset by some adapter. Flush it!
1833 * FIXME: To be removed when all adapter drivers will be updated!
1835 jtag_execute_queue();
1837 } else if (transport_is_swd() || transport_is_hla() ||
1838 transport_is_dapdirect_swd() || transport_is_dapdirect_jtag() ||
1839 transport_is_swim()) {
1840 if (trst == TRST_ASSERT) {
1841 LOG_ERROR("transport %s has no trst signal",
1842 get_current_transport()->name);
1846 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1847 LOG_ERROR("adapter has no srst signal");
1850 adapter_system_reset(srst);
1854 if (trst == TRST_DEASSERT && srst == SRST_DEASSERT)
1857 LOG_ERROR("reset is not supported on transport %s",
1858 get_current_transport()->name);
1863 int adapter_assert_reset(void)
1865 if (transport_is_jtag()) {
1866 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1867 jtag_add_reset(1, 1);
1869 jtag_add_reset(0, 1);
1871 } else if (transport_is_swd() || transport_is_hla() ||
1872 transport_is_dapdirect_jtag() || transport_is_dapdirect_swd() ||
1873 transport_is_swim())
1874 return adapter_system_reset(1);
1875 else if (get_current_transport())
1876 LOG_ERROR("reset is not supported on %s",
1877 get_current_transport()->name);
1879 LOG_ERROR("transport is not selected");
1883 int adapter_deassert_reset(void)
1885 if (transport_is_jtag()) {
1886 jtag_add_reset(0, 0);
1888 } else if (transport_is_swd() || transport_is_hla() ||
1889 transport_is_dapdirect_jtag() || transport_is_dapdirect_swd() ||
1890 transport_is_swim())
1891 return adapter_system_reset(0);
1892 else if (get_current_transport())
1893 LOG_ERROR("reset is not supported on %s",
1894 get_current_transport()->name);
1896 LOG_ERROR("transport is not selected");
1900 int adapter_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1901 uint32_t port_size, unsigned int *trace_freq,
1902 unsigned int traceclkin_freq, uint16_t *prescaler)
1904 if (adapter_driver->config_trace) {
1905 return adapter_driver->config_trace(enabled, pin_protocol, port_size, trace_freq,
1906 traceclkin_freq, prescaler);
1907 } else if (enabled) {
1908 LOG_ERROR("The selected interface does not support tracing");
1915 int adapter_poll_trace(uint8_t *buf, size_t *size)
1917 if (adapter_driver->poll_trace)
1918 return adapter_driver->poll_trace(buf, size);