From ca1539bde69c849672a527fc8267839f0223b4e9 Mon Sep 17 00:00:00 2001 From: Jett Rink Date: Wed, 8 May 2019 07:34:00 -0600 Subject: i2c: add i2clookup host command Add a new host command that will allow you to lookup a well known device on the EC. This is useful for FAFT tests that want to talk directly with i2c devices but don't know the physical address for each platform. BRANCH=octopus BUG=b:119065537 TEST=Used this with new faft test in CL:1601300 Change-Id: I82c2d5462fcb4edbc92ea60765971190fed7ae81 Signed-off-by: Jett Rink Reviewed-on: https://chromium-review.googlesource.com/1601060 Reviewed-by: Daisuke Nojiri (cherry picked from commit 45434aed20e695e08fcbb3f74c43e03f6fa19bf2) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1615653 Reviewed-by: Diana Z --- common/i2c_master.c | 28 ++++++++++++++++++++++++++ include/ec_commands.h | 29 +++++++++++++++++++++++++++ util/ectool.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/common/i2c_master.c b/common/i2c_master.c index c2cc22f06c..73771f412e 100644 --- a/common/i2c_master.c +++ b/common/i2c_master.c @@ -761,6 +761,34 @@ static int i2c_command_passthru(struct host_cmd_handler_args *args) } DECLARE_HOST_COMMAND(EC_CMD_I2C_PASSTHRU, i2c_command_passthru, EC_VER_MASK(0)); +static int i2c_command_lookup(struct host_cmd_handler_args *args) +{ + const struct ec_params_i2c_lookup *params = args->params; + struct ec_response_i2c_lookup *resp = args->response; + + switch (params->type) { + case I2C_LOOKUP_TYPE_CBI_EEPROM: +#ifdef CONFIG_CROS_BOARD_INFO + resp->i2c_port = I2C_PORT_EEPROM; + /* Convert from 8-bit address to 7-bit address */ + resp->i2c_addr = I2C_ADDR_EEPROM >> 1; +#else + /* Lookup type is supported, but not present on system. */ + return EC_RES_UNAVAILABLE; +#endif /* CONFIG_CROS_BOARD_INFO */ + break; + default: + /* The type was unrecognized */ + return EC_RES_INVALID_PARAM; + } + + args->response_size = sizeof(*resp); + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_I2C_LOOKUP, i2c_command_lookup, EC_VER_MASK(0)); +/* If the params union expands in the future, need to bump EC_VER_MASK */ +BUILD_ASSERT(sizeof(struct ec_params_i2c_lookup) == 4); + void i2c_passthru_protect_port(uint32_t port) { if (port < I2C_PORT_COUNT) diff --git a/include/ec_commands.h b/include/ec_commands.h index ac7e3ba022..e9100b8a67 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -5147,6 +5147,35 @@ struct ec_response_rollback_info { /* Issue AP reset */ #define EC_CMD_AP_RESET 0x0125 +/*****************************************************************************/ +/* I2C lookup command + * + * Return values: + * EC_RES_UNAVAILABLE: Lookup type is supported but not present on system. + * EC_RES_INVALID_PARAM: The type was unrecognized. + */ + +#define EC_CMD_I2C_LOOKUP 0x0126 + +enum i2c_device_type { + I2C_LOOKUP_TYPE_CBI_EEPROM = 1, + I2C_LOOKUP_TYPE_COUNT, + I2C_LOOKUP_TYPE_MAX = 0xFFFF, +}; + +struct ec_params_i2c_lookup { + uint16_t type; /* enum i2c_device_type */ + /* Used for type specific parameters in future */ + union { + uint16_t reseved; + }; +} __ec_align2; + +struct ec_response_i2c_lookup { + uint16_t i2c_port; /* Physical port for device */ + uint16_t i2c_addr; /* 7-bit (or 10-bit) address */ +} __ec_align1; + /*****************************************************************************/ /* The command range 0x200-0x2FF is reserved for Rotor. */ diff --git a/util/ectool.c b/util/ectool.c index 15d04424af..63c5bad3d9 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -6022,7 +6022,6 @@ int cmd_i2c_write(int argc, char *argv[]) return 0; } - int cmd_i2c_xfer(int argc, char *argv[]) { unsigned int port, addr; @@ -6099,6 +6098,58 @@ int cmd_i2c_xfer(int argc, char *argv[]) return 0; } +static void cmd_i2c_lookup_help(const char *const cmd) +{ + fprintf(stderr, + "Usage: %s \n" + " is one of:\n" + " 1: CBI_EEPROM\n", + cmd); +} + +int cmd_i2c_lookup(int argc, char *argv[]) +{ + struct ec_params_i2c_lookup p; + struct ec_response_i2c_lookup r; + char *e; + int rv; + + if (argc != 2) { + cmd_i2c_lookup_help(argv[0]); + return -1; + } + + p.type = strtol(argv[1], &e, 0); + if (e && *e) { + fprintf(stderr, "Bad type.\n"); + cmd_i2c_lookup_help(argv[0]); + return -1; + } + + rv = ec_command(EC_CMD_I2C_LOOKUP, 0, &p, sizeof(p), &r, sizeof(r)); + + if (rv == -EC_RES_INVALID_PARAM - EECRESULT) { + fprintf(stderr, "Lookup type %d not supported.\n", p.type); + return rv; + } + + if (rv == -EC_RES_UNAVAILABLE - EECRESULT) { + fprintf(stderr, "Device not found\n"); + return rv; + } + + if (rv < 0) + return rv; + + /* + * Do not change the format of this print. firmware_ECCbiEeprom FAFT + * test depends on this, and will silently start skipping tests. + */ + printf("Port: %d; Address: 0x%02x (7-bit format)\n", r.i2c_port, + r.i2c_addr); + return 0; +} + int cmd_lcd_backlight(int argc, char *argv[]) { struct ec_params_switch_enable_backlight p; @@ -8464,6 +8515,7 @@ const struct command commands[] = { {"hello", cmd_hello}, {"hibdelay", cmd_hibdelay}, {"hostsleepstate", cmd_hostsleepstate}, + {"i2clookup", cmd_i2c_lookup}, {"i2cprotect", cmd_i2c_protect}, {"i2cread", cmd_i2c_read}, {"i2cwrite", cmd_i2c_write}, -- cgit v1.2.1