aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/device/Makefile.inc5
-rw-r--r--src/device/i2c.c46
-rw-r--r--src/include/device/i2c.h5
3 files changed, 56 insertions, 0 deletions
diff --git a/src/device/Makefile.inc b/src/device/Makefile.inc
index c2ade9675f..6bad0cde83 100644
--- a/src/device/Makefile.inc
+++ b/src/device/Makefile.inc
@@ -31,3 +31,8 @@ bootblock-$(CONFIG_SOFTWARE_I2C) += software_i2c.c
verstage-$(CONFIG_SOFTWARE_I2C) += software_i2c.c
romstage-$(CONFIG_SOFTWARE_I2C) += software_i2c.c
ramstage-$(CONFIG_SOFTWARE_I2C) += software_i2c.c
+
+bootblock-y += i2c.c
+verstage-y += i2c.c
+romstage-y += i2c.c
+ramstage-y += i2c.c
diff --git a/src/device/i2c.c b/src/device/i2c.c
new file mode 100644
index 0000000000..53bfdf0c94
--- /dev/null
+++ b/src/device/i2c.c
@@ -0,0 +1,46 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2016 MediaTek 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.
+ */
+
+#include <device/i2c.h>
+
+int i2c_read_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t *data,
+ uint8_t mask, uint8_t shift)
+{
+ int ret;
+ uint8_t buf = 0;
+
+ ret = i2c_readb(bus, chip, reg, &buf);
+
+ buf &= (mask << shift);
+ *data = (buf >> shift);
+
+ return ret;
+}
+
+int i2c_write_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t data,
+ uint8_t mask, uint8_t shift)
+{
+ int ret;
+ uint8_t buf = 0;
+
+ ret = i2c_readb(bus, chip, reg, &buf);
+
+ buf &= ~(mask << shift);
+ buf |= (data << shift);
+
+ ret |= i2c_writeb(bus, chip, reg, buf);
+
+ return ret;
+}
diff --git a/src/include/device/i2c.h b/src/include/device/i2c.h
index 8e59ba7c21..b152bb98c2 100644
--- a/src/include/device/i2c.h
+++ b/src/include/device/i2c.h
@@ -45,6 +45,11 @@ void software_i2c_wedge_ack(unsigned bus, u8 chip);
void software_i2c_wedge_read(unsigned bus, u8 chip, u8 reg, int bit_count);
void software_i2c_wedge_write(unsigned bus, u8 chip, u8 reg, int bit_count);
+int i2c_read_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t *data,
+ uint8_t mask, uint8_t shift);
+int i2c_write_field(unsigned bus, uint8_t chip, 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.