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
);
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
] = "JTAG controller reset (TLR or TRST)",
64 [JTAG_TAP_EVENT_ENABLE
] = "TAP enabled",
65 [JTAG_TAP_EVENT_DISABLE
] = "TAP disabled",
69 * JTAG adapters must initialize with TRST and SRST de-asserted
70 * (they're negative logic, so that means *high*)
72 static int jtag_trst
= 0;
73 static int jtag_srst
= 0;
76 * List all TAPs that have been created.
78 static jtag_tap_t
*__jtag_all_taps
= NULL
;
80 * The number of TAPs in the __jtag_all_taps list, used to track the
81 * assigned chain position to new TAPs
83 static unsigned jtag_num_taps
= 0;
85 static enum reset_types jtag_reset_config
= RESET_NONE
;
86 static tap_state_t cmd_queue_end_state
= TAP_RESET
;
87 tap_state_t cmd_queue_cur_state
= TAP_RESET
;
89 static bool jtag_verify_capture_ir
= true;
90 static int jtag_verify
= 1;
92 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
93 static int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
94 static int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
96 typedef struct jtag_event_callback_s
98 jtag_event_handler_t callback
;
100 struct jtag_event_callback_s
* next
;
101 } jtag_event_callback_t
;
103 /* callbacks to inform high-level handlers about JTAG state changes */
104 static jtag_event_callback_t
*jtag_event_callbacks
;
107 static int speed_khz
= 0;
108 /* speed to fallback to when RCLK is requested but not supported */
109 static int rclk_fallback_speed_khz
= 0;
110 static enum {CLOCK_MODE_SPEED
, CLOCK_MODE_KHZ
, CLOCK_MODE_RCLK
} clock_mode
;
111 static int jtag_speed
= 0;
113 static struct jtag_interface_s
*jtag
= NULL
;
116 jtag_interface_t
*jtag_interface
= NULL
;
118 void jtag_set_error(int error
)
120 if ((error
== ERROR_OK
) || (jtag_error
!= ERROR_OK
))
124 int jtag_get_error(void)
128 int jtag_error_clear(void)
130 int temp
= jtag_error
;
131 jtag_error
= ERROR_OK
;
136 jtag_tap_t
*jtag_all_taps(void)
138 return __jtag_all_taps
;
141 unsigned jtag_tap_count(void)
143 return jtag_num_taps
;
146 unsigned jtag_tap_count_enabled(void)
148 jtag_tap_t
*t
= jtag_all_taps();
159 /// Append a new TAP to the chain of all taps.
160 void jtag_tap_add(struct jtag_tap_s
*t
)
162 t
->abs_chain_position
= jtag_num_taps
++;
164 jtag_tap_t
**tap
= &__jtag_all_taps
;
166 tap
= &(*tap
)->next_tap
;
170 /* returns a pointer to the n-th device in the scan chain */
171 static inline jtag_tap_t
*jtag_tap_by_position(unsigned n
)
173 jtag_tap_t
*t
= jtag_all_taps();
181 jtag_tap_t
*jtag_tap_by_string(const char *s
)
183 /* try by name first */
184 jtag_tap_t
*t
= jtag_all_taps();
188 if (0 == strcmp(t
->dotted_name
, s
))
193 /* no tap found by name, so try to parse the name as a number */
195 if (parse_uint(s
, &n
) != ERROR_OK
)
198 /* FIXME remove this numeric fallback code late June 2010, along
199 * with all info in the User's Guide that TAPs have numeric IDs.
200 * Also update "scan_chain" output to not display the numbers.
202 t
= jtag_tap_by_position(n
);
204 LOG_WARNING("Specify TAP '%s' by name, not number %u",
210 jtag_tap_t
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
212 const char *cp
= Jim_GetString(o
, NULL
);
213 jtag_tap_t
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
217 Jim_SetResult_sprintf(interp
, "Tap '%s' could not be found", cp
);
221 jtag_tap_t
* jtag_tap_next_enabled(jtag_tap_t
* p
)
223 p
= p
? p
->next_tap
: jtag_all_taps();
233 const char *jtag_tap_name(const jtag_tap_t
*tap
)
235 return (tap
== NULL
) ? "(unknown)" : tap
->dotted_name
;
239 int jtag_register_event_callback(jtag_event_handler_t callback
, void *priv
)
241 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
243 if (callback
== NULL
)
245 return ERROR_INVALID_ARGUMENTS
;
250 while ((*callbacks_p
)->next
)
251 callbacks_p
= &((*callbacks_p
)->next
);
252 callbacks_p
= &((*callbacks_p
)->next
);
255 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
256 (*callbacks_p
)->callback
= callback
;
257 (*callbacks_p
)->priv
= priv
;
258 (*callbacks_p
)->next
= NULL
;
263 int jtag_unregister_event_callback(jtag_event_handler_t callback
, void *priv
)
265 jtag_event_callback_t
**callbacks_p
;
266 jtag_event_callback_t
**next
;
268 if (callback
== NULL
)
270 return ERROR_INVALID_ARGUMENTS
;
273 for (callbacks_p
= &jtag_event_callbacks
;
274 *callbacks_p
!= NULL
;
277 next
= &((*callbacks_p
)->next
);
279 if ((*callbacks_p
)->priv
!= priv
)
282 if ((*callbacks_p
)->callback
== callback
)
285 *callbacks_p
= *next
;
292 int jtag_call_event_callbacks(enum jtag_event event
)
294 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
296 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
300 jtag_event_callback_t
*next
;
302 /* callback may remove itself */
303 next
= callback
->next
;
304 callback
->callback(event
, callback
->priv
);
311 static void jtag_checks(void)
313 assert(jtag_trst
== 0);
316 static void jtag_prelude(tap_state_t state
)
320 assert(state
!= TAP_INVALID
);
322 cmd_queue_cur_state
= state
;
325 void jtag_alloc_in_value32(scan_field_t
*field
)
327 interface_jtag_alloc_in_value32(field
);
330 void jtag_add_ir_scan_noverify(int in_count
, const scan_field_t
*in_fields
,
335 int retval
= interface_jtag_add_ir_scan(in_count
, in_fields
, state
);
336 jtag_set_error(retval
);
340 void jtag_add_ir_scan(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
342 if (jtag_verify
&& jtag_verify_capture_ir
)
344 /* 8 x 32 bit id's is enough for all invocations */
346 for (int j
= 0; j
< in_num_fields
; j
++)
348 /* if we are to run a verification of the ir scan, we need to get the input back.
349 * We may have to allocate space if the caller didn't ask for the input back.
351 in_fields
[j
].check_value
= in_fields
[j
].tap
->expected
;
352 in_fields
[j
].check_mask
= in_fields
[j
].tap
->expected_mask
;
354 jtag_add_scan_check(jtag_add_ir_scan_noverify
, in_num_fields
, in_fields
, state
);
357 jtag_add_ir_scan_noverify(in_num_fields
, in_fields
, state
);
361 void jtag_add_plain_ir_scan(int in_num_fields
, const scan_field_t
*in_fields
,
366 int retval
= interface_jtag_add_plain_ir_scan(
367 in_num_fields
, in_fields
, state
);
368 jtag_set_error(retval
);
371 void jtag_add_callback(jtag_callback1_t f
, jtag_callback_data_t data0
)
373 interface_jtag_add_callback(f
, data0
);
376 void jtag_add_callback4(jtag_callback_t f
, jtag_callback_data_t data0
,
377 jtag_callback_data_t data1
, jtag_callback_data_t data2
,
378 jtag_callback_data_t data3
)
380 interface_jtag_add_callback4(f
, data0
, data1
, data2
, data3
);
383 int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
, uint8_t *in_check_mask
, int num_bits
);
385 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
)
387 return jtag_check_value_inner((uint8_t *)data0
, (uint8_t *)data1
, (uint8_t *)data2
, (int)data3
);
390 static void jtag_add_scan_check(void (*jtag_add_scan
)(int in_num_fields
, const scan_field_t
*in_fields
, tap_state_t state
),
391 int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
393 for (int i
= 0; i
< in_num_fields
; i
++)
395 struct scan_field_s
*field
= &in_fields
[i
];
396 field
->allocated
= 0;
398 if (field
->check_value
|| field
->in_value
)
400 interface_jtag_add_scan_check_alloc(field
);
404 jtag_add_scan(in_num_fields
, in_fields
, state
);
406 for (int i
= 0; i
< in_num_fields
; i
++)
408 if ((in_fields
[i
].check_value
!= NULL
) && (in_fields
[i
].in_value
!= NULL
))
410 /* this is synchronous for a minidriver */
411 jtag_add_callback4(jtag_check_value_mask_callback
, (jtag_callback_data_t
)in_fields
[i
].in_value
,
412 (jtag_callback_data_t
)in_fields
[i
].check_value
,
413 (jtag_callback_data_t
)in_fields
[i
].check_mask
,
414 (jtag_callback_data_t
)in_fields
[i
].num_bits
);
416 if (in_fields
[i
].allocated
)
418 free(in_fields
[i
].in_value
);
420 if (in_fields
[i
].modified
)
422 in_fields
[i
].in_value
= NULL
;
427 void jtag_add_dr_scan_check(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
431 jtag_add_scan_check(jtag_add_dr_scan
, in_num_fields
, in_fields
, state
);
434 jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
439 void jtag_add_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
445 retval
= interface_jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
446 jtag_set_error(retval
);
449 void jtag_add_plain_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
455 retval
= interface_jtag_add_plain_dr_scan(in_num_fields
, in_fields
, state
);
456 jtag_set_error(retval
);
459 void jtag_add_dr_out(jtag_tap_t
* tap
,
460 int num_fields
, const int* num_bits
, const uint32_t* value
,
461 tap_state_t end_state
)
463 assert(end_state
!= TAP_INVALID
);
465 cmd_queue_cur_state
= end_state
;
467 interface_jtag_add_dr_out(tap
,
468 num_fields
, num_bits
, value
,
472 void jtag_add_tlr(void)
474 jtag_prelude(TAP_RESET
);
475 jtag_set_error(interface_jtag_add_tlr());
476 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
479 void jtag_add_pathmove(int num_states
, const tap_state_t
*path
)
481 tap_state_t cur_state
= cmd_queue_cur_state
;
483 /* the last state has to be a stable state */
484 if (!tap_is_state_stable(path
[num_states
- 1]))
486 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
487 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
491 for (int i
= 0; i
< num_states
; i
++)
493 if (path
[i
] == TAP_RESET
)
495 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
496 jtag_set_error(ERROR_JTAG_STATE_INVALID
);
500 if (tap_state_transition(cur_state
, true) != path
[i
]
501 && tap_state_transition(cur_state
, false) != path
[i
])
503 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
504 tap_state_name(cur_state
), tap_state_name(path
[i
]));
505 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID
);
513 jtag_set_error(interface_jtag_add_pathmove(num_states
, path
));
514 cmd_queue_cur_state
= path
[num_states
- 1];
517 int jtag_add_statemove(tap_state_t goal_state
)
519 tap_state_t cur_state
= cmd_queue_cur_state
;
521 LOG_DEBUG("cur_state=%s goal_state=%s",
522 tap_state_name(cur_state
),
523 tap_state_name(goal_state
));
526 if (goal_state
== cur_state
)
527 ; /* nothing to do */
528 else if (goal_state
== TAP_RESET
)
532 else if (tap_is_state_stable(cur_state
) && tap_is_state_stable(goal_state
))
534 unsigned tms_bits
= tap_get_tms_path(cur_state
, goal_state
);
535 unsigned tms_count
= tap_get_tms_path_len(cur_state
, goal_state
);
536 tap_state_t moves
[8];
537 assert(tms_count
< DIM(moves
));
539 for (unsigned i
= 0; i
< tms_count
; i
++, tms_bits
>>= 1)
541 bool bit
= tms_bits
& 1;
543 cur_state
= tap_state_transition(cur_state
, bit
);
544 moves
[i
] = cur_state
;
547 jtag_add_pathmove(tms_count
, moves
);
549 else if (tap_state_transition(cur_state
, true) == goal_state
550 || tap_state_transition(cur_state
, false) == goal_state
)
552 jtag_add_pathmove(1, &goal_state
);
561 void jtag_add_runtest(int num_cycles
, tap_state_t state
)
564 jtag_set_error(interface_jtag_add_runtest(num_cycles
, state
));
568 void jtag_add_clocks(int num_cycles
)
570 if (!tap_is_state_stable(cmd_queue_cur_state
))
572 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
573 tap_state_name(cmd_queue_cur_state
));
574 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
581 jtag_set_error(interface_jtag_add_clocks(num_cycles
));
585 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
587 int trst_with_tlr
= 0;
591 /* FIX!!! there are *many* different cases here. A better
592 * approach is needed for legal combinations of transitions...
594 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
595 (jtag_reset_config
& RESET_HAS_TRST
)&&
596 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) == 0))
598 if (((req_tlr_or_trst
&&!jtag_trst
)||
599 (!req_tlr_or_trst
&& jtag_trst
))&&
600 ((req_srst
&&!jtag_srst
)||
601 (!req_srst
&& jtag_srst
)))
603 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
604 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
608 /* Make sure that jtag_reset_config allows the requested reset */
609 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
610 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
612 LOG_ERROR("BUG: requested reset would assert trst");
613 jtag_set_error(ERROR_FAIL
);
617 /* if TRST pulls SRST, we reset with TAP T-L-R */
618 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
623 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
625 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
626 jtag_set_error(ERROR_FAIL
);
632 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
643 /* Maybe change TRST and/or SRST signal state */
644 if (jtag_srst
!= new_srst
|| jtag_trst
!= new_trst
) {
647 retval
= interface_jtag_add_reset(new_trst
, new_srst
);
648 if (retval
!= ERROR_OK
)
649 jtag_set_error(retval
);
651 retval
= jtag_execute_queue();
653 if (retval
!= ERROR_OK
) {
654 LOG_ERROR("TRST/SRST error %d", retval
);
659 /* SRST resets everything hooked up to that signal */
660 if (jtag_srst
!= new_srst
) {
661 jtag_srst
= new_srst
;
663 LOG_DEBUG("SRST line asserted");
665 LOG_DEBUG("SRST line released");
666 if (jtag_nsrst_delay
)
667 jtag_add_sleep(jtag_nsrst_delay
* 1000);
671 /* Maybe enter the JTAG TAP_RESET state ...
672 * - using only TMS, TCK, and the JTAG state machine
673 * - or else more directly, using TRST
675 * TAP_RESET should be invisible to non-debug parts of the system.
678 LOG_DEBUG("JTAG reset with TLR instead of TRST");
679 jtag_set_end_state(TAP_RESET
);
682 } else if (jtag_trst
!= new_trst
) {
683 jtag_trst
= new_trst
;
685 /* we just asserted nTRST, so we're now in TAP_RESET;
686 * inform possible listeners about this
688 * REVISIT asserting TRST is less significant than
689 * being in TAP_RESET ... both entries (TRST, TLR)
690 * should trigger a callback.
692 LOG_DEBUG("TRST line asserted");
693 tap_set_state(TAP_RESET
);
694 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
696 LOG_DEBUG("TRST line released");
697 if (jtag_ntrst_delay
)
698 jtag_add_sleep(jtag_ntrst_delay
* 1000);
703 tap_state_t
jtag_set_end_state(tap_state_t state
)
705 if ((state
== TAP_DRSHIFT
)||(state
== TAP_IRSHIFT
))
707 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
710 if (state
!= TAP_INVALID
)
711 cmd_queue_end_state
= state
;
712 return cmd_queue_end_state
;
715 tap_state_t
jtag_get_end_state(void)
717 return cmd_queue_end_state
;
720 void jtag_add_sleep(uint32_t us
)
722 /// @todo Here, keep_alive() appears to be a layering violation!!!
724 jtag_set_error(interface_jtag_add_sleep(us
));
727 int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
, uint8_t *in_check_mask
, int num_bits
)
729 int retval
= ERROR_OK
;
731 int compare_failed
= 0;
734 compare_failed
= buf_cmp_mask(captured
, in_check_value
, in_check_mask
, num_bits
);
736 compare_failed
= buf_cmp(captured
, in_check_value
, num_bits
);
738 if (compare_failed
) {
739 /* An error handler could have caught the failing check
740 * only report a problem when there wasn't a handler, or if the handler
741 * acknowledged the error
744 LOG_WARNING("TAP %s:",
745 jtag_tap_name(field->tap));
749 char *captured_char
= buf_to_str(captured
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
750 char *in_check_value_char
= buf_to_str(in_check_value
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
754 char *in_check_mask_char
;
755 in_check_mask_char
= buf_to_str(in_check_mask
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
756 LOG_WARNING("value captured during scan didn't pass the requested check:");
757 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
758 captured_char
, in_check_value_char
, in_check_mask_char
);
759 free(in_check_mask_char
);
763 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
);
767 free(in_check_value_char
);
769 retval
= ERROR_JTAG_QUEUE_FAILED
;
776 void jtag_check_value_mask(scan_field_t
*field
, uint8_t *value
, uint8_t *mask
)
778 assert(field
->in_value
!= NULL
);
782 /* no checking to do */
786 jtag_execute_queue_noclear();
788 int retval
= jtag_check_value_inner(field
->in_value
, value
, mask
, field
->num_bits
);
789 jtag_set_error(retval
);
794 int default_interface_jtag_execute_queue(void)
798 LOG_ERROR("No JTAG interface configured yet. "
799 "Issue 'init' command in startup scripts "
800 "before communicating with targets.");
804 return jtag
->execute_queue();
807 void jtag_execute_queue_noclear(void)
809 jtag_flush_queue_count
++;
810 jtag_set_error(interface_jtag_execute_queue());
813 int jtag_get_flush_queue_count(void)
815 return jtag_flush_queue_count
;
818 int jtag_execute_queue(void)
820 jtag_execute_queue_noclear();
821 return jtag_error_clear();
824 static int jtag_reset_callback(enum jtag_event event
, void *priv
)
826 jtag_tap_t
*tap
= priv
;
830 if (event
== JTAG_TRST_ASSERTED
)
832 tap
->enabled
= !tap
->disabled_after_reset
;
834 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
841 void jtag_sleep(uint32_t us
)
843 alive_sleep(us
/1000);
846 /// maximum number of JTAG devices expected in the chain
847 #define JTAG_MAX_CHAIN_SIZE 20
849 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
850 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
851 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
853 static int jtag_examine_chain_execute(uint8_t *idcode_buffer
, unsigned num_idcode
)
855 scan_field_t field
= {
857 .num_bits
= num_idcode
* 32,
858 .out_value
= idcode_buffer
,
859 .in_value
= idcode_buffer
,
862 // initialize to the end of chain ID value
863 for (unsigned i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
864 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
866 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
867 return jtag_execute_queue();
870 static bool jtag_examine_chain_check(uint8_t *idcodes
, unsigned count
)
872 uint8_t zero_check
= 0x0;
873 uint8_t one_check
= 0xff;
875 for (unsigned i
= 0; i
< count
* 4; i
++)
877 zero_check
|= idcodes
[i
];
878 one_check
&= idcodes
[i
];
881 /* if there wasn't a single non-zero bit or if all bits were one,
882 * the scan is not valid */
883 if (zero_check
== 0x00 || one_check
== 0xff)
885 LOG_ERROR("JTAG communication failure: check connection, "
886 "JTAG interface, target power etc.");
892 static void jtag_examine_chain_display(enum log_levels level
, const char *msg
,
893 const char *name
, uint32_t idcode
)
895 log_printf_lf(level
, __FILE__
, __LINE__
, __FUNCTION__
,
896 "JTAG tap: %s %16.16s: 0x%08x "
897 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
899 (unsigned int)idcode
,
900 (unsigned int)EXTRACT_MFG(idcode
),
901 (unsigned int)EXTRACT_PART(idcode
),
902 (unsigned int)EXTRACT_VER(idcode
));
905 static bool jtag_idcode_is_final(uint32_t idcode
)
907 return idcode
== 0x000000FF || idcode
== 0xFFFFFFFF;
911 * This helper checks that remaining bits in the examined chain data are
912 * all as expected, but a single JTAG device requires only 64 bits to be
913 * read back correctly. This can help identify and diagnose problems
914 * with the JTAG chain earlier, gives more helpful/explicit error messages.
916 static void jtag_examine_chain_end(uint8_t *idcodes
, unsigned count
, unsigned max
)
918 bool triggered
= false;
919 for (; count
< max
- 31; count
+= 32)
921 uint32_t idcode
= buf_get_u32(idcodes
, count
, 32);
922 // do not trigger the warning if the data looks good
923 if (!triggered
&& jtag_idcode_is_final(idcode
))
925 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
926 count
, (unsigned int)idcode
);
931 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s
*tap
)
933 if (0 == tap
->expected_ids_cnt
)
935 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
937 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap
->idcode
)
938 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
943 /* Loop over the expected identification codes and test for a match */
945 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
947 if (tap
->idcode
== tap
->expected_ids
[ii
])
951 /* If none of the expected ids matched, log an error */
952 if (ii
!= tap
->expected_ids_cnt
)
954 LOG_INFO("JTAG Tap/device matched");
957 jtag_examine_chain_display(LOG_LVL_ERROR
, "got",
958 tap
->dotted_name
, tap
->idcode
);
959 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
962 snprintf(msg
, sizeof(msg
), "expected %hhu of %hhu",
963 ii
+ 1, tap
->expected_ids_cnt
);
964 jtag_examine_chain_display(LOG_LVL_ERROR
, msg
,
965 tap
->dotted_name
, tap
->expected_ids
[ii
]);
970 /* Try to examine chain layout according to IEEE 1149.1 §12
972 int jtag_examine_chain(void)
974 uint8_t idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
975 unsigned device_count
= 0;
977 jtag_examine_chain_execute(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
);
979 if (!jtag_examine_chain_check(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
))
980 return ERROR_JTAG_INIT_FAILED
;
982 /* point at the 1st tap */
983 jtag_tap_t
*tap
= jtag_tap_next_enabled(NULL
);
986 LOG_ERROR("JTAG: No taps enabled?");
987 return ERROR_JTAG_INIT_FAILED
;
990 for (unsigned bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
992 uint32_t idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
993 if ((idcode
& 1) == 0)
995 /* LSB must not be 0, this indicates a device in bypass */
996 LOG_WARNING("Tap/Device does not have IDCODE");
1004 * End of chain (invalid manufacturer ID) some devices, such
1005 * as AVR will output all 1's instead of TDI input value at
1008 if (jtag_idcode_is_final(idcode
))
1010 jtag_examine_chain_end(idcode_buffer
,
1011 bit_count
+ 32, JTAG_MAX_CHAIN_SIZE
* 32);
1015 jtag_examine_chain_display(LOG_LVL_INFO
, "tap/device found",
1016 tap
? tap
->dotted_name
: "(not-named)",
1025 tap
->idcode
= idcode
;
1027 // ensure the TAP ID does matches what was expected
1028 if (!jtag_examine_chain_match_tap(tap
))
1029 return ERROR_JTAG_INIT_FAILED
;
1031 tap
= jtag_tap_next_enabled(tap
);
1034 /* see if number of discovered devices matches configuration */
1035 if (device_count
!= jtag_tap_count_enabled())
1037 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
1038 "does not match (enabled) configuration (%i), total taps: %d",
1039 device_count
, jtag_tap_count_enabled(), jtag_tap_count());
1040 LOG_ERROR("check the config file and ensure proper JTAG communication"
1041 " (connections, speed, ...)");
1042 return ERROR_JTAG_INIT_FAILED
;
1048 int jtag_validate_chain(void)
1051 int total_ir_length
= 0;
1052 uint8_t *ir_test
= NULL
;
1057 total_ir_length
= 0;
1059 tap
= jtag_tap_next_enabled(tap
);
1063 total_ir_length
+= tap
->ir_length
;
1066 total_ir_length
+= 2;
1067 ir_test
= malloc(CEIL(total_ir_length
, 8));
1068 buf_set_ones(ir_test
, total_ir_length
);
1071 field
.num_bits
= total_ir_length
;
1072 field
.out_value
= ir_test
;
1073 field
.in_value
= ir_test
;
1076 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
1077 jtag_execute_queue();
1083 tap
= jtag_tap_next_enabled(tap
);
1088 val
= buf_get_u32(ir_test
, chain_pos
, 2);
1091 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1092 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
);
1095 return ERROR_JTAG_INIT_FAILED
;
1097 chain_pos
+= tap
->ir_length
;
1100 val
= buf_get_u32(ir_test
, chain_pos
, 2);
1103 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1104 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
);
1107 return ERROR_JTAG_INIT_FAILED
;
1116 void jtag_tap_init(jtag_tap_t
*tap
)
1118 assert(0 != tap
->ir_length
);
1120 tap
->expected
= malloc(tap
->ir_length
);
1121 tap
->expected_mask
= malloc(tap
->ir_length
);
1122 tap
->cur_instr
= malloc(tap
->ir_length
);
1124 /// @todo cope sanely with ir_length bigger than 32 bits
1125 buf_set_u32(tap
->expected
, 0, tap
->ir_length
, tap
->ir_capture_value
);
1126 buf_set_u32(tap
->expected_mask
, 0, tap
->ir_length
, tap
->ir_capture_mask
);
1127 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1129 // place TAP in bypass mode
1131 // register the reset callback for the TAP
1132 jtag_register_event_callback(&jtag_reset_callback
, tap
);
1134 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1135 "irlen %d, capture: 0x%x mask: 0x%x", tap
->dotted_name
,
1136 tap
->abs_chain_position
, tap
->ir_length
,
1137 (unsigned int)(tap
->ir_capture_value
), (unsigned int)(tap
->ir_capture_mask
));
1141 void jtag_tap_free(jtag_tap_t
*tap
)
1143 jtag_unregister_event_callback(&jtag_reset_callback
, tap
);
1145 /// @todo is anything missing? no memory leaks please
1146 free((void *)tap
->expected_ids
);
1147 free((void *)tap
->chip
);
1148 free((void *)tap
->tapname
);
1149 free((void *)tap
->dotted_name
);
1153 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1158 if (!jtag_interface
)
1160 /* nothing was previously specified by "interface" command */
1161 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1162 return ERROR_JTAG_INVALID_INTERFACE
;
1165 jtag
= jtag_interface
;
1166 if (jtag_interface
->init() != ERROR_OK
)
1169 return ERROR_JTAG_INIT_FAILED
;
1172 int requested_khz
= jtag_get_speed_khz();
1173 int actual_khz
= requested_khz
;
1174 int retval
= jtag_get_speed_readable(&actual_khz
);
1175 if (ERROR_OK
!= retval
)
1176 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1177 else if (actual_khz
)
1179 if ((CLOCK_MODE_RCLK
== clock_mode
)
1180 || ((CLOCK_MODE_KHZ
== clock_mode
) && !requested_khz
))
1182 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1186 LOG_INFO("clock speed %d kHz", actual_khz
);
1189 LOG_INFO("RCLK (adaptive clock speed)");
1194 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
1199 LOG_DEBUG("Init JTAG chain");
1201 tap
= jtag_tap_next_enabled(NULL
);
1203 LOG_ERROR("There are no enabled taps?");
1204 return ERROR_JTAG_INIT_FAILED
;
1208 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1211 /* examine chain first, as this could discover the real chain layout */
1212 if (jtag_examine_chain() != ERROR_OK
)
1214 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1217 if (jtag_validate_chain() != ERROR_OK
)
1219 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1225 int jtag_interface_quit(void)
1227 if (!jtag
|| !jtag
->quit
)
1230 // close the JTAG interface
1231 int result
= jtag
->quit();
1232 if (ERROR_OK
!= result
)
1233 LOG_ERROR("failed: %d", result
);
1239 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
1243 if ((retval
= jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1246 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1248 /* Reset can happen after a power cycle.
1250 * Ideally we would only assert TRST or run TLR before the target reset.
1252 * However w/srst_pulls_trst, trst is asserted together with the target
1253 * reset whether we want it or not.
1255 * NB! Some targets have JTAG circuitry disabled until a
1256 * trst & srst has been asserted.
1258 * NB! here we assume nsrst/ntrst delay are sufficient!
1260 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1263 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1264 if (jtag_reset_config
& RESET_HAS_SRST
)
1266 jtag_add_reset(1, 1);
1267 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) == 0)
1268 jtag_add_reset(0, 1);
1270 jtag_add_reset(0, 0);
1271 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1274 /* Check that we can communication on the JTAG chain + eventually we want to
1275 * be able to perform enumeration only after OpenOCD has started
1276 * telnet and GDB server
1278 * That would allow users to more easily perform any magic they need to before
1281 return jtag_init_inner(cmd_ctx
);
1284 int jtag_init(struct command_context_s
*cmd_ctx
)
1287 if ((retval
= jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1289 if (jtag_init_inner(cmd_ctx
) == ERROR_OK
)
1293 return jtag_init_reset(cmd_ctx
);
1296 unsigned jtag_get_speed_khz(void)
1301 static int jtag_khz_to_speed(unsigned khz
, int* speed
)
1303 LOG_DEBUG("convert khz to interface specific speed value");
1307 LOG_DEBUG("have interface set up");
1309 int retval
= jtag
->khz(jtag_get_speed_khz(), &speed_div1
);
1310 if (ERROR_OK
!= retval
)
1314 *speed
= speed_div1
;
1319 static int jtag_rclk_to_speed(unsigned fallback_speed_khz
, int* speed
)
1321 int retval
= jtag_khz_to_speed(0, speed
);
1322 if ((ERROR_OK
!= retval
) && fallback_speed_khz
)
1324 LOG_DEBUG("trying fallback speed...");
1325 retval
= jtag_khz_to_speed(fallback_speed_khz
, speed
);
1330 static int jtag_set_speed(int speed
)
1333 /* this command can be called during CONFIG,
1334 * in which case jtag isn't initialized */
1335 return jtag
? jtag
->speed(speed
) : ERROR_OK
;
1338 int jtag_config_speed(int speed
)
1340 LOG_DEBUG("handle jtag speed");
1341 clock_mode
= CLOCK_MODE_SPEED
;
1342 return jtag_set_speed(speed
);
1345 int jtag_config_khz(unsigned khz
)
1347 LOG_DEBUG("handle jtag khz");
1348 clock_mode
= CLOCK_MODE_KHZ
;
1350 int retval
= jtag_khz_to_speed(khz
, &speed
);
1351 return (ERROR_OK
!= retval
) ? retval
: jtag_set_speed(speed
);
1354 int jtag_config_rclk(unsigned fallback_speed_khz
)
1356 LOG_DEBUG("handle jtag rclk");
1357 clock_mode
= CLOCK_MODE_RCLK
;
1358 rclk_fallback_speed_khz
= fallback_speed_khz
;
1360 int retval
= jtag_rclk_to_speed(fallback_speed_khz
, &speed
);
1361 return (ERROR_OK
!= retval
) ? retval
: jtag_set_speed(speed
);
1364 int jtag_get_speed(void)
1369 case CLOCK_MODE_SPEED
:
1372 case CLOCK_MODE_KHZ
:
1373 jtag_khz_to_speed(jtag_get_speed_khz(), &speed
);
1375 case CLOCK_MODE_RCLK
:
1376 jtag_rclk_to_speed(rclk_fallback_speed_khz
, &speed
);
1379 LOG_ERROR("BUG: unknown jtag clock mode");
1386 int jtag_get_speed_readable(int *khz
)
1388 return jtag
? jtag
->speed_div(jtag_get_speed(), khz
) : ERROR_OK
;
1391 void jtag_set_verify(bool enable
)
1393 jtag_verify
= enable
;
1396 bool jtag_will_verify()
1401 void jtag_set_verify_capture_ir(bool enable
)
1403 jtag_verify_capture_ir
= enable
;
1406 bool jtag_will_verify_capture_ir()
1408 return jtag_verify_capture_ir
;
1411 int jtag_power_dropout(int *dropout
)
1413 return jtag
->power_dropout(dropout
);
1416 int jtag_srst_asserted(int *srst_asserted
)
1418 return jtag
->srst_asserted(srst_asserted
);
1421 enum reset_types
jtag_get_reset_config(void)
1423 return jtag_reset_config
;
1425 void jtag_set_reset_config(enum reset_types type
)
1427 jtag_reset_config
= type
;
1430 int jtag_get_trst(void)
1434 int jtag_get_srst(void)
1439 void jtag_set_nsrst_delay(unsigned delay
)
1441 jtag_nsrst_delay
= delay
;
1443 unsigned jtag_get_nsrst_delay(void)
1445 return jtag_nsrst_delay
;
1447 void jtag_set_ntrst_delay(unsigned delay
)
1449 jtag_ntrst_delay
= delay
;
1451 unsigned jtag_get_ntrst_delay(void)
1453 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)