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, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
35 #include "interface.h"
36 #include <transport/transport.h>
37 #include <helper/jep106.h>
38 #include <jtag/hla/hla_transport.h>
39 #include <jtag/hla/hla_interface.h>
45 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
47 #include "xsvf/xsvf.h"
49 /** The number of JTAG queue flushes (for profiling and debugging purposes). */
50 static int jtag_flush_queue_count;
52 /* Sleep this # of ms after flushing the queue */
53 static int jtag_flush_queue_sleep;
55 static void jtag_add_scan_check(struct jtag_tap *active,
56 void (*jtag_add_scan)(struct jtag_tap *active,
58 const struct scan_field *in_fields,
60 int in_num_fields, struct scan_field *in_fields, tap_state_t state);
63 * The jtag_error variable is set when an error occurs while executing
64 * the queue. Application code may set this using jtag_set_error(),
65 * when an error occurs during processing that should be reported during
66 * jtag_execute_queue().
68 * The value is set and cleared, but never read by normal application code.
70 * This value is returned (and cleared) by jtag_execute_queue().
72 static int jtag_error = ERROR_OK;
74 static const char *jtag_event_strings[] = {
75 [JTAG_TRST_ASSERTED] = "TAP reset",
76 [JTAG_TAP_EVENT_SETUP] = "TAP setup",
77 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
78 [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
82 * JTAG adapters must initialize with TRST and SRST de-asserted
83 * (they're negative logic, so that means *high*). But some
84 * hardware doesn't necessarily work that way ... so set things
85 * up so that jtag_init() always forces that state.
87 static int jtag_trst = -1;
88 static int jtag_srst = -1;
91 * List all TAPs that have been created.
93 static struct jtag_tap *__jtag_all_taps;
95 static enum reset_types jtag_reset_config = RESET_NONE;
96 tap_state_t cmd_queue_cur_state = TAP_RESET;
98 static bool jtag_verify_capture_ir = true;
99 static int jtag_verify = 1;
101 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
102 *deasserted (in ms) */
103 static int adapter_nsrst_delay; /* default to no nSRST delay */
104 static int jtag_ntrst_delay;/* default to no nTRST delay */
105 static int adapter_nsrst_assert_width; /* width of assertion */
106 static int jtag_ntrst_assert_width; /* width of assertion */
109 * Contains a single callback along with a pointer that will be passed
110 * when an event occurs.
112 struct jtag_event_callback {
113 /** a event callback */
114 jtag_event_handler_t callback;
115 /** the private data to pass to the callback */
117 /** the next callback */
118 struct jtag_event_callback *next;
121 /* callbacks to inform high-level handlers about JTAG state changes */
122 static struct jtag_event_callback *jtag_event_callbacks;
125 static int speed_khz;
126 /* speed to fallback to when RCLK is requested but not supported */
127 static int rclk_fallback_speed_khz;
128 static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
129 static int jtag_speed;
131 static struct jtag_interface *jtag;
134 struct jtag_interface *jtag_interface;
136 void jtag_set_flush_queue_sleep(int ms)
138 jtag_flush_queue_sleep = ms;
141 void jtag_set_error(int error)
143 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
148 int jtag_error_clear(void)
150 int temp = jtag_error;
151 jtag_error = ERROR_OK;
157 static bool jtag_poll = 1;
159 bool is_jtag_poll_safe(void)
161 /* Polling can be disabled explicitly with set_enabled(false).
162 * It is also implicitly disabled while TRST is active and
163 * while SRST is gating the JTAG clock.
165 if (!transport_is_jtag())
168 if (!jtag_poll || jtag_trst != 0)
170 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
173 bool jtag_poll_get_enabled(void)
178 void jtag_poll_set_enabled(bool value)
185 struct jtag_tap *jtag_all_taps(void)
187 return __jtag_all_taps;
190 unsigned jtag_tap_count(void)
192 struct jtag_tap *t = jtag_all_taps();
201 unsigned jtag_tap_count_enabled(void)
203 struct jtag_tap *t = jtag_all_taps();
213 /** Append a new TAP to the chain of all taps. */
214 void jtag_tap_add(struct jtag_tap *t)
216 unsigned jtag_num_taps = 0;
218 struct jtag_tap **tap = &__jtag_all_taps;
219 while (*tap != NULL) {
221 tap = &(*tap)->next_tap;
224 t->abs_chain_position = jtag_num_taps;
227 /* returns a pointer to the n-th device in the scan chain */
228 struct jtag_tap *jtag_tap_by_position(unsigned n)
230 struct jtag_tap *t = jtag_all_taps();
238 struct jtag_tap *jtag_tap_by_string(const char *s)
240 /* try by name first */
241 struct jtag_tap *t = jtag_all_taps();
244 if (0 == strcmp(t->dotted_name, s))
249 /* no tap found by name, so try to parse the name as a number */
251 if (parse_uint(s, &n) != ERROR_OK)
254 /* FIXME remove this numeric fallback code late June 2010, along
255 * with all info in the User's Guide that TAPs have numeric IDs.
256 * Also update "scan_chain" output to not display the numbers.
258 t = jtag_tap_by_position(n);
260 LOG_WARNING("Specify TAP '%s' by name, not number %u",
266 struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p)
268 p = p ? p->next_tap : jtag_all_taps();
277 const char *jtag_tap_name(const struct jtag_tap *tap)
279 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
283 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
285 struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
287 if (callback == NULL)
288 return ERROR_COMMAND_SYNTAX_ERROR;
291 while ((*callbacks_p)->next)
292 callbacks_p = &((*callbacks_p)->next);
293 callbacks_p = &((*callbacks_p)->next);
296 (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
297 (*callbacks_p)->callback = callback;
298 (*callbacks_p)->priv = priv;
299 (*callbacks_p)->next = NULL;
304 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
306 struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
308 if (callback == NULL)
309 return ERROR_COMMAND_SYNTAX_ERROR;
312 if (((*p)->priv != priv) || ((*p)->callback != callback)) {
325 int jtag_call_event_callbacks(enum jtag_event event)
327 struct jtag_event_callback *callback = jtag_event_callbacks;
329 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
332 struct jtag_event_callback *next;
334 /* callback may remove itself */
335 next = callback->next;
336 callback->callback(event, callback->priv);
343 static void jtag_checks(void)
345 assert(jtag_trst == 0);
348 static void jtag_prelude(tap_state_t state)
352 assert(state != TAP_INVALID);
354 cmd_queue_cur_state = state;
357 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
362 int retval = interface_jtag_add_ir_scan(active, in_fields, state);
363 jtag_set_error(retval);
366 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
368 const struct scan_field *in_fields,
371 jtag_add_ir_scan_noverify(active, in_fields, state);
374 /* If fields->in_value is filled out, then the captured IR value will be checked */
375 void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
377 assert(state != TAP_RESET);
379 if (jtag_verify && jtag_verify_capture_ir) {
380 /* 8 x 32 bit id's is enough for all invocations */
382 /* if we are to run a verification of the ir scan, we need to get the input back.
383 * We may have to allocate space if the caller didn't ask for the input back.
385 in_fields->check_value = active->expected;
386 in_fields->check_mask = active->expected_mask;
387 jtag_add_scan_check(active, jtag_add_ir_scan_noverify_callback, 1, in_fields,
390 jtag_add_ir_scan_noverify(active, in_fields, state);
393 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
396 assert(out_bits != NULL);
397 assert(state != TAP_RESET);
401 int retval = interface_jtag_add_plain_ir_scan(
402 num_bits, out_bits, in_bits, state);
403 jtag_set_error(retval);
406 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
407 uint8_t *in_check_mask, int num_bits);
409 static int jtag_check_value_mask_callback(jtag_callback_data_t data0,
410 jtag_callback_data_t data1,
411 jtag_callback_data_t data2,
412 jtag_callback_data_t data3)
414 return jtag_check_value_inner((uint8_t *)data0,
420 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
421 struct jtag_tap *active,
423 const struct scan_field *in_fields,
425 int in_num_fields, struct scan_field *in_fields, tap_state_t state)
427 jtag_add_scan(active, in_num_fields, in_fields, state);
429 for (int i = 0; i < in_num_fields; i++) {
430 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL)) {
431 /* this is synchronous for a minidriver */
432 jtag_add_callback4(jtag_check_value_mask_callback,
433 (jtag_callback_data_t)in_fields[i].in_value,
434 (jtag_callback_data_t)in_fields[i].check_value,
435 (jtag_callback_data_t)in_fields[i].check_mask,
436 (jtag_callback_data_t)in_fields[i].num_bits);
441 void jtag_add_dr_scan_check(struct jtag_tap *active,
443 struct scan_field *in_fields,
447 jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
449 jtag_add_dr_scan(active, in_num_fields, in_fields, state);
453 void jtag_add_dr_scan(struct jtag_tap *active,
455 const struct scan_field *in_fields,
458 assert(state != TAP_RESET);
463 retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
464 jtag_set_error(retval);
467 void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
470 assert(out_bits != NULL);
471 assert(state != TAP_RESET);
476 retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
477 jtag_set_error(retval);
480 void jtag_add_tlr(void)
482 jtag_prelude(TAP_RESET);
483 jtag_set_error(interface_jtag_add_tlr());
485 /* NOTE: order here matches TRST path in jtag_add_reset() */
486 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
487 jtag_notify_event(JTAG_TRST_ASSERTED);
491 * If supported by the underlying adapter, this clocks a raw bit sequence
492 * onto TMS for switching betwen JTAG and SWD modes.
494 * DO NOT use this to bypass the integrity checks and logging provided
495 * by the jtag_add_pathmove() and jtag_add_statemove() calls.
497 * @param nbits How many bits to clock out.
498 * @param seq The bit sequence. The LSB is bit 0 of seq[0].
499 * @param state The JTAG tap state to record on completion. Use
500 * TAP_INVALID to represent being in in SWD mode.
502 * @todo Update naming conventions to stop assuming everything is JTAG.
504 int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
508 if (!(jtag->supported & DEBUG_CAP_TMS_SEQ))
509 return ERROR_JTAG_NOT_IMPLEMENTED;
512 cmd_queue_cur_state = state;
514 retval = interface_add_tms_seq(nbits, seq, state);
515 jtag_set_error(retval);
519 void jtag_add_pathmove(int num_states, const tap_state_t *path)
521 tap_state_t cur_state = cmd_queue_cur_state;
523 /* the last state has to be a stable state */
524 if (!tap_is_state_stable(path[num_states - 1])) {
525 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
526 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
530 for (int i = 0; i < num_states; i++) {
531 if (path[i] == TAP_RESET) {
532 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
533 jtag_set_error(ERROR_JTAG_STATE_INVALID);
537 if (tap_state_transition(cur_state, true) != path[i] &&
538 tap_state_transition(cur_state, false) != path[i]) {
539 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
540 tap_state_name(cur_state), tap_state_name(path[i]));
541 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
549 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
550 cmd_queue_cur_state = path[num_states - 1];
553 int jtag_add_statemove(tap_state_t goal_state)
555 tap_state_t cur_state = cmd_queue_cur_state;
557 if (goal_state != cur_state) {
558 LOG_DEBUG("cur_state=%s goal_state=%s",
559 tap_state_name(cur_state),
560 tap_state_name(goal_state));
563 /* If goal is RESET, be paranoid and force that that transition
564 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
566 if (goal_state == TAP_RESET)
568 else if (goal_state == cur_state)
571 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state)) {
572 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
573 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
574 tap_state_t moves[8];
575 assert(tms_count < ARRAY_SIZE(moves));
577 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
578 bool bit = tms_bits & 1;
580 cur_state = tap_state_transition(cur_state, bit);
581 moves[i] = cur_state;
584 jtag_add_pathmove(tms_count, moves);
585 } else if (tap_state_transition(cur_state, true) == goal_state
586 || tap_state_transition(cur_state, false) == goal_state)
587 jtag_add_pathmove(1, &goal_state);
594 void jtag_add_runtest(int num_cycles, tap_state_t state)
597 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
601 void jtag_add_clocks(int num_cycles)
603 if (!tap_is_state_stable(cmd_queue_cur_state)) {
604 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
605 tap_state_name(cmd_queue_cur_state));
606 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
610 if (num_cycles > 0) {
612 jtag_set_error(interface_jtag_add_clocks(num_cycles));
616 void swd_add_reset(int req_srst)
619 if (!(jtag_reset_config & RESET_HAS_SRST)) {
620 LOG_ERROR("BUG: can't assert SRST");
621 jtag_set_error(ERROR_FAIL);
627 /* Maybe change SRST signal state */
628 if (jtag_srst != req_srst) {
631 retval = interface_jtag_add_reset(0, req_srst);
632 if (retval != ERROR_OK)
633 jtag_set_error(retval);
635 retval = jtag_execute_queue();
637 if (retval != ERROR_OK) {
638 LOG_ERROR("TRST/SRST error");
642 /* SRST resets everything hooked up to that signal */
643 jtag_srst = req_srst;
645 LOG_DEBUG("SRST line asserted");
646 if (adapter_nsrst_assert_width)
647 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
649 LOG_DEBUG("SRST line released");
650 if (adapter_nsrst_delay)
651 jtag_add_sleep(adapter_nsrst_delay * 1000);
654 retval = jtag_execute_queue();
655 if (retval != ERROR_OK) {
656 LOG_ERROR("SRST timings error");
662 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
664 int trst_with_tlr = 0;
668 /* Without SRST, we must use target-specific JTAG operations
669 * on each target; callers should not be requesting SRST when
670 * that signal doesn't exist.
672 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
673 * can kick in even if the JTAG adapter can't drive TRST.
676 if (!(jtag_reset_config & RESET_HAS_SRST)) {
677 LOG_ERROR("BUG: can't assert SRST");
678 jtag_set_error(ERROR_FAIL);
681 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
682 && !req_tlr_or_trst) {
683 LOG_ERROR("BUG: can't assert only SRST");
684 jtag_set_error(ERROR_FAIL);
690 /* JTAG reset (entry to TAP_RESET state) can always be achieved
691 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
692 * state first. TRST accelerates it, and bypasses those states.
694 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
695 * can kick in even if the JTAG adapter can't drive SRST.
697 if (req_tlr_or_trst) {
698 if (!(jtag_reset_config & RESET_HAS_TRST))
700 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
707 /* Maybe change TRST and/or SRST signal state */
708 if (jtag_srst != new_srst || jtag_trst != new_trst) {
711 retval = interface_jtag_add_reset(new_trst, new_srst);
712 if (retval != ERROR_OK)
713 jtag_set_error(retval);
715 retval = jtag_execute_queue();
717 if (retval != ERROR_OK) {
718 LOG_ERROR("TRST/SRST error");
723 /* SRST resets everything hooked up to that signal */
724 if (jtag_srst != new_srst) {
725 jtag_srst = new_srst;
727 LOG_DEBUG("SRST line asserted");
728 if (adapter_nsrst_assert_width)
729 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
731 LOG_DEBUG("SRST line released");
732 if (adapter_nsrst_delay)
733 jtag_add_sleep(adapter_nsrst_delay * 1000);
737 /* Maybe enter the JTAG TAP_RESET state ...
738 * - using only TMS, TCK, and the JTAG state machine
739 * - or else more directly, using TRST
741 * TAP_RESET should be invisible to non-debug parts of the system.
744 LOG_DEBUG("JTAG reset with TLR instead of TRST");
747 } else if (jtag_trst != new_trst) {
748 jtag_trst = new_trst;
750 LOG_DEBUG("TRST line asserted");
751 tap_set_state(TAP_RESET);
752 if (jtag_ntrst_assert_width)
753 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
755 LOG_DEBUG("TRST line released");
756 if (jtag_ntrst_delay)
757 jtag_add_sleep(jtag_ntrst_delay * 1000);
759 /* We just asserted nTRST, so we're now in TAP_RESET.
760 * Inform possible listeners about this, now that
761 * JTAG instructions and data can be shifted. This
762 * sequence must match jtag_add_tlr().
764 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
765 jtag_notify_event(JTAG_TRST_ASSERTED);
770 void jtag_add_sleep(uint32_t us)
772 /** @todo Here, keep_alive() appears to be a layering violation!!! */
774 jtag_set_error(interface_jtag_add_sleep(us));
777 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
778 uint8_t *in_check_mask, int num_bits)
780 int retval = ERROR_OK;
784 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
786 compare_failed = buf_cmp(captured, in_check_value, num_bits);
788 if (compare_failed) {
789 char *captured_str, *in_check_value_str;
790 int bits = (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits;
792 /* NOTE: we've lost diagnostic context here -- 'which tap' */
794 captured_str = buf_to_str(captured, bits, 16);
795 in_check_value_str = buf_to_str(in_check_value, bits, 16);
797 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
799 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
802 free(in_check_value_str);
805 char *in_check_mask_str;
807 in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
808 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
809 free(in_check_mask_str);
812 retval = ERROR_JTAG_QUEUE_FAILED;
817 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
819 assert(field->in_value != NULL);
822 /* no checking to do */
826 jtag_execute_queue_noclear();
828 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
829 jtag_set_error(retval);
832 int default_interface_jtag_execute_queue(void)
835 LOG_ERROR("No JTAG interface configured yet. "
836 "Issue 'init' command in startup scripts "
837 "before communicating with targets.");
841 int result = jtag->execute_queue();
844 /* Only build this if we use a regular driver with a command queue.
845 * Otherwise jtag_command_queue won't be found at compile/link time. Its
846 * definition is in jtag/commands.c, which is only built/linked by
847 * jtag/Makefile.am if MINIDRIVER_DUMMY || !MINIDRIVER, but those variables
848 * aren't accessible here. */
849 struct jtag_command *cmd = jtag_command_queue;
850 while (debug_level >= LOG_LVL_DEBUG && cmd) {
853 LOG_DEBUG_IO("JTAG %s SCAN to %s",
854 cmd->cmd.scan->ir_scan ? "IR" : "DR",
855 tap_state_name(cmd->cmd.scan->end_state));
856 for (int i = 0; i < cmd->cmd.scan->num_fields; i++) {
857 struct scan_field *field = cmd->cmd.scan->fields + i;
858 if (field->out_value) {
859 char *str = buf_to_str(field->out_value, field->num_bits, 16);
860 LOG_DEBUG_IO(" %db out: %s", field->num_bits, str);
863 if (field->in_value) {
864 char *str = buf_to_str(field->in_value, field->num_bits, 16);
865 LOG_DEBUG_IO(" %db in: %s", field->num_bits, str);
871 LOG_DEBUG_IO("JTAG TLR RESET to %s",
872 tap_state_name(cmd->cmd.statemove->end_state));
875 LOG_DEBUG_IO("JTAG RUNTEST %d cycles to %s",
876 cmd->cmd.runtest->num_cycles,
877 tap_state_name(cmd->cmd.runtest->end_state));
881 const char *reset_str[3] = {
882 "leave", "deassert", "assert"
884 LOG_DEBUG_IO("JTAG RESET %s TRST, %s SRST",
885 reset_str[cmd->cmd.reset->trst + 1],
886 reset_str[cmd->cmd.reset->srst + 1]);
890 LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
893 LOG_DEBUG_IO("JTAG SLEEP (TODO)");
895 case JTAG_STABLECLOCKS:
896 LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
899 LOG_DEBUG_IO("JTAG TMS (TODO)");
902 LOG_ERROR("Unknown JTAG command: %d", cmd->type);
912 void jtag_execute_queue_noclear(void)
914 jtag_flush_queue_count++;
915 jtag_set_error(interface_jtag_execute_queue());
917 if (jtag_flush_queue_sleep > 0) {
918 /* For debug purposes it can be useful to test performance
919 * or behavior when delaying after flushing the queue,
920 * e.g. to simulate long roundtrip times.
922 usleep(jtag_flush_queue_sleep * 1000);
926 int jtag_get_flush_queue_count(void)
928 return jtag_flush_queue_count;
931 int jtag_execute_queue(void)
933 jtag_execute_queue_noclear();
934 return jtag_error_clear();
937 static int jtag_reset_callback(enum jtag_event event, void *priv)
939 struct jtag_tap *tap = priv;
941 if (event == JTAG_TRST_ASSERTED) {
942 tap->enabled = !tap->disabled_after_reset;
944 /* current instruction is either BYPASS or IDCODE */
945 buf_set_ones(tap->cur_instr, tap->ir_length);
952 /* sleep at least us microseconds. When we sleep more than 1000ms we
953 * do an alive sleep, i.e. keep GDB alive. Note that we could starve
954 * GDB if we slept for <1000ms many times.
956 void jtag_sleep(uint32_t us)
961 alive_sleep((us+999)/1000);
964 #define JTAG_MAX_AUTO_TAPS 20
966 #define EXTRACT_JEP106_BANK(X) (((X) & 0xf00) >> 8)
967 #define EXTRACT_JEP106_ID(X) (((X) & 0xfe) >> 1)
968 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
969 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
970 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
972 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
973 * know that no valid TAP will have it as an IDCODE value.
975 #define END_OF_CHAIN_FLAG 0xffffffff
977 /* a larger IR length than we ever expect to autoprobe */
978 #define JTAG_IRLEN_MAX 60
980 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
982 struct scan_field field = {
983 .num_bits = num_idcode * 32,
984 .out_value = idcode_buffer,
985 .in_value = idcode_buffer,
988 /* initialize to the end of chain ID value */
989 for (unsigned i = 0; i < num_idcode; i++)
990 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
992 jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
994 return jtag_execute_queue();
997 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
999 uint8_t zero_check = 0x0;
1000 uint8_t one_check = 0xff;
1002 for (unsigned i = 0; i < count * 4; i++) {
1003 zero_check |= idcodes[i];
1004 one_check &= idcodes[i];
1007 /* if there wasn't a single non-zero bit or if all bits were one,
1008 * the scan is not valid. We wrote a mix of both values; either
1010 * - There's a hardware issue (almost certainly):
1011 * + all-zeroes can mean a target stuck in JTAG reset
1012 * + all-ones tends to mean no target
1013 * - The scan chain is WAY longer than we can handle, *AND* either
1014 * + there are several hundreds of TAPs in bypass, or
1015 * + at least a few dozen TAPs all have an all-ones IDCODE
1017 if (zero_check == 0x00 || one_check == 0xff) {
1018 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
1019 (zero_check == 0x00) ? "zeroes" : "ones");
1020 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
1026 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
1027 const char *name, uint32_t idcode)
1029 log_printf_lf(level, __FILE__, __LINE__, __func__,
1030 "JTAG tap: %s %16.16s: 0x%08x "
1031 "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
1033 (unsigned int)idcode,
1034 (unsigned int)EXTRACT_MFG(idcode),
1035 jep106_manufacturer(EXTRACT_JEP106_BANK(idcode), EXTRACT_JEP106_ID(idcode)),
1036 (unsigned int)EXTRACT_PART(idcode),
1037 (unsigned int)EXTRACT_VER(idcode));
1040 static bool jtag_idcode_is_final(uint32_t idcode)
1043 * Some devices, such as AVR8, will output all 1's instead
1044 * of TDI input value at end of chain. Allow those values
1045 * instead of failing.
1047 return idcode == END_OF_CHAIN_FLAG;
1051 * This helper checks that remaining bits in the examined chain data are
1052 * all as expected, but a single JTAG device requires only 64 bits to be
1053 * read back correctly. This can help identify and diagnose problems
1054 * with the JTAG chain earlier, gives more helpful/explicit error messages.
1055 * Returns TRUE iff garbage was found.
1057 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
1059 bool triggered = false;
1060 for (; count < max - 31; count += 32) {
1061 uint32_t idcode = buf_get_u32(idcodes, count, 32);
1063 /* do not trigger the warning if the data looks good */
1064 if (jtag_idcode_is_final(idcode))
1066 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
1067 count, (unsigned int)idcode);
1073 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
1076 if (tap->expected_ids_cnt == 0 || !tap->hasidcode)
1079 /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1080 uint32_t mask = tap->ignore_version ? ~(0xfU << 28) : ~0U;
1081 uint32_t idcode = tap->idcode & mask;
1083 /* Loop over the expected identification codes and test for a match */
1084 for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1085 uint32_t expected = tap->expected_ids[ii] & mask;
1087 if (idcode == expected)
1090 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1091 if (0 == tap->expected_ids[ii])
1095 /* If none of the expected ids matched, warn */
1096 jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
1097 tap->dotted_name, tap->idcode);
1098 for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1101 snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, tap->expected_ids_cnt);
1102 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1103 tap->dotted_name, tap->expected_ids[ii]);
1108 /* Try to examine chain layout according to IEEE 1149.1 §12
1109 * This is called a "blind interrogation" of the scan chain.
1111 static int jtag_examine_chain(void)
1114 unsigned max_taps = jtag_tap_count();
1116 /* Autoprobe up to this many. */
1117 if (max_taps < JTAG_MAX_AUTO_TAPS)
1118 max_taps = JTAG_MAX_AUTO_TAPS;
1120 /* Add room for end-of-chain marker. */
1123 uint8_t *idcode_buffer = malloc(max_taps * 4);
1124 if (idcode_buffer == NULL)
1125 return ERROR_JTAG_INIT_FAILED;
1127 /* DR scan to collect BYPASS or IDCODE register contents.
1128 * Then make sure the scan data has both ones and zeroes.
1130 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1131 retval = jtag_examine_chain_execute(idcode_buffer, max_taps);
1132 if (retval != ERROR_OK)
1134 if (!jtag_examine_chain_check(idcode_buffer, max_taps)) {
1135 retval = ERROR_JTAG_INIT_FAILED;
1139 /* Point at the 1st predefined tap, if any */
1140 struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
1142 unsigned bit_count = 0;
1143 unsigned autocount = 0;
1144 for (unsigned i = 0; i < max_taps; i++) {
1145 assert(bit_count < max_taps * 32);
1146 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1148 /* No predefined TAP? Auto-probe. */
1150 /* Is there another TAP? */
1151 if (jtag_idcode_is_final(idcode))
1154 /* Default everything in this TAP except IR length.
1156 * REVISIT create a jtag_alloc(chip, tap) routine, and
1157 * share it with jim_newtap_cmd().
1159 tap = calloc(1, sizeof *tap);
1161 retval = ERROR_FAIL;
1165 tap->chip = alloc_printf("auto%u", autocount++);
1166 tap->tapname = strdup("tap");
1167 tap->dotted_name = alloc_printf("%s.%s", tap->chip, tap->tapname);
1169 tap->ir_length = 0; /* ... signifying irlen autoprobe */
1170 tap->ir_capture_mask = 0x03;
1171 tap->ir_capture_value = 0x01;
1173 tap->enabled = true;
1178 if ((idcode & 1) == 0) {
1179 /* Zero for LSB indicates a device in bypass */
1180 LOG_INFO("TAP %s does not have valid IDCODE (idcode=0x%x)",
1181 tap->dotted_name, idcode);
1182 tap->hasidcode = false;
1187 /* Friendly devices support IDCODE */
1188 tap->hasidcode = true;
1189 tap->idcode = idcode;
1190 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found", tap->dotted_name, idcode);
1195 /* ensure the TAP ID matches what was expected */
1196 if (!jtag_examine_chain_match_tap(tap))
1197 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1199 tap = jtag_tap_next_enabled(tap);
1202 /* After those IDCODE or BYPASS register values should be
1203 * only the data we fed into the scan chain.
1205 if (jtag_examine_chain_end(idcode_buffer, bit_count, max_taps * 32)) {
1206 LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
1207 retval = ERROR_JTAG_INIT_FAILED;
1211 /* Return success or, for backwards compatibility if only
1212 * some IDCODE values mismatched, a soft/continuable fault.
1215 free(idcode_buffer);
1220 * Validate the date loaded by entry to the Capture-IR state, to help
1221 * find errors related to scan chain configuration (wrong IR lengths)
1224 * Entry state can be anything. On non-error exit, all TAPs are in
1225 * bypass mode. On error exits, the scan chain is reset.
1227 static int jtag_validate_ircapture(void)
1229 struct jtag_tap *tap;
1230 int total_ir_length = 0;
1231 uint8_t *ir_test = NULL;
1232 struct scan_field field;
1237 /* when autoprobing, accomodate huge IR lengths */
1238 for (tap = NULL, total_ir_length = 0;
1239 (tap = jtag_tap_next_enabled(tap)) != NULL;
1240 total_ir_length += tap->ir_length) {
1241 if (tap->ir_length == 0)
1242 total_ir_length += JTAG_IRLEN_MAX;
1245 /* increase length to add 2 bit sentinel after scan */
1246 total_ir_length += 2;
1248 ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1249 if (ir_test == NULL)
1252 /* after this scan, all TAPs will capture BYPASS instructions */
1253 buf_set_ones(ir_test, total_ir_length);
1255 field.num_bits = total_ir_length;
1256 field.out_value = ir_test;
1257 field.in_value = ir_test;
1259 jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
1261 LOG_DEBUG("IR capture validation scan");
1262 retval = jtag_execute_queue();
1263 if (retval != ERROR_OK)
1270 tap = jtag_tap_next_enabled(tap);
1274 /* If we're autoprobing, guess IR lengths. They must be at
1275 * least two bits. Guessing will fail if (a) any TAP does
1276 * not conform to the JTAG spec; or (b) when the upper bits
1277 * captured from some conforming TAP are nonzero. Or if
1278 * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
1279 * an implementation limit, which could someday be raised.
1281 * REVISIT optimization: if there's a *single* TAP we can
1282 * lift restrictions (a) and (b) by scanning a recognizable
1283 * pattern before the all-ones BYPASS. Check for where the
1284 * pattern starts in the result, instead of an 0...01 value.
1286 * REVISIT alternative approach: escape to some tcl code
1287 * which could provide more knowledge, based on IDCODE; and
1288 * only guess when that has no success.
1290 if (tap->ir_length == 0) {
1292 while ((val = buf_get_u64(ir_test, chain_pos, tap->ir_length + 1)) == 1
1293 && tap->ir_length < JTAG_IRLEN_MAX) {
1296 LOG_WARNING("AUTO %s - use \"jtag newtap " "%s %s -irlen %d "
1297 "-expected-id 0x%08" PRIx32 "\"",
1298 tap->dotted_name, tap->chip, tap->tapname, tap->ir_length, tap->idcode);
1301 /* Validate the two LSBs, which must be 01 per JTAG spec.
1303 * Or ... more bits could be provided by TAP declaration.
1304 * Plus, some taps (notably in i.MX series chips) violate
1305 * this part of the JTAG spec, so their capture mask/value
1306 * attributes might disable this test.
1308 val = buf_get_u64(ir_test, chain_pos, tap->ir_length);
1309 if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1310 LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64 " not 0x%0*" PRIx32,
1312 (tap->ir_length + 7) / tap->ir_length, val,
1313 (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
1315 retval = ERROR_JTAG_INIT_FAILED;
1318 LOG_DEBUG("%s: IR capture 0x%0*" PRIx64, jtag_tap_name(tap),
1319 (tap->ir_length + 7) / tap->ir_length, val);
1320 chain_pos += tap->ir_length;
1323 /* verify the '11' sentinel we wrote is returned at the end */
1324 val = buf_get_u64(ir_test, chain_pos, 2);
1326 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1328 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1331 retval = ERROR_JTAG_INIT_FAILED;
1336 if (retval != ERROR_OK) {
1338 jtag_execute_queue();
1343 void jtag_tap_init(struct jtag_tap *tap)
1345 unsigned ir_len_bits;
1346 unsigned ir_len_bytes;
1348 /* if we're autoprobing, cope with potentially huge ir_length */
1349 ir_len_bits = tap->ir_length ? : JTAG_IRLEN_MAX;
1350 ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
1352 tap->expected = calloc(1, ir_len_bytes);
1353 tap->expected_mask = calloc(1, ir_len_bytes);
1354 tap->cur_instr = malloc(ir_len_bytes);
1356 /** @todo cope better with ir_length bigger than 32 bits */
1357 if (ir_len_bits > 32)
1360 buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
1361 buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
1363 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1365 buf_set_ones(tap->cur_instr, tap->ir_length);
1367 /* register the reset callback for the TAP */
1368 jtag_register_event_callback(&jtag_reset_callback, tap);
1371 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1372 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1373 tap->abs_chain_position, tap->ir_length,
1374 (unsigned) tap->ir_capture_value,
1375 (unsigned) tap->ir_capture_mask);
1378 void jtag_tap_free(struct jtag_tap *tap)
1380 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1382 struct jtag_tap_event_action *jteap = tap->event_action;
1384 struct jtag_tap_event_action *next = jteap->next;
1385 Jim_DecrRefCount(jteap->interp, jteap->body);
1390 free(tap->expected);
1391 free(tap->expected_mask);
1392 free(tap->expected_ids);
1393 free(tap->cur_instr);
1396 free(tap->dotted_name);
1401 * Do low-level setup like initializing registers, output signals,
1404 int adapter_init(struct command_context *cmd_ctx)
1409 if (!jtag_interface) {
1410 /* nothing was previously specified by "interface" command */
1411 LOG_ERROR("Debug Adapter has to be specified, "
1412 "see \"interface\" command");
1413 return ERROR_JTAG_INVALID_INTERFACE;
1417 retval = jtag_interface->init();
1418 if (retval != ERROR_OK)
1420 jtag = jtag_interface;
1422 if (jtag->speed == NULL) {
1423 LOG_INFO("This adapter doesn't support configurable speed");
1427 if (CLOCK_MODE_UNSELECTED == clock_mode) {
1428 LOG_ERROR("An adapter speed is not selected in the init script."
1429 " Insert a call to adapter_khz or jtag_rclk to proceed.");
1430 return ERROR_JTAG_INIT_FAILED;
1433 int requested_khz = jtag_get_speed_khz();
1434 int actual_khz = requested_khz;
1435 int jtag_speed_var = 0;
1436 retval = jtag_get_speed(&jtag_speed_var);
1437 if (retval != ERROR_OK)
1439 retval = jtag->speed(jtag_speed_var);
1440 if (retval != ERROR_OK)
1442 retval = jtag_get_speed_readable(&actual_khz);
1443 if (ERROR_OK != retval)
1444 LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
1445 else if (actual_khz) {
1446 /* Adaptive clocking -- JTAG-specific */
1447 if ((CLOCK_MODE_RCLK == clock_mode)
1448 || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz)) {
1449 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1452 LOG_INFO("clock speed %d kHz", actual_khz);
1454 LOG_INFO("RCLK (adaptive clock speed)");
1459 int jtag_init_inner(struct command_context *cmd_ctx)
1461 struct jtag_tap *tap;
1463 bool issue_setup = true;
1465 LOG_DEBUG("Init JTAG chain");
1467 tap = jtag_tap_next_enabled(NULL);
1469 /* Once JTAG itself is properly set up, and the scan chain
1470 * isn't absurdly large, IDCODE autoprobe should work fine.
1472 * But ... IRLEN autoprobe can fail even on systems which
1473 * are fully conformant to JTAG. Also, JTAG setup can be
1474 * quite finicky on some systems.
1476 * REVISIT: if TAP autoprobe works OK, then in many cases
1477 * we could escape to tcl code and set up targets based on
1478 * the TAP's IDCODE values.
1480 LOG_WARNING("There are no enabled taps. "
1481 "AUTO PROBING MIGHT NOT WORK!!");
1483 /* REVISIT default clock will often be too fast ... */
1487 retval = jtag_execute_queue();
1488 if (retval != ERROR_OK)
1491 /* Examine DR values first. This discovers problems which will
1492 * prevent communication ... hardware issues like TDO stuck, or
1493 * configuring the wrong number of (enabled) TAPs.
1495 retval = jtag_examine_chain();
1498 /* complete success */
1501 /* For backward compatibility reasons, try coping with
1502 * configuration errors involving only ID mismatches.
1503 * We might be able to talk to the devices.
1505 * Also the device might be powered down during startup.
1507 * After OpenOCD starts, we can try to power on the device
1510 LOG_ERROR("Trying to use configured scan chain anyway...");
1511 issue_setup = false;
1515 /* Now look at IR values. Problems here will prevent real
1516 * communication. They mostly mean that the IR length is
1517 * wrong ... or that the IR capture value is wrong. (The
1518 * latter is uncommon, but easily worked around: provide
1519 * ircapture/irmask values during TAP setup.)
1521 retval = jtag_validate_ircapture();
1522 if (retval != ERROR_OK) {
1523 /* The target might be powered down. The user
1524 * can power it up and reset it after firing
1527 issue_setup = false;
1531 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1533 LOG_WARNING("Bypassing JTAG setup events due to errors");
1539 int adapter_quit(void)
1541 if (jtag && jtag->quit) {
1542 /* close the JTAG interface */
1543 int result = jtag->quit();
1544 if (ERROR_OK != result)
1545 LOG_ERROR("failed: %d", result);
1548 struct jtag_tap *t = jtag_all_taps();
1550 struct jtag_tap *n = t->next_tap;
1558 int swd_init_reset(struct command_context *cmd_ctx)
1560 int retval = adapter_init(cmd_ctx);
1561 if (retval != ERROR_OK)
1564 LOG_DEBUG("Initializing with hard SRST reset");
1566 if (jtag_reset_config & RESET_HAS_SRST)
1569 retval = jtag_execute_queue();
1573 int jtag_init_reset(struct command_context *cmd_ctx)
1575 int retval = adapter_init(cmd_ctx);
1576 if (retval != ERROR_OK)
1579 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1582 * This procedure is used by default when OpenOCD triggers a reset.
1583 * It's now done through an overridable Tcl "init_reset" wrapper.
1585 * This started out as a more powerful "get JTAG working" reset than
1586 * jtag_init_inner(), applying TRST because some chips won't activate
1587 * JTAG without a TRST cycle (presumed to be async, though some of
1588 * those chips synchronize JTAG activation using TCK).
1590 * But some chips only activate JTAG as part of an SRST cycle; SRST
1591 * got mixed in. So it became a hard reset routine, which got used
1592 * in more places, and which coped with JTAG reset being forced as
1593 * part of SRST (srst_pulls_trst).
1595 * And even more corner cases started to surface: TRST and/or SRST
1596 * assertion timings matter; some chips need other JTAG operations;
1597 * TRST/SRST sequences can need to be different from these, etc.
1599 * Systems should override that wrapper to support system-specific
1600 * requirements that this not-fully-generic code doesn't handle.
1602 * REVISIT once Tcl code can read the reset_config modes, this won't
1603 * need to be a C routine at all...
1605 if (jtag_reset_config & RESET_HAS_SRST) {
1606 jtag_add_reset(1, 1);
1607 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1608 jtag_add_reset(0, 1);
1610 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1613 /* some targets enable us to connect with srst asserted */
1614 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1615 if (jtag_reset_config & RESET_SRST_NO_GATING)
1616 jtag_add_reset(0, 1);
1618 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1619 jtag_add_reset(0, 0);
1622 jtag_add_reset(0, 0);
1623 retval = jtag_execute_queue();
1624 if (retval != ERROR_OK)
1627 /* Check that we can communication on the JTAG chain + eventually we want to
1628 * be able to perform enumeration only after OpenOCD has started
1629 * telnet and GDB server
1631 * That would allow users to more easily perform any magic they need to before
1634 return jtag_init_inner(cmd_ctx);
1637 int jtag_init(struct command_context *cmd_ctx)
1639 int retval = adapter_init(cmd_ctx);
1640 if (retval != ERROR_OK)
1643 /* guard against oddball hardware: force resets to be inactive */
1644 jtag_add_reset(0, 0);
1646 /* some targets enable us to connect with srst asserted */
1647 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1648 if (jtag_reset_config & RESET_SRST_NO_GATING)
1649 jtag_add_reset(0, 1);
1651 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1653 retval = jtag_execute_queue();
1654 if (retval != ERROR_OK)
1657 if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1663 unsigned jtag_get_speed_khz(void)
1668 static int adapter_khz_to_speed(unsigned khz, int *speed)
1670 LOG_DEBUG("convert khz to interface specific speed value");
1674 LOG_DEBUG("have interface set up");
1676 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1680 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1681 if (ERROR_OK != retval)
1683 *speed = speed_div1;
1687 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int *speed)
1689 int retval = adapter_khz_to_speed(0, speed);
1690 if ((ERROR_OK != retval) && fallback_speed_khz) {
1691 LOG_DEBUG("trying fallback speed...");
1692 retval = adapter_khz_to_speed(fallback_speed_khz, speed);
1697 static int jtag_set_speed(int speed)
1700 /* this command can be called during CONFIG,
1701 * in which case jtag isn't initialized */
1702 return jtag ? jtag->speed(speed) : ERROR_OK;
1705 int jtag_config_khz(unsigned khz)
1707 LOG_DEBUG("handle jtag khz");
1708 clock_mode = CLOCK_MODE_KHZ;
1710 int retval = adapter_khz_to_speed(khz, &speed);
1711 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1714 int jtag_config_rclk(unsigned fallback_speed_khz)
1716 LOG_DEBUG("handle jtag rclk");
1717 clock_mode = CLOCK_MODE_RCLK;
1718 rclk_fallback_speed_khz = fallback_speed_khz;
1720 int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1721 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1724 int jtag_get_speed(int *speed)
1726 switch (clock_mode) {
1727 case CLOCK_MODE_KHZ:
1728 adapter_khz_to_speed(jtag_get_speed_khz(), speed);
1730 case CLOCK_MODE_RCLK:
1731 jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
1734 LOG_ERROR("BUG: unknown jtag clock mode");
1740 int jtag_get_speed_readable(int *khz)
1742 int jtag_speed_var = 0;
1743 int retval = jtag_get_speed(&jtag_speed_var);
1744 if (retval != ERROR_OK)
1748 if (!jtag->speed_div) {
1749 LOG_ERROR("Translation from jtag_speed to khz not implemented");
1752 return jtag->speed_div(jtag_speed_var, khz);
1755 void jtag_set_verify(bool enable)
1757 jtag_verify = enable;
1760 bool jtag_will_verify()
1765 void jtag_set_verify_capture_ir(bool enable)
1767 jtag_verify_capture_ir = enable;
1770 bool jtag_will_verify_capture_ir()
1772 return jtag_verify_capture_ir;
1775 int jtag_power_dropout(int *dropout)
1778 /* TODO: as the jtag interface is not valid all
1779 * we can do at the moment is exit OpenOCD */
1780 LOG_ERROR("No Valid JTAG Interface Configured.");
1783 if (jtag->power_dropout)
1784 return jtag->power_dropout(dropout);
1786 *dropout = 0; /* by default we can't detect power dropout */
1790 int jtag_srst_asserted(int *srst_asserted)
1792 if (jtag->srst_asserted)
1793 return jtag->srst_asserted(srst_asserted);
1795 *srst_asserted = 0; /* by default we can't detect srst asserted */
1799 enum reset_types jtag_get_reset_config(void)
1801 return jtag_reset_config;
1803 void jtag_set_reset_config(enum reset_types type)
1805 jtag_reset_config = type;
1808 int jtag_get_trst(void)
1810 return jtag_trst == 1;
1812 int jtag_get_srst(void)
1814 return jtag_srst == 1;
1817 void jtag_set_nsrst_delay(unsigned delay)
1819 adapter_nsrst_delay = delay;
1821 unsigned jtag_get_nsrst_delay(void)
1823 return adapter_nsrst_delay;
1825 void jtag_set_ntrst_delay(unsigned delay)
1827 jtag_ntrst_delay = delay;
1829 unsigned jtag_get_ntrst_delay(void)
1831 return jtag_ntrst_delay;
1835 void jtag_set_nsrst_assert_width(unsigned delay)
1837 adapter_nsrst_assert_width = delay;
1839 unsigned jtag_get_nsrst_assert_width(void)
1841 return adapter_nsrst_assert_width;
1843 void jtag_set_ntrst_assert_width(unsigned delay)
1845 jtag_ntrst_assert_width = delay;
1847 unsigned jtag_get_ntrst_assert_width(void)
1849 return jtag_ntrst_assert_width;
1852 static int jtag_select(struct command_context *ctx)
1856 /* NOTE: interface init must already have been done.
1857 * That works with only C code ... no Tcl glue required.
1860 retval = jtag_register_commands(ctx);
1862 if (retval != ERROR_OK)
1865 retval = svf_register_commands(ctx);
1867 if (retval != ERROR_OK)
1870 return xsvf_register_commands(ctx);
1873 static struct transport jtag_transport = {
1875 .select = jtag_select,
1879 static void jtag_constructor(void) __attribute__((constructor));
1880 static void jtag_constructor(void)
1882 transport_register(&jtag_transport);
1885 /** Returns true if the current debug session
1886 * is using JTAG as its transport.
1888 bool transport_is_jtag(void)
1890 return get_current_transport() == &jtag_transport;
1893 int adapter_resets(int trst, int srst)
1895 if (get_current_transport() == NULL) {
1896 LOG_ERROR("transport is not selected");
1900 if (transport_is_jtag()) {
1901 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1902 LOG_ERROR("adapter has no srst signal");
1906 /* adapters without trst signal will eventually use tlr sequence */
1907 jtag_add_reset(trst, srst);
1909 } else if (transport_is_swd()) {
1910 if (trst == TRST_ASSERT) {
1911 LOG_ERROR("transport swd has no trst signal");
1915 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1916 LOG_ERROR("adapter has no srst signal");
1919 swd_add_reset(srst);
1921 } else if (transport_is_hla()) {
1922 if (trst == TRST_ASSERT) {
1923 LOG_ERROR("transport %s has no trst signal",
1924 get_current_transport()->name);
1928 if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1929 LOG_ERROR("adapter has no srst signal");
1932 return hl_interface_reset(srst);
1935 if (trst == TRST_DEASSERT && srst == SRST_DEASSERT)
1938 LOG_ERROR("reset is not supported on transport %s",
1939 get_current_transport()->name);
1944 void adapter_assert_reset(void)
1946 if (transport_is_jtag()) {
1947 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1948 jtag_add_reset(1, 1);
1950 jtag_add_reset(0, 1);
1951 } else if (transport_is_swd())
1953 else if (get_current_transport() != NULL)
1954 LOG_ERROR("reset is not supported on %s",
1955 get_current_transport()->name);
1957 LOG_ERROR("transport is not selected");
1960 void adapter_deassert_reset(void)
1962 if (transport_is_jtag())
1963 jtag_add_reset(0, 0);
1964 else if (transport_is_swd())
1966 else if (get_current_transport() != NULL)
1967 LOG_ERROR("reset is not supported on %s",
1968 get_current_transport()->name);
1970 LOG_ERROR("transport is not selected");
1973 int adapter_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1974 uint32_t port_size, unsigned int *trace_freq,
1975 unsigned int traceclkin_freq, uint16_t *prescaler)
1977 if (jtag->config_trace) {
1978 return jtag->config_trace(enabled, pin_protocol, port_size, trace_freq,
1979 traceclkin_freq, prescaler);
1980 } else if (enabled) {
1981 LOG_ERROR("The selected interface does not support tracing");
1988 int adapter_poll_trace(uint8_t *buf, size_t *size)
1990 if (jtag->poll_trace)
1991 return jtag->poll_trace(buf, size);