X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Fjtag%2Fadapter.c;h=47a1d794b3bcfd65b86f1363de5b481e2126b620;hb=4cb3c9fae2a6a6cb5cf2c664179edace22abf5f6;hp=e11f4e9067efdc9a1e51be1d9361409e89f3312a;hpb=e162200ab2cd6c2202bd437dca91137eacf9021c;p=openocd.git diff --git a/src/jtag/adapter.c b/src/jtag/adapter.c index e11f4e9067..47a1d794b3 100644 --- a/src/jtag/adapter.c +++ b/src/jtag/adapter.c @@ -1,41 +1,22 @@ -/*************************************************************************** - * Copyright (C) 2005 by Dominic Rath * - * Dominic.Rath@gmx.de * - * * - * Copyright (C) 2007-2010 Øyvind Harboe * - * oyvind.harboe@zylin.com * - * * - * Copyright (C) 2009 SoftPLC Corporation * - * http://softplc.com * - * dick@softplc.com * - * * - * Copyright (C) 2009 Zachary T Welch * - * zw@superlucidity.net * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2005 by Dominic Rath + * Copyright (C) 2007-2010 Øyvind Harboe + * Copyright (C) 2009 SoftPLC Corporation, http://softplc.com, Dick Hollenbeck + * Copyright (C) 2009 Zachary T Welch + * Copyright (C) 2018 Pengutronix, Oleksij Rempel + */ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include "adapter.h" #include "jtag.h" #include "minidriver.h" #include "interface.h" #include "interfaces.h" #include -#include #ifdef HAVE_STRINGS_H #include @@ -46,51 +27,109 @@ * Holds support for configuring debug adapters from TCl scripts. */ -extern struct jtag_interface *jtag_interface; +struct adapter_driver *adapter_driver; const char * const jtag_only[] = { "jtag", NULL }; -static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv) +/** + * Adapter configuration + */ +static struct { + char *usb_location; +} adapter_config; + +/* + * 1 char: bus + * 2 * 7 chars: max 7 ports + * 1 char: test for overflow + * ------ + * 16 chars + */ +#define USB_MAX_LOCATION_LENGTH 16 + +#ifdef HAVE_LIBUSB_GET_PORT_NUMBERS +static void adapter_usb_set_location(const char *location) { - Jim_GetOptInfo goi; - Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1); + if (strnlen(location, USB_MAX_LOCATION_LENGTH) == USB_MAX_LOCATION_LENGTH) + LOG_WARNING("usb location string is too long!!"); - /* return the name of the interface */ - /* TCL code might need to know the exact type... */ - /* FUTURE: we allow this as a means to "set" the interface. */ - if (goi.argc != 0) { - Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); - return JIM_ERR; - } - const char *name = jtag_interface ? jtag_interface->name : NULL; - Jim_SetResultString(goi.interp, name ? : "undefined", -1); - return JIM_OK; -} + free(adapter_config.usb_location); -static int default_khz(int khz, int *jtag_speed) -{ - LOG_ERROR("Translation from khz to jtag_speed not implemented"); - return ERROR_FAIL; + adapter_config.usb_location = strndup(location, USB_MAX_LOCATION_LENGTH); } +#endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */ -static int default_speed_div(int speed, int *khz) +const char *adapter_usb_get_location(void) { - LOG_ERROR("Translation from jtag_speed to khz not implemented"); - return ERROR_FAIL; + return adapter_config.usb_location; } -static int default_power_dropout(int *dropout) +bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len) { - *dropout = 0; /* by default we can't detect power dropout */ - return ERROR_OK; + size_t path_step, string_length; + char *ptr, *loc; + bool equal = false; + + if (!adapter_usb_get_location()) + return equal; + + /* strtok need non const char */ + loc = strndup(adapter_usb_get_location(), USB_MAX_LOCATION_LENGTH); + string_length = strnlen(loc, USB_MAX_LOCATION_LENGTH); + + ptr = strtok(loc, "-"); + if (!ptr) { + LOG_WARNING("no '-' in usb path\n"); + goto done; + } + + string_length -= strnlen(ptr, string_length); + /* check bus mismatch */ + if (atoi(ptr) != dev_bus) + goto done; + + path_step = 0; + while (path_step < path_len) { + ptr = strtok(NULL, "."); + + /* no more tokens in path */ + if (!ptr) + break; + + /* path mismatch at some step */ + if (path_step < path_len && atoi(ptr) != port_path[path_step]) + break; + + path_step++; + string_length -= strnlen(ptr, string_length) + 1; + }; + + /* walked the full path, all elements match */ + if (path_step == path_len && !string_length) + equal = true; + +done: + free(loc); + return equal; } -static int default_srst_asserted(int *srst_asserted) +static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { - *srst_asserted = 0; /* by default we can't detect srst asserted */ - return ERROR_OK; + struct jim_getopt_info goi; + jim_getopt_setup(&goi, interp, argc-1, argv + 1); + + /* return the name of the interface */ + /* TCL code might need to know the exact type... */ + /* FUTURE: we allow this as a means to "set" the interface. */ + if (goi.argc != 0) { + Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)"); + return JIM_ERR; + } + const char *name = adapter_driver ? adapter_driver->name : NULL; + Jim_SetResultString(goi.interp, name ? name : "undefined", -1); + return JIM_OK; } -COMMAND_HANDLER(interface_transport_command) +COMMAND_HANDLER(adapter_transports_command) { char **transports; int retval; @@ -109,26 +148,26 @@ COMMAND_HANDLER(interface_transport_command) return retval; } -COMMAND_HANDLER(handle_interface_list_command) +COMMAND_HANDLER(handle_adapter_list_command) { - if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0) + if (strcmp(CMD_NAME, "list") == 0 && CMD_ARGC > 0) return ERROR_COMMAND_SYNTAX_ERROR; - command_print(CMD, "The following debug interfaces are available:"); - for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) { - const char *name = jtag_interfaces[i]->name; + command_print(CMD, "The following debug adapters are available:"); + for (unsigned i = 0; adapter_drivers[i]; i++) { + const char *name = adapter_drivers[i]->name; command_print(CMD, "%u: %s", i + 1, name); } return ERROR_OK; } -COMMAND_HANDLER(handle_interface_command) +COMMAND_HANDLER(handle_adapter_driver_command) { int retval; /* check whether the interface is already configured */ - if (jtag_interface) { + if (adapter_driver) { LOG_WARNING("Interface already configured, ignoring"); return ERROR_OK; } @@ -137,42 +176,20 @@ COMMAND_HANDLER(handle_interface_command) if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0') return ERROR_COMMAND_SYNTAX_ERROR; - for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) { - if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0) + for (unsigned i = 0; adapter_drivers[i]; i++) { + if (strcmp(CMD_ARGV[0], adapter_drivers[i]->name) != 0) continue; - if (NULL != jtag_interfaces[i]->commands) { + if (adapter_drivers[i]->commands) { retval = register_commands(CMD_CTX, NULL, - jtag_interfaces[i]->commands); - if (ERROR_OK != retval) + adapter_drivers[i]->commands); + if (retval != ERROR_OK) return retval; } - jtag_interface = jtag_interfaces[i]; - - /* LEGACY SUPPORT ... adapter drivers must declare what - * transports they allow. Until they all do so, assume - * the legacy drivers are JTAG-only - */ - if (!jtag_interface->transports) - LOG_WARNING("Adapter driver '%s' did not declare " - "which transports it allows; assuming " - "legacy JTAG-only", jtag_interface->name); - retval = allow_transports(CMD_CTX, jtag_interface->transports - ? jtag_interface->transports : jtag_only); - if (ERROR_OK != retval) - return retval; + adapter_driver = adapter_drivers[i]; - if (jtag_interface->khz == NULL) - jtag_interface->khz = default_khz; - if (jtag_interface->speed_div == NULL) - jtag_interface->speed_div = default_speed_div; - if (jtag_interface->power_dropout == NULL) - jtag_interface->power_dropout = default_power_dropout; - if (jtag_interface->srst_asserted == NULL) - jtag_interface->srst_asserted = default_srst_asserted; - - return ERROR_OK; + return allow_transports(CMD_CTX, adapter_driver->transports); } /* no valid interface was found (i.e. the configuration option, @@ -180,7 +197,7 @@ COMMAND_HANDLER(handle_interface_command) */ LOG_ERROR("The specified debug interface was not found (%s)", CMD_ARGV[0]); - CALL_COMMAND_HANDLER(handle_interface_list_command); + CALL_COMMAND_HANDLER(handle_adapter_list_command); return ERROR_JTAG_INVALID_INTERFACE; } @@ -401,7 +418,7 @@ next: return ERROR_OK; } -COMMAND_HANDLER(handle_adapter_nsrst_delay_command) +COMMAND_HANDLER(handle_adapter_srst_delay_command) { if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; @@ -411,11 +428,11 @@ COMMAND_HANDLER(handle_adapter_nsrst_delay_command) jtag_set_nsrst_delay(delay); } - command_print(CMD, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay()); + command_print(CMD, "adapter srst delay: %u", jtag_get_nsrst_delay()); return ERROR_OK; } -COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command) +COMMAND_HANDLER(handle_adapter_srst_pulse_width_command) { if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; @@ -425,11 +442,11 @@ COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command) jtag_set_nsrst_assert_width(width); } - command_print(CMD, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width()); + command_print(CMD, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width()); return ERROR_OK; } -COMMAND_HANDLER(handle_adapter_khz_command) +COMMAND_HANDLER(handle_adapter_speed_command) { if (CMD_ARGC > 1) return ERROR_COMMAND_SYNTAX_ERROR; @@ -440,13 +457,13 @@ COMMAND_HANDLER(handle_adapter_khz_command) COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz); retval = jtag_config_khz(khz); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; } int cur_speed = jtag_get_speed_khz(); retval = jtag_get_speed_readable(&cur_speed); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; if (cur_speed) @@ -457,14 +474,99 @@ COMMAND_HANDLER(handle_adapter_khz_command) return retval; } -#ifndef HAVE_JTAG_MINIDRIVER_H +COMMAND_HANDLER(handle_adapter_reset_de_assert) +{ + enum values { + VALUE_UNDEFINED = -1, + VALUE_DEASSERT = 0, + VALUE_ASSERT = 1, + }; + enum values value; + enum values srst = VALUE_UNDEFINED; + enum values trst = VALUE_UNDEFINED; + enum reset_types jtag_reset_config = jtag_get_reset_config(); + char *signal; + + if (CMD_ARGC == 0) { + if (transport_is_jtag()) { + if (jtag_reset_config & RESET_HAS_TRST) + signal = jtag_get_trst() ? "asserted" : "deasserted"; + else + signal = "not present"; + command_print(CMD, "trst %s", signal); + } + + if (jtag_reset_config & RESET_HAS_SRST) + signal = jtag_get_srst() ? "asserted" : "deasserted"; + else + signal = "not present"; + command_print(CMD, "srst %s", signal); + + return ERROR_OK; + } + + if (CMD_ARGC != 1 && CMD_ARGC != 3) + return ERROR_COMMAND_SYNTAX_ERROR; + + value = (strcmp(CMD_NAME, "assert") == 0) ? VALUE_ASSERT : VALUE_DEASSERT; + if (strcmp(CMD_ARGV[0], "srst") == 0) + srst = value; + else if (strcmp(CMD_ARGV[0], "trst") == 0) + trst = value; + else + return ERROR_COMMAND_SYNTAX_ERROR; + + if (CMD_ARGC == 3) { + if (strcmp(CMD_ARGV[1], "assert") == 0) + value = VALUE_ASSERT; + else if (strcmp(CMD_ARGV[1], "deassert") == 0) + value = VALUE_DEASSERT; + else + return ERROR_COMMAND_SYNTAX_ERROR; + + if (strcmp(CMD_ARGV[2], "srst") == 0 && srst == VALUE_UNDEFINED) + srst = value; + else if (strcmp(CMD_ARGV[2], "trst") == 0 && trst == VALUE_UNDEFINED) + trst = value; + else + return ERROR_COMMAND_SYNTAX_ERROR; + } + + if (trst == VALUE_UNDEFINED) { + if (transport_is_jtag()) + trst = jtag_get_trst() ? VALUE_ASSERT : VALUE_DEASSERT; + else + trst = VALUE_DEASSERT; /* unused, safe value */ + } + + if (srst == VALUE_UNDEFINED) { + if (jtag_reset_config & RESET_HAS_SRST) + srst = jtag_get_srst() ? VALUE_ASSERT : VALUE_DEASSERT; + else + srst = VALUE_DEASSERT; /* unused, safe value */ + } + + if (trst == VALUE_ASSERT && !transport_is_jtag()) { + LOG_ERROR("transport has no trst signal"); + return ERROR_FAIL; + } + + if (srst == VALUE_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) { + LOG_ERROR("adapter has no srst signal"); + return ERROR_FAIL; + } + + return adapter_resets((trst == VALUE_DEASSERT) ? TRST_DEASSERT : TRST_ASSERT, + (srst == VALUE_DEASSERT) ? SRST_DEASSERT : SRST_ASSERT); +} + #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS COMMAND_HANDLER(handle_usb_location_command) { if (CMD_ARGC == 1) - jtag_usb_set_location(CMD_ARGV[0]); + adapter_usb_set_location(CMD_ARGV[0]); - command_print(CMD, "adapter usb location: %s", jtag_usb_get_location()); + command_print(CMD, "adapter usb location: %s", adapter_usb_get_location()); return ERROR_OK; } @@ -482,80 +584,102 @@ static const struct command_registration adapter_usb_command_handlers[] = { #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */ COMMAND_REGISTRATION_DONE }; -#endif /* MINIDRIVER */ -static const struct command_registration adapter_command_handlers[] = { -#ifndef HAVE_JTAG_MINIDRIVER_H +static const struct command_registration adapter_srst_command_handlers[] = { { - .name = "usb", + .name = "delay", + .handler = handle_adapter_srst_delay_command, .mode = COMMAND_ANY, - .help = "usb adapter command group", - .usage = "", - .chain = adapter_usb_command_handlers, + .help = "delay after deasserting SRST in ms", + .usage = "[milliseconds]", + }, + { + .name = "pulse_width", + .handler = handle_adapter_srst_pulse_width_command, + .mode = COMMAND_ANY, + .help = "SRST assertion pulse width in ms", + .usage = "[milliseconds]", }, -#endif /* MINIDRIVER */ COMMAND_REGISTRATION_DONE }; -static const struct command_registration interface_command_handlers[] = { +static const struct command_registration adapter_command_handlers[] = { { - .name = "adapter", - .mode = COMMAND_ANY, - .help = "adapter command group", - .usage = "", - .chain = adapter_command_handlers, + .name = "driver", + .handler = handle_adapter_driver_command, + .mode = COMMAND_CONFIG, + .help = "Select a debug adapter driver", + .usage = "driver_name", }, { - .name = "adapter_khz", - .handler = handle_adapter_khz_command, + .name = "speed", + .handler = handle_adapter_speed_command, .mode = COMMAND_ANY, .help = "With an argument, change to the specified maximum " "jtag speed. For JTAG, 0 KHz signifies adaptive " - " clocking. " + "clocking. " "With or without argument, display current setting.", .usage = "[khz]", }, { - .name = "adapter_name", + .name = "list", + .handler = handle_adapter_list_command, + .mode = COMMAND_ANY, + .help = "List all built-in debug adapter drivers", + .usage = "", + }, + { + .name = "name", .mode = COMMAND_ANY, .jim_handler = jim_adapter_name, .help = "Returns the name of the currently " "selected adapter (driver)", }, { - .name = "adapter_nsrst_delay", - .handler = handle_adapter_nsrst_delay_command, + .name = "srst", .mode = COMMAND_ANY, - .help = "delay after deasserting SRST in ms", - .usage = "[milliseconds]", + .help = "srst adapter command group", + .usage = "", + .chain = adapter_srst_command_handlers, + }, + { + .name = "transports", + .handler = adapter_transports_command, + .mode = COMMAND_CONFIG, + .help = "Declare transports the adapter supports.", + .usage = "transport ...", }, { - .name = "adapter_nsrst_assert_width", - .handler = handle_adapter_nsrst_assert_width_command, + .name = "usb", .mode = COMMAND_ANY, - .help = "delay after asserting SRST in ms", - .usage = "[milliseconds]", + .help = "usb adapter command group", + .usage = "", + .chain = adapter_usb_command_handlers, }, { - .name = "interface", - .handler = handle_interface_command, - .mode = COMMAND_CONFIG, - .help = "Select a debug adapter interface (driver)", - .usage = "driver_name", + .name = "assert", + .handler = handle_adapter_reset_de_assert, + .mode = COMMAND_EXEC, + .help = "Controls SRST and TRST lines.", + .usage = "|deassert [srst|trst [assert|deassert srst|trst]]", }, { - .name = "interface_transports", - .handler = interface_transport_command, - .mode = COMMAND_CONFIG, - .help = "Declare transports the interface supports.", - .usage = "transport ... ", + .name = "deassert", + .handler = handle_adapter_reset_de_assert, + .mode = COMMAND_EXEC, + .help = "Controls SRST and TRST lines.", + .usage = "|assert [srst|trst [deassert|assert srst|trst]]", }, + COMMAND_REGISTRATION_DONE +}; + +static const struct command_registration interface_command_handlers[] = { { - .name = "interface_list", - .handler = handle_interface_list_command, + .name = "adapter", .mode = COMMAND_ANY, - .help = "List all built-in debug adapter interfaces (drivers)", + .help = "adapter command group", .usage = "", + .chain = adapter_command_handlers, }, { .name = "reset_config",