diff options
author | Zhuohao Lee <zhuohao@chromium.org> | 2018-08-06 14:18:55 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-08-07 01:50:39 -0700 |
commit | 7c47fcc96889866ee143e01b56f78b3732657a7f (patch) | |
tree | 7e2455270ced9d72e447aabfa6806df1e0b14565 | |
parent | b3ff32c510e8842d9ee043d9c9b09a1efb60f9db (diff) | |
download | chrome-ec-7c47fcc96889866ee143e01b56f78b3732657a7f.tar.gz |
i2c: add i2c_lock before and after calling i2c_xfer
According to the include/i2c.h, we need to call i2c_lock
before and after calling i2c_xfer. This patch adds the lock
to protect the i2c data transmission.
BUG=none
BRANCH=master
TEST=make buildall -j
Change-Id: If125333902105c35ca332c154bbb8012c363d1bf
Signed-off-by: Zhuohao Lee <zhuohao@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1163543
Reviewed-by: Nick Sanders <nsanders@chromium.org>
-rw-r--r-- | board/sweetberry/board.c | 2 | ||||
-rw-r--r-- | chip/stm32/usb_power.c | 3 | ||||
-rw-r--r-- | common/usb_i2c.c | 10 |
3 files changed, 12 insertions, 3 deletions
diff --git a/board/sweetberry/board.c b/board/sweetberry/board.c index 9488695e1e..8640cfd87f 100644 --- a/board/sweetberry/board.c +++ b/board/sweetberry/board.c @@ -121,6 +121,8 @@ static void board_init(void) uint8_t tmp; /* i2c 0 has a tendancy to get wedged. TODO(nsanders): why? */ + i2c_lock(0, 1); i2c_xfer(0, 0, NULL, 0, &tmp, 1, I2C_XFER_SINGLE); + i2c_lock(0, 0); } DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT); diff --git a/chip/stm32/usb_power.c b/chip/stm32/usb_power.c index bf9e824f84..cc4f723322 100644 --- a/chip/stm32/usb_power.c +++ b/chip/stm32/usb_power.c @@ -411,8 +411,11 @@ uint16_t ina2xx_readagain(uint8_t port, uint8_t addr) int res; uint16_t val; + i2c_lock(port, 1); res = i2c_xfer(port, addr, NULL, 0, (uint8_t *)&val, sizeof(uint16_t), I2C_XFER_SINGLE); + i2c_lock(port, 0); + if (res) { CPRINTS("INA2XX I2C readagain failed p:%d a:%02x", (int)port, (int)addr); diff --git a/common/usb_i2c.c b/common/usb_i2c.c index 4c17ff487e..4ceb4fe031 100644 --- a/common/usb_i2c.c +++ b/common/usb_i2c.c @@ -109,6 +109,8 @@ void usb_i2c_execute(struct usb_i2c_config const *config) } else if (portindex >= i2c_ports_used) { config->buffer[0] = USB_I2C_PORT_INVALID; } else { + int ret; + /* * TODO (crbug.com/750397): Add security. This currently * blindly passes through ALL I2C commands on any bus the EC @@ -116,12 +118,14 @@ void usb_i2c_execute(struct usb_i2c_config const *config) * EC_CMD_I2C_PASSTHRU, which can protect ports and ranges. */ port = i2c_ports[portindex].port; - config->buffer[0] = usb_i2c_map_error( - i2c_xfer(port, slave_addr, + i2c_lock(port, 1); + ret = i2c_xfer(port, slave_addr, (uint8_t *)(config->buffer + 2) + offset, write_count, (uint8_t *)(config->buffer + 2), - read_count, I2C_XFER_SINGLE)); + read_count, I2C_XFER_SINGLE); + i2c_lock(port, 0); + config->buffer[0] = usb_i2c_map_error(ret); } usb_i2c_write_packet(config, read_count + 4); } |