summaryrefslogtreecommitdiff
path: root/src/device
diff options
context:
space:
mode:
Diffstat (limited to 'src/device')
-rw-r--r--src/device/Makefile.inc1
-rw-r--r--src/device/i2c.c83
-rw-r--r--src/device/i2c_bus.c161
3 files changed, 162 insertions, 83 deletions
diff --git a/src/device/Makefile.inc b/src/device/Makefile.inc
index 8e6bf5be60..a1d592051b 100644
--- a/src/device/Makefile.inc
+++ b/src/device/Makefile.inc
@@ -37,3 +37,4 @@ bootblock-y += i2c.c
verstage-y += i2c.c
romstage-y += i2c.c
ramstage-y += i2c.c
+ramstage-y += i2c_bus.c
diff --git a/src/device/i2c.c b/src/device/i2c.c
index 6fdd5cff26..aa695caf03 100644
--- a/src/device/i2c.c
+++ b/src/device/i2c.c
@@ -13,92 +13,9 @@
* GNU General Public License for more details.
*/
-#include <device/device.h>
-#include <device/i2c_bus.h>
#include <device/i2c_simple.h>
#include <stdint.h>
-#if ENV_RAMSTAGE
-/* Return I2C operations for a bus */
-static inline const struct i2c_bus_operations *ops_i2c_bus(struct bus *bus)
-{
- if (bus && bus->dev && bus->dev->ops)
- return bus->dev->ops->ops_i2c_bus;
- return NULL;
-}
-
-int i2c_dev_find_bus(struct device *dev)
-{
- const struct i2c_bus_operations *ops;
- struct bus *pbus;
-
- if (!dev)
- return -1;
-
- /* Locate parent bus with I2C controller ops */
- pbus = dev->bus;
- while (pbus && pbus->dev && !ops_i2c_bus(pbus))
- if (pbus->dev->bus != pbus)
- pbus = pbus->dev->bus;
-
- /* Check if this I2C controller ops implements dev_to_bus() */
- ops = ops_i2c_bus(pbus);
- if (!ops || !ops->dev_to_bus)
- return -1;
-
- /* Use controller ops to determine the bus number */
- return ops->dev_to_bus(pbus->dev);
-}
-
-int i2c_dev_transfer(struct device *dev, struct i2c_msg *segments, int count)
-{
- int bus = i2c_dev_find_bus(dev);
- if (bus < 0)
- return -1;
- return i2c_transfer(bus, segments, count);
-}
-
-int i2c_dev_readb(struct device *dev, uint8_t reg, uint8_t *data)
-{
- int bus = i2c_dev_find_bus(dev);
- if (bus < 0)
- return -1;
- return i2c_readb(bus, dev->path.i2c.device, reg, data);
-}
-
-int i2c_dev_writeb(struct device *dev, uint8_t reg, uint8_t data)
-{
- int bus = i2c_dev_find_bus(dev);
- if (bus < 0)
- return -1;
- return i2c_writeb(bus, dev->path.i2c.device, reg, data);
-}
-
-int i2c_dev_read_bytes(struct device *dev, uint8_t reg, uint8_t *data, int len)
-{
- int bus = i2c_dev_find_bus(dev);
- if (bus < 0)
- return -1;
- return i2c_read_bytes(bus, dev->path.i2c.device, reg, data, len);
-}
-
-int i2c_dev_read_raw(struct device *dev, uint8_t *data, int len)
-{
- int bus = i2c_dev_find_bus(dev);
- if (bus < 0)
- return -1;
- return i2c_read_raw(bus, dev->path.i2c.device, data, len);
-}
-
-int i2c_dev_write_raw(struct device *dev, uint8_t *data, int len)
-{
- int bus = i2c_dev_find_bus(dev);
- if (bus < 0)
- return -1;
- return i2c_write_raw(bus, dev->path.i2c.device, data, len);
-}
-#endif
-
int i2c_read_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t *data,
uint8_t mask, uint8_t shift)
{
diff --git a/src/device/i2c_bus.c b/src/device/i2c_bus.c
new file mode 100644
index 0000000000..1c543efaba
--- /dev/null
+++ b/src/device/i2c_bus.c
@@ -0,0 +1,161 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * 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.
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <console/console.h>
+#include <device/smbus.h>
+#include <device/i2c_bus.h>
+
+struct bus *i2c_link(struct device *const dev)
+{
+ if (!dev || !dev->bus)
+ return NULL;
+
+ struct bus *link = dev->bus;
+ while (link) {
+ struct device *const parent = link->dev;
+
+ if (parent && parent->ops &&
+ (parent->ops->ops_i2c_bus || parent->ops->ops_smbus_bus))
+ break;
+
+ if (parent && parent->bus)
+ link = parent->bus;
+ else
+ link = NULL;
+ }
+
+ if (!link) {
+ printk(BIOS_ALERT, "%s Cannot find I2C or SMBus bus operations",
+ dev_path(dev));
+ }
+
+ return link;
+}
+
+int i2c_readb(struct device *const dev)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ uint8_t val;
+ const struct i2c_msg msg = {
+ .flags = I2C_M_RD,
+ .slave = dev->path.i2c.device,
+ .buf = &val,
+ .len = sizeof(val),
+ };
+
+ const int ret = busdev->ops->ops_i2c_bus->
+ transfer(busdev, &msg, 1);
+ if (ret)
+ return ret;
+ else
+ return val;
+ } else if (busdev->ops->ops_smbus_bus->recv_byte) {
+ return busdev->ops->ops_smbus_bus->recv_byte(dev);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->recv_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}
+
+int i2c_writeb(struct device *const dev, uint8_t val)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ const struct i2c_msg msg = {
+ .flags = 0,
+ .slave = dev->path.i2c.device,
+ .buf = &val,
+ .len = sizeof(val),
+ };
+ return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1);
+ } else if (busdev->ops->ops_smbus_bus->send_byte) {
+ return busdev->ops->ops_smbus_bus->send_byte(dev, val);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->send_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}
+
+int i2c_readb_at(struct device *const dev, uint8_t off)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ uint8_t val;
+ const struct i2c_msg msg[] = {
+ {
+ .flags = 0,
+ .slave = dev->path.i2c.device,
+ .buf = &off,
+ .len = sizeof(off),
+ },
+ {
+ .flags = I2C_M_RD,
+ .slave = dev->path.i2c.device,
+ .buf = &val,
+ .len = sizeof(val),
+ },
+ };
+
+ const int ret = busdev->ops->ops_i2c_bus->
+ transfer(busdev, msg, ARRAY_SIZE(msg));
+ if (ret)
+ return ret;
+ else
+ return val;
+ } else if (busdev->ops->ops_smbus_bus->read_byte) {
+ return busdev->ops->ops_smbus_bus->read_byte(dev, off);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->read_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}
+
+int i2c_writeb_at(struct device *const dev,
+ const uint8_t off, const uint8_t val)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ uint8_t buf[] = { off, val };
+ const struct i2c_msg msg = {
+ .flags = 0,
+ .slave = dev->path.i2c.device,
+ .buf = buf,
+ .len = sizeof(buf),
+ };
+ return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1);
+ } else if (busdev->ops->ops_smbus_bus->write_byte) {
+ return busdev->ops->ops_smbus_bus->write_byte(dev, off, val);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->write_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}