1 /***************************************************************************
2 * Copyright (C) 2009 Zachary T Welch *
3 * zw@superlucidity.net *
5 * Copyright (C) 2007,2008,2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2005 by Dominic Rath *
13 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
36 #include "interface.h"
37 #include <transport/transport.h>
38 #include <helper/jep106.h>
39 #include "helper/system.h"
45 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
47 #include "xsvf/xsvf.h"
49 /* ipdbg are utilities to debug IP-cores. It uses JTAG for transport. */
50 #include "server/ipdbg.h"
52 /** The number of JTAG queue flushes (for profiling and debugging purposes). */
53 static int jtag_flush_queue_count;
55 /* Sleep this # of ms after flushing the queue */
56 static int jtag_flush_queue_sleep;
58 static void jtag_add_scan_check(struct jtag_tap *active,
59 void (*jtag_add_scan)(struct jtag_tap *active,
61 const struct scan_field *in_fields,
63 int in_num_fields, struct scan_field *in_fields, tap_state_t state);
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().
71 * The value is set and cleared, but never read by normal application code.
73 * This value is returned (and cleared) by jtag_execute_queue().
75 static int jtag_error = ERROR_OK;
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",
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.
90 static int jtag_trst = -1;
91 static int jtag_srst = -1;
94 * List all TAPs that have been created.
96 static struct jtag_tap *__jtag_all_taps;
98 static enum reset_types jtag_reset_config = RESET_NONE;
99 tap_state_t cmd_queue_cur_state = TAP_RESET;
101 static bool jtag_verify_capture_ir = true;
102 static int jtag_verify = 1;
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 */
112 * Contains a single callback along with a pointer that will be passed
113 * when an event occurs.
115 struct jtag_event_callback {
116 /** a event callback */
117 jtag_event_handler_t callback;
118 /** the private data to pass to the callback */
120 /** the next callback */
121 struct jtag_event_callback *next;
124 /* callbacks to inform high-level handlers about JTAG state changes */
125 static struct jtag_event_callback *jtag_event_callbacks;
127 extern struct adapter_driver *adapter_driver;
129 void jtag_set_flush_queue_sleep(int ms)
131 jtag_flush_queue_sleep = ms;
134 void jtag_set_error(int error)
136 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
141 int jtag_error_clear(void)
143 int temp = jtag_error;
144 jtag_error = ERROR_OK;
150 static bool jtag_poll = 1;
152 bool is_jtag_poll_safe(void)
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.
158 if (!transport_is_jtag())
161 if (!jtag_poll || jtag_trst != 0)
163 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
166 bool jtag_poll_get_enabled(void)
171 void jtag_poll_set_enabled(bool value)
178 struct jtag_tap *jtag_all_taps(void)
180 return __jtag_all_taps;
183 unsigned jtag_tap_count(void)
185 struct jtag_tap *t = jtag_all_taps();
194 unsigned jtag_tap_count_enabled(void)
196 struct jtag_tap *t = jtag_all_taps();
206 /** Append a new TAP to the chain of all taps. */
207 static void jtag_tap_add(struct jtag_tap *t)
209 unsigned jtag_num_taps = 0;
211 struct jtag_tap **tap = &__jtag_all_taps;
214 tap = &(*tap)->next_tap;
217 t->abs_chain_position = jtag_num_taps;
220 /* returns a pointer to the n-th device in the scan chain */
221 struct jtag_tap *jtag_tap_by_position(unsigned n)
223 struct jtag_tap *t = jtag_all_taps();
231 struct jtag_tap *jtag_tap_by_string(const char *s)
233 /* try by name first */
234 struct jtag_tap *t = jtag_all_taps();
237 if (strcmp(t->dotted_name, s) == 0)
242 /* no tap found by name, so try to parse the name as a number */
244 if (parse_uint(s, &n) != ERROR_OK)
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.
251 t = jtag_tap_by_position(n);
253 LOG_WARNING("Specify TAP '%s' by name, not number %u",
259 struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p)
261 p = p ? p->next_tap : jtag_all_taps();
270 const char *jtag_tap_name(const struct jtag_tap *tap)
272 return (!tap) ? "(unknown)" : tap->dotted_name;
276 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
278 struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
281 return ERROR_COMMAND_SYNTAX_ERROR;
284 while ((*callbacks_p)->next)
285 callbacks_p = &((*callbacks_p)->next);
286 callbacks_p = &((*callbacks_p)->next);
289 (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
290 (*callbacks_p)->callback = callback;
291 (*callbacks_p)->priv = priv;
292 (*callbacks_p)->next = NULL;
297 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
299 struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
302 return ERROR_COMMAND_SYNTAX_ERROR;
305 if (((*p)->priv != priv) || ((*p)->callback != callback)) {
318 int jtag_call_event_callbacks(enum jtag_event event)
320 struct jtag_event_callback *callback = jtag_event_callbacks;
322 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
325 struct jtag_event_callback *next;
327 /* callback may remove itself */
328 next = callback->next;
329 callback->callback(event, callback->priv);
336 static void jtag_checks(void)
338 assert(jtag_trst == 0);
341 static void jtag_prelude(tap_state_t state)
345 assert(state != TAP_INVALID);
347 cmd_queue_cur_state = state;
350 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
355 int retval = interface_jtag_add_ir_scan(active, in_fields, state);
356 jtag_set_error(retval);
359 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
361 const struct scan_field *in_fields,
364 jtag_add_ir_scan_noverify(active, in_fields, state);
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)
370 assert(state != TAP_RESET);
372 if (jtag_verify && jtag_verify_capture_ir) {
373 /* 8 x 32 bit id's is enough for all invocations */
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.
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,
383 jtag_add_ir_scan_noverify(active, in_fields, state);
386 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
390 assert(state != TAP_RESET);
394 int retval = interface_jtag_add_plain_ir_scan(
395 num_bits, out_bits, in_bits, state);
396 jtag_set_error(retval);
399 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
400 uint8_t *in_check_mask, int num_bits);
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)
407 return jtag_check_value_inner((uint8_t *)data0,
413 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
414 struct jtag_tap *active,
416 const struct scan_field *in_fields,
418 int in_num_fields, struct scan_field *in_fields, tap_state_t state)
420 jtag_add_scan(active, in_num_fields, in_fields, state);
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);
433 void jtag_add_dr_scan_check(struct jtag_tap *active,
435 struct scan_field *in_fields,
439 jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
441 jtag_add_dr_scan(active, in_num_fields, in_fields, state);
445 void jtag_add_dr_scan(struct jtag_tap *active,
447 const struct scan_field *in_fields,
450 assert(state != TAP_RESET);
455 retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
456 jtag_set_error(retval);
459 void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
463 assert(state != TAP_RESET);
468 retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
469 jtag_set_error(retval);
472 void jtag_add_tlr(void)
474 jtag_prelude(TAP_RESET);
475 jtag_set_error(interface_jtag_add_tlr());
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);
483 * If supported by the underlying adapter, this clocks a raw bit sequence
484 * onto TMS for switching between JTAG and SWD modes.
486 * DO NOT use this to bypass the integrity checks and logging provided
487 * by the jtag_add_pathmove() and jtag_add_statemove() calls.
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.
494 * @todo Update naming conventions to stop assuming everything is JTAG.
496 int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
500 if (!(adapter_driver->jtag_ops->supported & DEBUG_CAP_TMS_SEQ))
501 return ERROR_JTAG_NOT_IMPLEMENTED;
504 cmd_queue_cur_state = state;
506 retval = interface_add_tms_seq(nbits, seq, state);
507 jtag_set_error(retval);
511 void jtag_add_pathmove(int num_states, const tap_state_t *path)
513 tap_state_t cur_state = cmd_queue_cur_state;
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);
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);
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);
541 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
542 cmd_queue_cur_state = path[num_states - 1];
545 int jtag_add_statemove(tap_state_t goal_state)
547 tap_state_t cur_state = cmd_queue_cur_state;
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));
555 /* If goal is RESET, be paranoid and force that that transition
556 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
558 if (goal_state == TAP_RESET)
560 else if (goal_state == cur_state)
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));
569 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
570 bool bit = tms_bits & 1;
572 cur_state = tap_state_transition(cur_state, bit);
573 moves[i] = cur_state;
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);
586 void jtag_add_runtest(int num_cycles, tap_state_t state)
589 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
593 void jtag_add_clocks(int num_cycles)
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);
602 if (num_cycles > 0) {
604 jtag_set_error(interface_jtag_add_clocks(num_cycles));
608 static int adapter_system_reset(int req_srst)
613 if (!(jtag_reset_config & RESET_HAS_SRST)) {
614 LOG_ERROR("BUG: can't assert SRST");
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");
627 jtag_srst = req_srst;
630 LOG_DEBUG("SRST line asserted");
631 if (adapter_nsrst_assert_width)
632 jtag_sleep(adapter_nsrst_assert_width * 1000);
634 LOG_DEBUG("SRST line released");
635 if (adapter_nsrst_delay)
636 jtag_sleep(adapter_nsrst_delay * 1000);
643 static void legacy_jtag_add_reset(int req_tlr_or_trst, int req_srst)
645 int trst_with_tlr = 0;
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.
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.
657 if (!(jtag_reset_config & RESET_HAS_SRST)) {
658 LOG_ERROR("BUG: can't assert SRST");
659 jtag_set_error(ERROR_FAIL);
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);
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.
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.
678 if (req_tlr_or_trst) {
679 if (!(jtag_reset_config & RESET_HAS_TRST))
681 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
688 /* Maybe change TRST and/or SRST signal state */
689 if (jtag_srst != new_srst || jtag_trst != new_trst) {
692 retval = interface_jtag_add_reset(new_trst, new_srst);
693 if (retval != ERROR_OK)
694 jtag_set_error(retval);
696 retval = jtag_execute_queue();
698 if (retval != ERROR_OK) {
699 LOG_ERROR("TRST/SRST error");
704 /* SRST resets everything hooked up to that signal */
705 if (jtag_srst != new_srst) {
706 jtag_srst = new_srst;
708 LOG_DEBUG("SRST line asserted");
709 if (adapter_nsrst_assert_width)
710 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
712 LOG_DEBUG("SRST line released");
713 if (adapter_nsrst_delay)
714 jtag_add_sleep(adapter_nsrst_delay * 1000);
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
722 * TAP_RESET should be invisible to non-debug parts of the system.
725 LOG_DEBUG("JTAG reset with TLR instead of TRST");
728 } else if (jtag_trst != new_trst) {
729 jtag_trst = new_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);
736 LOG_DEBUG("TRST line released");
737 if (jtag_ntrst_delay)
738 jtag_add_sleep(jtag_ntrst_delay * 1000);
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().
745 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
746 jtag_notify_event(JTAG_TRST_ASSERTED);
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)
755 int trst_with_tlr = 0;
759 if (!adapter_driver->reset) {
760 legacy_jtag_add_reset(req_tlr_or_trst, req_srst);
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.
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.
772 if (!(jtag_reset_config & RESET_HAS_SRST)) {
773 LOG_ERROR("BUG: can't assert SRST");
774 jtag_set_error(ERROR_FAIL);
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);
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.
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.
793 if (req_tlr_or_trst) {
794 if (!(jtag_reset_config & RESET_HAS_TRST))
796 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
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();
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");
816 /* SRST resets everything hooked up to that signal */
817 if (jtag_srst != new_srst) {
818 jtag_srst = new_srst;
820 LOG_DEBUG("SRST line asserted");
821 if (adapter_nsrst_assert_width)
822 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
824 LOG_DEBUG("SRST line released");
825 if (adapter_nsrst_delay)
826 jtag_add_sleep(adapter_nsrst_delay * 1000);
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
834 * TAP_RESET should be invisible to non-debug parts of the system.
837 LOG_DEBUG("JTAG reset with TLR instead of TRST");
839 jtag_execute_queue();
841 } else if (jtag_trst != new_trst) {
842 jtag_trst = new_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);
849 LOG_DEBUG("TRST line released");
850 if (jtag_ntrst_delay)
851 jtag_add_sleep(jtag_ntrst_delay * 1000);
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().
858 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
859 jtag_notify_event(JTAG_TRST_ASSERTED);
864 void jtag_add_sleep(uint32_t us)
866 /** @todo Here, keep_alive() appears to be a layering violation!!! */
868 jtag_set_error(interface_jtag_add_sleep(us));
871 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
872 uint8_t *in_check_mask, int num_bits)
874 int retval = ERROR_OK;
878 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
880 compare_failed = buf_cmp(captured, in_check_value, num_bits);
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;
886 /* NOTE: we've lost diagnostic context here -- 'which tap' */
888 captured_str = buf_to_hex_str(captured, bits);
889 in_check_value_str = buf_to_hex_str(in_check_value, bits);
891 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
893 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
896 free(in_check_value_str);
899 char *in_check_mask_str;
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);
906 retval = ERROR_JTAG_QUEUE_FAILED;
911 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
913 assert(field->in_value);
916 /* no checking to do */
920 jtag_execute_queue_noclear();
922 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
923 jtag_set_error(retval);
926 int default_interface_jtag_execute_queue(void)
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.");
935 if (!transport_is_jtag()) {
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 ?)
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)
948 int result = adapter_driver->jtag_ops->execute_queue();
950 struct jtag_command *cmd = jtag_command_queue;
951 while (debug_level >= LOG_LVL_DEBUG_IO && cmd) {
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);
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);
972 LOG_DEBUG_IO("JTAG TLR RESET to %s",
973 tap_state_name(cmd->cmd.statemove->end_state));
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));
982 const char *reset_str[3] = {
983 "leave", "deassert", "assert"
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]);
991 LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
994 LOG_DEBUG_IO("JTAG SLEEP (TODO)");
996 case JTAG_STABLECLOCKS:
997 LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
1000 LOG_DEBUG_IO("JTAG TMS (TODO)");
1003 LOG_ERROR("Unknown JTAG command: %d", cmd->type);
1012 void jtag_execute_queue_noclear(void)
1014 jtag_flush_queue_count++;
1015 jtag_set_error(interface_jtag_execute_queue());
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.
1022 usleep(jtag_flush_queue_sleep * 1000);
1026 int jtag_get_flush_queue_count(void)
1028 return jtag_flush_queue_count;
1031 int jtag_execute_queue(void)
1033 jtag_execute_queue_noclear();
1034 return jtag_error_clear();
1037 static int jtag_reset_callback(enum jtag_event event, void *priv)
1039 struct jtag_tap *tap = priv;
1041 if (event == JTAG_TRST_ASSERTED) {
1042 tap->enabled = !tap->disabled_after_reset;
1044 /* current instruction is either BYPASS or IDCODE */
1045 buf_set_ones(tap->cur_instr, tap->ir_length);
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.
1056 void jtag_sleep(uint32_t us)
1061 alive_sleep((us+999)/1000);
1064 #define JTAG_MAX_AUTO_TAPS 20
1066 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1067 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1068 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
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.
1073 #define END_OF_CHAIN_FLAG 0xffffffff
1075 /* a larger IR length than we ever expect to autoprobe */
1076 #define JTAG_IRLEN_MAX 60
1078 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
1080 struct scan_field field = {
1081 .num_bits = num_idcode * 32,
1082 .out_value = idcode_buffer,
1083 .in_value = idcode_buffer,
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);
1090 jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
1092 return jtag_execute_queue();
1095 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
1097 uint8_t zero_check = 0x0;
1098 uint8_t one_check = 0xff;
1100 for (unsigned i = 0; i < count * 4; i++) {
1101 zero_check |= idcodes[i];
1102 one_check &= idcodes[i];
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
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
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.");
1124 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
1125 const char *name, uint32_t idcode)
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)",
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));
1138 static bool jtag_idcode_is_final(uint32_t idcode)
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.
1145 return idcode == END_OF_CHAIN_FLAG;
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.
1155 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
1157 bool triggered = false;
1158 for (; count < max - 31; count += 32) {
1159 uint32_t idcode = buf_get_u32(idcodes, count, 32);
1161 /* do not trigger the warning if the data looks good */
1162 if (jtag_idcode_is_final(idcode))
1164 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
1165 count, (unsigned int)idcode);
1171 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
1174 if (tap->expected_ids_cnt == 0 || !tap->hasidcode)
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;
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;
1185 if (idcode == expected)
1188 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1189 if (tap->expected_ids[ii] == 0)
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++) {
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]);
1206 /* Try to examine chain layout according to IEEE 1149.1 §12
1207 * This is called a "blind interrogation" of the scan chain.
1209 static int jtag_examine_chain(void)
1212 unsigned max_taps = jtag_tap_count();
1214 /* Autoprobe up to this many. */
1215 if (max_taps < JTAG_MAX_AUTO_TAPS)
1216 max_taps = JTAG_MAX_AUTO_TAPS;
1218 /* Add room for end-of-chain marker. */
1221 uint8_t *idcode_buffer = calloc(4, max_taps);
1223 return ERROR_JTAG_INIT_FAILED;
1225 /* DR scan to collect BYPASS or IDCODE register contents.
1226 * Then make sure the scan data has both ones and zeroes.
1228 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1229 retval = jtag_examine_chain_execute(idcode_buffer, max_taps);
1230 if (retval != ERROR_OK)
1232 if (!jtag_examine_chain_check(idcode_buffer, max_taps)) {
1233 retval = ERROR_JTAG_INIT_FAILED;
1237 /* Point at the 1st predefined tap, if any */
1238 struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
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);
1246 /* No predefined TAP? Auto-probe. */
1248 /* Is there another TAP? */
1249 if (jtag_idcode_is_final(idcode))
1252 /* Default everything in this TAP except IR length.
1254 * REVISIT create a jtag_alloc(chip, tap) routine, and
1255 * share it with jim_newtap_cmd().
1257 tap = calloc(1, sizeof(*tap));
1259 retval = ERROR_FAIL;
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);
1267 tap->ir_length = 0; /* ... signifying irlen autoprobe */
1268 tap->ir_capture_mask = 0x03;
1269 tap->ir_capture_value = 0x01;
1271 tap->enabled = true;
1276 if ((idcode & 1) == 0) {
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;
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);
1293 /* ensure the TAP ID matches what was expected */
1294 if (!jtag_examine_chain_match_tap(tap))
1295 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1297 tap = jtag_tap_next_enabled(tap);
1300 /* After those IDCODE or BYPASS register values should be
1301 * only the data we fed into the scan chain.
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;
1309 /* Return success or, for backwards compatibility if only
1310 * some IDCODE values mismatched, a soft/continuable fault.
1313 free(idcode_buffer);
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)
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.
1325 static int jtag_validate_ircapture(void)
1327 struct jtag_tap *tap;
1328 uint8_t *ir_test = NULL;
1329 struct scan_field field;
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;
1339 total_ir_length += tap->ir_length;
1342 /* increase length to add 2 bit sentinel after scan */
1343 total_ir_length += 2;
1345 ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1349 /* after this scan, all TAPs will capture BYPASS instructions */
1350 buf_set_ones(ir_test, total_ir_length);
1352 field.num_bits = total_ir_length;
1353 field.out_value = ir_test;
1354 field.in_value = ir_test;
1356 jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
1358 LOG_DEBUG("IR capture validation scan");
1359 retval = jtag_execute_queue();
1360 if (retval != ERROR_OK)
1367 tap = jtag_tap_next_enabled(tap);
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.
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.
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.
1387 if (tap->ir_length == 0) {
1389 while (buf_get_u64(ir_test, chain_pos, tap->ir_length + 1) == 1
1390 && tap->ir_length < JTAG_IRLEN_MAX) {
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);
1398 /* Validate the two LSBs, which must be 01 per JTAG spec.
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.
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,
1409 (tap->ir_length + 7) / tap->ir_length, val,
1410 (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
1412 retval = ERROR_JTAG_INIT_FAILED;
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;
1420 /* verify the '11' sentinel we wrote is returned at the end */
1421 uint64_t val = buf_get_u64(ir_test, chain_pos, 2);
1423 char *cbuf = buf_to_hex_str(ir_test, total_ir_length);
1425 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1428 retval = ERROR_JTAG_INIT_FAILED;
1433 if (retval != ERROR_OK) {
1435 jtag_execute_queue();
1440 void jtag_tap_init(struct jtag_tap *tap)
1442 unsigned ir_len_bits;
1443 unsigned ir_len_bytes;
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);
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);
1453 /** @todo cope better with ir_length bigger than 32 bits */
1454 if (ir_len_bits > 32)
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);
1460 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1462 buf_set_ones(tap->cur_instr, tap->ir_length);
1464 /* register the reset callback for the TAP */
1465 jtag_register_event_callback(&jtag_reset_callback, tap);
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);
1475 void jtag_tap_free(struct jtag_tap *tap)
1477 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1479 struct jtag_tap_event_action *jteap = tap->event_action;
1481 struct jtag_tap_event_action *next = jteap->next;
1482 Jim_DecrRefCount(jteap->interp, jteap->body);
1487 free(tap->expected);
1488 free(tap->expected_mask);
1489 free(tap->expected_ids);
1490 free(tap->cur_instr);
1493 free(tap->dotted_name);
1497 int jtag_init_inner(struct command_context *cmd_ctx)
1499 struct jtag_tap *tap;
1501 bool issue_setup = true;
1503 LOG_DEBUG("Init JTAG chain");
1505 tap = jtag_tap_next_enabled(NULL);
1507 /* Once JTAG itself is properly set up, and the scan chain
1508 * isn't absurdly large, IDCODE autoprobe should work fine.
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.
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.
1518 LOG_WARNING("There are no enabled taps. "
1519 "AUTO PROBING MIGHT NOT WORK!!");
1521 /* REVISIT default clock will often be too fast ... */
1525 retval = jtag_execute_queue();
1526 if (retval != ERROR_OK)
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.
1533 retval = jtag_examine_chain();
1536 /* complete success */
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.
1543 * Also the device might be powered down during startup.
1545 * After OpenOCD starts, we can try to power on the device
1548 LOG_ERROR("Trying to use configured scan chain anyway...");
1549 issue_setup = false;
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.)
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
1565 issue_setup = false;
1569 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1571 LOG_WARNING("Bypassing JTAG setup events due to errors");
1577 int swd_init_reset(struct command_context *cmd_ctx)
1579 int retval, retval1;
1581 retval = adapter_init(cmd_ctx);
1582 if (retval != ERROR_OK)
1585 LOG_DEBUG("Initializing with hard SRST reset");
1587 if (jtag_reset_config & RESET_HAS_SRST)
1588 retval = adapter_system_reset(1);
1589 retval1 = adapter_system_reset(0);
1591 return (retval == ERROR_OK) ? retval1 : retval;
1594 int jtag_init_reset(struct command_context *cmd_ctx)
1596 int retval = adapter_init(cmd_ctx);
1597 if (retval != ERROR_OK)
1600 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1603 * This procedure is used by default when OpenOCD triggers a reset.
1604 * It's now done through an overridable Tcl "init_reset" wrapper.
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).
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).
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.
1620 * Systems should override that wrapper to support system-specific
1621 * requirements that this not-fully-generic code doesn't handle.
1623 * REVISIT once Tcl code can read the reset_config modes, this won't
1624 * need to be a C routine at all...
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);
1631 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
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);
1639 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1640 jtag_add_reset(0, 0);
1643 jtag_add_reset(0, 0);
1644 retval = jtag_execute_queue();
1645 if (retval != ERROR_OK)
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
1652 * That would allow users to more easily perform any magic they need to before
1655 return jtag_init_inner(cmd_ctx);
1658 int jtag_init(struct command_context *cmd_ctx)
1660 int retval = adapter_init(cmd_ctx);
1661 if (retval != ERROR_OK)
1664 /* guard against oddball hardware: force resets to be inactive */
1665 jtag_add_reset(0, 0);
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);
1672 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1674 retval = jtag_execute_queue();
1675 if (retval != ERROR_OK)
1678 if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1684 void jtag_set_verify(bool enable)
1686 jtag_verify = enable;
1689 bool jtag_will_verify(void)
1694 void jtag_set_verify_capture_ir(bool enable)
1696 jtag_verify_capture_ir = enable;
1699 bool jtag_will_verify_capture_ir(void)
1701 return jtag_verify_capture_ir;
1704 int jtag_power_dropout(int *dropout)
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.");
1712 if (adapter_driver->power_dropout)
1713 return adapter_driver->power_dropout(dropout);
1715 *dropout = 0; /* by default we can't detect power dropout */
1719 int jtag_srst_asserted(int *srst_asserted)
1721 if (adapter_driver->srst_asserted)
1722 return adapter_driver->srst_asserted(srst_asserted);
1724 *srst_asserted = 0; /* by default we can't detect srst asserted */
1728 enum reset_types jtag_get_reset_config(void)
1730 return jtag_reset_config;
1732 void jtag_set_reset_config(enum reset_types type)
1734 jtag_reset_config = type;
1737 int jtag_get_trst(void)
1739 return jtag_trst == 1;
1741 int jtag_get_srst(void)
1743 return jtag_srst == 1;
1746 void jtag_set_nsrst_delay(unsigned delay)
1748 adapter_nsrst_delay = delay;
1750 unsigned jtag_get_nsrst_delay(void)
1752 return adapter_nsrst_delay;
1754 void jtag_set_ntrst_delay(unsigned delay)
1756 jtag_ntrst_delay = delay;
1758 unsigned jtag_get_ntrst_delay(void)
1760 return jtag_ntrst_delay;
1764 void jtag_set_nsrst_assert_width(unsigned delay)
1766 adapter_nsrst_assert_width = delay;
1768 unsigned jtag_get_nsrst_assert_width(void)
1770 return adapter_nsrst_assert_width;
1772 void jtag_set_ntrst_assert_width(unsigned delay)
1774 jtag_ntrst_assert_width = delay;
1776 unsigned jtag_get_ntrst_assert_width(void)
1778 return jtag_ntrst_assert_width;
1781 static int jtag_select(struct command_context *ctx)
1785 /* NOTE: interface init must already have been done.
1786 * That works with only C code ... no Tcl glue required.
1789 retval = jtag_register_commands(ctx);
1791 if (retval != ERROR_OK)
1794 retval = svf_register_commands(ctx);
1796 if (retval != ERROR_OK)
1799 retval = xsvf_register_commands(ctx);
1801 if (retval != ERROR_OK)
1804 return ipdbg_register_commands(ctx);
1807 static struct transport jtag_transport = {
1809 .select = jtag_select,
1813 static void jtag_constructor(void) __attribute__((constructor));
1814 static void jtag_constructor(void)
1816 transport_register(&jtag_transport);
1819 /** Returns true if the current debug session
1820 * is using JTAG as its transport.
1822 bool transport_is_jtag(void)
1824 return get_current_transport() == &jtag_transport;
1827 int adapter_resets(int trst, int srst)
1829 if (!get_current_transport()) {
1830 LOG_ERROR("transport is not selected");
1834 if (transport_is_jtag()) {
1835 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1836 LOG_ERROR("adapter has no srst signal");
1840 /* adapters without trst signal will eventually use tlr sequence */
1841 jtag_add_reset(trst, srst);
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!
1846 jtag_execute_queue();
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);
1857 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1858 LOG_ERROR("adapter has no srst signal");
1861 adapter_system_reset(srst);
1865 if (trst == TRST_DEASSERT && srst == SRST_DEASSERT)
1868 LOG_ERROR("reset is not supported on transport %s",
1869 get_current_transport()->name);
1874 int adapter_assert_reset(void)
1876 if (transport_is_jtag()) {
1877 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1878 jtag_add_reset(1, 1);
1880 jtag_add_reset(0, 1);
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);
1890 LOG_ERROR("transport is not selected");
1894 int adapter_deassert_reset(void)
1896 if (transport_is_jtag()) {
1897 jtag_add_reset(0, 0);
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);
1907 LOG_ERROR("transport is not selected");
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)
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");
1926 int adapter_poll_trace(uint8_t *buf, size_t *size)
1928 if (adapter_driver->poll_trace)
1929 return adapter_driver->poll_trace(buf, size);