diff options
author | Chun-Ta Lin <itspeter@google.com> | 2017-06-21 15:14:37 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-11-24 02:39:04 -0800 |
commit | f326fb05b564b0975a9870b2831f9372539202db (patch) | |
tree | 30aae36290a70e21b502f387f2636370259f0176 /common/usb_i2c.c | |
parent | bfa592f1673948469eca8f532da69462c4ecdbe6 (diff) | |
download | chrome-ec-f326fb05b564b0975a9870b2831f9372539202db.tar.gz |
hammer: enable large block reading on usb i2c passthru.
Originally, i2c passthru is limited to use I2C_XFER_SINGLE flag where
it can only read at most 255 bytes at a time. For application that
requires larger i2c bus reading, we change the flag setting and the
command protocol.
TEST=old ./touchpad_updater still works (previous protocol)
TEST=new ./touchpad_updater can get more than 500 bytes per transaction
TEST=Debug message only print when -d assigned.
./touchpad_updater -d
TEST=Manually change #define CONFIG_USB_I2C_MAX_READ_COUNT (1024 - 6)
to #define CONFIG_USB_I2C_MAX_READ_COUNT (1024 - 4) and trigger
POWER_OF_TWO assertion.
BRANCH=none
BUG=b:35587174, b:63993891
Change-Id: Id75b11ea49ba89bab8e18af24d47219030c778c5
Signed-off-by: Chun-Ta Lin <itspeter@google.com>
Reviewed-on: https://chromium-review.googlesource.com/542716
Commit-Ready: Chun-ta Lin <itspeter@chromium.org>
Tested-by: Chun-ta Lin <itspeter@chromium.org>
Reviewed-by: Nicolas Boichat <drinkcat@chromium.org>
Diffstat (limited to 'common/usb_i2c.c')
-rw-r--r-- | common/usb_i2c.c | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/common/usb_i2c.c b/common/usb_i2c.c index a6f5acc99c..de8314f8fa 100644 --- a/common/usb_i2c.c +++ b/common/usb_i2c.c @@ -22,6 +22,8 @@ #define CPRINTS(format, args...) cprints(CC_I2C, format, ## args) +#define MAX_BYTES_IN_ONE_READING 254 + USB_I2C_CONFIG(i2c, USB_IFACE_I2C, @@ -45,7 +47,7 @@ static uint8_t usb_i2c_read_packet(struct usb_i2c_config const *config) } static void usb_i2c_write_packet(struct usb_i2c_config const *config, - uint8_t count) + size_t count) { QUEUE_ADD_UNITS(config->tx_queue, config->buffer, count); } @@ -76,26 +78,37 @@ static uint8_t usb_i2c_executable(struct usb_i2c_config const *config) void usb_i2c_execute(struct usb_i2c_config const *config) { /* Payload is ready to execute. */ - uint8_t count = usb_i2c_read_packet(config); + uint8_t count = usb_i2c_read_packet(config); int portindex = (config->buffer[0] >> 0) & 0xff; /* Convert 7-bit slave address to chromium EC 8-bit address. */ uint8_t slave_addr = (config->buffer[0] >> 7) & 0xfe; int write_count = (config->buffer[1] >> 0) & 0xff; int read_count = (config->buffer[1] >> 8) & 0xff; - int port; + uint8_t *payload = (uint8_t *)(config->buffer + 2); + int xfer_flag = I2C_XFER_START; + int offset = 0; /* Offset for extended reading header. */ + int rv = 0; + int complete_bytes = 0; + int bytes_to_read, port; config->buffer[0] = 0; config->buffer[1] = 0; + if (read_count & 0x80) { + read_count = ((config->buffer[2] & 0xff) << 7) | + (read_count & 0x7f); + offset = 2; + } + if (!count || (!read_count && !write_count)) return; if (!usb_i2c_board_is_enabled()) { config->buffer[0] = USB_I2C_DISABLED; } else if (write_count > CONFIG_USB_I2C_MAX_WRITE_COUNT || - write_count != (count - 4)) { + write_count != (count - 4 - offset)) { config->buffer[0] = USB_I2C_WRITE_COUNT_INVALID; - } else if (read_count > USB_I2C_MAX_READ_COUNT) { + } else if (read_count > CONFIG_USB_I2C_MAX_READ_COUNT) { config->buffer[0] = USB_I2C_READ_COUNT_INVALID; } else if (portindex >= i2c_ports_used) { config->buffer[0] = USB_I2C_PORT_INVALID; @@ -107,12 +120,28 @@ 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, - (uint8_t *)(config->buffer + 2), - write_count, - (uint8_t *)(config->buffer + 2), - read_count, I2C_XFER_SINGLE)); + /* + * Because The I2C_XFER_SINGLE might limit the reading to be + * less than 255 bytes (For example, register design of + * STM32_I2C_CR2 in i2c-stm32f0.c), we hold the STOP bits for + * reading request larger than 255 bytes. + */ + do { + bytes_to_read = read_count - complete_bytes; + if (bytes_to_read > MAX_BYTES_IN_ONE_READING) + bytes_to_read = MAX_BYTES_IN_ONE_READING; + else + xfer_flag |= I2C_XFER_STOP; + rv = i2c_xfer( + port, slave_addr, + complete_bytes ? NULL : payload + offset, + complete_bytes ? 0 : write_count, + payload + complete_bytes, + bytes_to_read, xfer_flag); + complete_bytes += bytes_to_read; + xfer_flag = 0; + } while (complete_bytes < read_count && !rv); + config->buffer[0] = usb_i2c_map_error(rv); } usb_i2c_write_packet(config, read_count + 4); } |