From 8fbf88fd8cbcd76274a2f418f08a80fc4d93fe13 Mon Sep 17 00:00:00 2001 From: Felix Held Date: Tue, 8 Mar 2022 17:48:59 +0100 Subject: include/device/i2c_simple: add i2c_2ba_read_bytes function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To read data from an I2C EEPROM that uses 2 byte offsets or any other I2C device that uses 2 byte offsets, first the two offset bytes are sent to the device and then the data bytes are read from it. The main difference to the existing i2c_read_bytes is that that function will only send one offset byte to the I2C device. TEST=Reading the contents of an EEPROM on the AMD Chausie board works Signed-off-by: Felix Held Change-Id: I224e434bb2654aabef6302c1525112e44c4b21fa Reviewed-on: https://review.coreboot.org/c/coreboot/+/63791 Tested-by: build bot (Jenkins) Reviewed-by: Fred Reitberger Reviewed-by: Kyösti Mälkki --- src/include/device/i2c_simple.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src') diff --git a/src/include/device/i2c_simple.h b/src/include/device/i2c_simple.h index 8f389b3107..9f3af1130e 100644 --- a/src/include/device/i2c_simple.h +++ b/src/include/device/i2c_simple.h @@ -142,4 +142,32 @@ static inline int i2c_writeb(unsigned int bus, uint8_t slave, uint8_t reg, return i2c_transfer(bus, &seg, 1); } +/** + * Read multi-bytes from an I2C device with two bytes register address/offset + * with two segments in one frame + * + * [start][slave addr][w][register high addr][register low addr] + * [start][slave addr][r][data...][stop] + */ +static inline int i2c_2ba_read_bytes(unsigned int bus, uint8_t slave, uint16_t offset, + uint8_t *data, int len) +{ + struct i2c_msg seg[2]; + uint8_t eeprom_offset[2]; + + eeprom_offset[0] = offset >> 8; + eeprom_offset[1] = offset & 0xff; + + seg[0].flags = 0; + seg[0].slave = slave; + seg[0].buf = eeprom_offset; + seg[0].len = sizeof(eeprom_offset); + seg[1].flags = I2C_M_RD; + seg[1].slave = slave; + seg[1].buf = data; + seg[1].len = len; + + return i2c_transfer(bus, seg, ARRAY_SIZE(seg)); +} + #endif /* _DEVICE_I2C_SIMPLE_H_ */ -- cgit v1.2.3