X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fjtag%2Ftcl.c;h=ffb5d276544427fe7beb55075c84a2c1190b2708;hb=c6e323b9838254b338310ec165a5345635c5d177;hp=bb86a325b0da2de65925e3efdf18671168b3bddb;hpb=7124be824792eee3f973eb2cf7e1c851cbd2a923;p=fw%2Fopenocd diff --git a/src/jtag/tcl.c b/src/jtag/tcl.c index bb86a325b..ffb5d2765 100644 --- a/src/jtag/tcl.c +++ b/src/jtag/tcl.c @@ -51,6 +51,17 @@ static const Jim_Nvp nvp_jtag_tap_event[] = { extern struct jtag_interface *jtag_interface; +struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o) +{ + const char *cp = Jim_GetString(o, NULL); + struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL; + if (NULL == cp) + cp = "(unknown)"; + if (NULL == t) + Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp); + return t; +} + static bool scan_is_safe(tap_state_t state) { switch (state) @@ -258,26 +269,39 @@ static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const return JIM_OK; } +/* REVISIT Just what about these should "move" ... ? + * These registrations, into the main JTAG table? + * + * There's a minor compatibility issue, these all show up twice; + * that's not desirable: + * - jtag drscan ... NOT DOCUMENTED! + * - drscan ... + * + * The "irscan" command (for example) doesn't show twice. + */ static const struct command_registration jtag_command_handlers_to_move[] = { { .name = "drscan", .mode = COMMAND_EXEC, - .jim_handler = &Jim_Command_drscan, - .help = "execute DR scan " - " ...", + .jim_handler = Jim_Command_drscan, + .help = "Execute Data Register (DR) scan for one TAP. " + "Other TAPs must be in BYPASS mode.", + .usage = "tap_name [num_bits value]* ['-endstate' state_name]", }, { .name = "flush_count", .mode = COMMAND_EXEC, - .jim_handler = &Jim_Command_flush_count, - .help = "returns number of times the JTAG queue has been flushed", + .jim_handler = Jim_Command_flush_count, + .help = "Returns the number of times the JTAG queue " + "has been flushed.", }, { .name = "pathmove", .mode = COMMAND_EXEC, - .jim_handler = &Jim_Command_pathmove, - .usage = ",,... ", - .help = "move JTAG to state1 then to state2, state3, etc.", + .jim_handler = Jim_Command_pathmove, + .usage = "start_state state1 [state2 [state3 ...]]", + .help = "Move JTAG state machine from current state " + "(start_state) to state1, then state2, state3, etc.", }, COMMAND_REGISTRATION_DONE }; @@ -341,8 +365,9 @@ static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap) if (!found) jteap = calloc(1, sizeof(*jteap)); else if (NULL != jteap->body) - Jim_DecrRefCount(interp, jteap->body); + Jim_DecrRefCount(goi->interp, jteap->body); + jteap->interp = goi->interp; jteap->event = n->value; Jim_Obj *o; @@ -359,6 +384,7 @@ static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap) } else if (found) { + jteap->interp = goi->interp; Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body)); } @@ -406,27 +432,109 @@ static int is_bad_irval(int ir_length, jim_wide w) return (w & v) != 0; } +static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi, + struct jtag_tap *pTap) +{ + jim_wide w; + int e = Jim_GetOpt_Wide(goi, &w); + if (e != JIM_OK) { + Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name); + return e; + } + + unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt; + uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t)); + if (new_expected_ids == NULL) + { + Jim_SetResult_sprintf(goi->interp, "no memory"); + return JIM_ERR; + } + + memcpy(new_expected_ids, pTap->expected_ids, expected_len); + + new_expected_ids[pTap->expected_ids_cnt] = w; + + free(pTap->expected_ids); + pTap->expected_ids = new_expected_ids; + pTap->expected_ids_cnt++; + + return JIM_OK; +} + +#define NTAP_OPT_IRLEN 0 +#define NTAP_OPT_IRMASK 1 +#define NTAP_OPT_IRCAPTURE 2 +#define NTAP_OPT_ENABLED 3 +#define NTAP_OPT_DISABLED 4 +#define NTAP_OPT_EXPECTED_ID 5 +#define NTAP_OPT_VERSION 6 + +static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi, + struct jtag_tap *pTap) +{ + jim_wide w; + int e = Jim_GetOpt_Wide(goi, &w); + if (e != JIM_OK) + { + Jim_SetResult_sprintf(goi->interp, + "option: %s bad parameter", n->name); + free((void *)pTap->dotted_name); + return e; + } + switch (n->value) { + case NTAP_OPT_IRLEN: + if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) + { + LOG_WARNING("%s: huge IR length %d", + pTap->dotted_name, (int) w); + } + pTap->ir_length = w; + break; + case NTAP_OPT_IRMASK: + if (is_bad_irval(pTap->ir_length, w)) + { + LOG_ERROR("%s: IR mask %x too big", + pTap->dotted_name, + (int) w); + return JIM_ERR; + } + if ((w & 3) != 3) + LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name); + pTap->ir_capture_mask = w; + break; + case NTAP_OPT_IRCAPTURE: + if (is_bad_irval(pTap->ir_length, w)) + { + LOG_ERROR("%s: IR capture %x too big", + pTap->dotted_name, (int) w); + return JIM_ERR; + } + if ((w & 3) != 1) + LOG_WARNING("%s: nonstandard IR value", + pTap->dotted_name); + pTap->ir_capture_value = w; + break; + default: + return JIM_ERR; + } + return JIM_OK; +} + static int jim_newtap_cmd(Jim_GetOptInfo *goi) { struct jtag_tap *pTap; - jim_wide w; int x; int e; Jim_Nvp *n; char *cp; const Jim_Nvp opts[] = { -#define NTAP_OPT_IRLEN 0 { .name = "-irlen" , .value = NTAP_OPT_IRLEN }, -#define NTAP_OPT_IRMASK 1 { .name = "-irmask" , .value = NTAP_OPT_IRMASK }, -#define NTAP_OPT_IRCAPTURE 2 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE }, -#define NTAP_OPT_ENABLED 3 { .name = "-enable" , .value = NTAP_OPT_ENABLED }, -#define NTAP_OPT_DISABLED 4 { .name = "-disable" , .value = NTAP_OPT_DISABLED }, -#define NTAP_OPT_EXPECTED_ID 5 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID }, + { .name = "-ignore-version" , .value = NTAP_OPT_VERSION }, { .name = NULL , .value = -1 }, }; @@ -483,81 +591,28 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi) pTap->disabled_after_reset = true; break; case NTAP_OPT_EXPECTED_ID: - { - uint32_t *new_expected_ids; - - e = Jim_GetOpt_Wide(goi, &w); - if (e != JIM_OK) { - Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name); + e = jim_newtap_expected_id(n, goi, pTap); + if (JIM_OK != e) + { free((void *)pTap->dotted_name); free(pTap); return e; } - - new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1)); - if (new_expected_ids == NULL) { - Jim_SetResult_sprintf(goi->interp, "no memory"); - free((void *)pTap->dotted_name); - free(pTap); - return JIM_ERR; - } - - memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt); - - new_expected_ids[pTap->expected_ids_cnt] = w; - - free(pTap->expected_ids); - pTap->expected_ids = new_expected_ids; - pTap->expected_ids_cnt++; break; - } case NTAP_OPT_IRLEN: case NTAP_OPT_IRMASK: case NTAP_OPT_IRCAPTURE: - e = Jim_GetOpt_Wide(goi, &w); - if (e != JIM_OK) { - Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name); + e = jim_newtap_ir_param(n, goi, pTap); + if (JIM_OK != e) + { free((void *)pTap->dotted_name); free(pTap); return e; } - switch (n->value) { - case NTAP_OPT_IRLEN: - if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) - LOG_WARNING("%s: huge IR length %d", - pTap->dotted_name, - (int) w); - pTap->ir_length = w; - break; - case NTAP_OPT_IRMASK: - if (is_bad_irval(pTap->ir_length, w)) { - LOG_ERROR("%s: IR mask %x too big", - pTap->dotted_name, - (int) w); - free((void *)pTap->dotted_name); - free(pTap); - return ERROR_FAIL; - } - if ((w & 3) != 3) - LOG_WARNING("%s: nonstandard IR mask", - pTap->dotted_name); - pTap->ir_capture_mask = w; - break; - case NTAP_OPT_IRCAPTURE: - if (is_bad_irval(pTap->ir_length, w)) { - LOG_ERROR("%s: IR capture %x too big", - pTap->dotted_name, - (int) w); - free((void *)pTap->dotted_name); - free(pTap); - return ERROR_FAIL; - } - if ((w & 3) != 1) - LOG_WARNING("%s: nonstandard IR value", - pTap->dotted_name); - pTap->ir_capture_value = w; - break; - } + break; + case NTAP_OPT_VERSION: + pTap->ignore_version = true; + break; } /* switch (n->value) */ } /* while (goi->argc) */ @@ -568,7 +623,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi) if (pTap->ir_length != 0) { jtag_tap_init(pTap); - return ERROR_OK; + return JIM_OK; } Jim_SetResult_sprintf(goi->interp, @@ -592,9 +647,9 @@ static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e) tap->dotted_name, e, nvp->name, Jim_GetString(jteap->body, NULL)); - if (Jim_EvalObj(interp, jteap->body) != JIM_OK) + if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK) { - Jim_PrintErrorMessage(interp); + Jim_PrintErrorMessage(jteap->interp); continue; } @@ -616,7 +671,8 @@ static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e) } } -static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +static int +jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_GetOptInfo goi; Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); @@ -782,71 +838,109 @@ static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return JIM_OK; } +COMMAND_HANDLER(handle_jtag_init_command) +{ + if (CMD_ARGC != 0) + return ERROR_COMMAND_SYNTAX_ERROR; + + static bool jtag_initialized = false; + if (jtag_initialized) + { + LOG_INFO("'jtag init' has already been called"); + return ERROR_OK; + } + jtag_initialized = true; + + LOG_DEBUG("Initializing jtag devices..."); + return jtag_init(CMD_CTX); +} + static const struct command_registration jtag_subcommand_handlers[] = { + { + .name = "init", + .mode = COMMAND_ANY, + .handler = handle_jtag_init_command, + .help = "initialize jtag scan chain", + }, { .name = "interface", .mode = COMMAND_ANY, - .jim_handler = &jim_jtag_interface, - .help = "Returns the selected interface", + .jim_handler = jim_jtag_interface, + .help = "Returns the name of the currently selected interface.", }, { .name = "arp_init", .mode = COMMAND_ANY, - .jim_handler = &jim_jtag_arp_init, + .jim_handler = jim_jtag_arp_init, + .help = "Validates JTAG scan chain against the list of " + "declared TAPs using just the four standard JTAG " + "signals.", }, { .name = "arp_init-reset", .mode = COMMAND_ANY, - .jim_handler = &jim_jtag_arp_init_reset, + .jim_handler = jim_jtag_arp_init_reset, + .help = "Uses TRST and SRST to try resetting everything on " + "the JTAG scan chain, then performs 'jtag arp_init'." }, { .name = "newtap", .mode = COMMAND_CONFIG, - .jim_handler = &jim_jtag_newtap, - .help = "Create a new TAP instance", - .usage = " -irlen [-ircapture ] " - "[-irmask ] [-enable|-disable]", + .jim_handler = jim_jtag_newtap, + .help = "Create a new TAP instance named basename.tap_type, " + "and appends it to the scan chain.", + .usage = "basename tap_type '-irlen' count " + "['-enable'|'-disable'] " + "['-expected_id' number] " + "['-ignore-version'] " + "['-ircapture' number] " + "['-mask' number] ", }, { .name = "tapisenabled", .mode = COMMAND_EXEC, - .jim_handler = &jim_jtag_tap_enabler, - .help = "Returns a integer indicating TAP state (0/1)", - .usage = "", + .jim_handler = jim_jtag_tap_enabler, + .help = "Returns a Tcl boolean (0/1) indicating whether " + "the TAP is enabled (1) or not (0).", + .usage = "tap_name", }, { .name = "tapenable", .mode = COMMAND_EXEC, - .jim_handler = &jim_jtag_tap_enabler, - .help = "Enable the specified TAP", - .usage = "", + .jim_handler = jim_jtag_tap_enabler, + .help = "Try to enable the specified TAP using the " + "'tap-enable' TAP event.", + .usage = "tap_name", }, { .name = "tapdisable", .mode = COMMAND_EXEC, - .jim_handler = &jim_jtag_tap_enabler, - .help = "Enable the specified TAP", - .usage = "", + .jim_handler = jim_jtag_tap_enabler, + .help = "Try to disable the specified TAP using the " + "'tap-disable' TAP event.", + .usage = "tap_name", }, { .name = "configure", .mode = COMMAND_EXEC, - .jim_handler = &jim_jtag_configure, - .help = "Enable the specified TAP", - .usage = " [ ...]", + .jim_handler = jim_jtag_configure, + .help = "Provide a Tcl handler for the specified " + "TAP event.", + .usage = "tap_name '-event' event_name handler", }, { .name = "cget", .mode = COMMAND_EXEC, - .jim_handler = &jim_jtag_configure, - .help = "Enable the specified TAP", - .usage = " [ ...]", + .jim_handler = jim_jtag_configure, + .help = "Return any Tcl handler for the specified " + "TAP event.", + .usage = "tap_name '-event' event_name", }, { .name = "names", .mode = COMMAND_ANY, - .jim_handler = &jim_jtag_names, - .help = "Returns list of all JTAG tap names", + .jim_handler = jim_jtag_names, + .help = "Returns list of all JTAG tap names.", }, { .chain = jtag_command_handlers_to_move, @@ -953,32 +1047,47 @@ COMMAND_HANDLER(handle_interface_command) COMMAND_HANDLER(handle_scan_chain_command) { struct jtag_tap *tap; + char expected_id[12]; tap = jtag_all_taps(); - command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr "); - command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------"); + command_print(CMD_CTX, +" TapName Enabled IdCode Expected IrLen IrCap IrMask"); + command_print(CMD_CTX, +"-- ------------------- -------- ---------- ---------- ----- ----- ------"); while (tap) { - uint32_t expected, expected_mask, cur_instr, ii; + uint32_t expected, expected_mask, ii; + + snprintf(expected_id, sizeof expected_id, "0x%08x", + (unsigned)((tap->expected_ids_cnt > 0) + ? tap->expected_ids[0] + : 0)); + if (tap->ignore_version) + expected_id[2] = '*'; + expected = buf_get_u32(tap->expected, 0, tap->ir_length); expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length); - cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length); command_print(CMD_CTX, - "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x", + "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x", tap->abs_chain_position, tap->dotted_name, tap->enabled ? 'Y' : 'n', (unsigned int)(tap->idcode), - (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0), + expected_id, (unsigned int)(tap->ir_length), (unsigned int)(expected), - (unsigned int)(expected_mask), - (unsigned int)(cur_instr)); + (unsigned int)(expected_mask)); for (ii = 1; ii < tap->expected_ids_cnt; ii++) { - command_print(CMD_CTX, " | | | | 0x%08x | | | | ", - (unsigned int)(tap->expected_ids[ii])); + snprintf(expected_id, sizeof expected_id, "0x%08x", + (unsigned) tap->expected_ids[1]); + if (tap->ignore_version) + expected_id[2] = '*'; + + command_print(CMD_CTX, + " %s", + expected_id); } tap = tap->next_tap; @@ -1494,34 +1603,38 @@ COMMAND_HANDLER(handle_tms_sequence_command) static const struct command_registration jtag_command_handlers[] = { { .name = "interface", - .handler = &handle_interface_command, + .handler = handle_interface_command, .mode = COMMAND_CONFIG, - .help = "select a JTAG interface", - .usage = "", + .help = "Select a JTAG interface", + .usage = "driver_name", }, { .name = "interface_list", - .handler = &handle_interface_list_command, + .handler = handle_interface_list_command, .mode = COMMAND_ANY, - .help = "list all built-in interfaces", + .help = "List all built-in interfaces", }, { .name = "jtag_khz", - .handler = &handle_jtag_khz_command, + .handler = handle_jtag_khz_command, .mode = COMMAND_ANY, - .help = "set maximum jtag speed (if supported)", - .usage = "", + .help = "With an argument, change to the specified maximum " + "jtag speed. Pass 0 to require adaptive clocking. " + "With or without argument, display current setting.", + .usage = "[khz]", }, { .name = "jtag_rclk", - .handler = &handle_jtag_rclk_command, + .handler = handle_jtag_rclk_command, .mode = COMMAND_ANY, - .help = "set JTAG speed to RCLK or use fallback speed", - .usage = "", + .help = "With an argument, change to to use adaptive clocking " + "if possible; else to use the fallback speed. " + "With or without argument, display current setting.", + .usage = "[fallback_speed_khz]", }, { .name = "reset_config", - .handler = &handle_reset_config_command, + .handler = handle_reset_config_command, .mode = COMMAND_ANY, .help = "configure JTAG reset behavior", .usage = "[none|trst_only|srst_only|trst_and_srst] " @@ -1532,79 +1645,87 @@ static const struct command_registration jtag_command_handlers[] = { }, { .name = "jtag_nsrst_delay", - .handler = &handle_jtag_nsrst_delay_command, + .handler = handle_jtag_nsrst_delay_command, .mode = COMMAND_ANY, .help = "delay after deasserting srst in ms", - .usage = "", + .usage = "[milliseconds]", }, { .name = "jtag_ntrst_delay", - .handler = &handle_jtag_ntrst_delay_command, + .handler = handle_jtag_ntrst_delay_command, .mode = COMMAND_ANY, .help = "delay after deasserting trst in ms", - .usage = "" + .usage = "[milliseconds]", }, { .name = "jtag_nsrst_assert_width", - .handler = &handle_jtag_nsrst_assert_width_command, + .handler = handle_jtag_nsrst_assert_width_command, .mode = COMMAND_ANY, .help = "delay after asserting srst in ms", - .usage = "" + .usage = "[milliseconds]", }, { .name = "jtag_ntrst_assert_width", - .handler = &handle_jtag_ntrst_assert_width_command, + .handler = handle_jtag_ntrst_assert_width_command, .mode = COMMAND_ANY, .help = "delay after asserting trst in ms", - .usage = "" + .usage = "[milliseconds]", }, { .name = "scan_chain", - .handler = &handle_scan_chain_command, - .mode = COMMAND_EXEC, + .handler = handle_scan_chain_command, + .mode = COMMAND_ANY, .help = "print current scan chain configuration", }, { .name = "jtag_reset", - .handler = &handle_jtag_reset_command, + .handler = handle_jtag_reset_command, .mode = COMMAND_EXEC, - .help = "toggle reset lines", - .usage = " ", + .help = "Set reset line values. Value '1' is active, " + "value '0' is inactive.", + .usage = "trst_active srst_active", }, { .name = "runtest", - .handler = &handle_runtest_command, + .handler = handle_runtest_command, .mode = COMMAND_EXEC, - .help = "move to Run-Test/Idle, and execute ", - .usage = "" + .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.", + .usage = "num_cycles" }, { .name = "irscan", - .handler = &handle_irscan_command, + .handler = handle_irscan_command, .mode = COMMAND_EXEC, - .help = "execute IR scan", - .usage = " [dev2] [instr2] ...", + .help = "Execute Instruction Register (DR) scan. The " + "specified opcodes are put into each TAP's IR, " + "and other TAPs are put in BYPASS.", + .usage = "[tap_name instruction]* ['-endstate' state_name]", }, { .name = "verify_ircapture", - .handler = &handle_verify_ircapture_command, + .handler = handle_verify_ircapture_command, .mode = COMMAND_ANY, - .help = "verify value captured during Capture-IR", - .usage = "", + .help = "Display or assign flag controlling whether to " + "verify values captured during Capture-IR.", + .usage = "['enable'|'disable']", }, { .name = "verify_jtag", - .handler = &handle_verify_jtag_command, + .handler = handle_verify_jtag_command, .mode = COMMAND_ANY, - .help = "verify value capture", - .usage = "", + .help = "Display or assign flag controlling whether to " + "verify values captured during IR and DR scans.", + .usage = "['enable'|'disable']", }, { .name = "tms_sequence", - .handler = &handle_tms_sequence_command, + .handler = handle_tms_sequence_command, .mode = COMMAND_ANY, - .help = "choose short(default) or long tms_sequence", - .usage = "", + .help = "Display or change what style TMS sequences to use " + "for JTAG state transitions: short (default) or " + "long. Only for working around JTAG bugs.", + /* Specifically for working around DRIVER bugs... */ + .usage = "['short'|'long']", }, { .name = "jtag",