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 ***************************************************************************/
35 #include "interface.h"
36 #include <transport/transport.h>
37 #include <helper/jep106.h>
43 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
45 #include "xsvf/xsvf.h"
47 /** The number of JTAG queue flushes (for profiling and debugging purposes). */
48 static int jtag_flush_queue_count
;
50 /* Sleep this # of ms after flushing the queue */
51 static int jtag_flush_queue_sleep
;
53 static void jtag_add_scan_check(struct jtag_tap
*active
,
54 void (*jtag_add_scan
)(struct jtag_tap
*active
,
56 const struct scan_field
*in_fields
,
58 int in_num_fields
, struct scan_field
*in_fields
, tap_state_t state
);
61 * The jtag_error variable is set when an error occurs while executing
62 * the queue. Application code may set this using jtag_set_error(),
63 * when an error occurs during processing that should be reported during
64 * jtag_execute_queue().
66 * The value is set and cleared, but never read by normal application code.
68 * This value is returned (and cleared) by jtag_execute_queue().
70 static int jtag_error
= ERROR_OK
;
72 static const char *jtag_event_strings
[] = {
73 [JTAG_TRST_ASSERTED
] = "TAP reset",
74 [JTAG_TAP_EVENT_SETUP
] = "TAP setup",
75 [JTAG_TAP_EVENT_ENABLE
] = "TAP enabled",
76 [JTAG_TAP_EVENT_DISABLE
] = "TAP disabled",
80 * JTAG adapters must initialize with TRST and SRST de-asserted
81 * (they're negative logic, so that means *high*). But some
82 * hardware doesn't necessarily work that way ... so set things
83 * up so that jtag_init() always forces that state.
85 static int jtag_trst
= -1;
86 static int jtag_srst
= -1;
89 * List all TAPs that have been created.
91 static struct jtag_tap
*__jtag_all_taps
;
93 static enum reset_types jtag_reset_config
= RESET_NONE
;
94 tap_state_t cmd_queue_cur_state
= TAP_RESET
;
96 static bool jtag_verify_capture_ir
= true;
97 static int jtag_verify
= 1;
99 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
100 *deasserted (in ms) */
101 static int adapter_nsrst_delay
; /* default to no nSRST delay */
102 static int jtag_ntrst_delay
;/* default to no nTRST delay */
103 static int adapter_nsrst_assert_width
; /* width of assertion */
104 static int jtag_ntrst_assert_width
; /* width of assertion */
107 * Contains a single callback along with a pointer that will be passed
108 * when an event occurs.
110 struct jtag_event_callback
{
111 /** a event callback */
112 jtag_event_handler_t callback
;
113 /** the private data to pass to the callback */
115 /** the next callback */
116 struct jtag_event_callback
*next
;
119 /* callbacks to inform high-level handlers about JTAG state changes */
120 static struct jtag_event_callback
*jtag_event_callbacks
;
123 static int speed_khz
;
124 /* speed to fallback to when RCLK is requested but not supported */
125 static int rclk_fallback_speed_khz
;
126 static enum {CLOCK_MODE_UNSELECTED
, CLOCK_MODE_KHZ
, CLOCK_MODE_RCLK
} clock_mode
;
127 static int jtag_speed
;
129 static struct jtag_interface
*jtag
;
132 struct jtag_interface
*jtag_interface
;
134 void jtag_set_flush_queue_sleep(int ms
)
136 jtag_flush_queue_sleep
= ms
;
139 void jtag_set_error(int error
)
141 if ((error
== ERROR_OK
) || (jtag_error
!= ERROR_OK
))
146 int jtag_error_clear(void)
148 int temp
= jtag_error
;
149 jtag_error
= ERROR_OK
;
155 static bool jtag_poll
= 1;
157 bool is_jtag_poll_safe(void)
159 /* Polling can be disabled explicitly with set_enabled(false).
160 * It is also implicitly disabled while TRST is active and
161 * while SRST is gating the JTAG clock.
163 if (!transport_is_jtag())
166 if (!jtag_poll
|| jtag_trst
!= 0)
168 return jtag_srst
== 0 || (jtag_reset_config
& RESET_SRST_NO_GATING
);
171 bool jtag_poll_get_enabled(void)
176 void jtag_poll_set_enabled(bool value
)
183 struct jtag_tap
*jtag_all_taps(void)
185 return __jtag_all_taps
;
188 unsigned jtag_tap_count(void)
190 struct jtag_tap
*t
= jtag_all_taps();
199 unsigned jtag_tap_count_enabled(void)
201 struct jtag_tap
*t
= jtag_all_taps();
211 /** Append a new TAP to the chain of all taps. */
212 void jtag_tap_add(struct jtag_tap
*t
)
214 unsigned jtag_num_taps
= 0;
216 struct jtag_tap
**tap
= &__jtag_all_taps
;
217 while (*tap
!= NULL
) {
219 tap
= &(*tap
)->next_tap
;
222 t
->abs_chain_position
= jtag_num_taps
;
225 /* returns a pointer to the n-th device in the scan chain */
226 struct jtag_tap
*jtag_tap_by_position(unsigned n
)
228 struct jtag_tap
*t
= jtag_all_taps();
236 struct jtag_tap
*jtag_tap_by_string(const char *s
)
238 /* try by name first */
239 struct jtag_tap
*t
= jtag_all_taps();
242 if (0 == strcmp(t
->dotted_name
, s
))
247 /* no tap found by name, so try to parse the name as a number */
249 if (parse_uint(s
, &n
) != ERROR_OK
)
252 /* FIXME remove this numeric fallback code late June 2010, along
253 * with all info in the User's Guide that TAPs have numeric IDs.
254 * Also update "scan_chain" output to not display the numbers.
256 t
= jtag_tap_by_position(n
);
258 LOG_WARNING("Specify TAP '%s' by name, not number %u",
264 struct jtag_tap
*jtag_tap_next_enabled(struct jtag_tap
*p
)
266 p
= p
? p
->next_tap
: jtag_all_taps();
275 const char *jtag_tap_name(const struct jtag_tap
*tap
)
277 return (tap
== NULL
) ? "(unknown)" : tap
->dotted_name
;
281 int jtag_register_event_callback(jtag_event_handler_t callback
, void *priv
)
283 struct jtag_event_callback
**callbacks_p
= &jtag_event_callbacks
;
285 if (callback
== NULL
)
286 return ERROR_COMMAND_SYNTAX_ERROR
;
289 while ((*callbacks_p
)->next
)
290 callbacks_p
= &((*callbacks_p
)->next
);
291 callbacks_p
= &((*callbacks_p
)->next
);
294 (*callbacks_p
) = malloc(sizeof(struct jtag_event_callback
));
295 (*callbacks_p
)->callback
= callback
;
296 (*callbacks_p
)->priv
= priv
;
297 (*callbacks_p
)->next
= NULL
;
302 int jtag_unregister_event_callback(jtag_event_handler_t callback
, void *priv
)
304 struct jtag_event_callback
**p
= &jtag_event_callbacks
, *temp
;
306 if (callback
== NULL
)
307 return ERROR_COMMAND_SYNTAX_ERROR
;
310 if (((*p
)->priv
!= priv
) || ((*p
)->callback
!= callback
)) {
323 int jtag_call_event_callbacks(enum jtag_event event
)
325 struct jtag_event_callback
*callback
= jtag_event_callbacks
;
327 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
330 struct jtag_event_callback
*next
;
332 /* callback may remove itself */
333 next
= callback
->next
;
334 callback
->callback(event
, callback
->priv
);
341 static void jtag_checks(void)
343 assert(jtag_trst
== 0);
346 static void jtag_prelude(tap_state_t state
)
350 assert(state
!= TAP_INVALID
);
352 cmd_queue_cur_state
= state
;
355 void jtag_add_ir_scan_noverify(struct jtag_tap
*active
, const struct scan_field
*in_fields
,
360 int retval
= interface_jtag_add_ir_scan(active
, in_fields
, state
);
361 jtag_set_error(retval
);
364 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap
*active
,
366 const struct scan_field
*in_fields
,
369 jtag_add_ir_scan_noverify(active
, in_fields
, state
);
372 /* If fields->in_value is filled out, then the captured IR value will be checked */
373 void jtag_add_ir_scan(struct jtag_tap
*active
, struct scan_field
*in_fields
, tap_state_t state
)
375 assert(state
!= TAP_RESET
);
377 if (jtag_verify
&& jtag_verify_capture_ir
) {
378 /* 8 x 32 bit id's is enough for all invocations */
380 /* if we are to run a verification of the ir scan, we need to get the input back.
381 * We may have to allocate space if the caller didn't ask for the input back.
383 in_fields
->check_value
= active
->expected
;
384 in_fields
->check_mask
= active
->expected_mask
;
385 jtag_add_scan_check(active
, jtag_add_ir_scan_noverify_callback
, 1, in_fields
,
388 jtag_add_ir_scan_noverify(active
, in_fields
, state
);
391 void jtag_add_plain_ir_scan(int num_bits
, const uint8_t *out_bits
, uint8_t *in_bits
,
394 assert(out_bits
!= NULL
);
395 assert(state
!= TAP_RESET
);
399 int retval
= interface_jtag_add_plain_ir_scan(
400 num_bits
, out_bits
, in_bits
, state
);
401 jtag_set_error(retval
);
404 static int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
,
405 uint8_t *in_check_mask
, int num_bits
);
407 static int jtag_check_value_mask_callback(jtag_callback_data_t data0
,
408 jtag_callback_data_t data1
,
409 jtag_callback_data_t data2
,
410 jtag_callback_data_t data3
)
412 return jtag_check_value_inner((uint8_t *)data0
,
418 static void jtag_add_scan_check(struct jtag_tap
*active
, void (*jtag_add_scan
)(
419 struct jtag_tap
*active
,
421 const struct scan_field
*in_fields
,
423 int in_num_fields
, struct scan_field
*in_fields
, tap_state_t state
)
425 jtag_add_scan(active
, in_num_fields
, in_fields
, state
);
427 for (int i
= 0; i
< in_num_fields
; i
++) {
428 if ((in_fields
[i
].check_value
!= NULL
) && (in_fields
[i
].in_value
!= NULL
)) {
429 /* this is synchronous for a minidriver */
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
,
468 assert(out_bits
!= NULL
);
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 betwen 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 (!(jtag
->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 void swd_add_reset(int req_srst
)
617 if (!(jtag_reset_config
& RESET_HAS_SRST
)) {
618 LOG_ERROR("BUG: can't assert SRST");
619 jtag_set_error(ERROR_FAIL
);
625 /* Maybe change SRST signal state */
626 if (jtag_srst
!= req_srst
) {
629 retval
= interface_jtag_add_reset(0, req_srst
);
630 if (retval
!= ERROR_OK
)
631 jtag_set_error(retval
);
633 retval
= jtag_execute_queue();
635 if (retval
!= ERROR_OK
) {
636 LOG_ERROR("TRST/SRST error");
640 /* SRST resets everything hooked up to that signal */
641 jtag_srst
= req_srst
;
643 LOG_DEBUG("SRST line asserted");
644 if (adapter_nsrst_assert_width
)
645 jtag_add_sleep(adapter_nsrst_assert_width
* 1000);
647 LOG_DEBUG("SRST line released");
648 if (adapter_nsrst_delay
)
649 jtag_add_sleep(adapter_nsrst_delay
* 1000);
652 retval
= jtag_execute_queue();
653 if (retval
!= ERROR_OK
) {
654 LOG_ERROR("SRST timings error");
660 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
662 int trst_with_tlr
= 0;
666 /* Without SRST, we must use target-specific JTAG operations
667 * on each target; callers should not be requesting SRST when
668 * that signal doesn't exist.
670 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
671 * can kick in even if the JTAG adapter can't drive TRST.
674 if (!(jtag_reset_config
& RESET_HAS_SRST
)) {
675 LOG_ERROR("BUG: can't assert SRST");
676 jtag_set_error(ERROR_FAIL
);
679 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) != 0
680 && !req_tlr_or_trst
) {
681 LOG_ERROR("BUG: can't assert only SRST");
682 jtag_set_error(ERROR_FAIL
);
688 /* JTAG reset (entry to TAP_RESET state) can always be achieved
689 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
690 * state first. TRST accelerates it, and bypasses those states.
692 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
693 * can kick in even if the JTAG adapter can't drive SRST.
695 if (req_tlr_or_trst
) {
696 if (!(jtag_reset_config
& RESET_HAS_TRST
))
698 else if ((jtag_reset_config
& RESET_TRST_PULLS_SRST
) != 0
705 /* Maybe change TRST and/or SRST signal state */
706 if (jtag_srst
!= new_srst
|| jtag_trst
!= new_trst
) {
709 retval
= interface_jtag_add_reset(new_trst
, new_srst
);
710 if (retval
!= ERROR_OK
)
711 jtag_set_error(retval
);
713 retval
= jtag_execute_queue();
715 if (retval
!= ERROR_OK
) {
716 LOG_ERROR("TRST/SRST error");
721 /* SRST resets everything hooked up to that signal */
722 if (jtag_srst
!= new_srst
) {
723 jtag_srst
= new_srst
;
725 LOG_DEBUG("SRST line asserted");
726 if (adapter_nsrst_assert_width
)
727 jtag_add_sleep(adapter_nsrst_assert_width
* 1000);
729 LOG_DEBUG("SRST line released");
730 if (adapter_nsrst_delay
)
731 jtag_add_sleep(adapter_nsrst_delay
* 1000);
735 /* Maybe enter the JTAG TAP_RESET state ...
736 * - using only TMS, TCK, and the JTAG state machine
737 * - or else more directly, using TRST
739 * TAP_RESET should be invisible to non-debug parts of the system.
742 LOG_DEBUG("JTAG reset with TLR instead of TRST");
745 } else if (jtag_trst
!= new_trst
) {
746 jtag_trst
= new_trst
;
748 LOG_DEBUG("TRST line asserted");
749 tap_set_state(TAP_RESET
);
750 if (jtag_ntrst_assert_width
)
751 jtag_add_sleep(jtag_ntrst_assert_width
* 1000);
753 LOG_DEBUG("TRST line released");
754 if (jtag_ntrst_delay
)
755 jtag_add_sleep(jtag_ntrst_delay
* 1000);
757 /* We just asserted nTRST, so we're now in TAP_RESET.
758 * Inform possible listeners about this, now that
759 * JTAG instructions and data can be shifted. This
760 * sequence must match jtag_add_tlr().
762 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
763 jtag_notify_event(JTAG_TRST_ASSERTED
);
768 void jtag_add_sleep(uint32_t us
)
770 /** @todo Here, keep_alive() appears to be a layering violation!!! */
772 jtag_set_error(interface_jtag_add_sleep(us
));
775 static int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
,
776 uint8_t *in_check_mask
, int num_bits
)
778 int retval
= ERROR_OK
;
782 compare_failed
= buf_cmp_mask(captured
, in_check_value
, in_check_mask
, num_bits
);
784 compare_failed
= buf_cmp(captured
, in_check_value
, num_bits
);
786 if (compare_failed
) {
787 char *captured_str
, *in_check_value_str
;
788 int bits
= (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
;
790 /* NOTE: we've lost diagnostic context here -- 'which tap' */
792 captured_str
= buf_to_str(captured
, bits
, 16);
793 in_check_value_str
= buf_to_str(in_check_value
, bits
, 16);
795 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
797 LOG_WARNING(" check_value: 0x%s", in_check_value_str
);
800 free(in_check_value_str
);
803 char *in_check_mask_str
;
805 in_check_mask_str
= buf_to_str(in_check_mask
, bits
, 16);
806 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str
);
807 free(in_check_mask_str
);
810 retval
= ERROR_JTAG_QUEUE_FAILED
;
815 void jtag_check_value_mask(struct scan_field
*field
, uint8_t *value
, uint8_t *mask
)
817 assert(field
->in_value
!= NULL
);
820 /* no checking to do */
824 jtag_execute_queue_noclear();
826 int retval
= jtag_check_value_inner(field
->in_value
, value
, mask
, field
->num_bits
);
827 jtag_set_error(retval
);
830 int default_interface_jtag_execute_queue(void)
833 LOG_ERROR("No JTAG interface configured yet. "
834 "Issue 'init' command in startup scripts "
835 "before communicating with targets.");
839 int result
= jtag
->execute_queue();
842 /* Only build this if we use a regular driver with a command queue.
843 * Otherwise jtag_command_queue won't be found at compile/link time. Its
844 * definition is in jtag/commands.c, which is only built/linked by
845 * jtag/Makefile.am if MINIDRIVER_DUMMY || !MINIDRIVER, but those variables
846 * aren't accessible here. */
847 struct jtag_command
*cmd
= jtag_command_queue
;
848 while (debug_level
>= LOG_LVL_DEBUG
&& cmd
) {
851 LOG_DEBUG_IO("JTAG %s SCAN to %s",
852 cmd
->cmd
.scan
->ir_scan
? "IR" : "DR",
853 tap_state_name(cmd
->cmd
.scan
->end_state
));
854 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++) {
855 struct scan_field
*field
= cmd
->cmd
.scan
->fields
+ i
;
856 if (field
->out_value
) {
857 char *str
= buf_to_str(field
->out_value
, field
->num_bits
, 16);
858 LOG_DEBUG_IO(" %db out: %s", field
->num_bits
, str
);
861 if (field
->in_value
) {
862 char *str
= buf_to_str(field
->in_value
, field
->num_bits
, 16);
863 LOG_DEBUG_IO(" %db in: %s", field
->num_bits
, str
);
869 LOG_DEBUG_IO("JTAG TLR RESET to %s",
870 tap_state_name(cmd
->cmd
.statemove
->end_state
));
873 LOG_DEBUG_IO("JTAG RUNTEST %d cycles to %s",
874 cmd
->cmd
.runtest
->num_cycles
,
875 tap_state_name(cmd
->cmd
.runtest
->end_state
));
879 const char *reset_str
[3] = {
880 "leave", "deassert", "assert"
882 LOG_DEBUG_IO("JTAG RESET %s TRST, %s SRST",
883 reset_str
[cmd
->cmd
.reset
->trst
+ 1],
884 reset_str
[cmd
->cmd
.reset
->srst
+ 1]);
888 LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
891 LOG_DEBUG_IO("JTAG SLEEP (TODO)");
893 case JTAG_STABLECLOCKS
:
894 LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
897 LOG_DEBUG_IO("JTAG TMS (TODO)");
900 LOG_ERROR("Unknown JTAG command: %d", cmd
->type
);
910 void jtag_execute_queue_noclear(void)
912 jtag_flush_queue_count
++;
913 jtag_set_error(interface_jtag_execute_queue());
915 if (jtag_flush_queue_sleep
> 0) {
916 /* For debug purposes it can be useful to test performance
917 * or behavior when delaying after flushing the queue,
918 * e.g. to simulate long roundtrip times.
920 usleep(jtag_flush_queue_sleep
* 1000);
924 int jtag_get_flush_queue_count(void)
926 return jtag_flush_queue_count
;
929 int jtag_execute_queue(void)
931 jtag_execute_queue_noclear();
932 return jtag_error_clear();
935 static int jtag_reset_callback(enum jtag_event event
, void *priv
)
937 struct jtag_tap
*tap
= priv
;
939 if (event
== JTAG_TRST_ASSERTED
) {
940 tap
->enabled
= !tap
->disabled_after_reset
;
942 /* current instruction is either BYPASS or IDCODE */
943 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
950 /* sleep at least us microseconds. When we sleep more than 1000ms we
951 * do an alive sleep, i.e. keep GDB alive. Note that we could starve
952 * GDB if we slept for <1000ms many times.
954 void jtag_sleep(uint32_t us
)
959 alive_sleep((us
+999)/1000);
962 #define JTAG_MAX_AUTO_TAPS 20
964 #define EXTRACT_JEP106_BANK(X) (((X) & 0xf00) >> 8)
965 #define EXTRACT_JEP106_ID(X) (((X) & 0xfe) >> 1)
966 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
967 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
968 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
970 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
971 * know that no valid TAP will have it as an IDCODE value.
973 #define END_OF_CHAIN_FLAG 0xffffffff
975 /* a larger IR length than we ever expect to autoprobe */
976 #define JTAG_IRLEN_MAX 60
978 static int jtag_examine_chain_execute(uint8_t *idcode_buffer
, unsigned num_idcode
)
980 struct scan_field field
= {
981 .num_bits
= num_idcode
* 32,
982 .out_value
= idcode_buffer
,
983 .in_value
= idcode_buffer
,
986 /* initialize to the end of chain ID value */
987 for (unsigned i
= 0; i
< num_idcode
; i
++)
988 buf_set_u32(idcode_buffer
, i
* 32, 32, END_OF_CHAIN_FLAG
);
990 jtag_add_plain_dr_scan(field
.num_bits
, field
.out_value
, field
.in_value
, TAP_DRPAUSE
);
992 return jtag_execute_queue();
995 static bool jtag_examine_chain_check(uint8_t *idcodes
, unsigned count
)
997 uint8_t zero_check
= 0x0;
998 uint8_t one_check
= 0xff;
1000 for (unsigned i
= 0; i
< count
* 4; i
++) {
1001 zero_check
|= idcodes
[i
];
1002 one_check
&= idcodes
[i
];
1005 /* if there wasn't a single non-zero bit or if all bits were one,
1006 * the scan is not valid. We wrote a mix of both values; either
1008 * - There's a hardware issue (almost certainly):
1009 * + all-zeroes can mean a target stuck in JTAG reset
1010 * + all-ones tends to mean no target
1011 * - The scan chain is WAY longer than we can handle, *AND* either
1012 * + there are several hundreds of TAPs in bypass, or
1013 * + at least a few dozen TAPs all have an all-ones IDCODE
1015 if (zero_check
== 0x00 || one_check
== 0xff) {
1016 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
1017 (zero_check
== 0x00) ? "zeroes" : "ones");
1018 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
1024 static void jtag_examine_chain_display(enum log_levels level
, const char *msg
,
1025 const char *name
, uint32_t idcode
)
1027 log_printf_lf(level
, __FILE__
, __LINE__
, __func__
,
1028 "JTAG tap: %s %16.16s: 0x%08x "
1029 "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
1031 (unsigned int)idcode
,
1032 (unsigned int)EXTRACT_MFG(idcode
),
1033 jep106_manufacturer(EXTRACT_JEP106_BANK(idcode
), EXTRACT_JEP106_ID(idcode
)),
1034 (unsigned int)EXTRACT_PART(idcode
),
1035 (unsigned int)EXTRACT_VER(idcode
));
1038 static bool jtag_idcode_is_final(uint32_t idcode
)
1041 * Some devices, such as AVR8, will output all 1's instead
1042 * of TDI input value at end of chain. Allow those values
1043 * instead of failing.
1045 return idcode
== END_OF_CHAIN_FLAG
;
1049 * This helper checks that remaining bits in the examined chain data are
1050 * all as expected, but a single JTAG device requires only 64 bits to be
1051 * read back correctly. This can help identify and diagnose problems
1052 * with the JTAG chain earlier, gives more helpful/explicit error messages.
1053 * Returns TRUE iff garbage was found.
1055 static bool jtag_examine_chain_end(uint8_t *idcodes
, unsigned count
, unsigned max
)
1057 bool triggered
= false;
1058 for (; count
< max
- 31; count
+= 32) {
1059 uint32_t idcode
= buf_get_u32(idcodes
, count
, 32);
1061 /* do not trigger the warning if the data looks good */
1062 if (jtag_idcode_is_final(idcode
))
1064 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
1065 count
, (unsigned int)idcode
);
1071 static bool jtag_examine_chain_match_tap(const struct jtag_tap
*tap
)
1074 if (tap
->expected_ids_cnt
== 0 || !tap
->hasidcode
)
1077 /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1078 uint32_t mask
= tap
->ignore_version
? ~(0xfU
<< 28) : ~0U;
1079 uint32_t idcode
= tap
->idcode
& mask
;
1081 /* Loop over the expected identification codes and test for a match */
1082 for (unsigned ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1083 uint32_t expected
= tap
->expected_ids
[ii
] & mask
;
1085 if (idcode
== expected
)
1088 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1089 if (0 == tap
->expected_ids
[ii
])
1093 /* If none of the expected ids matched, warn */
1094 jtag_examine_chain_display(LOG_LVL_WARNING
, "UNEXPECTED",
1095 tap
->dotted_name
, tap
->idcode
);
1096 for (unsigned ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1099 snprintf(msg
, sizeof(msg
), "expected %u of %u", ii
+ 1, tap
->expected_ids_cnt
);
1100 jtag_examine_chain_display(LOG_LVL_ERROR
, msg
,
1101 tap
->dotted_name
, tap
->expected_ids
[ii
]);
1106 /* Try to examine chain layout according to IEEE 1149.1 §12
1107 * This is called a "blind interrogation" of the scan chain.
1109 static int jtag_examine_chain(void)
1112 unsigned max_taps
= jtag_tap_count();
1114 /* Autoprobe up to this many. */
1115 if (max_taps
< JTAG_MAX_AUTO_TAPS
)
1116 max_taps
= JTAG_MAX_AUTO_TAPS
;
1118 /* Add room for end-of-chain marker. */
1121 uint8_t *idcode_buffer
= malloc(max_taps
* 4);
1122 if (idcode_buffer
== NULL
)
1123 return ERROR_JTAG_INIT_FAILED
;
1125 /* DR scan to collect BYPASS or IDCODE register contents.
1126 * Then make sure the scan data has both ones and zeroes.
1128 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1129 retval
= jtag_examine_chain_execute(idcode_buffer
, max_taps
);
1130 if (retval
!= ERROR_OK
)
1132 if (!jtag_examine_chain_check(idcode_buffer
, max_taps
)) {
1133 retval
= ERROR_JTAG_INIT_FAILED
;
1137 /* Point at the 1st predefined tap, if any */
1138 struct jtag_tap
*tap
= jtag_tap_next_enabled(NULL
);
1140 unsigned bit_count
= 0;
1141 unsigned autocount
= 0;
1142 for (unsigned i
= 0; i
< max_taps
; i
++) {
1143 assert(bit_count
< max_taps
* 32);
1144 uint32_t idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1146 /* No predefined TAP? Auto-probe. */
1148 /* Is there another TAP? */
1149 if (jtag_idcode_is_final(idcode
))
1152 /* Default everything in this TAP except IR length.
1154 * REVISIT create a jtag_alloc(chip, tap) routine, and
1155 * share it with jim_newtap_cmd().
1157 tap
= calloc(1, sizeof *tap
);
1159 retval
= ERROR_FAIL
;
1163 tap
->chip
= alloc_printf("auto%u", autocount
++);
1164 tap
->tapname
= strdup("tap");
1165 tap
->dotted_name
= alloc_printf("%s.%s", tap
->chip
, tap
->tapname
);
1167 tap
->ir_length
= 0; /* ... signifying irlen autoprobe */
1168 tap
->ir_capture_mask
= 0x03;
1169 tap
->ir_capture_value
= 0x01;
1171 tap
->enabled
= true;
1176 if ((idcode
& 1) == 0) {
1177 /* Zero for LSB indicates a device in bypass */
1178 LOG_INFO("TAP %s does not have valid IDCODE (idcode=0x%x)",
1179 tap
->dotted_name
, idcode
);
1180 tap
->hasidcode
= false;
1185 /* Friendly devices support IDCODE */
1186 tap
->hasidcode
= true;
1187 tap
->idcode
= idcode
;
1188 jtag_examine_chain_display(LOG_LVL_INFO
, "tap/device found", tap
->dotted_name
, idcode
);
1193 /* ensure the TAP ID matches what was expected */
1194 if (!jtag_examine_chain_match_tap(tap
))
1195 retval
= ERROR_JTAG_INIT_SOFT_FAIL
;
1197 tap
= jtag_tap_next_enabled(tap
);
1200 /* After those IDCODE or BYPASS register values should be
1201 * only the data we fed into the scan chain.
1203 if (jtag_examine_chain_end(idcode_buffer
, bit_count
, max_taps
* 32)) {
1204 LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
1205 retval
= ERROR_JTAG_INIT_FAILED
;
1209 /* Return success or, for backwards compatibility if only
1210 * some IDCODE values mismatched, a soft/continuable fault.
1213 free(idcode_buffer
);
1218 * Validate the date loaded by entry to the Capture-IR state, to help
1219 * find errors related to scan chain configuration (wrong IR lengths)
1222 * Entry state can be anything. On non-error exit, all TAPs are in
1223 * bypass mode. On error exits, the scan chain is reset.
1225 static int jtag_validate_ircapture(void)
1227 struct jtag_tap
*tap
;
1228 int total_ir_length
= 0;
1229 uint8_t *ir_test
= NULL
;
1230 struct scan_field field
;
1235 /* when autoprobing, accomodate huge IR lengths */
1236 for (tap
= NULL
, total_ir_length
= 0;
1237 (tap
= jtag_tap_next_enabled(tap
)) != NULL
;
1238 total_ir_length
+= tap
->ir_length
) {
1239 if (tap
->ir_length
== 0)
1240 total_ir_length
+= JTAG_IRLEN_MAX
;
1243 /* increase length to add 2 bit sentinel after scan */
1244 total_ir_length
+= 2;
1246 ir_test
= malloc(DIV_ROUND_UP(total_ir_length
, 8));
1247 if (ir_test
== NULL
)
1250 /* after this scan, all TAPs will capture BYPASS instructions */
1251 buf_set_ones(ir_test
, total_ir_length
);
1253 field
.num_bits
= total_ir_length
;
1254 field
.out_value
= ir_test
;
1255 field
.in_value
= ir_test
;
1257 jtag_add_plain_ir_scan(field
.num_bits
, field
.out_value
, field
.in_value
, TAP_IDLE
);
1259 LOG_DEBUG("IR capture validation scan");
1260 retval
= jtag_execute_queue();
1261 if (retval
!= ERROR_OK
)
1268 tap
= jtag_tap_next_enabled(tap
);
1272 /* If we're autoprobing, guess IR lengths. They must be at
1273 * least two bits. Guessing will fail if (a) any TAP does
1274 * not conform to the JTAG spec; or (b) when the upper bits
1275 * captured from some conforming TAP are nonzero. Or if
1276 * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
1277 * an implementation limit, which could someday be raised.
1279 * REVISIT optimization: if there's a *single* TAP we can
1280 * lift restrictions (a) and (b) by scanning a recognizable
1281 * pattern before the all-ones BYPASS. Check for where the
1282 * pattern starts in the result, instead of an 0...01 value.
1284 * REVISIT alternative approach: escape to some tcl code
1285 * which could provide more knowledge, based on IDCODE; and
1286 * only guess when that has no success.
1288 if (tap
->ir_length
== 0) {
1290 while ((val
= buf_get_u64(ir_test
, chain_pos
, tap
->ir_length
+ 1)) == 1
1291 && tap
->ir_length
< JTAG_IRLEN_MAX
) {
1294 LOG_WARNING("AUTO %s - use \"jtag newtap " "%s %s -irlen %d "
1295 "-expected-id 0x%08" PRIx32
"\"",
1296 tap
->dotted_name
, tap
->chip
, tap
->tapname
, tap
->ir_length
, tap
->idcode
);
1299 /* Validate the two LSBs, which must be 01 per JTAG spec.
1301 * Or ... more bits could be provided by TAP declaration.
1302 * Plus, some taps (notably in i.MX series chips) violate
1303 * this part of the JTAG spec, so their capture mask/value
1304 * attributes might disable this test.
1306 val
= buf_get_u64(ir_test
, chain_pos
, tap
->ir_length
);
1307 if ((val
& tap
->ir_capture_mask
) != tap
->ir_capture_value
) {
1308 LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64
" not 0x%0*" PRIx32
,
1310 (tap
->ir_length
+ 7) / tap
->ir_length
, val
,
1311 (tap
->ir_length
+ 7) / tap
->ir_length
, tap
->ir_capture_value
);
1313 retval
= ERROR_JTAG_INIT_FAILED
;
1316 LOG_DEBUG("%s: IR capture 0x%0*" PRIx64
, jtag_tap_name(tap
),
1317 (tap
->ir_length
+ 7) / tap
->ir_length
, val
);
1318 chain_pos
+= tap
->ir_length
;
1321 /* verify the '11' sentinel we wrote is returned at the end */
1322 val
= buf_get_u64(ir_test
, chain_pos
, 2);
1324 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1326 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1329 retval
= ERROR_JTAG_INIT_FAILED
;
1334 if (retval
!= ERROR_OK
) {
1336 jtag_execute_queue();
1341 void jtag_tap_init(struct jtag_tap
*tap
)
1343 unsigned ir_len_bits
;
1344 unsigned ir_len_bytes
;
1346 /* if we're autoprobing, cope with potentially huge ir_length */
1347 ir_len_bits
= tap
->ir_length
? : JTAG_IRLEN_MAX
;
1348 ir_len_bytes
= DIV_ROUND_UP(ir_len_bits
, 8);
1350 tap
->expected
= calloc(1, ir_len_bytes
);
1351 tap
->expected_mask
= calloc(1, ir_len_bytes
);
1352 tap
->cur_instr
= malloc(ir_len_bytes
);
1354 /** @todo cope better with ir_length bigger than 32 bits */
1355 if (ir_len_bits
> 32)
1358 buf_set_u32(tap
->expected
, 0, ir_len_bits
, tap
->ir_capture_value
);
1359 buf_set_u32(tap
->expected_mask
, 0, ir_len_bits
, tap
->ir_capture_mask
);
1361 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1363 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1365 /* register the reset callback for the TAP */
1366 jtag_register_event_callback(&jtag_reset_callback
, tap
);
1369 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1370 "irlen %d, capture: 0x%x mask: 0x%x", tap
->dotted_name
,
1371 tap
->abs_chain_position
, tap
->ir_length
,
1372 (unsigned) tap
->ir_capture_value
,
1373 (unsigned) tap
->ir_capture_mask
);
1376 void jtag_tap_free(struct jtag_tap
*tap
)
1378 jtag_unregister_event_callback(&jtag_reset_callback
, tap
);
1380 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
1382 struct jtag_tap_event_action
*next
= jteap
->next
;
1383 Jim_DecrRefCount(jteap
->interp
, jteap
->body
);
1388 free(tap
->expected
);
1389 free(tap
->expected_mask
);
1390 free(tap
->expected_ids
);
1391 free(tap
->cur_instr
);
1394 free(tap
->dotted_name
);
1399 * Do low-level setup like initializing registers, output signals,
1402 int adapter_init(struct command_context
*cmd_ctx
)
1407 if (!jtag_interface
) {
1408 /* nothing was previously specified by "interface" command */
1409 LOG_ERROR("Debug Adapter has to be specified, "
1410 "see \"interface\" command");
1411 return ERROR_JTAG_INVALID_INTERFACE
;
1415 retval
= jtag_interface
->init();
1416 if (retval
!= ERROR_OK
)
1418 jtag
= jtag_interface
;
1420 if (jtag
->speed
== NULL
) {
1421 LOG_INFO("This adapter doesn't support configurable speed");
1425 if (CLOCK_MODE_UNSELECTED
== clock_mode
) {
1426 LOG_ERROR("An adapter speed is not selected in the init script."
1427 " Insert a call to adapter_khz or jtag_rclk to proceed.");
1428 return ERROR_JTAG_INIT_FAILED
;
1431 int requested_khz
= jtag_get_speed_khz();
1432 int actual_khz
= requested_khz
;
1433 int jtag_speed_var
= 0;
1434 retval
= jtag_get_speed(&jtag_speed_var
);
1435 if (retval
!= ERROR_OK
)
1437 retval
= jtag
->speed(jtag_speed_var
);
1438 if (retval
!= ERROR_OK
)
1440 retval
= jtag_get_speed_readable(&actual_khz
);
1441 if (ERROR_OK
!= retval
)
1442 LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var
);
1443 else if (actual_khz
) {
1444 /* Adaptive clocking -- JTAG-specific */
1445 if ((CLOCK_MODE_RCLK
== clock_mode
)
1446 || ((CLOCK_MODE_KHZ
== clock_mode
) && !requested_khz
)) {
1447 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1450 LOG_INFO("clock speed %d kHz", actual_khz
);
1452 LOG_INFO("RCLK (adaptive clock speed)");
1457 int jtag_init_inner(struct command_context
*cmd_ctx
)
1459 struct jtag_tap
*tap
;
1461 bool issue_setup
= true;
1463 LOG_DEBUG("Init JTAG chain");
1465 tap
= jtag_tap_next_enabled(NULL
);
1467 /* Once JTAG itself is properly set up, and the scan chain
1468 * isn't absurdly large, IDCODE autoprobe should work fine.
1470 * But ... IRLEN autoprobe can fail even on systems which
1471 * are fully conformant to JTAG. Also, JTAG setup can be
1472 * quite finicky on some systems.
1474 * REVISIT: if TAP autoprobe works OK, then in many cases
1475 * we could escape to tcl code and set up targets based on
1476 * the TAP's IDCODE values.
1478 LOG_WARNING("There are no enabled taps. "
1479 "AUTO PROBING MIGHT NOT WORK!!");
1481 /* REVISIT default clock will often be too fast ... */
1485 retval
= jtag_execute_queue();
1486 if (retval
!= ERROR_OK
)
1489 /* Examine DR values first. This discovers problems which will
1490 * prevent communication ... hardware issues like TDO stuck, or
1491 * configuring the wrong number of (enabled) TAPs.
1493 retval
= jtag_examine_chain();
1496 /* complete success */
1499 /* For backward compatibility reasons, try coping with
1500 * configuration errors involving only ID mismatches.
1501 * We might be able to talk to the devices.
1503 * Also the device might be powered down during startup.
1505 * After OpenOCD starts, we can try to power on the device
1508 LOG_ERROR("Trying to use configured scan chain anyway...");
1509 issue_setup
= false;
1513 /* Now look at IR values. Problems here will prevent real
1514 * communication. They mostly mean that the IR length is
1515 * wrong ... or that the IR capture value is wrong. (The
1516 * latter is uncommon, but easily worked around: provide
1517 * ircapture/irmask values during TAP setup.)
1519 retval
= jtag_validate_ircapture();
1520 if (retval
!= ERROR_OK
) {
1521 /* The target might be powered down. The user
1522 * can power it up and reset it after firing
1525 issue_setup
= false;
1529 jtag_notify_event(JTAG_TAP_EVENT_SETUP
);
1531 LOG_WARNING("Bypassing JTAG setup events due to errors");
1537 int adapter_quit(void)
1539 if (jtag
&& jtag
->quit
) {
1540 /* close the JTAG interface */
1541 int result
= jtag
->quit();
1542 if (ERROR_OK
!= result
)
1543 LOG_ERROR("failed: %d", result
);
1546 struct jtag_tap
*t
= jtag_all_taps();
1548 struct jtag_tap
*n
= t
->next_tap
;
1556 int swd_init_reset(struct command_context
*cmd_ctx
)
1558 int retval
= adapter_init(cmd_ctx
);
1559 if (retval
!= ERROR_OK
)
1562 LOG_DEBUG("Initializing with hard SRST reset");
1564 if (jtag_reset_config
& RESET_HAS_SRST
)
1567 retval
= jtag_execute_queue();
1571 int jtag_init_reset(struct command_context
*cmd_ctx
)
1573 int retval
= adapter_init(cmd_ctx
);
1574 if (retval
!= ERROR_OK
)
1577 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1580 * This procedure is used by default when OpenOCD triggers a reset.
1581 * It's now done through an overridable Tcl "init_reset" wrapper.
1583 * This started out as a more powerful "get JTAG working" reset than
1584 * jtag_init_inner(), applying TRST because some chips won't activate
1585 * JTAG without a TRST cycle (presumed to be async, though some of
1586 * those chips synchronize JTAG activation using TCK).
1588 * But some chips only activate JTAG as part of an SRST cycle; SRST
1589 * got mixed in. So it became a hard reset routine, which got used
1590 * in more places, and which coped with JTAG reset being forced as
1591 * part of SRST (srst_pulls_trst).
1593 * And even more corner cases started to surface: TRST and/or SRST
1594 * assertion timings matter; some chips need other JTAG operations;
1595 * TRST/SRST sequences can need to be different from these, etc.
1597 * Systems should override that wrapper to support system-specific
1598 * requirements that this not-fully-generic code doesn't handle.
1600 * REVISIT once Tcl code can read the reset_config modes, this won't
1601 * need to be a C routine at all...
1603 if (jtag_reset_config
& RESET_HAS_SRST
) {
1604 jtag_add_reset(1, 1);
1605 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) == 0)
1606 jtag_add_reset(0, 1);
1608 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1611 /* some targets enable us to connect with srst asserted */
1612 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
1613 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
1614 jtag_add_reset(0, 1);
1616 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1617 jtag_add_reset(0, 0);
1620 jtag_add_reset(0, 0);
1621 retval
= jtag_execute_queue();
1622 if (retval
!= ERROR_OK
)
1625 /* Check that we can communication on the JTAG chain + eventually we want to
1626 * be able to perform enumeration only after OpenOCD has started
1627 * telnet and GDB server
1629 * That would allow users to more easily perform any magic they need to before
1632 return jtag_init_inner(cmd_ctx
);
1635 int jtag_init(struct command_context
*cmd_ctx
)
1637 int retval
= adapter_init(cmd_ctx
);
1638 if (retval
!= ERROR_OK
)
1641 /* guard against oddball hardware: force resets to be inactive */
1642 jtag_add_reset(0, 0);
1644 /* some targets enable us to connect with srst asserted */
1645 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
1646 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
1647 jtag_add_reset(0, 1);
1649 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1651 retval
= jtag_execute_queue();
1652 if (retval
!= ERROR_OK
)
1655 if (Jim_Eval_Named(cmd_ctx
->interp
, "jtag_init", __FILE__
, __LINE__
) != JIM_OK
)
1661 unsigned jtag_get_speed_khz(void)
1666 static int adapter_khz_to_speed(unsigned khz
, int *speed
)
1668 LOG_DEBUG("convert khz to interface specific speed value");
1672 LOG_DEBUG("have interface set up");
1674 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1678 int retval
= jtag
->khz(jtag_get_speed_khz(), &speed_div1
);
1679 if (ERROR_OK
!= retval
)
1681 *speed
= speed_div1
;
1685 static int jtag_rclk_to_speed(unsigned fallback_speed_khz
, int *speed
)
1687 int retval
= adapter_khz_to_speed(0, speed
);
1688 if ((ERROR_OK
!= retval
) && fallback_speed_khz
) {
1689 LOG_DEBUG("trying fallback speed...");
1690 retval
= adapter_khz_to_speed(fallback_speed_khz
, speed
);
1695 static int jtag_set_speed(int speed
)
1698 /* this command can be called during CONFIG,
1699 * in which case jtag isn't initialized */
1700 return jtag
? jtag
->speed(speed
) : ERROR_OK
;
1703 int jtag_config_khz(unsigned khz
)
1705 LOG_DEBUG("handle jtag khz");
1706 clock_mode
= CLOCK_MODE_KHZ
;
1708 int retval
= adapter_khz_to_speed(khz
, &speed
);
1709 return (ERROR_OK
!= retval
) ? retval
: jtag_set_speed(speed
);
1712 int jtag_config_rclk(unsigned fallback_speed_khz
)
1714 LOG_DEBUG("handle jtag rclk");
1715 clock_mode
= CLOCK_MODE_RCLK
;
1716 rclk_fallback_speed_khz
= fallback_speed_khz
;
1718 int retval
= jtag_rclk_to_speed(fallback_speed_khz
, &speed
);
1719 return (ERROR_OK
!= retval
) ? retval
: jtag_set_speed(speed
);
1722 int jtag_get_speed(int *speed
)
1724 switch (clock_mode
) {
1725 case CLOCK_MODE_KHZ
:
1726 adapter_khz_to_speed(jtag_get_speed_khz(), speed
);
1728 case CLOCK_MODE_RCLK
:
1729 jtag_rclk_to_speed(rclk_fallback_speed_khz
, speed
);
1732 LOG_ERROR("BUG: unknown jtag clock mode");
1738 int jtag_get_speed_readable(int *khz
)
1740 int jtag_speed_var
= 0;
1741 int retval
= jtag_get_speed(&jtag_speed_var
);
1742 if (retval
!= ERROR_OK
)
1746 if (!jtag
->speed_div
) {
1747 LOG_ERROR("Translation from jtag_speed to khz not implemented");
1750 return jtag
->speed_div(jtag_speed_var
, khz
);
1753 void jtag_set_verify(bool enable
)
1755 jtag_verify
= enable
;
1758 bool jtag_will_verify()
1763 void jtag_set_verify_capture_ir(bool enable
)
1765 jtag_verify_capture_ir
= enable
;
1768 bool jtag_will_verify_capture_ir()
1770 return jtag_verify_capture_ir
;
1773 int jtag_power_dropout(int *dropout
)
1776 /* TODO: as the jtag interface is not valid all
1777 * we can do at the moment is exit OpenOCD */
1778 LOG_ERROR("No Valid JTAG Interface Configured.");
1781 if (jtag
->power_dropout
)
1782 return jtag
->power_dropout(dropout
);
1784 *dropout
= 0; /* by default we can't detect power dropout */
1788 int jtag_srst_asserted(int *srst_asserted
)
1790 if (jtag
->srst_asserted
)
1791 return jtag
->srst_asserted(srst_asserted
);
1793 *srst_asserted
= 0; /* by default we can't detect srst asserted */
1797 enum reset_types
jtag_get_reset_config(void)
1799 return jtag_reset_config
;
1801 void jtag_set_reset_config(enum reset_types type
)
1803 jtag_reset_config
= type
;
1806 int jtag_get_trst(void)
1808 return jtag_trst
== 1;
1810 int jtag_get_srst(void)
1812 return jtag_srst
== 1;
1815 void jtag_set_nsrst_delay(unsigned delay
)
1817 adapter_nsrst_delay
= delay
;
1819 unsigned jtag_get_nsrst_delay(void)
1821 return adapter_nsrst_delay
;
1823 void jtag_set_ntrst_delay(unsigned delay
)
1825 jtag_ntrst_delay
= delay
;
1827 unsigned jtag_get_ntrst_delay(void)
1829 return jtag_ntrst_delay
;
1833 void jtag_set_nsrst_assert_width(unsigned delay
)
1835 adapter_nsrst_assert_width
= delay
;
1837 unsigned jtag_get_nsrst_assert_width(void)
1839 return adapter_nsrst_assert_width
;
1841 void jtag_set_ntrst_assert_width(unsigned delay
)
1843 jtag_ntrst_assert_width
= delay
;
1845 unsigned jtag_get_ntrst_assert_width(void)
1847 return jtag_ntrst_assert_width
;
1850 static int jtag_select(struct command_context
*ctx
)
1854 /* NOTE: interface init must already have been done.
1855 * That works with only C code ... no Tcl glue required.
1858 retval
= jtag_register_commands(ctx
);
1860 if (retval
!= ERROR_OK
)
1863 retval
= svf_register_commands(ctx
);
1865 if (retval
!= ERROR_OK
)
1868 return xsvf_register_commands(ctx
);
1871 static struct transport jtag_transport
= {
1873 .select
= jtag_select
,
1877 static void jtag_constructor(void) __attribute__((constructor
));
1878 static void jtag_constructor(void)
1880 transport_register(&jtag_transport
);
1883 /** Returns true if the current debug session
1884 * is using JTAG as its transport.
1886 bool transport_is_jtag(void)
1888 return get_current_transport() == &jtag_transport
;
1891 void adapter_assert_reset(void)
1893 if (transport_is_jtag()) {
1894 if (jtag_reset_config
& RESET_SRST_PULLS_TRST
)
1895 jtag_add_reset(1, 1);
1897 jtag_add_reset(0, 1);
1898 } else if (transport_is_swd())
1900 else if (get_current_transport() != NULL
)
1901 LOG_ERROR("reset is not supported on %s",
1902 get_current_transport()->name
);
1904 LOG_ERROR("transport is not selected");
1907 void adapter_deassert_reset(void)
1909 if (transport_is_jtag())
1910 jtag_add_reset(0, 0);
1911 else if (transport_is_swd())
1913 else if (get_current_transport() != NULL
)
1914 LOG_ERROR("reset is not supported on %s",
1915 get_current_transport()->name
);
1917 LOG_ERROR("transport is not selected");
1920 int adapter_config_trace(bool enabled
, enum tpiu_pin_protocol pin_protocol
,
1921 uint32_t port_size
, unsigned int *trace_freq
,
1922 unsigned int traceclkin_freq
, uint16_t *prescaler
)
1924 if (jtag
->config_trace
) {
1925 return jtag
->config_trace(enabled
, pin_protocol
, port_size
, trace_freq
,
1926 traceclkin_freq
, prescaler
);
1927 } else if (enabled
) {
1928 LOG_ERROR("The selected interface does not support tracing");
1935 int adapter_poll_trace(uint8_t *buf
, size_t *size
)
1937 if (jtag
->poll_trace
)
1938 return jtag
->poll_trace(buf
, size
);