1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
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
);
49 /* note that this is not marked as static as it must be available from outside jtag.c for those
50 that implement the jtag_xxx() minidriver layer
52 int jtag_error
=ERROR_OK
;
54 char* jtag_event_strings
[] =
56 "JTAG controller reset (RESET or TRST)"
59 const Jim_Nvp nvp_jtag_tap_event
[] = {
60 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
61 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
63 { .name
= NULL
, .value
= -1 }
70 * List all TAPs that have been created.
72 static jtag_tap_t
*__jtag_all_taps
= NULL
;
74 * The number of TAPs in the __jtag_all_taps list, used to track the
75 * assigned chain position to new TAPs
77 static unsigned jtag_num_taps
= 0;
79 enum reset_types jtag_reset_config
= RESET_NONE
;
80 tap_state_t cmd_queue_end_state
= TAP_RESET
;
81 tap_state_t cmd_queue_cur_state
= TAP_RESET
;
83 int jtag_verify_capture_ir
= 1;
86 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
87 static int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
88 static int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
90 /* callbacks to inform high-level handlers about JTAG state changes */
91 jtag_event_callback_t
*jtag_event_callbacks
;
94 static int speed_khz
= 0;
95 /* flag if the kHz speed was defined */
98 struct jtag_interface_s
*jtag
= NULL
;
101 jtag_interface_t
*jtag_interface
= NULL
;
104 jtag_tap_t
*jtag_all_taps(void)
106 return __jtag_all_taps
;
109 unsigned jtag_tap_count(void)
111 return jtag_num_taps
;
114 unsigned jtag_tap_count_enabled(void)
116 jtag_tap_t
*t
= jtag_all_taps();
127 /// Append a new TAP to the chain of all taps.
128 void jtag_tap_add(struct jtag_tap_s
*t
)
130 t
->abs_chain_position
= jtag_num_taps
++;
132 jtag_tap_t
**tap
= &__jtag_all_taps
;
134 tap
= &(*tap
)->next_tap
;
138 jtag_tap_t
*jtag_tap_by_string(const char *s
)
140 /* try by name first */
141 jtag_tap_t
*t
= jtag_all_taps();
144 if (0 == strcmp(t
->dotted_name
, s
))
149 /* no tap found by name, so try to parse the name as a number */
151 unsigned n
= strtoul(s
, &cp
, 0);
152 if ((s
== cp
) || (*cp
!= 0))
155 return jtag_tap_by_position(n
);
158 jtag_tap_t
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
160 const char *cp
= Jim_GetString(o
, NULL
);
161 jtag_tap_t
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
165 Jim_SetResult_sprintf(interp
, "Tap '%s' could not be found", cp
);
169 /* returns a pointer to the n-th device in the scan chain */
170 jtag_tap_t
*jtag_tap_by_position(unsigned n
)
172 jtag_tap_t
*t
= jtag_all_taps();
180 const char *jtag_tap_name(const jtag_tap_t
*tap
)
182 return (tap
== NULL
) ? "(unknown)" : tap
->dotted_name
;
186 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
188 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
190 if (callback
== NULL
)
192 return ERROR_INVALID_ARGUMENTS
;
197 while ((*callbacks_p
)->next
)
198 callbacks_p
= &((*callbacks_p
)->next
);
199 callbacks_p
= &((*callbacks_p
)->next
);
202 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
203 (*callbacks_p
)->callback
= callback
;
204 (*callbacks_p
)->priv
= priv
;
205 (*callbacks_p
)->next
= NULL
;
210 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
212 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
214 if (callback
== NULL
)
216 return ERROR_INVALID_ARGUMENTS
;
221 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
222 if ((*callbacks_p
)->callback
== callback
)
225 *callbacks_p
= *next
;
233 int jtag_call_event_callbacks(enum jtag_event event
)
235 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
237 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
241 callback
->callback(event
, callback
->priv
);
242 callback
= callback
->next
;
248 static void jtag_checks(void)
250 assert(jtag_trst
== 0);
253 static void jtag_prelude(tap_state_t state
)
257 assert(state
!=TAP_INVALID
);
259 cmd_queue_cur_state
= state
;
262 void jtag_alloc_in_value32(scan_field_t
*field
)
264 interface_jtag_alloc_in_value32(field
);
267 void jtag_add_ir_scan_noverify(int in_count
, const scan_field_t
*in_fields
,
272 int retval
= interface_jtag_add_ir_scan(in_count
, in_fields
, state
);
273 jtag_set_error(retval
);
277 void jtag_add_ir_scan(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
279 if (jtag_verify
&&jtag_verify_capture_ir
)
281 /* 8 x 32 bit id's is enough for all invocations */
283 for (int j
= 0; j
< in_num_fields
; j
++)
285 /* if we are to run a verification of the ir scan, we need to get the input back.
286 * We may have to allocate space if the caller didn't ask for the input back.
288 in_fields
[j
].check_value
=in_fields
[j
].tap
->expected
;
289 in_fields
[j
].check_mask
=in_fields
[j
].tap
->expected_mask
;
291 jtag_add_scan_check(jtag_add_ir_scan_noverify
, in_num_fields
, in_fields
, state
);
294 jtag_add_ir_scan_noverify(in_num_fields
, in_fields
, state
);
298 void jtag_add_plain_ir_scan(int in_num_fields
, const scan_field_t
*in_fields
,
303 int retval
= interface_jtag_add_plain_ir_scan(
304 in_num_fields
, in_fields
, state
);
305 jtag_set_error(retval
);
308 void jtag_add_callback(jtag_callback1_t f
, u8
*in
)
310 interface_jtag_add_callback(f
, in
);
313 void jtag_add_callback4(jtag_callback_t f
, u8
*in
,
314 jtag_callback_data_t data1
, jtag_callback_data_t data2
,
315 jtag_callback_data_t data3
)
317 interface_jtag_add_callback4(f
, in
, data1
, data2
, data3
);
320 int jtag_check_value_inner(u8
*captured
, u8
*in_check_value
, u8
*in_check_mask
, int num_bits
);
322 static int jtag_check_value_mask_callback(u8
*in
, jtag_callback_data_t data1
, jtag_callback_data_t data2
, jtag_callback_data_t data3
)
324 return jtag_check_value_inner(in
, (u8
*)data1
, (u8
*)data2
, (int)data3
);
327 static void jtag_add_scan_check(void (*jtag_add_scan
)(int in_num_fields
, const scan_field_t
*in_fields
, tap_state_t state
),
328 int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
330 for (int i
= 0; i
< in_num_fields
; i
++)
332 struct scan_field_s
*field
= &in_fields
[i
];
333 field
->allocated
= 0;
335 if (field
->check_value
|| field
->in_value
)
337 interface_jtag_add_scan_check_alloc(field
);
341 jtag_add_scan(in_num_fields
, in_fields
, state
);
343 for (int i
= 0; i
< in_num_fields
; i
++)
345 if ((in_fields
[i
].check_value
!= NULL
) && (in_fields
[i
].in_value
!= NULL
))
347 /* this is synchronous for a minidriver */
348 jtag_add_callback4(jtag_check_value_mask_callback
, in_fields
[i
].in_value
,
349 (jtag_callback_data_t
)in_fields
[i
].check_value
,
350 (jtag_callback_data_t
)in_fields
[i
].check_mask
,
351 (jtag_callback_data_t
)in_fields
[i
].num_bits
);
353 if (in_fields
[i
].allocated
)
355 free(in_fields
[i
].in_value
);
357 if (in_fields
[i
].modified
)
359 in_fields
[i
].in_value
= NULL
;
364 void jtag_add_dr_scan_check(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
368 jtag_add_scan_check(jtag_add_dr_scan
, in_num_fields
, in_fields
, state
);
371 jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
376 void jtag_add_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
382 retval
= interface_jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
383 jtag_set_error(retval
);
386 void jtag_add_plain_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
392 retval
= interface_jtag_add_plain_dr_scan(in_num_fields
, in_fields
, state
);
393 jtag_set_error(retval
);
396 void jtag_add_dr_out(jtag_tap_t
* tap
,
397 int num_fields
, const int* num_bits
, const u32
* value
,
398 tap_state_t end_state
)
400 assert(end_state
!= TAP_INVALID
);
402 cmd_queue_cur_state
= end_state
;
404 interface_jtag_add_dr_out(tap
,
405 num_fields
, num_bits
, value
,
409 void jtag_add_tlr(void)
411 jtag_prelude(TAP_RESET
);
412 jtag_set_error(interface_jtag_add_tlr());
413 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
416 void jtag_add_pathmove(int num_states
, const tap_state_t
*path
)
418 tap_state_t cur_state
= cmd_queue_cur_state
;
420 /* the last state has to be a stable state */
421 if (!tap_is_state_stable(path
[num_states
- 1]))
423 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
424 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
428 for (int i
= 0; i
< num_states
; i
++)
430 if (path
[i
] == TAP_RESET
)
432 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
433 jtag_set_error(ERROR_JTAG_STATE_INVALID
);
437 if ( tap_state_transition(cur_state
, true) != path
[i
]
438 && tap_state_transition(cur_state
, false) != path
[i
])
440 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
441 tap_state_name(cur_state
), tap_state_name(path
[i
]));
442 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID
);
450 jtag_set_error(interface_jtag_add_pathmove(num_states
, path
));
451 cmd_queue_cur_state
= path
[num_states
- 1];
454 void jtag_add_runtest(int num_cycles
, tap_state_t state
)
457 jtag_set_error(interface_jtag_add_runtest(num_cycles
, state
));
461 void jtag_add_clocks(int num_cycles
)
463 if (!tap_is_state_stable(cmd_queue_cur_state
))
465 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
466 tap_state_name(cmd_queue_cur_state
));
467 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
474 jtag_set_error(interface_jtag_add_clocks(num_cycles
));
478 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
480 int trst_with_tlr
= 0;
482 /* FIX!!! there are *many* different cases here. A better
483 * approach is needed for legal combinations of transitions...
485 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
486 (jtag_reset_config
& RESET_HAS_TRST
)&&
487 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
489 if (((req_tlr_or_trst
&&!jtag_trst
)||
490 (!req_tlr_or_trst
&&jtag_trst
))&&
491 ((req_srst
&&!jtag_srst
)||
492 (!req_srst
&&jtag_srst
)))
494 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
495 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
499 /* Make sure that jtag_reset_config allows the requested reset */
500 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
501 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
503 LOG_ERROR("BUG: requested reset would assert trst");
504 jtag_set_error(ERROR_FAIL
);
508 /* if TRST pulls SRST, we reset with TAP T-L-R */
509 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
514 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
516 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
517 jtag_set_error(ERROR_FAIL
);
523 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
535 jtag_srst
= req_srst
;
537 int retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
538 if (retval
!= ERROR_OK
)
540 jtag_set_error(retval
);
543 jtag_execute_queue();
547 LOG_DEBUG("SRST line asserted");
551 LOG_DEBUG("SRST line released");
552 if (jtag_nsrst_delay
)
553 jtag_add_sleep(jtag_nsrst_delay
* 1000);
558 LOG_DEBUG("JTAG reset with RESET instead of TRST");
559 jtag_set_end_state(TAP_RESET
);
566 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
567 * and inform possible listeners about this
569 LOG_DEBUG("TRST line asserted");
570 tap_set_state(TAP_RESET
);
571 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
575 if (jtag_ntrst_delay
)
576 jtag_add_sleep(jtag_ntrst_delay
* 1000);
580 tap_state_t
jtag_set_end_state(tap_state_t state
)
582 if ((state
== TAP_DRSHIFT
)||(state
== TAP_IRSHIFT
))
584 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
587 if (state
!=TAP_INVALID
)
588 cmd_queue_end_state
= state
;
589 return cmd_queue_end_state
;
592 tap_state_t
jtag_get_end_state(void)
594 return cmd_queue_end_state
;
597 void jtag_add_sleep(u32 us
)
599 /// @todo Here, keep_alive() appears to be a layering violation!!!
601 jtag_set_error(interface_jtag_add_sleep(us
));
604 int jtag_check_value_inner(u8
*captured
, u8
*in_check_value
, u8
*in_check_mask
, int num_bits
)
606 int retval
= ERROR_OK
;
608 int compare_failed
= 0;
611 compare_failed
= buf_cmp_mask(captured
, in_check_value
, in_check_mask
, num_bits
);
613 compare_failed
= buf_cmp(captured
, in_check_value
, num_bits
);
616 /* An error handler could have caught the failing check
617 * only report a problem when there wasn't a handler, or if the handler
618 * acknowledged the error
621 LOG_WARNING("TAP %s:",
622 jtag_tap_name(field->tap));
626 char *captured_char
= buf_to_str(captured
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
627 char *in_check_value_char
= buf_to_str(in_check_value
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
631 char *in_check_mask_char
;
632 in_check_mask_char
= buf_to_str(in_check_mask
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
633 LOG_WARNING("value captured during scan didn't pass the requested check:");
634 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
635 captured_char
, in_check_value_char
, in_check_mask_char
);
636 free(in_check_mask_char
);
640 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char
, in_check_value_char
);
644 free(in_check_value_char
);
646 retval
= ERROR_JTAG_QUEUE_FAILED
;
653 void jtag_check_value_mask(scan_field_t
*field
, u8
*value
, u8
*mask
)
655 assert(field
->in_value
!= NULL
);
659 /* no checking to do */
663 jtag_execute_queue_noclear();
665 int retval
=jtag_check_value_inner(field
->in_value
, value
, mask
, field
->num_bits
);
666 jtag_set_error(retval
);
671 int default_interface_jtag_execute_queue(void)
675 LOG_ERROR("No JTAG interface configured yet. "
676 "Issue 'init' command in startup scripts "
677 "before communicating with targets.");
681 return jtag
->execute_queue();
684 void jtag_execute_queue_noclear(void)
686 jtag_flush_queue_count
++;
687 jtag_set_error(interface_jtag_execute_queue());
690 int jtag_get_flush_queue_count(void)
692 return jtag_flush_queue_count
;
695 int jtag_execute_queue(void)
697 jtag_execute_queue_noclear();
698 return jtag_error_clear();
701 static int jtag_reset_callback(enum jtag_event event
, void *priv
)
703 jtag_tap_t
*tap
= priv
;
707 if (event
== JTAG_TRST_ASSERTED
)
709 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
716 void jtag_sleep(u32 us
)
718 alive_sleep(us
/1000);
721 /// maximum number of JTAG devices expected in the chain
722 #define JTAG_MAX_CHAIN_SIZE 20
724 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
725 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
726 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
728 static int jtag_examine_chain_execute(u8
*idcode_buffer
, unsigned num_idcode
)
730 scan_field_t field
= {
732 .num_bits
= num_idcode
* 32,
733 .out_value
= idcode_buffer
,
734 .in_value
= idcode_buffer
,
737 // initialize to the end of chain ID value
738 for (unsigned i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
739 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
741 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
742 return jtag_execute_queue();
745 static bool jtag_examine_chain_check(u8
*idcodes
, unsigned count
)
750 for (unsigned i
= 0; i
< count
* 4; i
++)
752 zero_check
|= idcodes
[i
];
753 one_check
&= idcodes
[i
];
756 /* if there wasn't a single non-zero bit or if all bits were one,
757 * the scan is not valid */
758 if (zero_check
== 0x00 || one_check
== 0xff)
760 LOG_ERROR("JTAG communication failure: check connection, "
761 "JTAG interface, target power etc.");
767 static void jtag_examine_chain_display(enum log_levels level
, const char *msg
,
768 const char *name
, u32 idcode
)
770 log_printf_lf(level
, __FILE__
, __LINE__
, __FUNCTION__
,
771 "JTAG tap: %s %16.16s: 0x%08x "
772 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
774 EXTRACT_MFG(idcode
), EXTRACT_PART(idcode
), EXTRACT_VER(idcode
) );
777 static bool jtag_idcode_is_final(u32 idcode
)
779 return idcode
== 0x000000FF || idcode
== 0xFFFFFFFF;
783 * This helper checks that remaining bits in the examined chain data are
784 * all as expected, but a single JTAG device requires only 64 bits to be
785 * read back correctly. This can help identify and diagnose problems
786 * with the JTAG chain earlier, gives more helpful/explicit error messages.
788 static void jtag_examine_chain_end(u8
*idcodes
, unsigned count
, unsigned max
)
790 bool triggered
= false;
791 for ( ; count
< max
- 31; count
+= 32)
793 u32 idcode
= buf_get_u32(idcodes
, count
, 32);
794 // do not trigger the warning if the data looks good
795 if (!triggered
&& jtag_idcode_is_final(idcode
))
797 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
803 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s
*tap
)
805 if (0 == tap
->expected_ids_cnt
)
807 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
809 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap
->idcode
)
810 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
815 /* Loop over the expected identification codes and test for a match */
817 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
819 if (tap
->idcode
== tap
->expected_ids
[ii
])
823 /* If none of the expected ids matched, log an error */
824 if (ii
!= tap
->expected_ids_cnt
)
826 LOG_INFO("JTAG Tap/device matched");
829 jtag_examine_chain_display(LOG_LVL_ERROR
, "got",
830 tap
->dotted_name
, tap
->idcode
);
831 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
834 snprintf(msg
, sizeof(msg
), "expected %hhu of %hhu",
835 ii
+ 1, tap
->expected_ids_cnt
);
836 jtag_examine_chain_display(LOG_LVL_ERROR
, msg
,
837 tap
->dotted_name
, tap
->expected_ids
[ii
]);
842 /* Try to examine chain layout according to IEEE 1149.1 §12
844 int jtag_examine_chain(void)
846 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
847 unsigned device_count
= 0;
849 jtag_examine_chain_execute(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
);
851 if (!jtag_examine_chain_check(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
))
852 return ERROR_JTAG_INIT_FAILED
;
854 /* point at the 1st tap */
855 jtag_tap_t
*tap
= jtag_tap_next_enabled(NULL
);
858 LOG_ERROR("JTAG: No taps enabled?");
859 return ERROR_JTAG_INIT_FAILED
;
862 for (unsigned bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
864 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
865 if ((idcode
& 1) == 0)
867 /* LSB must not be 0, this indicates a device in bypass */
868 LOG_WARNING("Tap/Device does not have IDCODE");
876 * End of chain (invalid manufacturer ID) some devices, such
877 * as AVR will output all 1's instead of TDI input value at
880 if (jtag_idcode_is_final(idcode
))
882 jtag_examine_chain_end(idcode_buffer
,
883 bit_count
+ 32, JTAG_MAX_CHAIN_SIZE
* 32);
887 jtag_examine_chain_display(LOG_LVL_INFO
, "tap/device found",
888 tap
? tap
->dotted_name
: "(not-named)",
897 tap
->idcode
= idcode
;
899 // ensure the TAP ID does matches what was expected
900 if (!jtag_examine_chain_match_tap(tap
))
901 return ERROR_JTAG_INIT_FAILED
;
903 tap
= jtag_tap_next_enabled(tap
);
906 /* see if number of discovered devices matches configuration */
907 if (device_count
!= jtag_tap_count_enabled())
909 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
910 "does not match (enabled) configuration (%i), total taps: %d",
911 device_count
, jtag_tap_count_enabled(), jtag_tap_count());
912 LOG_ERROR("check the config file and ensure proper JTAG communication"
913 " (connections, speed, ...)");
914 return ERROR_JTAG_INIT_FAILED
;
920 int jtag_validate_chain(void)
923 int total_ir_length
= 0;
931 tap
= jtag_tap_next_enabled(tap
);
935 total_ir_length
+= tap
->ir_length
;
938 total_ir_length
+= 2;
939 ir_test
= malloc(CEIL(total_ir_length
, 8));
940 buf_set_ones(ir_test
, total_ir_length
);
943 field
.num_bits
= total_ir_length
;
944 field
.out_value
= ir_test
;
945 field
.in_value
= ir_test
;
948 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
949 jtag_execute_queue();
955 tap
= jtag_tap_next_enabled(tap
);
960 val
= buf_get_u32(ir_test
, chain_pos
, 2);
963 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
964 LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf
, jtag_tap_name(tap
), chain_pos
, val
);
967 return ERROR_JTAG_INIT_FAILED
;
969 chain_pos
+= tap
->ir_length
;
972 val
= buf_get_u32(ir_test
, chain_pos
, 2);
975 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
976 LOG_ERROR("Could not validate end of JTAG scan chain, IR mismatch, scan returned 0x%s. pos=%d expected 0x3 got %0x", cbuf
, chain_pos
, val
);
979 return ERROR_JTAG_INIT_FAILED
;
988 void jtag_tap_init(jtag_tap_t
*tap
)
990 assert(0 != tap
->ir_length
);
992 tap
->expected
= malloc(tap
->ir_length
);
993 tap
->expected_mask
= malloc(tap
->ir_length
);
994 tap
->cur_instr
= malloc(tap
->ir_length
);
996 buf_set_u32(tap
->expected
, 0, tap
->ir_length
, tap
->ir_capture_value
);
997 buf_set_u32(tap
->expected_mask
, 0, tap
->ir_length
, tap
->ir_capture_mask
);
998 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1000 // place TAP in bypass mode
1002 // register the reset callback for the TAP
1003 jtag_register_event_callback(&jtag_reset_callback
, tap
);
1005 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1006 "irlen %d, capture: 0x%x mask: 0x%x", tap
->dotted_name
,
1007 tap
->abs_chain_position
, tap
->ir_length
,
1008 tap
->ir_capture_value
, tap
->ir_capture_mask
);
1012 void jtag_tap_free(jtag_tap_t
*tap
)
1014 /// @todo is anything missing? no memory leaks please
1015 free((void *)tap
->expected_ids
);
1016 free((void *)tap
->chip
);
1017 free((void *)tap
->tapname
);
1018 free((void *)tap
->dotted_name
);
1022 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1027 if (!jtag_interface
)
1029 /* nothing was previously specified by "interface" command */
1030 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1031 return ERROR_JTAG_INVALID_INTERFACE
;
1035 jtag_interface
->khz(jtag_get_speed_khz(), &jtag_speed
);
1039 if (jtag_interface
->init() != ERROR_OK
)
1040 return ERROR_JTAG_INIT_FAILED
;
1042 jtag
= jtag_interface
;
1046 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
1051 LOG_DEBUG("Init JTAG chain");
1053 tap
= jtag_tap_next_enabled(NULL
);
1055 LOG_ERROR("There are no enabled taps?");
1056 return ERROR_JTAG_INIT_FAILED
;
1060 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
1063 /* examine chain first, as this could discover the real chain layout */
1064 if (jtag_examine_chain() != ERROR_OK
)
1066 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1069 if (jtag_validate_chain() != ERROR_OK
)
1071 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1077 int jtag_interface_quit(void)
1079 if (!jtag
|| !jtag
->quit
)
1082 // close the JTAG interface
1083 int result
= jtag
->quit();
1084 if (ERROR_OK
!= result
)
1085 LOG_ERROR("failed: %d", result
);
1091 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
1095 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1098 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
1100 /* Reset can happen after a power cycle.
1102 * Ideally we would only assert TRST or run RESET before the target reset.
1104 * However w/srst_pulls_trst, trst is asserted together with the target
1105 * reset whether we want it or not.
1107 * NB! Some targets have JTAG circuitry disabled until a
1108 * trst & srst has been asserted.
1110 * NB! here we assume nsrst/ntrst delay are sufficient!
1112 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1115 jtag_add_reset(1, 0); /* RESET or TRST */
1116 if (jtag_reset_config
& RESET_HAS_SRST
)
1118 jtag_add_reset(1, 1);
1119 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
1120 jtag_add_reset(0, 1);
1122 jtag_add_reset(0, 0);
1123 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1126 /* Check that we can communication on the JTAG chain + eventually we want to
1127 * be able to perform enumeration only after OpenOCD has started
1128 * telnet and GDB server
1130 * That would allow users to more easily perform any magic they need to before
1133 return jtag_init_inner(cmd_ctx
);
1136 int jtag_init(struct command_context_s
*cmd_ctx
)
1139 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1141 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
1145 return jtag_init_reset(cmd_ctx
);
1148 void jtag_set_speed_khz(unsigned khz
)
1152 unsigned jtag_get_speed_khz(void)
1157 void jtag_set_verify(bool enable
)
1159 jtag_verify
= enable
;
1162 bool jtag_will_verify()
1168 int jtag_power_dropout(int *dropout
)
1170 return jtag
->power_dropout(dropout
);
1173 int jtag_srst_asserted(int *srst_asserted
)
1175 return jtag
->srst_asserted(srst_asserted
);
1178 void jtag_tap_handle_event( jtag_tap_t
* tap
, enum jtag_tap_event e
)
1180 jtag_tap_event_action_t
* jteap
;
1183 jteap
= tap
->event_action
;
1187 if (jteap
->event
== e
) {
1189 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
1192 Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
)->name
,
1193 Jim_GetString(jteap
->body
, NULL
) );
1194 if (Jim_EvalObj(interp
, jteap
->body
) != JIM_OK
) {
1195 Jim_PrintErrorMessage(interp
);
1199 jteap
= jteap
->next
;
1203 LOG_DEBUG( "event %d %s - no action",
1205 Jim_Nvp_value2name_simple( nvp_jtag_tap_event
, e
)->name
);
1209 int jtag_add_statemove(tap_state_t goal_state
)
1211 tap_state_t cur_state
= cmd_queue_cur_state
;
1213 LOG_DEBUG( "cur_state=%s goal_state=%s",
1214 tap_state_name(cur_state
),
1215 tap_state_name(goal_state
) );
1218 if (goal_state
==cur_state
)
1219 ; /* nothing to do */
1220 else if( goal_state
==TAP_RESET
)
1224 else if( tap_is_state_stable(cur_state
) && tap_is_state_stable(goal_state
) )
1226 unsigned tms_bits
= tap_get_tms_path(cur_state
, goal_state
);
1227 unsigned tms_count
= tap_get_tms_path_len(cur_state
, goal_state
);
1228 tap_state_t moves
[8];
1229 assert(tms_count
< DIM(moves
));
1231 for (unsigned i
= 0; i
< tms_count
; i
++, tms_bits
>>= 1)
1233 bool bit
= tms_bits
& 1;
1235 cur_state
= tap_state_transition(cur_state
, bit
);
1236 moves
[i
] = cur_state
;
1239 jtag_add_pathmove(tms_count
, moves
);
1241 else if( tap_state_transition(cur_state
, true) == goal_state
1242 || tap_state_transition(cur_state
, false) == goal_state
)
1244 jtag_add_pathmove(1, &goal_state
);
1253 void jtag_set_nsrst_delay(unsigned delay
)
1255 jtag_nsrst_delay
= delay
;
1257 unsigned jtag_get_nsrst_delay(void)
1259 return jtag_nsrst_delay
;
1261 void jtag_set_ntrst_delay(unsigned delay
)
1263 jtag_ntrst_delay
= delay
;
1265 unsigned jtag_get_ntrst_delay(void)
1267 return jtag_ntrst_delay
;
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)