From 0f2dd1eff9930e30dddd9aabceb5d85ee3b4e980 Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Tue, 1 Aug 2017 14:02:40 +0200 Subject: include/device: Split i2c.h into three Split `i2c.h` into three pieces to ease reuse of the generic defi- nitions. No code is changed. * `i2c.h` - keeps the generic definitions * `i2c_simple.h` - holds the current, limited to one controller driver per board, devicetree independent I2C interface * `i2c_bus.h` - will become the devicetree compatible interface for native I2C (e.g. non-SMBus) controllers Change-Id: I382d45c70f9314588663e1284f264f877469c74d Signed-off-by: Nico Huber Reviewed-on: https://review.coreboot.org/20845 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin --- src/include/device/i2c.h | 159 ---------------------------------------- src/include/device/i2c_bus.h | 44 +++++++++++ src/include/device/i2c_simple.h | 157 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 159 deletions(-) create mode 100644 src/include/device/i2c_bus.h create mode 100644 src/include/device/i2c_simple.h (limited to 'src/include') diff --git a/src/include/device/i2c.h b/src/include/device/i2c.h index 9939bc60bf..d6ee15aa5a 100644 --- a/src/include/device/i2c.h +++ b/src/include/device/i2c.h @@ -17,7 +17,6 @@ #define _DEVICE_I2C_H_ #include -#include /** * struct i2c_msg - an I2C transaction segment beginning with START @@ -67,162 +66,4 @@ enum i2c_address_mode { I2C_MODE_10_BIT }; - -int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments, - int count); - -#define SOFTWARE_I2C_MAX_BUS 10 /* increase as necessary */ - -struct software_i2c_ops { - void (*set_sda)(unsigned int bus, int high); - void (*set_scl)(unsigned int bus, int high); - int (*get_sda)(unsigned int bus); - int (*get_scl)(unsigned int bus); -}; - -extern struct software_i2c_ops *software_i2c[]; - -int software_i2c_transfer(unsigned int bus, struct i2c_msg *segments, - int count); -void software_i2c_wedge_ack(unsigned int bus, u8 slave); -void software_i2c_wedge_read(unsigned int bus, u8 slave, u8 reg, int bit_count); -void software_i2c_wedge_write(unsigned int bus, u8 slave, u8 reg, - int bit_count); - -int i2c_read_field(unsigned int bus, uint8_t slave, uint8_t reg, uint8_t *data, - uint8_t mask, uint8_t shift); -int i2c_write_field(unsigned int bus, uint8_t slave, uint8_t reg, uint8_t data, - uint8_t mask, uint8_t shift); - -/* - * software_i2c is supposed to be a debug feature. It's usually not compiled in, - * but when it is it can be dynamically enabled at runtime for certain busses. - * Need this ugly stub to arbitrate since I2C device drivers hardcode - * 'i2c_transfer()' as their entry point. - */ -static inline int i2c_transfer(unsigned int bus, struct i2c_msg *segments, - int count) -{ - if (CONFIG_SOFTWARE_I2C) - if (bus < SOFTWARE_I2C_MAX_BUS && software_i2c[bus]) - return software_i2c_transfer(bus, segments, count); - - return platform_i2c_transfer(bus, segments, count); -} - -/* - * Read a raw chunk of data in one segment and one frame. - * - * [start][slave addr][r][data][stop] - */ -static inline int i2c_read_raw(unsigned int bus, uint8_t slave, uint8_t *data, - int len) -{ - struct i2c_msg seg = { - .flags = I2C_M_RD, .slave = slave, .buf = data, .len = len - }; - - return i2c_transfer(bus, &seg, 1); -} - -/* - * Write a raw chunk of data in one segment and one frame. - * - * [start][slave addr][w][data][stop] - */ -static inline int i2c_write_raw(unsigned int bus, uint8_t slave, uint8_t *data, - int len) -{ - struct i2c_msg seg = { - .flags = 0, .slave = slave, .buf = data, .len = len - }; - - return i2c_transfer(bus, &seg, 1); -} - -/** - * Read multi-bytes with two segments in one frame - * - * [start][slave addr][w][register addr][start][slave addr][r][data...][stop] - */ -static inline int i2c_read_bytes(unsigned int bus, uint8_t slave, uint8_t reg, - uint8_t *data, int len) -{ - struct i2c_msg seg[2]; - - seg[0].flags = 0; - seg[0].slave = slave; - seg[0].buf = ® - seg[0].len = 1; - 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)); -} - -/** - * Read a byte with two segments in one frame - * - * [start][slave addr][w][register addr][start][slave addr][r][data][stop] - */ -static inline int i2c_readb(unsigned int bus, uint8_t slave, uint8_t reg, - uint8_t *data) -{ - struct i2c_msg seg[2]; - - seg[0].flags = 0; - seg[0].slave = slave; - seg[0].buf = ® - seg[0].len = 1; - seg[1].flags = I2C_M_RD; - seg[1].slave = slave; - seg[1].buf = data; - seg[1].len = 1; - - return i2c_transfer(bus, seg, ARRAY_SIZE(seg)); -} - -/** - * Write a byte with one segment in one frame. - * - * [start][slave addr][w][register addr][data][stop] - */ -static inline int i2c_writeb(unsigned int bus, uint8_t slave, uint8_t reg, - uint8_t data) -{ - struct i2c_msg seg; - uint8_t buf[] = {reg, data}; - - seg.flags = 0; - seg.slave = slave; - seg.buf = buf; - seg.len = ARRAY_SIZE(buf); - - return i2c_transfer(bus, &seg, 1); -} - -/* I2C bus operation for ramstage drivers */ -struct device; -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); -}; - -/* Return I2C bus number for provided device, -1 if not found */ -int i2c_dev_find_bus(struct device *dev); - -/* 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); - #endif /* _DEVICE_I2C_H_ */ diff --git a/src/include/device/i2c_bus.h b/src/include/device/i2c_bus.h new file mode 100644 index 0000000000..474d9e5b2c --- /dev/null +++ b/src/include/device/i2c_bus.h @@ -0,0 +1,44 @@ +/* + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _DEVICE_I2C_BUS_H_ +#define _DEVICE_I2C_BUS_H_ + +#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); +}; + +/* Return I2C bus number for provided device, -1 if not found */ +int i2c_dev_find_bus(struct device *dev); + +/* 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); + +#endif /* _DEVICE_I2C_BUS_H_ */ diff --git a/src/include/device/i2c_simple.h b/src/include/device/i2c_simple.h new file mode 100644 index 0000000000..dcde601994 --- /dev/null +++ b/src/include/device/i2c_simple.h @@ -0,0 +1,157 @@ +/* + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _DEVICE_I2C_SIMPLE_H_ +#define _DEVICE_I2C_SIMPLE_H_ + +#include +#include + +int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments, + int count); + +#define SOFTWARE_I2C_MAX_BUS 10 /* increase as necessary */ + +struct software_i2c_ops { + void (*set_sda)(unsigned int bus, int high); + void (*set_scl)(unsigned int bus, int high); + int (*get_sda)(unsigned int bus); + int (*get_scl)(unsigned int bus); +}; + +extern struct software_i2c_ops *software_i2c[]; + +int software_i2c_transfer(unsigned int bus, struct i2c_msg *segments, + int count); +void software_i2c_wedge_ack(unsigned int bus, u8 slave); +void software_i2c_wedge_read(unsigned int bus, u8 slave, u8 reg, int bit_count); +void software_i2c_wedge_write(unsigned int bus, u8 slave, u8 reg, + int bit_count); + +int i2c_read_field(unsigned int bus, uint8_t slave, uint8_t reg, uint8_t *data, + uint8_t mask, uint8_t shift); +int i2c_write_field(unsigned int bus, uint8_t slave, uint8_t reg, uint8_t data, + uint8_t mask, uint8_t shift); + +/* + * software_i2c is supposed to be a debug feature. It's usually not compiled in, + * but when it is it can be dynamically enabled at runtime for certain busses. + * Need this ugly stub to arbitrate since I2C device drivers hardcode + * 'i2c_transfer()' as their entry point. + */ +static inline int i2c_transfer(unsigned int bus, struct i2c_msg *segments, + int count) +{ + if (CONFIG_SOFTWARE_I2C) + if (bus < SOFTWARE_I2C_MAX_BUS && software_i2c[bus]) + return software_i2c_transfer(bus, segments, count); + + return platform_i2c_transfer(bus, segments, count); +} + +/* + * Read a raw chunk of data in one segment and one frame. + * + * [start][slave addr][r][data][stop] + */ +static inline int i2c_read_raw(unsigned int bus, uint8_t slave, uint8_t *data, + int len) +{ + struct i2c_msg seg = { + .flags = I2C_M_RD, .slave = slave, .buf = data, .len = len + }; + + return i2c_transfer(bus, &seg, 1); +} + +/* + * Write a raw chunk of data in one segment and one frame. + * + * [start][slave addr][w][data][stop] + */ +static inline int i2c_write_raw(unsigned int bus, uint8_t slave, uint8_t *data, + int len) +{ + struct i2c_msg seg = { + .flags = 0, .slave = slave, .buf = data, .len = len + }; + + return i2c_transfer(bus, &seg, 1); +} + +/** + * Read multi-bytes with two segments in one frame + * + * [start][slave addr][w][register addr][start][slave addr][r][data...][stop] + */ +static inline int i2c_read_bytes(unsigned int bus, uint8_t slave, uint8_t reg, + uint8_t *data, int len) +{ + struct i2c_msg seg[2]; + + seg[0].flags = 0; + seg[0].slave = slave; + seg[0].buf = ® + seg[0].len = 1; + 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)); +} + +/** + * Read a byte with two segments in one frame + * + * [start][slave addr][w][register addr][start][slave addr][r][data][stop] + */ +static inline int i2c_readb(unsigned int bus, uint8_t slave, uint8_t reg, + uint8_t *data) +{ + struct i2c_msg seg[2]; + + seg[0].flags = 0; + seg[0].slave = slave; + seg[0].buf = ® + seg[0].len = 1; + seg[1].flags = I2C_M_RD; + seg[1].slave = slave; + seg[1].buf = data; + seg[1].len = 1; + + return i2c_transfer(bus, seg, ARRAY_SIZE(seg)); +} + +/** + * Write a byte with one segment in one frame. + * + * [start][slave addr][w][register addr][data][stop] + */ +static inline int i2c_writeb(unsigned int bus, uint8_t slave, uint8_t reg, + uint8_t data) +{ + struct i2c_msg seg; + uint8_t buf[] = {reg, data}; + + seg.flags = 0; + seg.slave = slave; + seg.buf = buf; + seg.len = ARRAY_SIZE(buf); + + return i2c_transfer(bus, &seg, 1); +} + +#endif /* _DEVICE_I2C_SIMPLE_H_ */ -- cgit v1.2.3