From 581738642fbeacdf97fc737a41b3128d72cf1a1c Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Tue, 1 Aug 2017 17:09:35 +0200 Subject: Reinvent I2C ops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not use the global platform_i2c_transfer() function that can only be implemented by a single driver. Instead, make a `struct device` aware transfer() function the only interface function for I2C controller dri- vers to implement. To not force the slave device drivers to be implemented either above generic I2C or specialized SMBus operations, we support SMBus control- lers in the slave device interface too. We start with four simple slave functions: i2c_readb(), i2c_writeb(), i2c_readb_at() and i2c_writeb_at(). They are all compatible to respec- tive SMBus functions. But we keep aliases because it would be weird to force e.g. an I2C EEPROM driver to call smbus_read_byte(). Change-Id: I98386f91bf4799ba3df84ec8bc0f64edd4142818 Signed-off-by: Nico Huber Reviewed-on: https://review.coreboot.org/20846 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin Reviewed-by: Kyösti Mälkki --- src/include/device/i2c_bus.h | 82 +++++++++++++++++++++++++++++++++--------- src/include/device/smbus_def.h | 10 +++--- src/include/types.h | 9 ++++- 3 files changed, 79 insertions(+), 22 deletions(-) (limited to 'src/include') diff --git a/src/include/device/i2c_bus.h b/src/include/device/i2c_bus.h index 474d9e5b2c..f1416a7411 100644 --- a/src/include/device/i2c_bus.h +++ b/src/include/device/i2c_bus.h @@ -1,8 +1,6 @@ /* * This file is part of the coreboot project. * - * Copyright (C) 2014 Google, Inc. - * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. @@ -16,29 +14,79 @@ #ifndef _DEVICE_I2C_BUS_H_ #define _DEVICE_I2C_BUS_H_ +#include #include #include #include /* I2C bus operation for ramstage drivers */ struct i2c_bus_operations { - /* - * This is an SOC specific method that can be provided to translate the - * 'struct device' for an I2C controller into a unique I2C bus number. - * Returns -1 if the bus number for this device cannot be determined. - */ - int (*dev_to_bus)(struct device *dev); + int (*transfer)(struct device *, const struct i2c_msg *, size_t count); }; -/* Return I2C bus number for provided device, -1 if not found */ -int i2c_dev_find_bus(struct device *dev); +/* + * Returns the first upstream facing link whose bus implements either + * `i2c_bus_operations` *or* `smbus_bus_operations`. + * + * If not NULL, guarantees that `->dev`, `->dev->ops` and either + * `->dev->ops->ops_i2c_bus` or `->dev->ops->ops_smbus_bus` are + * not NULL. + */ +struct bus *i2c_link(struct device *); + +/* + * Shorthand for `i2c_link(dev)->dev`. + * + * Returns NULL if i2c_link(dev) returns NULL. + */ +static inline struct device *i2c_busdev(struct device *dev) +{ + struct bus *const link = i2c_link(dev); + return link ? link->dev : NULL; +} + +/* + * Slave driver interface functions. These will look for the next + * `i2c_bus_operations` *or* `smbus_bus_operations` and perform the + * respective transfers. + * + * The interface is limited to what current slave drivers demand. + * Extend as required. + * + * All functions return a negative `enum cb_err` value on error. + * Either CB_ERR, CB_ERR_ARG or any CB_I2C_* (cf. include/types.h). + */ + +/* + * Reads one byte. + * Compatible to smbus_recv_byte(). + * + * Returns the read byte on success, negative `enum cb_err` value on error. + */ +int i2c_readb(struct device *); + +/* + * Writes the byte `val`. + * Compatible to smbus_send_byte(). + * + * Returns 0 on success, negative `enum cb_err` value on error. + */ +int i2c_writeb(struct device *, uint8_t val); -/* Variants of I2C helper functions that take a device instead of bus number */ -int i2c_dev_transfer(struct device *dev, struct i2c_msg *segments, int count); -int i2c_dev_readb(struct device *dev, uint8_t reg, uint8_t *data); -int i2c_dev_writeb(struct device *dev, uint8_t reg, uint8_t data); -int i2c_dev_read_bytes(struct device *dev, uint8_t reg, uint8_t *data, int len); -int i2c_dev_read_raw(struct device *dev, uint8_t *data, int len); -int i2c_dev_write_raw(struct device *dev, uint8_t *data, int len); +/* + * Sends the register offset `off` and reads one byte. + * Compatible to smbus_read_byte(). + * + * Returns the read byte on success, negative `enum cb_err` value on error. + */ +int i2c_readb_at(struct device *, uint8_t off); + +/* + * Sends the register offset `off` followed by the byte `val`. + * Compatible to smbus_write_byte(). + * + * Returns 0 on success, negative `enum cb_err` value on error. + */ +int i2c_writeb_at(struct device *, uint8_t off, uint8_t val); #endif /* _DEVICE_I2C_BUS_H_ */ diff --git a/src/include/device/smbus_def.h b/src/include/device/smbus_def.h index 0b07400b37..61d786107b 100644 --- a/src/include/device/smbus_def.h +++ b/src/include/device/smbus_def.h @@ -1,10 +1,12 @@ #ifndef DEVICE_SMBUS_DEF_H #define DEVICE_SMBUS_DEF_H +#include + /* Error results are negative success is >= 0 */ -#define SMBUS_ERROR -1 -#define SMBUS_WAIT_UNTIL_READY_TIMEOUT -2 -#define SMBUS_WAIT_UNTIL_DONE_TIMEOUT -3 -#define SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT -4 +#define SMBUS_ERROR CB_ERR +#define SMBUS_WAIT_UNTIL_READY_TIMEOUT CB_I2C_BUSY +#define SMBUS_WAIT_UNTIL_DONE_TIMEOUT CB_I2C_TIMEOUT +#define SMBUS_WAIT_UNTIL_ACTIVE_TIMEOUT CB_I2C_NO_DEVICE #endif /* DEVICE_SMBUS_DEF_H */ diff --git a/src/include/types.h b/src/include/types.h index df0947370f..7307cd5072 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -47,7 +47,14 @@ enum cb_err { /* Keyboard test failures */ CB_KBD_CONTROLLER_FAILURE = -200, - CB_KBD_INTERFACE_FAILURE = -201 + CB_KBD_INTERFACE_FAILURE = -201, + + /* I2C controller failures */ + CB_I2C_NO_DEVICE = -300, /**< Device is not responding */ + CB_I2C_BUSY = -301, /**< Device tells it's busy */ + CB_I2C_PROTOCOL_ERROR = -302, /**< Data lost or spurious slave + device response, try again? */ + CB_I2C_TIMEOUT = -303, /**< Transmission timed out */ }; #endif /* __TYPES_H */ -- cgit v1.2.3