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] = "TAP reset",
64 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
65 [JTAG_TAP_EVENT_POST_RESET] = "TAP post reset",
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*)
73 static int jtag_trst = 0;
74 static int jtag_srst = 0;
77 * List all TAPs that have been created.
79 static jtag_tap_t *__jtag_all_taps = NULL;
81 * The number of TAPs in the __jtag_all_taps list, used to track the
82 * assigned chain position to new TAPs
84 static unsigned jtag_num_taps = 0;
86 static enum reset_types jtag_reset_config = RESET_NONE;
87 static tap_state_t cmd_queue_end_state = TAP_RESET;
88 tap_state_t cmd_queue_cur_state = TAP_RESET;
90 static bool jtag_verify_capture_ir = true;
91 static int jtag_verify = 1;
93 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
94 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
95 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
97 typedef struct jtag_event_callback_s
99 jtag_event_handler_t callback;
101 struct jtag_event_callback_s* next;
102 } jtag_event_callback_t;
104 /* callbacks to inform high-level handlers about JTAG state changes */
105 static jtag_event_callback_t *jtag_event_callbacks;
108 static int speed_khz = 0;
109 /* speed to fallback to when RCLK is requested but not supported */
110 static int rclk_fallback_speed_khz = 0;
111 static enum {CLOCK_MODE_SPEED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
112 static int jtag_speed = 0;
114 static struct jtag_interface_s *jtag = NULL;
117 jtag_interface_t *jtag_interface = NULL;
119 void jtag_set_error(int error)
121 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
125 int jtag_get_error(void)
129 int jtag_error_clear(void)
131 int temp = jtag_error;
132 jtag_error = ERROR_OK;
137 jtag_tap_t *jtag_all_taps(void)
139 return __jtag_all_taps;
142 unsigned jtag_tap_count(void)
144 return jtag_num_taps;
147 unsigned jtag_tap_count_enabled(void)
149 jtag_tap_t *t = jtag_all_taps();
160 /// Append a new TAP to the chain of all taps.
161 void jtag_tap_add(struct jtag_tap_s *t)
163 t->abs_chain_position = jtag_num_taps++;
165 jtag_tap_t **tap = &__jtag_all_taps;
167 tap = &(*tap)->next_tap;
171 /* returns a pointer to the n-th device in the scan chain */
172 static inline jtag_tap_t *jtag_tap_by_position(unsigned n)
174 jtag_tap_t *t = jtag_all_taps();
182 jtag_tap_t *jtag_tap_by_string(const char *s)
184 /* try by name first */
185 jtag_tap_t *t = jtag_all_taps();
189 if (0 == strcmp(t->dotted_name, s))
194 /* no tap found by name, so try to parse the name as a number */
196 if (parse_uint(s, &n) != ERROR_OK)
199 /* FIXME remove this numeric fallback code late June 2010, along
200 * with all info in the User's Guide that TAPs have numeric IDs.
201 * Also update "scan_chain" output to not display the numbers.
203 t = jtag_tap_by_position(n);
205 LOG_WARNING("Specify TAP '%s' by name, not number %u",
211 jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
213 const char *cp = Jim_GetString(o, NULL);
214 jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
218 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
222 jtag_tap_t* jtag_tap_next_enabled(jtag_tap_t* p)
224 p = p ? p->next_tap : jtag_all_taps();
234 const char *jtag_tap_name(const jtag_tap_t *tap)
236 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
240 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
242 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
244 if (callback == NULL)
246 return ERROR_INVALID_ARGUMENTS;
251 while ((*callbacks_p)->next)
252 callbacks_p = &((*callbacks_p)->next);
253 callbacks_p = &((*callbacks_p)->next);
256 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
257 (*callbacks_p)->callback = callback;
258 (*callbacks_p)->priv = priv;
259 (*callbacks_p)->next = NULL;
264 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
266 jtag_event_callback_t **callbacks_p;
267 jtag_event_callback_t **next;
269 if (callback == NULL)
271 return ERROR_INVALID_ARGUMENTS;
274 for (callbacks_p = &jtag_event_callbacks;
275 *callbacks_p != NULL;
278 next = &((*callbacks_p)->next);
280 if ((*callbacks_p)->priv != priv)
283 if ((*callbacks_p)->callback == callback)
286 *callbacks_p = *next;
293 int jtag_call_event_callbacks(enum jtag_event event)
295 jtag_event_callback_t *callback = jtag_event_callbacks;
297 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
301 jtag_event_callback_t *next;
303 /* callback may remove itself */
304 next = callback->next;
305 callback->callback(event, callback->priv);
312 static void jtag_checks(void)
314 assert(jtag_trst == 0);
317 static void jtag_prelude(tap_state_t state)
321 assert(state != TAP_INVALID);
323 cmd_queue_cur_state = state;
326 void jtag_alloc_in_value32(scan_field_t *field)
328 interface_jtag_alloc_in_value32(field);
331 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
336 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
337 jtag_set_error(retval);
341 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
343 assert(state != TAP_RESET);
345 if (jtag_verify && jtag_verify_capture_ir)
347 /* 8 x 32 bit id's is enough for all invocations */
349 for (int j = 0; j < in_num_fields; j++)
351 /* if we are to run a verification of the ir scan, we need to get the input back.
352 * We may have to allocate space if the caller didn't ask for the input back.
354 in_fields[j].check_value = in_fields[j].tap->expected;
355 in_fields[j].check_mask = in_fields[j].tap->expected_mask;
357 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
360 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
364 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
367 assert(state != TAP_RESET);
371 int retval = interface_jtag_add_plain_ir_scan(
372 in_num_fields, in_fields, state);
373 jtag_set_error(retval);
376 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
378 interface_jtag_add_callback(f, data0);
381 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
382 jtag_callback_data_t data1, jtag_callback_data_t data2,
383 jtag_callback_data_t data3)
385 interface_jtag_add_callback4(f, data0, data1, data2, data3);
388 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
389 uint8_t *in_check_mask, int num_bits);
391 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)
393 return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
396 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
397 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
399 for (int i = 0; i < in_num_fields; i++)
401 struct scan_field_s *field = &in_fields[i];
402 field->allocated = 0;
404 if (field->check_value || field->in_value)
406 interface_jtag_add_scan_check_alloc(field);
410 jtag_add_scan(in_num_fields, in_fields, state);
412 for (int i = 0; i < in_num_fields; i++)
414 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
416 /* this is synchronous for a minidriver */
417 jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
418 (jtag_callback_data_t)in_fields[i].check_value,
419 (jtag_callback_data_t)in_fields[i].check_mask,
420 (jtag_callback_data_t)in_fields[i].num_bits);
422 if (in_fields[i].allocated)
424 free(in_fields[i].in_value);
426 if (in_fields[i].modified)
428 in_fields[i].in_value = NULL;
433 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
437 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
440 jtag_add_dr_scan(in_num_fields, in_fields, state);
445 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
448 assert(state != TAP_RESET);
453 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
454 jtag_set_error(retval);
457 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
460 assert(state != TAP_RESET);
465 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
466 jtag_set_error(retval);
469 void jtag_add_dr_out(jtag_tap_t* tap,
470 int num_fields, const int* num_bits, const uint32_t* value,
471 tap_state_t end_state)
473 assert(end_state != TAP_RESET);
474 assert(end_state != TAP_INVALID);
476 cmd_queue_cur_state = end_state;
478 interface_jtag_add_dr_out(tap,
479 num_fields, num_bits, value,
483 void jtag_add_tlr(void)
485 jtag_prelude(TAP_RESET);
486 jtag_set_error(interface_jtag_add_tlr());
488 /* NOTE: order here matches TRST path in jtag_add_reset() */
489 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
493 void jtag_add_pathmove(int num_states, const tap_state_t *path)
495 tap_state_t cur_state = cmd_queue_cur_state;
497 /* the last state has to be a stable state */
498 if (!tap_is_state_stable(path[num_states - 1]))
500 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
501 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
505 for (int i = 0; i < num_states; i++)
507 if (path[i] == TAP_RESET)
509 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
510 jtag_set_error(ERROR_JTAG_STATE_INVALID);
514 if (tap_state_transition(cur_state, true) != path[i]
515 && tap_state_transition(cur_state, false) != path[i])
517 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
518 tap_state_name(cur_state), tap_state_name(path[i]));
519 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
527 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
528 cmd_queue_cur_state = path[num_states - 1];
531 int jtag_add_statemove(tap_state_t goal_state)
533 tap_state_t cur_state = cmd_queue_cur_state;
535 LOG_DEBUG("cur_state=%s goal_state=%s",
536 tap_state_name(cur_state),
537 tap_state_name(goal_state));
540 if (goal_state == cur_state)
541 ; /* nothing to do */
542 else if (goal_state == TAP_RESET)
546 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
548 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
549 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
550 tap_state_t moves[8];
551 assert(tms_count < DIM(moves));
553 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
555 bool bit = tms_bits & 1;
557 cur_state = tap_state_transition(cur_state, bit);
558 moves[i] = cur_state;
561 jtag_add_pathmove(tms_count, moves);
563 else if (tap_state_transition(cur_state, true) == goal_state
564 || tap_state_transition(cur_state, false) == goal_state)
566 jtag_add_pathmove(1, &goal_state);
575 void jtag_add_runtest(int num_cycles, tap_state_t state)
578 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
582 void jtag_add_clocks(int num_cycles)
584 if (!tap_is_state_stable(cmd_queue_cur_state))
586 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
587 tap_state_name(cmd_queue_cur_state));
588 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
595 jtag_set_error(interface_jtag_add_clocks(num_cycles));
599 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
601 int trst_with_tlr = 0;
605 /* Without SRST, we must use target-specific JTAG operations
606 * on each target; callers should not be requesting SRST when
607 * that signal doesn't exist.
609 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
610 * can kick in even if the JTAG adapter can't drive TRST.
613 if (!(jtag_reset_config & RESET_HAS_SRST)) {
614 LOG_ERROR("BUG: can't assert SRST");
615 jtag_set_error(ERROR_FAIL);
618 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
619 && !req_tlr_or_trst) {
620 LOG_ERROR("BUG: can't assert only SRST");
621 jtag_set_error(ERROR_FAIL);
627 /* JTAG reset (entry to TAP_RESET state) can always be achieved
628 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
629 * state first. TRST accelerates it, and bypasses those states.
631 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
632 * can kick in even if the JTAG adapter can't drive SRST.
634 if (req_tlr_or_trst) {
635 if (!(jtag_reset_config & RESET_HAS_TRST))
637 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
644 /* Maybe change TRST and/or SRST signal state */
645 if (jtag_srst != new_srst || jtag_trst != new_trst) {
648 retval = interface_jtag_add_reset(new_trst, new_srst);
649 if (retval != ERROR_OK)
650 jtag_set_error(retval);
652 retval = jtag_execute_queue();
654 if (retval != ERROR_OK) {
655 LOG_ERROR("TRST/SRST error %d", retval);
660 /* SRST resets everything hooked up to that signal */
661 if (jtag_srst != new_srst) {
662 jtag_srst = new_srst;
664 LOG_DEBUG("SRST line asserted");
666 LOG_DEBUG("SRST line released");
667 if (jtag_nsrst_delay)
668 jtag_add_sleep(jtag_nsrst_delay * 1000);
672 /* Maybe enter the JTAG TAP_RESET state ...
673 * - using only TMS, TCK, and the JTAG state machine
674 * - or else more directly, using TRST
676 * TAP_RESET should be invisible to non-debug parts of the system.
679 LOG_DEBUG("JTAG reset with TLR instead of TRST");
680 jtag_set_end_state(TAP_RESET);
683 } else if (jtag_trst != new_trst) {
684 jtag_trst = new_trst;
686 LOG_DEBUG("TRST line asserted");
687 tap_set_state(TAP_RESET);
689 LOG_DEBUG("TRST line released");
690 if (jtag_ntrst_delay)
691 jtag_add_sleep(jtag_ntrst_delay * 1000);
693 /* We just asserted nTRST, so we're now in TAP_RESET.
694 * Inform possible listeners about this, now that
695 * JTAG instructions and data can be shifted. This
696 * sequence must match jtag_add_tlr().
698 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
704 tap_state_t jtag_set_end_state(tap_state_t state)
706 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
708 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
711 if (state != TAP_INVALID)
712 cmd_queue_end_state = state;
713 return cmd_queue_end_state;
716 tap_state_t jtag_get_end_state(void)
718 return cmd_queue_end_state;
721 void jtag_add_sleep(uint32_t us)
723 /// @todo Here, keep_alive() appears to be a layering violation!!!
725 jtag_set_error(interface_jtag_add_sleep(us));
728 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
729 uint8_t *in_check_mask, int num_bits)
731 int retval = ERROR_OK;
733 int compare_failed = 0;
736 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
738 compare_failed = buf_cmp(captured, in_check_value, num_bits);
740 if (compare_failed) {
741 char *captured_str, *in_check_value_str;
742 int bits = (num_bits > DEBUG_JTAG_IOZ)
746 /* NOTE: we've lost diagnostic context here -- 'which tap' */
748 captured_str = buf_to_str(captured, bits, 16);
749 in_check_value_str = buf_to_str(in_check_value, bits, 16);
751 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
753 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
756 free(in_check_value_str);
759 char *in_check_mask_str;
761 in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
762 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
763 free(in_check_mask_str);
766 retval = ERROR_JTAG_QUEUE_FAILED;
771 void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
773 assert(field->in_value != NULL);
777 /* no checking to do */
781 jtag_execute_queue_noclear();
783 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
784 jtag_set_error(retval);
789 int default_interface_jtag_execute_queue(void)
793 LOG_ERROR("No JTAG interface configured yet. "
794 "Issue 'init' command in startup scripts "
795 "before communicating with targets.");
799 return jtag->execute_queue();
802 void jtag_execute_queue_noclear(void)
804 jtag_flush_queue_count++;
805 jtag_set_error(interface_jtag_execute_queue());
808 int jtag_get_flush_queue_count(void)
810 return jtag_flush_queue_count;
813 int jtag_execute_queue(void)
815 jtag_execute_queue_noclear();
816 return jtag_error_clear();
819 static int jtag_reset_callback(enum jtag_event event, void *priv)
821 jtag_tap_t *tap = priv;
823 if (event == JTAG_TRST_ASSERTED)
825 tap->enabled = !tap->disabled_after_reset;
827 /* current instruction is either BYPASS or IDCODE */
828 buf_set_ones(tap->cur_instr, tap->ir_length);
835 void jtag_sleep(uint32_t us)
837 alive_sleep(us/1000);
840 /* Maximum number of enabled JTAG devices we expect in the scan chain,
841 * plus one (to detect garbage at the end). Devices that don't support
842 * IDCODE take up fewer bits, possibly allowing a few more devices.
844 #define JTAG_MAX_CHAIN_SIZE 20
846 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
847 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
848 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
850 #define END_OF_CHAIN_FLAG 0x000000ff
852 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
854 scan_field_t field = {
856 .num_bits = num_idcode * 32,
857 .out_value = idcode_buffer,
858 .in_value = idcode_buffer,
861 // initialize to the end of chain ID value
862 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
863 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
865 jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
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)
908 * Some devices, such as AVR8, will output all 1's instead
909 * of TDI input value at end of chain. Allow those values
910 * instead of failing.
912 return idcode == END_OF_CHAIN_FLAG || idcode == 0xFFFFFFFF;
916 * This helper checks that remaining bits in the examined chain data are
917 * all as expected, but a single JTAG device requires only 64 bits to be
918 * read back correctly. This can help identify and diagnose problems
919 * with the JTAG chain earlier, gives more helpful/explicit error messages.
920 * Returns TRUE iff garbage was found.
922 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
924 bool triggered = false;
925 for (; count < max - 31; count += 32)
927 uint32_t idcode = buf_get_u32(idcodes, count, 32);
928 // do not trigger the warning if the data looks good
929 if (!triggered && jtag_idcode_is_final(idcode))
931 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
932 count, (unsigned int)idcode);
938 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
940 /* ignore expected BYPASS codes; warn otherwise */
941 if (0 == tap->expected_ids_cnt && !tap->idcode)
944 /* Loop over the expected identification codes and test for a match */
946 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
948 if (tap->idcode == tap->expected_ids[ii])
952 /* If none of the expected ids matched, log an error */
953 jtag_examine_chain_display(LOG_LVL_ERROR, "UNEXPECTED",
954 tap->dotted_name, tap->idcode);
955 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
958 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
959 ii + 1, tap->expected_ids_cnt);
960 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
961 tap->dotted_name, tap->expected_ids[ii]);
966 /* Try to examine chain layout according to IEEE 1149.1 §12
968 static int jtag_examine_chain(void)
970 uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
973 /* DR scan to collect BYPASS or IDCODE register contents.
974 * Then make sure the scan data has both ones and zeroes.
976 jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
977 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
978 return ERROR_JTAG_INIT_FAILED;
980 /* point at the 1st tap */
981 jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
984 LOG_ERROR("JTAG: No taps enabled?");
985 return ERROR_JTAG_INIT_FAILED;
989 tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
990 tap = jtag_tap_next_enabled(tap))
992 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
994 if ((idcode & 1) == 0)
996 /* LSB must not be 0, this indicates a device in bypass */
997 LOG_WARNING("TAP %s does not have IDCODE",
1000 tap->hasidcode = false;
1006 /* Friendly devices support IDCODE */
1007 tap->hasidcode = true;
1008 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
1009 tap->dotted_name, idcode);
1013 tap->idcode = idcode;
1015 // ensure the TAP ID does matches what was expected
1016 if (!jtag_examine_chain_match_tap(tap))
1017 return ERROR_JTAG_INIT_FAILED;
1020 /* Fail if too many TAPs were enabled for us to verify them all. */
1022 LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
1024 return ERROR_JTAG_INIT_FAILED;
1027 /* After those IDCODE or BYPASS register values should be
1028 * only the data we fed into the scan chain.
1030 if (jtag_examine_chain_end(idcode_buffer, bit_count,
1031 8 * sizeof(idcode_buffer))) {
1032 LOG_ERROR("double-check your JTAG setup (interface, "
1033 "speed, TAPs, ...)");
1034 return ERROR_JTAG_INIT_FAILED;
1040 int jtag_validate_chain(void)
1043 int total_ir_length = 0;
1044 uint8_t *ir_test = NULL;
1049 total_ir_length = 0;
1051 tap = jtag_tap_next_enabled(tap);
1055 total_ir_length += tap->ir_length;
1058 total_ir_length += 2;
1059 ir_test = malloc(CEIL(total_ir_length, 8));
1060 buf_set_ones(ir_test, total_ir_length);
1063 field.num_bits = total_ir_length;
1064 field.out_value = ir_test;
1065 field.in_value = ir_test;
1068 jtag_add_plain_ir_scan(1, &field, TAP_IRPAUSE);
1072 retval = jtag_execute_queue();
1073 if (retval != ERROR_OK)
1080 tap = jtag_tap_next_enabled(tap);
1085 val = buf_get_u32(ir_test, chain_pos, 2);
1086 /* Only fail this check if we have IDCODE for this device */
1087 if ((val != 0x1)&&(tap->hasidcode))
1089 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1090 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);
1093 return ERROR_JTAG_INIT_FAILED;
1095 chain_pos += tap->ir_length;
1098 val = buf_get_u32(ir_test, chain_pos, 2);
1101 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1102 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);
1105 return ERROR_JTAG_INIT_FAILED;
1114 void jtag_tap_init(jtag_tap_t *tap)
1116 assert(0 != tap->ir_length);
1118 tap->expected = malloc(tap->ir_length);
1119 tap->expected_mask = malloc(tap->ir_length);
1120 tap->cur_instr = malloc(tap->ir_length);
1122 /// @todo cope sanely with ir_length bigger than 32 bits
1123 buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1124 buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1125 buf_set_ones(tap->cur_instr, tap->ir_length);
1127 // place TAP in bypass mode
1129 // register the reset callback for the TAP
1130 jtag_register_event_callback(&jtag_reset_callback, tap);
1132 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1133 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1134 tap->abs_chain_position, tap->ir_length,
1135 (unsigned int)(tap->ir_capture_value), (unsigned int)(tap->ir_capture_mask));
1139 void jtag_tap_free(jtag_tap_t *tap)
1141 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1143 /// @todo is anything missing? no memory leaks please
1144 free((void *)tap->expected_ids);
1145 free((void *)tap->chip);
1146 free((void *)tap->tapname);
1147 free((void *)tap->dotted_name);
1151 int jtag_interface_init(struct command_context_s *cmd_ctx)
1156 if (!jtag_interface)
1158 /* nothing was previously specified by "interface" command */
1159 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1160 return ERROR_JTAG_INVALID_INTERFACE;
1163 jtag = jtag_interface;
1164 if (jtag_interface->init() != ERROR_OK)
1167 return ERROR_JTAG_INIT_FAILED;
1170 int requested_khz = jtag_get_speed_khz();
1171 int actual_khz = requested_khz;
1172 int retval = jtag_get_speed_readable(&actual_khz);
1173 if (ERROR_OK != retval)
1174 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1175 else if (actual_khz)
1177 if ((CLOCK_MODE_RCLK == clock_mode)
1178 || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
1180 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1184 LOG_INFO("clock speed %d kHz", actual_khz);
1187 LOG_INFO("RCLK (adaptive clock speed)");
1192 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1197 LOG_DEBUG("Init JTAG chain");
1199 tap = jtag_tap_next_enabled(NULL);
1201 LOG_ERROR("There are no enabled taps?");
1202 return ERROR_JTAG_INIT_FAILED;
1206 if ((retval = jtag_execute_queue()) != ERROR_OK)
1209 /* examine chain first, as this could discover the real chain layout */
1210 if (jtag_examine_chain() != ERROR_OK)
1212 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1215 if (jtag_validate_chain() != ERROR_OK)
1217 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1223 int jtag_interface_quit(void)
1225 if (!jtag || !jtag->quit)
1228 // close the JTAG interface
1229 int result = jtag->quit();
1230 if (ERROR_OK != result)
1231 LOG_ERROR("failed: %d", result);
1237 int jtag_init_reset(struct command_context_s *cmd_ctx)
1241 if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1244 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1246 /* Reset can happen after a power cycle.
1248 * Ideally we would only assert TRST or run TLR before the target reset.
1250 * However w/srst_pulls_trst, trst is asserted together with the target
1251 * reset whether we want it or not.
1253 * NB! Some targets have JTAG circuitry disabled until a
1254 * trst & srst has been asserted.
1256 * NB! here we assume nsrst/ntrst delay are sufficient!
1258 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1261 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1262 if (jtag_reset_config & RESET_HAS_SRST)
1264 jtag_add_reset(1, 1);
1265 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1266 jtag_add_reset(0, 1);
1268 jtag_add_reset(0, 0);
1269 if ((retval = jtag_execute_queue()) != ERROR_OK)
1272 /* Check that we can communication on the JTAG chain + eventually we want to
1273 * be able to perform enumeration only after OpenOCD has started
1274 * telnet and GDB server
1276 * That would allow users to more easily perform any magic they need to before
1279 return jtag_init_inner(cmd_ctx);
1282 int jtag_init(struct command_context_s *cmd_ctx)
1285 if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1287 if (jtag_init_inner(cmd_ctx) == ERROR_OK)
1291 return jtag_init_reset(cmd_ctx);
1294 unsigned jtag_get_speed_khz(void)
1299 static int jtag_khz_to_speed(unsigned khz, int* speed)
1301 LOG_DEBUG("convert khz to interface specific speed value");
1305 LOG_DEBUG("have interface set up");
1307 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1308 if (ERROR_OK != retval)
1312 *speed = speed_div1;
1317 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
1319 int retval = jtag_khz_to_speed(0, speed);
1320 if ((ERROR_OK != retval) && fallback_speed_khz)
1322 LOG_DEBUG("trying fallback speed...");
1323 retval = jtag_khz_to_speed(fallback_speed_khz, speed);
1328 static int jtag_set_speed(int speed)
1331 /* this command can be called during CONFIG,
1332 * in which case jtag isn't initialized */
1333 return jtag ? jtag->speed(speed) : ERROR_OK;
1336 int jtag_config_speed(int speed)
1338 LOG_DEBUG("handle jtag speed");
1339 clock_mode = CLOCK_MODE_SPEED;
1340 return jtag_set_speed(speed);
1343 int jtag_config_khz(unsigned khz)
1345 LOG_DEBUG("handle jtag khz");
1346 clock_mode = CLOCK_MODE_KHZ;
1348 int retval = jtag_khz_to_speed(khz, &speed);
1349 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1352 int jtag_config_rclk(unsigned fallback_speed_khz)
1354 LOG_DEBUG("handle jtag rclk");
1355 clock_mode = CLOCK_MODE_RCLK;
1356 rclk_fallback_speed_khz = fallback_speed_khz;
1358 int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1359 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1362 int jtag_get_speed(void)
1367 case CLOCK_MODE_SPEED:
1370 case CLOCK_MODE_KHZ:
1371 jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
1373 case CLOCK_MODE_RCLK:
1374 jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
1377 LOG_ERROR("BUG: unknown jtag clock mode");
1384 int jtag_get_speed_readable(int *khz)
1386 return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
1389 void jtag_set_verify(bool enable)
1391 jtag_verify = enable;
1394 bool jtag_will_verify()
1399 void jtag_set_verify_capture_ir(bool enable)
1401 jtag_verify_capture_ir = enable;
1404 bool jtag_will_verify_capture_ir()
1406 return jtag_verify_capture_ir;
1409 int jtag_power_dropout(int *dropout)
1411 return jtag->power_dropout(dropout);
1414 int jtag_srst_asserted(int *srst_asserted)
1416 return jtag->srst_asserted(srst_asserted);
1419 enum reset_types jtag_get_reset_config(void)
1421 return jtag_reset_config;
1423 void jtag_set_reset_config(enum reset_types type)
1425 jtag_reset_config = type;
1428 int jtag_get_trst(void)
1432 int jtag_get_srst(void)
1437 void jtag_set_nsrst_delay(unsigned delay)
1439 jtag_nsrst_delay = delay;
1441 unsigned jtag_get_nsrst_delay(void)
1443 return jtag_nsrst_delay;
1445 void jtag_set_ntrst_delay(unsigned delay)
1447 jtag_ntrst_delay = delay;
1449 unsigned jtag_get_ntrst_delay(void)
1451 return jtag_ntrst_delay;