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, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
35 #include "minidriver.h"
36 #include "interface.h"
43 /// The number of JTAG queue flushes (for profiling and debugging purposes).
44 static int jtag_flush_queue_count
;
46 static void jtag_add_scan_check(void (*jtag_add_scan
)(int in_num_fields
, const scan_field_t
*in_fields
, tap_state_t state
),
47 int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
);
50 * The jtag_error variable is set when an error occurs while executing
51 * the queue. Application code may set this using jtag_set_error(),
52 * when an error occurs during processing that should be reported during
53 * jtag_execute_queue().
55 * Tts value may be checked with jtag_get_error() and cleared with
56 * jtag_error_clear(). This value is returned (and cleared) by
57 * jtag_execute_queue().
59 static int jtag_error
= ERROR_OK
;
61 static const char *jtag_event_strings
[] =
63 [JTAG_TRST_ASSERTED
] = "TAP reset",
64 [JTAG_TAP_EVENT_SETUP
] = "TAP setup",
65 [JTAG_TAP_EVENT_ENABLE
] = "TAP enabled",
66 [JTAG_TAP_EVENT_DISABLE
] = "TAP disabled",
70 * JTAG adapters must initialize with TRST and SRST de-asserted
71 * (they're negative logic, so that means *high*). But some
72 * hardware doesn't necessarily work that way ... so set things
73 * up so that jtag_init() always forces that state.
75 static int jtag_trst
= -1;
76 static int jtag_srst
= -1;
79 * List all TAPs that have been created.
81 static jtag_tap_t
*__jtag_all_taps
= NULL
;
83 * The number of TAPs in the __jtag_all_taps list, used to track the
84 * assigned chain position to new TAPs
86 static unsigned jtag_num_taps
= 0;
88 static enum reset_types jtag_reset_config
= RESET_NONE
;
89 static tap_state_t cmd_queue_end_state
= TAP_RESET
;
90 tap_state_t cmd_queue_cur_state
= TAP_RESET
;
92 static bool jtag_verify_capture_ir
= true;
93 static int jtag_verify
= 1;
95 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
96 static int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
97 static int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
98 static int jtag_nsrst_assert_width
= 0; /* width of assertion */
99 static int jtag_ntrst_assert_width
= 0; /* width of assertion */
101 typedef struct jtag_event_callback_s
103 jtag_event_handler_t callback
;
105 struct jtag_event_callback_s
* next
;
106 } jtag_event_callback_t
;
108 /* callbacks to inform high-level handlers about JTAG state changes */
109 static jtag_event_callback_t
*jtag_event_callbacks
;
112 static int speed_khz
= 0;
113 /* speed to fallback to when RCLK is requested but not supported */
114 static int rclk_fallback_speed_khz
= 0;
115 static enum {CLOCK_MODE_SPEED
, CLOCK_MODE_KHZ
, CLOCK_MODE_RCLK
} clock_mode
;
116 static int jtag_speed
= 0;
118 static struct jtag_interface_s
*jtag
= NULL
;
121 jtag_interface_t
*jtag_interface
= NULL
;
123 void jtag_set_error(int error
)
125 if ((error
== ERROR_OK
) || (jtag_error
!= ERROR_OK
))
129 int jtag_get_error(void)
133 int jtag_error_clear(void)
135 int temp
= jtag_error
;
136 jtag_error
= ERROR_OK
;
141 jtag_tap_t
*jtag_all_taps(void)
143 return __jtag_all_taps
;
146 unsigned jtag_tap_count(void)
148 return jtag_num_taps
;
151 unsigned jtag_tap_count_enabled(void)
153 jtag_tap_t
*t
= jtag_all_taps();
164 /// Append a new TAP to the chain of all taps.
165 void jtag_tap_add(struct jtag_tap_s
*t
)
167 t
->abs_chain_position
= jtag_num_taps
++;
169 jtag_tap_t
**tap
= &__jtag_all_taps
;
171 tap
= &(*tap
)->next_tap
;
175 /* returns a pointer to the n-th device in the scan chain */
176 static inline jtag_tap_t
*jtag_tap_by_position(unsigned n
)
178 jtag_tap_t
*t
= jtag_all_taps();
186 jtag_tap_t
*jtag_tap_by_string(const char *s
)
188 /* try by name first */
189 jtag_tap_t
*t
= jtag_all_taps();
193 if (0 == strcmp(t
->dotted_name
, s
))
198 /* no tap found by name, so try to parse the name as a number */
200 if (parse_uint(s
, &n
) != ERROR_OK
)
203 /* FIXME remove this numeric fallback code late June 2010, along
204 * with all info in the User's Guide that TAPs have numeric IDs.
205 * Also update "scan_chain" output to not display the numbers.
207 t
= jtag_tap_by_position(n
);
209 LOG_WARNING("Specify TAP '%s' by name, not number %u",
215 jtag_tap_t
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
217 const char *cp
= Jim_GetString(o
, NULL
);
218 jtag_tap_t
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
222 Jim_SetResult_sprintf(interp
, "Tap '%s' could not be found", cp
);
226 jtag_tap_t
* jtag_tap_next_enabled(jtag_tap_t
* p
)
228 p
= p
? p
->next_tap
: jtag_all_taps();
238 const char *jtag_tap_name(const jtag_tap_t
*tap
)
240 return (tap
== NULL
) ? "(unknown)" : tap
->dotted_name
;
244 int jtag_register_event_callback(jtag_event_handler_t callback
, void *priv
)
246 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
248 if (callback
== NULL
)
250 return ERROR_INVALID_ARGUMENTS
;
255 while ((*callbacks_p
)->next
)
256 callbacks_p
= &((*callbacks_p
)->next
);
257 callbacks_p
= &((*callbacks_p
)->next
);
260 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
261 (*callbacks_p
)->callback
= callback
;
262 (*callbacks_p
)->priv
= priv
;
263 (*callbacks_p
)->next
= NULL
;
268 int jtag_unregister_event_callback(jtag_event_handler_t callback
, void *priv
)
270 jtag_event_callback_t
**callbacks_p
;
271 jtag_event_callback_t
**next
;
273 if (callback
== NULL
)
275 return ERROR_INVALID_ARGUMENTS
;
278 for (callbacks_p
= &jtag_event_callbacks
;
279 *callbacks_p
!= NULL
;
282 next
= &((*callbacks_p
)->next
);
284 if ((*callbacks_p
)->priv
!= priv
)
287 if ((*callbacks_p
)->callback
== callback
)
290 *callbacks_p
= *next
;
297 int jtag_call_event_callbacks(enum jtag_event event
)
299 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
301 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
305 jtag_event_callback_t
*next
;
307 /* callback may remove itself */
308 next
= callback
->next
;
309 callback
->callback(event
, callback
->priv
);
316 static void jtag_checks(void)
318 assert(jtag_trst
== 0);
321 static void jtag_prelude(tap_state_t state
)
325 assert(state
!= TAP_INVALID
);
327 cmd_queue_cur_state
= state
;
330 void jtag_alloc_in_value32(scan_field_t
*field
)
332 interface_jtag_alloc_in_value32(field
);
335 void jtag_add_ir_scan_noverify(int in_count
, const scan_field_t
*in_fields
,
340 int retval
= interface_jtag_add_ir_scan(in_count
, in_fields
, state
);
341 jtag_set_error(retval
);
345 void jtag_add_ir_scan(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
347 assert(state
!= TAP_RESET
);
349 if (jtag_verify
&& jtag_verify_capture_ir
)
351 /* 8 x 32 bit id's is enough for all invocations */
353 for (int j
= 0; j
< in_num_fields
; j
++)
355 /* if we are to run a verification of the ir scan, we need to get the input back.
356 * We may have to allocate space if the caller didn't ask for the input back.
358 in_fields
[j
].check_value
= in_fields
[j
].tap
->expected
;
359 in_fields
[j
].check_mask
= in_fields
[j
].tap
->expected_mask
;
361 jtag_add_scan_check(jtag_add_ir_scan_noverify
, in_num_fields
, in_fields
, state
);
364 jtag_add_ir_scan_noverify(in_num_fields
, in_fields
, state
);
368 void jtag_add_plain_ir_scan(int in_num_fields
, const scan_field_t
*in_fields
,
371 assert(state
!= TAP_RESET
);
375 int retval
= interface_jtag_add_plain_ir_scan(
376 in_num_fields
, in_fields
, state
);
377 jtag_set_error(retval
);
380 void jtag_add_callback(jtag_callback1_t f
, jtag_callback_data_t data0
)
382 interface_jtag_add_callback(f
, data0
);
385 void jtag_add_callback4(jtag_callback_t f
, jtag_callback_data_t data0
,
386 jtag_callback_data_t data1
, jtag_callback_data_t data2
,
387 jtag_callback_data_t data3
)
389 interface_jtag_add_callback4(f
, data0
, data1
, data2
, data3
);
392 static int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
,
393 uint8_t *in_check_mask
, int num_bits
);
395 static int jtag_check_value_mask_callback(jtag_callback_data_t data0
, jtag_callback_data_t data1
, jtag_callback_data_t data2
, jtag_callback_data_t data3
)
397 return jtag_check_value_inner((uint8_t *)data0
, (uint8_t *)data1
, (uint8_t *)data2
, (int)data3
);
400 static void jtag_add_scan_check(void (*jtag_add_scan
)(int in_num_fields
, const scan_field_t
*in_fields
, tap_state_t state
),
401 int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
403 for (int i
= 0; i
< in_num_fields
; i
++)
405 struct scan_field_s
*field
= &in_fields
[i
];
406 field
->allocated
= 0;
408 if (field
->check_value
|| field
->in_value
)
410 interface_jtag_add_scan_check_alloc(field
);
414 jtag_add_scan(in_num_fields
, in_fields
, state
);
416 for (int i
= 0; i
< in_num_fields
; i
++)
418 if ((in_fields
[i
].check_value
!= NULL
) && (in_fields
[i
].in_value
!= NULL
))
420 /* this is synchronous for a minidriver */
421 jtag_add_callback4(jtag_check_value_mask_callback
, (jtag_callback_data_t
)in_fields
[i
].in_value
,
422 (jtag_callback_data_t
)in_fields
[i
].check_value
,
423 (jtag_callback_data_t
)in_fields
[i
].check_mask
,
424 (jtag_callback_data_t
)in_fields
[i
].num_bits
);
426 if (in_fields
[i
].allocated
)
428 free(in_fields
[i
].in_value
);
430 if (in_fields
[i
].modified
)
432 in_fields
[i
].in_value
= NULL
;
437 void jtag_add_dr_scan_check(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
441 jtag_add_scan_check(jtag_add_dr_scan
, in_num_fields
, in_fields
, state
);
444 jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
449 void jtag_add_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
452 assert(state
!= TAP_RESET
);
457 retval
= interface_jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
458 jtag_set_error(retval
);
461 void jtag_add_plain_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
464 assert(state
!= TAP_RESET
);
469 retval
= interface_jtag_add_plain_dr_scan(in_num_fields
, in_fields
, state
);
470 jtag_set_error(retval
);
473 void jtag_add_dr_out(jtag_tap_t
* tap
,
474 int num_fields
, const int* num_bits
, const uint32_t* value
,
475 tap_state_t end_state
)
477 assert(end_state
!= TAP_RESET
);
478 assert(end_state
!= TAP_INVALID
);
480 cmd_queue_cur_state
= end_state
;
482 interface_jtag_add_dr_out(tap
,
483 num_fields
, num_bits
, value
,
487 void jtag_add_tlr(void)
489 jtag_prelude(TAP_RESET
);
490 jtag_set_error(interface_jtag_add_tlr());
492 /* NOTE: order here matches TRST path in jtag_add_reset() */
493 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
494 jtag_notify_event(JTAG_TRST_ASSERTED
);
497 void jtag_add_pathmove(int num_states
, const tap_state_t
*path
)
499 tap_state_t cur_state
= cmd_queue_cur_state
;
501 /* the last state has to be a stable state */
502 if (!tap_is_state_stable(path
[num_states
- 1]))
504 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
505 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
509 for (int i
= 0; i
< num_states
; i
++)
511 if (path
[i
] == TAP_RESET
)
513 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
514 jtag_set_error(ERROR_JTAG_STATE_INVALID
);
518 if (tap_state_transition(cur_state
, true) != path
[i
]
519 && tap_state_transition(cur_state
, false) != path
[i
])
521 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
522 tap_state_name(cur_state
), tap_state_name(path
[i
]));
523 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID
);
531 jtag_set_error(interface_jtag_add_pathmove(num_states
, path
));
532 cmd_queue_cur_state
= path
[num_states
- 1];
535 int jtag_add_statemove(tap_state_t goal_state
)
537 tap_state_t cur_state
= cmd_queue_cur_state
;
539 LOG_DEBUG("cur_state=%s goal_state=%s",
540 tap_state_name(cur_state
),
541 tap_state_name(goal_state
));
544 if (goal_state
== cur_state
)
545 ; /* nothing to do */
546 else if (goal_state
== TAP_RESET
)
550 else if (tap_is_state_stable(cur_state
) && tap_is_state_stable(goal_state
))
552 unsigned tms_bits
= tap_get_tms_path(cur_state
, goal_state
);
553 unsigned tms_count
= tap_get_tms_path_len(cur_state
, goal_state
);
554 tap_state_t moves
[8];
555 assert(tms_count
< DIM(moves
));
557 for (unsigned i
= 0; i
< tms_count
; i
++, tms_bits
>>= 1)
559 bool bit
= tms_bits
& 1;
561 cur_state
= tap_state_transition(cur_state
, bit
);
562 moves
[i
] = cur_state
;
565 jtag_add_pathmove(tms_count
, moves
);
567 else if (tap_state_transition(cur_state
, true) == goal_state
568 || tap_state_transition(cur_state
, false) == goal_state
)
570 jtag_add_pathmove(1, &goal_state
);
579 void jtag_add_runtest(int num_cycles
, tap_state_t state
)
582 jtag_set_error(interface_jtag_add_runtest(num_cycles
, state
));
586 void jtag_add_clocks(int num_cycles
)
588 if (!tap_is_state_stable(cmd_queue_cur_state
))
590 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
591 tap_state_name(cmd_queue_cur_state
));
592 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
599 jtag_set_error(interface_jtag_add_clocks(num_cycles
));
603 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
605 int trst_with_tlr
= 0;
609 /* Without SRST, we must use target-specific JTAG operations
610 * on each target; callers should not be requesting SRST when
611 * that signal doesn't exist.
613 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
614 * can kick in even if the JTAG adapter can't drive TRST.
617 if (!(jtag_reset_config
& RESET_HAS_SRST
)) {
618 LOG_ERROR("BUG: can't assert SRST");
619 jtag_set_error(ERROR_FAIL
);
622 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) != 0
623 && !req_tlr_or_trst
) {
624 LOG_ERROR("BUG: can't assert only SRST");
625 jtag_set_error(ERROR_FAIL
);
631 /* JTAG reset (entry to TAP_RESET state) can always be achieved
632 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
633 * state first. TRST accelerates it, and bypasses those states.
635 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
636 * can kick in even if the JTAG adapter can't drive SRST.
638 if (req_tlr_or_trst
) {
639 if (!(jtag_reset_config
& RESET_HAS_TRST
))
641 else if ((jtag_reset_config
& RESET_TRST_PULLS_SRST
) != 0
648 /* Maybe change TRST and/or SRST signal state */
649 if (jtag_srst
!= new_srst
|| jtag_trst
!= new_trst
) {
652 retval
= interface_jtag_add_reset(new_trst
, new_srst
);
653 if (retval
!= ERROR_OK
)
654 jtag_set_error(retval
);
656 retval
= jtag_execute_queue();
658 if (retval
!= ERROR_OK
) {
659 LOG_ERROR("TRST/SRST error %d", retval
);
664 /* SRST resets everything hooked up to that signal */
665 if (jtag_srst
!= new_srst
) {
666 jtag_srst
= new_srst
;
669 LOG_DEBUG("SRST line asserted");
670 if (jtag_nsrst_assert_width
)
671 jtag_add_sleep(jtag_nsrst_assert_width
* 1000);
674 LOG_DEBUG("SRST line released");
675 if (jtag_nsrst_delay
)
676 jtag_add_sleep(jtag_nsrst_delay
* 1000);
680 /* Maybe enter the JTAG TAP_RESET state ...
681 * - using only TMS, TCK, and the JTAG state machine
682 * - or else more directly, using TRST
684 * TAP_RESET should be invisible to non-debug parts of the system.
687 LOG_DEBUG("JTAG reset with TLR instead of TRST");
688 jtag_set_end_state(TAP_RESET
);
691 } else if (jtag_trst
!= new_trst
) {
692 jtag_trst
= new_trst
;
694 LOG_DEBUG("TRST line asserted");
695 tap_set_state(TAP_RESET
);
696 if (jtag_ntrst_assert_width
)
697 jtag_add_sleep(jtag_ntrst_assert_width
* 1000);
699 LOG_DEBUG("TRST line released");
700 if (jtag_ntrst_delay
)
701 jtag_add_sleep(jtag_ntrst_delay
* 1000);
703 /* We just asserted nTRST, so we're now in TAP_RESET.
704 * Inform possible listeners about this, now that
705 * JTAG instructions and data can be shifted. This
706 * sequence must match jtag_add_tlr().
708 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
709 jtag_notify_event(JTAG_TRST_ASSERTED
);
714 tap_state_t
jtag_set_end_state(tap_state_t state
)
716 if ((state
== TAP_DRSHIFT
)||(state
== TAP_IRSHIFT
))
718 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
721 if (state
!= TAP_INVALID
)
722 cmd_queue_end_state
= state
;
723 return cmd_queue_end_state
;
726 tap_state_t
jtag_get_end_state(void)
728 return cmd_queue_end_state
;
731 void jtag_add_sleep(uint32_t us
)
733 /// @todo Here, keep_alive() appears to be a layering violation!!!
735 jtag_set_error(interface_jtag_add_sleep(us
));
738 static int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
,
739 uint8_t *in_check_mask
, int num_bits
)
741 int retval
= ERROR_OK
;
743 int compare_failed
= 0;
746 compare_failed
= buf_cmp_mask(captured
, in_check_value
, in_check_mask
, num_bits
);
748 compare_failed
= buf_cmp(captured
, in_check_value
, num_bits
);
750 if (compare_failed
) {
751 char *captured_str
, *in_check_value_str
;
752 int bits
= (num_bits
> DEBUG_JTAG_IOZ
)
756 /* NOTE: we've lost diagnostic context here -- 'which tap' */
758 captured_str
= buf_to_str(captured
, bits
, 16);
759 in_check_value_str
= buf_to_str(in_check_value
, bits
, 16);
761 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
763 LOG_WARNING(" check_value: 0x%s", in_check_value_str
);
766 free(in_check_value_str
);
769 char *in_check_mask_str
;
771 in_check_mask_str
= buf_to_str(in_check_mask
, bits
, 16);
772 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str
);
773 free(in_check_mask_str
);
776 retval
= ERROR_JTAG_QUEUE_FAILED
;
781 void jtag_check_value_mask(scan_field_t
*field
, uint8_t *value
, uint8_t *mask
)
783 assert(field
->in_value
!= NULL
);
787 /* no checking to do */
791 jtag_execute_queue_noclear();
793 int retval
= jtag_check_value_inner(field
->in_value
, value
, mask
, field
->num_bits
);
794 jtag_set_error(retval
);
799 int default_interface_jtag_execute_queue(void)
803 LOG_ERROR("No JTAG interface configured yet. "
804 "Issue 'init' command in startup scripts "
805 "before communicating with targets.");
809 return jtag
->execute_queue();
812 void jtag_execute_queue_noclear(void)
814 jtag_flush_queue_count
++;
815 jtag_set_error(interface_jtag_execute_queue());
818 int jtag_get_flush_queue_count(void)
820 return jtag_flush_queue_count
;
823 int jtag_execute_queue(void)
825 jtag_execute_queue_noclear();
826 return jtag_error_clear();
829 static int jtag_reset_callback(enum jtag_event event
, void *priv
)
831 jtag_tap_t
*tap
= priv
;
833 if (event
== JTAG_TRST_ASSERTED
)
835 tap
->enabled
= !tap
->disabled_after_reset
;
837 /* current instruction is either BYPASS or IDCODE */
838 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
845 void jtag_sleep(uint32_t us
)
847 alive_sleep(us
/1000);
850 /* Maximum number of enabled JTAG devices we expect in the scan chain,
851 * plus one (to detect garbage at the end). Devices that don't support
852 * IDCODE take up fewer bits, possibly allowing a few more devices.
854 #define JTAG_MAX_CHAIN_SIZE 20
856 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
857 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
858 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
860 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
861 * know that no valid TAP will have it as an IDCODE value.
863 #define END_OF_CHAIN_FLAG 0x000000ff
865 static int jtag_examine_chain_execute(uint8_t *idcode_buffer
, unsigned num_idcode
)
867 scan_field_t field
= {
869 .num_bits
= num_idcode
* 32,
870 .out_value
= idcode_buffer
,
871 .in_value
= idcode_buffer
,
874 // initialize to the end of chain ID value
875 for (unsigned i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
876 buf_set_u32(idcode_buffer
, i
* 32, 32, END_OF_CHAIN_FLAG
);
878 jtag_add_plain_dr_scan(1, &field
, TAP_DRPAUSE
);
880 return jtag_execute_queue();
883 static bool jtag_examine_chain_check(uint8_t *idcodes
, unsigned count
)
885 uint8_t zero_check
= 0x0;
886 uint8_t one_check
= 0xff;
888 for (unsigned i
= 0; i
< count
* 4; i
++)
890 zero_check
|= idcodes
[i
];
891 one_check
&= idcodes
[i
];
894 /* if there wasn't a single non-zero bit or if all bits were one,
895 * the scan is not valid */
896 if (zero_check
== 0x00 || one_check
== 0xff)
898 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
899 (zero_check
== 0x00) ? "zeroes" : "ones");
900 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
906 static void jtag_examine_chain_display(enum log_levels level
, const char *msg
,
907 const char *name
, uint32_t idcode
)
909 log_printf_lf(level
, __FILE__
, __LINE__
, __FUNCTION__
,
910 "JTAG tap: %s %16.16s: 0x%08x "
911 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
913 (unsigned int)idcode
,
914 (unsigned int)EXTRACT_MFG(idcode
),
915 (unsigned int)EXTRACT_PART(idcode
),
916 (unsigned int)EXTRACT_VER(idcode
));
919 static bool jtag_idcode_is_final(uint32_t idcode
)
922 * Some devices, such as AVR8, will output all 1's instead
923 * of TDI input value at end of chain. Allow those values
924 * instead of failing.
926 return idcode
== END_OF_CHAIN_FLAG
|| idcode
== 0xFFFFFFFF;
930 * This helper checks that remaining bits in the examined chain data are
931 * all as expected, but a single JTAG device requires only 64 bits to be
932 * read back correctly. This can help identify and diagnose problems
933 * with the JTAG chain earlier, gives more helpful/explicit error messages.
934 * Returns TRUE iff garbage was found.
936 static bool jtag_examine_chain_end(uint8_t *idcodes
, unsigned count
, unsigned max
)
938 bool triggered
= false;
939 for (; count
< max
- 31; count
+= 32)
941 uint32_t idcode
= buf_get_u32(idcodes
, count
, 32);
942 // do not trigger the warning if the data looks good
943 if (!triggered
&& jtag_idcode_is_final(idcode
))
945 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
946 count
, (unsigned int)idcode
);
952 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s
*tap
)
954 /* ignore expected BYPASS codes; warn otherwise */
955 if (0 == tap
->expected_ids_cnt
&& !tap
->idcode
)
958 /* Loop over the expected identification codes and test for a match */
960 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
962 if (tap
->idcode
== tap
->expected_ids
[ii
])
965 /* treat "-expected-id 0" as a "don't-warn" wildcard */
966 if (0 == tap
->expected_ids
[ii
])
970 /* If none of the expected ids matched, warn */
971 jtag_examine_chain_display(LOG_LVL_WARNING
, "UNEXPECTED",
972 tap
->dotted_name
, tap
->idcode
);
973 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
976 snprintf(msg
, sizeof(msg
), "expected %hhu of %hhu",
977 ii
+ 1, tap
->expected_ids_cnt
);
978 jtag_examine_chain_display(LOG_LVL_ERROR
, msg
,
979 tap
->dotted_name
, tap
->expected_ids
[ii
]);
984 /* Try to examine chain layout according to IEEE 1149.1 §12
985 * This is called a "blind interrogation" of the scan chain.
987 static int jtag_examine_chain(void)
989 uint8_t idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
992 /* DR scan to collect BYPASS or IDCODE register contents.
993 * Then make sure the scan data has both ones and zeroes.
995 jtag_examine_chain_execute(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
);
996 if (!jtag_examine_chain_check(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
))
997 return ERROR_JTAG_INIT_FAILED
;
999 /* point at the 1st tap */
1000 jtag_tap_t
*tap
= jtag_tap_next_enabled(NULL
);
1003 LOG_ERROR("JTAG: No taps enabled?");
1004 return ERROR_JTAG_INIT_FAILED
;
1008 tap
&& bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;
1009 tap
= jtag_tap_next_enabled(tap
))
1011 uint32_t idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1013 if ((idcode
& 1) == 0)
1015 /* LSB must not be 0, this indicates a device in bypass */
1016 LOG_WARNING("TAP %s does not have IDCODE",
1019 tap
->hasidcode
= false;
1025 /* Friendly devices support IDCODE */
1026 tap
->hasidcode
= true;
1027 jtag_examine_chain_display(LOG_LVL_INFO
, "tap/device found",
1028 tap
->dotted_name
, idcode
);
1032 tap
->idcode
= idcode
;
1034 /* ensure the TAP ID matches what was expected */
1035 if (!jtag_examine_chain_match_tap(tap
))
1036 return ERROR_JTAG_INIT_FAILED
;
1039 /* Fail if too many TAPs were enabled for us to verify them all. */
1041 LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
1043 return ERROR_JTAG_INIT_FAILED
;
1046 /* After those IDCODE or BYPASS register values should be
1047 * only the data we fed into the scan chain.
1049 if (jtag_examine_chain_end(idcode_buffer
, bit_count
,
1050 8 * sizeof(idcode_buffer
))) {
1051 LOG_ERROR("double-check your JTAG setup (interface, "
1052 "speed, TAPs, ...)");
1053 return ERROR_JTAG_INIT_FAILED
;
1060 * Validate the date loaded by entry to the Capture-IR state, to help
1061 * find errors related to scan chain configuration (wrong IR lengths)
1064 * Entry state can be anything. On non-error exit, all TAPs are in
1065 * bypass mode. On error exits, the scan chain is reset.
1067 static int jtag_validate_ircapture(void)
1070 int total_ir_length
= 0;
1071 uint8_t *ir_test
= NULL
;
1077 for (tap
= NULL
, total_ir_length
= 0;
1078 (tap
= jtag_tap_next_enabled(tap
)) != NULL
;
1079 total_ir_length
+= tap
->ir_length
)
1082 /* increase length to add 2 bit sentinel after scan */
1083 total_ir_length
+= 2;
1085 ir_test
= malloc(CEIL(total_ir_length
, 8));
1086 if (ir_test
== NULL
)
1089 /* after this scan, all TAPs will capture BYPASS instructions */
1090 buf_set_ones(ir_test
, total_ir_length
);
1093 field
.num_bits
= total_ir_length
;
1094 field
.out_value
= ir_test
;
1095 field
.in_value
= ir_test
;
1097 jtag_add_plain_ir_scan(1, &field
, TAP_IDLE
);
1099 LOG_DEBUG("IR capture validation scan");
1100 retval
= jtag_execute_queue();
1101 if (retval
!= ERROR_OK
)
1108 tap
= jtag_tap_next_enabled(tap
);
1113 /* Validate the two LSBs, which must be 01 per JTAG spec.
1115 * Or ... more bits could be provided by TAP declaration.
1116 * Plus, some taps (notably in i.MX series chips) violate
1117 * this part of the JTAG spec, so their capture mask/value
1118 * attributes might disable this test.
1120 val
= buf_get_u32(ir_test
, chain_pos
, tap
->ir_length
);
1121 if ((val
& tap
->ir_capture_mask
) != tap
->ir_capture_value
) {
1122 LOG_ERROR("%s: IR capture error; saw 0x%0*x not 0x%0*x",
1124 (tap
->ir_length
+ 7) / tap
->ir_length
,
1126 (tap
->ir_length
+ 7) / tap
->ir_length
,
1127 tap
->ir_capture_value
);
1129 retval
= ERROR_JTAG_INIT_FAILED
;
1132 LOG_DEBUG("%s: IR capture 0x%0*x", jtag_tap_name(tap
),
1133 (tap
->ir_length
+ 7) / tap
->ir_length
, val
);
1134 chain_pos
+= tap
->ir_length
;
1137 /* verify the '11' sentinel we wrote is returned at the end */
1138 val
= buf_get_u32(ir_test
, chain_pos
, 2);
1141 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1143 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1146 retval
= ERROR_JTAG_INIT_FAILED
;
1151 if (retval
!= ERROR_OK
) {
1153 jtag_execute_queue();
1159 void jtag_tap_init(jtag_tap_t
*tap
)
1161 assert(0 != tap
->ir_length
);
1163 /// @todo fix, this allocates one byte per bit for all three fields!
1164 tap
->expected
= malloc(tap
->ir_length
);
1165 tap
->expected_mask
= malloc(tap
->ir_length
);
1166 tap
->cur_instr
= malloc(tap
->ir_length
);
1168 /// @todo cope sanely with ir_length bigger than 32 bits
1169 buf_set_u32(tap
->expected
, 0, tap
->ir_length
, tap
->ir_capture_value
);
1170 buf_set_u32(tap
->expected_mask
, 0, tap
->ir_length
, tap
->ir_capture_mask
);
1171 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1173 // place TAP in bypass mode
1175 // register the reset callback for the TAP
1176 jtag_register_event_callback(&jtag_reset_callback
, tap
);
1178 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1179 "irlen %d, capture: 0x%x mask: 0x%x", tap
->dotted_name
,
1180 tap
->abs_chain_position
, tap
->ir_length
,
1181 (unsigned) tap
->ir_capture_value
,
1182 (unsigned) tap
->ir_capture_mask
);
1186 void jtag_tap_free(jtag_tap_t
*tap
)
1188 jtag_unregister_event_callback(&jtag_reset_callback
, tap
);
1190 /// @todo is anything missing? no memory leaks please
1191 free((void *)tap
->expected
);
1192 free((void *)tap
->expected_ids
);
1193 free((void *)tap
->chip
);
1194 free((void *)tap
->tapname
);
1195 free((void *)tap
->dotted_name
);
1199 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1204 if (!jtag_interface
)
1206 /* nothing was previously specified by "interface" command */
1207 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1208 return ERROR_JTAG_INVALID_INTERFACE
;
1211 jtag
= jtag_interface
;
1212 if (jtag_interface
->init() != ERROR_OK
)
1215 return ERROR_JTAG_INIT_FAILED
;
1218 int requested_khz
= jtag_get_speed_khz();
1219 int actual_khz
= requested_khz
;
1220 int retval
= jtag_get_speed_readable(&actual_khz
);
1221 if (ERROR_OK
!= retval
)
1222 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1223 else if (actual_khz
)
1225 if ((CLOCK_MODE_RCLK
== clock_mode
)
1226 || ((CLOCK_MODE_KHZ
== clock_mode
) && !requested_khz
))
1228 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1232 LOG_INFO("clock speed %d kHz", actual_khz
);
1235 LOG_INFO("RCLK (adaptive clock speed)");
1240 int jtag_init_inner(struct command_context_s
*cmd_ctx
)
1244 bool issue_setup
= true;
1246 LOG_DEBUG("Init JTAG chain");
1248 tap
= jtag_tap_next_enabled(NULL
);
1250 LOG_ERROR("There are no enabled taps?");
1251 return ERROR_JTAG_INIT_FAILED
;
1255 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1258 /* examine chain first, as this could discover the real chain layout */
1259 if (jtag_examine_chain() != ERROR_OK
)
1261 LOG_ERROR("Trying to use configured scan chain anyway...");
1262 issue_setup
= false;
1265 if (jtag_validate_ircapture() != ERROR_OK
)
1267 LOG_WARNING("Errors during IR capture, continuing anyway...");
1268 issue_setup
= false;
1272 jtag_notify_event(JTAG_TAP_EVENT_SETUP
);
1274 LOG_WARNING("Bypassing JTAG setup events due to errors");
1280 int jtag_interface_quit(void)
1282 if (!jtag
|| !jtag
->quit
)
1285 // close the JTAG interface
1286 int result
= jtag
->quit();
1287 if (ERROR_OK
!= result
)
1288 LOG_ERROR("failed: %d", result
);
1294 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
1298 if ((retval
= jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1301 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1303 /* Reset can happen after a power cycle.
1305 * Ideally we would only assert TRST or run TLR before the target reset.
1307 * However w/srst_pulls_trst, trst is asserted together with the target
1308 * reset whether we want it or not.
1310 * NB! Some targets have JTAG circuitry disabled until a
1311 * trst & srst has been asserted.
1313 * NB! here we assume nsrst/ntrst delay are sufficient!
1315 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1318 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1319 if (jtag_reset_config
& RESET_HAS_SRST
)
1321 jtag_add_reset(1, 1);
1322 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) == 0)
1323 jtag_add_reset(0, 1);
1325 jtag_add_reset(0, 0);
1326 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1329 /* Check that we can communication on the JTAG chain + eventually we want to
1330 * be able to perform enumeration only after OpenOCD has started
1331 * telnet and GDB server
1333 * That would allow users to more easily perform any magic they need to before
1336 return jtag_init_inner(cmd_ctx
);
1339 int jtag_init(struct command_context_s
*cmd_ctx
)
1343 if ((retval
= jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1346 /* guard against oddball hardware: force resets to be inactive */
1347 jtag_add_reset(0, 0);
1348 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1351 if (Jim_Eval_Named(interp
, "jtag_init", __FILE__
, __LINE__
) != JIM_OK
)
1357 unsigned jtag_get_speed_khz(void)
1362 static int jtag_khz_to_speed(unsigned khz
, int* speed
)
1364 LOG_DEBUG("convert khz to interface specific speed value");
1368 LOG_DEBUG("have interface set up");
1370 int retval
= jtag
->khz(jtag_get_speed_khz(), &speed_div1
);
1371 if (ERROR_OK
!= retval
)
1375 *speed
= speed_div1
;
1380 static int jtag_rclk_to_speed(unsigned fallback_speed_khz
, int* speed
)
1382 int retval
= jtag_khz_to_speed(0, speed
);
1383 if ((ERROR_OK
!= retval
) && fallback_speed_khz
)
1385 LOG_DEBUG("trying fallback speed...");
1386 retval
= jtag_khz_to_speed(fallback_speed_khz
, speed
);
1391 static int jtag_set_speed(int speed
)
1394 /* this command can be called during CONFIG,
1395 * in which case jtag isn't initialized */
1396 return jtag
? jtag
->speed(speed
) : ERROR_OK
;
1399 int jtag_config_speed(int speed
)
1401 LOG_DEBUG("handle jtag speed");
1402 clock_mode
= CLOCK_MODE_SPEED
;
1403 return jtag_set_speed(speed
);
1406 int jtag_config_khz(unsigned khz
)
1408 LOG_DEBUG("handle jtag khz");
1409 clock_mode
= CLOCK_MODE_KHZ
;
1411 int retval
= jtag_khz_to_speed(khz
, &speed
);
1412 return (ERROR_OK
!= retval
) ? retval
: jtag_set_speed(speed
);
1415 int jtag_config_rclk(unsigned fallback_speed_khz
)
1417 LOG_DEBUG("handle jtag rclk");
1418 clock_mode
= CLOCK_MODE_RCLK
;
1419 rclk_fallback_speed_khz
= fallback_speed_khz
;
1421 int retval
= jtag_rclk_to_speed(fallback_speed_khz
, &speed
);
1422 return (ERROR_OK
!= retval
) ? retval
: jtag_set_speed(speed
);
1425 int jtag_get_speed(void)
1430 case CLOCK_MODE_SPEED
:
1433 case CLOCK_MODE_KHZ
:
1434 jtag_khz_to_speed(jtag_get_speed_khz(), &speed
);
1436 case CLOCK_MODE_RCLK
:
1437 jtag_rclk_to_speed(rclk_fallback_speed_khz
, &speed
);
1440 LOG_ERROR("BUG: unknown jtag clock mode");
1447 int jtag_get_speed_readable(int *khz
)
1449 return jtag
? jtag
->speed_div(jtag_get_speed(), khz
) : ERROR_OK
;
1452 void jtag_set_verify(bool enable
)
1454 jtag_verify
= enable
;
1457 bool jtag_will_verify()
1462 void jtag_set_verify_capture_ir(bool enable
)
1464 jtag_verify_capture_ir
= enable
;
1467 bool jtag_will_verify_capture_ir()
1469 return jtag_verify_capture_ir
;
1472 int jtag_power_dropout(int *dropout
)
1474 return jtag
->power_dropout(dropout
);
1477 int jtag_srst_asserted(int *srst_asserted
)
1479 return jtag
->srst_asserted(srst_asserted
);
1482 enum reset_types
jtag_get_reset_config(void)
1484 return jtag_reset_config
;
1486 void jtag_set_reset_config(enum reset_types type
)
1488 jtag_reset_config
= type
;
1491 int jtag_get_trst(void)
1495 int jtag_get_srst(void)
1500 void jtag_set_nsrst_delay(unsigned delay
)
1502 jtag_nsrst_delay
= delay
;
1504 unsigned jtag_get_nsrst_delay(void)
1506 return jtag_nsrst_delay
;
1508 void jtag_set_ntrst_delay(unsigned delay
)
1510 jtag_ntrst_delay
= delay
;
1512 unsigned jtag_get_ntrst_delay(void)
1514 return jtag_ntrst_delay
;
1518 void jtag_set_nsrst_assert_width(unsigned delay
)
1520 jtag_nsrst_assert_width
= delay
;
1522 unsigned jtag_get_nsrst_assert_width(void)
1524 return jtag_nsrst_assert_width
;
1526 void jtag_set_ntrst_assert_width(unsigned delay
)
1528 jtag_ntrst_assert_width
= delay
;
1530 unsigned jtag_get_ntrst_assert_width(void)
1532 return jtag_ntrst_assert_width
;
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)