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
= true;
140 static bool jtag_poll_en
= true;
142 bool is_jtag_poll_safe(void)
144 /* Polling can be disabled explicitly with set_enabled(false).
145 * It can also be masked with mask().
146 * It is also implicitly disabled while TRST is active and
147 * while SRST is gating the JTAG clock.
152 if (!transport_is_jtag())
155 if (!jtag_poll
|| jtag_trst
!= 0)
157 return jtag_srst
== 0 || (jtag_reset_config
& RESET_SRST_NO_GATING
);
160 bool jtag_poll_get_enabled(void)
165 void jtag_poll_set_enabled(bool value
)
170 bool jtag_poll_mask(void)
172 bool retval
= jtag_poll_en
;
173 jtag_poll_en
= false;
177 void jtag_poll_unmask(bool saved
)
179 jtag_poll_en
= saved
;
184 struct jtag_tap
*jtag_all_taps(void)
186 return __jtag_all_taps
;
189 unsigned jtag_tap_count(void)
191 struct jtag_tap
*t
= jtag_all_taps();
200 unsigned jtag_tap_count_enabled(void)
202 struct jtag_tap
*t
= jtag_all_taps();
212 /** Append a new TAP to the chain of all taps. */
213 static void jtag_tap_add(struct jtag_tap
*t
)
215 unsigned jtag_num_taps
= 0;
217 struct jtag_tap
**tap
= &__jtag_all_taps
;
220 tap
= &(*tap
)->next_tap
;
223 t
->abs_chain_position
= jtag_num_taps
;
226 /* returns a pointer to the n-th device in the scan chain */
227 struct jtag_tap
*jtag_tap_by_position(unsigned n
)
229 struct jtag_tap
*t
= jtag_all_taps();
237 struct jtag_tap
*jtag_tap_by_string(const char *s
)
239 /* try by name first */
240 struct jtag_tap
*t
= jtag_all_taps();
243 if (strcmp(t
->dotted_name
, s
) == 0)
248 /* no tap found by name, so try to parse the name as a number */
250 if (parse_uint(s
, &n
) != ERROR_OK
)
253 /* FIXME remove this numeric fallback code late June 2010, along
254 * with all info in the User's Guide that TAPs have numeric IDs.
255 * Also update "scan_chain" output to not display the numbers.
257 t
= jtag_tap_by_position(n
);
259 LOG_WARNING("Specify TAP '%s' by name, not number %u",
265 struct jtag_tap
*jtag_tap_next_enabled(struct jtag_tap
*p
)
267 p
= p
? p
->next_tap
: jtag_all_taps();
276 const char *jtag_tap_name(const struct jtag_tap
*tap
)
278 return (!tap
) ? "(unknown)" : tap
->dotted_name
;
282 int jtag_register_event_callback(jtag_event_handler_t callback
, void *priv
)
284 struct jtag_event_callback
**callbacks_p
= &jtag_event_callbacks
;
287 return ERROR_COMMAND_SYNTAX_ERROR
;
290 while ((*callbacks_p
)->next
)
291 callbacks_p
= &((*callbacks_p
)->next
);
292 callbacks_p
= &((*callbacks_p
)->next
);
295 (*callbacks_p
) = malloc(sizeof(struct jtag_event_callback
));
296 (*callbacks_p
)->callback
= callback
;
297 (*callbacks_p
)->priv
= priv
;
298 (*callbacks_p
)->next
= NULL
;
303 int jtag_unregister_event_callback(jtag_event_handler_t callback
, void *priv
)
305 struct jtag_event_callback
**p
= &jtag_event_callbacks
, *temp
;
308 return ERROR_COMMAND_SYNTAX_ERROR
;
311 if (((*p
)->priv
!= priv
) || ((*p
)->callback
!= callback
)) {
324 int jtag_call_event_callbacks(enum jtag_event event
)
326 struct jtag_event_callback
*callback
= jtag_event_callbacks
;
328 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
331 struct jtag_event_callback
*next
;
333 /* callback may remove itself */
334 next
= callback
->next
;
335 callback
->callback(event
, callback
->priv
);
342 static void jtag_checks(void)
344 assert(jtag_trst
== 0);
347 static void jtag_prelude(tap_state_t state
)
351 assert(state
!= TAP_INVALID
);
353 cmd_queue_cur_state
= state
;
356 void jtag_add_ir_scan_noverify(struct jtag_tap
*active
, const struct scan_field
*in_fields
,
361 int retval
= interface_jtag_add_ir_scan(active
, in_fields
, state
);
362 jtag_set_error(retval
);
365 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap
*active
,
367 const struct scan_field
*in_fields
,
370 jtag_add_ir_scan_noverify(active
, in_fields
, state
);
373 /* If fields->in_value is filled out, then the captured IR value will be checked */
374 void jtag_add_ir_scan(struct jtag_tap
*active
, struct scan_field
*in_fields
, tap_state_t state
)
376 assert(state
!= TAP_RESET
);
378 if (jtag_verify
&& jtag_verify_capture_ir
) {
379 /* 8 x 32 bit id's is enough for all invocations */
381 /* if we are to run a verification of the ir scan, we need to get the input back.
382 * We may have to allocate space if the caller didn't ask for the input back.
384 in_fields
->check_value
= active
->expected
;
385 in_fields
->check_mask
= active
->expected_mask
;
386 jtag_add_scan_check(active
, jtag_add_ir_scan_noverify_callback
, 1, in_fields
,
389 jtag_add_ir_scan_noverify(active
, in_fields
, state
);
392 void jtag_add_plain_ir_scan(int num_bits
, const uint8_t *out_bits
, uint8_t *in_bits
,
396 assert(state
!= TAP_RESET
);
400 int retval
= interface_jtag_add_plain_ir_scan(
401 num_bits
, out_bits
, in_bits
, state
);
402 jtag_set_error(retval
);
405 static int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
,
406 uint8_t *in_check_mask
, int num_bits
);
408 static int jtag_check_value_mask_callback(jtag_callback_data_t data0
,
409 jtag_callback_data_t data1
,
410 jtag_callback_data_t data2
,
411 jtag_callback_data_t data3
)
413 return jtag_check_value_inner((uint8_t *)data0
,
419 static void jtag_add_scan_check(struct jtag_tap
*active
, void (*jtag_add_scan
)(
420 struct jtag_tap
*active
,
422 const struct scan_field
*in_fields
,
424 int in_num_fields
, struct scan_field
*in_fields
, tap_state_t state
)
426 jtag_add_scan(active
, in_num_fields
, in_fields
, state
);
428 for (int i
= 0; i
< in_num_fields
; i
++) {
429 if ((in_fields
[i
].check_value
) && (in_fields
[i
].in_value
)) {
430 jtag_add_callback4(jtag_check_value_mask_callback
,
431 (jtag_callback_data_t
)in_fields
[i
].in_value
,
432 (jtag_callback_data_t
)in_fields
[i
].check_value
,
433 (jtag_callback_data_t
)in_fields
[i
].check_mask
,
434 (jtag_callback_data_t
)in_fields
[i
].num_bits
);
439 void jtag_add_dr_scan_check(struct jtag_tap
*active
,
441 struct scan_field
*in_fields
,
445 jtag_add_scan_check(active
, jtag_add_dr_scan
, in_num_fields
, in_fields
, state
);
447 jtag_add_dr_scan(active
, in_num_fields
, in_fields
, state
);
451 void jtag_add_dr_scan(struct jtag_tap
*active
,
453 const struct scan_field
*in_fields
,
456 assert(state
!= TAP_RESET
);
461 retval
= interface_jtag_add_dr_scan(active
, in_num_fields
, in_fields
, state
);
462 jtag_set_error(retval
);
465 void jtag_add_plain_dr_scan(int num_bits
, const uint8_t *out_bits
, uint8_t *in_bits
,
469 assert(state
!= TAP_RESET
);
474 retval
= interface_jtag_add_plain_dr_scan(num_bits
, out_bits
, in_bits
, state
);
475 jtag_set_error(retval
);
478 void jtag_add_tlr(void)
480 jtag_prelude(TAP_RESET
);
481 jtag_set_error(interface_jtag_add_tlr());
483 /* NOTE: order here matches TRST path in jtag_add_reset() */
484 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
485 jtag_notify_event(JTAG_TRST_ASSERTED
);
489 * If supported by the underlying adapter, this clocks a raw bit sequence
490 * onto TMS for switching between JTAG and SWD modes.
492 * DO NOT use this to bypass the integrity checks and logging provided
493 * by the jtag_add_pathmove() and jtag_add_statemove() calls.
495 * @param nbits How many bits to clock out.
496 * @param seq The bit sequence. The LSB is bit 0 of seq[0].
497 * @param state The JTAG tap state to record on completion. Use
498 * TAP_INVALID to represent being in in SWD mode.
500 * @todo Update naming conventions to stop assuming everything is JTAG.
502 int jtag_add_tms_seq(unsigned nbits
, const uint8_t *seq
, enum tap_state state
)
506 if (!(adapter_driver
->jtag_ops
->supported
& DEBUG_CAP_TMS_SEQ
))
507 return ERROR_JTAG_NOT_IMPLEMENTED
;
510 cmd_queue_cur_state
= state
;
512 retval
= interface_add_tms_seq(nbits
, seq
, state
);
513 jtag_set_error(retval
);
517 void jtag_add_pathmove(int num_states
, const tap_state_t
*path
)
519 tap_state_t cur_state
= cmd_queue_cur_state
;
521 /* the last state has to be a stable state */
522 if (!tap_is_state_stable(path
[num_states
- 1])) {
523 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
524 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
528 for (int i
= 0; i
< num_states
; i
++) {
529 if (path
[i
] == TAP_RESET
) {
530 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
531 jtag_set_error(ERROR_JTAG_STATE_INVALID
);
535 if (tap_state_transition(cur_state
, true) != path
[i
] &&
536 tap_state_transition(cur_state
, false) != path
[i
]) {
537 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
538 tap_state_name(cur_state
), tap_state_name(path
[i
]));
539 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID
);
547 jtag_set_error(interface_jtag_add_pathmove(num_states
, path
));
548 cmd_queue_cur_state
= path
[num_states
- 1];
551 int jtag_add_statemove(tap_state_t goal_state
)
553 tap_state_t cur_state
= cmd_queue_cur_state
;
555 if (goal_state
!= cur_state
) {
556 LOG_DEBUG("cur_state=%s goal_state=%s",
557 tap_state_name(cur_state
),
558 tap_state_name(goal_state
));
561 /* If goal is RESET, be paranoid and force that that transition
562 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
564 if (goal_state
== TAP_RESET
)
566 else if (goal_state
== cur_state
)
569 else if (tap_is_state_stable(cur_state
) && tap_is_state_stable(goal_state
)) {
570 unsigned tms_bits
= tap_get_tms_path(cur_state
, goal_state
);
571 unsigned tms_count
= tap_get_tms_path_len(cur_state
, goal_state
);
572 tap_state_t moves
[8];
573 assert(tms_count
< ARRAY_SIZE(moves
));
575 for (unsigned i
= 0; i
< tms_count
; i
++, tms_bits
>>= 1) {
576 bool bit
= tms_bits
& 1;
578 cur_state
= tap_state_transition(cur_state
, bit
);
579 moves
[i
] = cur_state
;
582 jtag_add_pathmove(tms_count
, moves
);
583 } else if (tap_state_transition(cur_state
, true) == goal_state
584 || tap_state_transition(cur_state
, false) == goal_state
)
585 jtag_add_pathmove(1, &goal_state
);
592 void jtag_add_runtest(int num_cycles
, tap_state_t state
)
595 jtag_set_error(interface_jtag_add_runtest(num_cycles
, state
));
599 void jtag_add_clocks(int num_cycles
)
601 if (!tap_is_state_stable(cmd_queue_cur_state
)) {
602 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
603 tap_state_name(cmd_queue_cur_state
));
604 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
608 if (num_cycles
> 0) {
610 jtag_set_error(interface_jtag_add_clocks(num_cycles
));
614 static int adapter_system_reset(int req_srst
)
619 if (!(jtag_reset_config
& RESET_HAS_SRST
)) {
620 LOG_ERROR("BUG: can't assert SRST");
626 /* Maybe change SRST signal state */
627 if (jtag_srst
!= req_srst
) {
628 retval
= adapter_driver
->reset(0, req_srst
);
629 if (retval
!= ERROR_OK
) {
630 LOG_ERROR("SRST error");
633 jtag_srst
= req_srst
;
636 LOG_DEBUG("SRST line asserted");
637 if (adapter_nsrst_assert_width
)
638 jtag_sleep(adapter_nsrst_assert_width
* 1000);
640 LOG_DEBUG("SRST line released");
641 if (adapter_nsrst_delay
)
642 jtag_sleep(adapter_nsrst_delay
* 1000);
649 static void legacy_jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
651 int trst_with_tlr
= 0;
655 /* Without SRST, we must use target-specific JTAG operations
656 * on each target; callers should not be requesting SRST when
657 * that signal doesn't exist.
659 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
660 * can kick in even if the JTAG adapter can't drive TRST.
663 if (!(jtag_reset_config
& RESET_HAS_SRST
)) {
664 LOG_ERROR("BUG: can't assert SRST");
665 jtag_set_error(ERROR_FAIL
);
668 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) != 0
669 && !req_tlr_or_trst
) {
670 LOG_ERROR("BUG: can't assert only SRST");
671 jtag_set_error(ERROR_FAIL
);
677 /* JTAG reset (entry to TAP_RESET state) can always be achieved
678 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
679 * state first. TRST accelerates it, and bypasses those states.
681 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
682 * can kick in even if the JTAG adapter can't drive SRST.
684 if (req_tlr_or_trst
) {
685 if (!(jtag_reset_config
& RESET_HAS_TRST
))
687 else if ((jtag_reset_config
& RESET_TRST_PULLS_SRST
) != 0
694 /* Maybe change TRST and/or SRST signal state */
695 if (jtag_srst
!= new_srst
|| jtag_trst
!= new_trst
) {
698 retval
= interface_jtag_add_reset(new_trst
, new_srst
);
699 if (retval
!= ERROR_OK
)
700 jtag_set_error(retval
);
702 retval
= jtag_execute_queue();
704 if (retval
!= ERROR_OK
) {
705 LOG_ERROR("TRST/SRST error");
710 /* SRST resets everything hooked up to that signal */
711 if (jtag_srst
!= new_srst
) {
712 jtag_srst
= new_srst
;
714 LOG_DEBUG("SRST line asserted");
715 if (adapter_nsrst_assert_width
)
716 jtag_add_sleep(adapter_nsrst_assert_width
* 1000);
718 LOG_DEBUG("SRST line released");
719 if (adapter_nsrst_delay
)
720 jtag_add_sleep(adapter_nsrst_delay
* 1000);
724 /* Maybe enter the JTAG TAP_RESET state ...
725 * - using only TMS, TCK, and the JTAG state machine
726 * - or else more directly, using TRST
728 * TAP_RESET should be invisible to non-debug parts of the system.
731 LOG_DEBUG("JTAG reset with TLR instead of TRST");
734 } else if (jtag_trst
!= new_trst
) {
735 jtag_trst
= new_trst
;
737 LOG_DEBUG("TRST line asserted");
738 tap_set_state(TAP_RESET
);
739 if (jtag_ntrst_assert_width
)
740 jtag_add_sleep(jtag_ntrst_assert_width
* 1000);
742 LOG_DEBUG("TRST line released");
743 if (jtag_ntrst_delay
)
744 jtag_add_sleep(jtag_ntrst_delay
* 1000);
746 /* We just asserted nTRST, so we're now in TAP_RESET.
747 * Inform possible listeners about this, now that
748 * JTAG instructions and data can be shifted. This
749 * sequence must match jtag_add_tlr().
751 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
752 jtag_notify_event(JTAG_TRST_ASSERTED
);
757 /* FIXME: name is misleading; we do not plan to "add" reset into jtag queue */
758 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
761 int trst_with_tlr
= 0;
765 if (!adapter_driver
->reset
) {
766 legacy_jtag_add_reset(req_tlr_or_trst
, req_srst
);
770 /* Without SRST, we must use target-specific JTAG operations
771 * on each target; callers should not be requesting SRST when
772 * that signal doesn't exist.
774 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
775 * can kick in even if the JTAG adapter can't drive TRST.
778 if (!(jtag_reset_config
& RESET_HAS_SRST
)) {
779 LOG_ERROR("BUG: can't assert SRST");
780 jtag_set_error(ERROR_FAIL
);
783 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) != 0
784 && !req_tlr_or_trst
) {
785 LOG_ERROR("BUG: can't assert only SRST");
786 jtag_set_error(ERROR_FAIL
);
792 /* JTAG reset (entry to TAP_RESET state) can always be achieved
793 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
794 * state first. TRST accelerates it, and bypasses those states.
796 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
797 * can kick in even if the JTAG adapter can't drive SRST.
799 if (req_tlr_or_trst
) {
800 if (!(jtag_reset_config
& RESET_HAS_TRST
))
802 else if ((jtag_reset_config
& RESET_TRST_PULLS_SRST
) != 0
809 /* Maybe change TRST and/or SRST signal state */
810 if (jtag_srst
!= new_srst
|| jtag_trst
!= new_trst
) {
811 /* guarantee jtag queue empty before changing reset status */
812 jtag_execute_queue();
814 retval
= adapter_driver
->reset(new_trst
, new_srst
);
815 if (retval
!= ERROR_OK
) {
816 jtag_set_error(retval
);
817 LOG_ERROR("TRST/SRST error");
822 /* SRST resets everything hooked up to that signal */
823 if (jtag_srst
!= new_srst
) {
824 jtag_srst
= new_srst
;
826 LOG_DEBUG("SRST line asserted");
827 if (adapter_nsrst_assert_width
)
828 jtag_add_sleep(adapter_nsrst_assert_width
* 1000);
830 LOG_DEBUG("SRST line released");
831 if (adapter_nsrst_delay
)
832 jtag_add_sleep(adapter_nsrst_delay
* 1000);
836 /* Maybe enter the JTAG TAP_RESET state ...
837 * - using only TMS, TCK, and the JTAG state machine
838 * - or else more directly, using TRST
840 * TAP_RESET should be invisible to non-debug parts of the system.
843 LOG_DEBUG("JTAG reset with TLR instead of TRST");
845 jtag_execute_queue();
847 } else if (jtag_trst
!= new_trst
) {
848 jtag_trst
= new_trst
;
850 LOG_DEBUG("TRST line asserted");
851 tap_set_state(TAP_RESET
);
852 if (jtag_ntrst_assert_width
)
853 jtag_add_sleep(jtag_ntrst_assert_width
* 1000);
855 LOG_DEBUG("TRST line released");
856 if (jtag_ntrst_delay
)
857 jtag_add_sleep(jtag_ntrst_delay
* 1000);
859 /* We just asserted nTRST, so we're now in TAP_RESET.
860 * Inform possible listeners about this, now that
861 * JTAG instructions and data can be shifted. This
862 * sequence must match jtag_add_tlr().
864 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
865 jtag_notify_event(JTAG_TRST_ASSERTED
);
870 void jtag_add_sleep(uint32_t us
)
872 /** @todo Here, keep_alive() appears to be a layering violation!!! */
874 jtag_set_error(interface_jtag_add_sleep(us
));
877 static int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
,
878 uint8_t *in_check_mask
, int num_bits
)
880 int retval
= ERROR_OK
;
884 compare_failed
= buf_cmp_mask(captured
, in_check_value
, in_check_mask
, num_bits
);
886 compare_failed
= buf_cmp(captured
, in_check_value
, num_bits
);
888 if (compare_failed
) {
889 char *captured_str
, *in_check_value_str
;
890 int bits
= (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
;
892 /* NOTE: we've lost diagnostic context here -- 'which tap' */
894 captured_str
= buf_to_hex_str(captured
, bits
);
895 in_check_value_str
= buf_to_hex_str(in_check_value
, bits
);
897 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
899 LOG_WARNING(" check_value: 0x%s", in_check_value_str
);
902 free(in_check_value_str
);
905 char *in_check_mask_str
;
907 in_check_mask_str
= buf_to_hex_str(in_check_mask
, bits
);
908 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str
);
909 free(in_check_mask_str
);
912 retval
= ERROR_JTAG_QUEUE_FAILED
;
917 void jtag_check_value_mask(struct scan_field
*field
, uint8_t *value
, uint8_t *mask
)
919 assert(field
->in_value
);
922 /* no checking to do */
926 jtag_execute_queue_noclear();
928 int retval
= jtag_check_value_inner(field
->in_value
, value
, mask
, field
->num_bits
);
929 jtag_set_error(retval
);
932 int default_interface_jtag_execute_queue(void)
934 if (!is_adapter_initialized()) {
935 LOG_ERROR("No JTAG interface configured yet. "
936 "Issue 'init' command in startup scripts "
937 "before communicating with targets.");
941 if (!transport_is_jtag()) {
943 * FIXME: This should not happen!
944 * There could be old code that queues jtag commands with non jtag interfaces so, for
945 * the moment simply highlight it by log an error and return on empty execute_queue.
946 * We should fix it quitting with assert(0) because it is an internal error.
947 * The fix can be applied immediately after next release (v0.11.0 ?)
949 LOG_ERROR("JTAG API jtag_execute_queue() called on non JTAG interface");
950 if (!adapter_driver
->jtag_ops
|| !adapter_driver
->jtag_ops
->execute_queue
)
954 int result
= adapter_driver
->jtag_ops
->execute_queue();
956 struct jtag_command
*cmd
= jtag_command_queue
;
957 while (debug_level
>= LOG_LVL_DEBUG_IO
&& cmd
) {
960 LOG_DEBUG_IO("JTAG %s SCAN to %s",
961 cmd
->cmd
.scan
->ir_scan
? "IR" : "DR",
962 tap_state_name(cmd
->cmd
.scan
->end_state
));
963 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++) {
964 struct scan_field
*field
= cmd
->cmd
.scan
->fields
+ i
;
965 if (field
->out_value
) {
966 char *str
= buf_to_hex_str(field
->out_value
, field
->num_bits
);
967 LOG_DEBUG_IO(" %db out: %s", field
->num_bits
, str
);
970 if (field
->in_value
) {
971 char *str
= buf_to_hex_str(field
->in_value
, field
->num_bits
);
972 LOG_DEBUG_IO(" %db in: %s", field
->num_bits
, str
);
978 LOG_DEBUG_IO("JTAG TLR RESET to %s",
979 tap_state_name(cmd
->cmd
.statemove
->end_state
));
982 LOG_DEBUG_IO("JTAG RUNTEST %d cycles to %s",
983 cmd
->cmd
.runtest
->num_cycles
,
984 tap_state_name(cmd
->cmd
.runtest
->end_state
));
988 const char *reset_str
[3] = {
989 "leave", "deassert", "assert"
991 LOG_DEBUG_IO("JTAG RESET %s TRST, %s SRST",
992 reset_str
[cmd
->cmd
.reset
->trst
+ 1],
993 reset_str
[cmd
->cmd
.reset
->srst
+ 1]);
997 LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
1000 LOG_DEBUG_IO("JTAG SLEEP (TODO)");
1002 case JTAG_STABLECLOCKS
:
1003 LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
1006 LOG_DEBUG_IO("JTAG TMS (TODO)");
1009 LOG_ERROR("Unknown JTAG command: %d", cmd
->type
);
1018 void jtag_execute_queue_noclear(void)
1020 jtag_flush_queue_count
++;
1021 jtag_set_error(interface_jtag_execute_queue());
1023 if (jtag_flush_queue_sleep
> 0) {
1024 /* For debug purposes it can be useful to test performance
1025 * or behavior when delaying after flushing the queue,
1026 * e.g. to simulate long roundtrip times.
1028 usleep(jtag_flush_queue_sleep
* 1000);
1032 int jtag_get_flush_queue_count(void)
1034 return jtag_flush_queue_count
;
1037 int jtag_execute_queue(void)
1039 jtag_execute_queue_noclear();
1040 return jtag_error_clear();
1043 static int jtag_reset_callback(enum jtag_event event
, void *priv
)
1045 struct jtag_tap
*tap
= priv
;
1047 if (event
== JTAG_TRST_ASSERTED
) {
1048 tap
->enabled
= !tap
->disabled_after_reset
;
1050 /* current instruction is either BYPASS or IDCODE */
1051 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1058 /* sleep at least us microseconds. When we sleep more than 1000ms we
1059 * do an alive sleep, i.e. keep GDB alive. Note that we could starve
1060 * GDB if we slept for <1000ms many times.
1062 void jtag_sleep(uint32_t us
)
1067 alive_sleep((us
+999)/1000);
1070 #define JTAG_MAX_AUTO_TAPS 20
1072 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1073 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1074 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1076 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
1077 * know that no valid TAP will have it as an IDCODE value.
1079 #define END_OF_CHAIN_FLAG 0xffffffff
1081 /* a larger IR length than we ever expect to autoprobe */
1082 #define JTAG_IRLEN_MAX 60
1084 static int jtag_examine_chain_execute(uint8_t *idcode_buffer
, unsigned num_idcode
)
1086 struct scan_field field
= {
1087 .num_bits
= num_idcode
* 32,
1088 .out_value
= idcode_buffer
,
1089 .in_value
= idcode_buffer
,
1092 /* initialize to the end of chain ID value */
1093 for (unsigned i
= 0; i
< num_idcode
; i
++)
1094 buf_set_u32(idcode_buffer
, i
* 32, 32, END_OF_CHAIN_FLAG
);
1096 jtag_add_plain_dr_scan(field
.num_bits
, field
.out_value
, field
.in_value
, TAP_DRPAUSE
);
1098 return jtag_execute_queue();
1101 static bool jtag_examine_chain_check(uint8_t *idcodes
, unsigned count
)
1103 uint8_t zero_check
= 0x0;
1104 uint8_t one_check
= 0xff;
1106 for (unsigned i
= 0; i
< count
* 4; i
++) {
1107 zero_check
|= idcodes
[i
];
1108 one_check
&= idcodes
[i
];
1111 /* if there wasn't a single non-zero bit or if all bits were one,
1112 * the scan is not valid. We wrote a mix of both values; either
1114 * - There's a hardware issue (almost certainly):
1115 * + all-zeroes can mean a target stuck in JTAG reset
1116 * + all-ones tends to mean no target
1117 * - The scan chain is WAY longer than we can handle, *AND* either
1118 * + there are several hundreds of TAPs in bypass, or
1119 * + at least a few dozen TAPs all have an all-ones IDCODE
1121 if (zero_check
== 0x00 || one_check
== 0xff) {
1122 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
1123 (zero_check
== 0x00) ? "zeroes" : "ones");
1124 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
1130 static void jtag_examine_chain_display(enum log_levels level
, const char *msg
,
1131 const char *name
, uint32_t idcode
)
1133 log_printf_lf(level
, __FILE__
, __LINE__
, __func__
,
1134 "JTAG tap: %s %16.16s: 0x%08x "
1135 "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
1137 (unsigned int)idcode
,
1138 (unsigned int)EXTRACT_MFG(idcode
),
1139 jep106_manufacturer(EXTRACT_MFG(idcode
)),
1140 (unsigned int)EXTRACT_PART(idcode
),
1141 (unsigned int)EXTRACT_VER(idcode
));
1144 static bool jtag_idcode_is_final(uint32_t idcode
)
1147 * Some devices, such as AVR8, will output all 1's instead
1148 * of TDI input value at end of chain. Allow those values
1149 * instead of failing.
1151 return idcode
== END_OF_CHAIN_FLAG
;
1155 * This helper checks that remaining bits in the examined chain data are
1156 * all as expected, but a single JTAG device requires only 64 bits to be
1157 * read back correctly. This can help identify and diagnose problems
1158 * with the JTAG chain earlier, gives more helpful/explicit error messages.
1159 * Returns TRUE iff garbage was found.
1161 static bool jtag_examine_chain_end(uint8_t *idcodes
, unsigned count
, unsigned max
)
1163 bool triggered
= false;
1164 for (; count
< max
- 31; count
+= 32) {
1165 uint32_t idcode
= buf_get_u32(idcodes
, count
, 32);
1167 /* do not trigger the warning if the data looks good */
1168 if (jtag_idcode_is_final(idcode
))
1170 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
1171 count
, (unsigned int)idcode
);
1177 static bool jtag_examine_chain_match_tap(const struct jtag_tap
*tap
)
1180 if (tap
->expected_ids_cnt
== 0 || !tap
->hasidcode
)
1183 /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1184 uint32_t mask
= tap
->ignore_version
? ~(0xfU
<< 28) : ~0U;
1185 uint32_t idcode
= tap
->idcode
& mask
;
1187 /* Loop over the expected identification codes and test for a match */
1188 for (unsigned ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1189 uint32_t expected
= tap
->expected_ids
[ii
] & mask
;
1191 if (idcode
== expected
)
1194 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1195 if (tap
->expected_ids
[ii
] == 0)
1199 /* If none of the expected ids matched, warn */
1200 jtag_examine_chain_display(LOG_LVL_WARNING
, "UNEXPECTED",
1201 tap
->dotted_name
, tap
->idcode
);
1202 for (unsigned ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1205 snprintf(msg
, sizeof(msg
), "expected %u of %u", ii
+ 1, tap
->expected_ids_cnt
);
1206 jtag_examine_chain_display(LOG_LVL_ERROR
, msg
,
1207 tap
->dotted_name
, tap
->expected_ids
[ii
]);
1212 /* Try to examine chain layout according to IEEE 1149.1 §12
1213 * This is called a "blind interrogation" of the scan chain.
1215 static int jtag_examine_chain(void)
1218 unsigned max_taps
= jtag_tap_count();
1220 /* Autoprobe up to this many. */
1221 if (max_taps
< JTAG_MAX_AUTO_TAPS
)
1222 max_taps
= JTAG_MAX_AUTO_TAPS
;
1224 /* Add room for end-of-chain marker. */
1227 uint8_t *idcode_buffer
= calloc(4, max_taps
);
1229 return ERROR_JTAG_INIT_FAILED
;
1231 /* DR scan to collect BYPASS or IDCODE register contents.
1232 * Then make sure the scan data has both ones and zeroes.
1234 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1235 retval
= jtag_examine_chain_execute(idcode_buffer
, max_taps
);
1236 if (retval
!= ERROR_OK
)
1238 if (!jtag_examine_chain_check(idcode_buffer
, max_taps
)) {
1239 retval
= ERROR_JTAG_INIT_FAILED
;
1243 /* Point at the 1st predefined tap, if any */
1244 struct jtag_tap
*tap
= jtag_tap_next_enabled(NULL
);
1246 unsigned bit_count
= 0;
1247 unsigned autocount
= 0;
1248 for (unsigned i
= 0; i
< max_taps
; i
++) {
1249 assert(bit_count
< max_taps
* 32);
1250 uint32_t idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1252 /* No predefined TAP? Auto-probe. */
1254 /* Is there another TAP? */
1255 if (jtag_idcode_is_final(idcode
))
1258 /* Default everything in this TAP except IR length.
1260 * REVISIT create a jtag_alloc(chip, tap) routine, and
1261 * share it with jim_newtap_cmd().
1263 tap
= calloc(1, sizeof(*tap
));
1265 retval
= ERROR_FAIL
;
1269 tap
->chip
= alloc_printf("auto%u", autocount
++);
1270 tap
->tapname
= strdup("tap");
1271 tap
->dotted_name
= alloc_printf("%s.%s", tap
->chip
, tap
->tapname
);
1273 tap
->ir_length
= 0; /* ... signifying irlen autoprobe */
1274 tap
->ir_capture_mask
= 0x03;
1275 tap
->ir_capture_value
= 0x01;
1277 tap
->enabled
= true;
1282 if ((idcode
& 1) == 0 && !tap
->ignore_bypass
) {
1283 /* Zero for LSB indicates a device in bypass */
1284 LOG_INFO("TAP %s does not have valid IDCODE (idcode=0x%" PRIx32
")",
1285 tap
->dotted_name
, idcode
);
1286 tap
->hasidcode
= false;
1291 /* Friendly devices support IDCODE */
1292 tap
->hasidcode
= true;
1293 tap
->idcode
= idcode
;
1294 jtag_examine_chain_display(LOG_LVL_INFO
, "tap/device found", tap
->dotted_name
, idcode
);
1299 /* ensure the TAP ID matches what was expected */
1300 if (!jtag_examine_chain_match_tap(tap
))
1301 retval
= ERROR_JTAG_INIT_SOFT_FAIL
;
1303 tap
= jtag_tap_next_enabled(tap
);
1306 /* After those IDCODE or BYPASS register values should be
1307 * only the data we fed into the scan chain.
1309 if (jtag_examine_chain_end(idcode_buffer
, bit_count
, max_taps
* 32)) {
1310 LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
1311 retval
= ERROR_JTAG_INIT_FAILED
;
1315 /* Return success or, for backwards compatibility if only
1316 * some IDCODE values mismatched, a soft/continuable fault.
1319 free(idcode_buffer
);
1324 * Validate the date loaded by entry to the Capture-IR state, to help
1325 * find errors related to scan chain configuration (wrong IR lengths)
1328 * Entry state can be anything. On non-error exit, all TAPs are in
1329 * bypass mode. On error exits, the scan chain is reset.
1331 static int jtag_validate_ircapture(void)
1333 struct jtag_tap
*tap
;
1334 uint8_t *ir_test
= NULL
;
1335 struct scan_field field
;
1339 /* when autoprobing, accommodate huge IR lengths */
1340 int total_ir_length
= 0;
1341 for (tap
= jtag_tap_next_enabled(NULL
); tap
; tap
= jtag_tap_next_enabled(tap
)) {
1342 if (tap
->ir_length
== 0)
1343 total_ir_length
+= JTAG_IRLEN_MAX
;
1345 total_ir_length
+= tap
->ir_length
;
1348 /* increase length to add 2 bit sentinel after scan */
1349 total_ir_length
+= 2;
1351 ir_test
= malloc(DIV_ROUND_UP(total_ir_length
, 8));
1355 /* after this scan, all TAPs will capture BYPASS instructions */
1356 buf_set_ones(ir_test
, total_ir_length
);
1358 field
.num_bits
= total_ir_length
;
1359 field
.out_value
= ir_test
;
1360 field
.in_value
= ir_test
;
1362 jtag_add_plain_ir_scan(field
.num_bits
, field
.out_value
, field
.in_value
, TAP_IDLE
);
1364 LOG_DEBUG("IR capture validation scan");
1365 retval
= jtag_execute_queue();
1366 if (retval
!= ERROR_OK
)
1373 tap
= jtag_tap_next_enabled(tap
);
1377 /* If we're autoprobing, guess IR lengths. They must be at
1378 * least two bits. Guessing will fail if (a) any TAP does
1379 * not conform to the JTAG spec; or (b) when the upper bits
1380 * captured from some conforming TAP are nonzero. Or if
1381 * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
1382 * an implementation limit, which could someday be raised.
1384 * REVISIT optimization: if there's a *single* TAP we can
1385 * lift restrictions (a) and (b) by scanning a recognizable
1386 * pattern before the all-ones BYPASS. Check for where the
1387 * pattern starts in the result, instead of an 0...01 value.
1389 * REVISIT alternative approach: escape to some tcl code
1390 * which could provide more knowledge, based on IDCODE; and
1391 * only guess when that has no success.
1393 if (tap
->ir_length
== 0) {
1395 while (buf_get_u64(ir_test
, chain_pos
, tap
->ir_length
+ 1) == 1
1396 && tap
->ir_length
< JTAG_IRLEN_MAX
) {
1399 LOG_WARNING("AUTO %s - use \"jtag newtap %s %s -irlen %d "
1400 "-expected-id 0x%08" PRIx32
"\"",
1401 tap
->dotted_name
, tap
->chip
, tap
->tapname
, tap
->ir_length
, tap
->idcode
);
1404 /* Validate the two LSBs, which must be 01 per JTAG spec.
1406 * Or ... more bits could be provided by TAP declaration.
1407 * Plus, some taps (notably in i.MX series chips) violate
1408 * this part of the JTAG spec, so their capture mask/value
1409 * attributes might disable this test.
1411 uint64_t val
= buf_get_u64(ir_test
, chain_pos
, tap
->ir_length
);
1412 if ((val
& tap
->ir_capture_mask
) != tap
->ir_capture_value
) {
1413 LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64
" not 0x%0*" PRIx32
,
1415 (tap
->ir_length
+ 7) / tap
->ir_length
, val
,
1416 (tap
->ir_length
+ 7) / tap
->ir_length
, tap
->ir_capture_value
);
1418 retval
= ERROR_JTAG_INIT_FAILED
;
1421 LOG_DEBUG("%s: IR capture 0x%0*" PRIx64
, jtag_tap_name(tap
),
1422 (tap
->ir_length
+ 7) / tap
->ir_length
, val
);
1423 chain_pos
+= tap
->ir_length
;
1426 /* verify the '11' sentinel we wrote is returned at the end */
1427 uint64_t val
= buf_get_u64(ir_test
, chain_pos
, 2);
1429 char *cbuf
= buf_to_hex_str(ir_test
, total_ir_length
);
1431 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1434 retval
= ERROR_JTAG_INIT_FAILED
;
1439 if (retval
!= ERROR_OK
) {
1441 jtag_execute_queue();
1446 void jtag_tap_init(struct jtag_tap
*tap
)
1448 unsigned ir_len_bits
;
1449 unsigned ir_len_bytes
;
1451 /* if we're autoprobing, cope with potentially huge ir_length */
1452 ir_len_bits
= tap
->ir_length
? tap
->ir_length
: JTAG_IRLEN_MAX
;
1453 ir_len_bytes
= DIV_ROUND_UP(ir_len_bits
, 8);
1455 tap
->expected
= calloc(1, ir_len_bytes
);
1456 tap
->expected_mask
= calloc(1, ir_len_bytes
);
1457 tap
->cur_instr
= malloc(ir_len_bytes
);
1459 /** @todo cope better with ir_length bigger than 32 bits */
1460 if (ir_len_bits
> 32)
1463 buf_set_u32(tap
->expected
, 0, ir_len_bits
, tap
->ir_capture_value
);
1464 buf_set_u32(tap
->expected_mask
, 0, ir_len_bits
, tap
->ir_capture_mask
);
1466 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1468 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1470 /* register the reset callback for the TAP */
1471 jtag_register_event_callback(&jtag_reset_callback
, tap
);
1474 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1475 "irlen %d, capture: 0x%x mask: 0x%x", tap
->dotted_name
,
1476 tap
->abs_chain_position
, tap
->ir_length
,
1477 (unsigned) tap
->ir_capture_value
,
1478 (unsigned) tap
->ir_capture_mask
);
1481 void jtag_tap_free(struct jtag_tap
*tap
)
1483 jtag_unregister_event_callback(&jtag_reset_callback
, tap
);
1485 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
1487 struct jtag_tap_event_action
*next
= jteap
->next
;
1488 Jim_DecrRefCount(jteap
->interp
, jteap
->body
);
1493 free(tap
->expected
);
1494 free(tap
->expected_mask
);
1495 free(tap
->expected_ids
);
1496 free(tap
->cur_instr
);
1499 free(tap
->dotted_name
);
1503 int jtag_init_inner(struct command_context
*cmd_ctx
)
1505 struct jtag_tap
*tap
;
1507 bool issue_setup
= true;
1509 LOG_DEBUG("Init JTAG chain");
1511 tap
= jtag_tap_next_enabled(NULL
);
1513 /* Once JTAG itself is properly set up, and the scan chain
1514 * isn't absurdly large, IDCODE autoprobe should work fine.
1516 * But ... IRLEN autoprobe can fail even on systems which
1517 * are fully conformant to JTAG. Also, JTAG setup can be
1518 * quite finicky on some systems.
1520 * REVISIT: if TAP autoprobe works OK, then in many cases
1521 * we could escape to tcl code and set up targets based on
1522 * the TAP's IDCODE values.
1524 LOG_WARNING("There are no enabled taps. "
1525 "AUTO PROBING MIGHT NOT WORK!!");
1527 /* REVISIT default clock will often be too fast ... */
1531 retval
= jtag_execute_queue();
1532 if (retval
!= ERROR_OK
)
1535 /* Examine DR values first. This discovers problems which will
1536 * prevent communication ... hardware issues like TDO stuck, or
1537 * configuring the wrong number of (enabled) TAPs.
1539 retval
= jtag_examine_chain();
1542 /* complete success */
1545 /* For backward compatibility reasons, try coping with
1546 * configuration errors involving only ID mismatches.
1547 * We might be able to talk to the devices.
1549 * Also the device might be powered down during startup.
1551 * After OpenOCD starts, we can try to power on the device
1554 LOG_ERROR("Trying to use configured scan chain anyway...");
1555 issue_setup
= false;
1559 /* Now look at IR values. Problems here will prevent real
1560 * communication. They mostly mean that the IR length is
1561 * wrong ... or that the IR capture value is wrong. (The
1562 * latter is uncommon, but easily worked around: provide
1563 * ircapture/irmask values during TAP setup.)
1565 retval
= jtag_validate_ircapture();
1566 if (retval
!= ERROR_OK
) {
1567 /* The target might be powered down. The user
1568 * can power it up and reset it after firing
1571 issue_setup
= false;
1575 jtag_notify_event(JTAG_TAP_EVENT_SETUP
);
1577 LOG_WARNING("Bypassing JTAG setup events due to errors");
1583 int swd_init_reset(struct command_context
*cmd_ctx
)
1585 int retval
, retval1
;
1587 retval
= adapter_init(cmd_ctx
);
1588 if (retval
!= ERROR_OK
)
1591 LOG_DEBUG("Initializing with hard SRST reset");
1593 if (jtag_reset_config
& RESET_HAS_SRST
)
1594 retval
= adapter_system_reset(1);
1595 retval1
= adapter_system_reset(0);
1597 return (retval
== ERROR_OK
) ? retval1
: retval
;
1600 int jtag_init_reset(struct command_context
*cmd_ctx
)
1602 int retval
= adapter_init(cmd_ctx
);
1603 if (retval
!= ERROR_OK
)
1606 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1609 * This procedure is used by default when OpenOCD triggers a reset.
1610 * It's now done through an overridable Tcl "init_reset" wrapper.
1612 * This started out as a more powerful "get JTAG working" reset than
1613 * jtag_init_inner(), applying TRST because some chips won't activate
1614 * JTAG without a TRST cycle (presumed to be async, though some of
1615 * those chips synchronize JTAG activation using TCK).
1617 * But some chips only activate JTAG as part of an SRST cycle; SRST
1618 * got mixed in. So it became a hard reset routine, which got used
1619 * in more places, and which coped with JTAG reset being forced as
1620 * part of SRST (srst_pulls_trst).
1622 * And even more corner cases started to surface: TRST and/or SRST
1623 * assertion timings matter; some chips need other JTAG operations;
1624 * TRST/SRST sequences can need to be different from these, etc.
1626 * Systems should override that wrapper to support system-specific
1627 * requirements that this not-fully-generic code doesn't handle.
1629 * REVISIT once Tcl code can read the reset_config modes, this won't
1630 * need to be a C routine at all...
1632 if (jtag_reset_config
& RESET_HAS_SRST
) {
1633 jtag_add_reset(1, 1);
1634 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) == 0)
1635 jtag_add_reset(0, 1);
1637 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1640 /* some targets enable us to connect with srst asserted */
1641 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
1642 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
1643 jtag_add_reset(0, 1);
1645 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1646 jtag_add_reset(0, 0);
1649 jtag_add_reset(0, 0);
1650 retval
= jtag_execute_queue();
1651 if (retval
!= ERROR_OK
)
1654 /* Check that we can communication on the JTAG chain + eventually we want to
1655 * be able to perform enumeration only after OpenOCD has started
1656 * telnet and GDB server
1658 * That would allow users to more easily perform any magic they need to before
1661 return jtag_init_inner(cmd_ctx
);
1664 int jtag_init(struct command_context
*cmd_ctx
)
1666 int retval
= adapter_init(cmd_ctx
);
1667 if (retval
!= ERROR_OK
)
1670 /* guard against oddball hardware: force resets to be inactive */
1671 jtag_add_reset(0, 0);
1673 /* some targets enable us to connect with srst asserted */
1674 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
1675 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
1676 jtag_add_reset(0, 1);
1678 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1680 retval
= jtag_execute_queue();
1681 if (retval
!= ERROR_OK
)
1684 if (Jim_Eval_Named(cmd_ctx
->interp
, "jtag_init", __FILE__
, __LINE__
) != JIM_OK
)
1690 void jtag_set_verify(bool enable
)
1692 jtag_verify
= enable
;
1695 bool jtag_will_verify(void)
1700 void jtag_set_verify_capture_ir(bool enable
)
1702 jtag_verify_capture_ir
= enable
;
1705 bool jtag_will_verify_capture_ir(void)
1707 return jtag_verify_capture_ir
;
1710 int jtag_power_dropout(int *dropout
)
1712 if (!is_adapter_initialized()) {
1713 /* TODO: as the jtag interface is not valid all
1714 * we can do at the moment is exit OpenOCD */
1715 LOG_ERROR("No Valid JTAG Interface Configured.");
1718 if (adapter_driver
->power_dropout
)
1719 return adapter_driver
->power_dropout(dropout
);
1721 *dropout
= 0; /* by default we can't detect power dropout */
1725 int jtag_srst_asserted(int *srst_asserted
)
1727 if (adapter_driver
->srst_asserted
)
1728 return adapter_driver
->srst_asserted(srst_asserted
);
1730 *srst_asserted
= 0; /* by default we can't detect srst asserted */
1734 enum reset_types
jtag_get_reset_config(void)
1736 return jtag_reset_config
;
1738 void jtag_set_reset_config(enum reset_types type
)
1740 jtag_reset_config
= type
;
1743 int jtag_get_trst(void)
1745 return jtag_trst
== 1;
1747 int jtag_get_srst(void)
1749 return jtag_srst
== 1;
1752 void jtag_set_nsrst_delay(unsigned delay
)
1754 adapter_nsrst_delay
= delay
;
1756 unsigned jtag_get_nsrst_delay(void)
1758 return adapter_nsrst_delay
;
1760 void jtag_set_ntrst_delay(unsigned delay
)
1762 jtag_ntrst_delay
= delay
;
1764 unsigned jtag_get_ntrst_delay(void)
1766 return jtag_ntrst_delay
;
1770 void jtag_set_nsrst_assert_width(unsigned delay
)
1772 adapter_nsrst_assert_width
= delay
;
1774 unsigned jtag_get_nsrst_assert_width(void)
1776 return adapter_nsrst_assert_width
;
1778 void jtag_set_ntrst_assert_width(unsigned delay
)
1780 jtag_ntrst_assert_width
= delay
;
1782 unsigned jtag_get_ntrst_assert_width(void)
1784 return jtag_ntrst_assert_width
;
1787 static int jtag_select(struct command_context
*ctx
)
1791 /* NOTE: interface init must already have been done.
1792 * That works with only C code ... no Tcl glue required.
1795 retval
= jtag_register_commands(ctx
);
1797 if (retval
!= ERROR_OK
)
1800 retval
= svf_register_commands(ctx
);
1802 if (retval
!= ERROR_OK
)
1805 retval
= xsvf_register_commands(ctx
);
1807 if (retval
!= ERROR_OK
)
1810 return ipdbg_register_commands(ctx
);
1813 static struct transport jtag_transport
= {
1815 .select
= jtag_select
,
1819 static void jtag_constructor(void) __attribute__((constructor
));
1820 static void jtag_constructor(void)
1822 transport_register(&jtag_transport
);
1825 /** Returns true if the current debug session
1826 * is using JTAG as its transport.
1828 bool transport_is_jtag(void)
1830 return get_current_transport() == &jtag_transport
;
1833 int adapter_resets(int trst
, int srst
)
1835 if (!get_current_transport()) {
1836 LOG_ERROR("transport is not selected");
1840 if (transport_is_jtag()) {
1841 if (srst
== SRST_ASSERT
&& !(jtag_reset_config
& RESET_HAS_SRST
)) {
1842 LOG_ERROR("adapter has no srst signal");
1846 /* adapters without trst signal will eventually use tlr sequence */
1847 jtag_add_reset(trst
, srst
);
1849 * The jtag queue is still used for reset by some adapter. Flush it!
1850 * FIXME: To be removed when all adapter drivers will be updated!
1852 jtag_execute_queue();
1854 } else if (transport_is_swd() || transport_is_hla() ||
1855 transport_is_dapdirect_swd() || transport_is_dapdirect_jtag() ||
1856 transport_is_swim()) {
1857 if (trst
== TRST_ASSERT
) {
1858 LOG_ERROR("transport %s has no trst signal",
1859 get_current_transport()->name
);
1863 if (srst
== SRST_ASSERT
&& !(jtag_reset_config
& RESET_HAS_SRST
)) {
1864 LOG_ERROR("adapter has no srst signal");
1867 adapter_system_reset(srst
);
1871 if (trst
== TRST_DEASSERT
&& srst
== SRST_DEASSERT
)
1874 LOG_ERROR("reset is not supported on transport %s",
1875 get_current_transport()->name
);
1880 int adapter_assert_reset(void)
1882 if (transport_is_jtag()) {
1883 if (jtag_reset_config
& RESET_SRST_PULLS_TRST
)
1884 jtag_add_reset(1, 1);
1886 jtag_add_reset(0, 1);
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(1);
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_deassert_reset(void)
1902 if (transport_is_jtag()) {
1903 jtag_add_reset(0, 0);
1905 } else if (transport_is_swd() || transport_is_hla() ||
1906 transport_is_dapdirect_jtag() || transport_is_dapdirect_swd() ||
1907 transport_is_swim())
1908 return adapter_system_reset(0);
1909 else if (get_current_transport())
1910 LOG_ERROR("reset is not supported on %s",
1911 get_current_transport()->name
);
1913 LOG_ERROR("transport is not selected");
1917 int adapter_config_trace(bool enabled
, enum tpiu_pin_protocol pin_protocol
,
1918 uint32_t port_size
, unsigned int *trace_freq
,
1919 unsigned int traceclkin_freq
, uint16_t *prescaler
)
1921 if (adapter_driver
->config_trace
) {
1922 return adapter_driver
->config_trace(enabled
, pin_protocol
, port_size
, trace_freq
,
1923 traceclkin_freq
, prescaler
);
1924 } else if (enabled
) {
1925 LOG_ERROR("The selected interface does not support tracing");
1932 int adapter_poll_trace(uint8_t *buf
, size_t *size
)
1934 if (adapter_driver
->poll_trace
)
1935 return adapter_driver
->poll_trace(buf
, size
);