From 00ea73ab166ebc8a2c4b5693b7fa776c76c7fe45 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Tue, 25 Jul 2017 16:22:27 -0700 Subject: usb_i2c: Fail if board I2C bridge is disabled Add usb_i2c_board_is_enabled(). On Cr50, this is now also connected to the I2C CCD capability. The USB-I2C bridge can only be used when the capability is available. On other platforms (Servo V4, etc.) where usb_i2c_board_enable() is a no-op, add a dummy implementation which always returns true. See go/cr50-ccd-wp for more information. BUG=b:62537474 BRANCH=cr50 TEST=manual with CR50_DEV=1 Connect host PC to dev board USB port On host PC: sudo servod -c ccd_cr50.xml -c reef_r1_inas.xml dut-control pp3300_ec_shv_reg --> fail, error 0x8001 ccdoops --> reset I2C config ccd i2c disable --> I2C disabled On host PC: sudo servod -c ccd_cr50.xml -c reef_r1_inas.xml dut-control pp3300_ec_shv_reg --> fail, error 0x0006 ccd i2c enable --> I2C enabled ccdunlock --> I2C disabled ccdoops --> I2C enabled ccdset i2c unlesslocked ccdlock --> I2C disabled ccdunlock --> I2C enabled Change-Id: Ia3df32e239a5f7c5915bc6c7e408ce0dc8b26c89 Signed-off-by: Randall Spangler Reviewed-on: https://chromium-review.googlesource.com/590577 Reviewed-by: Aseda Aboagye Reviewed-by: Mary Ruthven --- board/cr50/rdd.c | 2 ++ board/cr50/usb_i2c.c | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) (limited to 'board/cr50') diff --git a/board/cr50/rdd.c b/board/cr50/rdd.c index 622bd8f986..bf5a3ed8f0 100644 --- a/board/cr50/rdd.c +++ b/board/cr50/rdd.c @@ -289,6 +289,8 @@ static int command_ccd(int argc, char **argv) ccprintf("AP UART: %s\nEC UART: %s\n", uart_tx_is_connected(UART_AP) ? " enabled" : "disabled", uart_tx_is_connected(UART_EC) ? " enabled" : "disabled"); + ccprintf("I2C: %s\n", + usb_i2c_board_is_enabled() ? " enabled" : "disabled"); return EC_SUCCESS; } diff --git a/board/cr50/usb_i2c.c b/board/cr50/usb_i2c.c index 24a6fbdea6..0bc5b3f215 100644 --- a/board/cr50/usb_i2c.c +++ b/board/cr50/usb_i2c.c @@ -3,6 +3,7 @@ * found in the LICENSE file. */ +#include "case_closed_debug.h" #include "console.h" #include "device_state.h" #include "gpio.h" @@ -16,7 +17,7 @@ #define CPRINTS(format, args...) cprints(CC_USB, format, ## args) -static int i2c_enabled(void) +int usb_i2c_board_is_enabled(void) { return !gpio_get_level(GPIO_EN_PP3300_INA_L); } @@ -64,7 +65,7 @@ static void ina_connect(void) void usb_i2c_board_disable(void) { - if (!i2c_enabled()) + if (!usb_i2c_board_is_enabled()) return; ina_disconnect(); @@ -78,8 +79,41 @@ int usb_i2c_board_enable(void) return EC_ERROR_BUSY; } - if (!i2c_enabled()) + if (ccd_get_mode() != CCD_MODE_ENABLED) + return EC_ERROR_BUSY; + + if (!ccd_is_cap_enabled(CCD_CAP_I2C)) + return EC_ERROR_ACCESS_DENIED; + + if (!usb_i2c_board_is_enabled()) ina_connect(); return EC_SUCCESS; } + +/** + * CCD config change hook + */ +static void ccd_change_i2c(void) +{ + /* + * If the capability state doesn't match the current I2C enable state, + * try to make them match. + */ + if (usb_i2c_board_is_enabled() && !ccd_is_cap_enabled(CCD_CAP_I2C)) { + /* I2C bridge is enabled, but it's no longer allowed to be */ + usb_i2c_board_disable(); + } else if (!usb_i2c_board_is_enabled() && + ccd_is_cap_enabled(CCD_CAP_I2C)) { + /* + * I2C bridge is disabled, but is allowed to be enabled. Try + * enabling it. Note that this could fail for several reasons, + * such as CCD not connected, or servo attached. That's ok; + * those things will also attempt usb_i2c_board_enable() if + * their state changes later. + */ + usb_i2c_board_enable(); + } +} + +DECLARE_HOOK(HOOK_CCD_CHANGE, ccd_change_i2c, HOOK_PRIO_DEFAULT); -- cgit v1.2.1