1 /***************************************************************************
2 * Copyright (C) 2009 Zachary T Welch *
3 * zw@superlucidity.net *
5 * Copyright (C) 2007,2008,2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2005 by Dominic Rath *
13 * Dominic.Rath@gmx.de *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
29 ***************************************************************************/
37 #include "interface.h"
38 #include <transport/transport.h>
44 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
46 #include "xsvf/xsvf.h"
48 /** The number of JTAG queue flushes (for profiling and debugging purposes). */
49 static int jtag_flush_queue_count;
51 /* Sleep this # of ms after flushing the queue */
52 static int jtag_flush_queue_sleep;
54 static void jtag_add_scan_check(struct jtag_tap *active,
55 void (*jtag_add_scan)(struct jtag_tap *active,
57 const struct scan_field *in_fields,
59 int in_num_fields, struct scan_field *in_fields, tap_state_t state);
62 * The jtag_error variable is set when an error occurs while executing
63 * the queue. Application code may set this using jtag_set_error(),
64 * when an error occurs during processing that should be reported during
65 * jtag_execute_queue().
67 * The value is set and cleared, but never read by normal application code.
69 * This value is returned (and cleared) by jtag_execute_queue().
71 static int jtag_error = ERROR_OK;
73 static const char *jtag_event_strings[] = {
74 [JTAG_TRST_ASSERTED] = "TAP reset",
75 [JTAG_TAP_EVENT_SETUP] = "TAP setup",
76 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
77 [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
81 * JTAG adapters must initialize with TRST and SRST de-asserted
82 * (they're negative logic, so that means *high*). But some
83 * hardware doesn't necessarily work that way ... so set things
84 * up so that jtag_init() always forces that state.
86 static int jtag_trst = -1;
87 static int jtag_srst = -1;
90 * List all TAPs that have been created.
92 static struct jtag_tap *__jtag_all_taps;
94 * The number of TAPs in the __jtag_all_taps list, used to track the
95 * assigned chain position to new TAPs
97 static unsigned jtag_num_taps;
99 static enum reset_types jtag_reset_config = RESET_NONE;
100 tap_state_t cmd_queue_cur_state = TAP_RESET;
102 static bool jtag_verify_capture_ir = true;
103 static int jtag_verify = 1;
105 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
106 *deasserted (in ms) */
107 static int adapter_nsrst_delay; /* default to no nSRST delay */
108 static int jtag_ntrst_delay;/* default to no nTRST delay */
109 static int adapter_nsrst_assert_width; /* width of assertion */
110 static int jtag_ntrst_assert_width; /* width of assertion */
113 * Contains a single callback along with a pointer that will be passed
114 * when an event occurs.
116 struct jtag_event_callback {
117 /** a event callback */
118 jtag_event_handler_t callback;
119 /** the private data to pass to the callback */
121 /** the next callback */
122 struct jtag_event_callback *next;
125 /* callbacks to inform high-level handlers about JTAG state changes */
126 static struct jtag_event_callback *jtag_event_callbacks;
129 static int speed_khz;
130 /* speed to fallback to when RCLK is requested but not supported */
131 static int rclk_fallback_speed_khz;
132 static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
133 static int jtag_speed;
135 static struct jtag_interface *jtag;
138 struct jtag_interface *jtag_interface;
140 void jtag_set_flush_queue_sleep(int ms)
142 jtag_flush_queue_sleep = ms;
145 void jtag_set_error(int error)
147 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
152 int jtag_error_clear(void)
154 int temp = jtag_error;
155 jtag_error = ERROR_OK;
161 static bool jtag_poll = 1;
163 bool is_jtag_poll_safe(void)
165 /* Polling can be disabled explicitly with set_enabled(false).
166 * It is also implicitly disabled while TRST is active and
167 * while SRST is gating the JTAG clock.
169 if (!jtag_poll || jtag_trst != 0)
171 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
174 bool jtag_poll_get_enabled(void)
179 void jtag_poll_set_enabled(bool value)
186 struct jtag_tap *jtag_all_taps(void)
188 return __jtag_all_taps;
191 unsigned jtag_tap_count(void)
193 return jtag_num_taps;
196 unsigned jtag_tap_count_enabled(void)
198 struct jtag_tap *t = jtag_all_taps();
208 /** Append a new TAP to the chain of all taps. */
209 void jtag_tap_add(struct jtag_tap *t)
211 t->abs_chain_position = jtag_num_taps++;
213 struct jtag_tap **tap = &__jtag_all_taps;
215 tap = &(*tap)->next_tap;
219 /* returns a pointer to the n-th device in the scan chain */
220 struct jtag_tap *jtag_tap_by_position(unsigned n)
222 struct jtag_tap *t = jtag_all_taps();
230 struct jtag_tap *jtag_tap_by_string(const char *s)
232 /* try by name first */
233 struct jtag_tap *t = jtag_all_taps();
236 if (0 == strcmp(t->dotted_name, s))
241 /* no tap found by name, so try to parse the name as a number */
243 if (parse_uint(s, &n) != ERROR_OK)
246 /* FIXME remove this numeric fallback code late June 2010, along
247 * with all info in the User's Guide that TAPs have numeric IDs.
248 * Also update "scan_chain" output to not display the numbers.
250 t = jtag_tap_by_position(n);
252 LOG_WARNING("Specify TAP '%s' by name, not number %u",
258 struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p)
260 p = p ? p->next_tap : jtag_all_taps();
269 const char *jtag_tap_name(const struct jtag_tap *tap)
271 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
275 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
277 struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
279 if (callback == NULL)
280 return ERROR_COMMAND_SYNTAX_ERROR;
283 while ((*callbacks_p)->next)
284 callbacks_p = &((*callbacks_p)->next);
285 callbacks_p = &((*callbacks_p)->next);
288 (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
289 (*callbacks_p)->callback = callback;
290 (*callbacks_p)->priv = priv;
291 (*callbacks_p)->next = NULL;
296 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
298 struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
300 if (callback == NULL)
301 return ERROR_COMMAND_SYNTAX_ERROR;
304 if (((*p)->priv != priv) || ((*p)->callback != callback)) {
317 int jtag_call_event_callbacks(enum jtag_event event)
319 struct jtag_event_callback *callback = jtag_event_callbacks;
321 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
324 struct jtag_event_callback *next;
326 /* callback may remove itself */
327 next = callback->next;
328 callback->callback(event, callback->priv);
335 static void jtag_checks(void)
337 assert(jtag_trst == 0);
340 static void jtag_prelude(tap_state_t state)
344 assert(state != TAP_INVALID);
346 cmd_queue_cur_state = state;
349 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
354 int retval = interface_jtag_add_ir_scan(active, in_fields, state);
355 jtag_set_error(retval);
358 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
360 const struct scan_field *in_fields,
363 jtag_add_ir_scan_noverify(active, in_fields, state);
366 /* If fields->in_value is filled out, then the captured IR value will be checked */
367 void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
369 assert(state != TAP_RESET);
371 if (jtag_verify && jtag_verify_capture_ir) {
372 /* 8 x 32 bit id's is enough for all invocations */
374 /* if we are to run a verification of the ir scan, we need to get the input back.
375 * We may have to allocate space if the caller didn't ask for the input back.
377 in_fields->check_value = active->expected;
378 in_fields->check_mask = active->expected_mask;
379 jtag_add_scan_check(active, jtag_add_ir_scan_noverify_callback, 1, in_fields,
382 jtag_add_ir_scan_noverify(active, in_fields, state);
385 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
388 assert(out_bits != NULL);
389 assert(state != TAP_RESET);
393 int retval = interface_jtag_add_plain_ir_scan(
394 num_bits, out_bits, in_bits, state);
395 jtag_set_error(retval);
398 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
399 uint8_t *in_check_mask, int num_bits);
401 static int jtag_check_value_mask_callback(jtag_callback_data_t data0,
402 jtag_callback_data_t data1,
403 jtag_callback_data_t data2,
404 jtag_callback_data_t data3)
406 return jtag_check_value_inner((uint8_t *)data0,
412 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
413 struct jtag_tap *active,
415 const struct scan_field *in_fields,
417 int in_num_fields, struct scan_field *in_fields, tap_state_t state)
419 jtag_add_scan(active, in_num_fields, in_fields, state);
421 for (int i = 0; i < in_num_fields; i++) {
422 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL)) {
423 /* this is synchronous for a minidriver */
424 jtag_add_callback4(jtag_check_value_mask_callback,
425 (jtag_callback_data_t)in_fields[i].in_value,
426 (jtag_callback_data_t)in_fields[i].check_value,
427 (jtag_callback_data_t)in_fields[i].check_mask,
428 (jtag_callback_data_t)in_fields[i].num_bits);
433 void jtag_add_dr_scan_check(struct jtag_tap *active,
435 struct scan_field *in_fields,
439 jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
441 jtag_add_dr_scan(active, in_num_fields, in_fields, state);
445 void jtag_add_dr_scan(struct jtag_tap *active,
447 const struct scan_field *in_fields,
450 assert(state != TAP_RESET);
455 retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
456 jtag_set_error(retval);
459 void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
462 assert(out_bits != NULL);
463 assert(state != TAP_RESET);
468 retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
469 jtag_set_error(retval);
472 void jtag_add_tlr(void)
474 jtag_prelude(TAP_RESET);
475 jtag_set_error(interface_jtag_add_tlr());
477 /* NOTE: order here matches TRST path in jtag_add_reset() */
478 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
479 jtag_notify_event(JTAG_TRST_ASSERTED);
483 * If supported by the underlying adapter, this clocks a raw bit sequence
484 * onto TMS for switching betwen JTAG and SWD modes.
486 * DO NOT use this to bypass the integrity checks and logging provided
487 * by the jtag_add_pathmove() and jtag_add_statemove() calls.
489 * @param nbits How many bits to clock out.
490 * @param seq The bit sequence. The LSB is bit 0 of seq[0].
491 * @param state The JTAG tap state to record on completion. Use
492 * TAP_INVALID to represent being in in SWD mode.
494 * @todo Update naming conventions to stop assuming everything is JTAG.
496 int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
500 if (!(jtag->supported & DEBUG_CAP_TMS_SEQ))
501 return ERROR_JTAG_NOT_IMPLEMENTED;
504 cmd_queue_cur_state = state;
506 retval = interface_add_tms_seq(nbits, seq, state);
507 jtag_set_error(retval);
511 void jtag_add_pathmove(int num_states, const tap_state_t *path)
513 tap_state_t cur_state = cmd_queue_cur_state;
515 /* the last state has to be a stable state */
516 if (!tap_is_state_stable(path[num_states - 1])) {
517 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
518 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
522 for (int i = 0; i < num_states; i++) {
523 if (path[i] == TAP_RESET) {
524 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
525 jtag_set_error(ERROR_JTAG_STATE_INVALID);
529 if (tap_state_transition(cur_state, true) != path[i] &&
530 tap_state_transition(cur_state, false) != path[i]) {
531 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
532 tap_state_name(cur_state), tap_state_name(path[i]));
533 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
541 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
542 cmd_queue_cur_state = path[num_states - 1];
545 int jtag_add_statemove(tap_state_t goal_state)
547 tap_state_t cur_state = cmd_queue_cur_state;
549 if (goal_state != cur_state) {
550 LOG_DEBUG("cur_state=%s goal_state=%s",
551 tap_state_name(cur_state),
552 tap_state_name(goal_state));
555 /* If goal is RESET, be paranoid and force that that transition
556 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
558 if (goal_state == TAP_RESET)
560 else if (goal_state == cur_state)
563 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state)) {
564 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
565 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
566 tap_state_t moves[8];
567 assert(tms_count < ARRAY_SIZE(moves));
569 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
570 bool bit = tms_bits & 1;
572 cur_state = tap_state_transition(cur_state, bit);
573 moves[i] = cur_state;
576 jtag_add_pathmove(tms_count, moves);
577 } else if (tap_state_transition(cur_state, true) == goal_state
578 || tap_state_transition(cur_state, false) == goal_state)
579 jtag_add_pathmove(1, &goal_state);
586 void jtag_add_runtest(int num_cycles, tap_state_t state)
589 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
593 void jtag_add_clocks(int num_cycles)
595 if (!tap_is_state_stable(cmd_queue_cur_state)) {
596 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
597 tap_state_name(cmd_queue_cur_state));
598 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
602 if (num_cycles > 0) {
604 jtag_set_error(interface_jtag_add_clocks(num_cycles));
608 void swd_add_reset(int req_srst)
611 if (!(jtag_reset_config & RESET_HAS_SRST)) {
612 LOG_ERROR("BUG: can't assert SRST");
613 jtag_set_error(ERROR_FAIL);
619 /* Maybe change SRST signal state */
620 if (jtag_srst != req_srst) {
623 retval = interface_jtag_add_reset(0, req_srst);
624 if (retval != ERROR_OK)
625 jtag_set_error(retval);
627 retval = jtag_execute_queue();
629 if (retval != ERROR_OK) {
630 LOG_ERROR("TRST/SRST error");
634 /* SRST resets everything hooked up to that signal */
635 jtag_srst = req_srst;
637 LOG_DEBUG("SRST line asserted");
638 if (adapter_nsrst_assert_width)
639 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
641 LOG_DEBUG("SRST line released");
642 if (adapter_nsrst_delay)
643 jtag_add_sleep(adapter_nsrst_delay * 1000);
648 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
650 int trst_with_tlr = 0;
654 /* Without SRST, we must use target-specific JTAG operations
655 * on each target; callers should not be requesting SRST when
656 * that signal doesn't exist.
658 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
659 * can kick in even if the JTAG adapter can't drive TRST.
662 if (!(jtag_reset_config & RESET_HAS_SRST)) {
663 LOG_ERROR("BUG: can't assert SRST");
664 jtag_set_error(ERROR_FAIL);
667 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
668 && !req_tlr_or_trst) {
669 LOG_ERROR("BUG: can't assert only SRST");
670 jtag_set_error(ERROR_FAIL);
676 /* JTAG reset (entry to TAP_RESET state) can always be achieved
677 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
678 * state first. TRST accelerates it, and bypasses those states.
680 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
681 * can kick in even if the JTAG adapter can't drive SRST.
683 if (req_tlr_or_trst) {
684 if (!(jtag_reset_config & RESET_HAS_TRST))
686 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
693 /* Maybe change TRST and/or SRST signal state */
694 if (jtag_srst != new_srst || jtag_trst != new_trst) {
697 retval = interface_jtag_add_reset(new_trst, new_srst);
698 if (retval != ERROR_OK)
699 jtag_set_error(retval);
701 retval = jtag_execute_queue();
703 if (retval != ERROR_OK) {
704 LOG_ERROR("TRST/SRST error");
709 /* SRST resets everything hooked up to that signal */
710 if (jtag_srst != new_srst) {
711 jtag_srst = new_srst;
713 LOG_DEBUG("SRST line asserted");
714 if (adapter_nsrst_assert_width)
715 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
717 LOG_DEBUG("SRST line released");
718 if (adapter_nsrst_delay)
719 jtag_add_sleep(adapter_nsrst_delay * 1000);
723 /* Maybe enter the JTAG TAP_RESET state ...
724 * - using only TMS, TCK, and the JTAG state machine
725 * - or else more directly, using TRST
727 * TAP_RESET should be invisible to non-debug parts of the system.
730 LOG_DEBUG("JTAG reset with TLR instead of TRST");
733 } else if (jtag_trst != new_trst) {
734 jtag_trst = new_trst;
736 LOG_DEBUG("TRST line asserted");
737 tap_set_state(TAP_RESET);
738 if (jtag_ntrst_assert_width)
739 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
741 LOG_DEBUG("TRST line released");
742 if (jtag_ntrst_delay)
743 jtag_add_sleep(jtag_ntrst_delay * 1000);
745 /* We just asserted nTRST, so we're now in TAP_RESET.
746 * Inform possible listeners about this, now that
747 * JTAG instructions and data can be shifted. This
748 * sequence must match jtag_add_tlr().
750 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
751 jtag_notify_event(JTAG_TRST_ASSERTED);
756 void jtag_add_sleep(uint32_t us)
758 /** @todo Here, keep_alive() appears to be a layering violation!!! */
760 jtag_set_error(interface_jtag_add_sleep(us));
763 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
764 uint8_t *in_check_mask, int num_bits)
766 int retval = ERROR_OK;
770 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
772 compare_failed = buf_cmp(captured, in_check_value, num_bits);
774 if (compare_failed) {
775 char *captured_str, *in_check_value_str;
776 int bits = (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits;
778 /* NOTE: we've lost diagnostic context here -- 'which tap' */
780 captured_str = buf_to_str(captured, bits, 16);
781 in_check_value_str = buf_to_str(in_check_value, bits, 16);
783 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
785 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
788 free(in_check_value_str);
791 char *in_check_mask_str;
793 in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
794 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
795 free(in_check_mask_str);
798 retval = ERROR_JTAG_QUEUE_FAILED;
803 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
805 assert(field->in_value != NULL);
808 /* no checking to do */
812 jtag_execute_queue_noclear();
814 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
815 jtag_set_error(retval);
818 int default_interface_jtag_execute_queue(void)
821 LOG_ERROR("No JTAG interface configured yet. "
822 "Issue 'init' command in startup scripts "
823 "before communicating with targets.");
827 return jtag->execute_queue();
830 void jtag_execute_queue_noclear(void)
832 jtag_flush_queue_count++;
833 jtag_set_error(interface_jtag_execute_queue());
835 if (jtag_flush_queue_sleep > 0) {
836 /* For debug purposes it can be useful to test performance
837 * or behavior when delaying after flushing the queue,
838 * e.g. to simulate long roundtrip times.
840 usleep(jtag_flush_queue_sleep * 1000);
844 int jtag_get_flush_queue_count(void)
846 return jtag_flush_queue_count;
849 int jtag_execute_queue(void)
851 jtag_execute_queue_noclear();
852 return jtag_error_clear();
855 static int jtag_reset_callback(enum jtag_event event, void *priv)
857 struct jtag_tap *tap = priv;
859 if (event == JTAG_TRST_ASSERTED) {
860 tap->enabled = !tap->disabled_after_reset;
862 /* current instruction is either BYPASS or IDCODE */
863 buf_set_ones(tap->cur_instr, tap->ir_length);
870 /* sleep at least us microseconds. When we sleep more than 1000ms we
871 * do an alive sleep, i.e. keep GDB alive. Note that we could starve
872 * GDB if we slept for <1000ms many times.
874 void jtag_sleep(uint32_t us)
879 alive_sleep((us+999)/1000);
882 /* Maximum number of enabled JTAG devices we expect in the scan chain,
883 * plus one (to detect garbage at the end). Devices that don't support
884 * IDCODE take up fewer bits, possibly allowing a few more devices.
886 #define JTAG_MAX_CHAIN_SIZE 20
888 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
889 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
890 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
892 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
893 * know that no valid TAP will have it as an IDCODE value.
895 #define END_OF_CHAIN_FLAG 0xffffffff
897 /* a larger IR length than we ever expect to autoprobe */
898 #define JTAG_IRLEN_MAX 60
900 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
902 struct scan_field field = {
903 .num_bits = num_idcode * 32,
904 .out_value = idcode_buffer,
905 .in_value = idcode_buffer,
908 /* initialize to the end of chain ID value */
909 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
910 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
912 jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
914 return jtag_execute_queue();
917 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
919 uint8_t zero_check = 0x0;
920 uint8_t one_check = 0xff;
922 for (unsigned i = 0; i < count * 4; i++) {
923 zero_check |= idcodes[i];
924 one_check &= idcodes[i];
927 /* if there wasn't a single non-zero bit or if all bits were one,
928 * the scan is not valid. We wrote a mix of both values; either
930 * - There's a hardware issue (almost certainly):
931 * + all-zeroes can mean a target stuck in JTAG reset
932 * + all-ones tends to mean no target
933 * - The scan chain is WAY longer than we can handle, *AND* either
934 * + there are several hundreds of TAPs in bypass, or
935 * + at least a few dozen TAPs all have an all-ones IDCODE
937 if (zero_check == 0x00 || one_check == 0xff) {
938 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
939 (zero_check == 0x00) ? "zeroes" : "ones");
940 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
946 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
947 const char *name, uint32_t idcode)
949 log_printf_lf(level, __FILE__, __LINE__, __func__,
950 "JTAG tap: %s %16.16s: 0x%08x "
951 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
953 (unsigned int)idcode,
954 (unsigned int)EXTRACT_MFG(idcode),
955 (unsigned int)EXTRACT_PART(idcode),
956 (unsigned int)EXTRACT_VER(idcode));
959 static bool jtag_idcode_is_final(uint32_t idcode)
962 * Some devices, such as AVR8, will output all 1's instead
963 * of TDI input value at end of chain. Allow those values
964 * instead of failing.
966 return idcode == END_OF_CHAIN_FLAG;
970 * This helper checks that remaining bits in the examined chain data are
971 * all as expected, but a single JTAG device requires only 64 bits to be
972 * read back correctly. This can help identify and diagnose problems
973 * with the JTAG chain earlier, gives more helpful/explicit error messages.
974 * Returns TRUE iff garbage was found.
976 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
978 bool triggered = false;
979 for (; count < max - 31; count += 32) {
980 uint32_t idcode = buf_get_u32(idcodes, count, 32);
982 /* do not trigger the warning if the data looks good */
983 if (jtag_idcode_is_final(idcode))
985 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
986 count, (unsigned int)idcode);
992 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
994 uint32_t idcode = tap->idcode;
996 /* ignore expected BYPASS codes; warn otherwise */
997 if (0 == tap->expected_ids_cnt && !idcode)
1000 /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1001 uint32_t mask = tap->ignore_version ? ~(0xf << 28) : ~0;
1005 /* Loop over the expected identification codes and test for a match */
1006 unsigned ii, limit = tap->expected_ids_cnt;
1008 for (ii = 0; ii < limit; ii++) {
1009 uint32_t expected = tap->expected_ids[ii] & mask;
1011 if (idcode == expected)
1014 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1015 if (0 == tap->expected_ids[ii])
1019 /* If none of the expected ids matched, warn */
1020 jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
1021 tap->dotted_name, tap->idcode);
1022 for (ii = 0; ii < limit; ii++) {
1025 snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, limit);
1026 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1027 tap->dotted_name, tap->expected_ids[ii]);
1032 /* Try to examine chain layout according to IEEE 1149.1 §12
1033 * This is called a "blind interrogation" of the scan chain.
1035 static int jtag_examine_chain(void)
1037 uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1041 bool autoprobe = false;
1043 /* DR scan to collect BYPASS or IDCODE register contents.
1044 * Then make sure the scan data has both ones and zeroes.
1046 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1047 retval = jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
1048 if (retval != ERROR_OK)
1050 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
1051 return ERROR_JTAG_INIT_FAILED;
1053 /* point at the 1st tap */
1054 struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
1060 tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
1061 tap = jtag_tap_next_enabled(tap)) {
1062 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1064 if ((idcode & 1) == 0) {
1065 /* Zero for LSB indicates a device in bypass */
1066 LOG_INFO("TAP %s does not have IDCODE",
1069 tap->hasidcode = false;
1073 /* Friendly devices support IDCODE */
1074 tap->hasidcode = true;
1075 jtag_examine_chain_display(LOG_LVL_INFO,
1077 tap->dotted_name, idcode);
1081 tap->idcode = idcode;
1083 /* ensure the TAP ID matches what was expected */
1084 if (!jtag_examine_chain_match_tap(tap))
1085 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1088 /* Fail if too many TAPs were enabled for us to verify them all. */
1090 LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
1092 return ERROR_JTAG_INIT_FAILED;
1095 /* if autoprobing, the tap list is still empty ... populate it! */
1096 while (autoprobe && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31) {
1100 /* Is there another TAP? */
1101 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1102 if (jtag_idcode_is_final(idcode))
1105 /* Default everything in this TAP except IR length.
1107 * REVISIT create a jtag_alloc(chip, tap) routine, and
1108 * share it with jim_newtap_cmd().
1110 tap = calloc(1, sizeof *tap);
1114 sprintf(buf, "auto%d", tapcount++);
1115 tap->chip = strdup(buf);
1116 tap->tapname = strdup("tap");
1118 sprintf(buf, "%s.%s", tap->chip, tap->tapname);
1119 tap->dotted_name = strdup(buf);
1121 /* tap->ir_length == 0 ... signifying irlen autoprobe */
1122 tap->ir_capture_mask = 0x03;
1123 tap->ir_capture_value = 0x01;
1125 tap->enabled = true;
1127 if ((idcode & 1) == 0) {
1129 tap->hasidcode = false;
1132 tap->hasidcode = true;
1133 tap->idcode = idcode;
1135 tap->expected_ids_cnt = 1;
1136 tap->expected_ids = malloc(sizeof(uint32_t));
1137 tap->expected_ids[0] = idcode;
1140 LOG_WARNING("AUTO %s - use \"jtag newtap "
1141 "%s %s -expected-id 0x%8.8" PRIx32 " ...\"",
1142 tap->dotted_name, tap->chip, tap->tapname,
1148 /* After those IDCODE or BYPASS register values should be
1149 * only the data we fed into the scan chain.
1151 if (jtag_examine_chain_end(idcode_buffer, bit_count,
1152 8 * sizeof(idcode_buffer))) {
1153 LOG_ERROR("double-check your JTAG setup (interface, "
1154 "speed, missing TAPs, ...)");
1155 return ERROR_JTAG_INIT_FAILED;
1158 /* Return success or, for backwards compatibility if only
1159 * some IDCODE values mismatched, a soft/continuable fault.
1165 * Validate the date loaded by entry to the Capture-IR state, to help
1166 * find errors related to scan chain configuration (wrong IR lengths)
1169 * Entry state can be anything. On non-error exit, all TAPs are in
1170 * bypass mode. On error exits, the scan chain is reset.
1172 static int jtag_validate_ircapture(void)
1174 struct jtag_tap *tap;
1175 int total_ir_length = 0;
1176 uint8_t *ir_test = NULL;
1177 struct scan_field field;
1182 /* when autoprobing, accomodate huge IR lengths */
1183 for (tap = NULL, total_ir_length = 0;
1184 (tap = jtag_tap_next_enabled(tap)) != NULL;
1185 total_ir_length += tap->ir_length) {
1186 if (tap->ir_length == 0)
1187 total_ir_length += JTAG_IRLEN_MAX;
1190 /* increase length to add 2 bit sentinel after scan */
1191 total_ir_length += 2;
1193 ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1194 if (ir_test == NULL)
1197 /* after this scan, all TAPs will capture BYPASS instructions */
1198 buf_set_ones(ir_test, total_ir_length);
1200 field.num_bits = total_ir_length;
1201 field.out_value = ir_test;
1202 field.in_value = ir_test;
1204 jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
1206 LOG_DEBUG("IR capture validation scan");
1207 retval = jtag_execute_queue();
1208 if (retval != ERROR_OK)
1215 tap = jtag_tap_next_enabled(tap);
1219 /* If we're autoprobing, guess IR lengths. They must be at
1220 * least two bits. Guessing will fail if (a) any TAP does
1221 * not conform to the JTAG spec; or (b) when the upper bits
1222 * captured from some conforming TAP are nonzero. Or if
1223 * (c) an IR length is longer than 32 bits -- which is only
1224 * an implementation limit, which could someday be raised.
1226 * REVISIT optimization: if there's a *single* TAP we can
1227 * lift restrictions (a) and (b) by scanning a recognizable
1228 * pattern before the all-ones BYPASS. Check for where the
1229 * pattern starts in the result, instead of an 0...01 value.
1231 * REVISIT alternative approach: escape to some tcl code
1232 * which could provide more knowledge, based on IDCODE; and
1233 * only guess when that has no success.
1235 if (tap->ir_length == 0) {
1237 while ((val = buf_get_u64(ir_test, chain_pos, tap->ir_length + 1)) == 1
1238 && tap->ir_length <= 64) {
1241 LOG_WARNING("AUTO %s - use \"... -irlen %d\"",
1242 jtag_tap_name(tap), tap->ir_length);
1245 /* Validate the two LSBs, which must be 01 per JTAG spec.
1247 * Or ... more bits could be provided by TAP declaration.
1248 * Plus, some taps (notably in i.MX series chips) violate
1249 * this part of the JTAG spec, so their capture mask/value
1250 * attributes might disable this test.
1252 val = buf_get_u64(ir_test, chain_pos, tap->ir_length);
1253 if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1254 LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64 " not 0x%0*" PRIx32,
1256 (tap->ir_length + 7) / tap->ir_length, val,
1257 (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
1259 retval = ERROR_JTAG_INIT_FAILED;
1262 LOG_DEBUG("%s: IR capture 0x%0*" PRIx64, jtag_tap_name(tap),
1263 (tap->ir_length + 7) / tap->ir_length, val);
1264 chain_pos += tap->ir_length;
1267 /* verify the '11' sentinel we wrote is returned at the end */
1268 val = buf_get_u64(ir_test, chain_pos, 2);
1270 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1272 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1275 retval = ERROR_JTAG_INIT_FAILED;
1280 if (retval != ERROR_OK) {
1282 jtag_execute_queue();
1287 void jtag_tap_init(struct jtag_tap *tap)
1289 unsigned ir_len_bits;
1290 unsigned ir_len_bytes;
1292 /* if we're autoprobing, cope with potentially huge ir_length */
1293 ir_len_bits = tap->ir_length ? : JTAG_IRLEN_MAX;
1294 ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
1296 tap->expected = calloc(1, ir_len_bytes);
1297 tap->expected_mask = calloc(1, ir_len_bytes);
1298 tap->cur_instr = malloc(ir_len_bytes);
1300 /** @todo cope better with ir_length bigger than 32 bits */
1301 if (ir_len_bits > 32)
1304 buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
1305 buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
1307 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1309 buf_set_ones(tap->cur_instr, tap->ir_length);
1311 /* register the reset callback for the TAP */
1312 jtag_register_event_callback(&jtag_reset_callback, tap);
1315 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1316 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1317 tap->abs_chain_position, tap->ir_length,
1318 (unsigned) tap->ir_capture_value,
1319 (unsigned) tap->ir_capture_mask);
1322 void jtag_tap_free(struct jtag_tap *tap)
1324 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1326 /** @todo is anything missing? no memory leaks please */
1327 free(tap->expected);
1328 free(tap->expected_ids);
1331 free(tap->dotted_name);
1336 * Do low-level setup like initializing registers, output signals,
1339 int adapter_init(struct command_context *cmd_ctx)
1344 if (!jtag_interface) {
1345 /* nothing was previously specified by "interface" command */
1346 LOG_ERROR("Debug Adapter has to be specified, "
1347 "see \"interface\" command");
1348 return ERROR_JTAG_INVALID_INTERFACE;
1352 retval = jtag_interface->init();
1353 if (retval != ERROR_OK)
1355 jtag = jtag_interface;
1357 /* LEGACY SUPPORT ... adapter drivers must declare what
1358 * transports they allow. Until they all do so, assume
1359 * the legacy drivers are JTAG-only
1361 if (!transports_are_declared()) {
1362 LOG_ERROR("Adapter driver '%s' did not declare "
1363 "which transports it allows; assuming "
1364 "JTAG-only", jtag->name);
1365 retval = allow_transports(cmd_ctx, jtag_only);
1366 if (retval != ERROR_OK)
1370 if (jtag->speed == NULL) {
1371 LOG_INFO("This adapter doesn't support configurable speed");
1375 if (CLOCK_MODE_UNSELECTED == clock_mode) {
1376 LOG_ERROR("An adapter speed is not selected in the init script."
1377 " Insert a call to adapter_khz or jtag_rclk to proceed.");
1378 return ERROR_JTAG_INIT_FAILED;
1381 int requested_khz = jtag_get_speed_khz();
1382 int actual_khz = requested_khz;
1383 int jtag_speed_var = 0;
1384 retval = jtag_get_speed(&jtag_speed_var);
1385 if (retval != ERROR_OK)
1387 retval = jtag->speed(jtag_speed_var);
1388 if (retval != ERROR_OK)
1390 retval = jtag_get_speed_readable(&actual_khz);
1391 if (ERROR_OK != retval)
1392 LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
1393 else if (actual_khz) {
1394 /* Adaptive clocking -- JTAG-specific */
1395 if ((CLOCK_MODE_RCLK == clock_mode)
1396 || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz)) {
1397 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1400 LOG_INFO("clock speed %d kHz", actual_khz);
1402 LOG_INFO("RCLK (adaptive clock speed)");
1407 int jtag_init_inner(struct command_context *cmd_ctx)
1409 struct jtag_tap *tap;
1411 bool issue_setup = true;
1413 LOG_DEBUG("Init JTAG chain");
1415 tap = jtag_tap_next_enabled(NULL);
1417 /* Once JTAG itself is properly set up, and the scan chain
1418 * isn't absurdly large, IDCODE autoprobe should work fine.
1420 * But ... IRLEN autoprobe can fail even on systems which
1421 * are fully conformant to JTAG. Also, JTAG setup can be
1422 * quite finicky on some systems.
1424 * REVISIT: if TAP autoprobe works OK, then in many cases
1425 * we could escape to tcl code and set up targets based on
1426 * the TAP's IDCODE values.
1428 LOG_WARNING("There are no enabled taps. "
1429 "AUTO PROBING MIGHT NOT WORK!!");
1431 /* REVISIT default clock will often be too fast ... */
1435 retval = jtag_execute_queue();
1436 if (retval != ERROR_OK)
1439 /* Examine DR values first. This discovers problems which will
1440 * prevent communication ... hardware issues like TDO stuck, or
1441 * configuring the wrong number of (enabled) TAPs.
1443 retval = jtag_examine_chain();
1446 /* complete success */
1449 /* For backward compatibility reasons, try coping with
1450 * configuration errors involving only ID mismatches.
1451 * We might be able to talk to the devices.
1453 * Also the device might be powered down during startup.
1455 * After OpenOCD starts, we can try to power on the device
1458 LOG_ERROR("Trying to use configured scan chain anyway...");
1459 issue_setup = false;
1463 /* Now look at IR values. Problems here will prevent real
1464 * communication. They mostly mean that the IR length is
1465 * wrong ... or that the IR capture value is wrong. (The
1466 * latter is uncommon, but easily worked around: provide
1467 * ircapture/irmask values during TAP setup.)
1469 retval = jtag_validate_ircapture();
1470 if (retval != ERROR_OK) {
1471 /* The target might be powered down. The user
1472 * can power it up and reset it after firing
1475 issue_setup = false;
1479 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1481 LOG_WARNING("Bypassing JTAG setup events due to errors");
1487 int adapter_quit(void)
1489 if (!jtag || !jtag->quit)
1492 /* close the JTAG interface */
1493 int result = jtag->quit();
1494 if (ERROR_OK != result)
1495 LOG_ERROR("failed: %d", result);
1500 int swd_init_reset(struct command_context *cmd_ctx)
1502 int retval = adapter_init(cmd_ctx);
1503 if (retval != ERROR_OK)
1506 LOG_DEBUG("Initializing with hard SRST reset");
1508 if (jtag_reset_config & RESET_HAS_SRST)
1511 retval = jtag_execute_queue();
1515 int jtag_init_reset(struct command_context *cmd_ctx)
1517 int retval = adapter_init(cmd_ctx);
1518 if (retval != ERROR_OK)
1521 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1524 * This procedure is used by default when OpenOCD triggers a reset.
1525 * It's now done through an overridable Tcl "init_reset" wrapper.
1527 * This started out as a more powerful "get JTAG working" reset than
1528 * jtag_init_inner(), applying TRST because some chips won't activate
1529 * JTAG without a TRST cycle (presumed to be async, though some of
1530 * those chips synchronize JTAG activation using TCK).
1532 * But some chips only activate JTAG as part of an SRST cycle; SRST
1533 * got mixed in. So it became a hard reset routine, which got used
1534 * in more places, and which coped with JTAG reset being forced as
1535 * part of SRST (srst_pulls_trst).
1537 * And even more corner cases started to surface: TRST and/or SRST
1538 * assertion timings matter; some chips need other JTAG operations;
1539 * TRST/SRST sequences can need to be different from these, etc.
1541 * Systems should override that wrapper to support system-specific
1542 * requirements that this not-fully-generic code doesn't handle.
1544 * REVISIT once Tcl code can read the reset_config modes, this won't
1545 * need to be a C routine at all...
1547 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1548 if (jtag_reset_config & RESET_HAS_SRST) {
1549 jtag_add_reset(1, 1);
1550 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1551 jtag_add_reset(0, 1);
1554 /* some targets enable us to connect with srst asserted */
1555 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1556 if (jtag_reset_config & RESET_SRST_NO_GATING)
1557 jtag_add_reset(0, 1);
1559 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1560 jtag_add_reset(0, 0);
1563 jtag_add_reset(0, 0);
1564 retval = jtag_execute_queue();
1565 if (retval != ERROR_OK)
1568 /* Check that we can communication on the JTAG chain + eventually we want to
1569 * be able to perform enumeration only after OpenOCD has started
1570 * telnet and GDB server
1572 * That would allow users to more easily perform any magic they need to before
1575 return jtag_init_inner(cmd_ctx);
1578 int jtag_init(struct command_context *cmd_ctx)
1580 int retval = adapter_init(cmd_ctx);
1581 if (retval != ERROR_OK)
1584 /* guard against oddball hardware: force resets to be inactive */
1585 jtag_add_reset(0, 0);
1587 /* some targets enable us to connect with srst asserted */
1588 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1589 if (jtag_reset_config & RESET_SRST_NO_GATING)
1590 jtag_add_reset(0, 1);
1592 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1594 retval = jtag_execute_queue();
1595 if (retval != ERROR_OK)
1598 if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1604 unsigned jtag_get_speed_khz(void)
1609 static int adapter_khz_to_speed(unsigned khz, int *speed)
1611 LOG_DEBUG("convert khz to interface specific speed value");
1614 LOG_DEBUG("have interface set up");
1616 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1617 if (ERROR_OK != retval)
1619 *speed = speed_div1;
1624 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int *speed)
1626 int retval = adapter_khz_to_speed(0, speed);
1627 if ((ERROR_OK != retval) && fallback_speed_khz) {
1628 LOG_DEBUG("trying fallback speed...");
1629 retval = adapter_khz_to_speed(fallback_speed_khz, speed);
1634 static int jtag_set_speed(int speed)
1637 /* this command can be called during CONFIG,
1638 * in which case jtag isn't initialized */
1639 return jtag ? jtag->speed(speed) : ERROR_OK;
1642 int jtag_config_khz(unsigned khz)
1644 LOG_DEBUG("handle jtag khz");
1645 clock_mode = CLOCK_MODE_KHZ;
1647 int retval = adapter_khz_to_speed(khz, &speed);
1648 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1651 int jtag_config_rclk(unsigned fallback_speed_khz)
1653 LOG_DEBUG("handle jtag rclk");
1654 clock_mode = CLOCK_MODE_RCLK;
1655 rclk_fallback_speed_khz = fallback_speed_khz;
1657 int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1658 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1661 int jtag_get_speed(int *speed)
1663 switch (clock_mode) {
1664 case CLOCK_MODE_KHZ:
1665 adapter_khz_to_speed(jtag_get_speed_khz(), speed);
1667 case CLOCK_MODE_RCLK:
1668 jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
1671 LOG_ERROR("BUG: unknown jtag clock mode");
1677 int jtag_get_speed_readable(int *khz)
1679 int jtag_speed_var = 0;
1680 int retval = jtag_get_speed(&jtag_speed_var);
1681 if (retval != ERROR_OK)
1683 return jtag ? jtag->speed_div(jtag_speed_var, khz) : ERROR_OK;
1686 void jtag_set_verify(bool enable)
1688 jtag_verify = enable;
1691 bool jtag_will_verify()
1696 void jtag_set_verify_capture_ir(bool enable)
1698 jtag_verify_capture_ir = enable;
1701 bool jtag_will_verify_capture_ir()
1703 return jtag_verify_capture_ir;
1706 int jtag_power_dropout(int *dropout)
1709 /* TODO: as the jtag interface is not valid all
1710 * we can do at the moment is exit OpenOCD */
1711 LOG_ERROR("No Valid JTAG Interface Configured.");
1714 return jtag->power_dropout(dropout);
1717 int jtag_srst_asserted(int *srst_asserted)
1719 return jtag->srst_asserted(srst_asserted);
1722 enum reset_types jtag_get_reset_config(void)
1724 return jtag_reset_config;
1726 void jtag_set_reset_config(enum reset_types type)
1728 jtag_reset_config = type;
1731 int jtag_get_trst(void)
1735 int jtag_get_srst(void)
1740 void jtag_set_nsrst_delay(unsigned delay)
1742 adapter_nsrst_delay = delay;
1744 unsigned jtag_get_nsrst_delay(void)
1746 return adapter_nsrst_delay;
1748 void jtag_set_ntrst_delay(unsigned delay)
1750 jtag_ntrst_delay = delay;
1752 unsigned jtag_get_ntrst_delay(void)
1754 return jtag_ntrst_delay;
1758 void jtag_set_nsrst_assert_width(unsigned delay)
1760 adapter_nsrst_assert_width = delay;
1762 unsigned jtag_get_nsrst_assert_width(void)
1764 return adapter_nsrst_assert_width;
1766 void jtag_set_ntrst_assert_width(unsigned delay)
1768 jtag_ntrst_assert_width = delay;
1770 unsigned jtag_get_ntrst_assert_width(void)
1772 return jtag_ntrst_assert_width;
1775 static int jtag_select(struct command_context *ctx)
1779 /* NOTE: interface init must already have been done.
1780 * That works with only C code ... no Tcl glue required.
1783 retval = jtag_register_commands(ctx);
1785 if (retval != ERROR_OK)
1788 retval = svf_register_commands(ctx);
1790 if (retval != ERROR_OK)
1793 return xsvf_register_commands(ctx);
1796 static struct transport jtag_transport = {
1798 .select = jtag_select,
1802 static void jtag_constructor(void) __attribute__((constructor));
1803 static void jtag_constructor(void)
1805 transport_register(&jtag_transport);
1808 /** Returns true if the current debug session
1809 * is using JTAG as its transport.
1811 bool transport_is_jtag(void)
1813 return get_current_transport() == &jtag_transport;
1816 void adapter_assert_reset(void)
1818 if (transport_is_jtag()) {
1819 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1820 jtag_add_reset(1, 1);
1822 jtag_add_reset(0, 1);
1823 } else if (transport_is_swd())
1825 else if (transport_is_cmsis_dap())
1826 swd_add_reset(1); /* FIXME */
1827 else if (get_current_transport() != NULL)
1828 LOG_ERROR("reset is not supported on %s",
1829 get_current_transport()->name);
1831 LOG_ERROR("transport is not selected");
1834 void adapter_deassert_reset(void)
1836 if (transport_is_jtag())
1837 jtag_add_reset(0, 0);
1838 else if (transport_is_swd())
1840 else if (transport_is_cmsis_dap())
1841 swd_add_reset(0); /* FIXME */
1842 else if (get_current_transport() != NULL)
1843 LOG_ERROR("reset is not supported on %s",
1844 get_current_transport()->name);
1846 LOG_ERROR("transport is not selected");